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 2767a41f505eSJanis Schoetterl-Glausch static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags) 2768e9e9feebSJanis Schoetterl-Glausch { 2769a41f505eSJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size) 2770a41f505eSJanis Schoetterl-Glausch return -EINVAL; 2771a41f505eSJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE) 2772a41f505eSJanis Schoetterl-Glausch return -E2BIG; 2773a41f505eSJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 2774a41f505eSJanis Schoetterl-Glausch if (mop->key > 0xf) 2775a41f505eSJanis Schoetterl-Glausch return -EINVAL; 2776a41f505eSJanis Schoetterl-Glausch } else { 2777a41f505eSJanis Schoetterl-Glausch mop->key = 0; 2778a41f505eSJanis Schoetterl-Glausch } 2779a41f505eSJanis Schoetterl-Glausch return 0; 2780e9e9feebSJanis Schoetterl-Glausch } 2781e9e9feebSJanis Schoetterl-Glausch 27828550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop) 2783ef11c946SJanis Schoetterl-Glausch { 2784ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 27850d6d4d23SJanis Schoetterl-Glausch enum gacc_mode acc_mode; 2786ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL; 2787ef11c946SJanis Schoetterl-Glausch int r, srcu_idx; 2788ef11c946SJanis Schoetterl-Glausch 2789a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION | 2790a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY); 2791a41f505eSJanis Schoetterl-Glausch if (r) 2792a41f505eSJanis Schoetterl-Glausch return r; 2793a41f505eSJanis Schoetterl-Glausch 2794ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 2795ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size); 2796ef11c946SJanis Schoetterl-Glausch if (!tmpbuf) 2797ef11c946SJanis Schoetterl-Glausch return -ENOMEM; 2798ef11c946SJanis Schoetterl-Glausch } 2799ef11c946SJanis Schoetterl-Glausch 2800ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 2801ef11c946SJanis Schoetterl-Glausch 2802ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 2803ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 2804ef11c946SJanis Schoetterl-Glausch goto out_unlock; 2805ef11c946SJanis Schoetterl-Glausch } 2806ef11c946SJanis Schoetterl-Glausch 28070d6d4d23SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE; 2808ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 28090d6d4d23SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key); 28100d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 28110d6d4d23SJanis Schoetterl-Glausch } 28120d6d4d23SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) { 2813ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2814ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key); 28150d6d4d23SJanis Schoetterl-Glausch if (r) 28160d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 2817ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) 2818ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2819ef11c946SJanis Schoetterl-Glausch } else { 2820ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) { 2821ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 28220d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 2823ef11c946SJanis Schoetterl-Glausch } 2824ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2825ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key); 2826ef11c946SJanis Schoetterl-Glausch } 2827ef11c946SJanis Schoetterl-Glausch 2828ef11c946SJanis Schoetterl-Glausch out_unlock: 2829ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 2830ef11c946SJanis Schoetterl-Glausch 2831ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf); 2832ef11c946SJanis Schoetterl-Glausch return r; 2833ef11c946SJanis Schoetterl-Glausch } 2834ef11c946SJanis Schoetterl-Glausch 28358550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) 28368550bcb7SJanis Schoetterl-Glausch { 28378550bcb7SJanis Schoetterl-Glausch /* 28388550bcb7SJanis Schoetterl-Glausch * This is technically a heuristic only, if the kvm->lock is not 28398550bcb7SJanis Schoetterl-Glausch * taken, it is not guaranteed that the vm is/remains non-protected. 28408550bcb7SJanis Schoetterl-Glausch * This is ok from a kernel perspective, wrongdoing is detected 28418550bcb7SJanis Schoetterl-Glausch * on the access, -EFAULT is returned and the vm may crash the 28428550bcb7SJanis Schoetterl-Glausch * next time it accesses the memory in question. 28438550bcb7SJanis Schoetterl-Glausch * There is no sane usecase to do switching and a memop on two 28448550bcb7SJanis Schoetterl-Glausch * different CPUs at the same time. 28458550bcb7SJanis Schoetterl-Glausch */ 28468550bcb7SJanis Schoetterl-Glausch if (kvm_s390_pv_get_handle(kvm)) 28478550bcb7SJanis Schoetterl-Glausch return -EINVAL; 28488550bcb7SJanis Schoetterl-Glausch 28498550bcb7SJanis Schoetterl-Glausch switch (mop->op) { 28508550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ: 28518550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE: 28528550bcb7SJanis Schoetterl-Glausch return kvm_s390_vm_mem_op_abs(kvm, mop); 28538550bcb7SJanis Schoetterl-Glausch default: 28548550bcb7SJanis Schoetterl-Glausch return -EINVAL; 28558550bcb7SJanis Schoetterl-Glausch } 28568550bcb7SJanis Schoetterl-Glausch } 28578550bcb7SJanis Schoetterl-Glausch 2858b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2859b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2860b0c632dbSHeiko Carstens { 2861b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2862b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2863f2061656SDominik Dingel struct kvm_device_attr attr; 2864b0c632dbSHeiko Carstens int r; 2865b0c632dbSHeiko Carstens 2866b0c632dbSHeiko Carstens switch (ioctl) { 2867ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2868ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2869ba5c1e9bSCarsten Otte 2870ba5c1e9bSCarsten Otte r = -EFAULT; 2871ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2872ba5c1e9bSCarsten Otte break; 2873ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2874ba5c1e9bSCarsten Otte break; 2875ba5c1e9bSCarsten Otte } 287684223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 287784223598SCornelia Huck struct kvm_irq_routing_entry routing; 287884223598SCornelia Huck 287984223598SCornelia Huck r = -EINVAL; 288084223598SCornelia Huck if (kvm->arch.use_irqchip) { 288184223598SCornelia Huck /* Set up dummy routing. */ 288284223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2883152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 288484223598SCornelia Huck } 288584223598SCornelia Huck break; 288684223598SCornelia Huck } 2887f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2888f2061656SDominik Dingel r = -EFAULT; 2889f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2890f2061656SDominik Dingel break; 2891f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2892f2061656SDominik Dingel break; 2893f2061656SDominik Dingel } 2894f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2895f2061656SDominik Dingel r = -EFAULT; 2896f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2897f2061656SDominik Dingel break; 2898f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2899f2061656SDominik Dingel break; 2900f2061656SDominik Dingel } 2901f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2902f2061656SDominik Dingel r = -EFAULT; 2903f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2904f2061656SDominik Dingel break; 2905f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2906f2061656SDominik Dingel break; 2907f2061656SDominik Dingel } 290830ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 290930ee2a98SJason J. Herne struct kvm_s390_skeys args; 291030ee2a98SJason J. Herne 291130ee2a98SJason J. Herne r = -EFAULT; 291230ee2a98SJason J. Herne if (copy_from_user(&args, argp, 291330ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 291430ee2a98SJason J. Herne break; 291530ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 291630ee2a98SJason J. Herne break; 291730ee2a98SJason J. Herne } 291830ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 291930ee2a98SJason J. Herne struct kvm_s390_skeys args; 292030ee2a98SJason J. Herne 292130ee2a98SJason J. Herne r = -EFAULT; 292230ee2a98SJason J. Herne if (copy_from_user(&args, argp, 292330ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 292430ee2a98SJason J. Herne break; 292530ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 292630ee2a98SJason J. Herne break; 292730ee2a98SJason J. Herne } 29284036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 29294036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29304036e387SClaudio Imbrenda 29314036e387SClaudio Imbrenda r = -EFAULT; 29324036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29334036e387SClaudio Imbrenda break; 29341de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29354036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 29361de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29374036e387SClaudio Imbrenda if (!r) { 29384036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 29394036e387SClaudio Imbrenda if (r) 29404036e387SClaudio Imbrenda r = -EFAULT; 29414036e387SClaudio Imbrenda } 29424036e387SClaudio Imbrenda break; 29434036e387SClaudio Imbrenda } 29444036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 29454036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29464036e387SClaudio Imbrenda 29474036e387SClaudio Imbrenda r = -EFAULT; 29484036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29494036e387SClaudio Imbrenda break; 29501de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29514036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 29521de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29534036e387SClaudio Imbrenda break; 29544036e387SClaudio Imbrenda } 295529b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 295629b40f10SJanosch Frank struct kvm_pv_cmd args; 295729b40f10SJanosch Frank 295867cf68b6SEric Farman /* protvirt means user cpu state */ 295967cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 296029b40f10SJanosch Frank r = 0; 296129b40f10SJanosch Frank if (!is_prot_virt_host()) { 296229b40f10SJanosch Frank r = -EINVAL; 296329b40f10SJanosch Frank break; 296429b40f10SJanosch Frank } 296529b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 296629b40f10SJanosch Frank r = -EFAULT; 296729b40f10SJanosch Frank break; 296829b40f10SJanosch Frank } 296929b40f10SJanosch Frank if (args.flags) { 297029b40f10SJanosch Frank r = -EINVAL; 297129b40f10SJanosch Frank break; 297229b40f10SJanosch Frank } 2973fb491d55SClaudio Imbrenda /* must be called without kvm->lock */ 297429b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 297529b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 297629b40f10SJanosch Frank r = -EFAULT; 297729b40f10SJanosch Frank break; 297829b40f10SJanosch Frank } 297929b40f10SJanosch Frank break; 298029b40f10SJanosch Frank } 2981ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: { 2982ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op; 2983ef11c946SJanis Schoetterl-Glausch 2984ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 2985ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op); 2986ef11c946SJanis Schoetterl-Glausch else 2987ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2988ef11c946SJanis Schoetterl-Glausch break; 2989ef11c946SJanis Schoetterl-Glausch } 2990db1c875eSMatthew Rosato case KVM_S390_ZPCI_OP: { 2991db1c875eSMatthew Rosato struct kvm_s390_zpci_op args; 2992db1c875eSMatthew Rosato 2993db1c875eSMatthew Rosato r = -EINVAL; 2994db1c875eSMatthew Rosato if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 2995db1c875eSMatthew Rosato break; 2996db1c875eSMatthew Rosato if (copy_from_user(&args, argp, sizeof(args))) { 2997db1c875eSMatthew Rosato r = -EFAULT; 2998db1c875eSMatthew Rosato break; 2999db1c875eSMatthew Rosato } 3000db1c875eSMatthew Rosato r = kvm_s390_pci_zpci_op(kvm, &args); 3001db1c875eSMatthew Rosato break; 3002db1c875eSMatthew Rosato } 3003b0c632dbSHeiko Carstens default: 3004367e1319SAvi Kivity r = -ENOTTY; 3005b0c632dbSHeiko Carstens } 3006b0c632dbSHeiko Carstens 3007b0c632dbSHeiko Carstens return r; 3008b0c632dbSHeiko Carstens } 3009b0c632dbSHeiko Carstens 301045c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 301145c9b47cSTony Krowiak { 3012e585b24aSTony Krowiak struct ap_config_info info; 301345c9b47cSTony Krowiak 3014e585b24aSTony Krowiak if (ap_instructions_available()) { 3015e585b24aSTony Krowiak if (ap_qci(&info) == 0) 3016e585b24aSTony Krowiak return info.apxa; 301745c9b47cSTony Krowiak } 301845c9b47cSTony Krowiak 301945c9b47cSTony Krowiak return 0; 302045c9b47cSTony Krowiak } 302145c9b47cSTony Krowiak 3022e585b24aSTony Krowiak /* 3023e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 3024e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 3025e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 3026e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 3027e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 3028e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 3029e585b24aSTony Krowiak */ 303045c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 303145c9b47cSTony Krowiak { 303245c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 303345c9b47cSTony Krowiak 3034e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 3035e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 3036e585b24aSTony Krowiak 3037e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 3038e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3039e585b24aSTony Krowiak return; 3040e585b24aSTony Krowiak 304145c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 304245c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 304345c9b47cSTony Krowiak else 304445c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 304545c9b47cSTony Krowiak } 304645c9b47cSTony Krowiak 304786956e70STony Krowiak /* 304886956e70STony Krowiak * kvm_arch_crypto_set_masks 304986956e70STony Krowiak * 305086956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 305186956e70STony Krowiak * to be set. 305286956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 305386956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 305486956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 305586956e70STony Krowiak * 305686956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 305786956e70STony Krowiak * which the KVM guest is granted access. 305886956e70STony Krowiak * 305986956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 306086956e70STony Krowiak * function. 306186956e70STony Krowiak */ 30620e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 30630e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 30640e237e44SPierre Morel { 30650e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 30660e237e44SPierre Morel 30670e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 30680e237e44SPierre Morel 30690e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 30700e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 30710e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 30720e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 30730e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 30740e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 30750e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 30760e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 30770e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 30780e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 30790e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 30800e237e44SPierre Morel break; 30810e237e44SPierre Morel case CRYCB_FORMAT1: 30820e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 30830e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 30840e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 30850e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 30860e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 30870e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 30880e237e44SPierre Morel *((unsigned short *)adm)); 30890e237e44SPierre Morel break; 30900e237e44SPierre Morel default: /* Can not happen */ 30910e237e44SPierre Morel break; 30920e237e44SPierre Morel } 30930e237e44SPierre Morel 30940e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 30950e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 30960e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 30970e237e44SPierre Morel } 30980e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 30990e237e44SPierre Morel 310086956e70STony Krowiak /* 310186956e70STony Krowiak * kvm_arch_crypto_clear_masks 310286956e70STony Krowiak * 310386956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 310486956e70STony Krowiak * to be cleared. 310586956e70STony Krowiak * 310686956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 310786956e70STony Krowiak * which the KVM guest is granted access. 310886956e70STony Krowiak * 310986956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 311086956e70STony Krowiak * function. 311186956e70STony Krowiak */ 311242104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 311342104598STony Krowiak { 311442104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 311542104598STony Krowiak 311642104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 311742104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 311842104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 311942104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 312042104598STony Krowiak 31210e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 31226cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 31236cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 312442104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 312542104598STony Krowiak } 312642104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 312742104598STony Krowiak 31289bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 31299d8d5786SMichael Mueller { 31309bb0ec09SDavid Hildenbrand struct cpuid cpuid; 31319bb0ec09SDavid Hildenbrand 31329bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 31339bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 31349bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 31359d8d5786SMichael Mueller } 31369d8d5786SMichael Mueller 3137c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 31385102ee87STony Krowiak { 3139c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 314045c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 31411e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 31425102ee87STony Krowiak 3143e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3144e585b24aSTony Krowiak return; 3145e585b24aSTony Krowiak 3146ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 3147ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 3148ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 3149ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 3150ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 3151ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 3152ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 31535102ee87STony Krowiak } 31545102ee87STony Krowiak 31557d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 31567d43bafcSEugene (jno) Dvurechenski { 31577d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 31585e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 31597d43bafcSEugene (jno) Dvurechenski else 31607d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 31617d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 31627d43bafcSEugene (jno) Dvurechenski } 31637d43bafcSEugene (jno) Dvurechenski 316409340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm) 316509340b2fSMatthew Rosato { 316609340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 316709340b2fSMatthew Rosato kvm_s390_pci_clear_list(kvm); 316809340b2fSMatthew Rosato 316909340b2fSMatthew Rosato __kvm_arch_free_vm(kvm); 317009340b2fSMatthew Rosato } 317109340b2fSMatthew Rosato 3172e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 3173b0c632dbSHeiko Carstens { 3174c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 31759d8d5786SMichael Mueller int i, rc; 3176b0c632dbSHeiko Carstens char debug_name[16]; 3177f6c137ffSChristian Borntraeger static unsigned long sca_offset; 3178b0c632dbSHeiko Carstens 3179e08b9637SCarsten Otte rc = -EINVAL; 3180e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3181e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 3182e08b9637SCarsten Otte goto out_err; 3183e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 3184e08b9637SCarsten Otte goto out_err; 3185e08b9637SCarsten Otte #else 3186e08b9637SCarsten Otte if (type) 3187e08b9637SCarsten Otte goto out_err; 3188e08b9637SCarsten Otte #endif 3189e08b9637SCarsten Otte 3190b0c632dbSHeiko Carstens rc = s390_enable_sie(); 3191b0c632dbSHeiko Carstens if (rc) 3192d89f5effSJan Kiszka goto out_err; 3193b0c632dbSHeiko Carstens 3194b290411aSCarsten Otte rc = -ENOMEM; 3195b290411aSCarsten Otte 319676a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 319776a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 31985e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 31999ac96d75SDavid Hildenbrand /* start with basic SCA */ 320076a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 3201b0c632dbSHeiko Carstens if (!kvm->arch.sca) 3202d89f5effSJan Kiszka goto out_err; 32030d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 3204c5c2c393SDavid Hildenbrand sca_offset += 16; 3205bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 3206c5c2c393SDavid Hildenbrand sca_offset = 0; 3207bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 3208bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 32090d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 3210b0c632dbSHeiko Carstens 3211b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 3212b0c632dbSHeiko Carstens 32131cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 3214b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 321540f5b735SDominik Dingel goto out_err; 3216b0c632dbSHeiko Carstens 321719114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 3218c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 3219c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 3220c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 322140f5b735SDominik Dingel goto out_err; 32229d8d5786SMichael Mueller 322325c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 3224c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 3225c3b9e3e1SChristian Borntraeger 3226c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 322717e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 3228c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 3229c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 323017e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 3231c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 3232c3b9e3e1SChristian Borntraeger } 3233346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 3234981467c9SMichael Mueller 32351935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 32361935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 32371935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 32381935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 323995ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 324095ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 32411bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 32421bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 32431bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 32441bab1c02SClaudio Imbrenda } 324595ca2cb5SJanosch Frank 324605f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 324705f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 324805f31e3bSPierre Morel 32499bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 325037c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 32519d8d5786SMichael Mueller 3252c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 32535102ee87STony Krowiak 325409340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 325509340b2fSMatthew Rosato mutex_lock(&kvm->lock); 325609340b2fSMatthew Rosato kvm_s390_pci_init_list(kvm); 325709340b2fSMatthew Rosato kvm_s390_vcpu_pci_enable_interp(kvm); 325809340b2fSMatthew Rosato mutex_unlock(&kvm->lock); 325909340b2fSMatthew Rosato } 326009340b2fSMatthew Rosato 326151978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 3262ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 32636d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 32646d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 32658a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 3266a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 3267ba5c1e9bSCarsten Otte 3268b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 326978f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 3270b0c632dbSHeiko Carstens 3271e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 3272e08b9637SCarsten Otte kvm->arch.gmap = NULL; 3273a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 3274e08b9637SCarsten Otte } else { 327532e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 3276ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 327732e6b236SGuenther Hutzl else 3278ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 327932e6b236SGuenther Hutzl sclp.hamax + 1); 32806ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 3281598841caSCarsten Otte if (!kvm->arch.gmap) 328240f5b735SDominik Dingel goto out_err; 32832c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 328424eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 3285e08b9637SCarsten Otte } 3286fa6b7fe9SCornelia Huck 3287c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 328855531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 32898ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 3290a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 3291cc674ef2SMichael Mueller if (use_gisa) 3292d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 3293fb491d55SClaudio Imbrenda INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup); 3294fb491d55SClaudio Imbrenda kvm->arch.pv.set_aside = NULL; 32958335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 32968ad35755SDavid Hildenbrand 3297d89f5effSJan Kiszka return 0; 3298d89f5effSJan Kiszka out_err: 3299c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 330040f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 33017d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 330278f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 3303d89f5effSJan Kiszka return rc; 3304b0c632dbSHeiko Carstens } 3305b0c632dbSHeiko Carstens 3306d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 3307d329c035SChristian Borntraeger { 330829b40f10SJanosch Frank u16 rc, rrc; 330929b40f10SJanosch Frank 3310d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 3311ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 331267335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 33133c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 3314bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 3315a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 331624fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 331727e0393fSCarsten Otte 331827e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 33196ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 332027e0393fSCarsten Otte 3321e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 3322b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 332329b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 332429b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 332529b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 3326d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 3327d329c035SChristian Borntraeger } 3328d329c035SChristian Borntraeger 3329b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 3330b0c632dbSHeiko Carstens { 333129b40f10SJanosch Frank u16 rc, rrc; 333229b40f10SJanosch Frank 333327592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 33347d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 3335d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 333629b40f10SJanosch Frank /* 333729b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 333829b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 3339fb491d55SClaudio Imbrenda * can mess with the pv state. 334029b40f10SJanosch Frank */ 3341fb491d55SClaudio Imbrenda kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc); 3342ca2fd060SClaudio Imbrenda /* 3343ca2fd060SClaudio Imbrenda * Remove the mmu notifier only when the whole KVM VM is torn down, 3344ca2fd060SClaudio Imbrenda * and only if one was registered to begin with. If the VM is 3345ca2fd060SClaudio Imbrenda * currently not protected, but has been previously been protected, 3346ca2fd060SClaudio Imbrenda * then it's possible that the notifier is still registered. 3347ca2fd060SClaudio Imbrenda */ 3348ca2fd060SClaudio Imbrenda if (kvm->arch.pv.mmu_notifier.ops) 3349ca2fd060SClaudio Imbrenda mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm); 3350ca2fd060SClaudio Imbrenda 335129b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 3352c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 335327e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 33546ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 3355841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 335667335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 3357a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 33588335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 3359b0c632dbSHeiko Carstens } 3360b0c632dbSHeiko Carstens 3361b0c632dbSHeiko Carstens /* Section: vcpu related */ 3362dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 3363b0c632dbSHeiko Carstens { 33646ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 336527e0393fSCarsten Otte if (!vcpu->arch.gmap) 336627e0393fSCarsten Otte return -ENOMEM; 33672c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 3368dafd032aSDominik Dingel 336927e0393fSCarsten Otte return 0; 337027e0393fSCarsten Otte } 337127e0393fSCarsten Otte 3372a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 3373a6e2f683SEugene (jno) Dvurechenski { 3374a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 3375a6940674SDavid Hildenbrand return; 33765e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 33777d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 33787d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 33797d43bafcSEugene (jno) Dvurechenski 33807d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 33817d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 33827d43bafcSEugene (jno) Dvurechenski } else { 3383bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 3384a6e2f683SEugene (jno) Dvurechenski 3385a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3386a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 3387a6e2f683SEugene (jno) Dvurechenski } 33885e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 33897d43bafcSEugene (jno) Dvurechenski } 3390a6e2f683SEugene (jno) Dvurechenski 3391eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 3392a6e2f683SEugene (jno) Dvurechenski { 3393a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3394fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca); 3395a6940674SDavid Hildenbrand 3396a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 3397fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3398fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3399f07afa04SDavid Hildenbrand return; 3400a6940674SDavid Hildenbrand } 3401eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 3402eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 3403eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 3404fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 34057d43bafcSEugene (jno) Dvurechenski 3406fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3407fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3408fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK; 34090c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 3410eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 34117d43bafcSEugene (jno) Dvurechenski } else { 3412eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 3413fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 3414a6e2f683SEugene (jno) Dvurechenski 3415fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3416fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3417fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3418eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3419a6e2f683SEugene (jno) Dvurechenski } 3420eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 34215e044315SEugene (jno) Dvurechenski } 34225e044315SEugene (jno) Dvurechenski 34235e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 34245e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 34255e044315SEugene (jno) Dvurechenski { 34265e044315SEugene (jno) Dvurechenski d->sda = s->sda; 34275e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 34285e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 34295e044315SEugene (jno) Dvurechenski } 34305e044315SEugene (jno) Dvurechenski 34315e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 34325e044315SEugene (jno) Dvurechenski { 34335e044315SEugene (jno) Dvurechenski int i; 34345e044315SEugene (jno) Dvurechenski 34355e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 34365e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 34375e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 34385e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 34395e044315SEugene (jno) Dvurechenski } 34405e044315SEugene (jno) Dvurechenski 34415e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 34425e044315SEugene (jno) Dvurechenski { 34435e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 34445e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 34455e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 344646808a4cSMarc Zyngier unsigned long vcpu_idx; 34475e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 3448fe0ef003SNico Boehr phys_addr_t new_sca_phys; 34495e044315SEugene (jno) Dvurechenski 345029b40f10SJanosch Frank if (kvm->arch.use_esca) 345129b40f10SJanosch Frank return 0; 345229b40f10SJanosch Frank 3453c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 34545e044315SEugene (jno) Dvurechenski if (!new_sca) 34555e044315SEugene (jno) Dvurechenski return -ENOMEM; 34565e044315SEugene (jno) Dvurechenski 3457fe0ef003SNico Boehr new_sca_phys = virt_to_phys(new_sca); 3458fe0ef003SNico Boehr scaoh = new_sca_phys >> 32; 3459fe0ef003SNico Boehr scaol = new_sca_phys & ESCA_SCAOL_MASK; 34605e044315SEugene (jno) Dvurechenski 34615e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 34625e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 34635e044315SEugene (jno) Dvurechenski 34645e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 34655e044315SEugene (jno) Dvurechenski 34665e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 34675e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 34685e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 34690c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 34705e044315SEugene (jno) Dvurechenski } 34715e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 34725e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 34735e044315SEugene (jno) Dvurechenski 34745e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 34755e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 34765e044315SEugene (jno) Dvurechenski 34775e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 34785e044315SEugene (jno) Dvurechenski 34798335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 34808335713aSChristian Borntraeger old_sca, kvm->arch.sca); 34815e044315SEugene (jno) Dvurechenski return 0; 34827d43bafcSEugene (jno) Dvurechenski } 3483a6e2f683SEugene (jno) Dvurechenski 3484a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 3485a6e2f683SEugene (jno) Dvurechenski { 34865e044315SEugene (jno) Dvurechenski int rc; 34875e044315SEugene (jno) Dvurechenski 3488a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3489a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 3490a6940674SDavid Hildenbrand return true; 3491a6940674SDavid Hildenbrand return false; 3492a6940674SDavid Hildenbrand } 34935e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 34945e044315SEugene (jno) Dvurechenski return true; 349576a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 34965e044315SEugene (jno) Dvurechenski return false; 34975e044315SEugene (jno) Dvurechenski 34985e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 34995e044315SEugene (jno) Dvurechenski 35005e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 3501a6e2f683SEugene (jno) Dvurechenski } 3502a6e2f683SEugene (jno) Dvurechenski 3503db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3504db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3505db0758b2SDavid Hildenbrand { 3506db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 35079c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3508db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35099c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3510db0758b2SDavid Hildenbrand } 3511db0758b2SDavid Hildenbrand 3512db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3513db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3514db0758b2SDavid Hildenbrand { 3515db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 35169c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3517db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3518db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 35199c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3520db0758b2SDavid Hildenbrand } 3521db0758b2SDavid Hildenbrand 3522db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3523db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3524db0758b2SDavid Hildenbrand { 3525db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3526db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3527db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3528db0758b2SDavid Hildenbrand } 3529db0758b2SDavid Hildenbrand 3530db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3531db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3532db0758b2SDavid Hildenbrand { 3533db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3534db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3535db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3536db0758b2SDavid Hildenbrand } 3537db0758b2SDavid Hildenbrand 3538db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3539db0758b2SDavid Hildenbrand { 3540db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3541db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3542db0758b2SDavid Hildenbrand preempt_enable(); 3543db0758b2SDavid Hildenbrand } 3544db0758b2SDavid Hildenbrand 3545db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3546db0758b2SDavid Hildenbrand { 3547db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3548db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3549db0758b2SDavid Hildenbrand preempt_enable(); 3550db0758b2SDavid Hildenbrand } 3551db0758b2SDavid Hildenbrand 35524287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 35534287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 35544287f247SDavid Hildenbrand { 3555db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35569c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3557db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3558db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35594287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 35609c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3561db0758b2SDavid Hildenbrand preempt_enable(); 35624287f247SDavid Hildenbrand } 35634287f247SDavid Hildenbrand 3564db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 35654287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 35664287f247SDavid Hildenbrand { 35679c23a131SDavid Hildenbrand unsigned int seq; 3568db0758b2SDavid Hildenbrand __u64 value; 3569db0758b2SDavid Hildenbrand 3570db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 35714287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3572db0758b2SDavid Hildenbrand 35739c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35749c23a131SDavid Hildenbrand do { 35759c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 35769c23a131SDavid Hildenbrand /* 35779c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 35789c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 35799c23a131SDavid Hildenbrand */ 35809c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3581db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 35829c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 35839c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3584db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 35859c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 35869c23a131SDavid Hildenbrand preempt_enable(); 3587db0758b2SDavid Hildenbrand return value; 35884287f247SDavid Hildenbrand } 35894287f247SDavid Hildenbrand 3590b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3591b0c632dbSHeiko Carstens { 35929977e886SHendrik Brueckner 359337d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3594ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 35955ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3596db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 359701a745acSDavid Hildenbrand vcpu->cpu = cpu; 3598b0c632dbSHeiko Carstens } 3599b0c632dbSHeiko Carstens 3600b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3601b0c632dbSHeiko Carstens { 360201a745acSDavid Hildenbrand vcpu->cpu = -1; 36035ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3604db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 36059daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 360637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 360737d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 36089977e886SHendrik Brueckner 3609b0c632dbSHeiko Carstens } 3610b0c632dbSHeiko Carstens 361131928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 361242897d86SMarcelo Tosatti { 361372f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3614fdf03650SFan Zhang preempt_disable(); 361572f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3616d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3617fdf03650SFan Zhang preempt_enable(); 361872f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 361925508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3620dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3621eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 362225508824SDavid Hildenbrand } 36236502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 36246502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 362537d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 362637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 362742897d86SMarcelo Tosatti } 362842897d86SMarcelo Tosatti 36298ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 36308ec2fa52SChristian Borntraeger { 36318ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 36328ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 36338ec2fa52SChristian Borntraeger return true; 36348ec2fa52SChristian Borntraeger return false; 36358ec2fa52SChristian Borntraeger } 36368ec2fa52SChristian Borntraeger 36378ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 36388ec2fa52SChristian Borntraeger { 36398ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 36408ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 36418ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 36428ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 36438ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 36448ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 36458ec2fa52SChristian Borntraeger 36468ec2fa52SChristian Borntraeger } 36478ec2fa52SChristian Borntraeger 36485102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 36495102ee87STony Krowiak { 3650e585b24aSTony Krowiak /* 3651e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3652e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3653e585b24aSTony Krowiak */ 3654e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 36555102ee87STony Krowiak return; 36565102ee87STony Krowiak 3657e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3658a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 365937940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 36608ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3661a374e892STony Krowiak 3662e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3663e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3664e585b24aSTony Krowiak 3665e585b24aSTony Krowiak /* Set up protected key support */ 36668ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3667a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 36688ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 36698ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 36708ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 36718ec2fa52SChristian Borntraeger } 36728ec2fa52SChristian Borntraeger 3673a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3674a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 36755102ee87STony Krowiak } 36765102ee87STony Krowiak 3677b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3678b31605c1SDominik Dingel { 3679fe0ef003SNico Boehr free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo)); 3680b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3681b31605c1SDominik Dingel } 3682b31605c1SDominik Dingel 3683b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3684b31605c1SDominik Dingel { 3685fe0ef003SNico Boehr void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); 3686fe0ef003SNico Boehr 3687fe0ef003SNico Boehr if (!cbrlo_page) 3688b31605c1SDominik Dingel return -ENOMEM; 3689fe0ef003SNico Boehr 3690fe0ef003SNico Boehr vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page); 3691b31605c1SDominik Dingel return 0; 3692b31605c1SDominik Dingel } 3693b31605c1SDominik Dingel 369491520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 369591520f1aSMichael Mueller { 369691520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 369791520f1aSMichael Mueller 369891520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 369980bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3700fe0ef003SNico Boehr vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list); 370191520f1aSMichael Mueller } 370291520f1aSMichael Mueller 3703ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3704ff72bb55SSean Christopherson { 3705b31605c1SDominik Dingel int rc = 0; 370629b40f10SJanosch Frank u16 uvrc, uvrrc; 3707b31288faSKonstantin Weitz 37089e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 37099e6dabefSCornelia Huck CPUSTAT_SM | 3710a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3711a4a4f191SGuenther Hutzl 371253df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3713ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 371453df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3715ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3716a4a4f191SGuenther Hutzl 371791520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 371891520f1aSMichael Mueller 3719bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3720bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 37210c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3722bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 37230c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 372424fe0195SPierre Morel if (test_kvm_facility(vcpu->kvm, 11)) 372524fe0195SPierre Morel vcpu->arch.sie_block->ecb |= ECB_PTF; 3726f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 37270c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 37287119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 37297119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 37307feb6bb8SMichael Mueller 3731c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 37320c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3733cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 37340c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 37350c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 373648ee7d3aSDavid Hildenbrand if (sclp.has_cei) 37370c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 373811ad65b7SDavid Hildenbrand if (sclp.has_ib) 37390c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 374037c5f6c8SDavid Hildenbrand if (sclp.has_siif) 37410c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 374237c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 37430c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 374418280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 37450c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 37460c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 374713211ea7SEric Farman } 37488fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 37498fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3750a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3751a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3752d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3753d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3754d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3755d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3756d7c5cb01SMichael Mueller } 3757fe0ef003SNico Boehr vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC; 3758fe0ef003SNico Boehr vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb); 3759730cd632SFarhan Ali 3760730cd632SFarhan Ali if (sclp.has_kss) 3761ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3762730cd632SFarhan Ali else 3763492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 37645a5e6536SMatthew Rosato 3765e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3766b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3767b31605c1SDominik Dingel if (rc) 3768b31605c1SDominik Dingel return rc; 3769b31288faSKonstantin Weitz } 37700ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3771ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 37729d8d5786SMichael Mueller 377367d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 377467d49d52SCollin Walling 37755102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 37765102ee87STony Krowiak 37773f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 37783f4bbb43SMatthew Rosato 377929b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 378029b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 378129b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 378229b40f10SJanosch Frank if (rc) 378329b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 378429b40f10SJanosch Frank } 378529b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 378629b40f10SJanosch Frank 3787b31605c1SDominik Dingel return rc; 3788b0c632dbSHeiko Carstens } 3789b0c632dbSHeiko Carstens 3790897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3791897cc38eSSean Christopherson { 3792897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3793897cc38eSSean Christopherson return -EINVAL; 3794897cc38eSSean Christopherson return 0; 3795897cc38eSSean Christopherson } 3796897cc38eSSean Christopherson 3797e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3798b0c632dbSHeiko Carstens { 37997feb6bb8SMichael Mueller struct sie_page *sie_page; 3800897cc38eSSean Christopherson int rc; 38014d47555aSCarsten Otte 3802da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3803c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 38047feb6bb8SMichael Mueller if (!sie_page) 3805e529ef66SSean Christopherson return -ENOMEM; 3806b0c632dbSHeiko Carstens 38077feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 3808fe0ef003SNico Boehr vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb); 38097feb6bb8SMichael Mueller 3810efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3811efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3812efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3813efed1104SDavid Hildenbrand 3814e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3815ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3816ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm); 38179c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3818ba5c1e9bSCarsten Otte 3819321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3820321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3821321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3822321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3823321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3824321f8ee5SSean Christopherson KVM_SYNC_CRS | 3825321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 382623a60f83SCollin Walling KVM_SYNC_PFAULT | 382723a60f83SCollin Walling KVM_SYNC_DIAG318; 3828321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3829321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3830321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3831321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3832321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3833321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3834321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3835321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3836321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3837321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3838321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3839321f8ee5SSean Christopherson */ 3840321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3841321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3842321f8ee5SSean Christopherson else 3843321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3844321f8ee5SSean Christopherson 3845321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3846321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3847321f8ee5SSean Christopherson if (rc) 3848a2017f17SSean Christopherson goto out_free_sie_block; 3849321f8ee5SSean Christopherson } 3850321f8ee5SSean Christopherson 3851e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3852e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3853e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3854b0c632dbSHeiko Carstens 3855ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3856ff72bb55SSean Christopherson if (rc) 3857ff72bb55SSean Christopherson goto out_ucontrol_uninit; 385824fe0195SPierre Morel 385924fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 3860e529ef66SSean Christopherson return 0; 3861e529ef66SSean Christopherson 3862ff72bb55SSean Christopherson out_ucontrol_uninit: 3863ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3864ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 38657b06bf2fSWei Yongjun out_free_sie_block: 38667b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3867e529ef66SSean Christopherson return rc; 3868b0c632dbSHeiko Carstens } 3869b0c632dbSHeiko Carstens 3870b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3871b0c632dbSHeiko Carstens { 38729b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 38739a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3874b0c632dbSHeiko Carstens } 3875b0c632dbSHeiko Carstens 3876199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3877199b5763SLongpeng(Mike) { 38780546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3879199b5763SLongpeng(Mike) } 3880199b5763SLongpeng(Mike) 388127406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 388249b99e1eSChristian Borntraeger { 3883805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 388461a6df54SDavid Hildenbrand exit_sie(vcpu); 388549b99e1eSChristian Borntraeger } 388649b99e1eSChristian Borntraeger 388727406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 388849b99e1eSChristian Borntraeger { 3889805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 389049b99e1eSChristian Borntraeger } 389149b99e1eSChristian Borntraeger 38928e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 38938e236546SChristian Borntraeger { 3894805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 389561a6df54SDavid Hildenbrand exit_sie(vcpu); 38968e236546SChristian Borntraeger } 38978e236546SChristian Borntraeger 38989ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 38999ea59728SDavid Hildenbrand { 39009ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 39019ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 39029ea59728SDavid Hildenbrand } 39039ea59728SDavid Hildenbrand 39048e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 39058e236546SChristian Borntraeger { 39069bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 39078e236546SChristian Borntraeger } 39088e236546SChristian Borntraeger 390949b99e1eSChristian Borntraeger /* 39109ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 391149b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 391249b99e1eSChristian Borntraeger * return immediately. */ 391349b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 391449b99e1eSChristian Borntraeger { 3915ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 39169ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 391749b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 391849b99e1eSChristian Borntraeger cpu_relax(); 391949b99e1eSChristian Borntraeger } 392049b99e1eSChristian Borntraeger 39218e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 39228e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 392349b99e1eSChristian Borntraeger { 3924df06dae3SSean Christopherson __kvm_make_request(req, vcpu); 39258e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 392649b99e1eSChristian Borntraeger } 392749b99e1eSChristian Borntraeger 3928414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3929414d3b07SMartin Schwidefsky unsigned long end) 39302c70fe44SChristian Borntraeger { 39312c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 39322c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3933414d3b07SMartin Schwidefsky unsigned long prefix; 393446808a4cSMarc Zyngier unsigned long i; 39352c70fe44SChristian Borntraeger 393665d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 393765d0b0d4SDavid Hildenbrand return; 3938414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3939414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3940414d3b07SMartin Schwidefsky return; 39412c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 39422c70fe44SChristian Borntraeger /* match against both prefix pages */ 3943414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3944414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3945414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3946414d3b07SMartin Schwidefsky start, end); 3947cc65c3a1SSean Christopherson kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 39482c70fe44SChristian Borntraeger } 39492c70fe44SChristian Borntraeger } 39502c70fe44SChristian Borntraeger } 39512c70fe44SChristian Borntraeger 39528b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 39538b905d28SChristian Borntraeger { 39548b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 39558b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 39566f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) { 39578b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 39588b905d28SChristian Borntraeger return true; 39598b905d28SChristian Borntraeger } 39608b905d28SChristian Borntraeger return false; 39618b905d28SChristian Borntraeger } 39628b905d28SChristian Borntraeger 3963b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3964b6d33834SChristoffer Dall { 3965b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3966b6d33834SChristoffer Dall BUG(); 3967b6d33834SChristoffer Dall return 0; 3968b6d33834SChristoffer Dall } 3969b6d33834SChristoffer Dall 397014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 397114eebd91SCarsten Otte struct kvm_one_reg *reg) 397214eebd91SCarsten Otte { 397314eebd91SCarsten Otte int r = -EINVAL; 397414eebd91SCarsten Otte 397514eebd91SCarsten Otte switch (reg->id) { 397629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 397729b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 397829b7c71bSCarsten Otte (u32 __user *)reg->addr); 397929b7c71bSCarsten Otte break; 398029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 398129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 398229b7c71bSCarsten Otte (u64 __user *)reg->addr); 398329b7c71bSCarsten Otte break; 398446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 39854287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 398646a6dd1cSJason J. herne (u64 __user *)reg->addr); 398746a6dd1cSJason J. herne break; 398846a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 398946a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 399046a6dd1cSJason J. herne (u64 __user *)reg->addr); 399146a6dd1cSJason J. herne break; 3992536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3993536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3994536336c2SDominik Dingel (u64 __user *)reg->addr); 3995536336c2SDominik Dingel break; 3996536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3997536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3998536336c2SDominik Dingel (u64 __user *)reg->addr); 3999536336c2SDominik Dingel break; 4000536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4001536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 4002536336c2SDominik Dingel (u64 __user *)reg->addr); 4003536336c2SDominik Dingel break; 4004672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4005672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 4006672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4007672550fbSChristian Borntraeger break; 4008afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4009afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 4010afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4011afa45ff5SChristian Borntraeger break; 401214eebd91SCarsten Otte default: 401314eebd91SCarsten Otte break; 401414eebd91SCarsten Otte } 401514eebd91SCarsten Otte 401614eebd91SCarsten Otte return r; 401714eebd91SCarsten Otte } 401814eebd91SCarsten Otte 401914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 402014eebd91SCarsten Otte struct kvm_one_reg *reg) 402114eebd91SCarsten Otte { 402214eebd91SCarsten Otte int r = -EINVAL; 40234287f247SDavid Hildenbrand __u64 val; 402414eebd91SCarsten Otte 402514eebd91SCarsten Otte switch (reg->id) { 402629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 402729b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 402829b7c71bSCarsten Otte (u32 __user *)reg->addr); 402929b7c71bSCarsten Otte break; 403029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 403129b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 403229b7c71bSCarsten Otte (u64 __user *)reg->addr); 403329b7c71bSCarsten Otte break; 403446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 40354287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 40364287f247SDavid Hildenbrand if (!r) 40374287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 403846a6dd1cSJason J. herne break; 403946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 404046a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 404146a6dd1cSJason J. herne (u64 __user *)reg->addr); 404246a6dd1cSJason J. herne break; 4043536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 4044536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 4045536336c2SDominik Dingel (u64 __user *)reg->addr); 40469fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 40479fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4048536336c2SDominik Dingel break; 4049536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 4050536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 4051536336c2SDominik Dingel (u64 __user *)reg->addr); 4052536336c2SDominik Dingel break; 4053536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4054536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 4055536336c2SDominik Dingel (u64 __user *)reg->addr); 4056536336c2SDominik Dingel break; 4057672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4058672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 4059672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4060672550fbSChristian Borntraeger break; 4061afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4062afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 4063afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4064afa45ff5SChristian Borntraeger break; 406514eebd91SCarsten Otte default: 406614eebd91SCarsten Otte break; 406714eebd91SCarsten Otte } 406814eebd91SCarsten Otte 406914eebd91SCarsten Otte return r; 407014eebd91SCarsten Otte } 4071b6d33834SChristoffer Dall 40727de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 4073b0c632dbSHeiko Carstens { 40747de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 40757de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 40767de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 40777de3f142SJanosch Frank 40787de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 40797de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 40807de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 40817de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 40827de3f142SJanosch Frank } 40837de3f142SJanosch Frank 40847de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 40857de3f142SJanosch Frank { 40867de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 40877de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 40887de3f142SJanosch Frank 4089e93fc7b4SChristian Borntraeger /* 4090e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 4091e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 4092e93fc7b4SChristian Borntraeger */ 40937de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 40947de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 40957de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 40967de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 40977de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 40987de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 40997de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 41007de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 4101e93fc7b4SChristian Borntraeger 4102e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 4103e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 4104e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4105e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 4106e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 4107e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 4108e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 4109e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 4110e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 4111e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4112e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 4113e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 41147de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 41150f303504SJanosch Frank /* 41160f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 41170f303504SJanosch Frank * them are overlayed and they are not accessible in this case 41180f303504SJanosch Frank * anyway. 41190f303504SJanosch Frank */ 41200f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 41217de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 41227de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 41237de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 41240f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 41250f303504SJanosch Frank } 41267de3f142SJanosch Frank } 41277de3f142SJanosch Frank 41287de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 41297de3f142SJanosch Frank { 41307de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 41317de3f142SJanosch Frank 41327de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 41337de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 41347de3f142SJanosch Frank 41357de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 41367de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 41377de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 41387de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 41397de3f142SJanosch Frank 41407de3f142SJanosch Frank regs->etoken = 0; 41417de3f142SJanosch Frank regs->etoken_extension = 0; 4142b0c632dbSHeiko Carstens } 4143b0c632dbSHeiko Carstens 4144b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4145b0c632dbSHeiko Carstens { 4146875656feSChristoffer Dall vcpu_load(vcpu); 41475a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 4148875656feSChristoffer Dall vcpu_put(vcpu); 4149b0c632dbSHeiko Carstens return 0; 4150b0c632dbSHeiko Carstens } 4151b0c632dbSHeiko Carstens 4152b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4153b0c632dbSHeiko Carstens { 41541fc9b76bSChristoffer Dall vcpu_load(vcpu); 41555a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 41561fc9b76bSChristoffer Dall vcpu_put(vcpu); 4157b0c632dbSHeiko Carstens return 0; 4158b0c632dbSHeiko Carstens } 4159b0c632dbSHeiko Carstens 4160b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 4161b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4162b0c632dbSHeiko Carstens { 4163b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 4164b4ef9d4eSChristoffer Dall 416559674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 4166b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 4167b4ef9d4eSChristoffer Dall 4168b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 4169b0c632dbSHeiko Carstens return 0; 4170b0c632dbSHeiko Carstens } 4171b0c632dbSHeiko Carstens 4172b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 4173b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4174b0c632dbSHeiko Carstens { 4175bcdec41cSChristoffer Dall vcpu_load(vcpu); 4176bcdec41cSChristoffer Dall 417759674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 4178b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 4179bcdec41cSChristoffer Dall 4180bcdec41cSChristoffer Dall vcpu_put(vcpu); 4181b0c632dbSHeiko Carstens return 0; 4182b0c632dbSHeiko Carstens } 4183b0c632dbSHeiko Carstens 4184b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4185b0c632dbSHeiko Carstens { 41866a96bc7fSChristoffer Dall int ret = 0; 41876a96bc7fSChristoffer Dall 41886a96bc7fSChristoffer Dall vcpu_load(vcpu); 41896a96bc7fSChristoffer Dall 41906a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 41916a96bc7fSChristoffer Dall ret = -EINVAL; 41926a96bc7fSChristoffer Dall goto out; 41936a96bc7fSChristoffer Dall } 4194e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 41959abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4196a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 4197a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 41989abc2a08SDavid Hildenbrand else 4199a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 42006a96bc7fSChristoffer Dall 42016a96bc7fSChristoffer Dall out: 42026a96bc7fSChristoffer Dall vcpu_put(vcpu); 42036a96bc7fSChristoffer Dall return ret; 4204b0c632dbSHeiko Carstens } 4205b0c632dbSHeiko Carstens 4206b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4207b0c632dbSHeiko Carstens { 42081393123eSChristoffer Dall vcpu_load(vcpu); 42091393123eSChristoffer Dall 42109abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 42119abc2a08SDavid Hildenbrand save_fpu_regs(); 42129abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4213a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 4214a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 42159abc2a08SDavid Hildenbrand else 4216a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 4217e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 42181393123eSChristoffer Dall 42191393123eSChristoffer Dall vcpu_put(vcpu); 4220b0c632dbSHeiko Carstens return 0; 4221b0c632dbSHeiko Carstens } 4222b0c632dbSHeiko Carstens 4223b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 4224b0c632dbSHeiko Carstens { 4225b0c632dbSHeiko Carstens int rc = 0; 4226b0c632dbSHeiko Carstens 42277a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4228b0c632dbSHeiko Carstens rc = -EBUSY; 4229d7b0b5ebSCarsten Otte else { 4230d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 4231d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 4232d7b0b5ebSCarsten Otte } 4233b0c632dbSHeiko Carstens return rc; 4234b0c632dbSHeiko Carstens } 4235b0c632dbSHeiko Carstens 4236b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 4237b0c632dbSHeiko Carstens struct kvm_translation *tr) 4238b0c632dbSHeiko Carstens { 4239b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 4240b0c632dbSHeiko Carstens } 4241b0c632dbSHeiko Carstens 424227291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 424327291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 424427291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 424527291e21SDavid Hildenbrand 4246d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 4247d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 4248b0c632dbSHeiko Carstens { 424927291e21SDavid Hildenbrand int rc = 0; 425027291e21SDavid Hildenbrand 425166b56562SChristoffer Dall vcpu_load(vcpu); 425266b56562SChristoffer Dall 425327291e21SDavid Hildenbrand vcpu->guest_debug = 0; 425427291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 425527291e21SDavid Hildenbrand 425666b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 425766b56562SChristoffer Dall rc = -EINVAL; 425866b56562SChristoffer Dall goto out; 425966b56562SChristoffer Dall } 426066b56562SChristoffer Dall if (!sclp.has_gpere) { 426166b56562SChristoffer Dall rc = -EINVAL; 426266b56562SChristoffer Dall goto out; 426366b56562SChristoffer Dall } 426427291e21SDavid Hildenbrand 426527291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 426627291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 426727291e21SDavid Hildenbrand /* enforce guest PER */ 4268ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 426927291e21SDavid Hildenbrand 427027291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 427127291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 427227291e21SDavid Hildenbrand } else { 42739daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 427427291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 427527291e21SDavid Hildenbrand } 427627291e21SDavid Hildenbrand 427727291e21SDavid Hildenbrand if (rc) { 427827291e21SDavid Hildenbrand vcpu->guest_debug = 0; 427927291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 42809daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 428127291e21SDavid Hildenbrand } 428227291e21SDavid Hildenbrand 428366b56562SChristoffer Dall out: 428466b56562SChristoffer Dall vcpu_put(vcpu); 428527291e21SDavid Hildenbrand return rc; 4286b0c632dbSHeiko Carstens } 4287b0c632dbSHeiko Carstens 428862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 428962d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 429062d9f0dbSMarcelo Tosatti { 4291fd232561SChristoffer Dall int ret; 4292fd232561SChristoffer Dall 4293fd232561SChristoffer Dall vcpu_load(vcpu); 4294fd232561SChristoffer Dall 42956352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 4296fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 42976352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 4298fd232561SChristoffer Dall 4299fd232561SChristoffer Dall vcpu_put(vcpu); 4300fd232561SChristoffer Dall return ret; 430162d9f0dbSMarcelo Tosatti } 430262d9f0dbSMarcelo Tosatti 430362d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 430462d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 430562d9f0dbSMarcelo Tosatti { 43066352e4d2SDavid Hildenbrand int rc = 0; 43076352e4d2SDavid Hildenbrand 4308e83dff5eSChristoffer Dall vcpu_load(vcpu); 4309e83dff5eSChristoffer Dall 43106352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 431167cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 43126352e4d2SDavid Hildenbrand 43136352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 43146352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 4315fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 43166352e4d2SDavid Hildenbrand break; 43176352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 4318fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 43196352e4d2SDavid Hildenbrand break; 43206352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 43217c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 43227c36a3fcSJanosch Frank rc = -ENXIO; 43237c36a3fcSJanosch Frank break; 43247c36a3fcSJanosch Frank } 43257c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 43267c36a3fcSJanosch Frank break; 43276352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 43283b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 43296352e4d2SDavid Hildenbrand default: 43306352e4d2SDavid Hildenbrand rc = -ENXIO; 43316352e4d2SDavid Hildenbrand } 43326352e4d2SDavid Hildenbrand 4333e83dff5eSChristoffer Dall vcpu_put(vcpu); 43346352e4d2SDavid Hildenbrand return rc; 433562d9f0dbSMarcelo Tosatti } 433662d9f0dbSMarcelo Tosatti 43378ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 43388ad35755SDavid Hildenbrand { 43398d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 43408ad35755SDavid Hildenbrand } 43418ad35755SDavid Hildenbrand 43422c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 43432c70fe44SChristian Borntraeger { 43448ad35755SDavid Hildenbrand retry: 43458e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 43462fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 4347586b7ccdSChristian Borntraeger return 0; 43482c70fe44SChristian Borntraeger /* 4349cc65c3a1SSean Christopherson * If the guest prefix changed, re-arm the ipte notifier for the 4350b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 43512c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 43522c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 43532c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 43542c70fe44SChristian Borntraeger */ 4355cc65c3a1SSean Christopherson if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) { 43562c70fe44SChristian Borntraeger int rc; 4357b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 4358fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 4359b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 4360aca411a4SJulius Niedworok if (rc) { 4361cc65c3a1SSean Christopherson kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 43622c70fe44SChristian Borntraeger return rc; 4363aca411a4SJulius Niedworok } 43648ad35755SDavid Hildenbrand goto retry; 43652c70fe44SChristian Borntraeger } 43668ad35755SDavid Hildenbrand 4367d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 4368d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 4369d3d692c8SDavid Hildenbrand goto retry; 4370d3d692c8SDavid Hildenbrand } 4371d3d692c8SDavid Hildenbrand 43728ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 43738ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 43748ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 4375ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 43768ad35755SDavid Hildenbrand } 43778ad35755SDavid Hildenbrand goto retry; 43788ad35755SDavid Hildenbrand } 43798ad35755SDavid Hildenbrand 43808ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 43818ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 43828ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 43839daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 43848ad35755SDavid Hildenbrand } 43858ad35755SDavid Hildenbrand goto retry; 43868ad35755SDavid Hildenbrand } 43878ad35755SDavid Hildenbrand 43886502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 43896502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 43906502a34cSDavid Hildenbrand goto retry; 43916502a34cSDavid Hildenbrand } 43926502a34cSDavid Hildenbrand 4393190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 4394190df4a2SClaudio Imbrenda /* 4395c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 4396190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 4397190df4a2SClaudio Imbrenda * functionalities needed for live migration. 4398190df4a2SClaudio Imbrenda */ 4399190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 4400190df4a2SClaudio Imbrenda goto retry; 4401190df4a2SClaudio Imbrenda } 4402190df4a2SClaudio Imbrenda 4403190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 4404190df4a2SClaudio Imbrenda /* 4405c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 4406c9f0a2b8SJanosch Frank * CMM has been used. 4407190df4a2SClaudio Imbrenda */ 4408190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 4409c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 4410190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 4411190df4a2SClaudio Imbrenda goto retry; 4412190df4a2SClaudio Imbrenda } 4413190df4a2SClaudio Imbrenda 44143194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 44153194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 44160759d068SDavid Hildenbrand 44172c70fe44SChristian Borntraeger return 0; 44182c70fe44SChristian Borntraeger } 44192c70fe44SChristian Borntraeger 4420c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 44218fa1696eSCollin L. Walling { 44228fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 44232cfd7b73SHeiko Carstens union tod_clock clk; 442446808a4cSMarc Zyngier unsigned long i; 44258fa1696eSCollin L. Walling 44268fa1696eSCollin L. Walling preempt_disable(); 44278fa1696eSCollin L. Walling 44282cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 44298fa1696eSCollin L. Walling 44302cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 44310e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 44320e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 44332cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 44348fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 44358fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 44360e7def5fSDavid Hildenbrand } 44378fa1696eSCollin L. Walling 44388fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 44398fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 44408fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 44418fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 44428fa1696eSCollin L. Walling } 44438fa1696eSCollin L. Walling 44448fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 44458fa1696eSCollin L. Walling preempt_enable(); 4446c0573ba5SClaudio Imbrenda } 4447c0573ba5SClaudio Imbrenda 4448c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4449c0573ba5SClaudio Imbrenda { 4450c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock)) 4451c0573ba5SClaudio Imbrenda return 0; 4452c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 4453c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock); 4454c0573ba5SClaudio Imbrenda return 1; 4455c0573ba5SClaudio Imbrenda } 4456c0573ba5SClaudio Imbrenda 4457fa576c58SThomas Huth /** 4458fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 4459fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 4460fa576c58SThomas Huth * @gpa: Guest physical address 4461fa576c58SThomas Huth * @writable: Whether the page should be writable or not 4462fa576c58SThomas Huth * 4463fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 4464fa576c58SThomas Huth * 4465fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 4466fa576c58SThomas Huth */ 4467fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 446824eb3a82SDominik Dingel { 4469527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 4470527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 447124eb3a82SDominik Dingel } 447224eb3a82SDominik Dingel 44733c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 44743c038e6bSDominik Dingel unsigned long token) 44753c038e6bSDominik Dingel { 44763c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 4477383d0b05SJens Freimann struct kvm_s390_irq irq; 44783c038e6bSDominik Dingel 44793c038e6bSDominik Dingel if (start_token) { 4480383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 4481383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 4482383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 44833c038e6bSDominik Dingel } else { 44843c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 4485383d0b05SJens Freimann inti.parm64 = token; 44863c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 44873c038e6bSDominik Dingel } 44883c038e6bSDominik Dingel } 44893c038e6bSDominik Dingel 44902a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 44913c038e6bSDominik Dingel struct kvm_async_pf *work) 44923c038e6bSDominik Dingel { 44933c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 44943c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 44952a18b7e7SVitaly Kuznetsov 44962a18b7e7SVitaly Kuznetsov return true; 44973c038e6bSDominik Dingel } 44983c038e6bSDominik Dingel 44993c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 45003c038e6bSDominik Dingel struct kvm_async_pf *work) 45013c038e6bSDominik Dingel { 45023c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 45033c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 45043c038e6bSDominik Dingel } 45053c038e6bSDominik Dingel 45063c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 45073c038e6bSDominik Dingel struct kvm_async_pf *work) 45083c038e6bSDominik Dingel { 45093c038e6bSDominik Dingel /* s390 will always inject the page directly */ 45103c038e6bSDominik Dingel } 45113c038e6bSDominik Dingel 45127c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 45133c038e6bSDominik Dingel { 45143c038e6bSDominik Dingel /* 45153c038e6bSDominik Dingel * s390 will always inject the page directly, 45163c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 45173c038e6bSDominik Dingel */ 45183c038e6bSDominik Dingel return true; 45193c038e6bSDominik Dingel } 45203c038e6bSDominik Dingel 4521e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 45223c038e6bSDominik Dingel { 45233c038e6bSDominik Dingel hva_t hva; 45243c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 45253c038e6bSDominik Dingel 45263c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4527e8c22266SVitaly Kuznetsov return false; 45283c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 45293c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4530e8c22266SVitaly Kuznetsov return false; 45313c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4532e8c22266SVitaly Kuznetsov return false; 45339a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4534e8c22266SVitaly Kuznetsov return false; 4535b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4536e8c22266SVitaly Kuznetsov return false; 45373c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4538e8c22266SVitaly Kuznetsov return false; 45393c038e6bSDominik Dingel 454081480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 454181480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 454281480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4543e8c22266SVitaly Kuznetsov return false; 45443c038e6bSDominik Dingel 4545e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 45463c038e6bSDominik Dingel } 45473c038e6bSDominik Dingel 45483fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4549b0c632dbSHeiko Carstens { 45503fb4c40fSThomas Huth int rc, cpuflags; 4551e168bf8dSCarsten Otte 45523c038e6bSDominik Dingel /* 45533c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 45543c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 45553c038e6bSDominik Dingel * handled outside the worker. 45563c038e6bSDominik Dingel */ 45573c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 45583c038e6bSDominik Dingel 45597ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 45607ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4561b0c632dbSHeiko Carstens 4562b0c632dbSHeiko Carstens if (need_resched()) 4563b0c632dbSHeiko Carstens schedule(); 4564b0c632dbSHeiko Carstens 456579395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 456679395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 456779395031SJens Freimann if (rc) 456879395031SJens Freimann return rc; 456979395031SJens Freimann } 45700ff31867SCarsten Otte 45712c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 45722c70fe44SChristian Borntraeger if (rc) 45732c70fe44SChristian Borntraeger return rc; 45742c70fe44SChristian Borntraeger 457527291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 457627291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 457727291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 457827291e21SDavid Hildenbrand } 457927291e21SDavid Hildenbrand 45804eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 45819f30f621SMichael Mueller 4582b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 45833fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 45843fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 45853fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 45862b29a9fdSDominik Dingel 45873fb4c40fSThomas Huth return 0; 45883fb4c40fSThomas Huth } 45893fb4c40fSThomas Huth 4590492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4591492d8642SThomas Huth { 459256317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 459356317920SDavid Hildenbrand .code = PGM_ADDRESSING, 459456317920SDavid Hildenbrand }; 459556317920SDavid Hildenbrand u8 opcode, ilen; 4596492d8642SThomas Huth int rc; 4597492d8642SThomas Huth 4598492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4599492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4600492d8642SThomas Huth 4601492d8642SThomas Huth /* 4602492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4603492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4604492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4605492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4606492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4607492d8642SThomas Huth * to be able to forward the PSW. 4608492d8642SThomas Huth */ 46093fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 461056317920SDavid Hildenbrand ilen = insn_length(opcode); 46119b0d721aSDavid Hildenbrand if (rc < 0) { 46129b0d721aSDavid Hildenbrand return rc; 46139b0d721aSDavid Hildenbrand } else if (rc) { 46149b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 46159b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 46169b0d721aSDavid Hildenbrand * nullification if necessary. 46179b0d721aSDavid Hildenbrand */ 46189b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 46199b0d721aSDavid Hildenbrand ilen = 4; 46209b0d721aSDavid Hildenbrand } 462156317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 462256317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 462356317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4624492d8642SThomas Huth } 4625492d8642SThomas Huth 46263fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 46273fb4c40fSThomas Huth { 46284d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 46294d62fcc0SQingFeng Hao struct sie_page *sie_page; 46304d62fcc0SQingFeng Hao 46312b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 46322b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 46332b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 46342b29a9fdSDominik Dingel 463527291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 463627291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 463727291e21SDavid Hildenbrand 46387ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 46397ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 464071f116bfSDavid Hildenbrand 46414d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 46424d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 46434d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 46444d62fcc0SQingFeng Hao struct sie_page, sie_block); 46454d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 46464d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 46474d62fcc0SQingFeng Hao return 0; 46484d62fcc0SQingFeng Hao } 46494d62fcc0SQingFeng Hao 465071f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 465171f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 465271f116bfSDavid Hildenbrand 465371f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 465471f116bfSDavid Hildenbrand return rc; 465571f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 465671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 465771f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 465871f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 465971f116bfSDavid Hildenbrand return -EREMOTE; 466071f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 466171f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 466271f116bfSDavid Hildenbrand return 0; 4663210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4664210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4665210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4666210b1607SThomas Huth current->thread.gmap_addr; 4667210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 466871f116bfSDavid Hildenbrand return -EREMOTE; 466924eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 46703c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 467124eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 467271f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 467371f116bfSDavid Hildenbrand return 0; 467450a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 467571f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4676fa576c58SThomas Huth } 467771f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 46783fb4c40fSThomas Huth } 46793fb4c40fSThomas Huth 46803adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 46813fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 46823fb4c40fSThomas Huth { 46833fb4c40fSThomas Huth int rc, exit_reason; 4684c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 46853fb4c40fSThomas Huth 4686800c1065SThomas Huth /* 4687800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4688800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4689800c1065SThomas Huth */ 46902031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 4691800c1065SThomas Huth 4692a76ccff6SThomas Huth do { 46933fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 46943fb4c40fSThomas Huth if (rc) 4695a76ccff6SThomas Huth break; 46963fb4c40fSThomas Huth 46972031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 46983fb4c40fSThomas Huth /* 4699a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4700a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 47013fb4c40fSThomas Huth */ 47020097d12eSChristian Borntraeger local_irq_disable(); 47036edaa530SPaolo Bonzini guest_enter_irqoff(); 4704db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 47050097d12eSChristian Borntraeger local_irq_enable(); 4706c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4707c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4708c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4709c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4710c8aac234SJanosch Frank } 471156e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 471256e62a73SSven Schnelle load_fpu_regs(); 4713a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4714a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4715c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4716c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4717c8aac234SJanosch Frank sie_page->pv_grregs, 4718c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 47193adae0b4SJanosch Frank /* 47203adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 47213adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 47223adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 47233adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 47243adae0b4SJanosch Frank */ 47253adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 47263adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 47273adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 47283adae0b4SJanosch Frank } 4729c8aac234SJanosch Frank } 47300097d12eSChristian Borntraeger local_irq_disable(); 4731db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 47326edaa530SPaolo Bonzini guest_exit_irqoff(); 47330097d12eSChristian Borntraeger local_irq_enable(); 47342031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 47353fb4c40fSThomas Huth 47363fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 473727291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 47383fb4c40fSThomas Huth 47392031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 4740e168bf8dSCarsten Otte return rc; 4741b0c632dbSHeiko Carstens } 4742b0c632dbSHeiko Carstens 47432f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4744b028ee3eSDavid Hildenbrand { 47452f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 47464d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 47474e0b1ab7SFan Zhang struct gs_cb *gscb; 47484d5f2c04SChristian Borntraeger 47494d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 47504e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4751b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4752b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4753b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4754b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4755b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4756b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4757b028ee3eSDavid Hildenbrand } 4758b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4759b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4760b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4761b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 47629fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 47639fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4764b028ee3eSDavid Hildenbrand } 476523a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 476623a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 476723a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 47683fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 476923a60f83SCollin Walling } 477080cd8763SFan Zhang /* 477180cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 477280cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 477380cd8763SFan Zhang */ 477480cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 47754d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4776bb59c2daSAlice Frosi riccb->v && 47770c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 47784d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 47790c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 478080cd8763SFan Zhang } 47814e0b1ab7SFan Zhang /* 47824e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 47834e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 47844e0b1ab7SFan Zhang */ 47854e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 47864e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 47874e0b1ab7SFan Zhang gscb->gssm && 47884e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 47894e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 47904e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 47914e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 47924e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 479380cd8763SFan Zhang } 479435b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 479535b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 479635b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 479735b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 479835b3fde6SChristian Borntraeger } 47994e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 48004e0b1ab7SFan Zhang preempt_disable(); 48014e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 48024e0b1ab7SFan Zhang if (current->thread.gs_cb) { 48034e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 48044e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 48054e0b1ab7SFan Zhang } 48064e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 48074e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 48084e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 48094e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 48104e0b1ab7SFan Zhang } 48114e0b1ab7SFan Zhang preempt_enable(); 48124e0b1ab7SFan Zhang } 4813a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4814811ea797SJanosch Frank } 4815811ea797SJanosch Frank 48162f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4817811ea797SJanosch Frank { 48182f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48192f0a83beSTianjia Zhang 4820811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4821811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4822811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4823811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4824811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4825811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4826811ea797SJanosch Frank } 4827811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4828811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4829811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4830811ea797SJanosch Frank } 4831811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4832811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4833811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4834811ea797SJanosch Frank save_fpu_regs(); 4835811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4836811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4837811ea797SJanosch Frank if (MACHINE_HAS_VX) 4838811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4839811ea797SJanosch Frank else 4840811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4841811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4842811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4843811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4844811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4845811ea797SJanosch Frank 4846811ea797SJanosch Frank /* Sync fmt2 only data */ 4847811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 48482f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4849811ea797SJanosch Frank } else { 4850811ea797SJanosch Frank /* 4851811ea797SJanosch Frank * In several places we have to modify our internal view to 4852811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4853811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4854811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4855811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4856811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4857811ea797SJanosch Frank * do only accept the condition code from userspace. 4858811ea797SJanosch Frank */ 4859811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4860811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4861811ea797SJanosch Frank PSW_MASK_CC; 4862811ea797SJanosch Frank } 486380cd8763SFan Zhang 4864b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4865b028ee3eSDavid Hildenbrand } 4866b028ee3eSDavid Hildenbrand 48672f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4868b028ee3eSDavid Hildenbrand { 48692f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48702f0a83beSTianjia Zhang 4871b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4872b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4873b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 487435b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 487523a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 48764e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 487744bada28SHeiko Carstens preempt_disable(); 48784e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 48794e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 48804e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 48814e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 48824e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 48834e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 48844e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 48854e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 488644bada28SHeiko Carstens preempt_enable(); 48874e0b1ab7SFan Zhang } 4888a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4889b028ee3eSDavid Hildenbrand } 4890b028ee3eSDavid Hildenbrand 48912f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4892811ea797SJanosch Frank { 48932f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48942f0a83beSTianjia Zhang 4895811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4896811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4897811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4898811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4899811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4900811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4901811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4902811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4903811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4904811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4905811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4906811ea797SJanosch Frank /* Save guest register state */ 4907811ea797SJanosch Frank save_fpu_regs(); 4908811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4909811ea797SJanosch Frank /* Restore will be done lazily at return */ 4910811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4911811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4912811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 49132f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4914811ea797SJanosch Frank } 4915811ea797SJanosch Frank 49161b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4917b0c632dbSHeiko Carstens { 49181b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 49198f2abe6aSChristian Borntraeger int rc; 4920b0c632dbSHeiko Carstens 49210460eb35SJanosch Frank /* 49220460eb35SJanosch Frank * Running a VM while dumping always has the potential to 49230460eb35SJanosch Frank * produce inconsistent dump data. But for PV vcpus a SIE 49240460eb35SJanosch Frank * entry while dumping could also lead to a fatal validity 49250460eb35SJanosch Frank * intercept which we absolutely want to avoid. 49260460eb35SJanosch Frank */ 49270460eb35SJanosch Frank if (vcpu->kvm->arch.pv.dumping) 49280460eb35SJanosch Frank return -EINVAL; 49290460eb35SJanosch Frank 4930460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4931460df4c1SPaolo Bonzini return -EINTR; 4932460df4c1SPaolo Bonzini 4933200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4934200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4935200824f5SThomas Huth return -EINVAL; 4936200824f5SThomas Huth 4937accb757dSChristoffer Dall vcpu_load(vcpu); 4938accb757dSChristoffer Dall 493927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 494027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4941accb757dSChristoffer Dall rc = 0; 4942accb757dSChristoffer Dall goto out; 494327291e21SDavid Hildenbrand } 494427291e21SDavid Hildenbrand 494520b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4946b0c632dbSHeiko Carstens 4947fe28c786SJanosch Frank /* 4948fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4949fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4950fe28c786SJanosch Frank */ 49516352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 49526852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 49536352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4954ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 49556352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4956accb757dSChristoffer Dall rc = -EINVAL; 4957accb757dSChristoffer Dall goto out; 49586352e4d2SDavid Hildenbrand } 4959b0c632dbSHeiko Carstens 49602f0a83beSTianjia Zhang sync_regs(vcpu); 4961db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4962d7b0b5ebSCarsten Otte 4963dab4079dSHeiko Carstens might_fault(); 4964e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 49659ace903dSChristian Ehrhardt 4966b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4967b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 49688f2abe6aSChristian Borntraeger rc = -EINTR; 4969b1d16c49SChristian Ehrhardt } 49708f2abe6aSChristian Borntraeger 497127291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 497227291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 497327291e21SDavid Hildenbrand rc = 0; 497427291e21SDavid Hildenbrand } 497527291e21SDavid Hildenbrand 49768f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 497771f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 49788f2abe6aSChristian Borntraeger rc = 0; 49798f2abe6aSChristian Borntraeger } 49808f2abe6aSChristian Borntraeger 4981db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 49822f0a83beSTianjia Zhang store_regs(vcpu); 4983d7b0b5ebSCarsten Otte 498420b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4985b0c632dbSHeiko Carstens 4986b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4987accb757dSChristoffer Dall out: 4988accb757dSChristoffer Dall vcpu_put(vcpu); 49897e8e6ab4SHeiko Carstens return rc; 4990b0c632dbSHeiko Carstens } 4991b0c632dbSHeiko Carstens 4992b0c632dbSHeiko Carstens /* 4993b0c632dbSHeiko Carstens * store status at address 4994b0c632dbSHeiko Carstens * we use have two special cases: 4995b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4996b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4997b0c632dbSHeiko Carstens */ 4998d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4999b0c632dbSHeiko Carstens { 5000092670cdSCarsten Otte unsigned char archmode = 1; 50019abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 5002fda902cbSMichael Mueller unsigned int px; 50034287f247SDavid Hildenbrand u64 clkcomp, cputm; 5004d0bce605SHeiko Carstens int rc; 5005b0c632dbSHeiko Carstens 5006d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 5007d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 5008d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 5009b0c632dbSHeiko Carstens return -EFAULT; 5010d9a3a09aSMartin Schwidefsky gpa = 0; 5011d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 5012d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 5013b0c632dbSHeiko Carstens return -EFAULT; 5014d9a3a09aSMartin Schwidefsky gpa = px; 5015d9a3a09aSMartin Schwidefsky } else 5016d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 50179abc2a08SDavid Hildenbrand 50189abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 50199abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 50209522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 5021d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50229abc2a08SDavid Hildenbrand fprs, 128); 50239abc2a08SDavid Hildenbrand } else { 50249abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50256fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 50269abc2a08SDavid Hildenbrand } 5027d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 5028d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 5029d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 5030d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 5031d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 5032fda902cbSMichael Mueller &px, 4); 5033d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 50349abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 5035d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 5036d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 50374287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 5038d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 50394287f247SDavid Hildenbrand &cputm, 8); 5040178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 5041d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 5042d0bce605SHeiko Carstens &clkcomp, 8); 5043d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 5044d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 5045d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 5046d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 5047d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 5048b0c632dbSHeiko Carstens } 5049b0c632dbSHeiko Carstens 5050e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 5051e879892cSThomas Huth { 5052e879892cSThomas Huth /* 5053e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 505431d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 5055e879892cSThomas Huth * it into the save area 5056e879892cSThomas Huth */ 5057d0164ee2SHendrik Brueckner save_fpu_regs(); 50589abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 5059e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 5060e879892cSThomas Huth 5061e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 5062e879892cSThomas Huth } 5063e879892cSThomas Huth 50648ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50658ad35755SDavid Hildenbrand { 50668ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 50678e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 50688ad35755SDavid Hildenbrand } 50698ad35755SDavid Hildenbrand 50708ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 50718ad35755SDavid Hildenbrand { 507246808a4cSMarc Zyngier unsigned long i; 50738ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 50748ad35755SDavid Hildenbrand 50758ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 50768ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 50778ad35755SDavid Hildenbrand } 50788ad35755SDavid Hildenbrand } 50798ad35755SDavid Hildenbrand 50808ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50818ad35755SDavid Hildenbrand { 508209a400e7SDavid Hildenbrand if (!sclp.has_ibs) 508309a400e7SDavid Hildenbrand return; 50848ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 50858e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 50868ad35755SDavid Hildenbrand } 50878ad35755SDavid Hildenbrand 5088fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 50896852d7b6SDavid Hildenbrand { 5090fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 50918ad35755SDavid Hildenbrand 50928ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 5093fe28c786SJanosch Frank return 0; 50948ad35755SDavid Hildenbrand 50956852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 50968ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5097433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 50988ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 50998ad35755SDavid Hildenbrand 5100fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 5101fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5102fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 5103fe28c786SJanosch Frank if (r) { 5104fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5105fe28c786SJanosch Frank return r; 5106fe28c786SJanosch Frank } 5107fe28c786SJanosch Frank } 5108fe28c786SJanosch Frank 51098ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5110113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 51118ad35755SDavid Hildenbrand started_vcpus++; 51128ad35755SDavid Hildenbrand } 51138ad35755SDavid Hildenbrand 51148ad35755SDavid Hildenbrand if (started_vcpus == 0) { 51158ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 51168ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 51178ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 51188ad35755SDavid Hildenbrand /* 51198ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 51208ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 512138860756SBhaskar Chowdhury * outstanding ENABLE requests. 51228ad35755SDavid Hildenbrand */ 51238ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 51248ad35755SDavid Hildenbrand } 51258ad35755SDavid Hildenbrand 51269daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 51278ad35755SDavid Hildenbrand /* 512872f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 512972f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 513072f21820SChristian Borntraeger * refresh our view. 513172f21820SChristian Borntraeger */ 513272f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 513372f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 513472f21820SChristian Borntraeger /* 51358ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 51368ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 51378ad35755SDavid Hildenbrand */ 5138d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 5139433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5140fe28c786SJanosch Frank return 0; 51416852d7b6SDavid Hildenbrand } 51426852d7b6SDavid Hildenbrand 5143fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 51446852d7b6SDavid Hildenbrand { 5145fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 51468ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 51478ad35755SDavid Hildenbrand 51488ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 5149fe28c786SJanosch Frank return 0; 51508ad35755SDavid Hildenbrand 51516852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 51528ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5153433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 51548ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 51558ad35755SDavid Hildenbrand 5156fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 5157fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5158fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 5159fe28c786SJanosch Frank if (r) { 5160fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5161fe28c786SJanosch Frank return r; 5162fe28c786SJanosch Frank } 5163fe28c786SJanosch Frank } 5164fe28c786SJanosch Frank 5165812de046SEric Farman /* 5166812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag, 5167812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders 5168812de046SEric Farman * have been fully processed. This will ensure that the VCPU 5169812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE. 5170812de046SEric Farman */ 5171812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 51726cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 517332f5ff63SDavid Hildenbrand 51748ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 51758ad35755SDavid Hildenbrand 51768ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5177113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 5178113d10bcSMarc Zyngier 5179113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 51808ad35755SDavid Hildenbrand started_vcpus++; 5181113d10bcSMarc Zyngier started_vcpu = tmp; 51828ad35755SDavid Hildenbrand } 51838ad35755SDavid Hildenbrand } 51848ad35755SDavid Hildenbrand 51858ad35755SDavid Hildenbrand if (started_vcpus == 1) { 51868ad35755SDavid Hildenbrand /* 51878ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 51888ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 51898ad35755SDavid Hildenbrand */ 51908ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 51918ad35755SDavid Hildenbrand } 51928ad35755SDavid Hildenbrand 5193433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5194fe28c786SJanosch Frank return 0; 51956852d7b6SDavid Hildenbrand } 51966852d7b6SDavid Hildenbrand 5197d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 5198d6712df9SCornelia Huck struct kvm_enable_cap *cap) 5199d6712df9SCornelia Huck { 5200d6712df9SCornelia Huck int r; 5201d6712df9SCornelia Huck 5202d6712df9SCornelia Huck if (cap->flags) 5203d6712df9SCornelia Huck return -EINVAL; 5204d6712df9SCornelia Huck 5205d6712df9SCornelia Huck switch (cap->cap) { 5206fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 5207fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 5208fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 5209c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 5210fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 5211fa6b7fe9SCornelia Huck } 5212fa6b7fe9SCornelia Huck r = 0; 5213fa6b7fe9SCornelia Huck break; 5214d6712df9SCornelia Huck default: 5215d6712df9SCornelia Huck r = -EINVAL; 5216d6712df9SCornelia Huck break; 5217d6712df9SCornelia Huck } 5218d6712df9SCornelia Huck return r; 5219d6712df9SCornelia Huck } 5220d6712df9SCornelia Huck 52210e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu, 522219e12277SJanosch Frank struct kvm_s390_mem_op *mop) 522319e12277SJanosch Frank { 522419e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 5225b99f4512SNico Boehr void *sida_addr; 522619e12277SJanosch Frank int r = 0; 522719e12277SJanosch Frank 522819e12277SJanosch Frank if (mop->flags || !mop->size) 522919e12277SJanosch Frank return -EINVAL; 523019e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 523119e12277SJanosch Frank return -EINVAL; 523219e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 523319e12277SJanosch Frank return -E2BIG; 52342c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu)) 52352c212e1bSJanis Schoetterl-Glausch return -EINVAL; 523619e12277SJanosch Frank 5237b99f4512SNico Boehr sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset; 5238b99f4512SNico Boehr 523919e12277SJanosch Frank switch (mop->op) { 524019e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 5241b99f4512SNico Boehr if (copy_to_user(uaddr, sida_addr, mop->size)) 524219e12277SJanosch Frank r = -EFAULT; 524319e12277SJanosch Frank 524419e12277SJanosch Frank break; 524519e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 5246b99f4512SNico Boehr if (copy_from_user(sida_addr, uaddr, mop->size)) 524719e12277SJanosch Frank r = -EFAULT; 524819e12277SJanosch Frank break; 524919e12277SJanosch Frank } 525019e12277SJanosch Frank return r; 525119e12277SJanosch Frank } 52520e1234c0SJanis Schoetterl-Glausch 52530e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, 525441408c28SThomas Huth struct kvm_s390_mem_op *mop) 525541408c28SThomas Huth { 525641408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 5257*701422b3SJanis Schoetterl-Glausch enum gacc_mode acc_mode; 525841408c28SThomas Huth void *tmpbuf = NULL; 5259a41f505eSJanis Schoetterl-Glausch int r; 526041408c28SThomas Huth 5261a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION | 5262a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY | 5263a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_SKEY_PROTECTION); 5264a41f505eSJanis Schoetterl-Glausch if (r) 5265a41f505eSJanis Schoetterl-Glausch return r; 5266a41f505eSJanis Schoetterl-Glausch if (mop->ar >= NUM_ACRS) 526741408c28SThomas Huth return -EINVAL; 526819e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 526919e12277SJanosch Frank return -EINVAL; 527041408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 527141408c28SThomas Huth tmpbuf = vmalloc(mop->size); 527241408c28SThomas Huth if (!tmpbuf) 527341408c28SThomas Huth return -ENOMEM; 527441408c28SThomas Huth } 527541408c28SThomas Huth 5276*701422b3SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE; 527741408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 5278e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 5279*701422b3SJanis Schoetterl-Glausch acc_mode, mop->key); 5280*701422b3SJanis Schoetterl-Glausch goto out_inject; 528141408c28SThomas Huth } 5282*701422b3SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) { 5283e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5284e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 5285*701422b3SJanis Schoetterl-Glausch if (r) 5286*701422b3SJanis Schoetterl-Glausch goto out_inject; 5287*701422b3SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) { 528841408c28SThomas Huth r = -EFAULT; 5289*701422b3SJanis Schoetterl-Glausch goto out_free; 529041408c28SThomas Huth } 5291*701422b3SJanis Schoetterl-Glausch } else { 529241408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 529341408c28SThomas Huth r = -EFAULT; 5294*701422b3SJanis Schoetterl-Glausch goto out_free; 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 } 529941408c28SThomas Huth 5300*701422b3SJanis Schoetterl-Glausch out_inject: 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 5304*701422b3SJanis Schoetterl-Glausch out_free: 530541408c28SThomas Huth vfree(tmpbuf); 530641408c28SThomas Huth return r; 530741408c28SThomas Huth } 530841408c28SThomas Huth 53090e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu, 531019e12277SJanosch Frank struct kvm_s390_mem_op *mop) 531119e12277SJanosch Frank { 531219e12277SJanosch Frank int r, srcu_idx; 531319e12277SJanosch Frank 531419e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 531519e12277SJanosch Frank 531619e12277SJanosch Frank switch (mop->op) { 531719e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 531819e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 53190e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop); 532019e12277SJanosch Frank break; 532119e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 532219e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 532319e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 53240e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop); 532519e12277SJanosch Frank break; 532619e12277SJanosch Frank default: 532719e12277SJanosch Frank r = -EINVAL; 532819e12277SJanosch Frank } 532919e12277SJanosch Frank 533019e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 533119e12277SJanosch Frank return r; 533219e12277SJanosch Frank } 533319e12277SJanosch Frank 53345cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 5335b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 5336b0c632dbSHeiko Carstens { 5337b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 5338b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 5339b0c632dbSHeiko Carstens 534093736624SAvi Kivity switch (ioctl) { 534147b43c52SJens Freimann case KVM_S390_IRQ: { 534247b43c52SJens Freimann struct kvm_s390_irq s390irq; 534347b43c52SJens Freimann 534447b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 53459b062471SChristoffer Dall return -EFAULT; 53469b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 534747b43c52SJens Freimann } 534893736624SAvi Kivity case KVM_S390_INTERRUPT: { 5349ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 535053936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 5351ba5c1e9bSCarsten Otte 5352ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 53539b062471SChristoffer Dall return -EFAULT; 5354383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 5355383d0b05SJens Freimann return -EINVAL; 53569b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 5357ba5c1e9bSCarsten Otte } 53589b062471SChristoffer Dall } 53595cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 53605cb0944cSPaolo Bonzini } 53615cb0944cSPaolo Bonzini 53628aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu, 53638aba0958SJanosch Frank struct kvm_pv_cmd *cmd) 53648aba0958SJanosch Frank { 53658aba0958SJanosch Frank struct kvm_s390_pv_dmp dmp; 53668aba0958SJanosch Frank void *data; 53678aba0958SJanosch Frank int ret; 53688aba0958SJanosch Frank 53698aba0958SJanosch Frank /* Dump initialization is a prerequisite */ 53708aba0958SJanosch Frank if (!vcpu->kvm->arch.pv.dumping) 53718aba0958SJanosch Frank return -EINVAL; 53728aba0958SJanosch Frank 53738aba0958SJanosch Frank if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp))) 53748aba0958SJanosch Frank return -EFAULT; 53758aba0958SJanosch Frank 53768aba0958SJanosch Frank /* We only handle this subcmd right now */ 53778aba0958SJanosch Frank if (dmp.subcmd != KVM_PV_DUMP_CPU) 53788aba0958SJanosch Frank return -EINVAL; 53798aba0958SJanosch Frank 53808aba0958SJanosch Frank /* CPU dump length is the same as create cpu storage donation. */ 53818aba0958SJanosch Frank if (dmp.buff_len != uv_info.guest_cpu_stor_len) 53828aba0958SJanosch Frank return -EINVAL; 53838aba0958SJanosch Frank 53848aba0958SJanosch Frank data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL); 53858aba0958SJanosch Frank if (!data) 53868aba0958SJanosch Frank return -ENOMEM; 53878aba0958SJanosch Frank 53888aba0958SJanosch Frank ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc); 53898aba0958SJanosch Frank 53908aba0958SJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x", 53918aba0958SJanosch Frank vcpu->vcpu_id, cmd->rc, cmd->rrc); 53928aba0958SJanosch Frank 53938aba0958SJanosch Frank if (ret) 53948aba0958SJanosch Frank ret = -EINVAL; 53958aba0958SJanosch Frank 53968aba0958SJanosch Frank /* On success copy over the dump data */ 53978aba0958SJanosch Frank if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len)) 53988aba0958SJanosch Frank ret = -EFAULT; 53998aba0958SJanosch Frank 54008aba0958SJanosch Frank kvfree(data); 54018aba0958SJanosch Frank return ret; 54028aba0958SJanosch Frank } 54038aba0958SJanosch Frank 54045cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 54055cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 54065cb0944cSPaolo Bonzini { 54075cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 54085cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 54095cb0944cSPaolo Bonzini int idx; 54105cb0944cSPaolo Bonzini long r; 54118a8378faSJanosch Frank u16 rc, rrc; 54129b062471SChristoffer Dall 54139b062471SChristoffer Dall vcpu_load(vcpu); 54149b062471SChristoffer Dall 54159b062471SChristoffer Dall switch (ioctl) { 5416b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 5417800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 541855680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 5419800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 5420bc923cc9SAvi Kivity break; 5421b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 5422b0c632dbSHeiko Carstens psw_t psw; 5423b0c632dbSHeiko Carstens 5424bc923cc9SAvi Kivity r = -EFAULT; 5425b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 5426bc923cc9SAvi Kivity break; 5427bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 5428bc923cc9SAvi Kivity break; 5429b0c632dbSHeiko Carstens } 54307de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 54317de3f142SJanosch Frank r = 0; 54327de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 54338a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54348a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54358a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 54368a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 54378a8378faSJanosch Frank rc, rrc); 54388a8378faSJanosch Frank } 54397de3f142SJanosch Frank break; 5440b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 54417de3f142SJanosch Frank r = 0; 54427de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 54438a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54448a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54458a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 54468a8378faSJanosch Frank &rc, &rrc); 54478a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 54488a8378faSJanosch Frank rc, rrc); 54498a8378faSJanosch Frank } 54507de3f142SJanosch Frank break; 54517de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 54527de3f142SJanosch Frank r = 0; 54537de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 54548a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54558a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54568a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 54578a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 54588a8378faSJanosch Frank rc, rrc); 54598a8378faSJanosch Frank } 5460bc923cc9SAvi Kivity break; 546114eebd91SCarsten Otte case KVM_SET_ONE_REG: 546214eebd91SCarsten Otte case KVM_GET_ONE_REG: { 546314eebd91SCarsten Otte struct kvm_one_reg reg; 546468cf7b1fSJanosch Frank r = -EINVAL; 546568cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 546668cf7b1fSJanosch Frank break; 546714eebd91SCarsten Otte r = -EFAULT; 546814eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 546914eebd91SCarsten Otte break; 547014eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 547114eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 547214eebd91SCarsten Otte else 547314eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 547414eebd91SCarsten Otte break; 547514eebd91SCarsten Otte } 547627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 547727e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 547827e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 547927e0393fSCarsten Otte 548027e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 548127e0393fSCarsten Otte r = -EFAULT; 548227e0393fSCarsten Otte break; 548327e0393fSCarsten Otte } 548427e0393fSCarsten Otte 548527e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 548627e0393fSCarsten Otte r = -EINVAL; 548727e0393fSCarsten Otte break; 548827e0393fSCarsten Otte } 548927e0393fSCarsten Otte 549027e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 549127e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 549227e0393fSCarsten Otte break; 549327e0393fSCarsten Otte } 549427e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 549527e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 549627e0393fSCarsten Otte 549727e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 549827e0393fSCarsten Otte r = -EFAULT; 549927e0393fSCarsten Otte break; 550027e0393fSCarsten Otte } 550127e0393fSCarsten Otte 550227e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 550327e0393fSCarsten Otte r = -EINVAL; 550427e0393fSCarsten Otte break; 550527e0393fSCarsten Otte } 550627e0393fSCarsten Otte 550727e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 550827e0393fSCarsten Otte ucasmap.length); 550927e0393fSCarsten Otte break; 551027e0393fSCarsten Otte } 551127e0393fSCarsten Otte #endif 5512ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 5513527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 5514ccc7910fSCarsten Otte break; 5515ccc7910fSCarsten Otte } 5516d6712df9SCornelia Huck case KVM_ENABLE_CAP: 5517d6712df9SCornelia Huck { 5518d6712df9SCornelia Huck struct kvm_enable_cap cap; 5519d6712df9SCornelia Huck r = -EFAULT; 5520d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 5521d6712df9SCornelia Huck break; 5522d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 5523d6712df9SCornelia Huck break; 5524d6712df9SCornelia Huck } 552541408c28SThomas Huth case KVM_S390_MEM_OP: { 552641408c28SThomas Huth struct kvm_s390_mem_op mem_op; 552741408c28SThomas Huth 552841408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 55290e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op); 553041408c28SThomas Huth else 553141408c28SThomas Huth r = -EFAULT; 553241408c28SThomas Huth break; 553341408c28SThomas Huth } 5534816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 5535816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5536816c7667SJens Freimann 5537816c7667SJens Freimann r = -EFAULT; 5538816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5539816c7667SJens Freimann break; 5540816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 5541816c7667SJens Freimann irq_state.len == 0 || 5542816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 5543816c7667SJens Freimann r = -EINVAL; 5544816c7667SJens Freimann break; 5545816c7667SJens Freimann } 5546bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5547816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 5548816c7667SJens Freimann (void __user *) irq_state.buf, 5549816c7667SJens Freimann irq_state.len); 5550816c7667SJens Freimann break; 5551816c7667SJens Freimann } 5552816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 5553816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5554816c7667SJens Freimann 5555816c7667SJens Freimann r = -EFAULT; 5556816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5557816c7667SJens Freimann break; 5558816c7667SJens Freimann if (irq_state.len == 0) { 5559816c7667SJens Freimann r = -EINVAL; 5560816c7667SJens Freimann break; 5561816c7667SJens Freimann } 5562bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5563816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 5564816c7667SJens Freimann (__u8 __user *) irq_state.buf, 5565816c7667SJens Freimann irq_state.len); 5566816c7667SJens Freimann break; 5567816c7667SJens Freimann } 55688aba0958SJanosch Frank case KVM_S390_PV_CPU_COMMAND: { 55698aba0958SJanosch Frank struct kvm_pv_cmd cmd; 55708aba0958SJanosch Frank 55718aba0958SJanosch Frank r = -EINVAL; 55728aba0958SJanosch Frank if (!is_prot_virt_host()) 55738aba0958SJanosch Frank break; 55748aba0958SJanosch Frank 55758aba0958SJanosch Frank r = -EFAULT; 55768aba0958SJanosch Frank if (copy_from_user(&cmd, argp, sizeof(cmd))) 55778aba0958SJanosch Frank break; 55788aba0958SJanosch Frank 55798aba0958SJanosch Frank r = -EINVAL; 55808aba0958SJanosch Frank if (cmd.flags) 55818aba0958SJanosch Frank break; 55828aba0958SJanosch Frank 55838aba0958SJanosch Frank /* We only handle this cmd right now */ 55848aba0958SJanosch Frank if (cmd.cmd != KVM_PV_DUMP) 55858aba0958SJanosch Frank break; 55868aba0958SJanosch Frank 55878aba0958SJanosch Frank r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd); 55888aba0958SJanosch Frank 55898aba0958SJanosch Frank /* Always copy over UV rc / rrc data */ 55908aba0958SJanosch Frank if (copy_to_user((__u8 __user *)argp, &cmd.rc, 55918aba0958SJanosch Frank sizeof(cmd.rc) + sizeof(cmd.rrc))) 55928aba0958SJanosch Frank r = -EFAULT; 55938aba0958SJanosch Frank break; 55948aba0958SJanosch Frank } 5595b0c632dbSHeiko Carstens default: 55963e6afcf1SCarsten Otte r = -ENOTTY; 5597b0c632dbSHeiko Carstens } 55989b062471SChristoffer Dall 55999b062471SChristoffer Dall vcpu_put(vcpu); 5600bc923cc9SAvi Kivity return r; 5601b0c632dbSHeiko Carstens } 5602b0c632dbSHeiko Carstens 56031499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 56045b1c1493SCarsten Otte { 56055b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 56065b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 56075b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 56085b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 56095b1c1493SCarsten Otte get_page(vmf->page); 56105b1c1493SCarsten Otte return 0; 56115b1c1493SCarsten Otte } 56125b1c1493SCarsten Otte #endif 56135b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 56145b1c1493SCarsten Otte } 56155b1c1493SCarsten Otte 5616d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 5617d663b8a2SPaolo Bonzini { 5618d663b8a2SPaolo Bonzini return true; 5619d663b8a2SPaolo Bonzini } 5620d663b8a2SPaolo Bonzini 5621b0c632dbSHeiko Carstens /* Section: memory related */ 5622f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5623537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5624537a17b3SSean Christopherson struct kvm_memory_slot *new, 56257b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5626b0c632dbSHeiko Carstens { 5627ec5c8697SSean Christopherson gpa_t size; 5628ec5c8697SSean Christopherson 5629ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5630ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5631ec5c8697SSean Christopherson return -EINVAL; 5632ec5c8697SSean Christopherson 5633f2d3155eSNico Boehr if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) { 5634f2d3155eSNico Boehr /* 5635f2d3155eSNico Boehr * A few sanity checks. We can have memory slots which have to be 5636f2d3155eSNico Boehr * located/ended at a segment boundary (1MB). The memory in userland is 5637f2d3155eSNico Boehr * ok to be fragmented into various different vmas. It is okay to mmap() 5638f2d3155eSNico Boehr * and munmap() stuff in this slot after doing this call at any time 5639f2d3155eSNico Boehr */ 5640b0c632dbSHeiko Carstens 5641cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5642b0c632dbSHeiko Carstens return -EINVAL; 5643b0c632dbSHeiko Carstens 5644ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5645cf5b4869SSean Christopherson if (size & 0xffffful) 5646b0c632dbSHeiko Carstens return -EINVAL; 5647b0c632dbSHeiko Carstens 5648cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5649a3a92c31SDominik Dingel return -EINVAL; 5650f2d3155eSNico Boehr } 5651f2d3155eSNico Boehr 5652f2d3155eSNico Boehr if (!kvm->arch.migration_mode) 5653f2d3155eSNico Boehr return 0; 5654f2d3155eSNico Boehr 5655f2d3155eSNico Boehr /* 5656f2d3155eSNico Boehr * Turn off migration mode when: 5657f2d3155eSNico Boehr * - userspace creates a new memslot with dirty logging off, 5658f2d3155eSNico Boehr * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and 5659f2d3155eSNico Boehr * dirty logging is turned off. 5660f2d3155eSNico Boehr * Migration mode expects dirty page logging being enabled to store 5661f2d3155eSNico Boehr * its dirty bitmap. 5662f2d3155eSNico Boehr */ 5663f2d3155eSNico Boehr if (change != KVM_MR_DELETE && 5664f2d3155eSNico Boehr !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) 5665f2d3155eSNico Boehr WARN(kvm_s390_vm_stop_migration(kvm), 5666f2d3155eSNico Boehr "Failed to stop migration mode"); 5667a3a92c31SDominik Dingel 5668f7784b8eSMarcelo Tosatti return 0; 5669f7784b8eSMarcelo Tosatti } 5670f7784b8eSMarcelo Tosatti 5671f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 56729d4c197cSSean Christopherson struct kvm_memory_slot *old, 5673f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 56748482644aSTakuya Yoshikawa enum kvm_mr_change change) 5675f7784b8eSMarcelo Tosatti { 567619ec166cSChristian Borntraeger int rc = 0; 5677f7784b8eSMarcelo Tosatti 567819ec166cSChristian Borntraeger switch (change) { 567919ec166cSChristian Borntraeger case KVM_MR_DELETE: 568019ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 568119ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 568219ec166cSChristian Borntraeger break; 568319ec166cSChristian Borntraeger case KVM_MR_MOVE: 568419ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 568519ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 568619ec166cSChristian Borntraeger if (rc) 568719ec166cSChristian Borntraeger break; 56883b684a42SJoe Perches fallthrough; 568919ec166cSChristian Borntraeger case KVM_MR_CREATE: 5690cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5691cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5692cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 569319ec166cSChristian Borntraeger break; 569419ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 569519ec166cSChristian Borntraeger break; 569619ec166cSChristian Borntraeger default: 569719ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 569819ec166cSChristian Borntraeger } 5699598841caSCarsten Otte if (rc) 5700ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5701598841caSCarsten Otte return; 5702b0c632dbSHeiko Carstens } 5703b0c632dbSHeiko Carstens 570460a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 570560a37709SAlexander Yarygin { 570660a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 570760a37709SAlexander Yarygin 570860a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 570960a37709SAlexander Yarygin } 571060a37709SAlexander Yarygin 5711b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5712b0c632dbSHeiko Carstens { 571360a37709SAlexander Yarygin int i; 571460a37709SAlexander Yarygin 571507197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 57168d43d570SMichael Mueller pr_info("SIE is not available\n"); 571707197fd0SDavid Hildenbrand return -ENODEV; 571807197fd0SDavid Hildenbrand } 571907197fd0SDavid Hildenbrand 5720a4499382SJanosch Frank if (nested && hpage) { 57218d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5722a4499382SJanosch Frank return -EINVAL; 5723a4499382SJanosch Frank } 5724a4499382SJanosch Frank 572560a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5726c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 572717e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 572860a37709SAlexander Yarygin 57299d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5730b0c632dbSHeiko Carstens } 5731b0c632dbSHeiko Carstens 5732b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5733b0c632dbSHeiko Carstens { 5734b0c632dbSHeiko Carstens kvm_exit(); 5735b0c632dbSHeiko Carstens } 5736b0c632dbSHeiko Carstens 5737b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5738b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5739566af940SCornelia Huck 5740566af940SCornelia Huck /* 5741566af940SCornelia Huck * Enable autoloading of the kvm module. 5742566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5743566af940SCornelia Huck * since x86 takes a different approach. 5744566af940SCornelia Huck */ 5745566af940SCornelia Huck #include <linux/miscdevice.h> 5746566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5747566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5748