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 } 2331e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2332e0d2773dSJanosch Frank r = -EINVAL; 2333e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2334e0d2773dSJanosch Frank break; 2335e0d2773dSJanosch Frank 2336e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2337e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2338e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2339e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2340e0d2773dSJanosch Frank break; 2341e0d2773dSJanosch Frank } 2342e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2343e0d2773dSJanosch Frank r = -EINVAL; 2344e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2345e0d2773dSJanosch Frank break; 2346e0d2773dSJanosch Frank 2347e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2348e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2349e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2350e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2351e0d2773dSJanosch Frank break; 2352e0d2773dSJanosch Frank } 235329b40f10SJanosch Frank default: 235429b40f10SJanosch Frank r = -ENOTTY; 235529b40f10SJanosch Frank } 235629b40f10SJanosch Frank return r; 235729b40f10SJanosch Frank } 235829b40f10SJanosch Frank 2359b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2360b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2361b0c632dbSHeiko Carstens { 2362b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2363b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2364f2061656SDominik Dingel struct kvm_device_attr attr; 2365b0c632dbSHeiko Carstens int r; 2366b0c632dbSHeiko Carstens 2367b0c632dbSHeiko Carstens switch (ioctl) { 2368ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2369ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2370ba5c1e9bSCarsten Otte 2371ba5c1e9bSCarsten Otte r = -EFAULT; 2372ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2373ba5c1e9bSCarsten Otte break; 2374ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2375ba5c1e9bSCarsten Otte break; 2376ba5c1e9bSCarsten Otte } 237784223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 237884223598SCornelia Huck struct kvm_irq_routing_entry routing; 237984223598SCornelia Huck 238084223598SCornelia Huck r = -EINVAL; 238184223598SCornelia Huck if (kvm->arch.use_irqchip) { 238284223598SCornelia Huck /* Set up dummy routing. */ 238384223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2384152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 238584223598SCornelia Huck } 238684223598SCornelia Huck break; 238784223598SCornelia Huck } 2388f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2389f2061656SDominik Dingel r = -EFAULT; 2390f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2391f2061656SDominik Dingel break; 2392f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2393f2061656SDominik Dingel break; 2394f2061656SDominik Dingel } 2395f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2396f2061656SDominik Dingel r = -EFAULT; 2397f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2398f2061656SDominik Dingel break; 2399f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2400f2061656SDominik Dingel break; 2401f2061656SDominik Dingel } 2402f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2403f2061656SDominik Dingel r = -EFAULT; 2404f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2405f2061656SDominik Dingel break; 2406f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2407f2061656SDominik Dingel break; 2408f2061656SDominik Dingel } 240930ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 241030ee2a98SJason J. Herne struct kvm_s390_skeys args; 241130ee2a98SJason J. Herne 241230ee2a98SJason J. Herne r = -EFAULT; 241330ee2a98SJason J. Herne if (copy_from_user(&args, argp, 241430ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 241530ee2a98SJason J. Herne break; 241630ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 241730ee2a98SJason J. Herne break; 241830ee2a98SJason J. Herne } 241930ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 242030ee2a98SJason J. Herne struct kvm_s390_skeys args; 242130ee2a98SJason J. Herne 242230ee2a98SJason J. Herne r = -EFAULT; 242330ee2a98SJason J. Herne if (copy_from_user(&args, argp, 242430ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 242530ee2a98SJason J. Herne break; 242630ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 242730ee2a98SJason J. Herne break; 242830ee2a98SJason J. Herne } 24294036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 24304036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24314036e387SClaudio Imbrenda 24324036e387SClaudio Imbrenda r = -EFAULT; 24334036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24344036e387SClaudio Imbrenda break; 24351de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24364036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 24371de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24384036e387SClaudio Imbrenda if (!r) { 24394036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 24404036e387SClaudio Imbrenda if (r) 24414036e387SClaudio Imbrenda r = -EFAULT; 24424036e387SClaudio Imbrenda } 24434036e387SClaudio Imbrenda break; 24444036e387SClaudio Imbrenda } 24454036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 24464036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 24474036e387SClaudio Imbrenda 24484036e387SClaudio Imbrenda r = -EFAULT; 24494036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 24504036e387SClaudio Imbrenda break; 24511de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 24524036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 24531de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 24544036e387SClaudio Imbrenda break; 24554036e387SClaudio Imbrenda } 245629b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 245729b40f10SJanosch Frank struct kvm_pv_cmd args; 245829b40f10SJanosch Frank 2459fe28c786SJanosch Frank /* protvirt means user sigp */ 2460fe28c786SJanosch Frank kvm->arch.user_cpu_state_ctrl = 1; 246129b40f10SJanosch Frank r = 0; 246229b40f10SJanosch Frank if (!is_prot_virt_host()) { 246329b40f10SJanosch Frank r = -EINVAL; 246429b40f10SJanosch Frank break; 246529b40f10SJanosch Frank } 246629b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 246729b40f10SJanosch Frank r = -EFAULT; 246829b40f10SJanosch Frank break; 246929b40f10SJanosch Frank } 247029b40f10SJanosch Frank if (args.flags) { 247129b40f10SJanosch Frank r = -EINVAL; 247229b40f10SJanosch Frank break; 247329b40f10SJanosch Frank } 247429b40f10SJanosch Frank mutex_lock(&kvm->lock); 247529b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 247629b40f10SJanosch Frank mutex_unlock(&kvm->lock); 247729b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 247829b40f10SJanosch Frank r = -EFAULT; 247929b40f10SJanosch Frank break; 248029b40f10SJanosch Frank } 248129b40f10SJanosch Frank break; 248229b40f10SJanosch Frank } 2483b0c632dbSHeiko Carstens default: 2484367e1319SAvi Kivity r = -ENOTTY; 2485b0c632dbSHeiko Carstens } 2486b0c632dbSHeiko Carstens 2487b0c632dbSHeiko Carstens return r; 2488b0c632dbSHeiko Carstens } 2489b0c632dbSHeiko Carstens 249045c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 249145c9b47cSTony Krowiak { 2492e585b24aSTony Krowiak struct ap_config_info info; 249345c9b47cSTony Krowiak 2494e585b24aSTony Krowiak if (ap_instructions_available()) { 2495e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2496e585b24aSTony Krowiak return info.apxa; 249745c9b47cSTony Krowiak } 249845c9b47cSTony Krowiak 249945c9b47cSTony Krowiak return 0; 250045c9b47cSTony Krowiak } 250145c9b47cSTony Krowiak 2502e585b24aSTony Krowiak /* 2503e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2504e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2505e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2506e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2507e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2508e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2509e585b24aSTony Krowiak */ 251045c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 251145c9b47cSTony Krowiak { 251245c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 251345c9b47cSTony Krowiak 2514e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2515e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2516e585b24aSTony Krowiak 2517e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2518e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2519e585b24aSTony Krowiak return; 2520e585b24aSTony Krowiak 252145c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 252245c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 252345c9b47cSTony Krowiak else 252445c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 252545c9b47cSTony Krowiak } 252645c9b47cSTony Krowiak 25270e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 25280e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 25290e237e44SPierre Morel { 25300e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 25310e237e44SPierre Morel 25320e237e44SPierre Morel mutex_lock(&kvm->lock); 25330e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 25340e237e44SPierre Morel 25350e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 25360e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 25370e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 25380e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 25390e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 25400e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 25410e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 25420e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 25430e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 25440e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 25450e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 25460e237e44SPierre Morel break; 25470e237e44SPierre Morel case CRYCB_FORMAT1: 25480e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 25490e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 25500e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 25510e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 25520e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 25530e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 25540e237e44SPierre Morel *((unsigned short *)adm)); 25550e237e44SPierre Morel break; 25560e237e44SPierre Morel default: /* Can not happen */ 25570e237e44SPierre Morel break; 25580e237e44SPierre Morel } 25590e237e44SPierre Morel 25600e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 25610e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 25620e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 25630e237e44SPierre Morel mutex_unlock(&kvm->lock); 25640e237e44SPierre Morel } 25650e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 25660e237e44SPierre Morel 256742104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 256842104598STony Krowiak { 256942104598STony Krowiak mutex_lock(&kvm->lock); 257042104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 257142104598STony Krowiak 257242104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 257342104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 257442104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 257542104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 257642104598STony Krowiak 25770e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 25786cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 25796cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 258042104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 258142104598STony Krowiak mutex_unlock(&kvm->lock); 258242104598STony Krowiak } 258342104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 258442104598STony Krowiak 25859bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 25869d8d5786SMichael Mueller { 25879bb0ec09SDavid Hildenbrand struct cpuid cpuid; 25889bb0ec09SDavid Hildenbrand 25899bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 25909bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 25919bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 25929d8d5786SMichael Mueller } 25939d8d5786SMichael Mueller 2594c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 25955102ee87STony Krowiak { 2596c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 259745c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 25985102ee87STony Krowiak 2599e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2600e585b24aSTony Krowiak return; 2601e585b24aSTony Krowiak 2602ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2603ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2604ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2605ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2606ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2607ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2608ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 26095102ee87STony Krowiak } 26105102ee87STony Krowiak 26117d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 26127d43bafcSEugene (jno) Dvurechenski { 26137d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 26145e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 26157d43bafcSEugene (jno) Dvurechenski else 26167d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 26177d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 26187d43bafcSEugene (jno) Dvurechenski } 26197d43bafcSEugene (jno) Dvurechenski 2620e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2621b0c632dbSHeiko Carstens { 262276a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 26239d8d5786SMichael Mueller int i, rc; 2624b0c632dbSHeiko Carstens char debug_name[16]; 2625f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2626b0c632dbSHeiko Carstens 2627e08b9637SCarsten Otte rc = -EINVAL; 2628e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2629e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2630e08b9637SCarsten Otte goto out_err; 2631e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2632e08b9637SCarsten Otte goto out_err; 2633e08b9637SCarsten Otte #else 2634e08b9637SCarsten Otte if (type) 2635e08b9637SCarsten Otte goto out_err; 2636e08b9637SCarsten Otte #endif 2637e08b9637SCarsten Otte 2638b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2639b0c632dbSHeiko Carstens if (rc) 2640d89f5effSJan Kiszka goto out_err; 2641b0c632dbSHeiko Carstens 2642b290411aSCarsten Otte rc = -ENOMEM; 2643b290411aSCarsten Otte 264476a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 264576a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 26465e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 26479ac96d75SDavid Hildenbrand /* start with basic SCA */ 264876a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2649b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2650d89f5effSJan Kiszka goto out_err; 26510d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 2652c5c2c393SDavid Hildenbrand sca_offset += 16; 2653bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2654c5c2c393SDavid Hildenbrand sca_offset = 0; 2655bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2656bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 26570d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 2658b0c632dbSHeiko Carstens 2659b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2660b0c632dbSHeiko Carstens 26611cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2662b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 266340f5b735SDominik Dingel goto out_err; 2664b0c632dbSHeiko Carstens 266519114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2666c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2667c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 2668c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 266940f5b735SDominik Dingel goto out_err; 26709d8d5786SMichael Mueller 267125c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2672c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2673c3b9e3e1SChristian Borntraeger 2674c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 2675c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] & 2676c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2677c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 2678c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] & 2679c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2680c3b9e3e1SChristian Borntraeger } 2681346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2682981467c9SMichael Mueller 26831935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 26841935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 26851935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 26861935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 268795ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 268895ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 26891bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 26901bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 26911bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 26921bab1c02SClaudio Imbrenda } 269395ca2cb5SJanosch Frank 269405f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 269505f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 269605f31e3bSPierre Morel 26979bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 269837c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 26999d8d5786SMichael Mueller 2700c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 27015102ee87STony Krowiak 270251978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2703ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 27046d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 27056d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 27068a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2707a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2708ba5c1e9bSCarsten Otte 2709b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 271078f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2711b0c632dbSHeiko Carstens 2712e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2713e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2714a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2715e08b9637SCarsten Otte } else { 271632e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2717ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 271832e6b236SGuenther Hutzl else 2719ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 272032e6b236SGuenther Hutzl sclp.hamax + 1); 27216ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2722598841caSCarsten Otte if (!kvm->arch.gmap) 272340f5b735SDominik Dingel goto out_err; 27242c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 272524eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2726e08b9637SCarsten Otte } 2727fa6b7fe9SCornelia Huck 2728c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 272955531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 27308ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2731a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2732d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 27338335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 27348ad35755SDavid Hildenbrand 2735d89f5effSJan Kiszka return 0; 2736d89f5effSJan Kiszka out_err: 2737c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 273840f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 27397d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 274078f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2741d89f5effSJan Kiszka return rc; 2742b0c632dbSHeiko Carstens } 2743b0c632dbSHeiko Carstens 2744d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2745d329c035SChristian Borntraeger { 274629b40f10SJanosch Frank u16 rc, rrc; 274729b40f10SJanosch Frank 2748d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2749ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 275067335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 27513c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2752bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2753a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 275427e0393fSCarsten Otte 275527e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 27566ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 275727e0393fSCarsten Otte 2758e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2759b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 276029b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 276129b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 276229b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 2763d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2764d329c035SChristian Borntraeger } 2765d329c035SChristian Borntraeger 2766d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 2767d329c035SChristian Borntraeger { 2768d329c035SChristian Borntraeger unsigned int i; 2769988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 2770d329c035SChristian Borntraeger 2771988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 27724543bdc0SSean Christopherson kvm_vcpu_destroy(vcpu); 2773988a2caeSGleb Natapov 2774988a2caeSGleb Natapov mutex_lock(&kvm->lock); 2775988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 2776d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 2777988a2caeSGleb Natapov 2778988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 2779988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 2780d329c035SChristian Borntraeger } 2781d329c035SChristian Borntraeger 2782b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2783b0c632dbSHeiko Carstens { 278429b40f10SJanosch Frank u16 rc, rrc; 278529b40f10SJanosch Frank 2786d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 27877d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2788d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 278929b40f10SJanosch Frank /* 279029b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 279129b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 279229b40f10SJanosch Frank * can mess with the pv state. To avoid lockdep_assert_held from 279329b40f10SJanosch Frank * complaining we do not use kvm_s390_pv_is_protected. 279429b40f10SJanosch Frank */ 279529b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 279629b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); 279729b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 2798c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 279927e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 28006ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2801841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 280267335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2803a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 28048335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2805b0c632dbSHeiko Carstens } 2806b0c632dbSHeiko Carstens 2807b0c632dbSHeiko Carstens /* Section: vcpu related */ 2808dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2809b0c632dbSHeiko Carstens { 28106ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 281127e0393fSCarsten Otte if (!vcpu->arch.gmap) 281227e0393fSCarsten Otte return -ENOMEM; 28132c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2814dafd032aSDominik Dingel 281527e0393fSCarsten Otte return 0; 281627e0393fSCarsten Otte } 281727e0393fSCarsten Otte 2818a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2819a6e2f683SEugene (jno) Dvurechenski { 2820a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2821a6940674SDavid Hildenbrand return; 28225e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 28237d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 28247d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 28257d43bafcSEugene (jno) Dvurechenski 28267d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28277d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 28287d43bafcSEugene (jno) Dvurechenski } else { 2829bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2830a6e2f683SEugene (jno) Dvurechenski 2831a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2832a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2833a6e2f683SEugene (jno) Dvurechenski } 28345e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 28357d43bafcSEugene (jno) Dvurechenski } 2836a6e2f683SEugene (jno) Dvurechenski 2837eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2838a6e2f683SEugene (jno) Dvurechenski { 2839a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2840a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2841a6940674SDavid Hildenbrand 2842a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2843a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2844a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2845f07afa04SDavid Hildenbrand return; 2846a6940674SDavid Hildenbrand } 2847eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2848eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2849eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 28507d43bafcSEugene (jno) Dvurechenski 2851eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 28527d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 28537d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 28540c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2855eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 28567d43bafcSEugene (jno) Dvurechenski } else { 2857eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2858a6e2f683SEugene (jno) Dvurechenski 2859eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2860a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2861a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2862eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2863a6e2f683SEugene (jno) Dvurechenski } 2864eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 28655e044315SEugene (jno) Dvurechenski } 28665e044315SEugene (jno) Dvurechenski 28675e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 28685e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 28695e044315SEugene (jno) Dvurechenski { 28705e044315SEugene (jno) Dvurechenski d->sda = s->sda; 28715e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 28725e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 28735e044315SEugene (jno) Dvurechenski } 28745e044315SEugene (jno) Dvurechenski 28755e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 28765e044315SEugene (jno) Dvurechenski { 28775e044315SEugene (jno) Dvurechenski int i; 28785e044315SEugene (jno) Dvurechenski 28795e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 28805e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 28815e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 28825e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 28835e044315SEugene (jno) Dvurechenski } 28845e044315SEugene (jno) Dvurechenski 28855e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 28865e044315SEugene (jno) Dvurechenski { 28875e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 28885e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 28895e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 28905e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 28915e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 28925e044315SEugene (jno) Dvurechenski 289329b40f10SJanosch Frank if (kvm->arch.use_esca) 289429b40f10SJanosch Frank return 0; 289529b40f10SJanosch Frank 28965e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 28975e044315SEugene (jno) Dvurechenski if (!new_sca) 28985e044315SEugene (jno) Dvurechenski return -ENOMEM; 28995e044315SEugene (jno) Dvurechenski 29005e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 29015e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 29025e044315SEugene (jno) Dvurechenski 29035e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 29045e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 29055e044315SEugene (jno) Dvurechenski 29065e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 29075e044315SEugene (jno) Dvurechenski 29085e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 29095e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 29105e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 29110c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 29125e044315SEugene (jno) Dvurechenski } 29135e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 29145e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 29155e044315SEugene (jno) Dvurechenski 29165e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 29175e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 29185e044315SEugene (jno) Dvurechenski 29195e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 29205e044315SEugene (jno) Dvurechenski 29218335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 29228335713aSChristian Borntraeger old_sca, kvm->arch.sca); 29235e044315SEugene (jno) Dvurechenski return 0; 29247d43bafcSEugene (jno) Dvurechenski } 2925a6e2f683SEugene (jno) Dvurechenski 2926a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2927a6e2f683SEugene (jno) Dvurechenski { 29285e044315SEugene (jno) Dvurechenski int rc; 29295e044315SEugene (jno) Dvurechenski 2930a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2931a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2932a6940674SDavid Hildenbrand return true; 2933a6940674SDavid Hildenbrand return false; 2934a6940674SDavid Hildenbrand } 29355e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 29365e044315SEugene (jno) Dvurechenski return true; 293776a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 29385e044315SEugene (jno) Dvurechenski return false; 29395e044315SEugene (jno) Dvurechenski 29405e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 29415e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 29425e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 29435e044315SEugene (jno) Dvurechenski 29445e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2945a6e2f683SEugene (jno) Dvurechenski } 2946a6e2f683SEugene (jno) Dvurechenski 2947db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2948db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2949db0758b2SDavid Hildenbrand { 2950db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 29519c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2952db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 29539c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2954db0758b2SDavid Hildenbrand } 2955db0758b2SDavid Hildenbrand 2956db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2957db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2958db0758b2SDavid Hildenbrand { 2959db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 29609c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2961db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 2962db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 29639c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2964db0758b2SDavid Hildenbrand } 2965db0758b2SDavid Hildenbrand 2966db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2967db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2968db0758b2SDavid Hildenbrand { 2969db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 2970db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 2971db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 2972db0758b2SDavid Hildenbrand } 2973db0758b2SDavid Hildenbrand 2974db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2975db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2976db0758b2SDavid Hildenbrand { 2977db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 2978db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2979db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 2980db0758b2SDavid Hildenbrand } 2981db0758b2SDavid Hildenbrand 2982db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2983db0758b2SDavid Hildenbrand { 2984db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2985db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 2986db0758b2SDavid Hildenbrand preempt_enable(); 2987db0758b2SDavid Hildenbrand } 2988db0758b2SDavid Hildenbrand 2989db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2990db0758b2SDavid Hildenbrand { 2991db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2992db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 2993db0758b2SDavid Hildenbrand preempt_enable(); 2994db0758b2SDavid Hildenbrand } 2995db0758b2SDavid Hildenbrand 29964287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 29974287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 29984287f247SDavid Hildenbrand { 2999db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 30009c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3001db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3002db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 30034287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 30049c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3005db0758b2SDavid Hildenbrand preempt_enable(); 30064287f247SDavid Hildenbrand } 30074287f247SDavid Hildenbrand 3008db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 30094287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 30104287f247SDavid Hildenbrand { 30119c23a131SDavid Hildenbrand unsigned int seq; 3012db0758b2SDavid Hildenbrand __u64 value; 3013db0758b2SDavid Hildenbrand 3014db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 30154287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3016db0758b2SDavid Hildenbrand 30179c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 30189c23a131SDavid Hildenbrand do { 30199c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 30209c23a131SDavid Hildenbrand /* 30219c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 30229c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 30239c23a131SDavid Hildenbrand */ 30249c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3025db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 30269c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 30279c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3028db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 30299c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 30309c23a131SDavid Hildenbrand preempt_enable(); 3031db0758b2SDavid Hildenbrand return value; 30324287f247SDavid Hildenbrand } 30334287f247SDavid Hildenbrand 3034b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3035b0c632dbSHeiko Carstens { 30369977e886SHendrik Brueckner 303737d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3038ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 30395ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3040db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 304101a745acSDavid Hildenbrand vcpu->cpu = cpu; 3042b0c632dbSHeiko Carstens } 3043b0c632dbSHeiko Carstens 3044b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3045b0c632dbSHeiko Carstens { 304601a745acSDavid Hildenbrand vcpu->cpu = -1; 30475ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3048db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 30499daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 305037d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 305137d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 30529977e886SHendrik Brueckner 3053b0c632dbSHeiko Carstens } 3054b0c632dbSHeiko Carstens 305531928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 305642897d86SMarcelo Tosatti { 305772f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3058fdf03650SFan Zhang preempt_disable(); 305972f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3060d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3061fdf03650SFan Zhang preempt_enable(); 306272f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 306325508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3064dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3065eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 306625508824SDavid Hildenbrand } 30676502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 30686502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 306937d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 307037d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 307142897d86SMarcelo Tosatti } 307242897d86SMarcelo Tosatti 30738ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 30748ec2fa52SChristian Borntraeger { 30758ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 30768ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 30778ec2fa52SChristian Borntraeger return true; 30788ec2fa52SChristian Borntraeger return false; 30798ec2fa52SChristian Borntraeger } 30808ec2fa52SChristian Borntraeger 30818ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 30828ec2fa52SChristian Borntraeger { 30838ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 30848ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 30858ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 30868ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 30878ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 30888ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 30898ec2fa52SChristian Borntraeger 30908ec2fa52SChristian Borntraeger } 30918ec2fa52SChristian Borntraeger 30925102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 30935102ee87STony Krowiak { 3094e585b24aSTony Krowiak /* 3095e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3096e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3097e585b24aSTony Krowiak */ 3098e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 30995102ee87STony Krowiak return; 31005102ee87STony Krowiak 3101e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3102a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 310337940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 31048ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3105a374e892STony Krowiak 3106e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3107e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3108e585b24aSTony Krowiak 3109e585b24aSTony Krowiak /* Set up protected key support */ 31108ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3111a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 31128ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 31138ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 31148ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 31158ec2fa52SChristian Borntraeger } 31168ec2fa52SChristian Borntraeger 3117a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3118a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 31195102ee87STony Krowiak } 31205102ee87STony Krowiak 3121b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3122b31605c1SDominik Dingel { 3123b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 3124b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3125b31605c1SDominik Dingel } 3126b31605c1SDominik Dingel 3127b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3128b31605c1SDominik Dingel { 3129b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 3130b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 3131b31605c1SDominik Dingel return -ENOMEM; 3132b31605c1SDominik Dingel return 0; 3133b31605c1SDominik Dingel } 3134b31605c1SDominik Dingel 313591520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 313691520f1aSMichael Mueller { 313791520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 313891520f1aSMichael Mueller 313991520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 314080bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3141c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 314291520f1aSMichael Mueller } 314391520f1aSMichael Mueller 3144ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3145ff72bb55SSean Christopherson { 3146b31605c1SDominik Dingel int rc = 0; 314729b40f10SJanosch Frank u16 uvrc, uvrrc; 3148b31288faSKonstantin Weitz 31499e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 31509e6dabefSCornelia Huck CPUSTAT_SM | 3151a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3152a4a4f191SGuenther Hutzl 315353df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3154ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 315553df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3156ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3157a4a4f191SGuenther Hutzl 315891520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 315991520f1aSMichael Mueller 3160bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3161bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 31620c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3163bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 31640c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 3165f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 31660c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 31677feb6bb8SMichael Mueller 3168c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 31690c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3170cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 31710c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 31720c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 317348ee7d3aSDavid Hildenbrand if (sclp.has_cei) 31740c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 317511ad65b7SDavid Hildenbrand if (sclp.has_ib) 31760c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 317737c5f6c8SDavid Hildenbrand if (sclp.has_siif) 31780c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 317937c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 31800c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 318118280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 31820c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 31830c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 318413211ea7SEric Farman } 31858fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 31868fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3187a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3188a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3189d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3190d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3191d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3192d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3193d7c5cb01SMichael Mueller } 31944e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 31954e0b1ab7SFan Zhang | SDNXC; 3196c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 3197730cd632SFarhan Ali 3198730cd632SFarhan Ali if (sclp.has_kss) 3199ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3200730cd632SFarhan Ali else 3201492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 32025a5e6536SMatthew Rosato 3203e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3204b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3205b31605c1SDominik Dingel if (rc) 3206b31605c1SDominik Dingel return rc; 3207b31288faSKonstantin Weitz } 32080ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3209ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 32109d8d5786SMichael Mueller 321167d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 321267d49d52SCollin Walling 32135102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 32145102ee87STony Krowiak 321529b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 321629b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 321729b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 321829b40f10SJanosch Frank if (rc) 321929b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 322029b40f10SJanosch Frank } 322129b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 322229b40f10SJanosch Frank 3223b31605c1SDominik Dingel return rc; 3224b0c632dbSHeiko Carstens } 3225b0c632dbSHeiko Carstens 3226897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3227897cc38eSSean Christopherson { 3228897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3229897cc38eSSean Christopherson return -EINVAL; 3230897cc38eSSean Christopherson return 0; 3231897cc38eSSean Christopherson } 3232897cc38eSSean Christopherson 3233e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3234b0c632dbSHeiko Carstens { 32357feb6bb8SMichael Mueller struct sie_page *sie_page; 3236897cc38eSSean Christopherson int rc; 32374d47555aSCarsten Otte 3238da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 32397feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 32407feb6bb8SMichael Mueller if (!sie_page) 3241e529ef66SSean Christopherson return -ENOMEM; 3242b0c632dbSHeiko Carstens 32437feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 32447feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 32457feb6bb8SMichael Mueller 3246efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3247efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3248efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3249efed1104SDavid Hildenbrand 3250e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3251ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3252e529ef66SSean Christopherson vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin; 32534b9f9525SMichael Mueller if (vcpu->arch.sie_block->gd && sclp.has_gisaf) 32544b9f9525SMichael Mueller vcpu->arch.sie_block->gd |= GISA_FORMAT1; 32559c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3256ba5c1e9bSCarsten Otte 3257321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3258321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3259321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3260321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3261321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3262321f8ee5SSean Christopherson KVM_SYNC_CRS | 3263321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 3264321f8ee5SSean Christopherson KVM_SYNC_PFAULT; 3265321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3266321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3267321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3268321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3269321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3270321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3271321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3272321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3273321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3274321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3275321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3276321f8ee5SSean Christopherson */ 3277321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3278321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3279321f8ee5SSean Christopherson else 3280321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3281321f8ee5SSean Christopherson 3282321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3283321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3284321f8ee5SSean Christopherson if (rc) 3285a2017f17SSean Christopherson goto out_free_sie_block; 3286321f8ee5SSean Christopherson } 3287321f8ee5SSean Christopherson 3288e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3289e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3290e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3291b0c632dbSHeiko Carstens 3292ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3293ff72bb55SSean Christopherson if (rc) 3294ff72bb55SSean Christopherson goto out_ucontrol_uninit; 3295e529ef66SSean Christopherson return 0; 3296e529ef66SSean Christopherson 3297ff72bb55SSean Christopherson out_ucontrol_uninit: 3298ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3299ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 33007b06bf2fSWei Yongjun out_free_sie_block: 33017b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3302e529ef66SSean Christopherson return rc; 3303b0c632dbSHeiko Carstens } 3304b0c632dbSHeiko Carstens 3305b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3306b0c632dbSHeiko Carstens { 33079a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3308b0c632dbSHeiko Carstens } 3309b0c632dbSHeiko Carstens 3310199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3311199b5763SLongpeng(Mike) { 33120546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3313199b5763SLongpeng(Mike) } 3314199b5763SLongpeng(Mike) 331527406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 331649b99e1eSChristian Borntraeger { 3317805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 331861a6df54SDavid Hildenbrand exit_sie(vcpu); 331949b99e1eSChristian Borntraeger } 332049b99e1eSChristian Borntraeger 332127406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 332249b99e1eSChristian Borntraeger { 3323805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 332449b99e1eSChristian Borntraeger } 332549b99e1eSChristian Borntraeger 33268e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 33278e236546SChristian Borntraeger { 3328805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 332961a6df54SDavid Hildenbrand exit_sie(vcpu); 33308e236546SChristian Borntraeger } 33318e236546SChristian Borntraeger 33329ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 33339ea59728SDavid Hildenbrand { 33349ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 33359ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 33369ea59728SDavid Hildenbrand } 33379ea59728SDavid Hildenbrand 33388e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 33398e236546SChristian Borntraeger { 33409bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 33418e236546SChristian Borntraeger } 33428e236546SChristian Borntraeger 334349b99e1eSChristian Borntraeger /* 33449ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 334549b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 334649b99e1eSChristian Borntraeger * return immediately. */ 334749b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 334849b99e1eSChristian Borntraeger { 3349ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 33509ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 335149b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 335249b99e1eSChristian Borntraeger cpu_relax(); 335349b99e1eSChristian Borntraeger } 335449b99e1eSChristian Borntraeger 33558e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 33568e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 335749b99e1eSChristian Borntraeger { 33588e236546SChristian Borntraeger kvm_make_request(req, vcpu); 33598e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 336049b99e1eSChristian Borntraeger } 336149b99e1eSChristian Borntraeger 3362414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3363414d3b07SMartin Schwidefsky unsigned long end) 33642c70fe44SChristian Borntraeger { 33652c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 33662c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3367414d3b07SMartin Schwidefsky unsigned long prefix; 3368414d3b07SMartin Schwidefsky int i; 33692c70fe44SChristian Borntraeger 337065d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 337165d0b0d4SDavid Hildenbrand return; 3372414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3373414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3374414d3b07SMartin Schwidefsky return; 33752c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 33762c70fe44SChristian Borntraeger /* match against both prefix pages */ 3377414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3378414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3379414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3380414d3b07SMartin Schwidefsky start, end); 33818e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 33822c70fe44SChristian Borntraeger } 33832c70fe44SChristian Borntraeger } 33842c70fe44SChristian Borntraeger } 33852c70fe44SChristian Borntraeger 33868b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 33878b905d28SChristian Borntraeger { 33888b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 33898b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 33908b905d28SChristian Borntraeger halt_poll_max_steal) { 33918b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 33928b905d28SChristian Borntraeger return true; 33938b905d28SChristian Borntraeger } 33948b905d28SChristian Borntraeger return false; 33958b905d28SChristian Borntraeger } 33968b905d28SChristian Borntraeger 3397b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3398b6d33834SChristoffer Dall { 3399b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3400b6d33834SChristoffer Dall BUG(); 3401b6d33834SChristoffer Dall return 0; 3402b6d33834SChristoffer Dall } 3403b6d33834SChristoffer Dall 340414eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 340514eebd91SCarsten Otte struct kvm_one_reg *reg) 340614eebd91SCarsten Otte { 340714eebd91SCarsten Otte int r = -EINVAL; 340814eebd91SCarsten Otte 340914eebd91SCarsten Otte switch (reg->id) { 341029b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 341129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 341229b7c71bSCarsten Otte (u32 __user *)reg->addr); 341329b7c71bSCarsten Otte break; 341429b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 341529b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 341629b7c71bSCarsten Otte (u64 __user *)reg->addr); 341729b7c71bSCarsten Otte break; 341846a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 34194287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 342046a6dd1cSJason J. herne (u64 __user *)reg->addr); 342146a6dd1cSJason J. herne break; 342246a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 342346a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 342446a6dd1cSJason J. herne (u64 __user *)reg->addr); 342546a6dd1cSJason J. herne break; 3426536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3427536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3428536336c2SDominik Dingel (u64 __user *)reg->addr); 3429536336c2SDominik Dingel break; 3430536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3431536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3432536336c2SDominik Dingel (u64 __user *)reg->addr); 3433536336c2SDominik Dingel break; 3434536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3435536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3436536336c2SDominik Dingel (u64 __user *)reg->addr); 3437536336c2SDominik Dingel break; 3438672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3439672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3440672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3441672550fbSChristian Borntraeger break; 3442afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3443afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3444afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3445afa45ff5SChristian Borntraeger break; 344614eebd91SCarsten Otte default: 344714eebd91SCarsten Otte break; 344814eebd91SCarsten Otte } 344914eebd91SCarsten Otte 345014eebd91SCarsten Otte return r; 345114eebd91SCarsten Otte } 345214eebd91SCarsten Otte 345314eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 345414eebd91SCarsten Otte struct kvm_one_reg *reg) 345514eebd91SCarsten Otte { 345614eebd91SCarsten Otte int r = -EINVAL; 34574287f247SDavid Hildenbrand __u64 val; 345814eebd91SCarsten Otte 345914eebd91SCarsten Otte switch (reg->id) { 346029b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 346129b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 346229b7c71bSCarsten Otte (u32 __user *)reg->addr); 346329b7c71bSCarsten Otte break; 346429b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 346529b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 346629b7c71bSCarsten Otte (u64 __user *)reg->addr); 346729b7c71bSCarsten Otte break; 346846a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 34694287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 34704287f247SDavid Hildenbrand if (!r) 34714287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 347246a6dd1cSJason J. herne break; 347346a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 347446a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 347546a6dd1cSJason J. herne (u64 __user *)reg->addr); 347646a6dd1cSJason J. herne break; 3477536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3478536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3479536336c2SDominik Dingel (u64 __user *)reg->addr); 34809fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 34819fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3482536336c2SDominik Dingel break; 3483536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3484536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3485536336c2SDominik Dingel (u64 __user *)reg->addr); 3486536336c2SDominik Dingel break; 3487536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3488536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3489536336c2SDominik Dingel (u64 __user *)reg->addr); 3490536336c2SDominik Dingel break; 3491672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3492672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3493672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3494672550fbSChristian Borntraeger break; 3495afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3496afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3497afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3498afa45ff5SChristian Borntraeger break; 349914eebd91SCarsten Otte default: 350014eebd91SCarsten Otte break; 350114eebd91SCarsten Otte } 350214eebd91SCarsten Otte 350314eebd91SCarsten Otte return r; 350414eebd91SCarsten Otte } 3505b6d33834SChristoffer Dall 35067de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 3507b0c632dbSHeiko Carstens { 35087de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 35097de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 35107de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 35117de3f142SJanosch Frank 35127de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 35137de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 35147de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 35157de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 35167de3f142SJanosch Frank } 35177de3f142SJanosch Frank 35187de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 35197de3f142SJanosch Frank { 35207de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 35217de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 35227de3f142SJanosch Frank 35237de3f142SJanosch Frank /* this equals initial cpu reset in pop, but we don't switch to ESA */ 35247de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 35257de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 35267de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 35277de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 35287de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 35297de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 35307de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 35317de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 35327de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 35330f303504SJanosch Frank /* 35340f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 35350f303504SJanosch Frank * them are overlayed and they are not accessible in this case 35360f303504SJanosch Frank * anyway. 35370f303504SJanosch Frank */ 35380f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 35397de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 35407de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 35417de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 35420f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 35430f303504SJanosch Frank } 35447de3f142SJanosch Frank } 35457de3f142SJanosch Frank 35467de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 35477de3f142SJanosch Frank { 35487de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 35497de3f142SJanosch Frank 35507de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 35517de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 35527de3f142SJanosch Frank 35537de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 35547de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 35557de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 35567de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 35577de3f142SJanosch Frank 35587de3f142SJanosch Frank regs->etoken = 0; 35597de3f142SJanosch Frank regs->etoken_extension = 0; 3560b0c632dbSHeiko Carstens } 3561b0c632dbSHeiko Carstens 3562b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3563b0c632dbSHeiko Carstens { 3564875656feSChristoffer Dall vcpu_load(vcpu); 35655a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3566875656feSChristoffer Dall vcpu_put(vcpu); 3567b0c632dbSHeiko Carstens return 0; 3568b0c632dbSHeiko Carstens } 3569b0c632dbSHeiko Carstens 3570b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3571b0c632dbSHeiko Carstens { 35721fc9b76bSChristoffer Dall vcpu_load(vcpu); 35735a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 35741fc9b76bSChristoffer Dall vcpu_put(vcpu); 3575b0c632dbSHeiko Carstens return 0; 3576b0c632dbSHeiko Carstens } 3577b0c632dbSHeiko Carstens 3578b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3579b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3580b0c632dbSHeiko Carstens { 3581b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3582b4ef9d4eSChristoffer Dall 358359674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3584b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3585b4ef9d4eSChristoffer Dall 3586b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3587b0c632dbSHeiko Carstens return 0; 3588b0c632dbSHeiko Carstens } 3589b0c632dbSHeiko Carstens 3590b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3591b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3592b0c632dbSHeiko Carstens { 3593bcdec41cSChristoffer Dall vcpu_load(vcpu); 3594bcdec41cSChristoffer Dall 359559674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3596b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3597bcdec41cSChristoffer Dall 3598bcdec41cSChristoffer Dall vcpu_put(vcpu); 3599b0c632dbSHeiko Carstens return 0; 3600b0c632dbSHeiko Carstens } 3601b0c632dbSHeiko Carstens 3602b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3603b0c632dbSHeiko Carstens { 36046a96bc7fSChristoffer Dall int ret = 0; 36056a96bc7fSChristoffer Dall 36066a96bc7fSChristoffer Dall vcpu_load(vcpu); 36076a96bc7fSChristoffer Dall 36086a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 36096a96bc7fSChristoffer Dall ret = -EINVAL; 36106a96bc7fSChristoffer Dall goto out; 36116a96bc7fSChristoffer Dall } 3612e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 36139abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3614a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3615a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 36169abc2a08SDavid Hildenbrand else 3617a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 36186a96bc7fSChristoffer Dall 36196a96bc7fSChristoffer Dall out: 36206a96bc7fSChristoffer Dall vcpu_put(vcpu); 36216a96bc7fSChristoffer Dall return ret; 3622b0c632dbSHeiko Carstens } 3623b0c632dbSHeiko Carstens 3624b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3625b0c632dbSHeiko Carstens { 36261393123eSChristoffer Dall vcpu_load(vcpu); 36271393123eSChristoffer Dall 36289abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 36299abc2a08SDavid Hildenbrand save_fpu_regs(); 36309abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3631a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3632a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 36339abc2a08SDavid Hildenbrand else 3634a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3635e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 36361393123eSChristoffer Dall 36371393123eSChristoffer Dall vcpu_put(vcpu); 3638b0c632dbSHeiko Carstens return 0; 3639b0c632dbSHeiko Carstens } 3640b0c632dbSHeiko Carstens 3641b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3642b0c632dbSHeiko Carstens { 3643b0c632dbSHeiko Carstens int rc = 0; 3644b0c632dbSHeiko Carstens 36457a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3646b0c632dbSHeiko Carstens rc = -EBUSY; 3647d7b0b5ebSCarsten Otte else { 3648d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3649d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3650d7b0b5ebSCarsten Otte } 3651b0c632dbSHeiko Carstens return rc; 3652b0c632dbSHeiko Carstens } 3653b0c632dbSHeiko Carstens 3654b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3655b0c632dbSHeiko Carstens struct kvm_translation *tr) 3656b0c632dbSHeiko Carstens { 3657b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3658b0c632dbSHeiko Carstens } 3659b0c632dbSHeiko Carstens 366027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 366127291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 366227291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 366327291e21SDavid Hildenbrand 3664d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3665d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3666b0c632dbSHeiko Carstens { 366727291e21SDavid Hildenbrand int rc = 0; 366827291e21SDavid Hildenbrand 366966b56562SChristoffer Dall vcpu_load(vcpu); 367066b56562SChristoffer Dall 367127291e21SDavid Hildenbrand vcpu->guest_debug = 0; 367227291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 367327291e21SDavid Hildenbrand 367466b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 367566b56562SChristoffer Dall rc = -EINVAL; 367666b56562SChristoffer Dall goto out; 367766b56562SChristoffer Dall } 367866b56562SChristoffer Dall if (!sclp.has_gpere) { 367966b56562SChristoffer Dall rc = -EINVAL; 368066b56562SChristoffer Dall goto out; 368166b56562SChristoffer Dall } 368227291e21SDavid Hildenbrand 368327291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 368427291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 368527291e21SDavid Hildenbrand /* enforce guest PER */ 3686ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 368727291e21SDavid Hildenbrand 368827291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 368927291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 369027291e21SDavid Hildenbrand } else { 36919daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 369227291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 369327291e21SDavid Hildenbrand } 369427291e21SDavid Hildenbrand 369527291e21SDavid Hildenbrand if (rc) { 369627291e21SDavid Hildenbrand vcpu->guest_debug = 0; 369727291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 36989daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 369927291e21SDavid Hildenbrand } 370027291e21SDavid Hildenbrand 370166b56562SChristoffer Dall out: 370266b56562SChristoffer Dall vcpu_put(vcpu); 370327291e21SDavid Hildenbrand return rc; 3704b0c632dbSHeiko Carstens } 3705b0c632dbSHeiko Carstens 370662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 370762d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 370862d9f0dbSMarcelo Tosatti { 3709fd232561SChristoffer Dall int ret; 3710fd232561SChristoffer Dall 3711fd232561SChristoffer Dall vcpu_load(vcpu); 3712fd232561SChristoffer Dall 37136352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3714fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 37156352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3716fd232561SChristoffer Dall 3717fd232561SChristoffer Dall vcpu_put(vcpu); 3718fd232561SChristoffer Dall return ret; 371962d9f0dbSMarcelo Tosatti } 372062d9f0dbSMarcelo Tosatti 372162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 372262d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 372362d9f0dbSMarcelo Tosatti { 37246352e4d2SDavid Hildenbrand int rc = 0; 37256352e4d2SDavid Hildenbrand 3726e83dff5eSChristoffer Dall vcpu_load(vcpu); 3727e83dff5eSChristoffer Dall 37286352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 37296352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 37306352e4d2SDavid Hildenbrand 37316352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 37326352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 3733fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 37346352e4d2SDavid Hildenbrand break; 37356352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 3736fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 37376352e4d2SDavid Hildenbrand break; 37386352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 3739*7c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 3740*7c36a3fcSJanosch Frank rc = -ENXIO; 3741*7c36a3fcSJanosch Frank break; 3742*7c36a3fcSJanosch Frank } 3743*7c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 3744*7c36a3fcSJanosch Frank break; 37456352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 37466352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 37476352e4d2SDavid Hildenbrand default: 37486352e4d2SDavid Hildenbrand rc = -ENXIO; 37496352e4d2SDavid Hildenbrand } 37506352e4d2SDavid Hildenbrand 3751e83dff5eSChristoffer Dall vcpu_put(vcpu); 37526352e4d2SDavid Hildenbrand return rc; 375362d9f0dbSMarcelo Tosatti } 375462d9f0dbSMarcelo Tosatti 37558ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 37568ad35755SDavid Hildenbrand { 37578d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 37588ad35755SDavid Hildenbrand } 37598ad35755SDavid Hildenbrand 37602c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 37612c70fe44SChristian Borntraeger { 37628ad35755SDavid Hildenbrand retry: 37638e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 37642fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3765586b7ccdSChristian Borntraeger return 0; 37662c70fe44SChristian Borntraeger /* 37672c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3768b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 37692c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 37702c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 37712c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 37722c70fe44SChristian Borntraeger */ 37738ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 37742c70fe44SChristian Borntraeger int rc; 3775b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3776fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3777b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3778aca411a4SJulius Niedworok if (rc) { 3779aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 37802c70fe44SChristian Borntraeger return rc; 3781aca411a4SJulius Niedworok } 37828ad35755SDavid Hildenbrand goto retry; 37832c70fe44SChristian Borntraeger } 37848ad35755SDavid Hildenbrand 3785d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3786d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3787d3d692c8SDavid Hildenbrand goto retry; 3788d3d692c8SDavid Hildenbrand } 3789d3d692c8SDavid Hildenbrand 37908ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 37918ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 37928ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3793ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 37948ad35755SDavid Hildenbrand } 37958ad35755SDavid Hildenbrand goto retry; 37968ad35755SDavid Hildenbrand } 37978ad35755SDavid Hildenbrand 37988ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 37998ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 38008ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 38019daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 38028ad35755SDavid Hildenbrand } 38038ad35755SDavid Hildenbrand goto retry; 38048ad35755SDavid Hildenbrand } 38058ad35755SDavid Hildenbrand 38066502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 38076502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 38086502a34cSDavid Hildenbrand goto retry; 38096502a34cSDavid Hildenbrand } 38106502a34cSDavid Hildenbrand 3811190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3812190df4a2SClaudio Imbrenda /* 3813c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3814190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3815190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3816190df4a2SClaudio Imbrenda */ 3817190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3818190df4a2SClaudio Imbrenda goto retry; 3819190df4a2SClaudio Imbrenda } 3820190df4a2SClaudio Imbrenda 3821190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3822190df4a2SClaudio Imbrenda /* 3823c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3824c9f0a2b8SJanosch Frank * CMM has been used. 3825190df4a2SClaudio Imbrenda */ 3826190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3827c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3828190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3829190df4a2SClaudio Imbrenda goto retry; 3830190df4a2SClaudio Imbrenda } 3831190df4a2SClaudio Imbrenda 38320759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 383372875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 38343194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 38353194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 38360759d068SDavid Hildenbrand 38372c70fe44SChristian Borntraeger return 0; 38382c70fe44SChristian Borntraeger } 38392c70fe44SChristian Borntraeger 38400e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, 38418fa1696eSCollin L. Walling const struct kvm_s390_vm_tod_clock *gtod) 38428fa1696eSCollin L. Walling { 38438fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 38448fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 38458fa1696eSCollin L. Walling int i; 38468fa1696eSCollin L. Walling 38478fa1696eSCollin L. Walling mutex_lock(&kvm->lock); 38488fa1696eSCollin L. Walling preempt_disable(); 38498fa1696eSCollin L. Walling 38508fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 38518fa1696eSCollin L. Walling 38528fa1696eSCollin L. Walling kvm->arch.epoch = gtod->tod - htod.tod; 38530e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 38540e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 38558fa1696eSCollin L. Walling kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx; 38568fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 38578fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 38580e7def5fSDavid Hildenbrand } 38598fa1696eSCollin L. Walling 38608fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 38618fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 38628fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 38638fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 38648fa1696eSCollin L. Walling } 38658fa1696eSCollin L. Walling 38668fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 38678fa1696eSCollin L. Walling preempt_enable(); 38688fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 38698fa1696eSCollin L. Walling } 38708fa1696eSCollin L. Walling 3871fa576c58SThomas Huth /** 3872fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 3873fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 3874fa576c58SThomas Huth * @gpa: Guest physical address 3875fa576c58SThomas Huth * @writable: Whether the page should be writable or not 3876fa576c58SThomas Huth * 3877fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 3878fa576c58SThomas Huth * 3879fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 3880fa576c58SThomas Huth */ 3881fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 388224eb3a82SDominik Dingel { 3883527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 3884527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 388524eb3a82SDominik Dingel } 388624eb3a82SDominik Dingel 38873c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 38883c038e6bSDominik Dingel unsigned long token) 38893c038e6bSDominik Dingel { 38903c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 3891383d0b05SJens Freimann struct kvm_s390_irq irq; 38923c038e6bSDominik Dingel 38933c038e6bSDominik Dingel if (start_token) { 3894383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 3895383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 3896383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 38973c038e6bSDominik Dingel } else { 38983c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 3899383d0b05SJens Freimann inti.parm64 = token; 39003c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 39013c038e6bSDominik Dingel } 39023c038e6bSDominik Dingel } 39033c038e6bSDominik Dingel 39043c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 39053c038e6bSDominik Dingel struct kvm_async_pf *work) 39063c038e6bSDominik Dingel { 39073c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 39083c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 39093c038e6bSDominik Dingel } 39103c038e6bSDominik Dingel 39113c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 39123c038e6bSDominik Dingel struct kvm_async_pf *work) 39133c038e6bSDominik Dingel { 39143c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 39153c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 39163c038e6bSDominik Dingel } 39173c038e6bSDominik Dingel 39183c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 39193c038e6bSDominik Dingel struct kvm_async_pf *work) 39203c038e6bSDominik Dingel { 39213c038e6bSDominik Dingel /* s390 will always inject the page directly */ 39223c038e6bSDominik Dingel } 39233c038e6bSDominik Dingel 39243c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 39253c038e6bSDominik Dingel { 39263c038e6bSDominik Dingel /* 39273c038e6bSDominik Dingel * s390 will always inject the page directly, 39283c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 39293c038e6bSDominik Dingel */ 39303c038e6bSDominik Dingel return true; 39313c038e6bSDominik Dingel } 39323c038e6bSDominik Dingel 39333c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 39343c038e6bSDominik Dingel { 39353c038e6bSDominik Dingel hva_t hva; 39363c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 39373c038e6bSDominik Dingel int rc; 39383c038e6bSDominik Dingel 39393c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 39403c038e6bSDominik Dingel return 0; 39413c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 39423c038e6bSDominik Dingel vcpu->arch.pfault_compare) 39433c038e6bSDominik Dingel return 0; 39443c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 39453c038e6bSDominik Dingel return 0; 39469a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 39473c038e6bSDominik Dingel return 0; 3948b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 39493c038e6bSDominik Dingel return 0; 39503c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 39513c038e6bSDominik Dingel return 0; 39523c038e6bSDominik Dingel 395381480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 395481480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 395581480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 39563c038e6bSDominik Dingel return 0; 39573c038e6bSDominik Dingel 39583c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 39593c038e6bSDominik Dingel return rc; 39603c038e6bSDominik Dingel } 39613c038e6bSDominik Dingel 39623fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 3963b0c632dbSHeiko Carstens { 39643fb4c40fSThomas Huth int rc, cpuflags; 3965e168bf8dSCarsten Otte 39663c038e6bSDominik Dingel /* 39673c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 39683c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 39693c038e6bSDominik Dingel * handled outside the worker. 39703c038e6bSDominik Dingel */ 39713c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 39723c038e6bSDominik Dingel 39737ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 39747ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 3975b0c632dbSHeiko Carstens 3976b0c632dbSHeiko Carstens if (need_resched()) 3977b0c632dbSHeiko Carstens schedule(); 3978b0c632dbSHeiko Carstens 3979d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 398071cde587SChristian Borntraeger s390_handle_mcck(); 398171cde587SChristian Borntraeger 398279395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 398379395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 398479395031SJens Freimann if (rc) 398579395031SJens Freimann return rc; 398679395031SJens Freimann } 39870ff31867SCarsten Otte 39882c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 39892c70fe44SChristian Borntraeger if (rc) 39902c70fe44SChristian Borntraeger return rc; 39912c70fe44SChristian Borntraeger 399227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 399327291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 399427291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 399527291e21SDavid Hildenbrand } 399627291e21SDavid Hildenbrand 39979f30f621SMichael Mueller clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask); 39989f30f621SMichael Mueller 3999b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 40003fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 40013fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 40023fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 40032b29a9fdSDominik Dingel 40043fb4c40fSThomas Huth return 0; 40053fb4c40fSThomas Huth } 40063fb4c40fSThomas Huth 4007492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4008492d8642SThomas Huth { 400956317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 401056317920SDavid Hildenbrand .code = PGM_ADDRESSING, 401156317920SDavid Hildenbrand }; 401256317920SDavid Hildenbrand u8 opcode, ilen; 4013492d8642SThomas Huth int rc; 4014492d8642SThomas Huth 4015492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4016492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4017492d8642SThomas Huth 4018492d8642SThomas Huth /* 4019492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4020492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4021492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4022492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4023492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4024492d8642SThomas Huth * to be able to forward the PSW. 4025492d8642SThomas Huth */ 40263fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 402756317920SDavid Hildenbrand ilen = insn_length(opcode); 40289b0d721aSDavid Hildenbrand if (rc < 0) { 40299b0d721aSDavid Hildenbrand return rc; 40309b0d721aSDavid Hildenbrand } else if (rc) { 40319b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 40329b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 40339b0d721aSDavid Hildenbrand * nullification if necessary. 40349b0d721aSDavid Hildenbrand */ 40359b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 40369b0d721aSDavid Hildenbrand ilen = 4; 40379b0d721aSDavid Hildenbrand } 403856317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 403956317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 404056317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4041492d8642SThomas Huth } 4042492d8642SThomas Huth 40433fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 40443fb4c40fSThomas Huth { 40454d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 40464d62fcc0SQingFeng Hao struct sie_page *sie_page; 40474d62fcc0SQingFeng Hao 40482b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 40492b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 40502b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 40512b29a9fdSDominik Dingel 405227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 405327291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 405427291e21SDavid Hildenbrand 40557ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 40567ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 405771f116bfSDavid Hildenbrand 40584d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 40594d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 40604d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 40614d62fcc0SQingFeng Hao struct sie_page, sie_block); 40624d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 40634d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 40644d62fcc0SQingFeng Hao return 0; 40654d62fcc0SQingFeng Hao } 40664d62fcc0SQingFeng Hao 406771f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 406871f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 406971f116bfSDavid Hildenbrand 407071f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 407171f116bfSDavid Hildenbrand return rc; 407271f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 407371f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 407471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 407571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 407671f116bfSDavid Hildenbrand return -EREMOTE; 407771f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 407871f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 407971f116bfSDavid Hildenbrand return 0; 4080210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4081210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4082210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4083210b1607SThomas Huth current->thread.gmap_addr; 4084210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 408571f116bfSDavid Hildenbrand return -EREMOTE; 408624eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 40873c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 408824eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 408971f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 409071f116bfSDavid Hildenbrand return 0; 409171f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4092fa576c58SThomas Huth } 409371f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 40943fb4c40fSThomas Huth } 40953fb4c40fSThomas Huth 40963fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 40973fb4c40fSThomas Huth { 40983fb4c40fSThomas Huth int rc, exit_reason; 4099c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 41003fb4c40fSThomas Huth 4101800c1065SThomas Huth /* 4102800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4103800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4104800c1065SThomas Huth */ 4105800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 4106800c1065SThomas Huth 4107a76ccff6SThomas Huth do { 41083fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 41093fb4c40fSThomas Huth if (rc) 4110a76ccff6SThomas Huth break; 41113fb4c40fSThomas Huth 4112800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 41133fb4c40fSThomas Huth /* 4114a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4115a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 41163fb4c40fSThomas Huth */ 41170097d12eSChristian Borntraeger local_irq_disable(); 41186edaa530SPaolo Bonzini guest_enter_irqoff(); 4119db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 41200097d12eSChristian Borntraeger local_irq_enable(); 4121c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4122c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4123c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4124c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4125c8aac234SJanosch Frank } 4126a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4127a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4128c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4129c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4130c8aac234SJanosch Frank sie_page->pv_grregs, 4131c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4132c8aac234SJanosch Frank } 41330097d12eSChristian Borntraeger local_irq_disable(); 4134db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 41356edaa530SPaolo Bonzini guest_exit_irqoff(); 41360097d12eSChristian Borntraeger local_irq_enable(); 4137800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 41383fb4c40fSThomas Huth 41393fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 414027291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 41413fb4c40fSThomas Huth 4142800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 4143e168bf8dSCarsten Otte return rc; 4144b0c632dbSHeiko Carstens } 4145b0c632dbSHeiko Carstens 4146811ea797SJanosch Frank static void sync_regs_fmt2(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4147b028ee3eSDavid Hildenbrand { 41484d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 41494e0b1ab7SFan Zhang struct gs_cb *gscb; 41504d5f2c04SChristian Borntraeger 41514d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 41524e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4153b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4154b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4155b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4156b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4157b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4158b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4159b028ee3eSDavid Hildenbrand } 4160b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4161b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4162b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4163b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 41649fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 41659fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4166b028ee3eSDavid Hildenbrand } 416780cd8763SFan Zhang /* 416880cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 416980cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 417080cd8763SFan Zhang */ 417180cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 41724d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4173bb59c2daSAlice Frosi riccb->v && 41740c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 41754d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 41760c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 417780cd8763SFan Zhang } 41784e0b1ab7SFan Zhang /* 41794e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 41804e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 41814e0b1ab7SFan Zhang */ 41824e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 41834e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 41844e0b1ab7SFan Zhang gscb->gssm && 41854e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 41864e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 41874e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 41884e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 41894e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 419080cd8763SFan Zhang } 419135b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 419235b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 419335b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 419435b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 419535b3fde6SChristian Borntraeger } 41964e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 41974e0b1ab7SFan Zhang preempt_disable(); 41984e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 41994e0b1ab7SFan Zhang if (current->thread.gs_cb) { 42004e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 42014e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 42024e0b1ab7SFan Zhang } 42034e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 42044e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 42054e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 42064e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 42074e0b1ab7SFan Zhang } 42084e0b1ab7SFan Zhang preempt_enable(); 42094e0b1ab7SFan Zhang } 4210a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4211811ea797SJanosch Frank } 4212811ea797SJanosch Frank 4213811ea797SJanosch Frank static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4214811ea797SJanosch Frank { 4215811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4216811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4217811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4218811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4219811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4220811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4221811ea797SJanosch Frank } 4222811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4223811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4224811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4225811ea797SJanosch Frank } 4226811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4227811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4228811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4229811ea797SJanosch Frank save_fpu_regs(); 4230811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4231811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4232811ea797SJanosch Frank if (MACHINE_HAS_VX) 4233811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4234811ea797SJanosch Frank else 4235811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4236811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4237811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4238811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4239811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4240811ea797SJanosch Frank 4241811ea797SJanosch Frank /* Sync fmt2 only data */ 4242811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 4243811ea797SJanosch Frank sync_regs_fmt2(vcpu, kvm_run); 4244811ea797SJanosch Frank } else { 4245811ea797SJanosch Frank /* 4246811ea797SJanosch Frank * In several places we have to modify our internal view to 4247811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4248811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4249811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4250811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4251811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4252811ea797SJanosch Frank * do only accept the condition code from userspace. 4253811ea797SJanosch Frank */ 4254811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4255811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4256811ea797SJanosch Frank PSW_MASK_CC; 4257811ea797SJanosch Frank } 425880cd8763SFan Zhang 4259b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4260b028ee3eSDavid Hildenbrand } 4261b028ee3eSDavid Hildenbrand 4262811ea797SJanosch Frank static void store_regs_fmt2(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4263b028ee3eSDavid Hildenbrand { 4264b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4265b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4266b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 426735b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 42684e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 42694e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 42704e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 42714e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 42724e0b1ab7SFan Zhang preempt_disable(); 42734e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 42744e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 42754e0b1ab7SFan Zhang preempt_enable(); 42764e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 42774e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 42784e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 42794e0b1ab7SFan Zhang } 4280a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4281b028ee3eSDavid Hildenbrand } 4282b028ee3eSDavid Hildenbrand 4283811ea797SJanosch Frank static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4284811ea797SJanosch Frank { 4285811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4286811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4287811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4288811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4289811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4290811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4291811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4292811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4293811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4294811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4295811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4296811ea797SJanosch Frank /* Save guest register state */ 4297811ea797SJanosch Frank save_fpu_regs(); 4298811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4299811ea797SJanosch Frank /* Restore will be done lazily at return */ 4300811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4301811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4302811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 4303811ea797SJanosch Frank store_regs_fmt2(vcpu, kvm_run); 4304811ea797SJanosch Frank } 4305811ea797SJanosch Frank 4306b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 4307b0c632dbSHeiko Carstens { 43088f2abe6aSChristian Borntraeger int rc; 4309b0c632dbSHeiko Carstens 4310460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4311460df4c1SPaolo Bonzini return -EINTR; 4312460df4c1SPaolo Bonzini 4313200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4314200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4315200824f5SThomas Huth return -EINVAL; 4316200824f5SThomas Huth 4317accb757dSChristoffer Dall vcpu_load(vcpu); 4318accb757dSChristoffer Dall 431927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 432027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4321accb757dSChristoffer Dall rc = 0; 4322accb757dSChristoffer Dall goto out; 432327291e21SDavid Hildenbrand } 432427291e21SDavid Hildenbrand 432520b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4326b0c632dbSHeiko Carstens 4327fe28c786SJanosch Frank /* 4328fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4329fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4330fe28c786SJanosch Frank */ 43316352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 43326852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 43336352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4334ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 43356352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4336accb757dSChristoffer Dall rc = -EINVAL; 4337accb757dSChristoffer Dall goto out; 43386352e4d2SDavid Hildenbrand } 4339b0c632dbSHeiko Carstens 4340b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 4341db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4342d7b0b5ebSCarsten Otte 4343dab4079dSHeiko Carstens might_fault(); 4344e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 43459ace903dSChristian Ehrhardt 4346b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4347b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 43488f2abe6aSChristian Borntraeger rc = -EINTR; 4349b1d16c49SChristian Ehrhardt } 43508f2abe6aSChristian Borntraeger 435127291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 435227291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 435327291e21SDavid Hildenbrand rc = 0; 435427291e21SDavid Hildenbrand } 435527291e21SDavid Hildenbrand 43568f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 435771f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 43588f2abe6aSChristian Borntraeger rc = 0; 43598f2abe6aSChristian Borntraeger } 43608f2abe6aSChristian Borntraeger 4361db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 4362b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 4363d7b0b5ebSCarsten Otte 436420b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4365b0c632dbSHeiko Carstens 4366b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4367accb757dSChristoffer Dall out: 4368accb757dSChristoffer Dall vcpu_put(vcpu); 43697e8e6ab4SHeiko Carstens return rc; 4370b0c632dbSHeiko Carstens } 4371b0c632dbSHeiko Carstens 4372b0c632dbSHeiko Carstens /* 4373b0c632dbSHeiko Carstens * store status at address 4374b0c632dbSHeiko Carstens * we use have two special cases: 4375b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4376b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4377b0c632dbSHeiko Carstens */ 4378d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4379b0c632dbSHeiko Carstens { 4380092670cdSCarsten Otte unsigned char archmode = 1; 43819abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4382fda902cbSMichael Mueller unsigned int px; 43834287f247SDavid Hildenbrand u64 clkcomp, cputm; 4384d0bce605SHeiko Carstens int rc; 4385b0c632dbSHeiko Carstens 4386d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4387d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4388d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4389b0c632dbSHeiko Carstens return -EFAULT; 4390d9a3a09aSMartin Schwidefsky gpa = 0; 4391d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4392d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4393b0c632dbSHeiko Carstens return -EFAULT; 4394d9a3a09aSMartin Schwidefsky gpa = px; 4395d9a3a09aSMartin Schwidefsky } else 4396d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 43979abc2a08SDavid Hildenbrand 43989abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 43999abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 44009522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4401d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 44029abc2a08SDavid Hildenbrand fprs, 128); 44039abc2a08SDavid Hildenbrand } else { 44049abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 44056fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 44069abc2a08SDavid Hildenbrand } 4407d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4408d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4409d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4410d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4411d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4412fda902cbSMichael Mueller &px, 4); 4413d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 44149abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4415d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4416d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 44174287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4418d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 44194287f247SDavid Hildenbrand &cputm, 8); 4420178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4421d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4422d0bce605SHeiko Carstens &clkcomp, 8); 4423d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4424d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4425d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4426d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4427d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4428b0c632dbSHeiko Carstens } 4429b0c632dbSHeiko Carstens 4430e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4431e879892cSThomas Huth { 4432e879892cSThomas Huth /* 4433e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 443431d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4435e879892cSThomas Huth * it into the save area 4436e879892cSThomas Huth */ 4437d0164ee2SHendrik Brueckner save_fpu_regs(); 44389abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4439e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4440e879892cSThomas Huth 4441e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4442e879892cSThomas Huth } 4443e879892cSThomas Huth 44448ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 44458ad35755SDavid Hildenbrand { 44468ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 44478e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 44488ad35755SDavid Hildenbrand } 44498ad35755SDavid Hildenbrand 44508ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 44518ad35755SDavid Hildenbrand { 44528ad35755SDavid Hildenbrand unsigned int i; 44538ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 44548ad35755SDavid Hildenbrand 44558ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 44568ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 44578ad35755SDavid Hildenbrand } 44588ad35755SDavid Hildenbrand } 44598ad35755SDavid Hildenbrand 44608ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 44618ad35755SDavid Hildenbrand { 446209a400e7SDavid Hildenbrand if (!sclp.has_ibs) 446309a400e7SDavid Hildenbrand return; 44648ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 44658e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 44668ad35755SDavid Hildenbrand } 44678ad35755SDavid Hildenbrand 4468fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 44696852d7b6SDavid Hildenbrand { 4470fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 44718ad35755SDavid Hildenbrand 44728ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4473fe28c786SJanosch Frank return 0; 44748ad35755SDavid Hildenbrand 44756852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 44768ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4477433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 44788ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 44798ad35755SDavid Hildenbrand 4480fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 4481fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4482fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 4483fe28c786SJanosch Frank if (r) { 4484fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4485fe28c786SJanosch Frank return r; 4486fe28c786SJanosch Frank } 4487fe28c786SJanosch Frank } 4488fe28c786SJanosch Frank 44898ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 44908ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 44918ad35755SDavid Hildenbrand started_vcpus++; 44928ad35755SDavid Hildenbrand } 44938ad35755SDavid Hildenbrand 44948ad35755SDavid Hildenbrand if (started_vcpus == 0) { 44958ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 44968ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 44978ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 44988ad35755SDavid Hildenbrand /* 44998ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 45008ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 45018ad35755SDavid Hildenbrand * oustanding ENABLE requests. 45028ad35755SDavid Hildenbrand */ 45038ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 45048ad35755SDavid Hildenbrand } 45058ad35755SDavid Hildenbrand 45069daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 45078ad35755SDavid Hildenbrand /* 45088ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 45098ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 45108ad35755SDavid Hildenbrand */ 4511d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4512433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4513fe28c786SJanosch Frank return 0; 45146852d7b6SDavid Hildenbrand } 45156852d7b6SDavid Hildenbrand 4516fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 45176852d7b6SDavid Hildenbrand { 4518fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 45198ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 45208ad35755SDavid Hildenbrand 45218ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 4522fe28c786SJanosch Frank return 0; 45238ad35755SDavid Hildenbrand 45246852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 45258ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4526433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 45278ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 45288ad35755SDavid Hildenbrand 4529fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 4530fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4531fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 4532fe28c786SJanosch Frank if (r) { 4533fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4534fe28c786SJanosch Frank return r; 4535fe28c786SJanosch Frank } 4536fe28c786SJanosch Frank } 4537fe28c786SJanosch Frank 453832f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 45396cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 454032f5ff63SDavid Hildenbrand 4541ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 45428ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 45438ad35755SDavid Hildenbrand 45448ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 45458ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 45468ad35755SDavid Hildenbrand started_vcpus++; 45478ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 45488ad35755SDavid Hildenbrand } 45498ad35755SDavid Hildenbrand } 45508ad35755SDavid Hildenbrand 45518ad35755SDavid Hildenbrand if (started_vcpus == 1) { 45528ad35755SDavid Hildenbrand /* 45538ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 45548ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 45558ad35755SDavid Hildenbrand */ 45568ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 45578ad35755SDavid Hildenbrand } 45588ad35755SDavid Hildenbrand 4559433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 4560fe28c786SJanosch Frank return 0; 45616852d7b6SDavid Hildenbrand } 45626852d7b6SDavid Hildenbrand 4563d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4564d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4565d6712df9SCornelia Huck { 4566d6712df9SCornelia Huck int r; 4567d6712df9SCornelia Huck 4568d6712df9SCornelia Huck if (cap->flags) 4569d6712df9SCornelia Huck return -EINVAL; 4570d6712df9SCornelia Huck 4571d6712df9SCornelia Huck switch (cap->cap) { 4572fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4573fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4574fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4575c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4576fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4577fa6b7fe9SCornelia Huck } 4578fa6b7fe9SCornelia Huck r = 0; 4579fa6b7fe9SCornelia Huck break; 4580d6712df9SCornelia Huck default: 4581d6712df9SCornelia Huck r = -EINVAL; 4582d6712df9SCornelia Huck break; 4583d6712df9SCornelia Huck } 4584d6712df9SCornelia Huck return r; 4585d6712df9SCornelia Huck } 4586d6712df9SCornelia Huck 458719e12277SJanosch Frank static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu, 458819e12277SJanosch Frank struct kvm_s390_mem_op *mop) 458919e12277SJanosch Frank { 459019e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 459119e12277SJanosch Frank int r = 0; 459219e12277SJanosch Frank 459319e12277SJanosch Frank if (mop->flags || !mop->size) 459419e12277SJanosch Frank return -EINVAL; 459519e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 459619e12277SJanosch Frank return -EINVAL; 459719e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 459819e12277SJanosch Frank return -E2BIG; 459919e12277SJanosch Frank 460019e12277SJanosch Frank switch (mop->op) { 460119e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 460219e12277SJanosch Frank if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) + 460319e12277SJanosch Frank mop->sida_offset), mop->size)) 460419e12277SJanosch Frank r = -EFAULT; 460519e12277SJanosch Frank 460619e12277SJanosch Frank break; 460719e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 460819e12277SJanosch Frank if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) + 460919e12277SJanosch Frank mop->sida_offset), uaddr, mop->size)) 461019e12277SJanosch Frank r = -EFAULT; 461119e12277SJanosch Frank break; 461219e12277SJanosch Frank } 461319e12277SJanosch Frank return r; 461419e12277SJanosch Frank } 461541408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 461641408c28SThomas Huth struct kvm_s390_mem_op *mop) 461741408c28SThomas Huth { 461841408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 461941408c28SThomas Huth void *tmpbuf = NULL; 462019e12277SJanosch Frank int r = 0; 462141408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 462241408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 462341408c28SThomas Huth 4624a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 462541408c28SThomas Huth return -EINVAL; 462641408c28SThomas Huth 462741408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 462841408c28SThomas Huth return -E2BIG; 462941408c28SThomas Huth 463019e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 463119e12277SJanosch Frank return -EINVAL; 463219e12277SJanosch Frank 463341408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 463441408c28SThomas Huth tmpbuf = vmalloc(mop->size); 463541408c28SThomas Huth if (!tmpbuf) 463641408c28SThomas Huth return -ENOMEM; 463741408c28SThomas Huth } 463841408c28SThomas Huth 463941408c28SThomas Huth switch (mop->op) { 464041408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 464141408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 464292c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 464392c96321SDavid Hildenbrand mop->size, GACC_FETCH); 464441408c28SThomas Huth break; 464541408c28SThomas Huth } 464641408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 464741408c28SThomas Huth if (r == 0) { 464841408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 464941408c28SThomas Huth r = -EFAULT; 465041408c28SThomas Huth } 465141408c28SThomas Huth break; 465241408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 465341408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 465492c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 465592c96321SDavid Hildenbrand mop->size, GACC_STORE); 465641408c28SThomas Huth break; 465741408c28SThomas Huth } 465841408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 465941408c28SThomas Huth r = -EFAULT; 466041408c28SThomas Huth break; 466141408c28SThomas Huth } 466241408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 466341408c28SThomas Huth break; 466441408c28SThomas Huth } 466541408c28SThomas Huth 466641408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 466741408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 466841408c28SThomas Huth 466941408c28SThomas Huth vfree(tmpbuf); 467041408c28SThomas Huth return r; 467141408c28SThomas Huth } 467241408c28SThomas Huth 467319e12277SJanosch Frank static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu, 467419e12277SJanosch Frank struct kvm_s390_mem_op *mop) 467519e12277SJanosch Frank { 467619e12277SJanosch Frank int r, srcu_idx; 467719e12277SJanosch Frank 467819e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 467919e12277SJanosch Frank 468019e12277SJanosch Frank switch (mop->op) { 468119e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 468219e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 468319e12277SJanosch Frank r = kvm_s390_guest_mem_op(vcpu, mop); 468419e12277SJanosch Frank break; 468519e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 468619e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 468719e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 468819e12277SJanosch Frank r = kvm_s390_guest_sida_op(vcpu, mop); 468919e12277SJanosch Frank break; 469019e12277SJanosch Frank default: 469119e12277SJanosch Frank r = -EINVAL; 469219e12277SJanosch Frank } 469319e12277SJanosch Frank 469419e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 469519e12277SJanosch Frank return r; 469619e12277SJanosch Frank } 469719e12277SJanosch Frank 46985cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4699b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4700b0c632dbSHeiko Carstens { 4701b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4702b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4703b0c632dbSHeiko Carstens 470493736624SAvi Kivity switch (ioctl) { 470547b43c52SJens Freimann case KVM_S390_IRQ: { 470647b43c52SJens Freimann struct kvm_s390_irq s390irq; 470747b43c52SJens Freimann 470847b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 47099b062471SChristoffer Dall return -EFAULT; 47109b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 471147b43c52SJens Freimann } 471293736624SAvi Kivity case KVM_S390_INTERRUPT: { 4713ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 471453936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 4715ba5c1e9bSCarsten Otte 4716ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 47179b062471SChristoffer Dall return -EFAULT; 4718383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4719383d0b05SJens Freimann return -EINVAL; 47209b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4721ba5c1e9bSCarsten Otte } 47229b062471SChristoffer Dall } 47235cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 47245cb0944cSPaolo Bonzini } 47255cb0944cSPaolo Bonzini 47265cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 47275cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 47285cb0944cSPaolo Bonzini { 47295cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 47305cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 47315cb0944cSPaolo Bonzini int idx; 47325cb0944cSPaolo Bonzini long r; 47339b062471SChristoffer Dall 47349b062471SChristoffer Dall vcpu_load(vcpu); 47359b062471SChristoffer Dall 47369b062471SChristoffer Dall switch (ioctl) { 4737b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4738800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 473955680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 4740800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4741bc923cc9SAvi Kivity break; 4742b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4743b0c632dbSHeiko Carstens psw_t psw; 4744b0c632dbSHeiko Carstens 4745bc923cc9SAvi Kivity r = -EFAULT; 4746b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4747bc923cc9SAvi Kivity break; 4748bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4749bc923cc9SAvi Kivity break; 4750b0c632dbSHeiko Carstens } 47517de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 47527de3f142SJanosch Frank r = 0; 47537de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 47547de3f142SJanosch Frank break; 4755b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 47567de3f142SJanosch Frank r = 0; 47577de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 47587de3f142SJanosch Frank break; 47597de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 47607de3f142SJanosch Frank r = 0; 47617de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 4762bc923cc9SAvi Kivity break; 476314eebd91SCarsten Otte case KVM_SET_ONE_REG: 476414eebd91SCarsten Otte case KVM_GET_ONE_REG: { 476514eebd91SCarsten Otte struct kvm_one_reg reg; 476668cf7b1fSJanosch Frank r = -EINVAL; 476768cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 476868cf7b1fSJanosch Frank break; 476914eebd91SCarsten Otte r = -EFAULT; 477014eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 477114eebd91SCarsten Otte break; 477214eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 477314eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 477414eebd91SCarsten Otte else 477514eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 477614eebd91SCarsten Otte break; 477714eebd91SCarsten Otte } 477827e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 477927e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 478027e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 478127e0393fSCarsten Otte 478227e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 478327e0393fSCarsten Otte r = -EFAULT; 478427e0393fSCarsten Otte break; 478527e0393fSCarsten Otte } 478627e0393fSCarsten Otte 478727e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 478827e0393fSCarsten Otte r = -EINVAL; 478927e0393fSCarsten Otte break; 479027e0393fSCarsten Otte } 479127e0393fSCarsten Otte 479227e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 479327e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 479427e0393fSCarsten Otte break; 479527e0393fSCarsten Otte } 479627e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 479727e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 479827e0393fSCarsten Otte 479927e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 480027e0393fSCarsten Otte r = -EFAULT; 480127e0393fSCarsten Otte break; 480227e0393fSCarsten Otte } 480327e0393fSCarsten Otte 480427e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 480527e0393fSCarsten Otte r = -EINVAL; 480627e0393fSCarsten Otte break; 480727e0393fSCarsten Otte } 480827e0393fSCarsten Otte 480927e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 481027e0393fSCarsten Otte ucasmap.length); 481127e0393fSCarsten Otte break; 481227e0393fSCarsten Otte } 481327e0393fSCarsten Otte #endif 4814ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 4815527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 4816ccc7910fSCarsten Otte break; 4817ccc7910fSCarsten Otte } 4818d6712df9SCornelia Huck case KVM_ENABLE_CAP: 4819d6712df9SCornelia Huck { 4820d6712df9SCornelia Huck struct kvm_enable_cap cap; 4821d6712df9SCornelia Huck r = -EFAULT; 4822d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 4823d6712df9SCornelia Huck break; 4824d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 4825d6712df9SCornelia Huck break; 4826d6712df9SCornelia Huck } 482741408c28SThomas Huth case KVM_S390_MEM_OP: { 482841408c28SThomas Huth struct kvm_s390_mem_op mem_op; 482941408c28SThomas Huth 483041408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 483119e12277SJanosch Frank r = kvm_s390_guest_memsida_op(vcpu, &mem_op); 483241408c28SThomas Huth else 483341408c28SThomas Huth r = -EFAULT; 483441408c28SThomas Huth break; 483541408c28SThomas Huth } 4836816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 4837816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4838816c7667SJens Freimann 4839816c7667SJens Freimann r = -EFAULT; 4840816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4841816c7667SJens Freimann break; 4842816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 4843816c7667SJens Freimann irq_state.len == 0 || 4844816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 4845816c7667SJens Freimann r = -EINVAL; 4846816c7667SJens Freimann break; 4847816c7667SJens Freimann } 4848bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4849816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 4850816c7667SJens Freimann (void __user *) irq_state.buf, 4851816c7667SJens Freimann irq_state.len); 4852816c7667SJens Freimann break; 4853816c7667SJens Freimann } 4854816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 4855816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4856816c7667SJens Freimann 4857816c7667SJens Freimann r = -EFAULT; 4858816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4859816c7667SJens Freimann break; 4860816c7667SJens Freimann if (irq_state.len == 0) { 4861816c7667SJens Freimann r = -EINVAL; 4862816c7667SJens Freimann break; 4863816c7667SJens Freimann } 4864bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4865816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 4866816c7667SJens Freimann (__u8 __user *) irq_state.buf, 4867816c7667SJens Freimann irq_state.len); 4868816c7667SJens Freimann break; 4869816c7667SJens Freimann } 4870b0c632dbSHeiko Carstens default: 48713e6afcf1SCarsten Otte r = -ENOTTY; 4872b0c632dbSHeiko Carstens } 48739b062471SChristoffer Dall 48749b062471SChristoffer Dall vcpu_put(vcpu); 4875bc923cc9SAvi Kivity return r; 4876b0c632dbSHeiko Carstens } 4877b0c632dbSHeiko Carstens 48781499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 48795b1c1493SCarsten Otte { 48805b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 48815b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 48825b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 48835b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 48845b1c1493SCarsten Otte get_page(vmf->page); 48855b1c1493SCarsten Otte return 0; 48865b1c1493SCarsten Otte } 48875b1c1493SCarsten Otte #endif 48885b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 48895b1c1493SCarsten Otte } 48905b1c1493SCarsten Otte 48915587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 48925587027cSAneesh Kumar K.V unsigned long npages) 4893db3fe4ebSTakuya Yoshikawa { 4894db3fe4ebSTakuya Yoshikawa return 0; 4895db3fe4ebSTakuya Yoshikawa } 4896db3fe4ebSTakuya Yoshikawa 4897b0c632dbSHeiko Carstens /* Section: memory related */ 4898f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 4899f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 490009170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 49017b6195a9STakuya Yoshikawa enum kvm_mr_change change) 4902b0c632dbSHeiko Carstens { 4903dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 4904dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 4905dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 4906dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 4907b0c632dbSHeiko Carstens 4908598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 4909b0c632dbSHeiko Carstens return -EINVAL; 4910b0c632dbSHeiko Carstens 4911598841caSCarsten Otte if (mem->memory_size & 0xffffful) 4912b0c632dbSHeiko Carstens return -EINVAL; 4913b0c632dbSHeiko Carstens 4914a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 4915a3a92c31SDominik Dingel return -EINVAL; 4916a3a92c31SDominik Dingel 491729b40f10SJanosch Frank /* When we are protected, we should not change the memory slots */ 491829b40f10SJanosch Frank if (kvm_s390_pv_get_handle(kvm)) 491929b40f10SJanosch Frank return -EINVAL; 4920f7784b8eSMarcelo Tosatti return 0; 4921f7784b8eSMarcelo Tosatti } 4922f7784b8eSMarcelo Tosatti 4923f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 492409170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 49258482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 4926f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 49278482644aSTakuya Yoshikawa enum kvm_mr_change change) 4928f7784b8eSMarcelo Tosatti { 492919ec166cSChristian Borntraeger int rc = 0; 4930f7784b8eSMarcelo Tosatti 493119ec166cSChristian Borntraeger switch (change) { 493219ec166cSChristian Borntraeger case KVM_MR_DELETE: 493319ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 493419ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 493519ec166cSChristian Borntraeger break; 493619ec166cSChristian Borntraeger case KVM_MR_MOVE: 493719ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 493819ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 493919ec166cSChristian Borntraeger if (rc) 494019ec166cSChristian Borntraeger break; 494119ec166cSChristian Borntraeger /* FALLTHROUGH */ 494219ec166cSChristian Borntraeger case KVM_MR_CREATE: 4943598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 4944598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 494519ec166cSChristian Borntraeger break; 494619ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 494719ec166cSChristian Borntraeger break; 494819ec166cSChristian Borntraeger default: 494919ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 495019ec166cSChristian Borntraeger } 4951598841caSCarsten Otte if (rc) 4952ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 4953598841caSCarsten Otte return; 4954b0c632dbSHeiko Carstens } 4955b0c632dbSHeiko Carstens 495660a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 495760a37709SAlexander Yarygin { 495860a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 495960a37709SAlexander Yarygin 496060a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 496160a37709SAlexander Yarygin } 496260a37709SAlexander Yarygin 49633491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 49643491caf2SChristian Borntraeger { 49653491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 49663491caf2SChristian Borntraeger } 49673491caf2SChristian Borntraeger 4968b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 4969b0c632dbSHeiko Carstens { 497060a37709SAlexander Yarygin int i; 497160a37709SAlexander Yarygin 497207197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 49738d43d570SMichael Mueller pr_info("SIE is not available\n"); 497407197fd0SDavid Hildenbrand return -ENODEV; 497507197fd0SDavid Hildenbrand } 497607197fd0SDavid Hildenbrand 4977a4499382SJanosch Frank if (nested && hpage) { 49788d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 4979a4499382SJanosch Frank return -EINVAL; 4980a4499382SJanosch Frank } 4981a4499382SJanosch Frank 498260a37709SAlexander Yarygin for (i = 0; i < 16; i++) 4983c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 498460a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 498560a37709SAlexander Yarygin 49869d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 4987b0c632dbSHeiko Carstens } 4988b0c632dbSHeiko Carstens 4989b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 4990b0c632dbSHeiko Carstens { 4991b0c632dbSHeiko Carstens kvm_exit(); 4992b0c632dbSHeiko Carstens } 4993b0c632dbSHeiko Carstens 4994b0c632dbSHeiko Carstens module_init(kvm_s390_init); 4995b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 4996566af940SCornelia Huck 4997566af940SCornelia Huck /* 4998566af940SCornelia Huck * Enable autoloading of the kvm module. 4999566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5000566af940SCornelia Huck * since x86 takes a different approach. 5001566af940SCornelia Huck */ 5002566af940SCornelia Huck #include <linux/miscdevice.h> 5003566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5004566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5005