1b0c632dbSHeiko Carstens /* 2a53c8fabSHeiko Carstens * hosting zSeries kernel virtual machines 3b0c632dbSHeiko Carstens * 4628eb9b8SChristian Ehrhardt * Copyright IBM Corp. 2008, 2009 5b0c632dbSHeiko Carstens * 6b0c632dbSHeiko Carstens * This program is free software; you can redistribute it and/or modify 7b0c632dbSHeiko Carstens * it under the terms of the GNU General Public License (version 2 only) 8b0c632dbSHeiko Carstens * as published by the Free Software Foundation. 9b0c632dbSHeiko Carstens * 10b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 11b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 12b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 13628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1415f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 15b0c632dbSHeiko Carstens */ 16b0c632dbSHeiko Carstens 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> 24b0c632dbSHeiko Carstens #include <linux/module.h> 25a374e892STony Krowiak #include <linux/random.h> 26b0c632dbSHeiko Carstens #include <linux/slab.h> 27ba5c1e9bSCarsten Otte #include <linux/timer.h> 2841408c28SThomas Huth #include <linux/vmalloc.h> 29cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 30b0c632dbSHeiko Carstens #include <asm/lowcore.h> 31b0c632dbSHeiko Carstens #include <asm/pgtable.h> 32f5daba1dSHeiko Carstens #include <asm/nmi.h> 33a0616cdeSDavid Howells #include <asm/switch_to.h> 34*6d3da241SJens Freimann #include <asm/isc.h> 351526bf9cSChristian Borntraeger #include <asm/sclp.h> 368f2abe6aSChristian Borntraeger #include "kvm-s390.h" 37b0c632dbSHeiko Carstens #include "gaccess.h" 38b0c632dbSHeiko Carstens 395786fffaSCornelia Huck #define CREATE_TRACE_POINTS 405786fffaSCornelia Huck #include "trace.h" 41ade38c31SCornelia Huck #include "trace-s390.h" 425786fffaSCornelia Huck 4341408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 4441408c28SThomas Huth 45b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 46b0c632dbSHeiko Carstens 47b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 48b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 490eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 508f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 518f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 528f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 538f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 54ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 55ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 56ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 57f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 58ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 59f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 60ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 61aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 62aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 63ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 647697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 65ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 66ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 67ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 68ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 69ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 70ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 71ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 7269d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 73453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 74453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 75453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 76453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 77453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 788a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 79453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 80453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 81b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 82453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 83453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 84bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 855288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 86bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 877697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 885288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 8942cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 9042cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 915288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 9242cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 9342cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 94cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 955288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 965288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 975288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 9842cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 9942cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 10042cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 101388186bcSChristian Borntraeger { "diagnose_10", VCPU_STAT(diagnose_10) }, 102e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 10341628d33SKonstantin Weitz { "diagnose_9c", VCPU_STAT(diagnose_9c) }, 104b0c632dbSHeiko Carstens { NULL } 105b0c632dbSHeiko Carstens }; 106b0c632dbSHeiko Carstens 1079d8d5786SMichael Mueller /* upper facilities limit for kvm */ 1089d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask[] = { 109a3ed8daeSChristian Borntraeger 0xffe6fffbfcfdfc40UL, 110a3ed8daeSChristian Borntraeger 0x205c800000000000UL, 1119d8d5786SMichael Mueller }; 112b0c632dbSHeiko Carstens 1139d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void) 11478c4b59fSMichael Mueller { 1159d8d5786SMichael Mueller BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64); 1169d8d5786SMichael Mueller return ARRAY_SIZE(kvm_s390_fac_list_mask); 11778c4b59fSMichael Mueller } 11878c4b59fSMichael Mueller 1199d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 1209d8d5786SMichael Mueller 121b0c632dbSHeiko Carstens /* Section: not file related */ 12213a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 123b0c632dbSHeiko Carstens { 124b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 12510474ae8SAlexander Graf return 0; 126b0c632dbSHeiko Carstens } 127b0c632dbSHeiko Carstens 1282c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address); 1292c70fe44SChristian Borntraeger 130b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 131b0c632dbSHeiko Carstens { 1322c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 1332c70fe44SChristian Borntraeger gmap_register_ipte_notifier(&gmap_notifier); 134b0c632dbSHeiko Carstens return 0; 135b0c632dbSHeiko Carstens } 136b0c632dbSHeiko Carstens 137b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 138b0c632dbSHeiko Carstens { 1392c70fe44SChristian Borntraeger gmap_unregister_ipte_notifier(&gmap_notifier); 140b0c632dbSHeiko Carstens } 141b0c632dbSHeiko Carstens 142b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 143b0c632dbSHeiko Carstens { 14484877d93SCornelia Huck /* Register floating interrupt controller interface. */ 14584877d93SCornelia Huck return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 146b0c632dbSHeiko Carstens } 147b0c632dbSHeiko Carstens 148b0c632dbSHeiko Carstens /* Section: device related */ 149b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 150b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 151b0c632dbSHeiko Carstens { 152b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 153b0c632dbSHeiko Carstens return s390_enable_sie(); 154b0c632dbSHeiko Carstens return -EINVAL; 155b0c632dbSHeiko Carstens } 156b0c632dbSHeiko Carstens 157784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 158b0c632dbSHeiko Carstens { 159d7b0b5ebSCarsten Otte int r; 160d7b0b5ebSCarsten Otte 1612bd0ac4eSCarsten Otte switch (ext) { 162d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 163b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 16452e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 1651efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1661efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 1671efd0f59SCarsten Otte #endif 1683c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 16960b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 17014eebd91SCarsten Otte case KVM_CAP_ONE_REG: 171d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 172fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 173ebc32262SCornelia Huck case KVM_CAP_IRQFD: 17410ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 175c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 176d938dc55SCornelia Huck case KVM_CAP_ENABLE_CAP_VM: 17778599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 178f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 1796352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 1802444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 181e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 18230ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 183d7b0b5ebSCarsten Otte r = 1; 184d7b0b5ebSCarsten Otte break; 18541408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 18641408c28SThomas Huth r = MEM_OP_MAX_SIZE; 18741408c28SThomas Huth break; 188e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 189e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 190e726b1bdSChristian Borntraeger r = KVM_MAX_VCPUS; 191e726b1bdSChristian Borntraeger break; 192e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 193e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 194e1e2e605SNick Wang break; 1951526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 196abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 1971526bf9cSChristian Borntraeger break; 19868c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 19968c55750SEric Farman r = MACHINE_HAS_VX; 20068c55750SEric Farman break; 2012bd0ac4eSCarsten Otte default: 202d7b0b5ebSCarsten Otte r = 0; 203b0c632dbSHeiko Carstens } 204d7b0b5ebSCarsten Otte return r; 2052bd0ac4eSCarsten Otte } 206b0c632dbSHeiko Carstens 20715f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 20815f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 20915f36ebdSJason J. Herne { 21015f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 21115f36ebdSJason J. Herne unsigned long address; 21215f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 21315f36ebdSJason J. Herne 21415f36ebdSJason J. Herne down_read(&gmap->mm->mmap_sem); 21515f36ebdSJason J. Herne /* Loop over all guest pages */ 21615f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 21715f36ebdSJason J. Herne for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) { 21815f36ebdSJason J. Herne address = gfn_to_hva_memslot(memslot, cur_gfn); 21915f36ebdSJason J. Herne 22015f36ebdSJason J. Herne if (gmap_test_and_clear_dirty(address, gmap)) 22115f36ebdSJason J. Herne mark_page_dirty(kvm, cur_gfn); 22215f36ebdSJason J. Herne } 22315f36ebdSJason J. Herne up_read(&gmap->mm->mmap_sem); 22415f36ebdSJason J. Herne } 22515f36ebdSJason J. Herne 226b0c632dbSHeiko Carstens /* Section: vm related */ 227b0c632dbSHeiko Carstens /* 228b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 229b0c632dbSHeiko Carstens */ 230b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 231b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 232b0c632dbSHeiko Carstens { 23315f36ebdSJason J. Herne int r; 23415f36ebdSJason J. Herne unsigned long n; 23515f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 23615f36ebdSJason J. Herne int is_dirty = 0; 23715f36ebdSJason J. Herne 23815f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 23915f36ebdSJason J. Herne 24015f36ebdSJason J. Herne r = -EINVAL; 24115f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 24215f36ebdSJason J. Herne goto out; 24315f36ebdSJason J. Herne 24415f36ebdSJason J. Herne memslot = id_to_memslot(kvm->memslots, log->slot); 24515f36ebdSJason J. Herne r = -ENOENT; 24615f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 24715f36ebdSJason J. Herne goto out; 24815f36ebdSJason J. Herne 24915f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 25015f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 25115f36ebdSJason J. Herne if (r) 25215f36ebdSJason J. Herne goto out; 25315f36ebdSJason J. Herne 25415f36ebdSJason J. Herne /* Clear the dirty log */ 25515f36ebdSJason J. Herne if (is_dirty) { 25615f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 25715f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 25815f36ebdSJason J. Herne } 25915f36ebdSJason J. Herne r = 0; 26015f36ebdSJason J. Herne out: 26115f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 26215f36ebdSJason J. Herne return r; 263b0c632dbSHeiko Carstens } 264b0c632dbSHeiko Carstens 265d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 266d938dc55SCornelia Huck { 267d938dc55SCornelia Huck int r; 268d938dc55SCornelia Huck 269d938dc55SCornelia Huck if (cap->flags) 270d938dc55SCornelia Huck return -EINVAL; 271d938dc55SCornelia Huck 272d938dc55SCornelia Huck switch (cap->cap) { 27384223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 27484223598SCornelia Huck kvm->arch.use_irqchip = 1; 27584223598SCornelia Huck r = 0; 27684223598SCornelia Huck break; 2772444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 2782444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 2792444b352SDavid Hildenbrand r = 0; 2802444b352SDavid Hildenbrand break; 28168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 28218280d8bSMichael Mueller if (MACHINE_HAS_VX) { 28318280d8bSMichael Mueller set_kvm_facility(kvm->arch.model.fac->mask, 129); 28418280d8bSMichael Mueller set_kvm_facility(kvm->arch.model.fac->list, 129); 28518280d8bSMichael Mueller r = 0; 28618280d8bSMichael Mueller } else 28718280d8bSMichael Mueller r = -EINVAL; 28868c55750SEric Farman break; 289e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 290e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 291e44fc8c9SEkaterina Tumanova r = 0; 292e44fc8c9SEkaterina Tumanova break; 293d938dc55SCornelia Huck default: 294d938dc55SCornelia Huck r = -EINVAL; 295d938dc55SCornelia Huck break; 296d938dc55SCornelia Huck } 297d938dc55SCornelia Huck return r; 298d938dc55SCornelia Huck } 299d938dc55SCornelia Huck 3008c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 3018c0a7ce6SDominik Dingel { 3028c0a7ce6SDominik Dingel int ret; 3038c0a7ce6SDominik Dingel 3048c0a7ce6SDominik Dingel switch (attr->attr) { 3058c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 3068c0a7ce6SDominik Dingel ret = 0; 3078c0a7ce6SDominik Dingel if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr)) 3088c0a7ce6SDominik Dingel ret = -EFAULT; 3098c0a7ce6SDominik Dingel break; 3108c0a7ce6SDominik Dingel default: 3118c0a7ce6SDominik Dingel ret = -ENXIO; 3128c0a7ce6SDominik Dingel break; 3138c0a7ce6SDominik Dingel } 3148c0a7ce6SDominik Dingel return ret; 3158c0a7ce6SDominik Dingel } 3168c0a7ce6SDominik Dingel 3178c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 3184f718eabSDominik Dingel { 3194f718eabSDominik Dingel int ret; 3204f718eabSDominik Dingel unsigned int idx; 3214f718eabSDominik Dingel switch (attr->attr) { 3224f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 3234f718eabSDominik Dingel ret = -EBUSY; 3244f718eabSDominik Dingel mutex_lock(&kvm->lock); 3254f718eabSDominik Dingel if (atomic_read(&kvm->online_vcpus) == 0) { 3264f718eabSDominik Dingel kvm->arch.use_cmma = 1; 3274f718eabSDominik Dingel ret = 0; 3284f718eabSDominik Dingel } 3294f718eabSDominik Dingel mutex_unlock(&kvm->lock); 3304f718eabSDominik Dingel break; 3314f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 3324f718eabSDominik Dingel mutex_lock(&kvm->lock); 3334f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 334a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 3354f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 3364f718eabSDominik Dingel mutex_unlock(&kvm->lock); 3374f718eabSDominik Dingel ret = 0; 3384f718eabSDominik Dingel break; 3398c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 3408c0a7ce6SDominik Dingel unsigned long new_limit; 3418c0a7ce6SDominik Dingel 3428c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 3438c0a7ce6SDominik Dingel return -EINVAL; 3448c0a7ce6SDominik Dingel 3458c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 3468c0a7ce6SDominik Dingel return -EFAULT; 3478c0a7ce6SDominik Dingel 3488c0a7ce6SDominik Dingel if (new_limit > kvm->arch.gmap->asce_end) 3498c0a7ce6SDominik Dingel return -E2BIG; 3508c0a7ce6SDominik Dingel 3518c0a7ce6SDominik Dingel ret = -EBUSY; 3528c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 3538c0a7ce6SDominik Dingel if (atomic_read(&kvm->online_vcpus) == 0) { 3548c0a7ce6SDominik Dingel /* gmap_alloc will round the limit up */ 3558c0a7ce6SDominik Dingel struct gmap *new = gmap_alloc(current->mm, new_limit); 3568c0a7ce6SDominik Dingel 3578c0a7ce6SDominik Dingel if (!new) { 3588c0a7ce6SDominik Dingel ret = -ENOMEM; 3598c0a7ce6SDominik Dingel } else { 3608c0a7ce6SDominik Dingel gmap_free(kvm->arch.gmap); 3618c0a7ce6SDominik Dingel new->private = kvm; 3628c0a7ce6SDominik Dingel kvm->arch.gmap = new; 3638c0a7ce6SDominik Dingel ret = 0; 3648c0a7ce6SDominik Dingel } 3658c0a7ce6SDominik Dingel } 3668c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 3678c0a7ce6SDominik Dingel break; 3688c0a7ce6SDominik Dingel } 3694f718eabSDominik Dingel default: 3704f718eabSDominik Dingel ret = -ENXIO; 3714f718eabSDominik Dingel break; 3724f718eabSDominik Dingel } 3734f718eabSDominik Dingel return ret; 3744f718eabSDominik Dingel } 3754f718eabSDominik Dingel 376a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 377a374e892STony Krowiak 378a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 379a374e892STony Krowiak { 380a374e892STony Krowiak struct kvm_vcpu *vcpu; 381a374e892STony Krowiak int i; 382a374e892STony Krowiak 3839d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 384a374e892STony Krowiak return -EINVAL; 385a374e892STony Krowiak 386a374e892STony Krowiak mutex_lock(&kvm->lock); 387a374e892STony Krowiak switch (attr->attr) { 388a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 389a374e892STony Krowiak get_random_bytes( 390a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 391a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 392a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 393a374e892STony Krowiak break; 394a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 395a374e892STony Krowiak get_random_bytes( 396a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 397a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 398a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 399a374e892STony Krowiak break; 400a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 401a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 402a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 403a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 404a374e892STony Krowiak break; 405a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 406a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 407a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 408a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 409a374e892STony Krowiak break; 410a374e892STony Krowiak default: 411a374e892STony Krowiak mutex_unlock(&kvm->lock); 412a374e892STony Krowiak return -ENXIO; 413a374e892STony Krowiak } 414a374e892STony Krowiak 415a374e892STony Krowiak kvm_for_each_vcpu(i, vcpu, kvm) { 416a374e892STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 417a374e892STony Krowiak exit_sie(vcpu); 418a374e892STony Krowiak } 419a374e892STony Krowiak mutex_unlock(&kvm->lock); 420a374e892STony Krowiak return 0; 421a374e892STony Krowiak } 422a374e892STony Krowiak 42372f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 42472f25020SJason J. Herne { 42572f25020SJason J. Herne u8 gtod_high; 42672f25020SJason J. Herne 42772f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 42872f25020SJason J. Herne sizeof(gtod_high))) 42972f25020SJason J. Herne return -EFAULT; 43072f25020SJason J. Herne 43172f25020SJason J. Herne if (gtod_high != 0) 43272f25020SJason J. Herne return -EINVAL; 43372f25020SJason J. Herne 43472f25020SJason J. Herne return 0; 43572f25020SJason J. Herne } 43672f25020SJason J. Herne 43772f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 43872f25020SJason J. Herne { 43972f25020SJason J. Herne struct kvm_vcpu *cur_vcpu; 44072f25020SJason J. Herne unsigned int vcpu_idx; 44172f25020SJason J. Herne u64 host_tod, gtod; 44272f25020SJason J. Herne int r; 44372f25020SJason J. Herne 44472f25020SJason J. Herne if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 44572f25020SJason J. Herne return -EFAULT; 44672f25020SJason J. Herne 44772f25020SJason J. Herne r = store_tod_clock(&host_tod); 44872f25020SJason J. Herne if (r) 44972f25020SJason J. Herne return r; 45072f25020SJason J. Herne 45172f25020SJason J. Herne mutex_lock(&kvm->lock); 45272f25020SJason J. Herne kvm->arch.epoch = gtod - host_tod; 45372f25020SJason J. Herne kvm_for_each_vcpu(vcpu_idx, cur_vcpu, kvm) { 45472f25020SJason J. Herne cur_vcpu->arch.sie_block->epoch = kvm->arch.epoch; 45572f25020SJason J. Herne exit_sie(cur_vcpu); 45672f25020SJason J. Herne } 45772f25020SJason J. Herne mutex_unlock(&kvm->lock); 45872f25020SJason J. Herne return 0; 45972f25020SJason J. Herne } 46072f25020SJason J. Herne 46172f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 46272f25020SJason J. Herne { 46372f25020SJason J. Herne int ret; 46472f25020SJason J. Herne 46572f25020SJason J. Herne if (attr->flags) 46672f25020SJason J. Herne return -EINVAL; 46772f25020SJason J. Herne 46872f25020SJason J. Herne switch (attr->attr) { 46972f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 47072f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 47172f25020SJason J. Herne break; 47272f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 47372f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 47472f25020SJason J. Herne break; 47572f25020SJason J. Herne default: 47672f25020SJason J. Herne ret = -ENXIO; 47772f25020SJason J. Herne break; 47872f25020SJason J. Herne } 47972f25020SJason J. Herne return ret; 48072f25020SJason J. Herne } 48172f25020SJason J. Herne 48272f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 48372f25020SJason J. Herne { 48472f25020SJason J. Herne u8 gtod_high = 0; 48572f25020SJason J. Herne 48672f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 48772f25020SJason J. Herne sizeof(gtod_high))) 48872f25020SJason J. Herne return -EFAULT; 48972f25020SJason J. Herne 49072f25020SJason J. Herne return 0; 49172f25020SJason J. Herne } 49272f25020SJason J. Herne 49372f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 49472f25020SJason J. Herne { 49572f25020SJason J. Herne u64 host_tod, gtod; 49672f25020SJason J. Herne int r; 49772f25020SJason J. Herne 49872f25020SJason J. Herne r = store_tod_clock(&host_tod); 49972f25020SJason J. Herne if (r) 50072f25020SJason J. Herne return r; 50172f25020SJason J. Herne 50272f25020SJason J. Herne gtod = host_tod + kvm->arch.epoch; 50372f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 50472f25020SJason J. Herne return -EFAULT; 50572f25020SJason J. Herne 50672f25020SJason J. Herne return 0; 50772f25020SJason J. Herne } 50872f25020SJason J. Herne 50972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 51072f25020SJason J. Herne { 51172f25020SJason J. Herne int ret; 51272f25020SJason J. Herne 51372f25020SJason J. Herne if (attr->flags) 51472f25020SJason J. Herne return -EINVAL; 51572f25020SJason J. Herne 51672f25020SJason J. Herne switch (attr->attr) { 51772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 51872f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 51972f25020SJason J. Herne break; 52072f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 52172f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 52272f25020SJason J. Herne break; 52372f25020SJason J. Herne default: 52472f25020SJason J. Herne ret = -ENXIO; 52572f25020SJason J. Herne break; 52672f25020SJason J. Herne } 52772f25020SJason J. Herne return ret; 52872f25020SJason J. Herne } 52972f25020SJason J. Herne 530658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 531658b6edaSMichael Mueller { 532658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 533658b6edaSMichael Mueller int ret = 0; 534658b6edaSMichael Mueller 535658b6edaSMichael Mueller mutex_lock(&kvm->lock); 536658b6edaSMichael Mueller if (atomic_read(&kvm->online_vcpus)) { 537658b6edaSMichael Mueller ret = -EBUSY; 538658b6edaSMichael Mueller goto out; 539658b6edaSMichael Mueller } 540658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 541658b6edaSMichael Mueller if (!proc) { 542658b6edaSMichael Mueller ret = -ENOMEM; 543658b6edaSMichael Mueller goto out; 544658b6edaSMichael Mueller } 545658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 546658b6edaSMichael Mueller sizeof(*proc))) { 547658b6edaSMichael Mueller memcpy(&kvm->arch.model.cpu_id, &proc->cpuid, 548658b6edaSMichael Mueller sizeof(struct cpuid)); 549658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 550981467c9SMichael Mueller memcpy(kvm->arch.model.fac->list, proc->fac_list, 551658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 552658b6edaSMichael Mueller } else 553658b6edaSMichael Mueller ret = -EFAULT; 554658b6edaSMichael Mueller kfree(proc); 555658b6edaSMichael Mueller out: 556658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 557658b6edaSMichael Mueller return ret; 558658b6edaSMichael Mueller } 559658b6edaSMichael Mueller 560658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 561658b6edaSMichael Mueller { 562658b6edaSMichael Mueller int ret = -ENXIO; 563658b6edaSMichael Mueller 564658b6edaSMichael Mueller switch (attr->attr) { 565658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 566658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 567658b6edaSMichael Mueller break; 568658b6edaSMichael Mueller } 569658b6edaSMichael Mueller return ret; 570658b6edaSMichael Mueller } 571658b6edaSMichael Mueller 572658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 573658b6edaSMichael Mueller { 574658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 575658b6edaSMichael Mueller int ret = 0; 576658b6edaSMichael Mueller 577658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 578658b6edaSMichael Mueller if (!proc) { 579658b6edaSMichael Mueller ret = -ENOMEM; 580658b6edaSMichael Mueller goto out; 581658b6edaSMichael Mueller } 582658b6edaSMichael Mueller memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid)); 583658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 584981467c9SMichael Mueller memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE); 585658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 586658b6edaSMichael Mueller ret = -EFAULT; 587658b6edaSMichael Mueller kfree(proc); 588658b6edaSMichael Mueller out: 589658b6edaSMichael Mueller return ret; 590658b6edaSMichael Mueller } 591658b6edaSMichael Mueller 592658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 593658b6edaSMichael Mueller { 594658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 595658b6edaSMichael Mueller int ret = 0; 596658b6edaSMichael Mueller 597658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 598658b6edaSMichael Mueller if (!mach) { 599658b6edaSMichael Mueller ret = -ENOMEM; 600658b6edaSMichael Mueller goto out; 601658b6edaSMichael Mueller } 602658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 603658b6edaSMichael Mueller mach->ibc = sclp_get_ibc(); 604981467c9SMichael Mueller memcpy(&mach->fac_mask, kvm->arch.model.fac->mask, 605981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 606658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 60794422ee8SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 608658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 609658b6edaSMichael Mueller ret = -EFAULT; 610658b6edaSMichael Mueller kfree(mach); 611658b6edaSMichael Mueller out: 612658b6edaSMichael Mueller return ret; 613658b6edaSMichael Mueller } 614658b6edaSMichael Mueller 615658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 616658b6edaSMichael Mueller { 617658b6edaSMichael Mueller int ret = -ENXIO; 618658b6edaSMichael Mueller 619658b6edaSMichael Mueller switch (attr->attr) { 620658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 621658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 622658b6edaSMichael Mueller break; 623658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 624658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 625658b6edaSMichael Mueller break; 626658b6edaSMichael Mueller } 627658b6edaSMichael Mueller return ret; 628658b6edaSMichael Mueller } 629658b6edaSMichael Mueller 630f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 631f2061656SDominik Dingel { 632f2061656SDominik Dingel int ret; 633f2061656SDominik Dingel 634f2061656SDominik Dingel switch (attr->group) { 6354f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 6368c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 6374f718eabSDominik Dingel break; 63872f25020SJason J. Herne case KVM_S390_VM_TOD: 63972f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 64072f25020SJason J. Herne break; 641658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 642658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 643658b6edaSMichael Mueller break; 644a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 645a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 646a374e892STony Krowiak break; 647f2061656SDominik Dingel default: 648f2061656SDominik Dingel ret = -ENXIO; 649f2061656SDominik Dingel break; 650f2061656SDominik Dingel } 651f2061656SDominik Dingel 652f2061656SDominik Dingel return ret; 653f2061656SDominik Dingel } 654f2061656SDominik Dingel 655f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 656f2061656SDominik Dingel { 6578c0a7ce6SDominik Dingel int ret; 6588c0a7ce6SDominik Dingel 6598c0a7ce6SDominik Dingel switch (attr->group) { 6608c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 6618c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 6628c0a7ce6SDominik Dingel break; 66372f25020SJason J. Herne case KVM_S390_VM_TOD: 66472f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 66572f25020SJason J. Herne break; 666658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 667658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 668658b6edaSMichael Mueller break; 6698c0a7ce6SDominik Dingel default: 6708c0a7ce6SDominik Dingel ret = -ENXIO; 6718c0a7ce6SDominik Dingel break; 6728c0a7ce6SDominik Dingel } 6738c0a7ce6SDominik Dingel 6748c0a7ce6SDominik Dingel return ret; 675f2061656SDominik Dingel } 676f2061656SDominik Dingel 677f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 678f2061656SDominik Dingel { 679f2061656SDominik Dingel int ret; 680f2061656SDominik Dingel 681f2061656SDominik Dingel switch (attr->group) { 6824f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 6834f718eabSDominik Dingel switch (attr->attr) { 6844f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 6854f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 6868c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 6874f718eabSDominik Dingel ret = 0; 6884f718eabSDominik Dingel break; 6894f718eabSDominik Dingel default: 6904f718eabSDominik Dingel ret = -ENXIO; 6914f718eabSDominik Dingel break; 6924f718eabSDominik Dingel } 6934f718eabSDominik Dingel break; 69472f25020SJason J. Herne case KVM_S390_VM_TOD: 69572f25020SJason J. Herne switch (attr->attr) { 69672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 69772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 69872f25020SJason J. Herne ret = 0; 69972f25020SJason J. Herne break; 70072f25020SJason J. Herne default: 70172f25020SJason J. Herne ret = -ENXIO; 70272f25020SJason J. Herne break; 70372f25020SJason J. Herne } 70472f25020SJason J. Herne break; 705658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 706658b6edaSMichael Mueller switch (attr->attr) { 707658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 708658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 709658b6edaSMichael Mueller ret = 0; 710658b6edaSMichael Mueller break; 711658b6edaSMichael Mueller default: 712658b6edaSMichael Mueller ret = -ENXIO; 713658b6edaSMichael Mueller break; 714658b6edaSMichael Mueller } 715658b6edaSMichael Mueller break; 716a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 717a374e892STony Krowiak switch (attr->attr) { 718a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 719a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 720a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 721a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 722a374e892STony Krowiak ret = 0; 723a374e892STony Krowiak break; 724a374e892STony Krowiak default: 725a374e892STony Krowiak ret = -ENXIO; 726a374e892STony Krowiak break; 727a374e892STony Krowiak } 728a374e892STony Krowiak break; 729f2061656SDominik Dingel default: 730f2061656SDominik Dingel ret = -ENXIO; 731f2061656SDominik Dingel break; 732f2061656SDominik Dingel } 733f2061656SDominik Dingel 734f2061656SDominik Dingel return ret; 735f2061656SDominik Dingel } 736f2061656SDominik Dingel 73730ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 73830ee2a98SJason J. Herne { 73930ee2a98SJason J. Herne uint8_t *keys; 74030ee2a98SJason J. Herne uint64_t hva; 74130ee2a98SJason J. Herne unsigned long curkey; 74230ee2a98SJason J. Herne int i, r = 0; 74330ee2a98SJason J. Herne 74430ee2a98SJason J. Herne if (args->flags != 0) 74530ee2a98SJason J. Herne return -EINVAL; 74630ee2a98SJason J. Herne 74730ee2a98SJason J. Herne /* Is this guest using storage keys? */ 74830ee2a98SJason J. Herne if (!mm_use_skey(current->mm)) 74930ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 75030ee2a98SJason J. Herne 75130ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 75230ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 75330ee2a98SJason J. Herne return -EINVAL; 75430ee2a98SJason J. Herne 75530ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 75630ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 75730ee2a98SJason J. Herne if (!keys) 75830ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 75930ee2a98SJason J. Herne if (!keys) 76030ee2a98SJason J. Herne return -ENOMEM; 76130ee2a98SJason J. Herne 76230ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 76330ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 76430ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 76530ee2a98SJason J. Herne r = -EFAULT; 76630ee2a98SJason J. Herne goto out; 76730ee2a98SJason J. Herne } 76830ee2a98SJason J. Herne 76930ee2a98SJason J. Herne curkey = get_guest_storage_key(current->mm, hva); 77030ee2a98SJason J. Herne if (IS_ERR_VALUE(curkey)) { 77130ee2a98SJason J. Herne r = curkey; 77230ee2a98SJason J. Herne goto out; 77330ee2a98SJason J. Herne } 77430ee2a98SJason J. Herne keys[i] = curkey; 77530ee2a98SJason J. Herne } 77630ee2a98SJason J. Herne 77730ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 77830ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 77930ee2a98SJason J. Herne if (r) 78030ee2a98SJason J. Herne r = -EFAULT; 78130ee2a98SJason J. Herne out: 78230ee2a98SJason J. Herne kvfree(keys); 78330ee2a98SJason J. Herne return r; 78430ee2a98SJason J. Herne } 78530ee2a98SJason J. Herne 78630ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 78730ee2a98SJason J. Herne { 78830ee2a98SJason J. Herne uint8_t *keys; 78930ee2a98SJason J. Herne uint64_t hva; 79030ee2a98SJason J. Herne int i, r = 0; 79130ee2a98SJason J. Herne 79230ee2a98SJason J. Herne if (args->flags != 0) 79330ee2a98SJason J. Herne return -EINVAL; 79430ee2a98SJason J. Herne 79530ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 79630ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 79730ee2a98SJason J. Herne return -EINVAL; 79830ee2a98SJason J. Herne 79930ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 80030ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 80130ee2a98SJason J. Herne if (!keys) 80230ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 80330ee2a98SJason J. Herne if (!keys) 80430ee2a98SJason J. Herne return -ENOMEM; 80530ee2a98SJason J. Herne 80630ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 80730ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 80830ee2a98SJason J. Herne if (r) { 80930ee2a98SJason J. Herne r = -EFAULT; 81030ee2a98SJason J. Herne goto out; 81130ee2a98SJason J. Herne } 81230ee2a98SJason J. Herne 81330ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 81430ee2a98SJason J. Herne s390_enable_skey(); 81530ee2a98SJason J. Herne 81630ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 81730ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 81830ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 81930ee2a98SJason J. Herne r = -EFAULT; 82030ee2a98SJason J. Herne goto out; 82130ee2a98SJason J. Herne } 82230ee2a98SJason J. Herne 82330ee2a98SJason J. Herne /* Lowest order bit is reserved */ 82430ee2a98SJason J. Herne if (keys[i] & 0x01) { 82530ee2a98SJason J. Herne r = -EINVAL; 82630ee2a98SJason J. Herne goto out; 82730ee2a98SJason J. Herne } 82830ee2a98SJason J. Herne 82930ee2a98SJason J. Herne r = set_guest_storage_key(current->mm, hva, 83030ee2a98SJason J. Herne (unsigned long)keys[i], 0); 83130ee2a98SJason J. Herne if (r) 83230ee2a98SJason J. Herne goto out; 83330ee2a98SJason J. Herne } 83430ee2a98SJason J. Herne out: 83530ee2a98SJason J. Herne kvfree(keys); 83630ee2a98SJason J. Herne return r; 83730ee2a98SJason J. Herne } 83830ee2a98SJason J. Herne 839b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 840b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 841b0c632dbSHeiko Carstens { 842b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 843b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 844f2061656SDominik Dingel struct kvm_device_attr attr; 845b0c632dbSHeiko Carstens int r; 846b0c632dbSHeiko Carstens 847b0c632dbSHeiko Carstens switch (ioctl) { 848ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 849ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 850ba5c1e9bSCarsten Otte 851ba5c1e9bSCarsten Otte r = -EFAULT; 852ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 853ba5c1e9bSCarsten Otte break; 854ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 855ba5c1e9bSCarsten Otte break; 856ba5c1e9bSCarsten Otte } 857d938dc55SCornelia Huck case KVM_ENABLE_CAP: { 858d938dc55SCornelia Huck struct kvm_enable_cap cap; 859d938dc55SCornelia Huck r = -EFAULT; 860d938dc55SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 861d938dc55SCornelia Huck break; 862d938dc55SCornelia Huck r = kvm_vm_ioctl_enable_cap(kvm, &cap); 863d938dc55SCornelia Huck break; 864d938dc55SCornelia Huck } 86584223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 86684223598SCornelia Huck struct kvm_irq_routing_entry routing; 86784223598SCornelia Huck 86884223598SCornelia Huck r = -EINVAL; 86984223598SCornelia Huck if (kvm->arch.use_irqchip) { 87084223598SCornelia Huck /* Set up dummy routing. */ 87184223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 87284223598SCornelia Huck kvm_set_irq_routing(kvm, &routing, 0, 0); 87384223598SCornelia Huck r = 0; 87484223598SCornelia Huck } 87584223598SCornelia Huck break; 87684223598SCornelia Huck } 877f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 878f2061656SDominik Dingel r = -EFAULT; 879f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 880f2061656SDominik Dingel break; 881f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 882f2061656SDominik Dingel break; 883f2061656SDominik Dingel } 884f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 885f2061656SDominik Dingel r = -EFAULT; 886f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 887f2061656SDominik Dingel break; 888f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 889f2061656SDominik Dingel break; 890f2061656SDominik Dingel } 891f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 892f2061656SDominik Dingel r = -EFAULT; 893f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 894f2061656SDominik Dingel break; 895f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 896f2061656SDominik Dingel break; 897f2061656SDominik Dingel } 89830ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 89930ee2a98SJason J. Herne struct kvm_s390_skeys args; 90030ee2a98SJason J. Herne 90130ee2a98SJason J. Herne r = -EFAULT; 90230ee2a98SJason J. Herne if (copy_from_user(&args, argp, 90330ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 90430ee2a98SJason J. Herne break; 90530ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 90630ee2a98SJason J. Herne break; 90730ee2a98SJason J. Herne } 90830ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 90930ee2a98SJason J. Herne struct kvm_s390_skeys args; 91030ee2a98SJason J. Herne 91130ee2a98SJason J. Herne r = -EFAULT; 91230ee2a98SJason J. Herne if (copy_from_user(&args, argp, 91330ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 91430ee2a98SJason J. Herne break; 91530ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 91630ee2a98SJason J. Herne break; 91730ee2a98SJason J. Herne } 918b0c632dbSHeiko Carstens default: 919367e1319SAvi Kivity r = -ENOTTY; 920b0c632dbSHeiko Carstens } 921b0c632dbSHeiko Carstens 922b0c632dbSHeiko Carstens return r; 923b0c632dbSHeiko Carstens } 924b0c632dbSHeiko Carstens 92545c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config) 92645c9b47cSTony Krowiak { 92745c9b47cSTony Krowiak u32 fcn_code = 0x04000000UL; 92886044c8cSChristian Borntraeger u32 cc = 0; 92945c9b47cSTony Krowiak 93086044c8cSChristian Borntraeger memset(config, 0, 128); 93145c9b47cSTony Krowiak asm volatile( 93245c9b47cSTony Krowiak "lgr 0,%1\n" 93345c9b47cSTony Krowiak "lgr 2,%2\n" 93445c9b47cSTony Krowiak ".long 0xb2af0000\n" /* PQAP(QCI) */ 93586044c8cSChristian Borntraeger "0: ipm %0\n" 93645c9b47cSTony Krowiak "srl %0,28\n" 93786044c8cSChristian Borntraeger "1:\n" 93886044c8cSChristian Borntraeger EX_TABLE(0b, 1b) 93986044c8cSChristian Borntraeger : "+r" (cc) 94045c9b47cSTony Krowiak : "r" (fcn_code), "r" (config) 94145c9b47cSTony Krowiak : "cc", "0", "2", "memory" 94245c9b47cSTony Krowiak ); 94345c9b47cSTony Krowiak 94445c9b47cSTony Krowiak return cc; 94545c9b47cSTony Krowiak } 94645c9b47cSTony Krowiak 94745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 94845c9b47cSTony Krowiak { 94945c9b47cSTony Krowiak u8 config[128]; 95045c9b47cSTony Krowiak int cc; 95145c9b47cSTony Krowiak 95245c9b47cSTony Krowiak if (test_facility(2) && test_facility(12)) { 95345c9b47cSTony Krowiak cc = kvm_s390_query_ap_config(config); 95445c9b47cSTony Krowiak 95545c9b47cSTony Krowiak if (cc) 95645c9b47cSTony Krowiak pr_err("PQAP(QCI) failed with cc=%d", cc); 95745c9b47cSTony Krowiak else 95845c9b47cSTony Krowiak return config[0] & 0x40; 95945c9b47cSTony Krowiak } 96045c9b47cSTony Krowiak 96145c9b47cSTony Krowiak return 0; 96245c9b47cSTony Krowiak } 96345c9b47cSTony Krowiak 96445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 96545c9b47cSTony Krowiak { 96645c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 96745c9b47cSTony Krowiak 96845c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 96945c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 97045c9b47cSTony Krowiak else 97145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 97245c9b47cSTony Krowiak } 97345c9b47cSTony Krowiak 9749d8d5786SMichael Mueller static void kvm_s390_get_cpu_id(struct cpuid *cpu_id) 9759d8d5786SMichael Mueller { 9769d8d5786SMichael Mueller get_cpu_id(cpu_id); 9779d8d5786SMichael Mueller cpu_id->version = 0xff; 9789d8d5786SMichael Mueller } 9799d8d5786SMichael Mueller 9805102ee87STony Krowiak static int kvm_s390_crypto_init(struct kvm *kvm) 9815102ee87STony Krowiak { 9829d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 9835102ee87STony Krowiak return 0; 9845102ee87STony Krowiak 9855102ee87STony Krowiak kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb), 9865102ee87STony Krowiak GFP_KERNEL | GFP_DMA); 9875102ee87STony Krowiak if (!kvm->arch.crypto.crycb) 9885102ee87STony Krowiak return -ENOMEM; 9895102ee87STony Krowiak 99045c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 9915102ee87STony Krowiak 992ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 993ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 994ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 995ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 996ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 997ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 998ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 999a374e892STony Krowiak 10005102ee87STony Krowiak return 0; 10015102ee87STony Krowiak } 10025102ee87STony Krowiak 1003e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1004b0c632dbSHeiko Carstens { 10059d8d5786SMichael Mueller int i, rc; 1006b0c632dbSHeiko Carstens char debug_name[16]; 1007f6c137ffSChristian Borntraeger static unsigned long sca_offset; 1008b0c632dbSHeiko Carstens 1009e08b9637SCarsten Otte rc = -EINVAL; 1010e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1011e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 1012e08b9637SCarsten Otte goto out_err; 1013e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 1014e08b9637SCarsten Otte goto out_err; 1015e08b9637SCarsten Otte #else 1016e08b9637SCarsten Otte if (type) 1017e08b9637SCarsten Otte goto out_err; 1018e08b9637SCarsten Otte #endif 1019e08b9637SCarsten Otte 1020b0c632dbSHeiko Carstens rc = s390_enable_sie(); 1021b0c632dbSHeiko Carstens if (rc) 1022d89f5effSJan Kiszka goto out_err; 1023b0c632dbSHeiko Carstens 1024b290411aSCarsten Otte rc = -ENOMEM; 1025b290411aSCarsten Otte 1026b0c632dbSHeiko Carstens kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL); 1027b0c632dbSHeiko Carstens if (!kvm->arch.sca) 1028d89f5effSJan Kiszka goto out_err; 1029f6c137ffSChristian Borntraeger spin_lock(&kvm_lock); 1030f6c137ffSChristian Borntraeger sca_offset = (sca_offset + 16) & 0x7f0; 1031f6c137ffSChristian Borntraeger kvm->arch.sca = (struct sca_block *) ((char *) kvm->arch.sca + sca_offset); 1032f6c137ffSChristian Borntraeger spin_unlock(&kvm_lock); 1033b0c632dbSHeiko Carstens 1034b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 1035b0c632dbSHeiko Carstens 1036b0c632dbSHeiko Carstens kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long)); 1037b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 103840f5b735SDominik Dingel goto out_err; 1039b0c632dbSHeiko Carstens 10409d8d5786SMichael Mueller /* 10419d8d5786SMichael Mueller * The architectural maximum amount of facilities is 16 kbit. To store 10429d8d5786SMichael Mueller * this amount, 2 kbyte of memory is required. Thus we need a full 1043981467c9SMichael Mueller * page to hold the guest facility list (arch.model.fac->list) and the 1044981467c9SMichael Mueller * facility mask (arch.model.fac->mask). Its address size has to be 10459d8d5786SMichael Mueller * 31 bits and word aligned. 10469d8d5786SMichael Mueller */ 10479d8d5786SMichael Mueller kvm->arch.model.fac = 1048981467c9SMichael Mueller (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 10499d8d5786SMichael Mueller if (!kvm->arch.model.fac) 105040f5b735SDominik Dingel goto out_err; 10519d8d5786SMichael Mueller 1052fb5bf93fSMichael Mueller /* Populate the facility mask initially. */ 1053981467c9SMichael Mueller memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list, 105494422ee8SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 10559d8d5786SMichael Mueller for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) { 10569d8d5786SMichael Mueller if (i < kvm_s390_fac_list_mask_size()) 1057981467c9SMichael Mueller kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i]; 10589d8d5786SMichael Mueller else 1059981467c9SMichael Mueller kvm->arch.model.fac->mask[i] = 0UL; 10609d8d5786SMichael Mueller } 10619d8d5786SMichael Mueller 1062981467c9SMichael Mueller /* Populate the facility list initially. */ 1063981467c9SMichael Mueller memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask, 1064981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1065981467c9SMichael Mueller 10669d8d5786SMichael Mueller kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id); 1067658b6edaSMichael Mueller kvm->arch.model.ibc = sclp_get_ibc() & 0x0fff; 10689d8d5786SMichael Mueller 10695102ee87STony Krowiak if (kvm_s390_crypto_init(kvm) < 0) 107040f5b735SDominik Dingel goto out_err; 10715102ee87STony Krowiak 1072ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 1073*6d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 1074*6d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 10758a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 1076a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 1077ba5c1e9bSCarsten Otte 1078b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 1079b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "%s", "vm created"); 1080b0c632dbSHeiko Carstens 1081e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 1082e08b9637SCarsten Otte kvm->arch.gmap = NULL; 1083e08b9637SCarsten Otte } else { 10840349985aSChristian Borntraeger kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1); 1085598841caSCarsten Otte if (!kvm->arch.gmap) 108640f5b735SDominik Dingel goto out_err; 10872c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 108824eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 1089e08b9637SCarsten Otte } 1090fa6b7fe9SCornelia Huck 1091fa6b7fe9SCornelia Huck kvm->arch.css_support = 0; 109284223598SCornelia Huck kvm->arch.use_irqchip = 0; 109372f25020SJason J. Herne kvm->arch.epoch = 0; 1094fa6b7fe9SCornelia Huck 10958ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 10968ad35755SDavid Hildenbrand 1097d89f5effSJan Kiszka return 0; 1098d89f5effSJan Kiszka out_err: 109940f5b735SDominik Dingel kfree(kvm->arch.crypto.crycb); 110040f5b735SDominik Dingel free_page((unsigned long)kvm->arch.model.fac); 110140f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 110240f5b735SDominik Dingel free_page((unsigned long)(kvm->arch.sca)); 1103d89f5effSJan Kiszka return rc; 1104b0c632dbSHeiko Carstens } 1105b0c632dbSHeiko Carstens 1106d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 1107d329c035SChristian Borntraeger { 1108d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 1109ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 111067335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 11113c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 111258f9460bSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 111358f9460bSCarsten Otte clear_bit(63 - vcpu->vcpu_id, 111458f9460bSCarsten Otte (unsigned long *) &vcpu->kvm->arch.sca->mcn); 1115abf4a71eSCarsten Otte if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda == 1116abf4a71eSCarsten Otte (__u64) vcpu->arch.sie_block) 1117abf4a71eSCarsten Otte vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0; 111858f9460bSCarsten Otte } 1119abf4a71eSCarsten Otte smp_mb(); 112027e0393fSCarsten Otte 112127e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 112227e0393fSCarsten Otte gmap_free(vcpu->arch.gmap); 112327e0393fSCarsten Otte 1124b31605c1SDominik Dingel if (kvm_s390_cmma_enabled(vcpu->kvm)) 1125b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 1126d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 1127b31288faSKonstantin Weitz 11286692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 1129b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 1130d329c035SChristian Borntraeger } 1131d329c035SChristian Borntraeger 1132d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 1133d329c035SChristian Borntraeger { 1134d329c035SChristian Borntraeger unsigned int i; 1135988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 1136d329c035SChristian Borntraeger 1137988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 1138988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 1139988a2caeSGleb Natapov 1140988a2caeSGleb Natapov mutex_lock(&kvm->lock); 1141988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 1142d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 1143988a2caeSGleb Natapov 1144988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 1145988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 1146d329c035SChristian Borntraeger } 1147d329c035SChristian Borntraeger 1148b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 1149b0c632dbSHeiko Carstens { 1150d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 11519d8d5786SMichael Mueller free_page((unsigned long)kvm->arch.model.fac); 1152b0c632dbSHeiko Carstens free_page((unsigned long)(kvm->arch.sca)); 1153d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 11545102ee87STony Krowiak kfree(kvm->arch.crypto.crycb); 115527e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 1156598841caSCarsten Otte gmap_free(kvm->arch.gmap); 1157841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 115867335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 1159b0c632dbSHeiko Carstens } 1160b0c632dbSHeiko Carstens 1161b0c632dbSHeiko Carstens /* Section: vcpu related */ 1162dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 1163b0c632dbSHeiko Carstens { 1164c6c956b8SMartin Schwidefsky vcpu->arch.gmap = gmap_alloc(current->mm, -1UL); 116527e0393fSCarsten Otte if (!vcpu->arch.gmap) 116627e0393fSCarsten Otte return -ENOMEM; 11672c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 1168dafd032aSDominik Dingel 116927e0393fSCarsten Otte return 0; 117027e0393fSCarsten Otte } 117127e0393fSCarsten Otte 1172dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 1173dafd032aSDominik Dingel { 1174dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 1175dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 117659674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 117759674c1aSChristian Borntraeger KVM_SYNC_GPRS | 11789eed0735SChristian Borntraeger KVM_SYNC_ACRS | 1179b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 1180b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 1181b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 118268c55750SEric Farman if (test_kvm_facility(vcpu->kvm, 129)) 118368c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 1184dafd032aSDominik Dingel 1185dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 1186dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 1187dafd032aSDominik Dingel 1188b0c632dbSHeiko Carstens return 0; 1189b0c632dbSHeiko Carstens } 1190b0c632dbSHeiko Carstens 1191b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1192b0c632dbSHeiko Carstens { 11934725c860SMartin Schwidefsky save_fp_ctl(&vcpu->arch.host_fpregs.fpc); 119418280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) 119568c55750SEric Farman save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs); 119668c55750SEric Farman else 11974725c860SMartin Schwidefsky save_fp_regs(vcpu->arch.host_fpregs.fprs); 1198b0c632dbSHeiko Carstens save_access_regs(vcpu->arch.host_acrs); 119918280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 120068c55750SEric Farman restore_fp_ctl(&vcpu->run->s.regs.fpc); 120168c55750SEric Farman restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); 120268c55750SEric Farman } else { 12034725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 12044725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.guest_fpregs.fprs); 120568c55750SEric Farman } 120659674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 1207480e5926SChristian Borntraeger gmap_enable(vcpu->arch.gmap); 12089e6dabefSCornelia Huck atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 1209b0c632dbSHeiko Carstens } 1210b0c632dbSHeiko Carstens 1211b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 1212b0c632dbSHeiko Carstens { 12139e6dabefSCornelia Huck atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 1214480e5926SChristian Borntraeger gmap_disable(vcpu->arch.gmap); 121518280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 121668c55750SEric Farman save_fp_ctl(&vcpu->run->s.regs.fpc); 121768c55750SEric Farman save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); 121868c55750SEric Farman } else { 12194725c860SMartin Schwidefsky save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 12204725c860SMartin Schwidefsky save_fp_regs(vcpu->arch.guest_fpregs.fprs); 122168c55750SEric Farman } 122259674c1aSChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 12234725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.host_fpregs.fpc); 122418280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) 122568c55750SEric Farman restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs); 122668c55750SEric Farman else 12274725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.host_fpregs.fprs); 1228b0c632dbSHeiko Carstens restore_access_regs(vcpu->arch.host_acrs); 1229b0c632dbSHeiko Carstens } 1230b0c632dbSHeiko Carstens 1231b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 1232b0c632dbSHeiko Carstens { 1233b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 1234b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 1235b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 12368d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 1237b0c632dbSHeiko Carstens vcpu->arch.sie_block->cputm = 0UL; 1238b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 1239b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 1240b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 1241b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 1242b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 1243b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fpc = 0; 1244b0c632dbSHeiko Carstens asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc)); 1245b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 1246672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 12473c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 12483c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 12496352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 12506852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 12512ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 1252b0c632dbSHeiko Carstens } 1253b0c632dbSHeiko Carstens 125431928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 125542897d86SMarcelo Tosatti { 125672f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 125772f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 125872f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 1259dafd032aSDominik Dingel if (!kvm_is_ucontrol(vcpu->kvm)) 1260dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 126142897d86SMarcelo Tosatti } 126242897d86SMarcelo Tosatti 12635102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 12645102ee87STony Krowiak { 12659d8d5786SMichael Mueller if (!test_kvm_facility(vcpu->kvm, 76)) 12665102ee87STony Krowiak return; 12675102ee87STony Krowiak 1268a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 1269a374e892STony Krowiak 1270a374e892STony Krowiak if (vcpu->kvm->arch.crypto.aes_kw) 1271a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 1272a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 1273a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 1274a374e892STony Krowiak 12755102ee87STony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 12765102ee87STony Krowiak } 12775102ee87STony Krowiak 1278b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 1279b31605c1SDominik Dingel { 1280b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 1281b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 1282b31605c1SDominik Dingel } 1283b31605c1SDominik Dingel 1284b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 1285b31605c1SDominik Dingel { 1286b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 1287b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 1288b31605c1SDominik Dingel return -ENOMEM; 1289b31605c1SDominik Dingel 1290b31605c1SDominik Dingel vcpu->arch.sie_block->ecb2 |= 0x80; 1291b31605c1SDominik Dingel vcpu->arch.sie_block->ecb2 &= ~0x08; 1292b31605c1SDominik Dingel return 0; 1293b31605c1SDominik Dingel } 1294b31605c1SDominik Dingel 129591520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 129691520f1aSMichael Mueller { 129791520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 129891520f1aSMichael Mueller 129991520f1aSMichael Mueller vcpu->arch.cpu_id = model->cpu_id; 130091520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 130191520f1aSMichael Mueller vcpu->arch.sie_block->fac = (int) (long) model->fac->list; 130291520f1aSMichael Mueller } 130391520f1aSMichael Mueller 1304b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 1305b0c632dbSHeiko Carstens { 1306b31605c1SDominik Dingel int rc = 0; 1307b31288faSKonstantin Weitz 13089e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 13099e6dabefSCornelia Huck CPUSTAT_SM | 131069d0d3a3SChristian Borntraeger CPUSTAT_STOPPED | 131169d0d3a3SChristian Borntraeger CPUSTAT_GED); 131291520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 131391520f1aSMichael Mueller 1314fc34531dSChristian Borntraeger vcpu->arch.sie_block->ecb = 6; 13159d8d5786SMichael Mueller if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73)) 13167feb6bb8SMichael Mueller vcpu->arch.sie_block->ecb |= 0x10; 13177feb6bb8SMichael Mueller 131869d0d3a3SChristian Borntraeger vcpu->arch.sie_block->ecb2 = 8; 1319ea5f4969SDavid Hildenbrand vcpu->arch.sie_block->eca = 0xC1002000U; 1320217a4406SHeiko Carstens if (sclp_has_siif()) 1321217a4406SHeiko Carstens vcpu->arch.sie_block->eca |= 1; 1322ea5f4969SDavid Hildenbrand if (sclp_has_sigpif()) 1323ea5f4969SDavid Hildenbrand vcpu->arch.sie_block->eca |= 0x10000000U; 132418280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 132513211ea7SEric Farman vcpu->arch.sie_block->eca |= 0x00020000; 132613211ea7SEric Farman vcpu->arch.sie_block->ecd |= 0x20000000; 132713211ea7SEric Farman } 1328492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 13295a5e6536SMatthew Rosato 1330b31605c1SDominik Dingel if (kvm_s390_cmma_enabled(vcpu->kvm)) { 1331b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 1332b31605c1SDominik Dingel if (rc) 1333b31605c1SDominik Dingel return rc; 1334b31288faSKonstantin Weitz } 13350ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1336ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 13379d8d5786SMichael Mueller 13385102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 13395102ee87STony Krowiak 1340b31605c1SDominik Dingel return rc; 1341b0c632dbSHeiko Carstens } 1342b0c632dbSHeiko Carstens 1343b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 1344b0c632dbSHeiko Carstens unsigned int id) 1345b0c632dbSHeiko Carstens { 13464d47555aSCarsten Otte struct kvm_vcpu *vcpu; 13477feb6bb8SMichael Mueller struct sie_page *sie_page; 13484d47555aSCarsten Otte int rc = -EINVAL; 1349b0c632dbSHeiko Carstens 13504d47555aSCarsten Otte if (id >= KVM_MAX_VCPUS) 13514d47555aSCarsten Otte goto out; 13524d47555aSCarsten Otte 13534d47555aSCarsten Otte rc = -ENOMEM; 13544d47555aSCarsten Otte 1355b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 1356b0c632dbSHeiko Carstens if (!vcpu) 13574d47555aSCarsten Otte goto out; 1358b0c632dbSHeiko Carstens 13597feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 13607feb6bb8SMichael Mueller if (!sie_page) 1361b0c632dbSHeiko Carstens goto out_free_cpu; 1362b0c632dbSHeiko Carstens 13637feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 13647feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 136568c55750SEric Farman vcpu->arch.host_vregs = &sie_page->vregs; 13667feb6bb8SMichael Mueller 1367b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 136858f9460bSCarsten Otte if (!kvm_is_ucontrol(kvm)) { 136958f9460bSCarsten Otte if (!kvm->arch.sca) { 137058f9460bSCarsten Otte WARN_ON_ONCE(1); 137158f9460bSCarsten Otte goto out_free_cpu; 137258f9460bSCarsten Otte } 1373abf4a71eSCarsten Otte if (!kvm->arch.sca->cpu[id].sda) 137458f9460bSCarsten Otte kvm->arch.sca->cpu[id].sda = 137558f9460bSCarsten Otte (__u64) vcpu->arch.sie_block; 137658f9460bSCarsten Otte vcpu->arch.sie_block->scaoh = 137758f9460bSCarsten Otte (__u32)(((__u64)kvm->arch.sca) >> 32); 1378b0c632dbSHeiko Carstens vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca; 1379fc34531dSChristian Borntraeger set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn); 138058f9460bSCarsten Otte } 1381b0c632dbSHeiko Carstens 1382ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 1383ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 1384d0321a24SChristian Borntraeger vcpu->arch.local_int.wq = &vcpu->wq; 13855288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 1386ba5c1e9bSCarsten Otte 1387b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 1388b0c632dbSHeiko Carstens if (rc) 13897b06bf2fSWei Yongjun goto out_free_sie_block; 1390b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu, 1391b0c632dbSHeiko Carstens vcpu->arch.sie_block); 1392ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 1393b0c632dbSHeiko Carstens 1394b0c632dbSHeiko Carstens return vcpu; 13957b06bf2fSWei Yongjun out_free_sie_block: 13967b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 1397b0c632dbSHeiko Carstens out_free_cpu: 1398b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 13994d47555aSCarsten Otte out: 1400b0c632dbSHeiko Carstens return ERR_PTR(rc); 1401b0c632dbSHeiko Carstens } 1402b0c632dbSHeiko Carstens 1403b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 1404b0c632dbSHeiko Carstens { 14059a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 1406b0c632dbSHeiko Carstens } 1407b0c632dbSHeiko Carstens 140849b99e1eSChristian Borntraeger void s390_vcpu_block(struct kvm_vcpu *vcpu) 140949b99e1eSChristian Borntraeger { 141049b99e1eSChristian Borntraeger atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 141149b99e1eSChristian Borntraeger } 141249b99e1eSChristian Borntraeger 141349b99e1eSChristian Borntraeger void s390_vcpu_unblock(struct kvm_vcpu *vcpu) 141449b99e1eSChristian Borntraeger { 141549b99e1eSChristian Borntraeger atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 141649b99e1eSChristian Borntraeger } 141749b99e1eSChristian Borntraeger 141849b99e1eSChristian Borntraeger /* 141949b99e1eSChristian Borntraeger * Kick a guest cpu out of SIE and wait until SIE is not running. 142049b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 142149b99e1eSChristian Borntraeger * return immediately. */ 142249b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 142349b99e1eSChristian Borntraeger { 142449b99e1eSChristian Borntraeger atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); 142549b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 142649b99e1eSChristian Borntraeger cpu_relax(); 142749b99e1eSChristian Borntraeger } 142849b99e1eSChristian Borntraeger 142949b99e1eSChristian Borntraeger /* Kick a guest cpu out of SIE and prevent SIE-reentry */ 143049b99e1eSChristian Borntraeger void exit_sie_sync(struct kvm_vcpu *vcpu) 143149b99e1eSChristian Borntraeger { 143249b99e1eSChristian Borntraeger s390_vcpu_block(vcpu); 143349b99e1eSChristian Borntraeger exit_sie(vcpu); 143449b99e1eSChristian Borntraeger } 143549b99e1eSChristian Borntraeger 14362c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address) 14372c70fe44SChristian Borntraeger { 14382c70fe44SChristian Borntraeger int i; 14392c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 14402c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 14412c70fe44SChristian Borntraeger 14422c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 14432c70fe44SChristian Borntraeger /* match against both prefix pages */ 1444fda902cbSMichael Mueller if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) { 14452c70fe44SChristian Borntraeger VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address); 14462c70fe44SChristian Borntraeger kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 14472c70fe44SChristian Borntraeger exit_sie_sync(vcpu); 14482c70fe44SChristian Borntraeger } 14492c70fe44SChristian Borntraeger } 14502c70fe44SChristian Borntraeger } 14512c70fe44SChristian Borntraeger 1452b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 1453b6d33834SChristoffer Dall { 1454b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 1455b6d33834SChristoffer Dall BUG(); 1456b6d33834SChristoffer Dall return 0; 1457b6d33834SChristoffer Dall } 1458b6d33834SChristoffer Dall 145914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 146014eebd91SCarsten Otte struct kvm_one_reg *reg) 146114eebd91SCarsten Otte { 146214eebd91SCarsten Otte int r = -EINVAL; 146314eebd91SCarsten Otte 146414eebd91SCarsten Otte switch (reg->id) { 146529b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 146629b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 146729b7c71bSCarsten Otte (u32 __user *)reg->addr); 146829b7c71bSCarsten Otte break; 146929b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 147029b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 147129b7c71bSCarsten Otte (u64 __user *)reg->addr); 147229b7c71bSCarsten Otte break; 147346a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 147446a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->cputm, 147546a6dd1cSJason J. herne (u64 __user *)reg->addr); 147646a6dd1cSJason J. herne break; 147746a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 147846a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 147946a6dd1cSJason J. herne (u64 __user *)reg->addr); 148046a6dd1cSJason J. herne break; 1481536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 1482536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 1483536336c2SDominik Dingel (u64 __user *)reg->addr); 1484536336c2SDominik Dingel break; 1485536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 1486536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 1487536336c2SDominik Dingel (u64 __user *)reg->addr); 1488536336c2SDominik Dingel break; 1489536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 1490536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 1491536336c2SDominik Dingel (u64 __user *)reg->addr); 1492536336c2SDominik Dingel break; 1493672550fbSChristian Borntraeger case KVM_REG_S390_PP: 1494672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 1495672550fbSChristian Borntraeger (u64 __user *)reg->addr); 1496672550fbSChristian Borntraeger break; 1497afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 1498afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 1499afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 1500afa45ff5SChristian Borntraeger break; 150114eebd91SCarsten Otte default: 150214eebd91SCarsten Otte break; 150314eebd91SCarsten Otte } 150414eebd91SCarsten Otte 150514eebd91SCarsten Otte return r; 150614eebd91SCarsten Otte } 150714eebd91SCarsten Otte 150814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 150914eebd91SCarsten Otte struct kvm_one_reg *reg) 151014eebd91SCarsten Otte { 151114eebd91SCarsten Otte int r = -EINVAL; 151214eebd91SCarsten Otte 151314eebd91SCarsten Otte switch (reg->id) { 151429b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 151529b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 151629b7c71bSCarsten Otte (u32 __user *)reg->addr); 151729b7c71bSCarsten Otte break; 151829b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 151929b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 152029b7c71bSCarsten Otte (u64 __user *)reg->addr); 152129b7c71bSCarsten Otte break; 152246a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 152346a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->cputm, 152446a6dd1cSJason J. herne (u64 __user *)reg->addr); 152546a6dd1cSJason J. herne break; 152646a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 152746a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 152846a6dd1cSJason J. herne (u64 __user *)reg->addr); 152946a6dd1cSJason J. herne break; 1530536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 1531536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 1532536336c2SDominik Dingel (u64 __user *)reg->addr); 15339fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 15349fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 1535536336c2SDominik Dingel break; 1536536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 1537536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 1538536336c2SDominik Dingel (u64 __user *)reg->addr); 1539536336c2SDominik Dingel break; 1540536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 1541536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 1542536336c2SDominik Dingel (u64 __user *)reg->addr); 1543536336c2SDominik Dingel break; 1544672550fbSChristian Borntraeger case KVM_REG_S390_PP: 1545672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 1546672550fbSChristian Borntraeger (u64 __user *)reg->addr); 1547672550fbSChristian Borntraeger break; 1548afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 1549afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 1550afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 1551afa45ff5SChristian Borntraeger break; 155214eebd91SCarsten Otte default: 155314eebd91SCarsten Otte break; 155414eebd91SCarsten Otte } 155514eebd91SCarsten Otte 155614eebd91SCarsten Otte return r; 155714eebd91SCarsten Otte } 1558b6d33834SChristoffer Dall 1559b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 1560b0c632dbSHeiko Carstens { 1561b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 1562b0c632dbSHeiko Carstens return 0; 1563b0c632dbSHeiko Carstens } 1564b0c632dbSHeiko Carstens 1565b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1566b0c632dbSHeiko Carstens { 15675a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 1568b0c632dbSHeiko Carstens return 0; 1569b0c632dbSHeiko Carstens } 1570b0c632dbSHeiko Carstens 1571b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1572b0c632dbSHeiko Carstens { 15735a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 1574b0c632dbSHeiko Carstens return 0; 1575b0c632dbSHeiko Carstens } 1576b0c632dbSHeiko Carstens 1577b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1578b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 1579b0c632dbSHeiko Carstens { 158059674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 1581b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 158259674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 1583b0c632dbSHeiko Carstens return 0; 1584b0c632dbSHeiko Carstens } 1585b0c632dbSHeiko Carstens 1586b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1587b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 1588b0c632dbSHeiko Carstens { 158959674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 1590b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 1591b0c632dbSHeiko Carstens return 0; 1592b0c632dbSHeiko Carstens } 1593b0c632dbSHeiko Carstens 1594b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1595b0c632dbSHeiko Carstens { 15964725c860SMartin Schwidefsky if (test_fp_ctl(fpu->fpc)) 15974725c860SMartin Schwidefsky return -EINVAL; 1598b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 15994725c860SMartin Schwidefsky vcpu->arch.guest_fpregs.fpc = fpu->fpc; 16004725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 16014725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.guest_fpregs.fprs); 1602b0c632dbSHeiko Carstens return 0; 1603b0c632dbSHeiko Carstens } 1604b0c632dbSHeiko Carstens 1605b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1606b0c632dbSHeiko Carstens { 1607b0c632dbSHeiko Carstens memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs)); 1608b0c632dbSHeiko Carstens fpu->fpc = vcpu->arch.guest_fpregs.fpc; 1609b0c632dbSHeiko Carstens return 0; 1610b0c632dbSHeiko Carstens } 1611b0c632dbSHeiko Carstens 1612b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 1613b0c632dbSHeiko Carstens { 1614b0c632dbSHeiko Carstens int rc = 0; 1615b0c632dbSHeiko Carstens 16167a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 1617b0c632dbSHeiko Carstens rc = -EBUSY; 1618d7b0b5ebSCarsten Otte else { 1619d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 1620d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 1621d7b0b5ebSCarsten Otte } 1622b0c632dbSHeiko Carstens return rc; 1623b0c632dbSHeiko Carstens } 1624b0c632dbSHeiko Carstens 1625b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1626b0c632dbSHeiko Carstens struct kvm_translation *tr) 1627b0c632dbSHeiko Carstens { 1628b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 1629b0c632dbSHeiko Carstens } 1630b0c632dbSHeiko Carstens 163127291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 163227291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 163327291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 163427291e21SDavid Hildenbrand 1635d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 1636d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 1637b0c632dbSHeiko Carstens { 163827291e21SDavid Hildenbrand int rc = 0; 163927291e21SDavid Hildenbrand 164027291e21SDavid Hildenbrand vcpu->guest_debug = 0; 164127291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 164227291e21SDavid Hildenbrand 16432de3bfc2SDavid Hildenbrand if (dbg->control & ~VALID_GUESTDBG_FLAGS) 164427291e21SDavid Hildenbrand return -EINVAL; 164527291e21SDavid Hildenbrand 164627291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 164727291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 164827291e21SDavid Hildenbrand /* enforce guest PER */ 164927291e21SDavid Hildenbrand atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 165027291e21SDavid Hildenbrand 165127291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 165227291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 165327291e21SDavid Hildenbrand } else { 165427291e21SDavid Hildenbrand atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 165527291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 165627291e21SDavid Hildenbrand } 165727291e21SDavid Hildenbrand 165827291e21SDavid Hildenbrand if (rc) { 165927291e21SDavid Hildenbrand vcpu->guest_debug = 0; 166027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 166127291e21SDavid Hildenbrand atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 166227291e21SDavid Hildenbrand } 166327291e21SDavid Hildenbrand 166427291e21SDavid Hildenbrand return rc; 1665b0c632dbSHeiko Carstens } 1666b0c632dbSHeiko Carstens 166762d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 166862d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 166962d9f0dbSMarcelo Tosatti { 16706352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 16716352e4d2SDavid Hildenbrand return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 16726352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 167362d9f0dbSMarcelo Tosatti } 167462d9f0dbSMarcelo Tosatti 167562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 167662d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 167762d9f0dbSMarcelo Tosatti { 16786352e4d2SDavid Hildenbrand int rc = 0; 16796352e4d2SDavid Hildenbrand 16806352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 16816352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 16826352e4d2SDavid Hildenbrand 16836352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 16846352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 16856352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 16866352e4d2SDavid Hildenbrand break; 16876352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 16886352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 16896352e4d2SDavid Hildenbrand break; 16906352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 16916352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 16926352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 16936352e4d2SDavid Hildenbrand default: 16946352e4d2SDavid Hildenbrand rc = -ENXIO; 16956352e4d2SDavid Hildenbrand } 16966352e4d2SDavid Hildenbrand 16976352e4d2SDavid Hildenbrand return rc; 169862d9f0dbSMarcelo Tosatti } 169962d9f0dbSMarcelo Tosatti 1700b31605c1SDominik Dingel bool kvm_s390_cmma_enabled(struct kvm *kvm) 1701b31605c1SDominik Dingel { 1702b31605c1SDominik Dingel if (!MACHINE_IS_LPAR) 1703b31605c1SDominik Dingel return false; 1704b31605c1SDominik Dingel /* only enable for z10 and later */ 1705b31605c1SDominik Dingel if (!MACHINE_HAS_EDAT1) 1706b31605c1SDominik Dingel return false; 1707b31605c1SDominik Dingel if (!kvm->arch.use_cmma) 1708b31605c1SDominik Dingel return false; 1709b31605c1SDominik Dingel return true; 1710b31605c1SDominik Dingel } 1711b31605c1SDominik Dingel 17128ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 17138ad35755SDavid Hildenbrand { 17148ad35755SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS; 17158ad35755SDavid Hildenbrand } 17168ad35755SDavid Hildenbrand 17172c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 17182c70fe44SChristian Borntraeger { 17198ad35755SDavid Hildenbrand retry: 17208ad35755SDavid Hildenbrand s390_vcpu_unblock(vcpu); 17212c70fe44SChristian Borntraeger /* 17222c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 17232c70fe44SChristian Borntraeger * guest prefix page. gmap_ipte_notify will wait on the ptl lock. 17242c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 17252c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 17262c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 17272c70fe44SChristian Borntraeger */ 17288ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 17292c70fe44SChristian Borntraeger int rc; 17302c70fe44SChristian Borntraeger rc = gmap_ipte_notify(vcpu->arch.gmap, 1731fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 17322c70fe44SChristian Borntraeger PAGE_SIZE * 2); 17332c70fe44SChristian Borntraeger if (rc) 17342c70fe44SChristian Borntraeger return rc; 17358ad35755SDavid Hildenbrand goto retry; 17362c70fe44SChristian Borntraeger } 17378ad35755SDavid Hildenbrand 1738d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 1739d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 1740d3d692c8SDavid Hildenbrand goto retry; 1741d3d692c8SDavid Hildenbrand } 1742d3d692c8SDavid Hildenbrand 17438ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 17448ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 17458ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 17468ad35755SDavid Hildenbrand atomic_set_mask(CPUSTAT_IBS, 17478ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 17488ad35755SDavid Hildenbrand } 17498ad35755SDavid Hildenbrand goto retry; 17508ad35755SDavid Hildenbrand } 17518ad35755SDavid Hildenbrand 17528ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 17538ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 17548ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 17558ad35755SDavid Hildenbrand atomic_clear_mask(CPUSTAT_IBS, 17568ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 17578ad35755SDavid Hildenbrand } 17588ad35755SDavid Hildenbrand goto retry; 17598ad35755SDavid Hildenbrand } 17608ad35755SDavid Hildenbrand 17610759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 17620759d068SDavid Hildenbrand clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 17630759d068SDavid Hildenbrand 17642c70fe44SChristian Borntraeger return 0; 17652c70fe44SChristian Borntraeger } 17662c70fe44SChristian Borntraeger 1767fa576c58SThomas Huth /** 1768fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 1769fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 1770fa576c58SThomas Huth * @gpa: Guest physical address 1771fa576c58SThomas Huth * @writable: Whether the page should be writable or not 1772fa576c58SThomas Huth * 1773fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 1774fa576c58SThomas Huth * 1775fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 1776fa576c58SThomas Huth */ 1777fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 177824eb3a82SDominik Dingel { 1779527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 1780527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 178124eb3a82SDominik Dingel } 178224eb3a82SDominik Dingel 17833c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 17843c038e6bSDominik Dingel unsigned long token) 17853c038e6bSDominik Dingel { 17863c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 1787383d0b05SJens Freimann struct kvm_s390_irq irq; 17883c038e6bSDominik Dingel 17893c038e6bSDominik Dingel if (start_token) { 1790383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 1791383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 1792383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 17933c038e6bSDominik Dingel } else { 17943c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 1795383d0b05SJens Freimann inti.parm64 = token; 17963c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 17973c038e6bSDominik Dingel } 17983c038e6bSDominik Dingel } 17993c038e6bSDominik Dingel 18003c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 18013c038e6bSDominik Dingel struct kvm_async_pf *work) 18023c038e6bSDominik Dingel { 18033c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 18043c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 18053c038e6bSDominik Dingel } 18063c038e6bSDominik Dingel 18073c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 18083c038e6bSDominik Dingel struct kvm_async_pf *work) 18093c038e6bSDominik Dingel { 18103c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 18113c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 18123c038e6bSDominik Dingel } 18133c038e6bSDominik Dingel 18143c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 18153c038e6bSDominik Dingel struct kvm_async_pf *work) 18163c038e6bSDominik Dingel { 18173c038e6bSDominik Dingel /* s390 will always inject the page directly */ 18183c038e6bSDominik Dingel } 18193c038e6bSDominik Dingel 18203c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 18213c038e6bSDominik Dingel { 18223c038e6bSDominik Dingel /* 18233c038e6bSDominik Dingel * s390 will always inject the page directly, 18243c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 18253c038e6bSDominik Dingel */ 18263c038e6bSDominik Dingel return true; 18273c038e6bSDominik Dingel } 18283c038e6bSDominik Dingel 18293c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 18303c038e6bSDominik Dingel { 18313c038e6bSDominik Dingel hva_t hva; 18323c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 18333c038e6bSDominik Dingel int rc; 18343c038e6bSDominik Dingel 18353c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 18363c038e6bSDominik Dingel return 0; 18373c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 18383c038e6bSDominik Dingel vcpu->arch.pfault_compare) 18393c038e6bSDominik Dingel return 0; 18403c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 18413c038e6bSDominik Dingel return 0; 18429a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 18433c038e6bSDominik Dingel return 0; 18443c038e6bSDominik Dingel if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) 18453c038e6bSDominik Dingel return 0; 18463c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 18473c038e6bSDominik Dingel return 0; 18483c038e6bSDominik Dingel 184981480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 185081480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 185181480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 18523c038e6bSDominik Dingel return 0; 18533c038e6bSDominik Dingel 18543c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 18553c038e6bSDominik Dingel return rc; 18563c038e6bSDominik Dingel } 18573c038e6bSDominik Dingel 18583fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 1859b0c632dbSHeiko Carstens { 18603fb4c40fSThomas Huth int rc, cpuflags; 1861e168bf8dSCarsten Otte 18623c038e6bSDominik Dingel /* 18633c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 18643c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 18653c038e6bSDominik Dingel * handled outside the worker. 18663c038e6bSDominik Dingel */ 18673c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 18683c038e6bSDominik Dingel 18695a32c1afSChristian Borntraeger memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16); 1870b0c632dbSHeiko Carstens 1871b0c632dbSHeiko Carstens if (need_resched()) 1872b0c632dbSHeiko Carstens schedule(); 1873b0c632dbSHeiko Carstens 1874d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 187571cde587SChristian Borntraeger s390_handle_mcck(); 187671cde587SChristian Borntraeger 187779395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 187879395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 187979395031SJens Freimann if (rc) 188079395031SJens Freimann return rc; 188179395031SJens Freimann } 18820ff31867SCarsten Otte 18832c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 18842c70fe44SChristian Borntraeger if (rc) 18852c70fe44SChristian Borntraeger return rc; 18862c70fe44SChristian Borntraeger 188727291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 188827291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 188927291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 189027291e21SDavid Hildenbrand } 189127291e21SDavid Hildenbrand 1892b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 18933fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 18943fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 18953fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 18962b29a9fdSDominik Dingel 18973fb4c40fSThomas Huth return 0; 18983fb4c40fSThomas Huth } 18993fb4c40fSThomas Huth 1900492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 1901492d8642SThomas Huth { 1902492d8642SThomas Huth psw_t *psw = &vcpu->arch.sie_block->gpsw; 1903492d8642SThomas Huth u8 opcode; 1904492d8642SThomas Huth int rc; 1905492d8642SThomas Huth 1906492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 1907492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 1908492d8642SThomas Huth 1909492d8642SThomas Huth /* 1910492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 1911492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 1912492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 1913492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 1914492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 1915492d8642SThomas Huth * to be able to forward the PSW. 1916492d8642SThomas Huth */ 19178ae04b8fSAlexander Yarygin rc = read_guest(vcpu, psw->addr, 0, &opcode, 1); 1918492d8642SThomas Huth if (rc) 1919492d8642SThomas Huth return kvm_s390_inject_prog_cond(vcpu, rc); 1920492d8642SThomas Huth psw->addr = __rewind_psw(*psw, -insn_length(opcode)); 1921492d8642SThomas Huth 1922492d8642SThomas Huth return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 1923492d8642SThomas Huth } 1924492d8642SThomas Huth 19253fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 19263fb4c40fSThomas Huth { 192724eb3a82SDominik Dingel int rc = -1; 19282b29a9fdSDominik Dingel 19292b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 19302b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 19312b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 19322b29a9fdSDominik Dingel 193327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 193427291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 193527291e21SDavid Hildenbrand 19363fb4c40fSThomas Huth if (exit_reason >= 0) { 19377c470539SMartin Schwidefsky rc = 0; 1938210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 1939210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 1940210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 1941210b1607SThomas Huth current->thread.gmap_addr; 1942210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 1943210b1607SThomas Huth rc = -EREMOTE; 194424eb3a82SDominik Dingel 194524eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 19463c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 194724eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 1948fa576c58SThomas Huth if (kvm_arch_setup_async_pf(vcpu)) { 194924eb3a82SDominik Dingel rc = 0; 1950fa576c58SThomas Huth } else { 1951fa576c58SThomas Huth gpa_t gpa = current->thread.gmap_addr; 1952fa576c58SThomas Huth rc = kvm_arch_fault_in_page(vcpu, gpa, 1); 1953fa576c58SThomas Huth } 195424eb3a82SDominik Dingel } 195524eb3a82SDominik Dingel 1956492d8642SThomas Huth if (rc == -1) 1957492d8642SThomas Huth rc = vcpu_post_run_fault_in_sie(vcpu); 1958b0c632dbSHeiko Carstens 19595a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16); 19603fb4c40fSThomas Huth 1961a76ccff6SThomas Huth if (rc == 0) { 1962a76ccff6SThomas Huth if (kvm_is_ucontrol(vcpu->kvm)) 19632955c83fSChristian Borntraeger /* Don't exit for host interrupts. */ 19642955c83fSChristian Borntraeger rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0; 1965a76ccff6SThomas Huth else 1966a76ccff6SThomas Huth rc = kvm_handle_sie_intercept(vcpu); 1967a76ccff6SThomas Huth } 1968a76ccff6SThomas Huth 19693fb4c40fSThomas Huth return rc; 19703fb4c40fSThomas Huth } 19713fb4c40fSThomas Huth 19723fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 19733fb4c40fSThomas Huth { 19743fb4c40fSThomas Huth int rc, exit_reason; 19753fb4c40fSThomas Huth 1976800c1065SThomas Huth /* 1977800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 1978800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 1979800c1065SThomas Huth */ 1980800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 1981800c1065SThomas Huth 1982a76ccff6SThomas Huth do { 19833fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 19843fb4c40fSThomas Huth if (rc) 1985a76ccff6SThomas Huth break; 19863fb4c40fSThomas Huth 1987800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 19883fb4c40fSThomas Huth /* 1989a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 1990a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 19913fb4c40fSThomas Huth */ 19923fb4c40fSThomas Huth preempt_disable(); 19933fb4c40fSThomas Huth kvm_guest_enter(); 19943fb4c40fSThomas Huth preempt_enable(); 1995a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 1996a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 19973fb4c40fSThomas Huth kvm_guest_exit(); 1998800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 19993fb4c40fSThomas Huth 20003fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 200127291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 20023fb4c40fSThomas Huth 2003800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 2004e168bf8dSCarsten Otte return rc; 2005b0c632dbSHeiko Carstens } 2006b0c632dbSHeiko Carstens 2007b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2008b028ee3eSDavid Hildenbrand { 2009b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 2010b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 2011b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 2012b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 2013b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 2014b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 2015d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 2016d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 2017b028ee3eSDavid Hildenbrand } 2018b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 2019b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm; 2020b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 2021b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 2022b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 2023b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 2024b028ee3eSDavid Hildenbrand } 2025b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 2026b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 2027b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 2028b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 20299fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 20309fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2031b028ee3eSDavid Hildenbrand } 2032b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 2033b028ee3eSDavid Hildenbrand } 2034b028ee3eSDavid Hildenbrand 2035b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2036b028ee3eSDavid Hildenbrand { 2037b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 2038b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 2039b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 2040b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 2041b028ee3eSDavid Hildenbrand kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm; 2042b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 2043b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 2044b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 2045b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 2046b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 2047b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 2048b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 2049b028ee3eSDavid Hildenbrand } 2050b028ee3eSDavid Hildenbrand 2051b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2052b0c632dbSHeiko Carstens { 20538f2abe6aSChristian Borntraeger int rc; 2054b0c632dbSHeiko Carstens sigset_t sigsaved; 2055b0c632dbSHeiko Carstens 205627291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 205727291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 205827291e21SDavid Hildenbrand return 0; 205927291e21SDavid Hildenbrand } 206027291e21SDavid Hildenbrand 2061b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2062b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 2063b0c632dbSHeiko Carstens 20646352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 20656852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 20666352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 20676352e4d2SDavid Hildenbrand pr_err_ratelimited("kvm-s390: can't run stopped vcpu %d\n", 20686352e4d2SDavid Hildenbrand vcpu->vcpu_id); 20696352e4d2SDavid Hildenbrand return -EINVAL; 20706352e4d2SDavid Hildenbrand } 2071b0c632dbSHeiko Carstens 2072b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 2073d7b0b5ebSCarsten Otte 2074dab4079dSHeiko Carstens might_fault(); 2075e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 20769ace903dSChristian Ehrhardt 2077b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 2078b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 20798f2abe6aSChristian Borntraeger rc = -EINTR; 2080b1d16c49SChristian Ehrhardt } 20818f2abe6aSChristian Borntraeger 208227291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 208327291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 208427291e21SDavid Hildenbrand rc = 0; 208527291e21SDavid Hildenbrand } 208627291e21SDavid Hildenbrand 2087b8e660b8SHeiko Carstens if (rc == -EOPNOTSUPP) { 20888f2abe6aSChristian Borntraeger /* intercept cannot be handled in-kernel, prepare kvm-run */ 20898f2abe6aSChristian Borntraeger kvm_run->exit_reason = KVM_EXIT_S390_SIEIC; 20908f2abe6aSChristian Borntraeger kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 20918f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 20928f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 20938f2abe6aSChristian Borntraeger rc = 0; 20948f2abe6aSChristian Borntraeger } 20958f2abe6aSChristian Borntraeger 20968f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 20978f2abe6aSChristian Borntraeger /* intercept was handled, but userspace support is needed 20988f2abe6aSChristian Borntraeger * kvm_run has been prepared by the handler */ 20998f2abe6aSChristian Borntraeger rc = 0; 21008f2abe6aSChristian Borntraeger } 21018f2abe6aSChristian Borntraeger 2102b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 2103d7b0b5ebSCarsten Otte 2104b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2105b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 2106b0c632dbSHeiko Carstens 2107b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 21087e8e6ab4SHeiko Carstens return rc; 2109b0c632dbSHeiko Carstens } 2110b0c632dbSHeiko Carstens 2111b0c632dbSHeiko Carstens /* 2112b0c632dbSHeiko Carstens * store status at address 2113b0c632dbSHeiko Carstens * we use have two special cases: 2114b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 2115b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 2116b0c632dbSHeiko Carstens */ 2117d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 2118b0c632dbSHeiko Carstens { 2119092670cdSCarsten Otte unsigned char archmode = 1; 2120fda902cbSMichael Mueller unsigned int px; 2121178bd789SThomas Huth u64 clkcomp; 2122d0bce605SHeiko Carstens int rc; 2123b0c632dbSHeiko Carstens 2124d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 2125d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 2126b0c632dbSHeiko Carstens return -EFAULT; 2127d0bce605SHeiko Carstens gpa = SAVE_AREA_BASE; 2128d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 2129d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 2130b0c632dbSHeiko Carstens return -EFAULT; 2131d0bce605SHeiko Carstens gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE); 2132d0bce605SHeiko Carstens } 2133d0bce605SHeiko Carstens rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs), 2134d0bce605SHeiko Carstens vcpu->arch.guest_fpregs.fprs, 128); 2135d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs), 2136d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 2137d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw), 2138d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 2139fda902cbSMichael Mueller px = kvm_s390_get_prefix(vcpu); 2140d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg), 2141fda902cbSMichael Mueller &px, 4); 2142d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, 2143d0bce605SHeiko Carstens gpa + offsetof(struct save_area, fp_ctrl_reg), 2144d0bce605SHeiko Carstens &vcpu->arch.guest_fpregs.fpc, 4); 2145d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg), 2146d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 2147d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer), 2148d0bce605SHeiko Carstens &vcpu->arch.sie_block->cputm, 8); 2149178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 2150d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp), 2151d0bce605SHeiko Carstens &clkcomp, 8); 2152d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs), 2153d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 2154d0bce605SHeiko Carstens rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs), 2155d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 2156d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 2157b0c632dbSHeiko Carstens } 2158b0c632dbSHeiko Carstens 2159e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 2160e879892cSThomas Huth { 2161e879892cSThomas Huth /* 2162e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 2163e879892cSThomas Huth * copying in vcpu load/put. Lets update our copies before we save 2164e879892cSThomas Huth * it into the save area 2165e879892cSThomas Huth */ 2166e879892cSThomas Huth save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 2167e879892cSThomas Huth save_fp_regs(vcpu->arch.guest_fpregs.fprs); 2168e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 2169e879892cSThomas Huth 2170e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 2171e879892cSThomas Huth } 2172e879892cSThomas Huth 2173bc17de7cSEric Farman /* 2174bc17de7cSEric Farman * store additional status at address 2175bc17de7cSEric Farman */ 2176bc17de7cSEric Farman int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu, 2177bc17de7cSEric Farman unsigned long gpa) 2178bc17de7cSEric Farman { 2179bc17de7cSEric Farman /* Only bits 0-53 are used for address formation */ 2180bc17de7cSEric Farman if (!(gpa & ~0x3ff)) 2181bc17de7cSEric Farman return 0; 2182bc17de7cSEric Farman 2183bc17de7cSEric Farman return write_guest_abs(vcpu, gpa & ~0x3ff, 2184bc17de7cSEric Farman (void *)&vcpu->run->s.regs.vrs, 512); 2185bc17de7cSEric Farman } 2186bc17de7cSEric Farman 2187bc17de7cSEric Farman int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr) 2188bc17de7cSEric Farman { 2189bc17de7cSEric Farman if (!test_kvm_facility(vcpu->kvm, 129)) 2190bc17de7cSEric Farman return 0; 2191bc17de7cSEric Farman 2192bc17de7cSEric Farman /* 2193bc17de7cSEric Farman * The guest VXRS are in the host VXRs due to the lazy 2194bc17de7cSEric Farman * copying in vcpu load/put. Let's update our copies before we save 2195bc17de7cSEric Farman * it into the save area. 2196bc17de7cSEric Farman */ 2197bc17de7cSEric Farman save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); 2198bc17de7cSEric Farman 2199bc17de7cSEric Farman return kvm_s390_store_adtl_status_unloaded(vcpu, addr); 2200bc17de7cSEric Farman } 2201bc17de7cSEric Farman 22028ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 22038ad35755SDavid Hildenbrand { 22048ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 22058ad35755SDavid Hildenbrand kvm_make_request(KVM_REQ_DISABLE_IBS, vcpu); 22068ad35755SDavid Hildenbrand exit_sie_sync(vcpu); 22078ad35755SDavid Hildenbrand } 22088ad35755SDavid Hildenbrand 22098ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 22108ad35755SDavid Hildenbrand { 22118ad35755SDavid Hildenbrand unsigned int i; 22128ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 22138ad35755SDavid Hildenbrand 22148ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 22158ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 22168ad35755SDavid Hildenbrand } 22178ad35755SDavid Hildenbrand } 22188ad35755SDavid Hildenbrand 22198ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 22208ad35755SDavid Hildenbrand { 22218ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 22228ad35755SDavid Hildenbrand kvm_make_request(KVM_REQ_ENABLE_IBS, vcpu); 22238ad35755SDavid Hildenbrand exit_sie_sync(vcpu); 22248ad35755SDavid Hildenbrand } 22258ad35755SDavid Hildenbrand 22266852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 22276852d7b6SDavid Hildenbrand { 22288ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 22298ad35755SDavid Hildenbrand 22308ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 22318ad35755SDavid Hildenbrand return; 22328ad35755SDavid Hildenbrand 22336852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 22348ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 2235433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 22368ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 22378ad35755SDavid Hildenbrand 22388ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 22398ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 22408ad35755SDavid Hildenbrand started_vcpus++; 22418ad35755SDavid Hildenbrand } 22428ad35755SDavid Hildenbrand 22438ad35755SDavid Hildenbrand if (started_vcpus == 0) { 22448ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 22458ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 22468ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 22478ad35755SDavid Hildenbrand /* 22488ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 22498ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 22508ad35755SDavid Hildenbrand * oustanding ENABLE requests. 22518ad35755SDavid Hildenbrand */ 22528ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 22538ad35755SDavid Hildenbrand } 22548ad35755SDavid Hildenbrand 22556852d7b6SDavid Hildenbrand atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 22568ad35755SDavid Hildenbrand /* 22578ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 22588ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 22598ad35755SDavid Hildenbrand */ 2260d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 2261433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 22628ad35755SDavid Hildenbrand return; 22636852d7b6SDavid Hildenbrand } 22646852d7b6SDavid Hildenbrand 22656852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 22666852d7b6SDavid Hildenbrand { 22678ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 22688ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 22698ad35755SDavid Hildenbrand 22708ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 22718ad35755SDavid Hildenbrand return; 22728ad35755SDavid Hildenbrand 22736852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 22748ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 2275433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 22768ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 22778ad35755SDavid Hildenbrand 227832f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 22796cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 228032f5ff63SDavid Hildenbrand 22816cddd432SDavid Hildenbrand atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 22828ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 22838ad35755SDavid Hildenbrand 22848ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 22858ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 22868ad35755SDavid Hildenbrand started_vcpus++; 22878ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 22888ad35755SDavid Hildenbrand } 22898ad35755SDavid Hildenbrand } 22908ad35755SDavid Hildenbrand 22918ad35755SDavid Hildenbrand if (started_vcpus == 1) { 22928ad35755SDavid Hildenbrand /* 22938ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 22948ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 22958ad35755SDavid Hildenbrand */ 22968ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 22978ad35755SDavid Hildenbrand } 22988ad35755SDavid Hildenbrand 2299433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 23008ad35755SDavid Hildenbrand return; 23016852d7b6SDavid Hildenbrand } 23026852d7b6SDavid Hildenbrand 2303d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 2304d6712df9SCornelia Huck struct kvm_enable_cap *cap) 2305d6712df9SCornelia Huck { 2306d6712df9SCornelia Huck int r; 2307d6712df9SCornelia Huck 2308d6712df9SCornelia Huck if (cap->flags) 2309d6712df9SCornelia Huck return -EINVAL; 2310d6712df9SCornelia Huck 2311d6712df9SCornelia Huck switch (cap->cap) { 2312fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 2313fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 2314fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 2315fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 2316fa6b7fe9SCornelia Huck } 2317fa6b7fe9SCornelia Huck r = 0; 2318fa6b7fe9SCornelia Huck break; 2319d6712df9SCornelia Huck default: 2320d6712df9SCornelia Huck r = -EINVAL; 2321d6712df9SCornelia Huck break; 2322d6712df9SCornelia Huck } 2323d6712df9SCornelia Huck return r; 2324d6712df9SCornelia Huck } 2325d6712df9SCornelia Huck 232641408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 232741408c28SThomas Huth struct kvm_s390_mem_op *mop) 232841408c28SThomas Huth { 232941408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 233041408c28SThomas Huth void *tmpbuf = NULL; 233141408c28SThomas Huth int r, srcu_idx; 233241408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 233341408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 233441408c28SThomas Huth 233541408c28SThomas Huth if (mop->flags & ~supported_flags) 233641408c28SThomas Huth return -EINVAL; 233741408c28SThomas Huth 233841408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 233941408c28SThomas Huth return -E2BIG; 234041408c28SThomas Huth 234141408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 234241408c28SThomas Huth tmpbuf = vmalloc(mop->size); 234341408c28SThomas Huth if (!tmpbuf) 234441408c28SThomas Huth return -ENOMEM; 234541408c28SThomas Huth } 234641408c28SThomas Huth 234741408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 234841408c28SThomas Huth 234941408c28SThomas Huth switch (mop->op) { 235041408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 235141408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 235241408c28SThomas Huth r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false); 235341408c28SThomas Huth break; 235441408c28SThomas Huth } 235541408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 235641408c28SThomas Huth if (r == 0) { 235741408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 235841408c28SThomas Huth r = -EFAULT; 235941408c28SThomas Huth } 236041408c28SThomas Huth break; 236141408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 236241408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 236341408c28SThomas Huth r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true); 236441408c28SThomas Huth break; 236541408c28SThomas Huth } 236641408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 236741408c28SThomas Huth r = -EFAULT; 236841408c28SThomas Huth break; 236941408c28SThomas Huth } 237041408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 237141408c28SThomas Huth break; 237241408c28SThomas Huth default: 237341408c28SThomas Huth r = -EINVAL; 237441408c28SThomas Huth } 237541408c28SThomas Huth 237641408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 237741408c28SThomas Huth 237841408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 237941408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 238041408c28SThomas Huth 238141408c28SThomas Huth vfree(tmpbuf); 238241408c28SThomas Huth return r; 238341408c28SThomas Huth } 238441408c28SThomas Huth 2385b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 2386b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2387b0c632dbSHeiko Carstens { 2388b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 2389b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2390800c1065SThomas Huth int idx; 2391bc923cc9SAvi Kivity long r; 2392b0c632dbSHeiko Carstens 239393736624SAvi Kivity switch (ioctl) { 239493736624SAvi Kivity case KVM_S390_INTERRUPT: { 2395ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2396383d0b05SJens Freimann struct kvm_s390_irq s390irq; 2397ba5c1e9bSCarsten Otte 239893736624SAvi Kivity r = -EFAULT; 2399ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 240093736624SAvi Kivity break; 2401383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 2402383d0b05SJens Freimann return -EINVAL; 2403383d0b05SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 240493736624SAvi Kivity break; 2405ba5c1e9bSCarsten Otte } 2406b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 2407800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 2408bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 2409800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 2410bc923cc9SAvi Kivity break; 2411b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 2412b0c632dbSHeiko Carstens psw_t psw; 2413b0c632dbSHeiko Carstens 2414bc923cc9SAvi Kivity r = -EFAULT; 2415b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 2416bc923cc9SAvi Kivity break; 2417bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 2418bc923cc9SAvi Kivity break; 2419b0c632dbSHeiko Carstens } 2420b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 2421bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 2422bc923cc9SAvi Kivity break; 242314eebd91SCarsten Otte case KVM_SET_ONE_REG: 242414eebd91SCarsten Otte case KVM_GET_ONE_REG: { 242514eebd91SCarsten Otte struct kvm_one_reg reg; 242614eebd91SCarsten Otte r = -EFAULT; 242714eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 242814eebd91SCarsten Otte break; 242914eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 243014eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 243114eebd91SCarsten Otte else 243214eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 243314eebd91SCarsten Otte break; 243414eebd91SCarsten Otte } 243527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 243627e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 243727e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 243827e0393fSCarsten Otte 243927e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 244027e0393fSCarsten Otte r = -EFAULT; 244127e0393fSCarsten Otte break; 244227e0393fSCarsten Otte } 244327e0393fSCarsten Otte 244427e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 244527e0393fSCarsten Otte r = -EINVAL; 244627e0393fSCarsten Otte break; 244727e0393fSCarsten Otte } 244827e0393fSCarsten Otte 244927e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 245027e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 245127e0393fSCarsten Otte break; 245227e0393fSCarsten Otte } 245327e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 245427e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 245527e0393fSCarsten Otte 245627e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 245727e0393fSCarsten Otte r = -EFAULT; 245827e0393fSCarsten Otte break; 245927e0393fSCarsten Otte } 246027e0393fSCarsten Otte 246127e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 246227e0393fSCarsten Otte r = -EINVAL; 246327e0393fSCarsten Otte break; 246427e0393fSCarsten Otte } 246527e0393fSCarsten Otte 246627e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 246727e0393fSCarsten Otte ucasmap.length); 246827e0393fSCarsten Otte break; 246927e0393fSCarsten Otte } 247027e0393fSCarsten Otte #endif 2471ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 2472527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 2473ccc7910fSCarsten Otte break; 2474ccc7910fSCarsten Otte } 2475d6712df9SCornelia Huck case KVM_ENABLE_CAP: 2476d6712df9SCornelia Huck { 2477d6712df9SCornelia Huck struct kvm_enable_cap cap; 2478d6712df9SCornelia Huck r = -EFAULT; 2479d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 2480d6712df9SCornelia Huck break; 2481d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 2482d6712df9SCornelia Huck break; 2483d6712df9SCornelia Huck } 248441408c28SThomas Huth case KVM_S390_MEM_OP: { 248541408c28SThomas Huth struct kvm_s390_mem_op mem_op; 248641408c28SThomas Huth 248741408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 248841408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 248941408c28SThomas Huth else 249041408c28SThomas Huth r = -EFAULT; 249141408c28SThomas Huth break; 249241408c28SThomas Huth } 2493b0c632dbSHeiko Carstens default: 24943e6afcf1SCarsten Otte r = -ENOTTY; 2495b0c632dbSHeiko Carstens } 2496bc923cc9SAvi Kivity return r; 2497b0c632dbSHeiko Carstens } 2498b0c632dbSHeiko Carstens 24995b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 25005b1c1493SCarsten Otte { 25015b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 25025b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 25035b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 25045b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 25055b1c1493SCarsten Otte get_page(vmf->page); 25065b1c1493SCarsten Otte return 0; 25075b1c1493SCarsten Otte } 25085b1c1493SCarsten Otte #endif 25095b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 25105b1c1493SCarsten Otte } 25115b1c1493SCarsten Otte 25125587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 25135587027cSAneesh Kumar K.V unsigned long npages) 2514db3fe4ebSTakuya Yoshikawa { 2515db3fe4ebSTakuya Yoshikawa return 0; 2516db3fe4ebSTakuya Yoshikawa } 2517db3fe4ebSTakuya Yoshikawa 2518b0c632dbSHeiko Carstens /* Section: memory related */ 2519f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 2520f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 25217b6195a9STakuya Yoshikawa struct kvm_userspace_memory_region *mem, 25227b6195a9STakuya Yoshikawa enum kvm_mr_change change) 2523b0c632dbSHeiko Carstens { 2524dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 2525dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 2526dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 2527dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 2528b0c632dbSHeiko Carstens 2529598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 2530b0c632dbSHeiko Carstens return -EINVAL; 2531b0c632dbSHeiko Carstens 2532598841caSCarsten Otte if (mem->memory_size & 0xffffful) 2533b0c632dbSHeiko Carstens return -EINVAL; 2534b0c632dbSHeiko Carstens 2535f7784b8eSMarcelo Tosatti return 0; 2536f7784b8eSMarcelo Tosatti } 2537f7784b8eSMarcelo Tosatti 2538f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 2539f7784b8eSMarcelo Tosatti struct kvm_userspace_memory_region *mem, 25408482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 25418482644aSTakuya Yoshikawa enum kvm_mr_change change) 2542f7784b8eSMarcelo Tosatti { 2543f7850c92SCarsten Otte int rc; 2544f7784b8eSMarcelo Tosatti 25452cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 25462cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 25472cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 25482cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 25492cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 25502cef4debSChristian Borntraeger */ 25512cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 25522cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 25532cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 25542cef4debSChristian Borntraeger return; 2555598841caSCarsten Otte 2556598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 2557598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 2558598841caSCarsten Otte if (rc) 2559f7850c92SCarsten Otte printk(KERN_WARNING "kvm-s390: failed to commit memory region\n"); 2560598841caSCarsten Otte return; 2561b0c632dbSHeiko Carstens } 2562b0c632dbSHeiko Carstens 2563b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 2564b0c632dbSHeiko Carstens { 25659d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 2566b0c632dbSHeiko Carstens } 2567b0c632dbSHeiko Carstens 2568b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 2569b0c632dbSHeiko Carstens { 2570b0c632dbSHeiko Carstens kvm_exit(); 2571b0c632dbSHeiko Carstens } 2572b0c632dbSHeiko Carstens 2573b0c632dbSHeiko Carstens module_init(kvm_s390_init); 2574b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 2575566af940SCornelia Huck 2576566af940SCornelia Huck /* 2577566af940SCornelia Huck * Enable autoloading of the kvm module. 2578566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 2579566af940SCornelia Huck * since x86 takes a different approach. 2580566af940SCornelia Huck */ 2581566af940SCornelia Huck #include <linux/miscdevice.h> 2582566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 2583566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 2584