1b0c632dbSHeiko Carstens /* 2a53c8fabSHeiko Carstens * hosting zSeries kernel virtual machines 3b0c632dbSHeiko Carstens * 4628eb9b8SChristian Ehrhardt * Copyright IBM Corp. 2008, 2009 5b0c632dbSHeiko Carstens * 6b0c632dbSHeiko Carstens * This program is free software; you can redistribute it and/or modify 7b0c632dbSHeiko Carstens * it under the terms of the GNU General Public License (version 2 only) 8b0c632dbSHeiko Carstens * as published by the Free Software Foundation. 9b0c632dbSHeiko Carstens * 10b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 11b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 12b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 13628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1415f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 15b0c632dbSHeiko Carstens */ 16b0c632dbSHeiko Carstens 17b0c632dbSHeiko Carstens #include <linux/compiler.h> 18b0c632dbSHeiko Carstens #include <linux/err.h> 19b0c632dbSHeiko Carstens #include <linux/fs.h> 20ca872302SChristian Borntraeger #include <linux/hrtimer.h> 21b0c632dbSHeiko Carstens #include <linux/init.h> 22b0c632dbSHeiko Carstens #include <linux/kvm.h> 23b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 24b0c632dbSHeiko Carstens #include <linux/module.h> 25a374e892STony Krowiak #include <linux/random.h> 26b0c632dbSHeiko Carstens #include <linux/slab.h> 27ba5c1e9bSCarsten Otte #include <linux/timer.h> 2841408c28SThomas Huth #include <linux/vmalloc.h> 2915c9705fSDavid Hildenbrand #include <linux/bitmap.h> 30cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 31b0c632dbSHeiko Carstens #include <asm/lowcore.h> 32fdf03650SFan Zhang #include <asm/etr.h> 33b0c632dbSHeiko Carstens #include <asm/pgtable.h> 341e133ab2SMartin Schwidefsky #include <asm/gmap.h> 35f5daba1dSHeiko Carstens #include <asm/nmi.h> 36a0616cdeSDavid Howells #include <asm/switch_to.h> 376d3da241SJens Freimann #include <asm/isc.h> 381526bf9cSChristian Borntraeger #include <asm/sclp.h> 390a763c78SDavid Hildenbrand #include <asm/cpacf.h> 400a763c78SDavid Hildenbrand #include <asm/etr.h> 418f2abe6aSChristian Borntraeger #include "kvm-s390.h" 42b0c632dbSHeiko Carstens #include "gaccess.h" 43b0c632dbSHeiko Carstens 44ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390" 45ea2cdd27SDavid Hildenbrand #undef pr_fmt 46ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 47ea2cdd27SDavid Hildenbrand 485786fffaSCornelia Huck #define CREATE_TRACE_POINTS 495786fffaSCornelia Huck #include "trace.h" 50ade38c31SCornelia Huck #include "trace-s390.h" 515786fffaSCornelia Huck 5241408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 53816c7667SJens Freimann #define LOCAL_IRQS 32 54816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 55816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 5641408c28SThomas Huth 57b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 58b0c632dbSHeiko Carstens 59b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 60b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 610eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 628f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 638f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 648f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 658f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 66ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 67ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 68ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 69a011eeb2SJanosch Frank { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, 70f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 7162bea5bfSPaolo Bonzini { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 723491caf2SChristian Borntraeger { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 73ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 74f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 75ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 76aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 77aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 78ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 797697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 80ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 81ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 82ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 83ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 84ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 85ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 86ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 8769d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 88453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 89453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 90453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 91453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 92453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 938a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 94453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 95453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 96b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 97453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 98453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 99bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 10095ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 1015288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 102bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1037697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1045288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 10542cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 10642cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1075288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 10842cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 10942cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 110cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1115288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1125288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1135288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 11442cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 11542cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 11642cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 117388186bcSChristian Borntraeger { "diagnose_10", VCPU_STAT(diagnose_10) }, 118e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 11941628d33SKonstantin Weitz { "diagnose_9c", VCPU_STAT(diagnose_9c) }, 120175a5c9eSChristian Borntraeger { "diagnose_258", VCPU_STAT(diagnose_258) }, 121175a5c9eSChristian Borntraeger { "diagnose_308", VCPU_STAT(diagnose_308) }, 122175a5c9eSChristian Borntraeger { "diagnose_500", VCPU_STAT(diagnose_500) }, 123b0c632dbSHeiko Carstens { NULL } 124b0c632dbSHeiko Carstens }; 125b0c632dbSHeiko Carstens 1269d8d5786SMichael Mueller /* upper facilities limit for kvm */ 12760a37709SAlexander Yarygin unsigned long kvm_s390_fac_list_mask[16] = { 12860a37709SAlexander Yarygin 0xffe6000000000000UL, 12960a37709SAlexander Yarygin 0x005e000000000000UL, 1309d8d5786SMichael Mueller }; 131b0c632dbSHeiko Carstens 1329d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void) 13378c4b59fSMichael Mueller { 1349d8d5786SMichael Mueller BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64); 1359d8d5786SMichael Mueller return ARRAY_SIZE(kvm_s390_fac_list_mask); 13678c4b59fSMichael Mueller } 13778c4b59fSMichael Mueller 13815c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 13915c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 1400a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 1410a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 14215c9705fSDavid Hildenbrand 1439d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 14478f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 1459d8d5786SMichael Mueller 146b0c632dbSHeiko Carstens /* Section: not file related */ 14713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 148b0c632dbSHeiko Carstens { 149b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 15010474ae8SAlexander Graf return 0; 151b0c632dbSHeiko Carstens } 152b0c632dbSHeiko Carstens 1532c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address); 1542c70fe44SChristian Borntraeger 155fdf03650SFan Zhang /* 156fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 157fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 158fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 159fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 160fdf03650SFan Zhang */ 161fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 162fdf03650SFan Zhang void *v) 163fdf03650SFan Zhang { 164fdf03650SFan Zhang struct kvm *kvm; 165fdf03650SFan Zhang struct kvm_vcpu *vcpu; 166fdf03650SFan Zhang int i; 167fdf03650SFan Zhang unsigned long long *delta = v; 168fdf03650SFan Zhang 169fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 170fdf03650SFan Zhang kvm->arch.epoch -= *delta; 171fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 172fdf03650SFan Zhang vcpu->arch.sie_block->epoch -= *delta; 173db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 174db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 175fdf03650SFan Zhang } 176fdf03650SFan Zhang } 177fdf03650SFan Zhang return NOTIFY_OK; 178fdf03650SFan Zhang } 179fdf03650SFan Zhang 180fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 181fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 182fdf03650SFan Zhang }; 183fdf03650SFan Zhang 184b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 185b0c632dbSHeiko Carstens { 1862c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 1872c70fe44SChristian Borntraeger gmap_register_ipte_notifier(&gmap_notifier); 188fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 189fdf03650SFan Zhang &kvm_clock_notifier); 190b0c632dbSHeiko Carstens return 0; 191b0c632dbSHeiko Carstens } 192b0c632dbSHeiko Carstens 193b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 194b0c632dbSHeiko Carstens { 1952c70fe44SChristian Borntraeger gmap_unregister_ipte_notifier(&gmap_notifier); 196fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 197fdf03650SFan Zhang &kvm_clock_notifier); 198b0c632dbSHeiko Carstens } 199b0c632dbSHeiko Carstens 20022be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 20122be5a13SDavid Hildenbrand { 20222be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 20322be5a13SDavid Hildenbrand } 20422be5a13SDavid Hildenbrand 2050a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 2060a763c78SDavid Hildenbrand { 2070a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 2080a763c78SDavid Hildenbrand int cc = 3; /* subfunction not available */ 2090a763c78SDavid Hildenbrand 2100a763c78SDavid Hildenbrand asm volatile( 2110a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 2120a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 2130a763c78SDavid Hildenbrand " ipm %0\n" 2140a763c78SDavid Hildenbrand " srl %0,28\n" 2150a763c78SDavid Hildenbrand : "=d" (cc) 2160a763c78SDavid Hildenbrand : "d" (r0) 2170a763c78SDavid Hildenbrand : "cc"); 2180a763c78SDavid Hildenbrand return cc == 0; 2190a763c78SDavid Hildenbrand } 2200a763c78SDavid Hildenbrand 22122be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 22222be5a13SDavid Hildenbrand { 2230a763c78SDavid Hildenbrand int i; 2240a763c78SDavid Hildenbrand 2250a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 2260a763c78SDavid Hildenbrand if (plo_test_bit(i)) 2270a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 2280a763c78SDavid Hildenbrand } 2290a763c78SDavid Hildenbrand 2300a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 2310a763c78SDavid Hildenbrand etr_ptff(kvm_s390_available_subfunc.ptff, ETR_PTFF_QAF); 2320a763c78SDavid Hildenbrand 2330a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 2340a763c78SDavid Hildenbrand __cpacf_query(CPACF_KMAC, kvm_s390_available_subfunc.kmac); 2350a763c78SDavid Hildenbrand __cpacf_query(CPACF_KMC, kvm_s390_available_subfunc.kmc); 2360a763c78SDavid Hildenbrand __cpacf_query(CPACF_KM, kvm_s390_available_subfunc.km); 2370a763c78SDavid Hildenbrand __cpacf_query(CPACF_KIMD, kvm_s390_available_subfunc.kimd); 2380a763c78SDavid Hildenbrand __cpacf_query(CPACF_KLMD, kvm_s390_available_subfunc.klmd); 2390a763c78SDavid Hildenbrand } 2400a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 2410a763c78SDavid Hildenbrand __cpacf_query(CPACF_PCKMO, kvm_s390_available_subfunc.pckmo); 2420a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 2430a763c78SDavid Hildenbrand __cpacf_query(CPACF_KMCTR, kvm_s390_available_subfunc.kmctr); 2440a763c78SDavid Hildenbrand __cpacf_query(CPACF_KMF, kvm_s390_available_subfunc.kmf); 2450a763c78SDavid Hildenbrand __cpacf_query(CPACF_KMO, kvm_s390_available_subfunc.kmo); 2460a763c78SDavid Hildenbrand __cpacf_query(CPACF_PCC, kvm_s390_available_subfunc.pcc); 2470a763c78SDavid Hildenbrand } 2480a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 2490a763c78SDavid Hildenbrand __cpacf_query(CPACF_PPNO, kvm_s390_available_subfunc.ppno); 2500a763c78SDavid Hildenbrand 25122be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 25222be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 25322be5a13SDavid Hildenbrand } 25422be5a13SDavid Hildenbrand 255b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 256b0c632dbSHeiko Carstens { 25778f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 25878f26131SChristian Borntraeger if (!kvm_s390_dbf) 25978f26131SChristian Borntraeger return -ENOMEM; 26078f26131SChristian Borntraeger 26178f26131SChristian Borntraeger if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { 26278f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 26378f26131SChristian Borntraeger return -ENOMEM; 26478f26131SChristian Borntraeger } 26578f26131SChristian Borntraeger 26622be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 26722be5a13SDavid Hildenbrand 26884877d93SCornelia Huck /* Register floating interrupt controller interface. */ 26984877d93SCornelia Huck return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 270b0c632dbSHeiko Carstens } 271b0c632dbSHeiko Carstens 27278f26131SChristian Borntraeger void kvm_arch_exit(void) 27378f26131SChristian Borntraeger { 27478f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 27578f26131SChristian Borntraeger } 27678f26131SChristian Borntraeger 277b0c632dbSHeiko Carstens /* Section: device related */ 278b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 279b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 280b0c632dbSHeiko Carstens { 281b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 282b0c632dbSHeiko Carstens return s390_enable_sie(); 283b0c632dbSHeiko Carstens return -EINVAL; 284b0c632dbSHeiko Carstens } 285b0c632dbSHeiko Carstens 286784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 287b0c632dbSHeiko Carstens { 288d7b0b5ebSCarsten Otte int r; 289d7b0b5ebSCarsten Otte 2902bd0ac4eSCarsten Otte switch (ext) { 291d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 292b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 29352e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 2941efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2951efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 2961efd0f59SCarsten Otte #endif 2973c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 29860b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 29914eebd91SCarsten Otte case KVM_CAP_ONE_REG: 300d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 301fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 30210ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 303c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 304d938dc55SCornelia Huck case KVM_CAP_ENABLE_CAP_VM: 30578599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 306f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 3076352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 30847b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 3092444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 310e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 31130ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 312816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 313d7b0b5ebSCarsten Otte r = 1; 314d7b0b5ebSCarsten Otte break; 31541408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 31641408c28SThomas Huth r = MEM_OP_MAX_SIZE; 31741408c28SThomas Huth break; 318e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 319e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 32076a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 32176a6dd72SDavid Hildenbrand if (sclp.has_esca && sclp.has_64bscao) 32276a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 323e726b1bdSChristian Borntraeger break; 324e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 325e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 326e1e2e605SNick Wang break; 3271526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 328abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 3291526bf9cSChristian Borntraeger break; 33068c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 33168c55750SEric Farman r = MACHINE_HAS_VX; 33268c55750SEric Farman break; 333c6e5f166SFan Zhang case KVM_CAP_S390_RI: 334c6e5f166SFan Zhang r = test_facility(64); 335c6e5f166SFan Zhang break; 3362bd0ac4eSCarsten Otte default: 337d7b0b5ebSCarsten Otte r = 0; 338b0c632dbSHeiko Carstens } 339d7b0b5ebSCarsten Otte return r; 3402bd0ac4eSCarsten Otte } 341b0c632dbSHeiko Carstens 34215f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 34315f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 34415f36ebdSJason J. Herne { 34515f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 34615f36ebdSJason J. Herne unsigned long address; 34715f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 34815f36ebdSJason J. Herne 34915f36ebdSJason J. Herne /* Loop over all guest pages */ 35015f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 35115f36ebdSJason J. Herne for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) { 35215f36ebdSJason J. Herne address = gfn_to_hva_memslot(memslot, cur_gfn); 35315f36ebdSJason J. Herne 3541e133ab2SMartin Schwidefsky if (test_and_clear_guest_dirty(gmap->mm, address)) 35515f36ebdSJason J. Herne mark_page_dirty(kvm, cur_gfn); 3561763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 3571763f8d0SChristian Borntraeger return; 35870c88a00SChristian Borntraeger cond_resched(); 35915f36ebdSJason J. Herne } 36015f36ebdSJason J. Herne } 36115f36ebdSJason J. Herne 362b0c632dbSHeiko Carstens /* Section: vm related */ 363a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 364a6e2f683SEugene (jno) Dvurechenski 365b0c632dbSHeiko Carstens /* 366b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 367b0c632dbSHeiko Carstens */ 368b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 369b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 370b0c632dbSHeiko Carstens { 37115f36ebdSJason J. Herne int r; 37215f36ebdSJason J. Herne unsigned long n; 3739f6b8029SPaolo Bonzini struct kvm_memslots *slots; 37415f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 37515f36ebdSJason J. Herne int is_dirty = 0; 37615f36ebdSJason J. Herne 37715f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 37815f36ebdSJason J. Herne 37915f36ebdSJason J. Herne r = -EINVAL; 38015f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 38115f36ebdSJason J. Herne goto out; 38215f36ebdSJason J. Herne 3839f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 3849f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 38515f36ebdSJason J. Herne r = -ENOENT; 38615f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 38715f36ebdSJason J. Herne goto out; 38815f36ebdSJason J. Herne 38915f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 39015f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 39115f36ebdSJason J. Herne if (r) 39215f36ebdSJason J. Herne goto out; 39315f36ebdSJason J. Herne 39415f36ebdSJason J. Herne /* Clear the dirty log */ 39515f36ebdSJason J. Herne if (is_dirty) { 39615f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 39715f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 39815f36ebdSJason J. Herne } 39915f36ebdSJason J. Herne r = 0; 40015f36ebdSJason J. Herne out: 40115f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 40215f36ebdSJason J. Herne return r; 403b0c632dbSHeiko Carstens } 404b0c632dbSHeiko Carstens 405d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 406d938dc55SCornelia Huck { 407d938dc55SCornelia Huck int r; 408d938dc55SCornelia Huck 409d938dc55SCornelia Huck if (cap->flags) 410d938dc55SCornelia Huck return -EINVAL; 411d938dc55SCornelia Huck 412d938dc55SCornelia Huck switch (cap->cap) { 41384223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 414c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 41584223598SCornelia Huck kvm->arch.use_irqchip = 1; 41684223598SCornelia Huck r = 0; 41784223598SCornelia Huck break; 4182444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 419c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 4202444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 4212444b352SDavid Hildenbrand r = 0; 4222444b352SDavid Hildenbrand break; 42368c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 4245967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 4255967c17bSDavid Hildenbrand if (atomic_read(&kvm->online_vcpus)) { 4265967c17bSDavid Hildenbrand r = -EBUSY; 4275967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 428c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 429c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 43018280d8bSMichael Mueller r = 0; 43118280d8bSMichael Mueller } else 43218280d8bSMichael Mueller r = -EINVAL; 4335967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 434c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 435c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 43668c55750SEric Farman break; 437c6e5f166SFan Zhang case KVM_CAP_S390_RI: 438c6e5f166SFan Zhang r = -EINVAL; 439c6e5f166SFan Zhang mutex_lock(&kvm->lock); 440c6e5f166SFan Zhang if (atomic_read(&kvm->online_vcpus)) { 441c6e5f166SFan Zhang r = -EBUSY; 442c6e5f166SFan Zhang } else if (test_facility(64)) { 443c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 444c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 445c6e5f166SFan Zhang r = 0; 446c6e5f166SFan Zhang } 447c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 448c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 449c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 450c6e5f166SFan Zhang break; 451e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 452c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 453e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 454e44fc8c9SEkaterina Tumanova r = 0; 455e44fc8c9SEkaterina Tumanova break; 456d938dc55SCornelia Huck default: 457d938dc55SCornelia Huck r = -EINVAL; 458d938dc55SCornelia Huck break; 459d938dc55SCornelia Huck } 460d938dc55SCornelia Huck return r; 461d938dc55SCornelia Huck } 462d938dc55SCornelia Huck 4638c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 4648c0a7ce6SDominik Dingel { 4658c0a7ce6SDominik Dingel int ret; 4668c0a7ce6SDominik Dingel 4678c0a7ce6SDominik Dingel switch (attr->attr) { 4688c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 4698c0a7ce6SDominik Dingel ret = 0; 470c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 471a3a92c31SDominik Dingel kvm->arch.mem_limit); 472a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 4738c0a7ce6SDominik Dingel ret = -EFAULT; 4748c0a7ce6SDominik Dingel break; 4758c0a7ce6SDominik Dingel default: 4768c0a7ce6SDominik Dingel ret = -ENXIO; 4778c0a7ce6SDominik Dingel break; 4788c0a7ce6SDominik Dingel } 4798c0a7ce6SDominik Dingel return ret; 4808c0a7ce6SDominik Dingel } 4818c0a7ce6SDominik Dingel 4828c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 4834f718eabSDominik Dingel { 4844f718eabSDominik Dingel int ret; 4854f718eabSDominik Dingel unsigned int idx; 4864f718eabSDominik Dingel switch (attr->attr) { 4874f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 488f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 489c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 490e6db1d61SDominik Dingel break; 491e6db1d61SDominik Dingel 4924f718eabSDominik Dingel ret = -EBUSY; 493c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 4944f718eabSDominik Dingel mutex_lock(&kvm->lock); 4954f718eabSDominik Dingel if (atomic_read(&kvm->online_vcpus) == 0) { 4964f718eabSDominik Dingel kvm->arch.use_cmma = 1; 4974f718eabSDominik Dingel ret = 0; 4984f718eabSDominik Dingel } 4994f718eabSDominik Dingel mutex_unlock(&kvm->lock); 5004f718eabSDominik Dingel break; 5014f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 502f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 503f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 504f9cbd9b0SDavid Hildenbrand break; 505c3489155SDominik Dingel ret = -EINVAL; 506c3489155SDominik Dingel if (!kvm->arch.use_cmma) 507c3489155SDominik Dingel break; 508c3489155SDominik Dingel 509c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 5104f718eabSDominik Dingel mutex_lock(&kvm->lock); 5114f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 512a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 5134f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 5144f718eabSDominik Dingel mutex_unlock(&kvm->lock); 5154f718eabSDominik Dingel ret = 0; 5164f718eabSDominik Dingel break; 5178c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 5188c0a7ce6SDominik Dingel unsigned long new_limit; 5198c0a7ce6SDominik Dingel 5208c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 5218c0a7ce6SDominik Dingel return -EINVAL; 5228c0a7ce6SDominik Dingel 5238c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 5248c0a7ce6SDominik Dingel return -EFAULT; 5258c0a7ce6SDominik Dingel 526a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 527a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 5288c0a7ce6SDominik Dingel return -E2BIG; 5298c0a7ce6SDominik Dingel 530a3a92c31SDominik Dingel if (!new_limit) 531a3a92c31SDominik Dingel return -EINVAL; 532a3a92c31SDominik Dingel 533a3a92c31SDominik Dingel /* gmap_alloc takes last usable address */ 534a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 535a3a92c31SDominik Dingel new_limit -= 1; 536a3a92c31SDominik Dingel 5378c0a7ce6SDominik Dingel ret = -EBUSY; 5388c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 5398c0a7ce6SDominik Dingel if (atomic_read(&kvm->online_vcpus) == 0) { 5408c0a7ce6SDominik Dingel /* gmap_alloc will round the limit up */ 5418c0a7ce6SDominik Dingel struct gmap *new = gmap_alloc(current->mm, new_limit); 5428c0a7ce6SDominik Dingel 5438c0a7ce6SDominik Dingel if (!new) { 5448c0a7ce6SDominik Dingel ret = -ENOMEM; 5458c0a7ce6SDominik Dingel } else { 5468c0a7ce6SDominik Dingel gmap_free(kvm->arch.gmap); 5478c0a7ce6SDominik Dingel new->private = kvm; 5488c0a7ce6SDominik Dingel kvm->arch.gmap = new; 5498c0a7ce6SDominik Dingel ret = 0; 5508c0a7ce6SDominik Dingel } 5518c0a7ce6SDominik Dingel } 5528c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 553a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 554a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 555a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 5568c0a7ce6SDominik Dingel break; 5578c0a7ce6SDominik Dingel } 5584f718eabSDominik Dingel default: 5594f718eabSDominik Dingel ret = -ENXIO; 5604f718eabSDominik Dingel break; 5614f718eabSDominik Dingel } 5624f718eabSDominik Dingel return ret; 5634f718eabSDominik Dingel } 5644f718eabSDominik Dingel 565a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 566a374e892STony Krowiak 567a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 568a374e892STony Krowiak { 569a374e892STony Krowiak struct kvm_vcpu *vcpu; 570a374e892STony Krowiak int i; 571a374e892STony Krowiak 5729d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 573a374e892STony Krowiak return -EINVAL; 574a374e892STony Krowiak 575a374e892STony Krowiak mutex_lock(&kvm->lock); 576a374e892STony Krowiak switch (attr->attr) { 577a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 578a374e892STony Krowiak get_random_bytes( 579a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 580a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 581a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 582c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 583a374e892STony Krowiak break; 584a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 585a374e892STony Krowiak get_random_bytes( 586a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 587a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 588a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 589c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 590a374e892STony Krowiak break; 591a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 592a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 593a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 594a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 595c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 596a374e892STony Krowiak break; 597a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 598a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 599a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 600a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 601c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 602a374e892STony Krowiak break; 603a374e892STony Krowiak default: 604a374e892STony Krowiak mutex_unlock(&kvm->lock); 605a374e892STony Krowiak return -ENXIO; 606a374e892STony Krowiak } 607a374e892STony Krowiak 608a374e892STony Krowiak kvm_for_each_vcpu(i, vcpu, kvm) { 609a374e892STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 610a374e892STony Krowiak exit_sie(vcpu); 611a374e892STony Krowiak } 612a374e892STony Krowiak mutex_unlock(&kvm->lock); 613a374e892STony Krowiak return 0; 614a374e892STony Krowiak } 615a374e892STony Krowiak 61672f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 61772f25020SJason J. Herne { 61872f25020SJason J. Herne u8 gtod_high; 61972f25020SJason J. Herne 62072f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 62172f25020SJason J. Herne sizeof(gtod_high))) 62272f25020SJason J. Herne return -EFAULT; 62372f25020SJason J. Herne 62472f25020SJason J. Herne if (gtod_high != 0) 62572f25020SJason J. Herne return -EINVAL; 62658c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 62772f25020SJason J. Herne 62872f25020SJason J. Herne return 0; 62972f25020SJason J. Herne } 63072f25020SJason J. Herne 63172f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 63272f25020SJason J. Herne { 6335a3d883aSDavid Hildenbrand u64 gtod; 63472f25020SJason J. Herne 63572f25020SJason J. Herne if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 63672f25020SJason J. Herne return -EFAULT; 63772f25020SJason J. Herne 63825ed1675SDavid Hildenbrand kvm_s390_set_tod_clock(kvm, gtod); 63958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod); 64072f25020SJason J. Herne return 0; 64172f25020SJason J. Herne } 64272f25020SJason J. Herne 64372f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 64472f25020SJason J. Herne { 64572f25020SJason J. Herne int ret; 64672f25020SJason J. Herne 64772f25020SJason J. Herne if (attr->flags) 64872f25020SJason J. Herne return -EINVAL; 64972f25020SJason J. Herne 65072f25020SJason J. Herne switch (attr->attr) { 65172f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 65272f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 65372f25020SJason J. Herne break; 65472f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 65572f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 65672f25020SJason J. Herne break; 65772f25020SJason J. Herne default: 65872f25020SJason J. Herne ret = -ENXIO; 65972f25020SJason J. Herne break; 66072f25020SJason J. Herne } 66172f25020SJason J. Herne return ret; 66272f25020SJason J. Herne } 66372f25020SJason J. Herne 66472f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 66572f25020SJason J. Herne { 66672f25020SJason J. Herne u8 gtod_high = 0; 66772f25020SJason J. Herne 66872f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 66972f25020SJason J. Herne sizeof(gtod_high))) 67072f25020SJason J. Herne return -EFAULT; 67158c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 67272f25020SJason J. Herne 67372f25020SJason J. Herne return 0; 67472f25020SJason J. Herne } 67572f25020SJason J. Herne 67672f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 67772f25020SJason J. Herne { 6785a3d883aSDavid Hildenbrand u64 gtod; 67972f25020SJason J. Herne 68060417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 68172f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 68272f25020SJason J. Herne return -EFAULT; 68358c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 68472f25020SJason J. Herne 68572f25020SJason J. Herne return 0; 68672f25020SJason J. Herne } 68772f25020SJason J. Herne 68872f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 68972f25020SJason J. Herne { 69072f25020SJason J. Herne int ret; 69172f25020SJason J. Herne 69272f25020SJason J. Herne if (attr->flags) 69372f25020SJason J. Herne return -EINVAL; 69472f25020SJason J. Herne 69572f25020SJason J. Herne switch (attr->attr) { 69672f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 69772f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 69872f25020SJason J. Herne break; 69972f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 70072f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 70172f25020SJason J. Herne break; 70272f25020SJason J. Herne default: 70372f25020SJason J. Herne ret = -ENXIO; 70472f25020SJason J. Herne break; 70572f25020SJason J. Herne } 70672f25020SJason J. Herne return ret; 70772f25020SJason J. Herne } 70872f25020SJason J. Herne 709658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 710658b6edaSMichael Mueller { 711658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 712053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 713658b6edaSMichael Mueller int ret = 0; 714658b6edaSMichael Mueller 715658b6edaSMichael Mueller mutex_lock(&kvm->lock); 716658b6edaSMichael Mueller if (atomic_read(&kvm->online_vcpus)) { 717658b6edaSMichael Mueller ret = -EBUSY; 718658b6edaSMichael Mueller goto out; 719658b6edaSMichael Mueller } 720658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 721658b6edaSMichael Mueller if (!proc) { 722658b6edaSMichael Mueller ret = -ENOMEM; 723658b6edaSMichael Mueller goto out; 724658b6edaSMichael Mueller } 725658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 726658b6edaSMichael Mueller sizeof(*proc))) { 7279bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 728053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 729053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 730053dd230SDavid Hildenbrand if (lowest_ibc) { 731053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 732053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 733053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 734053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 735053dd230SDavid Hildenbrand else 736658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 737053dd230SDavid Hildenbrand } 738c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 739658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 740658b6edaSMichael Mueller } else 741658b6edaSMichael Mueller ret = -EFAULT; 742658b6edaSMichael Mueller kfree(proc); 743658b6edaSMichael Mueller out: 744658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 745658b6edaSMichael Mueller return ret; 746658b6edaSMichael Mueller } 747658b6edaSMichael Mueller 74815c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 74915c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 75015c9705fSDavid Hildenbrand { 75115c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 75215c9705fSDavid Hildenbrand int ret = -EBUSY; 75315c9705fSDavid Hildenbrand 75415c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 75515c9705fSDavid Hildenbrand return -EFAULT; 75615c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 75715c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 75815c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 75915c9705fSDavid Hildenbrand return -EINVAL; 76015c9705fSDavid Hildenbrand 76115c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 76215c9705fSDavid Hildenbrand if (!atomic_read(&kvm->online_vcpus)) { 76315c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 76415c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 76515c9705fSDavid Hildenbrand ret = 0; 76615c9705fSDavid Hildenbrand } 76715c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 76815c9705fSDavid Hildenbrand return ret; 76915c9705fSDavid Hildenbrand } 77015c9705fSDavid Hildenbrand 7710a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 7720a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 7730a763c78SDavid Hildenbrand { 7740a763c78SDavid Hildenbrand /* 7750a763c78SDavid Hildenbrand * Once supported by kernel + hw, we have to store the subfunctions 7760a763c78SDavid Hildenbrand * in kvm->arch and remember that user space configured them. 7770a763c78SDavid Hildenbrand */ 7780a763c78SDavid Hildenbrand return -ENXIO; 7790a763c78SDavid Hildenbrand } 7800a763c78SDavid Hildenbrand 781658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 782658b6edaSMichael Mueller { 783658b6edaSMichael Mueller int ret = -ENXIO; 784658b6edaSMichael Mueller 785658b6edaSMichael Mueller switch (attr->attr) { 786658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 787658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 788658b6edaSMichael Mueller break; 78915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 79015c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 79115c9705fSDavid Hildenbrand break; 7920a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 7930a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 7940a763c78SDavid Hildenbrand break; 795658b6edaSMichael Mueller } 796658b6edaSMichael Mueller return ret; 797658b6edaSMichael Mueller } 798658b6edaSMichael Mueller 799658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 800658b6edaSMichael Mueller { 801658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 802658b6edaSMichael Mueller int ret = 0; 803658b6edaSMichael Mueller 804658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 805658b6edaSMichael Mueller if (!proc) { 806658b6edaSMichael Mueller ret = -ENOMEM; 807658b6edaSMichael Mueller goto out; 808658b6edaSMichael Mueller } 8099bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 810658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 811c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 812c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 813658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 814658b6edaSMichael Mueller ret = -EFAULT; 815658b6edaSMichael Mueller kfree(proc); 816658b6edaSMichael Mueller out: 817658b6edaSMichael Mueller return ret; 818658b6edaSMichael Mueller } 819658b6edaSMichael Mueller 820658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 821658b6edaSMichael Mueller { 822658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 823658b6edaSMichael Mueller int ret = 0; 824658b6edaSMichael Mueller 825658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 826658b6edaSMichael Mueller if (!mach) { 827658b6edaSMichael Mueller ret = -ENOMEM; 828658b6edaSMichael Mueller goto out; 829658b6edaSMichael Mueller } 830658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 83137c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 832c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 833981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 834658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 83594422ee8SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 836658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 837658b6edaSMichael Mueller ret = -EFAULT; 838658b6edaSMichael Mueller kfree(mach); 839658b6edaSMichael Mueller out: 840658b6edaSMichael Mueller return ret; 841658b6edaSMichael Mueller } 842658b6edaSMichael Mueller 84315c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 84415c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 84515c9705fSDavid Hildenbrand { 84615c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 84715c9705fSDavid Hildenbrand 84815c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 84915c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 85015c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 85115c9705fSDavid Hildenbrand return -EFAULT; 85215c9705fSDavid Hildenbrand return 0; 85315c9705fSDavid Hildenbrand } 85415c9705fSDavid Hildenbrand 85515c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 85615c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 85715c9705fSDavid Hildenbrand { 85815c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 85915c9705fSDavid Hildenbrand 86015c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 86115c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 86215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 86315c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 86415c9705fSDavid Hildenbrand return -EFAULT; 86515c9705fSDavid Hildenbrand return 0; 86615c9705fSDavid Hildenbrand } 86715c9705fSDavid Hildenbrand 8680a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 8690a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 8700a763c78SDavid Hildenbrand { 8710a763c78SDavid Hildenbrand /* 8720a763c78SDavid Hildenbrand * Once we can actually configure subfunctions (kernel + hw support), 8730a763c78SDavid Hildenbrand * we have to check if they were already set by user space, if so copy 8740a763c78SDavid Hildenbrand * them from kvm->arch. 8750a763c78SDavid Hildenbrand */ 8760a763c78SDavid Hildenbrand return -ENXIO; 8770a763c78SDavid Hildenbrand } 8780a763c78SDavid Hildenbrand 8790a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 8800a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 8810a763c78SDavid Hildenbrand { 8820a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 8830a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 8840a763c78SDavid Hildenbrand return -EFAULT; 8850a763c78SDavid Hildenbrand return 0; 8860a763c78SDavid Hildenbrand } 887658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 888658b6edaSMichael Mueller { 889658b6edaSMichael Mueller int ret = -ENXIO; 890658b6edaSMichael Mueller 891658b6edaSMichael Mueller switch (attr->attr) { 892658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 893658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 894658b6edaSMichael Mueller break; 895658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 896658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 897658b6edaSMichael Mueller break; 89815c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 89915c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 90015c9705fSDavid Hildenbrand break; 90115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 90215c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 90315c9705fSDavid Hildenbrand break; 9040a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 9050a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 9060a763c78SDavid Hildenbrand break; 9070a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 9080a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 9090a763c78SDavid Hildenbrand break; 910658b6edaSMichael Mueller } 911658b6edaSMichael Mueller return ret; 912658b6edaSMichael Mueller } 913658b6edaSMichael Mueller 914f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 915f2061656SDominik Dingel { 916f2061656SDominik Dingel int ret; 917f2061656SDominik Dingel 918f2061656SDominik Dingel switch (attr->group) { 9194f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 9208c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 9214f718eabSDominik Dingel break; 92272f25020SJason J. Herne case KVM_S390_VM_TOD: 92372f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 92472f25020SJason J. Herne break; 925658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 926658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 927658b6edaSMichael Mueller break; 928a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 929a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 930a374e892STony Krowiak break; 931f2061656SDominik Dingel default: 932f2061656SDominik Dingel ret = -ENXIO; 933f2061656SDominik Dingel break; 934f2061656SDominik Dingel } 935f2061656SDominik Dingel 936f2061656SDominik Dingel return ret; 937f2061656SDominik Dingel } 938f2061656SDominik Dingel 939f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 940f2061656SDominik Dingel { 9418c0a7ce6SDominik Dingel int ret; 9428c0a7ce6SDominik Dingel 9438c0a7ce6SDominik Dingel switch (attr->group) { 9448c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 9458c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 9468c0a7ce6SDominik Dingel break; 94772f25020SJason J. Herne case KVM_S390_VM_TOD: 94872f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 94972f25020SJason J. Herne break; 950658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 951658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 952658b6edaSMichael Mueller break; 9538c0a7ce6SDominik Dingel default: 9548c0a7ce6SDominik Dingel ret = -ENXIO; 9558c0a7ce6SDominik Dingel break; 9568c0a7ce6SDominik Dingel } 9578c0a7ce6SDominik Dingel 9588c0a7ce6SDominik Dingel return ret; 959f2061656SDominik Dingel } 960f2061656SDominik Dingel 961f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 962f2061656SDominik Dingel { 963f2061656SDominik Dingel int ret; 964f2061656SDominik Dingel 965f2061656SDominik Dingel switch (attr->group) { 9664f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 9674f718eabSDominik Dingel switch (attr->attr) { 9684f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 9694f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 970f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 971f9cbd9b0SDavid Hildenbrand break; 9728c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 9734f718eabSDominik Dingel ret = 0; 9744f718eabSDominik Dingel break; 9754f718eabSDominik Dingel default: 9764f718eabSDominik Dingel ret = -ENXIO; 9774f718eabSDominik Dingel break; 9784f718eabSDominik Dingel } 9794f718eabSDominik Dingel break; 98072f25020SJason J. Herne case KVM_S390_VM_TOD: 98172f25020SJason J. Herne switch (attr->attr) { 98272f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 98372f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 98472f25020SJason J. Herne ret = 0; 98572f25020SJason J. Herne break; 98672f25020SJason J. Herne default: 98772f25020SJason J. Herne ret = -ENXIO; 98872f25020SJason J. Herne break; 98972f25020SJason J. Herne } 99072f25020SJason J. Herne break; 991658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 992658b6edaSMichael Mueller switch (attr->attr) { 993658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 994658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 99515c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 99615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 9970a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 998658b6edaSMichael Mueller ret = 0; 999658b6edaSMichael Mueller break; 10000a763c78SDavid Hildenbrand /* configuring subfunctions is not supported yet */ 10010a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1002658b6edaSMichael Mueller default: 1003658b6edaSMichael Mueller ret = -ENXIO; 1004658b6edaSMichael Mueller break; 1005658b6edaSMichael Mueller } 1006658b6edaSMichael Mueller break; 1007a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1008a374e892STony Krowiak switch (attr->attr) { 1009a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1010a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1011a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1012a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1013a374e892STony Krowiak ret = 0; 1014a374e892STony Krowiak break; 1015a374e892STony Krowiak default: 1016a374e892STony Krowiak ret = -ENXIO; 1017a374e892STony Krowiak break; 1018a374e892STony Krowiak } 1019a374e892STony Krowiak break; 1020f2061656SDominik Dingel default: 1021f2061656SDominik Dingel ret = -ENXIO; 1022f2061656SDominik Dingel break; 1023f2061656SDominik Dingel } 1024f2061656SDominik Dingel 1025f2061656SDominik Dingel return ret; 1026f2061656SDominik Dingel } 1027f2061656SDominik Dingel 102830ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 102930ee2a98SJason J. Herne { 103030ee2a98SJason J. Herne uint8_t *keys; 103130ee2a98SJason J. Herne uint64_t hva; 103230ee2a98SJason J. Herne unsigned long curkey; 103330ee2a98SJason J. Herne int i, r = 0; 103430ee2a98SJason J. Herne 103530ee2a98SJason J. Herne if (args->flags != 0) 103630ee2a98SJason J. Herne return -EINVAL; 103730ee2a98SJason J. Herne 103830ee2a98SJason J. Herne /* Is this guest using storage keys? */ 103930ee2a98SJason J. Herne if (!mm_use_skey(current->mm)) 104030ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 104130ee2a98SJason J. Herne 104230ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 104330ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 104430ee2a98SJason J. Herne return -EINVAL; 104530ee2a98SJason J. Herne 104630ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 104730ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 104830ee2a98SJason J. Herne if (!keys) 104930ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 105030ee2a98SJason J. Herne if (!keys) 105130ee2a98SJason J. Herne return -ENOMEM; 105230ee2a98SJason J. Herne 105330ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 105430ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 105530ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 105630ee2a98SJason J. Herne r = -EFAULT; 105730ee2a98SJason J. Herne goto out; 105830ee2a98SJason J. Herne } 105930ee2a98SJason J. Herne 106030ee2a98SJason J. Herne curkey = get_guest_storage_key(current->mm, hva); 106130ee2a98SJason J. Herne if (IS_ERR_VALUE(curkey)) { 106230ee2a98SJason J. Herne r = curkey; 106330ee2a98SJason J. Herne goto out; 106430ee2a98SJason J. Herne } 106530ee2a98SJason J. Herne keys[i] = curkey; 106630ee2a98SJason J. Herne } 106730ee2a98SJason J. Herne 106830ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 106930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 107030ee2a98SJason J. Herne if (r) 107130ee2a98SJason J. Herne r = -EFAULT; 107230ee2a98SJason J. Herne out: 107330ee2a98SJason J. Herne kvfree(keys); 107430ee2a98SJason J. Herne return r; 107530ee2a98SJason J. Herne } 107630ee2a98SJason J. Herne 107730ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 107830ee2a98SJason J. Herne { 107930ee2a98SJason J. Herne uint8_t *keys; 108030ee2a98SJason J. Herne uint64_t hva; 108130ee2a98SJason J. Herne int i, r = 0; 108230ee2a98SJason J. Herne 108330ee2a98SJason J. Herne if (args->flags != 0) 108430ee2a98SJason J. Herne return -EINVAL; 108530ee2a98SJason J. Herne 108630ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 108730ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 108830ee2a98SJason J. Herne return -EINVAL; 108930ee2a98SJason J. Herne 109030ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 109130ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 109230ee2a98SJason J. Herne if (!keys) 109330ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 109430ee2a98SJason J. Herne if (!keys) 109530ee2a98SJason J. Herne return -ENOMEM; 109630ee2a98SJason J. Herne 109730ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 109830ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 109930ee2a98SJason J. Herne if (r) { 110030ee2a98SJason J. Herne r = -EFAULT; 110130ee2a98SJason J. Herne goto out; 110230ee2a98SJason J. Herne } 110330ee2a98SJason J. Herne 110430ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 110514d4a425SDominik Dingel r = s390_enable_skey(); 110614d4a425SDominik Dingel if (r) 110714d4a425SDominik Dingel goto out; 110830ee2a98SJason J. Herne 110930ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 111030ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 111130ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 111230ee2a98SJason J. Herne r = -EFAULT; 111330ee2a98SJason J. Herne goto out; 111430ee2a98SJason J. Herne } 111530ee2a98SJason J. Herne 111630ee2a98SJason J. Herne /* Lowest order bit is reserved */ 111730ee2a98SJason J. Herne if (keys[i] & 0x01) { 111830ee2a98SJason J. Herne r = -EINVAL; 111930ee2a98SJason J. Herne goto out; 112030ee2a98SJason J. Herne } 112130ee2a98SJason J. Herne 112230ee2a98SJason J. Herne r = set_guest_storage_key(current->mm, hva, 112330ee2a98SJason J. Herne (unsigned long)keys[i], 0); 112430ee2a98SJason J. Herne if (r) 112530ee2a98SJason J. Herne goto out; 112630ee2a98SJason J. Herne } 112730ee2a98SJason J. Herne out: 112830ee2a98SJason J. Herne kvfree(keys); 112930ee2a98SJason J. Herne return r; 113030ee2a98SJason J. Herne } 113130ee2a98SJason J. Herne 1132b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 1133b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 1134b0c632dbSHeiko Carstens { 1135b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 1136b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 1137f2061656SDominik Dingel struct kvm_device_attr attr; 1138b0c632dbSHeiko Carstens int r; 1139b0c632dbSHeiko Carstens 1140b0c632dbSHeiko Carstens switch (ioctl) { 1141ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 1142ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 1143ba5c1e9bSCarsten Otte 1144ba5c1e9bSCarsten Otte r = -EFAULT; 1145ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 1146ba5c1e9bSCarsten Otte break; 1147ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 1148ba5c1e9bSCarsten Otte break; 1149ba5c1e9bSCarsten Otte } 1150d938dc55SCornelia Huck case KVM_ENABLE_CAP: { 1151d938dc55SCornelia Huck struct kvm_enable_cap cap; 1152d938dc55SCornelia Huck r = -EFAULT; 1153d938dc55SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 1154d938dc55SCornelia Huck break; 1155d938dc55SCornelia Huck r = kvm_vm_ioctl_enable_cap(kvm, &cap); 1156d938dc55SCornelia Huck break; 1157d938dc55SCornelia Huck } 115884223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 115984223598SCornelia Huck struct kvm_irq_routing_entry routing; 116084223598SCornelia Huck 116184223598SCornelia Huck r = -EINVAL; 116284223598SCornelia Huck if (kvm->arch.use_irqchip) { 116384223598SCornelia Huck /* Set up dummy routing. */ 116484223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 1165152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 116684223598SCornelia Huck } 116784223598SCornelia Huck break; 116884223598SCornelia Huck } 1169f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 1170f2061656SDominik Dingel r = -EFAULT; 1171f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1172f2061656SDominik Dingel break; 1173f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 1174f2061656SDominik Dingel break; 1175f2061656SDominik Dingel } 1176f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 1177f2061656SDominik Dingel r = -EFAULT; 1178f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1179f2061656SDominik Dingel break; 1180f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 1181f2061656SDominik Dingel break; 1182f2061656SDominik Dingel } 1183f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 1184f2061656SDominik Dingel r = -EFAULT; 1185f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1186f2061656SDominik Dingel break; 1187f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 1188f2061656SDominik Dingel break; 1189f2061656SDominik Dingel } 119030ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 119130ee2a98SJason J. Herne struct kvm_s390_skeys args; 119230ee2a98SJason J. Herne 119330ee2a98SJason J. Herne r = -EFAULT; 119430ee2a98SJason J. Herne if (copy_from_user(&args, argp, 119530ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 119630ee2a98SJason J. Herne break; 119730ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 119830ee2a98SJason J. Herne break; 119930ee2a98SJason J. Herne } 120030ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 120130ee2a98SJason J. Herne struct kvm_s390_skeys args; 120230ee2a98SJason J. Herne 120330ee2a98SJason J. Herne r = -EFAULT; 120430ee2a98SJason J. Herne if (copy_from_user(&args, argp, 120530ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 120630ee2a98SJason J. Herne break; 120730ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 120830ee2a98SJason J. Herne break; 120930ee2a98SJason J. Herne } 1210b0c632dbSHeiko Carstens default: 1211367e1319SAvi Kivity r = -ENOTTY; 1212b0c632dbSHeiko Carstens } 1213b0c632dbSHeiko Carstens 1214b0c632dbSHeiko Carstens return r; 1215b0c632dbSHeiko Carstens } 1216b0c632dbSHeiko Carstens 121745c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config) 121845c9b47cSTony Krowiak { 121945c9b47cSTony Krowiak u32 fcn_code = 0x04000000UL; 122086044c8cSChristian Borntraeger u32 cc = 0; 122145c9b47cSTony Krowiak 122286044c8cSChristian Borntraeger memset(config, 0, 128); 122345c9b47cSTony Krowiak asm volatile( 122445c9b47cSTony Krowiak "lgr 0,%1\n" 122545c9b47cSTony Krowiak "lgr 2,%2\n" 122645c9b47cSTony Krowiak ".long 0xb2af0000\n" /* PQAP(QCI) */ 122786044c8cSChristian Borntraeger "0: ipm %0\n" 122845c9b47cSTony Krowiak "srl %0,28\n" 122986044c8cSChristian Borntraeger "1:\n" 123086044c8cSChristian Borntraeger EX_TABLE(0b, 1b) 123186044c8cSChristian Borntraeger : "+r" (cc) 123245c9b47cSTony Krowiak : "r" (fcn_code), "r" (config) 123345c9b47cSTony Krowiak : "cc", "0", "2", "memory" 123445c9b47cSTony Krowiak ); 123545c9b47cSTony Krowiak 123645c9b47cSTony Krowiak return cc; 123745c9b47cSTony Krowiak } 123845c9b47cSTony Krowiak 123945c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 124045c9b47cSTony Krowiak { 124145c9b47cSTony Krowiak u8 config[128]; 124245c9b47cSTony Krowiak int cc; 124345c9b47cSTony Krowiak 1244a6aacc3fSHeiko Carstens if (test_facility(12)) { 124545c9b47cSTony Krowiak cc = kvm_s390_query_ap_config(config); 124645c9b47cSTony Krowiak 124745c9b47cSTony Krowiak if (cc) 124845c9b47cSTony Krowiak pr_err("PQAP(QCI) failed with cc=%d", cc); 124945c9b47cSTony Krowiak else 125045c9b47cSTony Krowiak return config[0] & 0x40; 125145c9b47cSTony Krowiak } 125245c9b47cSTony Krowiak 125345c9b47cSTony Krowiak return 0; 125445c9b47cSTony Krowiak } 125545c9b47cSTony Krowiak 125645c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 125745c9b47cSTony Krowiak { 125845c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 125945c9b47cSTony Krowiak 126045c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 126145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 126245c9b47cSTony Krowiak else 126345c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 126445c9b47cSTony Krowiak } 126545c9b47cSTony Krowiak 12669bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 12679d8d5786SMichael Mueller { 12689bb0ec09SDavid Hildenbrand struct cpuid cpuid; 12699bb0ec09SDavid Hildenbrand 12709bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 12719bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 12729bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 12739d8d5786SMichael Mueller } 12749d8d5786SMichael Mueller 1275c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 12765102ee87STony Krowiak { 12779d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 1278c54f0d6aSDavid Hildenbrand return; 12795102ee87STony Krowiak 1280c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 128145c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 12825102ee87STony Krowiak 1283ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 1284ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 1285ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 1286ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1287ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1288ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1289ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 12905102ee87STony Krowiak } 12915102ee87STony Krowiak 12927d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 12937d43bafcSEugene (jno) Dvurechenski { 12947d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 12955e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 12967d43bafcSEugene (jno) Dvurechenski else 12977d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 12987d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 12997d43bafcSEugene (jno) Dvurechenski } 13007d43bafcSEugene (jno) Dvurechenski 1301e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1302b0c632dbSHeiko Carstens { 130376a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 13049d8d5786SMichael Mueller int i, rc; 1305b0c632dbSHeiko Carstens char debug_name[16]; 1306f6c137ffSChristian Borntraeger static unsigned long sca_offset; 1307b0c632dbSHeiko Carstens 1308e08b9637SCarsten Otte rc = -EINVAL; 1309e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1310e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 1311e08b9637SCarsten Otte goto out_err; 1312e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 1313e08b9637SCarsten Otte goto out_err; 1314e08b9637SCarsten Otte #else 1315e08b9637SCarsten Otte if (type) 1316e08b9637SCarsten Otte goto out_err; 1317e08b9637SCarsten Otte #endif 1318e08b9637SCarsten Otte 1319b0c632dbSHeiko Carstens rc = s390_enable_sie(); 1320b0c632dbSHeiko Carstens if (rc) 1321d89f5effSJan Kiszka goto out_err; 1322b0c632dbSHeiko Carstens 1323b290411aSCarsten Otte rc = -ENOMEM; 1324b290411aSCarsten Otte 13257d0a5e62SJanosch Frank ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500); 13267d0a5e62SJanosch Frank 13277d43bafcSEugene (jno) Dvurechenski kvm->arch.use_esca = 0; /* start with basic SCA */ 132876a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 132976a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 13305e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 133176a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 1332b0c632dbSHeiko Carstens if (!kvm->arch.sca) 1333d89f5effSJan Kiszka goto out_err; 1334f6c137ffSChristian Borntraeger spin_lock(&kvm_lock); 1335c5c2c393SDavid Hildenbrand sca_offset += 16; 1336bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 1337c5c2c393SDavid Hildenbrand sca_offset = 0; 1338bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 1339bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 1340f6c137ffSChristian Borntraeger spin_unlock(&kvm_lock); 1341b0c632dbSHeiko Carstens 1342b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 1343b0c632dbSHeiko Carstens 13441cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 1345b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 134640f5b735SDominik Dingel goto out_err; 1347b0c632dbSHeiko Carstens 1348c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 1349c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 1350c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 135140f5b735SDominik Dingel goto out_err; 13529d8d5786SMichael Mueller 1353fb5bf93fSMichael Mueller /* Populate the facility mask initially. */ 1354c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list, 135594422ee8SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 13569d8d5786SMichael Mueller for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) { 13579d8d5786SMichael Mueller if (i < kvm_s390_fac_list_mask_size()) 1358c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i]; 13599d8d5786SMichael Mueller else 1360c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] = 0UL; 13619d8d5786SMichael Mueller } 13629d8d5786SMichael Mueller 1363981467c9SMichael Mueller /* Populate the facility list initially. */ 1364c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 1365c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask, 1366981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1367981467c9SMichael Mueller 136895ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 136995ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 137095ca2cb5SJanosch Frank 13719bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 137237c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 13739d8d5786SMichael Mueller 1374c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 13755102ee87STony Krowiak 1376ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 13776d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 13786d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 13798a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 1380a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 1381ba5c1e9bSCarsten Otte 1382b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 138378f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 1384b0c632dbSHeiko Carstens 1385e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 1386e08b9637SCarsten Otte kvm->arch.gmap = NULL; 1387a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 1388e08b9637SCarsten Otte } else { 138932e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 1390a3a92c31SDominik Dingel kvm->arch.mem_limit = TASK_MAX_SIZE; 139132e6b236SGuenther Hutzl else 139232e6b236SGuenther Hutzl kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE, 139332e6b236SGuenther Hutzl sclp.hamax + 1); 1394a3a92c31SDominik Dingel kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1); 1395598841caSCarsten Otte if (!kvm->arch.gmap) 139640f5b735SDominik Dingel goto out_err; 13972c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 139824eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 1399e08b9637SCarsten Otte } 1400fa6b7fe9SCornelia Huck 1401fa6b7fe9SCornelia Huck kvm->arch.css_support = 0; 140284223598SCornelia Huck kvm->arch.use_irqchip = 0; 140372f25020SJason J. Herne kvm->arch.epoch = 0; 1404fa6b7fe9SCornelia Huck 14058ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 14068335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 14078ad35755SDavid Hildenbrand 1408d89f5effSJan Kiszka return 0; 1409d89f5effSJan Kiszka out_err: 1410c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 141140f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 14127d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 141378f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 1414d89f5effSJan Kiszka return rc; 1415b0c632dbSHeiko Carstens } 1416b0c632dbSHeiko Carstens 1417d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 1418d329c035SChristian Borntraeger { 1419d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 1420ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 142167335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 14223c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 1423bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 1424a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 142527e0393fSCarsten Otte 142627e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 142727e0393fSCarsten Otte gmap_free(vcpu->arch.gmap); 142827e0393fSCarsten Otte 1429e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 1430b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 1431d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 1432b31288faSKonstantin Weitz 14336692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 1434b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 1435d329c035SChristian Borntraeger } 1436d329c035SChristian Borntraeger 1437d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 1438d329c035SChristian Borntraeger { 1439d329c035SChristian Borntraeger unsigned int i; 1440988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 1441d329c035SChristian Borntraeger 1442988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 1443988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 1444988a2caeSGleb Natapov 1445988a2caeSGleb Natapov mutex_lock(&kvm->lock); 1446988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 1447d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 1448988a2caeSGleb Natapov 1449988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 1450988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 1451d329c035SChristian Borntraeger } 1452d329c035SChristian Borntraeger 1453b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 1454b0c632dbSHeiko Carstens { 1455d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 14567d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 1457d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 1458c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 145927e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 1460598841caSCarsten Otte gmap_free(kvm->arch.gmap); 1461841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 146267335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 14638335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 1464b0c632dbSHeiko Carstens } 1465b0c632dbSHeiko Carstens 1466b0c632dbSHeiko Carstens /* Section: vcpu related */ 1467dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 1468b0c632dbSHeiko Carstens { 1469c6c956b8SMartin Schwidefsky vcpu->arch.gmap = gmap_alloc(current->mm, -1UL); 147027e0393fSCarsten Otte if (!vcpu->arch.gmap) 147127e0393fSCarsten Otte return -ENOMEM; 14722c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 1473dafd032aSDominik Dingel 147427e0393fSCarsten Otte return 0; 147527e0393fSCarsten Otte } 147627e0393fSCarsten Otte 1477a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 1478a6e2f683SEugene (jno) Dvurechenski { 14795e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 14807d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 14817d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 14827d43bafcSEugene (jno) Dvurechenski 14837d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 14847d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 14857d43bafcSEugene (jno) Dvurechenski } else { 1486bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 1487a6e2f683SEugene (jno) Dvurechenski 1488a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 1489a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 1490a6e2f683SEugene (jno) Dvurechenski } 14915e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 14927d43bafcSEugene (jno) Dvurechenski } 1493a6e2f683SEugene (jno) Dvurechenski 1494eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 1495a6e2f683SEugene (jno) Dvurechenski { 1496eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 1497eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 1498eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 14997d43bafcSEugene (jno) Dvurechenski 1500eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 15017d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 15027d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 150325508824SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= 0x04U; 1504eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 15057d43bafcSEugene (jno) Dvurechenski } else { 1506eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 1507a6e2f683SEugene (jno) Dvurechenski 1508eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 1509a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 1510a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 1511eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 1512a6e2f683SEugene (jno) Dvurechenski } 1513eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 15145e044315SEugene (jno) Dvurechenski } 15155e044315SEugene (jno) Dvurechenski 15165e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 15175e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 15185e044315SEugene (jno) Dvurechenski { 15195e044315SEugene (jno) Dvurechenski d->sda = s->sda; 15205e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 15215e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 15225e044315SEugene (jno) Dvurechenski } 15235e044315SEugene (jno) Dvurechenski 15245e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 15255e044315SEugene (jno) Dvurechenski { 15265e044315SEugene (jno) Dvurechenski int i; 15275e044315SEugene (jno) Dvurechenski 15285e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 15295e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 15305e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 15315e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 15325e044315SEugene (jno) Dvurechenski } 15335e044315SEugene (jno) Dvurechenski 15345e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 15355e044315SEugene (jno) Dvurechenski { 15365e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 15375e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 15385e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 15395e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 15405e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 15415e044315SEugene (jno) Dvurechenski 15425e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 15435e044315SEugene (jno) Dvurechenski if (!new_sca) 15445e044315SEugene (jno) Dvurechenski return -ENOMEM; 15455e044315SEugene (jno) Dvurechenski 15465e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 15475e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 15485e044315SEugene (jno) Dvurechenski 15495e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 15505e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 15515e044315SEugene (jno) Dvurechenski 15525e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 15535e044315SEugene (jno) Dvurechenski 15545e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 15555e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 15565e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 15575e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->ecb2 |= 0x04U; 15585e044315SEugene (jno) Dvurechenski } 15595e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 15605e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 15615e044315SEugene (jno) Dvurechenski 15625e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 15635e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 15645e044315SEugene (jno) Dvurechenski 15655e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 15665e044315SEugene (jno) Dvurechenski 15678335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 15688335713aSChristian Borntraeger old_sca, kvm->arch.sca); 15695e044315SEugene (jno) Dvurechenski return 0; 15707d43bafcSEugene (jno) Dvurechenski } 1571a6e2f683SEugene (jno) Dvurechenski 1572a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 1573a6e2f683SEugene (jno) Dvurechenski { 15745e044315SEugene (jno) Dvurechenski int rc; 15755e044315SEugene (jno) Dvurechenski 15765e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 15775e044315SEugene (jno) Dvurechenski return true; 157876a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 15795e044315SEugene (jno) Dvurechenski return false; 15805e044315SEugene (jno) Dvurechenski 15815e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 15825e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 15835e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 15845e044315SEugene (jno) Dvurechenski 15855e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 1586a6e2f683SEugene (jno) Dvurechenski } 1587a6e2f683SEugene (jno) Dvurechenski 1588dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 1589dafd032aSDominik Dingel { 1590dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 1591dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 159259674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 159359674c1aSChristian Borntraeger KVM_SYNC_GPRS | 15949eed0735SChristian Borntraeger KVM_SYNC_ACRS | 1595b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 1596b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 1597b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 1598c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 1599c6e5f166SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 1600f6aa6dc4SDavid Hildenbrand /* fprs can be synchronized via vrs, even if the guest has no vx. With 1601f6aa6dc4SDavid Hildenbrand * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 1602f6aa6dc4SDavid Hildenbrand */ 1603f6aa6dc4SDavid Hildenbrand if (MACHINE_HAS_VX) 160468c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 16056fd8e67dSDavid Hildenbrand else 16066fd8e67dSDavid Hildenbrand vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 1607dafd032aSDominik Dingel 1608dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 1609dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 1610dafd032aSDominik Dingel 1611b0c632dbSHeiko Carstens return 0; 1612b0c632dbSHeiko Carstens } 1613b0c632dbSHeiko Carstens 1614db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1615db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1616db0758b2SDavid Hildenbrand { 1617db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 16189c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1619db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 16209c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1621db0758b2SDavid Hildenbrand } 1622db0758b2SDavid Hildenbrand 1623db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1624db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1625db0758b2SDavid Hildenbrand { 1626db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 16279c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1628db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 1629db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 16309c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1631db0758b2SDavid Hildenbrand } 1632db0758b2SDavid Hildenbrand 1633db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1634db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1635db0758b2SDavid Hildenbrand { 1636db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 1637db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 1638db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 1639db0758b2SDavid Hildenbrand } 1640db0758b2SDavid Hildenbrand 1641db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1642db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1643db0758b2SDavid Hildenbrand { 1644db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 1645db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 1646db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 1647db0758b2SDavid Hildenbrand } 1648db0758b2SDavid Hildenbrand 1649db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1650db0758b2SDavid Hildenbrand { 1651db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 1652db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 1653db0758b2SDavid Hildenbrand preempt_enable(); 1654db0758b2SDavid Hildenbrand } 1655db0758b2SDavid Hildenbrand 1656db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1657db0758b2SDavid Hildenbrand { 1658db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 1659db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 1660db0758b2SDavid Hildenbrand preempt_enable(); 1661db0758b2SDavid Hildenbrand } 1662db0758b2SDavid Hildenbrand 16634287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 16644287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 16654287f247SDavid Hildenbrand { 1666db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 16679c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1668db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 1669db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 16704287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 16719c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1672db0758b2SDavid Hildenbrand preempt_enable(); 16734287f247SDavid Hildenbrand } 16744287f247SDavid Hildenbrand 1675db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 16764287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 16774287f247SDavid Hildenbrand { 16789c23a131SDavid Hildenbrand unsigned int seq; 1679db0758b2SDavid Hildenbrand __u64 value; 1680db0758b2SDavid Hildenbrand 1681db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 16824287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 1683db0758b2SDavid Hildenbrand 16849c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 16859c23a131SDavid Hildenbrand do { 16869c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 16879c23a131SDavid Hildenbrand /* 16889c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 16899c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 16909c23a131SDavid Hildenbrand */ 16919c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 1692db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 16939c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 16949c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 1695db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 16969c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 16979c23a131SDavid Hildenbrand preempt_enable(); 1698db0758b2SDavid Hildenbrand return value; 16994287f247SDavid Hildenbrand } 17004287f247SDavid Hildenbrand 1701b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1702b0c632dbSHeiko Carstens { 17039977e886SHendrik Brueckner /* Save host register state */ 1704d0164ee2SHendrik Brueckner save_fpu_regs(); 17059abc2a08SDavid Hildenbrand vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 17069abc2a08SDavid Hildenbrand vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 170796b2d7a8SHendrik Brueckner 17086fd8e67dSDavid Hildenbrand if (MACHINE_HAS_VX) 17099abc2a08SDavid Hildenbrand current->thread.fpu.regs = vcpu->run->s.regs.vrs; 17106fd8e67dSDavid Hildenbrand else 17116fd8e67dSDavid Hildenbrand current->thread.fpu.regs = vcpu->run->s.regs.fprs; 17129abc2a08SDavid Hildenbrand current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 17139977e886SHendrik Brueckner if (test_fp_ctl(current->thread.fpu.fpc)) 171496b2d7a8SHendrik Brueckner /* User space provided an invalid FPC, let's clear it */ 17159977e886SHendrik Brueckner current->thread.fpu.fpc = 0; 17169977e886SHendrik Brueckner 17179977e886SHendrik Brueckner save_access_regs(vcpu->arch.host_acrs); 171859674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 1719480e5926SChristian Borntraeger gmap_enable(vcpu->arch.gmap); 1720805de8f4SPeter Zijlstra atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 17215ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 1722db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 172301a745acSDavid Hildenbrand vcpu->cpu = cpu; 1724b0c632dbSHeiko Carstens } 1725b0c632dbSHeiko Carstens 1726b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 1727b0c632dbSHeiko Carstens { 172801a745acSDavid Hildenbrand vcpu->cpu = -1; 17295ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 1730db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 1731805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 1732480e5926SChristian Borntraeger gmap_disable(vcpu->arch.gmap); 17339977e886SHendrik Brueckner 17349abc2a08SDavid Hildenbrand /* Save guest register state */ 1735d0164ee2SHendrik Brueckner save_fpu_regs(); 17369977e886SHendrik Brueckner vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 17379abc2a08SDavid Hildenbrand 17389abc2a08SDavid Hildenbrand /* Restore host register state */ 17399abc2a08SDavid Hildenbrand current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 17409abc2a08SDavid Hildenbrand current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 17419977e886SHendrik Brueckner 17429977e886SHendrik Brueckner save_access_regs(vcpu->run->s.regs.acrs); 1743b0c632dbSHeiko Carstens restore_access_regs(vcpu->arch.host_acrs); 1744b0c632dbSHeiko Carstens } 1745b0c632dbSHeiko Carstens 1746b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 1747b0c632dbSHeiko Carstens { 1748b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 1749b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 1750b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 17518d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 17524287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, 0); 1753b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 1754b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 1755b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 1756b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 1757b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 17589abc2a08SDavid Hildenbrand /* make sure the new fpc will be lazily loaded */ 17599abc2a08SDavid Hildenbrand save_fpu_regs(); 17609abc2a08SDavid Hildenbrand current->thread.fpu.fpc = 0; 1761b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 1762672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 17633c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 17643c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 17656352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 17666852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 17672ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 1768b0c632dbSHeiko Carstens } 1769b0c632dbSHeiko Carstens 177031928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 177142897d86SMarcelo Tosatti { 177272f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 1773fdf03650SFan Zhang preempt_disable(); 177472f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 1775fdf03650SFan Zhang preempt_enable(); 177672f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 177725508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 1778dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 1779eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 178025508824SDavid Hildenbrand } 178125508824SDavid Hildenbrand 178242897d86SMarcelo Tosatti } 178342897d86SMarcelo Tosatti 17845102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 17855102ee87STony Krowiak { 17869d8d5786SMichael Mueller if (!test_kvm_facility(vcpu->kvm, 76)) 17875102ee87STony Krowiak return; 17885102ee87STony Krowiak 1789a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 1790a374e892STony Krowiak 1791a374e892STony Krowiak if (vcpu->kvm->arch.crypto.aes_kw) 1792a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 1793a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 1794a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 1795a374e892STony Krowiak 17965102ee87STony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 17975102ee87STony Krowiak } 17985102ee87STony Krowiak 1799b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 1800b31605c1SDominik Dingel { 1801b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 1802b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 1803b31605c1SDominik Dingel } 1804b31605c1SDominik Dingel 1805b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 1806b31605c1SDominik Dingel { 1807b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 1808b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 1809b31605c1SDominik Dingel return -ENOMEM; 1810b31605c1SDominik Dingel 1811b31605c1SDominik Dingel vcpu->arch.sie_block->ecb2 |= 0x80; 1812b31605c1SDominik Dingel vcpu->arch.sie_block->ecb2 &= ~0x08; 1813b31605c1SDominik Dingel return 0; 1814b31605c1SDominik Dingel } 1815b31605c1SDominik Dingel 181691520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 181791520f1aSMichael Mueller { 181891520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 181991520f1aSMichael Mueller 182091520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 182180bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 1822c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 182391520f1aSMichael Mueller } 182491520f1aSMichael Mueller 1825b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 1826b0c632dbSHeiko Carstens { 1827b31605c1SDominik Dingel int rc = 0; 1828b31288faSKonstantin Weitz 18299e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 18309e6dabefSCornelia Huck CPUSTAT_SM | 1831a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 1832a4a4f191SGuenther Hutzl 183353df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 1834805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags); 183553df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 1836805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags); 1837a4a4f191SGuenther Hutzl 183891520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 183991520f1aSMichael Mueller 1840*bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 1841*bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 1842*bdab09f3SDavid Hildenbrand vcpu->arch.sie_block->ecb |= 0x02; 1843bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 1844bd50e8ecSDavid Hildenbrand vcpu->arch.sie_block->ecb |= 0x04; 18459d8d5786SMichael Mueller if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73)) 18467feb6bb8SMichael Mueller vcpu->arch.sie_block->ecb |= 0x10; 18477feb6bb8SMichael Mueller 1848873b425eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi) 1849d6af0b49SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= 0x08; 185048ee7d3aSDavid Hildenbrand vcpu->arch.sie_block->eca = 0x1002000U; 185148ee7d3aSDavid Hildenbrand if (sclp.has_cei) 185248ee7d3aSDavid Hildenbrand vcpu->arch.sie_block->eca |= 0x80000000U; 185311ad65b7SDavid Hildenbrand if (sclp.has_ib) 185411ad65b7SDavid Hildenbrand vcpu->arch.sie_block->eca |= 0x40000000U; 185537c5f6c8SDavid Hildenbrand if (sclp.has_siif) 1856217a4406SHeiko Carstens vcpu->arch.sie_block->eca |= 1; 185737c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 1858ea5f4969SDavid Hildenbrand vcpu->arch.sie_block->eca |= 0x10000000U; 1859c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 1860c6e5f166SFan Zhang vcpu->arch.sie_block->ecb3 |= 0x01; 186118280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 186213211ea7SEric Farman vcpu->arch.sie_block->eca |= 0x00020000; 186313211ea7SEric Farman vcpu->arch.sie_block->ecd |= 0x20000000; 186413211ea7SEric Farman } 1865c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 1866492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 186795ca2cb5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 74)) 186895ca2cb5SJanosch Frank vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 18695a5e6536SMatthew Rosato 1870e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 1871b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 1872b31605c1SDominik Dingel if (rc) 1873b31605c1SDominik Dingel return rc; 1874b31288faSKonstantin Weitz } 18750ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1876ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 18779d8d5786SMichael Mueller 18785102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 18795102ee87STony Krowiak 1880b31605c1SDominik Dingel return rc; 1881b0c632dbSHeiko Carstens } 1882b0c632dbSHeiko Carstens 1883b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 1884b0c632dbSHeiko Carstens unsigned int id) 1885b0c632dbSHeiko Carstens { 18864d47555aSCarsten Otte struct kvm_vcpu *vcpu; 18877feb6bb8SMichael Mueller struct sie_page *sie_page; 18884d47555aSCarsten Otte int rc = -EINVAL; 1889b0c632dbSHeiko Carstens 18904215825eSDavid Hildenbrand if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 18914d47555aSCarsten Otte goto out; 18924d47555aSCarsten Otte 18934d47555aSCarsten Otte rc = -ENOMEM; 18944d47555aSCarsten Otte 1895b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 1896b0c632dbSHeiko Carstens if (!vcpu) 18974d47555aSCarsten Otte goto out; 1898b0c632dbSHeiko Carstens 18997feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 19007feb6bb8SMichael Mueller if (!sie_page) 1901b0c632dbSHeiko Carstens goto out_free_cpu; 1902b0c632dbSHeiko Carstens 19037feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 19047feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 19057feb6bb8SMichael Mueller 1906efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 1907efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 1908efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 1909efed1104SDavid Hildenbrand 1910b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 1911ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 1912ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 1913d0321a24SChristian Borntraeger vcpu->arch.local_int.wq = &vcpu->wq; 19145288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 19159c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 1916ba5c1e9bSCarsten Otte 1917b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 1918b0c632dbSHeiko Carstens if (rc) 19199abc2a08SDavid Hildenbrand goto out_free_sie_block; 19208335713aSChristian Borntraeger VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu, 1921b0c632dbSHeiko Carstens vcpu->arch.sie_block); 1922ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 1923b0c632dbSHeiko Carstens 1924b0c632dbSHeiko Carstens return vcpu; 19257b06bf2fSWei Yongjun out_free_sie_block: 19267b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 1927b0c632dbSHeiko Carstens out_free_cpu: 1928b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 19294d47555aSCarsten Otte out: 1930b0c632dbSHeiko Carstens return ERR_PTR(rc); 1931b0c632dbSHeiko Carstens } 1932b0c632dbSHeiko Carstens 1933b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 1934b0c632dbSHeiko Carstens { 19359a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 1936b0c632dbSHeiko Carstens } 1937b0c632dbSHeiko Carstens 193827406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 193949b99e1eSChristian Borntraeger { 1940805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 194161a6df54SDavid Hildenbrand exit_sie(vcpu); 194249b99e1eSChristian Borntraeger } 194349b99e1eSChristian Borntraeger 194427406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 194549b99e1eSChristian Borntraeger { 1946805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 194749b99e1eSChristian Borntraeger } 194849b99e1eSChristian Borntraeger 19498e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 19508e236546SChristian Borntraeger { 1951805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 195261a6df54SDavid Hildenbrand exit_sie(vcpu); 19538e236546SChristian Borntraeger } 19548e236546SChristian Borntraeger 19558e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 19568e236546SChristian Borntraeger { 19579bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 19588e236546SChristian Borntraeger } 19598e236546SChristian Borntraeger 196049b99e1eSChristian Borntraeger /* 196149b99e1eSChristian Borntraeger * Kick a guest cpu out of SIE and wait until SIE is not running. 196249b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 196349b99e1eSChristian Borntraeger * return immediately. */ 196449b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 196549b99e1eSChristian Borntraeger { 1966805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); 196749b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 196849b99e1eSChristian Borntraeger cpu_relax(); 196949b99e1eSChristian Borntraeger } 197049b99e1eSChristian Borntraeger 19718e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 19728e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 197349b99e1eSChristian Borntraeger { 19748e236546SChristian Borntraeger kvm_make_request(req, vcpu); 19758e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 197649b99e1eSChristian Borntraeger } 197749b99e1eSChristian Borntraeger 19782c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address) 19792c70fe44SChristian Borntraeger { 19802c70fe44SChristian Borntraeger int i; 19812c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 19822c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 19832c70fe44SChristian Borntraeger 19842c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 19852c70fe44SChristian Borntraeger /* match against both prefix pages */ 1986fda902cbSMichael Mueller if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) { 19872c70fe44SChristian Borntraeger VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address); 19888e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 19892c70fe44SChristian Borntraeger } 19902c70fe44SChristian Borntraeger } 19912c70fe44SChristian Borntraeger } 19922c70fe44SChristian Borntraeger 1993b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 1994b6d33834SChristoffer Dall { 1995b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 1996b6d33834SChristoffer Dall BUG(); 1997b6d33834SChristoffer Dall return 0; 1998b6d33834SChristoffer Dall } 1999b6d33834SChristoffer Dall 200014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 200114eebd91SCarsten Otte struct kvm_one_reg *reg) 200214eebd91SCarsten Otte { 200314eebd91SCarsten Otte int r = -EINVAL; 200414eebd91SCarsten Otte 200514eebd91SCarsten Otte switch (reg->id) { 200629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 200729b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 200829b7c71bSCarsten Otte (u32 __user *)reg->addr); 200929b7c71bSCarsten Otte break; 201029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 201129b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 201229b7c71bSCarsten Otte (u64 __user *)reg->addr); 201329b7c71bSCarsten Otte break; 201446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 20154287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 201646a6dd1cSJason J. herne (u64 __user *)reg->addr); 201746a6dd1cSJason J. herne break; 201846a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 201946a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 202046a6dd1cSJason J. herne (u64 __user *)reg->addr); 202146a6dd1cSJason J. herne break; 2022536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2023536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 2024536336c2SDominik Dingel (u64 __user *)reg->addr); 2025536336c2SDominik Dingel break; 2026536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2027536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 2028536336c2SDominik Dingel (u64 __user *)reg->addr); 2029536336c2SDominik Dingel break; 2030536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2031536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 2032536336c2SDominik Dingel (u64 __user *)reg->addr); 2033536336c2SDominik Dingel break; 2034672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2035672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 2036672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2037672550fbSChristian Borntraeger break; 2038afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2039afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 2040afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2041afa45ff5SChristian Borntraeger break; 204214eebd91SCarsten Otte default: 204314eebd91SCarsten Otte break; 204414eebd91SCarsten Otte } 204514eebd91SCarsten Otte 204614eebd91SCarsten Otte return r; 204714eebd91SCarsten Otte } 204814eebd91SCarsten Otte 204914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 205014eebd91SCarsten Otte struct kvm_one_reg *reg) 205114eebd91SCarsten Otte { 205214eebd91SCarsten Otte int r = -EINVAL; 20534287f247SDavid Hildenbrand __u64 val; 205414eebd91SCarsten Otte 205514eebd91SCarsten Otte switch (reg->id) { 205629b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 205729b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 205829b7c71bSCarsten Otte (u32 __user *)reg->addr); 205929b7c71bSCarsten Otte break; 206029b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 206129b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 206229b7c71bSCarsten Otte (u64 __user *)reg->addr); 206329b7c71bSCarsten Otte break; 206446a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 20654287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 20664287f247SDavid Hildenbrand if (!r) 20674287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 206846a6dd1cSJason J. herne break; 206946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 207046a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 207146a6dd1cSJason J. herne (u64 __user *)reg->addr); 207246a6dd1cSJason J. herne break; 2073536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2074536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 2075536336c2SDominik Dingel (u64 __user *)reg->addr); 20769fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 20779fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2078536336c2SDominik Dingel break; 2079536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2080536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 2081536336c2SDominik Dingel (u64 __user *)reg->addr); 2082536336c2SDominik Dingel break; 2083536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2084536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 2085536336c2SDominik Dingel (u64 __user *)reg->addr); 2086536336c2SDominik Dingel break; 2087672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2088672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 2089672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2090672550fbSChristian Borntraeger break; 2091afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2092afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 2093afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2094afa45ff5SChristian Borntraeger break; 209514eebd91SCarsten Otte default: 209614eebd91SCarsten Otte break; 209714eebd91SCarsten Otte } 209814eebd91SCarsten Otte 209914eebd91SCarsten Otte return r; 210014eebd91SCarsten Otte } 2101b6d33834SChristoffer Dall 2102b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 2103b0c632dbSHeiko Carstens { 2104b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 2105b0c632dbSHeiko Carstens return 0; 2106b0c632dbSHeiko Carstens } 2107b0c632dbSHeiko Carstens 2108b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2109b0c632dbSHeiko Carstens { 21105a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 2111b0c632dbSHeiko Carstens return 0; 2112b0c632dbSHeiko Carstens } 2113b0c632dbSHeiko Carstens 2114b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2115b0c632dbSHeiko Carstens { 21165a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 2117b0c632dbSHeiko Carstens return 0; 2118b0c632dbSHeiko Carstens } 2119b0c632dbSHeiko Carstens 2120b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 2121b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2122b0c632dbSHeiko Carstens { 212359674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 2124b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 212559674c1aSChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 2126b0c632dbSHeiko Carstens return 0; 2127b0c632dbSHeiko Carstens } 2128b0c632dbSHeiko Carstens 2129b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 2130b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2131b0c632dbSHeiko Carstens { 213259674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 2133b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 2134b0c632dbSHeiko Carstens return 0; 2135b0c632dbSHeiko Carstens } 2136b0c632dbSHeiko Carstens 2137b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2138b0c632dbSHeiko Carstens { 21399abc2a08SDavid Hildenbrand /* make sure the new values will be lazily loaded */ 21409abc2a08SDavid Hildenbrand save_fpu_regs(); 21414725c860SMartin Schwidefsky if (test_fp_ctl(fpu->fpc)) 21424725c860SMartin Schwidefsky return -EINVAL; 21439abc2a08SDavid Hildenbrand current->thread.fpu.fpc = fpu->fpc; 21449abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 21459abc2a08SDavid Hildenbrand convert_fp_to_vx(current->thread.fpu.vxrs, (freg_t *)fpu->fprs); 21469abc2a08SDavid Hildenbrand else 21479abc2a08SDavid Hildenbrand memcpy(current->thread.fpu.fprs, &fpu->fprs, sizeof(fpu->fprs)); 2148b0c632dbSHeiko Carstens return 0; 2149b0c632dbSHeiko Carstens } 2150b0c632dbSHeiko Carstens 2151b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2152b0c632dbSHeiko Carstens { 21539abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 21549abc2a08SDavid Hildenbrand save_fpu_regs(); 21559abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 21569abc2a08SDavid Hildenbrand convert_vx_to_fp((freg_t *)fpu->fprs, current->thread.fpu.vxrs); 21579abc2a08SDavid Hildenbrand else 21589abc2a08SDavid Hildenbrand memcpy(fpu->fprs, current->thread.fpu.fprs, sizeof(fpu->fprs)); 21599abc2a08SDavid Hildenbrand fpu->fpc = current->thread.fpu.fpc; 2160b0c632dbSHeiko Carstens return 0; 2161b0c632dbSHeiko Carstens } 2162b0c632dbSHeiko Carstens 2163b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 2164b0c632dbSHeiko Carstens { 2165b0c632dbSHeiko Carstens int rc = 0; 2166b0c632dbSHeiko Carstens 21677a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 2168b0c632dbSHeiko Carstens rc = -EBUSY; 2169d7b0b5ebSCarsten Otte else { 2170d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 2171d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 2172d7b0b5ebSCarsten Otte } 2173b0c632dbSHeiko Carstens return rc; 2174b0c632dbSHeiko Carstens } 2175b0c632dbSHeiko Carstens 2176b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 2177b0c632dbSHeiko Carstens struct kvm_translation *tr) 2178b0c632dbSHeiko Carstens { 2179b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 2180b0c632dbSHeiko Carstens } 2181b0c632dbSHeiko Carstens 218227291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 218327291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 218427291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 218527291e21SDavid Hildenbrand 2186d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 2187d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 2188b0c632dbSHeiko Carstens { 218927291e21SDavid Hildenbrand int rc = 0; 219027291e21SDavid Hildenbrand 219127291e21SDavid Hildenbrand vcpu->guest_debug = 0; 219227291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 219327291e21SDavid Hildenbrand 21942de3bfc2SDavid Hildenbrand if (dbg->control & ~VALID_GUESTDBG_FLAGS) 219527291e21SDavid Hildenbrand return -EINVAL; 219689b5b4deSDavid Hildenbrand if (!sclp.has_gpere) 219789b5b4deSDavid Hildenbrand return -EINVAL; 219827291e21SDavid Hildenbrand 219927291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 220027291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 220127291e21SDavid Hildenbrand /* enforce guest PER */ 2202805de8f4SPeter Zijlstra atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 220327291e21SDavid Hildenbrand 220427291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 220527291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 220627291e21SDavid Hildenbrand } else { 2207805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 220827291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 220927291e21SDavid Hildenbrand } 221027291e21SDavid Hildenbrand 221127291e21SDavid Hildenbrand if (rc) { 221227291e21SDavid Hildenbrand vcpu->guest_debug = 0; 221327291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 2214805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 221527291e21SDavid Hildenbrand } 221627291e21SDavid Hildenbrand 221727291e21SDavid Hildenbrand return rc; 2218b0c632dbSHeiko Carstens } 2219b0c632dbSHeiko Carstens 222062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 222162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 222262d9f0dbSMarcelo Tosatti { 22236352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 22246352e4d2SDavid Hildenbrand return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 22256352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 222662d9f0dbSMarcelo Tosatti } 222762d9f0dbSMarcelo Tosatti 222862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 222962d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 223062d9f0dbSMarcelo Tosatti { 22316352e4d2SDavid Hildenbrand int rc = 0; 22326352e4d2SDavid Hildenbrand 22336352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 22346352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 22356352e4d2SDavid Hildenbrand 22366352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 22376352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 22386352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 22396352e4d2SDavid Hildenbrand break; 22406352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 22416352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 22426352e4d2SDavid Hildenbrand break; 22436352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 22446352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 22456352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 22466352e4d2SDavid Hildenbrand default: 22476352e4d2SDavid Hildenbrand rc = -ENXIO; 22486352e4d2SDavid Hildenbrand } 22496352e4d2SDavid Hildenbrand 22506352e4d2SDavid Hildenbrand return rc; 225162d9f0dbSMarcelo Tosatti } 225262d9f0dbSMarcelo Tosatti 22538ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 22548ad35755SDavid Hildenbrand { 22558ad35755SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS; 22568ad35755SDavid Hildenbrand } 22578ad35755SDavid Hildenbrand 22582c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 22592c70fe44SChristian Borntraeger { 22608ad35755SDavid Hildenbrand retry: 22618e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 2262586b7ccdSChristian Borntraeger if (!vcpu->requests) 2263586b7ccdSChristian Borntraeger return 0; 22642c70fe44SChristian Borntraeger /* 22652c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 22662c70fe44SChristian Borntraeger * guest prefix page. gmap_ipte_notify will wait on the ptl lock. 22672c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 22682c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 22692c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 22702c70fe44SChristian Borntraeger */ 22718ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 22722c70fe44SChristian Borntraeger int rc; 22732c70fe44SChristian Borntraeger rc = gmap_ipte_notify(vcpu->arch.gmap, 2274fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 22752c70fe44SChristian Borntraeger PAGE_SIZE * 2); 22762c70fe44SChristian Borntraeger if (rc) 22772c70fe44SChristian Borntraeger return rc; 22788ad35755SDavid Hildenbrand goto retry; 22792c70fe44SChristian Borntraeger } 22808ad35755SDavid Hildenbrand 2281d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 2282d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 2283d3d692c8SDavid Hildenbrand goto retry; 2284d3d692c8SDavid Hildenbrand } 2285d3d692c8SDavid Hildenbrand 22868ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 22878ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 22888ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 2289805de8f4SPeter Zijlstra atomic_or(CPUSTAT_IBS, 22908ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 22918ad35755SDavid Hildenbrand } 22928ad35755SDavid Hildenbrand goto retry; 22938ad35755SDavid Hildenbrand } 22948ad35755SDavid Hildenbrand 22958ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 22968ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 22978ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 2298805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_IBS, 22998ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 23008ad35755SDavid Hildenbrand } 23018ad35755SDavid Hildenbrand goto retry; 23028ad35755SDavid Hildenbrand } 23038ad35755SDavid Hildenbrand 23040759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 23050759d068SDavid Hildenbrand clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 23060759d068SDavid Hildenbrand 23072c70fe44SChristian Borntraeger return 0; 23082c70fe44SChristian Borntraeger } 23092c70fe44SChristian Borntraeger 231025ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod) 231125ed1675SDavid Hildenbrand { 231225ed1675SDavid Hildenbrand struct kvm_vcpu *vcpu; 231325ed1675SDavid Hildenbrand int i; 231425ed1675SDavid Hildenbrand 231525ed1675SDavid Hildenbrand mutex_lock(&kvm->lock); 231625ed1675SDavid Hildenbrand preempt_disable(); 231725ed1675SDavid Hildenbrand kvm->arch.epoch = tod - get_tod_clock(); 231825ed1675SDavid Hildenbrand kvm_s390_vcpu_block_all(kvm); 231925ed1675SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) 232025ed1675SDavid Hildenbrand vcpu->arch.sie_block->epoch = kvm->arch.epoch; 232125ed1675SDavid Hildenbrand kvm_s390_vcpu_unblock_all(kvm); 232225ed1675SDavid Hildenbrand preempt_enable(); 232325ed1675SDavid Hildenbrand mutex_unlock(&kvm->lock); 232425ed1675SDavid Hildenbrand } 232525ed1675SDavid Hildenbrand 2326fa576c58SThomas Huth /** 2327fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 2328fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 2329fa576c58SThomas Huth * @gpa: Guest physical address 2330fa576c58SThomas Huth * @writable: Whether the page should be writable or not 2331fa576c58SThomas Huth * 2332fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 2333fa576c58SThomas Huth * 2334fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 2335fa576c58SThomas Huth */ 2336fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 233724eb3a82SDominik Dingel { 2338527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 2339527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 234024eb3a82SDominik Dingel } 234124eb3a82SDominik Dingel 23423c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 23433c038e6bSDominik Dingel unsigned long token) 23443c038e6bSDominik Dingel { 23453c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 2346383d0b05SJens Freimann struct kvm_s390_irq irq; 23473c038e6bSDominik Dingel 23483c038e6bSDominik Dingel if (start_token) { 2349383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 2350383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 2351383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 23523c038e6bSDominik Dingel } else { 23533c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 2354383d0b05SJens Freimann inti.parm64 = token; 23553c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 23563c038e6bSDominik Dingel } 23573c038e6bSDominik Dingel } 23583c038e6bSDominik Dingel 23593c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 23603c038e6bSDominik Dingel struct kvm_async_pf *work) 23613c038e6bSDominik Dingel { 23623c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 23633c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 23643c038e6bSDominik Dingel } 23653c038e6bSDominik Dingel 23663c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 23673c038e6bSDominik Dingel struct kvm_async_pf *work) 23683c038e6bSDominik Dingel { 23693c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 23703c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 23713c038e6bSDominik Dingel } 23723c038e6bSDominik Dingel 23733c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 23743c038e6bSDominik Dingel struct kvm_async_pf *work) 23753c038e6bSDominik Dingel { 23763c038e6bSDominik Dingel /* s390 will always inject the page directly */ 23773c038e6bSDominik Dingel } 23783c038e6bSDominik Dingel 23793c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 23803c038e6bSDominik Dingel { 23813c038e6bSDominik Dingel /* 23823c038e6bSDominik Dingel * s390 will always inject the page directly, 23833c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 23843c038e6bSDominik Dingel */ 23853c038e6bSDominik Dingel return true; 23863c038e6bSDominik Dingel } 23873c038e6bSDominik Dingel 23883c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 23893c038e6bSDominik Dingel { 23903c038e6bSDominik Dingel hva_t hva; 23913c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 23923c038e6bSDominik Dingel int rc; 23933c038e6bSDominik Dingel 23943c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 23953c038e6bSDominik Dingel return 0; 23963c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 23973c038e6bSDominik Dingel vcpu->arch.pfault_compare) 23983c038e6bSDominik Dingel return 0; 23993c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 24003c038e6bSDominik Dingel return 0; 24019a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 24023c038e6bSDominik Dingel return 0; 24033c038e6bSDominik Dingel if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) 24043c038e6bSDominik Dingel return 0; 24053c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 24063c038e6bSDominik Dingel return 0; 24073c038e6bSDominik Dingel 240881480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 240981480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 241081480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 24113c038e6bSDominik Dingel return 0; 24123c038e6bSDominik Dingel 24133c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 24143c038e6bSDominik Dingel return rc; 24153c038e6bSDominik Dingel } 24163c038e6bSDominik Dingel 24173fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 2418b0c632dbSHeiko Carstens { 24193fb4c40fSThomas Huth int rc, cpuflags; 2420e168bf8dSCarsten Otte 24213c038e6bSDominik Dingel /* 24223c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 24233c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 24243c038e6bSDominik Dingel * handled outside the worker. 24253c038e6bSDominik Dingel */ 24263c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 24273c038e6bSDominik Dingel 24287ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 24297ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 2430b0c632dbSHeiko Carstens 2431b0c632dbSHeiko Carstens if (need_resched()) 2432b0c632dbSHeiko Carstens schedule(); 2433b0c632dbSHeiko Carstens 2434d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 243571cde587SChristian Borntraeger s390_handle_mcck(); 243671cde587SChristian Borntraeger 243779395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 243879395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 243979395031SJens Freimann if (rc) 244079395031SJens Freimann return rc; 244179395031SJens Freimann } 24420ff31867SCarsten Otte 24432c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 24442c70fe44SChristian Borntraeger if (rc) 24452c70fe44SChristian Borntraeger return rc; 24462c70fe44SChristian Borntraeger 244727291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 244827291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 244927291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 245027291e21SDavid Hildenbrand } 245127291e21SDavid Hildenbrand 2452b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 24533fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 24543fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 24553fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 24562b29a9fdSDominik Dingel 24573fb4c40fSThomas Huth return 0; 24583fb4c40fSThomas Huth } 24593fb4c40fSThomas Huth 2460492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 2461492d8642SThomas Huth { 246256317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 246356317920SDavid Hildenbrand .code = PGM_ADDRESSING, 246456317920SDavid Hildenbrand }; 246556317920SDavid Hildenbrand u8 opcode, ilen; 2466492d8642SThomas Huth int rc; 2467492d8642SThomas Huth 2468492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 2469492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 2470492d8642SThomas Huth 2471492d8642SThomas Huth /* 2472492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 2473492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 2474492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 2475492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 2476492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 2477492d8642SThomas Huth * to be able to forward the PSW. 2478492d8642SThomas Huth */ 247965977322SDavid Hildenbrand rc = read_guest_instr(vcpu, &opcode, 1); 248056317920SDavid Hildenbrand ilen = insn_length(opcode); 24819b0d721aSDavid Hildenbrand if (rc < 0) { 24829b0d721aSDavid Hildenbrand return rc; 24839b0d721aSDavid Hildenbrand } else if (rc) { 24849b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 24859b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 24869b0d721aSDavid Hildenbrand * nullification if necessary. 24879b0d721aSDavid Hildenbrand */ 24889b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 24899b0d721aSDavid Hildenbrand ilen = 4; 24909b0d721aSDavid Hildenbrand } 249156317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 249256317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 249356317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 2494492d8642SThomas Huth } 2495492d8642SThomas Huth 24963fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 24973fb4c40fSThomas Huth { 24982b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 24992b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 25002b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 25012b29a9fdSDominik Dingel 250227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 250327291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 250427291e21SDavid Hildenbrand 25057ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 25067ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 250771f116bfSDavid Hildenbrand 250871f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 250971f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 251071f116bfSDavid Hildenbrand 251171f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 251271f116bfSDavid Hildenbrand return rc; 251371f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 251471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 251571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 251671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 251771f116bfSDavid Hildenbrand return -EREMOTE; 251871f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 251971f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 252071f116bfSDavid Hildenbrand return 0; 2521210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 2522210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 2523210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 2524210b1607SThomas Huth current->thread.gmap_addr; 2525210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 252671f116bfSDavid Hildenbrand return -EREMOTE; 252724eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 25283c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 252924eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 253071f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 253171f116bfSDavid Hildenbrand return 0; 253271f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 2533fa576c58SThomas Huth } 253471f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 25353fb4c40fSThomas Huth } 25363fb4c40fSThomas Huth 25373fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 25383fb4c40fSThomas Huth { 25393fb4c40fSThomas Huth int rc, exit_reason; 25403fb4c40fSThomas Huth 2541800c1065SThomas Huth /* 2542800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 2543800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 2544800c1065SThomas Huth */ 2545800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 2546800c1065SThomas Huth 2547a76ccff6SThomas Huth do { 25483fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 25493fb4c40fSThomas Huth if (rc) 2550a76ccff6SThomas Huth break; 25513fb4c40fSThomas Huth 2552800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 25533fb4c40fSThomas Huth /* 2554a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 2555a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 25563fb4c40fSThomas Huth */ 25570097d12eSChristian Borntraeger local_irq_disable(); 25580097d12eSChristian Borntraeger __kvm_guest_enter(); 2559db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 25600097d12eSChristian Borntraeger local_irq_enable(); 2561a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 2562a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 25630097d12eSChristian Borntraeger local_irq_disable(); 2564db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 25650097d12eSChristian Borntraeger __kvm_guest_exit(); 25660097d12eSChristian Borntraeger local_irq_enable(); 2567800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 25683fb4c40fSThomas Huth 25693fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 257027291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 25713fb4c40fSThomas Huth 2572800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 2573e168bf8dSCarsten Otte return rc; 2574b0c632dbSHeiko Carstens } 2575b0c632dbSHeiko Carstens 2576b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2577b028ee3eSDavid Hildenbrand { 2578b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 2579b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 2580b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 2581b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 2582b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 2583b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 2584d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 2585d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 2586b028ee3eSDavid Hildenbrand } 2587b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 25884287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 2589b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 2590b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 2591b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 2592b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 2593b028ee3eSDavid Hildenbrand } 2594b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 2595b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 2596b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 2597b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 25989fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 25999fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2600b028ee3eSDavid Hildenbrand } 2601b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 2602b028ee3eSDavid Hildenbrand } 2603b028ee3eSDavid Hildenbrand 2604b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2605b028ee3eSDavid Hildenbrand { 2606b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 2607b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 2608b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 2609b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 26104287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 2611b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 2612b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 2613b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 2614b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 2615b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 2616b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 2617b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 2618b028ee3eSDavid Hildenbrand } 2619b028ee3eSDavid Hildenbrand 2620b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2621b0c632dbSHeiko Carstens { 26228f2abe6aSChristian Borntraeger int rc; 2623b0c632dbSHeiko Carstens sigset_t sigsaved; 2624b0c632dbSHeiko Carstens 262527291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 262627291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 262727291e21SDavid Hildenbrand return 0; 262827291e21SDavid Hildenbrand } 262927291e21SDavid Hildenbrand 2630b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2631b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 2632b0c632dbSHeiko Carstens 26336352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 26346852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 26356352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 2636ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 26376352e4d2SDavid Hildenbrand vcpu->vcpu_id); 26386352e4d2SDavid Hildenbrand return -EINVAL; 26396352e4d2SDavid Hildenbrand } 2640b0c632dbSHeiko Carstens 2641b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 2642db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 2643d7b0b5ebSCarsten Otte 2644dab4079dSHeiko Carstens might_fault(); 2645e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 26469ace903dSChristian Ehrhardt 2647b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 2648b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 26498f2abe6aSChristian Borntraeger rc = -EINTR; 2650b1d16c49SChristian Ehrhardt } 26518f2abe6aSChristian Borntraeger 265227291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 265327291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 265427291e21SDavid Hildenbrand rc = 0; 265527291e21SDavid Hildenbrand } 265627291e21SDavid Hildenbrand 26578f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 265871f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 26598f2abe6aSChristian Borntraeger rc = 0; 26608f2abe6aSChristian Borntraeger } 26618f2abe6aSChristian Borntraeger 2662db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 2663b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 2664d7b0b5ebSCarsten Otte 2665b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2666b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 2667b0c632dbSHeiko Carstens 2668b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 26697e8e6ab4SHeiko Carstens return rc; 2670b0c632dbSHeiko Carstens } 2671b0c632dbSHeiko Carstens 2672b0c632dbSHeiko Carstens /* 2673b0c632dbSHeiko Carstens * store status at address 2674b0c632dbSHeiko Carstens * we use have two special cases: 2675b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 2676b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 2677b0c632dbSHeiko Carstens */ 2678d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 2679b0c632dbSHeiko Carstens { 2680092670cdSCarsten Otte unsigned char archmode = 1; 26819abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 2682fda902cbSMichael Mueller unsigned int px; 26834287f247SDavid Hildenbrand u64 clkcomp, cputm; 2684d0bce605SHeiko Carstens int rc; 2685b0c632dbSHeiko Carstens 2686d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 2687d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 2688d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 2689b0c632dbSHeiko Carstens return -EFAULT; 2690d9a3a09aSMartin Schwidefsky gpa = 0; 2691d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 2692d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 2693b0c632dbSHeiko Carstens return -EFAULT; 2694d9a3a09aSMartin Schwidefsky gpa = px; 2695d9a3a09aSMartin Schwidefsky } else 2696d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 26979abc2a08SDavid Hildenbrand 26989abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 26999abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 27009522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 2701d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 27029abc2a08SDavid Hildenbrand fprs, 128); 27039abc2a08SDavid Hildenbrand } else { 27049abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 27056fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 27069abc2a08SDavid Hildenbrand } 2707d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 2708d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 2709d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 2710d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 2711d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 2712fda902cbSMichael Mueller &px, 4); 2713d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 27149abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 2715d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 2716d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 27174287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 2718d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 27194287f247SDavid Hildenbrand &cputm, 8); 2720178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 2721d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 2722d0bce605SHeiko Carstens &clkcomp, 8); 2723d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 2724d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 2725d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 2726d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 2727d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 2728b0c632dbSHeiko Carstens } 2729b0c632dbSHeiko Carstens 2730e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 2731e879892cSThomas Huth { 2732e879892cSThomas Huth /* 2733e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 2734e879892cSThomas Huth * copying in vcpu load/put. Lets update our copies before we save 2735e879892cSThomas Huth * it into the save area 2736e879892cSThomas Huth */ 2737d0164ee2SHendrik Brueckner save_fpu_regs(); 27389abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 2739e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 2740e879892cSThomas Huth 2741e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 2742e879892cSThomas Huth } 2743e879892cSThomas Huth 2744bc17de7cSEric Farman /* 2745bc17de7cSEric Farman * store additional status at address 2746bc17de7cSEric Farman */ 2747bc17de7cSEric Farman int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu, 2748bc17de7cSEric Farman unsigned long gpa) 2749bc17de7cSEric Farman { 2750bc17de7cSEric Farman /* Only bits 0-53 are used for address formation */ 2751bc17de7cSEric Farman if (!(gpa & ~0x3ff)) 2752bc17de7cSEric Farman return 0; 2753bc17de7cSEric Farman 2754bc17de7cSEric Farman return write_guest_abs(vcpu, gpa & ~0x3ff, 2755bc17de7cSEric Farman (void *)&vcpu->run->s.regs.vrs, 512); 2756bc17de7cSEric Farman } 2757bc17de7cSEric Farman 2758bc17de7cSEric Farman int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr) 2759bc17de7cSEric Farman { 2760bc17de7cSEric Farman if (!test_kvm_facility(vcpu->kvm, 129)) 2761bc17de7cSEric Farman return 0; 2762bc17de7cSEric Farman 2763bc17de7cSEric Farman /* 2764bc17de7cSEric Farman * The guest VXRS are in the host VXRs due to the lazy 27659977e886SHendrik Brueckner * copying in vcpu load/put. We can simply call save_fpu_regs() 27669977e886SHendrik Brueckner * to save the current register state because we are in the 27679977e886SHendrik Brueckner * middle of a load/put cycle. 27689977e886SHendrik Brueckner * 27699977e886SHendrik Brueckner * Let's update our copies before we save it into the save area. 2770bc17de7cSEric Farman */ 2771d0164ee2SHendrik Brueckner save_fpu_regs(); 2772bc17de7cSEric Farman 2773bc17de7cSEric Farman return kvm_s390_store_adtl_status_unloaded(vcpu, addr); 2774bc17de7cSEric Farman } 2775bc17de7cSEric Farman 27768ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 27778ad35755SDavid Hildenbrand { 27788ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 27798e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 27808ad35755SDavid Hildenbrand } 27818ad35755SDavid Hildenbrand 27828ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 27838ad35755SDavid Hildenbrand { 27848ad35755SDavid Hildenbrand unsigned int i; 27858ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 27868ad35755SDavid Hildenbrand 27878ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 27888ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 27898ad35755SDavid Hildenbrand } 27908ad35755SDavid Hildenbrand } 27918ad35755SDavid Hildenbrand 27928ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 27938ad35755SDavid Hildenbrand { 279409a400e7SDavid Hildenbrand if (!sclp.has_ibs) 279509a400e7SDavid Hildenbrand return; 27968ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 27978e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 27988ad35755SDavid Hildenbrand } 27998ad35755SDavid Hildenbrand 28006852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 28016852d7b6SDavid Hildenbrand { 28028ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 28038ad35755SDavid Hildenbrand 28048ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 28058ad35755SDavid Hildenbrand return; 28068ad35755SDavid Hildenbrand 28076852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 28088ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 2809433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 28108ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 28118ad35755SDavid Hildenbrand 28128ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 28138ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 28148ad35755SDavid Hildenbrand started_vcpus++; 28158ad35755SDavid Hildenbrand } 28168ad35755SDavid Hildenbrand 28178ad35755SDavid Hildenbrand if (started_vcpus == 0) { 28188ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 28198ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 28208ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 28218ad35755SDavid Hildenbrand /* 28228ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 28238ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 28248ad35755SDavid Hildenbrand * oustanding ENABLE requests. 28258ad35755SDavid Hildenbrand */ 28268ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 28278ad35755SDavid Hildenbrand } 28288ad35755SDavid Hildenbrand 2829805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 28308ad35755SDavid Hildenbrand /* 28318ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 28328ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 28338ad35755SDavid Hildenbrand */ 2834d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 2835433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 28368ad35755SDavid Hildenbrand return; 28376852d7b6SDavid Hildenbrand } 28386852d7b6SDavid Hildenbrand 28396852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 28406852d7b6SDavid Hildenbrand { 28418ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 28428ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 28438ad35755SDavid Hildenbrand 28448ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 28458ad35755SDavid Hildenbrand return; 28468ad35755SDavid Hildenbrand 28476852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 28488ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 2849433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 28508ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 28518ad35755SDavid Hildenbrand 285232f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 28536cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 285432f5ff63SDavid Hildenbrand 2855805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 28568ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 28578ad35755SDavid Hildenbrand 28588ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 28598ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 28608ad35755SDavid Hildenbrand started_vcpus++; 28618ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 28628ad35755SDavid Hildenbrand } 28638ad35755SDavid Hildenbrand } 28648ad35755SDavid Hildenbrand 28658ad35755SDavid Hildenbrand if (started_vcpus == 1) { 28668ad35755SDavid Hildenbrand /* 28678ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 28688ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 28698ad35755SDavid Hildenbrand */ 28708ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 28718ad35755SDavid Hildenbrand } 28728ad35755SDavid Hildenbrand 2873433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 28748ad35755SDavid Hildenbrand return; 28756852d7b6SDavid Hildenbrand } 28766852d7b6SDavid Hildenbrand 2877d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 2878d6712df9SCornelia Huck struct kvm_enable_cap *cap) 2879d6712df9SCornelia Huck { 2880d6712df9SCornelia Huck int r; 2881d6712df9SCornelia Huck 2882d6712df9SCornelia Huck if (cap->flags) 2883d6712df9SCornelia Huck return -EINVAL; 2884d6712df9SCornelia Huck 2885d6712df9SCornelia Huck switch (cap->cap) { 2886fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 2887fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 2888fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 2889c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 2890fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 2891fa6b7fe9SCornelia Huck } 2892fa6b7fe9SCornelia Huck r = 0; 2893fa6b7fe9SCornelia Huck break; 2894d6712df9SCornelia Huck default: 2895d6712df9SCornelia Huck r = -EINVAL; 2896d6712df9SCornelia Huck break; 2897d6712df9SCornelia Huck } 2898d6712df9SCornelia Huck return r; 2899d6712df9SCornelia Huck } 2900d6712df9SCornelia Huck 290141408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 290241408c28SThomas Huth struct kvm_s390_mem_op *mop) 290341408c28SThomas Huth { 290441408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 290541408c28SThomas Huth void *tmpbuf = NULL; 290641408c28SThomas Huth int r, srcu_idx; 290741408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 290841408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 290941408c28SThomas Huth 291041408c28SThomas Huth if (mop->flags & ~supported_flags) 291141408c28SThomas Huth return -EINVAL; 291241408c28SThomas Huth 291341408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 291441408c28SThomas Huth return -E2BIG; 291541408c28SThomas Huth 291641408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 291741408c28SThomas Huth tmpbuf = vmalloc(mop->size); 291841408c28SThomas Huth if (!tmpbuf) 291941408c28SThomas Huth return -ENOMEM; 292041408c28SThomas Huth } 292141408c28SThomas Huth 292241408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 292341408c28SThomas Huth 292441408c28SThomas Huth switch (mop->op) { 292541408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 292641408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 292792c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 292892c96321SDavid Hildenbrand mop->size, GACC_FETCH); 292941408c28SThomas Huth break; 293041408c28SThomas Huth } 293141408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 293241408c28SThomas Huth if (r == 0) { 293341408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 293441408c28SThomas Huth r = -EFAULT; 293541408c28SThomas Huth } 293641408c28SThomas Huth break; 293741408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 293841408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 293992c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 294092c96321SDavid Hildenbrand mop->size, GACC_STORE); 294141408c28SThomas Huth break; 294241408c28SThomas Huth } 294341408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 294441408c28SThomas Huth r = -EFAULT; 294541408c28SThomas Huth break; 294641408c28SThomas Huth } 294741408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 294841408c28SThomas Huth break; 294941408c28SThomas Huth default: 295041408c28SThomas Huth r = -EINVAL; 295141408c28SThomas Huth } 295241408c28SThomas Huth 295341408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 295441408c28SThomas Huth 295541408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 295641408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 295741408c28SThomas Huth 295841408c28SThomas Huth vfree(tmpbuf); 295941408c28SThomas Huth return r; 296041408c28SThomas Huth } 296141408c28SThomas Huth 2962b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 2963b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2964b0c632dbSHeiko Carstens { 2965b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 2966b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2967800c1065SThomas Huth int idx; 2968bc923cc9SAvi Kivity long r; 2969b0c632dbSHeiko Carstens 297093736624SAvi Kivity switch (ioctl) { 297147b43c52SJens Freimann case KVM_S390_IRQ: { 297247b43c52SJens Freimann struct kvm_s390_irq s390irq; 297347b43c52SJens Freimann 297447b43c52SJens Freimann r = -EFAULT; 297547b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 297647b43c52SJens Freimann break; 297747b43c52SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 297847b43c52SJens Freimann break; 297947b43c52SJens Freimann } 298093736624SAvi Kivity case KVM_S390_INTERRUPT: { 2981ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2982383d0b05SJens Freimann struct kvm_s390_irq s390irq; 2983ba5c1e9bSCarsten Otte 298493736624SAvi Kivity r = -EFAULT; 2985ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 298693736624SAvi Kivity break; 2987383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 2988383d0b05SJens Freimann return -EINVAL; 2989383d0b05SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 299093736624SAvi Kivity break; 2991ba5c1e9bSCarsten Otte } 2992b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 2993800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 2994bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 2995800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 2996bc923cc9SAvi Kivity break; 2997b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 2998b0c632dbSHeiko Carstens psw_t psw; 2999b0c632dbSHeiko Carstens 3000bc923cc9SAvi Kivity r = -EFAULT; 3001b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 3002bc923cc9SAvi Kivity break; 3003bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 3004bc923cc9SAvi Kivity break; 3005b0c632dbSHeiko Carstens } 3006b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 3007bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 3008bc923cc9SAvi Kivity break; 300914eebd91SCarsten Otte case KVM_SET_ONE_REG: 301014eebd91SCarsten Otte case KVM_GET_ONE_REG: { 301114eebd91SCarsten Otte struct kvm_one_reg reg; 301214eebd91SCarsten Otte r = -EFAULT; 301314eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 301414eebd91SCarsten Otte break; 301514eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 301614eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 301714eebd91SCarsten Otte else 301814eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 301914eebd91SCarsten Otte break; 302014eebd91SCarsten Otte } 302127e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 302227e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 302327e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 302427e0393fSCarsten Otte 302527e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 302627e0393fSCarsten Otte r = -EFAULT; 302727e0393fSCarsten Otte break; 302827e0393fSCarsten Otte } 302927e0393fSCarsten Otte 303027e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 303127e0393fSCarsten Otte r = -EINVAL; 303227e0393fSCarsten Otte break; 303327e0393fSCarsten Otte } 303427e0393fSCarsten Otte 303527e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 303627e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 303727e0393fSCarsten Otte break; 303827e0393fSCarsten Otte } 303927e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 304027e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 304127e0393fSCarsten Otte 304227e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 304327e0393fSCarsten Otte r = -EFAULT; 304427e0393fSCarsten Otte break; 304527e0393fSCarsten Otte } 304627e0393fSCarsten Otte 304727e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 304827e0393fSCarsten Otte r = -EINVAL; 304927e0393fSCarsten Otte break; 305027e0393fSCarsten Otte } 305127e0393fSCarsten Otte 305227e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 305327e0393fSCarsten Otte ucasmap.length); 305427e0393fSCarsten Otte break; 305527e0393fSCarsten Otte } 305627e0393fSCarsten Otte #endif 3057ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 3058527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 3059ccc7910fSCarsten Otte break; 3060ccc7910fSCarsten Otte } 3061d6712df9SCornelia Huck case KVM_ENABLE_CAP: 3062d6712df9SCornelia Huck { 3063d6712df9SCornelia Huck struct kvm_enable_cap cap; 3064d6712df9SCornelia Huck r = -EFAULT; 3065d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 3066d6712df9SCornelia Huck break; 3067d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 3068d6712df9SCornelia Huck break; 3069d6712df9SCornelia Huck } 307041408c28SThomas Huth case KVM_S390_MEM_OP: { 307141408c28SThomas Huth struct kvm_s390_mem_op mem_op; 307241408c28SThomas Huth 307341408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 307441408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 307541408c28SThomas Huth else 307641408c28SThomas Huth r = -EFAULT; 307741408c28SThomas Huth break; 307841408c28SThomas Huth } 3079816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 3080816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3081816c7667SJens Freimann 3082816c7667SJens Freimann r = -EFAULT; 3083816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3084816c7667SJens Freimann break; 3085816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 3086816c7667SJens Freimann irq_state.len == 0 || 3087816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 3088816c7667SJens Freimann r = -EINVAL; 3089816c7667SJens Freimann break; 3090816c7667SJens Freimann } 3091816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 3092816c7667SJens Freimann (void __user *) irq_state.buf, 3093816c7667SJens Freimann irq_state.len); 3094816c7667SJens Freimann break; 3095816c7667SJens Freimann } 3096816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 3097816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3098816c7667SJens Freimann 3099816c7667SJens Freimann r = -EFAULT; 3100816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3101816c7667SJens Freimann break; 3102816c7667SJens Freimann if (irq_state.len == 0) { 3103816c7667SJens Freimann r = -EINVAL; 3104816c7667SJens Freimann break; 3105816c7667SJens Freimann } 3106816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 3107816c7667SJens Freimann (__u8 __user *) irq_state.buf, 3108816c7667SJens Freimann irq_state.len); 3109816c7667SJens Freimann break; 3110816c7667SJens Freimann } 3111b0c632dbSHeiko Carstens default: 31123e6afcf1SCarsten Otte r = -ENOTTY; 3113b0c632dbSHeiko Carstens } 3114bc923cc9SAvi Kivity return r; 3115b0c632dbSHeiko Carstens } 3116b0c632dbSHeiko Carstens 31175b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 31185b1c1493SCarsten Otte { 31195b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 31205b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 31215b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 31225b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 31235b1c1493SCarsten Otte get_page(vmf->page); 31245b1c1493SCarsten Otte return 0; 31255b1c1493SCarsten Otte } 31265b1c1493SCarsten Otte #endif 31275b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 31285b1c1493SCarsten Otte } 31295b1c1493SCarsten Otte 31305587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 31315587027cSAneesh Kumar K.V unsigned long npages) 3132db3fe4ebSTakuya Yoshikawa { 3133db3fe4ebSTakuya Yoshikawa return 0; 3134db3fe4ebSTakuya Yoshikawa } 3135db3fe4ebSTakuya Yoshikawa 3136b0c632dbSHeiko Carstens /* Section: memory related */ 3137f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 3138f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 313909170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 31407b6195a9STakuya Yoshikawa enum kvm_mr_change change) 3141b0c632dbSHeiko Carstens { 3142dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 3143dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 3144dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 3145dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 3146b0c632dbSHeiko Carstens 3147598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 3148b0c632dbSHeiko Carstens return -EINVAL; 3149b0c632dbSHeiko Carstens 3150598841caSCarsten Otte if (mem->memory_size & 0xffffful) 3151b0c632dbSHeiko Carstens return -EINVAL; 3152b0c632dbSHeiko Carstens 3153a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 3154a3a92c31SDominik Dingel return -EINVAL; 3155a3a92c31SDominik Dingel 3156f7784b8eSMarcelo Tosatti return 0; 3157f7784b8eSMarcelo Tosatti } 3158f7784b8eSMarcelo Tosatti 3159f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 316009170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 31618482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 3162f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 31638482644aSTakuya Yoshikawa enum kvm_mr_change change) 3164f7784b8eSMarcelo Tosatti { 3165f7850c92SCarsten Otte int rc; 3166f7784b8eSMarcelo Tosatti 31672cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 31682cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 31692cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 31702cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 31712cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 31722cef4debSChristian Borntraeger */ 31732cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 31742cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 31752cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 31762cef4debSChristian Borntraeger return; 3177598841caSCarsten Otte 3178598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 3179598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 3180598841caSCarsten Otte if (rc) 3181ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 3182598841caSCarsten Otte return; 3183b0c632dbSHeiko Carstens } 3184b0c632dbSHeiko Carstens 318560a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 318660a37709SAlexander Yarygin { 318760a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 318860a37709SAlexander Yarygin 318960a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 319060a37709SAlexander Yarygin } 319160a37709SAlexander Yarygin 31923491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 31933491caf2SChristian Borntraeger { 31943491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 31953491caf2SChristian Borntraeger } 31963491caf2SChristian Borntraeger 3197b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 3198b0c632dbSHeiko Carstens { 319960a37709SAlexander Yarygin int i; 320060a37709SAlexander Yarygin 320107197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 320207197fd0SDavid Hildenbrand pr_info("SIE not available\n"); 320307197fd0SDavid Hildenbrand return -ENODEV; 320407197fd0SDavid Hildenbrand } 320507197fd0SDavid Hildenbrand 320660a37709SAlexander Yarygin for (i = 0; i < 16; i++) 320760a37709SAlexander Yarygin kvm_s390_fac_list_mask[i] |= 320860a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 320960a37709SAlexander Yarygin 32109d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 3211b0c632dbSHeiko Carstens } 3212b0c632dbSHeiko Carstens 3213b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 3214b0c632dbSHeiko Carstens { 3215b0c632dbSHeiko Carstens kvm_exit(); 3216b0c632dbSHeiko Carstens } 3217b0c632dbSHeiko Carstens 3218b0c632dbSHeiko Carstens module_init(kvm_s390_init); 3219b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 3220566af940SCornelia Huck 3221566af940SCornelia Huck /* 3222566af940SCornelia Huck * Enable autoloading of the kvm module. 3223566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 3224566af940SCornelia Huck * since x86 takes a different approach. 3225566af940SCornelia Huck */ 3226566af940SCornelia Huck #include <linux/miscdevice.h> 3227566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 3228566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 3229