1b0c632dbSHeiko Carstens /* 2b0c632dbSHeiko Carstens * s390host.c -- 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> 26b0c632dbSHeiko Carstens #include <asm/lowcore.h> 27b0c632dbSHeiko Carstens #include <asm/pgtable.h> 28f5daba1dSHeiko Carstens #include <asm/nmi.h> 29ef50f7acSChristian Borntraeger #include <asm/system.h> 308f2abe6aSChristian Borntraeger #include "kvm-s390.h" 31b0c632dbSHeiko Carstens #include "gaccess.h" 32b0c632dbSHeiko Carstens 33b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 34b0c632dbSHeiko Carstens 35b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 36b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 370eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 388f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 398f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 408f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 418f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 42ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 43ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 44ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 45f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 46ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 47ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 48ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 49ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 50ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 51ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 52ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 53ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 54ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 55453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 56453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 57453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 58453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 59453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 60453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 61453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 62453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 63453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 645288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 655288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 665288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 675288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 685288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 695288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 70e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 71b0c632dbSHeiko Carstens { NULL } 72b0c632dbSHeiko Carstens }; 73b0c632dbSHeiko Carstens 74ef50f7acSChristian Borntraeger static unsigned long long *facilities; 75b0c632dbSHeiko Carstens 76b0c632dbSHeiko Carstens /* Section: not file related */ 7710474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage) 78b0c632dbSHeiko Carstens { 79b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 8010474ae8SAlexander Graf return 0; 81b0c632dbSHeiko Carstens } 82b0c632dbSHeiko Carstens 83b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage) 84b0c632dbSHeiko Carstens { 85b0c632dbSHeiko Carstens } 86b0c632dbSHeiko Carstens 87b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 88b0c632dbSHeiko Carstens { 89b0c632dbSHeiko Carstens return 0; 90b0c632dbSHeiko Carstens } 91b0c632dbSHeiko Carstens 92b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 93b0c632dbSHeiko Carstens { 94b0c632dbSHeiko Carstens } 95b0c632dbSHeiko Carstens 96b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn) 97b0c632dbSHeiko Carstens { 98b0c632dbSHeiko Carstens } 99b0c632dbSHeiko Carstens 100b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 101b0c632dbSHeiko Carstens { 102b0c632dbSHeiko Carstens return 0; 103b0c632dbSHeiko Carstens } 104b0c632dbSHeiko Carstens 105b0c632dbSHeiko Carstens void kvm_arch_exit(void) 106b0c632dbSHeiko Carstens { 107b0c632dbSHeiko Carstens } 108b0c632dbSHeiko Carstens 109b0c632dbSHeiko Carstens /* Section: device related */ 110b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 111b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 112b0c632dbSHeiko Carstens { 113b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 114b0c632dbSHeiko Carstens return s390_enable_sie(); 115b0c632dbSHeiko Carstens return -EINVAL; 116b0c632dbSHeiko Carstens } 117b0c632dbSHeiko Carstens 118b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext) 119b0c632dbSHeiko Carstens { 120*d7b0b5ebSCarsten Otte int r; 121*d7b0b5ebSCarsten Otte 1222bd0ac4eSCarsten Otte switch (ext) { 123*d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 124*d7b0b5ebSCarsten Otte r = 1; 125*d7b0b5ebSCarsten Otte break; 1262bd0ac4eSCarsten Otte default: 127*d7b0b5ebSCarsten Otte r = 0; 128b0c632dbSHeiko Carstens } 129*d7b0b5ebSCarsten Otte return r; 1302bd0ac4eSCarsten Otte } 131b0c632dbSHeiko Carstens 132b0c632dbSHeiko Carstens /* Section: vm related */ 133b0c632dbSHeiko Carstens /* 134b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 135b0c632dbSHeiko Carstens */ 136b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 137b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 138b0c632dbSHeiko Carstens { 139b0c632dbSHeiko Carstens return 0; 140b0c632dbSHeiko Carstens } 141b0c632dbSHeiko Carstens 142b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 143b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 144b0c632dbSHeiko Carstens { 145b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 146b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 147b0c632dbSHeiko Carstens int r; 148b0c632dbSHeiko Carstens 149b0c632dbSHeiko Carstens switch (ioctl) { 150ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 151ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 152ba5c1e9bSCarsten Otte 153ba5c1e9bSCarsten Otte r = -EFAULT; 154ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 155ba5c1e9bSCarsten Otte break; 156ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 157ba5c1e9bSCarsten Otte break; 158ba5c1e9bSCarsten Otte } 159b0c632dbSHeiko Carstens default: 160367e1319SAvi Kivity r = -ENOTTY; 161b0c632dbSHeiko Carstens } 162b0c632dbSHeiko Carstens 163b0c632dbSHeiko Carstens return r; 164b0c632dbSHeiko Carstens } 165b0c632dbSHeiko Carstens 166b0c632dbSHeiko Carstens struct kvm *kvm_arch_create_vm(void) 167b0c632dbSHeiko Carstens { 168b0c632dbSHeiko Carstens struct kvm *kvm; 169b0c632dbSHeiko Carstens int rc; 170b0c632dbSHeiko Carstens char debug_name[16]; 171b0c632dbSHeiko Carstens 172b0c632dbSHeiko Carstens rc = s390_enable_sie(); 173b0c632dbSHeiko Carstens if (rc) 174b0c632dbSHeiko Carstens goto out_nokvm; 175b0c632dbSHeiko Carstens 176b0c632dbSHeiko Carstens rc = -ENOMEM; 177b0c632dbSHeiko Carstens kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); 178b0c632dbSHeiko Carstens if (!kvm) 179b0c632dbSHeiko Carstens goto out_nokvm; 180b0c632dbSHeiko Carstens 181b0c632dbSHeiko Carstens kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL); 182b0c632dbSHeiko Carstens if (!kvm->arch.sca) 183b0c632dbSHeiko Carstens goto out_nosca; 184b0c632dbSHeiko Carstens 185b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 186b0c632dbSHeiko Carstens 187b0c632dbSHeiko Carstens kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long)); 188b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 189b0c632dbSHeiko Carstens goto out_nodbf; 190b0c632dbSHeiko Carstens 191ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 192ba5c1e9bSCarsten Otte INIT_LIST_HEAD(&kvm->arch.float_int.list); 193ba5c1e9bSCarsten Otte 194b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 195b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "%s", "vm created"); 196b0c632dbSHeiko Carstens 197b0c632dbSHeiko Carstens return kvm; 198b0c632dbSHeiko Carstens out_nodbf: 199b0c632dbSHeiko Carstens free_page((unsigned long)(kvm->arch.sca)); 200b0c632dbSHeiko Carstens out_nosca: 201b0c632dbSHeiko Carstens kfree(kvm); 202b0c632dbSHeiko Carstens out_nokvm: 203b0c632dbSHeiko Carstens return ERR_PTR(rc); 204b0c632dbSHeiko Carstens } 205b0c632dbSHeiko Carstens 206d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 207d329c035SChristian Borntraeger { 208d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 209abf4a71eSCarsten Otte if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda == 210abf4a71eSCarsten Otte (__u64) vcpu->arch.sie_block) 211abf4a71eSCarsten Otte vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0; 212abf4a71eSCarsten Otte smp_mb(); 213d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2146692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 215d329c035SChristian Borntraeger kfree(vcpu); 216d329c035SChristian Borntraeger } 217d329c035SChristian Borntraeger 218d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 219d329c035SChristian Borntraeger { 220d329c035SChristian Borntraeger unsigned int i; 221988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 222d329c035SChristian Borntraeger 223988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 224988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 225988a2caeSGleb Natapov 226988a2caeSGleb Natapov mutex_lock(&kvm->lock); 227988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 228d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 229988a2caeSGleb Natapov 230988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 231988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 232d329c035SChristian Borntraeger } 233d329c035SChristian Borntraeger 234ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm) 235ad8ba2cdSSheng Yang { 236ad8ba2cdSSheng Yang } 237ad8ba2cdSSheng Yang 238b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 239b0c632dbSHeiko Carstens { 240d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 241dfdded7cSCarsten Otte kvm_free_physmem(kvm); 242b0c632dbSHeiko Carstens free_page((unsigned long)(kvm->arch.sca)); 243d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 244b0c632dbSHeiko Carstens kfree(kvm); 245b0c632dbSHeiko Carstens } 246b0c632dbSHeiko Carstens 247b0c632dbSHeiko Carstens /* Section: vcpu related */ 248b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 249b0c632dbSHeiko Carstens { 250b0c632dbSHeiko Carstens return 0; 251b0c632dbSHeiko Carstens } 252b0c632dbSHeiko Carstens 253b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) 254b0c632dbSHeiko Carstens { 2556692cef3SChristian Borntraeger /* Nothing todo */ 256b0c632dbSHeiko Carstens } 257b0c632dbSHeiko Carstens 258b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 259b0c632dbSHeiko Carstens { 260b0c632dbSHeiko Carstens save_fp_regs(&vcpu->arch.host_fpregs); 261b0c632dbSHeiko Carstens save_access_regs(vcpu->arch.host_acrs); 262b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK; 263b0c632dbSHeiko Carstens restore_fp_regs(&vcpu->arch.guest_fpregs); 264b0c632dbSHeiko Carstens restore_access_regs(vcpu->arch.guest_acrs); 265b0c632dbSHeiko Carstens } 266b0c632dbSHeiko Carstens 267b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 268b0c632dbSHeiko Carstens { 269b0c632dbSHeiko Carstens save_fp_regs(&vcpu->arch.guest_fpregs); 270b0c632dbSHeiko Carstens save_access_regs(vcpu->arch.guest_acrs); 271b0c632dbSHeiko Carstens restore_fp_regs(&vcpu->arch.host_fpregs); 272b0c632dbSHeiko Carstens restore_access_regs(vcpu->arch.host_acrs); 273b0c632dbSHeiko Carstens } 274b0c632dbSHeiko Carstens 275b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 276b0c632dbSHeiko Carstens { 277b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 278b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 279b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 280b0c632dbSHeiko Carstens vcpu->arch.sie_block->prefix = 0UL; 281b0c632dbSHeiko Carstens vcpu->arch.sie_block->ihcpu = 0xffff; 282b0c632dbSHeiko Carstens vcpu->arch.sie_block->cputm = 0UL; 283b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 284b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 285b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 286b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 287b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 288b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fpc = 0; 289b0c632dbSHeiko Carstens asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc)); 290b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 291b0c632dbSHeiko Carstens } 292b0c632dbSHeiko Carstens 293b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 294b0c632dbSHeiko Carstens { 295b0c632dbSHeiko Carstens atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH); 296628eb9b8SChristian Ehrhardt set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests); 297b0c632dbSHeiko Carstens vcpu->arch.sie_block->ecb = 2; 298b0c632dbSHeiko Carstens vcpu->arch.sie_block->eca = 0xC1002001U; 299ef50f7acSChristian Borntraeger vcpu->arch.sie_block->fac = (int) (long) facilities; 300ca872302SChristian Borntraeger hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); 301ca872302SChristian Borntraeger tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet, 302ba5c1e9bSCarsten Otte (unsigned long) vcpu); 303ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 304453423dcSChristian Borntraeger get_cpu_id(&vcpu->arch.cpu_id); 30592e6ecf3SChristian Borntraeger vcpu->arch.cpu_id.version = 0xff; 306b0c632dbSHeiko Carstens return 0; 307b0c632dbSHeiko Carstens } 308b0c632dbSHeiko Carstens 309b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 310b0c632dbSHeiko Carstens unsigned int id) 311b0c632dbSHeiko Carstens { 312b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL); 313b0c632dbSHeiko Carstens int rc = -ENOMEM; 314b0c632dbSHeiko Carstens 315b0c632dbSHeiko Carstens if (!vcpu) 316b0c632dbSHeiko Carstens goto out_nomem; 317b0c632dbSHeiko Carstens 318180c12fbSChristian Borntraeger vcpu->arch.sie_block = (struct kvm_s390_sie_block *) 319180c12fbSChristian Borntraeger get_zeroed_page(GFP_KERNEL); 320b0c632dbSHeiko Carstens 321b0c632dbSHeiko Carstens if (!vcpu->arch.sie_block) 322b0c632dbSHeiko Carstens goto out_free_cpu; 323b0c632dbSHeiko Carstens 324b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 325b0c632dbSHeiko Carstens BUG_ON(!kvm->arch.sca); 326abf4a71eSCarsten Otte if (!kvm->arch.sca->cpu[id].sda) 327b0c632dbSHeiko Carstens kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block; 328b0c632dbSHeiko Carstens vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32); 329b0c632dbSHeiko Carstens vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca; 330b0c632dbSHeiko Carstens 331ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 332ba5c1e9bSCarsten Otte INIT_LIST_HEAD(&vcpu->arch.local_int.list); 333ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 334b037a4f3SChristian Borntraeger spin_lock(&kvm->arch.float_int.lock); 335ba5c1e9bSCarsten Otte kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int; 336ba5c1e9bSCarsten Otte init_waitqueue_head(&vcpu->arch.local_int.wq); 3375288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 338b037a4f3SChristian Borntraeger spin_unlock(&kvm->arch.float_int.lock); 339ba5c1e9bSCarsten Otte 340b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 341b0c632dbSHeiko Carstens if (rc) 342b0c632dbSHeiko Carstens goto out_free_cpu; 343b0c632dbSHeiko Carstens VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu, 344b0c632dbSHeiko Carstens vcpu->arch.sie_block); 345b0c632dbSHeiko Carstens 346b0c632dbSHeiko Carstens return vcpu; 347b0c632dbSHeiko Carstens out_free_cpu: 348b0c632dbSHeiko Carstens kfree(vcpu); 349b0c632dbSHeiko Carstens out_nomem: 350b0c632dbSHeiko Carstens return ERR_PTR(rc); 351b0c632dbSHeiko Carstens } 352b0c632dbSHeiko Carstens 353b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 354b0c632dbSHeiko Carstens { 355b0c632dbSHeiko Carstens /* kvm common code refers to this, but never calls it */ 356b0c632dbSHeiko Carstens BUG(); 357b0c632dbSHeiko Carstens return 0; 358b0c632dbSHeiko Carstens } 359b0c632dbSHeiko Carstens 360b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 361b0c632dbSHeiko Carstens { 362b0c632dbSHeiko Carstens vcpu_load(vcpu); 363b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 364b0c632dbSHeiko Carstens vcpu_put(vcpu); 365b0c632dbSHeiko Carstens return 0; 366b0c632dbSHeiko Carstens } 367b0c632dbSHeiko Carstens 368b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 369b0c632dbSHeiko Carstens { 370b0c632dbSHeiko Carstens vcpu_load(vcpu); 371b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_gprs, ®s->gprs, sizeof(regs->gprs)); 372b0c632dbSHeiko Carstens vcpu_put(vcpu); 373b0c632dbSHeiko Carstens return 0; 374b0c632dbSHeiko Carstens } 375b0c632dbSHeiko Carstens 376b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 377b0c632dbSHeiko Carstens { 378b0c632dbSHeiko Carstens vcpu_load(vcpu); 379b0c632dbSHeiko Carstens memcpy(®s->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs)); 380b0c632dbSHeiko Carstens vcpu_put(vcpu); 381b0c632dbSHeiko Carstens return 0; 382b0c632dbSHeiko Carstens } 383b0c632dbSHeiko Carstens 384b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 385b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 386b0c632dbSHeiko Carstens { 387b0c632dbSHeiko Carstens vcpu_load(vcpu); 388b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs)); 389b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 390b0c632dbSHeiko Carstens vcpu_put(vcpu); 391b0c632dbSHeiko Carstens return 0; 392b0c632dbSHeiko Carstens } 393b0c632dbSHeiko Carstens 394b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 395b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 396b0c632dbSHeiko Carstens { 397b0c632dbSHeiko Carstens vcpu_load(vcpu); 398b0c632dbSHeiko Carstens memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs)); 399b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 400b0c632dbSHeiko Carstens vcpu_put(vcpu); 401b0c632dbSHeiko Carstens return 0; 402b0c632dbSHeiko Carstens } 403b0c632dbSHeiko Carstens 404b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 405b0c632dbSHeiko Carstens { 406b0c632dbSHeiko Carstens vcpu_load(vcpu); 407b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 408b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fpc = fpu->fpc; 409b0c632dbSHeiko Carstens vcpu_put(vcpu); 410b0c632dbSHeiko Carstens return 0; 411b0c632dbSHeiko Carstens } 412b0c632dbSHeiko Carstens 413b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 414b0c632dbSHeiko Carstens { 415b0c632dbSHeiko Carstens vcpu_load(vcpu); 416b0c632dbSHeiko Carstens memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs)); 417b0c632dbSHeiko Carstens fpu->fpc = vcpu->arch.guest_fpregs.fpc; 418b0c632dbSHeiko Carstens vcpu_put(vcpu); 419b0c632dbSHeiko Carstens return 0; 420b0c632dbSHeiko Carstens } 421b0c632dbSHeiko Carstens 422b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 423b0c632dbSHeiko Carstens { 424b0c632dbSHeiko Carstens int rc = 0; 425b0c632dbSHeiko Carstens 426b0c632dbSHeiko Carstens vcpu_load(vcpu); 427b0c632dbSHeiko Carstens if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING) 428b0c632dbSHeiko Carstens rc = -EBUSY; 429*d7b0b5ebSCarsten Otte else { 430*d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 431*d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 432*d7b0b5ebSCarsten Otte } 433b0c632dbSHeiko Carstens vcpu_put(vcpu); 434b0c632dbSHeiko Carstens return rc; 435b0c632dbSHeiko Carstens } 436b0c632dbSHeiko Carstens 437b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 438b0c632dbSHeiko Carstens struct kvm_translation *tr) 439b0c632dbSHeiko Carstens { 440b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 441b0c632dbSHeiko Carstens } 442b0c632dbSHeiko Carstens 443d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 444d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 445b0c632dbSHeiko Carstens { 446b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 447b0c632dbSHeiko Carstens } 448b0c632dbSHeiko Carstens 44962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 45062d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 45162d9f0dbSMarcelo Tosatti { 45262d9f0dbSMarcelo Tosatti return -EINVAL; /* not implemented yet */ 45362d9f0dbSMarcelo Tosatti } 45462d9f0dbSMarcelo Tosatti 45562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 45662d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 45762d9f0dbSMarcelo Tosatti { 45862d9f0dbSMarcelo Tosatti return -EINVAL; /* not implemented yet */ 45962d9f0dbSMarcelo Tosatti } 46062d9f0dbSMarcelo Tosatti 461b0c632dbSHeiko Carstens static void __vcpu_run(struct kvm_vcpu *vcpu) 462b0c632dbSHeiko Carstens { 463b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16); 464b0c632dbSHeiko Carstens 465b0c632dbSHeiko Carstens if (need_resched()) 466b0c632dbSHeiko Carstens schedule(); 467b0c632dbSHeiko Carstens 46871cde587SChristian Borntraeger if (test_thread_flag(TIF_MCCK_PENDING)) 46971cde587SChristian Borntraeger s390_handle_mcck(); 47071cde587SChristian Borntraeger 4710ff31867SCarsten Otte kvm_s390_deliver_pending_interrupts(vcpu); 4720ff31867SCarsten Otte 473b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 474b0c632dbSHeiko Carstens local_irq_disable(); 475b0c632dbSHeiko Carstens kvm_guest_enter(); 476b0c632dbSHeiko Carstens local_irq_enable(); 477b0c632dbSHeiko Carstens VCPU_EVENT(vcpu, 6, "entering sie flags %x", 478b0c632dbSHeiko Carstens atomic_read(&vcpu->arch.sie_block->cpuflags)); 4791f0d0f09SCarsten Otte if (sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs)) { 4801f0d0f09SCarsten Otte VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4811f0d0f09SCarsten Otte kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 4821f0d0f09SCarsten Otte } 483b0c632dbSHeiko Carstens VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 484b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode); 485b0c632dbSHeiko Carstens local_irq_disable(); 486b0c632dbSHeiko Carstens kvm_guest_exit(); 487b0c632dbSHeiko Carstens local_irq_enable(); 488b0c632dbSHeiko Carstens 489b0c632dbSHeiko Carstens memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16); 490b0c632dbSHeiko Carstens } 491b0c632dbSHeiko Carstens 492b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 493b0c632dbSHeiko Carstens { 4948f2abe6aSChristian Borntraeger int rc; 495b0c632dbSHeiko Carstens sigset_t sigsaved; 496b0c632dbSHeiko Carstens 497b0c632dbSHeiko Carstens vcpu_load(vcpu); 498b0c632dbSHeiko Carstens 4999ace903dSChristian Ehrhardt rerun_vcpu: 500628eb9b8SChristian Ehrhardt if (vcpu->requests) 501628eb9b8SChristian Ehrhardt if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) 502628eb9b8SChristian Ehrhardt kvm_s390_vcpu_set_mem(vcpu); 503628eb9b8SChristian Ehrhardt 50451e4d5abSCarsten Otte /* verify, that memory has been registered */ 505628eb9b8SChristian Ehrhardt if (!vcpu->arch.sie_block->gmslm) { 50651e4d5abSCarsten Otte vcpu_put(vcpu); 507628eb9b8SChristian Ehrhardt VCPU_EVENT(vcpu, 3, "%s", "no memory registered to run vcpu"); 50851e4d5abSCarsten Otte return -EINVAL; 50951e4d5abSCarsten Otte } 51051e4d5abSCarsten Otte 511b0c632dbSHeiko Carstens if (vcpu->sigset_active) 512b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 513b0c632dbSHeiko Carstens 514b0c632dbSHeiko Carstens atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 515b0c632dbSHeiko Carstens 516ba5c1e9bSCarsten Otte BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL); 517ba5c1e9bSCarsten Otte 5188f2abe6aSChristian Borntraeger switch (kvm_run->exit_reason) { 5198f2abe6aSChristian Borntraeger case KVM_EXIT_S390_SIEIC: 5208f2abe6aSChristian Borntraeger case KVM_EXIT_UNKNOWN: 5219ace903dSChristian Ehrhardt case KVM_EXIT_INTR: 5228f2abe6aSChristian Borntraeger case KVM_EXIT_S390_RESET: 5238f2abe6aSChristian Borntraeger break; 5248f2abe6aSChristian Borntraeger default: 5258f2abe6aSChristian Borntraeger BUG(); 5268f2abe6aSChristian Borntraeger } 5278f2abe6aSChristian Borntraeger 528*d7b0b5ebSCarsten Otte vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 529*d7b0b5ebSCarsten Otte vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 530*d7b0b5ebSCarsten Otte 531dab4079dSHeiko Carstens might_fault(); 5328f2abe6aSChristian Borntraeger 5338f2abe6aSChristian Borntraeger do { 534b0c632dbSHeiko Carstens __vcpu_run(vcpu); 5358f2abe6aSChristian Borntraeger rc = kvm_handle_sie_intercept(vcpu); 5368f2abe6aSChristian Borntraeger } while (!signal_pending(current) && !rc); 5378f2abe6aSChristian Borntraeger 5389ace903dSChristian Ehrhardt if (rc == SIE_INTERCEPT_RERUNVCPU) 5399ace903dSChristian Ehrhardt goto rerun_vcpu; 5409ace903dSChristian Ehrhardt 541b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 542b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 5438f2abe6aSChristian Borntraeger rc = -EINTR; 544b1d16c49SChristian Ehrhardt } 5458f2abe6aSChristian Borntraeger 5468f2abe6aSChristian Borntraeger if (rc == -ENOTSUPP) { 5478f2abe6aSChristian Borntraeger /* intercept cannot be handled in-kernel, prepare kvm-run */ 5488f2abe6aSChristian Borntraeger kvm_run->exit_reason = KVM_EXIT_S390_SIEIC; 5498f2abe6aSChristian Borntraeger kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 5508f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 5518f2abe6aSChristian Borntraeger kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 5528f2abe6aSChristian Borntraeger rc = 0; 5538f2abe6aSChristian Borntraeger } 5548f2abe6aSChristian Borntraeger 5558f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 5568f2abe6aSChristian Borntraeger /* intercept was handled, but userspace support is needed 5578f2abe6aSChristian Borntraeger * kvm_run has been prepared by the handler */ 5588f2abe6aSChristian Borntraeger rc = 0; 5598f2abe6aSChristian Borntraeger } 5608f2abe6aSChristian Borntraeger 561*d7b0b5ebSCarsten Otte kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 562*d7b0b5ebSCarsten Otte kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 563*d7b0b5ebSCarsten Otte 564b0c632dbSHeiko Carstens if (vcpu->sigset_active) 565b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 566b0c632dbSHeiko Carstens 567b0c632dbSHeiko Carstens vcpu_put(vcpu); 568b0c632dbSHeiko Carstens 569b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 5707e8e6ab4SHeiko Carstens return rc; 571b0c632dbSHeiko Carstens } 572b0c632dbSHeiko Carstens 573b0c632dbSHeiko Carstens static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, const void *from, 574b0c632dbSHeiko Carstens unsigned long n, int prefix) 575b0c632dbSHeiko Carstens { 576b0c632dbSHeiko Carstens if (prefix) 577b0c632dbSHeiko Carstens return copy_to_guest(vcpu, guestdest, from, n); 578b0c632dbSHeiko Carstens else 579b0c632dbSHeiko Carstens return copy_to_guest_absolute(vcpu, guestdest, from, n); 580b0c632dbSHeiko Carstens } 581b0c632dbSHeiko Carstens 582b0c632dbSHeiko Carstens /* 583b0c632dbSHeiko Carstens * store status at address 584b0c632dbSHeiko Carstens * we use have two special cases: 585b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 586b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 587b0c632dbSHeiko Carstens */ 588b0c632dbSHeiko Carstens int __kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 589b0c632dbSHeiko Carstens { 590b0c632dbSHeiko Carstens const unsigned char archmode = 1; 591b0c632dbSHeiko Carstens int prefix; 592b0c632dbSHeiko Carstens 593b0c632dbSHeiko Carstens if (addr == KVM_S390_STORE_STATUS_NOADDR) { 594b0c632dbSHeiko Carstens if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1)) 595b0c632dbSHeiko Carstens return -EFAULT; 596b0c632dbSHeiko Carstens addr = SAVE_AREA_BASE; 597b0c632dbSHeiko Carstens prefix = 0; 598b0c632dbSHeiko Carstens } else if (addr == KVM_S390_STORE_STATUS_PREFIXED) { 599b0c632dbSHeiko Carstens if (copy_to_guest(vcpu, 163ul, &archmode, 1)) 600b0c632dbSHeiko Carstens return -EFAULT; 601b0c632dbSHeiko Carstens addr = SAVE_AREA_BASE; 602b0c632dbSHeiko Carstens prefix = 1; 603b0c632dbSHeiko Carstens } else 604b0c632dbSHeiko Carstens prefix = 0; 605b0c632dbSHeiko Carstens 606b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, fp_regs), 607b0c632dbSHeiko Carstens vcpu->arch.guest_fpregs.fprs, 128, prefix)) 608b0c632dbSHeiko Carstens return -EFAULT; 609b0c632dbSHeiko Carstens 610b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, gp_regs), 611b0c632dbSHeiko Carstens vcpu->arch.guest_gprs, 128, prefix)) 612b0c632dbSHeiko Carstens return -EFAULT; 613b0c632dbSHeiko Carstens 614b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, psw), 615b0c632dbSHeiko Carstens &vcpu->arch.sie_block->gpsw, 16, prefix)) 616b0c632dbSHeiko Carstens return -EFAULT; 617b0c632dbSHeiko Carstens 618b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, pref_reg), 619b0c632dbSHeiko Carstens &vcpu->arch.sie_block->prefix, 4, prefix)) 620b0c632dbSHeiko Carstens return -EFAULT; 621b0c632dbSHeiko Carstens 622b0c632dbSHeiko Carstens if (__guestcopy(vcpu, 623b0c632dbSHeiko Carstens addr + offsetof(struct save_area_s390x, fp_ctrl_reg), 624b0c632dbSHeiko Carstens &vcpu->arch.guest_fpregs.fpc, 4, prefix)) 625b0c632dbSHeiko Carstens return -EFAULT; 626b0c632dbSHeiko Carstens 627b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, tod_reg), 628b0c632dbSHeiko Carstens &vcpu->arch.sie_block->todpr, 4, prefix)) 629b0c632dbSHeiko Carstens return -EFAULT; 630b0c632dbSHeiko Carstens 631b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, timer), 632b0c632dbSHeiko Carstens &vcpu->arch.sie_block->cputm, 8, prefix)) 633b0c632dbSHeiko Carstens return -EFAULT; 634b0c632dbSHeiko Carstens 635b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, clk_cmp), 636b0c632dbSHeiko Carstens &vcpu->arch.sie_block->ckc, 8, prefix)) 637b0c632dbSHeiko Carstens return -EFAULT; 638b0c632dbSHeiko Carstens 639b0c632dbSHeiko Carstens if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, acc_regs), 640b0c632dbSHeiko Carstens &vcpu->arch.guest_acrs, 64, prefix)) 641b0c632dbSHeiko Carstens return -EFAULT; 642b0c632dbSHeiko Carstens 643b0c632dbSHeiko Carstens if (__guestcopy(vcpu, 644b0c632dbSHeiko Carstens addr + offsetof(struct save_area_s390x, ctrl_regs), 645b0c632dbSHeiko Carstens &vcpu->arch.sie_block->gcr, 128, prefix)) 646b0c632dbSHeiko Carstens return -EFAULT; 647b0c632dbSHeiko Carstens return 0; 648b0c632dbSHeiko Carstens } 649b0c632dbSHeiko Carstens 650b0c632dbSHeiko Carstens static int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 651b0c632dbSHeiko Carstens { 652b0c632dbSHeiko Carstens int rc; 653b0c632dbSHeiko Carstens 654b0c632dbSHeiko Carstens vcpu_load(vcpu); 655b0c632dbSHeiko Carstens rc = __kvm_s390_vcpu_store_status(vcpu, addr); 656b0c632dbSHeiko Carstens vcpu_put(vcpu); 657b0c632dbSHeiko Carstens return rc; 658b0c632dbSHeiko Carstens } 659b0c632dbSHeiko Carstens 660b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 661b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 662b0c632dbSHeiko Carstens { 663b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 664b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 665b0c632dbSHeiko Carstens 666b0c632dbSHeiko Carstens switch (ioctl) { 667ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 668ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 669ba5c1e9bSCarsten Otte 670ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 671ba5c1e9bSCarsten Otte return -EFAULT; 672ba5c1e9bSCarsten Otte return kvm_s390_inject_vcpu(vcpu, &s390int); 673ba5c1e9bSCarsten Otte } 674b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 675b0c632dbSHeiko Carstens return kvm_s390_vcpu_store_status(vcpu, arg); 676b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 677b0c632dbSHeiko Carstens psw_t psw; 678b0c632dbSHeiko Carstens 679b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 680b0c632dbSHeiko Carstens return -EFAULT; 681b0c632dbSHeiko Carstens return kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 682b0c632dbSHeiko Carstens } 683b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 684b0c632dbSHeiko Carstens return kvm_arch_vcpu_ioctl_initial_reset(vcpu); 685b0c632dbSHeiko Carstens default: 686b0c632dbSHeiko Carstens ; 687b0c632dbSHeiko Carstens } 688b0c632dbSHeiko Carstens return -EINVAL; 689b0c632dbSHeiko Carstens } 690b0c632dbSHeiko Carstens 691b0c632dbSHeiko Carstens /* Section: memory related */ 692b0c632dbSHeiko Carstens int kvm_arch_set_memory_region(struct kvm *kvm, 693b0c632dbSHeiko Carstens struct kvm_userspace_memory_region *mem, 694b0c632dbSHeiko Carstens struct kvm_memory_slot old, 695b0c632dbSHeiko Carstens int user_alloc) 696b0c632dbSHeiko Carstens { 6972668dab7SCarsten Otte int i; 698988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 6992668dab7SCarsten Otte 700b0c632dbSHeiko Carstens /* A few sanity checks. We can have exactly one memory slot which has 701b0c632dbSHeiko Carstens to start at guest virtual zero and which has to be located at a 702b0c632dbSHeiko Carstens page boundary in userland and which has to end at a page boundary. 703b0c632dbSHeiko Carstens The memory in userland is ok to be fragmented into various different 704b0c632dbSHeiko Carstens vmas. It is okay to mmap() and munmap() stuff in this slot after 705b0c632dbSHeiko Carstens doing this call at any time */ 706b0c632dbSHeiko Carstens 707628eb9b8SChristian Ehrhardt if (mem->slot) 708b0c632dbSHeiko Carstens return -EINVAL; 709b0c632dbSHeiko Carstens 710b0c632dbSHeiko Carstens if (mem->guest_phys_addr) 711b0c632dbSHeiko Carstens return -EINVAL; 712b0c632dbSHeiko Carstens 713b0c632dbSHeiko Carstens if (mem->userspace_addr & (PAGE_SIZE - 1)) 714b0c632dbSHeiko Carstens return -EINVAL; 715b0c632dbSHeiko Carstens 716b0c632dbSHeiko Carstens if (mem->memory_size & (PAGE_SIZE - 1)) 717b0c632dbSHeiko Carstens return -EINVAL; 718b0c632dbSHeiko Carstens 7192668dab7SCarsten Otte if (!user_alloc) 7202668dab7SCarsten Otte return -EINVAL; 7212668dab7SCarsten Otte 722628eb9b8SChristian Ehrhardt /* request update of sie control block for all available vcpus */ 723988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) { 724988a2caeSGleb Natapov if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) 725628eb9b8SChristian Ehrhardt continue; 726988a2caeSGleb Natapov kvm_s390_inject_sigp_stop(vcpu, ACTION_RELOADVCPU_ON_STOP); 7272668dab7SCarsten Otte } 728b0c632dbSHeiko Carstens 729b0c632dbSHeiko Carstens return 0; 730b0c632dbSHeiko Carstens } 731b0c632dbSHeiko Carstens 73234d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm) 73334d4cb8fSMarcelo Tosatti { 73434d4cb8fSMarcelo Tosatti } 73534d4cb8fSMarcelo Tosatti 736b0c632dbSHeiko Carstens gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) 737b0c632dbSHeiko Carstens { 738b0c632dbSHeiko Carstens return gfn; 739b0c632dbSHeiko Carstens } 740b0c632dbSHeiko Carstens 741b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 742b0c632dbSHeiko Carstens { 743ef50f7acSChristian Borntraeger int ret; 744ef50f7acSChristian Borntraeger ret = kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE); 745ef50f7acSChristian Borntraeger if (ret) 746ef50f7acSChristian Borntraeger return ret; 747ef50f7acSChristian Borntraeger 748ef50f7acSChristian Borntraeger /* 749ef50f7acSChristian Borntraeger * guests can ask for up to 255+1 double words, we need a full page 750ef50f7acSChristian Borntraeger * to hold the maximum amount of facilites. On the other hand, we 751ef50f7acSChristian Borntraeger * only set facilities that are known to work in KVM. 752ef50f7acSChristian Borntraeger */ 753ef50f7acSChristian Borntraeger facilities = (unsigned long long *) get_zeroed_page(GFP_DMA); 754ef50f7acSChristian Borntraeger if (!facilities) { 755ef50f7acSChristian Borntraeger kvm_exit(); 756ef50f7acSChristian Borntraeger return -ENOMEM; 757ef50f7acSChristian Borntraeger } 758ef50f7acSChristian Borntraeger stfle(facilities, 1); 759ef50f7acSChristian Borntraeger facilities[0] &= 0xff00fff3f0700000ULL; 760ef50f7acSChristian Borntraeger return 0; 761b0c632dbSHeiko Carstens } 762b0c632dbSHeiko Carstens 763b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 764b0c632dbSHeiko Carstens { 765ef50f7acSChristian Borntraeger free_page((unsigned long) facilities); 766b0c632dbSHeiko Carstens kvm_exit(); 767b0c632dbSHeiko Carstens } 768b0c632dbSHeiko Carstens 769b0c632dbSHeiko Carstens module_init(kvm_s390_init); 770b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 771