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> 9b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 10628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1115f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 12b0c632dbSHeiko Carstens */ 13b0c632dbSHeiko Carstens 147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 167aedd9d4SMichael Mueller 17b0c632dbSHeiko Carstens #include <linux/compiler.h> 18b0c632dbSHeiko Carstens #include <linux/err.h> 19b0c632dbSHeiko Carstens #include <linux/fs.h> 20ca872302SChristian Borntraeger #include <linux/hrtimer.h> 21b0c632dbSHeiko Carstens #include <linux/init.h> 22b0c632dbSHeiko Carstens #include <linux/kvm.h> 23b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 24b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 25b0c632dbSHeiko Carstens #include <linux/module.h> 26d3217967SPaul Gortmaker #include <linux/moduleparam.h> 27a374e892STony Krowiak #include <linux/random.h> 28b0c632dbSHeiko Carstens #include <linux/slab.h> 29ba5c1e9bSCarsten Otte #include <linux/timer.h> 3041408c28SThomas Huth #include <linux/vmalloc.h> 3115c9705fSDavid Hildenbrand #include <linux/bitmap.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33190df4a2SClaudio Imbrenda #include <linux/string.h> 3465fddcfcSMike Rapoport #include <linux/pgtable.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" 51b0c632dbSHeiko Carstens 525786fffaSCornelia Huck #define CREATE_TRACE_POINTS 535786fffaSCornelia Huck #include "trace.h" 54ade38c31SCornelia Huck #include "trace-s390.h" 555786fffaSCornelia Huck 5641408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 57816c7667SJens Freimann #define LOCAL_IRQS 32 58816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 59816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 6041408c28SThomas Huth 61fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = { 62fcfe1baeSJing Zhang KVM_GENERIC_VM_STATS(), 63fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_io), 64fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_float_mchk), 65fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_pfault_done), 66fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_service_signal), 67fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_virtio) 68fcfe1baeSJing Zhang }; 69fcfe1baeSJing Zhang 70fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = { 71fcfe1baeSJing Zhang .name_size = KVM_STATS_NAME_SIZE, 72fcfe1baeSJing Zhang .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), 73fcfe1baeSJing Zhang .id_offset = sizeof(struct kvm_stats_header), 74fcfe1baeSJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 75fcfe1baeSJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 76fcfe1baeSJing Zhang sizeof(kvm_vm_stats_desc), 77fcfe1baeSJing Zhang }; 78fcfe1baeSJing Zhang 79ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { 80ce55c049SJing Zhang KVM_GENERIC_VCPU_STATS(), 81ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_userspace), 82ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_null), 83ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_request), 84ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_io_request), 85ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_interrupt), 86ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_stop_request), 87ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_validity), 88ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instruction), 89ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_pei), 90ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, halt_no_poll_steal), 91ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctl), 92ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctlg), 93ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctl), 94ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctg), 95ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_program_interruption), 96ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instr_and_program), 97ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_operation_exception), 98ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_ckc), 99ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_cputm), 100ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_external_call), 101ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_emergency_signal), 102ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_service_signal), 103ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_virtio), 104ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_stop_signal), 105ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_prefix_signal), 106ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_restart_signal), 107ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_program), 108ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_io), 109ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_machine_check), 110ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_wait_state), 111ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_ckc), 112ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_cputm), 113ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_external_call), 114ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_emergency_signal), 115ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_mchk), 116ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_pfault_init), 117ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_program), 118ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_restart), 119ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_set_prefix), 120ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_stop_signal), 121ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_epsw), 122ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_gs), 123ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_io_other), 124ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpsw), 125ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpswe), 126ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_pfmf), 127ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ptff), 128ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sck), 129ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sckpf), 130ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stidp), 131ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_spx), 132ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stpx), 133ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stap), 134ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_iske), 135ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ri), 136ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_rrbe), 137ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sske), 138ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock), 139ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stsi), 140ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stfl), 141ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tb), 142ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tpi), 143ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tprot), 144ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tsch), 145ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sie), 146ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_essa), 147ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sthyi), 148ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense), 149ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running), 150ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call), 151ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency), 152ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency), 153ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_start), 154ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop), 155ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status), 156ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status), 157ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status), 158ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_arch), 159ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix), 160ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_restart), 161ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset), 162ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset), 163ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown), 164bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_10), 165bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_44), 166bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c), 167bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_ignored), 168bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_forward), 169bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_258), 170bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_308), 171bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_500), 172bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_other), 173ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, pfault_sync) 174ce55c049SJing Zhang }; 175ce55c049SJing Zhang 176ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = { 177ce55c049SJing Zhang .name_size = KVM_STATS_NAME_SIZE, 178ce55c049SJing Zhang .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc), 179ce55c049SJing Zhang .id_offset = sizeof(struct kvm_stats_header), 180ce55c049SJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 181ce55c049SJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 182ce55c049SJing Zhang sizeof(kvm_vcpu_stats_desc), 183ce55c049SJing Zhang }; 184ce55c049SJing Zhang 185a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 186a411edf1SDavid Hildenbrand static int nested; 187a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 188a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 189a411edf1SDavid Hildenbrand 190a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 191a4499382SJanosch Frank static int hpage; 192a4499382SJanosch Frank module_param(hpage, int, 0444); 193a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 194b0c632dbSHeiko Carstens 1958b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */ 1968b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10; 1978b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644); 198b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); 1998b905d28SChristian Borntraeger 200cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */ 201cc674ef2SMichael Mueller static bool use_gisa = true; 202cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644); 203cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it."); 204cc674ef2SMichael Mueller 20587e28a15SPierre Morel /* maximum diag9c forwarding per second */ 20687e28a15SPierre Morel unsigned int diag9c_forwarding_hz; 20787e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644); 20887e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off"); 20987e28a15SPierre Morel 210c3b9e3e1SChristian Borntraeger /* 211c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 212c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 213c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 214c3b9e3e1SChristian Borntraeger */ 215c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 216c3b9e3e1SChristian Borntraeger 217c3b9e3e1SChristian Borntraeger /* 218c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 219c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 220c3b9e3e1SChristian Borntraeger */ 221c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 222c3b9e3e1SChristian Borntraeger /* 223c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 224c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 225c3b9e3e1SChristian Borntraeger */ 226c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 227c3b9e3e1SChristian Borntraeger 228c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 22978c4b59fSMichael Mueller { 230c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 231c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 232c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 23317e89e13SSven Schnelle sizeof(stfle_fac_list)); 234c3b9e3e1SChristian Borntraeger 235c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 23678c4b59fSMichael Mueller } 23778c4b59fSMichael Mueller 23815c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 23915c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2400a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2410a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 24215c9705fSDavid Hildenbrand 2439d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 244a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 24578f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2463e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv; 2479d8d5786SMichael Mueller 248b0c632dbSHeiko Carstens /* Section: not file related */ 24913a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 250b0c632dbSHeiko Carstens { 251b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 25210474ae8SAlexander Graf return 0; 253b0c632dbSHeiko Carstens } 254b0c632dbSHeiko Carstens 255b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque) 256f257d6dcSSean Christopherson { 257f257d6dcSSean Christopherson return 0; 258f257d6dcSSean Christopherson } 259f257d6dcSSean Christopherson 26029b40f10SJanosch Frank /* forward declarations */ 261414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 262414d3b07SMartin Schwidefsky unsigned long end); 26329b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm); 2642c70fe44SChristian Borntraeger 2651575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2661575767eSDavid Hildenbrand { 2671575767eSDavid Hildenbrand u8 delta_idx = 0; 2681575767eSDavid Hildenbrand 2691575767eSDavid Hildenbrand /* 2701575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2711575767eSDavid Hildenbrand * -delta to the epoch. 2721575767eSDavid Hildenbrand */ 2731575767eSDavid Hildenbrand delta = -delta; 2741575767eSDavid Hildenbrand 2751575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2761575767eSDavid Hildenbrand if ((s64)delta < 0) 2771575767eSDavid Hildenbrand delta_idx = -1; 2781575767eSDavid Hildenbrand 2791575767eSDavid Hildenbrand scb->epoch += delta; 2801575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2811575767eSDavid Hildenbrand scb->epdx += delta_idx; 2821575767eSDavid Hildenbrand if (scb->epoch < delta) 2831575767eSDavid Hildenbrand scb->epdx += 1; 2841575767eSDavid Hildenbrand } 2851575767eSDavid Hildenbrand } 2861575767eSDavid Hildenbrand 287fdf03650SFan Zhang /* 288fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 289fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 290fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 291fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 292fdf03650SFan Zhang */ 293fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 294fdf03650SFan Zhang void *v) 295fdf03650SFan Zhang { 296fdf03650SFan Zhang struct kvm *kvm; 297fdf03650SFan Zhang struct kvm_vcpu *vcpu; 29846808a4cSMarc Zyngier unsigned long i; 299fdf03650SFan Zhang unsigned long long *delta = v; 300fdf03650SFan Zhang 301fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 302fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 3031575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 3041575767eSDavid Hildenbrand if (i == 0) { 3051575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 3061575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 3071575767eSDavid Hildenbrand } 308db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 309db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 31091473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 3111575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 3121575767eSDavid Hildenbrand *delta); 313fdf03650SFan Zhang } 314fdf03650SFan Zhang } 315fdf03650SFan Zhang return NOTIFY_OK; 316fdf03650SFan Zhang } 317fdf03650SFan Zhang 318fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 319fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 320fdf03650SFan Zhang }; 321fdf03650SFan Zhang 322b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque) 323b0c632dbSHeiko Carstens { 3242c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 325b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 326a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 327a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 328fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 329fdf03650SFan Zhang &kvm_clock_notifier); 330b0c632dbSHeiko Carstens return 0; 331b0c632dbSHeiko Carstens } 332b0c632dbSHeiko Carstens 333b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 334b0c632dbSHeiko Carstens { 335b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 336a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 337fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 338fdf03650SFan Zhang &kvm_clock_notifier); 339b0c632dbSHeiko Carstens } 340b0c632dbSHeiko Carstens 34122be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 34222be5a13SDavid Hildenbrand { 34322be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 34422be5a13SDavid Hildenbrand } 34522be5a13SDavid Hildenbrand 3460a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3470a763c78SDavid Hildenbrand { 3484fa3b91bSHeiko Carstens unsigned long function = (unsigned long)nr | 0x100; 349d051ae53SHeiko Carstens int cc; 3500a763c78SDavid Hildenbrand 3510a763c78SDavid Hildenbrand asm volatile( 3524fa3b91bSHeiko Carstens " lgr 0,%[function]\n" 3530a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3540a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3550a763c78SDavid Hildenbrand " ipm %0\n" 3560a763c78SDavid Hildenbrand " srl %0,28\n" 3570a763c78SDavid Hildenbrand : "=d" (cc) 3584fa3b91bSHeiko Carstens : [function] "d" (function) 3594fa3b91bSHeiko Carstens : "cc", "0"); 3600a763c78SDavid Hildenbrand return cc == 0; 3610a763c78SDavid Hildenbrand } 3620a763c78SDavid Hildenbrand 363d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 364d6681397SChristian Borntraeger { 365d6681397SChristian Borntraeger asm volatile( 3664fa3b91bSHeiko Carstens " lghi 0,0\n" 3674fa3b91bSHeiko Carstens " lgr 1,%[query]\n" 3684fa3b91bSHeiko Carstens /* Parameter registers are ignored */ 369d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 370b1c41ac3SHeiko Carstens : 3714fa3b91bSHeiko Carstens : [query] "d" ((unsigned long)query), [opc] "i" (opcode) 3724fa3b91bSHeiko Carstens : "cc", "memory", "0", "1"); 373d6681397SChristian Borntraeger } 374d6681397SChristian Borntraeger 375173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3764f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 377173aec2dSChristian Borntraeger 37822be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 37922be5a13SDavid Hildenbrand { 3800a763c78SDavid Hildenbrand int i; 3810a763c78SDavid Hildenbrand 3820a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3830a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3840a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3850a763c78SDavid Hildenbrand } 3860a763c78SDavid Hildenbrand 3870a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 388221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 389221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 390221bb8a4SLinus Torvalds PTFF_QAF); 3910a763c78SDavid Hildenbrand 3920a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 39369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 39469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 39569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 39669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 39769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 39869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 39969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 40069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 40169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 40269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 4030a763c78SDavid Hildenbrand } 4040a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 40569c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 40669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 4070a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 40869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 40969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 41069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 41169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 41269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 41369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 41469c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 41569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 4160a763c78SDavid Hildenbrand } 4170a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 418985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 41969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 4200a763c78SDavid Hildenbrand 421e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 422e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 423e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 424e000b8e0SJason J. Herne 42513209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 42613209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 42713209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 42813209ad0SChristian Borntraeger 429173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 430173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 431173aec2dSChristian Borntraeger 4324f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4334f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4344f45b90eSChristian Borntraeger 43522be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 43622be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 437a3508fbeSDavid Hildenbrand /* 438a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 439a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 440a3508fbeSDavid Hildenbrand */ 441a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 442a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 443a3508fbeSDavid Hildenbrand return; 444a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 44519c439b5SDavid Hildenbrand if (sclp.has_64bscao) 44619c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4470615a326SDavid Hildenbrand if (sclp.has_siif) 4480615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 44977d18f6dSDavid Hildenbrand if (sclp.has_gpere) 45077d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 451a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 452a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4535630a8e8SDavid Hildenbrand if (sclp.has_ib) 4545630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 45513ee3f67SDavid Hildenbrand if (sclp.has_cei) 45613ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4577fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4587fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 459730cd632SFarhan Ali if (sclp.has_kss) 460730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4615d3876a8SDavid Hildenbrand /* 4625d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4635d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4645d3876a8SDavid Hildenbrand * instead of the real storage key. 4655d3876a8SDavid Hildenbrand * 4665d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4675d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4685d3876a8SDavid Hildenbrand * 4695d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4705d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4715d3876a8SDavid Hildenbrand * 4725d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4735d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4745d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4755d3876a8SDavid Hildenbrand * 4765d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4775d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4785d3876a8SDavid Hildenbrand */ 47922be5a13SDavid Hildenbrand } 48022be5a13SDavid Hildenbrand 481b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 482b0c632dbSHeiko Carstens { 483f76f6371SJanosch Frank int rc = -ENOMEM; 484308c3e66SMichael Mueller 48578f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 48678f26131SChristian Borntraeger if (!kvm_s390_dbf) 48778f26131SChristian Borntraeger return -ENOMEM; 48878f26131SChristian Borntraeger 4893e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); 4903e6c5568SJanosch Frank if (!kvm_s390_dbf_uv) 4913e6c5568SJanosch Frank goto out; 4923e6c5568SJanosch Frank 4933e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || 4943e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) 495f76f6371SJanosch Frank goto out; 49678f26131SChristian Borntraeger 49722be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 49822be5a13SDavid Hildenbrand 49984877d93SCornelia Huck /* Register floating interrupt controller interface. */ 500308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 501308c3e66SMichael Mueller if (rc) { 5028d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 503f76f6371SJanosch Frank goto out; 504308c3e66SMichael Mueller } 505b1d1e76eSMichael Mueller 506b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 507b1d1e76eSMichael Mueller if (rc) 508f76f6371SJanosch Frank goto out; 509b1d1e76eSMichael Mueller 510308c3e66SMichael Mueller return 0; 511308c3e66SMichael Mueller 512f76f6371SJanosch Frank out: 513f76f6371SJanosch Frank kvm_arch_exit(); 514308c3e66SMichael Mueller return rc; 515b0c632dbSHeiko Carstens } 516b0c632dbSHeiko Carstens 51778f26131SChristian Borntraeger void kvm_arch_exit(void) 51878f26131SChristian Borntraeger { 5191282c21eSMichael Mueller kvm_s390_gib_destroy(); 52078f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 5213e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv); 52278f26131SChristian Borntraeger } 52378f26131SChristian Borntraeger 524b0c632dbSHeiko Carstens /* Section: device related */ 525b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 526b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 527b0c632dbSHeiko Carstens { 528b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 529b0c632dbSHeiko Carstens return s390_enable_sie(); 530b0c632dbSHeiko Carstens return -EINVAL; 531b0c632dbSHeiko Carstens } 532b0c632dbSHeiko Carstens 533784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 534b0c632dbSHeiko Carstens { 535d7b0b5ebSCarsten Otte int r; 536d7b0b5ebSCarsten Otte 5372bd0ac4eSCarsten Otte switch (ext) { 538d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 539b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 54052e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5411efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5421efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5431efd0f59SCarsten Otte #endif 5443c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 54560b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 54614eebd91SCarsten Otte case KVM_CAP_ONE_REG: 547d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 548fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 54910ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 550c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 55178599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 552f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5536352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 554460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 55547b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5562444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 557e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 55830ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 559816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5606502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5614036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 56247a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 563da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 5647de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS: 565b9b2782cSPeter Xu case KVM_CAP_SET_GUEST_DEBUG: 56623a60f83SCollin Walling case KVM_CAP_S390_DIAG318: 567d004079eSJanis Schoetterl-Glausch case KVM_CAP_S390_MEM_OP_EXTENSION: 568d7b0b5ebSCarsten Otte r = 1; 569d7b0b5ebSCarsten Otte break; 570a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 571a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK; 572a43b80b7SMaxim Levitsky break; 573a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 574a4499382SJanosch Frank r = 0; 57540ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 576a4499382SJanosch Frank r = 1; 577a4499382SJanosch Frank break; 57841408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 57941408c28SThomas Huth r = MEM_OP_MAX_SIZE; 58041408c28SThomas Huth break; 581e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 582e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 583a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 58476a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 585a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 586a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 587a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 58876a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 58982cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS) 59082cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r); 591e726b1bdSChristian Borntraeger break; 5921526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 593abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 5941526bf9cSChristian Borntraeger break; 59568c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 59668c55750SEric Farman r = MACHINE_HAS_VX; 59768c55750SEric Farman break; 598c6e5f166SFan Zhang case KVM_CAP_S390_RI: 599c6e5f166SFan Zhang r = test_facility(64); 600c6e5f166SFan Zhang break; 6014e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 6024e0b1ab7SFan Zhang r = test_facility(133); 6034e0b1ab7SFan Zhang break; 60435b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 60535b3fde6SChristian Borntraeger r = test_facility(82); 60635b3fde6SChristian Borntraeger break; 60713da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED: 60813da9ae1SChristian Borntraeger r = is_prot_virt_host(); 60913da9ae1SChristian Borntraeger break; 6102bd0ac4eSCarsten Otte default: 611d7b0b5ebSCarsten Otte r = 0; 612b0c632dbSHeiko Carstens } 613d7b0b5ebSCarsten Otte return r; 6142bd0ac4eSCarsten Otte } 615b0c632dbSHeiko Carstens 6160dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 61715f36ebdSJason J. Herne { 6180959e168SJanosch Frank int i; 61915f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 6200959e168SJanosch Frank unsigned long gaddr, vmaddr; 62115f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 6220959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 62315f36ebdSJason J. Herne 6240959e168SJanosch Frank /* Loop over all guest segments */ 6250959e168SJanosch Frank cur_gfn = memslot->base_gfn; 62615f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 6270959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 6280959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 6290959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 6300959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 6310959e168SJanosch Frank continue; 63215f36ebdSJason J. Herne 6330959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6340959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6350959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6360959e168SJanosch Frank if (test_bit(i, bitmap)) 6370959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6380959e168SJanosch Frank } 6390959e168SJanosch Frank 6401763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6411763f8d0SChristian Borntraeger return; 64270c88a00SChristian Borntraeger cond_resched(); 64315f36ebdSJason J. Herne } 64415f36ebdSJason J. Herne } 64515f36ebdSJason J. Herne 646b0c632dbSHeiko Carstens /* Section: vm related */ 647a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 648a6e2f683SEugene (jno) Dvurechenski 649b0c632dbSHeiko Carstens /* 650b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 651b0c632dbSHeiko Carstens */ 652b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 653b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 654b0c632dbSHeiko Carstens { 65515f36ebdSJason J. Herne int r; 65615f36ebdSJason J. Herne unsigned long n; 65715f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 6582a49f61dSSean Christopherson int is_dirty; 65915f36ebdSJason J. Herne 660e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 661e1e8a962SJanosch Frank return -EINVAL; 662e1e8a962SJanosch Frank 66315f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 66415f36ebdSJason J. Herne 66515f36ebdSJason J. Herne r = -EINVAL; 66615f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 66715f36ebdSJason J. Herne goto out; 66815f36ebdSJason J. Herne 6692a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); 67015f36ebdSJason J. Herne if (r) 67115f36ebdSJason J. Herne goto out; 67215f36ebdSJason J. Herne 67315f36ebdSJason J. Herne /* Clear the dirty log */ 67415f36ebdSJason J. Herne if (is_dirty) { 67515f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 67615f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 67715f36ebdSJason J. Herne } 67815f36ebdSJason J. Herne r = 0; 67915f36ebdSJason J. Herne out: 68015f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 68115f36ebdSJason J. Herne return r; 682b0c632dbSHeiko Carstens } 683b0c632dbSHeiko Carstens 6846502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 6856502a34cSDavid Hildenbrand { 68646808a4cSMarc Zyngier unsigned long i; 6876502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 6886502a34cSDavid Hildenbrand 6896502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 6906502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 6916502a34cSDavid Hildenbrand } 6926502a34cSDavid Hildenbrand } 6936502a34cSDavid Hildenbrand 694e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 695d938dc55SCornelia Huck { 696d938dc55SCornelia Huck int r; 697d938dc55SCornelia Huck 698d938dc55SCornelia Huck if (cap->flags) 699d938dc55SCornelia Huck return -EINVAL; 700d938dc55SCornelia Huck 701d938dc55SCornelia Huck switch (cap->cap) { 70284223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 703c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 70484223598SCornelia Huck kvm->arch.use_irqchip = 1; 70584223598SCornelia Huck r = 0; 70684223598SCornelia Huck break; 7072444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 708c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 7092444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 7102444b352SDavid Hildenbrand r = 0; 7112444b352SDavid Hildenbrand break; 71268c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 7135967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 714a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 7155967c17bSDavid Hildenbrand r = -EBUSY; 7165967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 717c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 718c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 7192f87d942SGuenther Hutzl if (test_facility(134)) { 7202f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 7212f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 7222f87d942SGuenther Hutzl } 72353743aa7SMaxim Samoylov if (test_facility(135)) { 72453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 72553743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 72653743aa7SMaxim Samoylov } 7277832e91cSChristian Borntraeger if (test_facility(148)) { 7287832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7297832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7307832e91cSChristian Borntraeger } 731d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 732d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 733d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 734d5cb6ab1SChristian Borntraeger } 7351f703d2cSChristian Borntraeger if (test_facility(192)) { 7361f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192); 7371f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192); 7381f703d2cSChristian Borntraeger } 73918280d8bSMichael Mueller r = 0; 74018280d8bSMichael Mueller } else 74118280d8bSMichael Mueller r = -EINVAL; 7425967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 743c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 744c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 74568c55750SEric Farman break; 746c6e5f166SFan Zhang case KVM_CAP_S390_RI: 747c6e5f166SFan Zhang r = -EINVAL; 748c6e5f166SFan Zhang mutex_lock(&kvm->lock); 749a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 750c6e5f166SFan Zhang r = -EBUSY; 751c6e5f166SFan Zhang } else if (test_facility(64)) { 752c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 753c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 754c6e5f166SFan Zhang r = 0; 755c6e5f166SFan Zhang } 756c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 757c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 758c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 759c6e5f166SFan Zhang break; 76047a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 76147a4693eSYi Min Zhao mutex_lock(&kvm->lock); 76247a4693eSYi Min Zhao if (kvm->created_vcpus) { 76347a4693eSYi Min Zhao r = -EBUSY; 76447a4693eSYi Min Zhao } else { 76547a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 76647a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 76747a4693eSYi Min Zhao r = 0; 76847a4693eSYi Min Zhao } 76947a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 77047a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 77147a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 77247a4693eSYi Min Zhao break; 7734e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 7744e0b1ab7SFan Zhang r = -EINVAL; 7754e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 776241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 7774e0b1ab7SFan Zhang r = -EBUSY; 7784e0b1ab7SFan Zhang } else if (test_facility(133)) { 7794e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 7804e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 7814e0b1ab7SFan Zhang r = 0; 7824e0b1ab7SFan Zhang } 7834e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 7844e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 7854e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 7864e0b1ab7SFan Zhang break; 787a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 788a4499382SJanosch Frank mutex_lock(&kvm->lock); 789a4499382SJanosch Frank if (kvm->created_vcpus) 790a4499382SJanosch Frank r = -EBUSY; 79140ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 792a4499382SJanosch Frank r = -EINVAL; 793a4499382SJanosch Frank else { 794a4499382SJanosch Frank r = 0; 795d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 796a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 797d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 798a4499382SJanosch Frank /* 799a4499382SJanosch Frank * We might have to create fake 4k page 800a4499382SJanosch Frank * tables. To avoid that the hardware works on 801a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 802a4499382SJanosch Frank */ 803a4499382SJanosch Frank kvm->arch.use_skf = 0; 804a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 805a4499382SJanosch Frank } 806a4499382SJanosch Frank mutex_unlock(&kvm->lock); 807a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 808a4499382SJanosch Frank r ? "(not available)" : "(success)"); 809a4499382SJanosch Frank break; 810e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 811c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 812e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 813e44fc8c9SEkaterina Tumanova r = 0; 814e44fc8c9SEkaterina Tumanova break; 8156502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 8166502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 8176502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 8186502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 8196502a34cSDavid Hildenbrand r = 0; 8206502a34cSDavid Hildenbrand break; 821d938dc55SCornelia Huck default: 822d938dc55SCornelia Huck r = -EINVAL; 823d938dc55SCornelia Huck break; 824d938dc55SCornelia Huck } 825d938dc55SCornelia Huck return r; 826d938dc55SCornelia Huck } 827d938dc55SCornelia Huck 8288c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8298c0a7ce6SDominik Dingel { 8308c0a7ce6SDominik Dingel int ret; 8318c0a7ce6SDominik Dingel 8328c0a7ce6SDominik Dingel switch (attr->attr) { 8338c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8348c0a7ce6SDominik Dingel ret = 0; 835c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 836a3a92c31SDominik Dingel kvm->arch.mem_limit); 837a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8388c0a7ce6SDominik Dingel ret = -EFAULT; 8398c0a7ce6SDominik Dingel break; 8408c0a7ce6SDominik Dingel default: 8418c0a7ce6SDominik Dingel ret = -ENXIO; 8428c0a7ce6SDominik Dingel break; 8438c0a7ce6SDominik Dingel } 8448c0a7ce6SDominik Dingel return ret; 8458c0a7ce6SDominik Dingel } 8468c0a7ce6SDominik Dingel 8478c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8484f718eabSDominik Dingel { 8494f718eabSDominik Dingel int ret; 8504f718eabSDominik Dingel unsigned int idx; 8514f718eabSDominik Dingel switch (attr->attr) { 8524f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 853f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 854c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 855e6db1d61SDominik Dingel break; 856e6db1d61SDominik Dingel 857c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 8584f718eabSDominik Dingel mutex_lock(&kvm->lock); 859a4499382SJanosch Frank if (kvm->created_vcpus) 860a4499382SJanosch Frank ret = -EBUSY; 861a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 862a4499382SJanosch Frank ret = -EINVAL; 863a4499382SJanosch Frank else { 8644f718eabSDominik Dingel kvm->arch.use_cmma = 1; 865c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 866c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 8674f718eabSDominik Dingel ret = 0; 8684f718eabSDominik Dingel } 8694f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8704f718eabSDominik Dingel break; 8714f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 872f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 873f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 874f9cbd9b0SDavid Hildenbrand break; 875c3489155SDominik Dingel ret = -EINVAL; 876c3489155SDominik Dingel if (!kvm->arch.use_cmma) 877c3489155SDominik Dingel break; 878c3489155SDominik Dingel 879c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 8804f718eabSDominik Dingel mutex_lock(&kvm->lock); 8814f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 882a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 8834f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 8844f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8854f718eabSDominik Dingel ret = 0; 8864f718eabSDominik Dingel break; 8878c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 8888c0a7ce6SDominik Dingel unsigned long new_limit; 8898c0a7ce6SDominik Dingel 8908c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 8918c0a7ce6SDominik Dingel return -EINVAL; 8928c0a7ce6SDominik Dingel 8938c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 8948c0a7ce6SDominik Dingel return -EFAULT; 8958c0a7ce6SDominik Dingel 896a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 897a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 8988c0a7ce6SDominik Dingel return -E2BIG; 8998c0a7ce6SDominik Dingel 900a3a92c31SDominik Dingel if (!new_limit) 901a3a92c31SDominik Dingel return -EINVAL; 902a3a92c31SDominik Dingel 9036ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 904a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 905a3a92c31SDominik Dingel new_limit -= 1; 906a3a92c31SDominik Dingel 9078c0a7ce6SDominik Dingel ret = -EBUSY; 9088c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 909a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 9106ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 9116ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 9128c0a7ce6SDominik Dingel 9138c0a7ce6SDominik Dingel if (!new) { 9148c0a7ce6SDominik Dingel ret = -ENOMEM; 9158c0a7ce6SDominik Dingel } else { 9166ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 9178c0a7ce6SDominik Dingel new->private = kvm; 9188c0a7ce6SDominik Dingel kvm->arch.gmap = new; 9198c0a7ce6SDominik Dingel ret = 0; 9208c0a7ce6SDominik Dingel } 9218c0a7ce6SDominik Dingel } 9228c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 923a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 924a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 925a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 9268c0a7ce6SDominik Dingel break; 9278c0a7ce6SDominik Dingel } 9284f718eabSDominik Dingel default: 9294f718eabSDominik Dingel ret = -ENXIO; 9304f718eabSDominik Dingel break; 9314f718eabSDominik Dingel } 9324f718eabSDominik Dingel return ret; 9334f718eabSDominik Dingel } 9344f718eabSDominik Dingel 935a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 936a374e892STony Krowiak 93720c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 938a374e892STony Krowiak { 939a374e892STony Krowiak struct kvm_vcpu *vcpu; 94046808a4cSMarc Zyngier unsigned long i; 941a374e892STony Krowiak 94220c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 94320c922f0STony Krowiak 9443194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 94520c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 9463194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 9473194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 9483194cdb7SDavid Hildenbrand } 94920c922f0STony Krowiak 95020c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 95120c922f0STony Krowiak } 95220c922f0STony Krowiak 95320c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 95420c922f0STony Krowiak { 955a374e892STony Krowiak mutex_lock(&kvm->lock); 956a374e892STony Krowiak switch (attr->attr) { 957a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 9588e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9598e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 96037940fb0STony Krowiak return -EINVAL; 9618e41bd54SChristian Borntraeger } 962a374e892STony Krowiak get_random_bytes( 963a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 964a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 965a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 966c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 967a374e892STony Krowiak break; 968a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 9698e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9708e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 97137940fb0STony Krowiak return -EINVAL; 9728e41bd54SChristian Borntraeger } 973a374e892STony Krowiak get_random_bytes( 974a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 975a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 976a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 977c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 978a374e892STony Krowiak break; 979a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 9808e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9818e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 98237940fb0STony Krowiak return -EINVAL; 9838e41bd54SChristian Borntraeger } 984a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 985a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 986a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 987c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 988a374e892STony Krowiak break; 989a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 9908e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9918e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 99237940fb0STony Krowiak return -EINVAL; 9938e41bd54SChristian Borntraeger } 994a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 995a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 996a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 997c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 998a374e892STony Krowiak break; 99937940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 100037940fb0STony Krowiak if (!ap_instructions_available()) { 100137940fb0STony Krowiak mutex_unlock(&kvm->lock); 100237940fb0STony Krowiak return -EOPNOTSUPP; 100337940fb0STony Krowiak } 100437940fb0STony Krowiak kvm->arch.crypto.apie = 1; 100537940fb0STony Krowiak break; 100637940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 100737940fb0STony Krowiak if (!ap_instructions_available()) { 100837940fb0STony Krowiak mutex_unlock(&kvm->lock); 100937940fb0STony Krowiak return -EOPNOTSUPP; 101037940fb0STony Krowiak } 101137940fb0STony Krowiak kvm->arch.crypto.apie = 0; 101237940fb0STony Krowiak break; 1013a374e892STony Krowiak default: 1014a374e892STony Krowiak mutex_unlock(&kvm->lock); 1015a374e892STony Krowiak return -ENXIO; 1016a374e892STony Krowiak } 1017a374e892STony Krowiak 101820c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 1019a374e892STony Krowiak mutex_unlock(&kvm->lock); 1020a374e892STony Krowiak return 0; 1021a374e892STony Krowiak } 1022a374e892STony Krowiak 1023190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 1024190df4a2SClaudio Imbrenda { 102546808a4cSMarc Zyngier unsigned long cx; 1026190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 1027190df4a2SClaudio Imbrenda 1028190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1029190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1030190df4a2SClaudio Imbrenda } 1031190df4a2SClaudio Imbrenda 1032190df4a2SClaudio Imbrenda /* 1033190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 10341de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1035190df4a2SClaudio Imbrenda */ 1036190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1037190df4a2SClaudio Imbrenda { 1038190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1039190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1040afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1041a54d8066SMaciej S. Szmigiero int bkt; 1042190df4a2SClaudio Imbrenda 1043190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1044afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1045190df4a2SClaudio Imbrenda return 0; 1046190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1047a54d8066SMaciej S. Szmigiero if (!slots || kvm_memslots_empty(slots)) 1048190df4a2SClaudio Imbrenda return -EINVAL; 1049190df4a2SClaudio Imbrenda 1050afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1051afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1052afdad616SClaudio Imbrenda return 0; 1053190df4a2SClaudio Imbrenda } 1054190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1055a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(ms, bkt, slots) { 105613a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 105713a17cc0SIgor Mammedov return -EINVAL; 1058afdad616SClaudio Imbrenda /* 1059afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1060afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1061afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1062afdad616SClaudio Imbrenda * attributes. 1063afdad616SClaudio Imbrenda */ 1064afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1065afdad616SClaudio Imbrenda ram_pages += ms->npages; 1066190df4a2SClaudio Imbrenda } 1067afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1068afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1069190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1070190df4a2SClaudio Imbrenda return 0; 1071190df4a2SClaudio Imbrenda } 1072190df4a2SClaudio Imbrenda 1073190df4a2SClaudio Imbrenda /* 10741de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1075190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1076190df4a2SClaudio Imbrenda */ 1077190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1078190df4a2SClaudio Imbrenda { 1079190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1080afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1081190df4a2SClaudio Imbrenda return 0; 1082afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1083afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1084190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1085190df4a2SClaudio Imbrenda return 0; 1086190df4a2SClaudio Imbrenda } 1087190df4a2SClaudio Imbrenda 1088190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1089190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1090190df4a2SClaudio Imbrenda { 10911de1ea7eSChristian Borntraeger int res = -ENXIO; 1092190df4a2SClaudio Imbrenda 10931de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1094190df4a2SClaudio Imbrenda switch (attr->attr) { 1095190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1096190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1097190df4a2SClaudio Imbrenda break; 1098190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1099190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1100190df4a2SClaudio Imbrenda break; 1101190df4a2SClaudio Imbrenda default: 1102190df4a2SClaudio Imbrenda break; 1103190df4a2SClaudio Imbrenda } 11041de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1105190df4a2SClaudio Imbrenda 1106190df4a2SClaudio Imbrenda return res; 1107190df4a2SClaudio Imbrenda } 1108190df4a2SClaudio Imbrenda 1109190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1110190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1111190df4a2SClaudio Imbrenda { 1112afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1113190df4a2SClaudio Imbrenda 1114190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1115190df4a2SClaudio Imbrenda return -ENXIO; 1116190df4a2SClaudio Imbrenda 1117190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1118190df4a2SClaudio Imbrenda return -EFAULT; 1119190df4a2SClaudio Imbrenda return 0; 1120190df4a2SClaudio Imbrenda } 1121190df4a2SClaudio Imbrenda 11228fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 11238fa1696eSCollin L. Walling { 11248fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 11258fa1696eSCollin L. Walling 11268fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 11278fa1696eSCollin L. Walling return -EFAULT; 11288fa1696eSCollin L. Walling 11290e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 11308fa1696eSCollin L. Walling return -EINVAL; 11310e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11328fa1696eSCollin L. Walling 11338fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 11348fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11358fa1696eSCollin L. Walling 11368fa1696eSCollin L. Walling return 0; 11378fa1696eSCollin L. Walling } 11388fa1696eSCollin L. Walling 113972f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 114072f25020SJason J. Herne { 114172f25020SJason J. Herne u8 gtod_high; 114272f25020SJason J. Herne 114372f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 114472f25020SJason J. Herne sizeof(gtod_high))) 114572f25020SJason J. Herne return -EFAULT; 114672f25020SJason J. Herne 114772f25020SJason J. Herne if (gtod_high != 0) 114872f25020SJason J. Herne return -EINVAL; 114958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 115072f25020SJason J. Herne 115172f25020SJason J. Herne return 0; 115272f25020SJason J. Herne } 115372f25020SJason J. Herne 115472f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 115572f25020SJason J. Herne { 11560e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 115772f25020SJason J. Herne 11580e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 11590e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 116072f25020SJason J. Herne return -EFAULT; 116172f25020SJason J. Herne 11620e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11630e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 116472f25020SJason J. Herne return 0; 116572f25020SJason J. Herne } 116672f25020SJason J. Herne 116772f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 116872f25020SJason J. Herne { 116972f25020SJason J. Herne int ret; 117072f25020SJason J. Herne 117172f25020SJason J. Herne if (attr->flags) 117272f25020SJason J. Herne return -EINVAL; 117372f25020SJason J. Herne 117472f25020SJason J. Herne switch (attr->attr) { 11758fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 11768fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 11778fa1696eSCollin L. Walling break; 117872f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 117972f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 118072f25020SJason J. Herne break; 118172f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 118272f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 118372f25020SJason J. Herne break; 118472f25020SJason J. Herne default: 118572f25020SJason J. Herne ret = -ENXIO; 118672f25020SJason J. Herne break; 118772f25020SJason J. Herne } 118872f25020SJason J. Herne return ret; 118972f25020SJason J. Herne } 119072f25020SJason J. Herne 119133d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 11928fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 11938fa1696eSCollin L. Walling { 11942cfd7b73SHeiko Carstens union tod_clock clk; 11958fa1696eSCollin L. Walling 11968fa1696eSCollin L. Walling preempt_disable(); 11978fa1696eSCollin L. Walling 11982cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 11998fa1696eSCollin L. Walling 12002cfd7b73SHeiko Carstens gtod->tod = clk.tod + kvm->arch.epoch; 120133d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 120233d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 12032cfd7b73SHeiko Carstens gtod->epoch_idx = clk.ei + kvm->arch.epdx; 12042cfd7b73SHeiko Carstens if (gtod->tod < clk.tod) 12058fa1696eSCollin L. Walling gtod->epoch_idx += 1; 120633d1b272SDavid Hildenbrand } 12078fa1696eSCollin L. Walling 12088fa1696eSCollin L. Walling preempt_enable(); 12098fa1696eSCollin L. Walling } 12108fa1696eSCollin L. Walling 12118fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 12128fa1696eSCollin L. Walling { 12138fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 12148fa1696eSCollin L. Walling 12158fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 121633d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 12178fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 12188fa1696eSCollin L. Walling return -EFAULT; 12198fa1696eSCollin L. Walling 12208fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 12218fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 12228fa1696eSCollin L. Walling return 0; 12238fa1696eSCollin L. Walling } 12248fa1696eSCollin L. Walling 122572f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 122672f25020SJason J. Herne { 122772f25020SJason J. Herne u8 gtod_high = 0; 122872f25020SJason J. Herne 122972f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 123072f25020SJason J. Herne sizeof(gtod_high))) 123172f25020SJason J. Herne return -EFAULT; 123258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 123372f25020SJason J. Herne 123472f25020SJason J. Herne return 0; 123572f25020SJason J. Herne } 123672f25020SJason J. Herne 123772f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 123872f25020SJason J. Herne { 12395a3d883aSDavid Hildenbrand u64 gtod; 124072f25020SJason J. Herne 124160417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 124272f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 124372f25020SJason J. Herne return -EFAULT; 124458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 124572f25020SJason J. Herne 124672f25020SJason J. Herne return 0; 124772f25020SJason J. Herne } 124872f25020SJason J. Herne 124972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 125072f25020SJason J. Herne { 125172f25020SJason J. Herne int ret; 125272f25020SJason J. Herne 125372f25020SJason J. Herne if (attr->flags) 125472f25020SJason J. Herne return -EINVAL; 125572f25020SJason J. Herne 125672f25020SJason J. Herne switch (attr->attr) { 12578fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12588fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 12598fa1696eSCollin L. Walling break; 126072f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 126172f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 126272f25020SJason J. Herne break; 126372f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 126472f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 126572f25020SJason J. Herne break; 126672f25020SJason J. Herne default: 126772f25020SJason J. Herne ret = -ENXIO; 126872f25020SJason J. Herne break; 126972f25020SJason J. Herne } 127072f25020SJason J. Herne return ret; 127172f25020SJason J. Herne } 127272f25020SJason J. Herne 1273658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1274658b6edaSMichael Mueller { 1275658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1276053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1277658b6edaSMichael Mueller int ret = 0; 1278658b6edaSMichael Mueller 1279658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1280a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1281658b6edaSMichael Mueller ret = -EBUSY; 1282658b6edaSMichael Mueller goto out; 1283658b6edaSMichael Mueller } 1284c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1285658b6edaSMichael Mueller if (!proc) { 1286658b6edaSMichael Mueller ret = -ENOMEM; 1287658b6edaSMichael Mueller goto out; 1288658b6edaSMichael Mueller } 1289658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1290658b6edaSMichael Mueller sizeof(*proc))) { 12919bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1292053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1293053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 12940487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1295053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1296053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1297053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1298053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1299053dd230SDavid Hildenbrand else 1300658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1301053dd230SDavid Hildenbrand } 1302c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1303658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1304a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1305a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1306a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1307a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1308a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1309a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1310a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1311658b6edaSMichael Mueller } else 1312658b6edaSMichael Mueller ret = -EFAULT; 1313658b6edaSMichael Mueller kfree(proc); 1314658b6edaSMichael Mueller out: 1315658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1316658b6edaSMichael Mueller return ret; 1317658b6edaSMichael Mueller } 1318658b6edaSMichael Mueller 131915c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 132015c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 132115c9705fSDavid Hildenbrand { 132215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 132315c9705fSDavid Hildenbrand 132415c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 132515c9705fSDavid Hildenbrand return -EFAULT; 132615c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 132715c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 132815c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 132915c9705fSDavid Hildenbrand return -EINVAL; 133015c9705fSDavid Hildenbrand 133115c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 13322f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 13332f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 13342f8311c9SChristian Borntraeger return -EBUSY; 13352f8311c9SChristian Borntraeger } 133615c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 133715c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 133815c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 13392f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 13402f8311c9SChristian Borntraeger data.feat[0], 13412f8311c9SChristian Borntraeger data.feat[1], 13422f8311c9SChristian Borntraeger data.feat[2]); 13432f8311c9SChristian Borntraeger return 0; 134415c9705fSDavid Hildenbrand } 134515c9705fSDavid Hildenbrand 13460a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 13470a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 13480a763c78SDavid Hildenbrand { 1349346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1350346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1351346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1352346fa2f8SChristian Borntraeger return -EBUSY; 1353346fa2f8SChristian Borntraeger } 1354346fa2f8SChristian Borntraeger 1355346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1356346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1357346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1358346fa2f8SChristian Borntraeger return -EFAULT; 1359346fa2f8SChristian Borntraeger } 1360346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1361346fa2f8SChristian Borntraeger 136211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 136311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 136411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 136511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 136611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 136711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 136811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 136911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 137011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 137111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 137211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 137311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 137411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 137511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 137611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 137711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 137811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 137911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 138011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 138111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 138211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 138311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 138411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 138511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 138611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 138711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 138811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 138911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 139011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 139111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 139211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 139311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 139411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 139511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 139611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 139711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 139811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 139911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 140011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 140111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 140211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 140311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 140411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 140511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 140613209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 140713209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 140813209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1409173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1410173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1411173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1412173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1413173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 14144f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 14154f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 14164f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 14174f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 14184f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 141911ba5961SChristian Borntraeger 1420346fa2f8SChristian Borntraeger return 0; 14210a763c78SDavid Hildenbrand } 14220a763c78SDavid Hildenbrand 1423658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1424658b6edaSMichael Mueller { 1425658b6edaSMichael Mueller int ret = -ENXIO; 1426658b6edaSMichael Mueller 1427658b6edaSMichael Mueller switch (attr->attr) { 1428658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1429658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1430658b6edaSMichael Mueller break; 143115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 143215c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 143315c9705fSDavid Hildenbrand break; 14340a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 14350a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 14360a763c78SDavid Hildenbrand break; 1437658b6edaSMichael Mueller } 1438658b6edaSMichael Mueller return ret; 1439658b6edaSMichael Mueller } 1440658b6edaSMichael Mueller 1441658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1442658b6edaSMichael Mueller { 1443658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1444658b6edaSMichael Mueller int ret = 0; 1445658b6edaSMichael Mueller 1446c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1447658b6edaSMichael Mueller if (!proc) { 1448658b6edaSMichael Mueller ret = -ENOMEM; 1449658b6edaSMichael Mueller goto out; 1450658b6edaSMichael Mueller } 14519bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1452658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1453c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1454c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1455a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1456a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1457a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1458a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1459a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1460a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1461a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1462658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1463658b6edaSMichael Mueller ret = -EFAULT; 1464658b6edaSMichael Mueller kfree(proc); 1465658b6edaSMichael Mueller out: 1466658b6edaSMichael Mueller return ret; 1467658b6edaSMichael Mueller } 1468658b6edaSMichael Mueller 1469658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1470658b6edaSMichael Mueller { 1471658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1472658b6edaSMichael Mueller int ret = 0; 1473658b6edaSMichael Mueller 1474c4196218SChristian Borntraeger mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT); 1475658b6edaSMichael Mueller if (!mach) { 1476658b6edaSMichael Mueller ret = -ENOMEM; 1477658b6edaSMichael Mueller goto out; 1478658b6edaSMichael Mueller } 1479658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 148037c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1481c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1482981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 148317e89e13SSven Schnelle memcpy((unsigned long *)&mach->fac_list, stfle_fac_list, 148417e89e13SSven Schnelle sizeof(stfle_fac_list)); 1485a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1486a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1487a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1488a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1489a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1490a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1491a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1492a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1493a8c39dd7SChristian Borntraeger mach->fac_list[0], 1494a8c39dd7SChristian Borntraeger mach->fac_list[1], 1495a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1496658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1497658b6edaSMichael Mueller ret = -EFAULT; 1498658b6edaSMichael Mueller kfree(mach); 1499658b6edaSMichael Mueller out: 1500658b6edaSMichael Mueller return ret; 1501658b6edaSMichael Mueller } 1502658b6edaSMichael Mueller 150315c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 150415c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 150515c9705fSDavid Hildenbrand { 150615c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 150715c9705fSDavid Hildenbrand 150815c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 150915c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 151015c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 151115c9705fSDavid Hildenbrand return -EFAULT; 15122f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 15132f8311c9SChristian Borntraeger data.feat[0], 15142f8311c9SChristian Borntraeger data.feat[1], 15152f8311c9SChristian Borntraeger data.feat[2]); 151615c9705fSDavid Hildenbrand return 0; 151715c9705fSDavid Hildenbrand } 151815c9705fSDavid Hildenbrand 151915c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 152015c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 152115c9705fSDavid Hildenbrand { 152215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 152315c9705fSDavid Hildenbrand 152415c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 152515c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 152615c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 152715c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 152815c9705fSDavid Hildenbrand return -EFAULT; 15292f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 15302f8311c9SChristian Borntraeger data.feat[0], 15312f8311c9SChristian Borntraeger data.feat[1], 15322f8311c9SChristian Borntraeger data.feat[2]); 153315c9705fSDavid Hildenbrand return 0; 153415c9705fSDavid Hildenbrand } 153515c9705fSDavid Hildenbrand 15360a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 15370a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15380a763c78SDavid Hildenbrand { 1539346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1540346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1541346fa2f8SChristian Borntraeger return -EFAULT; 1542346fa2f8SChristian Borntraeger 154311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 154411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 154511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 154611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 154711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 154811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 154911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 155011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 155111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 155211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 155311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 155411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 155511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 155611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 155711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 155811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 155911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 156011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 156111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 156211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 156311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 156411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 156511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 156611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 156711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 156811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 156911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 157011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 157111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 157211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 157311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 157411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 157511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 157611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 157711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 157811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 157911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 158011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 158111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 158211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 158311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 158411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 158511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 158611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 158713209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 158813209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 158913209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1590173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1591173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1592173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1593173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1594173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15954f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15964f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15974f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15984f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15994f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 160011ba5961SChristian Borntraeger 1601346fa2f8SChristian Borntraeger return 0; 16020a763c78SDavid Hildenbrand } 16030a763c78SDavid Hildenbrand 16040a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 16050a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 16060a763c78SDavid Hildenbrand { 16070a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 16080a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 16090a763c78SDavid Hildenbrand return -EFAULT; 161011ba5961SChristian Borntraeger 161111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 161211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 161311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 161411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 161511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 161611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 161711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 161811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 161911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 162011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 162111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 162211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 162311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 162411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 162511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 162611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 162711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 162811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 162911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 163011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 163111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 163211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 163311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 163411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 163511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 163611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 163711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 163811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 163911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 164011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 164111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 164211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 164311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 164411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 164511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 164611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 164711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 164811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 164911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 165011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 165111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 165211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 165311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 165411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 165513209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 165613209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 165713209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1658173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1659173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1660173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1661173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1662173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 16634f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 16644f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 16654f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 16664f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 16674f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 166811ba5961SChristian Borntraeger 16690a763c78SDavid Hildenbrand return 0; 16700a763c78SDavid Hildenbrand } 1671346fa2f8SChristian Borntraeger 1672658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1673658b6edaSMichael Mueller { 1674658b6edaSMichael Mueller int ret = -ENXIO; 1675658b6edaSMichael Mueller 1676658b6edaSMichael Mueller switch (attr->attr) { 1677658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1678658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1679658b6edaSMichael Mueller break; 1680658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1681658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1682658b6edaSMichael Mueller break; 168315c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 168415c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 168515c9705fSDavid Hildenbrand break; 168615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 168715c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 168815c9705fSDavid Hildenbrand break; 16890a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 16900a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 16910a763c78SDavid Hildenbrand break; 16920a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 16930a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 16940a763c78SDavid Hildenbrand break; 1695658b6edaSMichael Mueller } 1696658b6edaSMichael Mueller return ret; 1697658b6edaSMichael Mueller } 1698658b6edaSMichael Mueller 1699f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1700f2061656SDominik Dingel { 1701f2061656SDominik Dingel int ret; 1702f2061656SDominik Dingel 1703f2061656SDominik Dingel switch (attr->group) { 17044f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 17058c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 17064f718eabSDominik Dingel break; 170772f25020SJason J. Herne case KVM_S390_VM_TOD: 170872f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 170972f25020SJason J. Herne break; 1710658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1711658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1712658b6edaSMichael Mueller break; 1713a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1714a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1715a374e892STony Krowiak break; 1716190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1717190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1718190df4a2SClaudio Imbrenda break; 1719f2061656SDominik Dingel default: 1720f2061656SDominik Dingel ret = -ENXIO; 1721f2061656SDominik Dingel break; 1722f2061656SDominik Dingel } 1723f2061656SDominik Dingel 1724f2061656SDominik Dingel return ret; 1725f2061656SDominik Dingel } 1726f2061656SDominik Dingel 1727f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1728f2061656SDominik Dingel { 17298c0a7ce6SDominik Dingel int ret; 17308c0a7ce6SDominik Dingel 17318c0a7ce6SDominik Dingel switch (attr->group) { 17328c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 17338c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 17348c0a7ce6SDominik Dingel break; 173572f25020SJason J. Herne case KVM_S390_VM_TOD: 173672f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 173772f25020SJason J. Herne break; 1738658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1739658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1740658b6edaSMichael Mueller break; 1741190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1742190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1743190df4a2SClaudio Imbrenda break; 17448c0a7ce6SDominik Dingel default: 17458c0a7ce6SDominik Dingel ret = -ENXIO; 17468c0a7ce6SDominik Dingel break; 17478c0a7ce6SDominik Dingel } 17488c0a7ce6SDominik Dingel 17498c0a7ce6SDominik Dingel return ret; 1750f2061656SDominik Dingel } 1751f2061656SDominik Dingel 1752f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1753f2061656SDominik Dingel { 1754f2061656SDominik Dingel int ret; 1755f2061656SDominik Dingel 1756f2061656SDominik Dingel switch (attr->group) { 17574f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 17584f718eabSDominik Dingel switch (attr->attr) { 17594f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 17604f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1761f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1762f9cbd9b0SDavid Hildenbrand break; 17638c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 17644f718eabSDominik Dingel ret = 0; 17654f718eabSDominik Dingel break; 17664f718eabSDominik Dingel default: 17674f718eabSDominik Dingel ret = -ENXIO; 17684f718eabSDominik Dingel break; 17694f718eabSDominik Dingel } 17704f718eabSDominik Dingel break; 177172f25020SJason J. Herne case KVM_S390_VM_TOD: 177272f25020SJason J. Herne switch (attr->attr) { 177372f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 177472f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 177572f25020SJason J. Herne ret = 0; 177672f25020SJason J. Herne break; 177772f25020SJason J. Herne default: 177872f25020SJason J. Herne ret = -ENXIO; 177972f25020SJason J. Herne break; 178072f25020SJason J. Herne } 178172f25020SJason J. Herne break; 1782658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1783658b6edaSMichael Mueller switch (attr->attr) { 1784658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1785658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 178615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 178715c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 17880a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1789346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1790658b6edaSMichael Mueller ret = 0; 1791658b6edaSMichael Mueller break; 1792658b6edaSMichael Mueller default: 1793658b6edaSMichael Mueller ret = -ENXIO; 1794658b6edaSMichael Mueller break; 1795658b6edaSMichael Mueller } 1796658b6edaSMichael Mueller break; 1797a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1798a374e892STony Krowiak switch (attr->attr) { 1799a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1800a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1801a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1802a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1803a374e892STony Krowiak ret = 0; 1804a374e892STony Krowiak break; 180537940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 180637940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 180737940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 180837940fb0STony Krowiak break; 1809a374e892STony Krowiak default: 1810a374e892STony Krowiak ret = -ENXIO; 1811a374e892STony Krowiak break; 1812a374e892STony Krowiak } 1813a374e892STony Krowiak break; 1814190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1815190df4a2SClaudio Imbrenda ret = 0; 1816190df4a2SClaudio Imbrenda break; 1817f2061656SDominik Dingel default: 1818f2061656SDominik Dingel ret = -ENXIO; 1819f2061656SDominik Dingel break; 1820f2061656SDominik Dingel } 1821f2061656SDominik Dingel 1822f2061656SDominik Dingel return ret; 1823f2061656SDominik Dingel } 1824f2061656SDominik Dingel 182530ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 182630ee2a98SJason J. Herne { 182730ee2a98SJason J. Herne uint8_t *keys; 182830ee2a98SJason J. Herne uint64_t hva; 18294f899147SChristian Borntraeger int srcu_idx, i, r = 0; 183030ee2a98SJason J. Herne 183130ee2a98SJason J. Herne if (args->flags != 0) 183230ee2a98SJason J. Herne return -EINVAL; 183330ee2a98SJason J. Herne 183430ee2a98SJason J. Herne /* Is this guest using storage keys? */ 183555531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 183630ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 183730ee2a98SJason J. Herne 183830ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 183930ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 184030ee2a98SJason J. Herne return -EINVAL; 184130ee2a98SJason J. Herne 1842c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 184330ee2a98SJason J. Herne if (!keys) 184430ee2a98SJason J. Herne return -ENOMEM; 184530ee2a98SJason J. Herne 1846d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 18474f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 184830ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 184930ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 185030ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 185130ee2a98SJason J. Herne r = -EFAULT; 1852d3ed1ceeSMartin Schwidefsky break; 185330ee2a98SJason J. Herne } 185430ee2a98SJason J. Herne 1855154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1856154c8c19SDavid Hildenbrand if (r) 1857d3ed1ceeSMartin Schwidefsky break; 185830ee2a98SJason J. Herne } 18594f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1860d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 186130ee2a98SJason J. Herne 1862d3ed1ceeSMartin Schwidefsky if (!r) { 186330ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 186430ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 186530ee2a98SJason J. Herne if (r) 186630ee2a98SJason J. Herne r = -EFAULT; 1867d3ed1ceeSMartin Schwidefsky } 1868d3ed1ceeSMartin Schwidefsky 186930ee2a98SJason J. Herne kvfree(keys); 187030ee2a98SJason J. Herne return r; 187130ee2a98SJason J. Herne } 187230ee2a98SJason J. Herne 187330ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 187430ee2a98SJason J. Herne { 187530ee2a98SJason J. Herne uint8_t *keys; 187630ee2a98SJason J. Herne uint64_t hva; 18774f899147SChristian Borntraeger int srcu_idx, i, r = 0; 1878bd096f64SJanosch Frank bool unlocked; 187930ee2a98SJason J. Herne 188030ee2a98SJason J. Herne if (args->flags != 0) 188130ee2a98SJason J. Herne return -EINVAL; 188230ee2a98SJason J. Herne 188330ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 188430ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 188530ee2a98SJason J. Herne return -EINVAL; 188630ee2a98SJason J. Herne 1887c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 188830ee2a98SJason J. Herne if (!keys) 188930ee2a98SJason J. Herne return -ENOMEM; 189030ee2a98SJason J. Herne 189130ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 189230ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 189330ee2a98SJason J. Herne if (r) { 189430ee2a98SJason J. Herne r = -EFAULT; 189530ee2a98SJason J. Herne goto out; 189630ee2a98SJason J. Herne } 189730ee2a98SJason J. Herne 189830ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 189914d4a425SDominik Dingel r = s390_enable_skey(); 190014d4a425SDominik Dingel if (r) 190114d4a425SDominik Dingel goto out; 190230ee2a98SJason J. Herne 1903bd096f64SJanosch Frank i = 0; 1904d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 19054f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 1906bd096f64SJanosch Frank while (i < args->count) { 1907bd096f64SJanosch Frank unlocked = false; 190830ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 190930ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 191030ee2a98SJason J. Herne r = -EFAULT; 1911d3ed1ceeSMartin Schwidefsky break; 191230ee2a98SJason J. Herne } 191330ee2a98SJason J. Herne 191430ee2a98SJason J. Herne /* Lowest order bit is reserved */ 191530ee2a98SJason J. Herne if (keys[i] & 0x01) { 191630ee2a98SJason J. Herne r = -EINVAL; 1917d3ed1ceeSMartin Schwidefsky break; 191830ee2a98SJason J. Herne } 191930ee2a98SJason J. Herne 1920fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 1921bd096f64SJanosch Frank if (r) { 192264019a2eSPeter Xu r = fixup_user_fault(current->mm, hva, 1923bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 192430ee2a98SJason J. Herne if (r) 1925d3ed1ceeSMartin Schwidefsky break; 192630ee2a98SJason J. Herne } 1927bd096f64SJanosch Frank if (!r) 1928bd096f64SJanosch Frank i++; 1929bd096f64SJanosch Frank } 19304f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1931d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 193230ee2a98SJason J. Herne out: 193330ee2a98SJason J. Herne kvfree(keys); 193430ee2a98SJason J. Herne return r; 193530ee2a98SJason J. Herne } 193630ee2a98SJason J. Herne 19374036e387SClaudio Imbrenda /* 19384036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 19394036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 19404036e387SClaudio Imbrenda * two longs. 19414036e387SClaudio Imbrenda */ 19424036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 19434036e387SClaudio Imbrenda /* for consistency */ 19444036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 19454036e387SClaudio Imbrenda 1946afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1947afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1948afdad616SClaudio Imbrenda { 1949afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 1950afdad616SClaudio Imbrenda 1951afdad616SClaudio Imbrenda args->count = 0; 1952afdad616SClaudio Imbrenda while (args->count < bufsize) { 1953afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1954afdad616SClaudio Imbrenda /* 1955afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 1956afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 1957afdad616SClaudio Imbrenda */ 1958afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1959afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 1960afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1961afdad616SClaudio Imbrenda pgstev = 0; 1962afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1963afdad616SClaudio Imbrenda cur_gfn++; 1964afdad616SClaudio Imbrenda } 1965afdad616SClaudio Imbrenda 1966afdad616SClaudio Imbrenda return 0; 1967afdad616SClaudio Imbrenda } 1968afdad616SClaudio Imbrenda 1969c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots, 1970c928bfc2SMaciej S. Szmigiero gfn_t gfn) 1971c928bfc2SMaciej S. Szmigiero { 1972c928bfc2SMaciej S. Szmigiero return ____gfn_to_memslot(slots, gfn, true); 1973c928bfc2SMaciej S. Szmigiero } 1974c928bfc2SMaciej S. Szmigiero 1975afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 1976afdad616SClaudio Imbrenda unsigned long cur_gfn) 1977afdad616SClaudio Imbrenda { 1978c928bfc2SMaciej S. Szmigiero struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn); 1979afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 1980a54d8066SMaciej S. Szmigiero struct rb_node *mnode = &ms->gfn_node[slots->node_idx]; 1981afdad616SClaudio Imbrenda 1982afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 1983a54d8066SMaciej S. Szmigiero mnode = rb_next(mnode); 1984afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 1985a54d8066SMaciej S. Szmigiero if (!mnode) 1986a54d8066SMaciej S. Szmigiero mnode = rb_first(&slots->gfn_tree); 1987afdad616SClaudio Imbrenda 1988a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 1989afdad616SClaudio Imbrenda ofs = 0; 1990afdad616SClaudio Imbrenda } 1991afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 1992a54d8066SMaciej S. Szmigiero while (ofs >= ms->npages && (mnode = rb_next(mnode))) { 1993a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 1994b5c7e7ecSYury Norov ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages); 1995afdad616SClaudio Imbrenda } 1996afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 1997afdad616SClaudio Imbrenda } 1998afdad616SClaudio Imbrenda 1999afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2000afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2001afdad616SClaudio Imbrenda { 2002afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2003afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2004afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2005afdad616SClaudio Imbrenda 2006a54d8066SMaciej S. Szmigiero if (unlikely(kvm_memslots_empty(slots))) 20070774a964SSean Christopherson return 0; 20080774a964SSean Christopherson 2009afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2010afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2011afdad616SClaudio Imbrenda args->count = 0; 2012afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2013afdad616SClaudio Imbrenda if (!ms) 2014afdad616SClaudio Imbrenda return 0; 2015afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 20166a656832SMaciej S. Szmigiero mem_end = kvm_s390_get_gfn_end(slots); 2017afdad616SClaudio Imbrenda 2018afdad616SClaudio Imbrenda while (args->count < bufsize) { 2019afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2020afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2021afdad616SClaudio Imbrenda return 0; 2022afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2023afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2024afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2025afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2026afdad616SClaudio Imbrenda pgstev = 0; 2027afdad616SClaudio Imbrenda /* Save the value */ 2028afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2029afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2030afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2031afdad616SClaudio Imbrenda return 0; 2032afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2033afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2034afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2035afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2036afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2037afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2038afdad616SClaudio Imbrenda return 0; 2039afdad616SClaudio Imbrenda cur_gfn++; 2040afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2041afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2042afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2043afdad616SClaudio Imbrenda if (!ms) 2044afdad616SClaudio Imbrenda return 0; 2045afdad616SClaudio Imbrenda } 2046afdad616SClaudio Imbrenda } 2047afdad616SClaudio Imbrenda return 0; 2048afdad616SClaudio Imbrenda } 2049afdad616SClaudio Imbrenda 2050afdad616SClaudio Imbrenda /* 20514036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 20524036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 20534036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 20544036e387SClaudio Imbrenda * no trailing clean bytes are saved. 20554036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 20564036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 20574036e387SClaudio Imbrenda */ 20584036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 20594036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 20604036e387SClaudio Imbrenda { 2061afdad616SClaudio Imbrenda unsigned long bufsize; 2062afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2063afdad616SClaudio Imbrenda u8 *values; 20644036e387SClaudio Imbrenda 2065afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 20664036e387SClaudio Imbrenda return -ENXIO; 20674036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 20684036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 20694036e387SClaudio Imbrenda return -EINVAL; 20704036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 20714036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2072afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 20734036e387SClaudio Imbrenda return -EINVAL; 20744036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 20754036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2076c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 20774036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20784036e387SClaudio Imbrenda return 0; 20794036e387SClaudio Imbrenda } 20804036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2081afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 20824036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20834036e387SClaudio Imbrenda return 0; 20844036e387SClaudio Imbrenda } 20854036e387SClaudio Imbrenda 2086afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2087afdad616SClaudio Imbrenda if (!values) 20884036e387SClaudio Imbrenda return -ENOMEM; 20894036e387SClaudio Imbrenda 2090d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 20914036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2092afdad616SClaudio Imbrenda if (peek) 2093afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2094afdad616SClaudio Imbrenda else 2095afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 20964036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2097d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 20984036e387SClaudio Imbrenda 2099afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2100afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2101afdad616SClaudio Imbrenda else 2102afdad616SClaudio Imbrenda args->remaining = 0; 21034036e387SClaudio Imbrenda 2104afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2105afdad616SClaudio Imbrenda ret = -EFAULT; 2106afdad616SClaudio Imbrenda 2107afdad616SClaudio Imbrenda vfree(values); 2108afdad616SClaudio Imbrenda return ret; 21094036e387SClaudio Imbrenda } 21104036e387SClaudio Imbrenda 21114036e387SClaudio Imbrenda /* 21124036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 21134036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2114c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 21154036e387SClaudio Imbrenda */ 21164036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 21174036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 21184036e387SClaudio Imbrenda { 21194036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 21204036e387SClaudio Imbrenda uint8_t *bits; 21214036e387SClaudio Imbrenda int srcu_idx, r = 0; 21224036e387SClaudio Imbrenda 21234036e387SClaudio Imbrenda mask = args->mask; 21244036e387SClaudio Imbrenda 21254036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 21264036e387SClaudio Imbrenda return -ENXIO; 21274036e387SClaudio Imbrenda /* invalid/unsupported flags */ 21284036e387SClaudio Imbrenda if (args->flags != 0) 21294036e387SClaudio Imbrenda return -EINVAL; 21304036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 21314036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 21324036e387SClaudio Imbrenda return -EINVAL; 21334036e387SClaudio Imbrenda /* Nothing to do */ 21344036e387SClaudio Imbrenda if (args->count == 0) 21354036e387SClaudio Imbrenda return 0; 21364036e387SClaudio Imbrenda 213742bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 21384036e387SClaudio Imbrenda if (!bits) 21394036e387SClaudio Imbrenda return -ENOMEM; 21404036e387SClaudio Imbrenda 21414036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 21424036e387SClaudio Imbrenda if (r) { 21434036e387SClaudio Imbrenda r = -EFAULT; 21444036e387SClaudio Imbrenda goto out; 21454036e387SClaudio Imbrenda } 21464036e387SClaudio Imbrenda 2147d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 21484036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 21494036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 21504036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 21514036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 21524036e387SClaudio Imbrenda r = -EFAULT; 21534036e387SClaudio Imbrenda break; 21544036e387SClaudio Imbrenda } 21554036e387SClaudio Imbrenda 21564036e387SClaudio Imbrenda pgstev = bits[i]; 21574036e387SClaudio Imbrenda pgstev = pgstev << 24; 21581bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 21594036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 21604036e387SClaudio Imbrenda } 21614036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2162d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 21634036e387SClaudio Imbrenda 2164c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 2165d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 2166c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 2167d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 21684036e387SClaudio Imbrenda } 21694036e387SClaudio Imbrenda out: 21704036e387SClaudio Imbrenda vfree(bits); 21714036e387SClaudio Imbrenda return r; 21724036e387SClaudio Imbrenda } 21734036e387SClaudio Imbrenda 217429b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp) 217529b40f10SJanosch Frank { 217629b40f10SJanosch Frank struct kvm_vcpu *vcpu; 217729b40f10SJanosch Frank u16 rc, rrc; 217829b40f10SJanosch Frank int ret = 0; 217946808a4cSMarc Zyngier unsigned long i; 218029b40f10SJanosch Frank 218129b40f10SJanosch Frank /* 218229b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 218329b40f10SJanosch Frank * At the same time we must not free the assigned resources when 218429b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 218529b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 218629b40f10SJanosch Frank * behind. 218729b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 218829b40f10SJanosch Frank */ 218929b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 219029b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 219129b40f10SJanosch Frank if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) { 219229b40f10SJanosch Frank *rcp = rc; 219329b40f10SJanosch Frank *rrcp = rrc; 219429b40f10SJanosch Frank ret = -EIO; 219529b40f10SJanosch Frank } 219629b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 219729b40f10SJanosch Frank } 2198ee6a569dSMichael Mueller /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */ 2199ee6a569dSMichael Mueller if (use_gisa) 2200ee6a569dSMichael Mueller kvm_s390_gisa_enable(kvm); 220129b40f10SJanosch Frank return ret; 220229b40f10SJanosch Frank } 220329b40f10SJanosch Frank 220429b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 220529b40f10SJanosch Frank { 220646808a4cSMarc Zyngier unsigned long i; 220746808a4cSMarc Zyngier int r = 0; 220829b40f10SJanosch Frank u16 dummy; 220929b40f10SJanosch Frank 221029b40f10SJanosch Frank struct kvm_vcpu *vcpu; 221129b40f10SJanosch Frank 2212ee6a569dSMichael Mueller /* Disable the GISA if the ultravisor does not support AIV. */ 2213ee6a569dSMichael Mueller if (!test_bit_inv(BIT_UV_FEAT_AIV, &uv_info.uv_feature_indications)) 2214ee6a569dSMichael Mueller kvm_s390_gisa_disable(kvm); 2215ee6a569dSMichael Mueller 221629b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 221729b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 221829b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 221929b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 222029b40f10SJanosch Frank if (r) 222129b40f10SJanosch Frank break; 222229b40f10SJanosch Frank } 222329b40f10SJanosch Frank if (r) 222429b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 222529b40f10SJanosch Frank return r; 222629b40f10SJanosch Frank } 222729b40f10SJanosch Frank 222829b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 222929b40f10SJanosch Frank { 223029b40f10SJanosch Frank int r = 0; 223129b40f10SJanosch Frank u16 dummy; 223229b40f10SJanosch Frank void __user *argp = (void __user *)cmd->data; 223329b40f10SJanosch Frank 223429b40f10SJanosch Frank switch (cmd->cmd) { 223529b40f10SJanosch Frank case KVM_PV_ENABLE: { 223629b40f10SJanosch Frank r = -EINVAL; 223729b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 223829b40f10SJanosch Frank break; 223929b40f10SJanosch Frank 224029b40f10SJanosch Frank /* 224129b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 224229b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 224329b40f10SJanosch Frank */ 224429b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 224529b40f10SJanosch Frank if (r) 224629b40f10SJanosch Frank break; 224729b40f10SJanosch Frank 2248d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 2249fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2250d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 2251fa0c5eabSJanosch Frank if (r) 2252fa0c5eabSJanosch Frank break; 2253fa0c5eabSJanosch Frank 225429b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 225529b40f10SJanosch Frank if (r) 225629b40f10SJanosch Frank break; 225729b40f10SJanosch Frank 225829b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 225929b40f10SJanosch Frank if (r) 226029b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 22610890ddeaSChristian Borntraeger 22620890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 22630890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 226429b40f10SJanosch Frank break; 226529b40f10SJanosch Frank } 226629b40f10SJanosch Frank case KVM_PV_DISABLE: { 226729b40f10SJanosch Frank r = -EINVAL; 226829b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 226929b40f10SJanosch Frank break; 227029b40f10SJanosch Frank 227129b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 227229b40f10SJanosch Frank /* 227329b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 227429b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 227529b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 227629b40f10SJanosch Frank */ 227729b40f10SJanosch Frank if (r) 227829b40f10SJanosch Frank break; 227929b40f10SJanosch Frank r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc); 22800890ddeaSChristian Borntraeger 22810890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 22820890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 228329b40f10SJanosch Frank break; 228429b40f10SJanosch Frank } 228529b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 228629b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 228729b40f10SJanosch Frank void *hdr; 228829b40f10SJanosch Frank 228929b40f10SJanosch Frank r = -EINVAL; 229029b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 229129b40f10SJanosch Frank break; 229229b40f10SJanosch Frank 229329b40f10SJanosch Frank r = -EFAULT; 229429b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 229529b40f10SJanosch Frank break; 229629b40f10SJanosch Frank 229729b40f10SJanosch Frank /* Currently restricted to 8KB */ 229829b40f10SJanosch Frank r = -EINVAL; 229929b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 230029b40f10SJanosch Frank break; 230129b40f10SJanosch Frank 230229b40f10SJanosch Frank r = -ENOMEM; 230329b40f10SJanosch Frank hdr = vmalloc(parms.length); 230429b40f10SJanosch Frank if (!hdr) 230529b40f10SJanosch Frank break; 230629b40f10SJanosch Frank 230729b40f10SJanosch Frank r = -EFAULT; 230829b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 230929b40f10SJanosch Frank parms.length)) 231029b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 231129b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 231229b40f10SJanosch Frank 231329b40f10SJanosch Frank vfree(hdr); 231429b40f10SJanosch Frank break; 231529b40f10SJanosch Frank } 231629b40f10SJanosch Frank case KVM_PV_UNPACK: { 231729b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 231829b40f10SJanosch Frank 231929b40f10SJanosch Frank r = -EINVAL; 23201ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) 232129b40f10SJanosch Frank break; 232229b40f10SJanosch Frank 232329b40f10SJanosch Frank r = -EFAULT; 232429b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 232529b40f10SJanosch Frank break; 232629b40f10SJanosch Frank 232729b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 232829b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 232929b40f10SJanosch Frank break; 233029b40f10SJanosch Frank } 233129b40f10SJanosch Frank case KVM_PV_VERIFY: { 233229b40f10SJanosch Frank r = -EINVAL; 233329b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 233429b40f10SJanosch Frank break; 233529b40f10SJanosch Frank 233629b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 233729b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 233829b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 233929b40f10SJanosch Frank cmd->rrc); 234029b40f10SJanosch Frank break; 234129b40f10SJanosch Frank } 2342e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2343e0d2773dSJanosch Frank r = -EINVAL; 2344e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2345e0d2773dSJanosch Frank break; 2346e0d2773dSJanosch Frank 2347e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2348e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2349e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2350e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2351e0d2773dSJanosch Frank break; 2352e0d2773dSJanosch Frank } 2353e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2354e0d2773dSJanosch Frank r = -EINVAL; 2355e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2356e0d2773dSJanosch Frank break; 2357e0d2773dSJanosch Frank 2358e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2359e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2360e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2361e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2362e0d2773dSJanosch Frank break; 2363e0d2773dSJanosch Frank } 236429b40f10SJanosch Frank default: 236529b40f10SJanosch Frank r = -ENOTTY; 236629b40f10SJanosch Frank } 236729b40f10SJanosch Frank return r; 236829b40f10SJanosch Frank } 236929b40f10SJanosch Frank 2370e9e9feebSJanis Schoetterl-Glausch static bool access_key_invalid(u8 access_key) 2371e9e9feebSJanis Schoetterl-Glausch { 2372e9e9feebSJanis Schoetterl-Glausch return access_key > 0xf; 2373e9e9feebSJanis Schoetterl-Glausch } 2374e9e9feebSJanis Schoetterl-Glausch 2375ef11c946SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) 2376ef11c946SJanis Schoetterl-Glausch { 2377ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 2378ef11c946SJanis Schoetterl-Glausch u64 supported_flags; 2379ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL; 2380ef11c946SJanis Schoetterl-Glausch int r, srcu_idx; 2381ef11c946SJanis Schoetterl-Glausch 2382ef11c946SJanis Schoetterl-Glausch supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION 2383ef11c946SJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_CHECK_ONLY; 23843d9042f8SJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size) 2385ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2386ef11c946SJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE) 2387ef11c946SJanis Schoetterl-Glausch return -E2BIG; 2388ef11c946SJanis Schoetterl-Glausch if (kvm_s390_pv_is_protected(kvm)) 2389ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2390ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 2391ef11c946SJanis Schoetterl-Glausch if (access_key_invalid(mop->key)) 2392ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2393ef11c946SJanis Schoetterl-Glausch } else { 2394ef11c946SJanis Schoetterl-Glausch mop->key = 0; 2395ef11c946SJanis Schoetterl-Glausch } 2396ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 2397ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size); 2398ef11c946SJanis Schoetterl-Glausch if (!tmpbuf) 2399ef11c946SJanis Schoetterl-Glausch return -ENOMEM; 2400ef11c946SJanis Schoetterl-Glausch } 2401ef11c946SJanis Schoetterl-Glausch 2402ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 2403ef11c946SJanis Schoetterl-Glausch 2404ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 2405ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 2406ef11c946SJanis Schoetterl-Glausch goto out_unlock; 2407ef11c946SJanis Schoetterl-Glausch } 2408ef11c946SJanis Schoetterl-Glausch 2409ef11c946SJanis Schoetterl-Glausch switch (mop->op) { 2410ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ: { 2411ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2412ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key); 2413ef11c946SJanis Schoetterl-Glausch } else { 2414ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2415ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key); 2416ef11c946SJanis Schoetterl-Glausch if (r == 0) { 2417ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) 2418ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2419ef11c946SJanis Schoetterl-Glausch } 2420ef11c946SJanis Schoetterl-Glausch } 2421ef11c946SJanis Schoetterl-Glausch break; 2422ef11c946SJanis Schoetterl-Glausch } 2423ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE: { 2424ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2425ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key); 2426ef11c946SJanis Schoetterl-Glausch } else { 2427ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) { 2428ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2429ef11c946SJanis Schoetterl-Glausch break; 2430ef11c946SJanis Schoetterl-Glausch } 2431ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2432ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key); 2433ef11c946SJanis Schoetterl-Glausch } 2434ef11c946SJanis Schoetterl-Glausch break; 2435ef11c946SJanis Schoetterl-Glausch } 2436ef11c946SJanis Schoetterl-Glausch default: 2437ef11c946SJanis Schoetterl-Glausch r = -EINVAL; 2438ef11c946SJanis Schoetterl-Glausch } 2439ef11c946SJanis Schoetterl-Glausch 2440ef11c946SJanis Schoetterl-Glausch out_unlock: 2441ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 2442ef11c946SJanis Schoetterl-Glausch 2443ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf); 2444ef11c946SJanis Schoetterl-Glausch return r; 2445ef11c946SJanis Schoetterl-Glausch } 2446ef11c946SJanis Schoetterl-Glausch 2447b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2448b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2449b0c632dbSHeiko Carstens { 2450b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2451b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2452f2061656SDominik Dingel struct kvm_device_attr attr; 2453b0c632dbSHeiko Carstens int r; 2454b0c632dbSHeiko Carstens 2455b0c632dbSHeiko Carstens switch (ioctl) { 2456ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2457ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2458ba5c1e9bSCarsten Otte 2459ba5c1e9bSCarsten Otte r = -EFAULT; 2460ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2461ba5c1e9bSCarsten Otte break; 2462ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2463ba5c1e9bSCarsten Otte break; 2464ba5c1e9bSCarsten Otte } 246584223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 246684223598SCornelia Huck struct kvm_irq_routing_entry routing; 246784223598SCornelia Huck 246884223598SCornelia Huck r = -EINVAL; 246984223598SCornelia Huck if (kvm->arch.use_irqchip) { 247084223598SCornelia Huck /* Set up dummy routing. */ 247184223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2472152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 247384223598SCornelia Huck } 247484223598SCornelia Huck break; 247584223598SCornelia Huck } 2476f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2477f2061656SDominik Dingel r = -EFAULT; 2478f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2479f2061656SDominik Dingel break; 2480f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2481f2061656SDominik Dingel break; 2482f2061656SDominik Dingel } 2483f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2484f2061656SDominik Dingel r = -EFAULT; 2485f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2486f2061656SDominik Dingel break; 2487f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2488f2061656SDominik Dingel break; 2489f2061656SDominik Dingel } 2490f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2491f2061656SDominik Dingel r = -EFAULT; 2492f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2493f2061656SDominik Dingel break; 2494f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2495f2061656SDominik Dingel break; 2496f2061656SDominik Dingel } 249730ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 249830ee2a98SJason J. Herne struct kvm_s390_skeys args; 249930ee2a98SJason J. Herne 250030ee2a98SJason J. Herne r = -EFAULT; 250130ee2a98SJason J. Herne if (copy_from_user(&args, argp, 250230ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 250330ee2a98SJason J. Herne break; 250430ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 250530ee2a98SJason J. Herne break; 250630ee2a98SJason J. Herne } 250730ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 250830ee2a98SJason J. Herne struct kvm_s390_skeys args; 250930ee2a98SJason J. Herne 251030ee2a98SJason J. Herne r = -EFAULT; 251130ee2a98SJason J. Herne if (copy_from_user(&args, argp, 251230ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 251330ee2a98SJason J. Herne break; 251430ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 251530ee2a98SJason J. Herne break; 251630ee2a98SJason J. Herne } 25174036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 25184036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 25194036e387SClaudio Imbrenda 25204036e387SClaudio Imbrenda r = -EFAULT; 25214036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 25224036e387SClaudio Imbrenda break; 25231de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 25244036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 25251de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 25264036e387SClaudio Imbrenda if (!r) { 25274036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 25284036e387SClaudio Imbrenda if (r) 25294036e387SClaudio Imbrenda r = -EFAULT; 25304036e387SClaudio Imbrenda } 25314036e387SClaudio Imbrenda break; 25324036e387SClaudio Imbrenda } 25334036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 25344036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 25354036e387SClaudio Imbrenda 25364036e387SClaudio Imbrenda r = -EFAULT; 25374036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 25384036e387SClaudio Imbrenda break; 25391de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 25404036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 25411de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 25424036e387SClaudio Imbrenda break; 25434036e387SClaudio Imbrenda } 254429b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 254529b40f10SJanosch Frank struct kvm_pv_cmd args; 254629b40f10SJanosch Frank 254767cf68b6SEric Farman /* protvirt means user cpu state */ 254867cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 254929b40f10SJanosch Frank r = 0; 255029b40f10SJanosch Frank if (!is_prot_virt_host()) { 255129b40f10SJanosch Frank r = -EINVAL; 255229b40f10SJanosch Frank break; 255329b40f10SJanosch Frank } 255429b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 255529b40f10SJanosch Frank r = -EFAULT; 255629b40f10SJanosch Frank break; 255729b40f10SJanosch Frank } 255829b40f10SJanosch Frank if (args.flags) { 255929b40f10SJanosch Frank r = -EINVAL; 256029b40f10SJanosch Frank break; 256129b40f10SJanosch Frank } 256229b40f10SJanosch Frank mutex_lock(&kvm->lock); 256329b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 256429b40f10SJanosch Frank mutex_unlock(&kvm->lock); 256529b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 256629b40f10SJanosch Frank r = -EFAULT; 256729b40f10SJanosch Frank break; 256829b40f10SJanosch Frank } 256929b40f10SJanosch Frank break; 257029b40f10SJanosch Frank } 2571ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: { 2572ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op; 2573ef11c946SJanis Schoetterl-Glausch 2574ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 2575ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op); 2576ef11c946SJanis Schoetterl-Glausch else 2577ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2578ef11c946SJanis Schoetterl-Glausch break; 2579ef11c946SJanis Schoetterl-Glausch } 2580b0c632dbSHeiko Carstens default: 2581367e1319SAvi Kivity r = -ENOTTY; 2582b0c632dbSHeiko Carstens } 2583b0c632dbSHeiko Carstens 2584b0c632dbSHeiko Carstens return r; 2585b0c632dbSHeiko Carstens } 2586b0c632dbSHeiko Carstens 258745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 258845c9b47cSTony Krowiak { 2589e585b24aSTony Krowiak struct ap_config_info info; 259045c9b47cSTony Krowiak 2591e585b24aSTony Krowiak if (ap_instructions_available()) { 2592e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2593e585b24aSTony Krowiak return info.apxa; 259445c9b47cSTony Krowiak } 259545c9b47cSTony Krowiak 259645c9b47cSTony Krowiak return 0; 259745c9b47cSTony Krowiak } 259845c9b47cSTony Krowiak 2599e585b24aSTony Krowiak /* 2600e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2601e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2602e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2603e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2604e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2605e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2606e585b24aSTony Krowiak */ 260745c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 260845c9b47cSTony Krowiak { 260945c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 261045c9b47cSTony Krowiak 2611e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2612e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2613e585b24aSTony Krowiak 2614e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2615e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2616e585b24aSTony Krowiak return; 2617e585b24aSTony Krowiak 261845c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 261945c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 262045c9b47cSTony Krowiak else 262145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 262245c9b47cSTony Krowiak } 262345c9b47cSTony Krowiak 262486956e70STony Krowiak /* 262586956e70STony Krowiak * kvm_arch_crypto_set_masks 262686956e70STony Krowiak * 262786956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 262886956e70STony Krowiak * to be set. 262986956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 263086956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 263186956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 263286956e70STony Krowiak * 263386956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 263486956e70STony Krowiak * which the KVM guest is granted access. 263586956e70STony Krowiak * 263686956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 263786956e70STony Krowiak * function. 263886956e70STony Krowiak */ 26390e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 26400e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 26410e237e44SPierre Morel { 26420e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 26430e237e44SPierre Morel 26440e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 26450e237e44SPierre Morel 26460e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 26470e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 26480e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 26490e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 26500e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 26510e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 26520e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 26530e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 26540e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 26550e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 26560e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 26570e237e44SPierre Morel break; 26580e237e44SPierre Morel case CRYCB_FORMAT1: 26590e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 26600e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 26610e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 26620e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 26630e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 26640e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 26650e237e44SPierre Morel *((unsigned short *)adm)); 26660e237e44SPierre Morel break; 26670e237e44SPierre Morel default: /* Can not happen */ 26680e237e44SPierre Morel break; 26690e237e44SPierre Morel } 26700e237e44SPierre Morel 26710e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 26720e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 26730e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 26740e237e44SPierre Morel } 26750e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 26760e237e44SPierre Morel 267786956e70STony Krowiak /* 267886956e70STony Krowiak * kvm_arch_crypto_clear_masks 267986956e70STony Krowiak * 268086956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 268186956e70STony Krowiak * to be cleared. 268286956e70STony Krowiak * 268386956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 268486956e70STony Krowiak * which the KVM guest is granted access. 268586956e70STony Krowiak * 268686956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 268786956e70STony Krowiak * function. 268886956e70STony Krowiak */ 268942104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 269042104598STony Krowiak { 269142104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 269242104598STony Krowiak 269342104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 269442104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 269542104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 269642104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 269742104598STony Krowiak 26980e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 26996cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 27006cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 270142104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 270242104598STony Krowiak } 270342104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 270442104598STony Krowiak 27059bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 27069d8d5786SMichael Mueller { 27079bb0ec09SDavid Hildenbrand struct cpuid cpuid; 27089bb0ec09SDavid Hildenbrand 27099bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 27109bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 27119bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 27129d8d5786SMichael Mueller } 27139d8d5786SMichael Mueller 2714c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 27155102ee87STony Krowiak { 2716c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 271745c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 27181e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 27195102ee87STony Krowiak 2720e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2721e585b24aSTony Krowiak return; 2722e585b24aSTony Krowiak 2723ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2724ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2725ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2726ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2727ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2728ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2729ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 27305102ee87STony Krowiak } 27315102ee87STony Krowiak 27327d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 27337d43bafcSEugene (jno) Dvurechenski { 27347d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 27355e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 27367d43bafcSEugene (jno) Dvurechenski else 27377d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 27387d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 27397d43bafcSEugene (jno) Dvurechenski } 27407d43bafcSEugene (jno) Dvurechenski 2741e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2742b0c632dbSHeiko Carstens { 2743c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 27449d8d5786SMichael Mueller int i, rc; 2745b0c632dbSHeiko Carstens char debug_name[16]; 2746f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2747b0c632dbSHeiko Carstens 2748e08b9637SCarsten Otte rc = -EINVAL; 2749e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2750e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2751e08b9637SCarsten Otte goto out_err; 2752e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2753e08b9637SCarsten Otte goto out_err; 2754e08b9637SCarsten Otte #else 2755e08b9637SCarsten Otte if (type) 2756e08b9637SCarsten Otte goto out_err; 2757e08b9637SCarsten Otte #endif 2758e08b9637SCarsten Otte 2759b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2760b0c632dbSHeiko Carstens if (rc) 2761d89f5effSJan Kiszka goto out_err; 2762b0c632dbSHeiko Carstens 2763b290411aSCarsten Otte rc = -ENOMEM; 2764b290411aSCarsten Otte 276576a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 276676a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 27675e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 27689ac96d75SDavid Hildenbrand /* start with basic SCA */ 276976a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2770b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2771d89f5effSJan Kiszka goto out_err; 27720d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 2773c5c2c393SDavid Hildenbrand sca_offset += 16; 2774bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2775c5c2c393SDavid Hildenbrand sca_offset = 0; 2776bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2777bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 27780d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 2779b0c632dbSHeiko Carstens 2780b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2781b0c632dbSHeiko Carstens 27821cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2783b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 278440f5b735SDominik Dingel goto out_err; 2785b0c632dbSHeiko Carstens 278619114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2787c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2788c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 2789c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 279040f5b735SDominik Dingel goto out_err; 27919d8d5786SMichael Mueller 279225c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2793c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2794c3b9e3e1SChristian Borntraeger 2795c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 279617e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 2797c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2798c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 279917e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 2800c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2801c3b9e3e1SChristian Borntraeger } 2802346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2803981467c9SMichael Mueller 28041935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 28051935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 28061935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 28071935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 280895ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 280995ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 28101bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 28111bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 28121bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 28131bab1c02SClaudio Imbrenda } 281495ca2cb5SJanosch Frank 281505f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 281605f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 281705f31e3bSPierre Morel 28189bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 281937c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 28209d8d5786SMichael Mueller 2821c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 28225102ee87STony Krowiak 282351978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2824ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 28256d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 28266d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 28278a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2828a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2829ba5c1e9bSCarsten Otte 2830b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 283178f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2832b0c632dbSHeiko Carstens 2833e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2834e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2835a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2836e08b9637SCarsten Otte } else { 283732e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2838ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 283932e6b236SGuenther Hutzl else 2840ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 284132e6b236SGuenther Hutzl sclp.hamax + 1); 28426ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2843598841caSCarsten Otte if (!kvm->arch.gmap) 284440f5b735SDominik Dingel goto out_err; 28452c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 284624eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2847e08b9637SCarsten Otte } 2848fa6b7fe9SCornelia Huck 2849c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 285055531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 28518ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2852a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2853cc674ef2SMichael Mueller if (use_gisa) 2854d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 28558335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 28568ad35755SDavid Hildenbrand 2857d89f5effSJan Kiszka return 0; 2858d89f5effSJan Kiszka out_err: 2859c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 286040f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 28617d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 286278f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2863d89f5effSJan Kiszka return rc; 2864b0c632dbSHeiko Carstens } 2865b0c632dbSHeiko Carstens 2866d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2867d329c035SChristian Borntraeger { 286829b40f10SJanosch Frank u16 rc, rrc; 286929b40f10SJanosch Frank 2870d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2871ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 287267335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 28733c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2874bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2875a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 287627e0393fSCarsten Otte 287727e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 28786ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 287927e0393fSCarsten Otte 2880e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2881b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 288229b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 288329b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 288429b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 2885d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2886d329c035SChristian Borntraeger } 2887d329c035SChristian Borntraeger 2888b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2889b0c632dbSHeiko Carstens { 289029b40f10SJanosch Frank u16 rc, rrc; 289129b40f10SJanosch Frank 289227592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 28937d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2894d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 289529b40f10SJanosch Frank /* 289629b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 289729b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 289829b40f10SJanosch Frank * can mess with the pv state. To avoid lockdep_assert_held from 289929b40f10SJanosch Frank * complaining we do not use kvm_s390_pv_is_protected. 290029b40f10SJanosch Frank */ 290129b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 290229b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); 290329b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 2904c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 290527e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 29066ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2907841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 290867335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2909a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 29108335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2911b0c632dbSHeiko Carstens } 2912b0c632dbSHeiko Carstens 2913b0c632dbSHeiko Carstens /* Section: vcpu related */ 2914dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2915b0c632dbSHeiko Carstens { 29166ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 291727e0393fSCarsten Otte if (!vcpu->arch.gmap) 291827e0393fSCarsten Otte return -ENOMEM; 29192c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2920dafd032aSDominik Dingel 292127e0393fSCarsten Otte return 0; 292227e0393fSCarsten Otte } 292327e0393fSCarsten Otte 2924a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2925a6e2f683SEugene (jno) Dvurechenski { 2926a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2927a6940674SDavid Hildenbrand return; 29285e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 29297d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 29307d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 29317d43bafcSEugene (jno) Dvurechenski 29327d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 29337d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 29347d43bafcSEugene (jno) Dvurechenski } else { 2935bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2936a6e2f683SEugene (jno) Dvurechenski 2937a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2938a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2939a6e2f683SEugene (jno) Dvurechenski } 29405e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 29417d43bafcSEugene (jno) Dvurechenski } 2942a6e2f683SEugene (jno) Dvurechenski 2943eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2944a6e2f683SEugene (jno) Dvurechenski { 2945a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2946a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2947a6940674SDavid Hildenbrand 2948a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2949a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2950a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2951f07afa04SDavid Hildenbrand return; 2952a6940674SDavid Hildenbrand } 2953eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2954eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2955eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 29567d43bafcSEugene (jno) Dvurechenski 2957eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 29587d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 29597d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 29600c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2961eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 29627d43bafcSEugene (jno) Dvurechenski } else { 2963eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2964a6e2f683SEugene (jno) Dvurechenski 2965eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2966a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2967a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2968eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2969a6e2f683SEugene (jno) Dvurechenski } 2970eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 29715e044315SEugene (jno) Dvurechenski } 29725e044315SEugene (jno) Dvurechenski 29735e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 29745e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 29755e044315SEugene (jno) Dvurechenski { 29765e044315SEugene (jno) Dvurechenski d->sda = s->sda; 29775e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 29785e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 29795e044315SEugene (jno) Dvurechenski } 29805e044315SEugene (jno) Dvurechenski 29815e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 29825e044315SEugene (jno) Dvurechenski { 29835e044315SEugene (jno) Dvurechenski int i; 29845e044315SEugene (jno) Dvurechenski 29855e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 29865e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 29875e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 29885e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 29895e044315SEugene (jno) Dvurechenski } 29905e044315SEugene (jno) Dvurechenski 29915e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 29925e044315SEugene (jno) Dvurechenski { 29935e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 29945e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 29955e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 299646808a4cSMarc Zyngier unsigned long vcpu_idx; 29975e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 29985e044315SEugene (jno) Dvurechenski 299929b40f10SJanosch Frank if (kvm->arch.use_esca) 300029b40f10SJanosch Frank return 0; 300129b40f10SJanosch Frank 3002c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 30035e044315SEugene (jno) Dvurechenski if (!new_sca) 30045e044315SEugene (jno) Dvurechenski return -ENOMEM; 30055e044315SEugene (jno) Dvurechenski 30065e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 30075e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 30085e044315SEugene (jno) Dvurechenski 30095e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 30105e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 30115e044315SEugene (jno) Dvurechenski 30125e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 30135e044315SEugene (jno) Dvurechenski 30145e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 30155e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 30165e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 30170c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 30185e044315SEugene (jno) Dvurechenski } 30195e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 30205e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 30215e044315SEugene (jno) Dvurechenski 30225e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 30235e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 30245e044315SEugene (jno) Dvurechenski 30255e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 30265e044315SEugene (jno) Dvurechenski 30278335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 30288335713aSChristian Borntraeger old_sca, kvm->arch.sca); 30295e044315SEugene (jno) Dvurechenski return 0; 30307d43bafcSEugene (jno) Dvurechenski } 3031a6e2f683SEugene (jno) Dvurechenski 3032a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 3033a6e2f683SEugene (jno) Dvurechenski { 30345e044315SEugene (jno) Dvurechenski int rc; 30355e044315SEugene (jno) Dvurechenski 3036a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3037a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 3038a6940674SDavid Hildenbrand return true; 3039a6940674SDavid Hildenbrand return false; 3040a6940674SDavid Hildenbrand } 30415e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 30425e044315SEugene (jno) Dvurechenski return true; 304376a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 30445e044315SEugene (jno) Dvurechenski return false; 30455e044315SEugene (jno) Dvurechenski 30465e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 30475e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 30485e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 30495e044315SEugene (jno) Dvurechenski 30505e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 3051a6e2f683SEugene (jno) Dvurechenski } 3052a6e2f683SEugene (jno) Dvurechenski 3053db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3054db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3055db0758b2SDavid Hildenbrand { 3056db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 30579c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3058db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 30599c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3060db0758b2SDavid Hildenbrand } 3061db0758b2SDavid Hildenbrand 3062db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3063db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3064db0758b2SDavid Hildenbrand { 3065db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 30669c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3067db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3068db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 30699c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3070db0758b2SDavid Hildenbrand } 3071db0758b2SDavid Hildenbrand 3072db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3073db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3074db0758b2SDavid Hildenbrand { 3075db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3076db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3077db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3078db0758b2SDavid Hildenbrand } 3079db0758b2SDavid Hildenbrand 3080db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3081db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3082db0758b2SDavid Hildenbrand { 3083db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3084db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3085db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3086db0758b2SDavid Hildenbrand } 3087db0758b2SDavid Hildenbrand 3088db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3089db0758b2SDavid Hildenbrand { 3090db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3091db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3092db0758b2SDavid Hildenbrand preempt_enable(); 3093db0758b2SDavid Hildenbrand } 3094db0758b2SDavid Hildenbrand 3095db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3096db0758b2SDavid Hildenbrand { 3097db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3098db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3099db0758b2SDavid Hildenbrand preempt_enable(); 3100db0758b2SDavid Hildenbrand } 3101db0758b2SDavid Hildenbrand 31024287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 31034287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 31044287f247SDavid Hildenbrand { 3105db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 31069c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3107db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3108db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 31094287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 31109c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3111db0758b2SDavid Hildenbrand preempt_enable(); 31124287f247SDavid Hildenbrand } 31134287f247SDavid Hildenbrand 3114db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 31154287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 31164287f247SDavid Hildenbrand { 31179c23a131SDavid Hildenbrand unsigned int seq; 3118db0758b2SDavid Hildenbrand __u64 value; 3119db0758b2SDavid Hildenbrand 3120db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 31214287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3122db0758b2SDavid Hildenbrand 31239c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 31249c23a131SDavid Hildenbrand do { 31259c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 31269c23a131SDavid Hildenbrand /* 31279c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 31289c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 31299c23a131SDavid Hildenbrand */ 31309c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3131db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 31329c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 31339c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3134db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 31359c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 31369c23a131SDavid Hildenbrand preempt_enable(); 3137db0758b2SDavid Hildenbrand return value; 31384287f247SDavid Hildenbrand } 31394287f247SDavid Hildenbrand 3140b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3141b0c632dbSHeiko Carstens { 31429977e886SHendrik Brueckner 314337d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3144ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 31455ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3146db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 314701a745acSDavid Hildenbrand vcpu->cpu = cpu; 3148b0c632dbSHeiko Carstens } 3149b0c632dbSHeiko Carstens 3150b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3151b0c632dbSHeiko Carstens { 315201a745acSDavid Hildenbrand vcpu->cpu = -1; 31535ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3154db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 31559daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 315637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 315737d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 31589977e886SHendrik Brueckner 3159b0c632dbSHeiko Carstens } 3160b0c632dbSHeiko Carstens 316131928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 316242897d86SMarcelo Tosatti { 316372f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3164fdf03650SFan Zhang preempt_disable(); 316572f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3166d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3167fdf03650SFan Zhang preempt_enable(); 316872f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 316925508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3170dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3171eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 317225508824SDavid Hildenbrand } 31736502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 31746502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 317537d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 317637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 317742897d86SMarcelo Tosatti } 317842897d86SMarcelo Tosatti 31798ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 31808ec2fa52SChristian Borntraeger { 31818ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 31828ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 31838ec2fa52SChristian Borntraeger return true; 31848ec2fa52SChristian Borntraeger return false; 31858ec2fa52SChristian Borntraeger } 31868ec2fa52SChristian Borntraeger 31878ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 31888ec2fa52SChristian Borntraeger { 31898ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 31908ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 31918ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 31928ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 31938ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 31948ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 31958ec2fa52SChristian Borntraeger 31968ec2fa52SChristian Borntraeger } 31978ec2fa52SChristian Borntraeger 31985102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 31995102ee87STony Krowiak { 3200e585b24aSTony Krowiak /* 3201e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3202e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3203e585b24aSTony Krowiak */ 3204e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 32055102ee87STony Krowiak return; 32065102ee87STony Krowiak 3207e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3208a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 320937940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 32108ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3211a374e892STony Krowiak 3212e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3213e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3214e585b24aSTony Krowiak 3215e585b24aSTony Krowiak /* Set up protected key support */ 32168ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3217a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 32188ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 32198ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 32208ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 32218ec2fa52SChristian Borntraeger } 32228ec2fa52SChristian Borntraeger 3223a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3224a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 32255102ee87STony Krowiak } 32265102ee87STony Krowiak 3227b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3228b31605c1SDominik Dingel { 3229b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 3230b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3231b31605c1SDominik Dingel } 3232b31605c1SDominik Dingel 3233b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3234b31605c1SDominik Dingel { 3235c4196218SChristian Borntraeger vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT); 3236b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 3237b31605c1SDominik Dingel return -ENOMEM; 3238b31605c1SDominik Dingel return 0; 3239b31605c1SDominik Dingel } 3240b31605c1SDominik Dingel 324191520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 324291520f1aSMichael Mueller { 324391520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 324491520f1aSMichael Mueller 324591520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 324680bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3247c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 324891520f1aSMichael Mueller } 324991520f1aSMichael Mueller 3250ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3251ff72bb55SSean Christopherson { 3252b31605c1SDominik Dingel int rc = 0; 325329b40f10SJanosch Frank u16 uvrc, uvrrc; 3254b31288faSKonstantin Weitz 32559e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 32569e6dabefSCornelia Huck CPUSTAT_SM | 3257a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3258a4a4f191SGuenther Hutzl 325953df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3260ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 326153df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3262ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3263a4a4f191SGuenther Hutzl 326491520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 326591520f1aSMichael Mueller 3266bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3267bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 32680c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3269bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 32700c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 3271f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 32720c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 32737119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 32747119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 32757feb6bb8SMichael Mueller 3276c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 32770c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3278cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 32790c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 32800c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 328148ee7d3aSDavid Hildenbrand if (sclp.has_cei) 32820c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 328311ad65b7SDavid Hildenbrand if (sclp.has_ib) 32840c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 328537c5f6c8SDavid Hildenbrand if (sclp.has_siif) 32860c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 328737c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 32880c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 328918280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 32900c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 32910c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 329213211ea7SEric Farman } 32938fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 32948fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3295a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3296a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3297d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3298d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3299d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3300d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3301d7c5cb01SMichael Mueller } 33024e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 33034e0b1ab7SFan Zhang | SDNXC; 3304c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 3305730cd632SFarhan Ali 3306730cd632SFarhan Ali if (sclp.has_kss) 3307ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3308730cd632SFarhan Ali else 3309492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 33105a5e6536SMatthew Rosato 3311e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3312b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3313b31605c1SDominik Dingel if (rc) 3314b31605c1SDominik Dingel return rc; 3315b31288faSKonstantin Weitz } 33160ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3317ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 33189d8d5786SMichael Mueller 331967d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 332067d49d52SCollin Walling 33215102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 33225102ee87STony Krowiak 332329b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 332429b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 332529b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 332629b40f10SJanosch Frank if (rc) 332729b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 332829b40f10SJanosch Frank } 332929b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 333029b40f10SJanosch Frank 3331b31605c1SDominik Dingel return rc; 3332b0c632dbSHeiko Carstens } 3333b0c632dbSHeiko Carstens 3334897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3335897cc38eSSean Christopherson { 3336897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3337897cc38eSSean Christopherson return -EINVAL; 3338897cc38eSSean Christopherson return 0; 3339897cc38eSSean Christopherson } 3340897cc38eSSean Christopherson 3341e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3342b0c632dbSHeiko Carstens { 33437feb6bb8SMichael Mueller struct sie_page *sie_page; 3344897cc38eSSean Christopherson int rc; 33454d47555aSCarsten Otte 3346da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3347c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 33487feb6bb8SMichael Mueller if (!sie_page) 3349e529ef66SSean Christopherson return -ENOMEM; 3350b0c632dbSHeiko Carstens 33517feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 33527feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 33537feb6bb8SMichael Mueller 3354efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3355efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3356efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3357efed1104SDavid Hildenbrand 3358e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3359ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3360ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm); 33619c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3362ba5c1e9bSCarsten Otte 3363321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3364321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3365321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3366321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3367321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3368321f8ee5SSean Christopherson KVM_SYNC_CRS | 3369321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 337023a60f83SCollin Walling KVM_SYNC_PFAULT | 337123a60f83SCollin Walling KVM_SYNC_DIAG318; 3372321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3373321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3374321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3375321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3376321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3377321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3378321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3379321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3380321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3381321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3382321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3383321f8ee5SSean Christopherson */ 3384321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3385321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3386321f8ee5SSean Christopherson else 3387321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3388321f8ee5SSean Christopherson 3389321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3390321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3391321f8ee5SSean Christopherson if (rc) 3392a2017f17SSean Christopherson goto out_free_sie_block; 3393321f8ee5SSean Christopherson } 3394321f8ee5SSean Christopherson 3395e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3396e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3397e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3398b0c632dbSHeiko Carstens 3399ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3400ff72bb55SSean Christopherson if (rc) 3401ff72bb55SSean Christopherson goto out_ucontrol_uninit; 3402e529ef66SSean Christopherson return 0; 3403e529ef66SSean Christopherson 3404ff72bb55SSean Christopherson out_ucontrol_uninit: 3405ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3406ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 34077b06bf2fSWei Yongjun out_free_sie_block: 34087b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3409e529ef66SSean Christopherson return rc; 3410b0c632dbSHeiko Carstens } 3411b0c632dbSHeiko Carstens 3412b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3413b0c632dbSHeiko Carstens { 34149b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 34159a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3416b0c632dbSHeiko Carstens } 3417b0c632dbSHeiko Carstens 3418199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3419199b5763SLongpeng(Mike) { 34200546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3421199b5763SLongpeng(Mike) } 3422199b5763SLongpeng(Mike) 342327406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 342449b99e1eSChristian Borntraeger { 3425805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 342661a6df54SDavid Hildenbrand exit_sie(vcpu); 342749b99e1eSChristian Borntraeger } 342849b99e1eSChristian Borntraeger 342927406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 343049b99e1eSChristian Borntraeger { 3431805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 343249b99e1eSChristian Borntraeger } 343349b99e1eSChristian Borntraeger 34348e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 34358e236546SChristian Borntraeger { 3436805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 343761a6df54SDavid Hildenbrand exit_sie(vcpu); 34388e236546SChristian Borntraeger } 34398e236546SChristian Borntraeger 34409ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 34419ea59728SDavid Hildenbrand { 34429ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 34439ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 34449ea59728SDavid Hildenbrand } 34459ea59728SDavid Hildenbrand 34468e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 34478e236546SChristian Borntraeger { 34489bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 34498e236546SChristian Borntraeger } 34508e236546SChristian Borntraeger 345149b99e1eSChristian Borntraeger /* 34529ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 345349b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 345449b99e1eSChristian Borntraeger * return immediately. */ 345549b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 345649b99e1eSChristian Borntraeger { 3457ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 34589ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 345949b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 346049b99e1eSChristian Borntraeger cpu_relax(); 346149b99e1eSChristian Borntraeger } 346249b99e1eSChristian Borntraeger 34638e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 34648e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 346549b99e1eSChristian Borntraeger { 34668e236546SChristian Borntraeger kvm_make_request(req, vcpu); 34678e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 346849b99e1eSChristian Borntraeger } 346949b99e1eSChristian Borntraeger 3470414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3471414d3b07SMartin Schwidefsky unsigned long end) 34722c70fe44SChristian Borntraeger { 34732c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 34742c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3475414d3b07SMartin Schwidefsky unsigned long prefix; 347646808a4cSMarc Zyngier unsigned long i; 34772c70fe44SChristian Borntraeger 347865d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 347965d0b0d4SDavid Hildenbrand return; 3480414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3481414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3482414d3b07SMartin Schwidefsky return; 34832c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 34842c70fe44SChristian Borntraeger /* match against both prefix pages */ 3485414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3486414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3487414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3488414d3b07SMartin Schwidefsky start, end); 34898e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 34902c70fe44SChristian Borntraeger } 34912c70fe44SChristian Borntraeger } 34922c70fe44SChristian Borntraeger } 34932c70fe44SChristian Borntraeger 34948b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 34958b905d28SChristian Borntraeger { 34968b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 34978b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 34986f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) { 34998b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 35008b905d28SChristian Borntraeger return true; 35018b905d28SChristian Borntraeger } 35028b905d28SChristian Borntraeger return false; 35038b905d28SChristian Borntraeger } 35048b905d28SChristian Borntraeger 3505b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3506b6d33834SChristoffer Dall { 3507b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3508b6d33834SChristoffer Dall BUG(); 3509b6d33834SChristoffer Dall return 0; 3510b6d33834SChristoffer Dall } 3511b6d33834SChristoffer Dall 351214eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 351314eebd91SCarsten Otte struct kvm_one_reg *reg) 351414eebd91SCarsten Otte { 351514eebd91SCarsten Otte int r = -EINVAL; 351614eebd91SCarsten Otte 351714eebd91SCarsten Otte switch (reg->id) { 351829b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 351929b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 352029b7c71bSCarsten Otte (u32 __user *)reg->addr); 352129b7c71bSCarsten Otte break; 352229b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 352329b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 352429b7c71bSCarsten Otte (u64 __user *)reg->addr); 352529b7c71bSCarsten Otte break; 352646a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 35274287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 352846a6dd1cSJason J. herne (u64 __user *)reg->addr); 352946a6dd1cSJason J. herne break; 353046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 353146a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 353246a6dd1cSJason J. herne (u64 __user *)reg->addr); 353346a6dd1cSJason J. herne break; 3534536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3535536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3536536336c2SDominik Dingel (u64 __user *)reg->addr); 3537536336c2SDominik Dingel break; 3538536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3539536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3540536336c2SDominik Dingel (u64 __user *)reg->addr); 3541536336c2SDominik Dingel break; 3542536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3543536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3544536336c2SDominik Dingel (u64 __user *)reg->addr); 3545536336c2SDominik Dingel break; 3546672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3547672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3548672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3549672550fbSChristian Borntraeger break; 3550afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3551afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3552afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3553afa45ff5SChristian Borntraeger break; 355414eebd91SCarsten Otte default: 355514eebd91SCarsten Otte break; 355614eebd91SCarsten Otte } 355714eebd91SCarsten Otte 355814eebd91SCarsten Otte return r; 355914eebd91SCarsten Otte } 356014eebd91SCarsten Otte 356114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 356214eebd91SCarsten Otte struct kvm_one_reg *reg) 356314eebd91SCarsten Otte { 356414eebd91SCarsten Otte int r = -EINVAL; 35654287f247SDavid Hildenbrand __u64 val; 356614eebd91SCarsten Otte 356714eebd91SCarsten Otte switch (reg->id) { 356829b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 356929b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 357029b7c71bSCarsten Otte (u32 __user *)reg->addr); 357129b7c71bSCarsten Otte break; 357229b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 357329b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 357429b7c71bSCarsten Otte (u64 __user *)reg->addr); 357529b7c71bSCarsten Otte break; 357646a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 35774287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 35784287f247SDavid Hildenbrand if (!r) 35794287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 358046a6dd1cSJason J. herne break; 358146a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 358246a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 358346a6dd1cSJason J. herne (u64 __user *)reg->addr); 358446a6dd1cSJason J. herne break; 3585536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3586536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3587536336c2SDominik Dingel (u64 __user *)reg->addr); 35889fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 35899fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3590536336c2SDominik Dingel break; 3591536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3592536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3593536336c2SDominik Dingel (u64 __user *)reg->addr); 3594536336c2SDominik Dingel break; 3595536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3596536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3597536336c2SDominik Dingel (u64 __user *)reg->addr); 3598536336c2SDominik Dingel break; 3599672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3600672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3601672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3602672550fbSChristian Borntraeger break; 3603afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3604afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3605afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3606afa45ff5SChristian Borntraeger break; 360714eebd91SCarsten Otte default: 360814eebd91SCarsten Otte break; 360914eebd91SCarsten Otte } 361014eebd91SCarsten Otte 361114eebd91SCarsten Otte return r; 361214eebd91SCarsten Otte } 3613b6d33834SChristoffer Dall 36147de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 3615b0c632dbSHeiko Carstens { 36167de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 36177de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 36187de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 36197de3f142SJanosch Frank 36207de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 36217de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 36227de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 36237de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 36247de3f142SJanosch Frank } 36257de3f142SJanosch Frank 36267de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 36277de3f142SJanosch Frank { 36287de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 36297de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 36307de3f142SJanosch Frank 3631e93fc7b4SChristian Borntraeger /* 3632e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 3633e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 3634e93fc7b4SChristian Borntraeger */ 36357de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 36367de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 36377de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 36387de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 36397de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 36407de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 36417de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 36427de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 3643e93fc7b4SChristian Borntraeger 3644e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 3645e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 3646e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 3647e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 3648e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 3649e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 3650e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 3651e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 3652e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 3653e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 3654e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 3655e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 36567de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 36570f303504SJanosch Frank /* 36580f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 36590f303504SJanosch Frank * them are overlayed and they are not accessible in this case 36600f303504SJanosch Frank * anyway. 36610f303504SJanosch Frank */ 36620f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 36637de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 36647de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 36657de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 36660f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 36670f303504SJanosch Frank } 36687de3f142SJanosch Frank } 36697de3f142SJanosch Frank 36707de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 36717de3f142SJanosch Frank { 36727de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 36737de3f142SJanosch Frank 36747de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 36757de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 36767de3f142SJanosch Frank 36777de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 36787de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 36797de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 36807de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 36817de3f142SJanosch Frank 36827de3f142SJanosch Frank regs->etoken = 0; 36837de3f142SJanosch Frank regs->etoken_extension = 0; 3684b0c632dbSHeiko Carstens } 3685b0c632dbSHeiko Carstens 3686b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3687b0c632dbSHeiko Carstens { 3688875656feSChristoffer Dall vcpu_load(vcpu); 36895a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3690875656feSChristoffer Dall vcpu_put(vcpu); 3691b0c632dbSHeiko Carstens return 0; 3692b0c632dbSHeiko Carstens } 3693b0c632dbSHeiko Carstens 3694b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3695b0c632dbSHeiko Carstens { 36961fc9b76bSChristoffer Dall vcpu_load(vcpu); 36975a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 36981fc9b76bSChristoffer Dall vcpu_put(vcpu); 3699b0c632dbSHeiko Carstens return 0; 3700b0c632dbSHeiko Carstens } 3701b0c632dbSHeiko Carstens 3702b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3703b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3704b0c632dbSHeiko Carstens { 3705b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3706b4ef9d4eSChristoffer Dall 370759674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3708b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3709b4ef9d4eSChristoffer Dall 3710b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3711b0c632dbSHeiko Carstens return 0; 3712b0c632dbSHeiko Carstens } 3713b0c632dbSHeiko Carstens 3714b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3715b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3716b0c632dbSHeiko Carstens { 3717bcdec41cSChristoffer Dall vcpu_load(vcpu); 3718bcdec41cSChristoffer Dall 371959674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3720b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3721bcdec41cSChristoffer Dall 3722bcdec41cSChristoffer Dall vcpu_put(vcpu); 3723b0c632dbSHeiko Carstens return 0; 3724b0c632dbSHeiko Carstens } 3725b0c632dbSHeiko Carstens 3726b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3727b0c632dbSHeiko Carstens { 37286a96bc7fSChristoffer Dall int ret = 0; 37296a96bc7fSChristoffer Dall 37306a96bc7fSChristoffer Dall vcpu_load(vcpu); 37316a96bc7fSChristoffer Dall 37326a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 37336a96bc7fSChristoffer Dall ret = -EINVAL; 37346a96bc7fSChristoffer Dall goto out; 37356a96bc7fSChristoffer Dall } 3736e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 37379abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3738a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3739a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 37409abc2a08SDavid Hildenbrand else 3741a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 37426a96bc7fSChristoffer Dall 37436a96bc7fSChristoffer Dall out: 37446a96bc7fSChristoffer Dall vcpu_put(vcpu); 37456a96bc7fSChristoffer Dall return ret; 3746b0c632dbSHeiko Carstens } 3747b0c632dbSHeiko Carstens 3748b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3749b0c632dbSHeiko Carstens { 37501393123eSChristoffer Dall vcpu_load(vcpu); 37511393123eSChristoffer Dall 37529abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 37539abc2a08SDavid Hildenbrand save_fpu_regs(); 37549abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3755a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3756a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 37579abc2a08SDavid Hildenbrand else 3758a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3759e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 37601393123eSChristoffer Dall 37611393123eSChristoffer Dall vcpu_put(vcpu); 3762b0c632dbSHeiko Carstens return 0; 3763b0c632dbSHeiko Carstens } 3764b0c632dbSHeiko Carstens 3765b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3766b0c632dbSHeiko Carstens { 3767b0c632dbSHeiko Carstens int rc = 0; 3768b0c632dbSHeiko Carstens 37697a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3770b0c632dbSHeiko Carstens rc = -EBUSY; 3771d7b0b5ebSCarsten Otte else { 3772d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3773d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3774d7b0b5ebSCarsten Otte } 3775b0c632dbSHeiko Carstens return rc; 3776b0c632dbSHeiko Carstens } 3777b0c632dbSHeiko Carstens 3778b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3779b0c632dbSHeiko Carstens struct kvm_translation *tr) 3780b0c632dbSHeiko Carstens { 3781b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3782b0c632dbSHeiko Carstens } 3783b0c632dbSHeiko Carstens 378427291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 378527291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 378627291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 378727291e21SDavid Hildenbrand 3788d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3789d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3790b0c632dbSHeiko Carstens { 379127291e21SDavid Hildenbrand int rc = 0; 379227291e21SDavid Hildenbrand 379366b56562SChristoffer Dall vcpu_load(vcpu); 379466b56562SChristoffer Dall 379527291e21SDavid Hildenbrand vcpu->guest_debug = 0; 379627291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 379727291e21SDavid Hildenbrand 379866b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 379966b56562SChristoffer Dall rc = -EINVAL; 380066b56562SChristoffer Dall goto out; 380166b56562SChristoffer Dall } 380266b56562SChristoffer Dall if (!sclp.has_gpere) { 380366b56562SChristoffer Dall rc = -EINVAL; 380466b56562SChristoffer Dall goto out; 380566b56562SChristoffer Dall } 380627291e21SDavid Hildenbrand 380727291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 380827291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 380927291e21SDavid Hildenbrand /* enforce guest PER */ 3810ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 381127291e21SDavid Hildenbrand 381227291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 381327291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 381427291e21SDavid Hildenbrand } else { 38159daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 381627291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 381727291e21SDavid Hildenbrand } 381827291e21SDavid Hildenbrand 381927291e21SDavid Hildenbrand if (rc) { 382027291e21SDavid Hildenbrand vcpu->guest_debug = 0; 382127291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 38229daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 382327291e21SDavid Hildenbrand } 382427291e21SDavid Hildenbrand 382566b56562SChristoffer Dall out: 382666b56562SChristoffer Dall vcpu_put(vcpu); 382727291e21SDavid Hildenbrand return rc; 3828b0c632dbSHeiko Carstens } 3829b0c632dbSHeiko Carstens 383062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 383162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 383262d9f0dbSMarcelo Tosatti { 3833fd232561SChristoffer Dall int ret; 3834fd232561SChristoffer Dall 3835fd232561SChristoffer Dall vcpu_load(vcpu); 3836fd232561SChristoffer Dall 38376352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3838fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 38396352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3840fd232561SChristoffer Dall 3841fd232561SChristoffer Dall vcpu_put(vcpu); 3842fd232561SChristoffer Dall return ret; 384362d9f0dbSMarcelo Tosatti } 384462d9f0dbSMarcelo Tosatti 384562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 384662d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 384762d9f0dbSMarcelo Tosatti { 38486352e4d2SDavid Hildenbrand int rc = 0; 38496352e4d2SDavid Hildenbrand 3850e83dff5eSChristoffer Dall vcpu_load(vcpu); 3851e83dff5eSChristoffer Dall 38526352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 385367cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 38546352e4d2SDavid Hildenbrand 38556352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 38566352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 3857fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 38586352e4d2SDavid Hildenbrand break; 38596352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 3860fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 38616352e4d2SDavid Hildenbrand break; 38626352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 38637c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 38647c36a3fcSJanosch Frank rc = -ENXIO; 38657c36a3fcSJanosch Frank break; 38667c36a3fcSJanosch Frank } 38677c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 38687c36a3fcSJanosch Frank break; 38696352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 38703b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 38716352e4d2SDavid Hildenbrand default: 38726352e4d2SDavid Hildenbrand rc = -ENXIO; 38736352e4d2SDavid Hildenbrand } 38746352e4d2SDavid Hildenbrand 3875e83dff5eSChristoffer Dall vcpu_put(vcpu); 38766352e4d2SDavid Hildenbrand return rc; 387762d9f0dbSMarcelo Tosatti } 387862d9f0dbSMarcelo Tosatti 38798ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 38808ad35755SDavid Hildenbrand { 38818d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 38828ad35755SDavid Hildenbrand } 38838ad35755SDavid Hildenbrand 38842c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 38852c70fe44SChristian Borntraeger { 38868ad35755SDavid Hildenbrand retry: 38878e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 38882fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3889586b7ccdSChristian Borntraeger return 0; 38902c70fe44SChristian Borntraeger /* 38912c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3892b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 38932c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 38942c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 38952c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 38962c70fe44SChristian Borntraeger */ 38978ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 38982c70fe44SChristian Borntraeger int rc; 3899b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3900fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3901b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3902aca411a4SJulius Niedworok if (rc) { 3903aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 39042c70fe44SChristian Borntraeger return rc; 3905aca411a4SJulius Niedworok } 39068ad35755SDavid Hildenbrand goto retry; 39072c70fe44SChristian Borntraeger } 39088ad35755SDavid Hildenbrand 3909d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3910d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3911d3d692c8SDavid Hildenbrand goto retry; 3912d3d692c8SDavid Hildenbrand } 3913d3d692c8SDavid Hildenbrand 39148ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 39158ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 39168ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3917ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 39188ad35755SDavid Hildenbrand } 39198ad35755SDavid Hildenbrand goto retry; 39208ad35755SDavid Hildenbrand } 39218ad35755SDavid Hildenbrand 39228ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 39238ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 39248ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 39259daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 39268ad35755SDavid Hildenbrand } 39278ad35755SDavid Hildenbrand goto retry; 39288ad35755SDavid Hildenbrand } 39298ad35755SDavid Hildenbrand 39306502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 39316502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 39326502a34cSDavid Hildenbrand goto retry; 39336502a34cSDavid Hildenbrand } 39346502a34cSDavid Hildenbrand 3935190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3936190df4a2SClaudio Imbrenda /* 3937c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3938190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3939190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3940190df4a2SClaudio Imbrenda */ 3941190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3942190df4a2SClaudio Imbrenda goto retry; 3943190df4a2SClaudio Imbrenda } 3944190df4a2SClaudio Imbrenda 3945190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3946190df4a2SClaudio Imbrenda /* 3947c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3948c9f0a2b8SJanosch Frank * CMM has been used. 3949190df4a2SClaudio Imbrenda */ 3950190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3951c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3952190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3953190df4a2SClaudio Imbrenda goto retry; 3954190df4a2SClaudio Imbrenda } 3955190df4a2SClaudio Imbrenda 39560759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 395772875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 39583194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 39593194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 39600759d068SDavid Hildenbrand 39612c70fe44SChristian Borntraeger return 0; 39622c70fe44SChristian Borntraeger } 39632c70fe44SChristian Borntraeger 3964*c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 39658fa1696eSCollin L. Walling { 39668fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 39672cfd7b73SHeiko Carstens union tod_clock clk; 396846808a4cSMarc Zyngier unsigned long i; 39698fa1696eSCollin L. Walling 39708fa1696eSCollin L. Walling preempt_disable(); 39718fa1696eSCollin L. Walling 39722cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 39738fa1696eSCollin L. Walling 39742cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 39750e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 39760e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 39772cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 39788fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 39798fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 39800e7def5fSDavid Hildenbrand } 39818fa1696eSCollin L. Walling 39828fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 39838fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 39848fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 39858fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 39868fa1696eSCollin L. Walling } 39878fa1696eSCollin L. Walling 39888fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 39898fa1696eSCollin L. Walling preempt_enable(); 3990*c0573ba5SClaudio Imbrenda } 3991*c0573ba5SClaudio Imbrenda 3992*c0573ba5SClaudio Imbrenda void kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 3993*c0573ba5SClaudio Imbrenda { 3994*c0573ba5SClaudio Imbrenda mutex_lock(&kvm->lock); 3995*c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 39968fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 39978fa1696eSCollin L. Walling } 39988fa1696eSCollin L. Walling 3999*c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4000*c0573ba5SClaudio Imbrenda { 4001*c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock)) 4002*c0573ba5SClaudio Imbrenda return 0; 4003*c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 4004*c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock); 4005*c0573ba5SClaudio Imbrenda return 1; 4006*c0573ba5SClaudio Imbrenda } 4007*c0573ba5SClaudio Imbrenda 4008fa576c58SThomas Huth /** 4009fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 4010fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 4011fa576c58SThomas Huth * @gpa: Guest physical address 4012fa576c58SThomas Huth * @writable: Whether the page should be writable or not 4013fa576c58SThomas Huth * 4014fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 4015fa576c58SThomas Huth * 4016fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 4017fa576c58SThomas Huth */ 4018fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 401924eb3a82SDominik Dingel { 4020527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 4021527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 402224eb3a82SDominik Dingel } 402324eb3a82SDominik Dingel 40243c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 40253c038e6bSDominik Dingel unsigned long token) 40263c038e6bSDominik Dingel { 40273c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 4028383d0b05SJens Freimann struct kvm_s390_irq irq; 40293c038e6bSDominik Dingel 40303c038e6bSDominik Dingel if (start_token) { 4031383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 4032383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 4033383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 40343c038e6bSDominik Dingel } else { 40353c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 4036383d0b05SJens Freimann inti.parm64 = token; 40373c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 40383c038e6bSDominik Dingel } 40393c038e6bSDominik Dingel } 40403c038e6bSDominik Dingel 40412a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 40423c038e6bSDominik Dingel struct kvm_async_pf *work) 40433c038e6bSDominik Dingel { 40443c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 40453c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 40462a18b7e7SVitaly Kuznetsov 40472a18b7e7SVitaly Kuznetsov return true; 40483c038e6bSDominik Dingel } 40493c038e6bSDominik Dingel 40503c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 40513c038e6bSDominik Dingel struct kvm_async_pf *work) 40523c038e6bSDominik Dingel { 40533c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 40543c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 40553c038e6bSDominik Dingel } 40563c038e6bSDominik Dingel 40573c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 40583c038e6bSDominik Dingel struct kvm_async_pf *work) 40593c038e6bSDominik Dingel { 40603c038e6bSDominik Dingel /* s390 will always inject the page directly */ 40613c038e6bSDominik Dingel } 40623c038e6bSDominik Dingel 40637c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 40643c038e6bSDominik Dingel { 40653c038e6bSDominik Dingel /* 40663c038e6bSDominik Dingel * s390 will always inject the page directly, 40673c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 40683c038e6bSDominik Dingel */ 40693c038e6bSDominik Dingel return true; 40703c038e6bSDominik Dingel } 40713c038e6bSDominik Dingel 4072e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 40733c038e6bSDominik Dingel { 40743c038e6bSDominik Dingel hva_t hva; 40753c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 40763c038e6bSDominik Dingel 40773c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4078e8c22266SVitaly Kuznetsov return false; 40793c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 40803c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4081e8c22266SVitaly Kuznetsov return false; 40823c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4083e8c22266SVitaly Kuznetsov return false; 40849a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4085e8c22266SVitaly Kuznetsov return false; 4086b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4087e8c22266SVitaly Kuznetsov return false; 40883c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4089e8c22266SVitaly Kuznetsov return false; 40903c038e6bSDominik Dingel 409181480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 409281480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 409381480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4094e8c22266SVitaly Kuznetsov return false; 40953c038e6bSDominik Dingel 4096e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 40973c038e6bSDominik Dingel } 40983c038e6bSDominik Dingel 40993fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4100b0c632dbSHeiko Carstens { 41013fb4c40fSThomas Huth int rc, cpuflags; 4102e168bf8dSCarsten Otte 41033c038e6bSDominik Dingel /* 41043c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 41053c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 41063c038e6bSDominik Dingel * handled outside the worker. 41073c038e6bSDominik Dingel */ 41083c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 41093c038e6bSDominik Dingel 41107ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 41117ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4112b0c632dbSHeiko Carstens 4113b0c632dbSHeiko Carstens if (need_resched()) 4114b0c632dbSHeiko Carstens schedule(); 4115b0c632dbSHeiko Carstens 411679395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 411779395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 411879395031SJens Freimann if (rc) 411979395031SJens Freimann return rc; 412079395031SJens Freimann } 41210ff31867SCarsten Otte 41222c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 41232c70fe44SChristian Borntraeger if (rc) 41242c70fe44SChristian Borntraeger return rc; 41252c70fe44SChristian Borntraeger 412627291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 412727291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 412827291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 412927291e21SDavid Hildenbrand } 413027291e21SDavid Hildenbrand 41314eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 41329f30f621SMichael Mueller 4133b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 41343fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 41353fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 41363fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 41372b29a9fdSDominik Dingel 41383fb4c40fSThomas Huth return 0; 41393fb4c40fSThomas Huth } 41403fb4c40fSThomas Huth 4141492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4142492d8642SThomas Huth { 414356317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 414456317920SDavid Hildenbrand .code = PGM_ADDRESSING, 414556317920SDavid Hildenbrand }; 414656317920SDavid Hildenbrand u8 opcode, ilen; 4147492d8642SThomas Huth int rc; 4148492d8642SThomas Huth 4149492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4150492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4151492d8642SThomas Huth 4152492d8642SThomas Huth /* 4153492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4154492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4155492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4156492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4157492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4158492d8642SThomas Huth * to be able to forward the PSW. 4159492d8642SThomas Huth */ 41603fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 416156317920SDavid Hildenbrand ilen = insn_length(opcode); 41629b0d721aSDavid Hildenbrand if (rc < 0) { 41639b0d721aSDavid Hildenbrand return rc; 41649b0d721aSDavid Hildenbrand } else if (rc) { 41659b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 41669b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 41679b0d721aSDavid Hildenbrand * nullification if necessary. 41689b0d721aSDavid Hildenbrand */ 41699b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 41709b0d721aSDavid Hildenbrand ilen = 4; 41719b0d721aSDavid Hildenbrand } 417256317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 417356317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 417456317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4175492d8642SThomas Huth } 4176492d8642SThomas Huth 41773fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 41783fb4c40fSThomas Huth { 41794d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 41804d62fcc0SQingFeng Hao struct sie_page *sie_page; 41814d62fcc0SQingFeng Hao 41822b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 41832b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 41842b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 41852b29a9fdSDominik Dingel 418627291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 418727291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 418827291e21SDavid Hildenbrand 41897ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 41907ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 419171f116bfSDavid Hildenbrand 41924d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 41934d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 41944d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 41954d62fcc0SQingFeng Hao struct sie_page, sie_block); 41964d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 41974d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 41984d62fcc0SQingFeng Hao return 0; 41994d62fcc0SQingFeng Hao } 42004d62fcc0SQingFeng Hao 420171f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 420271f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 420371f116bfSDavid Hildenbrand 420471f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 420571f116bfSDavid Hildenbrand return rc; 420671f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 420771f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 420871f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 420971f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 421071f116bfSDavid Hildenbrand return -EREMOTE; 421171f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 421271f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 421371f116bfSDavid Hildenbrand return 0; 4214210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4215210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4216210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4217210b1607SThomas Huth current->thread.gmap_addr; 4218210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 421971f116bfSDavid Hildenbrand return -EREMOTE; 422024eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 42213c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 422224eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 422371f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 422471f116bfSDavid Hildenbrand return 0; 422550a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 422671f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4227fa576c58SThomas Huth } 422871f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 42293fb4c40fSThomas Huth } 42303fb4c40fSThomas Huth 42313adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 42323fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 42333fb4c40fSThomas Huth { 42343fb4c40fSThomas Huth int rc, exit_reason; 4235c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 42363fb4c40fSThomas Huth 4237800c1065SThomas Huth /* 4238800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4239800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4240800c1065SThomas Huth */ 4241800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 4242800c1065SThomas Huth 4243a76ccff6SThomas Huth do { 42443fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 42453fb4c40fSThomas Huth if (rc) 4246a76ccff6SThomas Huth break; 42473fb4c40fSThomas Huth 4248800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 42493fb4c40fSThomas Huth /* 4250a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4251a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 42523fb4c40fSThomas Huth */ 42530097d12eSChristian Borntraeger local_irq_disable(); 42546edaa530SPaolo Bonzini guest_enter_irqoff(); 4255db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 42560097d12eSChristian Borntraeger local_irq_enable(); 4257c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4258c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4259c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4260c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4261c8aac234SJanosch Frank } 426256e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 426356e62a73SSven Schnelle load_fpu_regs(); 4264a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4265a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4266c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4267c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4268c8aac234SJanosch Frank sie_page->pv_grregs, 4269c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 42703adae0b4SJanosch Frank /* 42713adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 42723adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 42733adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 42743adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 42753adae0b4SJanosch Frank */ 42763adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 42773adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 42783adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 42793adae0b4SJanosch Frank } 4280c8aac234SJanosch Frank } 42810097d12eSChristian Borntraeger local_irq_disable(); 4282db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 42836edaa530SPaolo Bonzini guest_exit_irqoff(); 42840097d12eSChristian Borntraeger local_irq_enable(); 4285800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 42863fb4c40fSThomas Huth 42873fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 428827291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 42893fb4c40fSThomas Huth 4290800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 4291e168bf8dSCarsten Otte return rc; 4292b0c632dbSHeiko Carstens } 4293b0c632dbSHeiko Carstens 42942f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4295b028ee3eSDavid Hildenbrand { 42962f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 42974d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 42984e0b1ab7SFan Zhang struct gs_cb *gscb; 42994d5f2c04SChristian Borntraeger 43004d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 43014e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4302b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4303b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4304b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4305b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4306b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4307b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4308b028ee3eSDavid Hildenbrand } 4309b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4310b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4311b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4312b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 43139fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 43149fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4315b028ee3eSDavid Hildenbrand } 431623a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 431723a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 431823a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 43193fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 432023a60f83SCollin Walling } 432180cd8763SFan Zhang /* 432280cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 432380cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 432480cd8763SFan Zhang */ 432580cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 43264d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4327bb59c2daSAlice Frosi riccb->v && 43280c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 43294d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 43300c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 433180cd8763SFan Zhang } 43324e0b1ab7SFan Zhang /* 43334e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 43344e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 43354e0b1ab7SFan Zhang */ 43364e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 43374e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 43384e0b1ab7SFan Zhang gscb->gssm && 43394e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 43404e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 43414e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 43424e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 43434e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 434480cd8763SFan Zhang } 434535b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 434635b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 434735b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 434835b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 434935b3fde6SChristian Borntraeger } 43504e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 43514e0b1ab7SFan Zhang preempt_disable(); 43524e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 43534e0b1ab7SFan Zhang if (current->thread.gs_cb) { 43544e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 43554e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 43564e0b1ab7SFan Zhang } 43574e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 43584e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 43594e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 43604e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 43614e0b1ab7SFan Zhang } 43624e0b1ab7SFan Zhang preempt_enable(); 43634e0b1ab7SFan Zhang } 4364a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4365811ea797SJanosch Frank } 4366811ea797SJanosch Frank 43672f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4368811ea797SJanosch Frank { 43692f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 43702f0a83beSTianjia Zhang 4371811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4372811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4373811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4374811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4375811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4376811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4377811ea797SJanosch Frank } 4378811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4379811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4380811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4381811ea797SJanosch Frank } 4382811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4383811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4384811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4385811ea797SJanosch Frank save_fpu_regs(); 4386811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4387811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4388811ea797SJanosch Frank if (MACHINE_HAS_VX) 4389811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4390811ea797SJanosch Frank else 4391811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4392811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4393811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4394811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4395811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4396811ea797SJanosch Frank 4397811ea797SJanosch Frank /* Sync fmt2 only data */ 4398811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 43992f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4400811ea797SJanosch Frank } else { 4401811ea797SJanosch Frank /* 4402811ea797SJanosch Frank * In several places we have to modify our internal view to 4403811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4404811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4405811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4406811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4407811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4408811ea797SJanosch Frank * do only accept the condition code from userspace. 4409811ea797SJanosch Frank */ 4410811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4411811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4412811ea797SJanosch Frank PSW_MASK_CC; 4413811ea797SJanosch Frank } 441480cd8763SFan Zhang 4415b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4416b028ee3eSDavid Hildenbrand } 4417b028ee3eSDavid Hildenbrand 44182f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4419b028ee3eSDavid Hildenbrand { 44202f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 44212f0a83beSTianjia Zhang 4422b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4423b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4424b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 442535b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 442623a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 44274e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 442844bada28SHeiko Carstens preempt_disable(); 44294e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 44304e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 44314e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 44324e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 44334e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 44344e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 44354e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 44364e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 443744bada28SHeiko Carstens preempt_enable(); 44384e0b1ab7SFan Zhang } 4439a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4440b028ee3eSDavid Hildenbrand } 4441b028ee3eSDavid Hildenbrand 44422f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4443811ea797SJanosch Frank { 44442f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 44452f0a83beSTianjia Zhang 4446811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4447811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4448811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4449811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4450811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4451811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4452811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4453811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4454811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4455811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4456811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4457811ea797SJanosch Frank /* Save guest register state */ 4458811ea797SJanosch Frank save_fpu_regs(); 4459811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4460811ea797SJanosch Frank /* Restore will be done lazily at return */ 4461811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4462811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4463811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 44642f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4465811ea797SJanosch Frank } 4466811ea797SJanosch Frank 44671b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4468b0c632dbSHeiko Carstens { 44691b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 44708f2abe6aSChristian Borntraeger int rc; 4471b0c632dbSHeiko Carstens 4472460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4473460df4c1SPaolo Bonzini return -EINTR; 4474460df4c1SPaolo Bonzini 4475200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4476200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4477200824f5SThomas Huth return -EINVAL; 4478200824f5SThomas Huth 4479accb757dSChristoffer Dall vcpu_load(vcpu); 4480accb757dSChristoffer Dall 448127291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 448227291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4483accb757dSChristoffer Dall rc = 0; 4484accb757dSChristoffer Dall goto out; 448527291e21SDavid Hildenbrand } 448627291e21SDavid Hildenbrand 448720b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4488b0c632dbSHeiko Carstens 4489fe28c786SJanosch Frank /* 4490fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4491fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4492fe28c786SJanosch Frank */ 44936352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 44946852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 44956352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4496ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 44976352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4498accb757dSChristoffer Dall rc = -EINVAL; 4499accb757dSChristoffer Dall goto out; 45006352e4d2SDavid Hildenbrand } 4501b0c632dbSHeiko Carstens 45022f0a83beSTianjia Zhang sync_regs(vcpu); 4503db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4504d7b0b5ebSCarsten Otte 4505dab4079dSHeiko Carstens might_fault(); 4506e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 45079ace903dSChristian Ehrhardt 4508b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4509b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 45108f2abe6aSChristian Borntraeger rc = -EINTR; 4511b1d16c49SChristian Ehrhardt } 45128f2abe6aSChristian Borntraeger 451327291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 451427291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 451527291e21SDavid Hildenbrand rc = 0; 451627291e21SDavid Hildenbrand } 451727291e21SDavid Hildenbrand 45188f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 451971f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 45208f2abe6aSChristian Borntraeger rc = 0; 45218f2abe6aSChristian Borntraeger } 45228f2abe6aSChristian Borntraeger 4523db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 45242f0a83beSTianjia Zhang store_regs(vcpu); 4525d7b0b5ebSCarsten Otte 452620b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4527b0c632dbSHeiko Carstens 4528b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4529accb757dSChristoffer Dall out: 4530accb757dSChristoffer Dall vcpu_put(vcpu); 45317e8e6ab4SHeiko Carstens return rc; 4532b0c632dbSHeiko Carstens } 4533b0c632dbSHeiko Carstens 4534b0c632dbSHeiko Carstens /* 4535b0c632dbSHeiko Carstens * store status at address 4536b0c632dbSHeiko Carstens * we use have two special cases: 4537b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4538b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4539b0c632dbSHeiko Carstens */ 4540d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4541b0c632dbSHeiko Carstens { 4542092670cdSCarsten Otte unsigned char archmode = 1; 45439abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4544fda902cbSMichael Mueller unsigned int px; 45454287f247SDavid Hildenbrand u64 clkcomp, cputm; 4546d0bce605SHeiko Carstens int rc; 4547b0c632dbSHeiko Carstens 4548d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4549d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4550d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4551b0c632dbSHeiko Carstens return -EFAULT; 4552d9a3a09aSMartin Schwidefsky gpa = 0; 4553d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4554d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4555b0c632dbSHeiko Carstens return -EFAULT; 4556d9a3a09aSMartin Schwidefsky gpa = px; 4557d9a3a09aSMartin Schwidefsky } else 4558d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 45599abc2a08SDavid Hildenbrand 45609abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 45619abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 45629522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4563d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 45649abc2a08SDavid Hildenbrand fprs, 128); 45659abc2a08SDavid Hildenbrand } else { 45669abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 45676fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 45689abc2a08SDavid Hildenbrand } 4569d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4570d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4571d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4572d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4573d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4574fda902cbSMichael Mueller &px, 4); 4575d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 45769abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4577d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4578d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 45794287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4580d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 45814287f247SDavid Hildenbrand &cputm, 8); 4582178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4583d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4584d0bce605SHeiko Carstens &clkcomp, 8); 4585d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4586d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4587d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4588d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4589d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4590b0c632dbSHeiko Carstens } 4591b0c632dbSHeiko Carstens 4592e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4593e879892cSThomas Huth { 4594e879892cSThomas Huth /* 4595e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 459631d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4597e879892cSThomas Huth * it into the save area 4598e879892cSThomas Huth */ 4599d0164ee2SHendrik Brueckner save_fpu_regs(); 46009abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4601e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4602e879892cSThomas Huth 4603e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4604e879892cSThomas Huth } 4605e879892cSThomas Huth 46068ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 46078ad35755SDavid Hildenbrand { 46088ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 46098e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 46108ad35755SDavid Hildenbrand } 46118ad35755SDavid Hildenbrand 46128ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 46138ad35755SDavid Hildenbrand { 461446808a4cSMarc Zyngier unsigned long i; 46158ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 46168ad35755SDavid Hildenbrand 46178ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 46188ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 46198ad35755SDavid Hildenbrand } 46208ad35755SDavid Hildenbrand } 46218ad35755SDavid Hildenbrand 46228ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 46238ad35755SDavid Hildenbrand { 462409a400e7SDavid Hildenbrand if (!sclp.has_ibs) 462509a400e7SDavid Hildenbrand return; 46268ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 46278e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 46288ad35755SDavid Hildenbrand } 46298ad35755SDavid Hildenbrand 4630fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 46316852d7b6SDavid Hildenbrand { 4632fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 46338ad35755SDavid Hildenbrand 46348ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4635fe28c786SJanosch Frank return 0; 46368ad35755SDavid Hildenbrand 46376852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 46388ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4639433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 46408ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 46418ad35755SDavid Hildenbrand 4642fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 4643fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4644fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 4645fe28c786SJanosch Frank if (r) { 4646fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4647fe28c786SJanosch Frank return r; 4648fe28c786SJanosch Frank } 4649fe28c786SJanosch Frank } 4650fe28c786SJanosch Frank 46518ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 4652113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 46538ad35755SDavid Hildenbrand started_vcpus++; 46548ad35755SDavid Hildenbrand } 46558ad35755SDavid Hildenbrand 46568ad35755SDavid Hildenbrand if (started_vcpus == 0) { 46578ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 46588ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 46598ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 46608ad35755SDavid Hildenbrand /* 46618ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 46628ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 466338860756SBhaskar Chowdhury * outstanding ENABLE requests. 46648ad35755SDavid Hildenbrand */ 46658ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 46668ad35755SDavid Hildenbrand } 46678ad35755SDavid Hildenbrand 46689daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 46698ad35755SDavid Hildenbrand /* 467072f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 467172f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 467272f21820SChristian Borntraeger * refresh our view. 467372f21820SChristian Borntraeger */ 467472f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 467572f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 467672f21820SChristian Borntraeger /* 46778ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 46788ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 46798ad35755SDavid Hildenbrand */ 4680d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4681433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4682fe28c786SJanosch Frank return 0; 46836852d7b6SDavid Hildenbrand } 46846852d7b6SDavid Hildenbrand 4685fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 46866852d7b6SDavid Hildenbrand { 4687fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 46888ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 46898ad35755SDavid Hildenbrand 46908ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 4691fe28c786SJanosch Frank return 0; 46928ad35755SDavid Hildenbrand 46936852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 46948ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4695433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 46968ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 46978ad35755SDavid Hildenbrand 4698fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 4699fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4700fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 4701fe28c786SJanosch Frank if (r) { 4702fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4703fe28c786SJanosch Frank return r; 4704fe28c786SJanosch Frank } 4705fe28c786SJanosch Frank } 4706fe28c786SJanosch Frank 4707812de046SEric Farman /* 4708812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag, 4709812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders 4710812de046SEric Farman * have been fully processed. This will ensure that the VCPU 4711812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE. 4712812de046SEric Farman */ 4713812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 47146cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 471532f5ff63SDavid Hildenbrand 47168ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 47178ad35755SDavid Hildenbrand 47188ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 4719113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 4720113d10bcSMarc Zyngier 4721113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 47228ad35755SDavid Hildenbrand started_vcpus++; 4723113d10bcSMarc Zyngier started_vcpu = tmp; 47248ad35755SDavid Hildenbrand } 47258ad35755SDavid Hildenbrand } 47268ad35755SDavid Hildenbrand 47278ad35755SDavid Hildenbrand if (started_vcpus == 1) { 47288ad35755SDavid Hildenbrand /* 47298ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 47308ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 47318ad35755SDavid Hildenbrand */ 47328ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 47338ad35755SDavid Hildenbrand } 47348ad35755SDavid Hildenbrand 4735433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4736fe28c786SJanosch Frank return 0; 47376852d7b6SDavid Hildenbrand } 47386852d7b6SDavid Hildenbrand 4739d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4740d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4741d6712df9SCornelia Huck { 4742d6712df9SCornelia Huck int r; 4743d6712df9SCornelia Huck 4744d6712df9SCornelia Huck if (cap->flags) 4745d6712df9SCornelia Huck return -EINVAL; 4746d6712df9SCornelia Huck 4747d6712df9SCornelia Huck switch (cap->cap) { 4748fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4749fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4750fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4751c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4752fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4753fa6b7fe9SCornelia Huck } 4754fa6b7fe9SCornelia Huck r = 0; 4755fa6b7fe9SCornelia Huck break; 4756d6712df9SCornelia Huck default: 4757d6712df9SCornelia Huck r = -EINVAL; 4758d6712df9SCornelia Huck break; 4759d6712df9SCornelia Huck } 4760d6712df9SCornelia Huck return r; 4761d6712df9SCornelia Huck } 4762d6712df9SCornelia Huck 47630e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu, 476419e12277SJanosch Frank struct kvm_s390_mem_op *mop) 476519e12277SJanosch Frank { 476619e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 476719e12277SJanosch Frank int r = 0; 476819e12277SJanosch Frank 476919e12277SJanosch Frank if (mop->flags || !mop->size) 477019e12277SJanosch Frank return -EINVAL; 477119e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 477219e12277SJanosch Frank return -EINVAL; 477319e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 477419e12277SJanosch Frank return -E2BIG; 47752c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu)) 47762c212e1bSJanis Schoetterl-Glausch return -EINVAL; 477719e12277SJanosch Frank 477819e12277SJanosch Frank switch (mop->op) { 477919e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 478019e12277SJanosch Frank if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) + 478119e12277SJanosch Frank mop->sida_offset), mop->size)) 478219e12277SJanosch Frank r = -EFAULT; 478319e12277SJanosch Frank 478419e12277SJanosch Frank break; 478519e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 478619e12277SJanosch Frank if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) + 478719e12277SJanosch Frank mop->sida_offset), uaddr, mop->size)) 478819e12277SJanosch Frank r = -EFAULT; 478919e12277SJanosch Frank break; 479019e12277SJanosch Frank } 479119e12277SJanosch Frank return r; 479219e12277SJanosch Frank } 47930e1234c0SJanis Schoetterl-Glausch 47940e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, 479541408c28SThomas Huth struct kvm_s390_mem_op *mop) 479641408c28SThomas Huth { 479741408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 479841408c28SThomas Huth void *tmpbuf = NULL; 479919e12277SJanosch Frank int r = 0; 480041408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 4801e9e9feebSJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_CHECK_ONLY 4802e9e9feebSJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_SKEY_PROTECTION; 480341408c28SThomas Huth 4804a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 480541408c28SThomas Huth return -EINVAL; 480641408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 480741408c28SThomas Huth return -E2BIG; 480819e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 480919e12277SJanosch Frank return -EINVAL; 4810e9e9feebSJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 4811e9e9feebSJanis Schoetterl-Glausch if (access_key_invalid(mop->key)) 4812e9e9feebSJanis Schoetterl-Glausch return -EINVAL; 4813e9e9feebSJanis Schoetterl-Glausch } else { 4814e9e9feebSJanis Schoetterl-Glausch mop->key = 0; 4815e9e9feebSJanis Schoetterl-Glausch } 481641408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 481741408c28SThomas Huth tmpbuf = vmalloc(mop->size); 481841408c28SThomas Huth if (!tmpbuf) 481941408c28SThomas Huth return -ENOMEM; 482041408c28SThomas Huth } 482141408c28SThomas Huth 482241408c28SThomas Huth switch (mop->op) { 482341408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 482441408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 4825e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 4826e9e9feebSJanis Schoetterl-Glausch GACC_FETCH, mop->key); 482741408c28SThomas Huth break; 482841408c28SThomas Huth } 4829e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 4830e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 483141408c28SThomas Huth if (r == 0) { 483241408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 483341408c28SThomas Huth r = -EFAULT; 483441408c28SThomas Huth } 483541408c28SThomas Huth break; 483641408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 483741408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 4838e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 4839e9e9feebSJanis Schoetterl-Glausch GACC_STORE, mop->key); 484041408c28SThomas Huth break; 484141408c28SThomas Huth } 484241408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 484341408c28SThomas Huth r = -EFAULT; 484441408c28SThomas Huth break; 484541408c28SThomas Huth } 4846e9e9feebSJanis Schoetterl-Glausch r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 4847e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 484841408c28SThomas Huth break; 484941408c28SThomas Huth } 485041408c28SThomas Huth 485141408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 485241408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 485341408c28SThomas Huth 485441408c28SThomas Huth vfree(tmpbuf); 485541408c28SThomas Huth return r; 485641408c28SThomas Huth } 485741408c28SThomas Huth 48580e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu, 485919e12277SJanosch Frank struct kvm_s390_mem_op *mop) 486019e12277SJanosch Frank { 486119e12277SJanosch Frank int r, srcu_idx; 486219e12277SJanosch Frank 486319e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 486419e12277SJanosch Frank 486519e12277SJanosch Frank switch (mop->op) { 486619e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 486719e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 48680e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop); 486919e12277SJanosch Frank break; 487019e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 487119e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 487219e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 48730e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop); 487419e12277SJanosch Frank break; 487519e12277SJanosch Frank default: 487619e12277SJanosch Frank r = -EINVAL; 487719e12277SJanosch Frank } 487819e12277SJanosch Frank 487919e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 488019e12277SJanosch Frank return r; 488119e12277SJanosch Frank } 488219e12277SJanosch Frank 48835cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4884b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4885b0c632dbSHeiko Carstens { 4886b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4887b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4888b0c632dbSHeiko Carstens 488993736624SAvi Kivity switch (ioctl) { 489047b43c52SJens Freimann case KVM_S390_IRQ: { 489147b43c52SJens Freimann struct kvm_s390_irq s390irq; 489247b43c52SJens Freimann 489347b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 48949b062471SChristoffer Dall return -EFAULT; 48959b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 489647b43c52SJens Freimann } 489793736624SAvi Kivity case KVM_S390_INTERRUPT: { 4898ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 489953936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 4900ba5c1e9bSCarsten Otte 4901ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 49029b062471SChristoffer Dall return -EFAULT; 4903383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4904383d0b05SJens Freimann return -EINVAL; 49059b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4906ba5c1e9bSCarsten Otte } 49079b062471SChristoffer Dall } 49085cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 49095cb0944cSPaolo Bonzini } 49105cb0944cSPaolo Bonzini 49115cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 49125cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 49135cb0944cSPaolo Bonzini { 49145cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 49155cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 49165cb0944cSPaolo Bonzini int idx; 49175cb0944cSPaolo Bonzini long r; 49188a8378faSJanosch Frank u16 rc, rrc; 49199b062471SChristoffer Dall 49209b062471SChristoffer Dall vcpu_load(vcpu); 49219b062471SChristoffer Dall 49229b062471SChristoffer Dall switch (ioctl) { 4923b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4924800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 492555680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 4926800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4927bc923cc9SAvi Kivity break; 4928b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4929b0c632dbSHeiko Carstens psw_t psw; 4930b0c632dbSHeiko Carstens 4931bc923cc9SAvi Kivity r = -EFAULT; 4932b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4933bc923cc9SAvi Kivity break; 4934bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4935bc923cc9SAvi Kivity break; 4936b0c632dbSHeiko Carstens } 49377de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 49387de3f142SJanosch Frank r = 0; 49397de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 49408a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 49418a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 49428a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 49438a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 49448a8378faSJanosch Frank rc, rrc); 49458a8378faSJanosch Frank } 49467de3f142SJanosch Frank break; 4947b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 49487de3f142SJanosch Frank r = 0; 49497de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 49508a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 49518a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 49528a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 49538a8378faSJanosch Frank &rc, &rrc); 49548a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 49558a8378faSJanosch Frank rc, rrc); 49568a8378faSJanosch Frank } 49577de3f142SJanosch Frank break; 49587de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 49597de3f142SJanosch Frank r = 0; 49607de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 49618a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 49628a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 49638a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 49648a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 49658a8378faSJanosch Frank rc, rrc); 49668a8378faSJanosch Frank } 4967bc923cc9SAvi Kivity break; 496814eebd91SCarsten Otte case KVM_SET_ONE_REG: 496914eebd91SCarsten Otte case KVM_GET_ONE_REG: { 497014eebd91SCarsten Otte struct kvm_one_reg reg; 497168cf7b1fSJanosch Frank r = -EINVAL; 497268cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 497368cf7b1fSJanosch Frank break; 497414eebd91SCarsten Otte r = -EFAULT; 497514eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 497614eebd91SCarsten Otte break; 497714eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 497814eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 497914eebd91SCarsten Otte else 498014eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 498114eebd91SCarsten Otte break; 498214eebd91SCarsten Otte } 498327e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 498427e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 498527e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 498627e0393fSCarsten Otte 498727e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 498827e0393fSCarsten Otte r = -EFAULT; 498927e0393fSCarsten Otte break; 499027e0393fSCarsten Otte } 499127e0393fSCarsten Otte 499227e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 499327e0393fSCarsten Otte r = -EINVAL; 499427e0393fSCarsten Otte break; 499527e0393fSCarsten Otte } 499627e0393fSCarsten Otte 499727e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 499827e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 499927e0393fSCarsten Otte break; 500027e0393fSCarsten Otte } 500127e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 500227e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 500327e0393fSCarsten Otte 500427e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 500527e0393fSCarsten Otte r = -EFAULT; 500627e0393fSCarsten Otte break; 500727e0393fSCarsten Otte } 500827e0393fSCarsten Otte 500927e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 501027e0393fSCarsten Otte r = -EINVAL; 501127e0393fSCarsten Otte break; 501227e0393fSCarsten Otte } 501327e0393fSCarsten Otte 501427e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 501527e0393fSCarsten Otte ucasmap.length); 501627e0393fSCarsten Otte break; 501727e0393fSCarsten Otte } 501827e0393fSCarsten Otte #endif 5019ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 5020527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 5021ccc7910fSCarsten Otte break; 5022ccc7910fSCarsten Otte } 5023d6712df9SCornelia Huck case KVM_ENABLE_CAP: 5024d6712df9SCornelia Huck { 5025d6712df9SCornelia Huck struct kvm_enable_cap cap; 5026d6712df9SCornelia Huck r = -EFAULT; 5027d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 5028d6712df9SCornelia Huck break; 5029d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 5030d6712df9SCornelia Huck break; 5031d6712df9SCornelia Huck } 503241408c28SThomas Huth case KVM_S390_MEM_OP: { 503341408c28SThomas Huth struct kvm_s390_mem_op mem_op; 503441408c28SThomas Huth 503541408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 50360e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op); 503741408c28SThomas Huth else 503841408c28SThomas Huth r = -EFAULT; 503941408c28SThomas Huth break; 504041408c28SThomas Huth } 5041816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 5042816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5043816c7667SJens Freimann 5044816c7667SJens Freimann r = -EFAULT; 5045816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5046816c7667SJens Freimann break; 5047816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 5048816c7667SJens Freimann irq_state.len == 0 || 5049816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 5050816c7667SJens Freimann r = -EINVAL; 5051816c7667SJens Freimann break; 5052816c7667SJens Freimann } 5053bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5054816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 5055816c7667SJens Freimann (void __user *) irq_state.buf, 5056816c7667SJens Freimann irq_state.len); 5057816c7667SJens Freimann break; 5058816c7667SJens Freimann } 5059816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 5060816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5061816c7667SJens Freimann 5062816c7667SJens Freimann r = -EFAULT; 5063816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5064816c7667SJens Freimann break; 5065816c7667SJens Freimann if (irq_state.len == 0) { 5066816c7667SJens Freimann r = -EINVAL; 5067816c7667SJens Freimann break; 5068816c7667SJens Freimann } 5069bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5070816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 5071816c7667SJens Freimann (__u8 __user *) irq_state.buf, 5072816c7667SJens Freimann irq_state.len); 5073816c7667SJens Freimann break; 5074816c7667SJens Freimann } 5075b0c632dbSHeiko Carstens default: 50763e6afcf1SCarsten Otte r = -ENOTTY; 5077b0c632dbSHeiko Carstens } 50789b062471SChristoffer Dall 50799b062471SChristoffer Dall vcpu_put(vcpu); 5080bc923cc9SAvi Kivity return r; 5081b0c632dbSHeiko Carstens } 5082b0c632dbSHeiko Carstens 50831499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 50845b1c1493SCarsten Otte { 50855b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 50865b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 50875b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 50885b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 50895b1c1493SCarsten Otte get_page(vmf->page); 50905b1c1493SCarsten Otte return 0; 50915b1c1493SCarsten Otte } 50925b1c1493SCarsten Otte #endif 50935b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 50945b1c1493SCarsten Otte } 50955b1c1493SCarsten Otte 5096b0c632dbSHeiko Carstens /* Section: memory related */ 5097f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5098537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5099537a17b3SSean Christopherson struct kvm_memory_slot *new, 51007b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5101b0c632dbSHeiko Carstens { 5102ec5c8697SSean Christopherson gpa_t size; 5103ec5c8697SSean Christopherson 5104ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5105ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5106ec5c8697SSean Christopherson return -EINVAL; 5107ec5c8697SSean Christopherson 5108ec5c8697SSean Christopherson if (change == KVM_MR_DELETE || change == KVM_MR_FLAGS_ONLY) 5109ec5c8697SSean Christopherson return 0; 5110cf5b4869SSean Christopherson 5111dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 5112dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 5113dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 5114dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 5115b0c632dbSHeiko Carstens 5116cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5117b0c632dbSHeiko Carstens return -EINVAL; 5118b0c632dbSHeiko Carstens 5119ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5120cf5b4869SSean Christopherson if (size & 0xffffful) 5121b0c632dbSHeiko Carstens return -EINVAL; 5122b0c632dbSHeiko Carstens 5123cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5124a3a92c31SDominik Dingel return -EINVAL; 5125a3a92c31SDominik Dingel 5126f7784b8eSMarcelo Tosatti return 0; 5127f7784b8eSMarcelo Tosatti } 5128f7784b8eSMarcelo Tosatti 5129f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 51309d4c197cSSean Christopherson struct kvm_memory_slot *old, 5131f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 51328482644aSTakuya Yoshikawa enum kvm_mr_change change) 5133f7784b8eSMarcelo Tosatti { 513419ec166cSChristian Borntraeger int rc = 0; 5135f7784b8eSMarcelo Tosatti 513619ec166cSChristian Borntraeger switch (change) { 513719ec166cSChristian Borntraeger case KVM_MR_DELETE: 513819ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 513919ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 514019ec166cSChristian Borntraeger break; 514119ec166cSChristian Borntraeger case KVM_MR_MOVE: 514219ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 514319ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 514419ec166cSChristian Borntraeger if (rc) 514519ec166cSChristian Borntraeger break; 51463b684a42SJoe Perches fallthrough; 514719ec166cSChristian Borntraeger case KVM_MR_CREATE: 5148cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5149cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5150cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 515119ec166cSChristian Borntraeger break; 515219ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 515319ec166cSChristian Borntraeger break; 515419ec166cSChristian Borntraeger default: 515519ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 515619ec166cSChristian Borntraeger } 5157598841caSCarsten Otte if (rc) 5158ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5159598841caSCarsten Otte return; 5160b0c632dbSHeiko Carstens } 5161b0c632dbSHeiko Carstens 516260a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 516360a37709SAlexander Yarygin { 516460a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 516560a37709SAlexander Yarygin 516660a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 516760a37709SAlexander Yarygin } 516860a37709SAlexander Yarygin 5169b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5170b0c632dbSHeiko Carstens { 517160a37709SAlexander Yarygin int i; 517260a37709SAlexander Yarygin 517307197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 51748d43d570SMichael Mueller pr_info("SIE is not available\n"); 517507197fd0SDavid Hildenbrand return -ENODEV; 517607197fd0SDavid Hildenbrand } 517707197fd0SDavid Hildenbrand 5178a4499382SJanosch Frank if (nested && hpage) { 51798d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5180a4499382SJanosch Frank return -EINVAL; 5181a4499382SJanosch Frank } 5182a4499382SJanosch Frank 518360a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5184c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 518517e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 518660a37709SAlexander Yarygin 51879d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5188b0c632dbSHeiko Carstens } 5189b0c632dbSHeiko Carstens 5190b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5191b0c632dbSHeiko Carstens { 5192b0c632dbSHeiko Carstens kvm_exit(); 5193b0c632dbSHeiko Carstens } 5194b0c632dbSHeiko Carstens 5195b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5196b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5197566af940SCornelia Huck 5198566af940SCornelia Huck /* 5199566af940SCornelia Huck * Enable autoloading of the kvm module. 5200566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5201566af940SCornelia Huck * since x86 takes a different approach. 5202566af940SCornelia Huck */ 5203566af940SCornelia Huck #include <linux/miscdevice.h> 5204566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5205566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5206