1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2b0c632dbSHeiko Carstens /* 3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x) 4b0c632dbSHeiko Carstens * 5a37cb07aSChristian Borntraeger * Copyright IBM Corp. 2008, 2018 6b0c632dbSHeiko Carstens * 7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 9b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 10628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1115f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 12b0c632dbSHeiko Carstens */ 13b0c632dbSHeiko Carstens 147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 167aedd9d4SMichael Mueller 17b0c632dbSHeiko Carstens #include <linux/compiler.h> 18b0c632dbSHeiko Carstens #include <linux/err.h> 19b0c632dbSHeiko Carstens #include <linux/fs.h> 20ca872302SChristian Borntraeger #include <linux/hrtimer.h> 21b0c632dbSHeiko Carstens #include <linux/init.h> 22b0c632dbSHeiko Carstens #include <linux/kvm.h> 23b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 24b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 25b0c632dbSHeiko Carstens #include <linux/module.h> 26d3217967SPaul Gortmaker #include <linux/moduleparam.h> 27a374e892STony Krowiak #include <linux/random.h> 28b0c632dbSHeiko Carstens #include <linux/slab.h> 29ba5c1e9bSCarsten Otte #include <linux/timer.h> 3041408c28SThomas Huth #include <linux/vmalloc.h> 3115c9705fSDavid Hildenbrand #include <linux/bitmap.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33190df4a2SClaudio Imbrenda #include <linux/string.h> 34174cd4b1SIngo Molnar 35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 36b0c632dbSHeiko Carstens #include <asm/lowcore.h> 37fd5ada04SMartin Schwidefsky #include <asm/stp.h> 38b0c632dbSHeiko Carstens #include <asm/pgtable.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> 478f2abe6aSChristian Borntraeger #include "kvm-s390.h" 48b0c632dbSHeiko Carstens #include "gaccess.h" 49b0c632dbSHeiko Carstens 505786fffaSCornelia Huck #define CREATE_TRACE_POINTS 515786fffaSCornelia Huck #include "trace.h" 52ade38c31SCornelia Huck #include "trace-s390.h" 535786fffaSCornelia Huck 5441408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 55816c7667SJens Freimann #define LOCAL_IRQS 32 56816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 57816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 5841408c28SThomas Huth 59b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 60ccc40c53SChristian Borntraeger #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM 61b0c632dbSHeiko Carstens 62b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 63b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 640eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 658f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 668f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 678f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 68a5e0aceaSChristian Borntraeger { "exit_io_request", VCPU_STAT(exit_io_request) }, 698f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 70ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 719ec6de19SAlexander Yarygin { "exit_pei", VCPU_STAT(exit_pei) }, 72ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 73ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 74a011eeb2SJanosch Frank { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, 75f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 7662bea5bfSPaolo Bonzini { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 773491caf2SChristian Borntraeger { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 78ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 79f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 80ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 81aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 82aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 83ccc40c53SChristian Borntraeger { "deliver_ckc", VCPU_STAT(deliver_ckc) }, 84ccc40c53SChristian Borntraeger { "deliver_cputm", VCPU_STAT(deliver_cputm) }, 85ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 867697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 87ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 88ccc40c53SChristian Borntraeger { "deliver_virtio", VCPU_STAT(deliver_virtio) }, 89ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 90ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 91ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 92ccc40c53SChristian Borntraeger { "deliver_program", VCPU_STAT(deliver_program) }, 93ccc40c53SChristian Borntraeger { "deliver_io", VCPU_STAT(deliver_io) }, 9432de0749SQingFeng Hao { "deliver_machine_check", VCPU_STAT(deliver_machine_check) }, 95ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 96ccc40c53SChristian Borntraeger { "inject_ckc", VCPU_STAT(inject_ckc) }, 97ccc40c53SChristian Borntraeger { "inject_cputm", VCPU_STAT(inject_cputm) }, 98ccc40c53SChristian Borntraeger { "inject_external_call", VCPU_STAT(inject_external_call) }, 99ccc40c53SChristian Borntraeger { "inject_float_mchk", VM_STAT(inject_float_mchk) }, 100ccc40c53SChristian Borntraeger { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) }, 101ccc40c53SChristian Borntraeger { "inject_io", VM_STAT(inject_io) }, 102ccc40c53SChristian Borntraeger { "inject_mchk", VCPU_STAT(inject_mchk) }, 103ccc40c53SChristian Borntraeger { "inject_pfault_done", VM_STAT(inject_pfault_done) }, 104ccc40c53SChristian Borntraeger { "inject_program", VCPU_STAT(inject_program) }, 105ccc40c53SChristian Borntraeger { "inject_restart", VCPU_STAT(inject_restart) }, 106ccc40c53SChristian Borntraeger { "inject_service_signal", VM_STAT(inject_service_signal) }, 107ccc40c53SChristian Borntraeger { "inject_set_prefix", VCPU_STAT(inject_set_prefix) }, 108ccc40c53SChristian Borntraeger { "inject_stop_signal", VCPU_STAT(inject_stop_signal) }, 109ccc40c53SChristian Borntraeger { "inject_pfault_init", VCPU_STAT(inject_pfault_init) }, 110ccc40c53SChristian Borntraeger { "inject_virtio", VM_STAT(inject_virtio) }, 111a37cb07aSChristian Borntraeger { "instruction_epsw", VCPU_STAT(instruction_epsw) }, 112a37cb07aSChristian Borntraeger { "instruction_gs", VCPU_STAT(instruction_gs) }, 113a37cb07aSChristian Borntraeger { "instruction_io_other", VCPU_STAT(instruction_io_other) }, 114a37cb07aSChristian Borntraeger { "instruction_lpsw", VCPU_STAT(instruction_lpsw) }, 115a37cb07aSChristian Borntraeger { "instruction_lpswe", VCPU_STAT(instruction_lpswe) }, 11669d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 117a37cb07aSChristian Borntraeger { "instruction_ptff", VCPU_STAT(instruction_ptff) }, 118453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 119a37cb07aSChristian Borntraeger { "instruction_sck", VCPU_STAT(instruction_sck) }, 120a37cb07aSChristian Borntraeger { "instruction_sckpf", VCPU_STAT(instruction_sckpf) }, 121453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 122453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 123453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 124a37cb07aSChristian Borntraeger { "instruction_iske", VCPU_STAT(instruction_iske) }, 125a37cb07aSChristian Borntraeger { "instruction_ri", VCPU_STAT(instruction_ri) }, 126a37cb07aSChristian Borntraeger { "instruction_rrbe", VCPU_STAT(instruction_rrbe) }, 127a37cb07aSChristian Borntraeger { "instruction_sske", VCPU_STAT(instruction_sske) }, 1288a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 129b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 130453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 131453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 132a37cb07aSChristian Borntraeger { "instruction_tb", VCPU_STAT(instruction_tb) }, 133a37cb07aSChristian Borntraeger { "instruction_tpi", VCPU_STAT(instruction_tpi) }, 134bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 135a37cb07aSChristian Borntraeger { "instruction_tsch", VCPU_STAT(instruction_tsch) }, 13695ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 137a3508fbeSDavid Hildenbrand { "instruction_sie", VCPU_STAT(instruction_sie) }, 1385288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 139bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1407697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1415288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 14242cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 14342cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1445288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 14542cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 14642cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 147cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1485288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1495288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1505288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 15142cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 15242cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 15342cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 154866c138cSChristian Borntraeger { "instruction_diag_10", VCPU_STAT(diagnose_10) }, 155866c138cSChristian Borntraeger { "instruction_diag_44", VCPU_STAT(diagnose_44) }, 156866c138cSChristian Borntraeger { "instruction_diag_9c", VCPU_STAT(diagnose_9c) }, 157866c138cSChristian Borntraeger { "instruction_diag_258", VCPU_STAT(diagnose_258) }, 158866c138cSChristian Borntraeger { "instruction_diag_308", VCPU_STAT(diagnose_308) }, 159866c138cSChristian Borntraeger { "instruction_diag_500", VCPU_STAT(diagnose_500) }, 160a37cb07aSChristian Borntraeger { "instruction_diag_other", VCPU_STAT(diagnose_other) }, 161b0c632dbSHeiko Carstens { NULL } 162b0c632dbSHeiko Carstens }; 163b0c632dbSHeiko Carstens 1648fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext { 1658fa1696eSCollin L. Walling __u8 epoch_idx; 1668fa1696eSCollin L. Walling __u64 tod; 1678fa1696eSCollin L. Walling __u8 reserved[7]; 1688fa1696eSCollin L. Walling } __packed; 1698fa1696eSCollin L. Walling 170a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 171a411edf1SDavid Hildenbrand static int nested; 172a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 173a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 174a411edf1SDavid Hildenbrand 175a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 176a4499382SJanosch Frank static int hpage; 177a4499382SJanosch Frank module_param(hpage, int, 0444); 178a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 179b0c632dbSHeiko Carstens 180c3b9e3e1SChristian Borntraeger /* 181c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 182c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 183c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 184c3b9e3e1SChristian Borntraeger */ 185c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 186c3b9e3e1SChristian Borntraeger 187c3b9e3e1SChristian Borntraeger /* 188c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 189c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 190c3b9e3e1SChristian Borntraeger */ 191c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 192c3b9e3e1SChristian Borntraeger /* 193c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 194c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 195c3b9e3e1SChristian Borntraeger */ 196c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 197c3b9e3e1SChristian Borntraeger 198c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 19978c4b59fSMichael Mueller { 200c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 201c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 202c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 203c3b9e3e1SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 204c3b9e3e1SChristian Borntraeger 205c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 20678c4b59fSMichael Mueller } 20778c4b59fSMichael Mueller 20815c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 20915c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2100a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2110a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 21215c9705fSDavid Hildenbrand 2139d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 214a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 21578f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2169d8d5786SMichael Mueller 217b0c632dbSHeiko Carstens /* Section: not file related */ 21813a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 219b0c632dbSHeiko Carstens { 220b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 22110474ae8SAlexander Graf return 0; 222b0c632dbSHeiko Carstens } 223b0c632dbSHeiko Carstens 224414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 225414d3b07SMartin Schwidefsky unsigned long end); 2262c70fe44SChristian Borntraeger 2271575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2281575767eSDavid Hildenbrand { 2291575767eSDavid Hildenbrand u8 delta_idx = 0; 2301575767eSDavid Hildenbrand 2311575767eSDavid Hildenbrand /* 2321575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2331575767eSDavid Hildenbrand * -delta to the epoch. 2341575767eSDavid Hildenbrand */ 2351575767eSDavid Hildenbrand delta = -delta; 2361575767eSDavid Hildenbrand 2371575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2381575767eSDavid Hildenbrand if ((s64)delta < 0) 2391575767eSDavid Hildenbrand delta_idx = -1; 2401575767eSDavid Hildenbrand 2411575767eSDavid Hildenbrand scb->epoch += delta; 2421575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2431575767eSDavid Hildenbrand scb->epdx += delta_idx; 2441575767eSDavid Hildenbrand if (scb->epoch < delta) 2451575767eSDavid Hildenbrand scb->epdx += 1; 2461575767eSDavid Hildenbrand } 2471575767eSDavid Hildenbrand } 2481575767eSDavid Hildenbrand 249fdf03650SFan Zhang /* 250fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 251fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 252fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 253fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 254fdf03650SFan Zhang */ 255fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 256fdf03650SFan Zhang void *v) 257fdf03650SFan Zhang { 258fdf03650SFan Zhang struct kvm *kvm; 259fdf03650SFan Zhang struct kvm_vcpu *vcpu; 260fdf03650SFan Zhang int i; 261fdf03650SFan Zhang unsigned long long *delta = v; 262fdf03650SFan Zhang 263fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 264fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 2651575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 2661575767eSDavid Hildenbrand if (i == 0) { 2671575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 2681575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 2691575767eSDavid Hildenbrand } 270db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 271db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 27291473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 2731575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 2741575767eSDavid Hildenbrand *delta); 275fdf03650SFan Zhang } 276fdf03650SFan Zhang } 277fdf03650SFan Zhang return NOTIFY_OK; 278fdf03650SFan Zhang } 279fdf03650SFan Zhang 280fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 281fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 282fdf03650SFan Zhang }; 283fdf03650SFan Zhang 284b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 285b0c632dbSHeiko Carstens { 2862c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 287b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 288a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 289a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 290fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 291fdf03650SFan Zhang &kvm_clock_notifier); 292b0c632dbSHeiko Carstens return 0; 293b0c632dbSHeiko Carstens } 294b0c632dbSHeiko Carstens 295b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 296b0c632dbSHeiko Carstens { 297b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 298a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 299fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 300fdf03650SFan Zhang &kvm_clock_notifier); 301b0c632dbSHeiko Carstens } 302b0c632dbSHeiko Carstens 30322be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 30422be5a13SDavid Hildenbrand { 30522be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 30622be5a13SDavid Hildenbrand } 30722be5a13SDavid Hildenbrand 3080a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3090a763c78SDavid Hildenbrand { 3100a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 311d051ae53SHeiko Carstens int cc; 3120a763c78SDavid Hildenbrand 3130a763c78SDavid Hildenbrand asm volatile( 3140a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3150a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3160a763c78SDavid Hildenbrand " ipm %0\n" 3170a763c78SDavid Hildenbrand " srl %0,28\n" 3180a763c78SDavid Hildenbrand : "=d" (cc) 3190a763c78SDavid Hildenbrand : "d" (r0) 3200a763c78SDavid Hildenbrand : "cc"); 3210a763c78SDavid Hildenbrand return cc == 0; 3220a763c78SDavid Hildenbrand } 3230a763c78SDavid Hildenbrand 324*d6681397SChristian Borntraeger static inline void __insn32_query(unsigned int opcode, u8 query[32]) 325*d6681397SChristian Borntraeger { 326*d6681397SChristian Borntraeger register unsigned long r0 asm("0") = 0; /* query function */ 327*d6681397SChristian Borntraeger register unsigned long r1 asm("1") = (unsigned long) query; 328*d6681397SChristian Borntraeger 329*d6681397SChristian Borntraeger asm volatile( 330*d6681397SChristian Borntraeger /* Parameter regs are ignored */ 331*d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 332*d6681397SChristian Borntraeger : "=m" (*query) 333*d6681397SChristian Borntraeger : "d" (r0), "a" (r1), [opc] "i" (opcode) 334*d6681397SChristian Borntraeger : "cc"); 335*d6681397SChristian Borntraeger } 336*d6681397SChristian Borntraeger 33722be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 33822be5a13SDavid Hildenbrand { 3390a763c78SDavid Hildenbrand int i; 3400a763c78SDavid Hildenbrand 3410a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3420a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3430a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3440a763c78SDavid Hildenbrand } 3450a763c78SDavid Hildenbrand 3460a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 347221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 348221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 349221bb8a4SLinus Torvalds PTFF_QAF); 3500a763c78SDavid Hildenbrand 3510a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 35269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 35369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 35469c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 35569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 35669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 35769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 35869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 35969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 36069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 36169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 3620a763c78SDavid Hildenbrand } 3630a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 36469c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 36569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 3660a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 36769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 36869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 36969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 37069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 37169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 37269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 37369c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 37469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 3750a763c78SDavid Hildenbrand } 3760a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 377985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 37869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 3790a763c78SDavid Hildenbrand 380e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 381e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 382e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 383e000b8e0SJason J. Herne 38413209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 38513209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 38613209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 38713209ad0SChristian Borntraeger 38822be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 38922be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 390a3508fbeSDavid Hildenbrand /* 391a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 392a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 393a3508fbeSDavid Hildenbrand */ 394a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 395a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 396a3508fbeSDavid Hildenbrand return; 397a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 39819c439b5SDavid Hildenbrand if (sclp.has_64bscao) 39919c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4000615a326SDavid Hildenbrand if (sclp.has_siif) 4010615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 40277d18f6dSDavid Hildenbrand if (sclp.has_gpere) 40377d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 404a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 405a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4065630a8e8SDavid Hildenbrand if (sclp.has_ib) 4075630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 40813ee3f67SDavid Hildenbrand if (sclp.has_cei) 40913ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4107fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4117fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 412730cd632SFarhan Ali if (sclp.has_kss) 413730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4145d3876a8SDavid Hildenbrand /* 4155d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4165d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4175d3876a8SDavid Hildenbrand * instead of the real storage key. 4185d3876a8SDavid Hildenbrand * 4195d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4205d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4215d3876a8SDavid Hildenbrand * 4225d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4235d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4245d3876a8SDavid Hildenbrand * 4255d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4265d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4275d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4285d3876a8SDavid Hildenbrand * 4295d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4305d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4315d3876a8SDavid Hildenbrand */ 43222be5a13SDavid Hildenbrand } 43322be5a13SDavid Hildenbrand 434b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 435b0c632dbSHeiko Carstens { 436308c3e66SMichael Mueller int rc; 437308c3e66SMichael Mueller 43878f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 43978f26131SChristian Borntraeger if (!kvm_s390_dbf) 44078f26131SChristian Borntraeger return -ENOMEM; 44178f26131SChristian Borntraeger 44278f26131SChristian Borntraeger if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { 443308c3e66SMichael Mueller rc = -ENOMEM; 444308c3e66SMichael Mueller goto out_debug_unreg; 44578f26131SChristian Borntraeger } 44678f26131SChristian Borntraeger 44722be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 44822be5a13SDavid Hildenbrand 44984877d93SCornelia Huck /* Register floating interrupt controller interface. */ 450308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 451308c3e66SMichael Mueller if (rc) { 4528d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 453308c3e66SMichael Mueller goto out_debug_unreg; 454308c3e66SMichael Mueller } 455b1d1e76eSMichael Mueller 456b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 457b1d1e76eSMichael Mueller if (rc) 458b1d1e76eSMichael Mueller goto out_gib_destroy; 459b1d1e76eSMichael Mueller 460308c3e66SMichael Mueller return 0; 461308c3e66SMichael Mueller 462b1d1e76eSMichael Mueller out_gib_destroy: 463b1d1e76eSMichael Mueller kvm_s390_gib_destroy(); 464308c3e66SMichael Mueller out_debug_unreg: 465308c3e66SMichael Mueller debug_unregister(kvm_s390_dbf); 466308c3e66SMichael Mueller return rc; 467b0c632dbSHeiko Carstens } 468b0c632dbSHeiko Carstens 46978f26131SChristian Borntraeger void kvm_arch_exit(void) 47078f26131SChristian Borntraeger { 4711282c21eSMichael Mueller kvm_s390_gib_destroy(); 47278f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 47378f26131SChristian Borntraeger } 47478f26131SChristian Borntraeger 475b0c632dbSHeiko Carstens /* Section: device related */ 476b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 477b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 478b0c632dbSHeiko Carstens { 479b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 480b0c632dbSHeiko Carstens return s390_enable_sie(); 481b0c632dbSHeiko Carstens return -EINVAL; 482b0c632dbSHeiko Carstens } 483b0c632dbSHeiko Carstens 484784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 485b0c632dbSHeiko Carstens { 486d7b0b5ebSCarsten Otte int r; 487d7b0b5ebSCarsten Otte 4882bd0ac4eSCarsten Otte switch (ext) { 489d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 490b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 49152e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 4921efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 4931efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 4941efd0f59SCarsten Otte #endif 4953c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 49660b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 49714eebd91SCarsten Otte case KVM_CAP_ONE_REG: 498d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 499fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 50010ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 501c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 50278599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 503f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5046352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 505460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 50647b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5072444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 508e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 50930ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 510816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5116502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5124036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 51347a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 514da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 515d7b0b5ebSCarsten Otte r = 1; 516d7b0b5ebSCarsten Otte break; 517a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 518a4499382SJanosch Frank r = 0; 51940ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 520a4499382SJanosch Frank r = 1; 521a4499382SJanosch Frank break; 52241408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 52341408c28SThomas Huth r = MEM_OP_MAX_SIZE; 52441408c28SThomas Huth break; 525e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 526e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 52776a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 528a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 529a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 530a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 53176a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 532e726b1bdSChristian Borntraeger break; 533e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 534e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 535e1e2e605SNick Wang break; 5361526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 537abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 5381526bf9cSChristian Borntraeger break; 53968c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 54068c55750SEric Farman r = MACHINE_HAS_VX; 54168c55750SEric Farman break; 542c6e5f166SFan Zhang case KVM_CAP_S390_RI: 543c6e5f166SFan Zhang r = test_facility(64); 544c6e5f166SFan Zhang break; 5454e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 5464e0b1ab7SFan Zhang r = test_facility(133); 5474e0b1ab7SFan Zhang break; 54835b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 54935b3fde6SChristian Borntraeger r = test_facility(82); 55035b3fde6SChristian Borntraeger break; 5512bd0ac4eSCarsten Otte default: 552d7b0b5ebSCarsten Otte r = 0; 553b0c632dbSHeiko Carstens } 554d7b0b5ebSCarsten Otte return r; 5552bd0ac4eSCarsten Otte } 556b0c632dbSHeiko Carstens 55715f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 55815f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 55915f36ebdSJason J. Herne { 5600959e168SJanosch Frank int i; 56115f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 5620959e168SJanosch Frank unsigned long gaddr, vmaddr; 56315f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 5640959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 56515f36ebdSJason J. Herne 5660959e168SJanosch Frank /* Loop over all guest segments */ 5670959e168SJanosch Frank cur_gfn = memslot->base_gfn; 56815f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 5690959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 5700959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 5710959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 5720959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 5730959e168SJanosch Frank continue; 57415f36ebdSJason J. Herne 5750959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 5760959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 5770959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 5780959e168SJanosch Frank if (test_bit(i, bitmap)) 5790959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 5800959e168SJanosch Frank } 5810959e168SJanosch Frank 5821763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 5831763f8d0SChristian Borntraeger return; 58470c88a00SChristian Borntraeger cond_resched(); 58515f36ebdSJason J. Herne } 58615f36ebdSJason J. Herne } 58715f36ebdSJason J. Herne 588b0c632dbSHeiko Carstens /* Section: vm related */ 589a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 590a6e2f683SEugene (jno) Dvurechenski 591b0c632dbSHeiko Carstens /* 592b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 593b0c632dbSHeiko Carstens */ 594b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 595b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 596b0c632dbSHeiko Carstens { 59715f36ebdSJason J. Herne int r; 59815f36ebdSJason J. Herne unsigned long n; 5999f6b8029SPaolo Bonzini struct kvm_memslots *slots; 60015f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 60115f36ebdSJason J. Herne int is_dirty = 0; 60215f36ebdSJason J. Herne 603e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 604e1e8a962SJanosch Frank return -EINVAL; 605e1e8a962SJanosch Frank 60615f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 60715f36ebdSJason J. Herne 60815f36ebdSJason J. Herne r = -EINVAL; 60915f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 61015f36ebdSJason J. Herne goto out; 61115f36ebdSJason J. Herne 6129f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 6139f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 61415f36ebdSJason J. Herne r = -ENOENT; 61515f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 61615f36ebdSJason J. Herne goto out; 61715f36ebdSJason J. Herne 61815f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 61915f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 62015f36ebdSJason J. Herne if (r) 62115f36ebdSJason J. Herne goto out; 62215f36ebdSJason J. Herne 62315f36ebdSJason J. Herne /* Clear the dirty log */ 62415f36ebdSJason J. Herne if (is_dirty) { 62515f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 62615f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 62715f36ebdSJason J. Herne } 62815f36ebdSJason J. Herne r = 0; 62915f36ebdSJason J. Herne out: 63015f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 63115f36ebdSJason J. Herne return r; 632b0c632dbSHeiko Carstens } 633b0c632dbSHeiko Carstens 6346502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 6356502a34cSDavid Hildenbrand { 6366502a34cSDavid Hildenbrand unsigned int i; 6376502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 6386502a34cSDavid Hildenbrand 6396502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 6406502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 6416502a34cSDavid Hildenbrand } 6426502a34cSDavid Hildenbrand } 6436502a34cSDavid Hildenbrand 644e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 645d938dc55SCornelia Huck { 646d938dc55SCornelia Huck int r; 647d938dc55SCornelia Huck 648d938dc55SCornelia Huck if (cap->flags) 649d938dc55SCornelia Huck return -EINVAL; 650d938dc55SCornelia Huck 651d938dc55SCornelia Huck switch (cap->cap) { 65284223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 653c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 65484223598SCornelia Huck kvm->arch.use_irqchip = 1; 65584223598SCornelia Huck r = 0; 65684223598SCornelia Huck break; 6572444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 658c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 6592444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 6602444b352SDavid Hildenbrand r = 0; 6612444b352SDavid Hildenbrand break; 66268c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 6635967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 664a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 6655967c17bSDavid Hildenbrand r = -EBUSY; 6665967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 667c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 668c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 6692f87d942SGuenther Hutzl if (test_facility(134)) { 6702f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 6712f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 6722f87d942SGuenther Hutzl } 67353743aa7SMaxim Samoylov if (test_facility(135)) { 67453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 67553743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 67653743aa7SMaxim Samoylov } 6777832e91cSChristian Borntraeger if (test_facility(148)) { 6787832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 6797832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 6807832e91cSChristian Borntraeger } 681d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 682d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 683d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 684d5cb6ab1SChristian Borntraeger } 68518280d8bSMichael Mueller r = 0; 68618280d8bSMichael Mueller } else 68718280d8bSMichael Mueller r = -EINVAL; 6885967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 689c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 690c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 69168c55750SEric Farman break; 692c6e5f166SFan Zhang case KVM_CAP_S390_RI: 693c6e5f166SFan Zhang r = -EINVAL; 694c6e5f166SFan Zhang mutex_lock(&kvm->lock); 695a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 696c6e5f166SFan Zhang r = -EBUSY; 697c6e5f166SFan Zhang } else if (test_facility(64)) { 698c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 699c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 700c6e5f166SFan Zhang r = 0; 701c6e5f166SFan Zhang } 702c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 703c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 704c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 705c6e5f166SFan Zhang break; 70647a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 70747a4693eSYi Min Zhao mutex_lock(&kvm->lock); 70847a4693eSYi Min Zhao if (kvm->created_vcpus) { 70947a4693eSYi Min Zhao r = -EBUSY; 71047a4693eSYi Min Zhao } else { 71147a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 71247a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 71347a4693eSYi Min Zhao r = 0; 71447a4693eSYi Min Zhao } 71547a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 71647a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 71747a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 71847a4693eSYi Min Zhao break; 7194e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 7204e0b1ab7SFan Zhang r = -EINVAL; 7214e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 722241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 7234e0b1ab7SFan Zhang r = -EBUSY; 7244e0b1ab7SFan Zhang } else if (test_facility(133)) { 7254e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 7264e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 7274e0b1ab7SFan Zhang r = 0; 7284e0b1ab7SFan Zhang } 7294e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 7304e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 7314e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 7324e0b1ab7SFan Zhang break; 733a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 734a4499382SJanosch Frank mutex_lock(&kvm->lock); 735a4499382SJanosch Frank if (kvm->created_vcpus) 736a4499382SJanosch Frank r = -EBUSY; 73740ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 738a4499382SJanosch Frank r = -EINVAL; 739a4499382SJanosch Frank else { 740a4499382SJanosch Frank r = 0; 741df88f318SJanosch Frank down_write(&kvm->mm->mmap_sem); 742a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 743df88f318SJanosch Frank up_write(&kvm->mm->mmap_sem); 744a4499382SJanosch Frank /* 745a4499382SJanosch Frank * We might have to create fake 4k page 746a4499382SJanosch Frank * tables. To avoid that the hardware works on 747a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 748a4499382SJanosch Frank */ 749a4499382SJanosch Frank kvm->arch.use_skf = 0; 750a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 751a4499382SJanosch Frank } 752a4499382SJanosch Frank mutex_unlock(&kvm->lock); 753a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 754a4499382SJanosch Frank r ? "(not available)" : "(success)"); 755a4499382SJanosch Frank break; 756e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 757c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 758e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 759e44fc8c9SEkaterina Tumanova r = 0; 760e44fc8c9SEkaterina Tumanova break; 7616502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 7626502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 7636502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 7646502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 7656502a34cSDavid Hildenbrand r = 0; 7666502a34cSDavid Hildenbrand break; 767d938dc55SCornelia Huck default: 768d938dc55SCornelia Huck r = -EINVAL; 769d938dc55SCornelia Huck break; 770d938dc55SCornelia Huck } 771d938dc55SCornelia Huck return r; 772d938dc55SCornelia Huck } 773d938dc55SCornelia Huck 7748c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 7758c0a7ce6SDominik Dingel { 7768c0a7ce6SDominik Dingel int ret; 7778c0a7ce6SDominik Dingel 7788c0a7ce6SDominik Dingel switch (attr->attr) { 7798c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 7808c0a7ce6SDominik Dingel ret = 0; 781c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 782a3a92c31SDominik Dingel kvm->arch.mem_limit); 783a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 7848c0a7ce6SDominik Dingel ret = -EFAULT; 7858c0a7ce6SDominik Dingel break; 7868c0a7ce6SDominik Dingel default: 7878c0a7ce6SDominik Dingel ret = -ENXIO; 7888c0a7ce6SDominik Dingel break; 7898c0a7ce6SDominik Dingel } 7908c0a7ce6SDominik Dingel return ret; 7918c0a7ce6SDominik Dingel } 7928c0a7ce6SDominik Dingel 7938c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 7944f718eabSDominik Dingel { 7954f718eabSDominik Dingel int ret; 7964f718eabSDominik Dingel unsigned int idx; 7974f718eabSDominik Dingel switch (attr->attr) { 7984f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 799f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 800c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 801e6db1d61SDominik Dingel break; 802e6db1d61SDominik Dingel 803c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 8044f718eabSDominik Dingel mutex_lock(&kvm->lock); 805a4499382SJanosch Frank if (kvm->created_vcpus) 806a4499382SJanosch Frank ret = -EBUSY; 807a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 808a4499382SJanosch Frank ret = -EINVAL; 809a4499382SJanosch Frank else { 8104f718eabSDominik Dingel kvm->arch.use_cmma = 1; 811c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 812c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 8134f718eabSDominik Dingel ret = 0; 8144f718eabSDominik Dingel } 8154f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8164f718eabSDominik Dingel break; 8174f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 818f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 819f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 820f9cbd9b0SDavid Hildenbrand break; 821c3489155SDominik Dingel ret = -EINVAL; 822c3489155SDominik Dingel if (!kvm->arch.use_cmma) 823c3489155SDominik Dingel break; 824c3489155SDominik Dingel 825c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 8264f718eabSDominik Dingel mutex_lock(&kvm->lock); 8274f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 828a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 8294f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 8304f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8314f718eabSDominik Dingel ret = 0; 8324f718eabSDominik Dingel break; 8338c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 8348c0a7ce6SDominik Dingel unsigned long new_limit; 8358c0a7ce6SDominik Dingel 8368c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 8378c0a7ce6SDominik Dingel return -EINVAL; 8388c0a7ce6SDominik Dingel 8398c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 8408c0a7ce6SDominik Dingel return -EFAULT; 8418c0a7ce6SDominik Dingel 842a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 843a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 8448c0a7ce6SDominik Dingel return -E2BIG; 8458c0a7ce6SDominik Dingel 846a3a92c31SDominik Dingel if (!new_limit) 847a3a92c31SDominik Dingel return -EINVAL; 848a3a92c31SDominik Dingel 8496ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 850a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 851a3a92c31SDominik Dingel new_limit -= 1; 852a3a92c31SDominik Dingel 8538c0a7ce6SDominik Dingel ret = -EBUSY; 8548c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 855a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 8566ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 8576ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 8588c0a7ce6SDominik Dingel 8598c0a7ce6SDominik Dingel if (!new) { 8608c0a7ce6SDominik Dingel ret = -ENOMEM; 8618c0a7ce6SDominik Dingel } else { 8626ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 8638c0a7ce6SDominik Dingel new->private = kvm; 8648c0a7ce6SDominik Dingel kvm->arch.gmap = new; 8658c0a7ce6SDominik Dingel ret = 0; 8668c0a7ce6SDominik Dingel } 8678c0a7ce6SDominik Dingel } 8688c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 869a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 870a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 871a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 8728c0a7ce6SDominik Dingel break; 8738c0a7ce6SDominik Dingel } 8744f718eabSDominik Dingel default: 8754f718eabSDominik Dingel ret = -ENXIO; 8764f718eabSDominik Dingel break; 8774f718eabSDominik Dingel } 8784f718eabSDominik Dingel return ret; 8794f718eabSDominik Dingel } 8804f718eabSDominik Dingel 881a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 882a374e892STony Krowiak 88320c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 884a374e892STony Krowiak { 885a374e892STony Krowiak struct kvm_vcpu *vcpu; 886a374e892STony Krowiak int i; 887a374e892STony Krowiak 88820c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 88920c922f0STony Krowiak 8903194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 89120c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 8923194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 8933194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 8943194cdb7SDavid Hildenbrand } 89520c922f0STony Krowiak 89620c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 89720c922f0STony Krowiak } 89820c922f0STony Krowiak 89920c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 90020c922f0STony Krowiak { 901a374e892STony Krowiak mutex_lock(&kvm->lock); 902a374e892STony Krowiak switch (attr->attr) { 903a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 9048e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9058e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 90637940fb0STony Krowiak return -EINVAL; 9078e41bd54SChristian Borntraeger } 908a374e892STony Krowiak get_random_bytes( 909a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 910a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 911a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 912c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 913a374e892STony Krowiak break; 914a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 9158e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9168e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 91737940fb0STony Krowiak return -EINVAL; 9188e41bd54SChristian Borntraeger } 919a374e892STony Krowiak get_random_bytes( 920a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 921a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 922a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 923c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 924a374e892STony Krowiak break; 925a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 9268e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9278e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 92837940fb0STony Krowiak return -EINVAL; 9298e41bd54SChristian Borntraeger } 930a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 931a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 932a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 933c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 934a374e892STony Krowiak break; 935a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 9368e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9378e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 93837940fb0STony Krowiak return -EINVAL; 9398e41bd54SChristian Borntraeger } 940a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 941a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 942a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 943c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 944a374e892STony Krowiak break; 94537940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 94637940fb0STony Krowiak if (!ap_instructions_available()) { 94737940fb0STony Krowiak mutex_unlock(&kvm->lock); 94837940fb0STony Krowiak return -EOPNOTSUPP; 94937940fb0STony Krowiak } 95037940fb0STony Krowiak kvm->arch.crypto.apie = 1; 95137940fb0STony Krowiak break; 95237940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 95337940fb0STony Krowiak if (!ap_instructions_available()) { 95437940fb0STony Krowiak mutex_unlock(&kvm->lock); 95537940fb0STony Krowiak return -EOPNOTSUPP; 95637940fb0STony Krowiak } 95737940fb0STony Krowiak kvm->arch.crypto.apie = 0; 95837940fb0STony Krowiak break; 959a374e892STony Krowiak default: 960a374e892STony Krowiak mutex_unlock(&kvm->lock); 961a374e892STony Krowiak return -ENXIO; 962a374e892STony Krowiak } 963a374e892STony Krowiak 96420c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 965a374e892STony Krowiak mutex_unlock(&kvm->lock); 966a374e892STony Krowiak return 0; 967a374e892STony Krowiak } 968a374e892STony Krowiak 969190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 970190df4a2SClaudio Imbrenda { 971190df4a2SClaudio Imbrenda int cx; 972190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 973190df4a2SClaudio Imbrenda 974190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 975190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 976190df4a2SClaudio Imbrenda } 977190df4a2SClaudio Imbrenda 978190df4a2SClaudio Imbrenda /* 979190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 9801de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 981190df4a2SClaudio Imbrenda */ 982190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 983190df4a2SClaudio Imbrenda { 984190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 985190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 986afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 987190df4a2SClaudio Imbrenda int slotnr; 988190df4a2SClaudio Imbrenda 989190df4a2SClaudio Imbrenda /* migration mode already enabled */ 990afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 991190df4a2SClaudio Imbrenda return 0; 992190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 993190df4a2SClaudio Imbrenda if (!slots || !slots->used_slots) 994190df4a2SClaudio Imbrenda return -EINVAL; 995190df4a2SClaudio Imbrenda 996afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 997afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 998afdad616SClaudio Imbrenda return 0; 999190df4a2SClaudio Imbrenda } 1000190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1001190df4a2SClaudio Imbrenda for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { 1002190df4a2SClaudio Imbrenda ms = slots->memslots + slotnr; 1003afdad616SClaudio Imbrenda /* 1004afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1005afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1006afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1007afdad616SClaudio Imbrenda * attributes. 1008afdad616SClaudio Imbrenda */ 1009afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1010afdad616SClaudio Imbrenda ram_pages += ms->npages; 1011190df4a2SClaudio Imbrenda } 1012afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1013afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1014190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1015190df4a2SClaudio Imbrenda return 0; 1016190df4a2SClaudio Imbrenda } 1017190df4a2SClaudio Imbrenda 1018190df4a2SClaudio Imbrenda /* 10191de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1020190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1021190df4a2SClaudio Imbrenda */ 1022190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1023190df4a2SClaudio Imbrenda { 1024190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1025afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1026190df4a2SClaudio Imbrenda return 0; 1027afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1028afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1029190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1030190df4a2SClaudio Imbrenda return 0; 1031190df4a2SClaudio Imbrenda } 1032190df4a2SClaudio Imbrenda 1033190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1034190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1035190df4a2SClaudio Imbrenda { 10361de1ea7eSChristian Borntraeger int res = -ENXIO; 1037190df4a2SClaudio Imbrenda 10381de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1039190df4a2SClaudio Imbrenda switch (attr->attr) { 1040190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1041190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1042190df4a2SClaudio Imbrenda break; 1043190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1044190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1045190df4a2SClaudio Imbrenda break; 1046190df4a2SClaudio Imbrenda default: 1047190df4a2SClaudio Imbrenda break; 1048190df4a2SClaudio Imbrenda } 10491de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1050190df4a2SClaudio Imbrenda 1051190df4a2SClaudio Imbrenda return res; 1052190df4a2SClaudio Imbrenda } 1053190df4a2SClaudio Imbrenda 1054190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1055190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1056190df4a2SClaudio Imbrenda { 1057afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1058190df4a2SClaudio Imbrenda 1059190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1060190df4a2SClaudio Imbrenda return -ENXIO; 1061190df4a2SClaudio Imbrenda 1062190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1063190df4a2SClaudio Imbrenda return -EFAULT; 1064190df4a2SClaudio Imbrenda return 0; 1065190df4a2SClaudio Imbrenda } 1066190df4a2SClaudio Imbrenda 10678fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 10688fa1696eSCollin L. Walling { 10698fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 10708fa1696eSCollin L. Walling 10718fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 10728fa1696eSCollin L. Walling return -EFAULT; 10738fa1696eSCollin L. Walling 10740e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 10758fa1696eSCollin L. Walling return -EINVAL; 10760e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 10778fa1696eSCollin L. Walling 10788fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 10798fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 10808fa1696eSCollin L. Walling 10818fa1696eSCollin L. Walling return 0; 10828fa1696eSCollin L. Walling } 10838fa1696eSCollin L. Walling 108472f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 108572f25020SJason J. Herne { 108672f25020SJason J. Herne u8 gtod_high; 108772f25020SJason J. Herne 108872f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 108972f25020SJason J. Herne sizeof(gtod_high))) 109072f25020SJason J. Herne return -EFAULT; 109172f25020SJason J. Herne 109272f25020SJason J. Herne if (gtod_high != 0) 109372f25020SJason J. Herne return -EINVAL; 109458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 109572f25020SJason J. Herne 109672f25020SJason J. Herne return 0; 109772f25020SJason J. Herne } 109872f25020SJason J. Herne 109972f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 110072f25020SJason J. Herne { 11010e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 110272f25020SJason J. Herne 11030e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 11040e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 110572f25020SJason J. Herne return -EFAULT; 110672f25020SJason J. Herne 11070e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11080e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 110972f25020SJason J. Herne return 0; 111072f25020SJason J. Herne } 111172f25020SJason J. Herne 111272f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 111372f25020SJason J. Herne { 111472f25020SJason J. Herne int ret; 111572f25020SJason J. Herne 111672f25020SJason J. Herne if (attr->flags) 111772f25020SJason J. Herne return -EINVAL; 111872f25020SJason J. Herne 111972f25020SJason J. Herne switch (attr->attr) { 11208fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 11218fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 11228fa1696eSCollin L. Walling break; 112372f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 112472f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 112572f25020SJason J. Herne break; 112672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 112772f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 112872f25020SJason J. Herne break; 112972f25020SJason J. Herne default: 113072f25020SJason J. Herne ret = -ENXIO; 113172f25020SJason J. Herne break; 113272f25020SJason J. Herne } 113372f25020SJason J. Herne return ret; 113472f25020SJason J. Herne } 113572f25020SJason J. Herne 113633d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 11378fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 11388fa1696eSCollin L. Walling { 11398fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 11408fa1696eSCollin L. Walling 11418fa1696eSCollin L. Walling preempt_disable(); 11428fa1696eSCollin L. Walling 11438fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 11448fa1696eSCollin L. Walling 11458fa1696eSCollin L. Walling gtod->tod = htod.tod + kvm->arch.epoch; 114633d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 114733d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 11488fa1696eSCollin L. Walling gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx; 11498fa1696eSCollin L. Walling if (gtod->tod < htod.tod) 11508fa1696eSCollin L. Walling gtod->epoch_idx += 1; 115133d1b272SDavid Hildenbrand } 11528fa1696eSCollin L. Walling 11538fa1696eSCollin L. Walling preempt_enable(); 11548fa1696eSCollin L. Walling } 11558fa1696eSCollin L. Walling 11568fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 11578fa1696eSCollin L. Walling { 11588fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 11598fa1696eSCollin L. Walling 11608fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 116133d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 11628fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 11638fa1696eSCollin L. Walling return -EFAULT; 11648fa1696eSCollin L. Walling 11658fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 11668fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11678fa1696eSCollin L. Walling return 0; 11688fa1696eSCollin L. Walling } 11698fa1696eSCollin L. Walling 117072f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 117172f25020SJason J. Herne { 117272f25020SJason J. Herne u8 gtod_high = 0; 117372f25020SJason J. Herne 117472f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 117572f25020SJason J. Herne sizeof(gtod_high))) 117672f25020SJason J. Herne return -EFAULT; 117758c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 117872f25020SJason J. Herne 117972f25020SJason J. Herne return 0; 118072f25020SJason J. Herne } 118172f25020SJason J. Herne 118272f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 118372f25020SJason J. Herne { 11845a3d883aSDavid Hildenbrand u64 gtod; 118572f25020SJason J. Herne 118660417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 118772f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 118872f25020SJason J. Herne return -EFAULT; 118958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 119072f25020SJason J. Herne 119172f25020SJason J. Herne return 0; 119272f25020SJason J. Herne } 119372f25020SJason J. Herne 119472f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 119572f25020SJason J. Herne { 119672f25020SJason J. Herne int ret; 119772f25020SJason J. Herne 119872f25020SJason J. Herne if (attr->flags) 119972f25020SJason J. Herne return -EINVAL; 120072f25020SJason J. Herne 120172f25020SJason J. Herne switch (attr->attr) { 12028fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12038fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 12048fa1696eSCollin L. Walling break; 120572f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 120672f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 120772f25020SJason J. Herne break; 120872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 120972f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 121072f25020SJason J. Herne break; 121172f25020SJason J. Herne default: 121272f25020SJason J. Herne ret = -ENXIO; 121372f25020SJason J. Herne break; 121472f25020SJason J. Herne } 121572f25020SJason J. Herne return ret; 121672f25020SJason J. Herne } 121772f25020SJason J. Herne 1218658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1219658b6edaSMichael Mueller { 1220658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1221053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1222658b6edaSMichael Mueller int ret = 0; 1223658b6edaSMichael Mueller 1224658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1225a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1226658b6edaSMichael Mueller ret = -EBUSY; 1227658b6edaSMichael Mueller goto out; 1228658b6edaSMichael Mueller } 1229658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1230658b6edaSMichael Mueller if (!proc) { 1231658b6edaSMichael Mueller ret = -ENOMEM; 1232658b6edaSMichael Mueller goto out; 1233658b6edaSMichael Mueller } 1234658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1235658b6edaSMichael Mueller sizeof(*proc))) { 12369bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1237053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1238053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 12390487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1240053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1241053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1242053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1243053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1244053dd230SDavid Hildenbrand else 1245658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1246053dd230SDavid Hildenbrand } 1247c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1248658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1249a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1250a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1251a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1252a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1253a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1254a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1255a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1256658b6edaSMichael Mueller } else 1257658b6edaSMichael Mueller ret = -EFAULT; 1258658b6edaSMichael Mueller kfree(proc); 1259658b6edaSMichael Mueller out: 1260658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1261658b6edaSMichael Mueller return ret; 1262658b6edaSMichael Mueller } 1263658b6edaSMichael Mueller 126415c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 126515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 126615c9705fSDavid Hildenbrand { 126715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 126815c9705fSDavid Hildenbrand 126915c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 127015c9705fSDavid Hildenbrand return -EFAULT; 127115c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 127215c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 127315c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 127415c9705fSDavid Hildenbrand return -EINVAL; 127515c9705fSDavid Hildenbrand 127615c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 12772f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 12782f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 12792f8311c9SChristian Borntraeger return -EBUSY; 12802f8311c9SChristian Borntraeger } 128115c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 128215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 128315c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 12842f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 12852f8311c9SChristian Borntraeger data.feat[0], 12862f8311c9SChristian Borntraeger data.feat[1], 12872f8311c9SChristian Borntraeger data.feat[2]); 12882f8311c9SChristian Borntraeger return 0; 128915c9705fSDavid Hildenbrand } 129015c9705fSDavid Hildenbrand 12910a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 12920a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 12930a763c78SDavid Hildenbrand { 1294346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1295346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1296346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1297346fa2f8SChristian Borntraeger return -EBUSY; 1298346fa2f8SChristian Borntraeger } 1299346fa2f8SChristian Borntraeger 1300346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1301346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1302346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1303346fa2f8SChristian Borntraeger return -EFAULT; 1304346fa2f8SChristian Borntraeger } 1305346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1306346fa2f8SChristian Borntraeger 130711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 130811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 130911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 131011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 131111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 131211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 131311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 131411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 131511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 131611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 131711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 131811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 131911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 132011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 132111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 132211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 132311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 132411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 132511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 132611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 132711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 132811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 132911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 133011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 133111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 133211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 133311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 133411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 133511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 133611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 133711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 133811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 133911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 134011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 134111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 134211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 134311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 134411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 134511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 134611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 134711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 134811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 134911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 135011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 135113209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 135213209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 135313209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 135411ba5961SChristian Borntraeger 1355346fa2f8SChristian Borntraeger return 0; 13560a763c78SDavid Hildenbrand } 13570a763c78SDavid Hildenbrand 1358658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1359658b6edaSMichael Mueller { 1360658b6edaSMichael Mueller int ret = -ENXIO; 1361658b6edaSMichael Mueller 1362658b6edaSMichael Mueller switch (attr->attr) { 1363658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1364658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1365658b6edaSMichael Mueller break; 136615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 136715c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 136815c9705fSDavid Hildenbrand break; 13690a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 13700a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 13710a763c78SDavid Hildenbrand break; 1372658b6edaSMichael Mueller } 1373658b6edaSMichael Mueller return ret; 1374658b6edaSMichael Mueller } 1375658b6edaSMichael Mueller 1376658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1377658b6edaSMichael Mueller { 1378658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1379658b6edaSMichael Mueller int ret = 0; 1380658b6edaSMichael Mueller 1381658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1382658b6edaSMichael Mueller if (!proc) { 1383658b6edaSMichael Mueller ret = -ENOMEM; 1384658b6edaSMichael Mueller goto out; 1385658b6edaSMichael Mueller } 13869bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1387658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1388c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1389c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1390a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1391a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1392a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1393a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1394a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1395a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1396a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1397658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1398658b6edaSMichael Mueller ret = -EFAULT; 1399658b6edaSMichael Mueller kfree(proc); 1400658b6edaSMichael Mueller out: 1401658b6edaSMichael Mueller return ret; 1402658b6edaSMichael Mueller } 1403658b6edaSMichael Mueller 1404658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1405658b6edaSMichael Mueller { 1406658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1407658b6edaSMichael Mueller int ret = 0; 1408658b6edaSMichael Mueller 1409658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 1410658b6edaSMichael Mueller if (!mach) { 1411658b6edaSMichael Mueller ret = -ENOMEM; 1412658b6edaSMichael Mueller goto out; 1413658b6edaSMichael Mueller } 1414658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 141537c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1416c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1417981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1418658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 141904478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 1420a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1421a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1422a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1423a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1424a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1425a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1426a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1427a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1428a8c39dd7SChristian Borntraeger mach->fac_list[0], 1429a8c39dd7SChristian Borntraeger mach->fac_list[1], 1430a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1431658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1432658b6edaSMichael Mueller ret = -EFAULT; 1433658b6edaSMichael Mueller kfree(mach); 1434658b6edaSMichael Mueller out: 1435658b6edaSMichael Mueller return ret; 1436658b6edaSMichael Mueller } 1437658b6edaSMichael Mueller 143815c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 143915c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 144015c9705fSDavid Hildenbrand { 144115c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 144215c9705fSDavid Hildenbrand 144315c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 144415c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 144515c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 144615c9705fSDavid Hildenbrand return -EFAULT; 14472f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14482f8311c9SChristian Borntraeger data.feat[0], 14492f8311c9SChristian Borntraeger data.feat[1], 14502f8311c9SChristian Borntraeger data.feat[2]); 145115c9705fSDavid Hildenbrand return 0; 145215c9705fSDavid Hildenbrand } 145315c9705fSDavid Hildenbrand 145415c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 145515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 145615c9705fSDavid Hildenbrand { 145715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 145815c9705fSDavid Hildenbrand 145915c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 146015c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 146115c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 146215c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 146315c9705fSDavid Hildenbrand return -EFAULT; 14642f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14652f8311c9SChristian Borntraeger data.feat[0], 14662f8311c9SChristian Borntraeger data.feat[1], 14672f8311c9SChristian Borntraeger data.feat[2]); 146815c9705fSDavid Hildenbrand return 0; 146915c9705fSDavid Hildenbrand } 147015c9705fSDavid Hildenbrand 14710a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 14720a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 14730a763c78SDavid Hildenbrand { 1474346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1475346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1476346fa2f8SChristian Borntraeger return -EFAULT; 1477346fa2f8SChristian Borntraeger 147811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 147911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 148011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 148111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 148211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 148311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 148411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 148511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 148611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 148711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 148811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 148911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 149011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 149111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 149211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 149311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 149411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 149511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 149611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 149711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 149811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 149911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 150011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 150111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 150211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 150311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 150411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 150511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 150611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 150711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 150811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 150911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 151011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 151111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 151211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 151311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 151411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 151511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 151611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 151711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 151811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 151911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 152011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 152111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 152213209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 152313209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 152413209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 152511ba5961SChristian Borntraeger 1526346fa2f8SChristian Borntraeger return 0; 15270a763c78SDavid Hildenbrand } 15280a763c78SDavid Hildenbrand 15290a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 15300a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15310a763c78SDavid Hildenbrand { 15320a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 15330a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 15340a763c78SDavid Hildenbrand return -EFAULT; 153511ba5961SChristian Borntraeger 153611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 153711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 153811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 153911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 154011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 154111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 154211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 154311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 154411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 154511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 154611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 154711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 154811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 154911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 155011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 155111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 155211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 155311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 155411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 155511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 155611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 155711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 155811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 155911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 156011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 156111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 156211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 156311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 156411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 156511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 156611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 156711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 156811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 156911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 157011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 157111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 157211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 157311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 157411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 157511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 157611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 157711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 157811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 157911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 158013209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 158113209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 158213209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 158311ba5961SChristian Borntraeger 15840a763c78SDavid Hildenbrand return 0; 15850a763c78SDavid Hildenbrand } 1586346fa2f8SChristian Borntraeger 1587658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1588658b6edaSMichael Mueller { 1589658b6edaSMichael Mueller int ret = -ENXIO; 1590658b6edaSMichael Mueller 1591658b6edaSMichael Mueller switch (attr->attr) { 1592658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1593658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1594658b6edaSMichael Mueller break; 1595658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1596658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1597658b6edaSMichael Mueller break; 159815c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 159915c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 160015c9705fSDavid Hildenbrand break; 160115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 160215c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 160315c9705fSDavid Hildenbrand break; 16040a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 16050a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 16060a763c78SDavid Hildenbrand break; 16070a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 16080a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 16090a763c78SDavid Hildenbrand break; 1610658b6edaSMichael Mueller } 1611658b6edaSMichael Mueller return ret; 1612658b6edaSMichael Mueller } 1613658b6edaSMichael Mueller 1614f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1615f2061656SDominik Dingel { 1616f2061656SDominik Dingel int ret; 1617f2061656SDominik Dingel 1618f2061656SDominik Dingel switch (attr->group) { 16194f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 16208c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 16214f718eabSDominik Dingel break; 162272f25020SJason J. Herne case KVM_S390_VM_TOD: 162372f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 162472f25020SJason J. Herne break; 1625658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1626658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1627658b6edaSMichael Mueller break; 1628a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1629a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1630a374e892STony Krowiak break; 1631190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1632190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1633190df4a2SClaudio Imbrenda break; 1634f2061656SDominik Dingel default: 1635f2061656SDominik Dingel ret = -ENXIO; 1636f2061656SDominik Dingel break; 1637f2061656SDominik Dingel } 1638f2061656SDominik Dingel 1639f2061656SDominik Dingel return ret; 1640f2061656SDominik Dingel } 1641f2061656SDominik Dingel 1642f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1643f2061656SDominik Dingel { 16448c0a7ce6SDominik Dingel int ret; 16458c0a7ce6SDominik Dingel 16468c0a7ce6SDominik Dingel switch (attr->group) { 16478c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 16488c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 16498c0a7ce6SDominik Dingel break; 165072f25020SJason J. Herne case KVM_S390_VM_TOD: 165172f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 165272f25020SJason J. Herne break; 1653658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1654658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1655658b6edaSMichael Mueller break; 1656190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1657190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1658190df4a2SClaudio Imbrenda break; 16598c0a7ce6SDominik Dingel default: 16608c0a7ce6SDominik Dingel ret = -ENXIO; 16618c0a7ce6SDominik Dingel break; 16628c0a7ce6SDominik Dingel } 16638c0a7ce6SDominik Dingel 16648c0a7ce6SDominik Dingel return ret; 1665f2061656SDominik Dingel } 1666f2061656SDominik Dingel 1667f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1668f2061656SDominik Dingel { 1669f2061656SDominik Dingel int ret; 1670f2061656SDominik Dingel 1671f2061656SDominik Dingel switch (attr->group) { 16724f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 16734f718eabSDominik Dingel switch (attr->attr) { 16744f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 16754f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1676f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1677f9cbd9b0SDavid Hildenbrand break; 16788c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 16794f718eabSDominik Dingel ret = 0; 16804f718eabSDominik Dingel break; 16814f718eabSDominik Dingel default: 16824f718eabSDominik Dingel ret = -ENXIO; 16834f718eabSDominik Dingel break; 16844f718eabSDominik Dingel } 16854f718eabSDominik Dingel break; 168672f25020SJason J. Herne case KVM_S390_VM_TOD: 168772f25020SJason J. Herne switch (attr->attr) { 168872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 168972f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 169072f25020SJason J. Herne ret = 0; 169172f25020SJason J. Herne break; 169272f25020SJason J. Herne default: 169372f25020SJason J. Herne ret = -ENXIO; 169472f25020SJason J. Herne break; 169572f25020SJason J. Herne } 169672f25020SJason J. Herne break; 1697658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1698658b6edaSMichael Mueller switch (attr->attr) { 1699658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1700658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 170115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 170215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 17030a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1704346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1705658b6edaSMichael Mueller ret = 0; 1706658b6edaSMichael Mueller break; 1707658b6edaSMichael Mueller default: 1708658b6edaSMichael Mueller ret = -ENXIO; 1709658b6edaSMichael Mueller break; 1710658b6edaSMichael Mueller } 1711658b6edaSMichael Mueller break; 1712a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1713a374e892STony Krowiak switch (attr->attr) { 1714a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1715a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1716a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1717a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1718a374e892STony Krowiak ret = 0; 1719a374e892STony Krowiak break; 172037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 172137940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 172237940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 172337940fb0STony Krowiak break; 1724a374e892STony Krowiak default: 1725a374e892STony Krowiak ret = -ENXIO; 1726a374e892STony Krowiak break; 1727a374e892STony Krowiak } 1728a374e892STony Krowiak break; 1729190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1730190df4a2SClaudio Imbrenda ret = 0; 1731190df4a2SClaudio Imbrenda break; 1732f2061656SDominik Dingel default: 1733f2061656SDominik Dingel ret = -ENXIO; 1734f2061656SDominik Dingel break; 1735f2061656SDominik Dingel } 1736f2061656SDominik Dingel 1737f2061656SDominik Dingel return ret; 1738f2061656SDominik Dingel } 1739f2061656SDominik Dingel 174030ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 174130ee2a98SJason J. Herne { 174230ee2a98SJason J. Herne uint8_t *keys; 174330ee2a98SJason J. Herne uint64_t hva; 17444f899147SChristian Borntraeger int srcu_idx, i, r = 0; 174530ee2a98SJason J. Herne 174630ee2a98SJason J. Herne if (args->flags != 0) 174730ee2a98SJason J. Herne return -EINVAL; 174830ee2a98SJason J. Herne 174930ee2a98SJason J. Herne /* Is this guest using storage keys? */ 175055531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 175130ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 175230ee2a98SJason J. Herne 175330ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 175430ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 175530ee2a98SJason J. Herne return -EINVAL; 175630ee2a98SJason J. Herne 1757752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 175830ee2a98SJason J. Herne if (!keys) 175930ee2a98SJason J. Herne return -ENOMEM; 176030ee2a98SJason J. Herne 1761d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 17624f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 176330ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 176430ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 176530ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 176630ee2a98SJason J. Herne r = -EFAULT; 1767d3ed1ceeSMartin Schwidefsky break; 176830ee2a98SJason J. Herne } 176930ee2a98SJason J. Herne 1770154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1771154c8c19SDavid Hildenbrand if (r) 1772d3ed1ceeSMartin Schwidefsky break; 177330ee2a98SJason J. Herne } 17744f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1775d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 177630ee2a98SJason J. Herne 1777d3ed1ceeSMartin Schwidefsky if (!r) { 177830ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 177930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 178030ee2a98SJason J. Herne if (r) 178130ee2a98SJason J. Herne r = -EFAULT; 1782d3ed1ceeSMartin Schwidefsky } 1783d3ed1ceeSMartin Schwidefsky 178430ee2a98SJason J. Herne kvfree(keys); 178530ee2a98SJason J. Herne return r; 178630ee2a98SJason J. Herne } 178730ee2a98SJason J. Herne 178830ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 178930ee2a98SJason J. Herne { 179030ee2a98SJason J. Herne uint8_t *keys; 179130ee2a98SJason J. Herne uint64_t hva; 17924f899147SChristian Borntraeger int srcu_idx, i, r = 0; 1793bd096f64SJanosch Frank bool unlocked; 179430ee2a98SJason J. Herne 179530ee2a98SJason J. Herne if (args->flags != 0) 179630ee2a98SJason J. Herne return -EINVAL; 179730ee2a98SJason J. Herne 179830ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 179930ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 180030ee2a98SJason J. Herne return -EINVAL; 180130ee2a98SJason J. Herne 1802752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 180330ee2a98SJason J. Herne if (!keys) 180430ee2a98SJason J. Herne return -ENOMEM; 180530ee2a98SJason J. Herne 180630ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 180730ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 180830ee2a98SJason J. Herne if (r) { 180930ee2a98SJason J. Herne r = -EFAULT; 181030ee2a98SJason J. Herne goto out; 181130ee2a98SJason J. Herne } 181230ee2a98SJason J. Herne 181330ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 181414d4a425SDominik Dingel r = s390_enable_skey(); 181514d4a425SDominik Dingel if (r) 181614d4a425SDominik Dingel goto out; 181730ee2a98SJason J. Herne 1818bd096f64SJanosch Frank i = 0; 1819d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 18204f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 1821bd096f64SJanosch Frank while (i < args->count) { 1822bd096f64SJanosch Frank unlocked = false; 182330ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 182430ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 182530ee2a98SJason J. Herne r = -EFAULT; 1826d3ed1ceeSMartin Schwidefsky break; 182730ee2a98SJason J. Herne } 182830ee2a98SJason J. Herne 182930ee2a98SJason J. Herne /* Lowest order bit is reserved */ 183030ee2a98SJason J. Herne if (keys[i] & 0x01) { 183130ee2a98SJason J. Herne r = -EINVAL; 1832d3ed1ceeSMartin Schwidefsky break; 183330ee2a98SJason J. Herne } 183430ee2a98SJason J. Herne 1835fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 1836bd096f64SJanosch Frank if (r) { 1837bd096f64SJanosch Frank r = fixup_user_fault(current, current->mm, hva, 1838bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 183930ee2a98SJason J. Herne if (r) 1840d3ed1ceeSMartin Schwidefsky break; 184130ee2a98SJason J. Herne } 1842bd096f64SJanosch Frank if (!r) 1843bd096f64SJanosch Frank i++; 1844bd096f64SJanosch Frank } 18454f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1846d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 184730ee2a98SJason J. Herne out: 184830ee2a98SJason J. Herne kvfree(keys); 184930ee2a98SJason J. Herne return r; 185030ee2a98SJason J. Herne } 185130ee2a98SJason J. Herne 18524036e387SClaudio Imbrenda /* 18534036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 18544036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 18554036e387SClaudio Imbrenda * two longs. 18564036e387SClaudio Imbrenda */ 18574036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 18584036e387SClaudio Imbrenda /* for consistency */ 18594036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 18604036e387SClaudio Imbrenda 18614036e387SClaudio Imbrenda /* 1862afdad616SClaudio Imbrenda * Similar to gfn_to_memslot, but returns the index of a memslot also when the 1863afdad616SClaudio Imbrenda * address falls in a hole. In that case the index of one of the memslots 1864afdad616SClaudio Imbrenda * bordering the hole is returned. 1865afdad616SClaudio Imbrenda */ 1866afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) 1867afdad616SClaudio Imbrenda { 1868afdad616SClaudio Imbrenda int start = 0, end = slots->used_slots; 1869afdad616SClaudio Imbrenda int slot = atomic_read(&slots->lru_slot); 1870afdad616SClaudio Imbrenda struct kvm_memory_slot *memslots = slots->memslots; 1871afdad616SClaudio Imbrenda 1872afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn && 1873afdad616SClaudio Imbrenda gfn < memslots[slot].base_gfn + memslots[slot].npages) 1874afdad616SClaudio Imbrenda return slot; 1875afdad616SClaudio Imbrenda 1876afdad616SClaudio Imbrenda while (start < end) { 1877afdad616SClaudio Imbrenda slot = start + (end - start) / 2; 1878afdad616SClaudio Imbrenda 1879afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn) 1880afdad616SClaudio Imbrenda end = slot; 1881afdad616SClaudio Imbrenda else 1882afdad616SClaudio Imbrenda start = slot + 1; 1883afdad616SClaudio Imbrenda } 1884afdad616SClaudio Imbrenda 1885afdad616SClaudio Imbrenda if (gfn >= memslots[start].base_gfn && 1886afdad616SClaudio Imbrenda gfn < memslots[start].base_gfn + memslots[start].npages) { 1887afdad616SClaudio Imbrenda atomic_set(&slots->lru_slot, start); 1888afdad616SClaudio Imbrenda } 1889afdad616SClaudio Imbrenda 1890afdad616SClaudio Imbrenda return start; 1891afdad616SClaudio Imbrenda } 1892afdad616SClaudio Imbrenda 1893afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1894afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1895afdad616SClaudio Imbrenda { 1896afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 1897afdad616SClaudio Imbrenda 1898afdad616SClaudio Imbrenda args->count = 0; 1899afdad616SClaudio Imbrenda while (args->count < bufsize) { 1900afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1901afdad616SClaudio Imbrenda /* 1902afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 1903afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 1904afdad616SClaudio Imbrenda */ 1905afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1906afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 1907afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1908afdad616SClaudio Imbrenda pgstev = 0; 1909afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1910afdad616SClaudio Imbrenda cur_gfn++; 1911afdad616SClaudio Imbrenda } 1912afdad616SClaudio Imbrenda 1913afdad616SClaudio Imbrenda return 0; 1914afdad616SClaudio Imbrenda } 1915afdad616SClaudio Imbrenda 1916afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 1917afdad616SClaudio Imbrenda unsigned long cur_gfn) 1918afdad616SClaudio Imbrenda { 1919afdad616SClaudio Imbrenda int slotidx = gfn_to_memslot_approx(slots, cur_gfn); 1920afdad616SClaudio Imbrenda struct kvm_memory_slot *ms = slots->memslots + slotidx; 1921afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 1922afdad616SClaudio Imbrenda 1923afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 1924afdad616SClaudio Imbrenda slotidx--; 1925afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 1926afdad616SClaudio Imbrenda if (slotidx < 0) 1927afdad616SClaudio Imbrenda slotidx = slots->used_slots - 1; 1928afdad616SClaudio Imbrenda 1929afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1930afdad616SClaudio Imbrenda ofs = 0; 1931afdad616SClaudio Imbrenda } 1932afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 1933afdad616SClaudio Imbrenda while ((slotidx > 0) && (ofs >= ms->npages)) { 1934afdad616SClaudio Imbrenda slotidx--; 1935afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1936afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); 1937afdad616SClaudio Imbrenda } 1938afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 1939afdad616SClaudio Imbrenda } 1940afdad616SClaudio Imbrenda 1941afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1942afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1943afdad616SClaudio Imbrenda { 1944afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 1945afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 1946afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 1947afdad616SClaudio Imbrenda 1948afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 1949afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 1950afdad616SClaudio Imbrenda args->count = 0; 1951afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 1952afdad616SClaudio Imbrenda if (!ms) 1953afdad616SClaudio Imbrenda return 0; 1954afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 1955afdad616SClaudio Imbrenda mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; 1956afdad616SClaudio Imbrenda 1957afdad616SClaudio Imbrenda while (args->count < bufsize) { 1958afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1959afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1960afdad616SClaudio Imbrenda return 0; 1961afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 1962afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 1963afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 1964afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1965afdad616SClaudio Imbrenda pgstev = 0; 1966afdad616SClaudio Imbrenda /* Save the value */ 1967afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1968afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 1969afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 1970afdad616SClaudio Imbrenda return 0; 1971afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 1972afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 1973afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 1974afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 1975afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 1976afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 1977afdad616SClaudio Imbrenda return 0; 1978afdad616SClaudio Imbrenda cur_gfn++; 1979afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 1980afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 1981afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 1982afdad616SClaudio Imbrenda if (!ms) 1983afdad616SClaudio Imbrenda return 0; 1984afdad616SClaudio Imbrenda } 1985afdad616SClaudio Imbrenda } 1986afdad616SClaudio Imbrenda return 0; 1987afdad616SClaudio Imbrenda } 1988afdad616SClaudio Imbrenda 1989afdad616SClaudio Imbrenda /* 19904036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 19914036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 19924036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 19934036e387SClaudio Imbrenda * no trailing clean bytes are saved. 19944036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 19954036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 19964036e387SClaudio Imbrenda */ 19974036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 19984036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 19994036e387SClaudio Imbrenda { 2000afdad616SClaudio Imbrenda unsigned long bufsize; 2001afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2002afdad616SClaudio Imbrenda u8 *values; 20034036e387SClaudio Imbrenda 2004afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 20054036e387SClaudio Imbrenda return -ENXIO; 20064036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 20074036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 20084036e387SClaudio Imbrenda return -EINVAL; 20094036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 20104036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2011afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 20124036e387SClaudio Imbrenda return -EINVAL; 20134036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 20144036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2015c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 20164036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20174036e387SClaudio Imbrenda return 0; 20184036e387SClaudio Imbrenda } 20194036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2020afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 20214036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20224036e387SClaudio Imbrenda return 0; 20234036e387SClaudio Imbrenda } 20244036e387SClaudio Imbrenda 2025afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2026afdad616SClaudio Imbrenda if (!values) 20274036e387SClaudio Imbrenda return -ENOMEM; 20284036e387SClaudio Imbrenda 20294036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 20304036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2031afdad616SClaudio Imbrenda if (peek) 2032afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2033afdad616SClaudio Imbrenda else 2034afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 20354036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 20364036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 20374036e387SClaudio Imbrenda 2038afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2039afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2040afdad616SClaudio Imbrenda else 2041afdad616SClaudio Imbrenda args->remaining = 0; 20424036e387SClaudio Imbrenda 2043afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2044afdad616SClaudio Imbrenda ret = -EFAULT; 2045afdad616SClaudio Imbrenda 2046afdad616SClaudio Imbrenda vfree(values); 2047afdad616SClaudio Imbrenda return ret; 20484036e387SClaudio Imbrenda } 20494036e387SClaudio Imbrenda 20504036e387SClaudio Imbrenda /* 20514036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 20524036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2053c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 20544036e387SClaudio Imbrenda */ 20554036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 20564036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 20574036e387SClaudio Imbrenda { 20584036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 20594036e387SClaudio Imbrenda uint8_t *bits; 20604036e387SClaudio Imbrenda int srcu_idx, r = 0; 20614036e387SClaudio Imbrenda 20624036e387SClaudio Imbrenda mask = args->mask; 20634036e387SClaudio Imbrenda 20644036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 20654036e387SClaudio Imbrenda return -ENXIO; 20664036e387SClaudio Imbrenda /* invalid/unsupported flags */ 20674036e387SClaudio Imbrenda if (args->flags != 0) 20684036e387SClaudio Imbrenda return -EINVAL; 20694036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 20704036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 20714036e387SClaudio Imbrenda return -EINVAL; 20724036e387SClaudio Imbrenda /* Nothing to do */ 20734036e387SClaudio Imbrenda if (args->count == 0) 20744036e387SClaudio Imbrenda return 0; 20754036e387SClaudio Imbrenda 207642bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 20774036e387SClaudio Imbrenda if (!bits) 20784036e387SClaudio Imbrenda return -ENOMEM; 20794036e387SClaudio Imbrenda 20804036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 20814036e387SClaudio Imbrenda if (r) { 20824036e387SClaudio Imbrenda r = -EFAULT; 20834036e387SClaudio Imbrenda goto out; 20844036e387SClaudio Imbrenda } 20854036e387SClaudio Imbrenda 20864036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 20874036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 20884036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 20894036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 20904036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 20914036e387SClaudio Imbrenda r = -EFAULT; 20924036e387SClaudio Imbrenda break; 20934036e387SClaudio Imbrenda } 20944036e387SClaudio Imbrenda 20954036e387SClaudio Imbrenda pgstev = bits[i]; 20964036e387SClaudio Imbrenda pgstev = pgstev << 24; 20971bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 20984036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 20994036e387SClaudio Imbrenda } 21004036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 21014036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 21024036e387SClaudio Imbrenda 2103c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 21044036e387SClaudio Imbrenda down_write(&kvm->mm->mmap_sem); 2105c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 21064036e387SClaudio Imbrenda up_write(&kvm->mm->mmap_sem); 21074036e387SClaudio Imbrenda } 21084036e387SClaudio Imbrenda out: 21094036e387SClaudio Imbrenda vfree(bits); 21104036e387SClaudio Imbrenda return r; 21114036e387SClaudio Imbrenda } 21124036e387SClaudio Imbrenda 2113b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2114b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2115b0c632dbSHeiko Carstens { 2116b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2117b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2118f2061656SDominik Dingel struct kvm_device_attr attr; 2119b0c632dbSHeiko Carstens int r; 2120b0c632dbSHeiko Carstens 2121b0c632dbSHeiko Carstens switch (ioctl) { 2122ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2123ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2124ba5c1e9bSCarsten Otte 2125ba5c1e9bSCarsten Otte r = -EFAULT; 2126ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2127ba5c1e9bSCarsten Otte break; 2128ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2129ba5c1e9bSCarsten Otte break; 2130ba5c1e9bSCarsten Otte } 213184223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 213284223598SCornelia Huck struct kvm_irq_routing_entry routing; 213384223598SCornelia Huck 213484223598SCornelia Huck r = -EINVAL; 213584223598SCornelia Huck if (kvm->arch.use_irqchip) { 213684223598SCornelia Huck /* Set up dummy routing. */ 213784223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2138152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 213984223598SCornelia Huck } 214084223598SCornelia Huck break; 214184223598SCornelia Huck } 2142f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2143f2061656SDominik Dingel r = -EFAULT; 2144f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2145f2061656SDominik Dingel break; 2146f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2147f2061656SDominik Dingel break; 2148f2061656SDominik Dingel } 2149f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2150f2061656SDominik Dingel r = -EFAULT; 2151f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2152f2061656SDominik Dingel break; 2153f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2154f2061656SDominik Dingel break; 2155f2061656SDominik Dingel } 2156f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2157f2061656SDominik Dingel r = -EFAULT; 2158f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2159f2061656SDominik Dingel break; 2160f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2161f2061656SDominik Dingel break; 2162f2061656SDominik Dingel } 216330ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 216430ee2a98SJason J. Herne struct kvm_s390_skeys args; 216530ee2a98SJason J. Herne 216630ee2a98SJason J. Herne r = -EFAULT; 216730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 216830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 216930ee2a98SJason J. Herne break; 217030ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 217130ee2a98SJason J. Herne break; 217230ee2a98SJason J. Herne } 217330ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 217430ee2a98SJason J. Herne struct kvm_s390_skeys args; 217530ee2a98SJason J. Herne 217630ee2a98SJason J. Herne r = -EFAULT; 217730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 217830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 217930ee2a98SJason J. Herne break; 218030ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 218130ee2a98SJason J. Herne break; 218230ee2a98SJason J. Herne } 21834036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 21844036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 21854036e387SClaudio Imbrenda 21864036e387SClaudio Imbrenda r = -EFAULT; 21874036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 21884036e387SClaudio Imbrenda break; 21891de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 21904036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 21911de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 21924036e387SClaudio Imbrenda if (!r) { 21934036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 21944036e387SClaudio Imbrenda if (r) 21954036e387SClaudio Imbrenda r = -EFAULT; 21964036e387SClaudio Imbrenda } 21974036e387SClaudio Imbrenda break; 21984036e387SClaudio Imbrenda } 21994036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 22004036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 22014036e387SClaudio Imbrenda 22024036e387SClaudio Imbrenda r = -EFAULT; 22034036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 22044036e387SClaudio Imbrenda break; 22051de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 22064036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 22071de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 22084036e387SClaudio Imbrenda break; 22094036e387SClaudio Imbrenda } 2210b0c632dbSHeiko Carstens default: 2211367e1319SAvi Kivity r = -ENOTTY; 2212b0c632dbSHeiko Carstens } 2213b0c632dbSHeiko Carstens 2214b0c632dbSHeiko Carstens return r; 2215b0c632dbSHeiko Carstens } 2216b0c632dbSHeiko Carstens 221745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 221845c9b47cSTony Krowiak { 2219e585b24aSTony Krowiak struct ap_config_info info; 222045c9b47cSTony Krowiak 2221e585b24aSTony Krowiak if (ap_instructions_available()) { 2222e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2223e585b24aSTony Krowiak return info.apxa; 222445c9b47cSTony Krowiak } 222545c9b47cSTony Krowiak 222645c9b47cSTony Krowiak return 0; 222745c9b47cSTony Krowiak } 222845c9b47cSTony Krowiak 2229e585b24aSTony Krowiak /* 2230e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2231e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2232e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2233e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2234e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2235e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2236e585b24aSTony Krowiak */ 223745c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 223845c9b47cSTony Krowiak { 223945c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 224045c9b47cSTony Krowiak 2241e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2242e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2243e585b24aSTony Krowiak 2244e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2245e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2246e585b24aSTony Krowiak return; 2247e585b24aSTony Krowiak 224845c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 224945c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 225045c9b47cSTony Krowiak else 225145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 225245c9b47cSTony Krowiak } 225345c9b47cSTony Krowiak 22540e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 22550e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 22560e237e44SPierre Morel { 22570e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 22580e237e44SPierre Morel 22590e237e44SPierre Morel mutex_lock(&kvm->lock); 22600e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 22610e237e44SPierre Morel 22620e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 22630e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 22640e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 22650e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 22660e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 22670e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 22680e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 22690e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 22700e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 22710e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 22720e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 22730e237e44SPierre Morel break; 22740e237e44SPierre Morel case CRYCB_FORMAT1: 22750e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 22760e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 22770e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 22780e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 22790e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 22800e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 22810e237e44SPierre Morel *((unsigned short *)adm)); 22820e237e44SPierre Morel break; 22830e237e44SPierre Morel default: /* Can not happen */ 22840e237e44SPierre Morel break; 22850e237e44SPierre Morel } 22860e237e44SPierre Morel 22870e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 22880e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 22890e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 22900e237e44SPierre Morel mutex_unlock(&kvm->lock); 22910e237e44SPierre Morel } 22920e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 22930e237e44SPierre Morel 229442104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 229542104598STony Krowiak { 229642104598STony Krowiak mutex_lock(&kvm->lock); 229742104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 229842104598STony Krowiak 229942104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 230042104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 230142104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 230242104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 230342104598STony Krowiak 23040e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 23056cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 23066cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 230742104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 230842104598STony Krowiak mutex_unlock(&kvm->lock); 230942104598STony Krowiak } 231042104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 231142104598STony Krowiak 23129bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 23139d8d5786SMichael Mueller { 23149bb0ec09SDavid Hildenbrand struct cpuid cpuid; 23159bb0ec09SDavid Hildenbrand 23169bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 23179bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 23189bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 23199d8d5786SMichael Mueller } 23209d8d5786SMichael Mueller 2321c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 23225102ee87STony Krowiak { 2323c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 232445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 23255102ee87STony Krowiak 2326e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2327e585b24aSTony Krowiak return; 2328e585b24aSTony Krowiak 2329ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2330ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2331ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2332ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2333ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2334ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2335ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 23365102ee87STony Krowiak } 23375102ee87STony Krowiak 23387d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 23397d43bafcSEugene (jno) Dvurechenski { 23407d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 23415e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 23427d43bafcSEugene (jno) Dvurechenski else 23437d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 23447d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 23457d43bafcSEugene (jno) Dvurechenski } 23467d43bafcSEugene (jno) Dvurechenski 2347e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2348b0c632dbSHeiko Carstens { 234976a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 23509d8d5786SMichael Mueller int i, rc; 2351b0c632dbSHeiko Carstens char debug_name[16]; 2352f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2353b0c632dbSHeiko Carstens 2354e08b9637SCarsten Otte rc = -EINVAL; 2355e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2356e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2357e08b9637SCarsten Otte goto out_err; 2358e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2359e08b9637SCarsten Otte goto out_err; 2360e08b9637SCarsten Otte #else 2361e08b9637SCarsten Otte if (type) 2362e08b9637SCarsten Otte goto out_err; 2363e08b9637SCarsten Otte #endif 2364e08b9637SCarsten Otte 2365b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2366b0c632dbSHeiko Carstens if (rc) 2367d89f5effSJan Kiszka goto out_err; 2368b0c632dbSHeiko Carstens 2369b290411aSCarsten Otte rc = -ENOMEM; 2370b290411aSCarsten Otte 237176a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 237276a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 23735e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 23749ac96d75SDavid Hildenbrand /* start with basic SCA */ 237576a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2376b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2377d89f5effSJan Kiszka goto out_err; 2378f6c137ffSChristian Borntraeger spin_lock(&kvm_lock); 2379c5c2c393SDavid Hildenbrand sca_offset += 16; 2380bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2381c5c2c393SDavid Hildenbrand sca_offset = 0; 2382bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2383bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 2384f6c137ffSChristian Borntraeger spin_unlock(&kvm_lock); 2385b0c632dbSHeiko Carstens 2386b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2387b0c632dbSHeiko Carstens 23881cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2389b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 239040f5b735SDominik Dingel goto out_err; 2391b0c632dbSHeiko Carstens 239219114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2393c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2394c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 2395c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 239640f5b735SDominik Dingel goto out_err; 23979d8d5786SMichael Mueller 239825c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2399c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2400c3b9e3e1SChristian Borntraeger 2401c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 2402c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] & 2403c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2404c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 2405c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] & 2406c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2407c3b9e3e1SChristian Borntraeger } 2408346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2409981467c9SMichael Mueller 24101935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 24111935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 24121935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 24131935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 241495ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 241595ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 24161bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 24171bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 24181bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 24191bab1c02SClaudio Imbrenda } 242095ca2cb5SJanosch Frank 24219bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 242237c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 24239d8d5786SMichael Mueller 2424c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 24255102ee87STony Krowiak 242651978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2427ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 24286d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 24296d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 24308a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2431a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2432ba5c1e9bSCarsten Otte 2433b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 243478f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2435b0c632dbSHeiko Carstens 2436e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2437e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2438a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2439e08b9637SCarsten Otte } else { 244032e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2441ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 244232e6b236SGuenther Hutzl else 2443ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 244432e6b236SGuenther Hutzl sclp.hamax + 1); 24456ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2446598841caSCarsten Otte if (!kvm->arch.gmap) 244740f5b735SDominik Dingel goto out_err; 24482c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 244924eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2450e08b9637SCarsten Otte } 2451fa6b7fe9SCornelia Huck 2452c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 245355531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 24548ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2455a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2456d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 24578335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 24588ad35755SDavid Hildenbrand 2459d89f5effSJan Kiszka return 0; 2460d89f5effSJan Kiszka out_err: 2461c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 246240f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 24637d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 246478f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2465d89f5effSJan Kiszka return rc; 2466b0c632dbSHeiko Carstens } 2467b0c632dbSHeiko Carstens 2468235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void) 2469235539b4SLuiz Capitulino { 2470235539b4SLuiz Capitulino return false; 2471235539b4SLuiz Capitulino } 2472235539b4SLuiz Capitulino 2473235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) 2474235539b4SLuiz Capitulino { 2475235539b4SLuiz Capitulino return 0; 2476235539b4SLuiz Capitulino } 2477235539b4SLuiz Capitulino 2478d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2479d329c035SChristian Borntraeger { 2480d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2481ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 248267335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 24833c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2484bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2485a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 248627e0393fSCarsten Otte 248727e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 24886ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 248927e0393fSCarsten Otte 2490e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2491b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 2492d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2493b31288faSKonstantin Weitz 24946692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 2495b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 2496d329c035SChristian Borntraeger } 2497d329c035SChristian Borntraeger 2498d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 2499d329c035SChristian Borntraeger { 2500d329c035SChristian Borntraeger unsigned int i; 2501988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 2502d329c035SChristian Borntraeger 2503988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 2504988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 2505988a2caeSGleb Natapov 2506988a2caeSGleb Natapov mutex_lock(&kvm->lock); 2507988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 2508d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 2509988a2caeSGleb Natapov 2510988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 2511988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 2512d329c035SChristian Borntraeger } 2513d329c035SChristian Borntraeger 2514b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2515b0c632dbSHeiko Carstens { 2516d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 25177d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2518d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 2519d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 2520c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 252127e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 25226ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2523841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 252467335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2525a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 25268335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2527b0c632dbSHeiko Carstens } 2528b0c632dbSHeiko Carstens 2529b0c632dbSHeiko Carstens /* Section: vcpu related */ 2530dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2531b0c632dbSHeiko Carstens { 25326ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 253327e0393fSCarsten Otte if (!vcpu->arch.gmap) 253427e0393fSCarsten Otte return -ENOMEM; 25352c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2536dafd032aSDominik Dingel 253727e0393fSCarsten Otte return 0; 253827e0393fSCarsten Otte } 253927e0393fSCarsten Otte 2540a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2541a6e2f683SEugene (jno) Dvurechenski { 2542a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2543a6940674SDavid Hildenbrand return; 25445e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 25457d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 25467d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 25477d43bafcSEugene (jno) Dvurechenski 25487d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 25497d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 25507d43bafcSEugene (jno) Dvurechenski } else { 2551bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2552a6e2f683SEugene (jno) Dvurechenski 2553a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2554a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2555a6e2f683SEugene (jno) Dvurechenski } 25565e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 25577d43bafcSEugene (jno) Dvurechenski } 2558a6e2f683SEugene (jno) Dvurechenski 2559eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2560a6e2f683SEugene (jno) Dvurechenski { 2561a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2562a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2563a6940674SDavid Hildenbrand 2564a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2565a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2566a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2567f07afa04SDavid Hildenbrand return; 2568a6940674SDavid Hildenbrand } 2569eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2570eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2571eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 25727d43bafcSEugene (jno) Dvurechenski 2573eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 25747d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 25757d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 25760c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2577eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 25787d43bafcSEugene (jno) Dvurechenski } else { 2579eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2580a6e2f683SEugene (jno) Dvurechenski 2581eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2582a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2583a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2584eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2585a6e2f683SEugene (jno) Dvurechenski } 2586eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 25875e044315SEugene (jno) Dvurechenski } 25885e044315SEugene (jno) Dvurechenski 25895e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 25905e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 25915e044315SEugene (jno) Dvurechenski { 25925e044315SEugene (jno) Dvurechenski d->sda = s->sda; 25935e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 25945e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 25955e044315SEugene (jno) Dvurechenski } 25965e044315SEugene (jno) Dvurechenski 25975e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 25985e044315SEugene (jno) Dvurechenski { 25995e044315SEugene (jno) Dvurechenski int i; 26005e044315SEugene (jno) Dvurechenski 26015e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 26025e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 26035e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 26045e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 26055e044315SEugene (jno) Dvurechenski } 26065e044315SEugene (jno) Dvurechenski 26075e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 26085e044315SEugene (jno) Dvurechenski { 26095e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 26105e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 26115e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 26125e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 26135e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 26145e044315SEugene (jno) Dvurechenski 26155e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 26165e044315SEugene (jno) Dvurechenski if (!new_sca) 26175e044315SEugene (jno) Dvurechenski return -ENOMEM; 26185e044315SEugene (jno) Dvurechenski 26195e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 26205e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 26215e044315SEugene (jno) Dvurechenski 26225e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 26235e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 26245e044315SEugene (jno) Dvurechenski 26255e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 26265e044315SEugene (jno) Dvurechenski 26275e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 26285e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 26295e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 26300c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 26315e044315SEugene (jno) Dvurechenski } 26325e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 26335e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 26345e044315SEugene (jno) Dvurechenski 26355e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 26365e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 26375e044315SEugene (jno) Dvurechenski 26385e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 26395e044315SEugene (jno) Dvurechenski 26408335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 26418335713aSChristian Borntraeger old_sca, kvm->arch.sca); 26425e044315SEugene (jno) Dvurechenski return 0; 26437d43bafcSEugene (jno) Dvurechenski } 2644a6e2f683SEugene (jno) Dvurechenski 2645a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2646a6e2f683SEugene (jno) Dvurechenski { 26475e044315SEugene (jno) Dvurechenski int rc; 26485e044315SEugene (jno) Dvurechenski 2649a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2650a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2651a6940674SDavid Hildenbrand return true; 2652a6940674SDavid Hildenbrand return false; 2653a6940674SDavid Hildenbrand } 26545e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 26555e044315SEugene (jno) Dvurechenski return true; 265676a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 26575e044315SEugene (jno) Dvurechenski return false; 26585e044315SEugene (jno) Dvurechenski 26595e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 26605e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 26615e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 26625e044315SEugene (jno) Dvurechenski 26635e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2664a6e2f683SEugene (jno) Dvurechenski } 2665a6e2f683SEugene (jno) Dvurechenski 2666dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 2667dafd032aSDominik Dingel { 2668dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 2669dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 267059674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 267159674c1aSChristian Borntraeger KVM_SYNC_GPRS | 26729eed0735SChristian Borntraeger KVM_SYNC_ACRS | 2673b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 2674b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 2675b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 267675a4615cSJulius Niedworok kvm_s390_set_prefix(vcpu, 0); 2677c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 2678c6e5f166SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 267935b3fde6SChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 82)) 268035b3fde6SChristian Borntraeger vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 26814e0b1ab7SFan Zhang if (test_kvm_facility(vcpu->kvm, 133)) 26824e0b1ab7SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 2683a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 2684a3da7b4aSChristian Borntraeger vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 2685f6aa6dc4SDavid Hildenbrand /* fprs can be synchronized via vrs, even if the guest has no vx. With 2686f6aa6dc4SDavid Hildenbrand * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 2687f6aa6dc4SDavid Hildenbrand */ 2688f6aa6dc4SDavid Hildenbrand if (MACHINE_HAS_VX) 268968c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 26906fd8e67dSDavid Hildenbrand else 26916fd8e67dSDavid Hildenbrand vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 2692dafd032aSDominik Dingel 2693dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 2694dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 2695dafd032aSDominik Dingel 2696b0c632dbSHeiko Carstens return 0; 2697b0c632dbSHeiko Carstens } 2698b0c632dbSHeiko Carstens 2699db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2700db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2701db0758b2SDavid Hildenbrand { 2702db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 27039c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2704db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 27059c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2706db0758b2SDavid Hildenbrand } 2707db0758b2SDavid Hildenbrand 2708db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2709db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2710db0758b2SDavid Hildenbrand { 2711db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 27129c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2713db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 2714db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 27159c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2716db0758b2SDavid Hildenbrand } 2717db0758b2SDavid Hildenbrand 2718db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2719db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2720db0758b2SDavid Hildenbrand { 2721db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 2722db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 2723db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 2724db0758b2SDavid Hildenbrand } 2725db0758b2SDavid Hildenbrand 2726db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2727db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2728db0758b2SDavid Hildenbrand { 2729db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 2730db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2731db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 2732db0758b2SDavid Hildenbrand } 2733db0758b2SDavid Hildenbrand 2734db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2735db0758b2SDavid Hildenbrand { 2736db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2737db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 2738db0758b2SDavid Hildenbrand preempt_enable(); 2739db0758b2SDavid Hildenbrand } 2740db0758b2SDavid Hildenbrand 2741db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2742db0758b2SDavid Hildenbrand { 2743db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2744db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 2745db0758b2SDavid Hildenbrand preempt_enable(); 2746db0758b2SDavid Hildenbrand } 2747db0758b2SDavid Hildenbrand 27484287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 27494287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 27504287f247SDavid Hildenbrand { 2751db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 27529c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2753db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 2754db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 27554287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 27569c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2757db0758b2SDavid Hildenbrand preempt_enable(); 27584287f247SDavid Hildenbrand } 27594287f247SDavid Hildenbrand 2760db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 27614287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 27624287f247SDavid Hildenbrand { 27639c23a131SDavid Hildenbrand unsigned int seq; 2764db0758b2SDavid Hildenbrand __u64 value; 2765db0758b2SDavid Hildenbrand 2766db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 27674287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 2768db0758b2SDavid Hildenbrand 27699c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 27709c23a131SDavid Hildenbrand do { 27719c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 27729c23a131SDavid Hildenbrand /* 27739c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 27749c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 27759c23a131SDavid Hildenbrand */ 27769c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 2777db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 27789c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 27799c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 2780db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 27819c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 27829c23a131SDavid Hildenbrand preempt_enable(); 2783db0758b2SDavid Hildenbrand return value; 27844287f247SDavid Hildenbrand } 27854287f247SDavid Hildenbrand 2786b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 2787b0c632dbSHeiko Carstens { 27889977e886SHendrik Brueckner 278937d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 2790ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 27915ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2792db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 279301a745acSDavid Hildenbrand vcpu->cpu = cpu; 2794b0c632dbSHeiko Carstens } 2795b0c632dbSHeiko Carstens 2796b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 2797b0c632dbSHeiko Carstens { 279801a745acSDavid Hildenbrand vcpu->cpu = -1; 27995ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2800db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 28019daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 280237d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 280337d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 28049977e886SHendrik Brueckner 2805b0c632dbSHeiko Carstens } 2806b0c632dbSHeiko Carstens 2807b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 2808b0c632dbSHeiko Carstens { 2809b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 2810b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 2811b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 28128d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 28134287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, 0); 2814b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 2815b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 2816b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 2817b9224cd7SDavid Hildenbrand vcpu->arch.sie_block->gcr[0] = CR0_UNUSED_56 | 2818b9224cd7SDavid Hildenbrand CR0_INTERRUPT_KEY_SUBMASK | 2819b9224cd7SDavid Hildenbrand CR0_MEASUREMENT_ALERT_SUBMASK; 2820b9224cd7SDavid Hildenbrand vcpu->arch.sie_block->gcr[14] = CR14_UNUSED_32 | 2821b9224cd7SDavid Hildenbrand CR14_UNUSED_33 | 2822b9224cd7SDavid Hildenbrand CR14_EXTERNAL_DAMAGE_SUBMASK; 28239abc2a08SDavid Hildenbrand /* make sure the new fpc will be lazily loaded */ 28249abc2a08SDavid Hildenbrand save_fpu_regs(); 28259abc2a08SDavid Hildenbrand current->thread.fpu.fpc = 0; 2826b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 2827672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 282835b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 28293c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 28303c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 28316352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 28326852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 28332ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 2834b0c632dbSHeiko Carstens } 2835b0c632dbSHeiko Carstens 283631928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 283742897d86SMarcelo Tosatti { 283872f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 2839fdf03650SFan Zhang preempt_disable(); 284072f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 2841d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 2842fdf03650SFan Zhang preempt_enable(); 284372f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 284425508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 2845dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 2846eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 284725508824SDavid Hildenbrand } 28486502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 28496502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 285037d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 285137d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 285242897d86SMarcelo Tosatti } 285342897d86SMarcelo Tosatti 28545102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 28555102ee87STony Krowiak { 2856e585b24aSTony Krowiak /* 2857e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 2858e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 2859e585b24aSTony Krowiak */ 2860e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 28615102ee87STony Krowiak return; 28625102ee87STony Krowiak 2863e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 2864a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 286537940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 2866a374e892STony Krowiak 2867e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 2868e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 2869e585b24aSTony Krowiak 2870e585b24aSTony Krowiak /* Set up protected key support */ 2871a374e892STony Krowiak if (vcpu->kvm->arch.crypto.aes_kw) 2872a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 2873a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 2874a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 28755102ee87STony Krowiak } 28765102ee87STony Krowiak 2877b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 2878b31605c1SDominik Dingel { 2879b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 2880b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 2881b31605c1SDominik Dingel } 2882b31605c1SDominik Dingel 2883b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 2884b31605c1SDominik Dingel { 2885b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 2886b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 2887b31605c1SDominik Dingel return -ENOMEM; 2888b31605c1SDominik Dingel return 0; 2889b31605c1SDominik Dingel } 2890b31605c1SDominik Dingel 289191520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 289291520f1aSMichael Mueller { 289391520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 289491520f1aSMichael Mueller 289591520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 289680bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 2897c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 289891520f1aSMichael Mueller } 289991520f1aSMichael Mueller 2900b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 2901b0c632dbSHeiko Carstens { 2902b31605c1SDominik Dingel int rc = 0; 2903b31288faSKonstantin Weitz 29049e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 29059e6dabefSCornelia Huck CPUSTAT_SM | 2906a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 2907a4a4f191SGuenther Hutzl 290853df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 2909ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 291053df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 2911ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 2912a4a4f191SGuenther Hutzl 291391520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 291491520f1aSMichael Mueller 2915bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 2916bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 29170c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 2918bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 29190c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 2920f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 29210c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 29227feb6bb8SMichael Mueller 2923c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 29240c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 2925cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 29260c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 29270c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 292848ee7d3aSDavid Hildenbrand if (sclp.has_cei) 29290c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 293011ad65b7SDavid Hildenbrand if (sclp.has_ib) 29310c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 293237c5f6c8SDavid Hildenbrand if (sclp.has_siif) 29330c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 293437c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 29350c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 293618280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 29370c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 29380c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 293913211ea7SEric Farman } 29408fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 29418fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 2942a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 2943a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 2944d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 2945d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 2946d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 2947d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 2948d7c5cb01SMichael Mueller } 29494e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 29504e0b1ab7SFan Zhang | SDNXC; 2951c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 2952730cd632SFarhan Ali 2953730cd632SFarhan Ali if (sclp.has_kss) 2954ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 2955730cd632SFarhan Ali else 2956492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 29575a5e6536SMatthew Rosato 2958e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 2959b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 2960b31605c1SDominik Dingel if (rc) 2961b31605c1SDominik Dingel return rc; 2962b31288faSKonstantin Weitz } 29630ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2964ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 29659d8d5786SMichael Mueller 296667d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 296767d49d52SCollin Walling 29685102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 29695102ee87STony Krowiak 2970b31605c1SDominik Dingel return rc; 2971b0c632dbSHeiko Carstens } 2972b0c632dbSHeiko Carstens 2973b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 2974b0c632dbSHeiko Carstens unsigned int id) 2975b0c632dbSHeiko Carstens { 29764d47555aSCarsten Otte struct kvm_vcpu *vcpu; 29777feb6bb8SMichael Mueller struct sie_page *sie_page; 29784d47555aSCarsten Otte int rc = -EINVAL; 2979b0c632dbSHeiko Carstens 29804215825eSDavid Hildenbrand if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 29814d47555aSCarsten Otte goto out; 29824d47555aSCarsten Otte 29834d47555aSCarsten Otte rc = -ENOMEM; 29844d47555aSCarsten Otte 2985b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 2986b0c632dbSHeiko Carstens if (!vcpu) 29874d47555aSCarsten Otte goto out; 2988b0c632dbSHeiko Carstens 2989da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 29907feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 29917feb6bb8SMichael Mueller if (!sie_page) 2992b0c632dbSHeiko Carstens goto out_free_cpu; 2993b0c632dbSHeiko Carstens 29947feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 29957feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 29967feb6bb8SMichael Mueller 2997efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 2998efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 2999efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3000efed1104SDavid Hildenbrand 3001b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 3002ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3003982cff42SMichael Mueller vcpu->arch.sie_block->gd = (u32)(u64)kvm->arch.gisa_int.origin; 30044b9f9525SMichael Mueller if (vcpu->arch.sie_block->gd && sclp.has_gisaf) 30054b9f9525SMichael Mueller vcpu->arch.sie_block->gd |= GISA_FORMAT1; 30069c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3007ba5c1e9bSCarsten Otte 3008b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 3009b0c632dbSHeiko Carstens if (rc) 30109abc2a08SDavid Hildenbrand goto out_free_sie_block; 30118335713aSChristian Borntraeger VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu, 3012b0c632dbSHeiko Carstens vcpu->arch.sie_block); 3013ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 3014b0c632dbSHeiko Carstens 3015b0c632dbSHeiko Carstens return vcpu; 30167b06bf2fSWei Yongjun out_free_sie_block: 30177b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3018b0c632dbSHeiko Carstens out_free_cpu: 3019b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 30204d47555aSCarsten Otte out: 3021b0c632dbSHeiko Carstens return ERR_PTR(rc); 3022b0c632dbSHeiko Carstens } 3023b0c632dbSHeiko Carstens 3024b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3025b0c632dbSHeiko Carstens { 30269a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3027b0c632dbSHeiko Carstens } 3028b0c632dbSHeiko Carstens 3029199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3030199b5763SLongpeng(Mike) { 30310546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3032199b5763SLongpeng(Mike) } 3033199b5763SLongpeng(Mike) 303427406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 303549b99e1eSChristian Borntraeger { 3036805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 303761a6df54SDavid Hildenbrand exit_sie(vcpu); 303849b99e1eSChristian Borntraeger } 303949b99e1eSChristian Borntraeger 304027406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 304149b99e1eSChristian Borntraeger { 3042805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 304349b99e1eSChristian Borntraeger } 304449b99e1eSChristian Borntraeger 30458e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 30468e236546SChristian Borntraeger { 3047805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 304861a6df54SDavid Hildenbrand exit_sie(vcpu); 30498e236546SChristian Borntraeger } 30508e236546SChristian Borntraeger 30519ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 30529ea59728SDavid Hildenbrand { 30539ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 30549ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 30559ea59728SDavid Hildenbrand } 30569ea59728SDavid Hildenbrand 30578e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 30588e236546SChristian Borntraeger { 30599bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 30608e236546SChristian Borntraeger } 30618e236546SChristian Borntraeger 306249b99e1eSChristian Borntraeger /* 30639ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 306449b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 306549b99e1eSChristian Borntraeger * return immediately. */ 306649b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 306749b99e1eSChristian Borntraeger { 3068ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 30699ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 307049b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 307149b99e1eSChristian Borntraeger cpu_relax(); 307249b99e1eSChristian Borntraeger } 307349b99e1eSChristian Borntraeger 30748e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 30758e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 307649b99e1eSChristian Borntraeger { 30778e236546SChristian Borntraeger kvm_make_request(req, vcpu); 30788e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 307949b99e1eSChristian Borntraeger } 308049b99e1eSChristian Borntraeger 3081414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3082414d3b07SMartin Schwidefsky unsigned long end) 30832c70fe44SChristian Borntraeger { 30842c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 30852c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3086414d3b07SMartin Schwidefsky unsigned long prefix; 3087414d3b07SMartin Schwidefsky int i; 30882c70fe44SChristian Borntraeger 308965d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 309065d0b0d4SDavid Hildenbrand return; 3091414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3092414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3093414d3b07SMartin Schwidefsky return; 30942c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 30952c70fe44SChristian Borntraeger /* match against both prefix pages */ 3096414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3097414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3098414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3099414d3b07SMartin Schwidefsky start, end); 31008e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 31012c70fe44SChristian Borntraeger } 31022c70fe44SChristian Borntraeger } 31032c70fe44SChristian Borntraeger } 31042c70fe44SChristian Borntraeger 3105b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3106b6d33834SChristoffer Dall { 3107b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3108b6d33834SChristoffer Dall BUG(); 3109b6d33834SChristoffer Dall return 0; 3110b6d33834SChristoffer Dall } 3111b6d33834SChristoffer Dall 311214eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 311314eebd91SCarsten Otte struct kvm_one_reg *reg) 311414eebd91SCarsten Otte { 311514eebd91SCarsten Otte int r = -EINVAL; 311614eebd91SCarsten Otte 311714eebd91SCarsten Otte switch (reg->id) { 311829b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 311929b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 312029b7c71bSCarsten Otte (u32 __user *)reg->addr); 312129b7c71bSCarsten Otte break; 312229b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 312329b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 312429b7c71bSCarsten Otte (u64 __user *)reg->addr); 312529b7c71bSCarsten Otte break; 312646a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 31274287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 312846a6dd1cSJason J. herne (u64 __user *)reg->addr); 312946a6dd1cSJason J. herne break; 313046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 313146a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 313246a6dd1cSJason J. herne (u64 __user *)reg->addr); 313346a6dd1cSJason J. herne break; 3134536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3135536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3136536336c2SDominik Dingel (u64 __user *)reg->addr); 3137536336c2SDominik Dingel break; 3138536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3139536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3140536336c2SDominik Dingel (u64 __user *)reg->addr); 3141536336c2SDominik Dingel break; 3142536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3143536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3144536336c2SDominik Dingel (u64 __user *)reg->addr); 3145536336c2SDominik Dingel break; 3146672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3147672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3148672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3149672550fbSChristian Borntraeger break; 3150afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3151afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3152afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3153afa45ff5SChristian Borntraeger break; 315414eebd91SCarsten Otte default: 315514eebd91SCarsten Otte break; 315614eebd91SCarsten Otte } 315714eebd91SCarsten Otte 315814eebd91SCarsten Otte return r; 315914eebd91SCarsten Otte } 316014eebd91SCarsten Otte 316114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 316214eebd91SCarsten Otte struct kvm_one_reg *reg) 316314eebd91SCarsten Otte { 316414eebd91SCarsten Otte int r = -EINVAL; 31654287f247SDavid Hildenbrand __u64 val; 316614eebd91SCarsten Otte 316714eebd91SCarsten Otte switch (reg->id) { 316829b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 316929b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 317029b7c71bSCarsten Otte (u32 __user *)reg->addr); 317129b7c71bSCarsten Otte break; 317229b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 317329b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 317429b7c71bSCarsten Otte (u64 __user *)reg->addr); 317529b7c71bSCarsten Otte break; 317646a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 31774287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 31784287f247SDavid Hildenbrand if (!r) 31794287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 318046a6dd1cSJason J. herne break; 318146a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 318246a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 318346a6dd1cSJason J. herne (u64 __user *)reg->addr); 318446a6dd1cSJason J. herne break; 3185536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3186536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3187536336c2SDominik Dingel (u64 __user *)reg->addr); 31889fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 31899fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3190536336c2SDominik Dingel break; 3191536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3192536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3193536336c2SDominik Dingel (u64 __user *)reg->addr); 3194536336c2SDominik Dingel break; 3195536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3196536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3197536336c2SDominik Dingel (u64 __user *)reg->addr); 3198536336c2SDominik Dingel break; 3199672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3200672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3201672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3202672550fbSChristian Borntraeger break; 3203afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3204afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3205afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3206afa45ff5SChristian Borntraeger break; 320714eebd91SCarsten Otte default: 320814eebd91SCarsten Otte break; 320914eebd91SCarsten Otte } 321014eebd91SCarsten Otte 321114eebd91SCarsten Otte return r; 321214eebd91SCarsten Otte } 3213b6d33834SChristoffer Dall 3214b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 3215b0c632dbSHeiko Carstens { 3216b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 3217b0c632dbSHeiko Carstens return 0; 3218b0c632dbSHeiko Carstens } 3219b0c632dbSHeiko Carstens 3220b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3221b0c632dbSHeiko Carstens { 3222875656feSChristoffer Dall vcpu_load(vcpu); 32235a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3224875656feSChristoffer Dall vcpu_put(vcpu); 3225b0c632dbSHeiko Carstens return 0; 3226b0c632dbSHeiko Carstens } 3227b0c632dbSHeiko Carstens 3228b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3229b0c632dbSHeiko Carstens { 32301fc9b76bSChristoffer Dall vcpu_load(vcpu); 32315a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 32321fc9b76bSChristoffer Dall vcpu_put(vcpu); 3233b0c632dbSHeiko Carstens return 0; 3234b0c632dbSHeiko Carstens } 3235b0c632dbSHeiko Carstens 3236b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3237b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3238b0c632dbSHeiko Carstens { 3239b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3240b4ef9d4eSChristoffer Dall 324159674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3242b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3243b4ef9d4eSChristoffer Dall 3244b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3245b0c632dbSHeiko Carstens return 0; 3246b0c632dbSHeiko Carstens } 3247b0c632dbSHeiko Carstens 3248b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3249b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3250b0c632dbSHeiko Carstens { 3251bcdec41cSChristoffer Dall vcpu_load(vcpu); 3252bcdec41cSChristoffer Dall 325359674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3254b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3255bcdec41cSChristoffer Dall 3256bcdec41cSChristoffer Dall vcpu_put(vcpu); 3257b0c632dbSHeiko Carstens return 0; 3258b0c632dbSHeiko Carstens } 3259b0c632dbSHeiko Carstens 3260b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3261b0c632dbSHeiko Carstens { 32626a96bc7fSChristoffer Dall int ret = 0; 32636a96bc7fSChristoffer Dall 32646a96bc7fSChristoffer Dall vcpu_load(vcpu); 32656a96bc7fSChristoffer Dall 32666a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 32676a96bc7fSChristoffer Dall ret = -EINVAL; 32686a96bc7fSChristoffer Dall goto out; 32696a96bc7fSChristoffer Dall } 3270e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 32719abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3272a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3273a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 32749abc2a08SDavid Hildenbrand else 3275a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 32766a96bc7fSChristoffer Dall 32776a96bc7fSChristoffer Dall out: 32786a96bc7fSChristoffer Dall vcpu_put(vcpu); 32796a96bc7fSChristoffer Dall return ret; 3280b0c632dbSHeiko Carstens } 3281b0c632dbSHeiko Carstens 3282b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3283b0c632dbSHeiko Carstens { 32841393123eSChristoffer Dall vcpu_load(vcpu); 32851393123eSChristoffer Dall 32869abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 32879abc2a08SDavid Hildenbrand save_fpu_regs(); 32889abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3289a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3290a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 32919abc2a08SDavid Hildenbrand else 3292a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3293e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 32941393123eSChristoffer Dall 32951393123eSChristoffer Dall vcpu_put(vcpu); 3296b0c632dbSHeiko Carstens return 0; 3297b0c632dbSHeiko Carstens } 3298b0c632dbSHeiko Carstens 3299b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3300b0c632dbSHeiko Carstens { 3301b0c632dbSHeiko Carstens int rc = 0; 3302b0c632dbSHeiko Carstens 33037a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3304b0c632dbSHeiko Carstens rc = -EBUSY; 3305d7b0b5ebSCarsten Otte else { 3306d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3307d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3308d7b0b5ebSCarsten Otte } 3309b0c632dbSHeiko Carstens return rc; 3310b0c632dbSHeiko Carstens } 3311b0c632dbSHeiko Carstens 3312b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3313b0c632dbSHeiko Carstens struct kvm_translation *tr) 3314b0c632dbSHeiko Carstens { 3315b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3316b0c632dbSHeiko Carstens } 3317b0c632dbSHeiko Carstens 331827291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 331927291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 332027291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 332127291e21SDavid Hildenbrand 3322d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3323d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3324b0c632dbSHeiko Carstens { 332527291e21SDavid Hildenbrand int rc = 0; 332627291e21SDavid Hildenbrand 332766b56562SChristoffer Dall vcpu_load(vcpu); 332866b56562SChristoffer Dall 332927291e21SDavid Hildenbrand vcpu->guest_debug = 0; 333027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 333127291e21SDavid Hildenbrand 333266b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 333366b56562SChristoffer Dall rc = -EINVAL; 333466b56562SChristoffer Dall goto out; 333566b56562SChristoffer Dall } 333666b56562SChristoffer Dall if (!sclp.has_gpere) { 333766b56562SChristoffer Dall rc = -EINVAL; 333866b56562SChristoffer Dall goto out; 333966b56562SChristoffer Dall } 334027291e21SDavid Hildenbrand 334127291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 334227291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 334327291e21SDavid Hildenbrand /* enforce guest PER */ 3344ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 334527291e21SDavid Hildenbrand 334627291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 334727291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 334827291e21SDavid Hildenbrand } else { 33499daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 335027291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 335127291e21SDavid Hildenbrand } 335227291e21SDavid Hildenbrand 335327291e21SDavid Hildenbrand if (rc) { 335427291e21SDavid Hildenbrand vcpu->guest_debug = 0; 335527291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 33569daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 335727291e21SDavid Hildenbrand } 335827291e21SDavid Hildenbrand 335966b56562SChristoffer Dall out: 336066b56562SChristoffer Dall vcpu_put(vcpu); 336127291e21SDavid Hildenbrand return rc; 3362b0c632dbSHeiko Carstens } 3363b0c632dbSHeiko Carstens 336462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 336562d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 336662d9f0dbSMarcelo Tosatti { 3367fd232561SChristoffer Dall int ret; 3368fd232561SChristoffer Dall 3369fd232561SChristoffer Dall vcpu_load(vcpu); 3370fd232561SChristoffer Dall 33716352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3372fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 33736352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3374fd232561SChristoffer Dall 3375fd232561SChristoffer Dall vcpu_put(vcpu); 3376fd232561SChristoffer Dall return ret; 337762d9f0dbSMarcelo Tosatti } 337862d9f0dbSMarcelo Tosatti 337962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 338062d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 338162d9f0dbSMarcelo Tosatti { 33826352e4d2SDavid Hildenbrand int rc = 0; 33836352e4d2SDavid Hildenbrand 3384e83dff5eSChristoffer Dall vcpu_load(vcpu); 3385e83dff5eSChristoffer Dall 33866352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 33876352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 33886352e4d2SDavid Hildenbrand 33896352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 33906352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 33916352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 33926352e4d2SDavid Hildenbrand break; 33936352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 33946352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 33956352e4d2SDavid Hildenbrand break; 33966352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 33976352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 33986352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 33996352e4d2SDavid Hildenbrand default: 34006352e4d2SDavid Hildenbrand rc = -ENXIO; 34016352e4d2SDavid Hildenbrand } 34026352e4d2SDavid Hildenbrand 3403e83dff5eSChristoffer Dall vcpu_put(vcpu); 34046352e4d2SDavid Hildenbrand return rc; 340562d9f0dbSMarcelo Tosatti } 340662d9f0dbSMarcelo Tosatti 34078ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 34088ad35755SDavid Hildenbrand { 34098d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 34108ad35755SDavid Hildenbrand } 34118ad35755SDavid Hildenbrand 34122c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 34132c70fe44SChristian Borntraeger { 34148ad35755SDavid Hildenbrand retry: 34158e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 34162fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3417586b7ccdSChristian Borntraeger return 0; 34182c70fe44SChristian Borntraeger /* 34192c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3420b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 34212c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 34222c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 34232c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 34242c70fe44SChristian Borntraeger */ 34258ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 34262c70fe44SChristian Borntraeger int rc; 3427b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3428fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3429b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3430aca411a4SJulius Niedworok if (rc) { 3431aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 34322c70fe44SChristian Borntraeger return rc; 3433aca411a4SJulius Niedworok } 34348ad35755SDavid Hildenbrand goto retry; 34352c70fe44SChristian Borntraeger } 34368ad35755SDavid Hildenbrand 3437d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3438d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3439d3d692c8SDavid Hildenbrand goto retry; 3440d3d692c8SDavid Hildenbrand } 3441d3d692c8SDavid Hildenbrand 34428ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 34438ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 34448ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3445ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 34468ad35755SDavid Hildenbrand } 34478ad35755SDavid Hildenbrand goto retry; 34488ad35755SDavid Hildenbrand } 34498ad35755SDavid Hildenbrand 34508ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 34518ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 34528ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 34539daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 34548ad35755SDavid Hildenbrand } 34558ad35755SDavid Hildenbrand goto retry; 34568ad35755SDavid Hildenbrand } 34578ad35755SDavid Hildenbrand 34586502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 34596502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 34606502a34cSDavid Hildenbrand goto retry; 34616502a34cSDavid Hildenbrand } 34626502a34cSDavid Hildenbrand 3463190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3464190df4a2SClaudio Imbrenda /* 3465c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3466190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3467190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3468190df4a2SClaudio Imbrenda */ 3469190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3470190df4a2SClaudio Imbrenda goto retry; 3471190df4a2SClaudio Imbrenda } 3472190df4a2SClaudio Imbrenda 3473190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3474190df4a2SClaudio Imbrenda /* 3475c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3476c9f0a2b8SJanosch Frank * CMM has been used. 3477190df4a2SClaudio Imbrenda */ 3478190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3479c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3480190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3481190df4a2SClaudio Imbrenda goto retry; 3482190df4a2SClaudio Imbrenda } 3483190df4a2SClaudio Imbrenda 34840759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 348572875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 34863194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 34873194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 34880759d068SDavid Hildenbrand 34892c70fe44SChristian Borntraeger return 0; 34902c70fe44SChristian Borntraeger } 34912c70fe44SChristian Borntraeger 34920e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, 34938fa1696eSCollin L. Walling const struct kvm_s390_vm_tod_clock *gtod) 34948fa1696eSCollin L. Walling { 34958fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 34968fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 34978fa1696eSCollin L. Walling int i; 34988fa1696eSCollin L. Walling 34998fa1696eSCollin L. Walling mutex_lock(&kvm->lock); 35008fa1696eSCollin L. Walling preempt_disable(); 35018fa1696eSCollin L. Walling 35028fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 35038fa1696eSCollin L. Walling 35048fa1696eSCollin L. Walling kvm->arch.epoch = gtod->tod - htod.tod; 35050e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 35060e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 35078fa1696eSCollin L. Walling kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx; 35088fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 35098fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 35100e7def5fSDavid Hildenbrand } 35118fa1696eSCollin L. Walling 35128fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 35138fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 35148fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 35158fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 35168fa1696eSCollin L. Walling } 35178fa1696eSCollin L. Walling 35188fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 35198fa1696eSCollin L. Walling preempt_enable(); 35208fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 35218fa1696eSCollin L. Walling } 35228fa1696eSCollin L. Walling 3523fa576c58SThomas Huth /** 3524fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 3525fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 3526fa576c58SThomas Huth * @gpa: Guest physical address 3527fa576c58SThomas Huth * @writable: Whether the page should be writable or not 3528fa576c58SThomas Huth * 3529fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 3530fa576c58SThomas Huth * 3531fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 3532fa576c58SThomas Huth */ 3533fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 353424eb3a82SDominik Dingel { 3535527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 3536527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 353724eb3a82SDominik Dingel } 353824eb3a82SDominik Dingel 35393c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 35403c038e6bSDominik Dingel unsigned long token) 35413c038e6bSDominik Dingel { 35423c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 3543383d0b05SJens Freimann struct kvm_s390_irq irq; 35443c038e6bSDominik Dingel 35453c038e6bSDominik Dingel if (start_token) { 3546383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 3547383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 3548383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 35493c038e6bSDominik Dingel } else { 35503c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 3551383d0b05SJens Freimann inti.parm64 = token; 35523c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 35533c038e6bSDominik Dingel } 35543c038e6bSDominik Dingel } 35553c038e6bSDominik Dingel 35563c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 35573c038e6bSDominik Dingel struct kvm_async_pf *work) 35583c038e6bSDominik Dingel { 35593c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 35603c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 35613c038e6bSDominik Dingel } 35623c038e6bSDominik Dingel 35633c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 35643c038e6bSDominik Dingel struct kvm_async_pf *work) 35653c038e6bSDominik Dingel { 35663c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 35673c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 35683c038e6bSDominik Dingel } 35693c038e6bSDominik Dingel 35703c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 35713c038e6bSDominik Dingel struct kvm_async_pf *work) 35723c038e6bSDominik Dingel { 35733c038e6bSDominik Dingel /* s390 will always inject the page directly */ 35743c038e6bSDominik Dingel } 35753c038e6bSDominik Dingel 35763c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 35773c038e6bSDominik Dingel { 35783c038e6bSDominik Dingel /* 35793c038e6bSDominik Dingel * s390 will always inject the page directly, 35803c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 35813c038e6bSDominik Dingel */ 35823c038e6bSDominik Dingel return true; 35833c038e6bSDominik Dingel } 35843c038e6bSDominik Dingel 35853c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 35863c038e6bSDominik Dingel { 35873c038e6bSDominik Dingel hva_t hva; 35883c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 35893c038e6bSDominik Dingel int rc; 35903c038e6bSDominik Dingel 35913c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 35923c038e6bSDominik Dingel return 0; 35933c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 35943c038e6bSDominik Dingel vcpu->arch.pfault_compare) 35953c038e6bSDominik Dingel return 0; 35963c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 35973c038e6bSDominik Dingel return 0; 35989a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 35993c038e6bSDominik Dingel return 0; 3600b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 36013c038e6bSDominik Dingel return 0; 36023c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 36033c038e6bSDominik Dingel return 0; 36043c038e6bSDominik Dingel 360581480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 360681480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 360781480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 36083c038e6bSDominik Dingel return 0; 36093c038e6bSDominik Dingel 36103c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 36113c038e6bSDominik Dingel return rc; 36123c038e6bSDominik Dingel } 36133c038e6bSDominik Dingel 36143fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 3615b0c632dbSHeiko Carstens { 36163fb4c40fSThomas Huth int rc, cpuflags; 3617e168bf8dSCarsten Otte 36183c038e6bSDominik Dingel /* 36193c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 36203c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 36213c038e6bSDominik Dingel * handled outside the worker. 36223c038e6bSDominik Dingel */ 36233c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 36243c038e6bSDominik Dingel 36257ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 36267ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 3627b0c632dbSHeiko Carstens 3628b0c632dbSHeiko Carstens if (need_resched()) 3629b0c632dbSHeiko Carstens schedule(); 3630b0c632dbSHeiko Carstens 3631d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 363271cde587SChristian Borntraeger s390_handle_mcck(); 363371cde587SChristian Borntraeger 363479395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 363579395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 363679395031SJens Freimann if (rc) 363779395031SJens Freimann return rc; 363879395031SJens Freimann } 36390ff31867SCarsten Otte 36402c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 36412c70fe44SChristian Borntraeger if (rc) 36422c70fe44SChristian Borntraeger return rc; 36432c70fe44SChristian Borntraeger 364427291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 364527291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 364627291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 364727291e21SDavid Hildenbrand } 364827291e21SDavid Hildenbrand 36499f30f621SMichael Mueller clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask); 36509f30f621SMichael Mueller 3651b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 36523fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 36533fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 36543fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 36552b29a9fdSDominik Dingel 36563fb4c40fSThomas Huth return 0; 36573fb4c40fSThomas Huth } 36583fb4c40fSThomas Huth 3659492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 3660492d8642SThomas Huth { 366156317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 366256317920SDavid Hildenbrand .code = PGM_ADDRESSING, 366356317920SDavid Hildenbrand }; 366456317920SDavid Hildenbrand u8 opcode, ilen; 3665492d8642SThomas Huth int rc; 3666492d8642SThomas Huth 3667492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 3668492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 3669492d8642SThomas Huth 3670492d8642SThomas Huth /* 3671492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 3672492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 3673492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 3674492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 3675492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 3676492d8642SThomas Huth * to be able to forward the PSW. 3677492d8642SThomas Huth */ 36783fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 367956317920SDavid Hildenbrand ilen = insn_length(opcode); 36809b0d721aSDavid Hildenbrand if (rc < 0) { 36819b0d721aSDavid Hildenbrand return rc; 36829b0d721aSDavid Hildenbrand } else if (rc) { 36839b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 36849b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 36859b0d721aSDavid Hildenbrand * nullification if necessary. 36869b0d721aSDavid Hildenbrand */ 36879b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 36889b0d721aSDavid Hildenbrand ilen = 4; 36899b0d721aSDavid Hildenbrand } 369056317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 369156317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 369256317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 3693492d8642SThomas Huth } 3694492d8642SThomas Huth 36953fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 36963fb4c40fSThomas Huth { 36974d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 36984d62fcc0SQingFeng Hao struct sie_page *sie_page; 36994d62fcc0SQingFeng Hao 37002b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 37012b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 37022b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 37032b29a9fdSDominik Dingel 370427291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 370527291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 370627291e21SDavid Hildenbrand 37077ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 37087ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 370971f116bfSDavid Hildenbrand 37104d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 37114d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 37124d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 37134d62fcc0SQingFeng Hao struct sie_page, sie_block); 37144d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 37154d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 37164d62fcc0SQingFeng Hao return 0; 37174d62fcc0SQingFeng Hao } 37184d62fcc0SQingFeng Hao 371971f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 372071f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 372171f116bfSDavid Hildenbrand 372271f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 372371f116bfSDavid Hildenbrand return rc; 372471f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 372571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 372671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 372771f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 372871f116bfSDavid Hildenbrand return -EREMOTE; 372971f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 373071f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 373171f116bfSDavid Hildenbrand return 0; 3732210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 3733210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 3734210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 3735210b1607SThomas Huth current->thread.gmap_addr; 3736210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 373771f116bfSDavid Hildenbrand return -EREMOTE; 373824eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 37393c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 374024eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 374171f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 374271f116bfSDavid Hildenbrand return 0; 374371f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 3744fa576c58SThomas Huth } 374571f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 37463fb4c40fSThomas Huth } 37473fb4c40fSThomas Huth 37483fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 37493fb4c40fSThomas Huth { 37503fb4c40fSThomas Huth int rc, exit_reason; 37513fb4c40fSThomas Huth 3752800c1065SThomas Huth /* 3753800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 3754800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 3755800c1065SThomas Huth */ 3756800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 3757800c1065SThomas Huth 3758a76ccff6SThomas Huth do { 37593fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 37603fb4c40fSThomas Huth if (rc) 3761a76ccff6SThomas Huth break; 37623fb4c40fSThomas Huth 3763800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 37643fb4c40fSThomas Huth /* 3765a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 3766a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 37673fb4c40fSThomas Huth */ 37680097d12eSChristian Borntraeger local_irq_disable(); 37696edaa530SPaolo Bonzini guest_enter_irqoff(); 3770db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 37710097d12eSChristian Borntraeger local_irq_enable(); 3772a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 3773a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 37740097d12eSChristian Borntraeger local_irq_disable(); 3775db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 37766edaa530SPaolo Bonzini guest_exit_irqoff(); 37770097d12eSChristian Borntraeger local_irq_enable(); 3778800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 37793fb4c40fSThomas Huth 37803fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 378127291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 37823fb4c40fSThomas Huth 3783800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 3784e168bf8dSCarsten Otte return rc; 3785b0c632dbSHeiko Carstens } 3786b0c632dbSHeiko Carstens 3787b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3788b028ee3eSDavid Hildenbrand { 37894d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 37904e0b1ab7SFan Zhang struct gs_cb *gscb; 37914d5f2c04SChristian Borntraeger 37924d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 37934e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 3794b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 3795b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 3796b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 3797b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 3798b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 3799b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 3800d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 3801d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 3802b028ee3eSDavid Hildenbrand } 3803b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 38044287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 3805b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 3806b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 3807b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 3808b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 3809b028ee3eSDavid Hildenbrand } 3810b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 3811b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 3812b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 3813b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 38149fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 38159fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3816b028ee3eSDavid Hildenbrand } 381780cd8763SFan Zhang /* 381880cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 381980cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 382080cd8763SFan Zhang */ 382180cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 38224d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 3823bb59c2daSAlice Frosi riccb->v && 38240c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 38254d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 38260c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 382780cd8763SFan Zhang } 38284e0b1ab7SFan Zhang /* 38294e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 38304e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 38314e0b1ab7SFan Zhang */ 38324e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 38334e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 38344e0b1ab7SFan Zhang gscb->gssm && 38354e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 38364e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 38374e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 38384e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 38394e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 384080cd8763SFan Zhang } 384135b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 384235b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 384335b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 384435b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 384535b3fde6SChristian Borntraeger } 384631d8b8d4SChristian Borntraeger save_access_regs(vcpu->arch.host_acrs); 384731d8b8d4SChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 3848e1788bb9SChristian Borntraeger /* save host (userspace) fprs/vrs */ 3849e1788bb9SChristian Borntraeger save_fpu_regs(); 3850e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 3851e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 3852e1788bb9SChristian Borntraeger if (MACHINE_HAS_VX) 3853e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.vrs; 3854e1788bb9SChristian Borntraeger else 3855e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.fprs; 3856e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 3857e1788bb9SChristian Borntraeger if (test_fp_ctl(current->thread.fpu.fpc)) 3858e1788bb9SChristian Borntraeger /* User space provided an invalid FPC, let's clear it */ 3859e1788bb9SChristian Borntraeger current->thread.fpu.fpc = 0; 38604e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 38614e0b1ab7SFan Zhang preempt_disable(); 38624e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 38634e0b1ab7SFan Zhang if (current->thread.gs_cb) { 38644e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 38654e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 38664e0b1ab7SFan Zhang } 38674e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 38684e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 38694e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 38704e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 38714e0b1ab7SFan Zhang } 38724e0b1ab7SFan Zhang preempt_enable(); 38734e0b1ab7SFan Zhang } 3874a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 387580cd8763SFan Zhang 3876b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 3877b028ee3eSDavid Hildenbrand } 3878b028ee3eSDavid Hildenbrand 3879b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3880b028ee3eSDavid Hildenbrand { 3881b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 3882b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 3883b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 3884b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 38854287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 3886b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 3887b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 3888b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 3889b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 3890b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 3891b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 3892b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 389335b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 389431d8b8d4SChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 389531d8b8d4SChristian Borntraeger restore_access_regs(vcpu->arch.host_acrs); 3896e1788bb9SChristian Borntraeger /* Save guest register state */ 3897e1788bb9SChristian Borntraeger save_fpu_regs(); 3898e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 3899e1788bb9SChristian Borntraeger /* Restore will be done lazily at return */ 3900e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 3901e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 39024e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 39034e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 39044e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 39054e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 39064e0b1ab7SFan Zhang preempt_disable(); 39074e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 39084e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 39094e0b1ab7SFan Zhang preempt_enable(); 39104e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 39114e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 39124e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 39134e0b1ab7SFan Zhang } 3914a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 3915b028ee3eSDavid Hildenbrand } 3916b028ee3eSDavid Hildenbrand 3917b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3918b0c632dbSHeiko Carstens { 39198f2abe6aSChristian Borntraeger int rc; 3920b0c632dbSHeiko Carstens 3921460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 3922460df4c1SPaolo Bonzini return -EINTR; 3923460df4c1SPaolo Bonzini 3924accb757dSChristoffer Dall vcpu_load(vcpu); 3925accb757dSChristoffer Dall 392627291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 392727291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 3928accb757dSChristoffer Dall rc = 0; 3929accb757dSChristoffer Dall goto out; 393027291e21SDavid Hildenbrand } 393127291e21SDavid Hildenbrand 393220b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 3933b0c632dbSHeiko Carstens 39346352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 39356852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 39366352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 3937ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 39386352e4d2SDavid Hildenbrand vcpu->vcpu_id); 3939accb757dSChristoffer Dall rc = -EINVAL; 3940accb757dSChristoffer Dall goto out; 39416352e4d2SDavid Hildenbrand } 3942b0c632dbSHeiko Carstens 3943b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 3944db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 3945d7b0b5ebSCarsten Otte 3946dab4079dSHeiko Carstens might_fault(); 3947e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 39489ace903dSChristian Ehrhardt 3949b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 3950b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 39518f2abe6aSChristian Borntraeger rc = -EINTR; 3952b1d16c49SChristian Ehrhardt } 39538f2abe6aSChristian Borntraeger 395427291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 395527291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 395627291e21SDavid Hildenbrand rc = 0; 395727291e21SDavid Hildenbrand } 395827291e21SDavid Hildenbrand 39598f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 396071f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 39618f2abe6aSChristian Borntraeger rc = 0; 39628f2abe6aSChristian Borntraeger } 39638f2abe6aSChristian Borntraeger 3964db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 3965b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 3966d7b0b5ebSCarsten Otte 396720b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 3968b0c632dbSHeiko Carstens 3969b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 3970accb757dSChristoffer Dall out: 3971accb757dSChristoffer Dall vcpu_put(vcpu); 39727e8e6ab4SHeiko Carstens return rc; 3973b0c632dbSHeiko Carstens } 3974b0c632dbSHeiko Carstens 3975b0c632dbSHeiko Carstens /* 3976b0c632dbSHeiko Carstens * store status at address 3977b0c632dbSHeiko Carstens * we use have two special cases: 3978b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 3979b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 3980b0c632dbSHeiko Carstens */ 3981d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 3982b0c632dbSHeiko Carstens { 3983092670cdSCarsten Otte unsigned char archmode = 1; 39849abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 3985fda902cbSMichael Mueller unsigned int px; 39864287f247SDavid Hildenbrand u64 clkcomp, cputm; 3987d0bce605SHeiko Carstens int rc; 3988b0c632dbSHeiko Carstens 3989d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 3990d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 3991d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 3992b0c632dbSHeiko Carstens return -EFAULT; 3993d9a3a09aSMartin Schwidefsky gpa = 0; 3994d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 3995d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 3996b0c632dbSHeiko Carstens return -EFAULT; 3997d9a3a09aSMartin Schwidefsky gpa = px; 3998d9a3a09aSMartin Schwidefsky } else 3999d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 40009abc2a08SDavid Hildenbrand 40019abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 40029abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 40039522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4004d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 40059abc2a08SDavid Hildenbrand fprs, 128); 40069abc2a08SDavid Hildenbrand } else { 40079abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 40086fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 40099abc2a08SDavid Hildenbrand } 4010d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4011d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4012d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4013d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4014d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4015fda902cbSMichael Mueller &px, 4); 4016d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 40179abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4018d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4019d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 40204287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4021d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 40224287f247SDavid Hildenbrand &cputm, 8); 4023178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4024d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4025d0bce605SHeiko Carstens &clkcomp, 8); 4026d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4027d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4028d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4029d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4030d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4031b0c632dbSHeiko Carstens } 4032b0c632dbSHeiko Carstens 4033e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4034e879892cSThomas Huth { 4035e879892cSThomas Huth /* 4036e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 403731d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4038e879892cSThomas Huth * it into the save area 4039e879892cSThomas Huth */ 4040d0164ee2SHendrik Brueckner save_fpu_regs(); 40419abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4042e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4043e879892cSThomas Huth 4044e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4045e879892cSThomas Huth } 4046e879892cSThomas Huth 40478ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 40488ad35755SDavid Hildenbrand { 40498ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 40508e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 40518ad35755SDavid Hildenbrand } 40528ad35755SDavid Hildenbrand 40538ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 40548ad35755SDavid Hildenbrand { 40558ad35755SDavid Hildenbrand unsigned int i; 40568ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 40578ad35755SDavid Hildenbrand 40588ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 40598ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 40608ad35755SDavid Hildenbrand } 40618ad35755SDavid Hildenbrand } 40628ad35755SDavid Hildenbrand 40638ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 40648ad35755SDavid Hildenbrand { 406509a400e7SDavid Hildenbrand if (!sclp.has_ibs) 406609a400e7SDavid Hildenbrand return; 40678ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 40688e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 40698ad35755SDavid Hildenbrand } 40708ad35755SDavid Hildenbrand 40716852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 40726852d7b6SDavid Hildenbrand { 40738ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 40748ad35755SDavid Hildenbrand 40758ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 40768ad35755SDavid Hildenbrand return; 40778ad35755SDavid Hildenbrand 40786852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 40798ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4080433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 40818ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 40828ad35755SDavid Hildenbrand 40838ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 40848ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 40858ad35755SDavid Hildenbrand started_vcpus++; 40868ad35755SDavid Hildenbrand } 40878ad35755SDavid Hildenbrand 40888ad35755SDavid Hildenbrand if (started_vcpus == 0) { 40898ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 40908ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 40918ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 40928ad35755SDavid Hildenbrand /* 40938ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 40948ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 40958ad35755SDavid Hildenbrand * oustanding ENABLE requests. 40968ad35755SDavid Hildenbrand */ 40978ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 40988ad35755SDavid Hildenbrand } 40998ad35755SDavid Hildenbrand 41009daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 41018ad35755SDavid Hildenbrand /* 41028ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 41038ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 41048ad35755SDavid Hildenbrand */ 4105d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4106433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 41078ad35755SDavid Hildenbrand return; 41086852d7b6SDavid Hildenbrand } 41096852d7b6SDavid Hildenbrand 41106852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 41116852d7b6SDavid Hildenbrand { 41128ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 41138ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 41148ad35755SDavid Hildenbrand 41158ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 41168ad35755SDavid Hildenbrand return; 41178ad35755SDavid Hildenbrand 41186852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 41198ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4120433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 41218ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 41228ad35755SDavid Hildenbrand 412332f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 41246cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 412532f5ff63SDavid Hildenbrand 4126ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 41278ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 41288ad35755SDavid Hildenbrand 41298ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 41308ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 41318ad35755SDavid Hildenbrand started_vcpus++; 41328ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 41338ad35755SDavid Hildenbrand } 41348ad35755SDavid Hildenbrand } 41358ad35755SDavid Hildenbrand 41368ad35755SDavid Hildenbrand if (started_vcpus == 1) { 41378ad35755SDavid Hildenbrand /* 41388ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 41398ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 41408ad35755SDavid Hildenbrand */ 41418ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 41428ad35755SDavid Hildenbrand } 41438ad35755SDavid Hildenbrand 4144433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 41458ad35755SDavid Hildenbrand return; 41466852d7b6SDavid Hildenbrand } 41476852d7b6SDavid Hildenbrand 4148d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4149d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4150d6712df9SCornelia Huck { 4151d6712df9SCornelia Huck int r; 4152d6712df9SCornelia Huck 4153d6712df9SCornelia Huck if (cap->flags) 4154d6712df9SCornelia Huck return -EINVAL; 4155d6712df9SCornelia Huck 4156d6712df9SCornelia Huck switch (cap->cap) { 4157fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4158fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4159fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4160c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4161fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4162fa6b7fe9SCornelia Huck } 4163fa6b7fe9SCornelia Huck r = 0; 4164fa6b7fe9SCornelia Huck break; 4165d6712df9SCornelia Huck default: 4166d6712df9SCornelia Huck r = -EINVAL; 4167d6712df9SCornelia Huck break; 4168d6712df9SCornelia Huck } 4169d6712df9SCornelia Huck return r; 4170d6712df9SCornelia Huck } 4171d6712df9SCornelia Huck 417241408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 417341408c28SThomas Huth struct kvm_s390_mem_op *mop) 417441408c28SThomas Huth { 417541408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 417641408c28SThomas Huth void *tmpbuf = NULL; 417741408c28SThomas Huth int r, srcu_idx; 417841408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 417941408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 418041408c28SThomas Huth 418141408c28SThomas Huth if (mop->flags & ~supported_flags) 418241408c28SThomas Huth return -EINVAL; 418341408c28SThomas Huth 418441408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 418541408c28SThomas Huth return -E2BIG; 418641408c28SThomas Huth 418741408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 418841408c28SThomas Huth tmpbuf = vmalloc(mop->size); 418941408c28SThomas Huth if (!tmpbuf) 419041408c28SThomas Huth return -ENOMEM; 419141408c28SThomas Huth } 419241408c28SThomas Huth 419341408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 419441408c28SThomas Huth 419541408c28SThomas Huth switch (mop->op) { 419641408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 419741408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 419892c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 419992c96321SDavid Hildenbrand mop->size, GACC_FETCH); 420041408c28SThomas Huth break; 420141408c28SThomas Huth } 420241408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 420341408c28SThomas Huth if (r == 0) { 420441408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 420541408c28SThomas Huth r = -EFAULT; 420641408c28SThomas Huth } 420741408c28SThomas Huth break; 420841408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 420941408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 421092c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 421192c96321SDavid Hildenbrand mop->size, GACC_STORE); 421241408c28SThomas Huth break; 421341408c28SThomas Huth } 421441408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 421541408c28SThomas Huth r = -EFAULT; 421641408c28SThomas Huth break; 421741408c28SThomas Huth } 421841408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 421941408c28SThomas Huth break; 422041408c28SThomas Huth default: 422141408c28SThomas Huth r = -EINVAL; 422241408c28SThomas Huth } 422341408c28SThomas Huth 422441408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 422541408c28SThomas Huth 422641408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 422741408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 422841408c28SThomas Huth 422941408c28SThomas Huth vfree(tmpbuf); 423041408c28SThomas Huth return r; 423141408c28SThomas Huth } 423241408c28SThomas Huth 42335cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4234b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4235b0c632dbSHeiko Carstens { 4236b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4237b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4238b0c632dbSHeiko Carstens 423993736624SAvi Kivity switch (ioctl) { 424047b43c52SJens Freimann case KVM_S390_IRQ: { 424147b43c52SJens Freimann struct kvm_s390_irq s390irq; 424247b43c52SJens Freimann 424347b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 42449b062471SChristoffer Dall return -EFAULT; 42459b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 424647b43c52SJens Freimann } 424793736624SAvi Kivity case KVM_S390_INTERRUPT: { 4248ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 4249383d0b05SJens Freimann struct kvm_s390_irq s390irq; 4250ba5c1e9bSCarsten Otte 4251ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 42529b062471SChristoffer Dall return -EFAULT; 4253383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4254383d0b05SJens Freimann return -EINVAL; 42559b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4256ba5c1e9bSCarsten Otte } 42579b062471SChristoffer Dall } 42585cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 42595cb0944cSPaolo Bonzini } 42605cb0944cSPaolo Bonzini 42615cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 42625cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 42635cb0944cSPaolo Bonzini { 42645cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 42655cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 42665cb0944cSPaolo Bonzini int idx; 42675cb0944cSPaolo Bonzini long r; 42689b062471SChristoffer Dall 42699b062471SChristoffer Dall vcpu_load(vcpu); 42709b062471SChristoffer Dall 42719b062471SChristoffer Dall switch (ioctl) { 4272b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4273800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 4274bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 4275800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4276bc923cc9SAvi Kivity break; 4277b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4278b0c632dbSHeiko Carstens psw_t psw; 4279b0c632dbSHeiko Carstens 4280bc923cc9SAvi Kivity r = -EFAULT; 4281b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4282bc923cc9SAvi Kivity break; 4283bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4284bc923cc9SAvi Kivity break; 4285b0c632dbSHeiko Carstens } 4286b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 4287bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 4288bc923cc9SAvi Kivity break; 428914eebd91SCarsten Otte case KVM_SET_ONE_REG: 429014eebd91SCarsten Otte case KVM_GET_ONE_REG: { 429114eebd91SCarsten Otte struct kvm_one_reg reg; 429214eebd91SCarsten Otte r = -EFAULT; 429314eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 429414eebd91SCarsten Otte break; 429514eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 429614eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 429714eebd91SCarsten Otte else 429814eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 429914eebd91SCarsten Otte break; 430014eebd91SCarsten Otte } 430127e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 430227e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 430327e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 430427e0393fSCarsten Otte 430527e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 430627e0393fSCarsten Otte r = -EFAULT; 430727e0393fSCarsten Otte break; 430827e0393fSCarsten Otte } 430927e0393fSCarsten Otte 431027e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 431127e0393fSCarsten Otte r = -EINVAL; 431227e0393fSCarsten Otte break; 431327e0393fSCarsten Otte } 431427e0393fSCarsten Otte 431527e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 431627e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 431727e0393fSCarsten Otte break; 431827e0393fSCarsten Otte } 431927e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 432027e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 432127e0393fSCarsten Otte 432227e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 432327e0393fSCarsten Otte r = -EFAULT; 432427e0393fSCarsten Otte break; 432527e0393fSCarsten Otte } 432627e0393fSCarsten Otte 432727e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 432827e0393fSCarsten Otte r = -EINVAL; 432927e0393fSCarsten Otte break; 433027e0393fSCarsten Otte } 433127e0393fSCarsten Otte 433227e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 433327e0393fSCarsten Otte ucasmap.length); 433427e0393fSCarsten Otte break; 433527e0393fSCarsten Otte } 433627e0393fSCarsten Otte #endif 4337ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 4338527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 4339ccc7910fSCarsten Otte break; 4340ccc7910fSCarsten Otte } 4341d6712df9SCornelia Huck case KVM_ENABLE_CAP: 4342d6712df9SCornelia Huck { 4343d6712df9SCornelia Huck struct kvm_enable_cap cap; 4344d6712df9SCornelia Huck r = -EFAULT; 4345d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 4346d6712df9SCornelia Huck break; 4347d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 4348d6712df9SCornelia Huck break; 4349d6712df9SCornelia Huck } 435041408c28SThomas Huth case KVM_S390_MEM_OP: { 435141408c28SThomas Huth struct kvm_s390_mem_op mem_op; 435241408c28SThomas Huth 435341408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 435441408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 435541408c28SThomas Huth else 435641408c28SThomas Huth r = -EFAULT; 435741408c28SThomas Huth break; 435841408c28SThomas Huth } 4359816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 4360816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4361816c7667SJens Freimann 4362816c7667SJens Freimann r = -EFAULT; 4363816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4364816c7667SJens Freimann break; 4365816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 4366816c7667SJens Freimann irq_state.len == 0 || 4367816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 4368816c7667SJens Freimann r = -EINVAL; 4369816c7667SJens Freimann break; 4370816c7667SJens Freimann } 4371bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4372816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 4373816c7667SJens Freimann (void __user *) irq_state.buf, 4374816c7667SJens Freimann irq_state.len); 4375816c7667SJens Freimann break; 4376816c7667SJens Freimann } 4377816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 4378816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4379816c7667SJens Freimann 4380816c7667SJens Freimann r = -EFAULT; 4381816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4382816c7667SJens Freimann break; 4383816c7667SJens Freimann if (irq_state.len == 0) { 4384816c7667SJens Freimann r = -EINVAL; 4385816c7667SJens Freimann break; 4386816c7667SJens Freimann } 4387bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4388816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 4389816c7667SJens Freimann (__u8 __user *) irq_state.buf, 4390816c7667SJens Freimann irq_state.len); 4391816c7667SJens Freimann break; 4392816c7667SJens Freimann } 4393b0c632dbSHeiko Carstens default: 43943e6afcf1SCarsten Otte r = -ENOTTY; 4395b0c632dbSHeiko Carstens } 43969b062471SChristoffer Dall 43979b062471SChristoffer Dall vcpu_put(vcpu); 4398bc923cc9SAvi Kivity return r; 4399b0c632dbSHeiko Carstens } 4400b0c632dbSHeiko Carstens 44011499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 44025b1c1493SCarsten Otte { 44035b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 44045b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 44055b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 44065b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 44075b1c1493SCarsten Otte get_page(vmf->page); 44085b1c1493SCarsten Otte return 0; 44095b1c1493SCarsten Otte } 44105b1c1493SCarsten Otte #endif 44115b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 44125b1c1493SCarsten Otte } 44135b1c1493SCarsten Otte 44145587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 44155587027cSAneesh Kumar K.V unsigned long npages) 4416db3fe4ebSTakuya Yoshikawa { 4417db3fe4ebSTakuya Yoshikawa return 0; 4418db3fe4ebSTakuya Yoshikawa } 4419db3fe4ebSTakuya Yoshikawa 4420b0c632dbSHeiko Carstens /* Section: memory related */ 4421f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 4422f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 442309170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 44247b6195a9STakuya Yoshikawa enum kvm_mr_change change) 4425b0c632dbSHeiko Carstens { 4426dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 4427dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 4428dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 4429dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 4430b0c632dbSHeiko Carstens 4431598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 4432b0c632dbSHeiko Carstens return -EINVAL; 4433b0c632dbSHeiko Carstens 4434598841caSCarsten Otte if (mem->memory_size & 0xffffful) 4435b0c632dbSHeiko Carstens return -EINVAL; 4436b0c632dbSHeiko Carstens 4437a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 4438a3a92c31SDominik Dingel return -EINVAL; 4439a3a92c31SDominik Dingel 4440f7784b8eSMarcelo Tosatti return 0; 4441f7784b8eSMarcelo Tosatti } 4442f7784b8eSMarcelo Tosatti 4443f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 444409170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 44458482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 4446f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 44478482644aSTakuya Yoshikawa enum kvm_mr_change change) 4448f7784b8eSMarcelo Tosatti { 4449f7850c92SCarsten Otte int rc; 4450f7784b8eSMarcelo Tosatti 44512cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 44522cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 44532cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 44542cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 44552cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 44562cef4debSChristian Borntraeger */ 44572cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 44582cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 44592cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 44602cef4debSChristian Borntraeger return; 4461598841caSCarsten Otte 4462598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 4463598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 4464598841caSCarsten Otte if (rc) 4465ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 4466598841caSCarsten Otte return; 4467b0c632dbSHeiko Carstens } 4468b0c632dbSHeiko Carstens 446960a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 447060a37709SAlexander Yarygin { 447160a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 447260a37709SAlexander Yarygin 447360a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 447460a37709SAlexander Yarygin } 447560a37709SAlexander Yarygin 44763491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 44773491caf2SChristian Borntraeger { 44783491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 44793491caf2SChristian Borntraeger } 44803491caf2SChristian Borntraeger 4481b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 4482b0c632dbSHeiko Carstens { 448360a37709SAlexander Yarygin int i; 448460a37709SAlexander Yarygin 448507197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 44868d43d570SMichael Mueller pr_info("SIE is not available\n"); 448707197fd0SDavid Hildenbrand return -ENODEV; 448807197fd0SDavid Hildenbrand } 448907197fd0SDavid Hildenbrand 4490a4499382SJanosch Frank if (nested && hpage) { 44918d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 4492a4499382SJanosch Frank return -EINVAL; 4493a4499382SJanosch Frank } 4494a4499382SJanosch Frank 449560a37709SAlexander Yarygin for (i = 0; i < 16; i++) 4496c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 449760a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 449860a37709SAlexander Yarygin 44999d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 4500b0c632dbSHeiko Carstens } 4501b0c632dbSHeiko Carstens 4502b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 4503b0c632dbSHeiko Carstens { 4504b0c632dbSHeiko Carstens kvm_exit(); 4505b0c632dbSHeiko Carstens } 4506b0c632dbSHeiko Carstens 4507b0c632dbSHeiko Carstens module_init(kvm_s390_init); 4508b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 4509566af940SCornelia Huck 4510566af940SCornelia Huck /* 4511566af940SCornelia Huck * Enable autoloading of the kvm module. 4512566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 4513566af940SCornelia Huck * since x86 takes a different approach. 4514566af940SCornelia Huck */ 4515566af940SCornelia Huck #include <linux/miscdevice.h> 4516566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 4517566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 4518