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> 24b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 25b0c632dbSHeiko Carstens #include <linux/module.h> 26d3217967SPaul Gortmaker #include <linux/moduleparam.h> 27a374e892STony Krowiak #include <linux/random.h> 28b0c632dbSHeiko Carstens #include <linux/slab.h> 29ba5c1e9bSCarsten Otte #include <linux/timer.h> 3041408c28SThomas Huth #include <linux/vmalloc.h> 3115c9705fSDavid Hildenbrand #include <linux/bitmap.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33190df4a2SClaudio Imbrenda #include <linux/string.h> 34174cd4b1SIngo Molnar 35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 36b0c632dbSHeiko Carstens #include <asm/lowcore.h> 37fd5ada04SMartin Schwidefsky #include <asm/stp.h> 38b0c632dbSHeiko Carstens #include <asm/pgtable.h> 391e133ab2SMartin Schwidefsky #include <asm/gmap.h> 40f5daba1dSHeiko Carstens #include <asm/nmi.h> 41a0616cdeSDavid Howells #include <asm/switch_to.h> 426d3da241SJens Freimann #include <asm/isc.h> 431526bf9cSChristian Borntraeger #include <asm/sclp.h> 440a763c78SDavid Hildenbrand #include <asm/cpacf.h> 45221bb8a4SLinus Torvalds #include <asm/timex.h> 468f2abe6aSChristian Borntraeger #include "kvm-s390.h" 47b0c632dbSHeiko Carstens #include "gaccess.h" 48b0c632dbSHeiko Carstens 49ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390" 50ea2cdd27SDavid Hildenbrand #undef pr_fmt 51ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 52ea2cdd27SDavid Hildenbrand 535786fffaSCornelia Huck #define CREATE_TRACE_POINTS 545786fffaSCornelia Huck #include "trace.h" 55ade38c31SCornelia Huck #include "trace-s390.h" 565786fffaSCornelia Huck 5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 58816c7667SJens Freimann #define LOCAL_IRQS 32 59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 60816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 6141408c28SThomas Huth 62b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 63b0c632dbSHeiko Carstens 64b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 65b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 660eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 678f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 688f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 698f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 708f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 71ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 729ec6de19SAlexander Yarygin { "exit_pei", VCPU_STAT(exit_pei) }, 73ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 74ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 75a011eeb2SJanosch Frank { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, 76f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 7762bea5bfSPaolo Bonzini { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 783491caf2SChristian Borntraeger { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 79ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 80f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 81ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 82aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 83aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 84ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 857697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 86ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 87ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 88ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 89ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 90ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 91ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 92ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 9369d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 94453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 95453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 96453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 97453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 98453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 998a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 100453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 101453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 102b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 103453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 104453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 105bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 10695ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 107a3508fbeSDavid Hildenbrand { "instruction_sie", VCPU_STAT(instruction_sie) }, 1085288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 109bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1107697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1115288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 11242cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 11342cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1145288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 11542cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 11642cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 117cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1185288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1195288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1205288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 12142cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 12242cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 12342cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 124388186bcSChristian Borntraeger { "diagnose_10", VCPU_STAT(diagnose_10) }, 125e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 12641628d33SKonstantin Weitz { "diagnose_9c", VCPU_STAT(diagnose_9c) }, 127175a5c9eSChristian Borntraeger { "diagnose_258", VCPU_STAT(diagnose_258) }, 128175a5c9eSChristian Borntraeger { "diagnose_308", VCPU_STAT(diagnose_308) }, 129175a5c9eSChristian Borntraeger { "diagnose_500", VCPU_STAT(diagnose_500) }, 130b0c632dbSHeiko Carstens { NULL } 131b0c632dbSHeiko Carstens }; 132b0c632dbSHeiko Carstens 133a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 134a411edf1SDavid Hildenbrand static int nested; 135a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 136a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 137a411edf1SDavid Hildenbrand 1389d8d5786SMichael Mueller /* upper facilities limit for kvm */ 139f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM }; 140b0c632dbSHeiko Carstens 1419d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void) 14278c4b59fSMichael Mueller { 1439d8d5786SMichael Mueller BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64); 1449d8d5786SMichael Mueller return ARRAY_SIZE(kvm_s390_fac_list_mask); 14578c4b59fSMichael Mueller } 14678c4b59fSMichael Mueller 14715c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 14815c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 1490a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 1500a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 15115c9705fSDavid Hildenbrand 1529d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 153a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 15478f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 1559d8d5786SMichael Mueller 156b0c632dbSHeiko Carstens /* Section: not file related */ 15713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 158b0c632dbSHeiko Carstens { 159b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 16010474ae8SAlexander Graf return 0; 161b0c632dbSHeiko Carstens } 162b0c632dbSHeiko Carstens 163414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 164414d3b07SMartin Schwidefsky unsigned long end); 1652c70fe44SChristian Borntraeger 166fdf03650SFan Zhang /* 167fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 168fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 169fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 170fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 171fdf03650SFan Zhang */ 172fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 173fdf03650SFan Zhang void *v) 174fdf03650SFan Zhang { 175fdf03650SFan Zhang struct kvm *kvm; 176fdf03650SFan Zhang struct kvm_vcpu *vcpu; 177fdf03650SFan Zhang int i; 178fdf03650SFan Zhang unsigned long long *delta = v; 179fdf03650SFan Zhang 180fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 181fdf03650SFan Zhang kvm->arch.epoch -= *delta; 182fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 183fdf03650SFan Zhang vcpu->arch.sie_block->epoch -= *delta; 184db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 185db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 18691473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 18791473b48SDavid Hildenbrand vcpu->arch.vsie_block->epoch -= *delta; 188fdf03650SFan Zhang } 189fdf03650SFan Zhang } 190fdf03650SFan Zhang return NOTIFY_OK; 191fdf03650SFan Zhang } 192fdf03650SFan Zhang 193fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 194fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 195fdf03650SFan Zhang }; 196fdf03650SFan Zhang 197b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 198b0c632dbSHeiko Carstens { 1992c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 200b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 201a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 202a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 203fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 204fdf03650SFan Zhang &kvm_clock_notifier); 205b0c632dbSHeiko Carstens return 0; 206b0c632dbSHeiko Carstens } 207b0c632dbSHeiko Carstens 208b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 209b0c632dbSHeiko Carstens { 210b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 211a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 212fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 213fdf03650SFan Zhang &kvm_clock_notifier); 214b0c632dbSHeiko Carstens } 215b0c632dbSHeiko Carstens 21622be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 21722be5a13SDavid Hildenbrand { 21822be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 21922be5a13SDavid Hildenbrand } 22022be5a13SDavid Hildenbrand 2210a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 2220a763c78SDavid Hildenbrand { 2230a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 224d051ae53SHeiko Carstens int cc; 2250a763c78SDavid Hildenbrand 2260a763c78SDavid Hildenbrand asm volatile( 2270a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 2280a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 2290a763c78SDavid Hildenbrand " ipm %0\n" 2300a763c78SDavid Hildenbrand " srl %0,28\n" 2310a763c78SDavid Hildenbrand : "=d" (cc) 2320a763c78SDavid Hildenbrand : "d" (r0) 2330a763c78SDavid Hildenbrand : "cc"); 2340a763c78SDavid Hildenbrand return cc == 0; 2350a763c78SDavid Hildenbrand } 2360a763c78SDavid Hildenbrand 23722be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 23822be5a13SDavid Hildenbrand { 2390a763c78SDavid Hildenbrand int i; 2400a763c78SDavid Hildenbrand 2410a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 2420a763c78SDavid Hildenbrand if (plo_test_bit(i)) 2430a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 2440a763c78SDavid Hildenbrand } 2450a763c78SDavid Hildenbrand 2460a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 247221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 248221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 249221bb8a4SLinus Torvalds PTFF_QAF); 2500a763c78SDavid Hildenbrand 2510a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 25269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 25369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 25469c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 25569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 25669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 25769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 25869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 25969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 26069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 26169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 2620a763c78SDavid Hildenbrand } 2630a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 26469c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 26569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 2660a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 26769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 26869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 26969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 27069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 27169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 27269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 27369c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 27469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 2750a763c78SDavid Hildenbrand } 2760a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 277985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 27869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 2790a763c78SDavid Hildenbrand 280e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 281e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 282e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 283e000b8e0SJason J. Herne 28422be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 28522be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 286a3508fbeSDavid Hildenbrand /* 287a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 288a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 289a3508fbeSDavid Hildenbrand */ 290a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 291a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 292a3508fbeSDavid Hildenbrand return; 293a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 29419c439b5SDavid Hildenbrand if (sclp.has_64bscao) 29519c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 2960615a326SDavid Hildenbrand if (sclp.has_siif) 2970615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 29877d18f6dSDavid Hildenbrand if (sclp.has_gpere) 29977d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 300a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 301a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 3025630a8e8SDavid Hildenbrand if (sclp.has_ib) 3035630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 30413ee3f67SDavid Hildenbrand if (sclp.has_cei) 30513ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 3067fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 3077fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 308730cd632SFarhan Ali if (sclp.has_kss) 309730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 3105d3876a8SDavid Hildenbrand /* 3115d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 3125d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 3135d3876a8SDavid Hildenbrand * instead of the real storage key. 3145d3876a8SDavid Hildenbrand * 3155d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 3165d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 3175d3876a8SDavid Hildenbrand * 3185d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 3195d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 3205d3876a8SDavid Hildenbrand * 3215d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 3225d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 3235d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 3245d3876a8SDavid Hildenbrand * 3255d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 3265d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 3275d3876a8SDavid Hildenbrand */ 32822be5a13SDavid Hildenbrand } 32922be5a13SDavid Hildenbrand 330b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 331b0c632dbSHeiko Carstens { 33278f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 33378f26131SChristian Borntraeger if (!kvm_s390_dbf) 33478f26131SChristian Borntraeger return -ENOMEM; 33578f26131SChristian Borntraeger 33678f26131SChristian Borntraeger if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { 33778f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 33878f26131SChristian Borntraeger return -ENOMEM; 33978f26131SChristian Borntraeger } 34078f26131SChristian Borntraeger 34122be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 34222be5a13SDavid Hildenbrand 34384877d93SCornelia Huck /* Register floating interrupt controller interface. */ 34484877d93SCornelia Huck return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 345b0c632dbSHeiko Carstens } 346b0c632dbSHeiko Carstens 34778f26131SChristian Borntraeger void kvm_arch_exit(void) 34878f26131SChristian Borntraeger { 34978f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 35078f26131SChristian Borntraeger } 35178f26131SChristian Borntraeger 352b0c632dbSHeiko Carstens /* Section: device related */ 353b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 354b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 355b0c632dbSHeiko Carstens { 356b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 357b0c632dbSHeiko Carstens return s390_enable_sie(); 358b0c632dbSHeiko Carstens return -EINVAL; 359b0c632dbSHeiko Carstens } 360b0c632dbSHeiko Carstens 361784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 362b0c632dbSHeiko Carstens { 363d7b0b5ebSCarsten Otte int r; 364d7b0b5ebSCarsten Otte 3652bd0ac4eSCarsten Otte switch (ext) { 366d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 367b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 36852e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 3691efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3701efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 3711efd0f59SCarsten Otte #endif 3723c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 37360b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 37414eebd91SCarsten Otte case KVM_CAP_ONE_REG: 375d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 376fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 37710ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 378c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 379d938dc55SCornelia Huck case KVM_CAP_ENABLE_CAP_VM: 38078599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 381f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 3826352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 383460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 38447b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 3852444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 386e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 38730ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 388816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 3896502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 3904036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 39147a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 392d7b0b5ebSCarsten Otte r = 1; 393d7b0b5ebSCarsten Otte break; 39441408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 39541408c28SThomas Huth r = MEM_OP_MAX_SIZE; 39641408c28SThomas Huth break; 397e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 398e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 39976a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 400a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 401a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 402a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 40376a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 404e726b1bdSChristian Borntraeger break; 405e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 406e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 407e1e2e605SNick Wang break; 4081526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 409abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 4101526bf9cSChristian Borntraeger break; 41168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 41268c55750SEric Farman r = MACHINE_HAS_VX; 41368c55750SEric Farman break; 414c6e5f166SFan Zhang case KVM_CAP_S390_RI: 415c6e5f166SFan Zhang r = test_facility(64); 416c6e5f166SFan Zhang break; 4174e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 4184e0b1ab7SFan Zhang r = test_facility(133); 4194e0b1ab7SFan Zhang break; 4202bd0ac4eSCarsten Otte default: 421d7b0b5ebSCarsten Otte r = 0; 422b0c632dbSHeiko Carstens } 423d7b0b5ebSCarsten Otte return r; 4242bd0ac4eSCarsten Otte } 425b0c632dbSHeiko Carstens 42615f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 42715f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 42815f36ebdSJason J. Herne { 42915f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 43015f36ebdSJason J. Herne unsigned long address; 43115f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 43215f36ebdSJason J. Herne 43315f36ebdSJason J. Herne /* Loop over all guest pages */ 43415f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 43515f36ebdSJason J. Herne for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) { 43615f36ebdSJason J. Herne address = gfn_to_hva_memslot(memslot, cur_gfn); 43715f36ebdSJason J. Herne 4381e133ab2SMartin Schwidefsky if (test_and_clear_guest_dirty(gmap->mm, address)) 43915f36ebdSJason J. Herne mark_page_dirty(kvm, cur_gfn); 4401763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 4411763f8d0SChristian Borntraeger return; 44270c88a00SChristian Borntraeger cond_resched(); 44315f36ebdSJason J. Herne } 44415f36ebdSJason J. Herne } 44515f36ebdSJason J. Herne 446b0c632dbSHeiko Carstens /* Section: vm related */ 447a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 448a6e2f683SEugene (jno) Dvurechenski 449b0c632dbSHeiko Carstens /* 450b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 451b0c632dbSHeiko Carstens */ 452b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 453b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 454b0c632dbSHeiko Carstens { 45515f36ebdSJason J. Herne int r; 45615f36ebdSJason J. Herne unsigned long n; 4579f6b8029SPaolo Bonzini struct kvm_memslots *slots; 45815f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 45915f36ebdSJason J. Herne int is_dirty = 0; 46015f36ebdSJason J. Herne 461e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 462e1e8a962SJanosch Frank return -EINVAL; 463e1e8a962SJanosch Frank 46415f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 46515f36ebdSJason J. Herne 46615f36ebdSJason J. Herne r = -EINVAL; 46715f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 46815f36ebdSJason J. Herne goto out; 46915f36ebdSJason J. Herne 4709f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 4719f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 47215f36ebdSJason J. Herne r = -ENOENT; 47315f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 47415f36ebdSJason J. Herne goto out; 47515f36ebdSJason J. Herne 47615f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 47715f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 47815f36ebdSJason J. Herne if (r) 47915f36ebdSJason J. Herne goto out; 48015f36ebdSJason J. Herne 48115f36ebdSJason J. Herne /* Clear the dirty log */ 48215f36ebdSJason J. Herne if (is_dirty) { 48315f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 48415f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 48515f36ebdSJason J. Herne } 48615f36ebdSJason J. Herne r = 0; 48715f36ebdSJason J. Herne out: 48815f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 48915f36ebdSJason J. Herne return r; 490b0c632dbSHeiko Carstens } 491b0c632dbSHeiko Carstens 4926502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 4936502a34cSDavid Hildenbrand { 4946502a34cSDavid Hildenbrand unsigned int i; 4956502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 4966502a34cSDavid Hildenbrand 4976502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 4986502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 4996502a34cSDavid Hildenbrand } 5006502a34cSDavid Hildenbrand } 5016502a34cSDavid Hildenbrand 502d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 503d938dc55SCornelia Huck { 504d938dc55SCornelia Huck int r; 505d938dc55SCornelia Huck 506d938dc55SCornelia Huck if (cap->flags) 507d938dc55SCornelia Huck return -EINVAL; 508d938dc55SCornelia Huck 509d938dc55SCornelia Huck switch (cap->cap) { 51084223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 511c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 51284223598SCornelia Huck kvm->arch.use_irqchip = 1; 51384223598SCornelia Huck r = 0; 51484223598SCornelia Huck break; 5152444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 516c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 5172444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 5182444b352SDavid Hildenbrand r = 0; 5192444b352SDavid Hildenbrand break; 52068c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 5215967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 522a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 5235967c17bSDavid Hildenbrand r = -EBUSY; 5245967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 525c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 526c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 5272f87d942SGuenther Hutzl if (test_facility(134)) { 5282f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 5292f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 5302f87d942SGuenther Hutzl } 53153743aa7SMaxim Samoylov if (test_facility(135)) { 53253743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 53353743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 53453743aa7SMaxim Samoylov } 53518280d8bSMichael Mueller r = 0; 53618280d8bSMichael Mueller } else 53718280d8bSMichael Mueller r = -EINVAL; 5385967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 539c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 540c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 54168c55750SEric Farman break; 542c6e5f166SFan Zhang case KVM_CAP_S390_RI: 543c6e5f166SFan Zhang r = -EINVAL; 544c6e5f166SFan Zhang mutex_lock(&kvm->lock); 545a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 546c6e5f166SFan Zhang r = -EBUSY; 547c6e5f166SFan Zhang } else if (test_facility(64)) { 548c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 549c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 550c6e5f166SFan Zhang r = 0; 551c6e5f166SFan Zhang } 552c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 553c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 554c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 555c6e5f166SFan Zhang break; 55647a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 55747a4693eSYi Min Zhao mutex_lock(&kvm->lock); 55847a4693eSYi Min Zhao if (kvm->created_vcpus) { 55947a4693eSYi Min Zhao r = -EBUSY; 56047a4693eSYi Min Zhao } else { 56147a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 56247a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 56347a4693eSYi Min Zhao r = 0; 56447a4693eSYi Min Zhao } 56547a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 56647a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 56747a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 56847a4693eSYi Min Zhao break; 5694e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 5704e0b1ab7SFan Zhang r = -EINVAL; 5714e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 5724e0b1ab7SFan Zhang if (atomic_read(&kvm->online_vcpus)) { 5734e0b1ab7SFan Zhang r = -EBUSY; 5744e0b1ab7SFan Zhang } else if (test_facility(133)) { 5754e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 5764e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 5774e0b1ab7SFan Zhang r = 0; 5784e0b1ab7SFan Zhang } 5794e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 5804e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 5814e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 5824e0b1ab7SFan Zhang break; 583e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 584c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 585e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 586e44fc8c9SEkaterina Tumanova r = 0; 587e44fc8c9SEkaterina Tumanova break; 5886502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5896502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 5906502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 5916502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 5926502a34cSDavid Hildenbrand r = 0; 5936502a34cSDavid Hildenbrand break; 594d938dc55SCornelia Huck default: 595d938dc55SCornelia Huck r = -EINVAL; 596d938dc55SCornelia Huck break; 597d938dc55SCornelia Huck } 598d938dc55SCornelia Huck return r; 599d938dc55SCornelia Huck } 600d938dc55SCornelia Huck 6018c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 6028c0a7ce6SDominik Dingel { 6038c0a7ce6SDominik Dingel int ret; 6048c0a7ce6SDominik Dingel 6058c0a7ce6SDominik Dingel switch (attr->attr) { 6068c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 6078c0a7ce6SDominik Dingel ret = 0; 608c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 609a3a92c31SDominik Dingel kvm->arch.mem_limit); 610a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 6118c0a7ce6SDominik Dingel ret = -EFAULT; 6128c0a7ce6SDominik Dingel break; 6138c0a7ce6SDominik Dingel default: 6148c0a7ce6SDominik Dingel ret = -ENXIO; 6158c0a7ce6SDominik Dingel break; 6168c0a7ce6SDominik Dingel } 6178c0a7ce6SDominik Dingel return ret; 6188c0a7ce6SDominik Dingel } 6198c0a7ce6SDominik Dingel 6208c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 6214f718eabSDominik Dingel { 6224f718eabSDominik Dingel int ret; 6234f718eabSDominik Dingel unsigned int idx; 6244f718eabSDominik Dingel switch (attr->attr) { 6254f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 626f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 627c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 628e6db1d61SDominik Dingel break; 629e6db1d61SDominik Dingel 6304f718eabSDominik Dingel ret = -EBUSY; 631c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 6324f718eabSDominik Dingel mutex_lock(&kvm->lock); 633a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 6344f718eabSDominik Dingel kvm->arch.use_cmma = 1; 6354f718eabSDominik Dingel ret = 0; 6364f718eabSDominik Dingel } 6374f718eabSDominik Dingel mutex_unlock(&kvm->lock); 6384f718eabSDominik Dingel break; 6394f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 640f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 641f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 642f9cbd9b0SDavid Hildenbrand break; 643c3489155SDominik Dingel ret = -EINVAL; 644c3489155SDominik Dingel if (!kvm->arch.use_cmma) 645c3489155SDominik Dingel break; 646c3489155SDominik Dingel 647c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 6484f718eabSDominik Dingel mutex_lock(&kvm->lock); 6494f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 650a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 6514f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 6524f718eabSDominik Dingel mutex_unlock(&kvm->lock); 6534f718eabSDominik Dingel ret = 0; 6544f718eabSDominik Dingel break; 6558c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 6568c0a7ce6SDominik Dingel unsigned long new_limit; 6578c0a7ce6SDominik Dingel 6588c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 6598c0a7ce6SDominik Dingel return -EINVAL; 6608c0a7ce6SDominik Dingel 6618c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 6628c0a7ce6SDominik Dingel return -EFAULT; 6638c0a7ce6SDominik Dingel 664a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 665a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 6668c0a7ce6SDominik Dingel return -E2BIG; 6678c0a7ce6SDominik Dingel 668a3a92c31SDominik Dingel if (!new_limit) 669a3a92c31SDominik Dingel return -EINVAL; 670a3a92c31SDominik Dingel 6716ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 672a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 673a3a92c31SDominik Dingel new_limit -= 1; 674a3a92c31SDominik Dingel 6758c0a7ce6SDominik Dingel ret = -EBUSY; 6768c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 677a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 6786ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 6796ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 6808c0a7ce6SDominik Dingel 6818c0a7ce6SDominik Dingel if (!new) { 6828c0a7ce6SDominik Dingel ret = -ENOMEM; 6838c0a7ce6SDominik Dingel } else { 6846ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 6858c0a7ce6SDominik Dingel new->private = kvm; 6868c0a7ce6SDominik Dingel kvm->arch.gmap = new; 6878c0a7ce6SDominik Dingel ret = 0; 6888c0a7ce6SDominik Dingel } 6898c0a7ce6SDominik Dingel } 6908c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 691a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 692a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 693a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 6948c0a7ce6SDominik Dingel break; 6958c0a7ce6SDominik Dingel } 6964f718eabSDominik Dingel default: 6974f718eabSDominik Dingel ret = -ENXIO; 6984f718eabSDominik Dingel break; 6994f718eabSDominik Dingel } 7004f718eabSDominik Dingel return ret; 7014f718eabSDominik Dingel } 7024f718eabSDominik Dingel 703a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 704a374e892STony Krowiak 705a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 706a374e892STony Krowiak { 707a374e892STony Krowiak struct kvm_vcpu *vcpu; 708a374e892STony Krowiak int i; 709a374e892STony Krowiak 7109d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 711a374e892STony Krowiak return -EINVAL; 712a374e892STony Krowiak 713a374e892STony Krowiak mutex_lock(&kvm->lock); 714a374e892STony Krowiak switch (attr->attr) { 715a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 716a374e892STony Krowiak get_random_bytes( 717a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 718a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 719a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 720c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 721a374e892STony Krowiak break; 722a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 723a374e892STony Krowiak get_random_bytes( 724a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 725a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 726a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 727c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 728a374e892STony Krowiak break; 729a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 730a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 731a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 732a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 733c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 734a374e892STony Krowiak break; 735a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 736a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 737a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 738a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 739c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 740a374e892STony Krowiak break; 741a374e892STony Krowiak default: 742a374e892STony Krowiak mutex_unlock(&kvm->lock); 743a374e892STony Krowiak return -ENXIO; 744a374e892STony Krowiak } 745a374e892STony Krowiak 746a374e892STony Krowiak kvm_for_each_vcpu(i, vcpu, kvm) { 747a374e892STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 748a374e892STony Krowiak exit_sie(vcpu); 749a374e892STony Krowiak } 750a374e892STony Krowiak mutex_unlock(&kvm->lock); 751a374e892STony Krowiak return 0; 752a374e892STony Krowiak } 753a374e892STony Krowiak 754190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 755190df4a2SClaudio Imbrenda { 756190df4a2SClaudio Imbrenda int cx; 757190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 758190df4a2SClaudio Imbrenda 759190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 760190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 761190df4a2SClaudio Imbrenda } 762190df4a2SClaudio Imbrenda 763190df4a2SClaudio Imbrenda /* 764190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 765190df4a2SClaudio Imbrenda * kvm->lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 766190df4a2SClaudio Imbrenda */ 767190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 768190df4a2SClaudio Imbrenda { 769190df4a2SClaudio Imbrenda struct kvm_s390_migration_state *mgs; 770190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 771190df4a2SClaudio Imbrenda /* should be the only one */ 772190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 773190df4a2SClaudio Imbrenda unsigned long ram_pages; 774190df4a2SClaudio Imbrenda int slotnr; 775190df4a2SClaudio Imbrenda 776190df4a2SClaudio Imbrenda /* migration mode already enabled */ 777190df4a2SClaudio Imbrenda if (kvm->arch.migration_state) 778190df4a2SClaudio Imbrenda return 0; 779190df4a2SClaudio Imbrenda 780190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 781190df4a2SClaudio Imbrenda if (!slots || !slots->used_slots) 782190df4a2SClaudio Imbrenda return -EINVAL; 783190df4a2SClaudio Imbrenda 784190df4a2SClaudio Imbrenda mgs = kzalloc(sizeof(*mgs), GFP_KERNEL); 785190df4a2SClaudio Imbrenda if (!mgs) 786190df4a2SClaudio Imbrenda return -ENOMEM; 787190df4a2SClaudio Imbrenda kvm->arch.migration_state = mgs; 788190df4a2SClaudio Imbrenda 789190df4a2SClaudio Imbrenda if (kvm->arch.use_cmma) { 790190df4a2SClaudio Imbrenda /* 791190df4a2SClaudio Imbrenda * Get the last slot. They should be sorted by base_gfn, so the 792190df4a2SClaudio Imbrenda * last slot is also the one at the end of the address space. 793190df4a2SClaudio Imbrenda * We have verified above that at least one slot is present. 794190df4a2SClaudio Imbrenda */ 795190df4a2SClaudio Imbrenda ms = slots->memslots + slots->used_slots - 1; 796190df4a2SClaudio Imbrenda /* round up so we only use full longs */ 797190df4a2SClaudio Imbrenda ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG); 798190df4a2SClaudio Imbrenda /* allocate enough bytes to store all the bits */ 799190df4a2SClaudio Imbrenda mgs->pgste_bitmap = vmalloc(ram_pages / 8); 800190df4a2SClaudio Imbrenda if (!mgs->pgste_bitmap) { 801190df4a2SClaudio Imbrenda kfree(mgs); 802190df4a2SClaudio Imbrenda kvm->arch.migration_state = NULL; 803190df4a2SClaudio Imbrenda return -ENOMEM; 804190df4a2SClaudio Imbrenda } 805190df4a2SClaudio Imbrenda 806190df4a2SClaudio Imbrenda mgs->bitmap_size = ram_pages; 807190df4a2SClaudio Imbrenda atomic64_set(&mgs->dirty_pages, ram_pages); 808190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 809190df4a2SClaudio Imbrenda for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { 810190df4a2SClaudio Imbrenda ms = slots->memslots + slotnr; 811190df4a2SClaudio Imbrenda bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages); 812190df4a2SClaudio Imbrenda } 813190df4a2SClaudio Imbrenda 814190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 815190df4a2SClaudio Imbrenda } 816190df4a2SClaudio Imbrenda return 0; 817190df4a2SClaudio Imbrenda } 818190df4a2SClaudio Imbrenda 819190df4a2SClaudio Imbrenda /* 820190df4a2SClaudio Imbrenda * Must be called with kvm->lock to avoid races with ourselves and 821190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 822190df4a2SClaudio Imbrenda */ 823190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 824190df4a2SClaudio Imbrenda { 825190df4a2SClaudio Imbrenda struct kvm_s390_migration_state *mgs; 826190df4a2SClaudio Imbrenda 827190df4a2SClaudio Imbrenda /* migration mode already disabled */ 828190df4a2SClaudio Imbrenda if (!kvm->arch.migration_state) 829190df4a2SClaudio Imbrenda return 0; 830190df4a2SClaudio Imbrenda mgs = kvm->arch.migration_state; 831190df4a2SClaudio Imbrenda kvm->arch.migration_state = NULL; 832190df4a2SClaudio Imbrenda 833190df4a2SClaudio Imbrenda if (kvm->arch.use_cmma) { 834190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 835190df4a2SClaudio Imbrenda vfree(mgs->pgste_bitmap); 836190df4a2SClaudio Imbrenda } 837190df4a2SClaudio Imbrenda kfree(mgs); 838190df4a2SClaudio Imbrenda return 0; 839190df4a2SClaudio Imbrenda } 840190df4a2SClaudio Imbrenda 841190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 842190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 843190df4a2SClaudio Imbrenda { 844190df4a2SClaudio Imbrenda int idx, res = -ENXIO; 845190df4a2SClaudio Imbrenda 846190df4a2SClaudio Imbrenda mutex_lock(&kvm->lock); 847190df4a2SClaudio Imbrenda switch (attr->attr) { 848190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 849190df4a2SClaudio Imbrenda idx = srcu_read_lock(&kvm->srcu); 850190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 851190df4a2SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, idx); 852190df4a2SClaudio Imbrenda break; 853190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 854190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 855190df4a2SClaudio Imbrenda break; 856190df4a2SClaudio Imbrenda default: 857190df4a2SClaudio Imbrenda break; 858190df4a2SClaudio Imbrenda } 859190df4a2SClaudio Imbrenda mutex_unlock(&kvm->lock); 860190df4a2SClaudio Imbrenda 861190df4a2SClaudio Imbrenda return res; 862190df4a2SClaudio Imbrenda } 863190df4a2SClaudio Imbrenda 864190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 865190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 866190df4a2SClaudio Imbrenda { 867190df4a2SClaudio Imbrenda u64 mig = (kvm->arch.migration_state != NULL); 868190df4a2SClaudio Imbrenda 869190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 870190df4a2SClaudio Imbrenda return -ENXIO; 871190df4a2SClaudio Imbrenda 872190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 873190df4a2SClaudio Imbrenda return -EFAULT; 874190df4a2SClaudio Imbrenda return 0; 875190df4a2SClaudio Imbrenda } 876190df4a2SClaudio Imbrenda 87772f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 87872f25020SJason J. Herne { 87972f25020SJason J. Herne u8 gtod_high; 88072f25020SJason J. Herne 88172f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 88272f25020SJason J. Herne sizeof(gtod_high))) 88372f25020SJason J. Herne return -EFAULT; 88472f25020SJason J. Herne 88572f25020SJason J. Herne if (gtod_high != 0) 88672f25020SJason J. Herne return -EINVAL; 88758c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 88872f25020SJason J. Herne 88972f25020SJason J. Herne return 0; 89072f25020SJason J. Herne } 89172f25020SJason J. Herne 89272f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 89372f25020SJason J. Herne { 8945a3d883aSDavid Hildenbrand u64 gtod; 89572f25020SJason J. Herne 89672f25020SJason J. Herne if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 89772f25020SJason J. Herne return -EFAULT; 89872f25020SJason J. Herne 89925ed1675SDavid Hildenbrand kvm_s390_set_tod_clock(kvm, gtod); 90058c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod); 90172f25020SJason J. Herne return 0; 90272f25020SJason J. Herne } 90372f25020SJason J. Herne 90472f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 90572f25020SJason J. Herne { 90672f25020SJason J. Herne int ret; 90772f25020SJason J. Herne 90872f25020SJason J. Herne if (attr->flags) 90972f25020SJason J. Herne return -EINVAL; 91072f25020SJason J. Herne 91172f25020SJason J. Herne switch (attr->attr) { 91272f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 91372f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 91472f25020SJason J. Herne break; 91572f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 91672f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 91772f25020SJason J. Herne break; 91872f25020SJason J. Herne default: 91972f25020SJason J. Herne ret = -ENXIO; 92072f25020SJason J. Herne break; 92172f25020SJason J. Herne } 92272f25020SJason J. Herne return ret; 92372f25020SJason J. Herne } 92472f25020SJason J. Herne 92572f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 92672f25020SJason J. Herne { 92772f25020SJason J. Herne u8 gtod_high = 0; 92872f25020SJason J. Herne 92972f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 93072f25020SJason J. Herne sizeof(gtod_high))) 93172f25020SJason J. Herne return -EFAULT; 93258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 93372f25020SJason J. Herne 93472f25020SJason J. Herne return 0; 93572f25020SJason J. Herne } 93672f25020SJason J. Herne 93772f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 93872f25020SJason J. Herne { 9395a3d883aSDavid Hildenbrand u64 gtod; 94072f25020SJason J. Herne 94160417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 94272f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 94372f25020SJason J. Herne return -EFAULT; 94458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 94572f25020SJason J. Herne 94672f25020SJason J. Herne return 0; 94772f25020SJason J. Herne } 94872f25020SJason J. Herne 94972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 95072f25020SJason J. Herne { 95172f25020SJason J. Herne int ret; 95272f25020SJason J. Herne 95372f25020SJason J. Herne if (attr->flags) 95472f25020SJason J. Herne return -EINVAL; 95572f25020SJason J. Herne 95672f25020SJason J. Herne switch (attr->attr) { 95772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 95872f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 95972f25020SJason J. Herne break; 96072f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 96172f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 96272f25020SJason J. Herne break; 96372f25020SJason J. Herne default: 96472f25020SJason J. Herne ret = -ENXIO; 96572f25020SJason J. Herne break; 96672f25020SJason J. Herne } 96772f25020SJason J. Herne return ret; 96872f25020SJason J. Herne } 96972f25020SJason J. Herne 970658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 971658b6edaSMichael Mueller { 972658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 973053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 974658b6edaSMichael Mueller int ret = 0; 975658b6edaSMichael Mueller 976658b6edaSMichael Mueller mutex_lock(&kvm->lock); 977a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 978658b6edaSMichael Mueller ret = -EBUSY; 979658b6edaSMichael Mueller goto out; 980658b6edaSMichael Mueller } 981658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 982658b6edaSMichael Mueller if (!proc) { 983658b6edaSMichael Mueller ret = -ENOMEM; 984658b6edaSMichael Mueller goto out; 985658b6edaSMichael Mueller } 986658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 987658b6edaSMichael Mueller sizeof(*proc))) { 9889bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 989053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 990053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 9910487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 992053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 993053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 994053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 995053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 996053dd230SDavid Hildenbrand else 997658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 998053dd230SDavid Hildenbrand } 999c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1000658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1001a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1002a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1003a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1004a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1005a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1006a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1007a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1008658b6edaSMichael Mueller } else 1009658b6edaSMichael Mueller ret = -EFAULT; 1010658b6edaSMichael Mueller kfree(proc); 1011658b6edaSMichael Mueller out: 1012658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1013658b6edaSMichael Mueller return ret; 1014658b6edaSMichael Mueller } 1015658b6edaSMichael Mueller 101615c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 101715c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 101815c9705fSDavid Hildenbrand { 101915c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 102015c9705fSDavid Hildenbrand int ret = -EBUSY; 102115c9705fSDavid Hildenbrand 102215c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 102315c9705fSDavid Hildenbrand return -EFAULT; 102415c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 102515c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 102615c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 102715c9705fSDavid Hildenbrand return -EINVAL; 102815c9705fSDavid Hildenbrand 102915c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 103015c9705fSDavid Hildenbrand if (!atomic_read(&kvm->online_vcpus)) { 103115c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 103215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 103315c9705fSDavid Hildenbrand ret = 0; 103415c9705fSDavid Hildenbrand } 103515c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 103615c9705fSDavid Hildenbrand return ret; 103715c9705fSDavid Hildenbrand } 103815c9705fSDavid Hildenbrand 10390a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 10400a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 10410a763c78SDavid Hildenbrand { 10420a763c78SDavid Hildenbrand /* 10430a763c78SDavid Hildenbrand * Once supported by kernel + hw, we have to store the subfunctions 10440a763c78SDavid Hildenbrand * in kvm->arch and remember that user space configured them. 10450a763c78SDavid Hildenbrand */ 10460a763c78SDavid Hildenbrand return -ENXIO; 10470a763c78SDavid Hildenbrand } 10480a763c78SDavid Hildenbrand 1049658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1050658b6edaSMichael Mueller { 1051658b6edaSMichael Mueller int ret = -ENXIO; 1052658b6edaSMichael Mueller 1053658b6edaSMichael Mueller switch (attr->attr) { 1054658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1055658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1056658b6edaSMichael Mueller break; 105715c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 105815c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 105915c9705fSDavid Hildenbrand break; 10600a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 10610a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 10620a763c78SDavid Hildenbrand break; 1063658b6edaSMichael Mueller } 1064658b6edaSMichael Mueller return ret; 1065658b6edaSMichael Mueller } 1066658b6edaSMichael Mueller 1067658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1068658b6edaSMichael Mueller { 1069658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1070658b6edaSMichael Mueller int ret = 0; 1071658b6edaSMichael Mueller 1072658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1073658b6edaSMichael Mueller if (!proc) { 1074658b6edaSMichael Mueller ret = -ENOMEM; 1075658b6edaSMichael Mueller goto out; 1076658b6edaSMichael Mueller } 10779bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1078658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1079c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1080c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1081a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1082a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1083a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1084a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1085a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1086a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1087a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1088658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1089658b6edaSMichael Mueller ret = -EFAULT; 1090658b6edaSMichael Mueller kfree(proc); 1091658b6edaSMichael Mueller out: 1092658b6edaSMichael Mueller return ret; 1093658b6edaSMichael Mueller } 1094658b6edaSMichael Mueller 1095658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1096658b6edaSMichael Mueller { 1097658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1098658b6edaSMichael Mueller int ret = 0; 1099658b6edaSMichael Mueller 1100658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 1101658b6edaSMichael Mueller if (!mach) { 1102658b6edaSMichael Mueller ret = -ENOMEM; 1103658b6edaSMichael Mueller goto out; 1104658b6edaSMichael Mueller } 1105658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 110637c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1107c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1108981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1109658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 111004478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 1111a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1112a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1113a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1114a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1115a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1116a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1117a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1118a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1119a8c39dd7SChristian Borntraeger mach->fac_list[0], 1120a8c39dd7SChristian Borntraeger mach->fac_list[1], 1121a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1122658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1123658b6edaSMichael Mueller ret = -EFAULT; 1124658b6edaSMichael Mueller kfree(mach); 1125658b6edaSMichael Mueller out: 1126658b6edaSMichael Mueller return ret; 1127658b6edaSMichael Mueller } 1128658b6edaSMichael Mueller 112915c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 113015c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 113115c9705fSDavid Hildenbrand { 113215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 113315c9705fSDavid Hildenbrand 113415c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 113515c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 113615c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 113715c9705fSDavid Hildenbrand return -EFAULT; 113815c9705fSDavid Hildenbrand return 0; 113915c9705fSDavid Hildenbrand } 114015c9705fSDavid Hildenbrand 114115c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 114215c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 114315c9705fSDavid Hildenbrand { 114415c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 114515c9705fSDavid Hildenbrand 114615c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 114715c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 114815c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 114915c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 115015c9705fSDavid Hildenbrand return -EFAULT; 115115c9705fSDavid Hildenbrand return 0; 115215c9705fSDavid Hildenbrand } 115315c9705fSDavid Hildenbrand 11540a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 11550a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 11560a763c78SDavid Hildenbrand { 11570a763c78SDavid Hildenbrand /* 11580a763c78SDavid Hildenbrand * Once we can actually configure subfunctions (kernel + hw support), 11590a763c78SDavid Hildenbrand * we have to check if they were already set by user space, if so copy 11600a763c78SDavid Hildenbrand * them from kvm->arch. 11610a763c78SDavid Hildenbrand */ 11620a763c78SDavid Hildenbrand return -ENXIO; 11630a763c78SDavid Hildenbrand } 11640a763c78SDavid Hildenbrand 11650a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 11660a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 11670a763c78SDavid Hildenbrand { 11680a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 11690a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 11700a763c78SDavid Hildenbrand return -EFAULT; 11710a763c78SDavid Hildenbrand return 0; 11720a763c78SDavid Hildenbrand } 1173658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1174658b6edaSMichael Mueller { 1175658b6edaSMichael Mueller int ret = -ENXIO; 1176658b6edaSMichael Mueller 1177658b6edaSMichael Mueller switch (attr->attr) { 1178658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1179658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1180658b6edaSMichael Mueller break; 1181658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1182658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1183658b6edaSMichael Mueller break; 118415c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 118515c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 118615c9705fSDavid Hildenbrand break; 118715c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 118815c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 118915c9705fSDavid Hildenbrand break; 11900a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 11910a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 11920a763c78SDavid Hildenbrand break; 11930a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 11940a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 11950a763c78SDavid Hildenbrand break; 1196658b6edaSMichael Mueller } 1197658b6edaSMichael Mueller return ret; 1198658b6edaSMichael Mueller } 1199658b6edaSMichael Mueller 1200f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1201f2061656SDominik Dingel { 1202f2061656SDominik Dingel int ret; 1203f2061656SDominik Dingel 1204f2061656SDominik Dingel switch (attr->group) { 12054f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 12068c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 12074f718eabSDominik Dingel break; 120872f25020SJason J. Herne case KVM_S390_VM_TOD: 120972f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 121072f25020SJason J. Herne break; 1211658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1212658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1213658b6edaSMichael Mueller break; 1214a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1215a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1216a374e892STony Krowiak break; 1217190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1218190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1219190df4a2SClaudio Imbrenda break; 1220f2061656SDominik Dingel default: 1221f2061656SDominik Dingel ret = -ENXIO; 1222f2061656SDominik Dingel break; 1223f2061656SDominik Dingel } 1224f2061656SDominik Dingel 1225f2061656SDominik Dingel return ret; 1226f2061656SDominik Dingel } 1227f2061656SDominik Dingel 1228f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1229f2061656SDominik Dingel { 12308c0a7ce6SDominik Dingel int ret; 12318c0a7ce6SDominik Dingel 12328c0a7ce6SDominik Dingel switch (attr->group) { 12338c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 12348c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 12358c0a7ce6SDominik Dingel break; 123672f25020SJason J. Herne case KVM_S390_VM_TOD: 123772f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 123872f25020SJason J. Herne break; 1239658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1240658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1241658b6edaSMichael Mueller break; 1242190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1243190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1244190df4a2SClaudio Imbrenda break; 12458c0a7ce6SDominik Dingel default: 12468c0a7ce6SDominik Dingel ret = -ENXIO; 12478c0a7ce6SDominik Dingel break; 12488c0a7ce6SDominik Dingel } 12498c0a7ce6SDominik Dingel 12508c0a7ce6SDominik Dingel return ret; 1251f2061656SDominik Dingel } 1252f2061656SDominik Dingel 1253f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1254f2061656SDominik Dingel { 1255f2061656SDominik Dingel int ret; 1256f2061656SDominik Dingel 1257f2061656SDominik Dingel switch (attr->group) { 12584f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 12594f718eabSDominik Dingel switch (attr->attr) { 12604f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 12614f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1262f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1263f9cbd9b0SDavid Hildenbrand break; 12648c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 12654f718eabSDominik Dingel ret = 0; 12664f718eabSDominik Dingel break; 12674f718eabSDominik Dingel default: 12684f718eabSDominik Dingel ret = -ENXIO; 12694f718eabSDominik Dingel break; 12704f718eabSDominik Dingel } 12714f718eabSDominik Dingel break; 127272f25020SJason J. Herne case KVM_S390_VM_TOD: 127372f25020SJason J. Herne switch (attr->attr) { 127472f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 127572f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 127672f25020SJason J. Herne ret = 0; 127772f25020SJason J. Herne break; 127872f25020SJason J. Herne default: 127972f25020SJason J. Herne ret = -ENXIO; 128072f25020SJason J. Herne break; 128172f25020SJason J. Herne } 128272f25020SJason J. Herne break; 1283658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1284658b6edaSMichael Mueller switch (attr->attr) { 1285658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1286658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 128715c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 128815c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 12890a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1290658b6edaSMichael Mueller ret = 0; 1291658b6edaSMichael Mueller break; 12920a763c78SDavid Hildenbrand /* configuring subfunctions is not supported yet */ 12930a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1294658b6edaSMichael Mueller default: 1295658b6edaSMichael Mueller ret = -ENXIO; 1296658b6edaSMichael Mueller break; 1297658b6edaSMichael Mueller } 1298658b6edaSMichael Mueller break; 1299a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1300a374e892STony Krowiak switch (attr->attr) { 1301a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1302a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1303a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1304a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1305a374e892STony Krowiak ret = 0; 1306a374e892STony Krowiak break; 1307a374e892STony Krowiak default: 1308a374e892STony Krowiak ret = -ENXIO; 1309a374e892STony Krowiak break; 1310a374e892STony Krowiak } 1311a374e892STony Krowiak break; 1312190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1313190df4a2SClaudio Imbrenda ret = 0; 1314190df4a2SClaudio Imbrenda break; 1315f2061656SDominik Dingel default: 1316f2061656SDominik Dingel ret = -ENXIO; 1317f2061656SDominik Dingel break; 1318f2061656SDominik Dingel } 1319f2061656SDominik Dingel 1320f2061656SDominik Dingel return ret; 1321f2061656SDominik Dingel } 1322f2061656SDominik Dingel 132330ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 132430ee2a98SJason J. Herne { 132530ee2a98SJason J. Herne uint8_t *keys; 132630ee2a98SJason J. Herne uint64_t hva; 1327*4f899147SChristian Borntraeger int srcu_idx, i, r = 0; 132830ee2a98SJason J. Herne 132930ee2a98SJason J. Herne if (args->flags != 0) 133030ee2a98SJason J. Herne return -EINVAL; 133130ee2a98SJason J. Herne 133230ee2a98SJason J. Herne /* Is this guest using storage keys? */ 133330ee2a98SJason J. Herne if (!mm_use_skey(current->mm)) 133430ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 133530ee2a98SJason J. Herne 133630ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 133730ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 133830ee2a98SJason J. Herne return -EINVAL; 133930ee2a98SJason J. Herne 1340752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 134130ee2a98SJason J. Herne if (!keys) 134230ee2a98SJason J. Herne return -ENOMEM; 134330ee2a98SJason J. Herne 1344d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 1345*4f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 134630ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 134730ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 134830ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 134930ee2a98SJason J. Herne r = -EFAULT; 1350d3ed1ceeSMartin Schwidefsky break; 135130ee2a98SJason J. Herne } 135230ee2a98SJason J. Herne 1353154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1354154c8c19SDavid Hildenbrand if (r) 1355d3ed1ceeSMartin Schwidefsky break; 135630ee2a98SJason J. Herne } 1357*4f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1358d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 135930ee2a98SJason J. Herne 1360d3ed1ceeSMartin Schwidefsky if (!r) { 136130ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 136230ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 136330ee2a98SJason J. Herne if (r) 136430ee2a98SJason J. Herne r = -EFAULT; 1365d3ed1ceeSMartin Schwidefsky } 1366d3ed1ceeSMartin Schwidefsky 136730ee2a98SJason J. Herne kvfree(keys); 136830ee2a98SJason J. Herne return r; 136930ee2a98SJason J. Herne } 137030ee2a98SJason J. Herne 137130ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 137230ee2a98SJason J. Herne { 137330ee2a98SJason J. Herne uint8_t *keys; 137430ee2a98SJason J. Herne uint64_t hva; 1375*4f899147SChristian Borntraeger int srcu_idx, i, r = 0; 137630ee2a98SJason J. Herne 137730ee2a98SJason J. Herne if (args->flags != 0) 137830ee2a98SJason J. Herne return -EINVAL; 137930ee2a98SJason J. Herne 138030ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 138130ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 138230ee2a98SJason J. Herne return -EINVAL; 138330ee2a98SJason J. Herne 1384752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 138530ee2a98SJason J. Herne if (!keys) 138630ee2a98SJason J. Herne return -ENOMEM; 138730ee2a98SJason J. Herne 138830ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 138930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 139030ee2a98SJason J. Herne if (r) { 139130ee2a98SJason J. Herne r = -EFAULT; 139230ee2a98SJason J. Herne goto out; 139330ee2a98SJason J. Herne } 139430ee2a98SJason J. Herne 139530ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 139614d4a425SDominik Dingel r = s390_enable_skey(); 139714d4a425SDominik Dingel if (r) 139814d4a425SDominik Dingel goto out; 139930ee2a98SJason J. Herne 1400d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 1401*4f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 140230ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 140330ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 140430ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 140530ee2a98SJason J. Herne r = -EFAULT; 1406d3ed1ceeSMartin Schwidefsky break; 140730ee2a98SJason J. Herne } 140830ee2a98SJason J. Herne 140930ee2a98SJason J. Herne /* Lowest order bit is reserved */ 141030ee2a98SJason J. Herne if (keys[i] & 0x01) { 141130ee2a98SJason J. Herne r = -EINVAL; 1412d3ed1ceeSMartin Schwidefsky break; 141330ee2a98SJason J. Herne } 141430ee2a98SJason J. Herne 1415fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 141630ee2a98SJason J. Herne if (r) 1417d3ed1ceeSMartin Schwidefsky break; 141830ee2a98SJason J. Herne } 1419*4f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1420d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 142130ee2a98SJason J. Herne out: 142230ee2a98SJason J. Herne kvfree(keys); 142330ee2a98SJason J. Herne return r; 142430ee2a98SJason J. Herne } 142530ee2a98SJason J. Herne 14264036e387SClaudio Imbrenda /* 14274036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 14284036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 14294036e387SClaudio Imbrenda * two longs. 14304036e387SClaudio Imbrenda */ 14314036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 14324036e387SClaudio Imbrenda /* for consistency */ 14334036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 14344036e387SClaudio Imbrenda 14354036e387SClaudio Imbrenda /* 14364036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 14374036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 14384036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 14394036e387SClaudio Imbrenda * no trailing clean bytes are saved. 14404036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 14414036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 14424036e387SClaudio Imbrenda */ 14434036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 14444036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 14454036e387SClaudio Imbrenda { 14464036e387SClaudio Imbrenda struct kvm_s390_migration_state *s = kvm->arch.migration_state; 14474036e387SClaudio Imbrenda unsigned long bufsize, hva, pgstev, i, next, cur; 14484036e387SClaudio Imbrenda int srcu_idx, peek, r = 0, rr; 14494036e387SClaudio Imbrenda u8 *res; 14504036e387SClaudio Imbrenda 14514036e387SClaudio Imbrenda cur = args->start_gfn; 14524036e387SClaudio Imbrenda i = next = pgstev = 0; 14534036e387SClaudio Imbrenda 14544036e387SClaudio Imbrenda if (unlikely(!kvm->arch.use_cmma)) 14554036e387SClaudio Imbrenda return -ENXIO; 14564036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 14574036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 14584036e387SClaudio Imbrenda return -EINVAL; 14594036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 14604036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 14614036e387SClaudio Imbrenda if (!peek && !s) 14624036e387SClaudio Imbrenda return -EINVAL; 14634036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 14644036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 14654036e387SClaudio Imbrenda if (!bufsize || !kvm->mm->context.use_cmma) { 14664036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 14674036e387SClaudio Imbrenda return 0; 14684036e387SClaudio Imbrenda } 14694036e387SClaudio Imbrenda 14704036e387SClaudio Imbrenda if (!peek) { 14714036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 14724036e387SClaudio Imbrenda if (!atomic64_read(&s->dirty_pages)) { 14734036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 14744036e387SClaudio Imbrenda return 0; 14754036e387SClaudio Imbrenda } 14764036e387SClaudio Imbrenda cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 14774036e387SClaudio Imbrenda args->start_gfn); 14784036e387SClaudio Imbrenda if (cur >= s->bitmap_size) /* nothing found, loop back */ 14794036e387SClaudio Imbrenda cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 0); 14804036e387SClaudio Imbrenda if (cur >= s->bitmap_size) { /* again! (very unlikely) */ 14814036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 14824036e387SClaudio Imbrenda return 0; 14834036e387SClaudio Imbrenda } 14844036e387SClaudio Imbrenda next = find_next_bit(s->pgste_bitmap, s->bitmap_size, cur + 1); 14854036e387SClaudio Imbrenda } 14864036e387SClaudio Imbrenda 14874036e387SClaudio Imbrenda res = vmalloc(bufsize); 14884036e387SClaudio Imbrenda if (!res) 14894036e387SClaudio Imbrenda return -ENOMEM; 14904036e387SClaudio Imbrenda 14914036e387SClaudio Imbrenda args->start_gfn = cur; 14924036e387SClaudio Imbrenda 14934036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 14944036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 14954036e387SClaudio Imbrenda while (i < bufsize) { 14964036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, cur); 14974036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 14984036e387SClaudio Imbrenda r = -EFAULT; 14994036e387SClaudio Imbrenda break; 15004036e387SClaudio Imbrenda } 15014036e387SClaudio Imbrenda /* decrement only if we actually flipped the bit to 0 */ 15024036e387SClaudio Imbrenda if (!peek && test_and_clear_bit(cur, s->pgste_bitmap)) 15034036e387SClaudio Imbrenda atomic64_dec(&s->dirty_pages); 15044036e387SClaudio Imbrenda r = get_pgste(kvm->mm, hva, &pgstev); 15054036e387SClaudio Imbrenda if (r < 0) 15064036e387SClaudio Imbrenda pgstev = 0; 15074036e387SClaudio Imbrenda /* save the value */ 15084036e387SClaudio Imbrenda res[i++] = (pgstev >> 24) & 0x3; 15094036e387SClaudio Imbrenda /* 15104036e387SClaudio Imbrenda * if the next bit is too far away, stop. 15114036e387SClaudio Imbrenda * if we reached the previous "next", find the next one 15124036e387SClaudio Imbrenda */ 15134036e387SClaudio Imbrenda if (!peek) { 15144036e387SClaudio Imbrenda if (next > cur + KVM_S390_MAX_BIT_DISTANCE) 15154036e387SClaudio Imbrenda break; 15164036e387SClaudio Imbrenda if (cur == next) 15174036e387SClaudio Imbrenda next = find_next_bit(s->pgste_bitmap, 15184036e387SClaudio Imbrenda s->bitmap_size, cur + 1); 15194036e387SClaudio Imbrenda /* reached the end of the bitmap or of the buffer, stop */ 15204036e387SClaudio Imbrenda if ((next >= s->bitmap_size) || 15214036e387SClaudio Imbrenda (next >= args->start_gfn + bufsize)) 15224036e387SClaudio Imbrenda break; 15234036e387SClaudio Imbrenda } 15244036e387SClaudio Imbrenda cur++; 15254036e387SClaudio Imbrenda } 15264036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 15274036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 15284036e387SClaudio Imbrenda args->count = i; 15294036e387SClaudio Imbrenda args->remaining = s ? atomic64_read(&s->dirty_pages) : 0; 15304036e387SClaudio Imbrenda 15314036e387SClaudio Imbrenda rr = copy_to_user((void __user *)args->values, res, args->count); 15324036e387SClaudio Imbrenda if (rr) 15334036e387SClaudio Imbrenda r = -EFAULT; 15344036e387SClaudio Imbrenda 15354036e387SClaudio Imbrenda vfree(res); 15364036e387SClaudio Imbrenda return r; 15374036e387SClaudio Imbrenda } 15384036e387SClaudio Imbrenda 15394036e387SClaudio Imbrenda /* 15404036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 15414036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 15424036e387SClaudio Imbrenda * set and the mm->context.use_cmma flag is set. 15434036e387SClaudio Imbrenda */ 15444036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 15454036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 15464036e387SClaudio Imbrenda { 15474036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 15484036e387SClaudio Imbrenda uint8_t *bits; 15494036e387SClaudio Imbrenda int srcu_idx, r = 0; 15504036e387SClaudio Imbrenda 15514036e387SClaudio Imbrenda mask = args->mask; 15524036e387SClaudio Imbrenda 15534036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 15544036e387SClaudio Imbrenda return -ENXIO; 15554036e387SClaudio Imbrenda /* invalid/unsupported flags */ 15564036e387SClaudio Imbrenda if (args->flags != 0) 15574036e387SClaudio Imbrenda return -EINVAL; 15584036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 15594036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 15604036e387SClaudio Imbrenda return -EINVAL; 15614036e387SClaudio Imbrenda /* Nothing to do */ 15624036e387SClaudio Imbrenda if (args->count == 0) 15634036e387SClaudio Imbrenda return 0; 15644036e387SClaudio Imbrenda 15654036e387SClaudio Imbrenda bits = vmalloc(sizeof(*bits) * args->count); 15664036e387SClaudio Imbrenda if (!bits) 15674036e387SClaudio Imbrenda return -ENOMEM; 15684036e387SClaudio Imbrenda 15694036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 15704036e387SClaudio Imbrenda if (r) { 15714036e387SClaudio Imbrenda r = -EFAULT; 15724036e387SClaudio Imbrenda goto out; 15734036e387SClaudio Imbrenda } 15744036e387SClaudio Imbrenda 15754036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 15764036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 15774036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 15784036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 15794036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 15804036e387SClaudio Imbrenda r = -EFAULT; 15814036e387SClaudio Imbrenda break; 15824036e387SClaudio Imbrenda } 15834036e387SClaudio Imbrenda 15844036e387SClaudio Imbrenda pgstev = bits[i]; 15854036e387SClaudio Imbrenda pgstev = pgstev << 24; 15864036e387SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK; 15874036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 15884036e387SClaudio Imbrenda } 15894036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 15904036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 15914036e387SClaudio Imbrenda 15924036e387SClaudio Imbrenda if (!kvm->mm->context.use_cmma) { 15934036e387SClaudio Imbrenda down_write(&kvm->mm->mmap_sem); 15944036e387SClaudio Imbrenda kvm->mm->context.use_cmma = 1; 15954036e387SClaudio Imbrenda up_write(&kvm->mm->mmap_sem); 15964036e387SClaudio Imbrenda } 15974036e387SClaudio Imbrenda out: 15984036e387SClaudio Imbrenda vfree(bits); 15994036e387SClaudio Imbrenda return r; 16004036e387SClaudio Imbrenda } 16014036e387SClaudio Imbrenda 1602b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 1603b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 1604b0c632dbSHeiko Carstens { 1605b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 1606b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 1607f2061656SDominik Dingel struct kvm_device_attr attr; 1608b0c632dbSHeiko Carstens int r; 1609b0c632dbSHeiko Carstens 1610b0c632dbSHeiko Carstens switch (ioctl) { 1611ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 1612ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 1613ba5c1e9bSCarsten Otte 1614ba5c1e9bSCarsten Otte r = -EFAULT; 1615ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 1616ba5c1e9bSCarsten Otte break; 1617ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 1618ba5c1e9bSCarsten Otte break; 1619ba5c1e9bSCarsten Otte } 1620d938dc55SCornelia Huck case KVM_ENABLE_CAP: { 1621d938dc55SCornelia Huck struct kvm_enable_cap cap; 1622d938dc55SCornelia Huck r = -EFAULT; 1623d938dc55SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 1624d938dc55SCornelia Huck break; 1625d938dc55SCornelia Huck r = kvm_vm_ioctl_enable_cap(kvm, &cap); 1626d938dc55SCornelia Huck break; 1627d938dc55SCornelia Huck } 162884223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 162984223598SCornelia Huck struct kvm_irq_routing_entry routing; 163084223598SCornelia Huck 163184223598SCornelia Huck r = -EINVAL; 163284223598SCornelia Huck if (kvm->arch.use_irqchip) { 163384223598SCornelia Huck /* Set up dummy routing. */ 163484223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 1635152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 163684223598SCornelia Huck } 163784223598SCornelia Huck break; 163884223598SCornelia Huck } 1639f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 1640f2061656SDominik Dingel r = -EFAULT; 1641f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1642f2061656SDominik Dingel break; 1643f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 1644f2061656SDominik Dingel break; 1645f2061656SDominik Dingel } 1646f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 1647f2061656SDominik Dingel r = -EFAULT; 1648f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1649f2061656SDominik Dingel break; 1650f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 1651f2061656SDominik Dingel break; 1652f2061656SDominik Dingel } 1653f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 1654f2061656SDominik Dingel r = -EFAULT; 1655f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1656f2061656SDominik Dingel break; 1657f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 1658f2061656SDominik Dingel break; 1659f2061656SDominik Dingel } 166030ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 166130ee2a98SJason J. Herne struct kvm_s390_skeys args; 166230ee2a98SJason J. Herne 166330ee2a98SJason J. Herne r = -EFAULT; 166430ee2a98SJason J. Herne if (copy_from_user(&args, argp, 166530ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 166630ee2a98SJason J. Herne break; 166730ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 166830ee2a98SJason J. Herne break; 166930ee2a98SJason J. Herne } 167030ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 167130ee2a98SJason J. Herne struct kvm_s390_skeys args; 167230ee2a98SJason J. Herne 167330ee2a98SJason J. Herne r = -EFAULT; 167430ee2a98SJason J. Herne if (copy_from_user(&args, argp, 167530ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 167630ee2a98SJason J. Herne break; 167730ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 167830ee2a98SJason J. Herne break; 167930ee2a98SJason J. Herne } 16804036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 16814036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 16824036e387SClaudio Imbrenda 16834036e387SClaudio Imbrenda r = -EFAULT; 16844036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 16854036e387SClaudio Imbrenda break; 16864036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 16874036e387SClaudio Imbrenda if (!r) { 16884036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 16894036e387SClaudio Imbrenda if (r) 16904036e387SClaudio Imbrenda r = -EFAULT; 16914036e387SClaudio Imbrenda } 16924036e387SClaudio Imbrenda break; 16934036e387SClaudio Imbrenda } 16944036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 16954036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 16964036e387SClaudio Imbrenda 16974036e387SClaudio Imbrenda r = -EFAULT; 16984036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 16994036e387SClaudio Imbrenda break; 17004036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 17014036e387SClaudio Imbrenda break; 17024036e387SClaudio Imbrenda } 1703b0c632dbSHeiko Carstens default: 1704367e1319SAvi Kivity r = -ENOTTY; 1705b0c632dbSHeiko Carstens } 1706b0c632dbSHeiko Carstens 1707b0c632dbSHeiko Carstens return r; 1708b0c632dbSHeiko Carstens } 1709b0c632dbSHeiko Carstens 171045c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config) 171145c9b47cSTony Krowiak { 171245c9b47cSTony Krowiak u32 fcn_code = 0x04000000UL; 171386044c8cSChristian Borntraeger u32 cc = 0; 171445c9b47cSTony Krowiak 171586044c8cSChristian Borntraeger memset(config, 0, 128); 171645c9b47cSTony Krowiak asm volatile( 171745c9b47cSTony Krowiak "lgr 0,%1\n" 171845c9b47cSTony Krowiak "lgr 2,%2\n" 171945c9b47cSTony Krowiak ".long 0xb2af0000\n" /* PQAP(QCI) */ 172086044c8cSChristian Borntraeger "0: ipm %0\n" 172145c9b47cSTony Krowiak "srl %0,28\n" 172286044c8cSChristian Borntraeger "1:\n" 172386044c8cSChristian Borntraeger EX_TABLE(0b, 1b) 172486044c8cSChristian Borntraeger : "+r" (cc) 172545c9b47cSTony Krowiak : "r" (fcn_code), "r" (config) 172645c9b47cSTony Krowiak : "cc", "0", "2", "memory" 172745c9b47cSTony Krowiak ); 172845c9b47cSTony Krowiak 172945c9b47cSTony Krowiak return cc; 173045c9b47cSTony Krowiak } 173145c9b47cSTony Krowiak 173245c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 173345c9b47cSTony Krowiak { 173445c9b47cSTony Krowiak u8 config[128]; 173545c9b47cSTony Krowiak int cc; 173645c9b47cSTony Krowiak 1737a6aacc3fSHeiko Carstens if (test_facility(12)) { 173845c9b47cSTony Krowiak cc = kvm_s390_query_ap_config(config); 173945c9b47cSTony Krowiak 174045c9b47cSTony Krowiak if (cc) 174145c9b47cSTony Krowiak pr_err("PQAP(QCI) failed with cc=%d", cc); 174245c9b47cSTony Krowiak else 174345c9b47cSTony Krowiak return config[0] & 0x40; 174445c9b47cSTony Krowiak } 174545c9b47cSTony Krowiak 174645c9b47cSTony Krowiak return 0; 174745c9b47cSTony Krowiak } 174845c9b47cSTony Krowiak 174945c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 175045c9b47cSTony Krowiak { 175145c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 175245c9b47cSTony Krowiak 175345c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 175445c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 175545c9b47cSTony Krowiak else 175645c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 175745c9b47cSTony Krowiak } 175845c9b47cSTony Krowiak 17599bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 17609d8d5786SMichael Mueller { 17619bb0ec09SDavid Hildenbrand struct cpuid cpuid; 17629bb0ec09SDavid Hildenbrand 17639bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 17649bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 17659bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 17669d8d5786SMichael Mueller } 17679d8d5786SMichael Mueller 1768c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 17695102ee87STony Krowiak { 17709d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 1771c54f0d6aSDavid Hildenbrand return; 17725102ee87STony Krowiak 1773c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 177445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 17755102ee87STony Krowiak 1776ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 1777ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 1778ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 1779ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1780ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1781ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1782ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 17835102ee87STony Krowiak } 17845102ee87STony Krowiak 17857d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 17867d43bafcSEugene (jno) Dvurechenski { 17877d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 17885e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 17897d43bafcSEugene (jno) Dvurechenski else 17907d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 17917d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 17927d43bafcSEugene (jno) Dvurechenski } 17937d43bafcSEugene (jno) Dvurechenski 1794e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1795b0c632dbSHeiko Carstens { 179676a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 17979d8d5786SMichael Mueller int i, rc; 1798b0c632dbSHeiko Carstens char debug_name[16]; 1799f6c137ffSChristian Borntraeger static unsigned long sca_offset; 1800b0c632dbSHeiko Carstens 1801e08b9637SCarsten Otte rc = -EINVAL; 1802e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1803e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 1804e08b9637SCarsten Otte goto out_err; 1805e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 1806e08b9637SCarsten Otte goto out_err; 1807e08b9637SCarsten Otte #else 1808e08b9637SCarsten Otte if (type) 1809e08b9637SCarsten Otte goto out_err; 1810e08b9637SCarsten Otte #endif 1811e08b9637SCarsten Otte 1812b0c632dbSHeiko Carstens rc = s390_enable_sie(); 1813b0c632dbSHeiko Carstens if (rc) 1814d89f5effSJan Kiszka goto out_err; 1815b0c632dbSHeiko Carstens 1816b290411aSCarsten Otte rc = -ENOMEM; 1817b290411aSCarsten Otte 18187d0a5e62SJanosch Frank ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500); 18197d0a5e62SJanosch Frank 18207d43bafcSEugene (jno) Dvurechenski kvm->arch.use_esca = 0; /* start with basic SCA */ 182176a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 182276a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 18235e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 182476a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 1825b0c632dbSHeiko Carstens if (!kvm->arch.sca) 1826d89f5effSJan Kiszka goto out_err; 1827f6c137ffSChristian Borntraeger spin_lock(&kvm_lock); 1828c5c2c393SDavid Hildenbrand sca_offset += 16; 1829bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 1830c5c2c393SDavid Hildenbrand sca_offset = 0; 1831bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 1832bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 1833f6c137ffSChristian Borntraeger spin_unlock(&kvm_lock); 1834b0c632dbSHeiko Carstens 1835b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 1836b0c632dbSHeiko Carstens 18371cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 1838b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 183940f5b735SDominik Dingel goto out_err; 1840b0c632dbSHeiko Carstens 1841c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 1842c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 1843c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 184440f5b735SDominik Dingel goto out_err; 18459d8d5786SMichael Mueller 1846fb5bf93fSMichael Mueller /* Populate the facility mask initially. */ 1847c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list, 184804478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 18499d8d5786SMichael Mueller for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) { 18509d8d5786SMichael Mueller if (i < kvm_s390_fac_list_mask_size()) 1851c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i]; 18529d8d5786SMichael Mueller else 1853c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] = 0UL; 18549d8d5786SMichael Mueller } 18559d8d5786SMichael Mueller 1856981467c9SMichael Mueller /* Populate the facility list initially. */ 1857c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 1858c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask, 1859981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1860981467c9SMichael Mueller 186195ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 186295ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 186395ca2cb5SJanosch Frank 18649bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 186537c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 18669d8d5786SMichael Mueller 1867c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 18685102ee87STony Krowiak 186951978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 187051978393SFei Li kvm->arch.float_int.simm = 0; 187151978393SFei Li kvm->arch.float_int.nimm = 0; 1872ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 18736d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 18746d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 18758a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 1876a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 1877ba5c1e9bSCarsten Otte 1878b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 187978f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 1880b0c632dbSHeiko Carstens 1881e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 1882e08b9637SCarsten Otte kvm->arch.gmap = NULL; 1883a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 1884e08b9637SCarsten Otte } else { 188532e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 1886ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 188732e6b236SGuenther Hutzl else 1888ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 188932e6b236SGuenther Hutzl sclp.hamax + 1); 18906ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 1891598841caSCarsten Otte if (!kvm->arch.gmap) 189240f5b735SDominik Dingel goto out_err; 18932c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 189424eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 1895e08b9637SCarsten Otte } 1896fa6b7fe9SCornelia Huck 1897fa6b7fe9SCornelia Huck kvm->arch.css_support = 0; 189884223598SCornelia Huck kvm->arch.use_irqchip = 0; 189972f25020SJason J. Herne kvm->arch.epoch = 0; 1900fa6b7fe9SCornelia Huck 19018ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 1902a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 19038335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 19048ad35755SDavid Hildenbrand 1905d89f5effSJan Kiszka return 0; 1906d89f5effSJan Kiszka out_err: 1907c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 190840f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 19097d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 191078f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 1911d89f5effSJan Kiszka return rc; 1912b0c632dbSHeiko Carstens } 1913b0c632dbSHeiko Carstens 1914235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void) 1915235539b4SLuiz Capitulino { 1916235539b4SLuiz Capitulino return false; 1917235539b4SLuiz Capitulino } 1918235539b4SLuiz Capitulino 1919235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) 1920235539b4SLuiz Capitulino { 1921235539b4SLuiz Capitulino return 0; 1922235539b4SLuiz Capitulino } 1923235539b4SLuiz Capitulino 1924d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 1925d329c035SChristian Borntraeger { 1926d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 1927ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 192867335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 19293c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 1930bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 1931a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 193227e0393fSCarsten Otte 193327e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 19346ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 193527e0393fSCarsten Otte 1936e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 1937b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 1938d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 1939b31288faSKonstantin Weitz 19406692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 1941b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 1942d329c035SChristian Borntraeger } 1943d329c035SChristian Borntraeger 1944d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 1945d329c035SChristian Borntraeger { 1946d329c035SChristian Borntraeger unsigned int i; 1947988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 1948d329c035SChristian Borntraeger 1949988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 1950988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 1951988a2caeSGleb Natapov 1952988a2caeSGleb Natapov mutex_lock(&kvm->lock); 1953988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 1954d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 1955988a2caeSGleb Natapov 1956988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 1957988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 1958d329c035SChristian Borntraeger } 1959d329c035SChristian Borntraeger 1960b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 1961b0c632dbSHeiko Carstens { 1962d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 19637d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 1964d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 1965c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 196627e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 19676ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 1968841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 196967335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 1970a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 1971190df4a2SClaudio Imbrenda if (kvm->arch.migration_state) { 1972190df4a2SClaudio Imbrenda vfree(kvm->arch.migration_state->pgste_bitmap); 1973190df4a2SClaudio Imbrenda kfree(kvm->arch.migration_state); 1974190df4a2SClaudio Imbrenda } 19758335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 1976b0c632dbSHeiko Carstens } 1977b0c632dbSHeiko Carstens 1978b0c632dbSHeiko Carstens /* Section: vcpu related */ 1979dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 1980b0c632dbSHeiko Carstens { 19816ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 198227e0393fSCarsten Otte if (!vcpu->arch.gmap) 198327e0393fSCarsten Otte return -ENOMEM; 19842c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 1985dafd032aSDominik Dingel 198627e0393fSCarsten Otte return 0; 198727e0393fSCarsten Otte } 198827e0393fSCarsten Otte 1989a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 1990a6e2f683SEugene (jno) Dvurechenski { 1991a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 1992a6940674SDavid Hildenbrand return; 19935e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 19947d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 19957d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 19967d43bafcSEugene (jno) Dvurechenski 19977d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 19987d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 19997d43bafcSEugene (jno) Dvurechenski } else { 2000bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2001a6e2f683SEugene (jno) Dvurechenski 2002a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2003a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2004a6e2f683SEugene (jno) Dvurechenski } 20055e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 20067d43bafcSEugene (jno) Dvurechenski } 2007a6e2f683SEugene (jno) Dvurechenski 2008eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2009a6e2f683SEugene (jno) Dvurechenski { 2010a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2011a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2012a6940674SDavid Hildenbrand 2013a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2014a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2015a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2016a6940674SDavid Hildenbrand } 2017eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2018eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2019eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 20207d43bafcSEugene (jno) Dvurechenski 2021eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 20227d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 20237d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 20240c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2025eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 20267d43bafcSEugene (jno) Dvurechenski } else { 2027eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2028a6e2f683SEugene (jno) Dvurechenski 2029eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2030a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2031a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2032eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2033a6e2f683SEugene (jno) Dvurechenski } 2034eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 20355e044315SEugene (jno) Dvurechenski } 20365e044315SEugene (jno) Dvurechenski 20375e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 20385e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 20395e044315SEugene (jno) Dvurechenski { 20405e044315SEugene (jno) Dvurechenski d->sda = s->sda; 20415e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 20425e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 20435e044315SEugene (jno) Dvurechenski } 20445e044315SEugene (jno) Dvurechenski 20455e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 20465e044315SEugene (jno) Dvurechenski { 20475e044315SEugene (jno) Dvurechenski int i; 20485e044315SEugene (jno) Dvurechenski 20495e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 20505e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 20515e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 20525e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 20535e044315SEugene (jno) Dvurechenski } 20545e044315SEugene (jno) Dvurechenski 20555e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 20565e044315SEugene (jno) Dvurechenski { 20575e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 20585e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 20595e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 20605e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 20615e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 20625e044315SEugene (jno) Dvurechenski 20635e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 20645e044315SEugene (jno) Dvurechenski if (!new_sca) 20655e044315SEugene (jno) Dvurechenski return -ENOMEM; 20665e044315SEugene (jno) Dvurechenski 20675e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 20685e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 20695e044315SEugene (jno) Dvurechenski 20705e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 20715e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 20725e044315SEugene (jno) Dvurechenski 20735e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 20745e044315SEugene (jno) Dvurechenski 20755e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 20765e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 20775e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 20780c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 20795e044315SEugene (jno) Dvurechenski } 20805e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 20815e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 20825e044315SEugene (jno) Dvurechenski 20835e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 20845e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 20855e044315SEugene (jno) Dvurechenski 20865e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 20875e044315SEugene (jno) Dvurechenski 20888335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 20898335713aSChristian Borntraeger old_sca, kvm->arch.sca); 20905e044315SEugene (jno) Dvurechenski return 0; 20917d43bafcSEugene (jno) Dvurechenski } 2092a6e2f683SEugene (jno) Dvurechenski 2093a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2094a6e2f683SEugene (jno) Dvurechenski { 20955e044315SEugene (jno) Dvurechenski int rc; 20965e044315SEugene (jno) Dvurechenski 2097a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2098a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2099a6940674SDavid Hildenbrand return true; 2100a6940674SDavid Hildenbrand return false; 2101a6940674SDavid Hildenbrand } 21025e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 21035e044315SEugene (jno) Dvurechenski return true; 210476a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 21055e044315SEugene (jno) Dvurechenski return false; 21065e044315SEugene (jno) Dvurechenski 21075e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 21085e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 21095e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 21105e044315SEugene (jno) Dvurechenski 21115e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2112a6e2f683SEugene (jno) Dvurechenski } 2113a6e2f683SEugene (jno) Dvurechenski 2114dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 2115dafd032aSDominik Dingel { 2116dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 2117dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 211859674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 211959674c1aSChristian Borntraeger KVM_SYNC_GPRS | 21209eed0735SChristian Borntraeger KVM_SYNC_ACRS | 2121b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 2122b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 2123b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 212475a4615cSJulius Niedworok kvm_s390_set_prefix(vcpu, 0); 2125c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 2126c6e5f166SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 21274e0b1ab7SFan Zhang if (test_kvm_facility(vcpu->kvm, 133)) 21284e0b1ab7SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 2129f6aa6dc4SDavid Hildenbrand /* fprs can be synchronized via vrs, even if the guest has no vx. With 2130f6aa6dc4SDavid Hildenbrand * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 2131f6aa6dc4SDavid Hildenbrand */ 2132f6aa6dc4SDavid Hildenbrand if (MACHINE_HAS_VX) 213368c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 21346fd8e67dSDavid Hildenbrand else 21356fd8e67dSDavid Hildenbrand vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 2136dafd032aSDominik Dingel 2137dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 2138dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 2139dafd032aSDominik Dingel 2140b0c632dbSHeiko Carstens return 0; 2141b0c632dbSHeiko Carstens } 2142b0c632dbSHeiko Carstens 2143db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2144db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2145db0758b2SDavid Hildenbrand { 2146db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 21479c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2148db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 21499c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2150db0758b2SDavid Hildenbrand } 2151db0758b2SDavid Hildenbrand 2152db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2153db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2154db0758b2SDavid Hildenbrand { 2155db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 21569c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2157db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 2158db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 21599c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2160db0758b2SDavid Hildenbrand } 2161db0758b2SDavid Hildenbrand 2162db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2163db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2164db0758b2SDavid Hildenbrand { 2165db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 2166db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 2167db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 2168db0758b2SDavid Hildenbrand } 2169db0758b2SDavid Hildenbrand 2170db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2171db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2172db0758b2SDavid Hildenbrand { 2173db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 2174db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2175db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 2176db0758b2SDavid Hildenbrand } 2177db0758b2SDavid Hildenbrand 2178db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2179db0758b2SDavid Hildenbrand { 2180db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2181db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 2182db0758b2SDavid Hildenbrand preempt_enable(); 2183db0758b2SDavid Hildenbrand } 2184db0758b2SDavid Hildenbrand 2185db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2186db0758b2SDavid Hildenbrand { 2187db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2188db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 2189db0758b2SDavid Hildenbrand preempt_enable(); 2190db0758b2SDavid Hildenbrand } 2191db0758b2SDavid Hildenbrand 21924287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 21934287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 21944287f247SDavid Hildenbrand { 2195db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 21969c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2197db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 2198db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 21994287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 22009c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2201db0758b2SDavid Hildenbrand preempt_enable(); 22024287f247SDavid Hildenbrand } 22034287f247SDavid Hildenbrand 2204db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 22054287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 22064287f247SDavid Hildenbrand { 22079c23a131SDavid Hildenbrand unsigned int seq; 2208db0758b2SDavid Hildenbrand __u64 value; 2209db0758b2SDavid Hildenbrand 2210db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 22114287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 2212db0758b2SDavid Hildenbrand 22139c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 22149c23a131SDavid Hildenbrand do { 22159c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 22169c23a131SDavid Hildenbrand /* 22179c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 22189c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 22199c23a131SDavid Hildenbrand */ 22209c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 2221db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 22229c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 22239c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 2224db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 22259c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 22269c23a131SDavid Hildenbrand preempt_enable(); 2227db0758b2SDavid Hildenbrand return value; 22284287f247SDavid Hildenbrand } 22294287f247SDavid Hildenbrand 2230b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 2231b0c632dbSHeiko Carstens { 22329977e886SHendrik Brueckner 223337d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 2234805de8f4SPeter Zijlstra atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 22355ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2236db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 223701a745acSDavid Hildenbrand vcpu->cpu = cpu; 2238b0c632dbSHeiko Carstens } 2239b0c632dbSHeiko Carstens 2240b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 2241b0c632dbSHeiko Carstens { 224201a745acSDavid Hildenbrand vcpu->cpu = -1; 22435ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2244db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2245805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 224637d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 224737d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 22489977e886SHendrik Brueckner 2249b0c632dbSHeiko Carstens } 2250b0c632dbSHeiko Carstens 2251b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 2252b0c632dbSHeiko Carstens { 2253b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 2254b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 2255b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 22568d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 22574287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, 0); 2258b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 2259b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 2260b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 2261b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 2262b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 22639abc2a08SDavid Hildenbrand /* make sure the new fpc will be lazily loaded */ 22649abc2a08SDavid Hildenbrand save_fpu_regs(); 22659abc2a08SDavid Hildenbrand current->thread.fpu.fpc = 0; 2266b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 2267672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 22683c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 22693c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 22706352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 22716852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 22722ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 2273b0c632dbSHeiko Carstens } 2274b0c632dbSHeiko Carstens 227531928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 227642897d86SMarcelo Tosatti { 227772f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 2278fdf03650SFan Zhang preempt_disable(); 227972f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 2280fdf03650SFan Zhang preempt_enable(); 228172f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 228225508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 2283dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 2284eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 228525508824SDavid Hildenbrand } 22866502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 22876502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 228837d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 228937d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 229042897d86SMarcelo Tosatti } 229142897d86SMarcelo Tosatti 22925102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 22935102ee87STony Krowiak { 22949d8d5786SMichael Mueller if (!test_kvm_facility(vcpu->kvm, 76)) 22955102ee87STony Krowiak return; 22965102ee87STony Krowiak 2297a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 2298a374e892STony Krowiak 2299a374e892STony Krowiak if (vcpu->kvm->arch.crypto.aes_kw) 2300a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 2301a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 2302a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 2303a374e892STony Krowiak 23045102ee87STony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 23055102ee87STony Krowiak } 23065102ee87STony Krowiak 2307b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 2308b31605c1SDominik Dingel { 2309b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 2310b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 2311b31605c1SDominik Dingel } 2312b31605c1SDominik Dingel 2313b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 2314b31605c1SDominik Dingel { 2315b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 2316b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 2317b31605c1SDominik Dingel return -ENOMEM; 2318b31605c1SDominik Dingel 23190c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI; 2320b31605c1SDominik Dingel return 0; 2321b31605c1SDominik Dingel } 2322b31605c1SDominik Dingel 232391520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 232491520f1aSMichael Mueller { 232591520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 232691520f1aSMichael Mueller 232791520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 232880bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 2329c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 233091520f1aSMichael Mueller } 233191520f1aSMichael Mueller 2332b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 2333b0c632dbSHeiko Carstens { 2334b31605c1SDominik Dingel int rc = 0; 2335b31288faSKonstantin Weitz 23369e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 23379e6dabefSCornelia Huck CPUSTAT_SM | 2338a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 2339a4a4f191SGuenther Hutzl 234053df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 2341805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags); 234253df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 2343805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags); 2344a4a4f191SGuenther Hutzl 234591520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 234691520f1aSMichael Mueller 2347bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 2348bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 23490c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 2350bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 23510c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 2352f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 23530c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 23547feb6bb8SMichael Mueller 2355873b425eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi) 23560c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 2357cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 23580c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 23590c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 236048ee7d3aSDavid Hildenbrand if (sclp.has_cei) 23610c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 236211ad65b7SDavid Hildenbrand if (sclp.has_ib) 23630c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 236437c5f6c8SDavid Hildenbrand if (sclp.has_siif) 23650c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 236637c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 23670c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 236818280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 23690c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 23700c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 237113211ea7SEric Farman } 23724e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 23734e0b1ab7SFan Zhang | SDNXC; 2374c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 2375730cd632SFarhan Ali 2376730cd632SFarhan Ali if (sclp.has_kss) 2377730cd632SFarhan Ali atomic_or(CPUSTAT_KSS, &vcpu->arch.sie_block->cpuflags); 2378730cd632SFarhan Ali else 2379492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 23805a5e6536SMatthew Rosato 2381e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 2382b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 2383b31605c1SDominik Dingel if (rc) 2384b31605c1SDominik Dingel return rc; 2385b31288faSKonstantin Weitz } 23860ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2387ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 23889d8d5786SMichael Mueller 23895102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 23905102ee87STony Krowiak 2391b31605c1SDominik Dingel return rc; 2392b0c632dbSHeiko Carstens } 2393b0c632dbSHeiko Carstens 2394b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 2395b0c632dbSHeiko Carstens unsigned int id) 2396b0c632dbSHeiko Carstens { 23974d47555aSCarsten Otte struct kvm_vcpu *vcpu; 23987feb6bb8SMichael Mueller struct sie_page *sie_page; 23994d47555aSCarsten Otte int rc = -EINVAL; 2400b0c632dbSHeiko Carstens 24014215825eSDavid Hildenbrand if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 24024d47555aSCarsten Otte goto out; 24034d47555aSCarsten Otte 24044d47555aSCarsten Otte rc = -ENOMEM; 24054d47555aSCarsten Otte 2406b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 2407b0c632dbSHeiko Carstens if (!vcpu) 24084d47555aSCarsten Otte goto out; 2409b0c632dbSHeiko Carstens 2410da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 24117feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 24127feb6bb8SMichael Mueller if (!sie_page) 2413b0c632dbSHeiko Carstens goto out_free_cpu; 2414b0c632dbSHeiko Carstens 24157feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 24167feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 24177feb6bb8SMichael Mueller 2418efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 2419efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 2420efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 2421efed1104SDavid Hildenbrand 2422b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 2423ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 2424ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 2425d0321a24SChristian Borntraeger vcpu->arch.local_int.wq = &vcpu->wq; 24265288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 24279c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 2428ba5c1e9bSCarsten Otte 2429b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 2430b0c632dbSHeiko Carstens if (rc) 24319abc2a08SDavid Hildenbrand goto out_free_sie_block; 24328335713aSChristian Borntraeger VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu, 2433b0c632dbSHeiko Carstens vcpu->arch.sie_block); 2434ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 2435b0c632dbSHeiko Carstens 2436b0c632dbSHeiko Carstens return vcpu; 24377b06bf2fSWei Yongjun out_free_sie_block: 24387b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 2439b0c632dbSHeiko Carstens out_free_cpu: 2440b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 24414d47555aSCarsten Otte out: 2442b0c632dbSHeiko Carstens return ERR_PTR(rc); 2443b0c632dbSHeiko Carstens } 2444b0c632dbSHeiko Carstens 2445b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 2446b0c632dbSHeiko Carstens { 24479a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 2448b0c632dbSHeiko Carstens } 2449b0c632dbSHeiko Carstens 245027406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 245149b99e1eSChristian Borntraeger { 2452805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 245361a6df54SDavid Hildenbrand exit_sie(vcpu); 245449b99e1eSChristian Borntraeger } 245549b99e1eSChristian Borntraeger 245627406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 245749b99e1eSChristian Borntraeger { 2458805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 245949b99e1eSChristian Borntraeger } 246049b99e1eSChristian Borntraeger 24618e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 24628e236546SChristian Borntraeger { 2463805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 246461a6df54SDavid Hildenbrand exit_sie(vcpu); 24658e236546SChristian Borntraeger } 24668e236546SChristian Borntraeger 24678e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 24688e236546SChristian Borntraeger { 24699bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 24708e236546SChristian Borntraeger } 24718e236546SChristian Borntraeger 247249b99e1eSChristian Borntraeger /* 247349b99e1eSChristian Borntraeger * Kick a guest cpu out of SIE and wait until SIE is not running. 247449b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 247549b99e1eSChristian Borntraeger * return immediately. */ 247649b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 247749b99e1eSChristian Borntraeger { 2478805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); 247949b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 248049b99e1eSChristian Borntraeger cpu_relax(); 248149b99e1eSChristian Borntraeger } 248249b99e1eSChristian Borntraeger 24838e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 24848e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 248549b99e1eSChristian Borntraeger { 24868e236546SChristian Borntraeger kvm_make_request(req, vcpu); 24878e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 248849b99e1eSChristian Borntraeger } 248949b99e1eSChristian Borntraeger 2490414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 2491414d3b07SMartin Schwidefsky unsigned long end) 24922c70fe44SChristian Borntraeger { 24932c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 24942c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 2495414d3b07SMartin Schwidefsky unsigned long prefix; 2496414d3b07SMartin Schwidefsky int i; 24972c70fe44SChristian Borntraeger 249865d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 249965d0b0d4SDavid Hildenbrand return; 2500414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 2501414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 2502414d3b07SMartin Schwidefsky return; 25032c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 25042c70fe44SChristian Borntraeger /* match against both prefix pages */ 2505414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 2506414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 2507414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 2508414d3b07SMartin Schwidefsky start, end); 25098e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 25102c70fe44SChristian Borntraeger } 25112c70fe44SChristian Borntraeger } 25122c70fe44SChristian Borntraeger } 25132c70fe44SChristian Borntraeger 2514b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 2515b6d33834SChristoffer Dall { 2516b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 2517b6d33834SChristoffer Dall BUG(); 2518b6d33834SChristoffer Dall return 0; 2519b6d33834SChristoffer Dall } 2520b6d33834SChristoffer Dall 252114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 252214eebd91SCarsten Otte struct kvm_one_reg *reg) 252314eebd91SCarsten Otte { 252414eebd91SCarsten Otte int r = -EINVAL; 252514eebd91SCarsten Otte 252614eebd91SCarsten Otte switch (reg->id) { 252729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 252829b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 252929b7c71bSCarsten Otte (u32 __user *)reg->addr); 253029b7c71bSCarsten Otte break; 253129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 253229b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 253329b7c71bSCarsten Otte (u64 __user *)reg->addr); 253429b7c71bSCarsten Otte break; 253546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 25364287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 253746a6dd1cSJason J. herne (u64 __user *)reg->addr); 253846a6dd1cSJason J. herne break; 253946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 254046a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 254146a6dd1cSJason J. herne (u64 __user *)reg->addr); 254246a6dd1cSJason J. herne break; 2543536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2544536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 2545536336c2SDominik Dingel (u64 __user *)reg->addr); 2546536336c2SDominik Dingel break; 2547536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2548536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 2549536336c2SDominik Dingel (u64 __user *)reg->addr); 2550536336c2SDominik Dingel break; 2551536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2552536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 2553536336c2SDominik Dingel (u64 __user *)reg->addr); 2554536336c2SDominik Dingel break; 2555672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2556672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 2557672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2558672550fbSChristian Borntraeger break; 2559afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2560afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 2561afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2562afa45ff5SChristian Borntraeger break; 256314eebd91SCarsten Otte default: 256414eebd91SCarsten Otte break; 256514eebd91SCarsten Otte } 256614eebd91SCarsten Otte 256714eebd91SCarsten Otte return r; 256814eebd91SCarsten Otte } 256914eebd91SCarsten Otte 257014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 257114eebd91SCarsten Otte struct kvm_one_reg *reg) 257214eebd91SCarsten Otte { 257314eebd91SCarsten Otte int r = -EINVAL; 25744287f247SDavid Hildenbrand __u64 val; 257514eebd91SCarsten Otte 257614eebd91SCarsten Otte switch (reg->id) { 257729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 257829b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 257929b7c71bSCarsten Otte (u32 __user *)reg->addr); 258029b7c71bSCarsten Otte break; 258129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 258229b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 258329b7c71bSCarsten Otte (u64 __user *)reg->addr); 258429b7c71bSCarsten Otte break; 258546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 25864287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 25874287f247SDavid Hildenbrand if (!r) 25884287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 258946a6dd1cSJason J. herne break; 259046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 259146a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 259246a6dd1cSJason J. herne (u64 __user *)reg->addr); 259346a6dd1cSJason J. herne break; 2594536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2595536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 2596536336c2SDominik Dingel (u64 __user *)reg->addr); 25979fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 25989fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2599536336c2SDominik Dingel break; 2600536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2601536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 2602536336c2SDominik Dingel (u64 __user *)reg->addr); 2603536336c2SDominik Dingel break; 2604536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2605536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 2606536336c2SDominik Dingel (u64 __user *)reg->addr); 2607536336c2SDominik Dingel break; 2608672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2609672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 2610672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2611672550fbSChristian Borntraeger break; 2612afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2613afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 2614afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2615afa45ff5SChristian Borntraeger break; 261614eebd91SCarsten Otte default: 261714eebd91SCarsten Otte break; 261814eebd91SCarsten Otte } 261914eebd91SCarsten Otte 262014eebd91SCarsten Otte return r; 262114eebd91SCarsten Otte } 2622b6d33834SChristoffer Dall 2623b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 2624b0c632dbSHeiko Carstens { 2625b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 2626b0c632dbSHeiko Carstens return 0; 2627b0c632dbSHeiko Carstens } 2628b0c632dbSHeiko Carstens 2629b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2630b0c632dbSHeiko Carstens { 26315a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 2632b0c632dbSHeiko Carstens return 0; 2633b0c632dbSHeiko Carstens } 2634b0c632dbSHeiko Carstens 2635b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2636b0c632dbSHeiko Carstens { 26375a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 2638b0c632dbSHeiko Carstens return 0; 2639b0c632dbSHeiko Carstens } 2640b0c632dbSHeiko Carstens 2641b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 2642b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2643b0c632dbSHeiko Carstens { 264459674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 2645b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 2646b0c632dbSHeiko Carstens return 0; 2647b0c632dbSHeiko Carstens } 2648b0c632dbSHeiko Carstens 2649b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 2650b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2651b0c632dbSHeiko Carstens { 265259674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 2653b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 2654b0c632dbSHeiko Carstens return 0; 2655b0c632dbSHeiko Carstens } 2656b0c632dbSHeiko Carstens 2657b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2658b0c632dbSHeiko Carstens { 26594725c860SMartin Schwidefsky if (test_fp_ctl(fpu->fpc)) 26604725c860SMartin Schwidefsky return -EINVAL; 2661e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 26629abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 2663a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 2664a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 26659abc2a08SDavid Hildenbrand else 2666a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 2667b0c632dbSHeiko Carstens return 0; 2668b0c632dbSHeiko Carstens } 2669b0c632dbSHeiko Carstens 2670b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2671b0c632dbSHeiko Carstens { 26729abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 26739abc2a08SDavid Hildenbrand save_fpu_regs(); 26749abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 2675a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 2676a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 26779abc2a08SDavid Hildenbrand else 2678a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 2679e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 2680b0c632dbSHeiko Carstens return 0; 2681b0c632dbSHeiko Carstens } 2682b0c632dbSHeiko Carstens 2683b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 2684b0c632dbSHeiko Carstens { 2685b0c632dbSHeiko Carstens int rc = 0; 2686b0c632dbSHeiko Carstens 26877a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 2688b0c632dbSHeiko Carstens rc = -EBUSY; 2689d7b0b5ebSCarsten Otte else { 2690d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 2691d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 2692d7b0b5ebSCarsten Otte } 2693b0c632dbSHeiko Carstens return rc; 2694b0c632dbSHeiko Carstens } 2695b0c632dbSHeiko Carstens 2696b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 2697b0c632dbSHeiko Carstens struct kvm_translation *tr) 2698b0c632dbSHeiko Carstens { 2699b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 2700b0c632dbSHeiko Carstens } 2701b0c632dbSHeiko Carstens 270227291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 270327291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 270427291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 270527291e21SDavid Hildenbrand 2706d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 2707d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 2708b0c632dbSHeiko Carstens { 270927291e21SDavid Hildenbrand int rc = 0; 271027291e21SDavid Hildenbrand 271127291e21SDavid Hildenbrand vcpu->guest_debug = 0; 271227291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 271327291e21SDavid Hildenbrand 27142de3bfc2SDavid Hildenbrand if (dbg->control & ~VALID_GUESTDBG_FLAGS) 271527291e21SDavid Hildenbrand return -EINVAL; 271689b5b4deSDavid Hildenbrand if (!sclp.has_gpere) 271789b5b4deSDavid Hildenbrand return -EINVAL; 271827291e21SDavid Hildenbrand 271927291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 272027291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 272127291e21SDavid Hildenbrand /* enforce guest PER */ 2722805de8f4SPeter Zijlstra atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 272327291e21SDavid Hildenbrand 272427291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 272527291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 272627291e21SDavid Hildenbrand } else { 2727805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 272827291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 272927291e21SDavid Hildenbrand } 273027291e21SDavid Hildenbrand 273127291e21SDavid Hildenbrand if (rc) { 273227291e21SDavid Hildenbrand vcpu->guest_debug = 0; 273327291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 2734805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 273527291e21SDavid Hildenbrand } 273627291e21SDavid Hildenbrand 273727291e21SDavid Hildenbrand return rc; 2738b0c632dbSHeiko Carstens } 2739b0c632dbSHeiko Carstens 274062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 274162d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 274262d9f0dbSMarcelo Tosatti { 27436352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 27446352e4d2SDavid Hildenbrand return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 27456352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 274662d9f0dbSMarcelo Tosatti } 274762d9f0dbSMarcelo Tosatti 274862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 274962d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 275062d9f0dbSMarcelo Tosatti { 27516352e4d2SDavid Hildenbrand int rc = 0; 27526352e4d2SDavid Hildenbrand 27536352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 27546352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 27556352e4d2SDavid Hildenbrand 27566352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 27576352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 27586352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 27596352e4d2SDavid Hildenbrand break; 27606352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 27616352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 27626352e4d2SDavid Hildenbrand break; 27636352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 27646352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 27656352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 27666352e4d2SDavid Hildenbrand default: 27676352e4d2SDavid Hildenbrand rc = -ENXIO; 27686352e4d2SDavid Hildenbrand } 27696352e4d2SDavid Hildenbrand 27706352e4d2SDavid Hildenbrand return rc; 277162d9f0dbSMarcelo Tosatti } 277262d9f0dbSMarcelo Tosatti 27738ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 27748ad35755SDavid Hildenbrand { 27758ad35755SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS; 27768ad35755SDavid Hildenbrand } 27778ad35755SDavid Hildenbrand 27782c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 27792c70fe44SChristian Borntraeger { 27808ad35755SDavid Hildenbrand retry: 27818e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 27822fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 2783586b7ccdSChristian Borntraeger return 0; 27842c70fe44SChristian Borntraeger /* 27852c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 2786b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 27872c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 27882c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 27892c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 27902c70fe44SChristian Borntraeger */ 27918ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 27922c70fe44SChristian Borntraeger int rc; 2793b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 2794fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 2795b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 2796aca411a4SJulius Niedworok if (rc) { 2797aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 27982c70fe44SChristian Borntraeger return rc; 2799aca411a4SJulius Niedworok } 28008ad35755SDavid Hildenbrand goto retry; 28012c70fe44SChristian Borntraeger } 28028ad35755SDavid Hildenbrand 2803d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 2804d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 2805d3d692c8SDavid Hildenbrand goto retry; 2806d3d692c8SDavid Hildenbrand } 2807d3d692c8SDavid Hildenbrand 28088ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 28098ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 28108ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 2811805de8f4SPeter Zijlstra atomic_or(CPUSTAT_IBS, 28128ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 28138ad35755SDavid Hildenbrand } 28148ad35755SDavid Hildenbrand goto retry; 28158ad35755SDavid Hildenbrand } 28168ad35755SDavid Hildenbrand 28178ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 28188ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 28198ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 2820805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_IBS, 28218ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 28228ad35755SDavid Hildenbrand } 28238ad35755SDavid Hildenbrand goto retry; 28248ad35755SDavid Hildenbrand } 28258ad35755SDavid Hildenbrand 28266502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 28276502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 28286502a34cSDavid Hildenbrand goto retry; 28296502a34cSDavid Hildenbrand } 28306502a34cSDavid Hildenbrand 2831190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 2832190df4a2SClaudio Imbrenda /* 2833190df4a2SClaudio Imbrenda * Disable CMMA virtualization; we will emulate the ESSA 2834190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 2835190df4a2SClaudio Imbrenda * functionalities needed for live migration. 2836190df4a2SClaudio Imbrenda */ 2837190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 2838190df4a2SClaudio Imbrenda goto retry; 2839190df4a2SClaudio Imbrenda } 2840190df4a2SClaudio Imbrenda 2841190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 2842190df4a2SClaudio Imbrenda /* 2843190df4a2SClaudio Imbrenda * Re-enable CMMA virtualization if CMMA is available and 2844190df4a2SClaudio Imbrenda * was used. 2845190df4a2SClaudio Imbrenda */ 2846190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 2847190df4a2SClaudio Imbrenda (vcpu->kvm->mm->context.use_cmma)) 2848190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 2849190df4a2SClaudio Imbrenda goto retry; 2850190df4a2SClaudio Imbrenda } 2851190df4a2SClaudio Imbrenda 28520759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 285372875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 28540759d068SDavid Hildenbrand 28552c70fe44SChristian Borntraeger return 0; 28562c70fe44SChristian Borntraeger } 28572c70fe44SChristian Borntraeger 285825ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod) 285925ed1675SDavid Hildenbrand { 286025ed1675SDavid Hildenbrand struct kvm_vcpu *vcpu; 286125ed1675SDavid Hildenbrand int i; 286225ed1675SDavid Hildenbrand 286325ed1675SDavid Hildenbrand mutex_lock(&kvm->lock); 286425ed1675SDavid Hildenbrand preempt_disable(); 286525ed1675SDavid Hildenbrand kvm->arch.epoch = tod - get_tod_clock(); 286625ed1675SDavid Hildenbrand kvm_s390_vcpu_block_all(kvm); 286725ed1675SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) 286825ed1675SDavid Hildenbrand vcpu->arch.sie_block->epoch = kvm->arch.epoch; 286925ed1675SDavid Hildenbrand kvm_s390_vcpu_unblock_all(kvm); 287025ed1675SDavid Hildenbrand preempt_enable(); 287125ed1675SDavid Hildenbrand mutex_unlock(&kvm->lock); 287225ed1675SDavid Hildenbrand } 287325ed1675SDavid Hildenbrand 2874fa576c58SThomas Huth /** 2875fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 2876fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 2877fa576c58SThomas Huth * @gpa: Guest physical address 2878fa576c58SThomas Huth * @writable: Whether the page should be writable or not 2879fa576c58SThomas Huth * 2880fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 2881fa576c58SThomas Huth * 2882fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 2883fa576c58SThomas Huth */ 2884fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 288524eb3a82SDominik Dingel { 2886527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 2887527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 288824eb3a82SDominik Dingel } 288924eb3a82SDominik Dingel 28903c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 28913c038e6bSDominik Dingel unsigned long token) 28923c038e6bSDominik Dingel { 28933c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 2894383d0b05SJens Freimann struct kvm_s390_irq irq; 28953c038e6bSDominik Dingel 28963c038e6bSDominik Dingel if (start_token) { 2897383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 2898383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 2899383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 29003c038e6bSDominik Dingel } else { 29013c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 2902383d0b05SJens Freimann inti.parm64 = token; 29033c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 29043c038e6bSDominik Dingel } 29053c038e6bSDominik Dingel } 29063c038e6bSDominik Dingel 29073c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 29083c038e6bSDominik Dingel struct kvm_async_pf *work) 29093c038e6bSDominik Dingel { 29103c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 29113c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 29123c038e6bSDominik Dingel } 29133c038e6bSDominik Dingel 29143c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 29153c038e6bSDominik Dingel struct kvm_async_pf *work) 29163c038e6bSDominik Dingel { 29173c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 29183c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 29193c038e6bSDominik Dingel } 29203c038e6bSDominik Dingel 29213c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 29223c038e6bSDominik Dingel struct kvm_async_pf *work) 29233c038e6bSDominik Dingel { 29243c038e6bSDominik Dingel /* s390 will always inject the page directly */ 29253c038e6bSDominik Dingel } 29263c038e6bSDominik Dingel 29273c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 29283c038e6bSDominik Dingel { 29293c038e6bSDominik Dingel /* 29303c038e6bSDominik Dingel * s390 will always inject the page directly, 29313c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 29323c038e6bSDominik Dingel */ 29333c038e6bSDominik Dingel return true; 29343c038e6bSDominik Dingel } 29353c038e6bSDominik Dingel 29363c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 29373c038e6bSDominik Dingel { 29383c038e6bSDominik Dingel hva_t hva; 29393c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 29403c038e6bSDominik Dingel int rc; 29413c038e6bSDominik Dingel 29423c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 29433c038e6bSDominik Dingel return 0; 29443c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 29453c038e6bSDominik Dingel vcpu->arch.pfault_compare) 29463c038e6bSDominik Dingel return 0; 29473c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 29483c038e6bSDominik Dingel return 0; 29499a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 29503c038e6bSDominik Dingel return 0; 29513c038e6bSDominik Dingel if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) 29523c038e6bSDominik Dingel return 0; 29533c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 29543c038e6bSDominik Dingel return 0; 29553c038e6bSDominik Dingel 295681480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 295781480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 295881480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 29593c038e6bSDominik Dingel return 0; 29603c038e6bSDominik Dingel 29613c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 29623c038e6bSDominik Dingel return rc; 29633c038e6bSDominik Dingel } 29643c038e6bSDominik Dingel 29653fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 2966b0c632dbSHeiko Carstens { 29673fb4c40fSThomas Huth int rc, cpuflags; 2968e168bf8dSCarsten Otte 29693c038e6bSDominik Dingel /* 29703c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 29713c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 29723c038e6bSDominik Dingel * handled outside the worker. 29733c038e6bSDominik Dingel */ 29743c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 29753c038e6bSDominik Dingel 29767ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 29777ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 2978b0c632dbSHeiko Carstens 2979b0c632dbSHeiko Carstens if (need_resched()) 2980b0c632dbSHeiko Carstens schedule(); 2981b0c632dbSHeiko Carstens 2982d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 298371cde587SChristian Borntraeger s390_handle_mcck(); 298471cde587SChristian Borntraeger 298579395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 298679395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 298779395031SJens Freimann if (rc) 298879395031SJens Freimann return rc; 298979395031SJens Freimann } 29900ff31867SCarsten Otte 29912c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 29922c70fe44SChristian Borntraeger if (rc) 29932c70fe44SChristian Borntraeger return rc; 29942c70fe44SChristian Borntraeger 299527291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 299627291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 299727291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 299827291e21SDavid Hildenbrand } 299927291e21SDavid Hildenbrand 3000b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 30013fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 30023fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 30033fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 30042b29a9fdSDominik Dingel 30053fb4c40fSThomas Huth return 0; 30063fb4c40fSThomas Huth } 30073fb4c40fSThomas Huth 3008492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 3009492d8642SThomas Huth { 301056317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 301156317920SDavid Hildenbrand .code = PGM_ADDRESSING, 301256317920SDavid Hildenbrand }; 301356317920SDavid Hildenbrand u8 opcode, ilen; 3014492d8642SThomas Huth int rc; 3015492d8642SThomas Huth 3016492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 3017492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 3018492d8642SThomas Huth 3019492d8642SThomas Huth /* 3020492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 3021492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 3022492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 3023492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 3024492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 3025492d8642SThomas Huth * to be able to forward the PSW. 3026492d8642SThomas Huth */ 30273fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 302856317920SDavid Hildenbrand ilen = insn_length(opcode); 30299b0d721aSDavid Hildenbrand if (rc < 0) { 30309b0d721aSDavid Hildenbrand return rc; 30319b0d721aSDavid Hildenbrand } else if (rc) { 30329b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 30339b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 30349b0d721aSDavid Hildenbrand * nullification if necessary. 30359b0d721aSDavid Hildenbrand */ 30369b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 30379b0d721aSDavid Hildenbrand ilen = 4; 30389b0d721aSDavid Hildenbrand } 303956317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 304056317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 304156317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 3042492d8642SThomas Huth } 3043492d8642SThomas Huth 30443fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 30453fb4c40fSThomas Huth { 30464d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 30474d62fcc0SQingFeng Hao struct sie_page *sie_page; 30484d62fcc0SQingFeng Hao 30492b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 30502b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 30512b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 30522b29a9fdSDominik Dingel 305327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 305427291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 305527291e21SDavid Hildenbrand 30567ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 30577ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 305871f116bfSDavid Hildenbrand 30594d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 30604d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 30614d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 30624d62fcc0SQingFeng Hao struct sie_page, sie_block); 30634d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 30644d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 30654d62fcc0SQingFeng Hao return 0; 30664d62fcc0SQingFeng Hao } 30674d62fcc0SQingFeng Hao 306871f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 306971f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 307071f116bfSDavid Hildenbrand 307171f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 307271f116bfSDavid Hildenbrand return rc; 307371f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 307471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 307571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 307671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 307771f116bfSDavid Hildenbrand return -EREMOTE; 307871f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 307971f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 308071f116bfSDavid Hildenbrand return 0; 3081210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 3082210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 3083210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 3084210b1607SThomas Huth current->thread.gmap_addr; 3085210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 308671f116bfSDavid Hildenbrand return -EREMOTE; 308724eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 30883c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 308924eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 309071f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 309171f116bfSDavid Hildenbrand return 0; 309271f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 3093fa576c58SThomas Huth } 309471f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 30953fb4c40fSThomas Huth } 30963fb4c40fSThomas Huth 30973fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 30983fb4c40fSThomas Huth { 30993fb4c40fSThomas Huth int rc, exit_reason; 31003fb4c40fSThomas Huth 3101800c1065SThomas Huth /* 3102800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 3103800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 3104800c1065SThomas Huth */ 3105800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 3106800c1065SThomas Huth 3107a76ccff6SThomas Huth do { 31083fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 31093fb4c40fSThomas Huth if (rc) 3110a76ccff6SThomas Huth break; 31113fb4c40fSThomas Huth 3112800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 31133fb4c40fSThomas Huth /* 3114a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 3115a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 31163fb4c40fSThomas Huth */ 31170097d12eSChristian Borntraeger local_irq_disable(); 31186edaa530SPaolo Bonzini guest_enter_irqoff(); 3119db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 31200097d12eSChristian Borntraeger local_irq_enable(); 3121a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 3122a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 31230097d12eSChristian Borntraeger local_irq_disable(); 3124db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 31256edaa530SPaolo Bonzini guest_exit_irqoff(); 31260097d12eSChristian Borntraeger local_irq_enable(); 3127800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 31283fb4c40fSThomas Huth 31293fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 313027291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 31313fb4c40fSThomas Huth 3132800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 3133e168bf8dSCarsten Otte return rc; 3134b0c632dbSHeiko Carstens } 3135b0c632dbSHeiko Carstens 3136b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3137b028ee3eSDavid Hildenbrand { 31384d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 31394e0b1ab7SFan Zhang struct gs_cb *gscb; 31404d5f2c04SChristian Borntraeger 31414d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 31424e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 3143b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 3144b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 3145b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 3146b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 3147b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 3148b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 3149d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 3150d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 3151b028ee3eSDavid Hildenbrand } 3152b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 31534287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 3154b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 3155b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 3156b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 3157b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 3158b028ee3eSDavid Hildenbrand } 3159b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 3160b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 3161b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 3162b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 31639fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 31649fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3165b028ee3eSDavid Hildenbrand } 316680cd8763SFan Zhang /* 316780cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 316880cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 316980cd8763SFan Zhang */ 317080cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 31714d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 31724d5f2c04SChristian Borntraeger riccb->valid && 31730c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 31744d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 31750c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 317680cd8763SFan Zhang } 31774e0b1ab7SFan Zhang /* 31784e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 31794e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 31804e0b1ab7SFan Zhang */ 31814e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 31824e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 31834e0b1ab7SFan Zhang gscb->gssm && 31844e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 31854e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 31864e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 31874e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 31884e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 318980cd8763SFan Zhang } 319031d8b8d4SChristian Borntraeger save_access_regs(vcpu->arch.host_acrs); 319131d8b8d4SChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 3192e1788bb9SChristian Borntraeger /* save host (userspace) fprs/vrs */ 3193e1788bb9SChristian Borntraeger save_fpu_regs(); 3194e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 3195e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 3196e1788bb9SChristian Borntraeger if (MACHINE_HAS_VX) 3197e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.vrs; 3198e1788bb9SChristian Borntraeger else 3199e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.fprs; 3200e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 3201e1788bb9SChristian Borntraeger if (test_fp_ctl(current->thread.fpu.fpc)) 3202e1788bb9SChristian Borntraeger /* User space provided an invalid FPC, let's clear it */ 3203e1788bb9SChristian Borntraeger current->thread.fpu.fpc = 0; 32044e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 32054e0b1ab7SFan Zhang preempt_disable(); 32064e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 32074e0b1ab7SFan Zhang if (current->thread.gs_cb) { 32084e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 32094e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 32104e0b1ab7SFan Zhang } 32114e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 32124e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 32134e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 32144e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 32154e0b1ab7SFan Zhang } 32164e0b1ab7SFan Zhang preempt_enable(); 32174e0b1ab7SFan Zhang } 321880cd8763SFan Zhang 3219b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 3220b028ee3eSDavid Hildenbrand } 3221b028ee3eSDavid Hildenbrand 3222b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3223b028ee3eSDavid Hildenbrand { 3224b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 3225b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 3226b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 3227b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 32284287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 3229b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 3230b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 3231b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 3232b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 3233b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 3234b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 3235b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 323631d8b8d4SChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 323731d8b8d4SChristian Borntraeger restore_access_regs(vcpu->arch.host_acrs); 3238e1788bb9SChristian Borntraeger /* Save guest register state */ 3239e1788bb9SChristian Borntraeger save_fpu_regs(); 3240e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 3241e1788bb9SChristian Borntraeger /* Restore will be done lazily at return */ 3242e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 3243e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 32444e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 32454e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 32464e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 32474e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 32484e0b1ab7SFan Zhang preempt_disable(); 32494e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 32504e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 32514e0b1ab7SFan Zhang preempt_enable(); 32524e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 32534e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 32544e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 32554e0b1ab7SFan Zhang } 3256e1788bb9SChristian Borntraeger 3257b028ee3eSDavid Hildenbrand } 3258b028ee3eSDavid Hildenbrand 3259b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3260b0c632dbSHeiko Carstens { 32618f2abe6aSChristian Borntraeger int rc; 3262b0c632dbSHeiko Carstens sigset_t sigsaved; 3263b0c632dbSHeiko Carstens 3264460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 3265460df4c1SPaolo Bonzini return -EINTR; 3266460df4c1SPaolo Bonzini 326727291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 326827291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 326927291e21SDavid Hildenbrand return 0; 327027291e21SDavid Hildenbrand } 327127291e21SDavid Hildenbrand 3272b0c632dbSHeiko Carstens if (vcpu->sigset_active) 3273b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 3274b0c632dbSHeiko Carstens 32756352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 32766852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 32776352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 3278ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 32796352e4d2SDavid Hildenbrand vcpu->vcpu_id); 32806352e4d2SDavid Hildenbrand return -EINVAL; 32816352e4d2SDavid Hildenbrand } 3282b0c632dbSHeiko Carstens 3283b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 3284db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 3285d7b0b5ebSCarsten Otte 3286dab4079dSHeiko Carstens might_fault(); 3287e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 32889ace903dSChristian Ehrhardt 3289b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 3290b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 32918f2abe6aSChristian Borntraeger rc = -EINTR; 3292b1d16c49SChristian Ehrhardt } 32938f2abe6aSChristian Borntraeger 329427291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 329527291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 329627291e21SDavid Hildenbrand rc = 0; 329727291e21SDavid Hildenbrand } 329827291e21SDavid Hildenbrand 32998f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 330071f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 33018f2abe6aSChristian Borntraeger rc = 0; 33028f2abe6aSChristian Borntraeger } 33038f2abe6aSChristian Borntraeger 3304db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 3305b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 3306d7b0b5ebSCarsten Otte 3307b0c632dbSHeiko Carstens if (vcpu->sigset_active) 3308b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 3309b0c632dbSHeiko Carstens 3310b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 33117e8e6ab4SHeiko Carstens return rc; 3312b0c632dbSHeiko Carstens } 3313b0c632dbSHeiko Carstens 3314b0c632dbSHeiko Carstens /* 3315b0c632dbSHeiko Carstens * store status at address 3316b0c632dbSHeiko Carstens * we use have two special cases: 3317b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 3318b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 3319b0c632dbSHeiko Carstens */ 3320d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 3321b0c632dbSHeiko Carstens { 3322092670cdSCarsten Otte unsigned char archmode = 1; 33239abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 3324fda902cbSMichael Mueller unsigned int px; 33254287f247SDavid Hildenbrand u64 clkcomp, cputm; 3326d0bce605SHeiko Carstens int rc; 3327b0c632dbSHeiko Carstens 3328d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 3329d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 3330d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 3331b0c632dbSHeiko Carstens return -EFAULT; 3332d9a3a09aSMartin Schwidefsky gpa = 0; 3333d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 3334d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 3335b0c632dbSHeiko Carstens return -EFAULT; 3336d9a3a09aSMartin Schwidefsky gpa = px; 3337d9a3a09aSMartin Schwidefsky } else 3338d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 33399abc2a08SDavid Hildenbrand 33409abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 33419abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 33429522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 3343d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 33449abc2a08SDavid Hildenbrand fprs, 128); 33459abc2a08SDavid Hildenbrand } else { 33469abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 33476fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 33489abc2a08SDavid Hildenbrand } 3349d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 3350d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 3351d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 3352d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 3353d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 3354fda902cbSMichael Mueller &px, 4); 3355d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 33569abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 3357d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 3358d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 33594287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 3360d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 33614287f247SDavid Hildenbrand &cputm, 8); 3362178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 3363d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 3364d0bce605SHeiko Carstens &clkcomp, 8); 3365d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 3366d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 3367d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 3368d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 3369d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 3370b0c632dbSHeiko Carstens } 3371b0c632dbSHeiko Carstens 3372e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 3373e879892cSThomas Huth { 3374e879892cSThomas Huth /* 3375e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 337631d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 3377e879892cSThomas Huth * it into the save area 3378e879892cSThomas Huth */ 3379d0164ee2SHendrik Brueckner save_fpu_regs(); 33809abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 3381e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 3382e879892cSThomas Huth 3383e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 3384e879892cSThomas Huth } 3385e879892cSThomas Huth 33868ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 33878ad35755SDavid Hildenbrand { 33888ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 33898e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 33908ad35755SDavid Hildenbrand } 33918ad35755SDavid Hildenbrand 33928ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 33938ad35755SDavid Hildenbrand { 33948ad35755SDavid Hildenbrand unsigned int i; 33958ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 33968ad35755SDavid Hildenbrand 33978ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 33988ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 33998ad35755SDavid Hildenbrand } 34008ad35755SDavid Hildenbrand } 34018ad35755SDavid Hildenbrand 34028ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 34038ad35755SDavid Hildenbrand { 340409a400e7SDavid Hildenbrand if (!sclp.has_ibs) 340509a400e7SDavid Hildenbrand return; 34068ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 34078e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 34088ad35755SDavid Hildenbrand } 34098ad35755SDavid Hildenbrand 34106852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 34116852d7b6SDavid Hildenbrand { 34128ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 34138ad35755SDavid Hildenbrand 34148ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 34158ad35755SDavid Hildenbrand return; 34168ad35755SDavid Hildenbrand 34176852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 34188ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 3419433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 34208ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 34218ad35755SDavid Hildenbrand 34228ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 34238ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 34248ad35755SDavid Hildenbrand started_vcpus++; 34258ad35755SDavid Hildenbrand } 34268ad35755SDavid Hildenbrand 34278ad35755SDavid Hildenbrand if (started_vcpus == 0) { 34288ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 34298ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 34308ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 34318ad35755SDavid Hildenbrand /* 34328ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 34338ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 34348ad35755SDavid Hildenbrand * oustanding ENABLE requests. 34358ad35755SDavid Hildenbrand */ 34368ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 34378ad35755SDavid Hildenbrand } 34388ad35755SDavid Hildenbrand 3439805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 34408ad35755SDavid Hildenbrand /* 34418ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 34428ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 34438ad35755SDavid Hildenbrand */ 3444d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 3445433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 34468ad35755SDavid Hildenbrand return; 34476852d7b6SDavid Hildenbrand } 34486852d7b6SDavid Hildenbrand 34496852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 34506852d7b6SDavid Hildenbrand { 34518ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 34528ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 34538ad35755SDavid Hildenbrand 34548ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 34558ad35755SDavid Hildenbrand return; 34568ad35755SDavid Hildenbrand 34576852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 34588ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 3459433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 34608ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 34618ad35755SDavid Hildenbrand 346232f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 34636cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 346432f5ff63SDavid Hildenbrand 3465805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 34668ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 34678ad35755SDavid Hildenbrand 34688ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 34698ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 34708ad35755SDavid Hildenbrand started_vcpus++; 34718ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 34728ad35755SDavid Hildenbrand } 34738ad35755SDavid Hildenbrand } 34748ad35755SDavid Hildenbrand 34758ad35755SDavid Hildenbrand if (started_vcpus == 1) { 34768ad35755SDavid Hildenbrand /* 34778ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 34788ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 34798ad35755SDavid Hildenbrand */ 34808ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 34818ad35755SDavid Hildenbrand } 34828ad35755SDavid Hildenbrand 3483433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 34848ad35755SDavid Hildenbrand return; 34856852d7b6SDavid Hildenbrand } 34866852d7b6SDavid Hildenbrand 3487d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 3488d6712df9SCornelia Huck struct kvm_enable_cap *cap) 3489d6712df9SCornelia Huck { 3490d6712df9SCornelia Huck int r; 3491d6712df9SCornelia Huck 3492d6712df9SCornelia Huck if (cap->flags) 3493d6712df9SCornelia Huck return -EINVAL; 3494d6712df9SCornelia Huck 3495d6712df9SCornelia Huck switch (cap->cap) { 3496fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 3497fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 3498fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 3499c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 3500fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 3501fa6b7fe9SCornelia Huck } 3502fa6b7fe9SCornelia Huck r = 0; 3503fa6b7fe9SCornelia Huck break; 3504d6712df9SCornelia Huck default: 3505d6712df9SCornelia Huck r = -EINVAL; 3506d6712df9SCornelia Huck break; 3507d6712df9SCornelia Huck } 3508d6712df9SCornelia Huck return r; 3509d6712df9SCornelia Huck } 3510d6712df9SCornelia Huck 351141408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 351241408c28SThomas Huth struct kvm_s390_mem_op *mop) 351341408c28SThomas Huth { 351441408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 351541408c28SThomas Huth void *tmpbuf = NULL; 351641408c28SThomas Huth int r, srcu_idx; 351741408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 351841408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 351941408c28SThomas Huth 352041408c28SThomas Huth if (mop->flags & ~supported_flags) 352141408c28SThomas Huth return -EINVAL; 352241408c28SThomas Huth 352341408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 352441408c28SThomas Huth return -E2BIG; 352541408c28SThomas Huth 352641408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 352741408c28SThomas Huth tmpbuf = vmalloc(mop->size); 352841408c28SThomas Huth if (!tmpbuf) 352941408c28SThomas Huth return -ENOMEM; 353041408c28SThomas Huth } 353141408c28SThomas Huth 353241408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 353341408c28SThomas Huth 353441408c28SThomas Huth switch (mop->op) { 353541408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 353641408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 353792c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 353892c96321SDavid Hildenbrand mop->size, GACC_FETCH); 353941408c28SThomas Huth break; 354041408c28SThomas Huth } 354141408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 354241408c28SThomas Huth if (r == 0) { 354341408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 354441408c28SThomas Huth r = -EFAULT; 354541408c28SThomas Huth } 354641408c28SThomas Huth break; 354741408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 354841408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 354992c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 355092c96321SDavid Hildenbrand mop->size, GACC_STORE); 355141408c28SThomas Huth break; 355241408c28SThomas Huth } 355341408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 355441408c28SThomas Huth r = -EFAULT; 355541408c28SThomas Huth break; 355641408c28SThomas Huth } 355741408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 355841408c28SThomas Huth break; 355941408c28SThomas Huth default: 356041408c28SThomas Huth r = -EINVAL; 356141408c28SThomas Huth } 356241408c28SThomas Huth 356341408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 356441408c28SThomas Huth 356541408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 356641408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 356741408c28SThomas Huth 356841408c28SThomas Huth vfree(tmpbuf); 356941408c28SThomas Huth return r; 357041408c28SThomas Huth } 357141408c28SThomas Huth 3572b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 3573b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 3574b0c632dbSHeiko Carstens { 3575b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 3576b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 3577800c1065SThomas Huth int idx; 3578bc923cc9SAvi Kivity long r; 3579b0c632dbSHeiko Carstens 358093736624SAvi Kivity switch (ioctl) { 358147b43c52SJens Freimann case KVM_S390_IRQ: { 358247b43c52SJens Freimann struct kvm_s390_irq s390irq; 358347b43c52SJens Freimann 358447b43c52SJens Freimann r = -EFAULT; 358547b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 358647b43c52SJens Freimann break; 358747b43c52SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 358847b43c52SJens Freimann break; 358947b43c52SJens Freimann } 359093736624SAvi Kivity case KVM_S390_INTERRUPT: { 3591ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 3592383d0b05SJens Freimann struct kvm_s390_irq s390irq; 3593ba5c1e9bSCarsten Otte 359493736624SAvi Kivity r = -EFAULT; 3595ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 359693736624SAvi Kivity break; 3597383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 3598383d0b05SJens Freimann return -EINVAL; 3599383d0b05SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 360093736624SAvi Kivity break; 3601ba5c1e9bSCarsten Otte } 3602b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 3603800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 3604bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 3605800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 3606bc923cc9SAvi Kivity break; 3607b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 3608b0c632dbSHeiko Carstens psw_t psw; 3609b0c632dbSHeiko Carstens 3610bc923cc9SAvi Kivity r = -EFAULT; 3611b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 3612bc923cc9SAvi Kivity break; 3613bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 3614bc923cc9SAvi Kivity break; 3615b0c632dbSHeiko Carstens } 3616b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 3617bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 3618bc923cc9SAvi Kivity break; 361914eebd91SCarsten Otte case KVM_SET_ONE_REG: 362014eebd91SCarsten Otte case KVM_GET_ONE_REG: { 362114eebd91SCarsten Otte struct kvm_one_reg reg; 362214eebd91SCarsten Otte r = -EFAULT; 362314eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 362414eebd91SCarsten Otte break; 362514eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 362614eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 362714eebd91SCarsten Otte else 362814eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 362914eebd91SCarsten Otte break; 363014eebd91SCarsten Otte } 363127e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 363227e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 363327e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 363427e0393fSCarsten Otte 363527e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 363627e0393fSCarsten Otte r = -EFAULT; 363727e0393fSCarsten Otte break; 363827e0393fSCarsten Otte } 363927e0393fSCarsten Otte 364027e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 364127e0393fSCarsten Otte r = -EINVAL; 364227e0393fSCarsten Otte break; 364327e0393fSCarsten Otte } 364427e0393fSCarsten Otte 364527e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 364627e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 364727e0393fSCarsten Otte break; 364827e0393fSCarsten Otte } 364927e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 365027e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 365127e0393fSCarsten Otte 365227e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 365327e0393fSCarsten Otte r = -EFAULT; 365427e0393fSCarsten Otte break; 365527e0393fSCarsten Otte } 365627e0393fSCarsten Otte 365727e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 365827e0393fSCarsten Otte r = -EINVAL; 365927e0393fSCarsten Otte break; 366027e0393fSCarsten Otte } 366127e0393fSCarsten Otte 366227e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 366327e0393fSCarsten Otte ucasmap.length); 366427e0393fSCarsten Otte break; 366527e0393fSCarsten Otte } 366627e0393fSCarsten Otte #endif 3667ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 3668527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 3669ccc7910fSCarsten Otte break; 3670ccc7910fSCarsten Otte } 3671d6712df9SCornelia Huck case KVM_ENABLE_CAP: 3672d6712df9SCornelia Huck { 3673d6712df9SCornelia Huck struct kvm_enable_cap cap; 3674d6712df9SCornelia Huck r = -EFAULT; 3675d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 3676d6712df9SCornelia Huck break; 3677d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 3678d6712df9SCornelia Huck break; 3679d6712df9SCornelia Huck } 368041408c28SThomas Huth case KVM_S390_MEM_OP: { 368141408c28SThomas Huth struct kvm_s390_mem_op mem_op; 368241408c28SThomas Huth 368341408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 368441408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 368541408c28SThomas Huth else 368641408c28SThomas Huth r = -EFAULT; 368741408c28SThomas Huth break; 368841408c28SThomas Huth } 3689816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 3690816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3691816c7667SJens Freimann 3692816c7667SJens Freimann r = -EFAULT; 3693816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3694816c7667SJens Freimann break; 3695816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 3696816c7667SJens Freimann irq_state.len == 0 || 3697816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 3698816c7667SJens Freimann r = -EINVAL; 3699816c7667SJens Freimann break; 3700816c7667SJens Freimann } 3701816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 3702816c7667SJens Freimann (void __user *) irq_state.buf, 3703816c7667SJens Freimann irq_state.len); 3704816c7667SJens Freimann break; 3705816c7667SJens Freimann } 3706816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 3707816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3708816c7667SJens Freimann 3709816c7667SJens Freimann r = -EFAULT; 3710816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3711816c7667SJens Freimann break; 3712816c7667SJens Freimann if (irq_state.len == 0) { 3713816c7667SJens Freimann r = -EINVAL; 3714816c7667SJens Freimann break; 3715816c7667SJens Freimann } 3716816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 3717816c7667SJens Freimann (__u8 __user *) irq_state.buf, 3718816c7667SJens Freimann irq_state.len); 3719816c7667SJens Freimann break; 3720816c7667SJens Freimann } 3721b0c632dbSHeiko Carstens default: 37223e6afcf1SCarsten Otte r = -ENOTTY; 3723b0c632dbSHeiko Carstens } 3724bc923cc9SAvi Kivity return r; 3725b0c632dbSHeiko Carstens } 3726b0c632dbSHeiko Carstens 37275b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 37285b1c1493SCarsten Otte { 37295b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 37305b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 37315b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 37325b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 37335b1c1493SCarsten Otte get_page(vmf->page); 37345b1c1493SCarsten Otte return 0; 37355b1c1493SCarsten Otte } 37365b1c1493SCarsten Otte #endif 37375b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 37385b1c1493SCarsten Otte } 37395b1c1493SCarsten Otte 37405587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 37415587027cSAneesh Kumar K.V unsigned long npages) 3742db3fe4ebSTakuya Yoshikawa { 3743db3fe4ebSTakuya Yoshikawa return 0; 3744db3fe4ebSTakuya Yoshikawa } 3745db3fe4ebSTakuya Yoshikawa 3746b0c632dbSHeiko Carstens /* Section: memory related */ 3747f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 3748f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 374909170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 37507b6195a9STakuya Yoshikawa enum kvm_mr_change change) 3751b0c632dbSHeiko Carstens { 3752dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 3753dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 3754dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 3755dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 3756b0c632dbSHeiko Carstens 3757598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 3758b0c632dbSHeiko Carstens return -EINVAL; 3759b0c632dbSHeiko Carstens 3760598841caSCarsten Otte if (mem->memory_size & 0xffffful) 3761b0c632dbSHeiko Carstens return -EINVAL; 3762b0c632dbSHeiko Carstens 3763a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 3764a3a92c31SDominik Dingel return -EINVAL; 3765a3a92c31SDominik Dingel 3766f7784b8eSMarcelo Tosatti return 0; 3767f7784b8eSMarcelo Tosatti } 3768f7784b8eSMarcelo Tosatti 3769f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 377009170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 37718482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 3772f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 37738482644aSTakuya Yoshikawa enum kvm_mr_change change) 3774f7784b8eSMarcelo Tosatti { 3775f7850c92SCarsten Otte int rc; 3776f7784b8eSMarcelo Tosatti 37772cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 37782cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 37792cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 37802cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 37812cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 37822cef4debSChristian Borntraeger */ 37832cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 37842cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 37852cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 37862cef4debSChristian Borntraeger return; 3787598841caSCarsten Otte 3788598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 3789598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 3790598841caSCarsten Otte if (rc) 3791ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 3792598841caSCarsten Otte return; 3793b0c632dbSHeiko Carstens } 3794b0c632dbSHeiko Carstens 379560a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 379660a37709SAlexander Yarygin { 379760a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 379860a37709SAlexander Yarygin 379960a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 380060a37709SAlexander Yarygin } 380160a37709SAlexander Yarygin 38023491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 38033491caf2SChristian Borntraeger { 38043491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 38053491caf2SChristian Borntraeger } 38063491caf2SChristian Borntraeger 3807b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 3808b0c632dbSHeiko Carstens { 380960a37709SAlexander Yarygin int i; 381060a37709SAlexander Yarygin 381107197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 381207197fd0SDavid Hildenbrand pr_info("SIE not available\n"); 381307197fd0SDavid Hildenbrand return -ENODEV; 381407197fd0SDavid Hildenbrand } 381507197fd0SDavid Hildenbrand 381660a37709SAlexander Yarygin for (i = 0; i < 16; i++) 381760a37709SAlexander Yarygin kvm_s390_fac_list_mask[i] |= 381860a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 381960a37709SAlexander Yarygin 38209d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 3821b0c632dbSHeiko Carstens } 3822b0c632dbSHeiko Carstens 3823b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 3824b0c632dbSHeiko Carstens { 3825b0c632dbSHeiko Carstens kvm_exit(); 3826b0c632dbSHeiko Carstens } 3827b0c632dbSHeiko Carstens 3828b0c632dbSHeiko Carstens module_init(kvm_s390_init); 3829b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 3830566af940SCornelia Huck 3831566af940SCornelia Huck /* 3832566af940SCornelia Huck * Enable autoloading of the kvm module. 3833566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 3834566af940SCornelia Huck * since x86 takes a different approach. 3835566af940SCornelia Huck */ 3836566af940SCornelia Huck #include <linux/miscdevice.h> 3837566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 3838566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 3839