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 212c3b9e3e1SChristian Borntraeger /* 213cc726886SClaudio Imbrenda * allow asynchronous deinit for protected guests; enable by default since 214cc726886SClaudio Imbrenda * the feature is opt-in anyway 215cc726886SClaudio Imbrenda */ 216cc726886SClaudio Imbrenda static int async_destroy = 1; 217cc726886SClaudio Imbrenda module_param(async_destroy, int, 0444); 218cc726886SClaudio Imbrenda MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests"); 219fb491d55SClaudio Imbrenda 220c3b9e3e1SChristian Borntraeger /* 221c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 222c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 223c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 224c3b9e3e1SChristian Borntraeger */ 225c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 226c3b9e3e1SChristian Borntraeger 227c3b9e3e1SChristian Borntraeger /* 228c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 229c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 230c3b9e3e1SChristian Borntraeger */ 231c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 232c3b9e3e1SChristian Borntraeger /* 233c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 234c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 235c3b9e3e1SChristian Borntraeger */ 236c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 237c3b9e3e1SChristian Borntraeger 238c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 23978c4b59fSMichael Mueller { 240c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 241c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 242c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 24317e89e13SSven Schnelle sizeof(stfle_fac_list)); 244c3b9e3e1SChristian Borntraeger 245c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 24678c4b59fSMichael Mueller } 24778c4b59fSMichael Mueller 24815c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 24915c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2500a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2510a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 25215c9705fSDavid Hildenbrand 2539d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 254a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 25578f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2563e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv; 2579d8d5786SMichael Mueller 258b0c632dbSHeiko Carstens /* Section: not file related */ 25913a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 260b0c632dbSHeiko Carstens { 261b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 26210474ae8SAlexander Graf return 0; 263b0c632dbSHeiko Carstens } 264b0c632dbSHeiko Carstens 265b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque) 266f257d6dcSSean Christopherson { 267f257d6dcSSean Christopherson return 0; 268f257d6dcSSean Christopherson } 269f257d6dcSSean Christopherson 27029b40f10SJanosch Frank /* forward declarations */ 271414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 272414d3b07SMartin Schwidefsky unsigned long end); 27329b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm); 2742c70fe44SChristian Borntraeger 2751575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2761575767eSDavid Hildenbrand { 2771575767eSDavid Hildenbrand u8 delta_idx = 0; 2781575767eSDavid Hildenbrand 2791575767eSDavid Hildenbrand /* 2801575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2811575767eSDavid Hildenbrand * -delta to the epoch. 2821575767eSDavid Hildenbrand */ 2831575767eSDavid Hildenbrand delta = -delta; 2841575767eSDavid Hildenbrand 2851575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2861575767eSDavid Hildenbrand if ((s64)delta < 0) 2871575767eSDavid Hildenbrand delta_idx = -1; 2881575767eSDavid Hildenbrand 2891575767eSDavid Hildenbrand scb->epoch += delta; 2901575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2911575767eSDavid Hildenbrand scb->epdx += delta_idx; 2921575767eSDavid Hildenbrand if (scb->epoch < delta) 2931575767eSDavid Hildenbrand scb->epdx += 1; 2941575767eSDavid Hildenbrand } 2951575767eSDavid Hildenbrand } 2961575767eSDavid Hildenbrand 297fdf03650SFan Zhang /* 298fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 299fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 300fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 301fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 302fdf03650SFan Zhang */ 303fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 304fdf03650SFan Zhang void *v) 305fdf03650SFan Zhang { 306fdf03650SFan Zhang struct kvm *kvm; 307fdf03650SFan Zhang struct kvm_vcpu *vcpu; 30846808a4cSMarc Zyngier unsigned long i; 309fdf03650SFan Zhang unsigned long long *delta = v; 310fdf03650SFan Zhang 311fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 312fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 3131575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 3141575767eSDavid Hildenbrand if (i == 0) { 3151575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 3161575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 3171575767eSDavid Hildenbrand } 318db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 319db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 32091473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 3211575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 3221575767eSDavid Hildenbrand *delta); 323fdf03650SFan Zhang } 324fdf03650SFan Zhang } 325fdf03650SFan Zhang return NOTIFY_OK; 326fdf03650SFan Zhang } 327fdf03650SFan Zhang 328fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 329fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 330fdf03650SFan Zhang }; 331fdf03650SFan Zhang 332b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque) 333b0c632dbSHeiko Carstens { 3342c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 335b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 336a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 337a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 338fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 339fdf03650SFan Zhang &kvm_clock_notifier); 340b0c632dbSHeiko Carstens return 0; 341b0c632dbSHeiko Carstens } 342b0c632dbSHeiko Carstens 343b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 344b0c632dbSHeiko Carstens { 345b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 346a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 347fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 348fdf03650SFan Zhang &kvm_clock_notifier); 349b0c632dbSHeiko Carstens } 350b0c632dbSHeiko Carstens 35122be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 35222be5a13SDavid Hildenbrand { 35322be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 35422be5a13SDavid Hildenbrand } 35522be5a13SDavid Hildenbrand 3560a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3570a763c78SDavid Hildenbrand { 3584fa3b91bSHeiko Carstens unsigned long function = (unsigned long)nr | 0x100; 359d051ae53SHeiko Carstens int cc; 3600a763c78SDavid Hildenbrand 3610a763c78SDavid Hildenbrand asm volatile( 3624fa3b91bSHeiko Carstens " lgr 0,%[function]\n" 3630a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3640a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3650a763c78SDavid Hildenbrand " ipm %0\n" 3660a763c78SDavid Hildenbrand " srl %0,28\n" 3670a763c78SDavid Hildenbrand : "=d" (cc) 3684fa3b91bSHeiko Carstens : [function] "d" (function) 3694fa3b91bSHeiko Carstens : "cc", "0"); 3700a763c78SDavid Hildenbrand return cc == 0; 3710a763c78SDavid Hildenbrand } 3720a763c78SDavid Hildenbrand 373d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 374d6681397SChristian Borntraeger { 375d6681397SChristian Borntraeger asm volatile( 3764fa3b91bSHeiko Carstens " lghi 0,0\n" 3774fa3b91bSHeiko Carstens " lgr 1,%[query]\n" 3784fa3b91bSHeiko Carstens /* Parameter registers are ignored */ 379d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 380b1c41ac3SHeiko Carstens : 3814fa3b91bSHeiko Carstens : [query] "d" ((unsigned long)query), [opc] "i" (opcode) 3824fa3b91bSHeiko Carstens : "cc", "memory", "0", "1"); 383d6681397SChristian Borntraeger } 384d6681397SChristian Borntraeger 385173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3864f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 387173aec2dSChristian Borntraeger 38822be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 38922be5a13SDavid Hildenbrand { 3900a763c78SDavid Hildenbrand int i; 3910a763c78SDavid Hildenbrand 3920a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3930a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3940a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3950a763c78SDavid Hildenbrand } 3960a763c78SDavid Hildenbrand 3970a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 398221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 399221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 400221bb8a4SLinus Torvalds PTFF_QAF); 4010a763c78SDavid Hildenbrand 4020a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 40369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 40469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 40569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 40669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 40769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 40869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 40969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 41069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 41169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 41269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 4130a763c78SDavid Hildenbrand } 4140a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 41569c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 41669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 4170a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 41869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 41969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 42069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 42169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 42269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 42369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 42469c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 42569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 4260a763c78SDavid Hildenbrand } 4270a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 428985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 42969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 4300a763c78SDavid Hildenbrand 431e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 432e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 433e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 434e000b8e0SJason J. Herne 43513209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 43613209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 43713209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 43813209ad0SChristian Borntraeger 439173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 440173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 441173aec2dSChristian Borntraeger 4424f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4434f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4444f45b90eSChristian Borntraeger 44522be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 44622be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 447a3508fbeSDavid Hildenbrand /* 448a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 449a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 450a3508fbeSDavid Hildenbrand */ 451a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 452a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 453a3508fbeSDavid Hildenbrand return; 454a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 45519c439b5SDavid Hildenbrand if (sclp.has_64bscao) 45619c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4570615a326SDavid Hildenbrand if (sclp.has_siif) 4580615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 45977d18f6dSDavid Hildenbrand if (sclp.has_gpere) 46077d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 461a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 462a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4635630a8e8SDavid Hildenbrand if (sclp.has_ib) 4645630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 46513ee3f67SDavid Hildenbrand if (sclp.has_cei) 46613ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4677fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4687fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 469730cd632SFarhan Ali if (sclp.has_kss) 470730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4715d3876a8SDavid Hildenbrand /* 4725d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4735d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4745d3876a8SDavid Hildenbrand * instead of the real storage key. 4755d3876a8SDavid Hildenbrand * 4765d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4775d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4785d3876a8SDavid Hildenbrand * 4795d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4805d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4815d3876a8SDavid Hildenbrand * 4825d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4835d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4845d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4855d3876a8SDavid Hildenbrand * 4865d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4875d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4885d3876a8SDavid Hildenbrand */ 48922be5a13SDavid Hildenbrand } 49022be5a13SDavid Hildenbrand 491b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 492b0c632dbSHeiko Carstens { 493f76f6371SJanosch Frank int rc = -ENOMEM; 494308c3e66SMichael Mueller 49578f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 49678f26131SChristian Borntraeger if (!kvm_s390_dbf) 49778f26131SChristian Borntraeger return -ENOMEM; 49878f26131SChristian Borntraeger 4993e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); 5003e6c5568SJanosch Frank if (!kvm_s390_dbf_uv) 5013e6c5568SJanosch Frank goto out; 5023e6c5568SJanosch Frank 5033e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || 5043e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) 505f76f6371SJanosch Frank goto out; 50678f26131SChristian Borntraeger 50722be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 50822be5a13SDavid Hildenbrand 50984877d93SCornelia Huck /* Register floating interrupt controller interface. */ 510308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 511308c3e66SMichael Mueller if (rc) { 5128d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 513f76f6371SJanosch Frank goto out; 514308c3e66SMichael Mueller } 515b1d1e76eSMichael Mueller 516189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 51798b1d33dSMatthew Rosato rc = kvm_s390_pci_init(); 51898b1d33dSMatthew Rosato if (rc) { 51998b1d33dSMatthew Rosato pr_err("Unable to allocate AIFT for PCI\n"); 52098b1d33dSMatthew Rosato goto out; 52198b1d33dSMatthew Rosato } 52298b1d33dSMatthew Rosato } 52398b1d33dSMatthew Rosato 524b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 525b1d1e76eSMichael Mueller if (rc) 526f76f6371SJanosch Frank goto out; 527b1d1e76eSMichael Mueller 528308c3e66SMichael Mueller return 0; 529308c3e66SMichael Mueller 530f76f6371SJanosch Frank out: 531f76f6371SJanosch Frank kvm_arch_exit(); 532308c3e66SMichael Mueller return rc; 533b0c632dbSHeiko Carstens } 534b0c632dbSHeiko Carstens 53578f26131SChristian Borntraeger void kvm_arch_exit(void) 53678f26131SChristian Borntraeger { 5371282c21eSMichael Mueller kvm_s390_gib_destroy(); 538189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 53998b1d33dSMatthew Rosato kvm_s390_pci_exit(); 54078f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 5413e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv); 54278f26131SChristian Borntraeger } 54378f26131SChristian Borntraeger 544b0c632dbSHeiko Carstens /* Section: device related */ 545b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 546b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 547b0c632dbSHeiko Carstens { 548b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 549b0c632dbSHeiko Carstens return s390_enable_sie(); 550b0c632dbSHeiko Carstens return -EINVAL; 551b0c632dbSHeiko Carstens } 552b0c632dbSHeiko Carstens 553784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 554b0c632dbSHeiko Carstens { 555d7b0b5ebSCarsten Otte int r; 556d7b0b5ebSCarsten Otte 5572bd0ac4eSCarsten Otte switch (ext) { 558d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 559b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 56052e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5611efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5621efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5631efd0f59SCarsten Otte #endif 5643c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 56560b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 56614eebd91SCarsten Otte case KVM_CAP_ONE_REG: 567d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 568fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 56910ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 570c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 57178599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 572f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5736352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 574460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 57547b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5762444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 577e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 57830ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 579816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5806502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5814036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 58247a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 583da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 5847de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS: 585b9b2782cSPeter Xu case KVM_CAP_SET_GUEST_DEBUG: 58623a60f83SCollin Walling case KVM_CAP_S390_DIAG318: 587d004079eSJanis Schoetterl-Glausch case KVM_CAP_S390_MEM_OP_EXTENSION: 588d7b0b5ebSCarsten Otte r = 1; 589d7b0b5ebSCarsten Otte break; 590a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 591a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK; 592a43b80b7SMaxim Levitsky break; 593a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 594a4499382SJanosch Frank r = 0; 59540ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 596a4499382SJanosch Frank r = 1; 597a4499382SJanosch Frank break; 59841408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 59941408c28SThomas Huth r = MEM_OP_MAX_SIZE; 60041408c28SThomas Huth break; 601e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 602e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 603a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 60476a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 605a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 606a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 607a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 60876a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 60982cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS) 61082cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r); 611e726b1bdSChristian Borntraeger break; 6121526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 613abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 6141526bf9cSChristian Borntraeger break; 61568c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 61668c55750SEric Farman r = MACHINE_HAS_VX; 61768c55750SEric Farman break; 618c6e5f166SFan Zhang case KVM_CAP_S390_RI: 619c6e5f166SFan Zhang r = test_facility(64); 620c6e5f166SFan Zhang break; 6214e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 6224e0b1ab7SFan Zhang r = test_facility(133); 6234e0b1ab7SFan Zhang break; 62435b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 62535b3fde6SChristian Borntraeger r = test_facility(82); 62635b3fde6SChristian Borntraeger break; 6278c516b25SClaudio Imbrenda case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE: 6288c516b25SClaudio Imbrenda r = async_destroy && is_prot_virt_host(); 6298c516b25SClaudio Imbrenda break; 63013da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED: 63113da9ae1SChristian Borntraeger r = is_prot_virt_host(); 63213da9ae1SChristian Borntraeger break; 633e9bf3acbSJanosch Frank case KVM_CAP_S390_PROTECTED_DUMP: { 634e9bf3acbSJanosch Frank u64 pv_cmds_dump[] = { 635e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_INIT, 636e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE, 637e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CPU, 638e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_COMPLETE, 639e9bf3acbSJanosch Frank }; 640e9bf3acbSJanosch Frank int i; 641e9bf3acbSJanosch Frank 642e9bf3acbSJanosch Frank r = is_prot_virt_host(); 643e9bf3acbSJanosch Frank 644e9bf3acbSJanosch Frank for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) { 645e9bf3acbSJanosch Frank if (!test_bit_inv(pv_cmds_dump[i], 646e9bf3acbSJanosch Frank (unsigned long *)&uv_info.inst_calls_list)) { 647e9bf3acbSJanosch Frank r = 0; 648e9bf3acbSJanosch Frank break; 649e9bf3acbSJanosch Frank } 650e9bf3acbSJanosch Frank } 651e9bf3acbSJanosch Frank break; 652e9bf3acbSJanosch Frank } 653db1c875eSMatthew Rosato case KVM_CAP_S390_ZPCI_OP: 654db1c875eSMatthew Rosato r = kvm_s390_pci_interp_allowed(); 655db1c875eSMatthew Rosato break; 656f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 657f5ecfee9SPierre Morel r = test_facility(11); 658f5ecfee9SPierre Morel break; 6592bd0ac4eSCarsten Otte default: 660d7b0b5ebSCarsten Otte r = 0; 661b0c632dbSHeiko Carstens } 662d7b0b5ebSCarsten Otte return r; 6632bd0ac4eSCarsten Otte } 664b0c632dbSHeiko Carstens 6650dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 66615f36ebdSJason J. Herne { 6670959e168SJanosch Frank int i; 66815f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 6690959e168SJanosch Frank unsigned long gaddr, vmaddr; 67015f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 6710959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 67215f36ebdSJason J. Herne 6730959e168SJanosch Frank /* Loop over all guest segments */ 6740959e168SJanosch Frank cur_gfn = memslot->base_gfn; 67515f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 6760959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 6770959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 6780959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 6790959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 6800959e168SJanosch Frank continue; 68115f36ebdSJason J. Herne 6820959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6830959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6840959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6850959e168SJanosch Frank if (test_bit(i, bitmap)) 6860959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6870959e168SJanosch Frank } 6880959e168SJanosch Frank 6891763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6901763f8d0SChristian Borntraeger return; 69170c88a00SChristian Borntraeger cond_resched(); 69215f36ebdSJason J. Herne } 69315f36ebdSJason J. Herne } 69415f36ebdSJason J. Herne 695b0c632dbSHeiko Carstens /* Section: vm related */ 696a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 697a6e2f683SEugene (jno) Dvurechenski 698b0c632dbSHeiko Carstens /* 699b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 700b0c632dbSHeiko Carstens */ 701b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 702b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 703b0c632dbSHeiko Carstens { 70415f36ebdSJason J. Herne int r; 70515f36ebdSJason J. Herne unsigned long n; 70615f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 7072a49f61dSSean Christopherson int is_dirty; 70815f36ebdSJason J. Herne 709e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 710e1e8a962SJanosch Frank return -EINVAL; 711e1e8a962SJanosch Frank 71215f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 71315f36ebdSJason J. Herne 71415f36ebdSJason J. Herne r = -EINVAL; 71515f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 71615f36ebdSJason J. Herne goto out; 71715f36ebdSJason J. Herne 7182a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); 71915f36ebdSJason J. Herne if (r) 72015f36ebdSJason J. Herne goto out; 72115f36ebdSJason J. Herne 72215f36ebdSJason J. Herne /* Clear the dirty log */ 72315f36ebdSJason J. Herne if (is_dirty) { 72415f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 72515f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 72615f36ebdSJason J. Herne } 72715f36ebdSJason J. Herne r = 0; 72815f36ebdSJason J. Herne out: 72915f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 73015f36ebdSJason J. Herne return r; 731b0c632dbSHeiko Carstens } 732b0c632dbSHeiko Carstens 7336502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 7346502a34cSDavid Hildenbrand { 73546808a4cSMarc Zyngier unsigned long i; 7366502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 7376502a34cSDavid Hildenbrand 7386502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 7396502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 7406502a34cSDavid Hildenbrand } 7416502a34cSDavid Hildenbrand } 7426502a34cSDavid Hildenbrand 743e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 744d938dc55SCornelia Huck { 745d938dc55SCornelia Huck int r; 746d938dc55SCornelia Huck 747d938dc55SCornelia Huck if (cap->flags) 748d938dc55SCornelia Huck return -EINVAL; 749d938dc55SCornelia Huck 750d938dc55SCornelia Huck switch (cap->cap) { 75184223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 752c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 75384223598SCornelia Huck kvm->arch.use_irqchip = 1; 75484223598SCornelia Huck r = 0; 75584223598SCornelia Huck break; 7562444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 757c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 7582444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 7592444b352SDavid Hildenbrand r = 0; 7602444b352SDavid Hildenbrand break; 76168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 7625967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 763a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 7645967c17bSDavid Hildenbrand r = -EBUSY; 7655967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 766c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 767c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 7682f87d942SGuenther Hutzl if (test_facility(134)) { 7692f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 7702f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 7712f87d942SGuenther Hutzl } 77253743aa7SMaxim Samoylov if (test_facility(135)) { 77353743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 77453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 77553743aa7SMaxim Samoylov } 7767832e91cSChristian Borntraeger if (test_facility(148)) { 7777832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7787832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7797832e91cSChristian Borntraeger } 780d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 781d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 782d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 783d5cb6ab1SChristian Borntraeger } 7841f703d2cSChristian Borntraeger if (test_facility(192)) { 7851f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192); 7861f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192); 7871f703d2cSChristian Borntraeger } 78818280d8bSMichael Mueller r = 0; 78918280d8bSMichael Mueller } else 79018280d8bSMichael Mueller r = -EINVAL; 7915967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 792c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 793c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 79468c55750SEric Farman break; 795c6e5f166SFan Zhang case KVM_CAP_S390_RI: 796c6e5f166SFan Zhang r = -EINVAL; 797c6e5f166SFan Zhang mutex_lock(&kvm->lock); 798a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 799c6e5f166SFan Zhang r = -EBUSY; 800c6e5f166SFan Zhang } else if (test_facility(64)) { 801c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 802c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 803c6e5f166SFan Zhang r = 0; 804c6e5f166SFan Zhang } 805c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 806c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 807c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 808c6e5f166SFan Zhang break; 80947a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 81047a4693eSYi Min Zhao mutex_lock(&kvm->lock); 81147a4693eSYi Min Zhao if (kvm->created_vcpus) { 81247a4693eSYi Min Zhao r = -EBUSY; 81347a4693eSYi Min Zhao } else { 81447a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 81547a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 81647a4693eSYi Min Zhao r = 0; 81747a4693eSYi Min Zhao } 81847a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 81947a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 82047a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 82147a4693eSYi Min Zhao break; 8224e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 8234e0b1ab7SFan Zhang r = -EINVAL; 8244e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 825241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 8264e0b1ab7SFan Zhang r = -EBUSY; 8274e0b1ab7SFan Zhang } else if (test_facility(133)) { 8284e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 8294e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 8304e0b1ab7SFan Zhang r = 0; 8314e0b1ab7SFan Zhang } 8324e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 8334e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 8344e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 8354e0b1ab7SFan Zhang break; 836a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 837a4499382SJanosch Frank mutex_lock(&kvm->lock); 838a4499382SJanosch Frank if (kvm->created_vcpus) 839a4499382SJanosch Frank r = -EBUSY; 84040ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 841a4499382SJanosch Frank r = -EINVAL; 842a4499382SJanosch Frank else { 843a4499382SJanosch Frank r = 0; 844d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 845a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 846d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 847a4499382SJanosch Frank /* 848a4499382SJanosch Frank * We might have to create fake 4k page 849a4499382SJanosch Frank * tables. To avoid that the hardware works on 850a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 851a4499382SJanosch Frank */ 852a4499382SJanosch Frank kvm->arch.use_skf = 0; 853a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 854a4499382SJanosch Frank } 855a4499382SJanosch Frank mutex_unlock(&kvm->lock); 856a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 857a4499382SJanosch Frank r ? "(not available)" : "(success)"); 858a4499382SJanosch Frank break; 859e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 860c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 861e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 862e44fc8c9SEkaterina Tumanova r = 0; 863e44fc8c9SEkaterina Tumanova break; 8646502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 8656502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 8666502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 8676502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 8686502a34cSDavid Hildenbrand r = 0; 8696502a34cSDavid Hildenbrand break; 870f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 871f5ecfee9SPierre Morel r = -EINVAL; 872f5ecfee9SPierre Morel mutex_lock(&kvm->lock); 873f5ecfee9SPierre Morel if (kvm->created_vcpus) { 874f5ecfee9SPierre Morel r = -EBUSY; 875f5ecfee9SPierre Morel } else if (test_facility(11)) { 876f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 11); 877f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_list, 11); 878f5ecfee9SPierre Morel r = 0; 879f5ecfee9SPierre Morel } 880f5ecfee9SPierre Morel mutex_unlock(&kvm->lock); 881f5ecfee9SPierre Morel VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s", 882f5ecfee9SPierre Morel r ? "(not available)" : "(success)"); 883f5ecfee9SPierre Morel break; 884d938dc55SCornelia Huck default: 885d938dc55SCornelia Huck r = -EINVAL; 886d938dc55SCornelia Huck break; 887d938dc55SCornelia Huck } 888d938dc55SCornelia Huck return r; 889d938dc55SCornelia Huck } 890d938dc55SCornelia Huck 8918c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8928c0a7ce6SDominik Dingel { 8938c0a7ce6SDominik Dingel int ret; 8948c0a7ce6SDominik Dingel 8958c0a7ce6SDominik Dingel switch (attr->attr) { 8968c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8978c0a7ce6SDominik Dingel ret = 0; 898c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 899a3a92c31SDominik Dingel kvm->arch.mem_limit); 900a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 9018c0a7ce6SDominik Dingel ret = -EFAULT; 9028c0a7ce6SDominik Dingel break; 9038c0a7ce6SDominik Dingel default: 9048c0a7ce6SDominik Dingel ret = -ENXIO; 9058c0a7ce6SDominik Dingel break; 9068c0a7ce6SDominik Dingel } 9078c0a7ce6SDominik Dingel return ret; 9088c0a7ce6SDominik Dingel } 9098c0a7ce6SDominik Dingel 9108c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 9114f718eabSDominik Dingel { 9124f718eabSDominik Dingel int ret; 9134f718eabSDominik Dingel unsigned int idx; 9144f718eabSDominik Dingel switch (attr->attr) { 9154f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 916f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 917c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 918e6db1d61SDominik Dingel break; 919e6db1d61SDominik Dingel 920c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 9214f718eabSDominik Dingel mutex_lock(&kvm->lock); 922a4499382SJanosch Frank if (kvm->created_vcpus) 923a4499382SJanosch Frank ret = -EBUSY; 924a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 925a4499382SJanosch Frank ret = -EINVAL; 926a4499382SJanosch Frank else { 9274f718eabSDominik Dingel kvm->arch.use_cmma = 1; 928c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 929c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 9304f718eabSDominik Dingel ret = 0; 9314f718eabSDominik Dingel } 9324f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9334f718eabSDominik Dingel break; 9344f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 935f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 936f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 937f9cbd9b0SDavid Hildenbrand break; 938c3489155SDominik Dingel ret = -EINVAL; 939c3489155SDominik Dingel if (!kvm->arch.use_cmma) 940c3489155SDominik Dingel break; 941c3489155SDominik Dingel 942c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 9434f718eabSDominik Dingel mutex_lock(&kvm->lock); 9444f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 945a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 9464f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 9474f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9484f718eabSDominik Dingel ret = 0; 9494f718eabSDominik Dingel break; 9508c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 9518c0a7ce6SDominik Dingel unsigned long new_limit; 9528c0a7ce6SDominik Dingel 9538c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 9548c0a7ce6SDominik Dingel return -EINVAL; 9558c0a7ce6SDominik Dingel 9568c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 9578c0a7ce6SDominik Dingel return -EFAULT; 9588c0a7ce6SDominik Dingel 959a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 960a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 9618c0a7ce6SDominik Dingel return -E2BIG; 9628c0a7ce6SDominik Dingel 963a3a92c31SDominik Dingel if (!new_limit) 964a3a92c31SDominik Dingel return -EINVAL; 965a3a92c31SDominik Dingel 9666ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 967a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 968a3a92c31SDominik Dingel new_limit -= 1; 969a3a92c31SDominik Dingel 9708c0a7ce6SDominik Dingel ret = -EBUSY; 9718c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 972a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 9736ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 9746ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 9758c0a7ce6SDominik Dingel 9768c0a7ce6SDominik Dingel if (!new) { 9778c0a7ce6SDominik Dingel ret = -ENOMEM; 9788c0a7ce6SDominik Dingel } else { 9796ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 9808c0a7ce6SDominik Dingel new->private = kvm; 9818c0a7ce6SDominik Dingel kvm->arch.gmap = new; 9828c0a7ce6SDominik Dingel ret = 0; 9838c0a7ce6SDominik Dingel } 9848c0a7ce6SDominik Dingel } 9858c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 986a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 987a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 988a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 9898c0a7ce6SDominik Dingel break; 9908c0a7ce6SDominik Dingel } 9914f718eabSDominik Dingel default: 9924f718eabSDominik Dingel ret = -ENXIO; 9934f718eabSDominik Dingel break; 9944f718eabSDominik Dingel } 9954f718eabSDominik Dingel return ret; 9964f718eabSDominik Dingel } 9974f718eabSDominik Dingel 998a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 999a374e892STony Krowiak 100020c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 1001a374e892STony Krowiak { 1002a374e892STony Krowiak struct kvm_vcpu *vcpu; 100346808a4cSMarc Zyngier unsigned long i; 1004a374e892STony Krowiak 100520c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 100620c922f0STony Krowiak 10073194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 100820c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 10093194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 10103194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 10113194cdb7SDavid Hildenbrand } 101220c922f0STony Krowiak 101320c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 101420c922f0STony Krowiak } 101520c922f0STony Krowiak 101620c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 101720c922f0STony Krowiak { 1018a374e892STony Krowiak mutex_lock(&kvm->lock); 1019a374e892STony Krowiak switch (attr->attr) { 1020a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 10218e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10228e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 102337940fb0STony Krowiak return -EINVAL; 10248e41bd54SChristian Borntraeger } 1025a374e892STony Krowiak get_random_bytes( 1026a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1027a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1028a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 1029c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 1030a374e892STony Krowiak break; 1031a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 10328e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10338e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 103437940fb0STony Krowiak return -EINVAL; 10358e41bd54SChristian Borntraeger } 1036a374e892STony Krowiak get_random_bytes( 1037a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1038a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1039a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 1040c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 1041a374e892STony Krowiak break; 1042a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 10438e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10448e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 104537940fb0STony Krowiak return -EINVAL; 10468e41bd54SChristian Borntraeger } 1047a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 1048a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 1049a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1050c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 1051a374e892STony Krowiak break; 1052a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 10538e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10548e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 105537940fb0STony Krowiak return -EINVAL; 10568e41bd54SChristian Borntraeger } 1057a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 1058a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 1059a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1060c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 1061a374e892STony Krowiak break; 106237940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 106337940fb0STony Krowiak if (!ap_instructions_available()) { 106437940fb0STony Krowiak mutex_unlock(&kvm->lock); 106537940fb0STony Krowiak return -EOPNOTSUPP; 106637940fb0STony Krowiak } 106737940fb0STony Krowiak kvm->arch.crypto.apie = 1; 106837940fb0STony Krowiak break; 106937940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 107037940fb0STony Krowiak if (!ap_instructions_available()) { 107137940fb0STony Krowiak mutex_unlock(&kvm->lock); 107237940fb0STony Krowiak return -EOPNOTSUPP; 107337940fb0STony Krowiak } 107437940fb0STony Krowiak kvm->arch.crypto.apie = 0; 107537940fb0STony Krowiak break; 1076a374e892STony Krowiak default: 1077a374e892STony Krowiak mutex_unlock(&kvm->lock); 1078a374e892STony Krowiak return -ENXIO; 1079a374e892STony Krowiak } 1080a374e892STony Krowiak 108120c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 1082a374e892STony Krowiak mutex_unlock(&kvm->lock); 1083a374e892STony Krowiak return 0; 1084a374e892STony Krowiak } 1085a374e892STony Krowiak 10863f4bbb43SMatthew Rosato static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu) 10873f4bbb43SMatthew Rosato { 10883f4bbb43SMatthew Rosato /* Only set the ECB bits after guest requests zPCI interpretation */ 10893f4bbb43SMatthew Rosato if (!vcpu->kvm->arch.use_zpci_interp) 10903f4bbb43SMatthew Rosato return; 10913f4bbb43SMatthew Rosato 10923f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI; 10933f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI; 10943f4bbb43SMatthew Rosato } 10953f4bbb43SMatthew Rosato 10963f4bbb43SMatthew Rosato void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm) 10973f4bbb43SMatthew Rosato { 10983f4bbb43SMatthew Rosato struct kvm_vcpu *vcpu; 10993f4bbb43SMatthew Rosato unsigned long i; 11003f4bbb43SMatthew Rosato 11013f4bbb43SMatthew Rosato lockdep_assert_held(&kvm->lock); 11023f4bbb43SMatthew Rosato 11033f4bbb43SMatthew Rosato if (!kvm_s390_pci_interp_allowed()) 11043f4bbb43SMatthew Rosato return; 11053f4bbb43SMatthew Rosato 11063f4bbb43SMatthew Rosato /* 11073f4bbb43SMatthew Rosato * If host is configured for PCI and the necessary facilities are 11083f4bbb43SMatthew Rosato * available, turn on interpretation for the life of this guest 11093f4bbb43SMatthew Rosato */ 11103f4bbb43SMatthew Rosato kvm->arch.use_zpci_interp = 1; 11113f4bbb43SMatthew Rosato 11123f4bbb43SMatthew Rosato kvm_s390_vcpu_block_all(kvm); 11133f4bbb43SMatthew Rosato 11143f4bbb43SMatthew Rosato kvm_for_each_vcpu(i, vcpu, kvm) { 11153f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 11163f4bbb43SMatthew Rosato kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 11173f4bbb43SMatthew Rosato } 11183f4bbb43SMatthew Rosato 11193f4bbb43SMatthew Rosato kvm_s390_vcpu_unblock_all(kvm); 11203f4bbb43SMatthew Rosato } 11213f4bbb43SMatthew Rosato 1122190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 1123190df4a2SClaudio Imbrenda { 112446808a4cSMarc Zyngier unsigned long cx; 1125190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 1126190df4a2SClaudio Imbrenda 1127190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1128190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1129190df4a2SClaudio Imbrenda } 1130190df4a2SClaudio Imbrenda 1131190df4a2SClaudio Imbrenda /* 1132190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 11331de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1134190df4a2SClaudio Imbrenda */ 1135190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1136190df4a2SClaudio Imbrenda { 1137190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1138190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1139afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1140a54d8066SMaciej S. Szmigiero int bkt; 1141190df4a2SClaudio Imbrenda 1142190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1143afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1144190df4a2SClaudio Imbrenda return 0; 1145190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1146a54d8066SMaciej S. Szmigiero if (!slots || kvm_memslots_empty(slots)) 1147190df4a2SClaudio Imbrenda return -EINVAL; 1148190df4a2SClaudio Imbrenda 1149afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1150afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1151afdad616SClaudio Imbrenda return 0; 1152190df4a2SClaudio Imbrenda } 1153190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1154a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(ms, bkt, slots) { 115513a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 115613a17cc0SIgor Mammedov return -EINVAL; 1157afdad616SClaudio Imbrenda /* 1158afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1159afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1160afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1161afdad616SClaudio Imbrenda * attributes. 1162afdad616SClaudio Imbrenda */ 1163afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1164afdad616SClaudio Imbrenda ram_pages += ms->npages; 1165190df4a2SClaudio Imbrenda } 1166afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1167afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1168190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1169190df4a2SClaudio Imbrenda return 0; 1170190df4a2SClaudio Imbrenda } 1171190df4a2SClaudio Imbrenda 1172190df4a2SClaudio Imbrenda /* 11731de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1174190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1175190df4a2SClaudio Imbrenda */ 1176190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1177190df4a2SClaudio Imbrenda { 1178190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1179afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1180190df4a2SClaudio Imbrenda return 0; 1181afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1182afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1183190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1184190df4a2SClaudio Imbrenda return 0; 1185190df4a2SClaudio Imbrenda } 1186190df4a2SClaudio Imbrenda 1187190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1188190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1189190df4a2SClaudio Imbrenda { 11901de1ea7eSChristian Borntraeger int res = -ENXIO; 1191190df4a2SClaudio Imbrenda 11921de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1193190df4a2SClaudio Imbrenda switch (attr->attr) { 1194190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1195190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1196190df4a2SClaudio Imbrenda break; 1197190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1198190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1199190df4a2SClaudio Imbrenda break; 1200190df4a2SClaudio Imbrenda default: 1201190df4a2SClaudio Imbrenda break; 1202190df4a2SClaudio Imbrenda } 12031de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1204190df4a2SClaudio Imbrenda 1205190df4a2SClaudio Imbrenda return res; 1206190df4a2SClaudio Imbrenda } 1207190df4a2SClaudio Imbrenda 1208190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1209190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1210190df4a2SClaudio Imbrenda { 1211afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1212190df4a2SClaudio Imbrenda 1213190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1214190df4a2SClaudio Imbrenda return -ENXIO; 1215190df4a2SClaudio Imbrenda 1216190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1217190df4a2SClaudio Imbrenda return -EFAULT; 1218190df4a2SClaudio Imbrenda return 0; 1219190df4a2SClaudio Imbrenda } 1220190df4a2SClaudio Imbrenda 12216973091dSNico Boehr static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod); 12226973091dSNico Boehr 12238fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 12248fa1696eSCollin L. Walling { 12258fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 12268fa1696eSCollin L. Walling 12278fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 12288fa1696eSCollin L. Walling return -EFAULT; 12298fa1696eSCollin L. Walling 12300e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 12318fa1696eSCollin L. Walling return -EINVAL; 12326973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od); 12338fa1696eSCollin L. Walling 12348fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 12358fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 12368fa1696eSCollin L. Walling 12378fa1696eSCollin L. Walling return 0; 12388fa1696eSCollin L. Walling } 12398fa1696eSCollin L. Walling 124072f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 124172f25020SJason J. Herne { 124272f25020SJason J. Herne u8 gtod_high; 124372f25020SJason J. Herne 124472f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 124572f25020SJason J. Herne sizeof(gtod_high))) 124672f25020SJason J. Herne return -EFAULT; 124772f25020SJason J. Herne 124872f25020SJason J. Herne if (gtod_high != 0) 124972f25020SJason J. Herne return -EINVAL; 125058c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 125172f25020SJason J. Herne 125272f25020SJason J. Herne return 0; 125372f25020SJason J. Herne } 125472f25020SJason J. Herne 125572f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 125672f25020SJason J. Herne { 12570e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 125872f25020SJason J. Herne 12590e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 12600e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 126172f25020SJason J. Herne return -EFAULT; 126272f25020SJason J. Herne 12636973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od); 12640e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 126572f25020SJason J. Herne return 0; 126672f25020SJason J. Herne } 126772f25020SJason J. Herne 126872f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 126972f25020SJason J. Herne { 127072f25020SJason J. Herne int ret; 127172f25020SJason J. Herne 127272f25020SJason J. Herne if (attr->flags) 127372f25020SJason J. Herne return -EINVAL; 127472f25020SJason J. Herne 12756973091dSNico Boehr mutex_lock(&kvm->lock); 12766973091dSNico Boehr /* 12776973091dSNico Boehr * For protected guests, the TOD is managed by the ultravisor, so trying 12786973091dSNico Boehr * to change it will never bring the expected results. 12796973091dSNico Boehr */ 12806973091dSNico Boehr if (kvm_s390_pv_is_protected(kvm)) { 12816973091dSNico Boehr ret = -EOPNOTSUPP; 12826973091dSNico Boehr goto out_unlock; 12836973091dSNico Boehr } 12846973091dSNico Boehr 128572f25020SJason J. Herne switch (attr->attr) { 12868fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12878fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 12888fa1696eSCollin L. Walling break; 128972f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 129072f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 129172f25020SJason J. Herne break; 129272f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 129372f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 129472f25020SJason J. Herne break; 129572f25020SJason J. Herne default: 129672f25020SJason J. Herne ret = -ENXIO; 129772f25020SJason J. Herne break; 129872f25020SJason J. Herne } 12996973091dSNico Boehr 13006973091dSNico Boehr out_unlock: 13016973091dSNico Boehr mutex_unlock(&kvm->lock); 130272f25020SJason J. Herne return ret; 130372f25020SJason J. Herne } 130472f25020SJason J. Herne 130533d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 13068fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 13078fa1696eSCollin L. Walling { 13082cfd7b73SHeiko Carstens union tod_clock clk; 13098fa1696eSCollin L. Walling 13108fa1696eSCollin L. Walling preempt_disable(); 13118fa1696eSCollin L. Walling 13122cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 13138fa1696eSCollin L. Walling 13142cfd7b73SHeiko Carstens gtod->tod = clk.tod + kvm->arch.epoch; 131533d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 131633d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 13172cfd7b73SHeiko Carstens gtod->epoch_idx = clk.ei + kvm->arch.epdx; 13182cfd7b73SHeiko Carstens if (gtod->tod < clk.tod) 13198fa1696eSCollin L. Walling gtod->epoch_idx += 1; 132033d1b272SDavid Hildenbrand } 13218fa1696eSCollin L. Walling 13228fa1696eSCollin L. Walling preempt_enable(); 13238fa1696eSCollin L. Walling } 13248fa1696eSCollin L. Walling 13258fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 13268fa1696eSCollin L. Walling { 13278fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 13288fa1696eSCollin L. Walling 13298fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 133033d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 13318fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 13328fa1696eSCollin L. Walling return -EFAULT; 13338fa1696eSCollin L. Walling 13348fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 13358fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 13368fa1696eSCollin L. Walling return 0; 13378fa1696eSCollin L. Walling } 13388fa1696eSCollin L. Walling 133972f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 134072f25020SJason J. Herne { 134172f25020SJason J. Herne u8 gtod_high = 0; 134272f25020SJason J. Herne 134372f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 134472f25020SJason J. Herne sizeof(gtod_high))) 134572f25020SJason J. Herne return -EFAULT; 134658c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 134772f25020SJason J. Herne 134872f25020SJason J. Herne return 0; 134972f25020SJason J. Herne } 135072f25020SJason J. Herne 135172f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 135272f25020SJason J. Herne { 13535a3d883aSDavid Hildenbrand u64 gtod; 135472f25020SJason J. Herne 135560417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 135672f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 135772f25020SJason J. Herne return -EFAULT; 135858c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 135972f25020SJason J. Herne 136072f25020SJason J. Herne return 0; 136172f25020SJason J. Herne } 136272f25020SJason J. Herne 136372f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 136472f25020SJason J. Herne { 136572f25020SJason J. Herne int ret; 136672f25020SJason J. Herne 136772f25020SJason J. Herne if (attr->flags) 136872f25020SJason J. Herne return -EINVAL; 136972f25020SJason J. Herne 137072f25020SJason J. Herne switch (attr->attr) { 13718fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 13728fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 13738fa1696eSCollin L. Walling break; 137472f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 137572f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 137672f25020SJason J. Herne break; 137772f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 137872f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 137972f25020SJason J. Herne break; 138072f25020SJason J. Herne default: 138172f25020SJason J. Herne ret = -ENXIO; 138272f25020SJason J. Herne break; 138372f25020SJason J. Herne } 138472f25020SJason J. Herne return ret; 138572f25020SJason J. Herne } 138672f25020SJason J. Herne 1387658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1388658b6edaSMichael Mueller { 1389658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1390053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1391658b6edaSMichael Mueller int ret = 0; 1392658b6edaSMichael Mueller 1393658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1394a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1395658b6edaSMichael Mueller ret = -EBUSY; 1396658b6edaSMichael Mueller goto out; 1397658b6edaSMichael Mueller } 1398c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1399658b6edaSMichael Mueller if (!proc) { 1400658b6edaSMichael Mueller ret = -ENOMEM; 1401658b6edaSMichael Mueller goto out; 1402658b6edaSMichael Mueller } 1403658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1404658b6edaSMichael Mueller sizeof(*proc))) { 14059bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1406053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1407053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 14080487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1409053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1410053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1411053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1412053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1413053dd230SDavid Hildenbrand else 1414658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1415053dd230SDavid Hildenbrand } 1416c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1417658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1418a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1419a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1420a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1421a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1422a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1423a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1424a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1425658b6edaSMichael Mueller } else 1426658b6edaSMichael Mueller ret = -EFAULT; 1427658b6edaSMichael Mueller kfree(proc); 1428658b6edaSMichael Mueller out: 1429658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1430658b6edaSMichael Mueller return ret; 1431658b6edaSMichael Mueller } 1432658b6edaSMichael Mueller 143315c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 143415c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 143515c9705fSDavid Hildenbrand { 143615c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 143715c9705fSDavid Hildenbrand 143815c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 143915c9705fSDavid Hildenbrand return -EFAULT; 144015c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 144115c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 144215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 144315c9705fSDavid Hildenbrand return -EINVAL; 144415c9705fSDavid Hildenbrand 144515c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 14462f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 14472f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 14482f8311c9SChristian Borntraeger return -EBUSY; 14492f8311c9SChristian Borntraeger } 1450da0f8e95SYury Norov bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 145115c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 14522f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14532f8311c9SChristian Borntraeger data.feat[0], 14542f8311c9SChristian Borntraeger data.feat[1], 14552f8311c9SChristian Borntraeger data.feat[2]); 14562f8311c9SChristian Borntraeger return 0; 145715c9705fSDavid Hildenbrand } 145815c9705fSDavid Hildenbrand 14590a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 14600a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 14610a763c78SDavid Hildenbrand { 1462346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1463346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1464346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1465346fa2f8SChristian Borntraeger return -EBUSY; 1466346fa2f8SChristian Borntraeger } 1467346fa2f8SChristian Borntraeger 1468346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1469346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1470346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1471346fa2f8SChristian Borntraeger return -EFAULT; 1472346fa2f8SChristian Borntraeger } 1473346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1474346fa2f8SChristian Borntraeger 147511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 147611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 147711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 147811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 147911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 148011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 148111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 148211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 148311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 148411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 148511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 148611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 148711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 148811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 148911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 149011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 149111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 149211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 149311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 149411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 149511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 149611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 149711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 149811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 149911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 150011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 150111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 150211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 150311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 150411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 150511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 150611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 150711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 150811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 150911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 151011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 151111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 151211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 151311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 151411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 151511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 151611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 151711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 151811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 151913209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 152013209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 152113209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1522173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1523173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1524173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1525173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1526173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15274f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15284f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15294f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15304f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15314f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 153211ba5961SChristian Borntraeger 1533346fa2f8SChristian Borntraeger return 0; 15340a763c78SDavid Hildenbrand } 15350a763c78SDavid Hildenbrand 1536658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1537658b6edaSMichael Mueller { 1538658b6edaSMichael Mueller int ret = -ENXIO; 1539658b6edaSMichael Mueller 1540658b6edaSMichael Mueller switch (attr->attr) { 1541658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1542658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1543658b6edaSMichael Mueller break; 154415c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 154515c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 154615c9705fSDavid Hildenbrand break; 15470a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 15480a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 15490a763c78SDavid Hildenbrand break; 1550658b6edaSMichael Mueller } 1551658b6edaSMichael Mueller return ret; 1552658b6edaSMichael Mueller } 1553658b6edaSMichael Mueller 1554658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1555658b6edaSMichael Mueller { 1556658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1557658b6edaSMichael Mueller int ret = 0; 1558658b6edaSMichael Mueller 1559c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1560658b6edaSMichael Mueller if (!proc) { 1561658b6edaSMichael Mueller ret = -ENOMEM; 1562658b6edaSMichael Mueller goto out; 1563658b6edaSMichael Mueller } 15649bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1565658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1566c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1567c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1568a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1569a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1570a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1571a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1572a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1573a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1574a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1575658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1576658b6edaSMichael Mueller ret = -EFAULT; 1577658b6edaSMichael Mueller kfree(proc); 1578658b6edaSMichael Mueller out: 1579658b6edaSMichael Mueller return ret; 1580658b6edaSMichael Mueller } 1581658b6edaSMichael Mueller 1582658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1583658b6edaSMichael Mueller { 1584658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1585658b6edaSMichael Mueller int ret = 0; 1586658b6edaSMichael Mueller 1587c4196218SChristian Borntraeger mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT); 1588658b6edaSMichael Mueller if (!mach) { 1589658b6edaSMichael Mueller ret = -ENOMEM; 1590658b6edaSMichael Mueller goto out; 1591658b6edaSMichael Mueller } 1592658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 159337c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1594c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1595981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 159617e89e13SSven Schnelle memcpy((unsigned long *)&mach->fac_list, stfle_fac_list, 159717e89e13SSven Schnelle sizeof(stfle_fac_list)); 1598a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1599a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1600a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1601a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1602a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1603a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1604a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1605a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1606a8c39dd7SChristian Borntraeger mach->fac_list[0], 1607a8c39dd7SChristian Borntraeger mach->fac_list[1], 1608a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1609658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1610658b6edaSMichael Mueller ret = -EFAULT; 1611658b6edaSMichael Mueller kfree(mach); 1612658b6edaSMichael Mueller out: 1613658b6edaSMichael Mueller return ret; 1614658b6edaSMichael Mueller } 1615658b6edaSMichael Mueller 161615c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 161715c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 161815c9705fSDavid Hildenbrand { 161915c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 162015c9705fSDavid Hildenbrand 1621da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 162215c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 162315c9705fSDavid Hildenbrand return -EFAULT; 16242f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16252f8311c9SChristian Borntraeger data.feat[0], 16262f8311c9SChristian Borntraeger data.feat[1], 16272f8311c9SChristian Borntraeger data.feat[2]); 162815c9705fSDavid Hildenbrand return 0; 162915c9705fSDavid Hildenbrand } 163015c9705fSDavid Hildenbrand 163115c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 163215c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 163315c9705fSDavid Hildenbrand { 163415c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 163515c9705fSDavid Hildenbrand 1636da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 163715c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 163815c9705fSDavid Hildenbrand return -EFAULT; 16392f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16402f8311c9SChristian Borntraeger data.feat[0], 16412f8311c9SChristian Borntraeger data.feat[1], 16422f8311c9SChristian Borntraeger data.feat[2]); 164315c9705fSDavid Hildenbrand return 0; 164415c9705fSDavid Hildenbrand } 164515c9705fSDavid Hildenbrand 16460a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 16470a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 16480a763c78SDavid Hildenbrand { 1649346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1650346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1651346fa2f8SChristian Borntraeger return -EFAULT; 1652346fa2f8SChristian Borntraeger 165311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 165411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 165511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 165611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 165711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 165811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 165911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 166011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 166111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 166211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 166311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 166411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 166511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 166611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 166711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 166811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 166911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 167011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 167111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 167211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 167311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 167411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 167511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 167611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 167711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 167811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 167911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 168011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 168111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 168211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 168311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 168411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 168511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 168611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 168711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 168811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 168911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 169011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 169111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 169211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 169311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 169411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 169511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 169611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 169713209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 169813209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 169913209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1700173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1701173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1702173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1703173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1704173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 17054f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 17064f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 17074f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 17084f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 17094f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 171011ba5961SChristian Borntraeger 1711346fa2f8SChristian Borntraeger return 0; 17120a763c78SDavid Hildenbrand } 17130a763c78SDavid Hildenbrand 17140a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 17150a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 17160a763c78SDavid Hildenbrand { 17170a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 17180a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 17190a763c78SDavid Hildenbrand return -EFAULT; 172011ba5961SChristian Borntraeger 172111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 172211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 172311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 172411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 172511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 172611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 172711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 172811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 172911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 173011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 173111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 173211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 173311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 173411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 173511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 173611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 173711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 173811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 173911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 174011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 174111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 174211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 174311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 174411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 174511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 174611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 174711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 174811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 174911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 175011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 175111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 175211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 175311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 175411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 175511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 175611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 175711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 175811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 175911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 176011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 176111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 176211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 176311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 176411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 176513209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 176613209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 176713209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1768173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1769173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1770173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1771173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1772173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 17734f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 17744f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 17754f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 17764f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 17774f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 177811ba5961SChristian Borntraeger 17790a763c78SDavid Hildenbrand return 0; 17800a763c78SDavid Hildenbrand } 1781346fa2f8SChristian Borntraeger 1782658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1783658b6edaSMichael Mueller { 1784658b6edaSMichael Mueller int ret = -ENXIO; 1785658b6edaSMichael Mueller 1786658b6edaSMichael Mueller switch (attr->attr) { 1787658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1788658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1789658b6edaSMichael Mueller break; 1790658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1791658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1792658b6edaSMichael Mueller break; 179315c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 179415c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 179515c9705fSDavid Hildenbrand break; 179615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 179715c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 179815c9705fSDavid Hildenbrand break; 17990a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 18000a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 18010a763c78SDavid Hildenbrand break; 18020a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 18030a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 18040a763c78SDavid Hildenbrand break; 1805658b6edaSMichael Mueller } 1806658b6edaSMichael Mueller return ret; 1807658b6edaSMichael Mueller } 1808658b6edaSMichael Mueller 180924fe0195SPierre Morel /** 181024fe0195SPierre Morel * kvm_s390_update_topology_change_report - update CPU topology change report 181124fe0195SPierre Morel * @kvm: guest KVM description 181224fe0195SPierre Morel * @val: set or clear the MTCR bit 181324fe0195SPierre Morel * 181424fe0195SPierre Morel * Updates the Multiprocessor Topology-Change-Report bit to signal 181524fe0195SPierre Morel * the guest with a topology change. 181624fe0195SPierre Morel * This is only relevant if the topology facility is present. 181724fe0195SPierre Morel * 181824fe0195SPierre Morel * The SCA version, bsca or esca, doesn't matter as offset is the same. 181924fe0195SPierre Morel */ 182024fe0195SPierre Morel static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val) 182124fe0195SPierre Morel { 182224fe0195SPierre Morel union sca_utility new, old; 182324fe0195SPierre Morel struct bsca_block *sca; 182424fe0195SPierre Morel 182524fe0195SPierre Morel read_lock(&kvm->arch.sca_lock); 182624fe0195SPierre Morel sca = kvm->arch.sca; 182724fe0195SPierre Morel do { 182824fe0195SPierre Morel old = READ_ONCE(sca->utility); 182924fe0195SPierre Morel new = old; 183024fe0195SPierre Morel new.mtcr = val; 183124fe0195SPierre Morel } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val); 183224fe0195SPierre Morel read_unlock(&kvm->arch.sca_lock); 183324fe0195SPierre Morel } 183424fe0195SPierre Morel 1835f5ecfee9SPierre Morel static int kvm_s390_set_topo_change_indication(struct kvm *kvm, 1836f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1837f5ecfee9SPierre Morel { 1838f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1839f5ecfee9SPierre Morel return -ENXIO; 1840f5ecfee9SPierre Morel 1841f5ecfee9SPierre Morel kvm_s390_update_topology_change_report(kvm, !!attr->attr); 1842f5ecfee9SPierre Morel return 0; 1843f5ecfee9SPierre Morel } 1844f5ecfee9SPierre Morel 1845f5ecfee9SPierre Morel static int kvm_s390_get_topo_change_indication(struct kvm *kvm, 1846f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1847f5ecfee9SPierre Morel { 1848f5ecfee9SPierre Morel u8 topo; 1849f5ecfee9SPierre Morel 1850f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1851f5ecfee9SPierre Morel return -ENXIO; 1852f5ecfee9SPierre Morel 1853f5ecfee9SPierre Morel read_lock(&kvm->arch.sca_lock); 1854f5ecfee9SPierre Morel topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr; 1855f5ecfee9SPierre Morel read_unlock(&kvm->arch.sca_lock); 1856f5ecfee9SPierre Morel 1857f5ecfee9SPierre Morel return put_user(topo, (u8 __user *)attr->addr); 1858f5ecfee9SPierre Morel } 1859f5ecfee9SPierre Morel 1860f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1861f2061656SDominik Dingel { 1862f2061656SDominik Dingel int ret; 1863f2061656SDominik Dingel 1864f2061656SDominik Dingel switch (attr->group) { 18654f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 18668c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 18674f718eabSDominik Dingel break; 186872f25020SJason J. Herne case KVM_S390_VM_TOD: 186972f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 187072f25020SJason J. Herne break; 1871658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1872658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1873658b6edaSMichael Mueller break; 1874a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1875a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1876a374e892STony Krowiak break; 1877190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1878190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1879190df4a2SClaudio Imbrenda break; 1880f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1881f5ecfee9SPierre Morel ret = kvm_s390_set_topo_change_indication(kvm, attr); 1882f5ecfee9SPierre Morel break; 1883f2061656SDominik Dingel default: 1884f2061656SDominik Dingel ret = -ENXIO; 1885f2061656SDominik Dingel break; 1886f2061656SDominik Dingel } 1887f2061656SDominik Dingel 1888f2061656SDominik Dingel return ret; 1889f2061656SDominik Dingel } 1890f2061656SDominik Dingel 1891f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1892f2061656SDominik Dingel { 18938c0a7ce6SDominik Dingel int ret; 18948c0a7ce6SDominik Dingel 18958c0a7ce6SDominik Dingel switch (attr->group) { 18968c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 18978c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 18988c0a7ce6SDominik Dingel break; 189972f25020SJason J. Herne case KVM_S390_VM_TOD: 190072f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 190172f25020SJason J. Herne break; 1902658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1903658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1904658b6edaSMichael Mueller break; 1905190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1906190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1907190df4a2SClaudio Imbrenda break; 1908f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1909f5ecfee9SPierre Morel ret = kvm_s390_get_topo_change_indication(kvm, attr); 1910f5ecfee9SPierre Morel break; 19118c0a7ce6SDominik Dingel default: 19128c0a7ce6SDominik Dingel ret = -ENXIO; 19138c0a7ce6SDominik Dingel break; 19148c0a7ce6SDominik Dingel } 19158c0a7ce6SDominik Dingel 19168c0a7ce6SDominik Dingel return ret; 1917f2061656SDominik Dingel } 1918f2061656SDominik Dingel 1919f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1920f2061656SDominik Dingel { 1921f2061656SDominik Dingel int ret; 1922f2061656SDominik Dingel 1923f2061656SDominik Dingel switch (attr->group) { 19244f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 19254f718eabSDominik Dingel switch (attr->attr) { 19264f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 19274f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1928f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1929f9cbd9b0SDavid Hildenbrand break; 19308c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 19314f718eabSDominik Dingel ret = 0; 19324f718eabSDominik Dingel break; 19334f718eabSDominik Dingel default: 19344f718eabSDominik Dingel ret = -ENXIO; 19354f718eabSDominik Dingel break; 19364f718eabSDominik Dingel } 19374f718eabSDominik Dingel break; 193872f25020SJason J. Herne case KVM_S390_VM_TOD: 193972f25020SJason J. Herne switch (attr->attr) { 194072f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 194172f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 194272f25020SJason J. Herne ret = 0; 194372f25020SJason J. Herne break; 194472f25020SJason J. Herne default: 194572f25020SJason J. Herne ret = -ENXIO; 194672f25020SJason J. Herne break; 194772f25020SJason J. Herne } 194872f25020SJason J. Herne break; 1949658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1950658b6edaSMichael Mueller switch (attr->attr) { 1951658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1952658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 195315c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 195415c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 19550a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1956346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1957658b6edaSMichael Mueller ret = 0; 1958658b6edaSMichael Mueller break; 1959658b6edaSMichael Mueller default: 1960658b6edaSMichael Mueller ret = -ENXIO; 1961658b6edaSMichael Mueller break; 1962658b6edaSMichael Mueller } 1963658b6edaSMichael Mueller break; 1964a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1965a374e892STony Krowiak switch (attr->attr) { 1966a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1967a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1968a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1969a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1970a374e892STony Krowiak ret = 0; 1971a374e892STony Krowiak break; 197237940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 197337940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 197437940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 197537940fb0STony Krowiak break; 1976a374e892STony Krowiak default: 1977a374e892STony Krowiak ret = -ENXIO; 1978a374e892STony Krowiak break; 1979a374e892STony Krowiak } 1980a374e892STony Krowiak break; 1981190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1982190df4a2SClaudio Imbrenda ret = 0; 1983190df4a2SClaudio Imbrenda break; 1984f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1985f5ecfee9SPierre Morel ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO; 1986f5ecfee9SPierre Morel break; 1987f2061656SDominik Dingel default: 1988f2061656SDominik Dingel ret = -ENXIO; 1989f2061656SDominik Dingel break; 1990f2061656SDominik Dingel } 1991f2061656SDominik Dingel 1992f2061656SDominik Dingel return ret; 1993f2061656SDominik Dingel } 1994f2061656SDominik Dingel 199530ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 199630ee2a98SJason J. Herne { 199730ee2a98SJason J. Herne uint8_t *keys; 199830ee2a98SJason J. Herne uint64_t hva; 19994f899147SChristian Borntraeger int srcu_idx, i, r = 0; 200030ee2a98SJason J. Herne 200130ee2a98SJason J. Herne if (args->flags != 0) 200230ee2a98SJason J. Herne return -EINVAL; 200330ee2a98SJason J. Herne 200430ee2a98SJason J. Herne /* Is this guest using storage keys? */ 200555531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 200630ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 200730ee2a98SJason J. Herne 200830ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 200930ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 201030ee2a98SJason J. Herne return -EINVAL; 201130ee2a98SJason J. Herne 2012c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 201330ee2a98SJason J. Herne if (!keys) 201430ee2a98SJason J. Herne return -ENOMEM; 201530ee2a98SJason J. Herne 2016d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 20174f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 201830ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 201930ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 202030ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 202130ee2a98SJason J. Herne r = -EFAULT; 2022d3ed1ceeSMartin Schwidefsky break; 202330ee2a98SJason J. Herne } 202430ee2a98SJason J. Herne 2025154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 2026154c8c19SDavid Hildenbrand if (r) 2027d3ed1ceeSMartin Schwidefsky break; 202830ee2a98SJason J. Herne } 20294f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2030d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 203130ee2a98SJason J. Herne 2032d3ed1ceeSMartin Schwidefsky if (!r) { 203330ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 203430ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 203530ee2a98SJason J. Herne if (r) 203630ee2a98SJason J. Herne r = -EFAULT; 2037d3ed1ceeSMartin Schwidefsky } 2038d3ed1ceeSMartin Schwidefsky 203930ee2a98SJason J. Herne kvfree(keys); 204030ee2a98SJason J. Herne return r; 204130ee2a98SJason J. Herne } 204230ee2a98SJason J. Herne 204330ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 204430ee2a98SJason J. Herne { 204530ee2a98SJason J. Herne uint8_t *keys; 204630ee2a98SJason J. Herne uint64_t hva; 20474f899147SChristian Borntraeger int srcu_idx, i, r = 0; 2048bd096f64SJanosch Frank bool unlocked; 204930ee2a98SJason J. Herne 205030ee2a98SJason J. Herne if (args->flags != 0) 205130ee2a98SJason J. Herne return -EINVAL; 205230ee2a98SJason J. Herne 205330ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 205430ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 205530ee2a98SJason J. Herne return -EINVAL; 205630ee2a98SJason J. Herne 2057c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 205830ee2a98SJason J. Herne if (!keys) 205930ee2a98SJason J. Herne return -ENOMEM; 206030ee2a98SJason J. Herne 206130ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 206230ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 206330ee2a98SJason J. Herne if (r) { 206430ee2a98SJason J. Herne r = -EFAULT; 206530ee2a98SJason J. Herne goto out; 206630ee2a98SJason J. Herne } 206730ee2a98SJason J. Herne 206830ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 206914d4a425SDominik Dingel r = s390_enable_skey(); 207014d4a425SDominik Dingel if (r) 207114d4a425SDominik Dingel goto out; 207230ee2a98SJason J. Herne 2073bd096f64SJanosch Frank i = 0; 2074d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 20754f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 2076bd096f64SJanosch Frank while (i < args->count) { 2077bd096f64SJanosch Frank unlocked = false; 207830ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 207930ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 208030ee2a98SJason J. Herne r = -EFAULT; 2081d3ed1ceeSMartin Schwidefsky break; 208230ee2a98SJason J. Herne } 208330ee2a98SJason J. Herne 208430ee2a98SJason J. Herne /* Lowest order bit is reserved */ 208530ee2a98SJason J. Herne if (keys[i] & 0x01) { 208630ee2a98SJason J. Herne r = -EINVAL; 2087d3ed1ceeSMartin Schwidefsky break; 208830ee2a98SJason J. Herne } 208930ee2a98SJason J. Herne 2090fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 2091bd096f64SJanosch Frank if (r) { 209264019a2eSPeter Xu r = fixup_user_fault(current->mm, hva, 2093bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 209430ee2a98SJason J. Herne if (r) 2095d3ed1ceeSMartin Schwidefsky break; 209630ee2a98SJason J. Herne } 2097bd096f64SJanosch Frank if (!r) 2098bd096f64SJanosch Frank i++; 2099bd096f64SJanosch Frank } 21004f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2101d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 210230ee2a98SJason J. Herne out: 210330ee2a98SJason J. Herne kvfree(keys); 210430ee2a98SJason J. Herne return r; 210530ee2a98SJason J. Herne } 210630ee2a98SJason J. Herne 21074036e387SClaudio Imbrenda /* 21084036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 21094036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 21104036e387SClaudio Imbrenda * two longs. 21114036e387SClaudio Imbrenda */ 21124036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 21134036e387SClaudio Imbrenda /* for consistency */ 21144036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 21154036e387SClaudio Imbrenda 2116afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2117afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2118afdad616SClaudio Imbrenda { 2119afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 2120afdad616SClaudio Imbrenda 2121afdad616SClaudio Imbrenda args->count = 0; 2122afdad616SClaudio Imbrenda while (args->count < bufsize) { 2123afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2124afdad616SClaudio Imbrenda /* 2125afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 2126afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 2127afdad616SClaudio Imbrenda */ 2128afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2129afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 2130afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2131afdad616SClaudio Imbrenda pgstev = 0; 2132afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2133afdad616SClaudio Imbrenda cur_gfn++; 2134afdad616SClaudio Imbrenda } 2135afdad616SClaudio Imbrenda 2136afdad616SClaudio Imbrenda return 0; 2137afdad616SClaudio Imbrenda } 2138afdad616SClaudio Imbrenda 2139c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots, 2140c928bfc2SMaciej S. Szmigiero gfn_t gfn) 2141c928bfc2SMaciej S. Szmigiero { 2142c928bfc2SMaciej S. Szmigiero return ____gfn_to_memslot(slots, gfn, true); 2143c928bfc2SMaciej S. Szmigiero } 2144c928bfc2SMaciej S. Szmigiero 2145afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 2146afdad616SClaudio Imbrenda unsigned long cur_gfn) 2147afdad616SClaudio Imbrenda { 2148c928bfc2SMaciej S. Szmigiero struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn); 2149afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 2150a54d8066SMaciej S. Szmigiero struct rb_node *mnode = &ms->gfn_node[slots->node_idx]; 2151afdad616SClaudio Imbrenda 2152afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 2153a54d8066SMaciej S. Szmigiero mnode = rb_next(mnode); 2154afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 2155a54d8066SMaciej S. Szmigiero if (!mnode) 2156a54d8066SMaciej S. Szmigiero mnode = rb_first(&slots->gfn_tree); 2157afdad616SClaudio Imbrenda 2158a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2159afdad616SClaudio Imbrenda ofs = 0; 2160afdad616SClaudio Imbrenda } 2161afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 2162a54d8066SMaciej S. Szmigiero while (ofs >= ms->npages && (mnode = rb_next(mnode))) { 2163a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2164b5c7e7ecSYury Norov ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages); 2165afdad616SClaudio Imbrenda } 2166afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 2167afdad616SClaudio Imbrenda } 2168afdad616SClaudio Imbrenda 2169afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2170afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2171afdad616SClaudio Imbrenda { 2172afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2173afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2174afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2175afdad616SClaudio Imbrenda 2176a54d8066SMaciej S. Szmigiero if (unlikely(kvm_memslots_empty(slots))) 21770774a964SSean Christopherson return 0; 21780774a964SSean Christopherson 2179afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2180afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2181afdad616SClaudio Imbrenda args->count = 0; 2182afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2183afdad616SClaudio Imbrenda if (!ms) 2184afdad616SClaudio Imbrenda return 0; 2185afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 21866a656832SMaciej S. Szmigiero mem_end = kvm_s390_get_gfn_end(slots); 2187afdad616SClaudio Imbrenda 2188afdad616SClaudio Imbrenda while (args->count < bufsize) { 2189afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2190afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2191afdad616SClaudio Imbrenda return 0; 2192afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2193afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2194afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2195afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2196afdad616SClaudio Imbrenda pgstev = 0; 2197afdad616SClaudio Imbrenda /* Save the value */ 2198afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2199afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2200afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2201afdad616SClaudio Imbrenda return 0; 2202afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2203afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2204afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2205afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2206afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2207afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2208afdad616SClaudio Imbrenda return 0; 2209afdad616SClaudio Imbrenda cur_gfn++; 2210afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2211afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2212afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2213afdad616SClaudio Imbrenda if (!ms) 2214afdad616SClaudio Imbrenda return 0; 2215afdad616SClaudio Imbrenda } 2216afdad616SClaudio Imbrenda } 2217afdad616SClaudio Imbrenda return 0; 2218afdad616SClaudio Imbrenda } 2219afdad616SClaudio Imbrenda 2220afdad616SClaudio Imbrenda /* 22214036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 22224036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 22234036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 22244036e387SClaudio Imbrenda * no trailing clean bytes are saved. 22254036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 22264036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 22274036e387SClaudio Imbrenda */ 22284036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 22294036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 22304036e387SClaudio Imbrenda { 2231afdad616SClaudio Imbrenda unsigned long bufsize; 2232afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2233afdad616SClaudio Imbrenda u8 *values; 22344036e387SClaudio Imbrenda 2235afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 22364036e387SClaudio Imbrenda return -ENXIO; 22374036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 22384036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 22394036e387SClaudio Imbrenda return -EINVAL; 22404036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 22414036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2242afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 22434036e387SClaudio Imbrenda return -EINVAL; 22444036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 22454036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2246c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 22474036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22484036e387SClaudio Imbrenda return 0; 22494036e387SClaudio Imbrenda } 22504036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2251afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 22524036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22534036e387SClaudio Imbrenda return 0; 22544036e387SClaudio Imbrenda } 22554036e387SClaudio Imbrenda 2256afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2257afdad616SClaudio Imbrenda if (!values) 22584036e387SClaudio Imbrenda return -ENOMEM; 22594036e387SClaudio Imbrenda 2260d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 22614036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2262afdad616SClaudio Imbrenda if (peek) 2263afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2264afdad616SClaudio Imbrenda else 2265afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 22664036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2267d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 22684036e387SClaudio Imbrenda 2269afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2270afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2271afdad616SClaudio Imbrenda else 2272afdad616SClaudio Imbrenda args->remaining = 0; 22734036e387SClaudio Imbrenda 2274afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2275afdad616SClaudio Imbrenda ret = -EFAULT; 2276afdad616SClaudio Imbrenda 2277afdad616SClaudio Imbrenda vfree(values); 2278afdad616SClaudio Imbrenda return ret; 22794036e387SClaudio Imbrenda } 22804036e387SClaudio Imbrenda 22814036e387SClaudio Imbrenda /* 22824036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 22834036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2284c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 22854036e387SClaudio Imbrenda */ 22864036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 22874036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 22884036e387SClaudio Imbrenda { 22894036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 22904036e387SClaudio Imbrenda uint8_t *bits; 22914036e387SClaudio Imbrenda int srcu_idx, r = 0; 22924036e387SClaudio Imbrenda 22934036e387SClaudio Imbrenda mask = args->mask; 22944036e387SClaudio Imbrenda 22954036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 22964036e387SClaudio Imbrenda return -ENXIO; 22974036e387SClaudio Imbrenda /* invalid/unsupported flags */ 22984036e387SClaudio Imbrenda if (args->flags != 0) 22994036e387SClaudio Imbrenda return -EINVAL; 23004036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 23014036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 23024036e387SClaudio Imbrenda return -EINVAL; 23034036e387SClaudio Imbrenda /* Nothing to do */ 23044036e387SClaudio Imbrenda if (args->count == 0) 23054036e387SClaudio Imbrenda return 0; 23064036e387SClaudio Imbrenda 230742bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 23084036e387SClaudio Imbrenda if (!bits) 23094036e387SClaudio Imbrenda return -ENOMEM; 23104036e387SClaudio Imbrenda 23114036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 23124036e387SClaudio Imbrenda if (r) { 23134036e387SClaudio Imbrenda r = -EFAULT; 23144036e387SClaudio Imbrenda goto out; 23154036e387SClaudio Imbrenda } 23164036e387SClaudio Imbrenda 2317d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 23184036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 23194036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 23204036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 23214036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 23224036e387SClaudio Imbrenda r = -EFAULT; 23234036e387SClaudio Imbrenda break; 23244036e387SClaudio Imbrenda } 23254036e387SClaudio Imbrenda 23264036e387SClaudio Imbrenda pgstev = bits[i]; 23274036e387SClaudio Imbrenda pgstev = pgstev << 24; 23281bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 23294036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 23304036e387SClaudio Imbrenda } 23314036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2332d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 23334036e387SClaudio Imbrenda 2334c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 2335d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 2336c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 2337d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 23384036e387SClaudio Imbrenda } 23394036e387SClaudio Imbrenda out: 23404036e387SClaudio Imbrenda vfree(bits); 23414036e387SClaudio Imbrenda return r; 23424036e387SClaudio Imbrenda } 23434036e387SClaudio Imbrenda 2344be48d86fSClaudio Imbrenda /** 2345be48d86fSClaudio Imbrenda * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to 2346be48d86fSClaudio Imbrenda * non protected. 2347be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2348be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2349be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2350be48d86fSClaudio Imbrenda * 2351be48d86fSClaudio Imbrenda * Does not stop in case of error, tries to convert as many 2352be48d86fSClaudio Imbrenda * CPUs as possible. In case of error, the RC and RRC of the last error are 2353be48d86fSClaudio Imbrenda * returned. 2354be48d86fSClaudio Imbrenda * 2355be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2356be48d86fSClaudio Imbrenda */ 2357be48d86fSClaudio Imbrenda int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 235829b40f10SJanosch Frank { 235929b40f10SJanosch Frank struct kvm_vcpu *vcpu; 236046808a4cSMarc Zyngier unsigned long i; 2361be48d86fSClaudio Imbrenda u16 _rc, _rrc; 2362be48d86fSClaudio Imbrenda int ret = 0; 236329b40f10SJanosch Frank 236429b40f10SJanosch Frank /* 236529b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 236629b40f10SJanosch Frank * At the same time we must not free the assigned resources when 236729b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 236829b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 236929b40f10SJanosch Frank * behind. 237029b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 237129b40f10SJanosch Frank */ 237229b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 237329b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 2374be48d86fSClaudio Imbrenda if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) { 2375be48d86fSClaudio Imbrenda *rc = _rc; 2376be48d86fSClaudio Imbrenda *rrc = _rrc; 237729b40f10SJanosch Frank ret = -EIO; 237829b40f10SJanosch Frank } 237929b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 238029b40f10SJanosch Frank } 2381ee6a569dSMichael Mueller /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */ 2382ee6a569dSMichael Mueller if (use_gisa) 2383ee6a569dSMichael Mueller kvm_s390_gisa_enable(kvm); 238429b40f10SJanosch Frank return ret; 238529b40f10SJanosch Frank } 238629b40f10SJanosch Frank 2387be48d86fSClaudio Imbrenda /** 2388be48d86fSClaudio Imbrenda * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM 2389be48d86fSClaudio Imbrenda * to protected. 2390be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2391be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2392be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2393be48d86fSClaudio Imbrenda * 2394be48d86fSClaudio Imbrenda * Tries to undo the conversion in case of error. 2395be48d86fSClaudio Imbrenda * 2396be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2397be48d86fSClaudio Imbrenda */ 239829b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 239929b40f10SJanosch Frank { 240046808a4cSMarc Zyngier unsigned long i; 240146808a4cSMarc Zyngier int r = 0; 240229b40f10SJanosch Frank u16 dummy; 240329b40f10SJanosch Frank 240429b40f10SJanosch Frank struct kvm_vcpu *vcpu; 240529b40f10SJanosch Frank 2406ee6a569dSMichael Mueller /* Disable the GISA if the ultravisor does not support AIV. */ 2407ee6a569dSMichael Mueller if (!test_bit_inv(BIT_UV_FEAT_AIV, &uv_info.uv_feature_indications)) 2408ee6a569dSMichael Mueller kvm_s390_gisa_disable(kvm); 2409ee6a569dSMichael Mueller 241029b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 241129b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 241229b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 241329b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 241429b40f10SJanosch Frank if (r) 241529b40f10SJanosch Frank break; 241629b40f10SJanosch Frank } 241729b40f10SJanosch Frank if (r) 241829b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 241929b40f10SJanosch Frank return r; 242029b40f10SJanosch Frank } 242129b40f10SJanosch Frank 242235d02493SJanosch Frank /* 242335d02493SJanosch Frank * Here we provide user space with a direct interface to query UV 242435d02493SJanosch Frank * related data like UV maxima and available features as well as 242535d02493SJanosch Frank * feature specific data. 242635d02493SJanosch Frank * 242735d02493SJanosch Frank * To facilitate future extension of the data structures we'll try to 242835d02493SJanosch Frank * write data up to the maximum requested length. 242935d02493SJanosch Frank */ 243035d02493SJanosch Frank static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info) 243135d02493SJanosch Frank { 243235d02493SJanosch Frank ssize_t len_min; 243335d02493SJanosch Frank 243435d02493SJanosch Frank switch (info->header.id) { 243535d02493SJanosch Frank case KVM_PV_INFO_VM: { 243635d02493SJanosch Frank len_min = sizeof(info->header) + sizeof(info->vm); 243735d02493SJanosch Frank 243835d02493SJanosch Frank if (info->header.len_max < len_min) 243935d02493SJanosch Frank return -EINVAL; 244035d02493SJanosch Frank 244135d02493SJanosch Frank memcpy(info->vm.inst_calls_list, 244235d02493SJanosch Frank uv_info.inst_calls_list, 244335d02493SJanosch Frank sizeof(uv_info.inst_calls_list)); 244435d02493SJanosch Frank 244535d02493SJanosch Frank /* It's max cpuid not max cpus, so it's off by one */ 244635d02493SJanosch Frank info->vm.max_cpus = uv_info.max_guest_cpu_id + 1; 244735d02493SJanosch Frank info->vm.max_guests = uv_info.max_num_sec_conf; 244835d02493SJanosch Frank info->vm.max_guest_addr = uv_info.max_sec_stor_addr; 244935d02493SJanosch Frank info->vm.feature_indication = uv_info.uv_feature_indications; 245035d02493SJanosch Frank 245135d02493SJanosch Frank return len_min; 245235d02493SJanosch Frank } 2453fe9a93e0SJanosch Frank case KVM_PV_INFO_DUMP: { 2454fe9a93e0SJanosch Frank len_min = sizeof(info->header) + sizeof(info->dump); 2455fe9a93e0SJanosch Frank 2456fe9a93e0SJanosch Frank if (info->header.len_max < len_min) 2457fe9a93e0SJanosch Frank return -EINVAL; 2458fe9a93e0SJanosch Frank 2459fe9a93e0SJanosch Frank info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len; 2460fe9a93e0SJanosch Frank info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len; 2461fe9a93e0SJanosch Frank info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len; 2462fe9a93e0SJanosch Frank return len_min; 2463fe9a93e0SJanosch Frank } 246435d02493SJanosch Frank default: 246535d02493SJanosch Frank return -EINVAL; 246635d02493SJanosch Frank } 246735d02493SJanosch Frank } 246835d02493SJanosch Frank 24690460eb35SJanosch Frank static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd, 24700460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp) 24710460eb35SJanosch Frank { 24720460eb35SJanosch Frank int r = -EINVAL; 24730460eb35SJanosch Frank void __user *result_buff = (void __user *)dmp.buff_addr; 24740460eb35SJanosch Frank 24750460eb35SJanosch Frank switch (dmp.subcmd) { 24760460eb35SJanosch Frank case KVM_PV_DUMP_INIT: { 24770460eb35SJanosch Frank if (kvm->arch.pv.dumping) 24780460eb35SJanosch Frank break; 24790460eb35SJanosch Frank 24800460eb35SJanosch Frank /* 24810460eb35SJanosch Frank * Block SIE entry as concurrent dump UVCs could lead 24820460eb35SJanosch Frank * to validities. 24830460eb35SJanosch Frank */ 24840460eb35SJanosch Frank kvm_s390_vcpu_block_all(kvm); 24850460eb35SJanosch Frank 24860460eb35SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 24870460eb35SJanosch Frank UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc); 24880460eb35SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x", 24890460eb35SJanosch Frank cmd->rc, cmd->rrc); 24900460eb35SJanosch Frank if (!r) { 24910460eb35SJanosch Frank kvm->arch.pv.dumping = true; 24920460eb35SJanosch Frank } else { 24930460eb35SJanosch Frank kvm_s390_vcpu_unblock_all(kvm); 24940460eb35SJanosch Frank r = -EINVAL; 24950460eb35SJanosch Frank } 24960460eb35SJanosch Frank break; 24970460eb35SJanosch Frank } 24980460eb35SJanosch Frank case KVM_PV_DUMP_CONFIG_STOR_STATE: { 24990460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 25000460eb35SJanosch Frank break; 25010460eb35SJanosch Frank 25020460eb35SJanosch Frank /* 25030460eb35SJanosch Frank * gaddr is an output parameter since we might stop 25040460eb35SJanosch Frank * early. As dmp will be copied back in our caller, we 25050460eb35SJanosch Frank * don't need to do it ourselves. 25060460eb35SJanosch Frank */ 25070460eb35SJanosch Frank r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len, 25080460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 25090460eb35SJanosch Frank break; 25100460eb35SJanosch Frank } 25110460eb35SJanosch Frank case KVM_PV_DUMP_COMPLETE: { 25120460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 25130460eb35SJanosch Frank break; 25140460eb35SJanosch Frank 25150460eb35SJanosch Frank r = -EINVAL; 25160460eb35SJanosch Frank if (dmp.buff_len < uv_info.conf_dump_finalize_len) 25170460eb35SJanosch Frank break; 25180460eb35SJanosch Frank 25190460eb35SJanosch Frank r = kvm_s390_pv_dump_complete(kvm, result_buff, 25200460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 25210460eb35SJanosch Frank break; 25220460eb35SJanosch Frank } 25230460eb35SJanosch Frank default: 25240460eb35SJanosch Frank r = -ENOTTY; 25250460eb35SJanosch Frank break; 25260460eb35SJanosch Frank } 25270460eb35SJanosch Frank 25280460eb35SJanosch Frank return r; 25290460eb35SJanosch Frank } 25300460eb35SJanosch Frank 253129b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 253229b40f10SJanosch Frank { 2533fb491d55SClaudio Imbrenda const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM); 2534fb491d55SClaudio Imbrenda void __user *argp = (void __user *)cmd->data; 253529b40f10SJanosch Frank int r = 0; 253629b40f10SJanosch Frank u16 dummy; 2537fb491d55SClaudio Imbrenda 2538fb491d55SClaudio Imbrenda if (need_lock) 2539fb491d55SClaudio Imbrenda mutex_lock(&kvm->lock); 254029b40f10SJanosch Frank 254129b40f10SJanosch Frank switch (cmd->cmd) { 254229b40f10SJanosch Frank case KVM_PV_ENABLE: { 254329b40f10SJanosch Frank r = -EINVAL; 254429b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 254529b40f10SJanosch Frank break; 254629b40f10SJanosch Frank 254729b40f10SJanosch Frank /* 254829b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 254929b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 255029b40f10SJanosch Frank */ 255129b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 255229b40f10SJanosch Frank if (r) 255329b40f10SJanosch Frank break; 255429b40f10SJanosch Frank 2555d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 2556fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2557d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 2558fa0c5eabSJanosch Frank if (r) 2559fa0c5eabSJanosch Frank break; 2560fa0c5eabSJanosch Frank 256129b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 256229b40f10SJanosch Frank if (r) 256329b40f10SJanosch Frank break; 256429b40f10SJanosch Frank 256529b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 256629b40f10SJanosch Frank if (r) 256729b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 25680890ddeaSChristian Borntraeger 25690890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 25700890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 257129b40f10SJanosch Frank break; 257229b40f10SJanosch Frank } 2573fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PREPARE: 2574fb491d55SClaudio Imbrenda r = -EINVAL; 2575fb491d55SClaudio Imbrenda if (!kvm_s390_pv_is_protected(kvm) || !async_destroy) 2576fb491d55SClaudio Imbrenda break; 2577fb491d55SClaudio Imbrenda 2578fb491d55SClaudio Imbrenda r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 2579fb491d55SClaudio Imbrenda /* 2580fb491d55SClaudio Imbrenda * If a CPU could not be destroyed, destroy VM will also fail. 2581fb491d55SClaudio Imbrenda * There is no point in trying to destroy it. Instead return 2582fb491d55SClaudio Imbrenda * the rc and rrc from the first CPU that failed destroying. 2583fb491d55SClaudio Imbrenda */ 2584fb491d55SClaudio Imbrenda if (r) 2585fb491d55SClaudio Imbrenda break; 2586fb491d55SClaudio Imbrenda r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc); 2587fb491d55SClaudio Imbrenda 2588fb491d55SClaudio Imbrenda /* no need to block service interrupts any more */ 2589fb491d55SClaudio Imbrenda clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 2590fb491d55SClaudio Imbrenda break; 2591fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PERFORM: 2592fb491d55SClaudio Imbrenda r = -EINVAL; 2593fb491d55SClaudio Imbrenda if (!async_destroy) 2594fb491d55SClaudio Imbrenda break; 2595fb491d55SClaudio Imbrenda /* kvm->lock must not be held; this is asserted inside the function. */ 2596fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc); 2597fb491d55SClaudio Imbrenda break; 259829b40f10SJanosch Frank case KVM_PV_DISABLE: { 259929b40f10SJanosch Frank r = -EINVAL; 260029b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 260129b40f10SJanosch Frank break; 260229b40f10SJanosch Frank 260329b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 260429b40f10SJanosch Frank /* 260529b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 260629b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 260729b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 260829b40f10SJanosch Frank */ 260929b40f10SJanosch Frank if (r) 261029b40f10SJanosch Frank break; 2611fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc); 26120890ddeaSChristian Borntraeger 26130890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 26140890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 261529b40f10SJanosch Frank break; 261629b40f10SJanosch Frank } 261729b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 261829b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 261929b40f10SJanosch Frank void *hdr; 262029b40f10SJanosch Frank 262129b40f10SJanosch Frank r = -EINVAL; 262229b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 262329b40f10SJanosch Frank break; 262429b40f10SJanosch Frank 262529b40f10SJanosch Frank r = -EFAULT; 262629b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 262729b40f10SJanosch Frank break; 262829b40f10SJanosch Frank 262929b40f10SJanosch Frank /* Currently restricted to 8KB */ 263029b40f10SJanosch Frank r = -EINVAL; 263129b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 263229b40f10SJanosch Frank break; 263329b40f10SJanosch Frank 263429b40f10SJanosch Frank r = -ENOMEM; 263529b40f10SJanosch Frank hdr = vmalloc(parms.length); 263629b40f10SJanosch Frank if (!hdr) 263729b40f10SJanosch Frank break; 263829b40f10SJanosch Frank 263929b40f10SJanosch Frank r = -EFAULT; 264029b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 264129b40f10SJanosch Frank parms.length)) 264229b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 264329b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 264429b40f10SJanosch Frank 264529b40f10SJanosch Frank vfree(hdr); 264629b40f10SJanosch Frank break; 264729b40f10SJanosch Frank } 264829b40f10SJanosch Frank case KVM_PV_UNPACK: { 264929b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 265029b40f10SJanosch Frank 265129b40f10SJanosch Frank r = -EINVAL; 26521ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) 265329b40f10SJanosch Frank break; 265429b40f10SJanosch Frank 265529b40f10SJanosch Frank r = -EFAULT; 265629b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 265729b40f10SJanosch Frank break; 265829b40f10SJanosch Frank 265929b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 266029b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 266129b40f10SJanosch Frank break; 266229b40f10SJanosch Frank } 266329b40f10SJanosch Frank case KVM_PV_VERIFY: { 266429b40f10SJanosch Frank r = -EINVAL; 266529b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 266629b40f10SJanosch Frank break; 266729b40f10SJanosch Frank 266829b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 266929b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 267029b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 267129b40f10SJanosch Frank cmd->rrc); 267229b40f10SJanosch Frank break; 267329b40f10SJanosch Frank } 2674e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2675e0d2773dSJanosch Frank r = -EINVAL; 2676e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2677e0d2773dSJanosch Frank break; 2678e0d2773dSJanosch Frank 2679e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2680e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2681e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2682e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2683e0d2773dSJanosch Frank break; 2684e0d2773dSJanosch Frank } 2685e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2686e0d2773dSJanosch Frank r = -EINVAL; 2687e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2688e0d2773dSJanosch Frank break; 2689e0d2773dSJanosch Frank 2690e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2691e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2692e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2693e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2694e0d2773dSJanosch Frank break; 2695e0d2773dSJanosch Frank } 269635d02493SJanosch Frank case KVM_PV_INFO: { 269735d02493SJanosch Frank struct kvm_s390_pv_info info = {}; 269835d02493SJanosch Frank ssize_t data_len; 269935d02493SJanosch Frank 270035d02493SJanosch Frank /* 270135d02493SJanosch Frank * No need to check the VM protection here. 270235d02493SJanosch Frank * 270335d02493SJanosch Frank * Maybe user space wants to query some of the data 270435d02493SJanosch Frank * when the VM is still unprotected. If we see the 270535d02493SJanosch Frank * need to fence a new data command we can still 270635d02493SJanosch Frank * return an error in the info handler. 270735d02493SJanosch Frank */ 270835d02493SJanosch Frank 270935d02493SJanosch Frank r = -EFAULT; 271035d02493SJanosch Frank if (copy_from_user(&info, argp, sizeof(info.header))) 271135d02493SJanosch Frank break; 271235d02493SJanosch Frank 271335d02493SJanosch Frank r = -EINVAL; 271435d02493SJanosch Frank if (info.header.len_max < sizeof(info.header)) 271535d02493SJanosch Frank break; 271635d02493SJanosch Frank 271735d02493SJanosch Frank data_len = kvm_s390_handle_pv_info(&info); 271835d02493SJanosch Frank if (data_len < 0) { 271935d02493SJanosch Frank r = data_len; 272035d02493SJanosch Frank break; 272135d02493SJanosch Frank } 272235d02493SJanosch Frank /* 272335d02493SJanosch Frank * If a data command struct is extended (multiple 272435d02493SJanosch Frank * times) this can be used to determine how much of it 272535d02493SJanosch Frank * is valid. 272635d02493SJanosch Frank */ 272735d02493SJanosch Frank info.header.len_written = data_len; 272835d02493SJanosch Frank 272935d02493SJanosch Frank r = -EFAULT; 273035d02493SJanosch Frank if (copy_to_user(argp, &info, data_len)) 273135d02493SJanosch Frank break; 273235d02493SJanosch Frank 273335d02493SJanosch Frank r = 0; 273435d02493SJanosch Frank break; 273535d02493SJanosch Frank } 27360460eb35SJanosch Frank case KVM_PV_DUMP: { 27370460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp; 27380460eb35SJanosch Frank 27390460eb35SJanosch Frank r = -EINVAL; 27400460eb35SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 27410460eb35SJanosch Frank break; 27420460eb35SJanosch Frank 27430460eb35SJanosch Frank r = -EFAULT; 27440460eb35SJanosch Frank if (copy_from_user(&dmp, argp, sizeof(dmp))) 27450460eb35SJanosch Frank break; 27460460eb35SJanosch Frank 27470460eb35SJanosch Frank r = kvm_s390_pv_dmp(kvm, cmd, dmp); 27480460eb35SJanosch Frank if (r) 27490460eb35SJanosch Frank break; 27500460eb35SJanosch Frank 27510460eb35SJanosch Frank if (copy_to_user(argp, &dmp, sizeof(dmp))) { 27520460eb35SJanosch Frank r = -EFAULT; 27530460eb35SJanosch Frank break; 27540460eb35SJanosch Frank } 27550460eb35SJanosch Frank 27560460eb35SJanosch Frank break; 27570460eb35SJanosch Frank } 275829b40f10SJanosch Frank default: 275929b40f10SJanosch Frank r = -ENOTTY; 276029b40f10SJanosch Frank } 2761fb491d55SClaudio Imbrenda if (need_lock) 2762fb491d55SClaudio Imbrenda mutex_unlock(&kvm->lock); 2763fb491d55SClaudio Imbrenda 276429b40f10SJanosch Frank return r; 276529b40f10SJanosch Frank } 276629b40f10SJanosch Frank 2767*a41f505eSJanis Schoetterl-Glausch static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags) 2768e9e9feebSJanis Schoetterl-Glausch { 2769*a41f505eSJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size) 2770*a41f505eSJanis Schoetterl-Glausch return -EINVAL; 2771*a41f505eSJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE) 2772*a41f505eSJanis Schoetterl-Glausch return -E2BIG; 2773*a41f505eSJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 2774*a41f505eSJanis Schoetterl-Glausch if (mop->key > 0xf) 2775*a41f505eSJanis Schoetterl-Glausch return -EINVAL; 2776*a41f505eSJanis Schoetterl-Glausch } else { 2777*a41f505eSJanis Schoetterl-Glausch mop->key = 0; 2778*a41f505eSJanis Schoetterl-Glausch } 2779*a41f505eSJanis Schoetterl-Glausch return 0; 2780e9e9feebSJanis Schoetterl-Glausch } 2781e9e9feebSJanis Schoetterl-Glausch 2782ef11c946SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) 2783ef11c946SJanis Schoetterl-Glausch { 2784ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 2785ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL; 2786ef11c946SJanis Schoetterl-Glausch int r, srcu_idx; 2787ef11c946SJanis Schoetterl-Glausch 2788*a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION | 2789*a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY); 2790*a41f505eSJanis Schoetterl-Glausch if (r) 2791*a41f505eSJanis Schoetterl-Glausch return r; 2792*a41f505eSJanis Schoetterl-Glausch 2793b5d12744SJanis Schoetterl-Glausch /* 2794b5d12744SJanis Schoetterl-Glausch * This is technically a heuristic only, if the kvm->lock is not 2795b5d12744SJanis Schoetterl-Glausch * taken, it is not guaranteed that the vm is/remains non-protected. 2796b5d12744SJanis Schoetterl-Glausch * This is ok from a kernel perspective, wrongdoing is detected 2797b5d12744SJanis Schoetterl-Glausch * on the access, -EFAULT is returned and the vm may crash the 2798b5d12744SJanis Schoetterl-Glausch * next time it accesses the memory in question. 2799b5d12744SJanis Schoetterl-Glausch * There is no sane usecase to do switching and a memop on two 2800b5d12744SJanis Schoetterl-Glausch * different CPUs at the same time. 2801b5d12744SJanis Schoetterl-Glausch */ 2802b5d12744SJanis Schoetterl-Glausch if (kvm_s390_pv_get_handle(kvm)) 2803ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2804ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 2805ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size); 2806ef11c946SJanis Schoetterl-Glausch if (!tmpbuf) 2807ef11c946SJanis Schoetterl-Glausch return -ENOMEM; 2808ef11c946SJanis Schoetterl-Glausch } 2809ef11c946SJanis Schoetterl-Glausch 2810ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 2811ef11c946SJanis Schoetterl-Glausch 2812ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 2813ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 2814ef11c946SJanis Schoetterl-Glausch goto out_unlock; 2815ef11c946SJanis Schoetterl-Glausch } 2816ef11c946SJanis Schoetterl-Glausch 2817ef11c946SJanis Schoetterl-Glausch switch (mop->op) { 2818ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ: { 2819ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2820ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key); 2821ef11c946SJanis Schoetterl-Glausch } else { 2822ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2823ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key); 2824ef11c946SJanis Schoetterl-Glausch if (r == 0) { 2825ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) 2826ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2827ef11c946SJanis Schoetterl-Glausch } 2828ef11c946SJanis Schoetterl-Glausch } 2829ef11c946SJanis Schoetterl-Glausch break; 2830ef11c946SJanis Schoetterl-Glausch } 2831ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE: { 2832ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2833ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key); 2834ef11c946SJanis Schoetterl-Glausch } else { 2835ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) { 2836ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2837ef11c946SJanis Schoetterl-Glausch break; 2838ef11c946SJanis Schoetterl-Glausch } 2839ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2840ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key); 2841ef11c946SJanis Schoetterl-Glausch } 2842ef11c946SJanis Schoetterl-Glausch break; 2843ef11c946SJanis Schoetterl-Glausch } 2844ef11c946SJanis Schoetterl-Glausch default: 2845ef11c946SJanis Schoetterl-Glausch r = -EINVAL; 2846ef11c946SJanis Schoetterl-Glausch } 2847ef11c946SJanis Schoetterl-Glausch 2848ef11c946SJanis Schoetterl-Glausch out_unlock: 2849ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 2850ef11c946SJanis Schoetterl-Glausch 2851ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf); 2852ef11c946SJanis Schoetterl-Glausch return r; 2853ef11c946SJanis Schoetterl-Glausch } 2854ef11c946SJanis Schoetterl-Glausch 2855b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2856b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2857b0c632dbSHeiko Carstens { 2858b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2859b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2860f2061656SDominik Dingel struct kvm_device_attr attr; 2861b0c632dbSHeiko Carstens int r; 2862b0c632dbSHeiko Carstens 2863b0c632dbSHeiko Carstens switch (ioctl) { 2864ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2865ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2866ba5c1e9bSCarsten Otte 2867ba5c1e9bSCarsten Otte r = -EFAULT; 2868ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2869ba5c1e9bSCarsten Otte break; 2870ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2871ba5c1e9bSCarsten Otte break; 2872ba5c1e9bSCarsten Otte } 287384223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 287484223598SCornelia Huck struct kvm_irq_routing_entry routing; 287584223598SCornelia Huck 287684223598SCornelia Huck r = -EINVAL; 287784223598SCornelia Huck if (kvm->arch.use_irqchip) { 287884223598SCornelia Huck /* Set up dummy routing. */ 287984223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2880152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 288184223598SCornelia Huck } 288284223598SCornelia Huck break; 288384223598SCornelia Huck } 2884f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2885f2061656SDominik Dingel r = -EFAULT; 2886f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2887f2061656SDominik Dingel break; 2888f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2889f2061656SDominik Dingel break; 2890f2061656SDominik Dingel } 2891f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2892f2061656SDominik Dingel r = -EFAULT; 2893f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2894f2061656SDominik Dingel break; 2895f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2896f2061656SDominik Dingel break; 2897f2061656SDominik Dingel } 2898f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2899f2061656SDominik Dingel r = -EFAULT; 2900f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2901f2061656SDominik Dingel break; 2902f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2903f2061656SDominik Dingel break; 2904f2061656SDominik Dingel } 290530ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 290630ee2a98SJason J. Herne struct kvm_s390_skeys args; 290730ee2a98SJason J. Herne 290830ee2a98SJason J. Herne r = -EFAULT; 290930ee2a98SJason J. Herne if (copy_from_user(&args, argp, 291030ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 291130ee2a98SJason J. Herne break; 291230ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 291330ee2a98SJason J. Herne break; 291430ee2a98SJason J. Herne } 291530ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 291630ee2a98SJason J. Herne struct kvm_s390_skeys args; 291730ee2a98SJason J. Herne 291830ee2a98SJason J. Herne r = -EFAULT; 291930ee2a98SJason J. Herne if (copy_from_user(&args, argp, 292030ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 292130ee2a98SJason J. Herne break; 292230ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 292330ee2a98SJason J. Herne break; 292430ee2a98SJason J. Herne } 29254036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 29264036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29274036e387SClaudio Imbrenda 29284036e387SClaudio Imbrenda r = -EFAULT; 29294036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29304036e387SClaudio Imbrenda break; 29311de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29324036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 29331de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29344036e387SClaudio Imbrenda if (!r) { 29354036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 29364036e387SClaudio Imbrenda if (r) 29374036e387SClaudio Imbrenda r = -EFAULT; 29384036e387SClaudio Imbrenda } 29394036e387SClaudio Imbrenda break; 29404036e387SClaudio Imbrenda } 29414036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 29424036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29434036e387SClaudio Imbrenda 29444036e387SClaudio Imbrenda r = -EFAULT; 29454036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29464036e387SClaudio Imbrenda break; 29471de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29484036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 29491de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29504036e387SClaudio Imbrenda break; 29514036e387SClaudio Imbrenda } 295229b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 295329b40f10SJanosch Frank struct kvm_pv_cmd args; 295429b40f10SJanosch Frank 295567cf68b6SEric Farman /* protvirt means user cpu state */ 295667cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 295729b40f10SJanosch Frank r = 0; 295829b40f10SJanosch Frank if (!is_prot_virt_host()) { 295929b40f10SJanosch Frank r = -EINVAL; 296029b40f10SJanosch Frank break; 296129b40f10SJanosch Frank } 296229b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 296329b40f10SJanosch Frank r = -EFAULT; 296429b40f10SJanosch Frank break; 296529b40f10SJanosch Frank } 296629b40f10SJanosch Frank if (args.flags) { 296729b40f10SJanosch Frank r = -EINVAL; 296829b40f10SJanosch Frank break; 296929b40f10SJanosch Frank } 2970fb491d55SClaudio Imbrenda /* must be called without kvm->lock */ 297129b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 297229b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 297329b40f10SJanosch Frank r = -EFAULT; 297429b40f10SJanosch Frank break; 297529b40f10SJanosch Frank } 297629b40f10SJanosch Frank break; 297729b40f10SJanosch Frank } 2978ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: { 2979ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op; 2980ef11c946SJanis Schoetterl-Glausch 2981ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 2982ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op); 2983ef11c946SJanis Schoetterl-Glausch else 2984ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2985ef11c946SJanis Schoetterl-Glausch break; 2986ef11c946SJanis Schoetterl-Glausch } 2987db1c875eSMatthew Rosato case KVM_S390_ZPCI_OP: { 2988db1c875eSMatthew Rosato struct kvm_s390_zpci_op args; 2989db1c875eSMatthew Rosato 2990db1c875eSMatthew Rosato r = -EINVAL; 2991db1c875eSMatthew Rosato if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 2992db1c875eSMatthew Rosato break; 2993db1c875eSMatthew Rosato if (copy_from_user(&args, argp, sizeof(args))) { 2994db1c875eSMatthew Rosato r = -EFAULT; 2995db1c875eSMatthew Rosato break; 2996db1c875eSMatthew Rosato } 2997db1c875eSMatthew Rosato r = kvm_s390_pci_zpci_op(kvm, &args); 2998db1c875eSMatthew Rosato break; 2999db1c875eSMatthew Rosato } 3000b0c632dbSHeiko Carstens default: 3001367e1319SAvi Kivity r = -ENOTTY; 3002b0c632dbSHeiko Carstens } 3003b0c632dbSHeiko Carstens 3004b0c632dbSHeiko Carstens return r; 3005b0c632dbSHeiko Carstens } 3006b0c632dbSHeiko Carstens 300745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 300845c9b47cSTony Krowiak { 3009e585b24aSTony Krowiak struct ap_config_info info; 301045c9b47cSTony Krowiak 3011e585b24aSTony Krowiak if (ap_instructions_available()) { 3012e585b24aSTony Krowiak if (ap_qci(&info) == 0) 3013e585b24aSTony Krowiak return info.apxa; 301445c9b47cSTony Krowiak } 301545c9b47cSTony Krowiak 301645c9b47cSTony Krowiak return 0; 301745c9b47cSTony Krowiak } 301845c9b47cSTony Krowiak 3019e585b24aSTony Krowiak /* 3020e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 3021e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 3022e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 3023e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 3024e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 3025e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 3026e585b24aSTony Krowiak */ 302745c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 302845c9b47cSTony Krowiak { 302945c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 303045c9b47cSTony Krowiak 3031e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 3032e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 3033e585b24aSTony Krowiak 3034e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 3035e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3036e585b24aSTony Krowiak return; 3037e585b24aSTony Krowiak 303845c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 303945c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 304045c9b47cSTony Krowiak else 304145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 304245c9b47cSTony Krowiak } 304345c9b47cSTony Krowiak 304486956e70STony Krowiak /* 304586956e70STony Krowiak * kvm_arch_crypto_set_masks 304686956e70STony Krowiak * 304786956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 304886956e70STony Krowiak * to be set. 304986956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 305086956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 305186956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 305286956e70STony Krowiak * 305386956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 305486956e70STony Krowiak * which the KVM guest is granted access. 305586956e70STony Krowiak * 305686956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 305786956e70STony Krowiak * function. 305886956e70STony Krowiak */ 30590e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 30600e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 30610e237e44SPierre Morel { 30620e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 30630e237e44SPierre Morel 30640e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 30650e237e44SPierre Morel 30660e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 30670e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 30680e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 30690e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 30700e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 30710e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 30720e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 30730e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 30740e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 30750e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 30760e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 30770e237e44SPierre Morel break; 30780e237e44SPierre Morel case CRYCB_FORMAT1: 30790e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 30800e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 30810e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 30820e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 30830e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 30840e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 30850e237e44SPierre Morel *((unsigned short *)adm)); 30860e237e44SPierre Morel break; 30870e237e44SPierre Morel default: /* Can not happen */ 30880e237e44SPierre Morel break; 30890e237e44SPierre Morel } 30900e237e44SPierre Morel 30910e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 30920e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 30930e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 30940e237e44SPierre Morel } 30950e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 30960e237e44SPierre Morel 309786956e70STony Krowiak /* 309886956e70STony Krowiak * kvm_arch_crypto_clear_masks 309986956e70STony Krowiak * 310086956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 310186956e70STony Krowiak * to be cleared. 310286956e70STony Krowiak * 310386956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 310486956e70STony Krowiak * which the KVM guest is granted access. 310586956e70STony Krowiak * 310686956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 310786956e70STony Krowiak * function. 310886956e70STony Krowiak */ 310942104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 311042104598STony Krowiak { 311142104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 311242104598STony Krowiak 311342104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 311442104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 311542104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 311642104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 311742104598STony Krowiak 31180e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 31196cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 31206cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 312142104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 312242104598STony Krowiak } 312342104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 312442104598STony Krowiak 31259bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 31269d8d5786SMichael Mueller { 31279bb0ec09SDavid Hildenbrand struct cpuid cpuid; 31289bb0ec09SDavid Hildenbrand 31299bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 31309bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 31319bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 31329d8d5786SMichael Mueller } 31339d8d5786SMichael Mueller 3134c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 31355102ee87STony Krowiak { 3136c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 313745c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 31381e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 31395102ee87STony Krowiak 3140e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3141e585b24aSTony Krowiak return; 3142e585b24aSTony Krowiak 3143ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 3144ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 3145ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 3146ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 3147ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 3148ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 3149ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 31505102ee87STony Krowiak } 31515102ee87STony Krowiak 31527d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 31537d43bafcSEugene (jno) Dvurechenski { 31547d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 31555e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 31567d43bafcSEugene (jno) Dvurechenski else 31577d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 31587d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 31597d43bafcSEugene (jno) Dvurechenski } 31607d43bafcSEugene (jno) Dvurechenski 316109340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm) 316209340b2fSMatthew Rosato { 316309340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 316409340b2fSMatthew Rosato kvm_s390_pci_clear_list(kvm); 316509340b2fSMatthew Rosato 316609340b2fSMatthew Rosato __kvm_arch_free_vm(kvm); 316709340b2fSMatthew Rosato } 316809340b2fSMatthew Rosato 3169e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 3170b0c632dbSHeiko Carstens { 3171c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 31729d8d5786SMichael Mueller int i, rc; 3173b0c632dbSHeiko Carstens char debug_name[16]; 3174f6c137ffSChristian Borntraeger static unsigned long sca_offset; 3175b0c632dbSHeiko Carstens 3176e08b9637SCarsten Otte rc = -EINVAL; 3177e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3178e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 3179e08b9637SCarsten Otte goto out_err; 3180e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 3181e08b9637SCarsten Otte goto out_err; 3182e08b9637SCarsten Otte #else 3183e08b9637SCarsten Otte if (type) 3184e08b9637SCarsten Otte goto out_err; 3185e08b9637SCarsten Otte #endif 3186e08b9637SCarsten Otte 3187b0c632dbSHeiko Carstens rc = s390_enable_sie(); 3188b0c632dbSHeiko Carstens if (rc) 3189d89f5effSJan Kiszka goto out_err; 3190b0c632dbSHeiko Carstens 3191b290411aSCarsten Otte rc = -ENOMEM; 3192b290411aSCarsten Otte 319376a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 319476a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 31955e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 31969ac96d75SDavid Hildenbrand /* start with basic SCA */ 319776a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 3198b0c632dbSHeiko Carstens if (!kvm->arch.sca) 3199d89f5effSJan Kiszka goto out_err; 32000d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 3201c5c2c393SDavid Hildenbrand sca_offset += 16; 3202bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 3203c5c2c393SDavid Hildenbrand sca_offset = 0; 3204bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 3205bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 32060d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 3207b0c632dbSHeiko Carstens 3208b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 3209b0c632dbSHeiko Carstens 32101cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 3211b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 321240f5b735SDominik Dingel goto out_err; 3213b0c632dbSHeiko Carstens 321419114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 3215c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 3216c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 3217c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 321840f5b735SDominik Dingel goto out_err; 32199d8d5786SMichael Mueller 322025c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 3221c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 3222c3b9e3e1SChristian Borntraeger 3223c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 322417e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 3225c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 3226c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 322717e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 3228c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 3229c3b9e3e1SChristian Borntraeger } 3230346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 3231981467c9SMichael Mueller 32321935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 32331935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 32341935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 32351935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 323695ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 323795ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 32381bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 32391bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 32401bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 32411bab1c02SClaudio Imbrenda } 324295ca2cb5SJanosch Frank 324305f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 324405f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 324505f31e3bSPierre Morel 32469bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 324737c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 32489d8d5786SMichael Mueller 3249c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 32505102ee87STony Krowiak 325109340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 325209340b2fSMatthew Rosato mutex_lock(&kvm->lock); 325309340b2fSMatthew Rosato kvm_s390_pci_init_list(kvm); 325409340b2fSMatthew Rosato kvm_s390_vcpu_pci_enable_interp(kvm); 325509340b2fSMatthew Rosato mutex_unlock(&kvm->lock); 325609340b2fSMatthew Rosato } 325709340b2fSMatthew Rosato 325851978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 3259ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 32606d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 32616d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 32628a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 3263a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 3264ba5c1e9bSCarsten Otte 3265b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 326678f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 3267b0c632dbSHeiko Carstens 3268e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 3269e08b9637SCarsten Otte kvm->arch.gmap = NULL; 3270a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 3271e08b9637SCarsten Otte } else { 327232e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 3273ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 327432e6b236SGuenther Hutzl else 3275ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 327632e6b236SGuenther Hutzl sclp.hamax + 1); 32776ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 3278598841caSCarsten Otte if (!kvm->arch.gmap) 327940f5b735SDominik Dingel goto out_err; 32802c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 328124eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 3282e08b9637SCarsten Otte } 3283fa6b7fe9SCornelia Huck 3284c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 328555531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 32868ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 3287a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 3288cc674ef2SMichael Mueller if (use_gisa) 3289d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 3290fb491d55SClaudio Imbrenda INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup); 3291fb491d55SClaudio Imbrenda kvm->arch.pv.set_aside = NULL; 32928335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 32938ad35755SDavid Hildenbrand 3294d89f5effSJan Kiszka return 0; 3295d89f5effSJan Kiszka out_err: 3296c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 329740f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 32987d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 329978f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 3300d89f5effSJan Kiszka return rc; 3301b0c632dbSHeiko Carstens } 3302b0c632dbSHeiko Carstens 3303d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 3304d329c035SChristian Borntraeger { 330529b40f10SJanosch Frank u16 rc, rrc; 330629b40f10SJanosch Frank 3307d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 3308ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 330967335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 33103c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 3311bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 3312a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 331324fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 331427e0393fSCarsten Otte 331527e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 33166ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 331727e0393fSCarsten Otte 3318e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 3319b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 332029b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 332129b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 332229b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 3323d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 3324d329c035SChristian Borntraeger } 3325d329c035SChristian Borntraeger 3326b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 3327b0c632dbSHeiko Carstens { 332829b40f10SJanosch Frank u16 rc, rrc; 332929b40f10SJanosch Frank 333027592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 33317d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 3332d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 333329b40f10SJanosch Frank /* 333429b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 333529b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 3336fb491d55SClaudio Imbrenda * can mess with the pv state. 333729b40f10SJanosch Frank */ 3338fb491d55SClaudio Imbrenda kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc); 3339ca2fd060SClaudio Imbrenda /* 3340ca2fd060SClaudio Imbrenda * Remove the mmu notifier only when the whole KVM VM is torn down, 3341ca2fd060SClaudio Imbrenda * and only if one was registered to begin with. If the VM is 3342ca2fd060SClaudio Imbrenda * currently not protected, but has been previously been protected, 3343ca2fd060SClaudio Imbrenda * then it's possible that the notifier is still registered. 3344ca2fd060SClaudio Imbrenda */ 3345ca2fd060SClaudio Imbrenda if (kvm->arch.pv.mmu_notifier.ops) 3346ca2fd060SClaudio Imbrenda mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm); 3347ca2fd060SClaudio Imbrenda 334829b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 3349c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 335027e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 33516ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 3352841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 335367335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 3354a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 33558335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 3356b0c632dbSHeiko Carstens } 3357b0c632dbSHeiko Carstens 3358b0c632dbSHeiko Carstens /* Section: vcpu related */ 3359dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 3360b0c632dbSHeiko Carstens { 33616ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 336227e0393fSCarsten Otte if (!vcpu->arch.gmap) 336327e0393fSCarsten Otte return -ENOMEM; 33642c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 3365dafd032aSDominik Dingel 336627e0393fSCarsten Otte return 0; 336727e0393fSCarsten Otte } 336827e0393fSCarsten Otte 3369a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 3370a6e2f683SEugene (jno) Dvurechenski { 3371a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 3372a6940674SDavid Hildenbrand return; 33735e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 33747d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 33757d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 33767d43bafcSEugene (jno) Dvurechenski 33777d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 33787d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 33797d43bafcSEugene (jno) Dvurechenski } else { 3380bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 3381a6e2f683SEugene (jno) Dvurechenski 3382a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3383a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 3384a6e2f683SEugene (jno) Dvurechenski } 33855e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 33867d43bafcSEugene (jno) Dvurechenski } 3387a6e2f683SEugene (jno) Dvurechenski 3388eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 3389a6e2f683SEugene (jno) Dvurechenski { 3390a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3391fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca); 3392a6940674SDavid Hildenbrand 3393a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 3394fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3395fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3396f07afa04SDavid Hildenbrand return; 3397a6940674SDavid Hildenbrand } 3398eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 3399eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 3400eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 3401fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 34027d43bafcSEugene (jno) Dvurechenski 3403fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3404fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3405fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK; 34060c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 3407eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 34087d43bafcSEugene (jno) Dvurechenski } else { 3409eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 3410fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 3411a6e2f683SEugene (jno) Dvurechenski 3412fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3413fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3414fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3415eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3416a6e2f683SEugene (jno) Dvurechenski } 3417eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 34185e044315SEugene (jno) Dvurechenski } 34195e044315SEugene (jno) Dvurechenski 34205e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 34215e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 34225e044315SEugene (jno) Dvurechenski { 34235e044315SEugene (jno) Dvurechenski d->sda = s->sda; 34245e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 34255e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 34265e044315SEugene (jno) Dvurechenski } 34275e044315SEugene (jno) Dvurechenski 34285e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 34295e044315SEugene (jno) Dvurechenski { 34305e044315SEugene (jno) Dvurechenski int i; 34315e044315SEugene (jno) Dvurechenski 34325e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 34335e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 34345e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 34355e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 34365e044315SEugene (jno) Dvurechenski } 34375e044315SEugene (jno) Dvurechenski 34385e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 34395e044315SEugene (jno) Dvurechenski { 34405e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 34415e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 34425e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 344346808a4cSMarc Zyngier unsigned long vcpu_idx; 34445e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 3445fe0ef003SNico Boehr phys_addr_t new_sca_phys; 34465e044315SEugene (jno) Dvurechenski 344729b40f10SJanosch Frank if (kvm->arch.use_esca) 344829b40f10SJanosch Frank return 0; 344929b40f10SJanosch Frank 3450c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 34515e044315SEugene (jno) Dvurechenski if (!new_sca) 34525e044315SEugene (jno) Dvurechenski return -ENOMEM; 34535e044315SEugene (jno) Dvurechenski 3454fe0ef003SNico Boehr new_sca_phys = virt_to_phys(new_sca); 3455fe0ef003SNico Boehr scaoh = new_sca_phys >> 32; 3456fe0ef003SNico Boehr scaol = new_sca_phys & ESCA_SCAOL_MASK; 34575e044315SEugene (jno) Dvurechenski 34585e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 34595e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 34605e044315SEugene (jno) Dvurechenski 34615e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 34625e044315SEugene (jno) Dvurechenski 34635e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 34645e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 34655e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 34660c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 34675e044315SEugene (jno) Dvurechenski } 34685e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 34695e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 34705e044315SEugene (jno) Dvurechenski 34715e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 34725e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 34735e044315SEugene (jno) Dvurechenski 34745e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 34755e044315SEugene (jno) Dvurechenski 34768335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 34778335713aSChristian Borntraeger old_sca, kvm->arch.sca); 34785e044315SEugene (jno) Dvurechenski return 0; 34797d43bafcSEugene (jno) Dvurechenski } 3480a6e2f683SEugene (jno) Dvurechenski 3481a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 3482a6e2f683SEugene (jno) Dvurechenski { 34835e044315SEugene (jno) Dvurechenski int rc; 34845e044315SEugene (jno) Dvurechenski 3485a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3486a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 3487a6940674SDavid Hildenbrand return true; 3488a6940674SDavid Hildenbrand return false; 3489a6940674SDavid Hildenbrand } 34905e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 34915e044315SEugene (jno) Dvurechenski return true; 349276a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 34935e044315SEugene (jno) Dvurechenski return false; 34945e044315SEugene (jno) Dvurechenski 34955e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 34965e044315SEugene (jno) Dvurechenski 34975e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 3498a6e2f683SEugene (jno) Dvurechenski } 3499a6e2f683SEugene (jno) Dvurechenski 3500db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3501db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3502db0758b2SDavid Hildenbrand { 3503db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 35049c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3505db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35069c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3507db0758b2SDavid Hildenbrand } 3508db0758b2SDavid Hildenbrand 3509db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3510db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3511db0758b2SDavid Hildenbrand { 3512db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 35139c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3514db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3515db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 35169c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3517db0758b2SDavid Hildenbrand } 3518db0758b2SDavid Hildenbrand 3519db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3520db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3521db0758b2SDavid Hildenbrand { 3522db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3523db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3524db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3525db0758b2SDavid Hildenbrand } 3526db0758b2SDavid Hildenbrand 3527db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3528db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3529db0758b2SDavid Hildenbrand { 3530db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3531db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3532db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3533db0758b2SDavid Hildenbrand } 3534db0758b2SDavid Hildenbrand 3535db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3536db0758b2SDavid Hildenbrand { 3537db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3538db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3539db0758b2SDavid Hildenbrand preempt_enable(); 3540db0758b2SDavid Hildenbrand } 3541db0758b2SDavid Hildenbrand 3542db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3543db0758b2SDavid Hildenbrand { 3544db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3545db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3546db0758b2SDavid Hildenbrand preempt_enable(); 3547db0758b2SDavid Hildenbrand } 3548db0758b2SDavid Hildenbrand 35494287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 35504287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 35514287f247SDavid Hildenbrand { 3552db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35539c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3554db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3555db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35564287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 35579c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3558db0758b2SDavid Hildenbrand preempt_enable(); 35594287f247SDavid Hildenbrand } 35604287f247SDavid Hildenbrand 3561db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 35624287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 35634287f247SDavid Hildenbrand { 35649c23a131SDavid Hildenbrand unsigned int seq; 3565db0758b2SDavid Hildenbrand __u64 value; 3566db0758b2SDavid Hildenbrand 3567db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 35684287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3569db0758b2SDavid Hildenbrand 35709c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35719c23a131SDavid Hildenbrand do { 35729c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 35739c23a131SDavid Hildenbrand /* 35749c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 35759c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 35769c23a131SDavid Hildenbrand */ 35779c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3578db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 35799c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 35809c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3581db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 35829c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 35839c23a131SDavid Hildenbrand preempt_enable(); 3584db0758b2SDavid Hildenbrand return value; 35854287f247SDavid Hildenbrand } 35864287f247SDavid Hildenbrand 3587b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3588b0c632dbSHeiko Carstens { 35899977e886SHendrik Brueckner 359037d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3591ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 35925ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3593db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 359401a745acSDavid Hildenbrand vcpu->cpu = cpu; 3595b0c632dbSHeiko Carstens } 3596b0c632dbSHeiko Carstens 3597b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3598b0c632dbSHeiko Carstens { 359901a745acSDavid Hildenbrand vcpu->cpu = -1; 36005ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3601db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 36029daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 360337d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 360437d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 36059977e886SHendrik Brueckner 3606b0c632dbSHeiko Carstens } 3607b0c632dbSHeiko Carstens 360831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 360942897d86SMarcelo Tosatti { 361072f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3611fdf03650SFan Zhang preempt_disable(); 361272f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3613d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3614fdf03650SFan Zhang preempt_enable(); 361572f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 361625508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3617dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3618eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 361925508824SDavid Hildenbrand } 36206502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 36216502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 362237d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 362337d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 362442897d86SMarcelo Tosatti } 362542897d86SMarcelo Tosatti 36268ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 36278ec2fa52SChristian Borntraeger { 36288ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 36298ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 36308ec2fa52SChristian Borntraeger return true; 36318ec2fa52SChristian Borntraeger return false; 36328ec2fa52SChristian Borntraeger } 36338ec2fa52SChristian Borntraeger 36348ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 36358ec2fa52SChristian Borntraeger { 36368ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 36378ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 36388ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 36398ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 36408ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 36418ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 36428ec2fa52SChristian Borntraeger 36438ec2fa52SChristian Borntraeger } 36448ec2fa52SChristian Borntraeger 36455102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 36465102ee87STony Krowiak { 3647e585b24aSTony Krowiak /* 3648e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3649e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3650e585b24aSTony Krowiak */ 3651e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 36525102ee87STony Krowiak return; 36535102ee87STony Krowiak 3654e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3655a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 365637940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 36578ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3658a374e892STony Krowiak 3659e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3660e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3661e585b24aSTony Krowiak 3662e585b24aSTony Krowiak /* Set up protected key support */ 36638ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3664a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 36658ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 36668ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 36678ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 36688ec2fa52SChristian Borntraeger } 36698ec2fa52SChristian Borntraeger 3670a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3671a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 36725102ee87STony Krowiak } 36735102ee87STony Krowiak 3674b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3675b31605c1SDominik Dingel { 3676fe0ef003SNico Boehr free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo)); 3677b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3678b31605c1SDominik Dingel } 3679b31605c1SDominik Dingel 3680b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3681b31605c1SDominik Dingel { 3682fe0ef003SNico Boehr void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); 3683fe0ef003SNico Boehr 3684fe0ef003SNico Boehr if (!cbrlo_page) 3685b31605c1SDominik Dingel return -ENOMEM; 3686fe0ef003SNico Boehr 3687fe0ef003SNico Boehr vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page); 3688b31605c1SDominik Dingel return 0; 3689b31605c1SDominik Dingel } 3690b31605c1SDominik Dingel 369191520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 369291520f1aSMichael Mueller { 369391520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 369491520f1aSMichael Mueller 369591520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 369680bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3697fe0ef003SNico Boehr vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list); 369891520f1aSMichael Mueller } 369991520f1aSMichael Mueller 3700ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3701ff72bb55SSean Christopherson { 3702b31605c1SDominik Dingel int rc = 0; 370329b40f10SJanosch Frank u16 uvrc, uvrrc; 3704b31288faSKonstantin Weitz 37059e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 37069e6dabefSCornelia Huck CPUSTAT_SM | 3707a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3708a4a4f191SGuenther Hutzl 370953df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3710ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 371153df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3712ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3713a4a4f191SGuenther Hutzl 371491520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 371591520f1aSMichael Mueller 3716bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3717bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 37180c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3719bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 37200c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 372124fe0195SPierre Morel if (test_kvm_facility(vcpu->kvm, 11)) 372224fe0195SPierre Morel vcpu->arch.sie_block->ecb |= ECB_PTF; 3723f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 37240c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 37257119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 37267119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 37277feb6bb8SMichael Mueller 3728c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 37290c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3730cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 37310c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 37320c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 373348ee7d3aSDavid Hildenbrand if (sclp.has_cei) 37340c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 373511ad65b7SDavid Hildenbrand if (sclp.has_ib) 37360c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 373737c5f6c8SDavid Hildenbrand if (sclp.has_siif) 37380c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 373937c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 37400c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 374118280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 37420c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 37430c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 374413211ea7SEric Farman } 37458fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 37468fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3747a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3748a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3749d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3750d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3751d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3752d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3753d7c5cb01SMichael Mueller } 3754fe0ef003SNico Boehr vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC; 3755fe0ef003SNico Boehr vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb); 3756730cd632SFarhan Ali 3757730cd632SFarhan Ali if (sclp.has_kss) 3758ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3759730cd632SFarhan Ali else 3760492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 37615a5e6536SMatthew Rosato 3762e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3763b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3764b31605c1SDominik Dingel if (rc) 3765b31605c1SDominik Dingel return rc; 3766b31288faSKonstantin Weitz } 37670ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3768ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 37699d8d5786SMichael Mueller 377067d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 377167d49d52SCollin Walling 37725102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 37735102ee87STony Krowiak 37743f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 37753f4bbb43SMatthew Rosato 377629b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 377729b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 377829b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 377929b40f10SJanosch Frank if (rc) 378029b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 378129b40f10SJanosch Frank } 378229b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 378329b40f10SJanosch Frank 3784b31605c1SDominik Dingel return rc; 3785b0c632dbSHeiko Carstens } 3786b0c632dbSHeiko Carstens 3787897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3788897cc38eSSean Christopherson { 3789897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3790897cc38eSSean Christopherson return -EINVAL; 3791897cc38eSSean Christopherson return 0; 3792897cc38eSSean Christopherson } 3793897cc38eSSean Christopherson 3794e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3795b0c632dbSHeiko Carstens { 37967feb6bb8SMichael Mueller struct sie_page *sie_page; 3797897cc38eSSean Christopherson int rc; 37984d47555aSCarsten Otte 3799da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3800c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 38017feb6bb8SMichael Mueller if (!sie_page) 3802e529ef66SSean Christopherson return -ENOMEM; 3803b0c632dbSHeiko Carstens 38047feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 3805fe0ef003SNico Boehr vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb); 38067feb6bb8SMichael Mueller 3807efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3808efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3809efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3810efed1104SDavid Hildenbrand 3811e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3812ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3813ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm); 38149c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3815ba5c1e9bSCarsten Otte 3816321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3817321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3818321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3819321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3820321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3821321f8ee5SSean Christopherson KVM_SYNC_CRS | 3822321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 382323a60f83SCollin Walling KVM_SYNC_PFAULT | 382423a60f83SCollin Walling KVM_SYNC_DIAG318; 3825321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3826321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3827321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3828321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3829321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3830321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3831321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3832321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3833321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3834321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3835321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3836321f8ee5SSean Christopherson */ 3837321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3838321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3839321f8ee5SSean Christopherson else 3840321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3841321f8ee5SSean Christopherson 3842321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3843321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3844321f8ee5SSean Christopherson if (rc) 3845a2017f17SSean Christopherson goto out_free_sie_block; 3846321f8ee5SSean Christopherson } 3847321f8ee5SSean Christopherson 3848e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3849e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3850e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3851b0c632dbSHeiko Carstens 3852ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3853ff72bb55SSean Christopherson if (rc) 3854ff72bb55SSean Christopherson goto out_ucontrol_uninit; 385524fe0195SPierre Morel 385624fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 3857e529ef66SSean Christopherson return 0; 3858e529ef66SSean Christopherson 3859ff72bb55SSean Christopherson out_ucontrol_uninit: 3860ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3861ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 38627b06bf2fSWei Yongjun out_free_sie_block: 38637b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3864e529ef66SSean Christopherson return rc; 3865b0c632dbSHeiko Carstens } 3866b0c632dbSHeiko Carstens 3867b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3868b0c632dbSHeiko Carstens { 38699b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 38709a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3871b0c632dbSHeiko Carstens } 3872b0c632dbSHeiko Carstens 3873199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3874199b5763SLongpeng(Mike) { 38750546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3876199b5763SLongpeng(Mike) } 3877199b5763SLongpeng(Mike) 387827406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 387949b99e1eSChristian Borntraeger { 3880805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 388161a6df54SDavid Hildenbrand exit_sie(vcpu); 388249b99e1eSChristian Borntraeger } 388349b99e1eSChristian Borntraeger 388427406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 388549b99e1eSChristian Borntraeger { 3886805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 388749b99e1eSChristian Borntraeger } 388849b99e1eSChristian Borntraeger 38898e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 38908e236546SChristian Borntraeger { 3891805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 389261a6df54SDavid Hildenbrand exit_sie(vcpu); 38938e236546SChristian Borntraeger } 38948e236546SChristian Borntraeger 38959ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 38969ea59728SDavid Hildenbrand { 38979ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 38989ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 38999ea59728SDavid Hildenbrand } 39009ea59728SDavid Hildenbrand 39018e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 39028e236546SChristian Borntraeger { 39039bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 39048e236546SChristian Borntraeger } 39058e236546SChristian Borntraeger 390649b99e1eSChristian Borntraeger /* 39079ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 390849b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 390949b99e1eSChristian Borntraeger * return immediately. */ 391049b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 391149b99e1eSChristian Borntraeger { 3912ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 39139ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 391449b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 391549b99e1eSChristian Borntraeger cpu_relax(); 391649b99e1eSChristian Borntraeger } 391749b99e1eSChristian Borntraeger 39188e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 39198e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 392049b99e1eSChristian Borntraeger { 3921df06dae3SSean Christopherson __kvm_make_request(req, vcpu); 39228e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 392349b99e1eSChristian Borntraeger } 392449b99e1eSChristian Borntraeger 3925414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3926414d3b07SMartin Schwidefsky unsigned long end) 39272c70fe44SChristian Borntraeger { 39282c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 39292c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3930414d3b07SMartin Schwidefsky unsigned long prefix; 393146808a4cSMarc Zyngier unsigned long i; 39322c70fe44SChristian Borntraeger 393365d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 393465d0b0d4SDavid Hildenbrand return; 3935414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3936414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3937414d3b07SMartin Schwidefsky return; 39382c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 39392c70fe44SChristian Borntraeger /* match against both prefix pages */ 3940414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3941414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3942414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3943414d3b07SMartin Schwidefsky start, end); 3944cc65c3a1SSean Christopherson kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 39452c70fe44SChristian Borntraeger } 39462c70fe44SChristian Borntraeger } 39472c70fe44SChristian Borntraeger } 39482c70fe44SChristian Borntraeger 39498b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 39508b905d28SChristian Borntraeger { 39518b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 39528b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 39536f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) { 39548b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 39558b905d28SChristian Borntraeger return true; 39568b905d28SChristian Borntraeger } 39578b905d28SChristian Borntraeger return false; 39588b905d28SChristian Borntraeger } 39598b905d28SChristian Borntraeger 3960b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3961b6d33834SChristoffer Dall { 3962b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3963b6d33834SChristoffer Dall BUG(); 3964b6d33834SChristoffer Dall return 0; 3965b6d33834SChristoffer Dall } 3966b6d33834SChristoffer Dall 396714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 396814eebd91SCarsten Otte struct kvm_one_reg *reg) 396914eebd91SCarsten Otte { 397014eebd91SCarsten Otte int r = -EINVAL; 397114eebd91SCarsten Otte 397214eebd91SCarsten Otte switch (reg->id) { 397329b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 397429b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 397529b7c71bSCarsten Otte (u32 __user *)reg->addr); 397629b7c71bSCarsten Otte break; 397729b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 397829b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 397929b7c71bSCarsten Otte (u64 __user *)reg->addr); 398029b7c71bSCarsten Otte break; 398146a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 39824287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 398346a6dd1cSJason J. herne (u64 __user *)reg->addr); 398446a6dd1cSJason J. herne break; 398546a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 398646a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 398746a6dd1cSJason J. herne (u64 __user *)reg->addr); 398846a6dd1cSJason J. herne break; 3989536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3990536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3991536336c2SDominik Dingel (u64 __user *)reg->addr); 3992536336c2SDominik Dingel break; 3993536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3994536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3995536336c2SDominik Dingel (u64 __user *)reg->addr); 3996536336c2SDominik Dingel break; 3997536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3998536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3999536336c2SDominik Dingel (u64 __user *)reg->addr); 4000536336c2SDominik Dingel break; 4001672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4002672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 4003672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4004672550fbSChristian Borntraeger break; 4005afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4006afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 4007afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4008afa45ff5SChristian Borntraeger break; 400914eebd91SCarsten Otte default: 401014eebd91SCarsten Otte break; 401114eebd91SCarsten Otte } 401214eebd91SCarsten Otte 401314eebd91SCarsten Otte return r; 401414eebd91SCarsten Otte } 401514eebd91SCarsten Otte 401614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 401714eebd91SCarsten Otte struct kvm_one_reg *reg) 401814eebd91SCarsten Otte { 401914eebd91SCarsten Otte int r = -EINVAL; 40204287f247SDavid Hildenbrand __u64 val; 402114eebd91SCarsten Otte 402214eebd91SCarsten Otte switch (reg->id) { 402329b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 402429b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 402529b7c71bSCarsten Otte (u32 __user *)reg->addr); 402629b7c71bSCarsten Otte break; 402729b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 402829b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 402929b7c71bSCarsten Otte (u64 __user *)reg->addr); 403029b7c71bSCarsten Otte break; 403146a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 40324287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 40334287f247SDavid Hildenbrand if (!r) 40344287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 403546a6dd1cSJason J. herne break; 403646a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 403746a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 403846a6dd1cSJason J. herne (u64 __user *)reg->addr); 403946a6dd1cSJason J. herne break; 4040536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 4041536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 4042536336c2SDominik Dingel (u64 __user *)reg->addr); 40439fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 40449fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4045536336c2SDominik Dingel break; 4046536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 4047536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 4048536336c2SDominik Dingel (u64 __user *)reg->addr); 4049536336c2SDominik Dingel break; 4050536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4051536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 4052536336c2SDominik Dingel (u64 __user *)reg->addr); 4053536336c2SDominik Dingel break; 4054672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4055672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 4056672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4057672550fbSChristian Borntraeger break; 4058afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4059afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 4060afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4061afa45ff5SChristian Borntraeger break; 406214eebd91SCarsten Otte default: 406314eebd91SCarsten Otte break; 406414eebd91SCarsten Otte } 406514eebd91SCarsten Otte 406614eebd91SCarsten Otte return r; 406714eebd91SCarsten Otte } 4068b6d33834SChristoffer Dall 40697de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 4070b0c632dbSHeiko Carstens { 40717de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 40727de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 40737de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 40747de3f142SJanosch Frank 40757de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 40767de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 40777de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 40787de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 40797de3f142SJanosch Frank } 40807de3f142SJanosch Frank 40817de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 40827de3f142SJanosch Frank { 40837de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 40847de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 40857de3f142SJanosch Frank 4086e93fc7b4SChristian Borntraeger /* 4087e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 4088e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 4089e93fc7b4SChristian Borntraeger */ 40907de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 40917de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 40927de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 40937de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 40947de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 40957de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 40967de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 40977de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 4098e93fc7b4SChristian Borntraeger 4099e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 4100e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 4101e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4102e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 4103e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 4104e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 4105e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 4106e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 4107e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 4108e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4109e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 4110e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 41117de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 41120f303504SJanosch Frank /* 41130f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 41140f303504SJanosch Frank * them are overlayed and they are not accessible in this case 41150f303504SJanosch Frank * anyway. 41160f303504SJanosch Frank */ 41170f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 41187de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 41197de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 41207de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 41210f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 41220f303504SJanosch Frank } 41237de3f142SJanosch Frank } 41247de3f142SJanosch Frank 41257de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 41267de3f142SJanosch Frank { 41277de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 41287de3f142SJanosch Frank 41297de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 41307de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 41317de3f142SJanosch Frank 41327de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 41337de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 41347de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 41357de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 41367de3f142SJanosch Frank 41377de3f142SJanosch Frank regs->etoken = 0; 41387de3f142SJanosch Frank regs->etoken_extension = 0; 4139b0c632dbSHeiko Carstens } 4140b0c632dbSHeiko Carstens 4141b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4142b0c632dbSHeiko Carstens { 4143875656feSChristoffer Dall vcpu_load(vcpu); 41445a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 4145875656feSChristoffer Dall vcpu_put(vcpu); 4146b0c632dbSHeiko Carstens return 0; 4147b0c632dbSHeiko Carstens } 4148b0c632dbSHeiko Carstens 4149b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4150b0c632dbSHeiko Carstens { 41511fc9b76bSChristoffer Dall vcpu_load(vcpu); 41525a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 41531fc9b76bSChristoffer Dall vcpu_put(vcpu); 4154b0c632dbSHeiko Carstens return 0; 4155b0c632dbSHeiko Carstens } 4156b0c632dbSHeiko Carstens 4157b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 4158b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4159b0c632dbSHeiko Carstens { 4160b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 4161b4ef9d4eSChristoffer Dall 416259674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 4163b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 4164b4ef9d4eSChristoffer Dall 4165b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 4166b0c632dbSHeiko Carstens return 0; 4167b0c632dbSHeiko Carstens } 4168b0c632dbSHeiko Carstens 4169b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 4170b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4171b0c632dbSHeiko Carstens { 4172bcdec41cSChristoffer Dall vcpu_load(vcpu); 4173bcdec41cSChristoffer Dall 417459674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 4175b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 4176bcdec41cSChristoffer Dall 4177bcdec41cSChristoffer Dall vcpu_put(vcpu); 4178b0c632dbSHeiko Carstens return 0; 4179b0c632dbSHeiko Carstens } 4180b0c632dbSHeiko Carstens 4181b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4182b0c632dbSHeiko Carstens { 41836a96bc7fSChristoffer Dall int ret = 0; 41846a96bc7fSChristoffer Dall 41856a96bc7fSChristoffer Dall vcpu_load(vcpu); 41866a96bc7fSChristoffer Dall 41876a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 41886a96bc7fSChristoffer Dall ret = -EINVAL; 41896a96bc7fSChristoffer Dall goto out; 41906a96bc7fSChristoffer Dall } 4191e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 41929abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4193a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 4194a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 41959abc2a08SDavid Hildenbrand else 4196a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 41976a96bc7fSChristoffer Dall 41986a96bc7fSChristoffer Dall out: 41996a96bc7fSChristoffer Dall vcpu_put(vcpu); 42006a96bc7fSChristoffer Dall return ret; 4201b0c632dbSHeiko Carstens } 4202b0c632dbSHeiko Carstens 4203b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4204b0c632dbSHeiko Carstens { 42051393123eSChristoffer Dall vcpu_load(vcpu); 42061393123eSChristoffer Dall 42079abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 42089abc2a08SDavid Hildenbrand save_fpu_regs(); 42099abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4210a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 4211a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 42129abc2a08SDavid Hildenbrand else 4213a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 4214e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 42151393123eSChristoffer Dall 42161393123eSChristoffer Dall vcpu_put(vcpu); 4217b0c632dbSHeiko Carstens return 0; 4218b0c632dbSHeiko Carstens } 4219b0c632dbSHeiko Carstens 4220b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 4221b0c632dbSHeiko Carstens { 4222b0c632dbSHeiko Carstens int rc = 0; 4223b0c632dbSHeiko Carstens 42247a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4225b0c632dbSHeiko Carstens rc = -EBUSY; 4226d7b0b5ebSCarsten Otte else { 4227d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 4228d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 4229d7b0b5ebSCarsten Otte } 4230b0c632dbSHeiko Carstens return rc; 4231b0c632dbSHeiko Carstens } 4232b0c632dbSHeiko Carstens 4233b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 4234b0c632dbSHeiko Carstens struct kvm_translation *tr) 4235b0c632dbSHeiko Carstens { 4236b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 4237b0c632dbSHeiko Carstens } 4238b0c632dbSHeiko Carstens 423927291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 424027291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 424127291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 424227291e21SDavid Hildenbrand 4243d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 4244d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 4245b0c632dbSHeiko Carstens { 424627291e21SDavid Hildenbrand int rc = 0; 424727291e21SDavid Hildenbrand 424866b56562SChristoffer Dall vcpu_load(vcpu); 424966b56562SChristoffer Dall 425027291e21SDavid Hildenbrand vcpu->guest_debug = 0; 425127291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 425227291e21SDavid Hildenbrand 425366b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 425466b56562SChristoffer Dall rc = -EINVAL; 425566b56562SChristoffer Dall goto out; 425666b56562SChristoffer Dall } 425766b56562SChristoffer Dall if (!sclp.has_gpere) { 425866b56562SChristoffer Dall rc = -EINVAL; 425966b56562SChristoffer Dall goto out; 426066b56562SChristoffer Dall } 426127291e21SDavid Hildenbrand 426227291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 426327291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 426427291e21SDavid Hildenbrand /* enforce guest PER */ 4265ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 426627291e21SDavid Hildenbrand 426727291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 426827291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 426927291e21SDavid Hildenbrand } else { 42709daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 427127291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 427227291e21SDavid Hildenbrand } 427327291e21SDavid Hildenbrand 427427291e21SDavid Hildenbrand if (rc) { 427527291e21SDavid Hildenbrand vcpu->guest_debug = 0; 427627291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 42779daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 427827291e21SDavid Hildenbrand } 427927291e21SDavid Hildenbrand 428066b56562SChristoffer Dall out: 428166b56562SChristoffer Dall vcpu_put(vcpu); 428227291e21SDavid Hildenbrand return rc; 4283b0c632dbSHeiko Carstens } 4284b0c632dbSHeiko Carstens 428562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 428662d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 428762d9f0dbSMarcelo Tosatti { 4288fd232561SChristoffer Dall int ret; 4289fd232561SChristoffer Dall 4290fd232561SChristoffer Dall vcpu_load(vcpu); 4291fd232561SChristoffer Dall 42926352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 4293fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 42946352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 4295fd232561SChristoffer Dall 4296fd232561SChristoffer Dall vcpu_put(vcpu); 4297fd232561SChristoffer Dall return ret; 429862d9f0dbSMarcelo Tosatti } 429962d9f0dbSMarcelo Tosatti 430062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 430162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 430262d9f0dbSMarcelo Tosatti { 43036352e4d2SDavid Hildenbrand int rc = 0; 43046352e4d2SDavid Hildenbrand 4305e83dff5eSChristoffer Dall vcpu_load(vcpu); 4306e83dff5eSChristoffer Dall 43076352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 430867cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 43096352e4d2SDavid Hildenbrand 43106352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 43116352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 4312fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 43136352e4d2SDavid Hildenbrand break; 43146352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 4315fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 43166352e4d2SDavid Hildenbrand break; 43176352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 43187c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 43197c36a3fcSJanosch Frank rc = -ENXIO; 43207c36a3fcSJanosch Frank break; 43217c36a3fcSJanosch Frank } 43227c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 43237c36a3fcSJanosch Frank break; 43246352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 43253b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 43266352e4d2SDavid Hildenbrand default: 43276352e4d2SDavid Hildenbrand rc = -ENXIO; 43286352e4d2SDavid Hildenbrand } 43296352e4d2SDavid Hildenbrand 4330e83dff5eSChristoffer Dall vcpu_put(vcpu); 43316352e4d2SDavid Hildenbrand return rc; 433262d9f0dbSMarcelo Tosatti } 433362d9f0dbSMarcelo Tosatti 43348ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 43358ad35755SDavid Hildenbrand { 43368d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 43378ad35755SDavid Hildenbrand } 43388ad35755SDavid Hildenbrand 43392c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 43402c70fe44SChristian Borntraeger { 43418ad35755SDavid Hildenbrand retry: 43428e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 43432fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 4344586b7ccdSChristian Borntraeger return 0; 43452c70fe44SChristian Borntraeger /* 4346cc65c3a1SSean Christopherson * If the guest prefix changed, re-arm the ipte notifier for the 4347b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 43482c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 43492c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 43502c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 43512c70fe44SChristian Borntraeger */ 4352cc65c3a1SSean Christopherson if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) { 43532c70fe44SChristian Borntraeger int rc; 4354b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 4355fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 4356b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 4357aca411a4SJulius Niedworok if (rc) { 4358cc65c3a1SSean Christopherson kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 43592c70fe44SChristian Borntraeger return rc; 4360aca411a4SJulius Niedworok } 43618ad35755SDavid Hildenbrand goto retry; 43622c70fe44SChristian Borntraeger } 43638ad35755SDavid Hildenbrand 4364d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 4365d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 4366d3d692c8SDavid Hildenbrand goto retry; 4367d3d692c8SDavid Hildenbrand } 4368d3d692c8SDavid Hildenbrand 43698ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 43708ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 43718ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 4372ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 43738ad35755SDavid Hildenbrand } 43748ad35755SDavid Hildenbrand goto retry; 43758ad35755SDavid Hildenbrand } 43768ad35755SDavid Hildenbrand 43778ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 43788ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 43798ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 43809daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 43818ad35755SDavid Hildenbrand } 43828ad35755SDavid Hildenbrand goto retry; 43838ad35755SDavid Hildenbrand } 43848ad35755SDavid Hildenbrand 43856502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 43866502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 43876502a34cSDavid Hildenbrand goto retry; 43886502a34cSDavid Hildenbrand } 43896502a34cSDavid Hildenbrand 4390190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 4391190df4a2SClaudio Imbrenda /* 4392c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 4393190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 4394190df4a2SClaudio Imbrenda * functionalities needed for live migration. 4395190df4a2SClaudio Imbrenda */ 4396190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 4397190df4a2SClaudio Imbrenda goto retry; 4398190df4a2SClaudio Imbrenda } 4399190df4a2SClaudio Imbrenda 4400190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 4401190df4a2SClaudio Imbrenda /* 4402c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 4403c9f0a2b8SJanosch Frank * CMM has been used. 4404190df4a2SClaudio Imbrenda */ 4405190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 4406c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 4407190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 4408190df4a2SClaudio Imbrenda goto retry; 4409190df4a2SClaudio Imbrenda } 4410190df4a2SClaudio Imbrenda 44113194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 44123194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 44130759d068SDavid Hildenbrand 44142c70fe44SChristian Borntraeger return 0; 44152c70fe44SChristian Borntraeger } 44162c70fe44SChristian Borntraeger 4417c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 44188fa1696eSCollin L. Walling { 44198fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 44202cfd7b73SHeiko Carstens union tod_clock clk; 442146808a4cSMarc Zyngier unsigned long i; 44228fa1696eSCollin L. Walling 44238fa1696eSCollin L. Walling preempt_disable(); 44248fa1696eSCollin L. Walling 44252cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 44268fa1696eSCollin L. Walling 44272cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 44280e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 44290e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 44302cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 44318fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 44328fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 44330e7def5fSDavid Hildenbrand } 44348fa1696eSCollin L. Walling 44358fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 44368fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 44378fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 44388fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 44398fa1696eSCollin L. Walling } 44408fa1696eSCollin L. Walling 44418fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 44428fa1696eSCollin L. Walling preempt_enable(); 4443c0573ba5SClaudio Imbrenda } 4444c0573ba5SClaudio Imbrenda 4445c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4446c0573ba5SClaudio Imbrenda { 4447c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock)) 4448c0573ba5SClaudio Imbrenda return 0; 4449c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 4450c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock); 4451c0573ba5SClaudio Imbrenda return 1; 4452c0573ba5SClaudio Imbrenda } 4453c0573ba5SClaudio Imbrenda 4454fa576c58SThomas Huth /** 4455fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 4456fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 4457fa576c58SThomas Huth * @gpa: Guest physical address 4458fa576c58SThomas Huth * @writable: Whether the page should be writable or not 4459fa576c58SThomas Huth * 4460fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 4461fa576c58SThomas Huth * 4462fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 4463fa576c58SThomas Huth */ 4464fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 446524eb3a82SDominik Dingel { 4466527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 4467527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 446824eb3a82SDominik Dingel } 446924eb3a82SDominik Dingel 44703c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 44713c038e6bSDominik Dingel unsigned long token) 44723c038e6bSDominik Dingel { 44733c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 4474383d0b05SJens Freimann struct kvm_s390_irq irq; 44753c038e6bSDominik Dingel 44763c038e6bSDominik Dingel if (start_token) { 4477383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 4478383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 4479383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 44803c038e6bSDominik Dingel } else { 44813c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 4482383d0b05SJens Freimann inti.parm64 = token; 44833c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 44843c038e6bSDominik Dingel } 44853c038e6bSDominik Dingel } 44863c038e6bSDominik Dingel 44872a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 44883c038e6bSDominik Dingel struct kvm_async_pf *work) 44893c038e6bSDominik Dingel { 44903c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 44913c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 44922a18b7e7SVitaly Kuznetsov 44932a18b7e7SVitaly Kuznetsov return true; 44943c038e6bSDominik Dingel } 44953c038e6bSDominik Dingel 44963c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 44973c038e6bSDominik Dingel struct kvm_async_pf *work) 44983c038e6bSDominik Dingel { 44993c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 45003c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 45013c038e6bSDominik Dingel } 45023c038e6bSDominik Dingel 45033c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 45043c038e6bSDominik Dingel struct kvm_async_pf *work) 45053c038e6bSDominik Dingel { 45063c038e6bSDominik Dingel /* s390 will always inject the page directly */ 45073c038e6bSDominik Dingel } 45083c038e6bSDominik Dingel 45097c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 45103c038e6bSDominik Dingel { 45113c038e6bSDominik Dingel /* 45123c038e6bSDominik Dingel * s390 will always inject the page directly, 45133c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 45143c038e6bSDominik Dingel */ 45153c038e6bSDominik Dingel return true; 45163c038e6bSDominik Dingel } 45173c038e6bSDominik Dingel 4518e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 45193c038e6bSDominik Dingel { 45203c038e6bSDominik Dingel hva_t hva; 45213c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 45223c038e6bSDominik Dingel 45233c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4524e8c22266SVitaly Kuznetsov return false; 45253c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 45263c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4527e8c22266SVitaly Kuznetsov return false; 45283c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4529e8c22266SVitaly Kuznetsov return false; 45309a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4531e8c22266SVitaly Kuznetsov return false; 4532b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4533e8c22266SVitaly Kuznetsov return false; 45343c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4535e8c22266SVitaly Kuznetsov return false; 45363c038e6bSDominik Dingel 453781480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 453881480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 453981480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4540e8c22266SVitaly Kuznetsov return false; 45413c038e6bSDominik Dingel 4542e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 45433c038e6bSDominik Dingel } 45443c038e6bSDominik Dingel 45453fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4546b0c632dbSHeiko Carstens { 45473fb4c40fSThomas Huth int rc, cpuflags; 4548e168bf8dSCarsten Otte 45493c038e6bSDominik Dingel /* 45503c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 45513c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 45523c038e6bSDominik Dingel * handled outside the worker. 45533c038e6bSDominik Dingel */ 45543c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 45553c038e6bSDominik Dingel 45567ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 45577ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4558b0c632dbSHeiko Carstens 4559b0c632dbSHeiko Carstens if (need_resched()) 4560b0c632dbSHeiko Carstens schedule(); 4561b0c632dbSHeiko Carstens 456279395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 456379395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 456479395031SJens Freimann if (rc) 456579395031SJens Freimann return rc; 456679395031SJens Freimann } 45670ff31867SCarsten Otte 45682c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 45692c70fe44SChristian Borntraeger if (rc) 45702c70fe44SChristian Borntraeger return rc; 45712c70fe44SChristian Borntraeger 457227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 457327291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 457427291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 457527291e21SDavid Hildenbrand } 457627291e21SDavid Hildenbrand 45774eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 45789f30f621SMichael Mueller 4579b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 45803fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 45813fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 45823fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 45832b29a9fdSDominik Dingel 45843fb4c40fSThomas Huth return 0; 45853fb4c40fSThomas Huth } 45863fb4c40fSThomas Huth 4587492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4588492d8642SThomas Huth { 458956317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 459056317920SDavid Hildenbrand .code = PGM_ADDRESSING, 459156317920SDavid Hildenbrand }; 459256317920SDavid Hildenbrand u8 opcode, ilen; 4593492d8642SThomas Huth int rc; 4594492d8642SThomas Huth 4595492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4596492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4597492d8642SThomas Huth 4598492d8642SThomas Huth /* 4599492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4600492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4601492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4602492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4603492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4604492d8642SThomas Huth * to be able to forward the PSW. 4605492d8642SThomas Huth */ 46063fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 460756317920SDavid Hildenbrand ilen = insn_length(opcode); 46089b0d721aSDavid Hildenbrand if (rc < 0) { 46099b0d721aSDavid Hildenbrand return rc; 46109b0d721aSDavid Hildenbrand } else if (rc) { 46119b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 46129b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 46139b0d721aSDavid Hildenbrand * nullification if necessary. 46149b0d721aSDavid Hildenbrand */ 46159b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 46169b0d721aSDavid Hildenbrand ilen = 4; 46179b0d721aSDavid Hildenbrand } 461856317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 461956317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 462056317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4621492d8642SThomas Huth } 4622492d8642SThomas Huth 46233fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 46243fb4c40fSThomas Huth { 46254d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 46264d62fcc0SQingFeng Hao struct sie_page *sie_page; 46274d62fcc0SQingFeng Hao 46282b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 46292b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 46302b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 46312b29a9fdSDominik Dingel 463227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 463327291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 463427291e21SDavid Hildenbrand 46357ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 46367ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 463771f116bfSDavid Hildenbrand 46384d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 46394d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 46404d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 46414d62fcc0SQingFeng Hao struct sie_page, sie_block); 46424d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 46434d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 46444d62fcc0SQingFeng Hao return 0; 46454d62fcc0SQingFeng Hao } 46464d62fcc0SQingFeng Hao 464771f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 464871f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 464971f116bfSDavid Hildenbrand 465071f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 465171f116bfSDavid Hildenbrand return rc; 465271f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 465371f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 465471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 465571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 465671f116bfSDavid Hildenbrand return -EREMOTE; 465771f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 465871f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 465971f116bfSDavid Hildenbrand return 0; 4660210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4661210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4662210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4663210b1607SThomas Huth current->thread.gmap_addr; 4664210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 466571f116bfSDavid Hildenbrand return -EREMOTE; 466624eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 46673c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 466824eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 466971f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 467071f116bfSDavid Hildenbrand return 0; 467150a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 467271f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4673fa576c58SThomas Huth } 467471f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 46753fb4c40fSThomas Huth } 46763fb4c40fSThomas Huth 46773adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 46783fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 46793fb4c40fSThomas Huth { 46803fb4c40fSThomas Huth int rc, exit_reason; 4681c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 46823fb4c40fSThomas Huth 4683800c1065SThomas Huth /* 4684800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4685800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4686800c1065SThomas Huth */ 46872031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 4688800c1065SThomas Huth 4689a76ccff6SThomas Huth do { 46903fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 46913fb4c40fSThomas Huth if (rc) 4692a76ccff6SThomas Huth break; 46933fb4c40fSThomas Huth 46942031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 46953fb4c40fSThomas Huth /* 4696a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4697a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 46983fb4c40fSThomas Huth */ 46990097d12eSChristian Borntraeger local_irq_disable(); 47006edaa530SPaolo Bonzini guest_enter_irqoff(); 4701db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 47020097d12eSChristian Borntraeger local_irq_enable(); 4703c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4704c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4705c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4706c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4707c8aac234SJanosch Frank } 470856e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 470956e62a73SSven Schnelle load_fpu_regs(); 4710a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4711a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4712c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4713c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4714c8aac234SJanosch Frank sie_page->pv_grregs, 4715c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 47163adae0b4SJanosch Frank /* 47173adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 47183adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 47193adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 47203adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 47213adae0b4SJanosch Frank */ 47223adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 47233adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 47243adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 47253adae0b4SJanosch Frank } 4726c8aac234SJanosch Frank } 47270097d12eSChristian Borntraeger local_irq_disable(); 4728db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 47296edaa530SPaolo Bonzini guest_exit_irqoff(); 47300097d12eSChristian Borntraeger local_irq_enable(); 47312031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 47323fb4c40fSThomas Huth 47333fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 473427291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 47353fb4c40fSThomas Huth 47362031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 4737e168bf8dSCarsten Otte return rc; 4738b0c632dbSHeiko Carstens } 4739b0c632dbSHeiko Carstens 47402f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4741b028ee3eSDavid Hildenbrand { 47422f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 47434d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 47444e0b1ab7SFan Zhang struct gs_cb *gscb; 47454d5f2c04SChristian Borntraeger 47464d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 47474e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4748b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4749b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4750b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4751b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4752b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4753b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4754b028ee3eSDavid Hildenbrand } 4755b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4756b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4757b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4758b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 47599fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 47609fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4761b028ee3eSDavid Hildenbrand } 476223a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 476323a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 476423a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 47653fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 476623a60f83SCollin Walling } 476780cd8763SFan Zhang /* 476880cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 476980cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 477080cd8763SFan Zhang */ 477180cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 47724d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4773bb59c2daSAlice Frosi riccb->v && 47740c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 47754d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 47760c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 477780cd8763SFan Zhang } 47784e0b1ab7SFan Zhang /* 47794e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 47804e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 47814e0b1ab7SFan Zhang */ 47824e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 47834e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 47844e0b1ab7SFan Zhang gscb->gssm && 47854e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 47864e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 47874e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 47884e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 47894e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 479080cd8763SFan Zhang } 479135b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 479235b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 479335b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 479435b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 479535b3fde6SChristian Borntraeger } 47964e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 47974e0b1ab7SFan Zhang preempt_disable(); 47984e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 47994e0b1ab7SFan Zhang if (current->thread.gs_cb) { 48004e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 48014e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 48024e0b1ab7SFan Zhang } 48034e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 48044e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 48054e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 48064e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 48074e0b1ab7SFan Zhang } 48084e0b1ab7SFan Zhang preempt_enable(); 48094e0b1ab7SFan Zhang } 4810a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4811811ea797SJanosch Frank } 4812811ea797SJanosch Frank 48132f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4814811ea797SJanosch Frank { 48152f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48162f0a83beSTianjia Zhang 4817811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4818811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4819811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4820811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4821811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4822811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4823811ea797SJanosch Frank } 4824811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4825811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4826811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4827811ea797SJanosch Frank } 4828811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4829811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4830811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4831811ea797SJanosch Frank save_fpu_regs(); 4832811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4833811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4834811ea797SJanosch Frank if (MACHINE_HAS_VX) 4835811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4836811ea797SJanosch Frank else 4837811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4838811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4839811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4840811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4841811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4842811ea797SJanosch Frank 4843811ea797SJanosch Frank /* Sync fmt2 only data */ 4844811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 48452f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4846811ea797SJanosch Frank } else { 4847811ea797SJanosch Frank /* 4848811ea797SJanosch Frank * In several places we have to modify our internal view to 4849811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4850811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4851811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4852811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4853811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4854811ea797SJanosch Frank * do only accept the condition code from userspace. 4855811ea797SJanosch Frank */ 4856811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4857811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4858811ea797SJanosch Frank PSW_MASK_CC; 4859811ea797SJanosch Frank } 486080cd8763SFan Zhang 4861b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4862b028ee3eSDavid Hildenbrand } 4863b028ee3eSDavid Hildenbrand 48642f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4865b028ee3eSDavid Hildenbrand { 48662f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48672f0a83beSTianjia Zhang 4868b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4869b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4870b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 487135b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 487223a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 48734e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 487444bada28SHeiko Carstens preempt_disable(); 48754e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 48764e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 48774e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 48784e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 48794e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 48804e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 48814e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 48824e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 488344bada28SHeiko Carstens preempt_enable(); 48844e0b1ab7SFan Zhang } 4885a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4886b028ee3eSDavid Hildenbrand } 4887b028ee3eSDavid Hildenbrand 48882f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4889811ea797SJanosch Frank { 48902f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48912f0a83beSTianjia Zhang 4892811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4893811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4894811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4895811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4896811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4897811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4898811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4899811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4900811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4901811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4902811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4903811ea797SJanosch Frank /* Save guest register state */ 4904811ea797SJanosch Frank save_fpu_regs(); 4905811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4906811ea797SJanosch Frank /* Restore will be done lazily at return */ 4907811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4908811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4909811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 49102f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4911811ea797SJanosch Frank } 4912811ea797SJanosch Frank 49131b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4914b0c632dbSHeiko Carstens { 49151b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 49168f2abe6aSChristian Borntraeger int rc; 4917b0c632dbSHeiko Carstens 49180460eb35SJanosch Frank /* 49190460eb35SJanosch Frank * Running a VM while dumping always has the potential to 49200460eb35SJanosch Frank * produce inconsistent dump data. But for PV vcpus a SIE 49210460eb35SJanosch Frank * entry while dumping could also lead to a fatal validity 49220460eb35SJanosch Frank * intercept which we absolutely want to avoid. 49230460eb35SJanosch Frank */ 49240460eb35SJanosch Frank if (vcpu->kvm->arch.pv.dumping) 49250460eb35SJanosch Frank return -EINVAL; 49260460eb35SJanosch Frank 4927460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4928460df4c1SPaolo Bonzini return -EINTR; 4929460df4c1SPaolo Bonzini 4930200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4931200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4932200824f5SThomas Huth return -EINVAL; 4933200824f5SThomas Huth 4934accb757dSChristoffer Dall vcpu_load(vcpu); 4935accb757dSChristoffer Dall 493627291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 493727291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4938accb757dSChristoffer Dall rc = 0; 4939accb757dSChristoffer Dall goto out; 494027291e21SDavid Hildenbrand } 494127291e21SDavid Hildenbrand 494220b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4943b0c632dbSHeiko Carstens 4944fe28c786SJanosch Frank /* 4945fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4946fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4947fe28c786SJanosch Frank */ 49486352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 49496852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 49506352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4951ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 49526352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4953accb757dSChristoffer Dall rc = -EINVAL; 4954accb757dSChristoffer Dall goto out; 49556352e4d2SDavid Hildenbrand } 4956b0c632dbSHeiko Carstens 49572f0a83beSTianjia Zhang sync_regs(vcpu); 4958db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4959d7b0b5ebSCarsten Otte 4960dab4079dSHeiko Carstens might_fault(); 4961e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 49629ace903dSChristian Ehrhardt 4963b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4964b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 49658f2abe6aSChristian Borntraeger rc = -EINTR; 4966b1d16c49SChristian Ehrhardt } 49678f2abe6aSChristian Borntraeger 496827291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 496927291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 497027291e21SDavid Hildenbrand rc = 0; 497127291e21SDavid Hildenbrand } 497227291e21SDavid Hildenbrand 49738f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 497471f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 49758f2abe6aSChristian Borntraeger rc = 0; 49768f2abe6aSChristian Borntraeger } 49778f2abe6aSChristian Borntraeger 4978db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 49792f0a83beSTianjia Zhang store_regs(vcpu); 4980d7b0b5ebSCarsten Otte 498120b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4982b0c632dbSHeiko Carstens 4983b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4984accb757dSChristoffer Dall out: 4985accb757dSChristoffer Dall vcpu_put(vcpu); 49867e8e6ab4SHeiko Carstens return rc; 4987b0c632dbSHeiko Carstens } 4988b0c632dbSHeiko Carstens 4989b0c632dbSHeiko Carstens /* 4990b0c632dbSHeiko Carstens * store status at address 4991b0c632dbSHeiko Carstens * we use have two special cases: 4992b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4993b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4994b0c632dbSHeiko Carstens */ 4995d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4996b0c632dbSHeiko Carstens { 4997092670cdSCarsten Otte unsigned char archmode = 1; 49989abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4999fda902cbSMichael Mueller unsigned int px; 50004287f247SDavid Hildenbrand u64 clkcomp, cputm; 5001d0bce605SHeiko Carstens int rc; 5002b0c632dbSHeiko Carstens 5003d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 5004d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 5005d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 5006b0c632dbSHeiko Carstens return -EFAULT; 5007d9a3a09aSMartin Schwidefsky gpa = 0; 5008d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 5009d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 5010b0c632dbSHeiko Carstens return -EFAULT; 5011d9a3a09aSMartin Schwidefsky gpa = px; 5012d9a3a09aSMartin Schwidefsky } else 5013d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 50149abc2a08SDavid Hildenbrand 50159abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 50169abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 50179522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 5018d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50199abc2a08SDavid Hildenbrand fprs, 128); 50209abc2a08SDavid Hildenbrand } else { 50219abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50226fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 50239abc2a08SDavid Hildenbrand } 5024d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 5025d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 5026d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 5027d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 5028d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 5029fda902cbSMichael Mueller &px, 4); 5030d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 50319abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 5032d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 5033d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 50344287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 5035d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 50364287f247SDavid Hildenbrand &cputm, 8); 5037178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 5038d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 5039d0bce605SHeiko Carstens &clkcomp, 8); 5040d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 5041d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 5042d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 5043d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 5044d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 5045b0c632dbSHeiko Carstens } 5046b0c632dbSHeiko Carstens 5047e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 5048e879892cSThomas Huth { 5049e879892cSThomas Huth /* 5050e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 505131d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 5052e879892cSThomas Huth * it into the save area 5053e879892cSThomas Huth */ 5054d0164ee2SHendrik Brueckner save_fpu_regs(); 50559abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 5056e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 5057e879892cSThomas Huth 5058e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 5059e879892cSThomas Huth } 5060e879892cSThomas Huth 50618ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50628ad35755SDavid Hildenbrand { 50638ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 50648e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 50658ad35755SDavid Hildenbrand } 50668ad35755SDavid Hildenbrand 50678ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 50688ad35755SDavid Hildenbrand { 506946808a4cSMarc Zyngier unsigned long i; 50708ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 50718ad35755SDavid Hildenbrand 50728ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 50738ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 50748ad35755SDavid Hildenbrand } 50758ad35755SDavid Hildenbrand } 50768ad35755SDavid Hildenbrand 50778ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50788ad35755SDavid Hildenbrand { 507909a400e7SDavid Hildenbrand if (!sclp.has_ibs) 508009a400e7SDavid Hildenbrand return; 50818ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 50828e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 50838ad35755SDavid Hildenbrand } 50848ad35755SDavid Hildenbrand 5085fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 50866852d7b6SDavid Hildenbrand { 5087fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 50888ad35755SDavid Hildenbrand 50898ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 5090fe28c786SJanosch Frank return 0; 50918ad35755SDavid Hildenbrand 50926852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 50938ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5094433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 50958ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 50968ad35755SDavid Hildenbrand 5097fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 5098fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5099fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 5100fe28c786SJanosch Frank if (r) { 5101fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5102fe28c786SJanosch Frank return r; 5103fe28c786SJanosch Frank } 5104fe28c786SJanosch Frank } 5105fe28c786SJanosch Frank 51068ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5107113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 51088ad35755SDavid Hildenbrand started_vcpus++; 51098ad35755SDavid Hildenbrand } 51108ad35755SDavid Hildenbrand 51118ad35755SDavid Hildenbrand if (started_vcpus == 0) { 51128ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 51138ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 51148ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 51158ad35755SDavid Hildenbrand /* 51168ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 51178ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 511838860756SBhaskar Chowdhury * outstanding ENABLE requests. 51198ad35755SDavid Hildenbrand */ 51208ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 51218ad35755SDavid Hildenbrand } 51228ad35755SDavid Hildenbrand 51239daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 51248ad35755SDavid Hildenbrand /* 512572f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 512672f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 512772f21820SChristian Borntraeger * refresh our view. 512872f21820SChristian Borntraeger */ 512972f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 513072f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 513172f21820SChristian Borntraeger /* 51328ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 51338ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 51348ad35755SDavid Hildenbrand */ 5135d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 5136433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5137fe28c786SJanosch Frank return 0; 51386852d7b6SDavid Hildenbrand } 51396852d7b6SDavid Hildenbrand 5140fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 51416852d7b6SDavid Hildenbrand { 5142fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 51438ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 51448ad35755SDavid Hildenbrand 51458ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 5146fe28c786SJanosch Frank return 0; 51478ad35755SDavid Hildenbrand 51486852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 51498ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5150433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 51518ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 51528ad35755SDavid Hildenbrand 5153fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 5154fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5155fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 5156fe28c786SJanosch Frank if (r) { 5157fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5158fe28c786SJanosch Frank return r; 5159fe28c786SJanosch Frank } 5160fe28c786SJanosch Frank } 5161fe28c786SJanosch Frank 5162812de046SEric Farman /* 5163812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag, 5164812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders 5165812de046SEric Farman * have been fully processed. This will ensure that the VCPU 5166812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE. 5167812de046SEric Farman */ 5168812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 51696cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 517032f5ff63SDavid Hildenbrand 51718ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 51728ad35755SDavid Hildenbrand 51738ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5174113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 5175113d10bcSMarc Zyngier 5176113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 51778ad35755SDavid Hildenbrand started_vcpus++; 5178113d10bcSMarc Zyngier started_vcpu = tmp; 51798ad35755SDavid Hildenbrand } 51808ad35755SDavid Hildenbrand } 51818ad35755SDavid Hildenbrand 51828ad35755SDavid Hildenbrand if (started_vcpus == 1) { 51838ad35755SDavid Hildenbrand /* 51848ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 51858ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 51868ad35755SDavid Hildenbrand */ 51878ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 51888ad35755SDavid Hildenbrand } 51898ad35755SDavid Hildenbrand 5190433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5191fe28c786SJanosch Frank return 0; 51926852d7b6SDavid Hildenbrand } 51936852d7b6SDavid Hildenbrand 5194d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 5195d6712df9SCornelia Huck struct kvm_enable_cap *cap) 5196d6712df9SCornelia Huck { 5197d6712df9SCornelia Huck int r; 5198d6712df9SCornelia Huck 5199d6712df9SCornelia Huck if (cap->flags) 5200d6712df9SCornelia Huck return -EINVAL; 5201d6712df9SCornelia Huck 5202d6712df9SCornelia Huck switch (cap->cap) { 5203fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 5204fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 5205fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 5206c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 5207fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 5208fa6b7fe9SCornelia Huck } 5209fa6b7fe9SCornelia Huck r = 0; 5210fa6b7fe9SCornelia Huck break; 5211d6712df9SCornelia Huck default: 5212d6712df9SCornelia Huck r = -EINVAL; 5213d6712df9SCornelia Huck break; 5214d6712df9SCornelia Huck } 5215d6712df9SCornelia Huck return r; 5216d6712df9SCornelia Huck } 5217d6712df9SCornelia Huck 52180e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu, 521919e12277SJanosch Frank struct kvm_s390_mem_op *mop) 522019e12277SJanosch Frank { 522119e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 5222b99f4512SNico Boehr void *sida_addr; 522319e12277SJanosch Frank int r = 0; 522419e12277SJanosch Frank 522519e12277SJanosch Frank if (mop->flags || !mop->size) 522619e12277SJanosch Frank return -EINVAL; 522719e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 522819e12277SJanosch Frank return -EINVAL; 522919e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 523019e12277SJanosch Frank return -E2BIG; 52312c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu)) 52322c212e1bSJanis Schoetterl-Glausch return -EINVAL; 523319e12277SJanosch Frank 5234b99f4512SNico Boehr sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset; 5235b99f4512SNico Boehr 523619e12277SJanosch Frank switch (mop->op) { 523719e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 5238b99f4512SNico Boehr if (copy_to_user(uaddr, sida_addr, mop->size)) 523919e12277SJanosch Frank r = -EFAULT; 524019e12277SJanosch Frank 524119e12277SJanosch Frank break; 524219e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 5243b99f4512SNico Boehr if (copy_from_user(sida_addr, uaddr, mop->size)) 524419e12277SJanosch Frank r = -EFAULT; 524519e12277SJanosch Frank break; 524619e12277SJanosch Frank } 524719e12277SJanosch Frank return r; 524819e12277SJanosch Frank } 52490e1234c0SJanis Schoetterl-Glausch 52500e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, 525141408c28SThomas Huth struct kvm_s390_mem_op *mop) 525241408c28SThomas Huth { 525341408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 525441408c28SThomas Huth void *tmpbuf = NULL; 5255*a41f505eSJanis Schoetterl-Glausch int r; 525641408c28SThomas Huth 5257*a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION | 5258*a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY | 5259*a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_SKEY_PROTECTION); 5260*a41f505eSJanis Schoetterl-Glausch if (r) 5261*a41f505eSJanis Schoetterl-Glausch return r; 5262*a41f505eSJanis Schoetterl-Glausch if (mop->ar >= NUM_ACRS) 526341408c28SThomas Huth return -EINVAL; 526419e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 526519e12277SJanosch Frank return -EINVAL; 526641408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 526741408c28SThomas Huth tmpbuf = vmalloc(mop->size); 526841408c28SThomas Huth if (!tmpbuf) 526941408c28SThomas Huth return -ENOMEM; 527041408c28SThomas Huth } 527141408c28SThomas Huth 527241408c28SThomas Huth switch (mop->op) { 527341408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 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_FETCH, mop->key); 527741408c28SThomas Huth break; 527841408c28SThomas Huth } 5279e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5280e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 528141408c28SThomas Huth if (r == 0) { 528241408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 528341408c28SThomas Huth r = -EFAULT; 528441408c28SThomas Huth } 528541408c28SThomas Huth break; 528641408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 528741408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 5288e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 5289e9e9feebSJanis Schoetterl-Glausch GACC_STORE, mop->key); 529041408c28SThomas Huth break; 529141408c28SThomas Huth } 529241408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 529341408c28SThomas Huth r = -EFAULT; 529441408c28SThomas Huth break; 529541408c28SThomas Huth } 5296e9e9feebSJanis Schoetterl-Glausch r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5297e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 529841408c28SThomas Huth break; 529941408c28SThomas Huth } 530041408c28SThomas Huth 530141408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 530241408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 530341408c28SThomas Huth 530441408c28SThomas Huth vfree(tmpbuf); 530541408c28SThomas Huth return r; 530641408c28SThomas Huth } 530741408c28SThomas Huth 53080e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu, 530919e12277SJanosch Frank struct kvm_s390_mem_op *mop) 531019e12277SJanosch Frank { 531119e12277SJanosch Frank int r, srcu_idx; 531219e12277SJanosch Frank 531319e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 531419e12277SJanosch Frank 531519e12277SJanosch Frank switch (mop->op) { 531619e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 531719e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 53180e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop); 531919e12277SJanosch Frank break; 532019e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 532119e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 532219e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 53230e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop); 532419e12277SJanosch Frank break; 532519e12277SJanosch Frank default: 532619e12277SJanosch Frank r = -EINVAL; 532719e12277SJanosch Frank } 532819e12277SJanosch Frank 532919e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 533019e12277SJanosch Frank return r; 533119e12277SJanosch Frank } 533219e12277SJanosch Frank 53335cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 5334b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 5335b0c632dbSHeiko Carstens { 5336b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 5337b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 5338b0c632dbSHeiko Carstens 533993736624SAvi Kivity switch (ioctl) { 534047b43c52SJens Freimann case KVM_S390_IRQ: { 534147b43c52SJens Freimann struct kvm_s390_irq s390irq; 534247b43c52SJens Freimann 534347b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 53449b062471SChristoffer Dall return -EFAULT; 53459b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 534647b43c52SJens Freimann } 534793736624SAvi Kivity case KVM_S390_INTERRUPT: { 5348ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 534953936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 5350ba5c1e9bSCarsten Otte 5351ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 53529b062471SChristoffer Dall return -EFAULT; 5353383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 5354383d0b05SJens Freimann return -EINVAL; 53559b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 5356ba5c1e9bSCarsten Otte } 53579b062471SChristoffer Dall } 53585cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 53595cb0944cSPaolo Bonzini } 53605cb0944cSPaolo Bonzini 53618aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu, 53628aba0958SJanosch Frank struct kvm_pv_cmd *cmd) 53638aba0958SJanosch Frank { 53648aba0958SJanosch Frank struct kvm_s390_pv_dmp dmp; 53658aba0958SJanosch Frank void *data; 53668aba0958SJanosch Frank int ret; 53678aba0958SJanosch Frank 53688aba0958SJanosch Frank /* Dump initialization is a prerequisite */ 53698aba0958SJanosch Frank if (!vcpu->kvm->arch.pv.dumping) 53708aba0958SJanosch Frank return -EINVAL; 53718aba0958SJanosch Frank 53728aba0958SJanosch Frank if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp))) 53738aba0958SJanosch Frank return -EFAULT; 53748aba0958SJanosch Frank 53758aba0958SJanosch Frank /* We only handle this subcmd right now */ 53768aba0958SJanosch Frank if (dmp.subcmd != KVM_PV_DUMP_CPU) 53778aba0958SJanosch Frank return -EINVAL; 53788aba0958SJanosch Frank 53798aba0958SJanosch Frank /* CPU dump length is the same as create cpu storage donation. */ 53808aba0958SJanosch Frank if (dmp.buff_len != uv_info.guest_cpu_stor_len) 53818aba0958SJanosch Frank return -EINVAL; 53828aba0958SJanosch Frank 53838aba0958SJanosch Frank data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL); 53848aba0958SJanosch Frank if (!data) 53858aba0958SJanosch Frank return -ENOMEM; 53868aba0958SJanosch Frank 53878aba0958SJanosch Frank ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc); 53888aba0958SJanosch Frank 53898aba0958SJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x", 53908aba0958SJanosch Frank vcpu->vcpu_id, cmd->rc, cmd->rrc); 53918aba0958SJanosch Frank 53928aba0958SJanosch Frank if (ret) 53938aba0958SJanosch Frank ret = -EINVAL; 53948aba0958SJanosch Frank 53958aba0958SJanosch Frank /* On success copy over the dump data */ 53968aba0958SJanosch Frank if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len)) 53978aba0958SJanosch Frank ret = -EFAULT; 53988aba0958SJanosch Frank 53998aba0958SJanosch Frank kvfree(data); 54008aba0958SJanosch Frank return ret; 54018aba0958SJanosch Frank } 54028aba0958SJanosch Frank 54035cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 54045cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 54055cb0944cSPaolo Bonzini { 54065cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 54075cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 54085cb0944cSPaolo Bonzini int idx; 54095cb0944cSPaolo Bonzini long r; 54108a8378faSJanosch Frank u16 rc, rrc; 54119b062471SChristoffer Dall 54129b062471SChristoffer Dall vcpu_load(vcpu); 54139b062471SChristoffer Dall 54149b062471SChristoffer Dall switch (ioctl) { 5415b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 5416800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 541755680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 5418800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 5419bc923cc9SAvi Kivity break; 5420b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 5421b0c632dbSHeiko Carstens psw_t psw; 5422b0c632dbSHeiko Carstens 5423bc923cc9SAvi Kivity r = -EFAULT; 5424b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 5425bc923cc9SAvi Kivity break; 5426bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 5427bc923cc9SAvi Kivity break; 5428b0c632dbSHeiko Carstens } 54297de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 54307de3f142SJanosch Frank r = 0; 54317de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 54328a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54338a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54348a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 54358a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 54368a8378faSJanosch Frank rc, rrc); 54378a8378faSJanosch Frank } 54387de3f142SJanosch Frank break; 5439b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 54407de3f142SJanosch Frank r = 0; 54417de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 54428a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54438a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54448a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 54458a8378faSJanosch Frank &rc, &rrc); 54468a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 54478a8378faSJanosch Frank rc, rrc); 54488a8378faSJanosch Frank } 54497de3f142SJanosch Frank break; 54507de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 54517de3f142SJanosch Frank r = 0; 54527de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 54538a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54548a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54558a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 54568a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 54578a8378faSJanosch Frank rc, rrc); 54588a8378faSJanosch Frank } 5459bc923cc9SAvi Kivity break; 546014eebd91SCarsten Otte case KVM_SET_ONE_REG: 546114eebd91SCarsten Otte case KVM_GET_ONE_REG: { 546214eebd91SCarsten Otte struct kvm_one_reg reg; 546368cf7b1fSJanosch Frank r = -EINVAL; 546468cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 546568cf7b1fSJanosch Frank break; 546614eebd91SCarsten Otte r = -EFAULT; 546714eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 546814eebd91SCarsten Otte break; 546914eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 547014eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 547114eebd91SCarsten Otte else 547214eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 547314eebd91SCarsten Otte break; 547414eebd91SCarsten Otte } 547527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 547627e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 547727e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 547827e0393fSCarsten Otte 547927e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 548027e0393fSCarsten Otte r = -EFAULT; 548127e0393fSCarsten Otte break; 548227e0393fSCarsten Otte } 548327e0393fSCarsten Otte 548427e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 548527e0393fSCarsten Otte r = -EINVAL; 548627e0393fSCarsten Otte break; 548727e0393fSCarsten Otte } 548827e0393fSCarsten Otte 548927e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 549027e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 549127e0393fSCarsten Otte break; 549227e0393fSCarsten Otte } 549327e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 549427e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 549527e0393fSCarsten Otte 549627e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 549727e0393fSCarsten Otte r = -EFAULT; 549827e0393fSCarsten Otte break; 549927e0393fSCarsten Otte } 550027e0393fSCarsten Otte 550127e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 550227e0393fSCarsten Otte r = -EINVAL; 550327e0393fSCarsten Otte break; 550427e0393fSCarsten Otte } 550527e0393fSCarsten Otte 550627e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 550727e0393fSCarsten Otte ucasmap.length); 550827e0393fSCarsten Otte break; 550927e0393fSCarsten Otte } 551027e0393fSCarsten Otte #endif 5511ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 5512527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 5513ccc7910fSCarsten Otte break; 5514ccc7910fSCarsten Otte } 5515d6712df9SCornelia Huck case KVM_ENABLE_CAP: 5516d6712df9SCornelia Huck { 5517d6712df9SCornelia Huck struct kvm_enable_cap cap; 5518d6712df9SCornelia Huck r = -EFAULT; 5519d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 5520d6712df9SCornelia Huck break; 5521d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 5522d6712df9SCornelia Huck break; 5523d6712df9SCornelia Huck } 552441408c28SThomas Huth case KVM_S390_MEM_OP: { 552541408c28SThomas Huth struct kvm_s390_mem_op mem_op; 552641408c28SThomas Huth 552741408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 55280e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op); 552941408c28SThomas Huth else 553041408c28SThomas Huth r = -EFAULT; 553141408c28SThomas Huth break; 553241408c28SThomas Huth } 5533816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 5534816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5535816c7667SJens Freimann 5536816c7667SJens Freimann r = -EFAULT; 5537816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5538816c7667SJens Freimann break; 5539816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 5540816c7667SJens Freimann irq_state.len == 0 || 5541816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 5542816c7667SJens Freimann r = -EINVAL; 5543816c7667SJens Freimann break; 5544816c7667SJens Freimann } 5545bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5546816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 5547816c7667SJens Freimann (void __user *) irq_state.buf, 5548816c7667SJens Freimann irq_state.len); 5549816c7667SJens Freimann break; 5550816c7667SJens Freimann } 5551816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 5552816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5553816c7667SJens Freimann 5554816c7667SJens Freimann r = -EFAULT; 5555816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5556816c7667SJens Freimann break; 5557816c7667SJens Freimann if (irq_state.len == 0) { 5558816c7667SJens Freimann r = -EINVAL; 5559816c7667SJens Freimann break; 5560816c7667SJens Freimann } 5561bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5562816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 5563816c7667SJens Freimann (__u8 __user *) irq_state.buf, 5564816c7667SJens Freimann irq_state.len); 5565816c7667SJens Freimann break; 5566816c7667SJens Freimann } 55678aba0958SJanosch Frank case KVM_S390_PV_CPU_COMMAND: { 55688aba0958SJanosch Frank struct kvm_pv_cmd cmd; 55698aba0958SJanosch Frank 55708aba0958SJanosch Frank r = -EINVAL; 55718aba0958SJanosch Frank if (!is_prot_virt_host()) 55728aba0958SJanosch Frank break; 55738aba0958SJanosch Frank 55748aba0958SJanosch Frank r = -EFAULT; 55758aba0958SJanosch Frank if (copy_from_user(&cmd, argp, sizeof(cmd))) 55768aba0958SJanosch Frank break; 55778aba0958SJanosch Frank 55788aba0958SJanosch Frank r = -EINVAL; 55798aba0958SJanosch Frank if (cmd.flags) 55808aba0958SJanosch Frank break; 55818aba0958SJanosch Frank 55828aba0958SJanosch Frank /* We only handle this cmd right now */ 55838aba0958SJanosch Frank if (cmd.cmd != KVM_PV_DUMP) 55848aba0958SJanosch Frank break; 55858aba0958SJanosch Frank 55868aba0958SJanosch Frank r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd); 55878aba0958SJanosch Frank 55888aba0958SJanosch Frank /* Always copy over UV rc / rrc data */ 55898aba0958SJanosch Frank if (copy_to_user((__u8 __user *)argp, &cmd.rc, 55908aba0958SJanosch Frank sizeof(cmd.rc) + sizeof(cmd.rrc))) 55918aba0958SJanosch Frank r = -EFAULT; 55928aba0958SJanosch Frank break; 55938aba0958SJanosch Frank } 5594b0c632dbSHeiko Carstens default: 55953e6afcf1SCarsten Otte r = -ENOTTY; 5596b0c632dbSHeiko Carstens } 55979b062471SChristoffer Dall 55989b062471SChristoffer Dall vcpu_put(vcpu); 5599bc923cc9SAvi Kivity return r; 5600b0c632dbSHeiko Carstens } 5601b0c632dbSHeiko Carstens 56021499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 56035b1c1493SCarsten Otte { 56045b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 56055b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 56065b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 56075b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 56085b1c1493SCarsten Otte get_page(vmf->page); 56095b1c1493SCarsten Otte return 0; 56105b1c1493SCarsten Otte } 56115b1c1493SCarsten Otte #endif 56125b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 56135b1c1493SCarsten Otte } 56145b1c1493SCarsten Otte 5615d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 5616d663b8a2SPaolo Bonzini { 5617d663b8a2SPaolo Bonzini return true; 5618d663b8a2SPaolo Bonzini } 5619d663b8a2SPaolo Bonzini 5620b0c632dbSHeiko Carstens /* Section: memory related */ 5621f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5622537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5623537a17b3SSean Christopherson struct kvm_memory_slot *new, 56247b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5625b0c632dbSHeiko Carstens { 5626ec5c8697SSean Christopherson gpa_t size; 5627ec5c8697SSean Christopherson 5628ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5629ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5630ec5c8697SSean Christopherson return -EINVAL; 5631ec5c8697SSean Christopherson 5632f2d3155eSNico Boehr if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) { 5633f2d3155eSNico Boehr /* 5634f2d3155eSNico Boehr * A few sanity checks. We can have memory slots which have to be 5635f2d3155eSNico Boehr * located/ended at a segment boundary (1MB). The memory in userland is 5636f2d3155eSNico Boehr * ok to be fragmented into various different vmas. It is okay to mmap() 5637f2d3155eSNico Boehr * and munmap() stuff in this slot after doing this call at any time 5638f2d3155eSNico Boehr */ 5639b0c632dbSHeiko Carstens 5640cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5641b0c632dbSHeiko Carstens return -EINVAL; 5642b0c632dbSHeiko Carstens 5643ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5644cf5b4869SSean Christopherson if (size & 0xffffful) 5645b0c632dbSHeiko Carstens return -EINVAL; 5646b0c632dbSHeiko Carstens 5647cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5648a3a92c31SDominik Dingel return -EINVAL; 5649f2d3155eSNico Boehr } 5650f2d3155eSNico Boehr 5651f2d3155eSNico Boehr if (!kvm->arch.migration_mode) 5652f2d3155eSNico Boehr return 0; 5653f2d3155eSNico Boehr 5654f2d3155eSNico Boehr /* 5655f2d3155eSNico Boehr * Turn off migration mode when: 5656f2d3155eSNico Boehr * - userspace creates a new memslot with dirty logging off, 5657f2d3155eSNico Boehr * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and 5658f2d3155eSNico Boehr * dirty logging is turned off. 5659f2d3155eSNico Boehr * Migration mode expects dirty page logging being enabled to store 5660f2d3155eSNico Boehr * its dirty bitmap. 5661f2d3155eSNico Boehr */ 5662f2d3155eSNico Boehr if (change != KVM_MR_DELETE && 5663f2d3155eSNico Boehr !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) 5664f2d3155eSNico Boehr WARN(kvm_s390_vm_stop_migration(kvm), 5665f2d3155eSNico Boehr "Failed to stop migration mode"); 5666a3a92c31SDominik Dingel 5667f7784b8eSMarcelo Tosatti return 0; 5668f7784b8eSMarcelo Tosatti } 5669f7784b8eSMarcelo Tosatti 5670f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 56719d4c197cSSean Christopherson struct kvm_memory_slot *old, 5672f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 56738482644aSTakuya Yoshikawa enum kvm_mr_change change) 5674f7784b8eSMarcelo Tosatti { 567519ec166cSChristian Borntraeger int rc = 0; 5676f7784b8eSMarcelo Tosatti 567719ec166cSChristian Borntraeger switch (change) { 567819ec166cSChristian Borntraeger case KVM_MR_DELETE: 567919ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 568019ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 568119ec166cSChristian Borntraeger break; 568219ec166cSChristian Borntraeger case KVM_MR_MOVE: 568319ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 568419ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 568519ec166cSChristian Borntraeger if (rc) 568619ec166cSChristian Borntraeger break; 56873b684a42SJoe Perches fallthrough; 568819ec166cSChristian Borntraeger case KVM_MR_CREATE: 5689cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5690cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5691cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 569219ec166cSChristian Borntraeger break; 569319ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 569419ec166cSChristian Borntraeger break; 569519ec166cSChristian Borntraeger default: 569619ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 569719ec166cSChristian Borntraeger } 5698598841caSCarsten Otte if (rc) 5699ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5700598841caSCarsten Otte return; 5701b0c632dbSHeiko Carstens } 5702b0c632dbSHeiko Carstens 570360a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 570460a37709SAlexander Yarygin { 570560a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 570660a37709SAlexander Yarygin 570760a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 570860a37709SAlexander Yarygin } 570960a37709SAlexander Yarygin 5710b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5711b0c632dbSHeiko Carstens { 571260a37709SAlexander Yarygin int i; 571360a37709SAlexander Yarygin 571407197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 57158d43d570SMichael Mueller pr_info("SIE is not available\n"); 571607197fd0SDavid Hildenbrand return -ENODEV; 571707197fd0SDavid Hildenbrand } 571807197fd0SDavid Hildenbrand 5719a4499382SJanosch Frank if (nested && hpage) { 57208d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5721a4499382SJanosch Frank return -EINVAL; 5722a4499382SJanosch Frank } 5723a4499382SJanosch Frank 572460a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5725c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 572617e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 572760a37709SAlexander Yarygin 57289d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5729b0c632dbSHeiko Carstens } 5730b0c632dbSHeiko Carstens 5731b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5732b0c632dbSHeiko Carstens { 5733b0c632dbSHeiko Carstens kvm_exit(); 5734b0c632dbSHeiko Carstens } 5735b0c632dbSHeiko Carstens 5736b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5737b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5738566af940SCornelia Huck 5739566af940SCornelia Huck /* 5740566af940SCornelia Huck * Enable autoloading of the kvm module. 5741566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5742566af940SCornelia Huck * since x86 takes a different approach. 5743566af940SCornelia Huck */ 5744566af940SCornelia Huck #include <linux/miscdevice.h> 5745566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5746566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5747