1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2b0c632dbSHeiko Carstens /* 3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x) 4b0c632dbSHeiko Carstens * 53e6c5568SJanosch Frank * Copyright IBM Corp. 2008, 2020 6b0c632dbSHeiko Carstens * 7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 9628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1015f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 11b0c632dbSHeiko Carstens */ 12b0c632dbSHeiko Carstens 137aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 147aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 157aedd9d4SMichael Mueller 16b0c632dbSHeiko Carstens #include <linux/compiler.h> 17b0c632dbSHeiko Carstens #include <linux/err.h> 18b0c632dbSHeiko Carstens #include <linux/fs.h> 19ca872302SChristian Borntraeger #include <linux/hrtimer.h> 20b0c632dbSHeiko Carstens #include <linux/init.h> 21b0c632dbSHeiko Carstens #include <linux/kvm.h> 22b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 23b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 24b0c632dbSHeiko Carstens #include <linux/module.h> 25d3217967SPaul Gortmaker #include <linux/moduleparam.h> 26a374e892STony Krowiak #include <linux/random.h> 27b0c632dbSHeiko Carstens #include <linux/slab.h> 28ba5c1e9bSCarsten Otte #include <linux/timer.h> 2941408c28SThomas Huth #include <linux/vmalloc.h> 3015c9705fSDavid Hildenbrand #include <linux/bitmap.h> 31174cd4b1SIngo Molnar #include <linux/sched/signal.h> 32190df4a2SClaudio Imbrenda #include <linux/string.h> 3365fddcfcSMike Rapoport #include <linux/pgtable.h> 34ca2fd060SClaudio Imbrenda #include <linux/mmu_notifier.h> 35174cd4b1SIngo Molnar 36cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 37b0c632dbSHeiko Carstens #include <asm/lowcore.h> 38fd5ada04SMartin Schwidefsky #include <asm/stp.h> 391e133ab2SMartin Schwidefsky #include <asm/gmap.h> 40f5daba1dSHeiko Carstens #include <asm/nmi.h> 41a0616cdeSDavid Howells #include <asm/switch_to.h> 426d3da241SJens Freimann #include <asm/isc.h> 431526bf9cSChristian Borntraeger #include <asm/sclp.h> 440a763c78SDavid Hildenbrand #include <asm/cpacf.h> 45221bb8a4SLinus Torvalds #include <asm/timex.h> 46e585b24aSTony Krowiak #include <asm/ap.h> 4729b40f10SJanosch Frank #include <asm/uv.h> 4856e62a73SSven Schnelle #include <asm/fpu/api.h> 498f2abe6aSChristian Borntraeger #include "kvm-s390.h" 50b0c632dbSHeiko Carstens #include "gaccess.h" 5198b1d33dSMatthew Rosato #include "pci.h" 52b0c632dbSHeiko Carstens 535786fffaSCornelia Huck #define CREATE_TRACE_POINTS 545786fffaSCornelia Huck #include "trace.h" 55ade38c31SCornelia Huck #include "trace-s390.h" 565786fffaSCornelia Huck 5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 58816c7667SJens Freimann #define LOCAL_IRQS 32 59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 60816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 6141408c28SThomas Huth 62fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = { 63fcfe1baeSJing Zhang KVM_GENERIC_VM_STATS(), 64fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_io), 65fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_float_mchk), 66fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_pfault_done), 67fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_service_signal), 6873f91b00SMatthew Rosato STATS_DESC_COUNTER(VM, inject_virtio), 6973f91b00SMatthew Rosato STATS_DESC_COUNTER(VM, aen_forward) 70fcfe1baeSJing Zhang }; 71fcfe1baeSJing Zhang 72fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = { 73fcfe1baeSJing Zhang .name_size = KVM_STATS_NAME_SIZE, 74fcfe1baeSJing Zhang .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), 75fcfe1baeSJing Zhang .id_offset = sizeof(struct kvm_stats_header), 76fcfe1baeSJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 77fcfe1baeSJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 78fcfe1baeSJing Zhang sizeof(kvm_vm_stats_desc), 79fcfe1baeSJing Zhang }; 80fcfe1baeSJing Zhang 81ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { 82ce55c049SJing Zhang KVM_GENERIC_VCPU_STATS(), 83ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_userspace), 84ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_null), 85ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_request), 86ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_io_request), 87ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_interrupt), 88ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_stop_request), 89ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_validity), 90ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instruction), 91ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_pei), 92ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, halt_no_poll_steal), 93ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctl), 94ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctlg), 95ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctl), 96ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctg), 97ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_program_interruption), 98ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instr_and_program), 99ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_operation_exception), 100ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_ckc), 101ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_cputm), 102ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_external_call), 103ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_emergency_signal), 104ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_service_signal), 105ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_virtio), 106ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_stop_signal), 107ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_prefix_signal), 108ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_restart_signal), 109ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_program), 110ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_io), 111ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_machine_check), 112ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_wait_state), 113ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_ckc), 114ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_cputm), 115ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_external_call), 116ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_emergency_signal), 117ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_mchk), 118ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_pfault_init), 119ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_program), 120ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_restart), 121ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_set_prefix), 122ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_stop_signal), 123ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_epsw), 124ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_gs), 125ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_io_other), 126ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpsw), 127ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpswe), 128ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_pfmf), 129ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ptff), 130ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sck), 131ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sckpf), 132ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stidp), 133ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_spx), 134ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stpx), 135ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stap), 136ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_iske), 137ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ri), 138ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_rrbe), 139ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sske), 140ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock), 141ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stsi), 142ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stfl), 143ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tb), 144ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tpi), 145ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tprot), 146ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tsch), 147ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sie), 148ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_essa), 149ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sthyi), 150ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense), 151ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running), 152ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call), 153ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency), 154ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency), 155ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_start), 156ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop), 157ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status), 158ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status), 159ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status), 160ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_arch), 161ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix), 162ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_restart), 163ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset), 164ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset), 165ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown), 166bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_10), 167bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_44), 168bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c), 169bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_ignored), 170bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_forward), 171bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_258), 172bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_308), 173bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_500), 174bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_other), 175ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, pfault_sync) 176ce55c049SJing Zhang }; 177ce55c049SJing Zhang 178ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = { 179ce55c049SJing Zhang .name_size = KVM_STATS_NAME_SIZE, 180ce55c049SJing Zhang .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc), 181ce55c049SJing Zhang .id_offset = sizeof(struct kvm_stats_header), 182ce55c049SJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 183ce55c049SJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 184ce55c049SJing Zhang sizeof(kvm_vcpu_stats_desc), 185ce55c049SJing Zhang }; 186ce55c049SJing Zhang 187a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 188a411edf1SDavid Hildenbrand static int nested; 189a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 190a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 191a411edf1SDavid Hildenbrand 192a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 193a4499382SJanosch Frank static int hpage; 194a4499382SJanosch Frank module_param(hpage, int, 0444); 195a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 196b0c632dbSHeiko Carstens 1978b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */ 1988b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10; 1998b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644); 200b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); 2018b905d28SChristian Borntraeger 202cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */ 203cc674ef2SMichael Mueller static bool use_gisa = true; 204cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644); 205cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it."); 206cc674ef2SMichael Mueller 20787e28a15SPierre Morel /* maximum diag9c forwarding per second */ 20887e28a15SPierre Morel unsigned int diag9c_forwarding_hz; 20987e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644); 21087e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off"); 21187e28a15SPierre Morel 212c3b9e3e1SChristian Borntraeger /* 213cc726886SClaudio Imbrenda * allow asynchronous deinit for protected guests; enable by default since 214cc726886SClaudio Imbrenda * the feature is opt-in anyway 215cc726886SClaudio Imbrenda */ 216cc726886SClaudio Imbrenda static int async_destroy = 1; 217cc726886SClaudio Imbrenda module_param(async_destroy, int, 0444); 218cc726886SClaudio Imbrenda MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests"); 219fb491d55SClaudio Imbrenda 220c3b9e3e1SChristian Borntraeger /* 221c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 222c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 223c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 224c3b9e3e1SChristian Borntraeger */ 225c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 226c3b9e3e1SChristian Borntraeger 227c3b9e3e1SChristian Borntraeger /* 228c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 229c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 230c3b9e3e1SChristian Borntraeger */ 231c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 232c3b9e3e1SChristian Borntraeger /* 233c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 234c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 235c3b9e3e1SChristian Borntraeger */ 236c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 237c3b9e3e1SChristian Borntraeger 238c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 23978c4b59fSMichael Mueller { 240c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 241c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 242c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 24317e89e13SSven Schnelle sizeof(stfle_fac_list)); 244c3b9e3e1SChristian Borntraeger 245c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 24678c4b59fSMichael Mueller } 24778c4b59fSMichael Mueller 24815c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 24915c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2500a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2510a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 25215c9705fSDavid Hildenbrand 2539d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 254a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 25578f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2563e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv; 2579d8d5786SMichael Mueller 258b0c632dbSHeiko Carstens /* Section: not file related */ 25929b40f10SJanosch Frank /* forward declarations */ 260414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 261414d3b07SMartin Schwidefsky unsigned long end); 26229b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm); 2632c70fe44SChristian Borntraeger 2641575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2651575767eSDavid Hildenbrand { 2661575767eSDavid Hildenbrand u8 delta_idx = 0; 2671575767eSDavid Hildenbrand 2681575767eSDavid Hildenbrand /* 2691575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2701575767eSDavid Hildenbrand * -delta to the epoch. 2711575767eSDavid Hildenbrand */ 2721575767eSDavid Hildenbrand delta = -delta; 2731575767eSDavid Hildenbrand 2741575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2751575767eSDavid Hildenbrand if ((s64)delta < 0) 2761575767eSDavid Hildenbrand delta_idx = -1; 2771575767eSDavid Hildenbrand 2781575767eSDavid Hildenbrand scb->epoch += delta; 2791575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2801575767eSDavid Hildenbrand scb->epdx += delta_idx; 2811575767eSDavid Hildenbrand if (scb->epoch < delta) 2821575767eSDavid Hildenbrand scb->epdx += 1; 2831575767eSDavid Hildenbrand } 2841575767eSDavid Hildenbrand } 2851575767eSDavid Hildenbrand 286fdf03650SFan Zhang /* 287fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 288fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 289fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 290fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 291fdf03650SFan Zhang */ 292fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 293fdf03650SFan Zhang void *v) 294fdf03650SFan Zhang { 295fdf03650SFan Zhang struct kvm *kvm; 296fdf03650SFan Zhang struct kvm_vcpu *vcpu; 29746808a4cSMarc Zyngier unsigned long i; 298fdf03650SFan Zhang unsigned long long *delta = v; 299fdf03650SFan Zhang 300fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 301fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 3021575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 3031575767eSDavid Hildenbrand if (i == 0) { 3041575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 3051575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 3061575767eSDavid Hildenbrand } 307db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 308db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 30991473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 3101575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 3111575767eSDavid Hildenbrand *delta); 312fdf03650SFan Zhang } 313fdf03650SFan Zhang } 314fdf03650SFan Zhang return NOTIFY_OK; 315fdf03650SFan Zhang } 316fdf03650SFan Zhang 317fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 318fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 319fdf03650SFan Zhang }; 320fdf03650SFan Zhang 32122be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 32222be5a13SDavid Hildenbrand { 32322be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 32422be5a13SDavid Hildenbrand } 32522be5a13SDavid Hildenbrand 3260a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3270a763c78SDavid Hildenbrand { 3284fa3b91bSHeiko Carstens unsigned long function = (unsigned long)nr | 0x100; 329d051ae53SHeiko Carstens int cc; 3300a763c78SDavid Hildenbrand 3310a763c78SDavid Hildenbrand asm volatile( 3324fa3b91bSHeiko Carstens " lgr 0,%[function]\n" 3330a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3340a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3350a763c78SDavid Hildenbrand " ipm %0\n" 3360a763c78SDavid Hildenbrand " srl %0,28\n" 3370a763c78SDavid Hildenbrand : "=d" (cc) 3384fa3b91bSHeiko Carstens : [function] "d" (function) 3394fa3b91bSHeiko Carstens : "cc", "0"); 3400a763c78SDavid Hildenbrand return cc == 0; 3410a763c78SDavid Hildenbrand } 3420a763c78SDavid Hildenbrand 343d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 344d6681397SChristian Borntraeger { 345d6681397SChristian Borntraeger asm volatile( 3464fa3b91bSHeiko Carstens " lghi 0,0\n" 3474fa3b91bSHeiko Carstens " lgr 1,%[query]\n" 3484fa3b91bSHeiko Carstens /* Parameter registers are ignored */ 349d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 350b1c41ac3SHeiko Carstens : 3514fa3b91bSHeiko Carstens : [query] "d" ((unsigned long)query), [opc] "i" (opcode) 3524fa3b91bSHeiko Carstens : "cc", "memory", "0", "1"); 353d6681397SChristian Borntraeger } 354d6681397SChristian Borntraeger 355173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3564f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 357173aec2dSChristian Borntraeger 3586c30cd2eSSean Christopherson static void __init kvm_s390_cpu_feat_init(void) 35922be5a13SDavid Hildenbrand { 3600a763c78SDavid Hildenbrand int i; 3610a763c78SDavid Hildenbrand 3620a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3630a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3640a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3650a763c78SDavid Hildenbrand } 3660a763c78SDavid Hildenbrand 3670a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 368221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 369221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 370221bb8a4SLinus Torvalds PTFF_QAF); 3710a763c78SDavid Hildenbrand 3720a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 37369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 37469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 37569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 37669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 37769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 37869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 37969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 38069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 38169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 38269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 3830a763c78SDavid Hildenbrand } 3840a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 38569c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 38669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 3870a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 38869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 38969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 39069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 39169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 39269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 39369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 39469c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 39569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 3960a763c78SDavid Hildenbrand } 3970a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 398985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 39969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 4000a763c78SDavid Hildenbrand 401e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 402e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 403e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 404e000b8e0SJason J. Herne 40513209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 40613209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 40713209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 40813209ad0SChristian Borntraeger 409173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 410173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 411173aec2dSChristian Borntraeger 4124f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4134f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4144f45b90eSChristian Borntraeger 41522be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 41622be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 417a3508fbeSDavid Hildenbrand /* 418a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 419a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 420a3508fbeSDavid Hildenbrand */ 421a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 422a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 423a3508fbeSDavid Hildenbrand return; 424a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 42519c439b5SDavid Hildenbrand if (sclp.has_64bscao) 42619c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4270615a326SDavid Hildenbrand if (sclp.has_siif) 4280615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 42977d18f6dSDavid Hildenbrand if (sclp.has_gpere) 43077d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 431a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 432a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4335630a8e8SDavid Hildenbrand if (sclp.has_ib) 4345630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 43513ee3f67SDavid Hildenbrand if (sclp.has_cei) 43613ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4377fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4387fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 439730cd632SFarhan Ali if (sclp.has_kss) 440730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4415d3876a8SDavid Hildenbrand /* 4425d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4435d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4445d3876a8SDavid Hildenbrand * instead of the real storage key. 4455d3876a8SDavid Hildenbrand * 4465d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4475d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4485d3876a8SDavid Hildenbrand * 4495d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4505d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4515d3876a8SDavid Hildenbrand * 4525d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4535d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4545d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4555d3876a8SDavid Hildenbrand * 4565d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4575d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4585d3876a8SDavid Hildenbrand */ 45922be5a13SDavid Hildenbrand } 46022be5a13SDavid Hildenbrand 4616c30cd2eSSean Christopherson static int __init __kvm_s390_init(void) 462b0c632dbSHeiko Carstens { 463f76f6371SJanosch Frank int rc = -ENOMEM; 464308c3e66SMichael Mueller 46578f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 46678f26131SChristian Borntraeger if (!kvm_s390_dbf) 46778f26131SChristian Borntraeger return -ENOMEM; 46878f26131SChristian Borntraeger 4693e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); 4703e6c5568SJanosch Frank if (!kvm_s390_dbf_uv) 471b801ef42SSean Christopherson goto err_kvm_uv; 4723e6c5568SJanosch Frank 4733e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || 4743e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) 475b801ef42SSean Christopherson goto err_debug_view; 47678f26131SChristian Borntraeger 47722be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 47822be5a13SDavid Hildenbrand 47984877d93SCornelia Huck /* Register floating interrupt controller interface. */ 480308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 481308c3e66SMichael Mueller if (rc) { 4828d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 483b801ef42SSean Christopherson goto err_flic; 484308c3e66SMichael Mueller } 485b1d1e76eSMichael Mueller 486189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 48798b1d33dSMatthew Rosato rc = kvm_s390_pci_init(); 48898b1d33dSMatthew Rosato if (rc) { 48998b1d33dSMatthew Rosato pr_err("Unable to allocate AIFT for PCI\n"); 490b801ef42SSean Christopherson goto err_pci; 49198b1d33dSMatthew Rosato } 49298b1d33dSMatthew Rosato } 49398b1d33dSMatthew Rosato 494b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 495b1d1e76eSMichael Mueller if (rc) 496b801ef42SSean Christopherson goto err_gib; 497b1d1e76eSMichael Mueller 498e43f5762SSean Christopherson gmap_notifier.notifier_call = kvm_gmap_notifier; 499e43f5762SSean Christopherson gmap_register_pte_notifier(&gmap_notifier); 500e43f5762SSean Christopherson vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 501e43f5762SSean Christopherson gmap_register_pte_notifier(&vsie_gmap_notifier); 502e43f5762SSean Christopherson atomic_notifier_chain_register(&s390_epoch_delta_notifier, 503e43f5762SSean Christopherson &kvm_clock_notifier); 504e43f5762SSean Christopherson 505308c3e66SMichael Mueller return 0; 506308c3e66SMichael Mueller 507b801ef42SSean Christopherson err_gib: 508b801ef42SSean Christopherson if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 509b801ef42SSean Christopherson kvm_s390_pci_exit(); 510b801ef42SSean Christopherson err_pci: 511b801ef42SSean Christopherson err_flic: 512b801ef42SSean Christopherson err_debug_view: 513b801ef42SSean Christopherson debug_unregister(kvm_s390_dbf_uv); 514b801ef42SSean Christopherson err_kvm_uv: 515b801ef42SSean Christopherson debug_unregister(kvm_s390_dbf); 516308c3e66SMichael Mueller return rc; 517b0c632dbSHeiko Carstens } 518b0c632dbSHeiko Carstens 519b8449265SSean Christopherson static void __kvm_s390_exit(void) 52078f26131SChristian Borntraeger { 521e43f5762SSean Christopherson gmap_unregister_pte_notifier(&gmap_notifier); 522e43f5762SSean Christopherson gmap_unregister_pte_notifier(&vsie_gmap_notifier); 523e43f5762SSean Christopherson atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 524e43f5762SSean Christopherson &kvm_clock_notifier); 525e43f5762SSean Christopherson 5261282c21eSMichael Mueller kvm_s390_gib_destroy(); 527189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 52898b1d33dSMatthew Rosato kvm_s390_pci_exit(); 52978f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 5303e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv); 53178f26131SChristian Borntraeger } 53278f26131SChristian Borntraeger 533b0c632dbSHeiko Carstens /* Section: device related */ 534b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 535b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 536b0c632dbSHeiko Carstens { 537b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 538b0c632dbSHeiko Carstens return s390_enable_sie(); 539b0c632dbSHeiko Carstens return -EINVAL; 540b0c632dbSHeiko Carstens } 541b0c632dbSHeiko Carstens 542784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 543b0c632dbSHeiko Carstens { 544d7b0b5ebSCarsten Otte int r; 545d7b0b5ebSCarsten Otte 5462bd0ac4eSCarsten Otte switch (ext) { 547d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 548b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 54952e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5501efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5511efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5521efd0f59SCarsten Otte #endif 5533c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 55460b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 55514eebd91SCarsten Otte case KVM_CAP_ONE_REG: 556d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 557fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 55810ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 559c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 56078599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 561f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5626352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 563460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 56447b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5652444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 566e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 56730ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 568816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5696502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5704036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 57147a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 572da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 5737de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS: 574b9b2782cSPeter Xu case KVM_CAP_SET_GUEST_DEBUG: 57523a60f83SCollin Walling case KVM_CAP_S390_DIAG318: 576*52882b9cSAlexey Kardashevskiy case KVM_CAP_IRQFD_RESAMPLE: 577d7b0b5ebSCarsten Otte r = 1; 578d7b0b5ebSCarsten Otte break; 579a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 580a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK; 581a43b80b7SMaxim Levitsky break; 582a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 583a4499382SJanosch Frank r = 0; 58440ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 585a4499382SJanosch Frank r = 1; 586a4499382SJanosch Frank break; 58741408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 58841408c28SThomas Huth r = MEM_OP_MAX_SIZE; 58941408c28SThomas Huth break; 5903fd49805SJanis Schoetterl-Glausch case KVM_CAP_S390_MEM_OP_EXTENSION: 5913fd49805SJanis Schoetterl-Glausch /* 5923fd49805SJanis Schoetterl-Glausch * Flag bits indicating which extensions are supported. 5933fd49805SJanis Schoetterl-Glausch * If r > 0, the base extension must also be supported/indicated, 5943fd49805SJanis Schoetterl-Glausch * in order to maintain backwards compatibility. 5953fd49805SJanis Schoetterl-Glausch */ 5963fd49805SJanis Schoetterl-Glausch r = KVM_S390_MEMOP_EXTENSION_CAP_BASE | 5973fd49805SJanis Schoetterl-Glausch KVM_S390_MEMOP_EXTENSION_CAP_CMPXCHG; 5983fd49805SJanis Schoetterl-Glausch break; 599e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 600e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 601a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 60276a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 603a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 604a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 605a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 60676a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 60782cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS) 60882cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r); 609e726b1bdSChristian Borntraeger break; 6101526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 611abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 6121526bf9cSChristian Borntraeger break; 61368c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 61468c55750SEric Farman r = MACHINE_HAS_VX; 61568c55750SEric Farman break; 616c6e5f166SFan Zhang case KVM_CAP_S390_RI: 617c6e5f166SFan Zhang r = test_facility(64); 618c6e5f166SFan Zhang break; 6194e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 6204e0b1ab7SFan Zhang r = test_facility(133); 6214e0b1ab7SFan Zhang break; 62235b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 62335b3fde6SChristian Borntraeger r = test_facility(82); 62435b3fde6SChristian Borntraeger break; 6258c516b25SClaudio Imbrenda case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE: 6268c516b25SClaudio Imbrenda r = async_destroy && is_prot_virt_host(); 6278c516b25SClaudio Imbrenda break; 62813da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED: 62913da9ae1SChristian Borntraeger r = is_prot_virt_host(); 63013da9ae1SChristian Borntraeger break; 631e9bf3acbSJanosch Frank case KVM_CAP_S390_PROTECTED_DUMP: { 632e9bf3acbSJanosch Frank u64 pv_cmds_dump[] = { 633e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_INIT, 634e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE, 635e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CPU, 636e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_COMPLETE, 637e9bf3acbSJanosch Frank }; 638e9bf3acbSJanosch Frank int i; 639e9bf3acbSJanosch Frank 640e9bf3acbSJanosch Frank r = is_prot_virt_host(); 641e9bf3acbSJanosch Frank 642e9bf3acbSJanosch Frank for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) { 643e9bf3acbSJanosch Frank if (!test_bit_inv(pv_cmds_dump[i], 644e9bf3acbSJanosch Frank (unsigned long *)&uv_info.inst_calls_list)) { 645e9bf3acbSJanosch Frank r = 0; 646e9bf3acbSJanosch Frank break; 647e9bf3acbSJanosch Frank } 648e9bf3acbSJanosch Frank } 649e9bf3acbSJanosch Frank break; 650e9bf3acbSJanosch Frank } 651db1c875eSMatthew Rosato case KVM_CAP_S390_ZPCI_OP: 652db1c875eSMatthew Rosato r = kvm_s390_pci_interp_allowed(); 653db1c875eSMatthew Rosato break; 654f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 655f5ecfee9SPierre Morel r = test_facility(11); 656f5ecfee9SPierre Morel break; 6572bd0ac4eSCarsten Otte default: 658d7b0b5ebSCarsten Otte r = 0; 659b0c632dbSHeiko Carstens } 660d7b0b5ebSCarsten Otte return r; 6612bd0ac4eSCarsten Otte } 662b0c632dbSHeiko Carstens 6630dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 66415f36ebdSJason J. Herne { 6650959e168SJanosch Frank int i; 66615f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 6670959e168SJanosch Frank unsigned long gaddr, vmaddr; 66815f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 6690959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 67015f36ebdSJason J. Herne 6710959e168SJanosch Frank /* Loop over all guest segments */ 6720959e168SJanosch Frank cur_gfn = memslot->base_gfn; 67315f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 6740959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 6750959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 6760959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 6770959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 6780959e168SJanosch Frank continue; 67915f36ebdSJason J. Herne 6800959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6810959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6820959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6830959e168SJanosch Frank if (test_bit(i, bitmap)) 6840959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6850959e168SJanosch Frank } 6860959e168SJanosch Frank 6871763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6881763f8d0SChristian Borntraeger return; 68970c88a00SChristian Borntraeger cond_resched(); 69015f36ebdSJason J. Herne } 69115f36ebdSJason J. Herne } 69215f36ebdSJason J. Herne 693b0c632dbSHeiko Carstens /* Section: vm related */ 694a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 695a6e2f683SEugene (jno) Dvurechenski 696b0c632dbSHeiko Carstens /* 697b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 698b0c632dbSHeiko Carstens */ 699b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 700b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 701b0c632dbSHeiko Carstens { 70215f36ebdSJason J. Herne int r; 70315f36ebdSJason J. Herne unsigned long n; 70415f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 7052a49f61dSSean Christopherson int is_dirty; 70615f36ebdSJason J. Herne 707e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 708e1e8a962SJanosch Frank return -EINVAL; 709e1e8a962SJanosch Frank 71015f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 71115f36ebdSJason J. Herne 71215f36ebdSJason J. Herne r = -EINVAL; 71315f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 71415f36ebdSJason J. Herne goto out; 71515f36ebdSJason J. Herne 7162a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); 71715f36ebdSJason J. Herne if (r) 71815f36ebdSJason J. Herne goto out; 71915f36ebdSJason J. Herne 72015f36ebdSJason J. Herne /* Clear the dirty log */ 72115f36ebdSJason J. Herne if (is_dirty) { 72215f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 72315f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 72415f36ebdSJason J. Herne } 72515f36ebdSJason J. Herne r = 0; 72615f36ebdSJason J. Herne out: 72715f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 72815f36ebdSJason J. Herne return r; 729b0c632dbSHeiko Carstens } 730b0c632dbSHeiko Carstens 7316502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 7326502a34cSDavid Hildenbrand { 73346808a4cSMarc Zyngier unsigned long i; 7346502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 7356502a34cSDavid Hildenbrand 7366502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 7376502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 7386502a34cSDavid Hildenbrand } 7396502a34cSDavid Hildenbrand } 7406502a34cSDavid Hildenbrand 741e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 742d938dc55SCornelia Huck { 743d938dc55SCornelia Huck int r; 744d938dc55SCornelia Huck 745d938dc55SCornelia Huck if (cap->flags) 746d938dc55SCornelia Huck return -EINVAL; 747d938dc55SCornelia Huck 748d938dc55SCornelia Huck switch (cap->cap) { 74984223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 750c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 75184223598SCornelia Huck kvm->arch.use_irqchip = 1; 75284223598SCornelia Huck r = 0; 75384223598SCornelia Huck break; 7542444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 755c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 7562444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 7572444b352SDavid Hildenbrand r = 0; 7582444b352SDavid Hildenbrand break; 75968c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 7605967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 761a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 7625967c17bSDavid Hildenbrand r = -EBUSY; 7635967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 764c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 765c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 7662f87d942SGuenther Hutzl if (test_facility(134)) { 7672f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 7682f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 7692f87d942SGuenther Hutzl } 77053743aa7SMaxim Samoylov if (test_facility(135)) { 77153743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 77253743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 77353743aa7SMaxim Samoylov } 7747832e91cSChristian Borntraeger if (test_facility(148)) { 7757832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7767832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7777832e91cSChristian Borntraeger } 778d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 779d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 780d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 781d5cb6ab1SChristian Borntraeger } 7821f703d2cSChristian Borntraeger if (test_facility(192)) { 7831f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192); 7841f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192); 7851f703d2cSChristian Borntraeger } 78618280d8bSMichael Mueller r = 0; 78718280d8bSMichael Mueller } else 78818280d8bSMichael Mueller r = -EINVAL; 7895967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 790c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 791c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 79268c55750SEric Farman break; 793c6e5f166SFan Zhang case KVM_CAP_S390_RI: 794c6e5f166SFan Zhang r = -EINVAL; 795c6e5f166SFan Zhang mutex_lock(&kvm->lock); 796a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 797c6e5f166SFan Zhang r = -EBUSY; 798c6e5f166SFan Zhang } else if (test_facility(64)) { 799c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 800c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 801c6e5f166SFan Zhang r = 0; 802c6e5f166SFan Zhang } 803c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 804c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 805c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 806c6e5f166SFan Zhang break; 80747a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 80847a4693eSYi Min Zhao mutex_lock(&kvm->lock); 80947a4693eSYi Min Zhao if (kvm->created_vcpus) { 81047a4693eSYi Min Zhao r = -EBUSY; 81147a4693eSYi Min Zhao } else { 81247a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 81347a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 81447a4693eSYi Min Zhao r = 0; 81547a4693eSYi Min Zhao } 81647a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 81747a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 81847a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 81947a4693eSYi Min Zhao break; 8204e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 8214e0b1ab7SFan Zhang r = -EINVAL; 8224e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 823241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 8244e0b1ab7SFan Zhang r = -EBUSY; 8254e0b1ab7SFan Zhang } else if (test_facility(133)) { 8264e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 8274e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 8284e0b1ab7SFan Zhang r = 0; 8294e0b1ab7SFan Zhang } 8304e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 8314e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 8324e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 8334e0b1ab7SFan Zhang break; 834a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 835a4499382SJanosch Frank mutex_lock(&kvm->lock); 836a4499382SJanosch Frank if (kvm->created_vcpus) 837a4499382SJanosch Frank r = -EBUSY; 83840ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 839a4499382SJanosch Frank r = -EINVAL; 840a4499382SJanosch Frank else { 841a4499382SJanosch Frank r = 0; 842d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 843a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 844d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 845a4499382SJanosch Frank /* 846a4499382SJanosch Frank * We might have to create fake 4k page 847a4499382SJanosch Frank * tables. To avoid that the hardware works on 848a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 849a4499382SJanosch Frank */ 850a4499382SJanosch Frank kvm->arch.use_skf = 0; 851a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 852a4499382SJanosch Frank } 853a4499382SJanosch Frank mutex_unlock(&kvm->lock); 854a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 855a4499382SJanosch Frank r ? "(not available)" : "(success)"); 856a4499382SJanosch Frank break; 857e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 858c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 859e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 860e44fc8c9SEkaterina Tumanova r = 0; 861e44fc8c9SEkaterina Tumanova break; 8626502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 8636502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 8646502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 8656502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 8666502a34cSDavid Hildenbrand r = 0; 8676502a34cSDavid Hildenbrand break; 868f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 869f5ecfee9SPierre Morel r = -EINVAL; 870f5ecfee9SPierre Morel mutex_lock(&kvm->lock); 871f5ecfee9SPierre Morel if (kvm->created_vcpus) { 872f5ecfee9SPierre Morel r = -EBUSY; 873f5ecfee9SPierre Morel } else if (test_facility(11)) { 874f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 11); 875f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_list, 11); 876f5ecfee9SPierre Morel r = 0; 877f5ecfee9SPierre Morel } 878f5ecfee9SPierre Morel mutex_unlock(&kvm->lock); 879f5ecfee9SPierre Morel VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s", 880f5ecfee9SPierre Morel r ? "(not available)" : "(success)"); 881f5ecfee9SPierre Morel break; 882d938dc55SCornelia Huck default: 883d938dc55SCornelia Huck r = -EINVAL; 884d938dc55SCornelia Huck break; 885d938dc55SCornelia Huck } 886d938dc55SCornelia Huck return r; 887d938dc55SCornelia Huck } 888d938dc55SCornelia Huck 8898c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8908c0a7ce6SDominik Dingel { 8918c0a7ce6SDominik Dingel int ret; 8928c0a7ce6SDominik Dingel 8938c0a7ce6SDominik Dingel switch (attr->attr) { 8948c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8958c0a7ce6SDominik Dingel ret = 0; 896c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 897a3a92c31SDominik Dingel kvm->arch.mem_limit); 898a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8998c0a7ce6SDominik Dingel ret = -EFAULT; 9008c0a7ce6SDominik Dingel break; 9018c0a7ce6SDominik Dingel default: 9028c0a7ce6SDominik Dingel ret = -ENXIO; 9038c0a7ce6SDominik Dingel break; 9048c0a7ce6SDominik Dingel } 9058c0a7ce6SDominik Dingel return ret; 9068c0a7ce6SDominik Dingel } 9078c0a7ce6SDominik Dingel 9088c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 9094f718eabSDominik Dingel { 9104f718eabSDominik Dingel int ret; 9114f718eabSDominik Dingel unsigned int idx; 9124f718eabSDominik Dingel switch (attr->attr) { 9134f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 914f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 915c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 916e6db1d61SDominik Dingel break; 917e6db1d61SDominik Dingel 918c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 9194f718eabSDominik Dingel mutex_lock(&kvm->lock); 920a4499382SJanosch Frank if (kvm->created_vcpus) 921a4499382SJanosch Frank ret = -EBUSY; 922a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 923a4499382SJanosch Frank ret = -EINVAL; 924a4499382SJanosch Frank else { 9254f718eabSDominik Dingel kvm->arch.use_cmma = 1; 926c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 927c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 9284f718eabSDominik Dingel ret = 0; 9294f718eabSDominik Dingel } 9304f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9314f718eabSDominik Dingel break; 9324f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 933f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 934f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 935f9cbd9b0SDavid Hildenbrand break; 936c3489155SDominik Dingel ret = -EINVAL; 937c3489155SDominik Dingel if (!kvm->arch.use_cmma) 938c3489155SDominik Dingel break; 939c3489155SDominik Dingel 940c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 9414f718eabSDominik Dingel mutex_lock(&kvm->lock); 9424f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 943a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 9444f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 9454f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9464f718eabSDominik Dingel ret = 0; 9474f718eabSDominik Dingel break; 9488c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 9498c0a7ce6SDominik Dingel unsigned long new_limit; 9508c0a7ce6SDominik Dingel 9518c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 9528c0a7ce6SDominik Dingel return -EINVAL; 9538c0a7ce6SDominik Dingel 9548c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 9558c0a7ce6SDominik Dingel return -EFAULT; 9568c0a7ce6SDominik Dingel 957a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 958a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 9598c0a7ce6SDominik Dingel return -E2BIG; 9608c0a7ce6SDominik Dingel 961a3a92c31SDominik Dingel if (!new_limit) 962a3a92c31SDominik Dingel return -EINVAL; 963a3a92c31SDominik Dingel 9646ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 965a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 966a3a92c31SDominik Dingel new_limit -= 1; 967a3a92c31SDominik Dingel 9688c0a7ce6SDominik Dingel ret = -EBUSY; 9698c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 970a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 9716ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 9726ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 9738c0a7ce6SDominik Dingel 9748c0a7ce6SDominik Dingel if (!new) { 9758c0a7ce6SDominik Dingel ret = -ENOMEM; 9768c0a7ce6SDominik Dingel } else { 9776ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 9788c0a7ce6SDominik Dingel new->private = kvm; 9798c0a7ce6SDominik Dingel kvm->arch.gmap = new; 9808c0a7ce6SDominik Dingel ret = 0; 9818c0a7ce6SDominik Dingel } 9828c0a7ce6SDominik Dingel } 9838c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 984a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 985a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 986a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 9878c0a7ce6SDominik Dingel break; 9888c0a7ce6SDominik Dingel } 9894f718eabSDominik Dingel default: 9904f718eabSDominik Dingel ret = -ENXIO; 9914f718eabSDominik Dingel break; 9924f718eabSDominik Dingel } 9934f718eabSDominik Dingel return ret; 9944f718eabSDominik Dingel } 9954f718eabSDominik Dingel 996a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 997a374e892STony Krowiak 99820c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 999a374e892STony Krowiak { 1000a374e892STony Krowiak struct kvm_vcpu *vcpu; 100146808a4cSMarc Zyngier unsigned long i; 1002a374e892STony Krowiak 100320c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 100420c922f0STony Krowiak 10053194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 100620c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 10073194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 10083194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 10093194cdb7SDavid Hildenbrand } 101020c922f0STony Krowiak 101120c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 101220c922f0STony Krowiak } 101320c922f0STony Krowiak 101420c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 101520c922f0STony Krowiak { 1016a374e892STony Krowiak mutex_lock(&kvm->lock); 1017a374e892STony Krowiak switch (attr->attr) { 1018a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 10198e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10208e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 102137940fb0STony Krowiak return -EINVAL; 10228e41bd54SChristian Borntraeger } 1023a374e892STony Krowiak get_random_bytes( 1024a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1025a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1026a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 1027c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 1028a374e892STony Krowiak break; 1029a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 10308e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10318e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 103237940fb0STony Krowiak return -EINVAL; 10338e41bd54SChristian Borntraeger } 1034a374e892STony Krowiak get_random_bytes( 1035a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1036a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1037a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 1038c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 1039a374e892STony Krowiak break; 1040a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 10418e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10428e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 104337940fb0STony Krowiak return -EINVAL; 10448e41bd54SChristian Borntraeger } 1045a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 1046a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 1047a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1048c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 1049a374e892STony Krowiak break; 1050a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 10518e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10528e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 105337940fb0STony Krowiak return -EINVAL; 10548e41bd54SChristian Borntraeger } 1055a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 1056a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 1057a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1058c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 1059a374e892STony Krowiak break; 106037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 106137940fb0STony Krowiak if (!ap_instructions_available()) { 106237940fb0STony Krowiak mutex_unlock(&kvm->lock); 106337940fb0STony Krowiak return -EOPNOTSUPP; 106437940fb0STony Krowiak } 106537940fb0STony Krowiak kvm->arch.crypto.apie = 1; 106637940fb0STony Krowiak break; 106737940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 106837940fb0STony Krowiak if (!ap_instructions_available()) { 106937940fb0STony Krowiak mutex_unlock(&kvm->lock); 107037940fb0STony Krowiak return -EOPNOTSUPP; 107137940fb0STony Krowiak } 107237940fb0STony Krowiak kvm->arch.crypto.apie = 0; 107337940fb0STony Krowiak break; 1074a374e892STony Krowiak default: 1075a374e892STony Krowiak mutex_unlock(&kvm->lock); 1076a374e892STony Krowiak return -ENXIO; 1077a374e892STony Krowiak } 1078a374e892STony Krowiak 107920c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 1080a374e892STony Krowiak mutex_unlock(&kvm->lock); 1081a374e892STony Krowiak return 0; 1082a374e892STony Krowiak } 1083a374e892STony Krowiak 10843f4bbb43SMatthew Rosato static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu) 10853f4bbb43SMatthew Rosato { 10863f4bbb43SMatthew Rosato /* Only set the ECB bits after guest requests zPCI interpretation */ 10873f4bbb43SMatthew Rosato if (!vcpu->kvm->arch.use_zpci_interp) 10883f4bbb43SMatthew Rosato return; 10893f4bbb43SMatthew Rosato 10903f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI; 10913f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI; 10923f4bbb43SMatthew Rosato } 10933f4bbb43SMatthew Rosato 10943f4bbb43SMatthew Rosato void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm) 10953f4bbb43SMatthew Rosato { 10963f4bbb43SMatthew Rosato struct kvm_vcpu *vcpu; 10973f4bbb43SMatthew Rosato unsigned long i; 10983f4bbb43SMatthew Rosato 10993f4bbb43SMatthew Rosato lockdep_assert_held(&kvm->lock); 11003f4bbb43SMatthew Rosato 11013f4bbb43SMatthew Rosato if (!kvm_s390_pci_interp_allowed()) 11023f4bbb43SMatthew Rosato return; 11033f4bbb43SMatthew Rosato 11043f4bbb43SMatthew Rosato /* 11053f4bbb43SMatthew Rosato * If host is configured for PCI and the necessary facilities are 11063f4bbb43SMatthew Rosato * available, turn on interpretation for the life of this guest 11073f4bbb43SMatthew Rosato */ 11083f4bbb43SMatthew Rosato kvm->arch.use_zpci_interp = 1; 11093f4bbb43SMatthew Rosato 11103f4bbb43SMatthew Rosato kvm_s390_vcpu_block_all(kvm); 11113f4bbb43SMatthew Rosato 11123f4bbb43SMatthew Rosato kvm_for_each_vcpu(i, vcpu, kvm) { 11133f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 11143f4bbb43SMatthew Rosato kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 11153f4bbb43SMatthew Rosato } 11163f4bbb43SMatthew Rosato 11173f4bbb43SMatthew Rosato kvm_s390_vcpu_unblock_all(kvm); 11183f4bbb43SMatthew Rosato } 11193f4bbb43SMatthew Rosato 1120190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 1121190df4a2SClaudio Imbrenda { 112246808a4cSMarc Zyngier unsigned long cx; 1123190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 1124190df4a2SClaudio Imbrenda 1125190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1126190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1127190df4a2SClaudio Imbrenda } 1128190df4a2SClaudio Imbrenda 1129190df4a2SClaudio Imbrenda /* 1130190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 11311de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1132190df4a2SClaudio Imbrenda */ 1133190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1134190df4a2SClaudio Imbrenda { 1135190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1136190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1137afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1138a54d8066SMaciej S. Szmigiero int bkt; 1139190df4a2SClaudio Imbrenda 1140190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1141afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1142190df4a2SClaudio Imbrenda return 0; 1143190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1144a54d8066SMaciej S. Szmigiero if (!slots || kvm_memslots_empty(slots)) 1145190df4a2SClaudio Imbrenda return -EINVAL; 1146190df4a2SClaudio Imbrenda 1147afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1148afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1149afdad616SClaudio Imbrenda return 0; 1150190df4a2SClaudio Imbrenda } 1151190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1152a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(ms, bkt, slots) { 115313a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 115413a17cc0SIgor Mammedov return -EINVAL; 1155afdad616SClaudio Imbrenda /* 1156afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1157afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1158afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1159afdad616SClaudio Imbrenda * attributes. 1160afdad616SClaudio Imbrenda */ 1161afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1162afdad616SClaudio Imbrenda ram_pages += ms->npages; 1163190df4a2SClaudio Imbrenda } 1164afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1165afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1166190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1167190df4a2SClaudio Imbrenda return 0; 1168190df4a2SClaudio Imbrenda } 1169190df4a2SClaudio Imbrenda 1170190df4a2SClaudio Imbrenda /* 11711de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1172190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1173190df4a2SClaudio Imbrenda */ 1174190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1175190df4a2SClaudio Imbrenda { 1176190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1177afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1178190df4a2SClaudio Imbrenda return 0; 1179afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1180afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1181190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1182190df4a2SClaudio Imbrenda return 0; 1183190df4a2SClaudio Imbrenda } 1184190df4a2SClaudio Imbrenda 1185190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1186190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1187190df4a2SClaudio Imbrenda { 11881de1ea7eSChristian Borntraeger int res = -ENXIO; 1189190df4a2SClaudio Imbrenda 11901de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1191190df4a2SClaudio Imbrenda switch (attr->attr) { 1192190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1193190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1194190df4a2SClaudio Imbrenda break; 1195190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1196190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1197190df4a2SClaudio Imbrenda break; 1198190df4a2SClaudio Imbrenda default: 1199190df4a2SClaudio Imbrenda break; 1200190df4a2SClaudio Imbrenda } 12011de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1202190df4a2SClaudio Imbrenda 1203190df4a2SClaudio Imbrenda return res; 1204190df4a2SClaudio Imbrenda } 1205190df4a2SClaudio Imbrenda 1206190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1207190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1208190df4a2SClaudio Imbrenda { 1209afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1210190df4a2SClaudio Imbrenda 1211190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1212190df4a2SClaudio Imbrenda return -ENXIO; 1213190df4a2SClaudio Imbrenda 1214190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1215190df4a2SClaudio Imbrenda return -EFAULT; 1216190df4a2SClaudio Imbrenda return 0; 1217190df4a2SClaudio Imbrenda } 1218190df4a2SClaudio Imbrenda 12196973091dSNico Boehr static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod); 12206973091dSNico Boehr 12218fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 12228fa1696eSCollin L. Walling { 12238fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 12248fa1696eSCollin L. Walling 12258fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 12268fa1696eSCollin L. Walling return -EFAULT; 12278fa1696eSCollin L. Walling 12280e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 12298fa1696eSCollin L. Walling return -EINVAL; 12306973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od); 12318fa1696eSCollin L. Walling 12328fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 12338fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 12348fa1696eSCollin L. Walling 12358fa1696eSCollin L. Walling return 0; 12368fa1696eSCollin L. Walling } 12378fa1696eSCollin L. Walling 123872f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 123972f25020SJason J. Herne { 124072f25020SJason J. Herne u8 gtod_high; 124172f25020SJason J. Herne 124272f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 124372f25020SJason J. Herne sizeof(gtod_high))) 124472f25020SJason J. Herne return -EFAULT; 124572f25020SJason J. Herne 124672f25020SJason J. Herne if (gtod_high != 0) 124772f25020SJason J. Herne return -EINVAL; 124858c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 124972f25020SJason J. Herne 125072f25020SJason J. Herne return 0; 125172f25020SJason J. Herne } 125272f25020SJason J. Herne 125372f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 125472f25020SJason J. Herne { 12550e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 125672f25020SJason J. Herne 12570e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 12580e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 125972f25020SJason J. Herne return -EFAULT; 126072f25020SJason J. Herne 12616973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od); 12620e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 126372f25020SJason J. Herne return 0; 126472f25020SJason J. Herne } 126572f25020SJason J. Herne 126672f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 126772f25020SJason J. Herne { 126872f25020SJason J. Herne int ret; 126972f25020SJason J. Herne 127072f25020SJason J. Herne if (attr->flags) 127172f25020SJason J. Herne return -EINVAL; 127272f25020SJason J. Herne 12736973091dSNico Boehr mutex_lock(&kvm->lock); 12746973091dSNico Boehr /* 12756973091dSNico Boehr * For protected guests, the TOD is managed by the ultravisor, so trying 12766973091dSNico Boehr * to change it will never bring the expected results. 12776973091dSNico Boehr */ 12786973091dSNico Boehr if (kvm_s390_pv_is_protected(kvm)) { 12796973091dSNico Boehr ret = -EOPNOTSUPP; 12806973091dSNico Boehr goto out_unlock; 12816973091dSNico Boehr } 12826973091dSNico Boehr 128372f25020SJason J. Herne switch (attr->attr) { 12848fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12858fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 12868fa1696eSCollin L. Walling break; 128772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 128872f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 128972f25020SJason J. Herne break; 129072f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 129172f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 129272f25020SJason J. Herne break; 129372f25020SJason J. Herne default: 129472f25020SJason J. Herne ret = -ENXIO; 129572f25020SJason J. Herne break; 129672f25020SJason J. Herne } 12976973091dSNico Boehr 12986973091dSNico Boehr out_unlock: 12996973091dSNico Boehr mutex_unlock(&kvm->lock); 130072f25020SJason J. Herne return ret; 130172f25020SJason J. Herne } 130272f25020SJason J. Herne 130333d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 13048fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 13058fa1696eSCollin L. Walling { 13062cfd7b73SHeiko Carstens union tod_clock clk; 13078fa1696eSCollin L. Walling 13088fa1696eSCollin L. Walling preempt_disable(); 13098fa1696eSCollin L. Walling 13102cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 13118fa1696eSCollin L. Walling 13122cfd7b73SHeiko Carstens gtod->tod = clk.tod + kvm->arch.epoch; 131333d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 131433d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 13152cfd7b73SHeiko Carstens gtod->epoch_idx = clk.ei + kvm->arch.epdx; 13162cfd7b73SHeiko Carstens if (gtod->tod < clk.tod) 13178fa1696eSCollin L. Walling gtod->epoch_idx += 1; 131833d1b272SDavid Hildenbrand } 13198fa1696eSCollin L. Walling 13208fa1696eSCollin L. Walling preempt_enable(); 13218fa1696eSCollin L. Walling } 13228fa1696eSCollin L. Walling 13238fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 13248fa1696eSCollin L. Walling { 13258fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 13268fa1696eSCollin L. Walling 13278fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 132833d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 13298fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 13308fa1696eSCollin L. Walling return -EFAULT; 13318fa1696eSCollin L. Walling 13328fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 13338fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 13348fa1696eSCollin L. Walling return 0; 13358fa1696eSCollin L. Walling } 13368fa1696eSCollin L. Walling 133772f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 133872f25020SJason J. Herne { 133972f25020SJason J. Herne u8 gtod_high = 0; 134072f25020SJason J. Herne 134172f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 134272f25020SJason J. Herne sizeof(gtod_high))) 134372f25020SJason J. Herne return -EFAULT; 134458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 134572f25020SJason J. Herne 134672f25020SJason J. Herne return 0; 134772f25020SJason J. Herne } 134872f25020SJason J. Herne 134972f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 135072f25020SJason J. Herne { 13515a3d883aSDavid Hildenbrand u64 gtod; 135272f25020SJason J. Herne 135360417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 135472f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 135572f25020SJason J. Herne return -EFAULT; 135658c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 135772f25020SJason J. Herne 135872f25020SJason J. Herne return 0; 135972f25020SJason J. Herne } 136072f25020SJason J. Herne 136172f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 136272f25020SJason J. Herne { 136372f25020SJason J. Herne int ret; 136472f25020SJason J. Herne 136572f25020SJason J. Herne if (attr->flags) 136672f25020SJason J. Herne return -EINVAL; 136772f25020SJason J. Herne 136872f25020SJason J. Herne switch (attr->attr) { 13698fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 13708fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 13718fa1696eSCollin L. Walling break; 137272f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 137372f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 137472f25020SJason J. Herne break; 137572f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 137672f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 137772f25020SJason J. Herne break; 137872f25020SJason J. Herne default: 137972f25020SJason J. Herne ret = -ENXIO; 138072f25020SJason J. Herne break; 138172f25020SJason J. Herne } 138272f25020SJason J. Herne return ret; 138372f25020SJason J. Herne } 138472f25020SJason J. Herne 1385658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1386658b6edaSMichael Mueller { 1387658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1388053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1389658b6edaSMichael Mueller int ret = 0; 1390658b6edaSMichael Mueller 1391658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1392a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1393658b6edaSMichael Mueller ret = -EBUSY; 1394658b6edaSMichael Mueller goto out; 1395658b6edaSMichael Mueller } 1396c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1397658b6edaSMichael Mueller if (!proc) { 1398658b6edaSMichael Mueller ret = -ENOMEM; 1399658b6edaSMichael Mueller goto out; 1400658b6edaSMichael Mueller } 1401658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1402658b6edaSMichael Mueller sizeof(*proc))) { 14039bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1404053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1405053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 14060487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1407053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1408053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1409053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1410053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1411053dd230SDavid Hildenbrand else 1412658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1413053dd230SDavid Hildenbrand } 1414c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1415658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1416a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1417a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1418a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1419a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1420a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1421a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1422a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1423658b6edaSMichael Mueller } else 1424658b6edaSMichael Mueller ret = -EFAULT; 1425658b6edaSMichael Mueller kfree(proc); 1426658b6edaSMichael Mueller out: 1427658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1428658b6edaSMichael Mueller return ret; 1429658b6edaSMichael Mueller } 1430658b6edaSMichael Mueller 143115c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 143215c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 143315c9705fSDavid Hildenbrand { 143415c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 143515c9705fSDavid Hildenbrand 143615c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 143715c9705fSDavid Hildenbrand return -EFAULT; 143815c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 143915c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 144015c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 144115c9705fSDavid Hildenbrand return -EINVAL; 144215c9705fSDavid Hildenbrand 144315c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 14442f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 14452f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 14462f8311c9SChristian Borntraeger return -EBUSY; 14472f8311c9SChristian Borntraeger } 1448da0f8e95SYury Norov bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 144915c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 14502f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14512f8311c9SChristian Borntraeger data.feat[0], 14522f8311c9SChristian Borntraeger data.feat[1], 14532f8311c9SChristian Borntraeger data.feat[2]); 14542f8311c9SChristian Borntraeger return 0; 145515c9705fSDavid Hildenbrand } 145615c9705fSDavid Hildenbrand 14570a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 14580a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 14590a763c78SDavid Hildenbrand { 1460346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1461346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1462346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1463346fa2f8SChristian Borntraeger return -EBUSY; 1464346fa2f8SChristian Borntraeger } 1465346fa2f8SChristian Borntraeger 1466346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1467346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1468346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1469346fa2f8SChristian Borntraeger return -EFAULT; 1470346fa2f8SChristian Borntraeger } 1471346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1472346fa2f8SChristian Borntraeger 147311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 147411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 147511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 147611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 147711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 147811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 147911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 148011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 148111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 148211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 148311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 148411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 148511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 148611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 148711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 148811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 148911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 149011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 149111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 149211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 149311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 149411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 149511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 149611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 149711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 149811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 149911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 150011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 150111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 150211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 150311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 150411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 150511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 150611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 150711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 150811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 150911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 151011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 151111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 151211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 151311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 151411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 151511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 151611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 151713209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 151813209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 151913209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1520173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1521173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1522173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1523173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1524173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15254f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15264f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15274f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15284f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15294f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 153011ba5961SChristian Borntraeger 1531346fa2f8SChristian Borntraeger return 0; 15320a763c78SDavid Hildenbrand } 15330a763c78SDavid Hildenbrand 1534658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1535658b6edaSMichael Mueller { 1536658b6edaSMichael Mueller int ret = -ENXIO; 1537658b6edaSMichael Mueller 1538658b6edaSMichael Mueller switch (attr->attr) { 1539658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1540658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1541658b6edaSMichael Mueller break; 154215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 154315c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 154415c9705fSDavid Hildenbrand break; 15450a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 15460a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 15470a763c78SDavid Hildenbrand break; 1548658b6edaSMichael Mueller } 1549658b6edaSMichael Mueller return ret; 1550658b6edaSMichael Mueller } 1551658b6edaSMichael Mueller 1552658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1553658b6edaSMichael Mueller { 1554658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1555658b6edaSMichael Mueller int ret = 0; 1556658b6edaSMichael Mueller 1557c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1558658b6edaSMichael Mueller if (!proc) { 1559658b6edaSMichael Mueller ret = -ENOMEM; 1560658b6edaSMichael Mueller goto out; 1561658b6edaSMichael Mueller } 15629bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1563658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1564c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1565c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1566a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1567a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1568a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1569a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1570a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1571a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1572a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1573658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1574658b6edaSMichael Mueller ret = -EFAULT; 1575658b6edaSMichael Mueller kfree(proc); 1576658b6edaSMichael Mueller out: 1577658b6edaSMichael Mueller return ret; 1578658b6edaSMichael Mueller } 1579658b6edaSMichael Mueller 1580658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1581658b6edaSMichael Mueller { 1582658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1583658b6edaSMichael Mueller int ret = 0; 1584658b6edaSMichael Mueller 1585c4196218SChristian Borntraeger mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT); 1586658b6edaSMichael Mueller if (!mach) { 1587658b6edaSMichael Mueller ret = -ENOMEM; 1588658b6edaSMichael Mueller goto out; 1589658b6edaSMichael Mueller } 1590658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 159137c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1592c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1593981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 159417e89e13SSven Schnelle memcpy((unsigned long *)&mach->fac_list, stfle_fac_list, 159517e89e13SSven Schnelle sizeof(stfle_fac_list)); 1596a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1597a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1598a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1599a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1600a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1601a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1602a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1603a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1604a8c39dd7SChristian Borntraeger mach->fac_list[0], 1605a8c39dd7SChristian Borntraeger mach->fac_list[1], 1606a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1607658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1608658b6edaSMichael Mueller ret = -EFAULT; 1609658b6edaSMichael Mueller kfree(mach); 1610658b6edaSMichael Mueller out: 1611658b6edaSMichael Mueller return ret; 1612658b6edaSMichael Mueller } 1613658b6edaSMichael Mueller 161415c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 161515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 161615c9705fSDavid Hildenbrand { 161715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 161815c9705fSDavid Hildenbrand 1619da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 162015c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 162115c9705fSDavid Hildenbrand return -EFAULT; 16222f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16232f8311c9SChristian Borntraeger data.feat[0], 16242f8311c9SChristian Borntraeger data.feat[1], 16252f8311c9SChristian Borntraeger data.feat[2]); 162615c9705fSDavid Hildenbrand return 0; 162715c9705fSDavid Hildenbrand } 162815c9705fSDavid Hildenbrand 162915c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 163015c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 163115c9705fSDavid Hildenbrand { 163215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 163315c9705fSDavid Hildenbrand 1634da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 163515c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 163615c9705fSDavid Hildenbrand return -EFAULT; 16372f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16382f8311c9SChristian Borntraeger data.feat[0], 16392f8311c9SChristian Borntraeger data.feat[1], 16402f8311c9SChristian Borntraeger data.feat[2]); 164115c9705fSDavid Hildenbrand return 0; 164215c9705fSDavid Hildenbrand } 164315c9705fSDavid Hildenbrand 16440a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 16450a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 16460a763c78SDavid Hildenbrand { 1647346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1648346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1649346fa2f8SChristian Borntraeger return -EFAULT; 1650346fa2f8SChristian Borntraeger 165111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 165211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 165311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 165411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 165511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 165611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 165711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 165811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 165911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 166011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 166111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 166211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 166311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 166411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 166511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 166611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 166711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 166811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 166911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 167011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 167111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 167211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 167311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 167411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 167511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 167611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 167711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 167811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 167911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 168011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 168111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 168211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 168311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 168411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 168511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 168611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 168711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 168811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 168911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 169011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 169111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 169211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 169311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 169411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 169513209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 169613209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 169713209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1698173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1699173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1700173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1701173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1702173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 17034f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 17044f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 17054f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 17064f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 17074f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 170811ba5961SChristian Borntraeger 1709346fa2f8SChristian Borntraeger return 0; 17100a763c78SDavid Hildenbrand } 17110a763c78SDavid Hildenbrand 17120a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 17130a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 17140a763c78SDavid Hildenbrand { 17150a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 17160a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 17170a763c78SDavid Hildenbrand return -EFAULT; 171811ba5961SChristian Borntraeger 171911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 172011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 172111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 172211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 172311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 172411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 172511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 172611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 172711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 172811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 172911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 173011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 173111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 173211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 173311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 173411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 173511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 173611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 173711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 173811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 173911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 174011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 174111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 174211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 174311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 174411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 174511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 174611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 174711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 174811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 174911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 175011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 175111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 175211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 175311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 175411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 175511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 175611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 175711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 175811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 175911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 176011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 176111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 176211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 176313209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 176413209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 176513209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1766173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1767173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1768173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1769173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1770173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 17714f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 17724f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 17734f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 17744f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 17754f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 177611ba5961SChristian Borntraeger 17770a763c78SDavid Hildenbrand return 0; 17780a763c78SDavid Hildenbrand } 1779346fa2f8SChristian Borntraeger 1780658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1781658b6edaSMichael Mueller { 1782658b6edaSMichael Mueller int ret = -ENXIO; 1783658b6edaSMichael Mueller 1784658b6edaSMichael Mueller switch (attr->attr) { 1785658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1786658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1787658b6edaSMichael Mueller break; 1788658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1789658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1790658b6edaSMichael Mueller break; 179115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 179215c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 179315c9705fSDavid Hildenbrand break; 179415c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 179515c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 179615c9705fSDavid Hildenbrand break; 17970a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 17980a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 17990a763c78SDavid Hildenbrand break; 18000a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 18010a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 18020a763c78SDavid Hildenbrand break; 1803658b6edaSMichael Mueller } 1804658b6edaSMichael Mueller return ret; 1805658b6edaSMichael Mueller } 1806658b6edaSMichael Mueller 180724fe0195SPierre Morel /** 180824fe0195SPierre Morel * kvm_s390_update_topology_change_report - update CPU topology change report 180924fe0195SPierre Morel * @kvm: guest KVM description 181024fe0195SPierre Morel * @val: set or clear the MTCR bit 181124fe0195SPierre Morel * 181224fe0195SPierre Morel * Updates the Multiprocessor Topology-Change-Report bit to signal 181324fe0195SPierre Morel * the guest with a topology change. 181424fe0195SPierre Morel * This is only relevant if the topology facility is present. 181524fe0195SPierre Morel * 181624fe0195SPierre Morel * The SCA version, bsca or esca, doesn't matter as offset is the same. 181724fe0195SPierre Morel */ 181824fe0195SPierre Morel static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val) 181924fe0195SPierre Morel { 182024fe0195SPierre Morel union sca_utility new, old; 182124fe0195SPierre Morel struct bsca_block *sca; 182224fe0195SPierre Morel 182324fe0195SPierre Morel read_lock(&kvm->arch.sca_lock); 182424fe0195SPierre Morel sca = kvm->arch.sca; 182524fe0195SPierre Morel do { 182624fe0195SPierre Morel old = READ_ONCE(sca->utility); 182724fe0195SPierre Morel new = old; 182824fe0195SPierre Morel new.mtcr = val; 182924fe0195SPierre Morel } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val); 183024fe0195SPierre Morel read_unlock(&kvm->arch.sca_lock); 183124fe0195SPierre Morel } 183224fe0195SPierre Morel 1833f5ecfee9SPierre Morel static int kvm_s390_set_topo_change_indication(struct kvm *kvm, 1834f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1835f5ecfee9SPierre Morel { 1836f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1837f5ecfee9SPierre Morel return -ENXIO; 1838f5ecfee9SPierre Morel 1839f5ecfee9SPierre Morel kvm_s390_update_topology_change_report(kvm, !!attr->attr); 1840f5ecfee9SPierre Morel return 0; 1841f5ecfee9SPierre Morel } 1842f5ecfee9SPierre Morel 1843f5ecfee9SPierre Morel static int kvm_s390_get_topo_change_indication(struct kvm *kvm, 1844f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1845f5ecfee9SPierre Morel { 1846f5ecfee9SPierre Morel u8 topo; 1847f5ecfee9SPierre Morel 1848f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1849f5ecfee9SPierre Morel return -ENXIO; 1850f5ecfee9SPierre Morel 1851f5ecfee9SPierre Morel read_lock(&kvm->arch.sca_lock); 1852f5ecfee9SPierre Morel topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr; 1853f5ecfee9SPierre Morel read_unlock(&kvm->arch.sca_lock); 1854f5ecfee9SPierre Morel 1855f5ecfee9SPierre Morel return put_user(topo, (u8 __user *)attr->addr); 1856f5ecfee9SPierre Morel } 1857f5ecfee9SPierre Morel 1858f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1859f2061656SDominik Dingel { 1860f2061656SDominik Dingel int ret; 1861f2061656SDominik Dingel 1862f2061656SDominik Dingel switch (attr->group) { 18634f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 18648c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 18654f718eabSDominik Dingel break; 186672f25020SJason J. Herne case KVM_S390_VM_TOD: 186772f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 186872f25020SJason J. Herne break; 1869658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1870658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1871658b6edaSMichael Mueller break; 1872a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1873a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1874a374e892STony Krowiak break; 1875190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1876190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1877190df4a2SClaudio Imbrenda break; 1878f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1879f5ecfee9SPierre Morel ret = kvm_s390_set_topo_change_indication(kvm, attr); 1880f5ecfee9SPierre Morel break; 1881f2061656SDominik Dingel default: 1882f2061656SDominik Dingel ret = -ENXIO; 1883f2061656SDominik Dingel break; 1884f2061656SDominik Dingel } 1885f2061656SDominik Dingel 1886f2061656SDominik Dingel return ret; 1887f2061656SDominik Dingel } 1888f2061656SDominik Dingel 1889f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1890f2061656SDominik Dingel { 18918c0a7ce6SDominik Dingel int ret; 18928c0a7ce6SDominik Dingel 18938c0a7ce6SDominik Dingel switch (attr->group) { 18948c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 18958c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 18968c0a7ce6SDominik Dingel break; 189772f25020SJason J. Herne case KVM_S390_VM_TOD: 189872f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 189972f25020SJason J. Herne break; 1900658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1901658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1902658b6edaSMichael Mueller break; 1903190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1904190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1905190df4a2SClaudio Imbrenda break; 1906f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1907f5ecfee9SPierre Morel ret = kvm_s390_get_topo_change_indication(kvm, attr); 1908f5ecfee9SPierre Morel break; 19098c0a7ce6SDominik Dingel default: 19108c0a7ce6SDominik Dingel ret = -ENXIO; 19118c0a7ce6SDominik Dingel break; 19128c0a7ce6SDominik Dingel } 19138c0a7ce6SDominik Dingel 19148c0a7ce6SDominik Dingel return ret; 1915f2061656SDominik Dingel } 1916f2061656SDominik Dingel 1917f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1918f2061656SDominik Dingel { 1919f2061656SDominik Dingel int ret; 1920f2061656SDominik Dingel 1921f2061656SDominik Dingel switch (attr->group) { 19224f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 19234f718eabSDominik Dingel switch (attr->attr) { 19244f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 19254f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1926f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1927f9cbd9b0SDavid Hildenbrand break; 19288c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 19294f718eabSDominik Dingel ret = 0; 19304f718eabSDominik Dingel break; 19314f718eabSDominik Dingel default: 19324f718eabSDominik Dingel ret = -ENXIO; 19334f718eabSDominik Dingel break; 19344f718eabSDominik Dingel } 19354f718eabSDominik Dingel break; 193672f25020SJason J. Herne case KVM_S390_VM_TOD: 193772f25020SJason J. Herne switch (attr->attr) { 193872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 193972f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 194072f25020SJason J. Herne ret = 0; 194172f25020SJason J. Herne break; 194272f25020SJason J. Herne default: 194372f25020SJason J. Herne ret = -ENXIO; 194472f25020SJason J. Herne break; 194572f25020SJason J. Herne } 194672f25020SJason J. Herne break; 1947658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1948658b6edaSMichael Mueller switch (attr->attr) { 1949658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1950658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 195115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 195215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 19530a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1954346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1955658b6edaSMichael Mueller ret = 0; 1956658b6edaSMichael Mueller break; 1957658b6edaSMichael Mueller default: 1958658b6edaSMichael Mueller ret = -ENXIO; 1959658b6edaSMichael Mueller break; 1960658b6edaSMichael Mueller } 1961658b6edaSMichael Mueller break; 1962a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1963a374e892STony Krowiak switch (attr->attr) { 1964a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1965a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1966a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1967a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1968a374e892STony Krowiak ret = 0; 1969a374e892STony Krowiak break; 197037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 197137940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 197237940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 197337940fb0STony Krowiak break; 1974a374e892STony Krowiak default: 1975a374e892STony Krowiak ret = -ENXIO; 1976a374e892STony Krowiak break; 1977a374e892STony Krowiak } 1978a374e892STony Krowiak break; 1979190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1980190df4a2SClaudio Imbrenda ret = 0; 1981190df4a2SClaudio Imbrenda break; 1982f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1983f5ecfee9SPierre Morel ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO; 1984f5ecfee9SPierre Morel break; 1985f2061656SDominik Dingel default: 1986f2061656SDominik Dingel ret = -ENXIO; 1987f2061656SDominik Dingel break; 1988f2061656SDominik Dingel } 1989f2061656SDominik Dingel 1990f2061656SDominik Dingel return ret; 1991f2061656SDominik Dingel } 1992f2061656SDominik Dingel 199330ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 199430ee2a98SJason J. Herne { 199530ee2a98SJason J. Herne uint8_t *keys; 199630ee2a98SJason J. Herne uint64_t hva; 19974f899147SChristian Borntraeger int srcu_idx, i, r = 0; 199830ee2a98SJason J. Herne 199930ee2a98SJason J. Herne if (args->flags != 0) 200030ee2a98SJason J. Herne return -EINVAL; 200130ee2a98SJason J. Herne 200230ee2a98SJason J. Herne /* Is this guest using storage keys? */ 200355531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 200430ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 200530ee2a98SJason J. Herne 200630ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 200730ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 200830ee2a98SJason J. Herne return -EINVAL; 200930ee2a98SJason J. Herne 2010c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 201130ee2a98SJason J. Herne if (!keys) 201230ee2a98SJason J. Herne return -ENOMEM; 201330ee2a98SJason J. Herne 2014d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 20154f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 201630ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 201730ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 201830ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 201930ee2a98SJason J. Herne r = -EFAULT; 2020d3ed1ceeSMartin Schwidefsky break; 202130ee2a98SJason J. Herne } 202230ee2a98SJason J. Herne 2023154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 2024154c8c19SDavid Hildenbrand if (r) 2025d3ed1ceeSMartin Schwidefsky break; 202630ee2a98SJason J. Herne } 20274f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2028d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 202930ee2a98SJason J. Herne 2030d3ed1ceeSMartin Schwidefsky if (!r) { 203130ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 203230ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 203330ee2a98SJason J. Herne if (r) 203430ee2a98SJason J. Herne r = -EFAULT; 2035d3ed1ceeSMartin Schwidefsky } 2036d3ed1ceeSMartin Schwidefsky 203730ee2a98SJason J. Herne kvfree(keys); 203830ee2a98SJason J. Herne return r; 203930ee2a98SJason J. Herne } 204030ee2a98SJason J. Herne 204130ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 204230ee2a98SJason J. Herne { 204330ee2a98SJason J. Herne uint8_t *keys; 204430ee2a98SJason J. Herne uint64_t hva; 20454f899147SChristian Borntraeger int srcu_idx, i, r = 0; 2046bd096f64SJanosch Frank bool unlocked; 204730ee2a98SJason J. Herne 204830ee2a98SJason J. Herne if (args->flags != 0) 204930ee2a98SJason J. Herne return -EINVAL; 205030ee2a98SJason J. Herne 205130ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 205230ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 205330ee2a98SJason J. Herne return -EINVAL; 205430ee2a98SJason J. Herne 2055c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 205630ee2a98SJason J. Herne if (!keys) 205730ee2a98SJason J. Herne return -ENOMEM; 205830ee2a98SJason J. Herne 205930ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 206030ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 206130ee2a98SJason J. Herne if (r) { 206230ee2a98SJason J. Herne r = -EFAULT; 206330ee2a98SJason J. Herne goto out; 206430ee2a98SJason J. Herne } 206530ee2a98SJason J. Herne 206630ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 206714d4a425SDominik Dingel r = s390_enable_skey(); 206814d4a425SDominik Dingel if (r) 206914d4a425SDominik Dingel goto out; 207030ee2a98SJason J. Herne 2071bd096f64SJanosch Frank i = 0; 2072d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 20734f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 2074bd096f64SJanosch Frank while (i < args->count) { 2075bd096f64SJanosch Frank unlocked = false; 207630ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 207730ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 207830ee2a98SJason J. Herne r = -EFAULT; 2079d3ed1ceeSMartin Schwidefsky break; 208030ee2a98SJason J. Herne } 208130ee2a98SJason J. Herne 208230ee2a98SJason J. Herne /* Lowest order bit is reserved */ 208330ee2a98SJason J. Herne if (keys[i] & 0x01) { 208430ee2a98SJason J. Herne r = -EINVAL; 2085d3ed1ceeSMartin Schwidefsky break; 208630ee2a98SJason J. Herne } 208730ee2a98SJason J. Herne 2088fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 2089bd096f64SJanosch Frank if (r) { 209064019a2eSPeter Xu r = fixup_user_fault(current->mm, hva, 2091bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 209230ee2a98SJason J. Herne if (r) 2093d3ed1ceeSMartin Schwidefsky break; 209430ee2a98SJason J. Herne } 2095bd096f64SJanosch Frank if (!r) 2096bd096f64SJanosch Frank i++; 2097bd096f64SJanosch Frank } 20984f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2099d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 210030ee2a98SJason J. Herne out: 210130ee2a98SJason J. Herne kvfree(keys); 210230ee2a98SJason J. Herne return r; 210330ee2a98SJason J. Herne } 210430ee2a98SJason J. Herne 21054036e387SClaudio Imbrenda /* 21064036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 21074036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 21084036e387SClaudio Imbrenda * two longs. 21094036e387SClaudio Imbrenda */ 21104036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 21114036e387SClaudio Imbrenda /* for consistency */ 21124036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 21134036e387SClaudio Imbrenda 2114afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2115afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2116afdad616SClaudio Imbrenda { 2117afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 2118afdad616SClaudio Imbrenda 2119afdad616SClaudio Imbrenda args->count = 0; 2120afdad616SClaudio Imbrenda while (args->count < bufsize) { 2121afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2122afdad616SClaudio Imbrenda /* 2123afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 2124afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 2125afdad616SClaudio Imbrenda */ 2126afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2127afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 2128afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2129afdad616SClaudio Imbrenda pgstev = 0; 2130afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2131afdad616SClaudio Imbrenda cur_gfn++; 2132afdad616SClaudio Imbrenda } 2133afdad616SClaudio Imbrenda 2134afdad616SClaudio Imbrenda return 0; 2135afdad616SClaudio Imbrenda } 2136afdad616SClaudio Imbrenda 2137c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots, 2138c928bfc2SMaciej S. Szmigiero gfn_t gfn) 2139c928bfc2SMaciej S. Szmigiero { 2140c928bfc2SMaciej S. Szmigiero return ____gfn_to_memslot(slots, gfn, true); 2141c928bfc2SMaciej S. Szmigiero } 2142c928bfc2SMaciej S. Szmigiero 2143afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 2144afdad616SClaudio Imbrenda unsigned long cur_gfn) 2145afdad616SClaudio Imbrenda { 2146c928bfc2SMaciej S. Szmigiero struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn); 2147afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 2148a54d8066SMaciej S. Szmigiero struct rb_node *mnode = &ms->gfn_node[slots->node_idx]; 2149afdad616SClaudio Imbrenda 2150afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 2151a54d8066SMaciej S. Szmigiero mnode = rb_next(mnode); 2152afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 2153a54d8066SMaciej S. Szmigiero if (!mnode) 2154a54d8066SMaciej S. Szmigiero mnode = rb_first(&slots->gfn_tree); 2155afdad616SClaudio Imbrenda 2156a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2157afdad616SClaudio Imbrenda ofs = 0; 2158afdad616SClaudio Imbrenda } 2159afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 2160a54d8066SMaciej S. Szmigiero while (ofs >= ms->npages && (mnode = rb_next(mnode))) { 2161a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2162b5c7e7ecSYury Norov ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages); 2163afdad616SClaudio Imbrenda } 2164afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 2165afdad616SClaudio Imbrenda } 2166afdad616SClaudio Imbrenda 2167afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2168afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2169afdad616SClaudio Imbrenda { 2170afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2171afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2172afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2173afdad616SClaudio Imbrenda 2174a54d8066SMaciej S. Szmigiero if (unlikely(kvm_memslots_empty(slots))) 21750774a964SSean Christopherson return 0; 21760774a964SSean Christopherson 2177afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2178afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2179afdad616SClaudio Imbrenda args->count = 0; 2180afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2181afdad616SClaudio Imbrenda if (!ms) 2182afdad616SClaudio Imbrenda return 0; 2183afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 21846a656832SMaciej S. Szmigiero mem_end = kvm_s390_get_gfn_end(slots); 2185afdad616SClaudio Imbrenda 2186afdad616SClaudio Imbrenda while (args->count < bufsize) { 2187afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2188afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2189afdad616SClaudio Imbrenda return 0; 2190afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2191afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2192afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2193afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2194afdad616SClaudio Imbrenda pgstev = 0; 2195afdad616SClaudio Imbrenda /* Save the value */ 2196afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2197afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2198afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2199afdad616SClaudio Imbrenda return 0; 2200afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2201afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2202afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2203afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2204afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2205afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2206afdad616SClaudio Imbrenda return 0; 2207afdad616SClaudio Imbrenda cur_gfn++; 2208afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2209afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2210afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2211afdad616SClaudio Imbrenda if (!ms) 2212afdad616SClaudio Imbrenda return 0; 2213afdad616SClaudio Imbrenda } 2214afdad616SClaudio Imbrenda } 2215afdad616SClaudio Imbrenda return 0; 2216afdad616SClaudio Imbrenda } 2217afdad616SClaudio Imbrenda 2218afdad616SClaudio Imbrenda /* 22194036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 22204036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 22214036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 22224036e387SClaudio Imbrenda * no trailing clean bytes are saved. 22234036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 22244036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 22254036e387SClaudio Imbrenda */ 22264036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 22274036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 22284036e387SClaudio Imbrenda { 2229afdad616SClaudio Imbrenda unsigned long bufsize; 2230afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2231afdad616SClaudio Imbrenda u8 *values; 22324036e387SClaudio Imbrenda 2233afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 22344036e387SClaudio Imbrenda return -ENXIO; 22354036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 22364036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 22374036e387SClaudio Imbrenda return -EINVAL; 22384036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 22394036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2240afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 22414036e387SClaudio Imbrenda return -EINVAL; 22424036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 22434036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2244c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 22454036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22464036e387SClaudio Imbrenda return 0; 22474036e387SClaudio Imbrenda } 22484036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2249afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 22504036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22514036e387SClaudio Imbrenda return 0; 22524036e387SClaudio Imbrenda } 22534036e387SClaudio Imbrenda 2254afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2255afdad616SClaudio Imbrenda if (!values) 22564036e387SClaudio Imbrenda return -ENOMEM; 22574036e387SClaudio Imbrenda 2258d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 22594036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2260afdad616SClaudio Imbrenda if (peek) 2261afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2262afdad616SClaudio Imbrenda else 2263afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 22644036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2265d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 22664036e387SClaudio Imbrenda 2267afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2268afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2269afdad616SClaudio Imbrenda else 2270afdad616SClaudio Imbrenda args->remaining = 0; 22714036e387SClaudio Imbrenda 2272afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2273afdad616SClaudio Imbrenda ret = -EFAULT; 2274afdad616SClaudio Imbrenda 2275afdad616SClaudio Imbrenda vfree(values); 2276afdad616SClaudio Imbrenda return ret; 22774036e387SClaudio Imbrenda } 22784036e387SClaudio Imbrenda 22794036e387SClaudio Imbrenda /* 22804036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 22814036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2282c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 22834036e387SClaudio Imbrenda */ 22844036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 22854036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 22864036e387SClaudio Imbrenda { 22874036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 22884036e387SClaudio Imbrenda uint8_t *bits; 22894036e387SClaudio Imbrenda int srcu_idx, r = 0; 22904036e387SClaudio Imbrenda 22914036e387SClaudio Imbrenda mask = args->mask; 22924036e387SClaudio Imbrenda 22934036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 22944036e387SClaudio Imbrenda return -ENXIO; 22954036e387SClaudio Imbrenda /* invalid/unsupported flags */ 22964036e387SClaudio Imbrenda if (args->flags != 0) 22974036e387SClaudio Imbrenda return -EINVAL; 22984036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 22994036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 23004036e387SClaudio Imbrenda return -EINVAL; 23014036e387SClaudio Imbrenda /* Nothing to do */ 23024036e387SClaudio Imbrenda if (args->count == 0) 23034036e387SClaudio Imbrenda return 0; 23044036e387SClaudio Imbrenda 230542bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 23064036e387SClaudio Imbrenda if (!bits) 23074036e387SClaudio Imbrenda return -ENOMEM; 23084036e387SClaudio Imbrenda 23094036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 23104036e387SClaudio Imbrenda if (r) { 23114036e387SClaudio Imbrenda r = -EFAULT; 23124036e387SClaudio Imbrenda goto out; 23134036e387SClaudio Imbrenda } 23144036e387SClaudio Imbrenda 2315d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 23164036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 23174036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 23184036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 23194036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 23204036e387SClaudio Imbrenda r = -EFAULT; 23214036e387SClaudio Imbrenda break; 23224036e387SClaudio Imbrenda } 23234036e387SClaudio Imbrenda 23244036e387SClaudio Imbrenda pgstev = bits[i]; 23254036e387SClaudio Imbrenda pgstev = pgstev << 24; 23261bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 23274036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 23284036e387SClaudio Imbrenda } 23294036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2330d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 23314036e387SClaudio Imbrenda 2332c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 2333d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 2334c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 2335d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 23364036e387SClaudio Imbrenda } 23374036e387SClaudio Imbrenda out: 23384036e387SClaudio Imbrenda vfree(bits); 23394036e387SClaudio Imbrenda return r; 23404036e387SClaudio Imbrenda } 23414036e387SClaudio Imbrenda 2342be48d86fSClaudio Imbrenda /** 2343be48d86fSClaudio Imbrenda * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to 2344be48d86fSClaudio Imbrenda * non protected. 2345be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2346be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2347be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2348be48d86fSClaudio Imbrenda * 2349be48d86fSClaudio Imbrenda * Does not stop in case of error, tries to convert as many 2350be48d86fSClaudio Imbrenda * CPUs as possible. In case of error, the RC and RRC of the last error are 2351be48d86fSClaudio Imbrenda * returned. 2352be48d86fSClaudio Imbrenda * 2353be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2354be48d86fSClaudio Imbrenda */ 2355be48d86fSClaudio Imbrenda int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 235629b40f10SJanosch Frank { 235729b40f10SJanosch Frank struct kvm_vcpu *vcpu; 235846808a4cSMarc Zyngier unsigned long i; 2359be48d86fSClaudio Imbrenda u16 _rc, _rrc; 2360be48d86fSClaudio Imbrenda int ret = 0; 236129b40f10SJanosch Frank 236229b40f10SJanosch Frank /* 236329b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 236429b40f10SJanosch Frank * At the same time we must not free the assigned resources when 236529b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 236629b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 236729b40f10SJanosch Frank * behind. 236829b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 236929b40f10SJanosch Frank */ 237029b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 237129b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 2372be48d86fSClaudio Imbrenda if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) { 2373be48d86fSClaudio Imbrenda *rc = _rc; 2374be48d86fSClaudio Imbrenda *rrc = _rrc; 237529b40f10SJanosch Frank ret = -EIO; 237629b40f10SJanosch Frank } 237729b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 237829b40f10SJanosch Frank } 2379ee6a569dSMichael Mueller /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */ 2380ee6a569dSMichael Mueller if (use_gisa) 2381ee6a569dSMichael Mueller kvm_s390_gisa_enable(kvm); 238229b40f10SJanosch Frank return ret; 238329b40f10SJanosch Frank } 238429b40f10SJanosch Frank 2385be48d86fSClaudio Imbrenda /** 2386be48d86fSClaudio Imbrenda * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM 2387be48d86fSClaudio Imbrenda * to protected. 2388be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2389be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2390be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2391be48d86fSClaudio Imbrenda * 2392be48d86fSClaudio Imbrenda * Tries to undo the conversion in case of error. 2393be48d86fSClaudio Imbrenda * 2394be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2395be48d86fSClaudio Imbrenda */ 239629b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 239729b40f10SJanosch Frank { 239846808a4cSMarc Zyngier unsigned long i; 239946808a4cSMarc Zyngier int r = 0; 240029b40f10SJanosch Frank u16 dummy; 240129b40f10SJanosch Frank 240229b40f10SJanosch Frank struct kvm_vcpu *vcpu; 240329b40f10SJanosch Frank 2404ee6a569dSMichael Mueller /* Disable the GISA if the ultravisor does not support AIV. */ 2405ee6a569dSMichael Mueller if (!test_bit_inv(BIT_UV_FEAT_AIV, &uv_info.uv_feature_indications)) 2406ee6a569dSMichael Mueller kvm_s390_gisa_disable(kvm); 2407ee6a569dSMichael Mueller 240829b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 240929b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 241029b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 241129b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 241229b40f10SJanosch Frank if (r) 241329b40f10SJanosch Frank break; 241429b40f10SJanosch Frank } 241529b40f10SJanosch Frank if (r) 241629b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 241729b40f10SJanosch Frank return r; 241829b40f10SJanosch Frank } 241929b40f10SJanosch Frank 242035d02493SJanosch Frank /* 242135d02493SJanosch Frank * Here we provide user space with a direct interface to query UV 242235d02493SJanosch Frank * related data like UV maxima and available features as well as 242335d02493SJanosch Frank * feature specific data. 242435d02493SJanosch Frank * 242535d02493SJanosch Frank * To facilitate future extension of the data structures we'll try to 242635d02493SJanosch Frank * write data up to the maximum requested length. 242735d02493SJanosch Frank */ 242835d02493SJanosch Frank static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info) 242935d02493SJanosch Frank { 243035d02493SJanosch Frank ssize_t len_min; 243135d02493SJanosch Frank 243235d02493SJanosch Frank switch (info->header.id) { 243335d02493SJanosch Frank case KVM_PV_INFO_VM: { 243435d02493SJanosch Frank len_min = sizeof(info->header) + sizeof(info->vm); 243535d02493SJanosch Frank 243635d02493SJanosch Frank if (info->header.len_max < len_min) 243735d02493SJanosch Frank return -EINVAL; 243835d02493SJanosch Frank 243935d02493SJanosch Frank memcpy(info->vm.inst_calls_list, 244035d02493SJanosch Frank uv_info.inst_calls_list, 244135d02493SJanosch Frank sizeof(uv_info.inst_calls_list)); 244235d02493SJanosch Frank 244335d02493SJanosch Frank /* It's max cpuid not max cpus, so it's off by one */ 244435d02493SJanosch Frank info->vm.max_cpus = uv_info.max_guest_cpu_id + 1; 244535d02493SJanosch Frank info->vm.max_guests = uv_info.max_num_sec_conf; 244635d02493SJanosch Frank info->vm.max_guest_addr = uv_info.max_sec_stor_addr; 244735d02493SJanosch Frank info->vm.feature_indication = uv_info.uv_feature_indications; 244835d02493SJanosch Frank 244935d02493SJanosch Frank return len_min; 245035d02493SJanosch Frank } 2451fe9a93e0SJanosch Frank case KVM_PV_INFO_DUMP: { 2452fe9a93e0SJanosch Frank len_min = sizeof(info->header) + sizeof(info->dump); 2453fe9a93e0SJanosch Frank 2454fe9a93e0SJanosch Frank if (info->header.len_max < len_min) 2455fe9a93e0SJanosch Frank return -EINVAL; 2456fe9a93e0SJanosch Frank 2457fe9a93e0SJanosch Frank info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len; 2458fe9a93e0SJanosch Frank info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len; 2459fe9a93e0SJanosch Frank info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len; 2460fe9a93e0SJanosch Frank return len_min; 2461fe9a93e0SJanosch Frank } 246235d02493SJanosch Frank default: 246335d02493SJanosch Frank return -EINVAL; 246435d02493SJanosch Frank } 246535d02493SJanosch Frank } 246635d02493SJanosch Frank 24670460eb35SJanosch Frank static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd, 24680460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp) 24690460eb35SJanosch Frank { 24700460eb35SJanosch Frank int r = -EINVAL; 24710460eb35SJanosch Frank void __user *result_buff = (void __user *)dmp.buff_addr; 24720460eb35SJanosch Frank 24730460eb35SJanosch Frank switch (dmp.subcmd) { 24740460eb35SJanosch Frank case KVM_PV_DUMP_INIT: { 24750460eb35SJanosch Frank if (kvm->arch.pv.dumping) 24760460eb35SJanosch Frank break; 24770460eb35SJanosch Frank 24780460eb35SJanosch Frank /* 24790460eb35SJanosch Frank * Block SIE entry as concurrent dump UVCs could lead 24800460eb35SJanosch Frank * to validities. 24810460eb35SJanosch Frank */ 24820460eb35SJanosch Frank kvm_s390_vcpu_block_all(kvm); 24830460eb35SJanosch Frank 24840460eb35SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 24850460eb35SJanosch Frank UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc); 24860460eb35SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x", 24870460eb35SJanosch Frank cmd->rc, cmd->rrc); 24880460eb35SJanosch Frank if (!r) { 24890460eb35SJanosch Frank kvm->arch.pv.dumping = true; 24900460eb35SJanosch Frank } else { 24910460eb35SJanosch Frank kvm_s390_vcpu_unblock_all(kvm); 24920460eb35SJanosch Frank r = -EINVAL; 24930460eb35SJanosch Frank } 24940460eb35SJanosch Frank break; 24950460eb35SJanosch Frank } 24960460eb35SJanosch Frank case KVM_PV_DUMP_CONFIG_STOR_STATE: { 24970460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 24980460eb35SJanosch Frank break; 24990460eb35SJanosch Frank 25000460eb35SJanosch Frank /* 25010460eb35SJanosch Frank * gaddr is an output parameter since we might stop 25020460eb35SJanosch Frank * early. As dmp will be copied back in our caller, we 25030460eb35SJanosch Frank * don't need to do it ourselves. 25040460eb35SJanosch Frank */ 25050460eb35SJanosch Frank r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len, 25060460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 25070460eb35SJanosch Frank break; 25080460eb35SJanosch Frank } 25090460eb35SJanosch Frank case KVM_PV_DUMP_COMPLETE: { 25100460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 25110460eb35SJanosch Frank break; 25120460eb35SJanosch Frank 25130460eb35SJanosch Frank r = -EINVAL; 25140460eb35SJanosch Frank if (dmp.buff_len < uv_info.conf_dump_finalize_len) 25150460eb35SJanosch Frank break; 25160460eb35SJanosch Frank 25170460eb35SJanosch Frank r = kvm_s390_pv_dump_complete(kvm, result_buff, 25180460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 25190460eb35SJanosch Frank break; 25200460eb35SJanosch Frank } 25210460eb35SJanosch Frank default: 25220460eb35SJanosch Frank r = -ENOTTY; 25230460eb35SJanosch Frank break; 25240460eb35SJanosch Frank } 25250460eb35SJanosch Frank 25260460eb35SJanosch Frank return r; 25270460eb35SJanosch Frank } 25280460eb35SJanosch Frank 252929b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 253029b40f10SJanosch Frank { 2531fb491d55SClaudio Imbrenda const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM); 2532fb491d55SClaudio Imbrenda void __user *argp = (void __user *)cmd->data; 253329b40f10SJanosch Frank int r = 0; 253429b40f10SJanosch Frank u16 dummy; 2535fb491d55SClaudio Imbrenda 2536fb491d55SClaudio Imbrenda if (need_lock) 2537fb491d55SClaudio Imbrenda mutex_lock(&kvm->lock); 253829b40f10SJanosch Frank 253929b40f10SJanosch Frank switch (cmd->cmd) { 254029b40f10SJanosch Frank case KVM_PV_ENABLE: { 254129b40f10SJanosch Frank r = -EINVAL; 254229b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 254329b40f10SJanosch Frank break; 254429b40f10SJanosch Frank 254529b40f10SJanosch Frank /* 254629b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 254729b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 254829b40f10SJanosch Frank */ 254929b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 255029b40f10SJanosch Frank if (r) 255129b40f10SJanosch Frank break; 255229b40f10SJanosch Frank 2553d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 2554fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2555d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 2556fa0c5eabSJanosch Frank if (r) 2557fa0c5eabSJanosch Frank break; 2558fa0c5eabSJanosch Frank 255929b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 256029b40f10SJanosch Frank if (r) 256129b40f10SJanosch Frank break; 256229b40f10SJanosch Frank 256329b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 256429b40f10SJanosch Frank if (r) 256529b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 25660890ddeaSChristian Borntraeger 25670890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 25680890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 256929b40f10SJanosch Frank break; 257029b40f10SJanosch Frank } 2571fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PREPARE: 2572fb491d55SClaudio Imbrenda r = -EINVAL; 2573fb491d55SClaudio Imbrenda if (!kvm_s390_pv_is_protected(kvm) || !async_destroy) 2574fb491d55SClaudio Imbrenda break; 2575fb491d55SClaudio Imbrenda 2576fb491d55SClaudio Imbrenda r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 2577fb491d55SClaudio Imbrenda /* 2578fb491d55SClaudio Imbrenda * If a CPU could not be destroyed, destroy VM will also fail. 2579fb491d55SClaudio Imbrenda * There is no point in trying to destroy it. Instead return 2580fb491d55SClaudio Imbrenda * the rc and rrc from the first CPU that failed destroying. 2581fb491d55SClaudio Imbrenda */ 2582fb491d55SClaudio Imbrenda if (r) 2583fb491d55SClaudio Imbrenda break; 2584fb491d55SClaudio Imbrenda r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc); 2585fb491d55SClaudio Imbrenda 2586fb491d55SClaudio Imbrenda /* no need to block service interrupts any more */ 2587fb491d55SClaudio Imbrenda clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 2588fb491d55SClaudio Imbrenda break; 2589fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PERFORM: 2590fb491d55SClaudio Imbrenda r = -EINVAL; 2591fb491d55SClaudio Imbrenda if (!async_destroy) 2592fb491d55SClaudio Imbrenda break; 2593fb491d55SClaudio Imbrenda /* kvm->lock must not be held; this is asserted inside the function. */ 2594fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc); 2595fb491d55SClaudio Imbrenda break; 259629b40f10SJanosch Frank case KVM_PV_DISABLE: { 259729b40f10SJanosch Frank r = -EINVAL; 259829b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 259929b40f10SJanosch Frank break; 260029b40f10SJanosch Frank 260129b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 260229b40f10SJanosch Frank /* 260329b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 260429b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 260529b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 260629b40f10SJanosch Frank */ 260729b40f10SJanosch Frank if (r) 260829b40f10SJanosch Frank break; 2609fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc); 26100890ddeaSChristian Borntraeger 26110890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 26120890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 261329b40f10SJanosch Frank break; 261429b40f10SJanosch Frank } 261529b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 261629b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 261729b40f10SJanosch Frank void *hdr; 261829b40f10SJanosch Frank 261929b40f10SJanosch Frank r = -EINVAL; 262029b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 262129b40f10SJanosch Frank break; 262229b40f10SJanosch Frank 262329b40f10SJanosch Frank r = -EFAULT; 262429b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 262529b40f10SJanosch Frank break; 262629b40f10SJanosch Frank 262729b40f10SJanosch Frank /* Currently restricted to 8KB */ 262829b40f10SJanosch Frank r = -EINVAL; 262929b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 263029b40f10SJanosch Frank break; 263129b40f10SJanosch Frank 263229b40f10SJanosch Frank r = -ENOMEM; 263329b40f10SJanosch Frank hdr = vmalloc(parms.length); 263429b40f10SJanosch Frank if (!hdr) 263529b40f10SJanosch Frank break; 263629b40f10SJanosch Frank 263729b40f10SJanosch Frank r = -EFAULT; 263829b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 263929b40f10SJanosch Frank parms.length)) 264029b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 264129b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 264229b40f10SJanosch Frank 264329b40f10SJanosch Frank vfree(hdr); 264429b40f10SJanosch Frank break; 264529b40f10SJanosch Frank } 264629b40f10SJanosch Frank case KVM_PV_UNPACK: { 264729b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 264829b40f10SJanosch Frank 264929b40f10SJanosch Frank r = -EINVAL; 26501ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) 265129b40f10SJanosch Frank break; 265229b40f10SJanosch Frank 265329b40f10SJanosch Frank r = -EFAULT; 265429b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 265529b40f10SJanosch Frank break; 265629b40f10SJanosch Frank 265729b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 265829b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 265929b40f10SJanosch Frank break; 266029b40f10SJanosch Frank } 266129b40f10SJanosch Frank case KVM_PV_VERIFY: { 266229b40f10SJanosch Frank r = -EINVAL; 266329b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 266429b40f10SJanosch Frank break; 266529b40f10SJanosch Frank 266629b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 266729b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 266829b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 266929b40f10SJanosch Frank cmd->rrc); 267029b40f10SJanosch Frank break; 267129b40f10SJanosch Frank } 2672e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2673e0d2773dSJanosch Frank r = -EINVAL; 2674e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2675e0d2773dSJanosch Frank break; 2676e0d2773dSJanosch Frank 2677e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2678e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2679e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2680e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2681e0d2773dSJanosch Frank break; 2682e0d2773dSJanosch Frank } 2683e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2684e0d2773dSJanosch Frank r = -EINVAL; 2685e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2686e0d2773dSJanosch Frank break; 2687e0d2773dSJanosch Frank 2688e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2689e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2690e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2691e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2692e0d2773dSJanosch Frank break; 2693e0d2773dSJanosch Frank } 269435d02493SJanosch Frank case KVM_PV_INFO: { 269535d02493SJanosch Frank struct kvm_s390_pv_info info = {}; 269635d02493SJanosch Frank ssize_t data_len; 269735d02493SJanosch Frank 269835d02493SJanosch Frank /* 269935d02493SJanosch Frank * No need to check the VM protection here. 270035d02493SJanosch Frank * 270135d02493SJanosch Frank * Maybe user space wants to query some of the data 270235d02493SJanosch Frank * when the VM is still unprotected. If we see the 270335d02493SJanosch Frank * need to fence a new data command we can still 270435d02493SJanosch Frank * return an error in the info handler. 270535d02493SJanosch Frank */ 270635d02493SJanosch Frank 270735d02493SJanosch Frank r = -EFAULT; 270835d02493SJanosch Frank if (copy_from_user(&info, argp, sizeof(info.header))) 270935d02493SJanosch Frank break; 271035d02493SJanosch Frank 271135d02493SJanosch Frank r = -EINVAL; 271235d02493SJanosch Frank if (info.header.len_max < sizeof(info.header)) 271335d02493SJanosch Frank break; 271435d02493SJanosch Frank 271535d02493SJanosch Frank data_len = kvm_s390_handle_pv_info(&info); 271635d02493SJanosch Frank if (data_len < 0) { 271735d02493SJanosch Frank r = data_len; 271835d02493SJanosch Frank break; 271935d02493SJanosch Frank } 272035d02493SJanosch Frank /* 272135d02493SJanosch Frank * If a data command struct is extended (multiple 272235d02493SJanosch Frank * times) this can be used to determine how much of it 272335d02493SJanosch Frank * is valid. 272435d02493SJanosch Frank */ 272535d02493SJanosch Frank info.header.len_written = data_len; 272635d02493SJanosch Frank 272735d02493SJanosch Frank r = -EFAULT; 272835d02493SJanosch Frank if (copy_to_user(argp, &info, data_len)) 272935d02493SJanosch Frank break; 273035d02493SJanosch Frank 273135d02493SJanosch Frank r = 0; 273235d02493SJanosch Frank break; 273335d02493SJanosch Frank } 27340460eb35SJanosch Frank case KVM_PV_DUMP: { 27350460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp; 27360460eb35SJanosch Frank 27370460eb35SJanosch Frank r = -EINVAL; 27380460eb35SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 27390460eb35SJanosch Frank break; 27400460eb35SJanosch Frank 27410460eb35SJanosch Frank r = -EFAULT; 27420460eb35SJanosch Frank if (copy_from_user(&dmp, argp, sizeof(dmp))) 27430460eb35SJanosch Frank break; 27440460eb35SJanosch Frank 27450460eb35SJanosch Frank r = kvm_s390_pv_dmp(kvm, cmd, dmp); 27460460eb35SJanosch Frank if (r) 27470460eb35SJanosch Frank break; 27480460eb35SJanosch Frank 27490460eb35SJanosch Frank if (copy_to_user(argp, &dmp, sizeof(dmp))) { 27500460eb35SJanosch Frank r = -EFAULT; 27510460eb35SJanosch Frank break; 27520460eb35SJanosch Frank } 27530460eb35SJanosch Frank 27540460eb35SJanosch Frank break; 27550460eb35SJanosch Frank } 275629b40f10SJanosch Frank default: 275729b40f10SJanosch Frank r = -ENOTTY; 275829b40f10SJanosch Frank } 2759fb491d55SClaudio Imbrenda if (need_lock) 2760fb491d55SClaudio Imbrenda mutex_unlock(&kvm->lock); 2761fb491d55SClaudio Imbrenda 276229b40f10SJanosch Frank return r; 276329b40f10SJanosch Frank } 276429b40f10SJanosch Frank 2765a41f505eSJanis Schoetterl-Glausch static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags) 2766e9e9feebSJanis Schoetterl-Glausch { 27673d9042f8SJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size) 2768ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2769ef11c946SJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE) 2770ef11c946SJanis Schoetterl-Glausch return -E2BIG; 2771ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 2772a41f505eSJanis Schoetterl-Glausch if (mop->key > 0xf) 2773ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2774ef11c946SJanis Schoetterl-Glausch } else { 2775ef11c946SJanis Schoetterl-Glausch mop->key = 0; 2776ef11c946SJanis Schoetterl-Glausch } 2777a41f505eSJanis Schoetterl-Glausch return 0; 2778e9e9feebSJanis Schoetterl-Glausch } 2779e9e9feebSJanis Schoetterl-Glausch 27808550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop) 2781ef11c946SJanis Schoetterl-Glausch { 2782ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 27830d6d4d23SJanis Schoetterl-Glausch enum gacc_mode acc_mode; 2784ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL; 2785ef11c946SJanis Schoetterl-Glausch int r, srcu_idx; 2786ef11c946SJanis Schoetterl-Glausch 2787a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION | 2788a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY); 2789a41f505eSJanis Schoetterl-Glausch if (r) 2790a41f505eSJanis Schoetterl-Glausch return r; 2791a41f505eSJanis Schoetterl-Glausch 2792ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 2793ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size); 2794ef11c946SJanis Schoetterl-Glausch if (!tmpbuf) 2795ef11c946SJanis Schoetterl-Glausch return -ENOMEM; 2796ef11c946SJanis Schoetterl-Glausch } 2797ef11c946SJanis Schoetterl-Glausch 2798ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 2799ef11c946SJanis Schoetterl-Glausch 2800ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 2801ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 2802ef11c946SJanis Schoetterl-Glausch goto out_unlock; 2803ef11c946SJanis Schoetterl-Glausch } 2804ef11c946SJanis Schoetterl-Glausch 28050d6d4d23SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE; 2806ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 28070d6d4d23SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key); 28080d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 28090d6d4d23SJanis Schoetterl-Glausch } 28100d6d4d23SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) { 2811ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2812ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key); 28130d6d4d23SJanis Schoetterl-Glausch if (r) 28140d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 2815ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) 2816ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2817ef11c946SJanis Schoetterl-Glausch } else { 2818ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) { 2819ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 28200d6d4d23SJanis Schoetterl-Glausch goto out_unlock; 2821ef11c946SJanis Schoetterl-Glausch } 2822ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2823ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key); 2824ef11c946SJanis Schoetterl-Glausch } 2825ef11c946SJanis Schoetterl-Glausch 2826ef11c946SJanis Schoetterl-Glausch out_unlock: 2827ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 2828ef11c946SJanis Schoetterl-Glausch 2829ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf); 2830ef11c946SJanis Schoetterl-Glausch return r; 2831ef11c946SJanis Schoetterl-Glausch } 2832ef11c946SJanis Schoetterl-Glausch 28333fd49805SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop) 28343fd49805SJanis Schoetterl-Glausch { 28353fd49805SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 28363fd49805SJanis Schoetterl-Glausch void __user *old_addr = (void __user *)mop->old_addr; 28373fd49805SJanis Schoetterl-Glausch union { 28383fd49805SJanis Schoetterl-Glausch __uint128_t quad; 28393fd49805SJanis Schoetterl-Glausch char raw[sizeof(__uint128_t)]; 28403fd49805SJanis Schoetterl-Glausch } old = { .quad = 0}, new = { .quad = 0 }; 28413fd49805SJanis Schoetterl-Glausch unsigned int off_in_quad = sizeof(new) - mop->size; 28423fd49805SJanis Schoetterl-Glausch int r, srcu_idx; 28433fd49805SJanis Schoetterl-Glausch bool success; 28443fd49805SJanis Schoetterl-Glausch 28453fd49805SJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION); 28463fd49805SJanis Schoetterl-Glausch if (r) 28473fd49805SJanis Schoetterl-Glausch return r; 28483fd49805SJanis Schoetterl-Glausch /* 28493fd49805SJanis Schoetterl-Glausch * This validates off_in_quad. Checking that size is a power 28503fd49805SJanis Schoetterl-Glausch * of two is not necessary, as cmpxchg_guest_abs_with_key 28513fd49805SJanis Schoetterl-Glausch * takes care of that 28523fd49805SJanis Schoetterl-Glausch */ 28533fd49805SJanis Schoetterl-Glausch if (mop->size > sizeof(new)) 28543fd49805SJanis Schoetterl-Glausch return -EINVAL; 28553fd49805SJanis Schoetterl-Glausch if (copy_from_user(&new.raw[off_in_quad], uaddr, mop->size)) 28563fd49805SJanis Schoetterl-Glausch return -EFAULT; 28573fd49805SJanis Schoetterl-Glausch if (copy_from_user(&old.raw[off_in_quad], old_addr, mop->size)) 28583fd49805SJanis Schoetterl-Glausch return -EFAULT; 28593fd49805SJanis Schoetterl-Glausch 28603fd49805SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 28613fd49805SJanis Schoetterl-Glausch 28623fd49805SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 28633fd49805SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 28643fd49805SJanis Schoetterl-Glausch goto out_unlock; 28653fd49805SJanis Schoetterl-Glausch } 28663fd49805SJanis Schoetterl-Glausch 28673fd49805SJanis Schoetterl-Glausch r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old.quad, 28683fd49805SJanis Schoetterl-Glausch new.quad, mop->key, &success); 28693fd49805SJanis Schoetterl-Glausch if (!success && copy_to_user(old_addr, &old.raw[off_in_quad], mop->size)) 28703fd49805SJanis Schoetterl-Glausch r = -EFAULT; 28713fd49805SJanis Schoetterl-Glausch 28723fd49805SJanis Schoetterl-Glausch out_unlock: 28733fd49805SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 28743fd49805SJanis Schoetterl-Glausch return r; 28753fd49805SJanis Schoetterl-Glausch } 28763fd49805SJanis Schoetterl-Glausch 28778550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) 28788550bcb7SJanis Schoetterl-Glausch { 28798550bcb7SJanis Schoetterl-Glausch /* 28808550bcb7SJanis Schoetterl-Glausch * This is technically a heuristic only, if the kvm->lock is not 28818550bcb7SJanis Schoetterl-Glausch * taken, it is not guaranteed that the vm is/remains non-protected. 28828550bcb7SJanis Schoetterl-Glausch * This is ok from a kernel perspective, wrongdoing is detected 28838550bcb7SJanis Schoetterl-Glausch * on the access, -EFAULT is returned and the vm may crash the 28848550bcb7SJanis Schoetterl-Glausch * next time it accesses the memory in question. 28858550bcb7SJanis Schoetterl-Glausch * There is no sane usecase to do switching and a memop on two 28868550bcb7SJanis Schoetterl-Glausch * different CPUs at the same time. 28878550bcb7SJanis Schoetterl-Glausch */ 28888550bcb7SJanis Schoetterl-Glausch if (kvm_s390_pv_get_handle(kvm)) 28898550bcb7SJanis Schoetterl-Glausch return -EINVAL; 28908550bcb7SJanis Schoetterl-Glausch 28918550bcb7SJanis Schoetterl-Glausch switch (mop->op) { 28928550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ: 28938550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE: 28948550bcb7SJanis Schoetterl-Glausch return kvm_s390_vm_mem_op_abs(kvm, mop); 28953fd49805SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_CMPXCHG: 28963fd49805SJanis Schoetterl-Glausch return kvm_s390_vm_mem_op_cmpxchg(kvm, mop); 28978550bcb7SJanis Schoetterl-Glausch default: 28988550bcb7SJanis Schoetterl-Glausch return -EINVAL; 28998550bcb7SJanis Schoetterl-Glausch } 29008550bcb7SJanis Schoetterl-Glausch } 29018550bcb7SJanis Schoetterl-Glausch 2902b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2903b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2904b0c632dbSHeiko Carstens { 2905b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2906b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2907f2061656SDominik Dingel struct kvm_device_attr attr; 2908b0c632dbSHeiko Carstens int r; 2909b0c632dbSHeiko Carstens 2910b0c632dbSHeiko Carstens switch (ioctl) { 2911ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2912ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2913ba5c1e9bSCarsten Otte 2914ba5c1e9bSCarsten Otte r = -EFAULT; 2915ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2916ba5c1e9bSCarsten Otte break; 2917ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2918ba5c1e9bSCarsten Otte break; 2919ba5c1e9bSCarsten Otte } 292084223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 292184223598SCornelia Huck struct kvm_irq_routing_entry routing; 292284223598SCornelia Huck 292384223598SCornelia Huck r = -EINVAL; 292484223598SCornelia Huck if (kvm->arch.use_irqchip) { 292584223598SCornelia Huck /* Set up dummy routing. */ 292684223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2927152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 292884223598SCornelia Huck } 292984223598SCornelia Huck break; 293084223598SCornelia Huck } 2931f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2932f2061656SDominik Dingel r = -EFAULT; 2933f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2934f2061656SDominik Dingel break; 2935f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2936f2061656SDominik Dingel break; 2937f2061656SDominik Dingel } 2938f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2939f2061656SDominik Dingel r = -EFAULT; 2940f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2941f2061656SDominik Dingel break; 2942f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2943f2061656SDominik Dingel break; 2944f2061656SDominik Dingel } 2945f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2946f2061656SDominik Dingel r = -EFAULT; 2947f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2948f2061656SDominik Dingel break; 2949f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2950f2061656SDominik Dingel break; 2951f2061656SDominik Dingel } 295230ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 295330ee2a98SJason J. Herne struct kvm_s390_skeys args; 295430ee2a98SJason J. Herne 295530ee2a98SJason J. Herne r = -EFAULT; 295630ee2a98SJason J. Herne if (copy_from_user(&args, argp, 295730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 295830ee2a98SJason J. Herne break; 295930ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 296030ee2a98SJason J. Herne break; 296130ee2a98SJason J. Herne } 296230ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 296330ee2a98SJason J. Herne struct kvm_s390_skeys args; 296430ee2a98SJason J. Herne 296530ee2a98SJason J. Herne r = -EFAULT; 296630ee2a98SJason J. Herne if (copy_from_user(&args, argp, 296730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 296830ee2a98SJason J. Herne break; 296930ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 297030ee2a98SJason J. Herne break; 297130ee2a98SJason J. Herne } 29724036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 29734036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29744036e387SClaudio Imbrenda 29754036e387SClaudio Imbrenda r = -EFAULT; 29764036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29774036e387SClaudio Imbrenda break; 29781de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29794036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 29801de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29814036e387SClaudio Imbrenda if (!r) { 29824036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 29834036e387SClaudio Imbrenda if (r) 29844036e387SClaudio Imbrenda r = -EFAULT; 29854036e387SClaudio Imbrenda } 29864036e387SClaudio Imbrenda break; 29874036e387SClaudio Imbrenda } 29884036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 29894036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29904036e387SClaudio Imbrenda 29914036e387SClaudio Imbrenda r = -EFAULT; 29924036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29934036e387SClaudio Imbrenda break; 29941de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29954036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 29961de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29974036e387SClaudio Imbrenda break; 29984036e387SClaudio Imbrenda } 299929b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 300029b40f10SJanosch Frank struct kvm_pv_cmd args; 300129b40f10SJanosch Frank 300267cf68b6SEric Farman /* protvirt means user cpu state */ 300367cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 300429b40f10SJanosch Frank r = 0; 300529b40f10SJanosch Frank if (!is_prot_virt_host()) { 300629b40f10SJanosch Frank r = -EINVAL; 300729b40f10SJanosch Frank break; 300829b40f10SJanosch Frank } 300929b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 301029b40f10SJanosch Frank r = -EFAULT; 301129b40f10SJanosch Frank break; 301229b40f10SJanosch Frank } 301329b40f10SJanosch Frank if (args.flags) { 301429b40f10SJanosch Frank r = -EINVAL; 301529b40f10SJanosch Frank break; 301629b40f10SJanosch Frank } 3017fb491d55SClaudio Imbrenda /* must be called without kvm->lock */ 301829b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 301929b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 302029b40f10SJanosch Frank r = -EFAULT; 302129b40f10SJanosch Frank break; 302229b40f10SJanosch Frank } 302329b40f10SJanosch Frank break; 302429b40f10SJanosch Frank } 3025ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: { 3026ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op; 3027ef11c946SJanis Schoetterl-Glausch 3028ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 3029ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op); 3030ef11c946SJanis Schoetterl-Glausch else 3031ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 3032ef11c946SJanis Schoetterl-Glausch break; 3033ef11c946SJanis Schoetterl-Glausch } 3034db1c875eSMatthew Rosato case KVM_S390_ZPCI_OP: { 3035db1c875eSMatthew Rosato struct kvm_s390_zpci_op args; 3036db1c875eSMatthew Rosato 3037db1c875eSMatthew Rosato r = -EINVAL; 3038db1c875eSMatthew Rosato if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 3039db1c875eSMatthew Rosato break; 3040db1c875eSMatthew Rosato if (copy_from_user(&args, argp, sizeof(args))) { 3041db1c875eSMatthew Rosato r = -EFAULT; 3042db1c875eSMatthew Rosato break; 3043db1c875eSMatthew Rosato } 3044db1c875eSMatthew Rosato r = kvm_s390_pci_zpci_op(kvm, &args); 3045db1c875eSMatthew Rosato break; 3046db1c875eSMatthew Rosato } 3047b0c632dbSHeiko Carstens default: 3048367e1319SAvi Kivity r = -ENOTTY; 3049b0c632dbSHeiko Carstens } 3050b0c632dbSHeiko Carstens 3051b0c632dbSHeiko Carstens return r; 3052b0c632dbSHeiko Carstens } 3053b0c632dbSHeiko Carstens 305445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 305545c9b47cSTony Krowiak { 3056e585b24aSTony Krowiak struct ap_config_info info; 305745c9b47cSTony Krowiak 3058e585b24aSTony Krowiak if (ap_instructions_available()) { 3059e585b24aSTony Krowiak if (ap_qci(&info) == 0) 3060e585b24aSTony Krowiak return info.apxa; 306145c9b47cSTony Krowiak } 306245c9b47cSTony Krowiak 306345c9b47cSTony Krowiak return 0; 306445c9b47cSTony Krowiak } 306545c9b47cSTony Krowiak 3066e585b24aSTony Krowiak /* 3067e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 3068e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 3069e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 3070e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 3071e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 3072e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 3073e585b24aSTony Krowiak */ 307445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 307545c9b47cSTony Krowiak { 307645c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 307745c9b47cSTony Krowiak 3078e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 3079e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 3080e585b24aSTony Krowiak 3081e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 3082e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3083e585b24aSTony Krowiak return; 3084e585b24aSTony Krowiak 308545c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 308645c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 308745c9b47cSTony Krowiak else 308845c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 308945c9b47cSTony Krowiak } 309045c9b47cSTony Krowiak 309186956e70STony Krowiak /* 309286956e70STony Krowiak * kvm_arch_crypto_set_masks 309386956e70STony Krowiak * 309486956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 309586956e70STony Krowiak * to be set. 309686956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 309786956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 309886956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 309986956e70STony Krowiak * 310086956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 310186956e70STony Krowiak * which the KVM guest is granted access. 310286956e70STony Krowiak * 310386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 310486956e70STony Krowiak * function. 310586956e70STony Krowiak */ 31060e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 31070e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 31080e237e44SPierre Morel { 31090e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 31100e237e44SPierre Morel 31110e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 31120e237e44SPierre Morel 31130e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 31140e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 31150e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 31160e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 31170e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 31180e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 31190e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 31200e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 31210e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 31220e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 31230e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 31240e237e44SPierre Morel break; 31250e237e44SPierre Morel case CRYCB_FORMAT1: 31260e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 31270e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 31280e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 31290e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 31300e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 31310e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 31320e237e44SPierre Morel *((unsigned short *)adm)); 31330e237e44SPierre Morel break; 31340e237e44SPierre Morel default: /* Can not happen */ 31350e237e44SPierre Morel break; 31360e237e44SPierre Morel } 31370e237e44SPierre Morel 31380e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 31390e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 31400e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 31410e237e44SPierre Morel } 31420e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 31430e237e44SPierre Morel 314486956e70STony Krowiak /* 314586956e70STony Krowiak * kvm_arch_crypto_clear_masks 314686956e70STony Krowiak * 314786956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 314886956e70STony Krowiak * to be cleared. 314986956e70STony Krowiak * 315086956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 315186956e70STony Krowiak * which the KVM guest is granted access. 315286956e70STony Krowiak * 315386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 315486956e70STony Krowiak * function. 315586956e70STony Krowiak */ 315642104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 315742104598STony Krowiak { 315842104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 315942104598STony Krowiak 316042104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 316142104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 316242104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 316342104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 316442104598STony Krowiak 31650e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 31666cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 31676cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 316842104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 316942104598STony Krowiak } 317042104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 317142104598STony Krowiak 31729bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 31739d8d5786SMichael Mueller { 31749bb0ec09SDavid Hildenbrand struct cpuid cpuid; 31759bb0ec09SDavid Hildenbrand 31769bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 31779bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 31789bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 31799d8d5786SMichael Mueller } 31809d8d5786SMichael Mueller 3181c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 31825102ee87STony Krowiak { 3183c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 318445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 31851e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 31865102ee87STony Krowiak 3187e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3188e585b24aSTony Krowiak return; 3189e585b24aSTony Krowiak 3190ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 3191ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 3192ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 3193ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 3194ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 3195ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 3196ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 31975102ee87STony Krowiak } 31985102ee87STony Krowiak 31997d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 32007d43bafcSEugene (jno) Dvurechenski { 32017d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 32025e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 32037d43bafcSEugene (jno) Dvurechenski else 32047d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 32057d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 32067d43bafcSEugene (jno) Dvurechenski } 32077d43bafcSEugene (jno) Dvurechenski 320809340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm) 320909340b2fSMatthew Rosato { 321009340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 321109340b2fSMatthew Rosato kvm_s390_pci_clear_list(kvm); 321209340b2fSMatthew Rosato 321309340b2fSMatthew Rosato __kvm_arch_free_vm(kvm); 321409340b2fSMatthew Rosato } 321509340b2fSMatthew Rosato 3216e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 3217b0c632dbSHeiko Carstens { 3218c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 32199d8d5786SMichael Mueller int i, rc; 3220b0c632dbSHeiko Carstens char debug_name[16]; 3221f6c137ffSChristian Borntraeger static unsigned long sca_offset; 3222b0c632dbSHeiko Carstens 3223e08b9637SCarsten Otte rc = -EINVAL; 3224e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3225e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 3226e08b9637SCarsten Otte goto out_err; 3227e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 3228e08b9637SCarsten Otte goto out_err; 3229e08b9637SCarsten Otte #else 3230e08b9637SCarsten Otte if (type) 3231e08b9637SCarsten Otte goto out_err; 3232e08b9637SCarsten Otte #endif 3233e08b9637SCarsten Otte 3234b0c632dbSHeiko Carstens rc = s390_enable_sie(); 3235b0c632dbSHeiko Carstens if (rc) 3236d89f5effSJan Kiszka goto out_err; 3237b0c632dbSHeiko Carstens 3238b290411aSCarsten Otte rc = -ENOMEM; 3239b290411aSCarsten Otte 324076a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 324176a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 32425e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 32439ac96d75SDavid Hildenbrand /* start with basic SCA */ 324476a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 3245b0c632dbSHeiko Carstens if (!kvm->arch.sca) 3246d89f5effSJan Kiszka goto out_err; 32470d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 3248c5c2c393SDavid Hildenbrand sca_offset += 16; 3249bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 3250c5c2c393SDavid Hildenbrand sca_offset = 0; 3251bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 3252bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 32530d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 3254b0c632dbSHeiko Carstens 3255b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 3256b0c632dbSHeiko Carstens 32571cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 3258b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 325940f5b735SDominik Dingel goto out_err; 3260b0c632dbSHeiko Carstens 326119114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 3262c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 3263c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 3264c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 326540f5b735SDominik Dingel goto out_err; 32669d8d5786SMichael Mueller 326725c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 3268c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 3269c3b9e3e1SChristian Borntraeger 3270c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 327117e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 3272c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 3273c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 327417e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 3275c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 3276c3b9e3e1SChristian Borntraeger } 3277346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 3278981467c9SMichael Mueller 32791935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 32801935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 32811935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 32821935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 328395ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 328495ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 32851bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 32861bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 32871bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 32881bab1c02SClaudio Imbrenda } 328995ca2cb5SJanosch Frank 329005f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 329105f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 329205f31e3bSPierre Morel 32939bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 329437c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 32959d8d5786SMichael Mueller 3296c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 32975102ee87STony Krowiak 329809340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 329909340b2fSMatthew Rosato mutex_lock(&kvm->lock); 330009340b2fSMatthew Rosato kvm_s390_pci_init_list(kvm); 330109340b2fSMatthew Rosato kvm_s390_vcpu_pci_enable_interp(kvm); 330209340b2fSMatthew Rosato mutex_unlock(&kvm->lock); 330309340b2fSMatthew Rosato } 330409340b2fSMatthew Rosato 330551978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 3306ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 33076d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 33086d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 33098a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 3310a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 3311ba5c1e9bSCarsten Otte 3312b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 331378f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 3314b0c632dbSHeiko Carstens 3315e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 3316e08b9637SCarsten Otte kvm->arch.gmap = NULL; 3317a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 3318e08b9637SCarsten Otte } else { 331932e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 3320ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 332132e6b236SGuenther Hutzl else 3322ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 332332e6b236SGuenther Hutzl sclp.hamax + 1); 33246ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 3325598841caSCarsten Otte if (!kvm->arch.gmap) 332640f5b735SDominik Dingel goto out_err; 33272c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 332824eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 3329e08b9637SCarsten Otte } 3330fa6b7fe9SCornelia Huck 3331c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 333255531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 33338ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 3334a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 3335cc674ef2SMichael Mueller if (use_gisa) 3336d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 3337fb491d55SClaudio Imbrenda INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup); 3338fb491d55SClaudio Imbrenda kvm->arch.pv.set_aside = NULL; 33398335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 33408ad35755SDavid Hildenbrand 3341d89f5effSJan Kiszka return 0; 3342d89f5effSJan Kiszka out_err: 3343c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 334440f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 33457d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 334678f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 3347d89f5effSJan Kiszka return rc; 3348b0c632dbSHeiko Carstens } 3349b0c632dbSHeiko Carstens 3350d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 3351d329c035SChristian Borntraeger { 335229b40f10SJanosch Frank u16 rc, rrc; 335329b40f10SJanosch Frank 3354d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 3355ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 335667335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 33573c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 3358bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 3359a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 336024fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 336127e0393fSCarsten Otte 336227e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 33636ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 336427e0393fSCarsten Otte 3365e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 3366b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 336729b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 336829b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 336929b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 3370d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 3371d329c035SChristian Borntraeger } 3372d329c035SChristian Borntraeger 3373b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 3374b0c632dbSHeiko Carstens { 337529b40f10SJanosch Frank u16 rc, rrc; 337629b40f10SJanosch Frank 337727592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 33787d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 3379d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 338029b40f10SJanosch Frank /* 338129b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 338229b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 3383fb491d55SClaudio Imbrenda * can mess with the pv state. 338429b40f10SJanosch Frank */ 3385fb491d55SClaudio Imbrenda kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc); 3386ca2fd060SClaudio Imbrenda /* 3387ca2fd060SClaudio Imbrenda * Remove the mmu notifier only when the whole KVM VM is torn down, 3388ca2fd060SClaudio Imbrenda * and only if one was registered to begin with. If the VM is 3389ca2fd060SClaudio Imbrenda * currently not protected, but has been previously been protected, 3390ca2fd060SClaudio Imbrenda * then it's possible that the notifier is still registered. 3391ca2fd060SClaudio Imbrenda */ 3392ca2fd060SClaudio Imbrenda if (kvm->arch.pv.mmu_notifier.ops) 3393ca2fd060SClaudio Imbrenda mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm); 3394ca2fd060SClaudio Imbrenda 339529b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 3396c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 339727e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 33986ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 3399841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 340067335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 3401a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 34028335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 3403b0c632dbSHeiko Carstens } 3404b0c632dbSHeiko Carstens 3405b0c632dbSHeiko Carstens /* Section: vcpu related */ 3406dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 3407b0c632dbSHeiko Carstens { 34086ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 340927e0393fSCarsten Otte if (!vcpu->arch.gmap) 341027e0393fSCarsten Otte return -ENOMEM; 34112c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 3412dafd032aSDominik Dingel 341327e0393fSCarsten Otte return 0; 341427e0393fSCarsten Otte } 341527e0393fSCarsten Otte 3416a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 3417a6e2f683SEugene (jno) Dvurechenski { 3418a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 3419a6940674SDavid Hildenbrand return; 34205e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 34217d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 34227d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 34237d43bafcSEugene (jno) Dvurechenski 34247d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 34257d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 34267d43bafcSEugene (jno) Dvurechenski } else { 3427bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 3428a6e2f683SEugene (jno) Dvurechenski 3429a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3430a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 3431a6e2f683SEugene (jno) Dvurechenski } 34325e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 34337d43bafcSEugene (jno) Dvurechenski } 3434a6e2f683SEugene (jno) Dvurechenski 3435eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 3436a6e2f683SEugene (jno) Dvurechenski { 3437a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3438fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca); 3439a6940674SDavid Hildenbrand 3440a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 3441fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3442fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3443f07afa04SDavid Hildenbrand return; 3444a6940674SDavid Hildenbrand } 3445eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 3446eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 3447eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 3448fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 34497d43bafcSEugene (jno) Dvurechenski 3450fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3451fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3452fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK; 34530c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 3454eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 34557d43bafcSEugene (jno) Dvurechenski } else { 3456eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 3457fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 3458a6e2f683SEugene (jno) Dvurechenski 3459fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3460fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3461fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3462eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3463a6e2f683SEugene (jno) Dvurechenski } 3464eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 34655e044315SEugene (jno) Dvurechenski } 34665e044315SEugene (jno) Dvurechenski 34675e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 34685e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 34695e044315SEugene (jno) Dvurechenski { 34705e044315SEugene (jno) Dvurechenski d->sda = s->sda; 34715e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 34725e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 34735e044315SEugene (jno) Dvurechenski } 34745e044315SEugene (jno) Dvurechenski 34755e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 34765e044315SEugene (jno) Dvurechenski { 34775e044315SEugene (jno) Dvurechenski int i; 34785e044315SEugene (jno) Dvurechenski 34795e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 34805e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 34815e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 34825e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 34835e044315SEugene (jno) Dvurechenski } 34845e044315SEugene (jno) Dvurechenski 34855e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 34865e044315SEugene (jno) Dvurechenski { 34875e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 34885e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 34895e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 349046808a4cSMarc Zyngier unsigned long vcpu_idx; 34915e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 3492fe0ef003SNico Boehr phys_addr_t new_sca_phys; 34935e044315SEugene (jno) Dvurechenski 349429b40f10SJanosch Frank if (kvm->arch.use_esca) 349529b40f10SJanosch Frank return 0; 349629b40f10SJanosch Frank 3497c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 34985e044315SEugene (jno) Dvurechenski if (!new_sca) 34995e044315SEugene (jno) Dvurechenski return -ENOMEM; 35005e044315SEugene (jno) Dvurechenski 3501fe0ef003SNico Boehr new_sca_phys = virt_to_phys(new_sca); 3502fe0ef003SNico Boehr scaoh = new_sca_phys >> 32; 3503fe0ef003SNico Boehr scaol = new_sca_phys & ESCA_SCAOL_MASK; 35045e044315SEugene (jno) Dvurechenski 35055e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 35065e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 35075e044315SEugene (jno) Dvurechenski 35085e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 35095e044315SEugene (jno) Dvurechenski 35105e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 35115e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 35125e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 35130c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 35145e044315SEugene (jno) Dvurechenski } 35155e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 35165e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 35175e044315SEugene (jno) Dvurechenski 35185e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 35195e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 35205e044315SEugene (jno) Dvurechenski 35215e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 35225e044315SEugene (jno) Dvurechenski 35238335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 35248335713aSChristian Borntraeger old_sca, kvm->arch.sca); 35255e044315SEugene (jno) Dvurechenski return 0; 35267d43bafcSEugene (jno) Dvurechenski } 3527a6e2f683SEugene (jno) Dvurechenski 3528a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 3529a6e2f683SEugene (jno) Dvurechenski { 35305e044315SEugene (jno) Dvurechenski int rc; 35315e044315SEugene (jno) Dvurechenski 3532a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3533a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 3534a6940674SDavid Hildenbrand return true; 3535a6940674SDavid Hildenbrand return false; 3536a6940674SDavid Hildenbrand } 35375e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 35385e044315SEugene (jno) Dvurechenski return true; 353976a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 35405e044315SEugene (jno) Dvurechenski return false; 35415e044315SEugene (jno) Dvurechenski 35425e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 35435e044315SEugene (jno) Dvurechenski 35445e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 3545a6e2f683SEugene (jno) Dvurechenski } 3546a6e2f683SEugene (jno) Dvurechenski 3547db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3548db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3549db0758b2SDavid Hildenbrand { 3550db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 35519c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3552db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35539c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3554db0758b2SDavid Hildenbrand } 3555db0758b2SDavid Hildenbrand 3556db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3557db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3558db0758b2SDavid Hildenbrand { 3559db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 35609c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3561db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3562db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 35639c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3564db0758b2SDavid Hildenbrand } 3565db0758b2SDavid Hildenbrand 3566db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3567db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3568db0758b2SDavid Hildenbrand { 3569db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3570db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3571db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3572db0758b2SDavid Hildenbrand } 3573db0758b2SDavid Hildenbrand 3574db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3575db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3576db0758b2SDavid Hildenbrand { 3577db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3578db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3579db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3580db0758b2SDavid Hildenbrand } 3581db0758b2SDavid Hildenbrand 3582db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3583db0758b2SDavid Hildenbrand { 3584db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3585db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3586db0758b2SDavid Hildenbrand preempt_enable(); 3587db0758b2SDavid Hildenbrand } 3588db0758b2SDavid Hildenbrand 3589db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3590db0758b2SDavid Hildenbrand { 3591db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3592db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3593db0758b2SDavid Hildenbrand preempt_enable(); 3594db0758b2SDavid Hildenbrand } 3595db0758b2SDavid Hildenbrand 35964287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 35974287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 35984287f247SDavid Hildenbrand { 3599db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 36009c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3601db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3602db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 36034287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 36049c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3605db0758b2SDavid Hildenbrand preempt_enable(); 36064287f247SDavid Hildenbrand } 36074287f247SDavid Hildenbrand 3608db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 36094287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 36104287f247SDavid Hildenbrand { 36119c23a131SDavid Hildenbrand unsigned int seq; 3612db0758b2SDavid Hildenbrand __u64 value; 3613db0758b2SDavid Hildenbrand 3614db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 36154287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3616db0758b2SDavid Hildenbrand 36179c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 36189c23a131SDavid Hildenbrand do { 36199c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 36209c23a131SDavid Hildenbrand /* 36219c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 36229c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 36239c23a131SDavid Hildenbrand */ 36249c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3625db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 36269c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 36279c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3628db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 36299c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 36309c23a131SDavid Hildenbrand preempt_enable(); 3631db0758b2SDavid Hildenbrand return value; 36324287f247SDavid Hildenbrand } 36334287f247SDavid Hildenbrand 3634b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3635b0c632dbSHeiko Carstens { 36369977e886SHendrik Brueckner 363737d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3638ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 36395ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3640db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 364101a745acSDavid Hildenbrand vcpu->cpu = cpu; 3642b0c632dbSHeiko Carstens } 3643b0c632dbSHeiko Carstens 3644b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3645b0c632dbSHeiko Carstens { 364601a745acSDavid Hildenbrand vcpu->cpu = -1; 36475ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3648db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 36499daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 365037d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 365137d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 36529977e886SHendrik Brueckner 3653b0c632dbSHeiko Carstens } 3654b0c632dbSHeiko Carstens 365531928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 365642897d86SMarcelo Tosatti { 365772f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3658fdf03650SFan Zhang preempt_disable(); 365972f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3660d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3661fdf03650SFan Zhang preempt_enable(); 366272f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 366325508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3664dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3665eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 366625508824SDavid Hildenbrand } 36676502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 36686502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 366937d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 367037d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 367142897d86SMarcelo Tosatti } 367242897d86SMarcelo Tosatti 36738ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 36748ec2fa52SChristian Borntraeger { 36758ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 36768ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 36778ec2fa52SChristian Borntraeger return true; 36788ec2fa52SChristian Borntraeger return false; 36798ec2fa52SChristian Borntraeger } 36808ec2fa52SChristian Borntraeger 36818ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 36828ec2fa52SChristian Borntraeger { 36838ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 36848ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 36858ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 36868ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 36878ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 36888ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 36898ec2fa52SChristian Borntraeger 36908ec2fa52SChristian Borntraeger } 36918ec2fa52SChristian Borntraeger 36925102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 36935102ee87STony Krowiak { 3694e585b24aSTony Krowiak /* 3695e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3696e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3697e585b24aSTony Krowiak */ 3698e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 36995102ee87STony Krowiak return; 37005102ee87STony Krowiak 3701e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3702a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 370337940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 37048ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3705a374e892STony Krowiak 3706e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3707e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3708e585b24aSTony Krowiak 3709e585b24aSTony Krowiak /* Set up protected key support */ 37108ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3711a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 37128ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 37138ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 37148ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 37158ec2fa52SChristian Borntraeger } 37168ec2fa52SChristian Borntraeger 3717a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3718a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 37195102ee87STony Krowiak } 37205102ee87STony Krowiak 3721b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3722b31605c1SDominik Dingel { 3723fe0ef003SNico Boehr free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo)); 3724b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3725b31605c1SDominik Dingel } 3726b31605c1SDominik Dingel 3727b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3728b31605c1SDominik Dingel { 3729fe0ef003SNico Boehr void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); 3730fe0ef003SNico Boehr 3731fe0ef003SNico Boehr if (!cbrlo_page) 3732b31605c1SDominik Dingel return -ENOMEM; 3733fe0ef003SNico Boehr 3734fe0ef003SNico Boehr vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page); 3735b31605c1SDominik Dingel return 0; 3736b31605c1SDominik Dingel } 3737b31605c1SDominik Dingel 373891520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 373991520f1aSMichael Mueller { 374091520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 374191520f1aSMichael Mueller 374291520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 374380bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3744fe0ef003SNico Boehr vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list); 374591520f1aSMichael Mueller } 374691520f1aSMichael Mueller 3747ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3748ff72bb55SSean Christopherson { 3749b31605c1SDominik Dingel int rc = 0; 375029b40f10SJanosch Frank u16 uvrc, uvrrc; 3751b31288faSKonstantin Weitz 37529e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 37539e6dabefSCornelia Huck CPUSTAT_SM | 3754a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3755a4a4f191SGuenther Hutzl 375653df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3757ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 375853df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3759ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3760a4a4f191SGuenther Hutzl 376191520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 376291520f1aSMichael Mueller 3763bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3764bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 37650c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3766bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 37670c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 376824fe0195SPierre Morel if (test_kvm_facility(vcpu->kvm, 11)) 376924fe0195SPierre Morel vcpu->arch.sie_block->ecb |= ECB_PTF; 3770f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 37710c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 37727119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 37737119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 37747feb6bb8SMichael Mueller 3775c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 37760c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3777cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 37780c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 37790c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 378048ee7d3aSDavid Hildenbrand if (sclp.has_cei) 37810c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 378211ad65b7SDavid Hildenbrand if (sclp.has_ib) 37830c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 378437c5f6c8SDavid Hildenbrand if (sclp.has_siif) 37850c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 378637c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 37870c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 378818280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 37890c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 37900c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 379113211ea7SEric Farman } 37928fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 37938fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3794a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3795a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3796d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3797d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3798d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3799d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3800d7c5cb01SMichael Mueller } 3801fe0ef003SNico Boehr vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC; 3802fe0ef003SNico Boehr vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb); 3803730cd632SFarhan Ali 3804730cd632SFarhan Ali if (sclp.has_kss) 3805ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3806730cd632SFarhan Ali else 3807492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 38085a5e6536SMatthew Rosato 3809e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3810b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3811b31605c1SDominik Dingel if (rc) 3812b31605c1SDominik Dingel return rc; 3813b31288faSKonstantin Weitz } 38140ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3815ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 38169d8d5786SMichael Mueller 381767d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 381867d49d52SCollin Walling 38195102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 38205102ee87STony Krowiak 38213f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 38223f4bbb43SMatthew Rosato 382329b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 382429b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 382529b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 382629b40f10SJanosch Frank if (rc) 382729b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 382829b40f10SJanosch Frank } 382929b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 383029b40f10SJanosch Frank 3831b31605c1SDominik Dingel return rc; 3832b0c632dbSHeiko Carstens } 3833b0c632dbSHeiko Carstens 3834897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3835897cc38eSSean Christopherson { 3836897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3837897cc38eSSean Christopherson return -EINVAL; 3838897cc38eSSean Christopherson return 0; 3839897cc38eSSean Christopherson } 3840897cc38eSSean Christopherson 3841e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3842b0c632dbSHeiko Carstens { 38437feb6bb8SMichael Mueller struct sie_page *sie_page; 3844897cc38eSSean Christopherson int rc; 38454d47555aSCarsten Otte 3846da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3847c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 38487feb6bb8SMichael Mueller if (!sie_page) 3849e529ef66SSean Christopherson return -ENOMEM; 3850b0c632dbSHeiko Carstens 38517feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 3852fe0ef003SNico Boehr vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb); 38537feb6bb8SMichael Mueller 3854efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3855efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3856efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3857efed1104SDavid Hildenbrand 3858e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3859ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3860ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm); 38619c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3862ba5c1e9bSCarsten Otte 3863321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3864321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3865321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3866321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3867321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3868321f8ee5SSean Christopherson KVM_SYNC_CRS | 3869321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 387023a60f83SCollin Walling KVM_SYNC_PFAULT | 387123a60f83SCollin Walling KVM_SYNC_DIAG318; 3872321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3873321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3874321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3875321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3876321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3877321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3878321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3879321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3880321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3881321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3882321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3883321f8ee5SSean Christopherson */ 3884321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3885321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3886321f8ee5SSean Christopherson else 3887321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3888321f8ee5SSean Christopherson 3889321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3890321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3891321f8ee5SSean Christopherson if (rc) 3892a2017f17SSean Christopherson goto out_free_sie_block; 3893321f8ee5SSean Christopherson } 3894321f8ee5SSean Christopherson 3895e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3896e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3897e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3898b0c632dbSHeiko Carstens 3899ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3900ff72bb55SSean Christopherson if (rc) 3901ff72bb55SSean Christopherson goto out_ucontrol_uninit; 390224fe0195SPierre Morel 390324fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 3904e529ef66SSean Christopherson return 0; 3905e529ef66SSean Christopherson 3906ff72bb55SSean Christopherson out_ucontrol_uninit: 3907ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3908ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 39097b06bf2fSWei Yongjun out_free_sie_block: 39107b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3911e529ef66SSean Christopherson return rc; 3912b0c632dbSHeiko Carstens } 3913b0c632dbSHeiko Carstens 3914b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3915b0c632dbSHeiko Carstens { 39169b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 39179a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3918b0c632dbSHeiko Carstens } 3919b0c632dbSHeiko Carstens 3920199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3921199b5763SLongpeng(Mike) { 39220546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3923199b5763SLongpeng(Mike) } 3924199b5763SLongpeng(Mike) 392527406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 392649b99e1eSChristian Borntraeger { 3927805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 392861a6df54SDavid Hildenbrand exit_sie(vcpu); 392949b99e1eSChristian Borntraeger } 393049b99e1eSChristian Borntraeger 393127406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 393249b99e1eSChristian Borntraeger { 3933805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 393449b99e1eSChristian Borntraeger } 393549b99e1eSChristian Borntraeger 39368e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 39378e236546SChristian Borntraeger { 3938805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 393961a6df54SDavid Hildenbrand exit_sie(vcpu); 39408e236546SChristian Borntraeger } 39418e236546SChristian Borntraeger 39429ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 39439ea59728SDavid Hildenbrand { 39449ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 39459ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 39469ea59728SDavid Hildenbrand } 39479ea59728SDavid Hildenbrand 39488e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 39498e236546SChristian Borntraeger { 39509bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 39518e236546SChristian Borntraeger } 39528e236546SChristian Borntraeger 395349b99e1eSChristian Borntraeger /* 39549ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 395549b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 395649b99e1eSChristian Borntraeger * return immediately. */ 395749b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 395849b99e1eSChristian Borntraeger { 3959ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 39609ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 396149b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 396249b99e1eSChristian Borntraeger cpu_relax(); 396349b99e1eSChristian Borntraeger } 396449b99e1eSChristian Borntraeger 39658e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 39668e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 396749b99e1eSChristian Borntraeger { 3968df06dae3SSean Christopherson __kvm_make_request(req, vcpu); 39698e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 397049b99e1eSChristian Borntraeger } 397149b99e1eSChristian Borntraeger 3972414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3973414d3b07SMartin Schwidefsky unsigned long end) 39742c70fe44SChristian Borntraeger { 39752c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 39762c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3977414d3b07SMartin Schwidefsky unsigned long prefix; 397846808a4cSMarc Zyngier unsigned long i; 39792c70fe44SChristian Borntraeger 398065d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 398165d0b0d4SDavid Hildenbrand return; 3982414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3983414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3984414d3b07SMartin Schwidefsky return; 39852c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 39862c70fe44SChristian Borntraeger /* match against both prefix pages */ 3987414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3988414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3989414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3990414d3b07SMartin Schwidefsky start, end); 3991cc65c3a1SSean Christopherson kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 39922c70fe44SChristian Borntraeger } 39932c70fe44SChristian Borntraeger } 39942c70fe44SChristian Borntraeger } 39952c70fe44SChristian Borntraeger 39968b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 39978b905d28SChristian Borntraeger { 39988b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 39998b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 40006f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) { 40018b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 40028b905d28SChristian Borntraeger return true; 40038b905d28SChristian Borntraeger } 40048b905d28SChristian Borntraeger return false; 40058b905d28SChristian Borntraeger } 40068b905d28SChristian Borntraeger 4007b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 4008b6d33834SChristoffer Dall { 4009b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 4010b6d33834SChristoffer Dall BUG(); 4011b6d33834SChristoffer Dall return 0; 4012b6d33834SChristoffer Dall } 4013b6d33834SChristoffer Dall 401414eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 401514eebd91SCarsten Otte struct kvm_one_reg *reg) 401614eebd91SCarsten Otte { 401714eebd91SCarsten Otte int r = -EINVAL; 401814eebd91SCarsten Otte 401914eebd91SCarsten Otte switch (reg->id) { 402029b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 402129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 402229b7c71bSCarsten Otte (u32 __user *)reg->addr); 402329b7c71bSCarsten Otte break; 402429b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 402529b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 402629b7c71bSCarsten Otte (u64 __user *)reg->addr); 402729b7c71bSCarsten Otte break; 402846a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 40294287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 403046a6dd1cSJason J. herne (u64 __user *)reg->addr); 403146a6dd1cSJason J. herne break; 403246a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 403346a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 403446a6dd1cSJason J. herne (u64 __user *)reg->addr); 403546a6dd1cSJason J. herne break; 4036536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 4037536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 4038536336c2SDominik Dingel (u64 __user *)reg->addr); 4039536336c2SDominik Dingel break; 4040536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 4041536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 4042536336c2SDominik Dingel (u64 __user *)reg->addr); 4043536336c2SDominik Dingel break; 4044536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4045536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 4046536336c2SDominik Dingel (u64 __user *)reg->addr); 4047536336c2SDominik Dingel break; 4048672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4049672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 4050672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4051672550fbSChristian Borntraeger break; 4052afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4053afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 4054afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4055afa45ff5SChristian Borntraeger break; 405614eebd91SCarsten Otte default: 405714eebd91SCarsten Otte break; 405814eebd91SCarsten Otte } 405914eebd91SCarsten Otte 406014eebd91SCarsten Otte return r; 406114eebd91SCarsten Otte } 406214eebd91SCarsten Otte 406314eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 406414eebd91SCarsten Otte struct kvm_one_reg *reg) 406514eebd91SCarsten Otte { 406614eebd91SCarsten Otte int r = -EINVAL; 40674287f247SDavid Hildenbrand __u64 val; 406814eebd91SCarsten Otte 406914eebd91SCarsten Otte switch (reg->id) { 407029b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 407129b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 407229b7c71bSCarsten Otte (u32 __user *)reg->addr); 407329b7c71bSCarsten Otte break; 407429b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 407529b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 407629b7c71bSCarsten Otte (u64 __user *)reg->addr); 407729b7c71bSCarsten Otte break; 407846a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 40794287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 40804287f247SDavid Hildenbrand if (!r) 40814287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 408246a6dd1cSJason J. herne break; 408346a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 408446a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 408546a6dd1cSJason J. herne (u64 __user *)reg->addr); 408646a6dd1cSJason J. herne break; 4087536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 4088536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 4089536336c2SDominik Dingel (u64 __user *)reg->addr); 40909fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 40919fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4092536336c2SDominik Dingel break; 4093536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 4094536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 4095536336c2SDominik Dingel (u64 __user *)reg->addr); 4096536336c2SDominik Dingel break; 4097536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4098536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 4099536336c2SDominik Dingel (u64 __user *)reg->addr); 4100536336c2SDominik Dingel break; 4101672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4102672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 4103672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4104672550fbSChristian Borntraeger break; 4105afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4106afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 4107afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4108afa45ff5SChristian Borntraeger break; 410914eebd91SCarsten Otte default: 411014eebd91SCarsten Otte break; 411114eebd91SCarsten Otte } 411214eebd91SCarsten Otte 411314eebd91SCarsten Otte return r; 411414eebd91SCarsten Otte } 4115b6d33834SChristoffer Dall 41167de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 4117b0c632dbSHeiko Carstens { 41187de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 41197de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 41207de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 41217de3f142SJanosch Frank 41227de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 41237de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 41247de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 41257de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 41267de3f142SJanosch Frank } 41277de3f142SJanosch Frank 41287de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 41297de3f142SJanosch Frank { 41307de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 41317de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 41327de3f142SJanosch Frank 4133e93fc7b4SChristian Borntraeger /* 4134e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 4135e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 4136e93fc7b4SChristian Borntraeger */ 41377de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 41387de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 41397de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 41407de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 41417de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 41427de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 41437de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 41447de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 4145e93fc7b4SChristian Borntraeger 4146e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 4147e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 4148e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4149e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 4150e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 4151e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 4152e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 4153e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 4154e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 4155e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4156e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 4157e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 41587de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 41590f303504SJanosch Frank /* 41600f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 41610f303504SJanosch Frank * them are overlayed and they are not accessible in this case 41620f303504SJanosch Frank * anyway. 41630f303504SJanosch Frank */ 41640f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 41657de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 41667de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 41677de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 41680f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 41690f303504SJanosch Frank } 41707de3f142SJanosch Frank } 41717de3f142SJanosch Frank 41727de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 41737de3f142SJanosch Frank { 41747de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 41757de3f142SJanosch Frank 41767de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 41777de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 41787de3f142SJanosch Frank 41797de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 41807de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 41817de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 41827de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 41837de3f142SJanosch Frank 41847de3f142SJanosch Frank regs->etoken = 0; 41857de3f142SJanosch Frank regs->etoken_extension = 0; 4186b0c632dbSHeiko Carstens } 4187b0c632dbSHeiko Carstens 4188b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4189b0c632dbSHeiko Carstens { 4190875656feSChristoffer Dall vcpu_load(vcpu); 41915a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 4192875656feSChristoffer Dall vcpu_put(vcpu); 4193b0c632dbSHeiko Carstens return 0; 4194b0c632dbSHeiko Carstens } 4195b0c632dbSHeiko Carstens 4196b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4197b0c632dbSHeiko Carstens { 41981fc9b76bSChristoffer Dall vcpu_load(vcpu); 41995a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 42001fc9b76bSChristoffer Dall vcpu_put(vcpu); 4201b0c632dbSHeiko Carstens return 0; 4202b0c632dbSHeiko Carstens } 4203b0c632dbSHeiko Carstens 4204b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 4205b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4206b0c632dbSHeiko Carstens { 4207b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 4208b4ef9d4eSChristoffer Dall 420959674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 4210b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 4211b4ef9d4eSChristoffer Dall 4212b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 4213b0c632dbSHeiko Carstens return 0; 4214b0c632dbSHeiko Carstens } 4215b0c632dbSHeiko Carstens 4216b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 4217b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4218b0c632dbSHeiko Carstens { 4219bcdec41cSChristoffer Dall vcpu_load(vcpu); 4220bcdec41cSChristoffer Dall 422159674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 4222b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 4223bcdec41cSChristoffer Dall 4224bcdec41cSChristoffer Dall vcpu_put(vcpu); 4225b0c632dbSHeiko Carstens return 0; 4226b0c632dbSHeiko Carstens } 4227b0c632dbSHeiko Carstens 4228b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4229b0c632dbSHeiko Carstens { 42306a96bc7fSChristoffer Dall int ret = 0; 42316a96bc7fSChristoffer Dall 42326a96bc7fSChristoffer Dall vcpu_load(vcpu); 42336a96bc7fSChristoffer Dall 42346a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 42356a96bc7fSChristoffer Dall ret = -EINVAL; 42366a96bc7fSChristoffer Dall goto out; 42376a96bc7fSChristoffer Dall } 4238e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 42399abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4240a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 4241a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 42429abc2a08SDavid Hildenbrand else 4243a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 42446a96bc7fSChristoffer Dall 42456a96bc7fSChristoffer Dall out: 42466a96bc7fSChristoffer Dall vcpu_put(vcpu); 42476a96bc7fSChristoffer Dall return ret; 4248b0c632dbSHeiko Carstens } 4249b0c632dbSHeiko Carstens 4250b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4251b0c632dbSHeiko Carstens { 42521393123eSChristoffer Dall vcpu_load(vcpu); 42531393123eSChristoffer Dall 42549abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 42559abc2a08SDavid Hildenbrand save_fpu_regs(); 42569abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4257a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 4258a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 42599abc2a08SDavid Hildenbrand else 4260a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 4261e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 42621393123eSChristoffer Dall 42631393123eSChristoffer Dall vcpu_put(vcpu); 4264b0c632dbSHeiko Carstens return 0; 4265b0c632dbSHeiko Carstens } 4266b0c632dbSHeiko Carstens 4267b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 4268b0c632dbSHeiko Carstens { 4269b0c632dbSHeiko Carstens int rc = 0; 4270b0c632dbSHeiko Carstens 42717a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4272b0c632dbSHeiko Carstens rc = -EBUSY; 4273d7b0b5ebSCarsten Otte else { 4274d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 4275d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 4276d7b0b5ebSCarsten Otte } 4277b0c632dbSHeiko Carstens return rc; 4278b0c632dbSHeiko Carstens } 4279b0c632dbSHeiko Carstens 4280b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 4281b0c632dbSHeiko Carstens struct kvm_translation *tr) 4282b0c632dbSHeiko Carstens { 4283b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 4284b0c632dbSHeiko Carstens } 4285b0c632dbSHeiko Carstens 428627291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 428727291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 428827291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 428927291e21SDavid Hildenbrand 4290d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 4291d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 4292b0c632dbSHeiko Carstens { 429327291e21SDavid Hildenbrand int rc = 0; 429427291e21SDavid Hildenbrand 429566b56562SChristoffer Dall vcpu_load(vcpu); 429666b56562SChristoffer Dall 429727291e21SDavid Hildenbrand vcpu->guest_debug = 0; 429827291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 429927291e21SDavid Hildenbrand 430066b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 430166b56562SChristoffer Dall rc = -EINVAL; 430266b56562SChristoffer Dall goto out; 430366b56562SChristoffer Dall } 430466b56562SChristoffer Dall if (!sclp.has_gpere) { 430566b56562SChristoffer Dall rc = -EINVAL; 430666b56562SChristoffer Dall goto out; 430766b56562SChristoffer Dall } 430827291e21SDavid Hildenbrand 430927291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 431027291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 431127291e21SDavid Hildenbrand /* enforce guest PER */ 4312ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 431327291e21SDavid Hildenbrand 431427291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 431527291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 431627291e21SDavid Hildenbrand } else { 43179daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 431827291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 431927291e21SDavid Hildenbrand } 432027291e21SDavid Hildenbrand 432127291e21SDavid Hildenbrand if (rc) { 432227291e21SDavid Hildenbrand vcpu->guest_debug = 0; 432327291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 43249daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 432527291e21SDavid Hildenbrand } 432627291e21SDavid Hildenbrand 432766b56562SChristoffer Dall out: 432866b56562SChristoffer Dall vcpu_put(vcpu); 432927291e21SDavid Hildenbrand return rc; 4330b0c632dbSHeiko Carstens } 4331b0c632dbSHeiko Carstens 433262d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 433362d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 433462d9f0dbSMarcelo Tosatti { 4335fd232561SChristoffer Dall int ret; 4336fd232561SChristoffer Dall 4337fd232561SChristoffer Dall vcpu_load(vcpu); 4338fd232561SChristoffer Dall 43396352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 4340fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 43416352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 4342fd232561SChristoffer Dall 4343fd232561SChristoffer Dall vcpu_put(vcpu); 4344fd232561SChristoffer Dall return ret; 434562d9f0dbSMarcelo Tosatti } 434662d9f0dbSMarcelo Tosatti 434762d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 434862d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 434962d9f0dbSMarcelo Tosatti { 43506352e4d2SDavid Hildenbrand int rc = 0; 43516352e4d2SDavid Hildenbrand 4352e83dff5eSChristoffer Dall vcpu_load(vcpu); 4353e83dff5eSChristoffer Dall 43546352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 435567cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 43566352e4d2SDavid Hildenbrand 43576352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 43586352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 4359fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 43606352e4d2SDavid Hildenbrand break; 43616352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 4362fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 43636352e4d2SDavid Hildenbrand break; 43646352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 43657c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 43667c36a3fcSJanosch Frank rc = -ENXIO; 43677c36a3fcSJanosch Frank break; 43687c36a3fcSJanosch Frank } 43697c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 43707c36a3fcSJanosch Frank break; 43716352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 43723b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 43736352e4d2SDavid Hildenbrand default: 43746352e4d2SDavid Hildenbrand rc = -ENXIO; 43756352e4d2SDavid Hildenbrand } 43766352e4d2SDavid Hildenbrand 4377e83dff5eSChristoffer Dall vcpu_put(vcpu); 43786352e4d2SDavid Hildenbrand return rc; 437962d9f0dbSMarcelo Tosatti } 438062d9f0dbSMarcelo Tosatti 43818ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 43828ad35755SDavid Hildenbrand { 43838d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 43848ad35755SDavid Hildenbrand } 43858ad35755SDavid Hildenbrand 43862c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 43872c70fe44SChristian Borntraeger { 43888ad35755SDavid Hildenbrand retry: 43898e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 43902fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 4391586b7ccdSChristian Borntraeger return 0; 43922c70fe44SChristian Borntraeger /* 4393cc65c3a1SSean Christopherson * If the guest prefix changed, re-arm the ipte notifier for the 4394b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 43952c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 43962c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 43972c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 43982c70fe44SChristian Borntraeger */ 4399cc65c3a1SSean Christopherson if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) { 44002c70fe44SChristian Borntraeger int rc; 4401b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 4402fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 4403b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 4404aca411a4SJulius Niedworok if (rc) { 4405cc65c3a1SSean Christopherson kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 44062c70fe44SChristian Borntraeger return rc; 4407aca411a4SJulius Niedworok } 44088ad35755SDavid Hildenbrand goto retry; 44092c70fe44SChristian Borntraeger } 44108ad35755SDavid Hildenbrand 4411d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 4412d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 4413d3d692c8SDavid Hildenbrand goto retry; 4414d3d692c8SDavid Hildenbrand } 4415d3d692c8SDavid Hildenbrand 44168ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 44178ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 44188ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 4419ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 44208ad35755SDavid Hildenbrand } 44218ad35755SDavid Hildenbrand goto retry; 44228ad35755SDavid Hildenbrand } 44238ad35755SDavid Hildenbrand 44248ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 44258ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 44268ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 44279daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 44288ad35755SDavid Hildenbrand } 44298ad35755SDavid Hildenbrand goto retry; 44308ad35755SDavid Hildenbrand } 44318ad35755SDavid Hildenbrand 44326502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 44336502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 44346502a34cSDavid Hildenbrand goto retry; 44356502a34cSDavid Hildenbrand } 44366502a34cSDavid Hildenbrand 4437190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 4438190df4a2SClaudio Imbrenda /* 4439c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 4440190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 4441190df4a2SClaudio Imbrenda * functionalities needed for live migration. 4442190df4a2SClaudio Imbrenda */ 4443190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 4444190df4a2SClaudio Imbrenda goto retry; 4445190df4a2SClaudio Imbrenda } 4446190df4a2SClaudio Imbrenda 4447190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 4448190df4a2SClaudio Imbrenda /* 4449c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 4450c9f0a2b8SJanosch Frank * CMM has been used. 4451190df4a2SClaudio Imbrenda */ 4452190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 4453c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 4454190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 4455190df4a2SClaudio Imbrenda goto retry; 4456190df4a2SClaudio Imbrenda } 4457190df4a2SClaudio Imbrenda 44583194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 44593194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 44600759d068SDavid Hildenbrand 44612c70fe44SChristian Borntraeger return 0; 44622c70fe44SChristian Borntraeger } 44632c70fe44SChristian Borntraeger 4464c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 44658fa1696eSCollin L. Walling { 44668fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 44672cfd7b73SHeiko Carstens union tod_clock clk; 446846808a4cSMarc Zyngier unsigned long i; 44698fa1696eSCollin L. Walling 44708fa1696eSCollin L. Walling preempt_disable(); 44718fa1696eSCollin L. Walling 44722cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 44738fa1696eSCollin L. Walling 44742cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 44750e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 44760e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 44772cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 44788fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 44798fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 44800e7def5fSDavid Hildenbrand } 44818fa1696eSCollin L. Walling 44828fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 44838fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 44848fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 44858fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 44868fa1696eSCollin L. Walling } 44878fa1696eSCollin L. Walling 44888fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 44898fa1696eSCollin L. Walling preempt_enable(); 4490c0573ba5SClaudio Imbrenda } 4491c0573ba5SClaudio Imbrenda 4492c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4493c0573ba5SClaudio Imbrenda { 4494c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock)) 4495c0573ba5SClaudio Imbrenda return 0; 4496c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 4497c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock); 4498c0573ba5SClaudio Imbrenda return 1; 4499c0573ba5SClaudio Imbrenda } 4500c0573ba5SClaudio Imbrenda 4501fa576c58SThomas Huth /** 4502fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 4503fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 4504fa576c58SThomas Huth * @gpa: Guest physical address 4505fa576c58SThomas Huth * @writable: Whether the page should be writable or not 4506fa576c58SThomas Huth * 4507fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 4508fa576c58SThomas Huth * 4509fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 4510fa576c58SThomas Huth */ 4511fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 451224eb3a82SDominik Dingel { 4513527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 4514527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 451524eb3a82SDominik Dingel } 451624eb3a82SDominik Dingel 45173c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 45183c038e6bSDominik Dingel unsigned long token) 45193c038e6bSDominik Dingel { 45203c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 4521383d0b05SJens Freimann struct kvm_s390_irq irq; 45223c038e6bSDominik Dingel 45233c038e6bSDominik Dingel if (start_token) { 4524383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 4525383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 4526383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 45273c038e6bSDominik Dingel } else { 45283c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 4529383d0b05SJens Freimann inti.parm64 = token; 45303c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 45313c038e6bSDominik Dingel } 45323c038e6bSDominik Dingel } 45333c038e6bSDominik Dingel 45342a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 45353c038e6bSDominik Dingel struct kvm_async_pf *work) 45363c038e6bSDominik Dingel { 45373c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 45383c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 45392a18b7e7SVitaly Kuznetsov 45402a18b7e7SVitaly Kuznetsov return true; 45413c038e6bSDominik Dingel } 45423c038e6bSDominik Dingel 45433c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 45443c038e6bSDominik Dingel struct kvm_async_pf *work) 45453c038e6bSDominik Dingel { 45463c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 45473c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 45483c038e6bSDominik Dingel } 45493c038e6bSDominik Dingel 45503c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 45513c038e6bSDominik Dingel struct kvm_async_pf *work) 45523c038e6bSDominik Dingel { 45533c038e6bSDominik Dingel /* s390 will always inject the page directly */ 45543c038e6bSDominik Dingel } 45553c038e6bSDominik Dingel 45567c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 45573c038e6bSDominik Dingel { 45583c038e6bSDominik Dingel /* 45593c038e6bSDominik Dingel * s390 will always inject the page directly, 45603c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 45613c038e6bSDominik Dingel */ 45623c038e6bSDominik Dingel return true; 45633c038e6bSDominik Dingel } 45643c038e6bSDominik Dingel 4565e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 45663c038e6bSDominik Dingel { 45673c038e6bSDominik Dingel hva_t hva; 45683c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 45693c038e6bSDominik Dingel 45703c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4571e8c22266SVitaly Kuznetsov return false; 45723c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 45733c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4574e8c22266SVitaly Kuznetsov return false; 45753c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4576e8c22266SVitaly Kuznetsov return false; 45779a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4578e8c22266SVitaly Kuznetsov return false; 4579b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4580e8c22266SVitaly Kuznetsov return false; 45813c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4582e8c22266SVitaly Kuznetsov return false; 45833c038e6bSDominik Dingel 458481480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 458581480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 458681480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4587e8c22266SVitaly Kuznetsov return false; 45883c038e6bSDominik Dingel 4589e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 45903c038e6bSDominik Dingel } 45913c038e6bSDominik Dingel 45923fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4593b0c632dbSHeiko Carstens { 45943fb4c40fSThomas Huth int rc, cpuflags; 4595e168bf8dSCarsten Otte 45963c038e6bSDominik Dingel /* 45973c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 45983c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 45993c038e6bSDominik Dingel * handled outside the worker. 46003c038e6bSDominik Dingel */ 46013c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 46023c038e6bSDominik Dingel 46037ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 46047ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4605b0c632dbSHeiko Carstens 4606b0c632dbSHeiko Carstens if (need_resched()) 4607b0c632dbSHeiko Carstens schedule(); 4608b0c632dbSHeiko Carstens 460979395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 461079395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 461179395031SJens Freimann if (rc) 461279395031SJens Freimann return rc; 461379395031SJens Freimann } 46140ff31867SCarsten Otte 46152c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 46162c70fe44SChristian Borntraeger if (rc) 46172c70fe44SChristian Borntraeger return rc; 46182c70fe44SChristian Borntraeger 461927291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 462027291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 462127291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 462227291e21SDavid Hildenbrand } 462327291e21SDavid Hildenbrand 46244eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 46259f30f621SMichael Mueller 4626b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 46273fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 46283fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 46293fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 46302b29a9fdSDominik Dingel 46313fb4c40fSThomas Huth return 0; 46323fb4c40fSThomas Huth } 46333fb4c40fSThomas Huth 4634492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4635492d8642SThomas Huth { 463656317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 463756317920SDavid Hildenbrand .code = PGM_ADDRESSING, 463856317920SDavid Hildenbrand }; 463956317920SDavid Hildenbrand u8 opcode, ilen; 4640492d8642SThomas Huth int rc; 4641492d8642SThomas Huth 4642492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4643492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4644492d8642SThomas Huth 4645492d8642SThomas Huth /* 4646492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4647492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4648492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4649492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4650492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4651492d8642SThomas Huth * to be able to forward the PSW. 4652492d8642SThomas Huth */ 46533fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 465456317920SDavid Hildenbrand ilen = insn_length(opcode); 46559b0d721aSDavid Hildenbrand if (rc < 0) { 46569b0d721aSDavid Hildenbrand return rc; 46579b0d721aSDavid Hildenbrand } else if (rc) { 46589b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 46599b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 46609b0d721aSDavid Hildenbrand * nullification if necessary. 46619b0d721aSDavid Hildenbrand */ 46629b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 46639b0d721aSDavid Hildenbrand ilen = 4; 46649b0d721aSDavid Hildenbrand } 466556317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 466656317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 466756317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4668492d8642SThomas Huth } 4669492d8642SThomas Huth 46703fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 46713fb4c40fSThomas Huth { 46724d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 46734d62fcc0SQingFeng Hao struct sie_page *sie_page; 46744d62fcc0SQingFeng Hao 46752b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 46762b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 46772b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 46782b29a9fdSDominik Dingel 467927291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 468027291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 468127291e21SDavid Hildenbrand 46827ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 46837ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 468471f116bfSDavid Hildenbrand 46854d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 46864d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 46874d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 46884d62fcc0SQingFeng Hao struct sie_page, sie_block); 46894d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 46904d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 46914d62fcc0SQingFeng Hao return 0; 46924d62fcc0SQingFeng Hao } 46934d62fcc0SQingFeng Hao 469471f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 469571f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 469671f116bfSDavid Hildenbrand 469771f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 469871f116bfSDavid Hildenbrand return rc; 469971f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 470071f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 470171f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 470271f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 470371f116bfSDavid Hildenbrand return -EREMOTE; 470471f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 470571f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 470671f116bfSDavid Hildenbrand return 0; 4707210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4708210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4709210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4710210b1607SThomas Huth current->thread.gmap_addr; 4711210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 471271f116bfSDavid Hildenbrand return -EREMOTE; 471324eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 47143c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 471524eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 471671f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 471771f116bfSDavid Hildenbrand return 0; 471850a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 471971f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4720fa576c58SThomas Huth } 472171f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 47223fb4c40fSThomas Huth } 47233fb4c40fSThomas Huth 47243adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 47253fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 47263fb4c40fSThomas Huth { 47273fb4c40fSThomas Huth int rc, exit_reason; 4728c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 47293fb4c40fSThomas Huth 4730800c1065SThomas Huth /* 4731800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4732800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4733800c1065SThomas Huth */ 47342031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 4735800c1065SThomas Huth 4736a76ccff6SThomas Huth do { 47373fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 47383fb4c40fSThomas Huth if (rc) 4739a76ccff6SThomas Huth break; 47403fb4c40fSThomas Huth 47412031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 47423fb4c40fSThomas Huth /* 4743a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4744a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 47453fb4c40fSThomas Huth */ 47460097d12eSChristian Borntraeger local_irq_disable(); 47476edaa530SPaolo Bonzini guest_enter_irqoff(); 4748db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 47490097d12eSChristian Borntraeger local_irq_enable(); 4750c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4751c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4752c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4753c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4754c8aac234SJanosch Frank } 475556e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 475656e62a73SSven Schnelle load_fpu_regs(); 4757a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4758a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4759c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4760c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4761c8aac234SJanosch Frank sie_page->pv_grregs, 4762c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 47633adae0b4SJanosch Frank /* 47643adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 47653adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 47663adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 47673adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 47683adae0b4SJanosch Frank */ 47693adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 47703adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 47713adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 47723adae0b4SJanosch Frank } 4773c8aac234SJanosch Frank } 47740097d12eSChristian Borntraeger local_irq_disable(); 4775db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 47766edaa530SPaolo Bonzini guest_exit_irqoff(); 47770097d12eSChristian Borntraeger local_irq_enable(); 47782031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 47793fb4c40fSThomas Huth 47803fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 478127291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 47823fb4c40fSThomas Huth 47832031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 4784e168bf8dSCarsten Otte return rc; 4785b0c632dbSHeiko Carstens } 4786b0c632dbSHeiko Carstens 47872f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4788b028ee3eSDavid Hildenbrand { 47892f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 47904d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 47914e0b1ab7SFan Zhang struct gs_cb *gscb; 47924d5f2c04SChristian Borntraeger 47934d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 47944e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4795b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4796b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4797b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4798b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4799b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4800b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4801b028ee3eSDavid Hildenbrand } 4802b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4803b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4804b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4805b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 48069fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 48079fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4808b028ee3eSDavid Hildenbrand } 480923a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 481023a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 481123a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 48123fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 481323a60f83SCollin Walling } 481480cd8763SFan Zhang /* 481580cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 481680cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 481780cd8763SFan Zhang */ 481880cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 48194d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4820bb59c2daSAlice Frosi riccb->v && 48210c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 48224d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 48230c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 482480cd8763SFan Zhang } 48254e0b1ab7SFan Zhang /* 48264e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 48274e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 48284e0b1ab7SFan Zhang */ 48294e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 48304e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 48314e0b1ab7SFan Zhang gscb->gssm && 48324e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 48334e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 48344e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 48354e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 48364e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 483780cd8763SFan Zhang } 483835b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 483935b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 484035b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 484135b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 484235b3fde6SChristian Borntraeger } 48434e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 48444e0b1ab7SFan Zhang preempt_disable(); 48454e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 48464e0b1ab7SFan Zhang if (current->thread.gs_cb) { 48474e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 48484e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 48494e0b1ab7SFan Zhang } 48504e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 48514e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 48524e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 48534e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 48544e0b1ab7SFan Zhang } 48554e0b1ab7SFan Zhang preempt_enable(); 48564e0b1ab7SFan Zhang } 4857a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4858811ea797SJanosch Frank } 4859811ea797SJanosch Frank 48602f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4861811ea797SJanosch Frank { 48622f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48632f0a83beSTianjia Zhang 4864811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4865811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4866811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4867811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4868811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4869811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4870811ea797SJanosch Frank } 4871811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4872811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4873811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4874811ea797SJanosch Frank } 4875811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4876811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4877811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4878811ea797SJanosch Frank save_fpu_regs(); 4879811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4880811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4881811ea797SJanosch Frank if (MACHINE_HAS_VX) 4882811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4883811ea797SJanosch Frank else 4884811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4885811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4886811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4887811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4888811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4889811ea797SJanosch Frank 4890811ea797SJanosch Frank /* Sync fmt2 only data */ 4891811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 48922f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4893811ea797SJanosch Frank } else { 4894811ea797SJanosch Frank /* 4895811ea797SJanosch Frank * In several places we have to modify our internal view to 4896811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4897811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4898811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4899811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4900811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4901811ea797SJanosch Frank * do only accept the condition code from userspace. 4902811ea797SJanosch Frank */ 4903811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4904811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4905811ea797SJanosch Frank PSW_MASK_CC; 4906811ea797SJanosch Frank } 490780cd8763SFan Zhang 4908b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4909b028ee3eSDavid Hildenbrand } 4910b028ee3eSDavid Hildenbrand 49112f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4912b028ee3eSDavid Hildenbrand { 49132f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 49142f0a83beSTianjia Zhang 4915b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4916b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4917b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 491835b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 491923a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 49204e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 492144bada28SHeiko Carstens preempt_disable(); 49224e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 49234e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 49244e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 49254e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 49264e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 49274e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 49284e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 49294e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 493044bada28SHeiko Carstens preempt_enable(); 49314e0b1ab7SFan Zhang } 4932a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4933b028ee3eSDavid Hildenbrand } 4934b028ee3eSDavid Hildenbrand 49352f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4936811ea797SJanosch Frank { 49372f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 49382f0a83beSTianjia Zhang 4939811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4940811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4941811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4942811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4943811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4944811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4945811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4946811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4947811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4948811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4949811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4950811ea797SJanosch Frank /* Save guest register state */ 4951811ea797SJanosch Frank save_fpu_regs(); 4952811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4953811ea797SJanosch Frank /* Restore will be done lazily at return */ 4954811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4955811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4956811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 49572f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4958811ea797SJanosch Frank } 4959811ea797SJanosch Frank 49601b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4961b0c632dbSHeiko Carstens { 49621b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 49638f2abe6aSChristian Borntraeger int rc; 4964b0c632dbSHeiko Carstens 49650460eb35SJanosch Frank /* 49660460eb35SJanosch Frank * Running a VM while dumping always has the potential to 49670460eb35SJanosch Frank * produce inconsistent dump data. But for PV vcpus a SIE 49680460eb35SJanosch Frank * entry while dumping could also lead to a fatal validity 49690460eb35SJanosch Frank * intercept which we absolutely want to avoid. 49700460eb35SJanosch Frank */ 49710460eb35SJanosch Frank if (vcpu->kvm->arch.pv.dumping) 49720460eb35SJanosch Frank return -EINVAL; 49730460eb35SJanosch Frank 4974460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4975460df4c1SPaolo Bonzini return -EINTR; 4976460df4c1SPaolo Bonzini 4977200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4978200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4979200824f5SThomas Huth return -EINVAL; 4980200824f5SThomas Huth 4981accb757dSChristoffer Dall vcpu_load(vcpu); 4982accb757dSChristoffer Dall 498327291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 498427291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4985accb757dSChristoffer Dall rc = 0; 4986accb757dSChristoffer Dall goto out; 498727291e21SDavid Hildenbrand } 498827291e21SDavid Hildenbrand 498920b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4990b0c632dbSHeiko Carstens 4991fe28c786SJanosch Frank /* 4992fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4993fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4994fe28c786SJanosch Frank */ 49956352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 49966852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 49976352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4998ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 49996352e4d2SDavid Hildenbrand vcpu->vcpu_id); 5000accb757dSChristoffer Dall rc = -EINVAL; 5001accb757dSChristoffer Dall goto out; 50026352e4d2SDavid Hildenbrand } 5003b0c632dbSHeiko Carstens 50042f0a83beSTianjia Zhang sync_regs(vcpu); 5005db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 5006d7b0b5ebSCarsten Otte 5007dab4079dSHeiko Carstens might_fault(); 5008e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 50099ace903dSChristian Ehrhardt 5010b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 5011b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 50128f2abe6aSChristian Borntraeger rc = -EINTR; 5013b1d16c49SChristian Ehrhardt } 50148f2abe6aSChristian Borntraeger 501527291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 501627291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 501727291e21SDavid Hildenbrand rc = 0; 501827291e21SDavid Hildenbrand } 501927291e21SDavid Hildenbrand 50208f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 502171f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 50228f2abe6aSChristian Borntraeger rc = 0; 50238f2abe6aSChristian Borntraeger } 50248f2abe6aSChristian Borntraeger 5025db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 50262f0a83beSTianjia Zhang store_regs(vcpu); 5027d7b0b5ebSCarsten Otte 502820b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 5029b0c632dbSHeiko Carstens 5030b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 5031accb757dSChristoffer Dall out: 5032accb757dSChristoffer Dall vcpu_put(vcpu); 50337e8e6ab4SHeiko Carstens return rc; 5034b0c632dbSHeiko Carstens } 5035b0c632dbSHeiko Carstens 5036b0c632dbSHeiko Carstens /* 5037b0c632dbSHeiko Carstens * store status at address 5038b0c632dbSHeiko Carstens * we use have two special cases: 5039b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 5040b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 5041b0c632dbSHeiko Carstens */ 5042d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 5043b0c632dbSHeiko Carstens { 5044092670cdSCarsten Otte unsigned char archmode = 1; 50459abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 5046fda902cbSMichael Mueller unsigned int px; 50474287f247SDavid Hildenbrand u64 clkcomp, cputm; 5048d0bce605SHeiko Carstens int rc; 5049b0c632dbSHeiko Carstens 5050d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 5051d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 5052d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 5053b0c632dbSHeiko Carstens return -EFAULT; 5054d9a3a09aSMartin Schwidefsky gpa = 0; 5055d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 5056d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 5057b0c632dbSHeiko Carstens return -EFAULT; 5058d9a3a09aSMartin Schwidefsky gpa = px; 5059d9a3a09aSMartin Schwidefsky } else 5060d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 50619abc2a08SDavid Hildenbrand 50629abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 50639abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 50649522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 5065d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50669abc2a08SDavid Hildenbrand fprs, 128); 50679abc2a08SDavid Hildenbrand } else { 50689abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50696fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 50709abc2a08SDavid Hildenbrand } 5071d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 5072d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 5073d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 5074d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 5075d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 5076fda902cbSMichael Mueller &px, 4); 5077d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 50789abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 5079d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 5080d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 50814287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 5082d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 50834287f247SDavid Hildenbrand &cputm, 8); 5084178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 5085d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 5086d0bce605SHeiko Carstens &clkcomp, 8); 5087d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 5088d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 5089d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 5090d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 5091d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 5092b0c632dbSHeiko Carstens } 5093b0c632dbSHeiko Carstens 5094e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 5095e879892cSThomas Huth { 5096e879892cSThomas Huth /* 5097e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 509831d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 5099e879892cSThomas Huth * it into the save area 5100e879892cSThomas Huth */ 5101d0164ee2SHendrik Brueckner save_fpu_regs(); 51029abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 5103e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 5104e879892cSThomas Huth 5105e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 5106e879892cSThomas Huth } 5107e879892cSThomas Huth 51088ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 51098ad35755SDavid Hildenbrand { 51108ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 51118e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 51128ad35755SDavid Hildenbrand } 51138ad35755SDavid Hildenbrand 51148ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 51158ad35755SDavid Hildenbrand { 511646808a4cSMarc Zyngier unsigned long i; 51178ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 51188ad35755SDavid Hildenbrand 51198ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 51208ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 51218ad35755SDavid Hildenbrand } 51228ad35755SDavid Hildenbrand } 51238ad35755SDavid Hildenbrand 51248ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 51258ad35755SDavid Hildenbrand { 512609a400e7SDavid Hildenbrand if (!sclp.has_ibs) 512709a400e7SDavid Hildenbrand return; 51288ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 51298e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 51308ad35755SDavid Hildenbrand } 51318ad35755SDavid Hildenbrand 5132fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 51336852d7b6SDavid Hildenbrand { 5134fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 51358ad35755SDavid Hildenbrand 51368ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 5137fe28c786SJanosch Frank return 0; 51388ad35755SDavid Hildenbrand 51396852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 51408ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5141433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 51428ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 51438ad35755SDavid Hildenbrand 5144fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 5145fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5146fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 5147fe28c786SJanosch Frank if (r) { 5148fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5149fe28c786SJanosch Frank return r; 5150fe28c786SJanosch Frank } 5151fe28c786SJanosch Frank } 5152fe28c786SJanosch Frank 51538ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5154113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 51558ad35755SDavid Hildenbrand started_vcpus++; 51568ad35755SDavid Hildenbrand } 51578ad35755SDavid Hildenbrand 51588ad35755SDavid Hildenbrand if (started_vcpus == 0) { 51598ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 51608ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 51618ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 51628ad35755SDavid Hildenbrand /* 51638ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 51648ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 516538860756SBhaskar Chowdhury * outstanding ENABLE requests. 51668ad35755SDavid Hildenbrand */ 51678ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 51688ad35755SDavid Hildenbrand } 51698ad35755SDavid Hildenbrand 51709daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 51718ad35755SDavid Hildenbrand /* 517272f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 517372f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 517472f21820SChristian Borntraeger * refresh our view. 517572f21820SChristian Borntraeger */ 517672f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 517772f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 517872f21820SChristian Borntraeger /* 51798ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 51808ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 51818ad35755SDavid Hildenbrand */ 5182d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 5183433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5184fe28c786SJanosch Frank return 0; 51856852d7b6SDavid Hildenbrand } 51866852d7b6SDavid Hildenbrand 5187fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 51886852d7b6SDavid Hildenbrand { 5189fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 51908ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 51918ad35755SDavid Hildenbrand 51928ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 5193fe28c786SJanosch Frank return 0; 51948ad35755SDavid Hildenbrand 51956852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 51968ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5197433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 51988ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 51998ad35755SDavid Hildenbrand 5200fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 5201fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5202fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 5203fe28c786SJanosch Frank if (r) { 5204fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5205fe28c786SJanosch Frank return r; 5206fe28c786SJanosch Frank } 5207fe28c786SJanosch Frank } 5208fe28c786SJanosch Frank 5209812de046SEric Farman /* 5210812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag, 5211812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders 5212812de046SEric Farman * have been fully processed. This will ensure that the VCPU 5213812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE. 5214812de046SEric Farman */ 5215812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 52166cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 521732f5ff63SDavid Hildenbrand 52188ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 52198ad35755SDavid Hildenbrand 52208ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5221113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 5222113d10bcSMarc Zyngier 5223113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 52248ad35755SDavid Hildenbrand started_vcpus++; 5225113d10bcSMarc Zyngier started_vcpu = tmp; 52268ad35755SDavid Hildenbrand } 52278ad35755SDavid Hildenbrand } 52288ad35755SDavid Hildenbrand 52298ad35755SDavid Hildenbrand if (started_vcpus == 1) { 52308ad35755SDavid Hildenbrand /* 52318ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 52328ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 52338ad35755SDavid Hildenbrand */ 52348ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 52358ad35755SDavid Hildenbrand } 52368ad35755SDavid Hildenbrand 5237433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5238fe28c786SJanosch Frank return 0; 52396852d7b6SDavid Hildenbrand } 52406852d7b6SDavid Hildenbrand 5241d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 5242d6712df9SCornelia Huck struct kvm_enable_cap *cap) 5243d6712df9SCornelia Huck { 5244d6712df9SCornelia Huck int r; 5245d6712df9SCornelia Huck 5246d6712df9SCornelia Huck if (cap->flags) 5247d6712df9SCornelia Huck return -EINVAL; 5248d6712df9SCornelia Huck 5249d6712df9SCornelia Huck switch (cap->cap) { 5250fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 5251fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 5252fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 5253c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 5254fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 5255fa6b7fe9SCornelia Huck } 5256fa6b7fe9SCornelia Huck r = 0; 5257fa6b7fe9SCornelia Huck break; 5258d6712df9SCornelia Huck default: 5259d6712df9SCornelia Huck r = -EINVAL; 5260d6712df9SCornelia Huck break; 5261d6712df9SCornelia Huck } 5262d6712df9SCornelia Huck return r; 5263d6712df9SCornelia Huck } 5264d6712df9SCornelia Huck 52650e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu, 526619e12277SJanosch Frank struct kvm_s390_mem_op *mop) 526719e12277SJanosch Frank { 526819e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 5269b99f4512SNico Boehr void *sida_addr; 527019e12277SJanosch Frank int r = 0; 527119e12277SJanosch Frank 527219e12277SJanosch Frank if (mop->flags || !mop->size) 527319e12277SJanosch Frank return -EINVAL; 527419e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 527519e12277SJanosch Frank return -EINVAL; 527619e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 527719e12277SJanosch Frank return -E2BIG; 52782c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu)) 52792c212e1bSJanis Schoetterl-Glausch return -EINVAL; 528019e12277SJanosch Frank 5281b99f4512SNico Boehr sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset; 5282b99f4512SNico Boehr 528319e12277SJanosch Frank switch (mop->op) { 528419e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 5285b99f4512SNico Boehr if (copy_to_user(uaddr, sida_addr, mop->size)) 528619e12277SJanosch Frank r = -EFAULT; 528719e12277SJanosch Frank 528819e12277SJanosch Frank break; 528919e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 5290b99f4512SNico Boehr if (copy_from_user(sida_addr, uaddr, mop->size)) 529119e12277SJanosch Frank r = -EFAULT; 529219e12277SJanosch Frank break; 529319e12277SJanosch Frank } 529419e12277SJanosch Frank return r; 529519e12277SJanosch Frank } 52960e1234c0SJanis Schoetterl-Glausch 52970e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, 529841408c28SThomas Huth struct kvm_s390_mem_op *mop) 529941408c28SThomas Huth { 530041408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 5301701422b3SJanis Schoetterl-Glausch enum gacc_mode acc_mode; 530241408c28SThomas Huth void *tmpbuf = NULL; 5303a41f505eSJanis Schoetterl-Glausch int r; 530441408c28SThomas Huth 5305a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION | 5306a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY | 5307a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_SKEY_PROTECTION); 5308a41f505eSJanis Schoetterl-Glausch if (r) 5309a41f505eSJanis Schoetterl-Glausch return r; 5310a41f505eSJanis Schoetterl-Glausch if (mop->ar >= NUM_ACRS) 531141408c28SThomas Huth return -EINVAL; 531219e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 531319e12277SJanosch Frank return -EINVAL; 531441408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 531541408c28SThomas Huth tmpbuf = vmalloc(mop->size); 531641408c28SThomas Huth if (!tmpbuf) 531741408c28SThomas Huth return -ENOMEM; 531841408c28SThomas Huth } 531941408c28SThomas Huth 5320701422b3SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE; 532141408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 5322e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 5323701422b3SJanis Schoetterl-Glausch acc_mode, mop->key); 5324701422b3SJanis Schoetterl-Glausch goto out_inject; 532541408c28SThomas Huth } 5326701422b3SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) { 5327e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5328e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 5329701422b3SJanis Schoetterl-Glausch if (r) 5330701422b3SJanis Schoetterl-Glausch goto out_inject; 5331701422b3SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) { 533241408c28SThomas Huth r = -EFAULT; 5333701422b3SJanis Schoetterl-Glausch goto out_free; 533441408c28SThomas Huth } 5335701422b3SJanis Schoetterl-Glausch } else { 533641408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 533741408c28SThomas Huth r = -EFAULT; 5338701422b3SJanis Schoetterl-Glausch goto out_free; 533941408c28SThomas Huth } 5340e9e9feebSJanis Schoetterl-Glausch r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5341e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 534241408c28SThomas Huth } 534341408c28SThomas Huth 5344701422b3SJanis Schoetterl-Glausch out_inject: 534541408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 534641408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 534741408c28SThomas Huth 5348701422b3SJanis Schoetterl-Glausch out_free: 534941408c28SThomas Huth vfree(tmpbuf); 535041408c28SThomas Huth return r; 535141408c28SThomas Huth } 535241408c28SThomas Huth 53530e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu, 535419e12277SJanosch Frank struct kvm_s390_mem_op *mop) 535519e12277SJanosch Frank { 535619e12277SJanosch Frank int r, srcu_idx; 535719e12277SJanosch Frank 535819e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 535919e12277SJanosch Frank 536019e12277SJanosch Frank switch (mop->op) { 536119e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 536219e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 53630e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop); 536419e12277SJanosch Frank break; 536519e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 536619e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 536719e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 53680e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop); 536919e12277SJanosch Frank break; 537019e12277SJanosch Frank default: 537119e12277SJanosch Frank r = -EINVAL; 537219e12277SJanosch Frank } 537319e12277SJanosch Frank 537419e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 537519e12277SJanosch Frank return r; 537619e12277SJanosch Frank } 537719e12277SJanosch Frank 53785cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 5379b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 5380b0c632dbSHeiko Carstens { 5381b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 5382b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 5383b0c632dbSHeiko Carstens 538493736624SAvi Kivity switch (ioctl) { 538547b43c52SJens Freimann case KVM_S390_IRQ: { 538647b43c52SJens Freimann struct kvm_s390_irq s390irq; 538747b43c52SJens Freimann 538847b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 53899b062471SChristoffer Dall return -EFAULT; 53909b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 539147b43c52SJens Freimann } 539293736624SAvi Kivity case KVM_S390_INTERRUPT: { 5393ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 539453936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 5395ba5c1e9bSCarsten Otte 5396ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 53979b062471SChristoffer Dall return -EFAULT; 5398383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 5399383d0b05SJens Freimann return -EINVAL; 54009b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 5401ba5c1e9bSCarsten Otte } 54029b062471SChristoffer Dall } 54035cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 54045cb0944cSPaolo Bonzini } 54055cb0944cSPaolo Bonzini 54068aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu, 54078aba0958SJanosch Frank struct kvm_pv_cmd *cmd) 54088aba0958SJanosch Frank { 54098aba0958SJanosch Frank struct kvm_s390_pv_dmp dmp; 54108aba0958SJanosch Frank void *data; 54118aba0958SJanosch Frank int ret; 54128aba0958SJanosch Frank 54138aba0958SJanosch Frank /* Dump initialization is a prerequisite */ 54148aba0958SJanosch Frank if (!vcpu->kvm->arch.pv.dumping) 54158aba0958SJanosch Frank return -EINVAL; 54168aba0958SJanosch Frank 54178aba0958SJanosch Frank if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp))) 54188aba0958SJanosch Frank return -EFAULT; 54198aba0958SJanosch Frank 54208aba0958SJanosch Frank /* We only handle this subcmd right now */ 54218aba0958SJanosch Frank if (dmp.subcmd != KVM_PV_DUMP_CPU) 54228aba0958SJanosch Frank return -EINVAL; 54238aba0958SJanosch Frank 54248aba0958SJanosch Frank /* CPU dump length is the same as create cpu storage donation. */ 54258aba0958SJanosch Frank if (dmp.buff_len != uv_info.guest_cpu_stor_len) 54268aba0958SJanosch Frank return -EINVAL; 54278aba0958SJanosch Frank 54288aba0958SJanosch Frank data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL); 54298aba0958SJanosch Frank if (!data) 54308aba0958SJanosch Frank return -ENOMEM; 54318aba0958SJanosch Frank 54328aba0958SJanosch Frank ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc); 54338aba0958SJanosch Frank 54348aba0958SJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x", 54358aba0958SJanosch Frank vcpu->vcpu_id, cmd->rc, cmd->rrc); 54368aba0958SJanosch Frank 54378aba0958SJanosch Frank if (ret) 54388aba0958SJanosch Frank ret = -EINVAL; 54398aba0958SJanosch Frank 54408aba0958SJanosch Frank /* On success copy over the dump data */ 54418aba0958SJanosch Frank if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len)) 54428aba0958SJanosch Frank ret = -EFAULT; 54438aba0958SJanosch Frank 54448aba0958SJanosch Frank kvfree(data); 54458aba0958SJanosch Frank return ret; 54468aba0958SJanosch Frank } 54478aba0958SJanosch Frank 54485cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 54495cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 54505cb0944cSPaolo Bonzini { 54515cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 54525cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 54535cb0944cSPaolo Bonzini int idx; 54545cb0944cSPaolo Bonzini long r; 54558a8378faSJanosch Frank u16 rc, rrc; 54569b062471SChristoffer Dall 54579b062471SChristoffer Dall vcpu_load(vcpu); 54589b062471SChristoffer Dall 54599b062471SChristoffer Dall switch (ioctl) { 5460b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 5461800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 546255680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 5463800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 5464bc923cc9SAvi Kivity break; 5465b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 5466b0c632dbSHeiko Carstens psw_t psw; 5467b0c632dbSHeiko Carstens 5468bc923cc9SAvi Kivity r = -EFAULT; 5469b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 5470bc923cc9SAvi Kivity break; 5471bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 5472bc923cc9SAvi Kivity break; 5473b0c632dbSHeiko Carstens } 54747de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 54757de3f142SJanosch Frank r = 0; 54767de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 54778a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54788a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54798a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 54808a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 54818a8378faSJanosch Frank rc, rrc); 54828a8378faSJanosch Frank } 54837de3f142SJanosch Frank break; 5484b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 54857de3f142SJanosch Frank r = 0; 54867de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 54878a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54888a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54898a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 54908a8378faSJanosch Frank &rc, &rrc); 54918a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 54928a8378faSJanosch Frank rc, rrc); 54938a8378faSJanosch Frank } 54947de3f142SJanosch Frank break; 54957de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 54967de3f142SJanosch Frank r = 0; 54977de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 54988a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54998a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 55008a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 55018a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 55028a8378faSJanosch Frank rc, rrc); 55038a8378faSJanosch Frank } 5504bc923cc9SAvi Kivity break; 550514eebd91SCarsten Otte case KVM_SET_ONE_REG: 550614eebd91SCarsten Otte case KVM_GET_ONE_REG: { 550714eebd91SCarsten Otte struct kvm_one_reg reg; 550868cf7b1fSJanosch Frank r = -EINVAL; 550968cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 551068cf7b1fSJanosch Frank break; 551114eebd91SCarsten Otte r = -EFAULT; 551214eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 551314eebd91SCarsten Otte break; 551414eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 551514eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 551614eebd91SCarsten Otte else 551714eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 551814eebd91SCarsten Otte break; 551914eebd91SCarsten Otte } 552027e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 552127e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 552227e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 552327e0393fSCarsten Otte 552427e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 552527e0393fSCarsten Otte r = -EFAULT; 552627e0393fSCarsten Otte break; 552727e0393fSCarsten Otte } 552827e0393fSCarsten Otte 552927e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 553027e0393fSCarsten Otte r = -EINVAL; 553127e0393fSCarsten Otte break; 553227e0393fSCarsten Otte } 553327e0393fSCarsten Otte 553427e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 553527e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 553627e0393fSCarsten Otte break; 553727e0393fSCarsten Otte } 553827e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 553927e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 554027e0393fSCarsten Otte 554127e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 554227e0393fSCarsten Otte r = -EFAULT; 554327e0393fSCarsten Otte break; 554427e0393fSCarsten Otte } 554527e0393fSCarsten Otte 554627e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 554727e0393fSCarsten Otte r = -EINVAL; 554827e0393fSCarsten Otte break; 554927e0393fSCarsten Otte } 555027e0393fSCarsten Otte 555127e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 555227e0393fSCarsten Otte ucasmap.length); 555327e0393fSCarsten Otte break; 555427e0393fSCarsten Otte } 555527e0393fSCarsten Otte #endif 5556ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 5557527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 5558ccc7910fSCarsten Otte break; 5559ccc7910fSCarsten Otte } 5560d6712df9SCornelia Huck case KVM_ENABLE_CAP: 5561d6712df9SCornelia Huck { 5562d6712df9SCornelia Huck struct kvm_enable_cap cap; 5563d6712df9SCornelia Huck r = -EFAULT; 5564d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 5565d6712df9SCornelia Huck break; 5566d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 5567d6712df9SCornelia Huck break; 5568d6712df9SCornelia Huck } 556941408c28SThomas Huth case KVM_S390_MEM_OP: { 557041408c28SThomas Huth struct kvm_s390_mem_op mem_op; 557141408c28SThomas Huth 557241408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 55730e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op); 557441408c28SThomas Huth else 557541408c28SThomas Huth r = -EFAULT; 557641408c28SThomas Huth break; 557741408c28SThomas Huth } 5578816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 5579816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5580816c7667SJens Freimann 5581816c7667SJens Freimann r = -EFAULT; 5582816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5583816c7667SJens Freimann break; 5584816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 5585816c7667SJens Freimann irq_state.len == 0 || 5586816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 5587816c7667SJens Freimann r = -EINVAL; 5588816c7667SJens Freimann break; 5589816c7667SJens Freimann } 5590bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5591816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 5592816c7667SJens Freimann (void __user *) irq_state.buf, 5593816c7667SJens Freimann irq_state.len); 5594816c7667SJens Freimann break; 5595816c7667SJens Freimann } 5596816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 5597816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5598816c7667SJens Freimann 5599816c7667SJens Freimann r = -EFAULT; 5600816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5601816c7667SJens Freimann break; 5602816c7667SJens Freimann if (irq_state.len == 0) { 5603816c7667SJens Freimann r = -EINVAL; 5604816c7667SJens Freimann break; 5605816c7667SJens Freimann } 5606bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5607816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 5608816c7667SJens Freimann (__u8 __user *) irq_state.buf, 5609816c7667SJens Freimann irq_state.len); 5610816c7667SJens Freimann break; 5611816c7667SJens Freimann } 56128aba0958SJanosch Frank case KVM_S390_PV_CPU_COMMAND: { 56138aba0958SJanosch Frank struct kvm_pv_cmd cmd; 56148aba0958SJanosch Frank 56158aba0958SJanosch Frank r = -EINVAL; 56168aba0958SJanosch Frank if (!is_prot_virt_host()) 56178aba0958SJanosch Frank break; 56188aba0958SJanosch Frank 56198aba0958SJanosch Frank r = -EFAULT; 56208aba0958SJanosch Frank if (copy_from_user(&cmd, argp, sizeof(cmd))) 56218aba0958SJanosch Frank break; 56228aba0958SJanosch Frank 56238aba0958SJanosch Frank r = -EINVAL; 56248aba0958SJanosch Frank if (cmd.flags) 56258aba0958SJanosch Frank break; 56268aba0958SJanosch Frank 56278aba0958SJanosch Frank /* We only handle this cmd right now */ 56288aba0958SJanosch Frank if (cmd.cmd != KVM_PV_DUMP) 56298aba0958SJanosch Frank break; 56308aba0958SJanosch Frank 56318aba0958SJanosch Frank r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd); 56328aba0958SJanosch Frank 56338aba0958SJanosch Frank /* Always copy over UV rc / rrc data */ 56348aba0958SJanosch Frank if (copy_to_user((__u8 __user *)argp, &cmd.rc, 56358aba0958SJanosch Frank sizeof(cmd.rc) + sizeof(cmd.rrc))) 56368aba0958SJanosch Frank r = -EFAULT; 56378aba0958SJanosch Frank break; 56388aba0958SJanosch Frank } 5639b0c632dbSHeiko Carstens default: 56403e6afcf1SCarsten Otte r = -ENOTTY; 5641b0c632dbSHeiko Carstens } 56429b062471SChristoffer Dall 56439b062471SChristoffer Dall vcpu_put(vcpu); 5644bc923cc9SAvi Kivity return r; 5645b0c632dbSHeiko Carstens } 5646b0c632dbSHeiko Carstens 56471499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 56485b1c1493SCarsten Otte { 56495b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 56505b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 56515b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 56525b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 56535b1c1493SCarsten Otte get_page(vmf->page); 56545b1c1493SCarsten Otte return 0; 56555b1c1493SCarsten Otte } 56565b1c1493SCarsten Otte #endif 56575b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 56585b1c1493SCarsten Otte } 56595b1c1493SCarsten Otte 5660d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 5661d663b8a2SPaolo Bonzini { 5662d663b8a2SPaolo Bonzini return true; 5663d663b8a2SPaolo Bonzini } 5664d663b8a2SPaolo Bonzini 5665b0c632dbSHeiko Carstens /* Section: memory related */ 5666f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5667537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5668537a17b3SSean Christopherson struct kvm_memory_slot *new, 56697b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5670b0c632dbSHeiko Carstens { 5671ec5c8697SSean Christopherson gpa_t size; 5672ec5c8697SSean Christopherson 5673ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5674ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5675ec5c8697SSean Christopherson return -EINVAL; 5676ec5c8697SSean Christopherson 5677f2d3155eSNico Boehr if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) { 5678f2d3155eSNico Boehr /* 5679f2d3155eSNico Boehr * A few sanity checks. We can have memory slots which have to be 5680f2d3155eSNico Boehr * located/ended at a segment boundary (1MB). The memory in userland is 5681f2d3155eSNico Boehr * ok to be fragmented into various different vmas. It is okay to mmap() 5682f2d3155eSNico Boehr * and munmap() stuff in this slot after doing this call at any time 5683f2d3155eSNico Boehr */ 5684b0c632dbSHeiko Carstens 5685cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5686b0c632dbSHeiko Carstens return -EINVAL; 5687b0c632dbSHeiko Carstens 5688ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5689cf5b4869SSean Christopherson if (size & 0xffffful) 5690b0c632dbSHeiko Carstens return -EINVAL; 5691b0c632dbSHeiko Carstens 5692cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5693a3a92c31SDominik Dingel return -EINVAL; 5694f2d3155eSNico Boehr } 5695f2d3155eSNico Boehr 5696f2d3155eSNico Boehr if (!kvm->arch.migration_mode) 5697f2d3155eSNico Boehr return 0; 5698f2d3155eSNico Boehr 5699f2d3155eSNico Boehr /* 5700f2d3155eSNico Boehr * Turn off migration mode when: 5701f2d3155eSNico Boehr * - userspace creates a new memslot with dirty logging off, 5702f2d3155eSNico Boehr * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and 5703f2d3155eSNico Boehr * dirty logging is turned off. 5704f2d3155eSNico Boehr * Migration mode expects dirty page logging being enabled to store 5705f2d3155eSNico Boehr * its dirty bitmap. 5706f2d3155eSNico Boehr */ 5707f2d3155eSNico Boehr if (change != KVM_MR_DELETE && 5708f2d3155eSNico Boehr !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) 5709f2d3155eSNico Boehr WARN(kvm_s390_vm_stop_migration(kvm), 5710f2d3155eSNico Boehr "Failed to stop migration mode"); 5711a3a92c31SDominik Dingel 5712f7784b8eSMarcelo Tosatti return 0; 5713f7784b8eSMarcelo Tosatti } 5714f7784b8eSMarcelo Tosatti 5715f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 57169d4c197cSSean Christopherson struct kvm_memory_slot *old, 5717f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 57188482644aSTakuya Yoshikawa enum kvm_mr_change change) 5719f7784b8eSMarcelo Tosatti { 572019ec166cSChristian Borntraeger int rc = 0; 5721f7784b8eSMarcelo Tosatti 572219ec166cSChristian Borntraeger switch (change) { 572319ec166cSChristian Borntraeger case KVM_MR_DELETE: 572419ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 572519ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 572619ec166cSChristian Borntraeger break; 572719ec166cSChristian Borntraeger case KVM_MR_MOVE: 572819ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 572919ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 573019ec166cSChristian Borntraeger if (rc) 573119ec166cSChristian Borntraeger break; 57323b684a42SJoe Perches fallthrough; 573319ec166cSChristian Borntraeger case KVM_MR_CREATE: 5734cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5735cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5736cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 573719ec166cSChristian Borntraeger break; 573819ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 573919ec166cSChristian Borntraeger break; 574019ec166cSChristian Borntraeger default: 574119ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 574219ec166cSChristian Borntraeger } 5743598841caSCarsten Otte if (rc) 5744ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5745598841caSCarsten Otte return; 5746b0c632dbSHeiko Carstens } 5747b0c632dbSHeiko Carstens 574860a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 574960a37709SAlexander Yarygin { 575060a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 575160a37709SAlexander Yarygin 575260a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 575360a37709SAlexander Yarygin } 575460a37709SAlexander Yarygin 5755b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5756b0c632dbSHeiko Carstens { 5757b8449265SSean Christopherson int i, r; 575860a37709SAlexander Yarygin 575907197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 57608d43d570SMichael Mueller pr_info("SIE is not available\n"); 576107197fd0SDavid Hildenbrand return -ENODEV; 576207197fd0SDavid Hildenbrand } 576307197fd0SDavid Hildenbrand 5764a4499382SJanosch Frank if (nested && hpage) { 57658d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5766a4499382SJanosch Frank return -EINVAL; 5767a4499382SJanosch Frank } 5768a4499382SJanosch Frank 576960a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5770c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 577117e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 577260a37709SAlexander Yarygin 5773b8449265SSean Christopherson r = __kvm_s390_init(); 5774b8449265SSean Christopherson if (r) 5775b8449265SSean Christopherson return r; 5776b8449265SSean Christopherson 577781a1cf9fSSean Christopherson r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5778b8449265SSean Christopherson if (r) { 5779b8449265SSean Christopherson __kvm_s390_exit(); 5780b8449265SSean Christopherson return r; 5781b8449265SSean Christopherson } 5782b8449265SSean Christopherson return 0; 5783b0c632dbSHeiko Carstens } 5784b0c632dbSHeiko Carstens 5785b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5786b0c632dbSHeiko Carstens { 5787b0c632dbSHeiko Carstens kvm_exit(); 5788b8449265SSean Christopherson 5789b8449265SSean Christopherson __kvm_s390_exit(); 5790b0c632dbSHeiko Carstens } 5791b0c632dbSHeiko Carstens 5792b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5793b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5794566af940SCornelia Huck 5795566af940SCornelia Huck /* 5796566af940SCornelia Huck * Enable autoloading of the kvm module. 5797566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5798566af940SCornelia Huck * since x86 takes a different approach. 5799566af940SCornelia Huck */ 5800566af940SCornelia Huck #include <linux/miscdevice.h> 5801566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5802566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5803