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> 14b0c632dbSHeiko Carstens */ 15b0c632dbSHeiko Carstens 16b0c632dbSHeiko Carstens #include <linux/compiler.h> 17b0c632dbSHeiko Carstens #include <linux/err.h> 18b0c632dbSHeiko Carstens #include <linux/fs.h> 19ca872302SChristian Borntraeger #include <linux/hrtimer.h> 20b0c632dbSHeiko Carstens #include <linux/init.h> 21b0c632dbSHeiko Carstens #include <linux/kvm.h> 22b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 23b0c632dbSHeiko Carstens #include <linux/module.h> 24b0c632dbSHeiko Carstens #include <linux/slab.h> 25ba5c1e9bSCarsten Otte #include <linux/timer.h> 26cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 27b0c632dbSHeiko Carstens #include <asm/lowcore.h> 28b0c632dbSHeiko Carstens #include <asm/pgtable.h> 29f5daba1dSHeiko Carstens #include <asm/nmi.h> 30a0616cdeSDavid Howells #include <asm/switch_to.h> 3178c4b59fSMichael Mueller #include <asm/facility.h> 321526bf9cSChristian Borntraeger #include <asm/sclp.h> 338f2abe6aSChristian Borntraeger #include "kvm-s390.h" 34b0c632dbSHeiko Carstens #include "gaccess.h" 35b0c632dbSHeiko Carstens 365786fffaSCornelia Huck #define CREATE_TRACE_POINTS 375786fffaSCornelia Huck #include "trace.h" 38ade38c31SCornelia Huck #include "trace-s390.h" 395786fffaSCornelia Huck 40b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 41b0c632dbSHeiko Carstens 42b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 43b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 440eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 458f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 468f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 478f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 488f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 49ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 50ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 51ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 52f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 53ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 54ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 557697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 56ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 57ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 58ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 59ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 60ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 61ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 62ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 6369d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 64453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 65453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 66453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 67453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 68453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 69453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 70453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 71453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 72453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 73bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 745288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 75bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 767697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 775288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 785288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 795288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 805288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 815288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 82388186bcSChristian Borntraeger { "diagnose_10", VCPU_STAT(diagnose_10) }, 83e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 8441628d33SKonstantin Weitz { "diagnose_9c", VCPU_STAT(diagnose_9c) }, 85b0c632dbSHeiko Carstens { NULL } 86b0c632dbSHeiko Carstens }; 87b0c632dbSHeiko Carstens 8878c4b59fSMichael Mueller unsigned long *vfacilities; 892c70fe44SChristian Borntraeger static struct gmap_notifier gmap_notifier; 90b0c632dbSHeiko Carstens 9178c4b59fSMichael Mueller /* test availability of vfacility */ 9278c4b59fSMichael Mueller static inline int test_vfacility(unsigned long nr) 9378c4b59fSMichael Mueller { 9478c4b59fSMichael Mueller return __test_facility(nr, (void *) vfacilities); 9578c4b59fSMichael Mueller } 9678c4b59fSMichael Mueller 97b0c632dbSHeiko Carstens /* Section: not file related */ 9810474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage) 99b0c632dbSHeiko Carstens { 100b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 10110474ae8SAlexander Graf return 0; 102b0c632dbSHeiko Carstens } 103b0c632dbSHeiko Carstens 104b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage) 105b0c632dbSHeiko Carstens { 106b0c632dbSHeiko Carstens } 107b0c632dbSHeiko Carstens 1082c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address); 1092c70fe44SChristian Borntraeger 110b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 111b0c632dbSHeiko Carstens { 1122c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 1132c70fe44SChristian Borntraeger gmap_register_ipte_notifier(&gmap_notifier); 114b0c632dbSHeiko Carstens return 0; 115b0c632dbSHeiko Carstens } 116b0c632dbSHeiko Carstens 117b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 118b0c632dbSHeiko Carstens { 1192c70fe44SChristian Borntraeger gmap_unregister_ipte_notifier(&gmap_notifier); 120b0c632dbSHeiko Carstens } 121b0c632dbSHeiko Carstens 122b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn) 123b0c632dbSHeiko Carstens { 124b0c632dbSHeiko Carstens } 125b0c632dbSHeiko Carstens 126b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 127b0c632dbSHeiko Carstens { 128b0c632dbSHeiko Carstens return 0; 129b0c632dbSHeiko Carstens } 130b0c632dbSHeiko Carstens 131b0c632dbSHeiko Carstens void kvm_arch_exit(void) 132b0c632dbSHeiko Carstens { 133b0c632dbSHeiko Carstens } 134b0c632dbSHeiko Carstens 135b0c632dbSHeiko Carstens /* Section: device related */ 136b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 137b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 138b0c632dbSHeiko Carstens { 139b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 140b0c632dbSHeiko Carstens return s390_enable_sie(); 141b0c632dbSHeiko Carstens return -EINVAL; 142b0c632dbSHeiko Carstens } 143b0c632dbSHeiko Carstens 144b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext) 145b0c632dbSHeiko Carstens { 146d7b0b5ebSCarsten Otte int r; 147d7b0b5ebSCarsten Otte 1482bd0ac4eSCarsten Otte switch (ext) { 149d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 150b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 15152e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 1521efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1531efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 1541efd0f59SCarsten Otte #endif 15560b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 15614eebd91SCarsten Otte case KVM_CAP_ONE_REG: 157d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 158fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 15910ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 160d7b0b5ebSCarsten Otte r = 1; 161d7b0b5ebSCarsten Otte break; 162e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 163e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 164e726b1bdSChristian Borntraeger r = KVM_MAX_VCPUS; 165e726b1bdSChristian Borntraeger break; 166e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 167e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 168e1e2e605SNick Wang break; 1691526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 170abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 1711526bf9cSChristian Borntraeger break; 1722bd0ac4eSCarsten Otte default: 173d7b0b5ebSCarsten Otte r = 0; 174b0c632dbSHeiko Carstens } 175d7b0b5ebSCarsten Otte return r; 1762bd0ac4eSCarsten Otte } 177b0c632dbSHeiko Carstens 178b0c632dbSHeiko Carstens /* Section: vm related */ 179b0c632dbSHeiko Carstens /* 180b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 181b0c632dbSHeiko Carstens */ 182b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 183b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 184b0c632dbSHeiko Carstens { 185b0c632dbSHeiko Carstens return 0; 186b0c632dbSHeiko Carstens } 187b0c632dbSHeiko Carstens 188b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 189b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 190b0c632dbSHeiko Carstens { 191b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 192b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 193b0c632dbSHeiko Carstens int r; 194b0c632dbSHeiko Carstens 195b0c632dbSHeiko Carstens switch (ioctl) { 196ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 197ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 198ba5c1e9bSCarsten Otte 199ba5c1e9bSCarsten Otte r = -EFAULT; 200ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 201ba5c1e9bSCarsten Otte break; 202ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 203ba5c1e9bSCarsten Otte break; 204ba5c1e9bSCarsten Otte } 205b0c632dbSHeiko Carstens default: 206367e1319SAvi Kivity r = -ENOTTY; 207b0c632dbSHeiko Carstens } 208b0c632dbSHeiko Carstens 209b0c632dbSHeiko Carstens return r; 210b0c632dbSHeiko Carstens } 211b0c632dbSHeiko Carstens 212e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 213b0c632dbSHeiko Carstens { 214b0c632dbSHeiko Carstens int rc; 215b0c632dbSHeiko Carstens char debug_name[16]; 216b0c632dbSHeiko Carstens 217e08b9637SCarsten Otte rc = -EINVAL; 218e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 219e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 220e08b9637SCarsten Otte goto out_err; 221e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 222e08b9637SCarsten Otte goto out_err; 223e08b9637SCarsten Otte #else 224e08b9637SCarsten Otte if (type) 225e08b9637SCarsten Otte goto out_err; 226e08b9637SCarsten Otte #endif 227e08b9637SCarsten Otte 228b0c632dbSHeiko Carstens rc = s390_enable_sie(); 229b0c632dbSHeiko Carstens if (rc) 230d89f5effSJan Kiszka goto out_err; 231b0c632dbSHeiko Carstens 232b290411aSCarsten Otte rc = -ENOMEM; 233b290411aSCarsten Otte 234b0c632dbSHeiko Carstens kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL); 235b0c632dbSHeiko Carstens if (!kvm->arch.sca) 236d89f5effSJan Kiszka goto out_err; 237b0c632dbSHeiko Carstens 238b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 239b0c632dbSHeiko Carstens 240b0c632dbSHeiko Carstens kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long)); 241b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 242b0c632dbSHeiko Carstens goto out_nodbf; 243b0c632dbSHeiko Carstens 244ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 245ba5c1e9bSCarsten Otte INIT_LIST_HEAD(&kvm->arch.float_int.list); 246ba5c1e9bSCarsten Otte 247b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 248b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "%s", "vm created"); 249b0c632dbSHeiko Carstens 250e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 251e08b9637SCarsten Otte kvm->arch.gmap = NULL; 252e08b9637SCarsten Otte } else { 253598841caSCarsten Otte kvm->arch.gmap = gmap_alloc(current->mm); 254598841caSCarsten Otte if (!kvm->arch.gmap) 255598841caSCarsten Otte goto out_nogmap; 2562c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 257e08b9637SCarsten Otte } 258fa6b7fe9SCornelia Huck 259fa6b7fe9SCornelia Huck kvm->arch.css_support = 0; 260fa6b7fe9SCornelia Huck 261d89f5effSJan Kiszka return 0; 262598841caSCarsten Otte out_nogmap: 263598841caSCarsten Otte debug_unregister(kvm->arch.dbf); 264b0c632dbSHeiko Carstens out_nodbf: 265b0c632dbSHeiko Carstens free_page((unsigned long)(kvm->arch.sca)); 266d89f5effSJan Kiszka out_err: 267d89f5effSJan Kiszka return rc; 268b0c632dbSHeiko Carstens } 269b0c632dbSHeiko Carstens 270d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 271d329c035SChristian Borntraeger { 272d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 273ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 27458f9460bSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 27558f9460bSCarsten Otte clear_bit(63 - vcpu->vcpu_id, 27658f9460bSCarsten Otte (unsigned long *) &vcpu->kvm->arch.sca->mcn); 277abf4a71eSCarsten Otte if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda == 278abf4a71eSCarsten Otte (__u64) vcpu->arch.sie_block) 279abf4a71eSCarsten Otte vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0; 28058f9460bSCarsten Otte } 281abf4a71eSCarsten Otte smp_mb(); 28227e0393fSCarsten Otte 28327e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 28427e0393fSCarsten Otte gmap_free(vcpu->arch.gmap); 28527e0393fSCarsten Otte 286d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2876692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 288b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 289d329c035SChristian Borntraeger } 290d329c035SChristian Borntraeger 291d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 292d329c035SChristian Borntraeger { 293d329c035SChristian Borntraeger unsigned int i; 294988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 295d329c035SChristian Borntraeger 296988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 297988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 298988a2caeSGleb Natapov 299988a2caeSGleb Natapov mutex_lock(&kvm->lock); 300988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 301d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 302988a2caeSGleb Natapov 303988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 304988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 305d329c035SChristian Borntraeger } 306d329c035SChristian Borntraeger 307ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm) 308ad8ba2cdSSheng Yang { 309ad8ba2cdSSheng Yang } 310ad8ba2cdSSheng Yang 311b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 312b0c632dbSHeiko Carstens { 313d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 314b0c632dbSHeiko Carstens free_page((unsigned long)(kvm->arch.sca)); 315d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 31627e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 317598841caSCarsten Otte gmap_free(kvm->arch.gmap); 318b0c632dbSHeiko Carstens } 319b0c632dbSHeiko Carstens 320b0c632dbSHeiko Carstens /* Section: vcpu related */ 321b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 322b0c632dbSHeiko Carstens { 32327e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) { 32427e0393fSCarsten Otte vcpu->arch.gmap = gmap_alloc(current->mm); 32527e0393fSCarsten Otte if (!vcpu->arch.gmap) 32627e0393fSCarsten Otte return -ENOMEM; 3272c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 32827e0393fSCarsten Otte return 0; 32927e0393fSCarsten Otte } 33027e0393fSCarsten Otte 331598841caSCarsten Otte vcpu->arch.gmap = vcpu->kvm->arch.gmap; 33259674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 33359674c1aSChristian Borntraeger KVM_SYNC_GPRS | 3349eed0735SChristian Borntraeger KVM_SYNC_ACRS | 3359eed0735SChristian Borntraeger KVM_SYNC_CRS; 336b0c632dbSHeiko Carstens return 0; 337b0c632dbSHeiko Carstens } 338b0c632dbSHeiko Carstens 339b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) 340b0c632dbSHeiko Carstens { 3416692cef3SChristian Borntraeger /* Nothing todo */ 342b0c632dbSHeiko Carstens } 343b0c632dbSHeiko Carstens 344b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 345b0c632dbSHeiko Carstens { 3464725c860SMartin Schwidefsky save_fp_ctl(&vcpu->arch.host_fpregs.fpc); 3474725c860SMartin Schwidefsky save_fp_regs(vcpu->arch.host_fpregs.fprs); 348b0c632dbSHeiko Carstens save_access_regs(vcpu->arch.host_acrs); 3494725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 3504725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.guest_fpregs.fprs); 35159674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 352480e5926SChristian Borntraeger gmap_enable(vcpu->arch.gmap); 3539e6dabefSCornelia Huck atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 354b0c632dbSHeiko Carstens } 355b0c632dbSHeiko Carstens 356b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 357b0c632dbSHeiko Carstens { 3589e6dabefSCornelia Huck atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 359480e5926SChristian Borntraeger gmap_disable(vcpu->arch.gmap); 3604725c860SMartin Schwidefsky save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 3614725c860SMartin Schwidefsky save_fp_regs(vcpu->arch.guest_fpregs.fprs); 36259674c1aSChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 3634725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.host_fpregs.fpc); 3644725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.host_fpregs.fprs); 365b0c632dbSHeiko Carstens restore_access_regs(vcpu->arch.host_acrs); 366b0c632dbSHeiko Carstens } 367b0c632dbSHeiko Carstens 368b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 369b0c632dbSHeiko Carstens { 370b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 371b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 372b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 3738d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 374b0c632dbSHeiko Carstens vcpu->arch.sie_block->cputm = 0UL; 375b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 376b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 377b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 378b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 379b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 380b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fpc = 0; 381b0c632dbSHeiko Carstens asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc)); 382b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 38361bde82cSChristian Borntraeger atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 384b0c632dbSHeiko Carstens } 385b0c632dbSHeiko Carstens 38642897d86SMarcelo Tosatti int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 38742897d86SMarcelo Tosatti { 38842897d86SMarcelo Tosatti return 0; 38942897d86SMarcelo Tosatti } 39042897d86SMarcelo Tosatti 391b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 392b0c632dbSHeiko Carstens { 3939e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 3949e6dabefSCornelia Huck CPUSTAT_SM | 39569d0d3a3SChristian Borntraeger CPUSTAT_STOPPED | 39669d0d3a3SChristian Borntraeger CPUSTAT_GED); 397fc34531dSChristian Borntraeger vcpu->arch.sie_block->ecb = 6; 398*7feb6bb8SMichael Mueller if (test_vfacility(50) && test_vfacility(73)) 399*7feb6bb8SMichael Mueller vcpu->arch.sie_block->ecb |= 0x10; 400*7feb6bb8SMichael Mueller 40169d0d3a3SChristian Borntraeger vcpu->arch.sie_block->ecb2 = 8; 402b0c632dbSHeiko Carstens vcpu->arch.sie_block->eca = 0xC1002001U; 40378c4b59fSMichael Mueller vcpu->arch.sie_block->fac = (int) (long) vfacilities; 404ca872302SChristian Borntraeger hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); 405ca872302SChristian Borntraeger tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet, 406ba5c1e9bSCarsten Otte (unsigned long) vcpu); 407ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 408453423dcSChristian Borntraeger get_cpu_id(&vcpu->arch.cpu_id); 40992e6ecf3SChristian Borntraeger vcpu->arch.cpu_id.version = 0xff; 410b0c632dbSHeiko Carstens return 0; 411b0c632dbSHeiko Carstens } 412b0c632dbSHeiko Carstens 413b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 414b0c632dbSHeiko Carstens unsigned int id) 415b0c632dbSHeiko Carstens { 4164d47555aSCarsten Otte struct kvm_vcpu *vcpu; 417*7feb6bb8SMichael Mueller struct sie_page *sie_page; 4184d47555aSCarsten Otte int rc = -EINVAL; 419b0c632dbSHeiko Carstens 4204d47555aSCarsten Otte if (id >= KVM_MAX_VCPUS) 4214d47555aSCarsten Otte goto out; 4224d47555aSCarsten Otte 4234d47555aSCarsten Otte rc = -ENOMEM; 4244d47555aSCarsten Otte 425b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 426b0c632dbSHeiko Carstens if (!vcpu) 4274d47555aSCarsten Otte goto out; 428b0c632dbSHeiko Carstens 429*7feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 430*7feb6bb8SMichael Mueller if (!sie_page) 431b0c632dbSHeiko Carstens goto out_free_cpu; 432b0c632dbSHeiko Carstens 433*7feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 434*7feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 435*7feb6bb8SMichael Mueller 436b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 43758f9460bSCarsten Otte if (!kvm_is_ucontrol(kvm)) { 43858f9460bSCarsten Otte if (!kvm->arch.sca) { 43958f9460bSCarsten Otte WARN_ON_ONCE(1); 44058f9460bSCarsten Otte goto out_free_cpu; 44158f9460bSCarsten Otte } 442abf4a71eSCarsten Otte if (!kvm->arch.sca->cpu[id].sda) 44358f9460bSCarsten Otte kvm->arch.sca->cpu[id].sda = 44458f9460bSCarsten Otte (__u64) vcpu->arch.sie_block; 44558f9460bSCarsten Otte vcpu->arch.sie_block->scaoh = 44658f9460bSCarsten Otte (__u32)(((__u64)kvm->arch.sca) >> 32); 447b0c632dbSHeiko Carstens vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca; 448fc34531dSChristian Borntraeger set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn); 44958f9460bSCarsten Otte } 450b0c632dbSHeiko Carstens 451ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 452ba5c1e9bSCarsten Otte INIT_LIST_HEAD(&vcpu->arch.local_int.list); 453ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 454b037a4f3SChristian Borntraeger spin_lock(&kvm->arch.float_int.lock); 455ba5c1e9bSCarsten Otte kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int; 456d0321a24SChristian Borntraeger vcpu->arch.local_int.wq = &vcpu->wq; 4575288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 458b037a4f3SChristian Borntraeger spin_unlock(&kvm->arch.float_int.lock); 459ba5c1e9bSCarsten Otte 460b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 461b0c632dbSHeiko Carstens if (rc) 4627b06bf2fSWei Yongjun goto out_free_sie_block; 463b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu, 464b0c632dbSHeiko Carstens vcpu->arch.sie_block); 465ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 466b0c632dbSHeiko Carstens 467b0c632dbSHeiko Carstens return vcpu; 4687b06bf2fSWei Yongjun out_free_sie_block: 4697b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 470b0c632dbSHeiko Carstens out_free_cpu: 471b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 4724d47555aSCarsten Otte out: 473b0c632dbSHeiko Carstens return ERR_PTR(rc); 474b0c632dbSHeiko Carstens } 475b0c632dbSHeiko Carstens 476b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 477b0c632dbSHeiko Carstens { 478b0c632dbSHeiko Carstens /* kvm common code refers to this, but never calls it */ 479b0c632dbSHeiko Carstens BUG(); 480b0c632dbSHeiko Carstens return 0; 481b0c632dbSHeiko Carstens } 482b0c632dbSHeiko Carstens 48349b99e1eSChristian Borntraeger void s390_vcpu_block(struct kvm_vcpu *vcpu) 48449b99e1eSChristian Borntraeger { 48549b99e1eSChristian Borntraeger atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 48649b99e1eSChristian Borntraeger } 48749b99e1eSChristian Borntraeger 48849b99e1eSChristian Borntraeger void s390_vcpu_unblock(struct kvm_vcpu *vcpu) 48949b99e1eSChristian Borntraeger { 49049b99e1eSChristian Borntraeger atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 49149b99e1eSChristian Borntraeger } 49249b99e1eSChristian Borntraeger 49349b99e1eSChristian Borntraeger /* 49449b99e1eSChristian Borntraeger * Kick a guest cpu out of SIE and wait until SIE is not running. 49549b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 49649b99e1eSChristian Borntraeger * return immediately. */ 49749b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 49849b99e1eSChristian Borntraeger { 49949b99e1eSChristian Borntraeger atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); 50049b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 50149b99e1eSChristian Borntraeger cpu_relax(); 50249b99e1eSChristian Borntraeger } 50349b99e1eSChristian Borntraeger 50449b99e1eSChristian Borntraeger /* Kick a guest cpu out of SIE and prevent SIE-reentry */ 50549b99e1eSChristian Borntraeger void exit_sie_sync(struct kvm_vcpu *vcpu) 50649b99e1eSChristian Borntraeger { 50749b99e1eSChristian Borntraeger s390_vcpu_block(vcpu); 50849b99e1eSChristian Borntraeger exit_sie(vcpu); 50949b99e1eSChristian Borntraeger } 51049b99e1eSChristian Borntraeger 5112c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address) 5122c70fe44SChristian Borntraeger { 5132c70fe44SChristian Borntraeger int i; 5142c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 5152c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 5162c70fe44SChristian Borntraeger 5172c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 5182c70fe44SChristian Borntraeger /* match against both prefix pages */ 5192c70fe44SChristian Borntraeger if (vcpu->arch.sie_block->prefix == (address & ~0x1000UL)) { 5202c70fe44SChristian Borntraeger VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address); 5212c70fe44SChristian Borntraeger kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 5222c70fe44SChristian Borntraeger exit_sie_sync(vcpu); 5232c70fe44SChristian Borntraeger } 5242c70fe44SChristian Borntraeger } 5252c70fe44SChristian Borntraeger } 5262c70fe44SChristian Borntraeger 527b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 528b6d33834SChristoffer Dall { 529b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 530b6d33834SChristoffer Dall BUG(); 531b6d33834SChristoffer Dall return 0; 532b6d33834SChristoffer Dall } 533b6d33834SChristoffer Dall 53414eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 53514eebd91SCarsten Otte struct kvm_one_reg *reg) 53614eebd91SCarsten Otte { 53714eebd91SCarsten Otte int r = -EINVAL; 53814eebd91SCarsten Otte 53914eebd91SCarsten Otte switch (reg->id) { 54029b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 54129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 54229b7c71bSCarsten Otte (u32 __user *)reg->addr); 54329b7c71bSCarsten Otte break; 54429b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 54529b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 54629b7c71bSCarsten Otte (u64 __user *)reg->addr); 54729b7c71bSCarsten Otte break; 54846a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 54946a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->cputm, 55046a6dd1cSJason J. herne (u64 __user *)reg->addr); 55146a6dd1cSJason J. herne break; 55246a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 55346a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 55446a6dd1cSJason J. herne (u64 __user *)reg->addr); 55546a6dd1cSJason J. herne break; 55614eebd91SCarsten Otte default: 55714eebd91SCarsten Otte break; 55814eebd91SCarsten Otte } 55914eebd91SCarsten Otte 56014eebd91SCarsten Otte return r; 56114eebd91SCarsten Otte } 56214eebd91SCarsten Otte 56314eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 56414eebd91SCarsten Otte struct kvm_one_reg *reg) 56514eebd91SCarsten Otte { 56614eebd91SCarsten Otte int r = -EINVAL; 56714eebd91SCarsten Otte 56814eebd91SCarsten Otte switch (reg->id) { 56929b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 57029b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 57129b7c71bSCarsten Otte (u32 __user *)reg->addr); 57229b7c71bSCarsten Otte break; 57329b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 57429b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 57529b7c71bSCarsten Otte (u64 __user *)reg->addr); 57629b7c71bSCarsten Otte break; 57746a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 57846a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->cputm, 57946a6dd1cSJason J. herne (u64 __user *)reg->addr); 58046a6dd1cSJason J. herne break; 58146a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 58246a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 58346a6dd1cSJason J. herne (u64 __user *)reg->addr); 58446a6dd1cSJason J. herne break; 58514eebd91SCarsten Otte default: 58614eebd91SCarsten Otte break; 58714eebd91SCarsten Otte } 58814eebd91SCarsten Otte 58914eebd91SCarsten Otte return r; 59014eebd91SCarsten Otte } 591b6d33834SChristoffer Dall 592b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 593b0c632dbSHeiko Carstens { 594b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 595b0c632dbSHeiko Carstens return 0; 596b0c632dbSHeiko Carstens } 597b0c632dbSHeiko Carstens 598b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 599b0c632dbSHeiko Carstens { 6005a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 601b0c632dbSHeiko Carstens return 0; 602b0c632dbSHeiko Carstens } 603b0c632dbSHeiko Carstens 604b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 605b0c632dbSHeiko Carstens { 6065a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 607b0c632dbSHeiko Carstens return 0; 608b0c632dbSHeiko Carstens } 609b0c632dbSHeiko Carstens 610b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 611b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 612b0c632dbSHeiko Carstens { 61359674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 614b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 61559674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 616b0c632dbSHeiko Carstens return 0; 617b0c632dbSHeiko Carstens } 618b0c632dbSHeiko Carstens 619b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 620b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 621b0c632dbSHeiko Carstens { 62259674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 623b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 624b0c632dbSHeiko Carstens return 0; 625b0c632dbSHeiko Carstens } 626b0c632dbSHeiko Carstens 627b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 628b0c632dbSHeiko Carstens { 6294725c860SMartin Schwidefsky if (test_fp_ctl(fpu->fpc)) 6304725c860SMartin Schwidefsky return -EINVAL; 631b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 6324725c860SMartin Schwidefsky vcpu->arch.guest_fpregs.fpc = fpu->fpc; 6334725c860SMartin Schwidefsky restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 6344725c860SMartin Schwidefsky restore_fp_regs(vcpu->arch.guest_fpregs.fprs); 635b0c632dbSHeiko Carstens return 0; 636b0c632dbSHeiko Carstens } 637b0c632dbSHeiko Carstens 638b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 639b0c632dbSHeiko Carstens { 640b0c632dbSHeiko Carstens memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs)); 641b0c632dbSHeiko Carstens fpu->fpc = vcpu->arch.guest_fpregs.fpc; 642b0c632dbSHeiko Carstens return 0; 643b0c632dbSHeiko Carstens } 644b0c632dbSHeiko Carstens 645b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 646b0c632dbSHeiko Carstens { 647b0c632dbSHeiko Carstens int rc = 0; 648b0c632dbSHeiko Carstens 6499e6dabefSCornelia Huck if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED)) 650b0c632dbSHeiko Carstens rc = -EBUSY; 651d7b0b5ebSCarsten Otte else { 652d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 653d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 654d7b0b5ebSCarsten Otte } 655b0c632dbSHeiko Carstens return rc; 656b0c632dbSHeiko Carstens } 657b0c632dbSHeiko Carstens 658b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 659b0c632dbSHeiko Carstens struct kvm_translation *tr) 660b0c632dbSHeiko Carstens { 661b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 662b0c632dbSHeiko Carstens } 663b0c632dbSHeiko Carstens 664d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 665d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 666b0c632dbSHeiko Carstens { 667b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 668b0c632dbSHeiko Carstens } 669b0c632dbSHeiko Carstens 67062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 67162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 67262d9f0dbSMarcelo Tosatti { 67362d9f0dbSMarcelo Tosatti return -EINVAL; /* not implemented yet */ 67462d9f0dbSMarcelo Tosatti } 67562d9f0dbSMarcelo Tosatti 67662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 67762d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 67862d9f0dbSMarcelo Tosatti { 67962d9f0dbSMarcelo Tosatti return -EINVAL; /* not implemented yet */ 68062d9f0dbSMarcelo Tosatti } 68162d9f0dbSMarcelo Tosatti 6822c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 6832c70fe44SChristian Borntraeger { 6842c70fe44SChristian Borntraeger /* 6852c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 6862c70fe44SChristian Borntraeger * guest prefix page. gmap_ipte_notify will wait on the ptl lock. 6872c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 6882c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 6892c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 6902c70fe44SChristian Borntraeger */ 6912c70fe44SChristian Borntraeger while (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 6922c70fe44SChristian Borntraeger int rc; 6932c70fe44SChristian Borntraeger rc = gmap_ipte_notify(vcpu->arch.gmap, 6942c70fe44SChristian Borntraeger vcpu->arch.sie_block->prefix, 6952c70fe44SChristian Borntraeger PAGE_SIZE * 2); 6962c70fe44SChristian Borntraeger if (rc) 6972c70fe44SChristian Borntraeger return rc; 6982c70fe44SChristian Borntraeger s390_vcpu_unblock(vcpu); 6992c70fe44SChristian Borntraeger } 7002c70fe44SChristian Borntraeger return 0; 7012c70fe44SChristian Borntraeger } 7022c70fe44SChristian Borntraeger 7033fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 704b0c632dbSHeiko Carstens { 7053fb4c40fSThomas Huth int rc, cpuflags; 706e168bf8dSCarsten Otte 7075a32c1afSChristian Borntraeger memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16); 708b0c632dbSHeiko Carstens 709b0c632dbSHeiko Carstens if (need_resched()) 710b0c632dbSHeiko Carstens schedule(); 711b0c632dbSHeiko Carstens 71271cde587SChristian Borntraeger if (test_thread_flag(TIF_MCCK_PENDING)) 71371cde587SChristian Borntraeger s390_handle_mcck(); 71471cde587SChristian Borntraeger 715d6b6d166SCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) 7160ff31867SCarsten Otte kvm_s390_deliver_pending_interrupts(vcpu); 7170ff31867SCarsten Otte 7182c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 7192c70fe44SChristian Borntraeger if (rc) 7202c70fe44SChristian Borntraeger return rc; 7212c70fe44SChristian Borntraeger 722b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 7233fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 7243fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 7253fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 7262b29a9fdSDominik Dingel 7273fb4c40fSThomas Huth return 0; 7283fb4c40fSThomas Huth } 7293fb4c40fSThomas Huth 7303fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 7313fb4c40fSThomas Huth { 7323fb4c40fSThomas Huth int rc; 7332b29a9fdSDominik Dingel 7342b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 7352b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 7362b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 7372b29a9fdSDominik Dingel 7383fb4c40fSThomas Huth if (exit_reason >= 0) { 7397c470539SMartin Schwidefsky rc = 0; 740210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 741210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 742210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 743210b1607SThomas Huth current->thread.gmap_addr; 744210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 745210b1607SThomas Huth rc = -EREMOTE; 7461f0d0f09SCarsten Otte } 747b0c632dbSHeiko Carstens 7485a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16); 7493fb4c40fSThomas Huth 750a76ccff6SThomas Huth if (rc == 0) { 751a76ccff6SThomas Huth if (kvm_is_ucontrol(vcpu->kvm)) 752a76ccff6SThomas Huth rc = -EOPNOTSUPP; 753a76ccff6SThomas Huth else 754a76ccff6SThomas Huth rc = kvm_handle_sie_intercept(vcpu); 755a76ccff6SThomas Huth } 756a76ccff6SThomas Huth 7573fb4c40fSThomas Huth return rc; 7583fb4c40fSThomas Huth } 7593fb4c40fSThomas Huth 7603fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 7613fb4c40fSThomas Huth { 7623fb4c40fSThomas Huth int rc, exit_reason; 7633fb4c40fSThomas Huth 764800c1065SThomas Huth /* 765800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 766800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 767800c1065SThomas Huth */ 768800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 769800c1065SThomas Huth 770a76ccff6SThomas Huth do { 7713fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 7723fb4c40fSThomas Huth if (rc) 773a76ccff6SThomas Huth break; 7743fb4c40fSThomas Huth 775800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 7763fb4c40fSThomas Huth /* 777a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 778a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 7793fb4c40fSThomas Huth */ 7803fb4c40fSThomas Huth preempt_disable(); 7813fb4c40fSThomas Huth kvm_guest_enter(); 7823fb4c40fSThomas Huth preempt_enable(); 783a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 784a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 7853fb4c40fSThomas Huth kvm_guest_exit(); 786800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 7873fb4c40fSThomas Huth 7883fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 789a76ccff6SThomas Huth } while (!signal_pending(current) && !rc); 7903fb4c40fSThomas Huth 791800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 792e168bf8dSCarsten Otte return rc; 793b0c632dbSHeiko Carstens } 794b0c632dbSHeiko Carstens 795b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 796b0c632dbSHeiko Carstens { 7978f2abe6aSChristian Borntraeger int rc; 798b0c632dbSHeiko Carstens sigset_t sigsaved; 799b0c632dbSHeiko Carstens 800b0c632dbSHeiko Carstens if (vcpu->sigset_active) 801b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 802b0c632dbSHeiko Carstens 8039e6dabefSCornelia Huck atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 804b0c632dbSHeiko Carstens 805ba5c1e9bSCarsten Otte BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL); 806ba5c1e9bSCarsten Otte 8078f2abe6aSChristian Borntraeger switch (kvm_run->exit_reason) { 8088f2abe6aSChristian Borntraeger case KVM_EXIT_S390_SIEIC: 8098f2abe6aSChristian Borntraeger case KVM_EXIT_UNKNOWN: 8109ace903dSChristian Ehrhardt case KVM_EXIT_INTR: 8118f2abe6aSChristian Borntraeger case KVM_EXIT_S390_RESET: 812e168bf8dSCarsten Otte case KVM_EXIT_S390_UCONTROL: 813fa6b7fe9SCornelia Huck case KVM_EXIT_S390_TSCH: 8148f2abe6aSChristian Borntraeger break; 8158f2abe6aSChristian Borntraeger default: 8168f2abe6aSChristian Borntraeger BUG(); 8178f2abe6aSChristian Borntraeger } 8188f2abe6aSChristian Borntraeger 819d7b0b5ebSCarsten Otte vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 820d7b0b5ebSCarsten Otte vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 82160b413c9SChristian Borntraeger if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) { 82260b413c9SChristian Borntraeger kvm_run->kvm_dirty_regs &= ~KVM_SYNC_PREFIX; 82360b413c9SChristian Borntraeger kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 82460b413c9SChristian Borntraeger } 8259eed0735SChristian Borntraeger if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 8269eed0735SChristian Borntraeger kvm_run->kvm_dirty_regs &= ~KVM_SYNC_CRS; 8279eed0735SChristian Borntraeger memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 8289eed0735SChristian Borntraeger kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 8299eed0735SChristian Borntraeger } 830d7b0b5ebSCarsten Otte 831dab4079dSHeiko Carstens might_fault(); 832e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 8339ace903dSChristian Ehrhardt 834b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 835b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 8368f2abe6aSChristian Borntraeger rc = -EINTR; 837b1d16c49SChristian Ehrhardt } 8388f2abe6aSChristian Borntraeger 839b8e660b8SHeiko Carstens if (rc == -EOPNOTSUPP) { 8408f2abe6aSChristian Borntraeger /* intercept cannot be handled in-kernel, prepare kvm-run */ 8418f2abe6aSChristian Borntraeger kvm_run->exit_reason = KVM_EXIT_S390_SIEIC; 8428f2abe6aSChristian Borntraeger kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 8438f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 8448f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 8458f2abe6aSChristian Borntraeger rc = 0; 8468f2abe6aSChristian Borntraeger } 8478f2abe6aSChristian Borntraeger 8488f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 8498f2abe6aSChristian Borntraeger /* intercept was handled, but userspace support is needed 8508f2abe6aSChristian Borntraeger * kvm_run has been prepared by the handler */ 8518f2abe6aSChristian Borntraeger rc = 0; 8528f2abe6aSChristian Borntraeger } 8538f2abe6aSChristian Borntraeger 854d7b0b5ebSCarsten Otte kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 855d7b0b5ebSCarsten Otte kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 85660b413c9SChristian Borntraeger kvm_run->s.regs.prefix = vcpu->arch.sie_block->prefix; 8579eed0735SChristian Borntraeger memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 858d7b0b5ebSCarsten Otte 859b0c632dbSHeiko Carstens if (vcpu->sigset_active) 860b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 861b0c632dbSHeiko Carstens 862b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 8637e8e6ab4SHeiko Carstens return rc; 864b0c632dbSHeiko Carstens } 865b0c632dbSHeiko Carstens 866092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from, 867b0c632dbSHeiko Carstens unsigned long n, int prefix) 868b0c632dbSHeiko Carstens { 869b0c632dbSHeiko Carstens if (prefix) 870b0c632dbSHeiko Carstens return copy_to_guest(vcpu, guestdest, from, n); 871b0c632dbSHeiko Carstens else 872b0c632dbSHeiko Carstens return copy_to_guest_absolute(vcpu, guestdest, from, n); 873b0c632dbSHeiko Carstens } 874b0c632dbSHeiko Carstens 875b0c632dbSHeiko Carstens /* 876b0c632dbSHeiko Carstens * store status at address 877b0c632dbSHeiko Carstens * we use have two special cases: 878b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 879b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 880b0c632dbSHeiko Carstens */ 881e879892cSThomas Huth int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr) 882b0c632dbSHeiko Carstens { 883092670cdSCarsten Otte unsigned char archmode = 1; 884b0c632dbSHeiko Carstens int prefix; 885178bd789SThomas Huth u64 clkcomp; 886b0c632dbSHeiko Carstens 887b0c632dbSHeiko Carstens if (addr == KVM_S390_STORE_STATUS_NOADDR) { 888b0c632dbSHeiko Carstens if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1)) 889b0c632dbSHeiko Carstens return -EFAULT; 890b0c632dbSHeiko Carstens addr = SAVE_AREA_BASE; 891b0c632dbSHeiko Carstens prefix = 0; 892b0c632dbSHeiko Carstens } else if (addr == KVM_S390_STORE_STATUS_PREFIXED) { 893b0c632dbSHeiko Carstens if (copy_to_guest(vcpu, 163ul, &archmode, 1)) 894b0c632dbSHeiko Carstens return -EFAULT; 895b0c632dbSHeiko Carstens addr = SAVE_AREA_BASE; 896b0c632dbSHeiko Carstens prefix = 1; 897b0c632dbSHeiko Carstens } else 898b0c632dbSHeiko Carstens prefix = 0; 899b0c632dbSHeiko Carstens 900f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs), 901b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fprs, 128, prefix)) 902b0c632dbSHeiko Carstens return -EFAULT; 903b0c632dbSHeiko Carstens 904f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs), 9055a32c1afSChristian Borntraeger vcpu->run->s.regs.gprs, 128, prefix)) 906b0c632dbSHeiko Carstens return -EFAULT; 907b0c632dbSHeiko Carstens 908f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw), 909b0c632dbSHeiko Carstens &vcpu->arch.sie_block->gpsw, 16, prefix)) 910b0c632dbSHeiko Carstens return -EFAULT; 911b0c632dbSHeiko Carstens 912f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg), 913b0c632dbSHeiko Carstens &vcpu->arch.sie_block->prefix, 4, prefix)) 914b0c632dbSHeiko Carstens return -EFAULT; 915b0c632dbSHeiko Carstens 916b0c632dbSHeiko Carstens if (__guestcopy(vcpu, 917f64ca217SHeiko Carstens addr + offsetof(struct save_area, fp_ctrl_reg), 918b0c632dbSHeiko Carstens &vcpu->arch.guest_fpregs.fpc, 4, prefix)) 919b0c632dbSHeiko Carstens return -EFAULT; 920b0c632dbSHeiko Carstens 921f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg), 922b0c632dbSHeiko Carstens &vcpu->arch.sie_block->todpr, 4, prefix)) 923b0c632dbSHeiko Carstens return -EFAULT; 924b0c632dbSHeiko Carstens 925f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer), 926b0c632dbSHeiko Carstens &vcpu->arch.sie_block->cputm, 8, prefix)) 927b0c632dbSHeiko Carstens return -EFAULT; 928b0c632dbSHeiko Carstens 929178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 930f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp), 931178bd789SThomas Huth &clkcomp, 8, prefix)) 932b0c632dbSHeiko Carstens return -EFAULT; 933b0c632dbSHeiko Carstens 934f64ca217SHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs), 93559674c1aSChristian Borntraeger &vcpu->run->s.regs.acrs, 64, prefix)) 936b0c632dbSHeiko Carstens return -EFAULT; 937b0c632dbSHeiko Carstens 938b0c632dbSHeiko Carstens if (__guestcopy(vcpu, 939f64ca217SHeiko Carstens addr + offsetof(struct save_area, ctrl_regs), 940b0c632dbSHeiko Carstens &vcpu->arch.sie_block->gcr, 128, prefix)) 941b0c632dbSHeiko Carstens return -EFAULT; 942b0c632dbSHeiko Carstens return 0; 943b0c632dbSHeiko Carstens } 944b0c632dbSHeiko Carstens 945e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 946e879892cSThomas Huth { 947e879892cSThomas Huth /* 948e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 949e879892cSThomas Huth * copying in vcpu load/put. Lets update our copies before we save 950e879892cSThomas Huth * it into the save area 951e879892cSThomas Huth */ 952e879892cSThomas Huth save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); 953e879892cSThomas Huth save_fp_regs(vcpu->arch.guest_fpregs.fprs); 954e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 955e879892cSThomas Huth 956e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 957e879892cSThomas Huth } 958e879892cSThomas Huth 959d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 960d6712df9SCornelia Huck struct kvm_enable_cap *cap) 961d6712df9SCornelia Huck { 962d6712df9SCornelia Huck int r; 963d6712df9SCornelia Huck 964d6712df9SCornelia Huck if (cap->flags) 965d6712df9SCornelia Huck return -EINVAL; 966d6712df9SCornelia Huck 967d6712df9SCornelia Huck switch (cap->cap) { 968fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 969fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 970fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 971fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 972fa6b7fe9SCornelia Huck } 973fa6b7fe9SCornelia Huck r = 0; 974fa6b7fe9SCornelia Huck break; 975d6712df9SCornelia Huck default: 976d6712df9SCornelia Huck r = -EINVAL; 977d6712df9SCornelia Huck break; 978d6712df9SCornelia Huck } 979d6712df9SCornelia Huck return r; 980d6712df9SCornelia Huck } 981d6712df9SCornelia Huck 982b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 983b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 984b0c632dbSHeiko Carstens { 985b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 986b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 987800c1065SThomas Huth int idx; 988bc923cc9SAvi Kivity long r; 989b0c632dbSHeiko Carstens 99093736624SAvi Kivity switch (ioctl) { 99193736624SAvi Kivity case KVM_S390_INTERRUPT: { 992ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 993ba5c1e9bSCarsten Otte 99493736624SAvi Kivity r = -EFAULT; 995ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 99693736624SAvi Kivity break; 99793736624SAvi Kivity r = kvm_s390_inject_vcpu(vcpu, &s390int); 99893736624SAvi Kivity break; 999ba5c1e9bSCarsten Otte } 1000b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 1001800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 1002bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 1003800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 1004bc923cc9SAvi Kivity break; 1005b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 1006b0c632dbSHeiko Carstens psw_t psw; 1007b0c632dbSHeiko Carstens 1008bc923cc9SAvi Kivity r = -EFAULT; 1009b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 1010bc923cc9SAvi Kivity break; 1011bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 1012bc923cc9SAvi Kivity break; 1013b0c632dbSHeiko Carstens } 1014b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 1015bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 1016bc923cc9SAvi Kivity break; 101714eebd91SCarsten Otte case KVM_SET_ONE_REG: 101814eebd91SCarsten Otte case KVM_GET_ONE_REG: { 101914eebd91SCarsten Otte struct kvm_one_reg reg; 102014eebd91SCarsten Otte r = -EFAULT; 102114eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 102214eebd91SCarsten Otte break; 102314eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 102414eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 102514eebd91SCarsten Otte else 102614eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 102714eebd91SCarsten Otte break; 102814eebd91SCarsten Otte } 102927e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 103027e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 103127e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 103227e0393fSCarsten Otte 103327e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 103427e0393fSCarsten Otte r = -EFAULT; 103527e0393fSCarsten Otte break; 103627e0393fSCarsten Otte } 103727e0393fSCarsten Otte 103827e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 103927e0393fSCarsten Otte r = -EINVAL; 104027e0393fSCarsten Otte break; 104127e0393fSCarsten Otte } 104227e0393fSCarsten Otte 104327e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 104427e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 104527e0393fSCarsten Otte break; 104627e0393fSCarsten Otte } 104727e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 104827e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 104927e0393fSCarsten Otte 105027e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 105127e0393fSCarsten Otte r = -EFAULT; 105227e0393fSCarsten Otte break; 105327e0393fSCarsten Otte } 105427e0393fSCarsten Otte 105527e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 105627e0393fSCarsten Otte r = -EINVAL; 105727e0393fSCarsten Otte break; 105827e0393fSCarsten Otte } 105927e0393fSCarsten Otte 106027e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 106127e0393fSCarsten Otte ucasmap.length); 106227e0393fSCarsten Otte break; 106327e0393fSCarsten Otte } 106427e0393fSCarsten Otte #endif 1065ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 1066ccc7910fSCarsten Otte r = gmap_fault(arg, vcpu->arch.gmap); 1067ccc7910fSCarsten Otte if (!IS_ERR_VALUE(r)) 1068ccc7910fSCarsten Otte r = 0; 1069ccc7910fSCarsten Otte break; 1070ccc7910fSCarsten Otte } 1071d6712df9SCornelia Huck case KVM_ENABLE_CAP: 1072d6712df9SCornelia Huck { 1073d6712df9SCornelia Huck struct kvm_enable_cap cap; 1074d6712df9SCornelia Huck r = -EFAULT; 1075d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 1076d6712df9SCornelia Huck break; 1077d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 1078d6712df9SCornelia Huck break; 1079d6712df9SCornelia Huck } 1080b0c632dbSHeiko Carstens default: 10813e6afcf1SCarsten Otte r = -ENOTTY; 1082b0c632dbSHeiko Carstens } 1083bc923cc9SAvi Kivity return r; 1084b0c632dbSHeiko Carstens } 1085b0c632dbSHeiko Carstens 10865b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 10875b1c1493SCarsten Otte { 10885b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 10895b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 10905b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 10915b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 10925b1c1493SCarsten Otte get_page(vmf->page); 10935b1c1493SCarsten Otte return 0; 10945b1c1493SCarsten Otte } 10955b1c1493SCarsten Otte #endif 10965b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 10975b1c1493SCarsten Otte } 10985b1c1493SCarsten Otte 10995587027cSAneesh Kumar K.V void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, 1100db3fe4ebSTakuya Yoshikawa struct kvm_memory_slot *dont) 1101db3fe4ebSTakuya Yoshikawa { 1102db3fe4ebSTakuya Yoshikawa } 1103db3fe4ebSTakuya Yoshikawa 11045587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 11055587027cSAneesh Kumar K.V unsigned long npages) 1106db3fe4ebSTakuya Yoshikawa { 1107db3fe4ebSTakuya Yoshikawa return 0; 1108db3fe4ebSTakuya Yoshikawa } 1109db3fe4ebSTakuya Yoshikawa 1110e59dbe09STakuya Yoshikawa void kvm_arch_memslots_updated(struct kvm *kvm) 1111e59dbe09STakuya Yoshikawa { 1112e59dbe09STakuya Yoshikawa } 1113e59dbe09STakuya Yoshikawa 1114b0c632dbSHeiko Carstens /* Section: memory related */ 1115f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 1116f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 11177b6195a9STakuya Yoshikawa struct kvm_userspace_memory_region *mem, 11187b6195a9STakuya Yoshikawa enum kvm_mr_change change) 1119b0c632dbSHeiko Carstens { 1120dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 1121dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 1122dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 1123dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 1124b0c632dbSHeiko Carstens 1125598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 1126b0c632dbSHeiko Carstens return -EINVAL; 1127b0c632dbSHeiko Carstens 1128598841caSCarsten Otte if (mem->memory_size & 0xffffful) 1129b0c632dbSHeiko Carstens return -EINVAL; 1130b0c632dbSHeiko Carstens 1131f7784b8eSMarcelo Tosatti return 0; 1132f7784b8eSMarcelo Tosatti } 1133f7784b8eSMarcelo Tosatti 1134f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 1135f7784b8eSMarcelo Tosatti struct kvm_userspace_memory_region *mem, 11368482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 11378482644aSTakuya Yoshikawa enum kvm_mr_change change) 1138f7784b8eSMarcelo Tosatti { 1139f7850c92SCarsten Otte int rc; 1140f7784b8eSMarcelo Tosatti 11412cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 11422cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 11432cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 11442cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 11452cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 11462cef4debSChristian Borntraeger */ 11472cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 11482cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 11492cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 11502cef4debSChristian Borntraeger return; 1151598841caSCarsten Otte 1152598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 1153598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 1154598841caSCarsten Otte if (rc) 1155f7850c92SCarsten Otte printk(KERN_WARNING "kvm-s390: failed to commit memory region\n"); 1156598841caSCarsten Otte return; 1157b0c632dbSHeiko Carstens } 1158b0c632dbSHeiko Carstens 11592df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_all(struct kvm *kvm) 11602df72e9bSMarcelo Tosatti { 11612df72e9bSMarcelo Tosatti } 11622df72e9bSMarcelo Tosatti 11632df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_memslot(struct kvm *kvm, 11642df72e9bSMarcelo Tosatti struct kvm_memory_slot *slot) 116534d4cb8fSMarcelo Tosatti { 116634d4cb8fSMarcelo Tosatti } 116734d4cb8fSMarcelo Tosatti 1168b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 1169b0c632dbSHeiko Carstens { 1170ef50f7acSChristian Borntraeger int ret; 11710ee75beaSAvi Kivity ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 1172ef50f7acSChristian Borntraeger if (ret) 1173ef50f7acSChristian Borntraeger return ret; 1174ef50f7acSChristian Borntraeger 1175ef50f7acSChristian Borntraeger /* 1176ef50f7acSChristian Borntraeger * guests can ask for up to 255+1 double words, we need a full page 117725985edcSLucas De Marchi * to hold the maximum amount of facilities. On the other hand, we 1178ef50f7acSChristian Borntraeger * only set facilities that are known to work in KVM. 1179ef50f7acSChristian Borntraeger */ 118078c4b59fSMichael Mueller vfacilities = (unsigned long *) get_zeroed_page(GFP_KERNEL|GFP_DMA); 118178c4b59fSMichael Mueller if (!vfacilities) { 1182ef50f7acSChristian Borntraeger kvm_exit(); 1183ef50f7acSChristian Borntraeger return -ENOMEM; 1184ef50f7acSChristian Borntraeger } 118578c4b59fSMichael Mueller memcpy(vfacilities, S390_lowcore.stfle_fac_list, 16); 1186*7feb6bb8SMichael Mueller vfacilities[0] &= 0xff82fff3f47c2000UL; 1187*7feb6bb8SMichael Mueller vfacilities[1] &= 0x005c000000000000UL; 1188ef50f7acSChristian Borntraeger return 0; 1189b0c632dbSHeiko Carstens } 1190b0c632dbSHeiko Carstens 1191b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 1192b0c632dbSHeiko Carstens { 119378c4b59fSMichael Mueller free_page((unsigned long) vfacilities); 1194b0c632dbSHeiko Carstens kvm_exit(); 1195b0c632dbSHeiko Carstens } 1196b0c632dbSHeiko Carstens 1197b0c632dbSHeiko Carstens module_init(kvm_s390_init); 1198b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 1199566af940SCornelia Huck 1200566af940SCornelia Huck /* 1201566af940SCornelia Huck * Enable autoloading of the kvm module. 1202566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 1203566af940SCornelia Huck * since x86 takes a different approach. 1204566af940SCornelia Huck */ 1205566af940SCornelia Huck #include <linux/miscdevice.h> 1206566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 1207566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 1208