1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2b0c632dbSHeiko Carstens /* 3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x) 4b0c632dbSHeiko Carstens * 53e6c5568SJanosch Frank * Copyright IBM Corp. 2008, 2020 6b0c632dbSHeiko Carstens * 7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 9b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 10628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1115f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 12b0c632dbSHeiko Carstens */ 13b0c632dbSHeiko Carstens 147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 167aedd9d4SMichael Mueller 17b0c632dbSHeiko Carstens #include <linux/compiler.h> 18b0c632dbSHeiko Carstens #include <linux/err.h> 19b0c632dbSHeiko Carstens #include <linux/fs.h> 20ca872302SChristian Borntraeger #include <linux/hrtimer.h> 21b0c632dbSHeiko Carstens #include <linux/init.h> 22b0c632dbSHeiko Carstens #include <linux/kvm.h> 23b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 24b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 25b0c632dbSHeiko Carstens #include <linux/module.h> 26d3217967SPaul Gortmaker #include <linux/moduleparam.h> 27a374e892STony Krowiak #include <linux/random.h> 28b0c632dbSHeiko Carstens #include <linux/slab.h> 29ba5c1e9bSCarsten Otte #include <linux/timer.h> 3041408c28SThomas Huth #include <linux/vmalloc.h> 3115c9705fSDavid Hildenbrand #include <linux/bitmap.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33190df4a2SClaudio Imbrenda #include <linux/string.h> 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> 4729b40f10SJanosch Frank #include <asm/uv.h> 488f2abe6aSChristian Borntraeger #include "kvm-s390.h" 49b0c632dbSHeiko Carstens #include "gaccess.h" 50b0c632dbSHeiko Carstens 515786fffaSCornelia Huck #define CREATE_TRACE_POINTS 525786fffaSCornelia Huck #include "trace.h" 53ade38c31SCornelia Huck #include "trace-s390.h" 545786fffaSCornelia Huck 5541408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 56816c7667SJens Freimann #define LOCAL_IRQS 32 57816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 58816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 5941408c28SThomas Huth 60b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 61ccc40c53SChristian Borntraeger #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM 62b0c632dbSHeiko Carstens 63b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 64b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 650eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 668f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 678f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 688f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 69a5e0aceaSChristian Borntraeger { "exit_io_request", VCPU_STAT(exit_io_request) }, 708f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 71ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 729ec6de19SAlexander Yarygin { "exit_pei", VCPU_STAT(exit_pei) }, 73ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 74ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 75a011eeb2SJanosch Frank { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, 76f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 7762bea5bfSPaolo Bonzini { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 783491caf2SChristian Borntraeger { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 798b905d28SChristian Borntraeger { "halt_no_poll_steal", VCPU_STAT(halt_no_poll_steal) }, 80ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 81f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 82ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 83aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 84aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 85ccc40c53SChristian Borntraeger { "deliver_ckc", VCPU_STAT(deliver_ckc) }, 86ccc40c53SChristian Borntraeger { "deliver_cputm", VCPU_STAT(deliver_cputm) }, 87ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 887697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 89ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 90ccc40c53SChristian Borntraeger { "deliver_virtio", VCPU_STAT(deliver_virtio) }, 91ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 92ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 93ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 94ccc40c53SChristian Borntraeger { "deliver_program", VCPU_STAT(deliver_program) }, 95ccc40c53SChristian Borntraeger { "deliver_io", VCPU_STAT(deliver_io) }, 9632de0749SQingFeng Hao { "deliver_machine_check", VCPU_STAT(deliver_machine_check) }, 97ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 98ccc40c53SChristian Borntraeger { "inject_ckc", VCPU_STAT(inject_ckc) }, 99ccc40c53SChristian Borntraeger { "inject_cputm", VCPU_STAT(inject_cputm) }, 100ccc40c53SChristian Borntraeger { "inject_external_call", VCPU_STAT(inject_external_call) }, 101ccc40c53SChristian Borntraeger { "inject_float_mchk", VM_STAT(inject_float_mchk) }, 102ccc40c53SChristian Borntraeger { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) }, 103ccc40c53SChristian Borntraeger { "inject_io", VM_STAT(inject_io) }, 104ccc40c53SChristian Borntraeger { "inject_mchk", VCPU_STAT(inject_mchk) }, 105ccc40c53SChristian Borntraeger { "inject_pfault_done", VM_STAT(inject_pfault_done) }, 106ccc40c53SChristian Borntraeger { "inject_program", VCPU_STAT(inject_program) }, 107ccc40c53SChristian Borntraeger { "inject_restart", VCPU_STAT(inject_restart) }, 108ccc40c53SChristian Borntraeger { "inject_service_signal", VM_STAT(inject_service_signal) }, 109ccc40c53SChristian Borntraeger { "inject_set_prefix", VCPU_STAT(inject_set_prefix) }, 110ccc40c53SChristian Borntraeger { "inject_stop_signal", VCPU_STAT(inject_stop_signal) }, 111ccc40c53SChristian Borntraeger { "inject_pfault_init", VCPU_STAT(inject_pfault_init) }, 112ccc40c53SChristian Borntraeger { "inject_virtio", VM_STAT(inject_virtio) }, 113a37cb07aSChristian Borntraeger { "instruction_epsw", VCPU_STAT(instruction_epsw) }, 114a37cb07aSChristian Borntraeger { "instruction_gs", VCPU_STAT(instruction_gs) }, 115a37cb07aSChristian Borntraeger { "instruction_io_other", VCPU_STAT(instruction_io_other) }, 116a37cb07aSChristian Borntraeger { "instruction_lpsw", VCPU_STAT(instruction_lpsw) }, 117a37cb07aSChristian Borntraeger { "instruction_lpswe", VCPU_STAT(instruction_lpswe) }, 11869d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 119a37cb07aSChristian Borntraeger { "instruction_ptff", VCPU_STAT(instruction_ptff) }, 120453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 121a37cb07aSChristian Borntraeger { "instruction_sck", VCPU_STAT(instruction_sck) }, 122a37cb07aSChristian Borntraeger { "instruction_sckpf", VCPU_STAT(instruction_sckpf) }, 123453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 124453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 125453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 126a37cb07aSChristian Borntraeger { "instruction_iske", VCPU_STAT(instruction_iske) }, 127a37cb07aSChristian Borntraeger { "instruction_ri", VCPU_STAT(instruction_ri) }, 128a37cb07aSChristian Borntraeger { "instruction_rrbe", VCPU_STAT(instruction_rrbe) }, 129a37cb07aSChristian Borntraeger { "instruction_sske", VCPU_STAT(instruction_sske) }, 1308a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 131b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 132453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 133453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 134a37cb07aSChristian Borntraeger { "instruction_tb", VCPU_STAT(instruction_tb) }, 135a37cb07aSChristian Borntraeger { "instruction_tpi", VCPU_STAT(instruction_tpi) }, 136bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 137a37cb07aSChristian Borntraeger { "instruction_tsch", VCPU_STAT(instruction_tsch) }, 13895ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 139a3508fbeSDavid Hildenbrand { "instruction_sie", VCPU_STAT(instruction_sie) }, 1405288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 141bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1427697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1435288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 14442cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 14542cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1465288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 14742cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 14842cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 149cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1505288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1515288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1525288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 15342cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 15442cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 15542cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 156866c138cSChristian Borntraeger { "instruction_diag_10", VCPU_STAT(diagnose_10) }, 157866c138cSChristian Borntraeger { "instruction_diag_44", VCPU_STAT(diagnose_44) }, 158866c138cSChristian Borntraeger { "instruction_diag_9c", VCPU_STAT(diagnose_9c) }, 1598474e5caSChristian Borntraeger { "diag_9c_ignored", VCPU_STAT(diagnose_9c_ignored) }, 160866c138cSChristian Borntraeger { "instruction_diag_258", VCPU_STAT(diagnose_258) }, 161866c138cSChristian Borntraeger { "instruction_diag_308", VCPU_STAT(diagnose_308) }, 162866c138cSChristian Borntraeger { "instruction_diag_500", VCPU_STAT(diagnose_500) }, 163a37cb07aSChristian Borntraeger { "instruction_diag_other", VCPU_STAT(diagnose_other) }, 164b0c632dbSHeiko Carstens { NULL } 165b0c632dbSHeiko Carstens }; 166b0c632dbSHeiko Carstens 1678fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext { 1688fa1696eSCollin L. Walling __u8 epoch_idx; 1698fa1696eSCollin L. Walling __u64 tod; 1708fa1696eSCollin L. Walling __u8 reserved[7]; 1718fa1696eSCollin L. Walling } __packed; 1728fa1696eSCollin L. Walling 173a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 174a411edf1SDavid Hildenbrand static int nested; 175a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 176a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 177a411edf1SDavid Hildenbrand 178a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 179a4499382SJanosch Frank static int hpage; 180a4499382SJanosch Frank module_param(hpage, int, 0444); 181a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 182b0c632dbSHeiko Carstens 1838b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */ 1848b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10; 1858b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644); 186b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); 1878b905d28SChristian Borntraeger 188c3b9e3e1SChristian Borntraeger /* 189c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 190c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 191c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 192c3b9e3e1SChristian Borntraeger */ 193c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 194c3b9e3e1SChristian Borntraeger 195c3b9e3e1SChristian Borntraeger /* 196c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 197c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 198c3b9e3e1SChristian Borntraeger */ 199c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 200c3b9e3e1SChristian Borntraeger /* 201c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 202c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 203c3b9e3e1SChristian Borntraeger */ 204c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 205c3b9e3e1SChristian Borntraeger 206c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 20778c4b59fSMichael Mueller { 208c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 209c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 210c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 211c3b9e3e1SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 212c3b9e3e1SChristian Borntraeger 213c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 21478c4b59fSMichael Mueller } 21578c4b59fSMichael Mueller 21615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 21715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2180a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2190a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 22015c9705fSDavid Hildenbrand 2219d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 222a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 22378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2243e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv; 2259d8d5786SMichael Mueller 226b0c632dbSHeiko Carstens /* Section: not file related */ 22713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 228b0c632dbSHeiko Carstens { 229b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 23010474ae8SAlexander Graf return 0; 231b0c632dbSHeiko Carstens } 232b0c632dbSHeiko Carstens 233f257d6dcSSean Christopherson int kvm_arch_check_processor_compat(void) 234f257d6dcSSean Christopherson { 235f257d6dcSSean Christopherson return 0; 236f257d6dcSSean Christopherson } 237f257d6dcSSean Christopherson 23829b40f10SJanosch Frank /* forward declarations */ 239414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 240414d3b07SMartin Schwidefsky unsigned long end); 24129b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm); 2422c70fe44SChristian Borntraeger 2431575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2441575767eSDavid Hildenbrand { 2451575767eSDavid Hildenbrand u8 delta_idx = 0; 2461575767eSDavid Hildenbrand 2471575767eSDavid Hildenbrand /* 2481575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2491575767eSDavid Hildenbrand * -delta to the epoch. 2501575767eSDavid Hildenbrand */ 2511575767eSDavid Hildenbrand delta = -delta; 2521575767eSDavid Hildenbrand 2531575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2541575767eSDavid Hildenbrand if ((s64)delta < 0) 2551575767eSDavid Hildenbrand delta_idx = -1; 2561575767eSDavid Hildenbrand 2571575767eSDavid Hildenbrand scb->epoch += delta; 2581575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2591575767eSDavid Hildenbrand scb->epdx += delta_idx; 2601575767eSDavid Hildenbrand if (scb->epoch < delta) 2611575767eSDavid Hildenbrand scb->epdx += 1; 2621575767eSDavid Hildenbrand } 2631575767eSDavid Hildenbrand } 2641575767eSDavid Hildenbrand 265fdf03650SFan Zhang /* 266fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 267fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 268fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 269fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 270fdf03650SFan Zhang */ 271fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 272fdf03650SFan Zhang void *v) 273fdf03650SFan Zhang { 274fdf03650SFan Zhang struct kvm *kvm; 275fdf03650SFan Zhang struct kvm_vcpu *vcpu; 276fdf03650SFan Zhang int i; 277fdf03650SFan Zhang unsigned long long *delta = v; 278fdf03650SFan Zhang 279fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 280fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 2811575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 2821575767eSDavid Hildenbrand if (i == 0) { 2831575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 2841575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 2851575767eSDavid Hildenbrand } 286db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 287db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 28891473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 2891575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 2901575767eSDavid Hildenbrand *delta); 291fdf03650SFan Zhang } 292fdf03650SFan Zhang } 293fdf03650SFan Zhang return NOTIFY_OK; 294fdf03650SFan Zhang } 295fdf03650SFan Zhang 296fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 297fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 298fdf03650SFan Zhang }; 299fdf03650SFan Zhang 300b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 301b0c632dbSHeiko Carstens { 3022c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 303b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 304a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 305a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 306fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 307fdf03650SFan Zhang &kvm_clock_notifier); 308b0c632dbSHeiko Carstens return 0; 309b0c632dbSHeiko Carstens } 310b0c632dbSHeiko Carstens 311b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 312b0c632dbSHeiko Carstens { 313b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 314a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 315fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 316fdf03650SFan Zhang &kvm_clock_notifier); 317b0c632dbSHeiko Carstens } 318b0c632dbSHeiko Carstens 31922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 32022be5a13SDavid Hildenbrand { 32122be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 32222be5a13SDavid Hildenbrand } 32322be5a13SDavid Hildenbrand 3240a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3250a763c78SDavid Hildenbrand { 3260a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 327d051ae53SHeiko Carstens int cc; 3280a763c78SDavid Hildenbrand 3290a763c78SDavid Hildenbrand asm volatile( 3300a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3310a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3320a763c78SDavid Hildenbrand " ipm %0\n" 3330a763c78SDavid Hildenbrand " srl %0,28\n" 3340a763c78SDavid Hildenbrand : "=d" (cc) 3350a763c78SDavid Hildenbrand : "d" (r0) 3360a763c78SDavid Hildenbrand : "cc"); 3370a763c78SDavid Hildenbrand return cc == 0; 3380a763c78SDavid Hildenbrand } 3390a763c78SDavid Hildenbrand 340d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 341d6681397SChristian Borntraeger { 342d6681397SChristian Borntraeger register unsigned long r0 asm("0") = 0; /* query function */ 343d6681397SChristian Borntraeger register unsigned long r1 asm("1") = (unsigned long) query; 344d6681397SChristian Borntraeger 345d6681397SChristian Borntraeger asm volatile( 346d6681397SChristian Borntraeger /* Parameter regs are ignored */ 347d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 348b1c41ac3SHeiko Carstens : 349d6681397SChristian Borntraeger : "d" (r0), "a" (r1), [opc] "i" (opcode) 350b1c41ac3SHeiko Carstens : "cc", "memory"); 351d6681397SChristian Borntraeger } 352d6681397SChristian Borntraeger 353173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3544f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 355173aec2dSChristian Borntraeger 35622be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 35722be5a13SDavid Hildenbrand { 3580a763c78SDavid Hildenbrand int i; 3590a763c78SDavid Hildenbrand 3600a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3610a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3620a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3630a763c78SDavid Hildenbrand } 3640a763c78SDavid Hildenbrand 3650a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 366221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 367221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 368221bb8a4SLinus Torvalds PTFF_QAF); 3690a763c78SDavid Hildenbrand 3700a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 37169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 37269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 37369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 37469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 37569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 37669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 37769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 37869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 37969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 38069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 3810a763c78SDavid Hildenbrand } 3820a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 38369c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 38469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 3850a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 38669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 38769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 38869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 38969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 39069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 39169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 39269c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 39369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 3940a763c78SDavid Hildenbrand } 3950a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 396985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 39769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 3980a763c78SDavid Hildenbrand 399e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 400e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 401e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 402e000b8e0SJason J. Herne 40313209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 40413209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 40513209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 40613209ad0SChristian Borntraeger 407173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 408173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 409173aec2dSChristian Borntraeger 4104f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4114f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4124f45b90eSChristian Borntraeger 41322be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 41422be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 415a3508fbeSDavid Hildenbrand /* 416a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 417a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 418a3508fbeSDavid Hildenbrand */ 419a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 420a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 421a3508fbeSDavid Hildenbrand return; 422a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 42319c439b5SDavid Hildenbrand if (sclp.has_64bscao) 42419c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4250615a326SDavid Hildenbrand if (sclp.has_siif) 4260615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 42777d18f6dSDavid Hildenbrand if (sclp.has_gpere) 42877d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 429a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 430a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4315630a8e8SDavid Hildenbrand if (sclp.has_ib) 4325630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 43313ee3f67SDavid Hildenbrand if (sclp.has_cei) 43413ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4357fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4367fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 437730cd632SFarhan Ali if (sclp.has_kss) 438730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4395d3876a8SDavid Hildenbrand /* 4405d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4415d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4425d3876a8SDavid Hildenbrand * instead of the real storage key. 4435d3876a8SDavid Hildenbrand * 4445d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4455d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4465d3876a8SDavid Hildenbrand * 4475d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4485d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4495d3876a8SDavid Hildenbrand * 4505d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4515d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4525d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4535d3876a8SDavid Hildenbrand * 4545d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4555d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4565d3876a8SDavid Hildenbrand */ 45722be5a13SDavid Hildenbrand } 45822be5a13SDavid Hildenbrand 459b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 460b0c632dbSHeiko Carstens { 461f76f6371SJanosch Frank int rc = -ENOMEM; 462308c3e66SMichael Mueller 46378f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 46478f26131SChristian Borntraeger if (!kvm_s390_dbf) 46578f26131SChristian Borntraeger return -ENOMEM; 46678f26131SChristian Borntraeger 4673e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); 4683e6c5568SJanosch Frank if (!kvm_s390_dbf_uv) 4693e6c5568SJanosch Frank goto out; 4703e6c5568SJanosch Frank 4713e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || 4723e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) 473f76f6371SJanosch Frank goto out; 47478f26131SChristian Borntraeger 47522be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 47622be5a13SDavid Hildenbrand 47784877d93SCornelia Huck /* Register floating interrupt controller interface. */ 478308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 479308c3e66SMichael Mueller if (rc) { 4808d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 481f76f6371SJanosch Frank goto out; 482308c3e66SMichael Mueller } 483b1d1e76eSMichael Mueller 484b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 485b1d1e76eSMichael Mueller if (rc) 486f76f6371SJanosch Frank goto out; 487b1d1e76eSMichael Mueller 488308c3e66SMichael Mueller return 0; 489308c3e66SMichael Mueller 490f76f6371SJanosch Frank out: 491f76f6371SJanosch Frank kvm_arch_exit(); 492308c3e66SMichael Mueller return rc; 493b0c632dbSHeiko Carstens } 494b0c632dbSHeiko Carstens 49578f26131SChristian Borntraeger void kvm_arch_exit(void) 49678f26131SChristian Borntraeger { 4971282c21eSMichael Mueller kvm_s390_gib_destroy(); 49878f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 4993e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv); 50078f26131SChristian Borntraeger } 50178f26131SChristian Borntraeger 502b0c632dbSHeiko Carstens /* Section: device related */ 503b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 504b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 505b0c632dbSHeiko Carstens { 506b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 507b0c632dbSHeiko Carstens return s390_enable_sie(); 508b0c632dbSHeiko Carstens return -EINVAL; 509b0c632dbSHeiko Carstens } 510b0c632dbSHeiko Carstens 511784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 512b0c632dbSHeiko Carstens { 513d7b0b5ebSCarsten Otte int r; 514d7b0b5ebSCarsten Otte 5152bd0ac4eSCarsten Otte switch (ext) { 516d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 517b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 51852e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5191efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5201efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5211efd0f59SCarsten Otte #endif 5223c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 52360b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 52414eebd91SCarsten Otte case KVM_CAP_ONE_REG: 525d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 526fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 52710ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 528c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 52978599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 530f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5316352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 532460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 53347b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5342444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 535e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 53630ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 537816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5386502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5394036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 54047a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 541da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 5427de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS: 543d7b0b5ebSCarsten Otte r = 1; 544d7b0b5ebSCarsten Otte break; 545a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 546a4499382SJanosch Frank r = 0; 54740ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 548a4499382SJanosch Frank r = 1; 549a4499382SJanosch Frank break; 55041408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 55141408c28SThomas Huth r = MEM_OP_MAX_SIZE; 55241408c28SThomas Huth break; 553e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 554e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 555a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 55676a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 557a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 558a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 559a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 56076a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 561e726b1bdSChristian Borntraeger break; 5621526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 563abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 5641526bf9cSChristian Borntraeger break; 56568c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 56668c55750SEric Farman r = MACHINE_HAS_VX; 56768c55750SEric Farman break; 568c6e5f166SFan Zhang case KVM_CAP_S390_RI: 569c6e5f166SFan Zhang r = test_facility(64); 570c6e5f166SFan Zhang break; 5714e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 5724e0b1ab7SFan Zhang r = test_facility(133); 5734e0b1ab7SFan Zhang break; 57435b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 57535b3fde6SChristian Borntraeger r = test_facility(82); 57635b3fde6SChristian Borntraeger break; 5772bd0ac4eSCarsten Otte default: 578d7b0b5ebSCarsten Otte r = 0; 579b0c632dbSHeiko Carstens } 580d7b0b5ebSCarsten Otte return r; 5812bd0ac4eSCarsten Otte } 582b0c632dbSHeiko Carstens 58315f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 58415f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 58515f36ebdSJason J. Herne { 5860959e168SJanosch Frank int i; 58715f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 5880959e168SJanosch Frank unsigned long gaddr, vmaddr; 58915f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 5900959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 59115f36ebdSJason J. Herne 5920959e168SJanosch Frank /* Loop over all guest segments */ 5930959e168SJanosch Frank cur_gfn = memslot->base_gfn; 59415f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 5950959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 5960959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 5970959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 5980959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 5990959e168SJanosch Frank continue; 60015f36ebdSJason J. Herne 6010959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6020959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6030959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6040959e168SJanosch Frank if (test_bit(i, bitmap)) 6050959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6060959e168SJanosch Frank } 6070959e168SJanosch Frank 6081763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6091763f8d0SChristian Borntraeger return; 61070c88a00SChristian Borntraeger cond_resched(); 61115f36ebdSJason J. Herne } 61215f36ebdSJason J. Herne } 61315f36ebdSJason J. Herne 614b0c632dbSHeiko Carstens /* Section: vm related */ 615a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 616a6e2f683SEugene (jno) Dvurechenski 617b0c632dbSHeiko Carstens /* 618b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 619b0c632dbSHeiko Carstens */ 620b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 621b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 622b0c632dbSHeiko Carstens { 62315f36ebdSJason J. Herne int r; 62415f36ebdSJason J. Herne unsigned long n; 6259f6b8029SPaolo Bonzini struct kvm_memslots *slots; 62615f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 62715f36ebdSJason J. Herne int is_dirty = 0; 62815f36ebdSJason J. Herne 629e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 630e1e8a962SJanosch Frank return -EINVAL; 631e1e8a962SJanosch Frank 63215f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 63315f36ebdSJason J. Herne 63415f36ebdSJason J. Herne r = -EINVAL; 63515f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 63615f36ebdSJason J. Herne goto out; 63715f36ebdSJason J. Herne 6389f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 6399f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 64015f36ebdSJason J. Herne r = -ENOENT; 64115f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 64215f36ebdSJason J. Herne goto out; 64315f36ebdSJason J. Herne 64415f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 64515f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 64615f36ebdSJason J. Herne if (r) 64715f36ebdSJason J. Herne goto out; 64815f36ebdSJason J. Herne 64915f36ebdSJason J. Herne /* Clear the dirty log */ 65015f36ebdSJason J. Herne if (is_dirty) { 65115f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 65215f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 65315f36ebdSJason J. Herne } 65415f36ebdSJason J. Herne r = 0; 65515f36ebdSJason J. Herne out: 65615f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 65715f36ebdSJason J. Herne return r; 658b0c632dbSHeiko Carstens } 659b0c632dbSHeiko Carstens 6606502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 6616502a34cSDavid Hildenbrand { 6626502a34cSDavid Hildenbrand unsigned int i; 6636502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 6646502a34cSDavid Hildenbrand 6656502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 6666502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 6676502a34cSDavid Hildenbrand } 6686502a34cSDavid Hildenbrand } 6696502a34cSDavid Hildenbrand 670e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 671d938dc55SCornelia Huck { 672d938dc55SCornelia Huck int r; 673d938dc55SCornelia Huck 674d938dc55SCornelia Huck if (cap->flags) 675d938dc55SCornelia Huck return -EINVAL; 676d938dc55SCornelia Huck 677d938dc55SCornelia Huck switch (cap->cap) { 67884223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 679c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 68084223598SCornelia Huck kvm->arch.use_irqchip = 1; 68184223598SCornelia Huck r = 0; 68284223598SCornelia Huck break; 6832444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 684c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 6852444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 6862444b352SDavid Hildenbrand r = 0; 6872444b352SDavid Hildenbrand break; 68868c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 6895967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 690a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 6915967c17bSDavid Hildenbrand r = -EBUSY; 6925967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 693c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 694c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 6952f87d942SGuenther Hutzl if (test_facility(134)) { 6962f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 6972f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 6982f87d942SGuenther Hutzl } 69953743aa7SMaxim Samoylov if (test_facility(135)) { 70053743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 70153743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 70253743aa7SMaxim Samoylov } 7037832e91cSChristian Borntraeger if (test_facility(148)) { 7047832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7057832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7067832e91cSChristian Borntraeger } 707d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 708d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 709d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 710d5cb6ab1SChristian Borntraeger } 71118280d8bSMichael Mueller r = 0; 71218280d8bSMichael Mueller } else 71318280d8bSMichael Mueller r = -EINVAL; 7145967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 715c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 716c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 71768c55750SEric Farman break; 718c6e5f166SFan Zhang case KVM_CAP_S390_RI: 719c6e5f166SFan Zhang r = -EINVAL; 720c6e5f166SFan Zhang mutex_lock(&kvm->lock); 721a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 722c6e5f166SFan Zhang r = -EBUSY; 723c6e5f166SFan Zhang } else if (test_facility(64)) { 724c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 725c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 726c6e5f166SFan Zhang r = 0; 727c6e5f166SFan Zhang } 728c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 729c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 730c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 731c6e5f166SFan Zhang break; 73247a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 73347a4693eSYi Min Zhao mutex_lock(&kvm->lock); 73447a4693eSYi Min Zhao if (kvm->created_vcpus) { 73547a4693eSYi Min Zhao r = -EBUSY; 73647a4693eSYi Min Zhao } else { 73747a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 73847a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 73947a4693eSYi Min Zhao r = 0; 74047a4693eSYi Min Zhao } 74147a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 74247a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 74347a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 74447a4693eSYi Min Zhao break; 7454e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 7464e0b1ab7SFan Zhang r = -EINVAL; 7474e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 748241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 7494e0b1ab7SFan Zhang r = -EBUSY; 7504e0b1ab7SFan Zhang } else if (test_facility(133)) { 7514e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 7524e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 7534e0b1ab7SFan Zhang r = 0; 7544e0b1ab7SFan Zhang } 7554e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 7564e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 7574e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 7584e0b1ab7SFan Zhang break; 759a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 760a4499382SJanosch Frank mutex_lock(&kvm->lock); 761a4499382SJanosch Frank if (kvm->created_vcpus) 762a4499382SJanosch Frank r = -EBUSY; 76340ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 764a4499382SJanosch Frank r = -EINVAL; 765a4499382SJanosch Frank else { 766a4499382SJanosch Frank r = 0; 767df88f318SJanosch Frank down_write(&kvm->mm->mmap_sem); 768a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 769df88f318SJanosch Frank up_write(&kvm->mm->mmap_sem); 770a4499382SJanosch Frank /* 771a4499382SJanosch Frank * We might have to create fake 4k page 772a4499382SJanosch Frank * tables. To avoid that the hardware works on 773a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 774a4499382SJanosch Frank */ 775a4499382SJanosch Frank kvm->arch.use_skf = 0; 776a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 777a4499382SJanosch Frank } 778a4499382SJanosch Frank mutex_unlock(&kvm->lock); 779a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 780a4499382SJanosch Frank r ? "(not available)" : "(success)"); 781a4499382SJanosch Frank break; 782e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 783c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 784e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 785e44fc8c9SEkaterina Tumanova r = 0; 786e44fc8c9SEkaterina Tumanova break; 7876502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 7886502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 7896502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 7906502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 7916502a34cSDavid Hildenbrand r = 0; 7926502a34cSDavid Hildenbrand break; 793d938dc55SCornelia Huck default: 794d938dc55SCornelia Huck r = -EINVAL; 795d938dc55SCornelia Huck break; 796d938dc55SCornelia Huck } 797d938dc55SCornelia Huck return r; 798d938dc55SCornelia Huck } 799d938dc55SCornelia Huck 8008c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8018c0a7ce6SDominik Dingel { 8028c0a7ce6SDominik Dingel int ret; 8038c0a7ce6SDominik Dingel 8048c0a7ce6SDominik Dingel switch (attr->attr) { 8058c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8068c0a7ce6SDominik Dingel ret = 0; 807c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 808a3a92c31SDominik Dingel kvm->arch.mem_limit); 809a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8108c0a7ce6SDominik Dingel ret = -EFAULT; 8118c0a7ce6SDominik Dingel break; 8128c0a7ce6SDominik Dingel default: 8138c0a7ce6SDominik Dingel ret = -ENXIO; 8148c0a7ce6SDominik Dingel break; 8158c0a7ce6SDominik Dingel } 8168c0a7ce6SDominik Dingel return ret; 8178c0a7ce6SDominik Dingel } 8188c0a7ce6SDominik Dingel 8198c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8204f718eabSDominik Dingel { 8214f718eabSDominik Dingel int ret; 8224f718eabSDominik Dingel unsigned int idx; 8234f718eabSDominik Dingel switch (attr->attr) { 8244f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 825f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 826c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 827e6db1d61SDominik Dingel break; 828e6db1d61SDominik Dingel 829c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 8304f718eabSDominik Dingel mutex_lock(&kvm->lock); 831a4499382SJanosch Frank if (kvm->created_vcpus) 832a4499382SJanosch Frank ret = -EBUSY; 833a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 834a4499382SJanosch Frank ret = -EINVAL; 835a4499382SJanosch Frank else { 8364f718eabSDominik Dingel kvm->arch.use_cmma = 1; 837c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 838c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 8394f718eabSDominik Dingel ret = 0; 8404f718eabSDominik Dingel } 8414f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8424f718eabSDominik Dingel break; 8434f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 844f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 845f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 846f9cbd9b0SDavid Hildenbrand break; 847c3489155SDominik Dingel ret = -EINVAL; 848c3489155SDominik Dingel if (!kvm->arch.use_cmma) 849c3489155SDominik Dingel break; 850c3489155SDominik Dingel 851c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 8524f718eabSDominik Dingel mutex_lock(&kvm->lock); 8534f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 854a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 8554f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 8564f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8574f718eabSDominik Dingel ret = 0; 8584f718eabSDominik Dingel break; 8598c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 8608c0a7ce6SDominik Dingel unsigned long new_limit; 8618c0a7ce6SDominik Dingel 8628c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 8638c0a7ce6SDominik Dingel return -EINVAL; 8648c0a7ce6SDominik Dingel 8658c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 8668c0a7ce6SDominik Dingel return -EFAULT; 8678c0a7ce6SDominik Dingel 868a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 869a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 8708c0a7ce6SDominik Dingel return -E2BIG; 8718c0a7ce6SDominik Dingel 872a3a92c31SDominik Dingel if (!new_limit) 873a3a92c31SDominik Dingel return -EINVAL; 874a3a92c31SDominik Dingel 8756ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 876a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 877a3a92c31SDominik Dingel new_limit -= 1; 878a3a92c31SDominik Dingel 8798c0a7ce6SDominik Dingel ret = -EBUSY; 8808c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 881a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 8826ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 8836ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 8848c0a7ce6SDominik Dingel 8858c0a7ce6SDominik Dingel if (!new) { 8868c0a7ce6SDominik Dingel ret = -ENOMEM; 8878c0a7ce6SDominik Dingel } else { 8886ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 8898c0a7ce6SDominik Dingel new->private = kvm; 8908c0a7ce6SDominik Dingel kvm->arch.gmap = new; 8918c0a7ce6SDominik Dingel ret = 0; 8928c0a7ce6SDominik Dingel } 8938c0a7ce6SDominik Dingel } 8948c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 895a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 896a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 897a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 8988c0a7ce6SDominik Dingel break; 8998c0a7ce6SDominik Dingel } 9004f718eabSDominik Dingel default: 9014f718eabSDominik Dingel ret = -ENXIO; 9024f718eabSDominik Dingel break; 9034f718eabSDominik Dingel } 9044f718eabSDominik Dingel return ret; 9054f718eabSDominik Dingel } 9064f718eabSDominik Dingel 907a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 908a374e892STony Krowiak 90920c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 910a374e892STony Krowiak { 911a374e892STony Krowiak struct kvm_vcpu *vcpu; 912a374e892STony Krowiak int i; 913a374e892STony Krowiak 91420c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 91520c922f0STony Krowiak 9163194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 91720c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 9183194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 9193194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 9203194cdb7SDavid Hildenbrand } 92120c922f0STony Krowiak 92220c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 92320c922f0STony Krowiak } 92420c922f0STony Krowiak 92520c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 92620c922f0STony Krowiak { 927a374e892STony Krowiak mutex_lock(&kvm->lock); 928a374e892STony Krowiak switch (attr->attr) { 929a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 9308e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9318e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 93237940fb0STony Krowiak return -EINVAL; 9338e41bd54SChristian Borntraeger } 934a374e892STony Krowiak get_random_bytes( 935a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 936a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 937a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 938c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 939a374e892STony Krowiak break; 940a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 9418e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9428e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 94337940fb0STony Krowiak return -EINVAL; 9448e41bd54SChristian Borntraeger } 945a374e892STony Krowiak get_random_bytes( 946a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 947a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 948a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 949c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 950a374e892STony Krowiak break; 951a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 9528e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9538e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 95437940fb0STony Krowiak return -EINVAL; 9558e41bd54SChristian Borntraeger } 956a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 957a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 958a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 959c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 960a374e892STony Krowiak break; 961a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 9628e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9638e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 96437940fb0STony Krowiak return -EINVAL; 9658e41bd54SChristian Borntraeger } 966a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 967a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 968a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 969c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 970a374e892STony Krowiak break; 97137940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 97237940fb0STony Krowiak if (!ap_instructions_available()) { 97337940fb0STony Krowiak mutex_unlock(&kvm->lock); 97437940fb0STony Krowiak return -EOPNOTSUPP; 97537940fb0STony Krowiak } 97637940fb0STony Krowiak kvm->arch.crypto.apie = 1; 97737940fb0STony Krowiak break; 97837940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 97937940fb0STony Krowiak if (!ap_instructions_available()) { 98037940fb0STony Krowiak mutex_unlock(&kvm->lock); 98137940fb0STony Krowiak return -EOPNOTSUPP; 98237940fb0STony Krowiak } 98337940fb0STony Krowiak kvm->arch.crypto.apie = 0; 98437940fb0STony Krowiak break; 985a374e892STony Krowiak default: 986a374e892STony Krowiak mutex_unlock(&kvm->lock); 987a374e892STony Krowiak return -ENXIO; 988a374e892STony Krowiak } 989a374e892STony Krowiak 99020c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 991a374e892STony Krowiak mutex_unlock(&kvm->lock); 992a374e892STony Krowiak return 0; 993a374e892STony Krowiak } 994a374e892STony Krowiak 995190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 996190df4a2SClaudio Imbrenda { 997190df4a2SClaudio Imbrenda int cx; 998190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 999190df4a2SClaudio Imbrenda 1000190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1001190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1002190df4a2SClaudio Imbrenda } 1003190df4a2SClaudio Imbrenda 1004190df4a2SClaudio Imbrenda /* 1005190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 10061de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1007190df4a2SClaudio Imbrenda */ 1008190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1009190df4a2SClaudio Imbrenda { 1010190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1011190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1012afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1013190df4a2SClaudio Imbrenda int slotnr; 1014190df4a2SClaudio Imbrenda 1015190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1016afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1017190df4a2SClaudio Imbrenda return 0; 1018190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1019190df4a2SClaudio Imbrenda if (!slots || !slots->used_slots) 1020190df4a2SClaudio Imbrenda return -EINVAL; 1021190df4a2SClaudio Imbrenda 1022afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1023afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1024afdad616SClaudio Imbrenda return 0; 1025190df4a2SClaudio Imbrenda } 1026190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1027190df4a2SClaudio Imbrenda for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { 1028190df4a2SClaudio Imbrenda ms = slots->memslots + slotnr; 102913a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 103013a17cc0SIgor Mammedov return -EINVAL; 1031afdad616SClaudio Imbrenda /* 1032afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1033afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1034afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1035afdad616SClaudio Imbrenda * attributes. 1036afdad616SClaudio Imbrenda */ 1037afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1038afdad616SClaudio Imbrenda ram_pages += ms->npages; 1039190df4a2SClaudio Imbrenda } 1040afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1041afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1042190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1043190df4a2SClaudio Imbrenda return 0; 1044190df4a2SClaudio Imbrenda } 1045190df4a2SClaudio Imbrenda 1046190df4a2SClaudio Imbrenda /* 10471de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1048190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1049190df4a2SClaudio Imbrenda */ 1050190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1051190df4a2SClaudio Imbrenda { 1052190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1053afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1054190df4a2SClaudio Imbrenda return 0; 1055afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1056afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1057190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1058190df4a2SClaudio Imbrenda return 0; 1059190df4a2SClaudio Imbrenda } 1060190df4a2SClaudio Imbrenda 1061190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1062190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1063190df4a2SClaudio Imbrenda { 10641de1ea7eSChristian Borntraeger int res = -ENXIO; 1065190df4a2SClaudio Imbrenda 10661de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1067190df4a2SClaudio Imbrenda switch (attr->attr) { 1068190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1069190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1070190df4a2SClaudio Imbrenda break; 1071190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1072190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1073190df4a2SClaudio Imbrenda break; 1074190df4a2SClaudio Imbrenda default: 1075190df4a2SClaudio Imbrenda break; 1076190df4a2SClaudio Imbrenda } 10771de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1078190df4a2SClaudio Imbrenda 1079190df4a2SClaudio Imbrenda return res; 1080190df4a2SClaudio Imbrenda } 1081190df4a2SClaudio Imbrenda 1082190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1083190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1084190df4a2SClaudio Imbrenda { 1085afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1086190df4a2SClaudio Imbrenda 1087190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1088190df4a2SClaudio Imbrenda return -ENXIO; 1089190df4a2SClaudio Imbrenda 1090190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1091190df4a2SClaudio Imbrenda return -EFAULT; 1092190df4a2SClaudio Imbrenda return 0; 1093190df4a2SClaudio Imbrenda } 1094190df4a2SClaudio Imbrenda 10958fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 10968fa1696eSCollin L. Walling { 10978fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 10988fa1696eSCollin L. Walling 10998fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 11008fa1696eSCollin L. Walling return -EFAULT; 11018fa1696eSCollin L. Walling 11020e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 11038fa1696eSCollin L. Walling return -EINVAL; 11040e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11058fa1696eSCollin L. Walling 11068fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 11078fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11088fa1696eSCollin L. Walling 11098fa1696eSCollin L. Walling return 0; 11108fa1696eSCollin L. Walling } 11118fa1696eSCollin L. Walling 111272f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 111372f25020SJason J. Herne { 111472f25020SJason J. Herne u8 gtod_high; 111572f25020SJason J. Herne 111672f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 111772f25020SJason J. Herne sizeof(gtod_high))) 111872f25020SJason J. Herne return -EFAULT; 111972f25020SJason J. Herne 112072f25020SJason J. Herne if (gtod_high != 0) 112172f25020SJason J. Herne return -EINVAL; 112258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 112372f25020SJason J. Herne 112472f25020SJason J. Herne return 0; 112572f25020SJason J. Herne } 112672f25020SJason J. Herne 112772f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 112872f25020SJason J. Herne { 11290e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 113072f25020SJason J. Herne 11310e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 11320e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 113372f25020SJason J. Herne return -EFAULT; 113472f25020SJason J. Herne 11350e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11360e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 113772f25020SJason J. Herne return 0; 113872f25020SJason J. Herne } 113972f25020SJason J. Herne 114072f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 114172f25020SJason J. Herne { 114272f25020SJason J. Herne int ret; 114372f25020SJason J. Herne 114472f25020SJason J. Herne if (attr->flags) 114572f25020SJason J. Herne return -EINVAL; 114672f25020SJason J. Herne 114772f25020SJason J. Herne switch (attr->attr) { 11488fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 11498fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 11508fa1696eSCollin L. Walling break; 115172f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 115272f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 115372f25020SJason J. Herne break; 115472f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 115572f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 115672f25020SJason J. Herne break; 115772f25020SJason J. Herne default: 115872f25020SJason J. Herne ret = -ENXIO; 115972f25020SJason J. Herne break; 116072f25020SJason J. Herne } 116172f25020SJason J. Herne return ret; 116272f25020SJason J. Herne } 116372f25020SJason J. Herne 116433d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 11658fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 11668fa1696eSCollin L. Walling { 11678fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 11688fa1696eSCollin L. Walling 11698fa1696eSCollin L. Walling preempt_disable(); 11708fa1696eSCollin L. Walling 11718fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 11728fa1696eSCollin L. Walling 11738fa1696eSCollin L. Walling gtod->tod = htod.tod + kvm->arch.epoch; 117433d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 117533d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 11768fa1696eSCollin L. Walling gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx; 11778fa1696eSCollin L. Walling if (gtod->tod < htod.tod) 11788fa1696eSCollin L. Walling gtod->epoch_idx += 1; 117933d1b272SDavid Hildenbrand } 11808fa1696eSCollin L. Walling 11818fa1696eSCollin L. Walling preempt_enable(); 11828fa1696eSCollin L. Walling } 11838fa1696eSCollin L. Walling 11848fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 11858fa1696eSCollin L. Walling { 11868fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 11878fa1696eSCollin L. Walling 11888fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 118933d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 11908fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 11918fa1696eSCollin L. Walling return -EFAULT; 11928fa1696eSCollin L. Walling 11938fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 11948fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11958fa1696eSCollin L. Walling return 0; 11968fa1696eSCollin L. Walling } 11978fa1696eSCollin L. Walling 119872f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 119972f25020SJason J. Herne { 120072f25020SJason J. Herne u8 gtod_high = 0; 120172f25020SJason J. Herne 120272f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 120372f25020SJason J. Herne sizeof(gtod_high))) 120472f25020SJason J. Herne return -EFAULT; 120558c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 120672f25020SJason J. Herne 120772f25020SJason J. Herne return 0; 120872f25020SJason J. Herne } 120972f25020SJason J. Herne 121072f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 121172f25020SJason J. Herne { 12125a3d883aSDavid Hildenbrand u64 gtod; 121372f25020SJason J. Herne 121460417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 121572f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 121672f25020SJason J. Herne return -EFAULT; 121758c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 121872f25020SJason J. Herne 121972f25020SJason J. Herne return 0; 122072f25020SJason J. Herne } 122172f25020SJason J. Herne 122272f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 122372f25020SJason J. Herne { 122472f25020SJason J. Herne int ret; 122572f25020SJason J. Herne 122672f25020SJason J. Herne if (attr->flags) 122772f25020SJason J. Herne return -EINVAL; 122872f25020SJason J. Herne 122972f25020SJason J. Herne switch (attr->attr) { 12308fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12318fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 12328fa1696eSCollin L. Walling break; 123372f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 123472f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 123572f25020SJason J. Herne break; 123672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 123772f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 123872f25020SJason J. Herne break; 123972f25020SJason J. Herne default: 124072f25020SJason J. Herne ret = -ENXIO; 124172f25020SJason J. Herne break; 124272f25020SJason J. Herne } 124372f25020SJason J. Herne return ret; 124472f25020SJason J. Herne } 124572f25020SJason J. Herne 1246658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1247658b6edaSMichael Mueller { 1248658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1249053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1250658b6edaSMichael Mueller int ret = 0; 1251658b6edaSMichael Mueller 1252658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1253a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1254658b6edaSMichael Mueller ret = -EBUSY; 1255658b6edaSMichael Mueller goto out; 1256658b6edaSMichael Mueller } 1257658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1258658b6edaSMichael Mueller if (!proc) { 1259658b6edaSMichael Mueller ret = -ENOMEM; 1260658b6edaSMichael Mueller goto out; 1261658b6edaSMichael Mueller } 1262658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1263658b6edaSMichael Mueller sizeof(*proc))) { 12649bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1265053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1266053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 12670487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1268053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1269053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1270053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1271053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1272053dd230SDavid Hildenbrand else 1273658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1274053dd230SDavid Hildenbrand } 1275c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1276658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1277a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1278a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1279a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1280a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1281a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1282a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1283a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1284658b6edaSMichael Mueller } else 1285658b6edaSMichael Mueller ret = -EFAULT; 1286658b6edaSMichael Mueller kfree(proc); 1287658b6edaSMichael Mueller out: 1288658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1289658b6edaSMichael Mueller return ret; 1290658b6edaSMichael Mueller } 1291658b6edaSMichael Mueller 129215c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 129315c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 129415c9705fSDavid Hildenbrand { 129515c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 129615c9705fSDavid Hildenbrand 129715c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 129815c9705fSDavid Hildenbrand return -EFAULT; 129915c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 130015c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 130115c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 130215c9705fSDavid Hildenbrand return -EINVAL; 130315c9705fSDavid Hildenbrand 130415c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 13052f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 13062f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 13072f8311c9SChristian Borntraeger return -EBUSY; 13082f8311c9SChristian Borntraeger } 130915c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 131015c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 131115c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 13122f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 13132f8311c9SChristian Borntraeger data.feat[0], 13142f8311c9SChristian Borntraeger data.feat[1], 13152f8311c9SChristian Borntraeger data.feat[2]); 13162f8311c9SChristian Borntraeger return 0; 131715c9705fSDavid Hildenbrand } 131815c9705fSDavid Hildenbrand 13190a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 13200a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 13210a763c78SDavid Hildenbrand { 1322346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1323346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1324346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1325346fa2f8SChristian Borntraeger return -EBUSY; 1326346fa2f8SChristian Borntraeger } 1327346fa2f8SChristian Borntraeger 1328346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1329346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1330346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1331346fa2f8SChristian Borntraeger return -EFAULT; 1332346fa2f8SChristian Borntraeger } 1333346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1334346fa2f8SChristian Borntraeger 133511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 133611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 133711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 133811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 133911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 134011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 134111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 134211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 134311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 134411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 134511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 134611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 134711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 134811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 134911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 135011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 135111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 135211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 135311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 135411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 135511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 135611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 135711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 135811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 135911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 136011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 136111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 136211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 136311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 136411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 136511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 136611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 136711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 136811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 136911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 137011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 137111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 137211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 137311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 137411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 137511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 137611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 137711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 137811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 137913209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 138013209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 138113209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1382173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1383173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1384173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1385173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1386173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 13874f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 13884f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 13894f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 13904f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 13914f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 139211ba5961SChristian Borntraeger 1393346fa2f8SChristian Borntraeger return 0; 13940a763c78SDavid Hildenbrand } 13950a763c78SDavid Hildenbrand 1396658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1397658b6edaSMichael Mueller { 1398658b6edaSMichael Mueller int ret = -ENXIO; 1399658b6edaSMichael Mueller 1400658b6edaSMichael Mueller switch (attr->attr) { 1401658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1402658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1403658b6edaSMichael Mueller break; 140415c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 140515c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 140615c9705fSDavid Hildenbrand break; 14070a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 14080a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 14090a763c78SDavid Hildenbrand break; 1410658b6edaSMichael Mueller } 1411658b6edaSMichael Mueller return ret; 1412658b6edaSMichael Mueller } 1413658b6edaSMichael Mueller 1414658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1415658b6edaSMichael Mueller { 1416658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1417658b6edaSMichael Mueller int ret = 0; 1418658b6edaSMichael Mueller 1419658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1420658b6edaSMichael Mueller if (!proc) { 1421658b6edaSMichael Mueller ret = -ENOMEM; 1422658b6edaSMichael Mueller goto out; 1423658b6edaSMichael Mueller } 14249bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1425658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1426c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1427c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1428a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1429a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1430a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1431a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1432a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1433a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1434a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1435658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1436658b6edaSMichael Mueller ret = -EFAULT; 1437658b6edaSMichael Mueller kfree(proc); 1438658b6edaSMichael Mueller out: 1439658b6edaSMichael Mueller return ret; 1440658b6edaSMichael Mueller } 1441658b6edaSMichael Mueller 1442658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1443658b6edaSMichael Mueller { 1444658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1445658b6edaSMichael Mueller int ret = 0; 1446658b6edaSMichael Mueller 1447658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 1448658b6edaSMichael Mueller if (!mach) { 1449658b6edaSMichael Mueller ret = -ENOMEM; 1450658b6edaSMichael Mueller goto out; 1451658b6edaSMichael Mueller } 1452658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 145337c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1454c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1455981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1456658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 145704478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 1458a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1459a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1460a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1461a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1462a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1463a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1464a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1465a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1466a8c39dd7SChristian Borntraeger mach->fac_list[0], 1467a8c39dd7SChristian Borntraeger mach->fac_list[1], 1468a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1469658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1470658b6edaSMichael Mueller ret = -EFAULT; 1471658b6edaSMichael Mueller kfree(mach); 1472658b6edaSMichael Mueller out: 1473658b6edaSMichael Mueller return ret; 1474658b6edaSMichael Mueller } 1475658b6edaSMichael Mueller 147615c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 147715c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 147815c9705fSDavid Hildenbrand { 147915c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 148015c9705fSDavid Hildenbrand 148115c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 148215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 148315c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 148415c9705fSDavid Hildenbrand return -EFAULT; 14852f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14862f8311c9SChristian Borntraeger data.feat[0], 14872f8311c9SChristian Borntraeger data.feat[1], 14882f8311c9SChristian Borntraeger data.feat[2]); 148915c9705fSDavid Hildenbrand return 0; 149015c9705fSDavid Hildenbrand } 149115c9705fSDavid Hildenbrand 149215c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 149315c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 149415c9705fSDavid Hildenbrand { 149515c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 149615c9705fSDavid Hildenbrand 149715c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 149815c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 149915c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 150015c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 150115c9705fSDavid Hildenbrand return -EFAULT; 15022f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 15032f8311c9SChristian Borntraeger data.feat[0], 15042f8311c9SChristian Borntraeger data.feat[1], 15052f8311c9SChristian Borntraeger data.feat[2]); 150615c9705fSDavid Hildenbrand return 0; 150715c9705fSDavid Hildenbrand } 150815c9705fSDavid Hildenbrand 15090a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 15100a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15110a763c78SDavid Hildenbrand { 1512346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1513346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1514346fa2f8SChristian Borntraeger return -EFAULT; 1515346fa2f8SChristian Borntraeger 151611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 151711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 151811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 151911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 152011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 152111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 152211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 152311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 152411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 152511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 152611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 152711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 152811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 152911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 153011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 153111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 153211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 153311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 153411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 153511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 153611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 153711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 153811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 153911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 154011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 154111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 154211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 154311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 154411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 154511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 154611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 154711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 154811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 154911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 155011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 155111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 155211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 155311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 155411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 155511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 155611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 155711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 155811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 155911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 156013209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 156113209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 156213209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1563173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1564173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1565173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1566173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1567173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15684f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15694f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15704f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15714f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15724f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 157311ba5961SChristian Borntraeger 1574346fa2f8SChristian Borntraeger return 0; 15750a763c78SDavid Hildenbrand } 15760a763c78SDavid Hildenbrand 15770a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 15780a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15790a763c78SDavid Hildenbrand { 15800a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 15810a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 15820a763c78SDavid Hildenbrand return -EFAULT; 158311ba5961SChristian Borntraeger 158411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 158511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 158611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 158711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 158811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 158911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 159011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 159111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 159211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 159311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 159411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 159511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 159611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 159711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 159811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 159911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 160011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 160111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 160211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 160311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 160411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 160511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 160611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 160711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 160811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 160911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 161011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 161111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 161211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 161311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 161411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 161511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 161611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 161711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 161811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 161911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 162011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 162111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 162211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 162311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 162411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 162511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 162611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 162711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 162813209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 162913209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 163013209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1631173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1632173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1633173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1634173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1635173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 16364f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 16374f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 16384f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 16394f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 16404f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 164111ba5961SChristian Borntraeger 16420a763c78SDavid Hildenbrand return 0; 16430a763c78SDavid Hildenbrand } 1644346fa2f8SChristian Borntraeger 1645658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1646658b6edaSMichael Mueller { 1647658b6edaSMichael Mueller int ret = -ENXIO; 1648658b6edaSMichael Mueller 1649658b6edaSMichael Mueller switch (attr->attr) { 1650658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1651658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1652658b6edaSMichael Mueller break; 1653658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1654658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1655658b6edaSMichael Mueller break; 165615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 165715c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 165815c9705fSDavid Hildenbrand break; 165915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 166015c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 166115c9705fSDavid Hildenbrand break; 16620a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 16630a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 16640a763c78SDavid Hildenbrand break; 16650a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 16660a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 16670a763c78SDavid Hildenbrand break; 1668658b6edaSMichael Mueller } 1669658b6edaSMichael Mueller return ret; 1670658b6edaSMichael Mueller } 1671658b6edaSMichael Mueller 1672f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1673f2061656SDominik Dingel { 1674f2061656SDominik Dingel int ret; 1675f2061656SDominik Dingel 1676f2061656SDominik Dingel switch (attr->group) { 16774f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 16788c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 16794f718eabSDominik Dingel break; 168072f25020SJason J. Herne case KVM_S390_VM_TOD: 168172f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 168272f25020SJason J. Herne break; 1683658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1684658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1685658b6edaSMichael Mueller break; 1686a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1687a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1688a374e892STony Krowiak break; 1689190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1690190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1691190df4a2SClaudio Imbrenda break; 1692f2061656SDominik Dingel default: 1693f2061656SDominik Dingel ret = -ENXIO; 1694f2061656SDominik Dingel break; 1695f2061656SDominik Dingel } 1696f2061656SDominik Dingel 1697f2061656SDominik Dingel return ret; 1698f2061656SDominik Dingel } 1699f2061656SDominik Dingel 1700f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1701f2061656SDominik Dingel { 17028c0a7ce6SDominik Dingel int ret; 17038c0a7ce6SDominik Dingel 17048c0a7ce6SDominik Dingel switch (attr->group) { 17058c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 17068c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 17078c0a7ce6SDominik Dingel break; 170872f25020SJason J. Herne case KVM_S390_VM_TOD: 170972f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 171072f25020SJason J. Herne break; 1711658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1712658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1713658b6edaSMichael Mueller break; 1714190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1715190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1716190df4a2SClaudio Imbrenda break; 17178c0a7ce6SDominik Dingel default: 17188c0a7ce6SDominik Dingel ret = -ENXIO; 17198c0a7ce6SDominik Dingel break; 17208c0a7ce6SDominik Dingel } 17218c0a7ce6SDominik Dingel 17228c0a7ce6SDominik Dingel return ret; 1723f2061656SDominik Dingel } 1724f2061656SDominik Dingel 1725f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1726f2061656SDominik Dingel { 1727f2061656SDominik Dingel int ret; 1728f2061656SDominik Dingel 1729f2061656SDominik Dingel switch (attr->group) { 17304f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 17314f718eabSDominik Dingel switch (attr->attr) { 17324f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 17334f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1734f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1735f9cbd9b0SDavid Hildenbrand break; 17368c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 17374f718eabSDominik Dingel ret = 0; 17384f718eabSDominik Dingel break; 17394f718eabSDominik Dingel default: 17404f718eabSDominik Dingel ret = -ENXIO; 17414f718eabSDominik Dingel break; 17424f718eabSDominik Dingel } 17434f718eabSDominik Dingel break; 174472f25020SJason J. Herne case KVM_S390_VM_TOD: 174572f25020SJason J. Herne switch (attr->attr) { 174672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 174772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 174872f25020SJason J. Herne ret = 0; 174972f25020SJason J. Herne break; 175072f25020SJason J. Herne default: 175172f25020SJason J. Herne ret = -ENXIO; 175272f25020SJason J. Herne break; 175372f25020SJason J. Herne } 175472f25020SJason J. Herne break; 1755658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1756658b6edaSMichael Mueller switch (attr->attr) { 1757658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1758658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 175915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 176015c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 17610a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1762346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1763658b6edaSMichael Mueller ret = 0; 1764658b6edaSMichael Mueller break; 1765658b6edaSMichael Mueller default: 1766658b6edaSMichael Mueller ret = -ENXIO; 1767658b6edaSMichael Mueller break; 1768658b6edaSMichael Mueller } 1769658b6edaSMichael Mueller break; 1770a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1771a374e892STony Krowiak switch (attr->attr) { 1772a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1773a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1774a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1775a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1776a374e892STony Krowiak ret = 0; 1777a374e892STony Krowiak break; 177837940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 177937940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 178037940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 178137940fb0STony Krowiak break; 1782a374e892STony Krowiak default: 1783a374e892STony Krowiak ret = -ENXIO; 1784a374e892STony Krowiak break; 1785a374e892STony Krowiak } 1786a374e892STony Krowiak break; 1787190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1788190df4a2SClaudio Imbrenda ret = 0; 1789190df4a2SClaudio Imbrenda break; 1790f2061656SDominik Dingel default: 1791f2061656SDominik Dingel ret = -ENXIO; 1792f2061656SDominik Dingel break; 1793f2061656SDominik Dingel } 1794f2061656SDominik Dingel 1795f2061656SDominik Dingel return ret; 1796f2061656SDominik Dingel } 1797f2061656SDominik Dingel 179830ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 179930ee2a98SJason J. Herne { 180030ee2a98SJason J. Herne uint8_t *keys; 180130ee2a98SJason J. Herne uint64_t hva; 18024f899147SChristian Borntraeger int srcu_idx, i, r = 0; 180330ee2a98SJason J. Herne 180430ee2a98SJason J. Herne if (args->flags != 0) 180530ee2a98SJason J. Herne return -EINVAL; 180630ee2a98SJason J. Herne 180730ee2a98SJason J. Herne /* Is this guest using storage keys? */ 180855531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 180930ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 181030ee2a98SJason J. Herne 181130ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 181230ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 181330ee2a98SJason J. Herne return -EINVAL; 181430ee2a98SJason J. Herne 1815752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 181630ee2a98SJason J. Herne if (!keys) 181730ee2a98SJason J. Herne return -ENOMEM; 181830ee2a98SJason J. Herne 1819d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 18204f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 182130ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 182230ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 182330ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 182430ee2a98SJason J. Herne r = -EFAULT; 1825d3ed1ceeSMartin Schwidefsky break; 182630ee2a98SJason J. Herne } 182730ee2a98SJason J. Herne 1828154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1829154c8c19SDavid Hildenbrand if (r) 1830d3ed1ceeSMartin Schwidefsky break; 183130ee2a98SJason J. Herne } 18324f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1833d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 183430ee2a98SJason J. Herne 1835d3ed1ceeSMartin Schwidefsky if (!r) { 183630ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 183730ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 183830ee2a98SJason J. Herne if (r) 183930ee2a98SJason J. Herne r = -EFAULT; 1840d3ed1ceeSMartin Schwidefsky } 1841d3ed1ceeSMartin Schwidefsky 184230ee2a98SJason J. Herne kvfree(keys); 184330ee2a98SJason J. Herne return r; 184430ee2a98SJason J. Herne } 184530ee2a98SJason J. Herne 184630ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 184730ee2a98SJason J. Herne { 184830ee2a98SJason J. Herne uint8_t *keys; 184930ee2a98SJason J. Herne uint64_t hva; 18504f899147SChristian Borntraeger int srcu_idx, i, r = 0; 1851bd096f64SJanosch Frank bool unlocked; 185230ee2a98SJason J. Herne 185330ee2a98SJason J. Herne if (args->flags != 0) 185430ee2a98SJason J. Herne return -EINVAL; 185530ee2a98SJason J. Herne 185630ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 185730ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 185830ee2a98SJason J. Herne return -EINVAL; 185930ee2a98SJason J. Herne 1860752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 186130ee2a98SJason J. Herne if (!keys) 186230ee2a98SJason J. Herne return -ENOMEM; 186330ee2a98SJason J. Herne 186430ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 186530ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 186630ee2a98SJason J. Herne if (r) { 186730ee2a98SJason J. Herne r = -EFAULT; 186830ee2a98SJason J. Herne goto out; 186930ee2a98SJason J. Herne } 187030ee2a98SJason J. Herne 187130ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 187214d4a425SDominik Dingel r = s390_enable_skey(); 187314d4a425SDominik Dingel if (r) 187414d4a425SDominik Dingel goto out; 187530ee2a98SJason J. Herne 1876bd096f64SJanosch Frank i = 0; 1877d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 18784f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 1879bd096f64SJanosch Frank while (i < args->count) { 1880bd096f64SJanosch Frank unlocked = false; 188130ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 188230ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 188330ee2a98SJason J. Herne r = -EFAULT; 1884d3ed1ceeSMartin Schwidefsky break; 188530ee2a98SJason J. Herne } 188630ee2a98SJason J. Herne 188730ee2a98SJason J. Herne /* Lowest order bit is reserved */ 188830ee2a98SJason J. Herne if (keys[i] & 0x01) { 188930ee2a98SJason J. Herne r = -EINVAL; 1890d3ed1ceeSMartin Schwidefsky break; 189130ee2a98SJason J. Herne } 189230ee2a98SJason J. Herne 1893fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 1894bd096f64SJanosch Frank if (r) { 1895bd096f64SJanosch Frank r = fixup_user_fault(current, current->mm, hva, 1896bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 189730ee2a98SJason J. Herne if (r) 1898d3ed1ceeSMartin Schwidefsky break; 189930ee2a98SJason J. Herne } 1900bd096f64SJanosch Frank if (!r) 1901bd096f64SJanosch Frank i++; 1902bd096f64SJanosch Frank } 19034f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1904d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 190530ee2a98SJason J. Herne out: 190630ee2a98SJason J. Herne kvfree(keys); 190730ee2a98SJason J. Herne return r; 190830ee2a98SJason J. Herne } 190930ee2a98SJason J. Herne 19104036e387SClaudio Imbrenda /* 19114036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 19124036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 19134036e387SClaudio Imbrenda * two longs. 19144036e387SClaudio Imbrenda */ 19154036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 19164036e387SClaudio Imbrenda /* for consistency */ 19174036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 19184036e387SClaudio Imbrenda 19194036e387SClaudio Imbrenda /* 1920afdad616SClaudio Imbrenda * Similar to gfn_to_memslot, but returns the index of a memslot also when the 1921afdad616SClaudio Imbrenda * address falls in a hole. In that case the index of one of the memslots 1922afdad616SClaudio Imbrenda * bordering the hole is returned. 1923afdad616SClaudio Imbrenda */ 1924afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) 1925afdad616SClaudio Imbrenda { 1926afdad616SClaudio Imbrenda int start = 0, end = slots->used_slots; 1927afdad616SClaudio Imbrenda int slot = atomic_read(&slots->lru_slot); 1928afdad616SClaudio Imbrenda struct kvm_memory_slot *memslots = slots->memslots; 1929afdad616SClaudio Imbrenda 1930afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn && 1931afdad616SClaudio Imbrenda gfn < memslots[slot].base_gfn + memslots[slot].npages) 1932afdad616SClaudio Imbrenda return slot; 1933afdad616SClaudio Imbrenda 1934afdad616SClaudio Imbrenda while (start < end) { 1935afdad616SClaudio Imbrenda slot = start + (end - start) / 2; 1936afdad616SClaudio Imbrenda 1937afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn) 1938afdad616SClaudio Imbrenda end = slot; 1939afdad616SClaudio Imbrenda else 1940afdad616SClaudio Imbrenda start = slot + 1; 1941afdad616SClaudio Imbrenda } 1942afdad616SClaudio Imbrenda 1943afdad616SClaudio Imbrenda if (gfn >= memslots[start].base_gfn && 1944afdad616SClaudio Imbrenda gfn < memslots[start].base_gfn + memslots[start].npages) { 1945afdad616SClaudio Imbrenda atomic_set(&slots->lru_slot, start); 1946afdad616SClaudio Imbrenda } 1947afdad616SClaudio Imbrenda 1948afdad616SClaudio Imbrenda return start; 1949afdad616SClaudio Imbrenda } 1950afdad616SClaudio Imbrenda 1951afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1952afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1953afdad616SClaudio Imbrenda { 1954afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 1955afdad616SClaudio Imbrenda 1956afdad616SClaudio Imbrenda args->count = 0; 1957afdad616SClaudio Imbrenda while (args->count < bufsize) { 1958afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1959afdad616SClaudio Imbrenda /* 1960afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 1961afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 1962afdad616SClaudio Imbrenda */ 1963afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1964afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 1965afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1966afdad616SClaudio Imbrenda pgstev = 0; 1967afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1968afdad616SClaudio Imbrenda cur_gfn++; 1969afdad616SClaudio Imbrenda } 1970afdad616SClaudio Imbrenda 1971afdad616SClaudio Imbrenda return 0; 1972afdad616SClaudio Imbrenda } 1973afdad616SClaudio Imbrenda 1974afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 1975afdad616SClaudio Imbrenda unsigned long cur_gfn) 1976afdad616SClaudio Imbrenda { 1977afdad616SClaudio Imbrenda int slotidx = gfn_to_memslot_approx(slots, cur_gfn); 1978afdad616SClaudio Imbrenda struct kvm_memory_slot *ms = slots->memslots + slotidx; 1979afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 1980afdad616SClaudio Imbrenda 1981afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 1982afdad616SClaudio Imbrenda slotidx--; 1983afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 1984afdad616SClaudio Imbrenda if (slotidx < 0) 1985afdad616SClaudio Imbrenda slotidx = slots->used_slots - 1; 1986afdad616SClaudio Imbrenda 1987afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1988afdad616SClaudio Imbrenda ofs = 0; 1989afdad616SClaudio Imbrenda } 1990afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 1991afdad616SClaudio Imbrenda while ((slotidx > 0) && (ofs >= ms->npages)) { 1992afdad616SClaudio Imbrenda slotidx--; 1993afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1994afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); 1995afdad616SClaudio Imbrenda } 1996afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 1997afdad616SClaudio Imbrenda } 1998afdad616SClaudio Imbrenda 1999afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2000afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2001afdad616SClaudio Imbrenda { 2002afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2003afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2004afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2005afdad616SClaudio Imbrenda 2006afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2007afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2008afdad616SClaudio Imbrenda args->count = 0; 2009afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2010afdad616SClaudio Imbrenda if (!ms) 2011afdad616SClaudio Imbrenda return 0; 2012afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2013afdad616SClaudio Imbrenda mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; 2014afdad616SClaudio Imbrenda 2015afdad616SClaudio Imbrenda while (args->count < bufsize) { 2016afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2017afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2018afdad616SClaudio Imbrenda return 0; 2019afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2020afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2021afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2022afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2023afdad616SClaudio Imbrenda pgstev = 0; 2024afdad616SClaudio Imbrenda /* Save the value */ 2025afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2026afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2027afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2028afdad616SClaudio Imbrenda return 0; 2029afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2030afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2031afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2032afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2033afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2034afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2035afdad616SClaudio Imbrenda return 0; 2036afdad616SClaudio Imbrenda cur_gfn++; 2037afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2038afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2039afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2040afdad616SClaudio Imbrenda if (!ms) 2041afdad616SClaudio Imbrenda return 0; 2042afdad616SClaudio Imbrenda } 2043afdad616SClaudio Imbrenda } 2044afdad616SClaudio Imbrenda return 0; 2045afdad616SClaudio Imbrenda } 2046afdad616SClaudio Imbrenda 2047afdad616SClaudio Imbrenda /* 20484036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 20494036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 20504036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 20514036e387SClaudio Imbrenda * no trailing clean bytes are saved. 20524036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 20534036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 20544036e387SClaudio Imbrenda */ 20554036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 20564036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 20574036e387SClaudio Imbrenda { 2058afdad616SClaudio Imbrenda unsigned long bufsize; 2059afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2060afdad616SClaudio Imbrenda u8 *values; 20614036e387SClaudio Imbrenda 2062afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 20634036e387SClaudio Imbrenda return -ENXIO; 20644036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 20654036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 20664036e387SClaudio Imbrenda return -EINVAL; 20674036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 20684036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2069afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 20704036e387SClaudio Imbrenda return -EINVAL; 20714036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 20724036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2073c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 20744036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20754036e387SClaudio Imbrenda return 0; 20764036e387SClaudio Imbrenda } 20774036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2078afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 20794036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20804036e387SClaudio Imbrenda return 0; 20814036e387SClaudio Imbrenda } 20824036e387SClaudio Imbrenda 2083afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2084afdad616SClaudio Imbrenda if (!values) 20854036e387SClaudio Imbrenda return -ENOMEM; 20864036e387SClaudio Imbrenda 20874036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 20884036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2089afdad616SClaudio Imbrenda if (peek) 2090afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2091afdad616SClaudio Imbrenda else 2092afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 20934036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 20944036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 20954036e387SClaudio Imbrenda 2096afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2097afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2098afdad616SClaudio Imbrenda else 2099afdad616SClaudio Imbrenda args->remaining = 0; 21004036e387SClaudio Imbrenda 2101afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2102afdad616SClaudio Imbrenda ret = -EFAULT; 2103afdad616SClaudio Imbrenda 2104afdad616SClaudio Imbrenda vfree(values); 2105afdad616SClaudio Imbrenda return ret; 21064036e387SClaudio Imbrenda } 21074036e387SClaudio Imbrenda 21084036e387SClaudio Imbrenda /* 21094036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 21104036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2111c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 21124036e387SClaudio Imbrenda */ 21134036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 21144036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 21154036e387SClaudio Imbrenda { 21164036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 21174036e387SClaudio Imbrenda uint8_t *bits; 21184036e387SClaudio Imbrenda int srcu_idx, r = 0; 21194036e387SClaudio Imbrenda 21204036e387SClaudio Imbrenda mask = args->mask; 21214036e387SClaudio Imbrenda 21224036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 21234036e387SClaudio Imbrenda return -ENXIO; 21244036e387SClaudio Imbrenda /* invalid/unsupported flags */ 21254036e387SClaudio Imbrenda if (args->flags != 0) 21264036e387SClaudio Imbrenda return -EINVAL; 21274036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 21284036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 21294036e387SClaudio Imbrenda return -EINVAL; 21304036e387SClaudio Imbrenda /* Nothing to do */ 21314036e387SClaudio Imbrenda if (args->count == 0) 21324036e387SClaudio Imbrenda return 0; 21334036e387SClaudio Imbrenda 213442bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 21354036e387SClaudio Imbrenda if (!bits) 21364036e387SClaudio Imbrenda return -ENOMEM; 21374036e387SClaudio Imbrenda 21384036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 21394036e387SClaudio Imbrenda if (r) { 21404036e387SClaudio Imbrenda r = -EFAULT; 21414036e387SClaudio Imbrenda goto out; 21424036e387SClaudio Imbrenda } 21434036e387SClaudio Imbrenda 21444036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 21454036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 21464036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 21474036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 21484036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 21494036e387SClaudio Imbrenda r = -EFAULT; 21504036e387SClaudio Imbrenda break; 21514036e387SClaudio Imbrenda } 21524036e387SClaudio Imbrenda 21534036e387SClaudio Imbrenda pgstev = bits[i]; 21544036e387SClaudio Imbrenda pgstev = pgstev << 24; 21551bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 21564036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 21574036e387SClaudio Imbrenda } 21584036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 21594036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 21604036e387SClaudio Imbrenda 2161c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 21624036e387SClaudio Imbrenda down_write(&kvm->mm->mmap_sem); 2163c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 21644036e387SClaudio Imbrenda up_write(&kvm->mm->mmap_sem); 21654036e387SClaudio Imbrenda } 21664036e387SClaudio Imbrenda out: 21674036e387SClaudio Imbrenda vfree(bits); 21684036e387SClaudio Imbrenda return r; 21694036e387SClaudio Imbrenda } 21704036e387SClaudio Imbrenda 217129b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp) 217229b40f10SJanosch Frank { 217329b40f10SJanosch Frank struct kvm_vcpu *vcpu; 217429b40f10SJanosch Frank u16 rc, rrc; 217529b40f10SJanosch Frank int ret = 0; 217629b40f10SJanosch Frank int i; 217729b40f10SJanosch Frank 217829b40f10SJanosch Frank /* 217929b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 218029b40f10SJanosch Frank * At the same time we must not free the assigned resources when 218129b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 218229b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 218329b40f10SJanosch Frank * behind. 218429b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 218529b40f10SJanosch Frank */ 218629b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 218729b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 218829b40f10SJanosch Frank if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) { 218929b40f10SJanosch Frank *rcp = rc; 219029b40f10SJanosch Frank *rrcp = rrc; 219129b40f10SJanosch Frank ret = -EIO; 219229b40f10SJanosch Frank } 219329b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 219429b40f10SJanosch Frank } 219529b40f10SJanosch Frank return ret; 219629b40f10SJanosch Frank } 219729b40f10SJanosch Frank 219829b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 219929b40f10SJanosch Frank { 220029b40f10SJanosch Frank int i, r = 0; 220129b40f10SJanosch Frank u16 dummy; 220229b40f10SJanosch Frank 220329b40f10SJanosch Frank struct kvm_vcpu *vcpu; 220429b40f10SJanosch Frank 220529b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 220629b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 220729b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 220829b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 220929b40f10SJanosch Frank if (r) 221029b40f10SJanosch Frank break; 221129b40f10SJanosch Frank } 221229b40f10SJanosch Frank if (r) 221329b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 221429b40f10SJanosch Frank return r; 221529b40f10SJanosch Frank } 221629b40f10SJanosch Frank 221729b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 221829b40f10SJanosch Frank { 221929b40f10SJanosch Frank int r = 0; 222029b40f10SJanosch Frank u16 dummy; 222129b40f10SJanosch Frank void __user *argp = (void __user *)cmd->data; 222229b40f10SJanosch Frank 222329b40f10SJanosch Frank switch (cmd->cmd) { 222429b40f10SJanosch Frank case KVM_PV_ENABLE: { 222529b40f10SJanosch Frank r = -EINVAL; 222629b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 222729b40f10SJanosch Frank break; 222829b40f10SJanosch Frank 222929b40f10SJanosch Frank /* 223029b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 223129b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 223229b40f10SJanosch Frank */ 223329b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 223429b40f10SJanosch Frank if (r) 223529b40f10SJanosch Frank break; 223629b40f10SJanosch Frank 2237fa0c5eabSJanosch Frank down_write(¤t->mm->mmap_sem); 2238fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2239fa0c5eabSJanosch Frank up_write(¤t->mm->mmap_sem); 2240fa0c5eabSJanosch Frank if (r) 2241fa0c5eabSJanosch Frank break; 2242fa0c5eabSJanosch Frank 224329b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 224429b40f10SJanosch Frank if (r) 224529b40f10SJanosch Frank break; 224629b40f10SJanosch Frank 224729b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 224829b40f10SJanosch Frank if (r) 224929b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 22500890ddeaSChristian Borntraeger 22510890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 22520890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 225329b40f10SJanosch Frank break; 225429b40f10SJanosch Frank } 225529b40f10SJanosch Frank case KVM_PV_DISABLE: { 225629b40f10SJanosch Frank r = -EINVAL; 225729b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 225829b40f10SJanosch Frank break; 225929b40f10SJanosch Frank 226029b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 226129b40f10SJanosch Frank /* 226229b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 226329b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 226429b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 226529b40f10SJanosch Frank */ 226629b40f10SJanosch Frank if (r) 226729b40f10SJanosch Frank break; 226829b40f10SJanosch Frank r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc); 22690890ddeaSChristian Borntraeger 22700890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 22710890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 227229b40f10SJanosch Frank break; 227329b40f10SJanosch Frank } 227429b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 227529b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 227629b40f10SJanosch Frank void *hdr; 227729b40f10SJanosch Frank 227829b40f10SJanosch Frank r = -EINVAL; 227929b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 228029b40f10SJanosch Frank break; 228129b40f10SJanosch Frank 228229b40f10SJanosch Frank r = -EFAULT; 228329b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 228429b40f10SJanosch Frank break; 228529b40f10SJanosch Frank 228629b40f10SJanosch Frank /* Currently restricted to 8KB */ 228729b40f10SJanosch Frank r = -EINVAL; 228829b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 228929b40f10SJanosch Frank break; 229029b40f10SJanosch Frank 229129b40f10SJanosch Frank r = -ENOMEM; 229229b40f10SJanosch Frank hdr = vmalloc(parms.length); 229329b40f10SJanosch Frank if (!hdr) 229429b40f10SJanosch Frank break; 229529b40f10SJanosch Frank 229629b40f10SJanosch Frank r = -EFAULT; 229729b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 229829b40f10SJanosch Frank parms.length)) 229929b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 230029b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 230129b40f10SJanosch Frank 230229b40f10SJanosch Frank vfree(hdr); 230329b40f10SJanosch Frank break; 230429b40f10SJanosch Frank } 230529b40f10SJanosch Frank case KVM_PV_UNPACK: { 230629b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 230729b40f10SJanosch Frank 230829b40f10SJanosch Frank r = -EINVAL; 230929b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 231029b40f10SJanosch Frank break; 231129b40f10SJanosch Frank 231229b40f10SJanosch Frank r = -EFAULT; 231329b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 231429b40f10SJanosch Frank break; 231529b40f10SJanosch Frank 231629b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 231729b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 231829b40f10SJanosch Frank break; 231929b40f10SJanosch Frank } 232029b40f10SJanosch Frank case KVM_PV_VERIFY: { 232129b40f10SJanosch Frank r = -EINVAL; 232229b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 232329b40f10SJanosch Frank break; 232429b40f10SJanosch Frank 232529b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 232629b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 232729b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 232829b40f10SJanosch Frank cmd->rrc); 232929b40f10SJanosch Frank break; 233029b40f10SJanosch Frank } 233129b40f10SJanosch Frank default: 233229b40f10SJanosch Frank r = -ENOTTY; 233329b40f10SJanosch Frank } 233429b40f10SJanosch Frank return r; 233529b40f10SJanosch Frank } 233629b40f10SJanosch Frank 2337b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2338b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2339b0c632dbSHeiko Carstens { 2340b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2341b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2342f2061656SDominik Dingel struct kvm_device_attr attr; 2343b0c632dbSHeiko Carstens int r; 2344b0c632dbSHeiko Carstens 2345b0c632dbSHeiko Carstens switch (ioctl) { 2346ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2347ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2348ba5c1e9bSCarsten Otte 2349ba5c1e9bSCarsten Otte r = -EFAULT; 2350ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2351ba5c1e9bSCarsten Otte break; 2352ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2353ba5c1e9bSCarsten Otte break; 2354ba5c1e9bSCarsten Otte } 235584223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 235684223598SCornelia Huck struct kvm_irq_routing_entry routing; 235784223598SCornelia Huck 235884223598SCornelia Huck r = -EINVAL; 235984223598SCornelia Huck if (kvm->arch.use_irqchip) { 236084223598SCornelia Huck /* Set up dummy routing. */ 236184223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2362152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 236384223598SCornelia Huck } 236484223598SCornelia Huck break; 236584223598SCornelia Huck } 2366f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2367f2061656SDominik Dingel r = -EFAULT; 2368f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2369f2061656SDominik Dingel break; 2370f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2371f2061656SDominik Dingel break; 2372f2061656SDominik Dingel } 2373f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2374f2061656SDominik Dingel r = -EFAULT; 2375f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2376f2061656SDominik Dingel break; 2377f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2378f2061656SDominik Dingel break; 2379f2061656SDominik Dingel } 2380f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2381f2061656SDominik Dingel r = -EFAULT; 2382f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2383f2061656SDominik Dingel break; 2384f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2385f2061656SDominik Dingel break; 2386f2061656SDominik Dingel } 238730ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 238830ee2a98SJason J. Herne struct kvm_s390_skeys args; 238930ee2a98SJason J. Herne 239030ee2a98SJason J. Herne r = -EFAULT; 239130ee2a98SJason J. Herne if (copy_from_user(&args, argp, 239230ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 239330ee2a98SJason J. Herne break; 239430ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 239530ee2a98SJason J. Herne break; 239630ee2a98SJason J. Herne } 239730ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 239830ee2a98SJason J. Herne struct kvm_s390_skeys args; 239930ee2a98SJason J. Herne 240030ee2a98SJason J. Herne r = -EFAULT; 240130ee2a98SJason J. Herne if (copy_from_user(&args, argp, 240230ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 240330ee2a98SJason J. Herne break; 240430ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 240530ee2a98SJason J. Herne break; 240630ee2a98SJason J. Herne } 24074036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 24084036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24094036e387SClaudio Imbrenda 24104036e387SClaudio Imbrenda r = -EFAULT; 24114036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24124036e387SClaudio Imbrenda break; 24131de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24144036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 24151de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24164036e387SClaudio Imbrenda if (!r) { 24174036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 24184036e387SClaudio Imbrenda if (r) 24194036e387SClaudio Imbrenda r = -EFAULT; 24204036e387SClaudio Imbrenda } 24214036e387SClaudio Imbrenda break; 24224036e387SClaudio Imbrenda } 24234036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 24244036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24254036e387SClaudio Imbrenda 24264036e387SClaudio Imbrenda r = -EFAULT; 24274036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24284036e387SClaudio Imbrenda break; 24291de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24304036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 24311de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24324036e387SClaudio Imbrenda break; 24334036e387SClaudio Imbrenda } 243429b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 243529b40f10SJanosch Frank struct kvm_pv_cmd args; 243629b40f10SJanosch Frank 243729b40f10SJanosch Frank r = 0; 243829b40f10SJanosch Frank if (!is_prot_virt_host()) { 243929b40f10SJanosch Frank r = -EINVAL; 244029b40f10SJanosch Frank break; 244129b40f10SJanosch Frank } 244229b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 244329b40f10SJanosch Frank r = -EFAULT; 244429b40f10SJanosch Frank break; 244529b40f10SJanosch Frank } 244629b40f10SJanosch Frank if (args.flags) { 244729b40f10SJanosch Frank r = -EINVAL; 244829b40f10SJanosch Frank break; 244929b40f10SJanosch Frank } 245029b40f10SJanosch Frank mutex_lock(&kvm->lock); 245129b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 245229b40f10SJanosch Frank mutex_unlock(&kvm->lock); 245329b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 245429b40f10SJanosch Frank r = -EFAULT; 245529b40f10SJanosch Frank break; 245629b40f10SJanosch Frank } 245729b40f10SJanosch Frank break; 245829b40f10SJanosch Frank } 2459b0c632dbSHeiko Carstens default: 2460367e1319SAvi Kivity r = -ENOTTY; 2461b0c632dbSHeiko Carstens } 2462b0c632dbSHeiko Carstens 2463b0c632dbSHeiko Carstens return r; 2464b0c632dbSHeiko Carstens } 2465b0c632dbSHeiko Carstens 246645c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 246745c9b47cSTony Krowiak { 2468e585b24aSTony Krowiak struct ap_config_info info; 246945c9b47cSTony Krowiak 2470e585b24aSTony Krowiak if (ap_instructions_available()) { 2471e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2472e585b24aSTony Krowiak return info.apxa; 247345c9b47cSTony Krowiak } 247445c9b47cSTony Krowiak 247545c9b47cSTony Krowiak return 0; 247645c9b47cSTony Krowiak } 247745c9b47cSTony Krowiak 2478e585b24aSTony Krowiak /* 2479e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2480e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2481e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2482e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2483e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2484e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2485e585b24aSTony Krowiak */ 248645c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 248745c9b47cSTony Krowiak { 248845c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 248945c9b47cSTony Krowiak 2490e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2491e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2492e585b24aSTony Krowiak 2493e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2494e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2495e585b24aSTony Krowiak return; 2496e585b24aSTony Krowiak 249745c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 249845c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 249945c9b47cSTony Krowiak else 250045c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 250145c9b47cSTony Krowiak } 250245c9b47cSTony Krowiak 25030e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 25040e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 25050e237e44SPierre Morel { 25060e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 25070e237e44SPierre Morel 25080e237e44SPierre Morel mutex_lock(&kvm->lock); 25090e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 25100e237e44SPierre Morel 25110e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 25120e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 25130e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 25140e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 25150e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 25160e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 25170e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 25180e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 25190e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 25200e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 25210e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 25220e237e44SPierre Morel break; 25230e237e44SPierre Morel case CRYCB_FORMAT1: 25240e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 25250e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 25260e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 25270e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 25280e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 25290e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 25300e237e44SPierre Morel *((unsigned short *)adm)); 25310e237e44SPierre Morel break; 25320e237e44SPierre Morel default: /* Can not happen */ 25330e237e44SPierre Morel break; 25340e237e44SPierre Morel } 25350e237e44SPierre Morel 25360e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 25370e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 25380e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 25390e237e44SPierre Morel mutex_unlock(&kvm->lock); 25400e237e44SPierre Morel } 25410e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 25420e237e44SPierre Morel 254342104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 254442104598STony Krowiak { 254542104598STony Krowiak mutex_lock(&kvm->lock); 254642104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 254742104598STony Krowiak 254842104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 254942104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 255042104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 255142104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 255242104598STony Krowiak 25530e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 25546cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 25556cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 255642104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 255742104598STony Krowiak mutex_unlock(&kvm->lock); 255842104598STony Krowiak } 255942104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 256042104598STony Krowiak 25619bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 25629d8d5786SMichael Mueller { 25639bb0ec09SDavid Hildenbrand struct cpuid cpuid; 25649bb0ec09SDavid Hildenbrand 25659bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 25669bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 25679bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 25689d8d5786SMichael Mueller } 25699d8d5786SMichael Mueller 2570c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 25715102ee87STony Krowiak { 2572c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 257345c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 25745102ee87STony Krowiak 2575e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2576e585b24aSTony Krowiak return; 2577e585b24aSTony Krowiak 2578ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2579ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2580ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2581ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2582ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2583ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2584ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 25855102ee87STony Krowiak } 25865102ee87STony Krowiak 25877d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 25887d43bafcSEugene (jno) Dvurechenski { 25897d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 25905e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 25917d43bafcSEugene (jno) Dvurechenski else 25927d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 25937d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 25947d43bafcSEugene (jno) Dvurechenski } 25957d43bafcSEugene (jno) Dvurechenski 2596e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2597b0c632dbSHeiko Carstens { 259876a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 25999d8d5786SMichael Mueller int i, rc; 2600b0c632dbSHeiko Carstens char debug_name[16]; 2601f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2602b0c632dbSHeiko Carstens 2603e08b9637SCarsten Otte rc = -EINVAL; 2604e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2605e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2606e08b9637SCarsten Otte goto out_err; 2607e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2608e08b9637SCarsten Otte goto out_err; 2609e08b9637SCarsten Otte #else 2610e08b9637SCarsten Otte if (type) 2611e08b9637SCarsten Otte goto out_err; 2612e08b9637SCarsten Otte #endif 2613e08b9637SCarsten Otte 2614b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2615b0c632dbSHeiko Carstens if (rc) 2616d89f5effSJan Kiszka goto out_err; 2617b0c632dbSHeiko Carstens 2618b290411aSCarsten Otte rc = -ENOMEM; 2619b290411aSCarsten Otte 262076a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 262176a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 26225e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 26239ac96d75SDavid Hildenbrand /* start with basic SCA */ 262476a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2625b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2626d89f5effSJan Kiszka goto out_err; 26270d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 2628c5c2c393SDavid Hildenbrand sca_offset += 16; 2629bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2630c5c2c393SDavid Hildenbrand sca_offset = 0; 2631bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2632bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 26330d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 2634b0c632dbSHeiko Carstens 2635b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2636b0c632dbSHeiko Carstens 26371cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2638b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 263940f5b735SDominik Dingel goto out_err; 2640b0c632dbSHeiko Carstens 264119114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2642c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2643c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 2644c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 264540f5b735SDominik Dingel goto out_err; 26469d8d5786SMichael Mueller 264725c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2648c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2649c3b9e3e1SChristian Borntraeger 2650c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 2651c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] & 2652c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2653c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 2654c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] & 2655c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2656c3b9e3e1SChristian Borntraeger } 2657346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2658981467c9SMichael Mueller 26591935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 26601935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 26611935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 26621935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 266395ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 266495ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 26651bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 26661bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 26671bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 26681bab1c02SClaudio Imbrenda } 266995ca2cb5SJanosch Frank 267005f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 267105f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 267205f31e3bSPierre Morel 26739bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 267437c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 26759d8d5786SMichael Mueller 2676c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 26775102ee87STony Krowiak 267851978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2679ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 26806d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 26816d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 26828a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2683a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2684ba5c1e9bSCarsten Otte 2685b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 268678f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2687b0c632dbSHeiko Carstens 2688e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2689e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2690a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2691e08b9637SCarsten Otte } else { 269232e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2693ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 269432e6b236SGuenther Hutzl else 2695ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 269632e6b236SGuenther Hutzl sclp.hamax + 1); 26976ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2698598841caSCarsten Otte if (!kvm->arch.gmap) 269940f5b735SDominik Dingel goto out_err; 27002c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 270124eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2702e08b9637SCarsten Otte } 2703fa6b7fe9SCornelia Huck 2704c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 270555531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 27068ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2707a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2708d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 27098335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 27108ad35755SDavid Hildenbrand 2711d89f5effSJan Kiszka return 0; 2712d89f5effSJan Kiszka out_err: 2713c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 271440f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 27157d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 271678f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2717d89f5effSJan Kiszka return rc; 2718b0c632dbSHeiko Carstens } 2719b0c632dbSHeiko Carstens 2720d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2721d329c035SChristian Borntraeger { 272229b40f10SJanosch Frank u16 rc, rrc; 272329b40f10SJanosch Frank 2724d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2725ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 272667335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 27273c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2728bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2729a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 273027e0393fSCarsten Otte 273127e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 27326ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 273327e0393fSCarsten Otte 2734e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2735b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 273629b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 273729b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 273829b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 2739d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2740d329c035SChristian Borntraeger } 2741d329c035SChristian Borntraeger 2742d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 2743d329c035SChristian Borntraeger { 2744d329c035SChristian Borntraeger unsigned int i; 2745988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 2746d329c035SChristian Borntraeger 2747988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 27484543bdc0SSean Christopherson kvm_vcpu_destroy(vcpu); 2749988a2caeSGleb Natapov 2750988a2caeSGleb Natapov mutex_lock(&kvm->lock); 2751988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 2752d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 2753988a2caeSGleb Natapov 2754988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 2755988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 2756d329c035SChristian Borntraeger } 2757d329c035SChristian Borntraeger 2758b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2759b0c632dbSHeiko Carstens { 276029b40f10SJanosch Frank u16 rc, rrc; 276129b40f10SJanosch Frank 2762d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 27637d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2764d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 276529b40f10SJanosch Frank /* 276629b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 276729b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 276829b40f10SJanosch Frank * can mess with the pv state. To avoid lockdep_assert_held from 276929b40f10SJanosch Frank * complaining we do not use kvm_s390_pv_is_protected. 277029b40f10SJanosch Frank */ 277129b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 277229b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); 277329b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 2774c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 277527e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 27766ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2777841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 277867335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2779a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 27808335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2781b0c632dbSHeiko Carstens } 2782b0c632dbSHeiko Carstens 2783b0c632dbSHeiko Carstens /* Section: vcpu related */ 2784dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2785b0c632dbSHeiko Carstens { 27866ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 278727e0393fSCarsten Otte if (!vcpu->arch.gmap) 278827e0393fSCarsten Otte return -ENOMEM; 27892c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2790dafd032aSDominik Dingel 279127e0393fSCarsten Otte return 0; 279227e0393fSCarsten Otte } 279327e0393fSCarsten Otte 2794a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2795a6e2f683SEugene (jno) Dvurechenski { 2796a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2797a6940674SDavid Hildenbrand return; 27985e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 27997d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 28007d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 28017d43bafcSEugene (jno) Dvurechenski 28027d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28037d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 28047d43bafcSEugene (jno) Dvurechenski } else { 2805bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2806a6e2f683SEugene (jno) Dvurechenski 2807a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2808a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2809a6e2f683SEugene (jno) Dvurechenski } 28105e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 28117d43bafcSEugene (jno) Dvurechenski } 2812a6e2f683SEugene (jno) Dvurechenski 2813eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2814a6e2f683SEugene (jno) Dvurechenski { 2815a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2816a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2817a6940674SDavid Hildenbrand 2818a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2819a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2820a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2821f07afa04SDavid Hildenbrand return; 2822a6940674SDavid Hildenbrand } 2823eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2824eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2825eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 28267d43bafcSEugene (jno) Dvurechenski 2827eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 28287d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 28297d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 28300c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2831eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28327d43bafcSEugene (jno) Dvurechenski } else { 2833eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2834a6e2f683SEugene (jno) Dvurechenski 2835eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2836a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2837a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2838eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2839a6e2f683SEugene (jno) Dvurechenski } 2840eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 28415e044315SEugene (jno) Dvurechenski } 28425e044315SEugene (jno) Dvurechenski 28435e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 28445e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 28455e044315SEugene (jno) Dvurechenski { 28465e044315SEugene (jno) Dvurechenski d->sda = s->sda; 28475e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 28485e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 28495e044315SEugene (jno) Dvurechenski } 28505e044315SEugene (jno) Dvurechenski 28515e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 28525e044315SEugene (jno) Dvurechenski { 28535e044315SEugene (jno) Dvurechenski int i; 28545e044315SEugene (jno) Dvurechenski 28555e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 28565e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 28575e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 28585e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 28595e044315SEugene (jno) Dvurechenski } 28605e044315SEugene (jno) Dvurechenski 28615e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 28625e044315SEugene (jno) Dvurechenski { 28635e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 28645e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 28655e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 28665e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 28675e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 28685e044315SEugene (jno) Dvurechenski 286929b40f10SJanosch Frank if (kvm->arch.use_esca) 287029b40f10SJanosch Frank return 0; 287129b40f10SJanosch Frank 28725e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 28735e044315SEugene (jno) Dvurechenski if (!new_sca) 28745e044315SEugene (jno) Dvurechenski return -ENOMEM; 28755e044315SEugene (jno) Dvurechenski 28765e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 28775e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 28785e044315SEugene (jno) Dvurechenski 28795e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 28805e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 28815e044315SEugene (jno) Dvurechenski 28825e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 28835e044315SEugene (jno) Dvurechenski 28845e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 28855e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 28865e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 28870c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 28885e044315SEugene (jno) Dvurechenski } 28895e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 28905e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 28915e044315SEugene (jno) Dvurechenski 28925e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 28935e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 28945e044315SEugene (jno) Dvurechenski 28955e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 28965e044315SEugene (jno) Dvurechenski 28978335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 28988335713aSChristian Borntraeger old_sca, kvm->arch.sca); 28995e044315SEugene (jno) Dvurechenski return 0; 29007d43bafcSEugene (jno) Dvurechenski } 2901a6e2f683SEugene (jno) Dvurechenski 2902a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2903a6e2f683SEugene (jno) Dvurechenski { 29045e044315SEugene (jno) Dvurechenski int rc; 29055e044315SEugene (jno) Dvurechenski 2906a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2907a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2908a6940674SDavid Hildenbrand return true; 2909a6940674SDavid Hildenbrand return false; 2910a6940674SDavid Hildenbrand } 29115e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 29125e044315SEugene (jno) Dvurechenski return true; 291376a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 29145e044315SEugene (jno) Dvurechenski return false; 29155e044315SEugene (jno) Dvurechenski 29165e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 29175e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 29185e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 29195e044315SEugene (jno) Dvurechenski 29205e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2921a6e2f683SEugene (jno) Dvurechenski } 2922a6e2f683SEugene (jno) Dvurechenski 2923db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2924db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2925db0758b2SDavid Hildenbrand { 2926db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 29279c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2928db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 29299c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2930db0758b2SDavid Hildenbrand } 2931db0758b2SDavid Hildenbrand 2932db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2933db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2934db0758b2SDavid Hildenbrand { 2935db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 29369c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2937db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 2938db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 29399c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2940db0758b2SDavid Hildenbrand } 2941db0758b2SDavid Hildenbrand 2942db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2943db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2944db0758b2SDavid Hildenbrand { 2945db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 2946db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 2947db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 2948db0758b2SDavid Hildenbrand } 2949db0758b2SDavid Hildenbrand 2950db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2951db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2952db0758b2SDavid Hildenbrand { 2953db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 2954db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2955db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 2956db0758b2SDavid Hildenbrand } 2957db0758b2SDavid Hildenbrand 2958db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2959db0758b2SDavid Hildenbrand { 2960db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2961db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 2962db0758b2SDavid Hildenbrand preempt_enable(); 2963db0758b2SDavid Hildenbrand } 2964db0758b2SDavid Hildenbrand 2965db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2966db0758b2SDavid Hildenbrand { 2967db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2968db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 2969db0758b2SDavid Hildenbrand preempt_enable(); 2970db0758b2SDavid Hildenbrand } 2971db0758b2SDavid Hildenbrand 29724287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 29734287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 29744287f247SDavid Hildenbrand { 2975db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 29769c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2977db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 2978db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 29794287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 29809c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2981db0758b2SDavid Hildenbrand preempt_enable(); 29824287f247SDavid Hildenbrand } 29834287f247SDavid Hildenbrand 2984db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 29854287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 29864287f247SDavid Hildenbrand { 29879c23a131SDavid Hildenbrand unsigned int seq; 2988db0758b2SDavid Hildenbrand __u64 value; 2989db0758b2SDavid Hildenbrand 2990db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 29914287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 2992db0758b2SDavid Hildenbrand 29939c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 29949c23a131SDavid Hildenbrand do { 29959c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 29969c23a131SDavid Hildenbrand /* 29979c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 29989c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 29999c23a131SDavid Hildenbrand */ 30009c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3001db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 30029c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 30039c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3004db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 30059c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 30069c23a131SDavid Hildenbrand preempt_enable(); 3007db0758b2SDavid Hildenbrand return value; 30084287f247SDavid Hildenbrand } 30094287f247SDavid Hildenbrand 3010b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3011b0c632dbSHeiko Carstens { 30129977e886SHendrik Brueckner 301337d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3014ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 30155ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3016db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 301701a745acSDavid Hildenbrand vcpu->cpu = cpu; 3018b0c632dbSHeiko Carstens } 3019b0c632dbSHeiko Carstens 3020b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3021b0c632dbSHeiko Carstens { 302201a745acSDavid Hildenbrand vcpu->cpu = -1; 30235ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3024db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 30259daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 302637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 302737d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 30289977e886SHendrik Brueckner 3029b0c632dbSHeiko Carstens } 3030b0c632dbSHeiko Carstens 303131928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 303242897d86SMarcelo Tosatti { 303372f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3034fdf03650SFan Zhang preempt_disable(); 303572f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3036d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3037fdf03650SFan Zhang preempt_enable(); 303872f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 303925508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3040dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3041eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 304225508824SDavid Hildenbrand } 30436502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 30446502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 304537d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 304637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 304742897d86SMarcelo Tosatti } 304842897d86SMarcelo Tosatti 30498ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 30508ec2fa52SChristian Borntraeger { 30518ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 30528ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 30538ec2fa52SChristian Borntraeger return true; 30548ec2fa52SChristian Borntraeger return false; 30558ec2fa52SChristian Borntraeger } 30568ec2fa52SChristian Borntraeger 30578ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 30588ec2fa52SChristian Borntraeger { 30598ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 30608ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 30618ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 30628ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 30638ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 30648ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 30658ec2fa52SChristian Borntraeger 30668ec2fa52SChristian Borntraeger } 30678ec2fa52SChristian Borntraeger 30685102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 30695102ee87STony Krowiak { 3070e585b24aSTony Krowiak /* 3071e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3072e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3073e585b24aSTony Krowiak */ 3074e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 30755102ee87STony Krowiak return; 30765102ee87STony Krowiak 3077e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3078a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 307937940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 30808ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3081a374e892STony Krowiak 3082e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3083e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3084e585b24aSTony Krowiak 3085e585b24aSTony Krowiak /* Set up protected key support */ 30868ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3087a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 30888ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 30898ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 30908ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 30918ec2fa52SChristian Borntraeger } 30928ec2fa52SChristian Borntraeger 3093a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3094a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 30955102ee87STony Krowiak } 30965102ee87STony Krowiak 3097b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3098b31605c1SDominik Dingel { 3099b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 3100b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3101b31605c1SDominik Dingel } 3102b31605c1SDominik Dingel 3103b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3104b31605c1SDominik Dingel { 3105b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 3106b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 3107b31605c1SDominik Dingel return -ENOMEM; 3108b31605c1SDominik Dingel return 0; 3109b31605c1SDominik Dingel } 3110b31605c1SDominik Dingel 311191520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 311291520f1aSMichael Mueller { 311391520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 311491520f1aSMichael Mueller 311591520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 311680bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3117c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 311891520f1aSMichael Mueller } 311991520f1aSMichael Mueller 3120ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3121ff72bb55SSean Christopherson { 3122b31605c1SDominik Dingel int rc = 0; 312329b40f10SJanosch Frank u16 uvrc, uvrrc; 3124b31288faSKonstantin Weitz 31259e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 31269e6dabefSCornelia Huck CPUSTAT_SM | 3127a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3128a4a4f191SGuenther Hutzl 312953df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3130ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 313153df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3132ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3133a4a4f191SGuenther Hutzl 313491520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 313591520f1aSMichael Mueller 3136bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3137bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 31380c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3139bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 31400c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 3141f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 31420c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 31437feb6bb8SMichael Mueller 3144c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 31450c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3146cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 31470c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 31480c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 314948ee7d3aSDavid Hildenbrand if (sclp.has_cei) 31500c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 315111ad65b7SDavid Hildenbrand if (sclp.has_ib) 31520c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 315337c5f6c8SDavid Hildenbrand if (sclp.has_siif) 31540c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 315537c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 31560c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 315718280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 31580c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 31590c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 316013211ea7SEric Farman } 31618fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 31628fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3163a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3164a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3165d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3166d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3167d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3168d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3169d7c5cb01SMichael Mueller } 31704e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 31714e0b1ab7SFan Zhang | SDNXC; 3172c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 3173730cd632SFarhan Ali 3174730cd632SFarhan Ali if (sclp.has_kss) 3175ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3176730cd632SFarhan Ali else 3177492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 31785a5e6536SMatthew Rosato 3179e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3180b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3181b31605c1SDominik Dingel if (rc) 3182b31605c1SDominik Dingel return rc; 3183b31288faSKonstantin Weitz } 31840ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3185ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 31869d8d5786SMichael Mueller 318767d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 318867d49d52SCollin Walling 31895102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 31905102ee87STony Krowiak 319129b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 319229b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 319329b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 319429b40f10SJanosch Frank if (rc) 319529b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 319629b40f10SJanosch Frank } 319729b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 319829b40f10SJanosch Frank 3199b31605c1SDominik Dingel return rc; 3200b0c632dbSHeiko Carstens } 3201b0c632dbSHeiko Carstens 3202897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3203897cc38eSSean Christopherson { 3204897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3205897cc38eSSean Christopherson return -EINVAL; 3206897cc38eSSean Christopherson return 0; 3207897cc38eSSean Christopherson } 3208897cc38eSSean Christopherson 3209e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3210b0c632dbSHeiko Carstens { 32117feb6bb8SMichael Mueller struct sie_page *sie_page; 3212897cc38eSSean Christopherson int rc; 32134d47555aSCarsten Otte 3214da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 32157feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 32167feb6bb8SMichael Mueller if (!sie_page) 3217e529ef66SSean Christopherson return -ENOMEM; 3218b0c632dbSHeiko Carstens 32197feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 32207feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 32217feb6bb8SMichael Mueller 3222efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3223efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3224efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3225efed1104SDavid Hildenbrand 3226e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3227ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3228e529ef66SSean Christopherson vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin; 32294b9f9525SMichael Mueller if (vcpu->arch.sie_block->gd && sclp.has_gisaf) 32304b9f9525SMichael Mueller vcpu->arch.sie_block->gd |= GISA_FORMAT1; 32319c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3232ba5c1e9bSCarsten Otte 3233321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3234321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3235321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3236321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3237321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3238321f8ee5SSean Christopherson KVM_SYNC_CRS | 3239321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 3240321f8ee5SSean Christopherson KVM_SYNC_PFAULT; 3241321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3242321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3243321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3244321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3245321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3246321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3247321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3248321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3249321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3250321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3251321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3252321f8ee5SSean Christopherson */ 3253321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3254321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3255321f8ee5SSean Christopherson else 3256321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3257321f8ee5SSean Christopherson 3258321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3259321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3260321f8ee5SSean Christopherson if (rc) 3261a2017f17SSean Christopherson goto out_free_sie_block; 3262321f8ee5SSean Christopherson } 3263321f8ee5SSean Christopherson 3264e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3265e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3266e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3267b0c632dbSHeiko Carstens 3268ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3269ff72bb55SSean Christopherson if (rc) 3270ff72bb55SSean Christopherson goto out_ucontrol_uninit; 3271e529ef66SSean Christopherson return 0; 3272e529ef66SSean Christopherson 3273ff72bb55SSean Christopherson out_ucontrol_uninit: 3274ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3275ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 32767b06bf2fSWei Yongjun out_free_sie_block: 32777b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3278e529ef66SSean Christopherson return rc; 3279b0c632dbSHeiko Carstens } 3280b0c632dbSHeiko Carstens 3281b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3282b0c632dbSHeiko Carstens { 32839a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3284b0c632dbSHeiko Carstens } 3285b0c632dbSHeiko Carstens 3286199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3287199b5763SLongpeng(Mike) { 32880546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3289199b5763SLongpeng(Mike) } 3290199b5763SLongpeng(Mike) 329127406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 329249b99e1eSChristian Borntraeger { 3293805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 329461a6df54SDavid Hildenbrand exit_sie(vcpu); 329549b99e1eSChristian Borntraeger } 329649b99e1eSChristian Borntraeger 329727406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 329849b99e1eSChristian Borntraeger { 3299805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 330049b99e1eSChristian Borntraeger } 330149b99e1eSChristian Borntraeger 33028e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 33038e236546SChristian Borntraeger { 3304805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 330561a6df54SDavid Hildenbrand exit_sie(vcpu); 33068e236546SChristian Borntraeger } 33078e236546SChristian Borntraeger 33089ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 33099ea59728SDavid Hildenbrand { 33109ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 33119ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 33129ea59728SDavid Hildenbrand } 33139ea59728SDavid Hildenbrand 33148e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 33158e236546SChristian Borntraeger { 33169bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 33178e236546SChristian Borntraeger } 33188e236546SChristian Borntraeger 331949b99e1eSChristian Borntraeger /* 33209ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 332149b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 332249b99e1eSChristian Borntraeger * return immediately. */ 332349b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 332449b99e1eSChristian Borntraeger { 3325ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 33269ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 332749b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 332849b99e1eSChristian Borntraeger cpu_relax(); 332949b99e1eSChristian Borntraeger } 333049b99e1eSChristian Borntraeger 33318e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 33328e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 333349b99e1eSChristian Borntraeger { 33348e236546SChristian Borntraeger kvm_make_request(req, vcpu); 33358e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 333649b99e1eSChristian Borntraeger } 333749b99e1eSChristian Borntraeger 3338414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3339414d3b07SMartin Schwidefsky unsigned long end) 33402c70fe44SChristian Borntraeger { 33412c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 33422c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3343414d3b07SMartin Schwidefsky unsigned long prefix; 3344414d3b07SMartin Schwidefsky int i; 33452c70fe44SChristian Borntraeger 334665d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 334765d0b0d4SDavid Hildenbrand return; 3348414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3349414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3350414d3b07SMartin Schwidefsky return; 33512c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 33522c70fe44SChristian Borntraeger /* match against both prefix pages */ 3353414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3354414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3355414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3356414d3b07SMartin Schwidefsky start, end); 33578e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 33582c70fe44SChristian Borntraeger } 33592c70fe44SChristian Borntraeger } 33602c70fe44SChristian Borntraeger } 33612c70fe44SChristian Borntraeger 33628b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 33638b905d28SChristian Borntraeger { 33648b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 33658b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 33668b905d28SChristian Borntraeger halt_poll_max_steal) { 33678b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 33688b905d28SChristian Borntraeger return true; 33698b905d28SChristian Borntraeger } 33708b905d28SChristian Borntraeger return false; 33718b905d28SChristian Borntraeger } 33728b905d28SChristian Borntraeger 3373b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3374b6d33834SChristoffer Dall { 3375b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3376b6d33834SChristoffer Dall BUG(); 3377b6d33834SChristoffer Dall return 0; 3378b6d33834SChristoffer Dall } 3379b6d33834SChristoffer Dall 338014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 338114eebd91SCarsten Otte struct kvm_one_reg *reg) 338214eebd91SCarsten Otte { 338314eebd91SCarsten Otte int r = -EINVAL; 338414eebd91SCarsten Otte 338514eebd91SCarsten Otte switch (reg->id) { 338629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 338729b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 338829b7c71bSCarsten Otte (u32 __user *)reg->addr); 338929b7c71bSCarsten Otte break; 339029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 339129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 339229b7c71bSCarsten Otte (u64 __user *)reg->addr); 339329b7c71bSCarsten Otte break; 339446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 33954287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 339646a6dd1cSJason J. herne (u64 __user *)reg->addr); 339746a6dd1cSJason J. herne break; 339846a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 339946a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 340046a6dd1cSJason J. herne (u64 __user *)reg->addr); 340146a6dd1cSJason J. herne break; 3402536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3403536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3404536336c2SDominik Dingel (u64 __user *)reg->addr); 3405536336c2SDominik Dingel break; 3406536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3407536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3408536336c2SDominik Dingel (u64 __user *)reg->addr); 3409536336c2SDominik Dingel break; 3410536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3411536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3412536336c2SDominik Dingel (u64 __user *)reg->addr); 3413536336c2SDominik Dingel break; 3414672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3415672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3416672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3417672550fbSChristian Borntraeger break; 3418afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3419afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3420afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3421afa45ff5SChristian Borntraeger break; 342214eebd91SCarsten Otte default: 342314eebd91SCarsten Otte break; 342414eebd91SCarsten Otte } 342514eebd91SCarsten Otte 342614eebd91SCarsten Otte return r; 342714eebd91SCarsten Otte } 342814eebd91SCarsten Otte 342914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 343014eebd91SCarsten Otte struct kvm_one_reg *reg) 343114eebd91SCarsten Otte { 343214eebd91SCarsten Otte int r = -EINVAL; 34334287f247SDavid Hildenbrand __u64 val; 343414eebd91SCarsten Otte 343514eebd91SCarsten Otte switch (reg->id) { 343629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 343729b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 343829b7c71bSCarsten Otte (u32 __user *)reg->addr); 343929b7c71bSCarsten Otte break; 344029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 344129b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 344229b7c71bSCarsten Otte (u64 __user *)reg->addr); 344329b7c71bSCarsten Otte break; 344446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 34454287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 34464287f247SDavid Hildenbrand if (!r) 34474287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 344846a6dd1cSJason J. herne break; 344946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 345046a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 345146a6dd1cSJason J. herne (u64 __user *)reg->addr); 345246a6dd1cSJason J. herne break; 3453536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3454536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3455536336c2SDominik Dingel (u64 __user *)reg->addr); 34569fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 34579fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3458536336c2SDominik Dingel break; 3459536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3460536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3461536336c2SDominik Dingel (u64 __user *)reg->addr); 3462536336c2SDominik Dingel break; 3463536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3464536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3465536336c2SDominik Dingel (u64 __user *)reg->addr); 3466536336c2SDominik Dingel break; 3467672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3468672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3469672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3470672550fbSChristian Borntraeger break; 3471afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3472afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3473afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3474afa45ff5SChristian Borntraeger break; 347514eebd91SCarsten Otte default: 347614eebd91SCarsten Otte break; 347714eebd91SCarsten Otte } 347814eebd91SCarsten Otte 347914eebd91SCarsten Otte return r; 348014eebd91SCarsten Otte } 3481b6d33834SChristoffer Dall 34827de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 3483b0c632dbSHeiko Carstens { 34847de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 34857de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 34867de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 34877de3f142SJanosch Frank 34887de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 34897de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 34907de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 34917de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 34927de3f142SJanosch Frank } 34937de3f142SJanosch Frank 34947de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 34957de3f142SJanosch Frank { 34967de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 34977de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 34987de3f142SJanosch Frank 34997de3f142SJanosch Frank /* this equals initial cpu reset in pop, but we don't switch to ESA */ 35007de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 35017de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 35027de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 35037de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 35047de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 35057de3f142SJanosch Frank vcpu->arch.sie_block->todpr = 0; 35067de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 35077de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 35087de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 35097de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 35107de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 35117de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 35127de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 35137de3f142SJanosch Frank } 35147de3f142SJanosch Frank 35157de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 35167de3f142SJanosch Frank { 35177de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 35187de3f142SJanosch Frank 35197de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 35207de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 35217de3f142SJanosch Frank 35227de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 35237de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 35247de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 35257de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 35267de3f142SJanosch Frank 35277de3f142SJanosch Frank regs->etoken = 0; 35287de3f142SJanosch Frank regs->etoken_extension = 0; 3529b0c632dbSHeiko Carstens } 3530b0c632dbSHeiko Carstens 3531b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3532b0c632dbSHeiko Carstens { 3533875656feSChristoffer Dall vcpu_load(vcpu); 35345a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3535875656feSChristoffer Dall vcpu_put(vcpu); 3536b0c632dbSHeiko Carstens return 0; 3537b0c632dbSHeiko Carstens } 3538b0c632dbSHeiko Carstens 3539b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3540b0c632dbSHeiko Carstens { 35411fc9b76bSChristoffer Dall vcpu_load(vcpu); 35425a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 35431fc9b76bSChristoffer Dall vcpu_put(vcpu); 3544b0c632dbSHeiko Carstens return 0; 3545b0c632dbSHeiko Carstens } 3546b0c632dbSHeiko Carstens 3547b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3548b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3549b0c632dbSHeiko Carstens { 3550b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3551b4ef9d4eSChristoffer Dall 355259674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3553b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3554b4ef9d4eSChristoffer Dall 3555b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3556b0c632dbSHeiko Carstens return 0; 3557b0c632dbSHeiko Carstens } 3558b0c632dbSHeiko Carstens 3559b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3560b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3561b0c632dbSHeiko Carstens { 3562bcdec41cSChristoffer Dall vcpu_load(vcpu); 3563bcdec41cSChristoffer Dall 356459674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3565b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3566bcdec41cSChristoffer Dall 3567bcdec41cSChristoffer Dall vcpu_put(vcpu); 3568b0c632dbSHeiko Carstens return 0; 3569b0c632dbSHeiko Carstens } 3570b0c632dbSHeiko Carstens 3571b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3572b0c632dbSHeiko Carstens { 35736a96bc7fSChristoffer Dall int ret = 0; 35746a96bc7fSChristoffer Dall 35756a96bc7fSChristoffer Dall vcpu_load(vcpu); 35766a96bc7fSChristoffer Dall 35776a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 35786a96bc7fSChristoffer Dall ret = -EINVAL; 35796a96bc7fSChristoffer Dall goto out; 35806a96bc7fSChristoffer Dall } 3581e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 35829abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3583a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3584a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 35859abc2a08SDavid Hildenbrand else 3586a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 35876a96bc7fSChristoffer Dall 35886a96bc7fSChristoffer Dall out: 35896a96bc7fSChristoffer Dall vcpu_put(vcpu); 35906a96bc7fSChristoffer Dall return ret; 3591b0c632dbSHeiko Carstens } 3592b0c632dbSHeiko Carstens 3593b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3594b0c632dbSHeiko Carstens { 35951393123eSChristoffer Dall vcpu_load(vcpu); 35961393123eSChristoffer Dall 35979abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 35989abc2a08SDavid Hildenbrand save_fpu_regs(); 35999abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3600a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3601a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 36029abc2a08SDavid Hildenbrand else 3603a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3604e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 36051393123eSChristoffer Dall 36061393123eSChristoffer Dall vcpu_put(vcpu); 3607b0c632dbSHeiko Carstens return 0; 3608b0c632dbSHeiko Carstens } 3609b0c632dbSHeiko Carstens 3610b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3611b0c632dbSHeiko Carstens { 3612b0c632dbSHeiko Carstens int rc = 0; 3613b0c632dbSHeiko Carstens 36147a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3615b0c632dbSHeiko Carstens rc = -EBUSY; 3616d7b0b5ebSCarsten Otte else { 3617d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3618d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3619d7b0b5ebSCarsten Otte } 3620b0c632dbSHeiko Carstens return rc; 3621b0c632dbSHeiko Carstens } 3622b0c632dbSHeiko Carstens 3623b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3624b0c632dbSHeiko Carstens struct kvm_translation *tr) 3625b0c632dbSHeiko Carstens { 3626b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3627b0c632dbSHeiko Carstens } 3628b0c632dbSHeiko Carstens 362927291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 363027291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 363127291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 363227291e21SDavid Hildenbrand 3633d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3634d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3635b0c632dbSHeiko Carstens { 363627291e21SDavid Hildenbrand int rc = 0; 363727291e21SDavid Hildenbrand 363866b56562SChristoffer Dall vcpu_load(vcpu); 363966b56562SChristoffer Dall 364027291e21SDavid Hildenbrand vcpu->guest_debug = 0; 364127291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 364227291e21SDavid Hildenbrand 364366b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 364466b56562SChristoffer Dall rc = -EINVAL; 364566b56562SChristoffer Dall goto out; 364666b56562SChristoffer Dall } 364766b56562SChristoffer Dall if (!sclp.has_gpere) { 364866b56562SChristoffer Dall rc = -EINVAL; 364966b56562SChristoffer Dall goto out; 365066b56562SChristoffer Dall } 365127291e21SDavid Hildenbrand 365227291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 365327291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 365427291e21SDavid Hildenbrand /* enforce guest PER */ 3655ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 365627291e21SDavid Hildenbrand 365727291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 365827291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 365927291e21SDavid Hildenbrand } else { 36609daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 366127291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 366227291e21SDavid Hildenbrand } 366327291e21SDavid Hildenbrand 366427291e21SDavid Hildenbrand if (rc) { 366527291e21SDavid Hildenbrand vcpu->guest_debug = 0; 366627291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 36679daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 366827291e21SDavid Hildenbrand } 366927291e21SDavid Hildenbrand 367066b56562SChristoffer Dall out: 367166b56562SChristoffer Dall vcpu_put(vcpu); 367227291e21SDavid Hildenbrand return rc; 3673b0c632dbSHeiko Carstens } 3674b0c632dbSHeiko Carstens 367562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 367662d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 367762d9f0dbSMarcelo Tosatti { 3678fd232561SChristoffer Dall int ret; 3679fd232561SChristoffer Dall 3680fd232561SChristoffer Dall vcpu_load(vcpu); 3681fd232561SChristoffer Dall 36826352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3683fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 36846352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3685fd232561SChristoffer Dall 3686fd232561SChristoffer Dall vcpu_put(vcpu); 3687fd232561SChristoffer Dall return ret; 368862d9f0dbSMarcelo Tosatti } 368962d9f0dbSMarcelo Tosatti 369062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 369162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 369262d9f0dbSMarcelo Tosatti { 36936352e4d2SDavid Hildenbrand int rc = 0; 36946352e4d2SDavid Hildenbrand 3695e83dff5eSChristoffer Dall vcpu_load(vcpu); 3696e83dff5eSChristoffer Dall 36976352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 36986352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 36996352e4d2SDavid Hildenbrand 37006352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 37016352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 37026352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 37036352e4d2SDavid Hildenbrand break; 37046352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 37056352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 37066352e4d2SDavid Hildenbrand break; 37076352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 37086352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 37096352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 37106352e4d2SDavid Hildenbrand default: 37116352e4d2SDavid Hildenbrand rc = -ENXIO; 37126352e4d2SDavid Hildenbrand } 37136352e4d2SDavid Hildenbrand 3714e83dff5eSChristoffer Dall vcpu_put(vcpu); 37156352e4d2SDavid Hildenbrand return rc; 371662d9f0dbSMarcelo Tosatti } 371762d9f0dbSMarcelo Tosatti 37188ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 37198ad35755SDavid Hildenbrand { 37208d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 37218ad35755SDavid Hildenbrand } 37228ad35755SDavid Hildenbrand 37232c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 37242c70fe44SChristian Borntraeger { 37258ad35755SDavid Hildenbrand retry: 37268e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 37272fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3728586b7ccdSChristian Borntraeger return 0; 37292c70fe44SChristian Borntraeger /* 37302c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3731b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 37322c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 37332c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 37342c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 37352c70fe44SChristian Borntraeger */ 37368ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 37372c70fe44SChristian Borntraeger int rc; 3738b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3739fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3740b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3741aca411a4SJulius Niedworok if (rc) { 3742aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 37432c70fe44SChristian Borntraeger return rc; 3744aca411a4SJulius Niedworok } 37458ad35755SDavid Hildenbrand goto retry; 37462c70fe44SChristian Borntraeger } 37478ad35755SDavid Hildenbrand 3748d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3749d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3750d3d692c8SDavid Hildenbrand goto retry; 3751d3d692c8SDavid Hildenbrand } 3752d3d692c8SDavid Hildenbrand 37538ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 37548ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 37558ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3756ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 37578ad35755SDavid Hildenbrand } 37588ad35755SDavid Hildenbrand goto retry; 37598ad35755SDavid Hildenbrand } 37608ad35755SDavid Hildenbrand 37618ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 37628ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 37638ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 37649daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 37658ad35755SDavid Hildenbrand } 37668ad35755SDavid Hildenbrand goto retry; 37678ad35755SDavid Hildenbrand } 37688ad35755SDavid Hildenbrand 37696502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 37706502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 37716502a34cSDavid Hildenbrand goto retry; 37726502a34cSDavid Hildenbrand } 37736502a34cSDavid Hildenbrand 3774190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3775190df4a2SClaudio Imbrenda /* 3776c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3777190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3778190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3779190df4a2SClaudio Imbrenda */ 3780190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3781190df4a2SClaudio Imbrenda goto retry; 3782190df4a2SClaudio Imbrenda } 3783190df4a2SClaudio Imbrenda 3784190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3785190df4a2SClaudio Imbrenda /* 3786c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3787c9f0a2b8SJanosch Frank * CMM has been used. 3788190df4a2SClaudio Imbrenda */ 3789190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3790c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3791190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3792190df4a2SClaudio Imbrenda goto retry; 3793190df4a2SClaudio Imbrenda } 3794190df4a2SClaudio Imbrenda 37950759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 379672875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 37973194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 37983194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 37990759d068SDavid Hildenbrand 38002c70fe44SChristian Borntraeger return 0; 38012c70fe44SChristian Borntraeger } 38022c70fe44SChristian Borntraeger 38030e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, 38048fa1696eSCollin L. Walling const struct kvm_s390_vm_tod_clock *gtod) 38058fa1696eSCollin L. Walling { 38068fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 38078fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 38088fa1696eSCollin L. Walling int i; 38098fa1696eSCollin L. Walling 38108fa1696eSCollin L. Walling mutex_lock(&kvm->lock); 38118fa1696eSCollin L. Walling preempt_disable(); 38128fa1696eSCollin L. Walling 38138fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 38148fa1696eSCollin L. Walling 38158fa1696eSCollin L. Walling kvm->arch.epoch = gtod->tod - htod.tod; 38160e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 38170e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 38188fa1696eSCollin L. Walling kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx; 38198fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 38208fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 38210e7def5fSDavid Hildenbrand } 38228fa1696eSCollin L. Walling 38238fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 38248fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 38258fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 38268fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 38278fa1696eSCollin L. Walling } 38288fa1696eSCollin L. Walling 38298fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 38308fa1696eSCollin L. Walling preempt_enable(); 38318fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 38328fa1696eSCollin L. Walling } 38338fa1696eSCollin L. Walling 3834fa576c58SThomas Huth /** 3835fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 3836fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 3837fa576c58SThomas Huth * @gpa: Guest physical address 3838fa576c58SThomas Huth * @writable: Whether the page should be writable or not 3839fa576c58SThomas Huth * 3840fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 3841fa576c58SThomas Huth * 3842fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 3843fa576c58SThomas Huth */ 3844fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 384524eb3a82SDominik Dingel { 3846527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 3847527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 384824eb3a82SDominik Dingel } 384924eb3a82SDominik Dingel 38503c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 38513c038e6bSDominik Dingel unsigned long token) 38523c038e6bSDominik Dingel { 38533c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 3854383d0b05SJens Freimann struct kvm_s390_irq irq; 38553c038e6bSDominik Dingel 38563c038e6bSDominik Dingel if (start_token) { 3857383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 3858383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 3859383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 38603c038e6bSDominik Dingel } else { 38613c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 3862383d0b05SJens Freimann inti.parm64 = token; 38633c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 38643c038e6bSDominik Dingel } 38653c038e6bSDominik Dingel } 38663c038e6bSDominik Dingel 38673c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 38683c038e6bSDominik Dingel struct kvm_async_pf *work) 38693c038e6bSDominik Dingel { 38703c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 38713c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 38723c038e6bSDominik Dingel } 38733c038e6bSDominik Dingel 38743c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 38753c038e6bSDominik Dingel struct kvm_async_pf *work) 38763c038e6bSDominik Dingel { 38773c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 38783c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 38793c038e6bSDominik Dingel } 38803c038e6bSDominik Dingel 38813c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 38823c038e6bSDominik Dingel struct kvm_async_pf *work) 38833c038e6bSDominik Dingel { 38843c038e6bSDominik Dingel /* s390 will always inject the page directly */ 38853c038e6bSDominik Dingel } 38863c038e6bSDominik Dingel 38873c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 38883c038e6bSDominik Dingel { 38893c038e6bSDominik Dingel /* 38903c038e6bSDominik Dingel * s390 will always inject the page directly, 38913c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 38923c038e6bSDominik Dingel */ 38933c038e6bSDominik Dingel return true; 38943c038e6bSDominik Dingel } 38953c038e6bSDominik Dingel 38963c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 38973c038e6bSDominik Dingel { 38983c038e6bSDominik Dingel hva_t hva; 38993c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 39003c038e6bSDominik Dingel int rc; 39013c038e6bSDominik Dingel 39023c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 39033c038e6bSDominik Dingel return 0; 39043c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 39053c038e6bSDominik Dingel vcpu->arch.pfault_compare) 39063c038e6bSDominik Dingel return 0; 39073c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 39083c038e6bSDominik Dingel return 0; 39099a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 39103c038e6bSDominik Dingel return 0; 3911b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 39123c038e6bSDominik Dingel return 0; 39133c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 39143c038e6bSDominik Dingel return 0; 39153c038e6bSDominik Dingel 391681480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 391781480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 391881480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 39193c038e6bSDominik Dingel return 0; 39203c038e6bSDominik Dingel 39213c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 39223c038e6bSDominik Dingel return rc; 39233c038e6bSDominik Dingel } 39243c038e6bSDominik Dingel 39253fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 3926b0c632dbSHeiko Carstens { 39273fb4c40fSThomas Huth int rc, cpuflags; 3928e168bf8dSCarsten Otte 39293c038e6bSDominik Dingel /* 39303c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 39313c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 39323c038e6bSDominik Dingel * handled outside the worker. 39333c038e6bSDominik Dingel */ 39343c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 39353c038e6bSDominik Dingel 39367ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 39377ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 3938b0c632dbSHeiko Carstens 3939b0c632dbSHeiko Carstens if (need_resched()) 3940b0c632dbSHeiko Carstens schedule(); 3941b0c632dbSHeiko Carstens 3942d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 394371cde587SChristian Borntraeger s390_handle_mcck(); 394471cde587SChristian Borntraeger 394579395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 394679395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 394779395031SJens Freimann if (rc) 394879395031SJens Freimann return rc; 394979395031SJens Freimann } 39500ff31867SCarsten Otte 39512c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 39522c70fe44SChristian Borntraeger if (rc) 39532c70fe44SChristian Borntraeger return rc; 39542c70fe44SChristian Borntraeger 395527291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 395627291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 395727291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 395827291e21SDavid Hildenbrand } 395927291e21SDavid Hildenbrand 39609f30f621SMichael Mueller clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask); 39619f30f621SMichael Mueller 3962b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 39633fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 39643fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 39653fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 39662b29a9fdSDominik Dingel 39673fb4c40fSThomas Huth return 0; 39683fb4c40fSThomas Huth } 39693fb4c40fSThomas Huth 3970492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 3971492d8642SThomas Huth { 397256317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 397356317920SDavid Hildenbrand .code = PGM_ADDRESSING, 397456317920SDavid Hildenbrand }; 397556317920SDavid Hildenbrand u8 opcode, ilen; 3976492d8642SThomas Huth int rc; 3977492d8642SThomas Huth 3978492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 3979492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 3980492d8642SThomas Huth 3981492d8642SThomas Huth /* 3982492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 3983492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 3984492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 3985492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 3986492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 3987492d8642SThomas Huth * to be able to forward the PSW. 3988492d8642SThomas Huth */ 39893fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 399056317920SDavid Hildenbrand ilen = insn_length(opcode); 39919b0d721aSDavid Hildenbrand if (rc < 0) { 39929b0d721aSDavid Hildenbrand return rc; 39939b0d721aSDavid Hildenbrand } else if (rc) { 39949b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 39959b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 39969b0d721aSDavid Hildenbrand * nullification if necessary. 39979b0d721aSDavid Hildenbrand */ 39989b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 39999b0d721aSDavid Hildenbrand ilen = 4; 40009b0d721aSDavid Hildenbrand } 400156317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 400256317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 400356317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4004492d8642SThomas Huth } 4005492d8642SThomas Huth 40063fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 40073fb4c40fSThomas Huth { 40084d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 40094d62fcc0SQingFeng Hao struct sie_page *sie_page; 40104d62fcc0SQingFeng Hao 40112b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 40122b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 40132b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 40142b29a9fdSDominik Dingel 401527291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 401627291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 401727291e21SDavid Hildenbrand 40187ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 40197ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 402071f116bfSDavid Hildenbrand 40214d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 40224d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 40234d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 40244d62fcc0SQingFeng Hao struct sie_page, sie_block); 40254d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 40264d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 40274d62fcc0SQingFeng Hao return 0; 40284d62fcc0SQingFeng Hao } 40294d62fcc0SQingFeng Hao 403071f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 403171f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 403271f116bfSDavid Hildenbrand 403371f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 403471f116bfSDavid Hildenbrand return rc; 403571f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 403671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 403771f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 403871f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 403971f116bfSDavid Hildenbrand return -EREMOTE; 404071f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 404171f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 404271f116bfSDavid Hildenbrand return 0; 4043210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4044210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4045210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4046210b1607SThomas Huth current->thread.gmap_addr; 4047210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 404871f116bfSDavid Hildenbrand return -EREMOTE; 404924eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 40503c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 405124eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 405271f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 405371f116bfSDavid Hildenbrand return 0; 405471f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4055fa576c58SThomas Huth } 405671f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 40573fb4c40fSThomas Huth } 40583fb4c40fSThomas Huth 40593fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 40603fb4c40fSThomas Huth { 40613fb4c40fSThomas Huth int rc, exit_reason; 4062*c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 40633fb4c40fSThomas Huth 4064800c1065SThomas Huth /* 4065800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4066800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4067800c1065SThomas Huth */ 4068800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 4069800c1065SThomas Huth 4070a76ccff6SThomas Huth do { 40713fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 40723fb4c40fSThomas Huth if (rc) 4073a76ccff6SThomas Huth break; 40743fb4c40fSThomas Huth 4075800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 40763fb4c40fSThomas Huth /* 4077a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4078a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 40793fb4c40fSThomas Huth */ 40800097d12eSChristian Borntraeger local_irq_disable(); 40816edaa530SPaolo Bonzini guest_enter_irqoff(); 4082db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 40830097d12eSChristian Borntraeger local_irq_enable(); 4084*c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4085*c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4086*c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4087*c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4088*c8aac234SJanosch Frank } 4089a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4090a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4091*c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4092*c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4093*c8aac234SJanosch Frank sie_page->pv_grregs, 4094*c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4095*c8aac234SJanosch Frank } 40960097d12eSChristian Borntraeger local_irq_disable(); 4097db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 40986edaa530SPaolo Bonzini guest_exit_irqoff(); 40990097d12eSChristian Borntraeger local_irq_enable(); 4100800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 41013fb4c40fSThomas Huth 41023fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 410327291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 41043fb4c40fSThomas Huth 4105800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 4106e168bf8dSCarsten Otte return rc; 4107b0c632dbSHeiko Carstens } 4108b0c632dbSHeiko Carstens 4109b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4110b028ee3eSDavid Hildenbrand { 41114d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 41124e0b1ab7SFan Zhang struct gs_cb *gscb; 41134d5f2c04SChristian Borntraeger 41144d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 41154e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4116b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4117b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4118b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4119b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4120b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4121b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4122d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 4123d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4124b028ee3eSDavid Hildenbrand } 4125b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 41264287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4127b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4128b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4129b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4130b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4131b028ee3eSDavid Hildenbrand } 4132b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4133b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4134b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4135b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 41369fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 41379fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4138b028ee3eSDavid Hildenbrand } 413980cd8763SFan Zhang /* 414080cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 414180cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 414280cd8763SFan Zhang */ 414380cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 41444d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4145bb59c2daSAlice Frosi riccb->v && 41460c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 41474d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 41480c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 414980cd8763SFan Zhang } 41504e0b1ab7SFan Zhang /* 41514e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 41524e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 41534e0b1ab7SFan Zhang */ 41544e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 41554e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 41564e0b1ab7SFan Zhang gscb->gssm && 41574e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 41584e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 41594e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 41604e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 41614e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 416280cd8763SFan Zhang } 416335b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 416435b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 416535b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 416635b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 416735b3fde6SChristian Borntraeger } 416831d8b8d4SChristian Borntraeger save_access_regs(vcpu->arch.host_acrs); 416931d8b8d4SChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 4170e1788bb9SChristian Borntraeger /* save host (userspace) fprs/vrs */ 4171e1788bb9SChristian Borntraeger save_fpu_regs(); 4172e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4173e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4174e1788bb9SChristian Borntraeger if (MACHINE_HAS_VX) 4175e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4176e1788bb9SChristian Borntraeger else 4177e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4178e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4179e1788bb9SChristian Borntraeger if (test_fp_ctl(current->thread.fpu.fpc)) 4180e1788bb9SChristian Borntraeger /* User space provided an invalid FPC, let's clear it */ 4181e1788bb9SChristian Borntraeger current->thread.fpu.fpc = 0; 41824e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 41834e0b1ab7SFan Zhang preempt_disable(); 41844e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 41854e0b1ab7SFan Zhang if (current->thread.gs_cb) { 41864e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 41874e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 41884e0b1ab7SFan Zhang } 41894e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 41904e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 41914e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 41924e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 41934e0b1ab7SFan Zhang } 41944e0b1ab7SFan Zhang preempt_enable(); 41954e0b1ab7SFan Zhang } 4196a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 419780cd8763SFan Zhang 4198b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4199b028ee3eSDavid Hildenbrand } 4200b028ee3eSDavid Hildenbrand 4201b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4202b028ee3eSDavid Hildenbrand { 4203b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4204b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4205b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4206b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 42074287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4208b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4209b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4210b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4211b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 4212b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4213b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4214b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 421535b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 421631d8b8d4SChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 421731d8b8d4SChristian Borntraeger restore_access_regs(vcpu->arch.host_acrs); 4218e1788bb9SChristian Borntraeger /* Save guest register state */ 4219e1788bb9SChristian Borntraeger save_fpu_regs(); 4220e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4221e1788bb9SChristian Borntraeger /* Restore will be done lazily at return */ 4222e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4223e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 42244e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 42254e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 42264e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 42274e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 42284e0b1ab7SFan Zhang preempt_disable(); 42294e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 42304e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 42314e0b1ab7SFan Zhang preempt_enable(); 42324e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 42334e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 42344e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 42354e0b1ab7SFan Zhang } 4236a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4237b028ee3eSDavid Hildenbrand } 4238b028ee3eSDavid Hildenbrand 4239b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4240b0c632dbSHeiko Carstens { 42418f2abe6aSChristian Borntraeger int rc; 4242b0c632dbSHeiko Carstens 4243460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4244460df4c1SPaolo Bonzini return -EINTR; 4245460df4c1SPaolo Bonzini 4246200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4247200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4248200824f5SThomas Huth return -EINVAL; 4249200824f5SThomas Huth 4250accb757dSChristoffer Dall vcpu_load(vcpu); 4251accb757dSChristoffer Dall 425227291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 425327291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4254accb757dSChristoffer Dall rc = 0; 4255accb757dSChristoffer Dall goto out; 425627291e21SDavid Hildenbrand } 425727291e21SDavid Hildenbrand 425820b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4259b0c632dbSHeiko Carstens 42606352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 42616852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 42626352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4263ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 42646352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4265accb757dSChristoffer Dall rc = -EINVAL; 4266accb757dSChristoffer Dall goto out; 42676352e4d2SDavid Hildenbrand } 4268b0c632dbSHeiko Carstens 4269b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 4270db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4271d7b0b5ebSCarsten Otte 4272dab4079dSHeiko Carstens might_fault(); 4273e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 42749ace903dSChristian Ehrhardt 4275b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4276b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 42778f2abe6aSChristian Borntraeger rc = -EINTR; 4278b1d16c49SChristian Ehrhardt } 42798f2abe6aSChristian Borntraeger 428027291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 428127291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 428227291e21SDavid Hildenbrand rc = 0; 428327291e21SDavid Hildenbrand } 428427291e21SDavid Hildenbrand 42858f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 428671f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 42878f2abe6aSChristian Borntraeger rc = 0; 42888f2abe6aSChristian Borntraeger } 42898f2abe6aSChristian Borntraeger 4290db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 4291b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 4292d7b0b5ebSCarsten Otte 429320b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4294b0c632dbSHeiko Carstens 4295b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4296accb757dSChristoffer Dall out: 4297accb757dSChristoffer Dall vcpu_put(vcpu); 42987e8e6ab4SHeiko Carstens return rc; 4299b0c632dbSHeiko Carstens } 4300b0c632dbSHeiko Carstens 4301b0c632dbSHeiko Carstens /* 4302b0c632dbSHeiko Carstens * store status at address 4303b0c632dbSHeiko Carstens * we use have two special cases: 4304b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4305b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4306b0c632dbSHeiko Carstens */ 4307d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4308b0c632dbSHeiko Carstens { 4309092670cdSCarsten Otte unsigned char archmode = 1; 43109abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4311fda902cbSMichael Mueller unsigned int px; 43124287f247SDavid Hildenbrand u64 clkcomp, cputm; 4313d0bce605SHeiko Carstens int rc; 4314b0c632dbSHeiko Carstens 4315d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4316d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4317d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4318b0c632dbSHeiko Carstens return -EFAULT; 4319d9a3a09aSMartin Schwidefsky gpa = 0; 4320d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4321d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4322b0c632dbSHeiko Carstens return -EFAULT; 4323d9a3a09aSMartin Schwidefsky gpa = px; 4324d9a3a09aSMartin Schwidefsky } else 4325d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 43269abc2a08SDavid Hildenbrand 43279abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 43289abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 43299522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4330d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 43319abc2a08SDavid Hildenbrand fprs, 128); 43329abc2a08SDavid Hildenbrand } else { 43339abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 43346fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 43359abc2a08SDavid Hildenbrand } 4336d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4337d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4338d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4339d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4340d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4341fda902cbSMichael Mueller &px, 4); 4342d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 43439abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4344d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4345d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 43464287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4347d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 43484287f247SDavid Hildenbrand &cputm, 8); 4349178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4350d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4351d0bce605SHeiko Carstens &clkcomp, 8); 4352d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4353d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4354d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4355d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4356d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4357b0c632dbSHeiko Carstens } 4358b0c632dbSHeiko Carstens 4359e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4360e879892cSThomas Huth { 4361e879892cSThomas Huth /* 4362e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 436331d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4364e879892cSThomas Huth * it into the save area 4365e879892cSThomas Huth */ 4366d0164ee2SHendrik Brueckner save_fpu_regs(); 43679abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4368e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4369e879892cSThomas Huth 4370e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4371e879892cSThomas Huth } 4372e879892cSThomas Huth 43738ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 43748ad35755SDavid Hildenbrand { 43758ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 43768e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 43778ad35755SDavid Hildenbrand } 43788ad35755SDavid Hildenbrand 43798ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 43808ad35755SDavid Hildenbrand { 43818ad35755SDavid Hildenbrand unsigned int i; 43828ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 43838ad35755SDavid Hildenbrand 43848ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 43858ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 43868ad35755SDavid Hildenbrand } 43878ad35755SDavid Hildenbrand } 43888ad35755SDavid Hildenbrand 43898ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 43908ad35755SDavid Hildenbrand { 439109a400e7SDavid Hildenbrand if (!sclp.has_ibs) 439209a400e7SDavid Hildenbrand return; 43938ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 43948e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 43958ad35755SDavid Hildenbrand } 43968ad35755SDavid Hildenbrand 43976852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 43986852d7b6SDavid Hildenbrand { 43998ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 44008ad35755SDavid Hildenbrand 44018ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 44028ad35755SDavid Hildenbrand return; 44038ad35755SDavid Hildenbrand 44046852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 44058ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4406433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 44078ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 44088ad35755SDavid Hildenbrand 44098ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 44108ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 44118ad35755SDavid Hildenbrand started_vcpus++; 44128ad35755SDavid Hildenbrand } 44138ad35755SDavid Hildenbrand 44148ad35755SDavid Hildenbrand if (started_vcpus == 0) { 44158ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 44168ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 44178ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 44188ad35755SDavid Hildenbrand /* 44198ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 44208ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 44218ad35755SDavid Hildenbrand * oustanding ENABLE requests. 44228ad35755SDavid Hildenbrand */ 44238ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 44248ad35755SDavid Hildenbrand } 44258ad35755SDavid Hildenbrand 44269daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 44278ad35755SDavid Hildenbrand /* 44288ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 44298ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 44308ad35755SDavid Hildenbrand */ 4431d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4432433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 44338ad35755SDavid Hildenbrand return; 44346852d7b6SDavid Hildenbrand } 44356852d7b6SDavid Hildenbrand 44366852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 44376852d7b6SDavid Hildenbrand { 44388ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 44398ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 44408ad35755SDavid Hildenbrand 44418ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 44428ad35755SDavid Hildenbrand return; 44438ad35755SDavid Hildenbrand 44446852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 44458ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4446433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 44478ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 44488ad35755SDavid Hildenbrand 444932f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 44506cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 445132f5ff63SDavid Hildenbrand 4452ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 44538ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 44548ad35755SDavid Hildenbrand 44558ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 44568ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 44578ad35755SDavid Hildenbrand started_vcpus++; 44588ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 44598ad35755SDavid Hildenbrand } 44608ad35755SDavid Hildenbrand } 44618ad35755SDavid Hildenbrand 44628ad35755SDavid Hildenbrand if (started_vcpus == 1) { 44638ad35755SDavid Hildenbrand /* 44648ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 44658ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 44668ad35755SDavid Hildenbrand */ 44678ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 44688ad35755SDavid Hildenbrand } 44698ad35755SDavid Hildenbrand 4470433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 44718ad35755SDavid Hildenbrand return; 44726852d7b6SDavid Hildenbrand } 44736852d7b6SDavid Hildenbrand 4474d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4475d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4476d6712df9SCornelia Huck { 4477d6712df9SCornelia Huck int r; 4478d6712df9SCornelia Huck 4479d6712df9SCornelia Huck if (cap->flags) 4480d6712df9SCornelia Huck return -EINVAL; 4481d6712df9SCornelia Huck 4482d6712df9SCornelia Huck switch (cap->cap) { 4483fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4484fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4485fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4486c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4487fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4488fa6b7fe9SCornelia Huck } 4489fa6b7fe9SCornelia Huck r = 0; 4490fa6b7fe9SCornelia Huck break; 4491d6712df9SCornelia Huck default: 4492d6712df9SCornelia Huck r = -EINVAL; 4493d6712df9SCornelia Huck break; 4494d6712df9SCornelia Huck } 4495d6712df9SCornelia Huck return r; 4496d6712df9SCornelia Huck } 4497d6712df9SCornelia Huck 449841408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 449941408c28SThomas Huth struct kvm_s390_mem_op *mop) 450041408c28SThomas Huth { 450141408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 450241408c28SThomas Huth void *tmpbuf = NULL; 450341408c28SThomas Huth int r, srcu_idx; 450441408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 450541408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 450641408c28SThomas Huth 4507a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 450841408c28SThomas Huth return -EINVAL; 450941408c28SThomas Huth 451041408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 451141408c28SThomas Huth return -E2BIG; 451241408c28SThomas Huth 451341408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 451441408c28SThomas Huth tmpbuf = vmalloc(mop->size); 451541408c28SThomas Huth if (!tmpbuf) 451641408c28SThomas Huth return -ENOMEM; 451741408c28SThomas Huth } 451841408c28SThomas Huth 451941408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 452041408c28SThomas Huth 452141408c28SThomas Huth switch (mop->op) { 452241408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 452341408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 452492c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 452592c96321SDavid Hildenbrand mop->size, GACC_FETCH); 452641408c28SThomas Huth break; 452741408c28SThomas Huth } 452841408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 452941408c28SThomas Huth if (r == 0) { 453041408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 453141408c28SThomas Huth r = -EFAULT; 453241408c28SThomas Huth } 453341408c28SThomas Huth break; 453441408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 453541408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 453692c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 453792c96321SDavid Hildenbrand mop->size, GACC_STORE); 453841408c28SThomas Huth break; 453941408c28SThomas Huth } 454041408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 454141408c28SThomas Huth r = -EFAULT; 454241408c28SThomas Huth break; 454341408c28SThomas Huth } 454441408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 454541408c28SThomas Huth break; 454641408c28SThomas Huth default: 454741408c28SThomas Huth r = -EINVAL; 454841408c28SThomas Huth } 454941408c28SThomas Huth 455041408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 455141408c28SThomas Huth 455241408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 455341408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 455441408c28SThomas Huth 455541408c28SThomas Huth vfree(tmpbuf); 455641408c28SThomas Huth return r; 455741408c28SThomas Huth } 455841408c28SThomas Huth 45595cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4560b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4561b0c632dbSHeiko Carstens { 4562b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4563b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4564b0c632dbSHeiko Carstens 456593736624SAvi Kivity switch (ioctl) { 456647b43c52SJens Freimann case KVM_S390_IRQ: { 456747b43c52SJens Freimann struct kvm_s390_irq s390irq; 456847b43c52SJens Freimann 456947b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 45709b062471SChristoffer Dall return -EFAULT; 45719b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 457247b43c52SJens Freimann } 457393736624SAvi Kivity case KVM_S390_INTERRUPT: { 4574ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 457553936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 4576ba5c1e9bSCarsten Otte 4577ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 45789b062471SChristoffer Dall return -EFAULT; 4579383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4580383d0b05SJens Freimann return -EINVAL; 45819b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4582ba5c1e9bSCarsten Otte } 45839b062471SChristoffer Dall } 45845cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 45855cb0944cSPaolo Bonzini } 45865cb0944cSPaolo Bonzini 45875cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 45885cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 45895cb0944cSPaolo Bonzini { 45905cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 45915cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 45925cb0944cSPaolo Bonzini int idx; 45935cb0944cSPaolo Bonzini long r; 45949b062471SChristoffer Dall 45959b062471SChristoffer Dall vcpu_load(vcpu); 45969b062471SChristoffer Dall 45979b062471SChristoffer Dall switch (ioctl) { 4598b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4599800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 460055680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 4601800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4602bc923cc9SAvi Kivity break; 4603b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4604b0c632dbSHeiko Carstens psw_t psw; 4605b0c632dbSHeiko Carstens 4606bc923cc9SAvi Kivity r = -EFAULT; 4607b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4608bc923cc9SAvi Kivity break; 4609bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4610bc923cc9SAvi Kivity break; 4611b0c632dbSHeiko Carstens } 46127de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 46137de3f142SJanosch Frank r = 0; 46147de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 46157de3f142SJanosch Frank break; 4616b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 46177de3f142SJanosch Frank r = 0; 46187de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 46197de3f142SJanosch Frank break; 46207de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 46217de3f142SJanosch Frank r = 0; 46227de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 4623bc923cc9SAvi Kivity break; 462414eebd91SCarsten Otte case KVM_SET_ONE_REG: 462514eebd91SCarsten Otte case KVM_GET_ONE_REG: { 462614eebd91SCarsten Otte struct kvm_one_reg reg; 462714eebd91SCarsten Otte r = -EFAULT; 462814eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 462914eebd91SCarsten Otte break; 463014eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 463114eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 463214eebd91SCarsten Otte else 463314eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 463414eebd91SCarsten Otte break; 463514eebd91SCarsten Otte } 463627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 463727e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 463827e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 463927e0393fSCarsten Otte 464027e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 464127e0393fSCarsten Otte r = -EFAULT; 464227e0393fSCarsten Otte break; 464327e0393fSCarsten Otte } 464427e0393fSCarsten Otte 464527e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 464627e0393fSCarsten Otte r = -EINVAL; 464727e0393fSCarsten Otte break; 464827e0393fSCarsten Otte } 464927e0393fSCarsten Otte 465027e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 465127e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 465227e0393fSCarsten Otte break; 465327e0393fSCarsten Otte } 465427e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 465527e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 465627e0393fSCarsten Otte 465727e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 465827e0393fSCarsten Otte r = -EFAULT; 465927e0393fSCarsten Otte break; 466027e0393fSCarsten Otte } 466127e0393fSCarsten Otte 466227e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 466327e0393fSCarsten Otte r = -EINVAL; 466427e0393fSCarsten Otte break; 466527e0393fSCarsten Otte } 466627e0393fSCarsten Otte 466727e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 466827e0393fSCarsten Otte ucasmap.length); 466927e0393fSCarsten Otte break; 467027e0393fSCarsten Otte } 467127e0393fSCarsten Otte #endif 4672ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 4673527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 4674ccc7910fSCarsten Otte break; 4675ccc7910fSCarsten Otte } 4676d6712df9SCornelia Huck case KVM_ENABLE_CAP: 4677d6712df9SCornelia Huck { 4678d6712df9SCornelia Huck struct kvm_enable_cap cap; 4679d6712df9SCornelia Huck r = -EFAULT; 4680d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 4681d6712df9SCornelia Huck break; 4682d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 4683d6712df9SCornelia Huck break; 4684d6712df9SCornelia Huck } 468541408c28SThomas Huth case KVM_S390_MEM_OP: { 468641408c28SThomas Huth struct kvm_s390_mem_op mem_op; 468741408c28SThomas Huth 468841408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 468941408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 469041408c28SThomas Huth else 469141408c28SThomas Huth r = -EFAULT; 469241408c28SThomas Huth break; 469341408c28SThomas Huth } 4694816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 4695816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4696816c7667SJens Freimann 4697816c7667SJens Freimann r = -EFAULT; 4698816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4699816c7667SJens Freimann break; 4700816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 4701816c7667SJens Freimann irq_state.len == 0 || 4702816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 4703816c7667SJens Freimann r = -EINVAL; 4704816c7667SJens Freimann break; 4705816c7667SJens Freimann } 4706bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4707816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 4708816c7667SJens Freimann (void __user *) irq_state.buf, 4709816c7667SJens Freimann irq_state.len); 4710816c7667SJens Freimann break; 4711816c7667SJens Freimann } 4712816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 4713816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4714816c7667SJens Freimann 4715816c7667SJens Freimann r = -EFAULT; 4716816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4717816c7667SJens Freimann break; 4718816c7667SJens Freimann if (irq_state.len == 0) { 4719816c7667SJens Freimann r = -EINVAL; 4720816c7667SJens Freimann break; 4721816c7667SJens Freimann } 4722bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4723816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 4724816c7667SJens Freimann (__u8 __user *) irq_state.buf, 4725816c7667SJens Freimann irq_state.len); 4726816c7667SJens Freimann break; 4727816c7667SJens Freimann } 4728b0c632dbSHeiko Carstens default: 47293e6afcf1SCarsten Otte r = -ENOTTY; 4730b0c632dbSHeiko Carstens } 47319b062471SChristoffer Dall 47329b062471SChristoffer Dall vcpu_put(vcpu); 4733bc923cc9SAvi Kivity return r; 4734b0c632dbSHeiko Carstens } 4735b0c632dbSHeiko Carstens 47361499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 47375b1c1493SCarsten Otte { 47385b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 47395b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 47405b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 47415b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 47425b1c1493SCarsten Otte get_page(vmf->page); 47435b1c1493SCarsten Otte return 0; 47445b1c1493SCarsten Otte } 47455b1c1493SCarsten Otte #endif 47465b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 47475b1c1493SCarsten Otte } 47485b1c1493SCarsten Otte 47495587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 47505587027cSAneesh Kumar K.V unsigned long npages) 4751db3fe4ebSTakuya Yoshikawa { 4752db3fe4ebSTakuya Yoshikawa return 0; 4753db3fe4ebSTakuya Yoshikawa } 4754db3fe4ebSTakuya Yoshikawa 4755b0c632dbSHeiko Carstens /* Section: memory related */ 4756f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 4757f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 475809170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 47597b6195a9STakuya Yoshikawa enum kvm_mr_change change) 4760b0c632dbSHeiko Carstens { 4761dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 4762dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 4763dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 4764dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 4765b0c632dbSHeiko Carstens 4766598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 4767b0c632dbSHeiko Carstens return -EINVAL; 4768b0c632dbSHeiko Carstens 4769598841caSCarsten Otte if (mem->memory_size & 0xffffful) 4770b0c632dbSHeiko Carstens return -EINVAL; 4771b0c632dbSHeiko Carstens 4772a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 4773a3a92c31SDominik Dingel return -EINVAL; 4774a3a92c31SDominik Dingel 477529b40f10SJanosch Frank /* When we are protected, we should not change the memory slots */ 477629b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 477729b40f10SJanosch Frank return -EINVAL; 4778f7784b8eSMarcelo Tosatti return 0; 4779f7784b8eSMarcelo Tosatti } 4780f7784b8eSMarcelo Tosatti 4781f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 478209170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 47838482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 4784f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 47858482644aSTakuya Yoshikawa enum kvm_mr_change change) 4786f7784b8eSMarcelo Tosatti { 478719ec166cSChristian Borntraeger int rc = 0; 4788f7784b8eSMarcelo Tosatti 478919ec166cSChristian Borntraeger switch (change) { 479019ec166cSChristian Borntraeger case KVM_MR_DELETE: 479119ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 479219ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 479319ec166cSChristian Borntraeger break; 479419ec166cSChristian Borntraeger case KVM_MR_MOVE: 479519ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 479619ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 479719ec166cSChristian Borntraeger if (rc) 479819ec166cSChristian Borntraeger break; 479919ec166cSChristian Borntraeger /* FALLTHROUGH */ 480019ec166cSChristian Borntraeger case KVM_MR_CREATE: 4801598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 4802598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 480319ec166cSChristian Borntraeger break; 480419ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 480519ec166cSChristian Borntraeger break; 480619ec166cSChristian Borntraeger default: 480719ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 480819ec166cSChristian Borntraeger } 4809598841caSCarsten Otte if (rc) 4810ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 4811598841caSCarsten Otte return; 4812b0c632dbSHeiko Carstens } 4813b0c632dbSHeiko Carstens 481460a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 481560a37709SAlexander Yarygin { 481660a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 481760a37709SAlexander Yarygin 481860a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 481960a37709SAlexander Yarygin } 482060a37709SAlexander Yarygin 48213491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 48223491caf2SChristian Borntraeger { 48233491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 48243491caf2SChristian Borntraeger } 48253491caf2SChristian Borntraeger 4826b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 4827b0c632dbSHeiko Carstens { 482860a37709SAlexander Yarygin int i; 482960a37709SAlexander Yarygin 483007197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 48318d43d570SMichael Mueller pr_info("SIE is not available\n"); 483207197fd0SDavid Hildenbrand return -ENODEV; 483307197fd0SDavid Hildenbrand } 483407197fd0SDavid Hildenbrand 4835a4499382SJanosch Frank if (nested && hpage) { 48368d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 4837a4499382SJanosch Frank return -EINVAL; 4838a4499382SJanosch Frank } 4839a4499382SJanosch Frank 484060a37709SAlexander Yarygin for (i = 0; i < 16; i++) 4841c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 484260a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 484360a37709SAlexander Yarygin 48449d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 4845b0c632dbSHeiko Carstens } 4846b0c632dbSHeiko Carstens 4847b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 4848b0c632dbSHeiko Carstens { 4849b0c632dbSHeiko Carstens kvm_exit(); 4850b0c632dbSHeiko Carstens } 4851b0c632dbSHeiko Carstens 4852b0c632dbSHeiko Carstens module_init(kvm_s390_init); 4853b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 4854566af940SCornelia Huck 4855566af940SCornelia Huck /* 4856566af940SCornelia Huck * Enable autoloading of the kvm module. 4857566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 4858566af940SCornelia Huck * since x86 takes a different approach. 4859566af940SCornelia Huck */ 4860566af940SCornelia Huck #include <linux/miscdevice.h> 4861566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 4862566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 4863