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: 567d7b0b5ebSCarsten Otte r = 1; 568d7b0b5ebSCarsten Otte break; 569a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 570a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK; 571a43b80b7SMaxim Levitsky break; 572a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 573a4499382SJanosch Frank r = 0; 57440ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 575a4499382SJanosch Frank r = 1; 576a4499382SJanosch Frank break; 57741408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 57841408c28SThomas Huth r = MEM_OP_MAX_SIZE; 57941408c28SThomas Huth break; 580e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 581e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 582a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 58376a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 584a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 585a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 586a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 58776a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 58882cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS) 58982cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r); 590e726b1bdSChristian Borntraeger break; 5911526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 592abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 5931526bf9cSChristian Borntraeger break; 59468c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 59568c55750SEric Farman r = MACHINE_HAS_VX; 59668c55750SEric Farman break; 597c6e5f166SFan Zhang case KVM_CAP_S390_RI: 598c6e5f166SFan Zhang r = test_facility(64); 599c6e5f166SFan Zhang break; 6004e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 6014e0b1ab7SFan Zhang r = test_facility(133); 6024e0b1ab7SFan Zhang break; 60335b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 60435b3fde6SChristian Borntraeger r = test_facility(82); 60535b3fde6SChristian Borntraeger break; 60613da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED: 60713da9ae1SChristian Borntraeger r = is_prot_virt_host(); 60813da9ae1SChristian Borntraeger break; 6092bd0ac4eSCarsten Otte default: 610d7b0b5ebSCarsten Otte r = 0; 611b0c632dbSHeiko Carstens } 612d7b0b5ebSCarsten Otte return r; 6132bd0ac4eSCarsten Otte } 614b0c632dbSHeiko Carstens 6150dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 61615f36ebdSJason J. Herne { 6170959e168SJanosch Frank int i; 61815f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 6190959e168SJanosch Frank unsigned long gaddr, vmaddr; 62015f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 6210959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 62215f36ebdSJason J. Herne 6230959e168SJanosch Frank /* Loop over all guest segments */ 6240959e168SJanosch Frank cur_gfn = memslot->base_gfn; 62515f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 6260959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 6270959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 6280959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 6290959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 6300959e168SJanosch Frank continue; 63115f36ebdSJason J. Herne 6320959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6330959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6340959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6350959e168SJanosch Frank if (test_bit(i, bitmap)) 6360959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6370959e168SJanosch Frank } 6380959e168SJanosch Frank 6391763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6401763f8d0SChristian Borntraeger return; 64170c88a00SChristian Borntraeger cond_resched(); 64215f36ebdSJason J. Herne } 64315f36ebdSJason J. Herne } 64415f36ebdSJason J. Herne 645b0c632dbSHeiko Carstens /* Section: vm related */ 646a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 647a6e2f683SEugene (jno) Dvurechenski 648b0c632dbSHeiko Carstens /* 649b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 650b0c632dbSHeiko Carstens */ 651b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 652b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 653b0c632dbSHeiko Carstens { 65415f36ebdSJason J. Herne int r; 65515f36ebdSJason J. Herne unsigned long n; 65615f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 6572a49f61dSSean Christopherson int is_dirty; 65815f36ebdSJason J. Herne 659e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 660e1e8a962SJanosch Frank return -EINVAL; 661e1e8a962SJanosch Frank 66215f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 66315f36ebdSJason J. Herne 66415f36ebdSJason J. Herne r = -EINVAL; 66515f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 66615f36ebdSJason J. Herne goto out; 66715f36ebdSJason J. Herne 6682a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); 66915f36ebdSJason J. Herne if (r) 67015f36ebdSJason J. Herne goto out; 67115f36ebdSJason J. Herne 67215f36ebdSJason J. Herne /* Clear the dirty log */ 67315f36ebdSJason J. Herne if (is_dirty) { 67415f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 67515f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 67615f36ebdSJason J. Herne } 67715f36ebdSJason J. Herne r = 0; 67815f36ebdSJason J. Herne out: 67915f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 68015f36ebdSJason J. Herne return r; 681b0c632dbSHeiko Carstens } 682b0c632dbSHeiko Carstens 6836502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 6846502a34cSDavid Hildenbrand { 68546808a4cSMarc Zyngier unsigned long i; 6866502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 6876502a34cSDavid Hildenbrand 6886502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 6896502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 6906502a34cSDavid Hildenbrand } 6916502a34cSDavid Hildenbrand } 6926502a34cSDavid Hildenbrand 693e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 694d938dc55SCornelia Huck { 695d938dc55SCornelia Huck int r; 696d938dc55SCornelia Huck 697d938dc55SCornelia Huck if (cap->flags) 698d938dc55SCornelia Huck return -EINVAL; 699d938dc55SCornelia Huck 700d938dc55SCornelia Huck switch (cap->cap) { 70184223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 702c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 70384223598SCornelia Huck kvm->arch.use_irqchip = 1; 70484223598SCornelia Huck r = 0; 70584223598SCornelia Huck break; 7062444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 707c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 7082444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 7092444b352SDavid Hildenbrand r = 0; 7102444b352SDavid Hildenbrand break; 71168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 7125967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 713a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 7145967c17bSDavid Hildenbrand r = -EBUSY; 7155967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 716c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 717c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 7182f87d942SGuenther Hutzl if (test_facility(134)) { 7192f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 7202f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 7212f87d942SGuenther Hutzl } 72253743aa7SMaxim Samoylov if (test_facility(135)) { 72353743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 72453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 72553743aa7SMaxim Samoylov } 7267832e91cSChristian Borntraeger if (test_facility(148)) { 7277832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7287832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7297832e91cSChristian Borntraeger } 730d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 731d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 732d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 733d5cb6ab1SChristian Borntraeger } 7341f703d2cSChristian Borntraeger if (test_facility(192)) { 7351f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192); 7361f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192); 7371f703d2cSChristian Borntraeger } 73818280d8bSMichael Mueller r = 0; 73918280d8bSMichael Mueller } else 74018280d8bSMichael Mueller r = -EINVAL; 7415967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 742c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 743c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 74468c55750SEric Farman break; 745c6e5f166SFan Zhang case KVM_CAP_S390_RI: 746c6e5f166SFan Zhang r = -EINVAL; 747c6e5f166SFan Zhang mutex_lock(&kvm->lock); 748a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 749c6e5f166SFan Zhang r = -EBUSY; 750c6e5f166SFan Zhang } else if (test_facility(64)) { 751c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 752c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 753c6e5f166SFan Zhang r = 0; 754c6e5f166SFan Zhang } 755c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 756c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 757c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 758c6e5f166SFan Zhang break; 75947a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 76047a4693eSYi Min Zhao mutex_lock(&kvm->lock); 76147a4693eSYi Min Zhao if (kvm->created_vcpus) { 76247a4693eSYi Min Zhao r = -EBUSY; 76347a4693eSYi Min Zhao } else { 76447a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 76547a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 76647a4693eSYi Min Zhao r = 0; 76747a4693eSYi Min Zhao } 76847a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 76947a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 77047a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 77147a4693eSYi Min Zhao break; 7724e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 7734e0b1ab7SFan Zhang r = -EINVAL; 7744e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 775241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 7764e0b1ab7SFan Zhang r = -EBUSY; 7774e0b1ab7SFan Zhang } else if (test_facility(133)) { 7784e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 7794e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 7804e0b1ab7SFan Zhang r = 0; 7814e0b1ab7SFan Zhang } 7824e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 7834e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 7844e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 7854e0b1ab7SFan Zhang break; 786a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 787a4499382SJanosch Frank mutex_lock(&kvm->lock); 788a4499382SJanosch Frank if (kvm->created_vcpus) 789a4499382SJanosch Frank r = -EBUSY; 79040ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 791a4499382SJanosch Frank r = -EINVAL; 792a4499382SJanosch Frank else { 793a4499382SJanosch Frank r = 0; 794d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 795a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 796d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 797a4499382SJanosch Frank /* 798a4499382SJanosch Frank * We might have to create fake 4k page 799a4499382SJanosch Frank * tables. To avoid that the hardware works on 800a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 801a4499382SJanosch Frank */ 802a4499382SJanosch Frank kvm->arch.use_skf = 0; 803a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 804a4499382SJanosch Frank } 805a4499382SJanosch Frank mutex_unlock(&kvm->lock); 806a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 807a4499382SJanosch Frank r ? "(not available)" : "(success)"); 808a4499382SJanosch Frank break; 809e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 810c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 811e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 812e44fc8c9SEkaterina Tumanova r = 0; 813e44fc8c9SEkaterina Tumanova break; 8146502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 8156502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 8166502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 8176502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 8186502a34cSDavid Hildenbrand r = 0; 8196502a34cSDavid Hildenbrand break; 820d938dc55SCornelia Huck default: 821d938dc55SCornelia Huck r = -EINVAL; 822d938dc55SCornelia Huck break; 823d938dc55SCornelia Huck } 824d938dc55SCornelia Huck return r; 825d938dc55SCornelia Huck } 826d938dc55SCornelia Huck 8278c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8288c0a7ce6SDominik Dingel { 8298c0a7ce6SDominik Dingel int ret; 8308c0a7ce6SDominik Dingel 8318c0a7ce6SDominik Dingel switch (attr->attr) { 8328c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8338c0a7ce6SDominik Dingel ret = 0; 834c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 835a3a92c31SDominik Dingel kvm->arch.mem_limit); 836a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8378c0a7ce6SDominik Dingel ret = -EFAULT; 8388c0a7ce6SDominik Dingel break; 8398c0a7ce6SDominik Dingel default: 8408c0a7ce6SDominik Dingel ret = -ENXIO; 8418c0a7ce6SDominik Dingel break; 8428c0a7ce6SDominik Dingel } 8438c0a7ce6SDominik Dingel return ret; 8448c0a7ce6SDominik Dingel } 8458c0a7ce6SDominik Dingel 8468c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8474f718eabSDominik Dingel { 8484f718eabSDominik Dingel int ret; 8494f718eabSDominik Dingel unsigned int idx; 8504f718eabSDominik Dingel switch (attr->attr) { 8514f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 852f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 853c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 854e6db1d61SDominik Dingel break; 855e6db1d61SDominik Dingel 856c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 8574f718eabSDominik Dingel mutex_lock(&kvm->lock); 858a4499382SJanosch Frank if (kvm->created_vcpus) 859a4499382SJanosch Frank ret = -EBUSY; 860a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 861a4499382SJanosch Frank ret = -EINVAL; 862a4499382SJanosch Frank else { 8634f718eabSDominik Dingel kvm->arch.use_cmma = 1; 864c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 865c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 8664f718eabSDominik Dingel ret = 0; 8674f718eabSDominik Dingel } 8684f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8694f718eabSDominik Dingel break; 8704f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 871f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 872f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 873f9cbd9b0SDavid Hildenbrand break; 874c3489155SDominik Dingel ret = -EINVAL; 875c3489155SDominik Dingel if (!kvm->arch.use_cmma) 876c3489155SDominik Dingel break; 877c3489155SDominik Dingel 878c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 8794f718eabSDominik Dingel mutex_lock(&kvm->lock); 8804f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 881a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 8824f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 8834f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8844f718eabSDominik Dingel ret = 0; 8854f718eabSDominik Dingel break; 8868c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 8878c0a7ce6SDominik Dingel unsigned long new_limit; 8888c0a7ce6SDominik Dingel 8898c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 8908c0a7ce6SDominik Dingel return -EINVAL; 8918c0a7ce6SDominik Dingel 8928c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 8938c0a7ce6SDominik Dingel return -EFAULT; 8948c0a7ce6SDominik Dingel 895a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 896a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 8978c0a7ce6SDominik Dingel return -E2BIG; 8988c0a7ce6SDominik Dingel 899a3a92c31SDominik Dingel if (!new_limit) 900a3a92c31SDominik Dingel return -EINVAL; 901a3a92c31SDominik Dingel 9026ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 903a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 904a3a92c31SDominik Dingel new_limit -= 1; 905a3a92c31SDominik Dingel 9068c0a7ce6SDominik Dingel ret = -EBUSY; 9078c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 908a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 9096ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 9106ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 9118c0a7ce6SDominik Dingel 9128c0a7ce6SDominik Dingel if (!new) { 9138c0a7ce6SDominik Dingel ret = -ENOMEM; 9148c0a7ce6SDominik Dingel } else { 9156ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 9168c0a7ce6SDominik Dingel new->private = kvm; 9178c0a7ce6SDominik Dingel kvm->arch.gmap = new; 9188c0a7ce6SDominik Dingel ret = 0; 9198c0a7ce6SDominik Dingel } 9208c0a7ce6SDominik Dingel } 9218c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 922a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 923a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 924a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 9258c0a7ce6SDominik Dingel break; 9268c0a7ce6SDominik Dingel } 9274f718eabSDominik Dingel default: 9284f718eabSDominik Dingel ret = -ENXIO; 9294f718eabSDominik Dingel break; 9304f718eabSDominik Dingel } 9314f718eabSDominik Dingel return ret; 9324f718eabSDominik Dingel } 9334f718eabSDominik Dingel 934a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 935a374e892STony Krowiak 93620c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 937a374e892STony Krowiak { 938a374e892STony Krowiak struct kvm_vcpu *vcpu; 93946808a4cSMarc Zyngier unsigned long i; 940a374e892STony Krowiak 94120c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 94220c922f0STony Krowiak 9433194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 94420c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 9453194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 9463194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 9473194cdb7SDavid Hildenbrand } 94820c922f0STony Krowiak 94920c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 95020c922f0STony Krowiak } 95120c922f0STony Krowiak 95220c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 95320c922f0STony Krowiak { 954a374e892STony Krowiak mutex_lock(&kvm->lock); 955a374e892STony Krowiak switch (attr->attr) { 956a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 9578e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9588e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 95937940fb0STony Krowiak return -EINVAL; 9608e41bd54SChristian Borntraeger } 961a374e892STony Krowiak get_random_bytes( 962a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 963a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 964a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 965c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 966a374e892STony Krowiak break; 967a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 9688e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9698e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 97037940fb0STony Krowiak return -EINVAL; 9718e41bd54SChristian Borntraeger } 972a374e892STony Krowiak get_random_bytes( 973a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 974a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 975a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 976c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 977a374e892STony Krowiak break; 978a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 9798e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9808e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 98137940fb0STony Krowiak return -EINVAL; 9828e41bd54SChristian Borntraeger } 983a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 984a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 985a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 986c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 987a374e892STony Krowiak break; 988a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 9898e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9908e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 99137940fb0STony Krowiak return -EINVAL; 9928e41bd54SChristian Borntraeger } 993a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 994a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 995a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 996c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 997a374e892STony Krowiak break; 99837940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 99937940fb0STony Krowiak if (!ap_instructions_available()) { 100037940fb0STony Krowiak mutex_unlock(&kvm->lock); 100137940fb0STony Krowiak return -EOPNOTSUPP; 100237940fb0STony Krowiak } 100337940fb0STony Krowiak kvm->arch.crypto.apie = 1; 100437940fb0STony Krowiak break; 100537940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 100637940fb0STony Krowiak if (!ap_instructions_available()) { 100737940fb0STony Krowiak mutex_unlock(&kvm->lock); 100837940fb0STony Krowiak return -EOPNOTSUPP; 100937940fb0STony Krowiak } 101037940fb0STony Krowiak kvm->arch.crypto.apie = 0; 101137940fb0STony Krowiak break; 1012a374e892STony Krowiak default: 1013a374e892STony Krowiak mutex_unlock(&kvm->lock); 1014a374e892STony Krowiak return -ENXIO; 1015a374e892STony Krowiak } 1016a374e892STony Krowiak 101720c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 1018a374e892STony Krowiak mutex_unlock(&kvm->lock); 1019a374e892STony Krowiak return 0; 1020a374e892STony Krowiak } 1021a374e892STony Krowiak 1022190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 1023190df4a2SClaudio Imbrenda { 102446808a4cSMarc Zyngier unsigned long cx; 1025190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 1026190df4a2SClaudio Imbrenda 1027190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1028190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1029190df4a2SClaudio Imbrenda } 1030190df4a2SClaudio Imbrenda 1031190df4a2SClaudio Imbrenda /* 1032190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 10331de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1034190df4a2SClaudio Imbrenda */ 1035190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1036190df4a2SClaudio Imbrenda { 1037190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1038190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1039afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1040190df4a2SClaudio Imbrenda int slotnr; 1041190df4a2SClaudio Imbrenda 1042190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1043afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1044190df4a2SClaudio Imbrenda return 0; 1045190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1046190df4a2SClaudio Imbrenda if (!slots || !slots->used_slots) 1047190df4a2SClaudio Imbrenda return -EINVAL; 1048190df4a2SClaudio Imbrenda 1049afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1050afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1051afdad616SClaudio Imbrenda return 0; 1052190df4a2SClaudio Imbrenda } 1053190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1054190df4a2SClaudio Imbrenda for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { 1055190df4a2SClaudio Imbrenda ms = slots->memslots + slotnr; 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 19464036e387SClaudio Imbrenda /* 1947afdad616SClaudio Imbrenda * Similar to gfn_to_memslot, but returns the index of a memslot also when the 1948afdad616SClaudio Imbrenda * address falls in a hole. In that case the index of one of the memslots 1949afdad616SClaudio Imbrenda * bordering the hole is returned. 1950afdad616SClaudio Imbrenda */ 1951afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) 1952afdad616SClaudio Imbrenda { 1953afdad616SClaudio Imbrenda int start = 0, end = slots->used_slots; 195487689270SDavid Matlack int slot = atomic_read(&slots->last_used_slot); 1955afdad616SClaudio Imbrenda struct kvm_memory_slot *memslots = slots->memslots; 1956afdad616SClaudio Imbrenda 1957afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn && 1958afdad616SClaudio Imbrenda gfn < memslots[slot].base_gfn + memslots[slot].npages) 1959afdad616SClaudio Imbrenda return slot; 1960afdad616SClaudio Imbrenda 1961afdad616SClaudio Imbrenda while (start < end) { 1962afdad616SClaudio Imbrenda slot = start + (end - start) / 2; 1963afdad616SClaudio Imbrenda 1964afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn) 1965afdad616SClaudio Imbrenda end = slot; 1966afdad616SClaudio Imbrenda else 1967afdad616SClaudio Imbrenda start = slot + 1; 1968afdad616SClaudio Imbrenda } 1969afdad616SClaudio Imbrenda 197097daa028SSean Christopherson if (start >= slots->used_slots) 197197daa028SSean Christopherson return slots->used_slots - 1; 197297daa028SSean Christopherson 1973afdad616SClaudio Imbrenda if (gfn >= memslots[start].base_gfn && 1974afdad616SClaudio Imbrenda gfn < memslots[start].base_gfn + memslots[start].npages) { 197587689270SDavid Matlack atomic_set(&slots->last_used_slot, start); 1976afdad616SClaudio Imbrenda } 1977afdad616SClaudio Imbrenda 1978afdad616SClaudio Imbrenda return start; 1979afdad616SClaudio Imbrenda } 1980afdad616SClaudio Imbrenda 1981afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1982afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1983afdad616SClaudio Imbrenda { 1984afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 1985afdad616SClaudio Imbrenda 1986afdad616SClaudio Imbrenda args->count = 0; 1987afdad616SClaudio Imbrenda while (args->count < bufsize) { 1988afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1989afdad616SClaudio Imbrenda /* 1990afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 1991afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 1992afdad616SClaudio Imbrenda */ 1993afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1994afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 1995afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1996afdad616SClaudio Imbrenda pgstev = 0; 1997afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1998afdad616SClaudio Imbrenda cur_gfn++; 1999afdad616SClaudio Imbrenda } 2000afdad616SClaudio Imbrenda 2001afdad616SClaudio Imbrenda return 0; 2002afdad616SClaudio Imbrenda } 2003afdad616SClaudio Imbrenda 2004afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 2005afdad616SClaudio Imbrenda unsigned long cur_gfn) 2006afdad616SClaudio Imbrenda { 2007afdad616SClaudio Imbrenda int slotidx = gfn_to_memslot_approx(slots, cur_gfn); 2008afdad616SClaudio Imbrenda struct kvm_memory_slot *ms = slots->memslots + slotidx; 2009afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 2010afdad616SClaudio Imbrenda 2011afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 2012afdad616SClaudio Imbrenda slotidx--; 2013afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 2014afdad616SClaudio Imbrenda if (slotidx < 0) 2015afdad616SClaudio Imbrenda slotidx = slots->used_slots - 1; 2016afdad616SClaudio Imbrenda 2017afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 2018afdad616SClaudio Imbrenda ofs = 0; 2019afdad616SClaudio Imbrenda } 2020afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 2021afdad616SClaudio Imbrenda while ((slotidx > 0) && (ofs >= ms->npages)) { 2022afdad616SClaudio Imbrenda slotidx--; 2023afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 2024afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); 2025afdad616SClaudio Imbrenda } 2026afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 2027afdad616SClaudio Imbrenda } 2028afdad616SClaudio Imbrenda 2029afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2030afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2031afdad616SClaudio Imbrenda { 2032afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2033afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2034afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2035afdad616SClaudio Imbrenda 20360774a964SSean Christopherson if (unlikely(!slots->used_slots)) 20370774a964SSean Christopherson return 0; 20380774a964SSean Christopherson 2039afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2040afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2041afdad616SClaudio Imbrenda args->count = 0; 2042afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2043afdad616SClaudio Imbrenda if (!ms) 2044afdad616SClaudio Imbrenda return 0; 2045afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2046afdad616SClaudio Imbrenda mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; 2047afdad616SClaudio Imbrenda 2048afdad616SClaudio Imbrenda while (args->count < bufsize) { 2049afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2050afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2051afdad616SClaudio Imbrenda return 0; 2052afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2053afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2054afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2055afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2056afdad616SClaudio Imbrenda pgstev = 0; 2057afdad616SClaudio Imbrenda /* Save the value */ 2058afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2059afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2060afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2061afdad616SClaudio Imbrenda return 0; 2062afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2063afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2064afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2065afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2066afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2067afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2068afdad616SClaudio Imbrenda return 0; 2069afdad616SClaudio Imbrenda cur_gfn++; 2070afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2071afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2072afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2073afdad616SClaudio Imbrenda if (!ms) 2074afdad616SClaudio Imbrenda return 0; 2075afdad616SClaudio Imbrenda } 2076afdad616SClaudio Imbrenda } 2077afdad616SClaudio Imbrenda return 0; 2078afdad616SClaudio Imbrenda } 2079afdad616SClaudio Imbrenda 2080afdad616SClaudio Imbrenda /* 20814036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 20824036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 20834036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 20844036e387SClaudio Imbrenda * no trailing clean bytes are saved. 20854036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 20864036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 20874036e387SClaudio Imbrenda */ 20884036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 20894036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 20904036e387SClaudio Imbrenda { 2091afdad616SClaudio Imbrenda unsigned long bufsize; 2092afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2093afdad616SClaudio Imbrenda u8 *values; 20944036e387SClaudio Imbrenda 2095afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 20964036e387SClaudio Imbrenda return -ENXIO; 20974036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 20984036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 20994036e387SClaudio Imbrenda return -EINVAL; 21004036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 21014036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2102afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 21034036e387SClaudio Imbrenda return -EINVAL; 21044036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 21054036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2106c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 21074036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 21084036e387SClaudio Imbrenda return 0; 21094036e387SClaudio Imbrenda } 21104036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2111afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 21124036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 21134036e387SClaudio Imbrenda return 0; 21144036e387SClaudio Imbrenda } 21154036e387SClaudio Imbrenda 2116afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2117afdad616SClaudio Imbrenda if (!values) 21184036e387SClaudio Imbrenda return -ENOMEM; 21194036e387SClaudio Imbrenda 2120d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 21214036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2122afdad616SClaudio Imbrenda if (peek) 2123afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2124afdad616SClaudio Imbrenda else 2125afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 21264036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2127d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 21284036e387SClaudio Imbrenda 2129afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2130afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2131afdad616SClaudio Imbrenda else 2132afdad616SClaudio Imbrenda args->remaining = 0; 21334036e387SClaudio Imbrenda 2134afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2135afdad616SClaudio Imbrenda ret = -EFAULT; 2136afdad616SClaudio Imbrenda 2137afdad616SClaudio Imbrenda vfree(values); 2138afdad616SClaudio Imbrenda return ret; 21394036e387SClaudio Imbrenda } 21404036e387SClaudio Imbrenda 21414036e387SClaudio Imbrenda /* 21424036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 21434036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2144c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 21454036e387SClaudio Imbrenda */ 21464036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 21474036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 21484036e387SClaudio Imbrenda { 21494036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 21504036e387SClaudio Imbrenda uint8_t *bits; 21514036e387SClaudio Imbrenda int srcu_idx, r = 0; 21524036e387SClaudio Imbrenda 21534036e387SClaudio Imbrenda mask = args->mask; 21544036e387SClaudio Imbrenda 21554036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 21564036e387SClaudio Imbrenda return -ENXIO; 21574036e387SClaudio Imbrenda /* invalid/unsupported flags */ 21584036e387SClaudio Imbrenda if (args->flags != 0) 21594036e387SClaudio Imbrenda return -EINVAL; 21604036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 21614036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 21624036e387SClaudio Imbrenda return -EINVAL; 21634036e387SClaudio Imbrenda /* Nothing to do */ 21644036e387SClaudio Imbrenda if (args->count == 0) 21654036e387SClaudio Imbrenda return 0; 21664036e387SClaudio Imbrenda 216742bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 21684036e387SClaudio Imbrenda if (!bits) 21694036e387SClaudio Imbrenda return -ENOMEM; 21704036e387SClaudio Imbrenda 21714036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 21724036e387SClaudio Imbrenda if (r) { 21734036e387SClaudio Imbrenda r = -EFAULT; 21744036e387SClaudio Imbrenda goto out; 21754036e387SClaudio Imbrenda } 21764036e387SClaudio Imbrenda 2177d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 21784036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 21794036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 21804036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 21814036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 21824036e387SClaudio Imbrenda r = -EFAULT; 21834036e387SClaudio Imbrenda break; 21844036e387SClaudio Imbrenda } 21854036e387SClaudio Imbrenda 21864036e387SClaudio Imbrenda pgstev = bits[i]; 21874036e387SClaudio Imbrenda pgstev = pgstev << 24; 21881bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 21894036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 21904036e387SClaudio Imbrenda } 21914036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2192d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 21934036e387SClaudio Imbrenda 2194c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 2195d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 2196c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 2197d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 21984036e387SClaudio Imbrenda } 21994036e387SClaudio Imbrenda out: 22004036e387SClaudio Imbrenda vfree(bits); 22014036e387SClaudio Imbrenda return r; 22024036e387SClaudio Imbrenda } 22034036e387SClaudio Imbrenda 220429b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp) 220529b40f10SJanosch Frank { 220629b40f10SJanosch Frank struct kvm_vcpu *vcpu; 220729b40f10SJanosch Frank u16 rc, rrc; 220829b40f10SJanosch Frank int ret = 0; 220946808a4cSMarc Zyngier unsigned long i; 221029b40f10SJanosch Frank 221129b40f10SJanosch Frank /* 221229b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 221329b40f10SJanosch Frank * At the same time we must not free the assigned resources when 221429b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 221529b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 221629b40f10SJanosch Frank * behind. 221729b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 221829b40f10SJanosch Frank */ 221929b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 222029b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 222129b40f10SJanosch Frank if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) { 222229b40f10SJanosch Frank *rcp = rc; 222329b40f10SJanosch Frank *rrcp = rrc; 222429b40f10SJanosch Frank ret = -EIO; 222529b40f10SJanosch Frank } 222629b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 222729b40f10SJanosch Frank } 222829b40f10SJanosch Frank return ret; 222929b40f10SJanosch Frank } 223029b40f10SJanosch Frank 223129b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 223229b40f10SJanosch Frank { 223346808a4cSMarc Zyngier unsigned long i; 223446808a4cSMarc Zyngier int r = 0; 223529b40f10SJanosch Frank u16 dummy; 223629b40f10SJanosch Frank 223729b40f10SJanosch Frank struct kvm_vcpu *vcpu; 223829b40f10SJanosch Frank 223929b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 224029b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 224129b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 224229b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 224329b40f10SJanosch Frank if (r) 224429b40f10SJanosch Frank break; 224529b40f10SJanosch Frank } 224629b40f10SJanosch Frank if (r) 224729b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 224829b40f10SJanosch Frank return r; 224929b40f10SJanosch Frank } 225029b40f10SJanosch Frank 225129b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 225229b40f10SJanosch Frank { 225329b40f10SJanosch Frank int r = 0; 225429b40f10SJanosch Frank u16 dummy; 225529b40f10SJanosch Frank void __user *argp = (void __user *)cmd->data; 225629b40f10SJanosch Frank 225729b40f10SJanosch Frank switch (cmd->cmd) { 225829b40f10SJanosch Frank case KVM_PV_ENABLE: { 225929b40f10SJanosch Frank r = -EINVAL; 226029b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 226129b40f10SJanosch Frank break; 226229b40f10SJanosch Frank 226329b40f10SJanosch Frank /* 226429b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 226529b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 226629b40f10SJanosch Frank */ 226729b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 226829b40f10SJanosch Frank if (r) 226929b40f10SJanosch Frank break; 227029b40f10SJanosch Frank 2271d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 2272fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2273d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 2274fa0c5eabSJanosch Frank if (r) 2275fa0c5eabSJanosch Frank break; 2276fa0c5eabSJanosch Frank 227729b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 227829b40f10SJanosch Frank if (r) 227929b40f10SJanosch Frank break; 228029b40f10SJanosch Frank 228129b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 228229b40f10SJanosch Frank if (r) 228329b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 22840890ddeaSChristian Borntraeger 22850890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 22860890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 228729b40f10SJanosch Frank break; 228829b40f10SJanosch Frank } 228929b40f10SJanosch Frank case KVM_PV_DISABLE: { 229029b40f10SJanosch Frank r = -EINVAL; 229129b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 229229b40f10SJanosch Frank break; 229329b40f10SJanosch Frank 229429b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 229529b40f10SJanosch Frank /* 229629b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 229729b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 229829b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 229929b40f10SJanosch Frank */ 230029b40f10SJanosch Frank if (r) 230129b40f10SJanosch Frank break; 230229b40f10SJanosch Frank r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc); 23030890ddeaSChristian Borntraeger 23040890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 23050890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 230629b40f10SJanosch Frank break; 230729b40f10SJanosch Frank } 230829b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 230929b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 231029b40f10SJanosch Frank void *hdr; 231129b40f10SJanosch Frank 231229b40f10SJanosch Frank r = -EINVAL; 231329b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 231429b40f10SJanosch Frank break; 231529b40f10SJanosch Frank 231629b40f10SJanosch Frank r = -EFAULT; 231729b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 231829b40f10SJanosch Frank break; 231929b40f10SJanosch Frank 232029b40f10SJanosch Frank /* Currently restricted to 8KB */ 232129b40f10SJanosch Frank r = -EINVAL; 232229b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 232329b40f10SJanosch Frank break; 232429b40f10SJanosch Frank 232529b40f10SJanosch Frank r = -ENOMEM; 232629b40f10SJanosch Frank hdr = vmalloc(parms.length); 232729b40f10SJanosch Frank if (!hdr) 232829b40f10SJanosch Frank break; 232929b40f10SJanosch Frank 233029b40f10SJanosch Frank r = -EFAULT; 233129b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 233229b40f10SJanosch Frank parms.length)) 233329b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 233429b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 233529b40f10SJanosch Frank 233629b40f10SJanosch Frank vfree(hdr); 233729b40f10SJanosch Frank break; 233829b40f10SJanosch Frank } 233929b40f10SJanosch Frank case KVM_PV_UNPACK: { 234029b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 234129b40f10SJanosch Frank 234229b40f10SJanosch Frank r = -EINVAL; 23431ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) 234429b40f10SJanosch Frank break; 234529b40f10SJanosch Frank 234629b40f10SJanosch Frank r = -EFAULT; 234729b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 234829b40f10SJanosch Frank break; 234929b40f10SJanosch Frank 235029b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 235129b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 235229b40f10SJanosch Frank break; 235329b40f10SJanosch Frank } 235429b40f10SJanosch Frank case KVM_PV_VERIFY: { 235529b40f10SJanosch Frank r = -EINVAL; 235629b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 235729b40f10SJanosch Frank break; 235829b40f10SJanosch Frank 235929b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 236029b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 236129b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 236229b40f10SJanosch Frank cmd->rrc); 236329b40f10SJanosch Frank break; 236429b40f10SJanosch Frank } 2365e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2366e0d2773dSJanosch Frank r = -EINVAL; 2367e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2368e0d2773dSJanosch Frank break; 2369e0d2773dSJanosch Frank 2370e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2371e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2372e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2373e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2374e0d2773dSJanosch Frank break; 2375e0d2773dSJanosch Frank } 2376e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2377e0d2773dSJanosch Frank r = -EINVAL; 2378e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2379e0d2773dSJanosch Frank break; 2380e0d2773dSJanosch Frank 2381e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2382e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2383e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2384e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2385e0d2773dSJanosch Frank break; 2386e0d2773dSJanosch Frank } 238729b40f10SJanosch Frank default: 238829b40f10SJanosch Frank r = -ENOTTY; 238929b40f10SJanosch Frank } 239029b40f10SJanosch Frank return r; 239129b40f10SJanosch Frank } 239229b40f10SJanosch Frank 2393b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2394b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2395b0c632dbSHeiko Carstens { 2396b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2397b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2398f2061656SDominik Dingel struct kvm_device_attr attr; 2399b0c632dbSHeiko Carstens int r; 2400b0c632dbSHeiko Carstens 2401b0c632dbSHeiko Carstens switch (ioctl) { 2402ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2403ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2404ba5c1e9bSCarsten Otte 2405ba5c1e9bSCarsten Otte r = -EFAULT; 2406ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2407ba5c1e9bSCarsten Otte break; 2408ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2409ba5c1e9bSCarsten Otte break; 2410ba5c1e9bSCarsten Otte } 241184223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 241284223598SCornelia Huck struct kvm_irq_routing_entry routing; 241384223598SCornelia Huck 241484223598SCornelia Huck r = -EINVAL; 241584223598SCornelia Huck if (kvm->arch.use_irqchip) { 241684223598SCornelia Huck /* Set up dummy routing. */ 241784223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2418152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 241984223598SCornelia Huck } 242084223598SCornelia Huck break; 242184223598SCornelia Huck } 2422f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2423f2061656SDominik Dingel r = -EFAULT; 2424f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2425f2061656SDominik Dingel break; 2426f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2427f2061656SDominik Dingel break; 2428f2061656SDominik Dingel } 2429f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2430f2061656SDominik Dingel r = -EFAULT; 2431f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2432f2061656SDominik Dingel break; 2433f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2434f2061656SDominik Dingel break; 2435f2061656SDominik Dingel } 2436f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2437f2061656SDominik Dingel r = -EFAULT; 2438f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2439f2061656SDominik Dingel break; 2440f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2441f2061656SDominik Dingel break; 2442f2061656SDominik Dingel } 244330ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 244430ee2a98SJason J. Herne struct kvm_s390_skeys args; 244530ee2a98SJason J. Herne 244630ee2a98SJason J. Herne r = -EFAULT; 244730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 244830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 244930ee2a98SJason J. Herne break; 245030ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 245130ee2a98SJason J. Herne break; 245230ee2a98SJason J. Herne } 245330ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 245430ee2a98SJason J. Herne struct kvm_s390_skeys args; 245530ee2a98SJason J. Herne 245630ee2a98SJason J. Herne r = -EFAULT; 245730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 245830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 245930ee2a98SJason J. Herne break; 246030ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 246130ee2a98SJason J. Herne break; 246230ee2a98SJason J. Herne } 24634036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 24644036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24654036e387SClaudio Imbrenda 24664036e387SClaudio Imbrenda r = -EFAULT; 24674036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24684036e387SClaudio Imbrenda break; 24691de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24704036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 24711de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24724036e387SClaudio Imbrenda if (!r) { 24734036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 24744036e387SClaudio Imbrenda if (r) 24754036e387SClaudio Imbrenda r = -EFAULT; 24764036e387SClaudio Imbrenda } 24774036e387SClaudio Imbrenda break; 24784036e387SClaudio Imbrenda } 24794036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 24804036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24814036e387SClaudio Imbrenda 24824036e387SClaudio Imbrenda r = -EFAULT; 24834036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24844036e387SClaudio Imbrenda break; 24851de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24864036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 24871de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24884036e387SClaudio Imbrenda break; 24894036e387SClaudio Imbrenda } 249029b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 249129b40f10SJanosch Frank struct kvm_pv_cmd args; 249229b40f10SJanosch Frank 249367cf68b6SEric Farman /* protvirt means user cpu state */ 249467cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 249529b40f10SJanosch Frank r = 0; 249629b40f10SJanosch Frank if (!is_prot_virt_host()) { 249729b40f10SJanosch Frank r = -EINVAL; 249829b40f10SJanosch Frank break; 249929b40f10SJanosch Frank } 250029b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 250129b40f10SJanosch Frank r = -EFAULT; 250229b40f10SJanosch Frank break; 250329b40f10SJanosch Frank } 250429b40f10SJanosch Frank if (args.flags) { 250529b40f10SJanosch Frank r = -EINVAL; 250629b40f10SJanosch Frank break; 250729b40f10SJanosch Frank } 250829b40f10SJanosch Frank mutex_lock(&kvm->lock); 250929b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 251029b40f10SJanosch Frank mutex_unlock(&kvm->lock); 251129b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 251229b40f10SJanosch Frank r = -EFAULT; 251329b40f10SJanosch Frank break; 251429b40f10SJanosch Frank } 251529b40f10SJanosch Frank break; 251629b40f10SJanosch Frank } 2517b0c632dbSHeiko Carstens default: 2518367e1319SAvi Kivity r = -ENOTTY; 2519b0c632dbSHeiko Carstens } 2520b0c632dbSHeiko Carstens 2521b0c632dbSHeiko Carstens return r; 2522b0c632dbSHeiko Carstens } 2523b0c632dbSHeiko Carstens 252445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 252545c9b47cSTony Krowiak { 2526e585b24aSTony Krowiak struct ap_config_info info; 252745c9b47cSTony Krowiak 2528e585b24aSTony Krowiak if (ap_instructions_available()) { 2529e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2530e585b24aSTony Krowiak return info.apxa; 253145c9b47cSTony Krowiak } 253245c9b47cSTony Krowiak 253345c9b47cSTony Krowiak return 0; 253445c9b47cSTony Krowiak } 253545c9b47cSTony Krowiak 2536e585b24aSTony Krowiak /* 2537e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2538e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2539e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2540e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2541e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2542e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2543e585b24aSTony Krowiak */ 254445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 254545c9b47cSTony Krowiak { 254645c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 254745c9b47cSTony Krowiak 2548e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2549e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2550e585b24aSTony Krowiak 2551e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2552e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2553e585b24aSTony Krowiak return; 2554e585b24aSTony Krowiak 255545c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 255645c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 255745c9b47cSTony Krowiak else 255845c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 255945c9b47cSTony Krowiak } 256045c9b47cSTony Krowiak 256186956e70STony Krowiak /* 256286956e70STony Krowiak * kvm_arch_crypto_set_masks 256386956e70STony Krowiak * 256486956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 256586956e70STony Krowiak * to be set. 256686956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 256786956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 256886956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 256986956e70STony Krowiak * 257086956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 257186956e70STony Krowiak * which the KVM guest is granted access. 257286956e70STony Krowiak * 257386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 257486956e70STony Krowiak * function. 257586956e70STony Krowiak */ 25760e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 25770e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 25780e237e44SPierre Morel { 25790e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 25800e237e44SPierre Morel 25810e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 25820e237e44SPierre Morel 25830e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 25840e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 25850e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 25860e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 25870e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 25880e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 25890e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 25900e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 25910e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 25920e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 25930e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 25940e237e44SPierre Morel break; 25950e237e44SPierre Morel case CRYCB_FORMAT1: 25960e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 25970e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 25980e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 25990e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 26000e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 26010e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 26020e237e44SPierre Morel *((unsigned short *)adm)); 26030e237e44SPierre Morel break; 26040e237e44SPierre Morel default: /* Can not happen */ 26050e237e44SPierre Morel break; 26060e237e44SPierre Morel } 26070e237e44SPierre Morel 26080e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 26090e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 26100e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 26110e237e44SPierre Morel } 26120e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 26130e237e44SPierre Morel 261486956e70STony Krowiak /* 261586956e70STony Krowiak * kvm_arch_crypto_clear_masks 261686956e70STony Krowiak * 261786956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 261886956e70STony Krowiak * to be cleared. 261986956e70STony Krowiak * 262086956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 262186956e70STony Krowiak * which the KVM guest is granted access. 262286956e70STony Krowiak * 262386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 262486956e70STony Krowiak * function. 262586956e70STony Krowiak */ 262642104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 262742104598STony Krowiak { 262842104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 262942104598STony Krowiak 263042104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 263142104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 263242104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 263342104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 263442104598STony Krowiak 26350e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 26366cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 26376cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 263842104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 263942104598STony Krowiak } 264042104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 264142104598STony Krowiak 26429bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 26439d8d5786SMichael Mueller { 26449bb0ec09SDavid Hildenbrand struct cpuid cpuid; 26459bb0ec09SDavid Hildenbrand 26469bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 26479bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 26489bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 26499d8d5786SMichael Mueller } 26509d8d5786SMichael Mueller 2651c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 26525102ee87STony Krowiak { 2653c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 265445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 26551e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 26565102ee87STony Krowiak 2657e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2658e585b24aSTony Krowiak return; 2659e585b24aSTony Krowiak 2660ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2661ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2662ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2663ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2664ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2665ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2666ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 26675102ee87STony Krowiak } 26685102ee87STony Krowiak 26697d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 26707d43bafcSEugene (jno) Dvurechenski { 26717d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 26725e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 26737d43bafcSEugene (jno) Dvurechenski else 26747d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 26757d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 26767d43bafcSEugene (jno) Dvurechenski } 26777d43bafcSEugene (jno) Dvurechenski 2678e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2679b0c632dbSHeiko Carstens { 2680c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 26819d8d5786SMichael Mueller int i, rc; 2682b0c632dbSHeiko Carstens char debug_name[16]; 2683f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2684b0c632dbSHeiko Carstens 2685e08b9637SCarsten Otte rc = -EINVAL; 2686e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2687e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2688e08b9637SCarsten Otte goto out_err; 2689e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2690e08b9637SCarsten Otte goto out_err; 2691e08b9637SCarsten Otte #else 2692e08b9637SCarsten Otte if (type) 2693e08b9637SCarsten Otte goto out_err; 2694e08b9637SCarsten Otte #endif 2695e08b9637SCarsten Otte 2696b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2697b0c632dbSHeiko Carstens if (rc) 2698d89f5effSJan Kiszka goto out_err; 2699b0c632dbSHeiko Carstens 2700b290411aSCarsten Otte rc = -ENOMEM; 2701b290411aSCarsten Otte 270276a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 270376a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 27045e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 27059ac96d75SDavid Hildenbrand /* start with basic SCA */ 270676a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2707b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2708d89f5effSJan Kiszka goto out_err; 27090d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 2710c5c2c393SDavid Hildenbrand sca_offset += 16; 2711bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2712c5c2c393SDavid Hildenbrand sca_offset = 0; 2713bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2714bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 27150d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 2716b0c632dbSHeiko Carstens 2717b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2718b0c632dbSHeiko Carstens 27191cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2720b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 272140f5b735SDominik Dingel goto out_err; 2722b0c632dbSHeiko Carstens 272319114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2724c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2725c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 2726c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 272740f5b735SDominik Dingel goto out_err; 27289d8d5786SMichael Mueller 272925c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2730c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2731c3b9e3e1SChristian Borntraeger 2732c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 273317e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 2734c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2735c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 273617e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 2737c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2738c3b9e3e1SChristian Borntraeger } 2739346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2740981467c9SMichael Mueller 27411935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 27421935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 27431935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 27441935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 274595ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 274695ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 27471bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 27481bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 27491bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 27501bab1c02SClaudio Imbrenda } 275195ca2cb5SJanosch Frank 275205f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 275305f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 275405f31e3bSPierre Morel 27559bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 275637c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 27579d8d5786SMichael Mueller 2758c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 27595102ee87STony Krowiak 276051978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2761ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 27626d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 27636d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 27648a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2765a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2766ba5c1e9bSCarsten Otte 2767b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 276878f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2769b0c632dbSHeiko Carstens 2770e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2771e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2772a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2773e08b9637SCarsten Otte } else { 277432e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2775ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 277632e6b236SGuenther Hutzl else 2777ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 277832e6b236SGuenther Hutzl sclp.hamax + 1); 27796ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2780598841caSCarsten Otte if (!kvm->arch.gmap) 278140f5b735SDominik Dingel goto out_err; 27822c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 278324eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2784e08b9637SCarsten Otte } 2785fa6b7fe9SCornelia Huck 2786c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 278755531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 27888ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2789a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2790cc674ef2SMichael Mueller if (use_gisa) 2791d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 27928335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 27938ad35755SDavid Hildenbrand 2794d89f5effSJan Kiszka return 0; 2795d89f5effSJan Kiszka out_err: 2796c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 279740f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 27987d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 279978f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2800d89f5effSJan Kiszka return rc; 2801b0c632dbSHeiko Carstens } 2802b0c632dbSHeiko Carstens 2803d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2804d329c035SChristian Borntraeger { 280529b40f10SJanosch Frank u16 rc, rrc; 280629b40f10SJanosch Frank 2807d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2808ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 280967335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 28103c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2811bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2812a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 281327e0393fSCarsten Otte 281427e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 28156ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 281627e0393fSCarsten Otte 2817e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2818b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 281929b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 282029b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 282129b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 2822d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2823d329c035SChristian Borntraeger } 2824d329c035SChristian Borntraeger 2825b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2826b0c632dbSHeiko Carstens { 282729b40f10SJanosch Frank u16 rc, rrc; 282829b40f10SJanosch Frank 282927592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 28307d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2831d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 283229b40f10SJanosch Frank /* 283329b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 283429b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 283529b40f10SJanosch Frank * can mess with the pv state. To avoid lockdep_assert_held from 283629b40f10SJanosch Frank * complaining we do not use kvm_s390_pv_is_protected. 283729b40f10SJanosch Frank */ 283829b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 283929b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); 284029b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 2841c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 284227e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 28436ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2844841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 284567335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2846a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 28478335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2848b0c632dbSHeiko Carstens } 2849b0c632dbSHeiko Carstens 2850b0c632dbSHeiko Carstens /* Section: vcpu related */ 2851dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2852b0c632dbSHeiko Carstens { 28536ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 285427e0393fSCarsten Otte if (!vcpu->arch.gmap) 285527e0393fSCarsten Otte return -ENOMEM; 28562c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2857dafd032aSDominik Dingel 285827e0393fSCarsten Otte return 0; 285927e0393fSCarsten Otte } 286027e0393fSCarsten Otte 2861a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2862a6e2f683SEugene (jno) Dvurechenski { 2863a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2864a6940674SDavid Hildenbrand return; 28655e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 28667d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 28677d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 28687d43bafcSEugene (jno) Dvurechenski 28697d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28707d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 28717d43bafcSEugene (jno) Dvurechenski } else { 2872bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2873a6e2f683SEugene (jno) Dvurechenski 2874a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2875a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2876a6e2f683SEugene (jno) Dvurechenski } 28775e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 28787d43bafcSEugene (jno) Dvurechenski } 2879a6e2f683SEugene (jno) Dvurechenski 2880eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2881a6e2f683SEugene (jno) Dvurechenski { 2882a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2883a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2884a6940674SDavid Hildenbrand 2885a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2886a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2887a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2888f07afa04SDavid Hildenbrand return; 2889a6940674SDavid Hildenbrand } 2890eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2891eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2892eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 28937d43bafcSEugene (jno) Dvurechenski 2894eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 28957d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 28967d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 28970c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2898eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28997d43bafcSEugene (jno) Dvurechenski } else { 2900eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2901a6e2f683SEugene (jno) Dvurechenski 2902eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2903a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2904a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2905eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2906a6e2f683SEugene (jno) Dvurechenski } 2907eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 29085e044315SEugene (jno) Dvurechenski } 29095e044315SEugene (jno) Dvurechenski 29105e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 29115e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 29125e044315SEugene (jno) Dvurechenski { 29135e044315SEugene (jno) Dvurechenski d->sda = s->sda; 29145e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 29155e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 29165e044315SEugene (jno) Dvurechenski } 29175e044315SEugene (jno) Dvurechenski 29185e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 29195e044315SEugene (jno) Dvurechenski { 29205e044315SEugene (jno) Dvurechenski int i; 29215e044315SEugene (jno) Dvurechenski 29225e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 29235e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 29245e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 29255e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 29265e044315SEugene (jno) Dvurechenski } 29275e044315SEugene (jno) Dvurechenski 29285e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 29295e044315SEugene (jno) Dvurechenski { 29305e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 29315e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 29325e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 293346808a4cSMarc Zyngier unsigned long vcpu_idx; 29345e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 29355e044315SEugene (jno) Dvurechenski 293629b40f10SJanosch Frank if (kvm->arch.use_esca) 293729b40f10SJanosch Frank return 0; 293829b40f10SJanosch Frank 2939c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 29405e044315SEugene (jno) Dvurechenski if (!new_sca) 29415e044315SEugene (jno) Dvurechenski return -ENOMEM; 29425e044315SEugene (jno) Dvurechenski 29435e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 29445e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 29455e044315SEugene (jno) Dvurechenski 29465e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 29475e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 29485e044315SEugene (jno) Dvurechenski 29495e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 29505e044315SEugene (jno) Dvurechenski 29515e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 29525e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 29535e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 29540c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 29555e044315SEugene (jno) Dvurechenski } 29565e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 29575e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 29585e044315SEugene (jno) Dvurechenski 29595e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 29605e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 29615e044315SEugene (jno) Dvurechenski 29625e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 29635e044315SEugene (jno) Dvurechenski 29648335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 29658335713aSChristian Borntraeger old_sca, kvm->arch.sca); 29665e044315SEugene (jno) Dvurechenski return 0; 29677d43bafcSEugene (jno) Dvurechenski } 2968a6e2f683SEugene (jno) Dvurechenski 2969a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2970a6e2f683SEugene (jno) Dvurechenski { 29715e044315SEugene (jno) Dvurechenski int rc; 29725e044315SEugene (jno) Dvurechenski 2973a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2974a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2975a6940674SDavid Hildenbrand return true; 2976a6940674SDavid Hildenbrand return false; 2977a6940674SDavid Hildenbrand } 29785e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 29795e044315SEugene (jno) Dvurechenski return true; 298076a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 29815e044315SEugene (jno) Dvurechenski return false; 29825e044315SEugene (jno) Dvurechenski 29835e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 29845e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 29855e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 29865e044315SEugene (jno) Dvurechenski 29875e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2988a6e2f683SEugene (jno) Dvurechenski } 2989a6e2f683SEugene (jno) Dvurechenski 2990db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2991db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2992db0758b2SDavid Hildenbrand { 2993db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 29949c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2995db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 29969c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2997db0758b2SDavid Hildenbrand } 2998db0758b2SDavid Hildenbrand 2999db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3000db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3001db0758b2SDavid Hildenbrand { 3002db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 30039c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3004db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3005db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 30069c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3007db0758b2SDavid Hildenbrand } 3008db0758b2SDavid Hildenbrand 3009db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3010db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3011db0758b2SDavid Hildenbrand { 3012db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3013db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3014db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3015db0758b2SDavid Hildenbrand } 3016db0758b2SDavid Hildenbrand 3017db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3018db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3019db0758b2SDavid Hildenbrand { 3020db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3021db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3022db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3023db0758b2SDavid Hildenbrand } 3024db0758b2SDavid Hildenbrand 3025db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3026db0758b2SDavid Hildenbrand { 3027db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3028db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3029db0758b2SDavid Hildenbrand preempt_enable(); 3030db0758b2SDavid Hildenbrand } 3031db0758b2SDavid Hildenbrand 3032db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3033db0758b2SDavid Hildenbrand { 3034db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3035db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3036db0758b2SDavid Hildenbrand preempt_enable(); 3037db0758b2SDavid Hildenbrand } 3038db0758b2SDavid Hildenbrand 30394287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 30404287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 30414287f247SDavid Hildenbrand { 3042db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 30439c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3044db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3045db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 30464287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 30479c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3048db0758b2SDavid Hildenbrand preempt_enable(); 30494287f247SDavid Hildenbrand } 30504287f247SDavid Hildenbrand 3051db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 30524287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 30534287f247SDavid Hildenbrand { 30549c23a131SDavid Hildenbrand unsigned int seq; 3055db0758b2SDavid Hildenbrand __u64 value; 3056db0758b2SDavid Hildenbrand 3057db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 30584287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3059db0758b2SDavid Hildenbrand 30609c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 30619c23a131SDavid Hildenbrand do { 30629c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 30639c23a131SDavid Hildenbrand /* 30649c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 30659c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 30669c23a131SDavid Hildenbrand */ 30679c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3068db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 30699c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 30709c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3071db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 30729c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 30739c23a131SDavid Hildenbrand preempt_enable(); 3074db0758b2SDavid Hildenbrand return value; 30754287f247SDavid Hildenbrand } 30764287f247SDavid Hildenbrand 3077b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3078b0c632dbSHeiko Carstens { 30799977e886SHendrik Brueckner 308037d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3081ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 30825ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3083db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 308401a745acSDavid Hildenbrand vcpu->cpu = cpu; 3085b0c632dbSHeiko Carstens } 3086b0c632dbSHeiko Carstens 3087b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3088b0c632dbSHeiko Carstens { 308901a745acSDavid Hildenbrand vcpu->cpu = -1; 30905ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3091db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 30929daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 309337d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 309437d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 30959977e886SHendrik Brueckner 3096b0c632dbSHeiko Carstens } 3097b0c632dbSHeiko Carstens 309831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 309942897d86SMarcelo Tosatti { 310072f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3101fdf03650SFan Zhang preempt_disable(); 310272f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3103d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3104fdf03650SFan Zhang preempt_enable(); 310572f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 310625508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3107dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3108eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 310925508824SDavid Hildenbrand } 31106502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 31116502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 311237d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 311337d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 311442897d86SMarcelo Tosatti } 311542897d86SMarcelo Tosatti 31168ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 31178ec2fa52SChristian Borntraeger { 31188ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 31198ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 31208ec2fa52SChristian Borntraeger return true; 31218ec2fa52SChristian Borntraeger return false; 31228ec2fa52SChristian Borntraeger } 31238ec2fa52SChristian Borntraeger 31248ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 31258ec2fa52SChristian Borntraeger { 31268ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 31278ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 31288ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 31298ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 31308ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 31318ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 31328ec2fa52SChristian Borntraeger 31338ec2fa52SChristian Borntraeger } 31348ec2fa52SChristian Borntraeger 31355102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 31365102ee87STony Krowiak { 3137e585b24aSTony Krowiak /* 3138e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3139e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3140e585b24aSTony Krowiak */ 3141e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 31425102ee87STony Krowiak return; 31435102ee87STony Krowiak 3144e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3145a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 314637940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 31478ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3148a374e892STony Krowiak 3149e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3150e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3151e585b24aSTony Krowiak 3152e585b24aSTony Krowiak /* Set up protected key support */ 31538ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3154a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 31558ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 31568ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 31578ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 31588ec2fa52SChristian Borntraeger } 31598ec2fa52SChristian Borntraeger 3160a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3161a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 31625102ee87STony Krowiak } 31635102ee87STony Krowiak 3164b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3165b31605c1SDominik Dingel { 3166b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 3167b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3168b31605c1SDominik Dingel } 3169b31605c1SDominik Dingel 3170b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3171b31605c1SDominik Dingel { 3172c4196218SChristian Borntraeger vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT); 3173b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 3174b31605c1SDominik Dingel return -ENOMEM; 3175b31605c1SDominik Dingel return 0; 3176b31605c1SDominik Dingel } 3177b31605c1SDominik Dingel 317891520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 317991520f1aSMichael Mueller { 318091520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 318191520f1aSMichael Mueller 318291520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 318380bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3184c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 318591520f1aSMichael Mueller } 318691520f1aSMichael Mueller 3187ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3188ff72bb55SSean Christopherson { 3189b31605c1SDominik Dingel int rc = 0; 319029b40f10SJanosch Frank u16 uvrc, uvrrc; 3191b31288faSKonstantin Weitz 31929e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 31939e6dabefSCornelia Huck CPUSTAT_SM | 3194a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3195a4a4f191SGuenther Hutzl 319653df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3197ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 319853df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3199ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3200a4a4f191SGuenther Hutzl 320191520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 320291520f1aSMichael Mueller 3203bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3204bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 32050c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3206bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 32070c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 3208f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 32090c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 32107119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 32117119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 32127feb6bb8SMichael Mueller 3213c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 32140c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3215cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 32160c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 32170c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 321848ee7d3aSDavid Hildenbrand if (sclp.has_cei) 32190c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 322011ad65b7SDavid Hildenbrand if (sclp.has_ib) 32210c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 322237c5f6c8SDavid Hildenbrand if (sclp.has_siif) 32230c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 322437c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 32250c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 322618280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 32270c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 32280c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 322913211ea7SEric Farman } 32308fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 32318fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3232a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3233a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3234d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3235d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3236d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3237d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3238d7c5cb01SMichael Mueller } 32394e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 32404e0b1ab7SFan Zhang | SDNXC; 3241c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 3242730cd632SFarhan Ali 3243730cd632SFarhan Ali if (sclp.has_kss) 3244ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3245730cd632SFarhan Ali else 3246492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 32475a5e6536SMatthew Rosato 3248e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3249b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3250b31605c1SDominik Dingel if (rc) 3251b31605c1SDominik Dingel return rc; 3252b31288faSKonstantin Weitz } 32530ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3254ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 32559d8d5786SMichael Mueller 325667d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 325767d49d52SCollin Walling 32585102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 32595102ee87STony Krowiak 326029b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 326129b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 326229b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 326329b40f10SJanosch Frank if (rc) 326429b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 326529b40f10SJanosch Frank } 326629b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 326729b40f10SJanosch Frank 3268b31605c1SDominik Dingel return rc; 3269b0c632dbSHeiko Carstens } 3270b0c632dbSHeiko Carstens 3271897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3272897cc38eSSean Christopherson { 3273897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3274897cc38eSSean Christopherson return -EINVAL; 3275897cc38eSSean Christopherson return 0; 3276897cc38eSSean Christopherson } 3277897cc38eSSean Christopherson 3278e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3279b0c632dbSHeiko Carstens { 32807feb6bb8SMichael Mueller struct sie_page *sie_page; 3281897cc38eSSean Christopherson int rc; 32824d47555aSCarsten Otte 3283da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3284c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 32857feb6bb8SMichael Mueller if (!sie_page) 3286e529ef66SSean Christopherson return -ENOMEM; 3287b0c632dbSHeiko Carstens 32887feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 32897feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 32907feb6bb8SMichael Mueller 3291efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3292efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3293efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3294efed1104SDavid Hildenbrand 3295e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3296ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3297e529ef66SSean Christopherson vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin; 32984b9f9525SMichael Mueller if (vcpu->arch.sie_block->gd && sclp.has_gisaf) 32994b9f9525SMichael Mueller vcpu->arch.sie_block->gd |= GISA_FORMAT1; 33009c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3301ba5c1e9bSCarsten Otte 3302321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3303321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3304321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3305321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3306321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3307321f8ee5SSean Christopherson KVM_SYNC_CRS | 3308321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 330923a60f83SCollin Walling KVM_SYNC_PFAULT | 331023a60f83SCollin Walling KVM_SYNC_DIAG318; 3311321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3312321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3313321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3314321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3315321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3316321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3317321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3318321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3319321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3320321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3321321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3322321f8ee5SSean Christopherson */ 3323321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3324321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3325321f8ee5SSean Christopherson else 3326321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3327321f8ee5SSean Christopherson 3328321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3329321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3330321f8ee5SSean Christopherson if (rc) 3331a2017f17SSean Christopherson goto out_free_sie_block; 3332321f8ee5SSean Christopherson } 3333321f8ee5SSean Christopherson 3334e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3335e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3336e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3337b0c632dbSHeiko Carstens 3338ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3339ff72bb55SSean Christopherson if (rc) 3340ff72bb55SSean Christopherson goto out_ucontrol_uninit; 3341e529ef66SSean Christopherson return 0; 3342e529ef66SSean Christopherson 3343ff72bb55SSean Christopherson out_ucontrol_uninit: 3344ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3345ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 33467b06bf2fSWei Yongjun out_free_sie_block: 33477b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3348e529ef66SSean Christopherson return rc; 3349b0c632dbSHeiko Carstens } 3350b0c632dbSHeiko Carstens 3351b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3352b0c632dbSHeiko Carstens { 33539b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 33549a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3355b0c632dbSHeiko Carstens } 3356b0c632dbSHeiko Carstens 3357199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3358199b5763SLongpeng(Mike) { 33590546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3360199b5763SLongpeng(Mike) } 3361199b5763SLongpeng(Mike) 336227406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 336349b99e1eSChristian Borntraeger { 3364805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 336561a6df54SDavid Hildenbrand exit_sie(vcpu); 336649b99e1eSChristian Borntraeger } 336749b99e1eSChristian Borntraeger 336827406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 336949b99e1eSChristian Borntraeger { 3370805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 337149b99e1eSChristian Borntraeger } 337249b99e1eSChristian Borntraeger 33738e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 33748e236546SChristian Borntraeger { 3375805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 337661a6df54SDavid Hildenbrand exit_sie(vcpu); 33778e236546SChristian Borntraeger } 33788e236546SChristian Borntraeger 33799ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 33809ea59728SDavid Hildenbrand { 33819ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 33829ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 33839ea59728SDavid Hildenbrand } 33849ea59728SDavid Hildenbrand 33858e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 33868e236546SChristian Borntraeger { 33879bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 33888e236546SChristian Borntraeger } 33898e236546SChristian Borntraeger 339049b99e1eSChristian Borntraeger /* 33919ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 339249b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 339349b99e1eSChristian Borntraeger * return immediately. */ 339449b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 339549b99e1eSChristian Borntraeger { 3396ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 33979ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 339849b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 339949b99e1eSChristian Borntraeger cpu_relax(); 340049b99e1eSChristian Borntraeger } 340149b99e1eSChristian Borntraeger 34028e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 34038e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 340449b99e1eSChristian Borntraeger { 34058e236546SChristian Borntraeger kvm_make_request(req, vcpu); 34068e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 340749b99e1eSChristian Borntraeger } 340849b99e1eSChristian Borntraeger 3409414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3410414d3b07SMartin Schwidefsky unsigned long end) 34112c70fe44SChristian Borntraeger { 34122c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 34132c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3414414d3b07SMartin Schwidefsky unsigned long prefix; 341546808a4cSMarc Zyngier unsigned long i; 34162c70fe44SChristian Borntraeger 341765d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 341865d0b0d4SDavid Hildenbrand return; 3419414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3420414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3421414d3b07SMartin Schwidefsky return; 34222c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 34232c70fe44SChristian Borntraeger /* match against both prefix pages */ 3424414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3425414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3426414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3427414d3b07SMartin Schwidefsky start, end); 34288e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 34292c70fe44SChristian Borntraeger } 34302c70fe44SChristian Borntraeger } 34312c70fe44SChristian Borntraeger } 34322c70fe44SChristian Borntraeger 34338b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 34348b905d28SChristian Borntraeger { 34358b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 34368b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 34378b905d28SChristian Borntraeger halt_poll_max_steal) { 34388b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 34398b905d28SChristian Borntraeger return true; 34408b905d28SChristian Borntraeger } 34418b905d28SChristian Borntraeger return false; 34428b905d28SChristian Borntraeger } 34438b905d28SChristian Borntraeger 3444b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3445b6d33834SChristoffer Dall { 3446b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3447b6d33834SChristoffer Dall BUG(); 3448b6d33834SChristoffer Dall return 0; 3449b6d33834SChristoffer Dall } 3450b6d33834SChristoffer Dall 345114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 345214eebd91SCarsten Otte struct kvm_one_reg *reg) 345314eebd91SCarsten Otte { 345414eebd91SCarsten Otte int r = -EINVAL; 345514eebd91SCarsten Otte 345614eebd91SCarsten Otte switch (reg->id) { 345729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 345829b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 345929b7c71bSCarsten Otte (u32 __user *)reg->addr); 346029b7c71bSCarsten Otte break; 346129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 346229b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 346329b7c71bSCarsten Otte (u64 __user *)reg->addr); 346429b7c71bSCarsten Otte break; 346546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 34664287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 346746a6dd1cSJason J. herne (u64 __user *)reg->addr); 346846a6dd1cSJason J. herne break; 346946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 347046a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 347146a6dd1cSJason J. herne (u64 __user *)reg->addr); 347246a6dd1cSJason J. herne break; 3473536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3474536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3475536336c2SDominik Dingel (u64 __user *)reg->addr); 3476536336c2SDominik Dingel break; 3477536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3478536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3479536336c2SDominik Dingel (u64 __user *)reg->addr); 3480536336c2SDominik Dingel break; 3481536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3482536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3483536336c2SDominik Dingel (u64 __user *)reg->addr); 3484536336c2SDominik Dingel break; 3485672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3486672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3487672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3488672550fbSChristian Borntraeger break; 3489afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3490afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3491afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3492afa45ff5SChristian Borntraeger break; 349314eebd91SCarsten Otte default: 349414eebd91SCarsten Otte break; 349514eebd91SCarsten Otte } 349614eebd91SCarsten Otte 349714eebd91SCarsten Otte return r; 349814eebd91SCarsten Otte } 349914eebd91SCarsten Otte 350014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 350114eebd91SCarsten Otte struct kvm_one_reg *reg) 350214eebd91SCarsten Otte { 350314eebd91SCarsten Otte int r = -EINVAL; 35044287f247SDavid Hildenbrand __u64 val; 350514eebd91SCarsten Otte 350614eebd91SCarsten Otte switch (reg->id) { 350729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 350829b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 350929b7c71bSCarsten Otte (u32 __user *)reg->addr); 351029b7c71bSCarsten Otte break; 351129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 351229b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 351329b7c71bSCarsten Otte (u64 __user *)reg->addr); 351429b7c71bSCarsten Otte break; 351546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 35164287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 35174287f247SDavid Hildenbrand if (!r) 35184287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 351946a6dd1cSJason J. herne break; 352046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 352146a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 352246a6dd1cSJason J. herne (u64 __user *)reg->addr); 352346a6dd1cSJason J. herne break; 3524536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3525536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3526536336c2SDominik Dingel (u64 __user *)reg->addr); 35279fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 35289fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3529536336c2SDominik Dingel break; 3530536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3531536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3532536336c2SDominik Dingel (u64 __user *)reg->addr); 3533536336c2SDominik Dingel break; 3534536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3535536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3536536336c2SDominik Dingel (u64 __user *)reg->addr); 3537536336c2SDominik Dingel break; 3538672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3539672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3540672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3541672550fbSChristian Borntraeger break; 3542afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3543afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3544afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3545afa45ff5SChristian Borntraeger break; 354614eebd91SCarsten Otte default: 354714eebd91SCarsten Otte break; 354814eebd91SCarsten Otte } 354914eebd91SCarsten Otte 355014eebd91SCarsten Otte return r; 355114eebd91SCarsten Otte } 3552b6d33834SChristoffer Dall 35537de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 3554b0c632dbSHeiko Carstens { 35557de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 35567de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 35577de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 35587de3f142SJanosch Frank 35597de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 35607de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 35617de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 35627de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 35637de3f142SJanosch Frank } 35647de3f142SJanosch Frank 35657de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 35667de3f142SJanosch Frank { 35677de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 35687de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 35697de3f142SJanosch Frank 3570e93fc7b4SChristian Borntraeger /* 3571e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 3572e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 3573e93fc7b4SChristian Borntraeger */ 35747de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 35757de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 35767de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 35777de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 35787de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 35797de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 35807de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 35817de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 3582e93fc7b4SChristian Borntraeger 3583e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 3584e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 3585e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 3586e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 3587e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 3588e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 3589e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 3590e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 3591e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 3592e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 3593e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 3594e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 35957de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 35960f303504SJanosch Frank /* 35970f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 35980f303504SJanosch Frank * them are overlayed and they are not accessible in this case 35990f303504SJanosch Frank * anyway. 36000f303504SJanosch Frank */ 36010f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 36027de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 36037de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 36047de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 36050f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 36060f303504SJanosch Frank } 36077de3f142SJanosch Frank } 36087de3f142SJanosch Frank 36097de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 36107de3f142SJanosch Frank { 36117de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 36127de3f142SJanosch Frank 36137de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 36147de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 36157de3f142SJanosch Frank 36167de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 36177de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 36187de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 36197de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 36207de3f142SJanosch Frank 36217de3f142SJanosch Frank regs->etoken = 0; 36227de3f142SJanosch Frank regs->etoken_extension = 0; 3623b0c632dbSHeiko Carstens } 3624b0c632dbSHeiko Carstens 3625b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3626b0c632dbSHeiko Carstens { 3627875656feSChristoffer Dall vcpu_load(vcpu); 36285a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3629875656feSChristoffer Dall vcpu_put(vcpu); 3630b0c632dbSHeiko Carstens return 0; 3631b0c632dbSHeiko Carstens } 3632b0c632dbSHeiko Carstens 3633b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3634b0c632dbSHeiko Carstens { 36351fc9b76bSChristoffer Dall vcpu_load(vcpu); 36365a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 36371fc9b76bSChristoffer Dall vcpu_put(vcpu); 3638b0c632dbSHeiko Carstens return 0; 3639b0c632dbSHeiko Carstens } 3640b0c632dbSHeiko Carstens 3641b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3642b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3643b0c632dbSHeiko Carstens { 3644b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3645b4ef9d4eSChristoffer Dall 364659674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3647b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3648b4ef9d4eSChristoffer Dall 3649b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3650b0c632dbSHeiko Carstens return 0; 3651b0c632dbSHeiko Carstens } 3652b0c632dbSHeiko Carstens 3653b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3654b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3655b0c632dbSHeiko Carstens { 3656bcdec41cSChristoffer Dall vcpu_load(vcpu); 3657bcdec41cSChristoffer Dall 365859674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3659b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3660bcdec41cSChristoffer Dall 3661bcdec41cSChristoffer Dall vcpu_put(vcpu); 3662b0c632dbSHeiko Carstens return 0; 3663b0c632dbSHeiko Carstens } 3664b0c632dbSHeiko Carstens 3665b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3666b0c632dbSHeiko Carstens { 36676a96bc7fSChristoffer Dall int ret = 0; 36686a96bc7fSChristoffer Dall 36696a96bc7fSChristoffer Dall vcpu_load(vcpu); 36706a96bc7fSChristoffer Dall 36716a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 36726a96bc7fSChristoffer Dall ret = -EINVAL; 36736a96bc7fSChristoffer Dall goto out; 36746a96bc7fSChristoffer Dall } 3675e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 36769abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3677a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3678a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 36799abc2a08SDavid Hildenbrand else 3680a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 36816a96bc7fSChristoffer Dall 36826a96bc7fSChristoffer Dall out: 36836a96bc7fSChristoffer Dall vcpu_put(vcpu); 36846a96bc7fSChristoffer Dall return ret; 3685b0c632dbSHeiko Carstens } 3686b0c632dbSHeiko Carstens 3687b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3688b0c632dbSHeiko Carstens { 36891393123eSChristoffer Dall vcpu_load(vcpu); 36901393123eSChristoffer Dall 36919abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 36929abc2a08SDavid Hildenbrand save_fpu_regs(); 36939abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3694a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3695a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 36969abc2a08SDavid Hildenbrand else 3697a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3698e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 36991393123eSChristoffer Dall 37001393123eSChristoffer Dall vcpu_put(vcpu); 3701b0c632dbSHeiko Carstens return 0; 3702b0c632dbSHeiko Carstens } 3703b0c632dbSHeiko Carstens 3704b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3705b0c632dbSHeiko Carstens { 3706b0c632dbSHeiko Carstens int rc = 0; 3707b0c632dbSHeiko Carstens 37087a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3709b0c632dbSHeiko Carstens rc = -EBUSY; 3710d7b0b5ebSCarsten Otte else { 3711d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3712d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3713d7b0b5ebSCarsten Otte } 3714b0c632dbSHeiko Carstens return rc; 3715b0c632dbSHeiko Carstens } 3716b0c632dbSHeiko Carstens 3717b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3718b0c632dbSHeiko Carstens struct kvm_translation *tr) 3719b0c632dbSHeiko Carstens { 3720b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3721b0c632dbSHeiko Carstens } 3722b0c632dbSHeiko Carstens 372327291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 372427291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 372527291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 372627291e21SDavid Hildenbrand 3727d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3728d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3729b0c632dbSHeiko Carstens { 373027291e21SDavid Hildenbrand int rc = 0; 373127291e21SDavid Hildenbrand 373266b56562SChristoffer Dall vcpu_load(vcpu); 373366b56562SChristoffer Dall 373427291e21SDavid Hildenbrand vcpu->guest_debug = 0; 373527291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 373627291e21SDavid Hildenbrand 373766b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 373866b56562SChristoffer Dall rc = -EINVAL; 373966b56562SChristoffer Dall goto out; 374066b56562SChristoffer Dall } 374166b56562SChristoffer Dall if (!sclp.has_gpere) { 374266b56562SChristoffer Dall rc = -EINVAL; 374366b56562SChristoffer Dall goto out; 374466b56562SChristoffer Dall } 374527291e21SDavid Hildenbrand 374627291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 374727291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 374827291e21SDavid Hildenbrand /* enforce guest PER */ 3749ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 375027291e21SDavid Hildenbrand 375127291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 375227291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 375327291e21SDavid Hildenbrand } else { 37549daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 375527291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 375627291e21SDavid Hildenbrand } 375727291e21SDavid Hildenbrand 375827291e21SDavid Hildenbrand if (rc) { 375927291e21SDavid Hildenbrand vcpu->guest_debug = 0; 376027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 37619daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 376227291e21SDavid Hildenbrand } 376327291e21SDavid Hildenbrand 376466b56562SChristoffer Dall out: 376566b56562SChristoffer Dall vcpu_put(vcpu); 376627291e21SDavid Hildenbrand return rc; 3767b0c632dbSHeiko Carstens } 3768b0c632dbSHeiko Carstens 376962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 377062d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 377162d9f0dbSMarcelo Tosatti { 3772fd232561SChristoffer Dall int ret; 3773fd232561SChristoffer Dall 3774fd232561SChristoffer Dall vcpu_load(vcpu); 3775fd232561SChristoffer Dall 37766352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3777fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 37786352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3779fd232561SChristoffer Dall 3780fd232561SChristoffer Dall vcpu_put(vcpu); 3781fd232561SChristoffer Dall return ret; 378262d9f0dbSMarcelo Tosatti } 378362d9f0dbSMarcelo Tosatti 378462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 378562d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 378662d9f0dbSMarcelo Tosatti { 37876352e4d2SDavid Hildenbrand int rc = 0; 37886352e4d2SDavid Hildenbrand 3789e83dff5eSChristoffer Dall vcpu_load(vcpu); 3790e83dff5eSChristoffer Dall 37916352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 379267cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 37936352e4d2SDavid Hildenbrand 37946352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 37956352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 3796fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 37976352e4d2SDavid Hildenbrand break; 37986352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 3799fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 38006352e4d2SDavid Hildenbrand break; 38016352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 38027c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 38037c36a3fcSJanosch Frank rc = -ENXIO; 38047c36a3fcSJanosch Frank break; 38057c36a3fcSJanosch Frank } 38067c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 38077c36a3fcSJanosch Frank break; 38086352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 38093b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 38106352e4d2SDavid Hildenbrand default: 38116352e4d2SDavid Hildenbrand rc = -ENXIO; 38126352e4d2SDavid Hildenbrand } 38136352e4d2SDavid Hildenbrand 3814e83dff5eSChristoffer Dall vcpu_put(vcpu); 38156352e4d2SDavid Hildenbrand return rc; 381662d9f0dbSMarcelo Tosatti } 381762d9f0dbSMarcelo Tosatti 38188ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 38198ad35755SDavid Hildenbrand { 38208d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 38218ad35755SDavid Hildenbrand } 38228ad35755SDavid Hildenbrand 38232c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 38242c70fe44SChristian Borntraeger { 38258ad35755SDavid Hildenbrand retry: 38268e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 38272fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3828586b7ccdSChristian Borntraeger return 0; 38292c70fe44SChristian Borntraeger /* 38302c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3831b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 38322c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 38332c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 38342c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 38352c70fe44SChristian Borntraeger */ 38368ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 38372c70fe44SChristian Borntraeger int rc; 3838b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3839fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3840b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3841aca411a4SJulius Niedworok if (rc) { 3842aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 38432c70fe44SChristian Borntraeger return rc; 3844aca411a4SJulius Niedworok } 38458ad35755SDavid Hildenbrand goto retry; 38462c70fe44SChristian Borntraeger } 38478ad35755SDavid Hildenbrand 3848d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3849d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3850d3d692c8SDavid Hildenbrand goto retry; 3851d3d692c8SDavid Hildenbrand } 3852d3d692c8SDavid Hildenbrand 38538ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 38548ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 38558ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3856ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 38578ad35755SDavid Hildenbrand } 38588ad35755SDavid Hildenbrand goto retry; 38598ad35755SDavid Hildenbrand } 38608ad35755SDavid Hildenbrand 38618ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 38628ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 38638ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 38649daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 38658ad35755SDavid Hildenbrand } 38668ad35755SDavid Hildenbrand goto retry; 38678ad35755SDavid Hildenbrand } 38688ad35755SDavid Hildenbrand 38696502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 38706502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 38716502a34cSDavid Hildenbrand goto retry; 38726502a34cSDavid Hildenbrand } 38736502a34cSDavid Hildenbrand 3874190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3875190df4a2SClaudio Imbrenda /* 3876c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3877190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3878190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3879190df4a2SClaudio Imbrenda */ 3880190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3881190df4a2SClaudio Imbrenda goto retry; 3882190df4a2SClaudio Imbrenda } 3883190df4a2SClaudio Imbrenda 3884190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3885190df4a2SClaudio Imbrenda /* 3886c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3887c9f0a2b8SJanosch Frank * CMM has been used. 3888190df4a2SClaudio Imbrenda */ 3889190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3890c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3891190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3892190df4a2SClaudio Imbrenda goto retry; 3893190df4a2SClaudio Imbrenda } 3894190df4a2SClaudio Imbrenda 38950759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 389672875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 38973194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 38983194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 38990759d068SDavid Hildenbrand 39002c70fe44SChristian Borntraeger return 0; 39012c70fe44SChristian Borntraeger } 39022c70fe44SChristian Borntraeger 39030e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, 39048fa1696eSCollin L. Walling const struct kvm_s390_vm_tod_clock *gtod) 39058fa1696eSCollin L. Walling { 39068fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 39072cfd7b73SHeiko Carstens union tod_clock clk; 390846808a4cSMarc Zyngier unsigned long i; 39098fa1696eSCollin L. Walling 39108fa1696eSCollin L. Walling mutex_lock(&kvm->lock); 39118fa1696eSCollin L. Walling preempt_disable(); 39128fa1696eSCollin L. Walling 39132cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 39148fa1696eSCollin L. Walling 39152cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 39160e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 39170e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 39182cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 39198fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 39208fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 39210e7def5fSDavid Hildenbrand } 39228fa1696eSCollin L. Walling 39238fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 39248fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 39258fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 39268fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 39278fa1696eSCollin L. Walling } 39288fa1696eSCollin L. Walling 39298fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 39308fa1696eSCollin L. Walling preempt_enable(); 39318fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 39328fa1696eSCollin L. Walling } 39338fa1696eSCollin L. Walling 3934fa576c58SThomas Huth /** 3935fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 3936fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 3937fa576c58SThomas Huth * @gpa: Guest physical address 3938fa576c58SThomas Huth * @writable: Whether the page should be writable or not 3939fa576c58SThomas Huth * 3940fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 3941fa576c58SThomas Huth * 3942fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 3943fa576c58SThomas Huth */ 3944fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 394524eb3a82SDominik Dingel { 3946527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 3947527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 394824eb3a82SDominik Dingel } 394924eb3a82SDominik Dingel 39503c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 39513c038e6bSDominik Dingel unsigned long token) 39523c038e6bSDominik Dingel { 39533c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 3954383d0b05SJens Freimann struct kvm_s390_irq irq; 39553c038e6bSDominik Dingel 39563c038e6bSDominik Dingel if (start_token) { 3957383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 3958383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 3959383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 39603c038e6bSDominik Dingel } else { 39613c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 3962383d0b05SJens Freimann inti.parm64 = token; 39633c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 39643c038e6bSDominik Dingel } 39653c038e6bSDominik Dingel } 39663c038e6bSDominik Dingel 39672a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 39683c038e6bSDominik Dingel struct kvm_async_pf *work) 39693c038e6bSDominik Dingel { 39703c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 39713c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 39722a18b7e7SVitaly Kuznetsov 39732a18b7e7SVitaly Kuznetsov return true; 39743c038e6bSDominik Dingel } 39753c038e6bSDominik Dingel 39763c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 39773c038e6bSDominik Dingel struct kvm_async_pf *work) 39783c038e6bSDominik Dingel { 39793c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 39803c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 39813c038e6bSDominik Dingel } 39823c038e6bSDominik Dingel 39833c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 39843c038e6bSDominik Dingel struct kvm_async_pf *work) 39853c038e6bSDominik Dingel { 39863c038e6bSDominik Dingel /* s390 will always inject the page directly */ 39873c038e6bSDominik Dingel } 39883c038e6bSDominik Dingel 39897c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 39903c038e6bSDominik Dingel { 39913c038e6bSDominik Dingel /* 39923c038e6bSDominik Dingel * s390 will always inject the page directly, 39933c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 39943c038e6bSDominik Dingel */ 39953c038e6bSDominik Dingel return true; 39963c038e6bSDominik Dingel } 39973c038e6bSDominik Dingel 3998e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 39993c038e6bSDominik Dingel { 40003c038e6bSDominik Dingel hva_t hva; 40013c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 40023c038e6bSDominik Dingel 40033c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4004e8c22266SVitaly Kuznetsov return false; 40053c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 40063c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4007e8c22266SVitaly Kuznetsov return false; 40083c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4009e8c22266SVitaly Kuznetsov return false; 40109a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4011e8c22266SVitaly Kuznetsov return false; 4012b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4013e8c22266SVitaly Kuznetsov return false; 40143c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4015e8c22266SVitaly Kuznetsov return false; 40163c038e6bSDominik Dingel 401781480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 401881480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 401981480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4020e8c22266SVitaly Kuznetsov return false; 40213c038e6bSDominik Dingel 4022e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 40233c038e6bSDominik Dingel } 40243c038e6bSDominik Dingel 40253fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4026b0c632dbSHeiko Carstens { 40273fb4c40fSThomas Huth int rc, cpuflags; 4028e168bf8dSCarsten Otte 40293c038e6bSDominik Dingel /* 40303c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 40313c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 40323c038e6bSDominik Dingel * handled outside the worker. 40333c038e6bSDominik Dingel */ 40343c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 40353c038e6bSDominik Dingel 40367ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 40377ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4038b0c632dbSHeiko Carstens 4039b0c632dbSHeiko Carstens if (need_resched()) 4040b0c632dbSHeiko Carstens schedule(); 4041b0c632dbSHeiko Carstens 404279395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 404379395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 404479395031SJens Freimann if (rc) 404579395031SJens Freimann return rc; 404679395031SJens Freimann } 40470ff31867SCarsten Otte 40482c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 40492c70fe44SChristian Borntraeger if (rc) 40502c70fe44SChristian Borntraeger return rc; 40512c70fe44SChristian Borntraeger 405227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 405327291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 405427291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 405527291e21SDavid Hildenbrand } 405627291e21SDavid Hildenbrand 40574eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 40589f30f621SMichael Mueller 4059b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 40603fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 40613fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 40623fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 40632b29a9fdSDominik Dingel 40643fb4c40fSThomas Huth return 0; 40653fb4c40fSThomas Huth } 40663fb4c40fSThomas Huth 4067492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4068492d8642SThomas Huth { 406956317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 407056317920SDavid Hildenbrand .code = PGM_ADDRESSING, 407156317920SDavid Hildenbrand }; 407256317920SDavid Hildenbrand u8 opcode, ilen; 4073492d8642SThomas Huth int rc; 4074492d8642SThomas Huth 4075492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4076492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4077492d8642SThomas Huth 4078492d8642SThomas Huth /* 4079492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4080492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4081492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4082492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4083492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4084492d8642SThomas Huth * to be able to forward the PSW. 4085492d8642SThomas Huth */ 40863fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 408756317920SDavid Hildenbrand ilen = insn_length(opcode); 40889b0d721aSDavid Hildenbrand if (rc < 0) { 40899b0d721aSDavid Hildenbrand return rc; 40909b0d721aSDavid Hildenbrand } else if (rc) { 40919b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 40929b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 40939b0d721aSDavid Hildenbrand * nullification if necessary. 40949b0d721aSDavid Hildenbrand */ 40959b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 40969b0d721aSDavid Hildenbrand ilen = 4; 40979b0d721aSDavid Hildenbrand } 409856317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 409956317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 410056317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4101492d8642SThomas Huth } 4102492d8642SThomas Huth 41033fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 41043fb4c40fSThomas Huth { 41054d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 41064d62fcc0SQingFeng Hao struct sie_page *sie_page; 41074d62fcc0SQingFeng Hao 41082b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 41092b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 41102b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 41112b29a9fdSDominik Dingel 411227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 411327291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 411427291e21SDavid Hildenbrand 41157ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 41167ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 411771f116bfSDavid Hildenbrand 41184d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 41194d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 41204d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 41214d62fcc0SQingFeng Hao struct sie_page, sie_block); 41224d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 41234d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 41244d62fcc0SQingFeng Hao return 0; 41254d62fcc0SQingFeng Hao } 41264d62fcc0SQingFeng Hao 412771f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 412871f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 412971f116bfSDavid Hildenbrand 413071f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 413171f116bfSDavid Hildenbrand return rc; 413271f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 413371f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 413471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 413571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 413671f116bfSDavid Hildenbrand return -EREMOTE; 413771f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 413871f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 413971f116bfSDavid Hildenbrand return 0; 4140210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4141210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4142210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4143210b1607SThomas Huth current->thread.gmap_addr; 4144210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 414571f116bfSDavid Hildenbrand return -EREMOTE; 414624eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 41473c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 414824eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 414971f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 415071f116bfSDavid Hildenbrand return 0; 415150a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 415271f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4153fa576c58SThomas Huth } 415471f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 41553fb4c40fSThomas Huth } 41563fb4c40fSThomas Huth 41573adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 41583fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 41593fb4c40fSThomas Huth { 41603fb4c40fSThomas Huth int rc, exit_reason; 4161c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 41623fb4c40fSThomas Huth 4163800c1065SThomas Huth /* 4164800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4165800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4166800c1065SThomas Huth */ 4167800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 4168800c1065SThomas Huth 4169a76ccff6SThomas Huth do { 41703fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 41713fb4c40fSThomas Huth if (rc) 4172a76ccff6SThomas Huth break; 41733fb4c40fSThomas Huth 4174800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 41753fb4c40fSThomas Huth /* 4176a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4177a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 41783fb4c40fSThomas Huth */ 41790097d12eSChristian Borntraeger local_irq_disable(); 41806edaa530SPaolo Bonzini guest_enter_irqoff(); 4181db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 41820097d12eSChristian Borntraeger local_irq_enable(); 4183c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4184c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4185c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4186c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4187c8aac234SJanosch Frank } 418856e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 418956e62a73SSven Schnelle load_fpu_regs(); 4190a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4191a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4192c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4193c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4194c8aac234SJanosch Frank sie_page->pv_grregs, 4195c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 41963adae0b4SJanosch Frank /* 41973adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 41983adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 41993adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 42003adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 42013adae0b4SJanosch Frank */ 42023adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 42033adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 42043adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 42053adae0b4SJanosch Frank } 4206c8aac234SJanosch Frank } 42070097d12eSChristian Borntraeger local_irq_disable(); 4208db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 42096edaa530SPaolo Bonzini guest_exit_irqoff(); 42100097d12eSChristian Borntraeger local_irq_enable(); 4211800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 42123fb4c40fSThomas Huth 42133fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 421427291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 42153fb4c40fSThomas Huth 4216800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 4217e168bf8dSCarsten Otte return rc; 4218b0c632dbSHeiko Carstens } 4219b0c632dbSHeiko Carstens 42202f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4221b028ee3eSDavid Hildenbrand { 42222f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 42234d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 42244e0b1ab7SFan Zhang struct gs_cb *gscb; 42254d5f2c04SChristian Borntraeger 42264d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 42274e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4228b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4229b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4230b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4231b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4232b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4233b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4234b028ee3eSDavid Hildenbrand } 4235b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4236b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4237b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4238b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 42399fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 42409fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4241b028ee3eSDavid Hildenbrand } 424223a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 424323a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 424423a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 42453fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 424623a60f83SCollin Walling } 424780cd8763SFan Zhang /* 424880cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 424980cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 425080cd8763SFan Zhang */ 425180cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 42524d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4253bb59c2daSAlice Frosi riccb->v && 42540c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 42554d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 42560c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 425780cd8763SFan Zhang } 42584e0b1ab7SFan Zhang /* 42594e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 42604e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 42614e0b1ab7SFan Zhang */ 42624e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 42634e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 42644e0b1ab7SFan Zhang gscb->gssm && 42654e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 42664e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 42674e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 42684e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 42694e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 427080cd8763SFan Zhang } 427135b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 427235b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 427335b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 427435b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 427535b3fde6SChristian Borntraeger } 42764e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 42774e0b1ab7SFan Zhang preempt_disable(); 42784e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 42794e0b1ab7SFan Zhang if (current->thread.gs_cb) { 42804e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 42814e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 42824e0b1ab7SFan Zhang } 42834e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 42844e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 42854e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 42864e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 42874e0b1ab7SFan Zhang } 42884e0b1ab7SFan Zhang preempt_enable(); 42894e0b1ab7SFan Zhang } 4290a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4291811ea797SJanosch Frank } 4292811ea797SJanosch Frank 42932f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4294811ea797SJanosch Frank { 42952f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 42962f0a83beSTianjia Zhang 4297811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4298811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4299811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4300811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4301811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4302811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4303811ea797SJanosch Frank } 4304811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4305811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4306811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4307811ea797SJanosch Frank } 4308811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4309811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4310811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4311811ea797SJanosch Frank save_fpu_regs(); 4312811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4313811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4314811ea797SJanosch Frank if (MACHINE_HAS_VX) 4315811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4316811ea797SJanosch Frank else 4317811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4318811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4319811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4320811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4321811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4322811ea797SJanosch Frank 4323811ea797SJanosch Frank /* Sync fmt2 only data */ 4324811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 43252f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4326811ea797SJanosch Frank } else { 4327811ea797SJanosch Frank /* 4328811ea797SJanosch Frank * In several places we have to modify our internal view to 4329811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4330811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4331811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4332811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4333811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4334811ea797SJanosch Frank * do only accept the condition code from userspace. 4335811ea797SJanosch Frank */ 4336811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4337811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4338811ea797SJanosch Frank PSW_MASK_CC; 4339811ea797SJanosch Frank } 434080cd8763SFan Zhang 4341b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4342b028ee3eSDavid Hildenbrand } 4343b028ee3eSDavid Hildenbrand 43442f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4345b028ee3eSDavid Hildenbrand { 43462f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 43472f0a83beSTianjia Zhang 4348b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4349b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4350b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 435135b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 435223a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 43534e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 435444bada28SHeiko Carstens preempt_disable(); 43554e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 43564e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 43574e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 43584e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 43594e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 43604e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 43614e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 43624e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 436344bada28SHeiko Carstens preempt_enable(); 43644e0b1ab7SFan Zhang } 4365a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4366b028ee3eSDavid Hildenbrand } 4367b028ee3eSDavid Hildenbrand 43682f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4369811ea797SJanosch Frank { 43702f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 43712f0a83beSTianjia Zhang 4372811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4373811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4374811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4375811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4376811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4377811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4378811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4379811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4380811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4381811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4382811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4383811ea797SJanosch Frank /* Save guest register state */ 4384811ea797SJanosch Frank save_fpu_regs(); 4385811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4386811ea797SJanosch Frank /* Restore will be done lazily at return */ 4387811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4388811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4389811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 43902f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4391811ea797SJanosch Frank } 4392811ea797SJanosch Frank 43931b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4394b0c632dbSHeiko Carstens { 43951b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 43968f2abe6aSChristian Borntraeger int rc; 4397b0c632dbSHeiko Carstens 4398460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4399460df4c1SPaolo Bonzini return -EINTR; 4400460df4c1SPaolo Bonzini 4401200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4402200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4403200824f5SThomas Huth return -EINVAL; 4404200824f5SThomas Huth 4405accb757dSChristoffer Dall vcpu_load(vcpu); 4406accb757dSChristoffer Dall 440727291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 440827291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4409accb757dSChristoffer Dall rc = 0; 4410accb757dSChristoffer Dall goto out; 441127291e21SDavid Hildenbrand } 441227291e21SDavid Hildenbrand 441320b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4414b0c632dbSHeiko Carstens 4415fe28c786SJanosch Frank /* 4416fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4417fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4418fe28c786SJanosch Frank */ 44196352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 44206852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 44216352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4422ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 44236352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4424accb757dSChristoffer Dall rc = -EINVAL; 4425accb757dSChristoffer Dall goto out; 44266352e4d2SDavid Hildenbrand } 4427b0c632dbSHeiko Carstens 44282f0a83beSTianjia Zhang sync_regs(vcpu); 4429db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4430d7b0b5ebSCarsten Otte 4431dab4079dSHeiko Carstens might_fault(); 4432e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 44339ace903dSChristian Ehrhardt 4434b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4435b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 44368f2abe6aSChristian Borntraeger rc = -EINTR; 4437b1d16c49SChristian Ehrhardt } 44388f2abe6aSChristian Borntraeger 443927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 444027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 444127291e21SDavid Hildenbrand rc = 0; 444227291e21SDavid Hildenbrand } 444327291e21SDavid Hildenbrand 44448f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 444571f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 44468f2abe6aSChristian Borntraeger rc = 0; 44478f2abe6aSChristian Borntraeger } 44488f2abe6aSChristian Borntraeger 4449db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 44502f0a83beSTianjia Zhang store_regs(vcpu); 4451d7b0b5ebSCarsten Otte 445220b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4453b0c632dbSHeiko Carstens 4454b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4455accb757dSChristoffer Dall out: 4456accb757dSChristoffer Dall vcpu_put(vcpu); 44577e8e6ab4SHeiko Carstens return rc; 4458b0c632dbSHeiko Carstens } 4459b0c632dbSHeiko Carstens 4460b0c632dbSHeiko Carstens /* 4461b0c632dbSHeiko Carstens * store status at address 4462b0c632dbSHeiko Carstens * we use have two special cases: 4463b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4464b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4465b0c632dbSHeiko Carstens */ 4466d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4467b0c632dbSHeiko Carstens { 4468092670cdSCarsten Otte unsigned char archmode = 1; 44699abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4470fda902cbSMichael Mueller unsigned int px; 44714287f247SDavid Hildenbrand u64 clkcomp, cputm; 4472d0bce605SHeiko Carstens int rc; 4473b0c632dbSHeiko Carstens 4474d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4475d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4476d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4477b0c632dbSHeiko Carstens return -EFAULT; 4478d9a3a09aSMartin Schwidefsky gpa = 0; 4479d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4480d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4481b0c632dbSHeiko Carstens return -EFAULT; 4482d9a3a09aSMartin Schwidefsky gpa = px; 4483d9a3a09aSMartin Schwidefsky } else 4484d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 44859abc2a08SDavid Hildenbrand 44869abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 44879abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 44889522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4489d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 44909abc2a08SDavid Hildenbrand fprs, 128); 44919abc2a08SDavid Hildenbrand } else { 44929abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 44936fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 44949abc2a08SDavid Hildenbrand } 4495d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4496d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4497d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4498d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4499d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4500fda902cbSMichael Mueller &px, 4); 4501d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 45029abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4503d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4504d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 45054287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4506d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 45074287f247SDavid Hildenbrand &cputm, 8); 4508178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4509d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4510d0bce605SHeiko Carstens &clkcomp, 8); 4511d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4512d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4513d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4514d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4515d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4516b0c632dbSHeiko Carstens } 4517b0c632dbSHeiko Carstens 4518e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4519e879892cSThomas Huth { 4520e879892cSThomas Huth /* 4521e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 452231d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4523e879892cSThomas Huth * it into the save area 4524e879892cSThomas Huth */ 4525d0164ee2SHendrik Brueckner save_fpu_regs(); 45269abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4527e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4528e879892cSThomas Huth 4529e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4530e879892cSThomas Huth } 4531e879892cSThomas Huth 45328ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 45338ad35755SDavid Hildenbrand { 45348ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 45358e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 45368ad35755SDavid Hildenbrand } 45378ad35755SDavid Hildenbrand 45388ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 45398ad35755SDavid Hildenbrand { 454046808a4cSMarc Zyngier unsigned long i; 45418ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 45428ad35755SDavid Hildenbrand 45438ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 45448ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 45458ad35755SDavid Hildenbrand } 45468ad35755SDavid Hildenbrand } 45478ad35755SDavid Hildenbrand 45488ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 45498ad35755SDavid Hildenbrand { 455009a400e7SDavid Hildenbrand if (!sclp.has_ibs) 455109a400e7SDavid Hildenbrand return; 45528ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 45538e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 45548ad35755SDavid Hildenbrand } 45558ad35755SDavid Hildenbrand 4556fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 45576852d7b6SDavid Hildenbrand { 4558fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 45598ad35755SDavid Hildenbrand 45608ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4561fe28c786SJanosch Frank return 0; 45628ad35755SDavid Hildenbrand 45636852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 45648ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4565433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 45668ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 45678ad35755SDavid Hildenbrand 4568fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 4569fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4570fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 4571fe28c786SJanosch Frank if (r) { 4572fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4573fe28c786SJanosch Frank return r; 4574fe28c786SJanosch Frank } 4575fe28c786SJanosch Frank } 4576fe28c786SJanosch Frank 45778ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 4578113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 45798ad35755SDavid Hildenbrand started_vcpus++; 45808ad35755SDavid Hildenbrand } 45818ad35755SDavid Hildenbrand 45828ad35755SDavid Hildenbrand if (started_vcpus == 0) { 45838ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 45848ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 45858ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 45868ad35755SDavid Hildenbrand /* 45878ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 45888ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 458938860756SBhaskar Chowdhury * outstanding ENABLE requests. 45908ad35755SDavid Hildenbrand */ 45918ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 45928ad35755SDavid Hildenbrand } 45938ad35755SDavid Hildenbrand 45949daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 45958ad35755SDavid Hildenbrand /* 459672f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 459772f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 459872f21820SChristian Borntraeger * refresh our view. 459972f21820SChristian Borntraeger */ 460072f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 460172f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 460272f21820SChristian Borntraeger /* 46038ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 46048ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 46058ad35755SDavid Hildenbrand */ 4606d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4607433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4608fe28c786SJanosch Frank return 0; 46096852d7b6SDavid Hildenbrand } 46106852d7b6SDavid Hildenbrand 4611fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 46126852d7b6SDavid Hildenbrand { 4613fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 46148ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 46158ad35755SDavid Hildenbrand 46168ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 4617fe28c786SJanosch Frank return 0; 46188ad35755SDavid Hildenbrand 46196852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 46208ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4621433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 46228ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 46238ad35755SDavid Hildenbrand 4624fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 4625fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4626fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 4627fe28c786SJanosch Frank if (r) { 4628fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4629fe28c786SJanosch Frank return r; 4630fe28c786SJanosch Frank } 4631fe28c786SJanosch Frank } 4632fe28c786SJanosch Frank 463332f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 46346cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 463532f5ff63SDavid Hildenbrand 4636ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 46378ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 46388ad35755SDavid Hildenbrand 46398ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 4640113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 4641113d10bcSMarc Zyngier 4642113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 46438ad35755SDavid Hildenbrand started_vcpus++; 4644113d10bcSMarc Zyngier started_vcpu = tmp; 46458ad35755SDavid Hildenbrand } 46468ad35755SDavid Hildenbrand } 46478ad35755SDavid Hildenbrand 46488ad35755SDavid Hildenbrand if (started_vcpus == 1) { 46498ad35755SDavid Hildenbrand /* 46508ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 46518ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 46528ad35755SDavid Hildenbrand */ 46538ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 46548ad35755SDavid Hildenbrand } 46558ad35755SDavid Hildenbrand 4656433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4657fe28c786SJanosch Frank return 0; 46586852d7b6SDavid Hildenbrand } 46596852d7b6SDavid Hildenbrand 4660d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4661d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4662d6712df9SCornelia Huck { 4663d6712df9SCornelia Huck int r; 4664d6712df9SCornelia Huck 4665d6712df9SCornelia Huck if (cap->flags) 4666d6712df9SCornelia Huck return -EINVAL; 4667d6712df9SCornelia Huck 4668d6712df9SCornelia Huck switch (cap->cap) { 4669fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4670fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4671fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4672c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4673fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4674fa6b7fe9SCornelia Huck } 4675fa6b7fe9SCornelia Huck r = 0; 4676fa6b7fe9SCornelia Huck break; 4677d6712df9SCornelia Huck default: 4678d6712df9SCornelia Huck r = -EINVAL; 4679d6712df9SCornelia Huck break; 4680d6712df9SCornelia Huck } 4681d6712df9SCornelia Huck return r; 4682d6712df9SCornelia Huck } 4683d6712df9SCornelia Huck 468419e12277SJanosch Frank static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu, 468519e12277SJanosch Frank struct kvm_s390_mem_op *mop) 468619e12277SJanosch Frank { 468719e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 468819e12277SJanosch Frank int r = 0; 468919e12277SJanosch Frank 469019e12277SJanosch Frank if (mop->flags || !mop->size) 469119e12277SJanosch Frank return -EINVAL; 469219e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 469319e12277SJanosch Frank return -EINVAL; 469419e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 469519e12277SJanosch Frank return -E2BIG; 469619e12277SJanosch Frank 469719e12277SJanosch Frank switch (mop->op) { 469819e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 469919e12277SJanosch Frank if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) + 470019e12277SJanosch Frank mop->sida_offset), mop->size)) 470119e12277SJanosch Frank r = -EFAULT; 470219e12277SJanosch Frank 470319e12277SJanosch Frank break; 470419e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 470519e12277SJanosch Frank if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) + 470619e12277SJanosch Frank mop->sida_offset), uaddr, mop->size)) 470719e12277SJanosch Frank r = -EFAULT; 470819e12277SJanosch Frank break; 470919e12277SJanosch Frank } 471019e12277SJanosch Frank return r; 471119e12277SJanosch Frank } 471241408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 471341408c28SThomas Huth struct kvm_s390_mem_op *mop) 471441408c28SThomas Huth { 471541408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 471641408c28SThomas Huth void *tmpbuf = NULL; 471719e12277SJanosch Frank int r = 0; 471841408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 471941408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 472041408c28SThomas Huth 4721a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 472241408c28SThomas Huth return -EINVAL; 472341408c28SThomas Huth 472441408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 472541408c28SThomas Huth return -E2BIG; 472641408c28SThomas Huth 472719e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 472819e12277SJanosch Frank return -EINVAL; 472919e12277SJanosch Frank 473041408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 473141408c28SThomas Huth tmpbuf = vmalloc(mop->size); 473241408c28SThomas Huth if (!tmpbuf) 473341408c28SThomas Huth return -ENOMEM; 473441408c28SThomas Huth } 473541408c28SThomas Huth 473641408c28SThomas Huth switch (mop->op) { 473741408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 473841408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 473992c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 474092c96321SDavid Hildenbrand mop->size, GACC_FETCH); 474141408c28SThomas Huth break; 474241408c28SThomas Huth } 474341408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 474441408c28SThomas Huth if (r == 0) { 474541408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 474641408c28SThomas Huth r = -EFAULT; 474741408c28SThomas Huth } 474841408c28SThomas Huth break; 474941408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 475041408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 475192c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 475292c96321SDavid Hildenbrand mop->size, GACC_STORE); 475341408c28SThomas Huth break; 475441408c28SThomas Huth } 475541408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 475641408c28SThomas Huth r = -EFAULT; 475741408c28SThomas Huth break; 475841408c28SThomas Huth } 475941408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 476041408c28SThomas Huth break; 476141408c28SThomas Huth } 476241408c28SThomas Huth 476341408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 476441408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 476541408c28SThomas Huth 476641408c28SThomas Huth vfree(tmpbuf); 476741408c28SThomas Huth return r; 476841408c28SThomas Huth } 476941408c28SThomas Huth 477019e12277SJanosch Frank static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu, 477119e12277SJanosch Frank struct kvm_s390_mem_op *mop) 477219e12277SJanosch Frank { 477319e12277SJanosch Frank int r, srcu_idx; 477419e12277SJanosch Frank 477519e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 477619e12277SJanosch Frank 477719e12277SJanosch Frank switch (mop->op) { 477819e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 477919e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 478019e12277SJanosch Frank r = kvm_s390_guest_mem_op(vcpu, mop); 478119e12277SJanosch Frank break; 478219e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 478319e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 478419e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 478519e12277SJanosch Frank r = kvm_s390_guest_sida_op(vcpu, mop); 478619e12277SJanosch Frank break; 478719e12277SJanosch Frank default: 478819e12277SJanosch Frank r = -EINVAL; 478919e12277SJanosch Frank } 479019e12277SJanosch Frank 479119e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 479219e12277SJanosch Frank return r; 479319e12277SJanosch Frank } 479419e12277SJanosch Frank 47955cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4796b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4797b0c632dbSHeiko Carstens { 4798b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4799b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4800b0c632dbSHeiko Carstens 480193736624SAvi Kivity switch (ioctl) { 480247b43c52SJens Freimann case KVM_S390_IRQ: { 480347b43c52SJens Freimann struct kvm_s390_irq s390irq; 480447b43c52SJens Freimann 480547b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 48069b062471SChristoffer Dall return -EFAULT; 48079b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 480847b43c52SJens Freimann } 480993736624SAvi Kivity case KVM_S390_INTERRUPT: { 4810ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 481153936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 4812ba5c1e9bSCarsten Otte 4813ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 48149b062471SChristoffer Dall return -EFAULT; 4815383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4816383d0b05SJens Freimann return -EINVAL; 48179b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4818ba5c1e9bSCarsten Otte } 48199b062471SChristoffer Dall } 48205cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 48215cb0944cSPaolo Bonzini } 48225cb0944cSPaolo Bonzini 48235cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 48245cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 48255cb0944cSPaolo Bonzini { 48265cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 48275cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 48285cb0944cSPaolo Bonzini int idx; 48295cb0944cSPaolo Bonzini long r; 48308a8378faSJanosch Frank u16 rc, rrc; 48319b062471SChristoffer Dall 48329b062471SChristoffer Dall vcpu_load(vcpu); 48339b062471SChristoffer Dall 48349b062471SChristoffer Dall switch (ioctl) { 4835b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4836800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 483755680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 4838800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4839bc923cc9SAvi Kivity break; 4840b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4841b0c632dbSHeiko Carstens psw_t psw; 4842b0c632dbSHeiko Carstens 4843bc923cc9SAvi Kivity r = -EFAULT; 4844b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4845bc923cc9SAvi Kivity break; 4846bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4847bc923cc9SAvi Kivity break; 4848b0c632dbSHeiko Carstens } 48497de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 48507de3f142SJanosch Frank r = 0; 48517de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 48528a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 48538a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 48548a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 48558a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 48568a8378faSJanosch Frank rc, rrc); 48578a8378faSJanosch Frank } 48587de3f142SJanosch Frank break; 4859b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 48607de3f142SJanosch Frank r = 0; 48617de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 48628a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 48638a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 48648a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 48658a8378faSJanosch Frank &rc, &rrc); 48668a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 48678a8378faSJanosch Frank rc, rrc); 48688a8378faSJanosch Frank } 48697de3f142SJanosch Frank break; 48707de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 48717de3f142SJanosch Frank r = 0; 48727de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 48738a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 48748a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 48758a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 48768a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 48778a8378faSJanosch Frank rc, rrc); 48788a8378faSJanosch Frank } 4879bc923cc9SAvi Kivity break; 488014eebd91SCarsten Otte case KVM_SET_ONE_REG: 488114eebd91SCarsten Otte case KVM_GET_ONE_REG: { 488214eebd91SCarsten Otte struct kvm_one_reg reg; 488368cf7b1fSJanosch Frank r = -EINVAL; 488468cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 488568cf7b1fSJanosch Frank break; 488614eebd91SCarsten Otte r = -EFAULT; 488714eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 488814eebd91SCarsten Otte break; 488914eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 489014eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 489114eebd91SCarsten Otte else 489214eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 489314eebd91SCarsten Otte break; 489414eebd91SCarsten Otte } 489527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 489627e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 489727e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 489827e0393fSCarsten Otte 489927e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 490027e0393fSCarsten Otte r = -EFAULT; 490127e0393fSCarsten Otte break; 490227e0393fSCarsten Otte } 490327e0393fSCarsten Otte 490427e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 490527e0393fSCarsten Otte r = -EINVAL; 490627e0393fSCarsten Otte break; 490727e0393fSCarsten Otte } 490827e0393fSCarsten Otte 490927e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 491027e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 491127e0393fSCarsten Otte break; 491227e0393fSCarsten Otte } 491327e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 491427e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 491527e0393fSCarsten Otte 491627e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 491727e0393fSCarsten Otte r = -EFAULT; 491827e0393fSCarsten Otte break; 491927e0393fSCarsten Otte } 492027e0393fSCarsten Otte 492127e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 492227e0393fSCarsten Otte r = -EINVAL; 492327e0393fSCarsten Otte break; 492427e0393fSCarsten Otte } 492527e0393fSCarsten Otte 492627e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 492727e0393fSCarsten Otte ucasmap.length); 492827e0393fSCarsten Otte break; 492927e0393fSCarsten Otte } 493027e0393fSCarsten Otte #endif 4931ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 4932527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 4933ccc7910fSCarsten Otte break; 4934ccc7910fSCarsten Otte } 4935d6712df9SCornelia Huck case KVM_ENABLE_CAP: 4936d6712df9SCornelia Huck { 4937d6712df9SCornelia Huck struct kvm_enable_cap cap; 4938d6712df9SCornelia Huck r = -EFAULT; 4939d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 4940d6712df9SCornelia Huck break; 4941d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 4942d6712df9SCornelia Huck break; 4943d6712df9SCornelia Huck } 494441408c28SThomas Huth case KVM_S390_MEM_OP: { 494541408c28SThomas Huth struct kvm_s390_mem_op mem_op; 494641408c28SThomas Huth 494741408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 494819e12277SJanosch Frank r = kvm_s390_guest_memsida_op(vcpu, &mem_op); 494941408c28SThomas Huth else 495041408c28SThomas Huth r = -EFAULT; 495141408c28SThomas Huth break; 495241408c28SThomas Huth } 4953816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 4954816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4955816c7667SJens Freimann 4956816c7667SJens Freimann r = -EFAULT; 4957816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4958816c7667SJens Freimann break; 4959816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 4960816c7667SJens Freimann irq_state.len == 0 || 4961816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 4962816c7667SJens Freimann r = -EINVAL; 4963816c7667SJens Freimann break; 4964816c7667SJens Freimann } 4965bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4966816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 4967816c7667SJens Freimann (void __user *) irq_state.buf, 4968816c7667SJens Freimann irq_state.len); 4969816c7667SJens Freimann break; 4970816c7667SJens Freimann } 4971816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 4972816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4973816c7667SJens Freimann 4974816c7667SJens Freimann r = -EFAULT; 4975816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4976816c7667SJens Freimann break; 4977816c7667SJens Freimann if (irq_state.len == 0) { 4978816c7667SJens Freimann r = -EINVAL; 4979816c7667SJens Freimann break; 4980816c7667SJens Freimann } 4981bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4982816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 4983816c7667SJens Freimann (__u8 __user *) irq_state.buf, 4984816c7667SJens Freimann irq_state.len); 4985816c7667SJens Freimann break; 4986816c7667SJens Freimann } 4987b0c632dbSHeiko Carstens default: 49883e6afcf1SCarsten Otte r = -ENOTTY; 4989b0c632dbSHeiko Carstens } 49909b062471SChristoffer Dall 49919b062471SChristoffer Dall vcpu_put(vcpu); 4992bc923cc9SAvi Kivity return r; 4993b0c632dbSHeiko Carstens } 4994b0c632dbSHeiko Carstens 49951499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 49965b1c1493SCarsten Otte { 49975b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 49985b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 49995b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 50005b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 50015b1c1493SCarsten Otte get_page(vmf->page); 50025b1c1493SCarsten Otte return 0; 50035b1c1493SCarsten Otte } 50045b1c1493SCarsten Otte #endif 50055b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 50065b1c1493SCarsten Otte } 50075b1c1493SCarsten Otte 5008b0c632dbSHeiko Carstens /* Section: memory related */ 5009f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5010537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5011537a17b3SSean Christopherson struct kvm_memory_slot *new, 50127b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5013b0c632dbSHeiko Carstens { 5014*ec5c8697SSean Christopherson gpa_t size; 5015*ec5c8697SSean Christopherson 5016*ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5017*ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5018*ec5c8697SSean Christopherson return -EINVAL; 5019*ec5c8697SSean Christopherson 5020*ec5c8697SSean Christopherson if (change == KVM_MR_DELETE || change == KVM_MR_FLAGS_ONLY) 5021*ec5c8697SSean Christopherson return 0; 5022cf5b4869SSean Christopherson 5023dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 5024dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 5025dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 5026dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 5027b0c632dbSHeiko Carstens 5028cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5029b0c632dbSHeiko Carstens return -EINVAL; 5030b0c632dbSHeiko Carstens 5031*ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5032cf5b4869SSean Christopherson if (size & 0xffffful) 5033b0c632dbSHeiko Carstens return -EINVAL; 5034b0c632dbSHeiko Carstens 5035cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5036a3a92c31SDominik Dingel return -EINVAL; 5037a3a92c31SDominik Dingel 5038f7784b8eSMarcelo Tosatti return 0; 5039f7784b8eSMarcelo Tosatti } 5040f7784b8eSMarcelo Tosatti 5041f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 50429d4c197cSSean Christopherson struct kvm_memory_slot *old, 5043f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 50448482644aSTakuya Yoshikawa enum kvm_mr_change change) 5045f7784b8eSMarcelo Tosatti { 504619ec166cSChristian Borntraeger int rc = 0; 5047f7784b8eSMarcelo Tosatti 504819ec166cSChristian Borntraeger switch (change) { 504919ec166cSChristian Borntraeger case KVM_MR_DELETE: 505019ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 505119ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 505219ec166cSChristian Borntraeger break; 505319ec166cSChristian Borntraeger case KVM_MR_MOVE: 505419ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 505519ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 505619ec166cSChristian Borntraeger if (rc) 505719ec166cSChristian Borntraeger break; 50583b684a42SJoe Perches fallthrough; 505919ec166cSChristian Borntraeger case KVM_MR_CREATE: 5060cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5061cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5062cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 506319ec166cSChristian Borntraeger break; 506419ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 506519ec166cSChristian Borntraeger break; 506619ec166cSChristian Borntraeger default: 506719ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 506819ec166cSChristian Borntraeger } 5069598841caSCarsten Otte if (rc) 5070ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5071598841caSCarsten Otte return; 5072b0c632dbSHeiko Carstens } 5073b0c632dbSHeiko Carstens 507460a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 507560a37709SAlexander Yarygin { 507660a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 507760a37709SAlexander Yarygin 507860a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 507960a37709SAlexander Yarygin } 508060a37709SAlexander Yarygin 50813491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 50823491caf2SChristian Borntraeger { 50833491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 50843491caf2SChristian Borntraeger } 50853491caf2SChristian Borntraeger 5086b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5087b0c632dbSHeiko Carstens { 508860a37709SAlexander Yarygin int i; 508960a37709SAlexander Yarygin 509007197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 50918d43d570SMichael Mueller pr_info("SIE is not available\n"); 509207197fd0SDavid Hildenbrand return -ENODEV; 509307197fd0SDavid Hildenbrand } 509407197fd0SDavid Hildenbrand 5095a4499382SJanosch Frank if (nested && hpage) { 50968d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5097a4499382SJanosch Frank return -EINVAL; 5098a4499382SJanosch Frank } 5099a4499382SJanosch Frank 510060a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5101c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 510217e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 510360a37709SAlexander Yarygin 51049d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5105b0c632dbSHeiko Carstens } 5106b0c632dbSHeiko Carstens 5107b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5108b0c632dbSHeiko Carstens { 5109b0c632dbSHeiko Carstens kvm_exit(); 5110b0c632dbSHeiko Carstens } 5111b0c632dbSHeiko Carstens 5112b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5113b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5114566af940SCornelia Huck 5115566af940SCornelia Huck /* 5116566af940SCornelia Huck * Enable autoloading of the kvm module. 5117566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5118566af940SCornelia Huck * since x86 takes a different approach. 5119566af940SCornelia Huck */ 5120566af940SCornelia Huck #include <linux/miscdevice.h> 5121566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5122566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5123