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> 33174cd4b1SIngo Molnar 34cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 35b0c632dbSHeiko Carstens #include <asm/lowcore.h> 36fd5ada04SMartin Schwidefsky #include <asm/stp.h> 37b0c632dbSHeiko Carstens #include <asm/pgtable.h> 381e133ab2SMartin Schwidefsky #include <asm/gmap.h> 39f5daba1dSHeiko Carstens #include <asm/nmi.h> 40a0616cdeSDavid Howells #include <asm/switch_to.h> 416d3da241SJens Freimann #include <asm/isc.h> 421526bf9cSChristian Borntraeger #include <asm/sclp.h> 430a763c78SDavid Hildenbrand #include <asm/cpacf.h> 44221bb8a4SLinus Torvalds #include <asm/timex.h> 458f2abe6aSChristian Borntraeger #include "kvm-s390.h" 46b0c632dbSHeiko Carstens #include "gaccess.h" 47b0c632dbSHeiko Carstens 48ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390" 49ea2cdd27SDavid Hildenbrand #undef pr_fmt 50ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 51ea2cdd27SDavid Hildenbrand 525786fffaSCornelia Huck #define CREATE_TRACE_POINTS 535786fffaSCornelia Huck #include "trace.h" 54ade38c31SCornelia Huck #include "trace-s390.h" 555786fffaSCornelia Huck 5641408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 57816c7667SJens Freimann #define LOCAL_IRQS 32 58816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 59816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 6041408c28SThomas Huth 61b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 62b0c632dbSHeiko Carstens 63b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 64b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 650eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 668f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 678f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 688f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 698f2abe6aSChristian Borntraeger { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, 70ba5c1e9bSCarsten Otte { "exit_instruction", VCPU_STAT(exit_instruction) }, 719ec6de19SAlexander Yarygin { "exit_pei", VCPU_STAT(exit_pei) }, 72ba5c1e9bSCarsten Otte { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 73ba5c1e9bSCarsten Otte { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 74a011eeb2SJanosch Frank { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, 75f7819512SPaolo Bonzini { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 7662bea5bfSPaolo Bonzini { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 773491caf2SChristian Borntraeger { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 78ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 79f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 80ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 81aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 82aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 83ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 847697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 85ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 86ba5c1e9bSCarsten Otte { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, 87ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 88ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 89ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 90ba5c1e9bSCarsten Otte { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, 91ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 9269d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 93453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 94453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 95453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 96453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 97453423dcSChristian Borntraeger { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, 988a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 99453423dcSChristian Borntraeger { "instruction_stsch", VCPU_STAT(instruction_stsch) }, 100453423dcSChristian Borntraeger { "instruction_chsc", VCPU_STAT(instruction_chsc) }, 101b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 102453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 103453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 104bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 10595ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 106a3508fbeSDavid Hildenbrand { "instruction_sie", VCPU_STAT(instruction_sie) }, 1075288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 108bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1097697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1105288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 11142cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 11242cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1135288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 11442cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 11542cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 116cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1175288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1185288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1195288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 12042cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 12142cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 12242cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 123388186bcSChristian Borntraeger { "diagnose_10", VCPU_STAT(diagnose_10) }, 124e28acfeaSChristian Borntraeger { "diagnose_44", VCPU_STAT(diagnose_44) }, 12541628d33SKonstantin Weitz { "diagnose_9c", VCPU_STAT(diagnose_9c) }, 126175a5c9eSChristian Borntraeger { "diagnose_258", VCPU_STAT(diagnose_258) }, 127175a5c9eSChristian Borntraeger { "diagnose_308", VCPU_STAT(diagnose_308) }, 128175a5c9eSChristian Borntraeger { "diagnose_500", VCPU_STAT(diagnose_500) }, 129b0c632dbSHeiko Carstens { NULL } 130b0c632dbSHeiko Carstens }; 131b0c632dbSHeiko Carstens 132a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 133a411edf1SDavid Hildenbrand static int nested; 134a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 135a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 136a411edf1SDavid Hildenbrand 1379d8d5786SMichael Mueller /* upper facilities limit for kvm */ 138f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM }; 139b0c632dbSHeiko Carstens 1409d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void) 14178c4b59fSMichael Mueller { 1429d8d5786SMichael Mueller BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64); 1439d8d5786SMichael Mueller return ARRAY_SIZE(kvm_s390_fac_list_mask); 14478c4b59fSMichael Mueller } 14578c4b59fSMichael Mueller 14615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 14715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 1480a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 1490a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 15015c9705fSDavid Hildenbrand 1519d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 152a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 15378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 1549d8d5786SMichael Mueller 155b0c632dbSHeiko Carstens /* Section: not file related */ 15613a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 157b0c632dbSHeiko Carstens { 158b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 15910474ae8SAlexander Graf return 0; 160b0c632dbSHeiko Carstens } 161b0c632dbSHeiko Carstens 162414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 163414d3b07SMartin Schwidefsky unsigned long end); 1642c70fe44SChristian Borntraeger 165fdf03650SFan Zhang /* 166fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 167fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 168fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 169fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 170fdf03650SFan Zhang */ 171fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 172fdf03650SFan Zhang void *v) 173fdf03650SFan Zhang { 174fdf03650SFan Zhang struct kvm *kvm; 175fdf03650SFan Zhang struct kvm_vcpu *vcpu; 176fdf03650SFan Zhang int i; 177fdf03650SFan Zhang unsigned long long *delta = v; 178fdf03650SFan Zhang 179fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 180fdf03650SFan Zhang kvm->arch.epoch -= *delta; 181fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 182fdf03650SFan Zhang vcpu->arch.sie_block->epoch -= *delta; 183db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 184db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 18591473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 18691473b48SDavid Hildenbrand vcpu->arch.vsie_block->epoch -= *delta; 187fdf03650SFan Zhang } 188fdf03650SFan Zhang } 189fdf03650SFan Zhang return NOTIFY_OK; 190fdf03650SFan Zhang } 191fdf03650SFan Zhang 192fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 193fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 194fdf03650SFan Zhang }; 195fdf03650SFan Zhang 196b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 197b0c632dbSHeiko Carstens { 1982c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 199b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 200a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 201a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 202fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 203fdf03650SFan Zhang &kvm_clock_notifier); 204b0c632dbSHeiko Carstens return 0; 205b0c632dbSHeiko Carstens } 206b0c632dbSHeiko Carstens 207b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 208b0c632dbSHeiko Carstens { 209b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 210a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 211fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 212fdf03650SFan Zhang &kvm_clock_notifier); 213b0c632dbSHeiko Carstens } 214b0c632dbSHeiko Carstens 21522be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 21622be5a13SDavid Hildenbrand { 21722be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 21822be5a13SDavid Hildenbrand } 21922be5a13SDavid Hildenbrand 2200a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 2210a763c78SDavid Hildenbrand { 2220a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 223d051ae53SHeiko Carstens int cc; 2240a763c78SDavid Hildenbrand 2250a763c78SDavid Hildenbrand asm volatile( 2260a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 2270a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 2280a763c78SDavid Hildenbrand " ipm %0\n" 2290a763c78SDavid Hildenbrand " srl %0,28\n" 2300a763c78SDavid Hildenbrand : "=d" (cc) 2310a763c78SDavid Hildenbrand : "d" (r0) 2320a763c78SDavid Hildenbrand : "cc"); 2330a763c78SDavid Hildenbrand return cc == 0; 2340a763c78SDavid Hildenbrand } 2350a763c78SDavid Hildenbrand 23622be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 23722be5a13SDavid Hildenbrand { 2380a763c78SDavid Hildenbrand int i; 2390a763c78SDavid Hildenbrand 2400a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 2410a763c78SDavid Hildenbrand if (plo_test_bit(i)) 2420a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 2430a763c78SDavid Hildenbrand } 2440a763c78SDavid Hildenbrand 2450a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 246221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 247221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 248221bb8a4SLinus Torvalds PTFF_QAF); 2490a763c78SDavid Hildenbrand 2500a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 25169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 25269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 25369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 25469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 25569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 25669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 25769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 25869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 25969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 26069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 2610a763c78SDavid Hildenbrand } 2620a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 26369c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 26469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 2650a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 26669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 26769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 26869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 26969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 27069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 27169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 27269c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 27369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 2740a763c78SDavid Hildenbrand } 2750a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 27669c0e360SMartin Schwidefsky __cpacf_query(CPACF_PPNO, (cpacf_mask_t *) 27769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 2780a763c78SDavid Hildenbrand 27922be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 28022be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 281a3508fbeSDavid Hildenbrand /* 282a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 283a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 284a3508fbeSDavid Hildenbrand */ 285a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 286a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 287a3508fbeSDavid Hildenbrand return; 288a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 28919c439b5SDavid Hildenbrand if (sclp.has_64bscao) 29019c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 2910615a326SDavid Hildenbrand if (sclp.has_siif) 2920615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 29377d18f6dSDavid Hildenbrand if (sclp.has_gpere) 29477d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 295a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 296a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 2975630a8e8SDavid Hildenbrand if (sclp.has_ib) 2985630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 29913ee3f67SDavid Hildenbrand if (sclp.has_cei) 30013ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 3017fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 3027fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 3035d3876a8SDavid Hildenbrand /* 3045d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 3055d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 3065d3876a8SDavid Hildenbrand * instead of the real storage key. 3075d3876a8SDavid Hildenbrand * 3085d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 3095d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 3105d3876a8SDavid Hildenbrand * 3115d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 3125d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 3135d3876a8SDavid Hildenbrand * 3145d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 3155d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 3165d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 3175d3876a8SDavid Hildenbrand * 3185d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 3195d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 3205d3876a8SDavid Hildenbrand */ 32122be5a13SDavid Hildenbrand } 32222be5a13SDavid Hildenbrand 323b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 324b0c632dbSHeiko Carstens { 32578f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 32678f26131SChristian Borntraeger if (!kvm_s390_dbf) 32778f26131SChristian Borntraeger return -ENOMEM; 32878f26131SChristian Borntraeger 32978f26131SChristian Borntraeger if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { 33078f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 33178f26131SChristian Borntraeger return -ENOMEM; 33278f26131SChristian Borntraeger } 33378f26131SChristian Borntraeger 33422be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 33522be5a13SDavid Hildenbrand 33684877d93SCornelia Huck /* Register floating interrupt controller interface. */ 33784877d93SCornelia Huck return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 338b0c632dbSHeiko Carstens } 339b0c632dbSHeiko Carstens 34078f26131SChristian Borntraeger void kvm_arch_exit(void) 34178f26131SChristian Borntraeger { 34278f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 34378f26131SChristian Borntraeger } 34478f26131SChristian Borntraeger 345b0c632dbSHeiko Carstens /* Section: device related */ 346b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 347b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 348b0c632dbSHeiko Carstens { 349b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 350b0c632dbSHeiko Carstens return s390_enable_sie(); 351b0c632dbSHeiko Carstens return -EINVAL; 352b0c632dbSHeiko Carstens } 353b0c632dbSHeiko Carstens 354784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 355b0c632dbSHeiko Carstens { 356d7b0b5ebSCarsten Otte int r; 357d7b0b5ebSCarsten Otte 3582bd0ac4eSCarsten Otte switch (ext) { 359d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 360b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 36152e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 3621efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3631efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 3641efd0f59SCarsten Otte #endif 3653c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 36660b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 36714eebd91SCarsten Otte case KVM_CAP_ONE_REG: 368d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 369fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 37010ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 371c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 372d938dc55SCornelia Huck case KVM_CAP_ENABLE_CAP_VM: 37378599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 374f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 3756352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 376460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 37747b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 3782444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 379e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 38030ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 381816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 3826502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 383d7b0b5ebSCarsten Otte r = 1; 384d7b0b5ebSCarsten Otte break; 38541408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 38641408c28SThomas Huth r = MEM_OP_MAX_SIZE; 38741408c28SThomas Huth break; 388e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 389e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 39076a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 391a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 392a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 393a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 39476a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 395e726b1bdSChristian Borntraeger break; 396e1e2e605SNick Wang case KVM_CAP_NR_MEMSLOTS: 397e1e2e605SNick Wang r = KVM_USER_MEM_SLOTS; 398e1e2e605SNick Wang break; 3991526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 400abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 4011526bf9cSChristian Borntraeger break; 40268c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 40368c55750SEric Farman r = MACHINE_HAS_VX; 40468c55750SEric Farman break; 405c6e5f166SFan Zhang case KVM_CAP_S390_RI: 406c6e5f166SFan Zhang r = test_facility(64); 407c6e5f166SFan Zhang break; 408*4e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 409*4e0b1ab7SFan Zhang r = test_facility(133); 410*4e0b1ab7SFan Zhang break; 4112bd0ac4eSCarsten Otte default: 412d7b0b5ebSCarsten Otte r = 0; 413b0c632dbSHeiko Carstens } 414d7b0b5ebSCarsten Otte return r; 4152bd0ac4eSCarsten Otte } 416b0c632dbSHeiko Carstens 41715f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 41815f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 41915f36ebdSJason J. Herne { 42015f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 42115f36ebdSJason J. Herne unsigned long address; 42215f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 42315f36ebdSJason J. Herne 42415f36ebdSJason J. Herne /* Loop over all guest pages */ 42515f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 42615f36ebdSJason J. Herne for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) { 42715f36ebdSJason J. Herne address = gfn_to_hva_memslot(memslot, cur_gfn); 42815f36ebdSJason J. Herne 4291e133ab2SMartin Schwidefsky if (test_and_clear_guest_dirty(gmap->mm, address)) 43015f36ebdSJason J. Herne mark_page_dirty(kvm, cur_gfn); 4311763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 4321763f8d0SChristian Borntraeger return; 43370c88a00SChristian Borntraeger cond_resched(); 43415f36ebdSJason J. Herne } 43515f36ebdSJason J. Herne } 43615f36ebdSJason J. Herne 437b0c632dbSHeiko Carstens /* Section: vm related */ 438a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 439a6e2f683SEugene (jno) Dvurechenski 440b0c632dbSHeiko Carstens /* 441b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 442b0c632dbSHeiko Carstens */ 443b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 444b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 445b0c632dbSHeiko Carstens { 44615f36ebdSJason J. Herne int r; 44715f36ebdSJason J. Herne unsigned long n; 4489f6b8029SPaolo Bonzini struct kvm_memslots *slots; 44915f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 45015f36ebdSJason J. Herne int is_dirty = 0; 45115f36ebdSJason J. Herne 452e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 453e1e8a962SJanosch Frank return -EINVAL; 454e1e8a962SJanosch Frank 45515f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 45615f36ebdSJason J. Herne 45715f36ebdSJason J. Herne r = -EINVAL; 45815f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 45915f36ebdSJason J. Herne goto out; 46015f36ebdSJason J. Herne 4619f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 4629f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 46315f36ebdSJason J. Herne r = -ENOENT; 46415f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 46515f36ebdSJason J. Herne goto out; 46615f36ebdSJason J. Herne 46715f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 46815f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 46915f36ebdSJason J. Herne if (r) 47015f36ebdSJason J. Herne goto out; 47115f36ebdSJason J. Herne 47215f36ebdSJason J. Herne /* Clear the dirty log */ 47315f36ebdSJason J. Herne if (is_dirty) { 47415f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 47515f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 47615f36ebdSJason J. Herne } 47715f36ebdSJason J. Herne r = 0; 47815f36ebdSJason J. Herne out: 47915f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 48015f36ebdSJason J. Herne return r; 481b0c632dbSHeiko Carstens } 482b0c632dbSHeiko Carstens 4836502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 4846502a34cSDavid Hildenbrand { 4856502a34cSDavid Hildenbrand unsigned int i; 4866502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 4876502a34cSDavid Hildenbrand 4886502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 4896502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 4906502a34cSDavid Hildenbrand } 4916502a34cSDavid Hildenbrand } 4926502a34cSDavid Hildenbrand 493d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 494d938dc55SCornelia Huck { 495d938dc55SCornelia Huck int r; 496d938dc55SCornelia Huck 497d938dc55SCornelia Huck if (cap->flags) 498d938dc55SCornelia Huck return -EINVAL; 499d938dc55SCornelia Huck 500d938dc55SCornelia Huck switch (cap->cap) { 50184223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 502c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 50384223598SCornelia Huck kvm->arch.use_irqchip = 1; 50484223598SCornelia Huck r = 0; 50584223598SCornelia Huck break; 5062444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 507c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 5082444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 5092444b352SDavid Hildenbrand r = 0; 5102444b352SDavid Hildenbrand break; 51168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 5125967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 513a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 5145967c17bSDavid Hildenbrand r = -EBUSY; 5155967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 516c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 517c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 5182f87d942SGuenther Hutzl if (test_facility(134)) { 5192f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 5202f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 5212f87d942SGuenther Hutzl } 52253743aa7SMaxim Samoylov if (test_facility(135)) { 52353743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 52453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 52553743aa7SMaxim Samoylov } 52618280d8bSMichael Mueller r = 0; 52718280d8bSMichael Mueller } else 52818280d8bSMichael Mueller r = -EINVAL; 5295967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 530c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 531c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 53268c55750SEric Farman break; 533c6e5f166SFan Zhang case KVM_CAP_S390_RI: 534c6e5f166SFan Zhang r = -EINVAL; 535c6e5f166SFan Zhang mutex_lock(&kvm->lock); 536a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 537c6e5f166SFan Zhang r = -EBUSY; 538c6e5f166SFan Zhang } else if (test_facility(64)) { 539c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 540c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 541c6e5f166SFan Zhang r = 0; 542c6e5f166SFan Zhang } 543c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 544c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 545c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 546c6e5f166SFan Zhang break; 547*4e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 548*4e0b1ab7SFan Zhang r = -EINVAL; 549*4e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 550*4e0b1ab7SFan Zhang if (atomic_read(&kvm->online_vcpus)) { 551*4e0b1ab7SFan Zhang r = -EBUSY; 552*4e0b1ab7SFan Zhang } else if (test_facility(133)) { 553*4e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 554*4e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 555*4e0b1ab7SFan Zhang r = 0; 556*4e0b1ab7SFan Zhang } 557*4e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 558*4e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 559*4e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 560*4e0b1ab7SFan Zhang break; 561e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 562c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 563e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 564e44fc8c9SEkaterina Tumanova r = 0; 565e44fc8c9SEkaterina Tumanova break; 5666502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5676502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 5686502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 5696502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 5706502a34cSDavid Hildenbrand r = 0; 5716502a34cSDavid Hildenbrand break; 572d938dc55SCornelia Huck default: 573d938dc55SCornelia Huck r = -EINVAL; 574d938dc55SCornelia Huck break; 575d938dc55SCornelia Huck } 576d938dc55SCornelia Huck return r; 577d938dc55SCornelia Huck } 578d938dc55SCornelia Huck 5798c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 5808c0a7ce6SDominik Dingel { 5818c0a7ce6SDominik Dingel int ret; 5828c0a7ce6SDominik Dingel 5838c0a7ce6SDominik Dingel switch (attr->attr) { 5848c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 5858c0a7ce6SDominik Dingel ret = 0; 586c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 587a3a92c31SDominik Dingel kvm->arch.mem_limit); 588a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 5898c0a7ce6SDominik Dingel ret = -EFAULT; 5908c0a7ce6SDominik Dingel break; 5918c0a7ce6SDominik Dingel default: 5928c0a7ce6SDominik Dingel ret = -ENXIO; 5938c0a7ce6SDominik Dingel break; 5948c0a7ce6SDominik Dingel } 5958c0a7ce6SDominik Dingel return ret; 5968c0a7ce6SDominik Dingel } 5978c0a7ce6SDominik Dingel 5988c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 5994f718eabSDominik Dingel { 6004f718eabSDominik Dingel int ret; 6014f718eabSDominik Dingel unsigned int idx; 6024f718eabSDominik Dingel switch (attr->attr) { 6034f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 604f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 605c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 606e6db1d61SDominik Dingel break; 607e6db1d61SDominik Dingel 6084f718eabSDominik Dingel ret = -EBUSY; 609c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 6104f718eabSDominik Dingel mutex_lock(&kvm->lock); 611a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 6124f718eabSDominik Dingel kvm->arch.use_cmma = 1; 6134f718eabSDominik Dingel ret = 0; 6144f718eabSDominik Dingel } 6154f718eabSDominik Dingel mutex_unlock(&kvm->lock); 6164f718eabSDominik Dingel break; 6174f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 618f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 619f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 620f9cbd9b0SDavid Hildenbrand break; 621c3489155SDominik Dingel ret = -EINVAL; 622c3489155SDominik Dingel if (!kvm->arch.use_cmma) 623c3489155SDominik Dingel break; 624c3489155SDominik Dingel 625c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 6264f718eabSDominik Dingel mutex_lock(&kvm->lock); 6274f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 628a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 6294f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 6304f718eabSDominik Dingel mutex_unlock(&kvm->lock); 6314f718eabSDominik Dingel ret = 0; 6324f718eabSDominik Dingel break; 6338c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 6348c0a7ce6SDominik Dingel unsigned long new_limit; 6358c0a7ce6SDominik Dingel 6368c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 6378c0a7ce6SDominik Dingel return -EINVAL; 6388c0a7ce6SDominik Dingel 6398c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 6408c0a7ce6SDominik Dingel return -EFAULT; 6418c0a7ce6SDominik Dingel 642a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 643a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 6448c0a7ce6SDominik Dingel return -E2BIG; 6458c0a7ce6SDominik Dingel 646a3a92c31SDominik Dingel if (!new_limit) 647a3a92c31SDominik Dingel return -EINVAL; 648a3a92c31SDominik Dingel 6496ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 650a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 651a3a92c31SDominik Dingel new_limit -= 1; 652a3a92c31SDominik Dingel 6538c0a7ce6SDominik Dingel ret = -EBUSY; 6548c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 655a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 6566ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 6576ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 6588c0a7ce6SDominik Dingel 6598c0a7ce6SDominik Dingel if (!new) { 6608c0a7ce6SDominik Dingel ret = -ENOMEM; 6618c0a7ce6SDominik Dingel } else { 6626ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 6638c0a7ce6SDominik Dingel new->private = kvm; 6648c0a7ce6SDominik Dingel kvm->arch.gmap = new; 6658c0a7ce6SDominik Dingel ret = 0; 6668c0a7ce6SDominik Dingel } 6678c0a7ce6SDominik Dingel } 6688c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 669a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 670a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 671a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 6728c0a7ce6SDominik Dingel break; 6738c0a7ce6SDominik Dingel } 6744f718eabSDominik Dingel default: 6754f718eabSDominik Dingel ret = -ENXIO; 6764f718eabSDominik Dingel break; 6774f718eabSDominik Dingel } 6784f718eabSDominik Dingel return ret; 6794f718eabSDominik Dingel } 6804f718eabSDominik Dingel 681a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 682a374e892STony Krowiak 683a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 684a374e892STony Krowiak { 685a374e892STony Krowiak struct kvm_vcpu *vcpu; 686a374e892STony Krowiak int i; 687a374e892STony Krowiak 6889d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 689a374e892STony Krowiak return -EINVAL; 690a374e892STony Krowiak 691a374e892STony Krowiak mutex_lock(&kvm->lock); 692a374e892STony Krowiak switch (attr->attr) { 693a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 694a374e892STony Krowiak get_random_bytes( 695a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 696a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 697a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 698c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 699a374e892STony Krowiak break; 700a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 701a374e892STony Krowiak get_random_bytes( 702a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 703a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 704a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 705c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 706a374e892STony Krowiak break; 707a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 708a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 709a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 710a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 711c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 712a374e892STony Krowiak break; 713a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 714a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 715a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 716a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 717c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 718a374e892STony Krowiak break; 719a374e892STony Krowiak default: 720a374e892STony Krowiak mutex_unlock(&kvm->lock); 721a374e892STony Krowiak return -ENXIO; 722a374e892STony Krowiak } 723a374e892STony Krowiak 724a374e892STony Krowiak kvm_for_each_vcpu(i, vcpu, kvm) { 725a374e892STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 726a374e892STony Krowiak exit_sie(vcpu); 727a374e892STony Krowiak } 728a374e892STony Krowiak mutex_unlock(&kvm->lock); 729a374e892STony Krowiak return 0; 730a374e892STony Krowiak } 731a374e892STony Krowiak 73272f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 73372f25020SJason J. Herne { 73472f25020SJason J. Herne u8 gtod_high; 73572f25020SJason J. Herne 73672f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 73772f25020SJason J. Herne sizeof(gtod_high))) 73872f25020SJason J. Herne return -EFAULT; 73972f25020SJason J. Herne 74072f25020SJason J. Herne if (gtod_high != 0) 74172f25020SJason J. Herne return -EINVAL; 74258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 74372f25020SJason J. Herne 74472f25020SJason J. Herne return 0; 74572f25020SJason J. Herne } 74672f25020SJason J. Herne 74772f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 74872f25020SJason J. Herne { 7495a3d883aSDavid Hildenbrand u64 gtod; 75072f25020SJason J. Herne 75172f25020SJason J. Herne if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 75272f25020SJason J. Herne return -EFAULT; 75372f25020SJason J. Herne 75425ed1675SDavid Hildenbrand kvm_s390_set_tod_clock(kvm, gtod); 75558c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod); 75672f25020SJason J. Herne return 0; 75772f25020SJason J. Herne } 75872f25020SJason J. Herne 75972f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 76072f25020SJason J. Herne { 76172f25020SJason J. Herne int ret; 76272f25020SJason J. Herne 76372f25020SJason J. Herne if (attr->flags) 76472f25020SJason J. Herne return -EINVAL; 76572f25020SJason J. Herne 76672f25020SJason J. Herne switch (attr->attr) { 76772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 76872f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 76972f25020SJason J. Herne break; 77072f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 77172f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 77272f25020SJason J. Herne break; 77372f25020SJason J. Herne default: 77472f25020SJason J. Herne ret = -ENXIO; 77572f25020SJason J. Herne break; 77672f25020SJason J. Herne } 77772f25020SJason J. Herne return ret; 77872f25020SJason J. Herne } 77972f25020SJason J. Herne 78072f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 78172f25020SJason J. Herne { 78272f25020SJason J. Herne u8 gtod_high = 0; 78372f25020SJason J. Herne 78472f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 78572f25020SJason J. Herne sizeof(gtod_high))) 78672f25020SJason J. Herne return -EFAULT; 78758c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 78872f25020SJason J. Herne 78972f25020SJason J. Herne return 0; 79072f25020SJason J. Herne } 79172f25020SJason J. Herne 79272f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 79372f25020SJason J. Herne { 7945a3d883aSDavid Hildenbrand u64 gtod; 79572f25020SJason J. Herne 79660417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 79772f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 79872f25020SJason J. Herne return -EFAULT; 79958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 80072f25020SJason J. Herne 80172f25020SJason J. Herne return 0; 80272f25020SJason J. Herne } 80372f25020SJason J. Herne 80472f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 80572f25020SJason J. Herne { 80672f25020SJason J. Herne int ret; 80772f25020SJason J. Herne 80872f25020SJason J. Herne if (attr->flags) 80972f25020SJason J. Herne return -EINVAL; 81072f25020SJason J. Herne 81172f25020SJason J. Herne switch (attr->attr) { 81272f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 81372f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 81472f25020SJason J. Herne break; 81572f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 81672f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 81772f25020SJason J. Herne break; 81872f25020SJason J. Herne default: 81972f25020SJason J. Herne ret = -ENXIO; 82072f25020SJason J. Herne break; 82172f25020SJason J. Herne } 82272f25020SJason J. Herne return ret; 82372f25020SJason J. Herne } 82472f25020SJason J. Herne 825658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 826658b6edaSMichael Mueller { 827658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 828053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 829658b6edaSMichael Mueller int ret = 0; 830658b6edaSMichael Mueller 831658b6edaSMichael Mueller mutex_lock(&kvm->lock); 832a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 833658b6edaSMichael Mueller ret = -EBUSY; 834658b6edaSMichael Mueller goto out; 835658b6edaSMichael Mueller } 836658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 837658b6edaSMichael Mueller if (!proc) { 838658b6edaSMichael Mueller ret = -ENOMEM; 839658b6edaSMichael Mueller goto out; 840658b6edaSMichael Mueller } 841658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 842658b6edaSMichael Mueller sizeof(*proc))) { 8439bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 844053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 845053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 8460487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 847053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 848053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 849053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 850053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 851053dd230SDavid Hildenbrand else 852658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 853053dd230SDavid Hildenbrand } 854c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 855658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 856a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 857a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 858a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 859a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 860a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 861a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 862a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 863658b6edaSMichael Mueller } else 864658b6edaSMichael Mueller ret = -EFAULT; 865658b6edaSMichael Mueller kfree(proc); 866658b6edaSMichael Mueller out: 867658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 868658b6edaSMichael Mueller return ret; 869658b6edaSMichael Mueller } 870658b6edaSMichael Mueller 87115c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 87215c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 87315c9705fSDavid Hildenbrand { 87415c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 87515c9705fSDavid Hildenbrand int ret = -EBUSY; 87615c9705fSDavid Hildenbrand 87715c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 87815c9705fSDavid Hildenbrand return -EFAULT; 87915c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 88015c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 88115c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 88215c9705fSDavid Hildenbrand return -EINVAL; 88315c9705fSDavid Hildenbrand 88415c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 88515c9705fSDavid Hildenbrand if (!atomic_read(&kvm->online_vcpus)) { 88615c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 88715c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 88815c9705fSDavid Hildenbrand ret = 0; 88915c9705fSDavid Hildenbrand } 89015c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 89115c9705fSDavid Hildenbrand return ret; 89215c9705fSDavid Hildenbrand } 89315c9705fSDavid Hildenbrand 8940a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 8950a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 8960a763c78SDavid Hildenbrand { 8970a763c78SDavid Hildenbrand /* 8980a763c78SDavid Hildenbrand * Once supported by kernel + hw, we have to store the subfunctions 8990a763c78SDavid Hildenbrand * in kvm->arch and remember that user space configured them. 9000a763c78SDavid Hildenbrand */ 9010a763c78SDavid Hildenbrand return -ENXIO; 9020a763c78SDavid Hildenbrand } 9030a763c78SDavid Hildenbrand 904658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 905658b6edaSMichael Mueller { 906658b6edaSMichael Mueller int ret = -ENXIO; 907658b6edaSMichael Mueller 908658b6edaSMichael Mueller switch (attr->attr) { 909658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 910658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 911658b6edaSMichael Mueller break; 91215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 91315c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 91415c9705fSDavid Hildenbrand break; 9150a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 9160a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 9170a763c78SDavid Hildenbrand break; 918658b6edaSMichael Mueller } 919658b6edaSMichael Mueller return ret; 920658b6edaSMichael Mueller } 921658b6edaSMichael Mueller 922658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 923658b6edaSMichael Mueller { 924658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 925658b6edaSMichael Mueller int ret = 0; 926658b6edaSMichael Mueller 927658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 928658b6edaSMichael Mueller if (!proc) { 929658b6edaSMichael Mueller ret = -ENOMEM; 930658b6edaSMichael Mueller goto out; 931658b6edaSMichael Mueller } 9329bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 933658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 934c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 935c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 936a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 937a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 938a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 939a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 940a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 941a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 942a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 943658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 944658b6edaSMichael Mueller ret = -EFAULT; 945658b6edaSMichael Mueller kfree(proc); 946658b6edaSMichael Mueller out: 947658b6edaSMichael Mueller return ret; 948658b6edaSMichael Mueller } 949658b6edaSMichael Mueller 950658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 951658b6edaSMichael Mueller { 952658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 953658b6edaSMichael Mueller int ret = 0; 954658b6edaSMichael Mueller 955658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 956658b6edaSMichael Mueller if (!mach) { 957658b6edaSMichael Mueller ret = -ENOMEM; 958658b6edaSMichael Mueller goto out; 959658b6edaSMichael Mueller } 960658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 96137c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 962c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 963981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 964658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 96504478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 966a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 967a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 968a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 969a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 970a8c39dd7SChristian Borntraeger mach->fac_mask[0], 971a8c39dd7SChristian Borntraeger mach->fac_mask[1], 972a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 973a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 974a8c39dd7SChristian Borntraeger mach->fac_list[0], 975a8c39dd7SChristian Borntraeger mach->fac_list[1], 976a8c39dd7SChristian Borntraeger mach->fac_list[2]); 977658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 978658b6edaSMichael Mueller ret = -EFAULT; 979658b6edaSMichael Mueller kfree(mach); 980658b6edaSMichael Mueller out: 981658b6edaSMichael Mueller return ret; 982658b6edaSMichael Mueller } 983658b6edaSMichael Mueller 98415c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 98515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 98615c9705fSDavid Hildenbrand { 98715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 98815c9705fSDavid Hildenbrand 98915c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 99015c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 99115c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 99215c9705fSDavid Hildenbrand return -EFAULT; 99315c9705fSDavid Hildenbrand return 0; 99415c9705fSDavid Hildenbrand } 99515c9705fSDavid Hildenbrand 99615c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 99715c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 99815c9705fSDavid Hildenbrand { 99915c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 100015c9705fSDavid Hildenbrand 100115c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 100215c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 100315c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 100415c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 100515c9705fSDavid Hildenbrand return -EFAULT; 100615c9705fSDavid Hildenbrand return 0; 100715c9705fSDavid Hildenbrand } 100815c9705fSDavid Hildenbrand 10090a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 10100a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 10110a763c78SDavid Hildenbrand { 10120a763c78SDavid Hildenbrand /* 10130a763c78SDavid Hildenbrand * Once we can actually configure subfunctions (kernel + hw support), 10140a763c78SDavid Hildenbrand * we have to check if they were already set by user space, if so copy 10150a763c78SDavid Hildenbrand * them from kvm->arch. 10160a763c78SDavid Hildenbrand */ 10170a763c78SDavid Hildenbrand return -ENXIO; 10180a763c78SDavid Hildenbrand } 10190a763c78SDavid Hildenbrand 10200a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 10210a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 10220a763c78SDavid Hildenbrand { 10230a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 10240a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 10250a763c78SDavid Hildenbrand return -EFAULT; 10260a763c78SDavid Hildenbrand return 0; 10270a763c78SDavid Hildenbrand } 1028658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1029658b6edaSMichael Mueller { 1030658b6edaSMichael Mueller int ret = -ENXIO; 1031658b6edaSMichael Mueller 1032658b6edaSMichael Mueller switch (attr->attr) { 1033658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1034658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1035658b6edaSMichael Mueller break; 1036658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1037658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1038658b6edaSMichael Mueller break; 103915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 104015c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 104115c9705fSDavid Hildenbrand break; 104215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 104315c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 104415c9705fSDavid Hildenbrand break; 10450a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 10460a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 10470a763c78SDavid Hildenbrand break; 10480a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 10490a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 10500a763c78SDavid Hildenbrand break; 1051658b6edaSMichael Mueller } 1052658b6edaSMichael Mueller return ret; 1053658b6edaSMichael Mueller } 1054658b6edaSMichael Mueller 1055f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1056f2061656SDominik Dingel { 1057f2061656SDominik Dingel int ret; 1058f2061656SDominik Dingel 1059f2061656SDominik Dingel switch (attr->group) { 10604f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 10618c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 10624f718eabSDominik Dingel break; 106372f25020SJason J. Herne case KVM_S390_VM_TOD: 106472f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 106572f25020SJason J. Herne break; 1066658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1067658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1068658b6edaSMichael Mueller break; 1069a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1070a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1071a374e892STony Krowiak break; 1072f2061656SDominik Dingel default: 1073f2061656SDominik Dingel ret = -ENXIO; 1074f2061656SDominik Dingel break; 1075f2061656SDominik Dingel } 1076f2061656SDominik Dingel 1077f2061656SDominik Dingel return ret; 1078f2061656SDominik Dingel } 1079f2061656SDominik Dingel 1080f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1081f2061656SDominik Dingel { 10828c0a7ce6SDominik Dingel int ret; 10838c0a7ce6SDominik Dingel 10848c0a7ce6SDominik Dingel switch (attr->group) { 10858c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 10868c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 10878c0a7ce6SDominik Dingel break; 108872f25020SJason J. Herne case KVM_S390_VM_TOD: 108972f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 109072f25020SJason J. Herne break; 1091658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1092658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1093658b6edaSMichael Mueller break; 10948c0a7ce6SDominik Dingel default: 10958c0a7ce6SDominik Dingel ret = -ENXIO; 10968c0a7ce6SDominik Dingel break; 10978c0a7ce6SDominik Dingel } 10988c0a7ce6SDominik Dingel 10998c0a7ce6SDominik Dingel return ret; 1100f2061656SDominik Dingel } 1101f2061656SDominik Dingel 1102f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1103f2061656SDominik Dingel { 1104f2061656SDominik Dingel int ret; 1105f2061656SDominik Dingel 1106f2061656SDominik Dingel switch (attr->group) { 11074f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 11084f718eabSDominik Dingel switch (attr->attr) { 11094f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 11104f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1111f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1112f9cbd9b0SDavid Hildenbrand break; 11138c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 11144f718eabSDominik Dingel ret = 0; 11154f718eabSDominik Dingel break; 11164f718eabSDominik Dingel default: 11174f718eabSDominik Dingel ret = -ENXIO; 11184f718eabSDominik Dingel break; 11194f718eabSDominik Dingel } 11204f718eabSDominik Dingel break; 112172f25020SJason J. Herne case KVM_S390_VM_TOD: 112272f25020SJason J. Herne switch (attr->attr) { 112372f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 112472f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 112572f25020SJason J. Herne ret = 0; 112672f25020SJason J. Herne break; 112772f25020SJason J. Herne default: 112872f25020SJason J. Herne ret = -ENXIO; 112972f25020SJason J. Herne break; 113072f25020SJason J. Herne } 113172f25020SJason J. Herne break; 1132658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1133658b6edaSMichael Mueller switch (attr->attr) { 1134658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1135658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 113615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 113715c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 11380a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1139658b6edaSMichael Mueller ret = 0; 1140658b6edaSMichael Mueller break; 11410a763c78SDavid Hildenbrand /* configuring subfunctions is not supported yet */ 11420a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1143658b6edaSMichael Mueller default: 1144658b6edaSMichael Mueller ret = -ENXIO; 1145658b6edaSMichael Mueller break; 1146658b6edaSMichael Mueller } 1147658b6edaSMichael Mueller break; 1148a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1149a374e892STony Krowiak switch (attr->attr) { 1150a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1151a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1152a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1153a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1154a374e892STony Krowiak ret = 0; 1155a374e892STony Krowiak break; 1156a374e892STony Krowiak default: 1157a374e892STony Krowiak ret = -ENXIO; 1158a374e892STony Krowiak break; 1159a374e892STony Krowiak } 1160a374e892STony Krowiak break; 1161f2061656SDominik Dingel default: 1162f2061656SDominik Dingel ret = -ENXIO; 1163f2061656SDominik Dingel break; 1164f2061656SDominik Dingel } 1165f2061656SDominik Dingel 1166f2061656SDominik Dingel return ret; 1167f2061656SDominik Dingel } 1168f2061656SDominik Dingel 116930ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 117030ee2a98SJason J. Herne { 117130ee2a98SJason J. Herne uint8_t *keys; 117230ee2a98SJason J. Herne uint64_t hva; 117330ee2a98SJason J. Herne int i, r = 0; 117430ee2a98SJason J. Herne 117530ee2a98SJason J. Herne if (args->flags != 0) 117630ee2a98SJason J. Herne return -EINVAL; 117730ee2a98SJason J. Herne 117830ee2a98SJason J. Herne /* Is this guest using storage keys? */ 117930ee2a98SJason J. Herne if (!mm_use_skey(current->mm)) 118030ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 118130ee2a98SJason J. Herne 118230ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 118330ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 118430ee2a98SJason J. Herne return -EINVAL; 118530ee2a98SJason J. Herne 118630ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 118730ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 118830ee2a98SJason J. Herne if (!keys) 118930ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 119030ee2a98SJason J. Herne if (!keys) 119130ee2a98SJason J. Herne return -ENOMEM; 119230ee2a98SJason J. Herne 1193d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 119430ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 119530ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 119630ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 119730ee2a98SJason J. Herne r = -EFAULT; 1198d3ed1ceeSMartin Schwidefsky break; 119930ee2a98SJason J. Herne } 120030ee2a98SJason J. Herne 1201154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1202154c8c19SDavid Hildenbrand if (r) 1203d3ed1ceeSMartin Schwidefsky break; 120430ee2a98SJason J. Herne } 1205d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 120630ee2a98SJason J. Herne 1207d3ed1ceeSMartin Schwidefsky if (!r) { 120830ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 120930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 121030ee2a98SJason J. Herne if (r) 121130ee2a98SJason J. Herne r = -EFAULT; 1212d3ed1ceeSMartin Schwidefsky } 1213d3ed1ceeSMartin Schwidefsky 121430ee2a98SJason J. Herne kvfree(keys); 121530ee2a98SJason J. Herne return r; 121630ee2a98SJason J. Herne } 121730ee2a98SJason J. Herne 121830ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 121930ee2a98SJason J. Herne { 122030ee2a98SJason J. Herne uint8_t *keys; 122130ee2a98SJason J. Herne uint64_t hva; 122230ee2a98SJason J. Herne int i, r = 0; 122330ee2a98SJason J. Herne 122430ee2a98SJason J. Herne if (args->flags != 0) 122530ee2a98SJason J. Herne return -EINVAL; 122630ee2a98SJason J. Herne 122730ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 122830ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 122930ee2a98SJason J. Herne return -EINVAL; 123030ee2a98SJason J. Herne 123130ee2a98SJason J. Herne keys = kmalloc_array(args->count, sizeof(uint8_t), 123230ee2a98SJason J. Herne GFP_KERNEL | __GFP_NOWARN); 123330ee2a98SJason J. Herne if (!keys) 123430ee2a98SJason J. Herne keys = vmalloc(sizeof(uint8_t) * args->count); 123530ee2a98SJason J. Herne if (!keys) 123630ee2a98SJason J. Herne return -ENOMEM; 123730ee2a98SJason J. Herne 123830ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 123930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 124030ee2a98SJason J. Herne if (r) { 124130ee2a98SJason J. Herne r = -EFAULT; 124230ee2a98SJason J. Herne goto out; 124330ee2a98SJason J. Herne } 124430ee2a98SJason J. Herne 124530ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 124614d4a425SDominik Dingel r = s390_enable_skey(); 124714d4a425SDominik Dingel if (r) 124814d4a425SDominik Dingel goto out; 124930ee2a98SJason J. Herne 1250d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 125130ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 125230ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 125330ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 125430ee2a98SJason J. Herne r = -EFAULT; 1255d3ed1ceeSMartin Schwidefsky break; 125630ee2a98SJason J. Herne } 125730ee2a98SJason J. Herne 125830ee2a98SJason J. Herne /* Lowest order bit is reserved */ 125930ee2a98SJason J. Herne if (keys[i] & 0x01) { 126030ee2a98SJason J. Herne r = -EINVAL; 1261d3ed1ceeSMartin Schwidefsky break; 126230ee2a98SJason J. Herne } 126330ee2a98SJason J. Herne 1264fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 126530ee2a98SJason J. Herne if (r) 1266d3ed1ceeSMartin Schwidefsky break; 126730ee2a98SJason J. Herne } 1268d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 126930ee2a98SJason J. Herne out: 127030ee2a98SJason J. Herne kvfree(keys); 127130ee2a98SJason J. Herne return r; 127230ee2a98SJason J. Herne } 127330ee2a98SJason J. Herne 1274b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 1275b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 1276b0c632dbSHeiko Carstens { 1277b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 1278b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 1279f2061656SDominik Dingel struct kvm_device_attr attr; 1280b0c632dbSHeiko Carstens int r; 1281b0c632dbSHeiko Carstens 1282b0c632dbSHeiko Carstens switch (ioctl) { 1283ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 1284ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 1285ba5c1e9bSCarsten Otte 1286ba5c1e9bSCarsten Otte r = -EFAULT; 1287ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 1288ba5c1e9bSCarsten Otte break; 1289ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 1290ba5c1e9bSCarsten Otte break; 1291ba5c1e9bSCarsten Otte } 1292d938dc55SCornelia Huck case KVM_ENABLE_CAP: { 1293d938dc55SCornelia Huck struct kvm_enable_cap cap; 1294d938dc55SCornelia Huck r = -EFAULT; 1295d938dc55SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 1296d938dc55SCornelia Huck break; 1297d938dc55SCornelia Huck r = kvm_vm_ioctl_enable_cap(kvm, &cap); 1298d938dc55SCornelia Huck break; 1299d938dc55SCornelia Huck } 130084223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 130184223598SCornelia Huck struct kvm_irq_routing_entry routing; 130284223598SCornelia Huck 130384223598SCornelia Huck r = -EINVAL; 130484223598SCornelia Huck if (kvm->arch.use_irqchip) { 130584223598SCornelia Huck /* Set up dummy routing. */ 130684223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 1307152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 130884223598SCornelia Huck } 130984223598SCornelia Huck break; 131084223598SCornelia Huck } 1311f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 1312f2061656SDominik Dingel r = -EFAULT; 1313f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1314f2061656SDominik Dingel break; 1315f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 1316f2061656SDominik Dingel break; 1317f2061656SDominik Dingel } 1318f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 1319f2061656SDominik Dingel r = -EFAULT; 1320f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1321f2061656SDominik Dingel break; 1322f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 1323f2061656SDominik Dingel break; 1324f2061656SDominik Dingel } 1325f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 1326f2061656SDominik Dingel r = -EFAULT; 1327f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 1328f2061656SDominik Dingel break; 1329f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 1330f2061656SDominik Dingel break; 1331f2061656SDominik Dingel } 133230ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 133330ee2a98SJason J. Herne struct kvm_s390_skeys args; 133430ee2a98SJason J. Herne 133530ee2a98SJason J. Herne r = -EFAULT; 133630ee2a98SJason J. Herne if (copy_from_user(&args, argp, 133730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 133830ee2a98SJason J. Herne break; 133930ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 134030ee2a98SJason J. Herne break; 134130ee2a98SJason J. Herne } 134230ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 134330ee2a98SJason J. Herne struct kvm_s390_skeys args; 134430ee2a98SJason J. Herne 134530ee2a98SJason J. Herne r = -EFAULT; 134630ee2a98SJason J. Herne if (copy_from_user(&args, argp, 134730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 134830ee2a98SJason J. Herne break; 134930ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 135030ee2a98SJason J. Herne break; 135130ee2a98SJason J. Herne } 1352b0c632dbSHeiko Carstens default: 1353367e1319SAvi Kivity r = -ENOTTY; 1354b0c632dbSHeiko Carstens } 1355b0c632dbSHeiko Carstens 1356b0c632dbSHeiko Carstens return r; 1357b0c632dbSHeiko Carstens } 1358b0c632dbSHeiko Carstens 135945c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config) 136045c9b47cSTony Krowiak { 136145c9b47cSTony Krowiak u32 fcn_code = 0x04000000UL; 136286044c8cSChristian Borntraeger u32 cc = 0; 136345c9b47cSTony Krowiak 136486044c8cSChristian Borntraeger memset(config, 0, 128); 136545c9b47cSTony Krowiak asm volatile( 136645c9b47cSTony Krowiak "lgr 0,%1\n" 136745c9b47cSTony Krowiak "lgr 2,%2\n" 136845c9b47cSTony Krowiak ".long 0xb2af0000\n" /* PQAP(QCI) */ 136986044c8cSChristian Borntraeger "0: ipm %0\n" 137045c9b47cSTony Krowiak "srl %0,28\n" 137186044c8cSChristian Borntraeger "1:\n" 137286044c8cSChristian Borntraeger EX_TABLE(0b, 1b) 137386044c8cSChristian Borntraeger : "+r" (cc) 137445c9b47cSTony Krowiak : "r" (fcn_code), "r" (config) 137545c9b47cSTony Krowiak : "cc", "0", "2", "memory" 137645c9b47cSTony Krowiak ); 137745c9b47cSTony Krowiak 137845c9b47cSTony Krowiak return cc; 137945c9b47cSTony Krowiak } 138045c9b47cSTony Krowiak 138145c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 138245c9b47cSTony Krowiak { 138345c9b47cSTony Krowiak u8 config[128]; 138445c9b47cSTony Krowiak int cc; 138545c9b47cSTony Krowiak 1386a6aacc3fSHeiko Carstens if (test_facility(12)) { 138745c9b47cSTony Krowiak cc = kvm_s390_query_ap_config(config); 138845c9b47cSTony Krowiak 138945c9b47cSTony Krowiak if (cc) 139045c9b47cSTony Krowiak pr_err("PQAP(QCI) failed with cc=%d", cc); 139145c9b47cSTony Krowiak else 139245c9b47cSTony Krowiak return config[0] & 0x40; 139345c9b47cSTony Krowiak } 139445c9b47cSTony Krowiak 139545c9b47cSTony Krowiak return 0; 139645c9b47cSTony Krowiak } 139745c9b47cSTony Krowiak 139845c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 139945c9b47cSTony Krowiak { 140045c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 140145c9b47cSTony Krowiak 140245c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 140345c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 140445c9b47cSTony Krowiak else 140545c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 140645c9b47cSTony Krowiak } 140745c9b47cSTony Krowiak 14089bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 14099d8d5786SMichael Mueller { 14109bb0ec09SDavid Hildenbrand struct cpuid cpuid; 14119bb0ec09SDavid Hildenbrand 14129bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 14139bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 14149bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 14159d8d5786SMichael Mueller } 14169d8d5786SMichael Mueller 1417c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 14185102ee87STony Krowiak { 14199d8d5786SMichael Mueller if (!test_kvm_facility(kvm, 76)) 1420c54f0d6aSDavid Hildenbrand return; 14215102ee87STony Krowiak 1422c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 142345c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 14245102ee87STony Krowiak 1425ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 1426ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 1427ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 1428ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1429ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1430ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1431ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 14325102ee87STony Krowiak } 14335102ee87STony Krowiak 14347d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 14357d43bafcSEugene (jno) Dvurechenski { 14367d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 14375e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 14387d43bafcSEugene (jno) Dvurechenski else 14397d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 14407d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 14417d43bafcSEugene (jno) Dvurechenski } 14427d43bafcSEugene (jno) Dvurechenski 1443e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1444b0c632dbSHeiko Carstens { 144576a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 14469d8d5786SMichael Mueller int i, rc; 1447b0c632dbSHeiko Carstens char debug_name[16]; 1448f6c137ffSChristian Borntraeger static unsigned long sca_offset; 1449b0c632dbSHeiko Carstens 1450e08b9637SCarsten Otte rc = -EINVAL; 1451e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 1452e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 1453e08b9637SCarsten Otte goto out_err; 1454e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 1455e08b9637SCarsten Otte goto out_err; 1456e08b9637SCarsten Otte #else 1457e08b9637SCarsten Otte if (type) 1458e08b9637SCarsten Otte goto out_err; 1459e08b9637SCarsten Otte #endif 1460e08b9637SCarsten Otte 1461b0c632dbSHeiko Carstens rc = s390_enable_sie(); 1462b0c632dbSHeiko Carstens if (rc) 1463d89f5effSJan Kiszka goto out_err; 1464b0c632dbSHeiko Carstens 1465b290411aSCarsten Otte rc = -ENOMEM; 1466b290411aSCarsten Otte 14677d0a5e62SJanosch Frank ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500); 14687d0a5e62SJanosch Frank 14697d43bafcSEugene (jno) Dvurechenski kvm->arch.use_esca = 0; /* start with basic SCA */ 147076a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 147176a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 14725e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 147376a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 1474b0c632dbSHeiko Carstens if (!kvm->arch.sca) 1475d89f5effSJan Kiszka goto out_err; 1476f6c137ffSChristian Borntraeger spin_lock(&kvm_lock); 1477c5c2c393SDavid Hildenbrand sca_offset += 16; 1478bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 1479c5c2c393SDavid Hildenbrand sca_offset = 0; 1480bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 1481bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 1482f6c137ffSChristian Borntraeger spin_unlock(&kvm_lock); 1483b0c632dbSHeiko Carstens 1484b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 1485b0c632dbSHeiko Carstens 14861cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 1487b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 148840f5b735SDominik Dingel goto out_err; 1489b0c632dbSHeiko Carstens 1490c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 1491c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 1492c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 149340f5b735SDominik Dingel goto out_err; 14949d8d5786SMichael Mueller 1495fb5bf93fSMichael Mueller /* Populate the facility mask initially. */ 1496c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list, 149704478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 14989d8d5786SMichael Mueller for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) { 14999d8d5786SMichael Mueller if (i < kvm_s390_fac_list_mask_size()) 1500c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i]; 15019d8d5786SMichael Mueller else 1502c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_mask[i] = 0UL; 15039d8d5786SMichael Mueller } 15049d8d5786SMichael Mueller 1505981467c9SMichael Mueller /* Populate the facility list initially. */ 1506c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 1507c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask, 1508981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1509981467c9SMichael Mueller 151095ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 151195ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 151295ca2cb5SJanosch Frank 15139bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 151437c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 15159d8d5786SMichael Mueller 1516c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 15175102ee87STony Krowiak 1518ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 15196d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 15206d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 15218a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 1522a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 1523ba5c1e9bSCarsten Otte 1524b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 152578f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 1526b0c632dbSHeiko Carstens 1527e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 1528e08b9637SCarsten Otte kvm->arch.gmap = NULL; 1529a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 1530e08b9637SCarsten Otte } else { 153132e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 1532a3a92c31SDominik Dingel kvm->arch.mem_limit = TASK_MAX_SIZE; 153332e6b236SGuenther Hutzl else 153432e6b236SGuenther Hutzl kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE, 153532e6b236SGuenther Hutzl sclp.hamax + 1); 15366ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 1537598841caSCarsten Otte if (!kvm->arch.gmap) 153840f5b735SDominik Dingel goto out_err; 15392c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 154024eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 1541e08b9637SCarsten Otte } 1542fa6b7fe9SCornelia Huck 1543fa6b7fe9SCornelia Huck kvm->arch.css_support = 0; 154484223598SCornelia Huck kvm->arch.use_irqchip = 0; 154572f25020SJason J. Herne kvm->arch.epoch = 0; 1546fa6b7fe9SCornelia Huck 15478ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 1548a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 15498335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 15508ad35755SDavid Hildenbrand 1551d89f5effSJan Kiszka return 0; 1552d89f5effSJan Kiszka out_err: 1553c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 155440f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 15557d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 155678f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 1557d89f5effSJan Kiszka return rc; 1558b0c632dbSHeiko Carstens } 1559b0c632dbSHeiko Carstens 1560235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void) 1561235539b4SLuiz Capitulino { 1562235539b4SLuiz Capitulino return false; 1563235539b4SLuiz Capitulino } 1564235539b4SLuiz Capitulino 1565235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) 1566235539b4SLuiz Capitulino { 1567235539b4SLuiz Capitulino return 0; 1568235539b4SLuiz Capitulino } 1569235539b4SLuiz Capitulino 1570d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 1571d329c035SChristian Borntraeger { 1572d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 1573ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 157467335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 15753c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 1576bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 1577a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 157827e0393fSCarsten Otte 157927e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 15806ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 158127e0393fSCarsten Otte 1582e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 1583b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 1584d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 1585b31288faSKonstantin Weitz 15866692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 1587b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 1588d329c035SChristian Borntraeger } 1589d329c035SChristian Borntraeger 1590d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 1591d329c035SChristian Borntraeger { 1592d329c035SChristian Borntraeger unsigned int i; 1593988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 1594d329c035SChristian Borntraeger 1595988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 1596988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 1597988a2caeSGleb Natapov 1598988a2caeSGleb Natapov mutex_lock(&kvm->lock); 1599988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 1600d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 1601988a2caeSGleb Natapov 1602988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 1603988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 1604d329c035SChristian Borntraeger } 1605d329c035SChristian Borntraeger 1606b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 1607b0c632dbSHeiko Carstens { 1608d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 16097d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 1610d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 1611c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 161227e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 16136ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 1614841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 161567335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 1616a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 16178335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 1618b0c632dbSHeiko Carstens } 1619b0c632dbSHeiko Carstens 1620b0c632dbSHeiko Carstens /* Section: vcpu related */ 1621dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 1622b0c632dbSHeiko Carstens { 16236ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 162427e0393fSCarsten Otte if (!vcpu->arch.gmap) 162527e0393fSCarsten Otte return -ENOMEM; 16262c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 1627dafd032aSDominik Dingel 162827e0393fSCarsten Otte return 0; 162927e0393fSCarsten Otte } 163027e0393fSCarsten Otte 1631a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 1632a6e2f683SEugene (jno) Dvurechenski { 1633a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 1634a6940674SDavid Hildenbrand return; 16355e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 16367d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 16377d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 16387d43bafcSEugene (jno) Dvurechenski 16397d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 16407d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 16417d43bafcSEugene (jno) Dvurechenski } else { 1642bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 1643a6e2f683SEugene (jno) Dvurechenski 1644a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 1645a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 1646a6e2f683SEugene (jno) Dvurechenski } 16475e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 16487d43bafcSEugene (jno) Dvurechenski } 1649a6e2f683SEugene (jno) Dvurechenski 1650eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 1651a6e2f683SEugene (jno) Dvurechenski { 1652a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 1653a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 1654a6940674SDavid Hildenbrand 1655a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 1656a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 1657a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 1658a6940674SDavid Hildenbrand } 1659eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 1660eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 1661eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 16627d43bafcSEugene (jno) Dvurechenski 1663eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 16647d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 16657d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 16660c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 1667eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 16687d43bafcSEugene (jno) Dvurechenski } else { 1669eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 1670a6e2f683SEugene (jno) Dvurechenski 1671eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 1672a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 1673a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 1674eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 1675a6e2f683SEugene (jno) Dvurechenski } 1676eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 16775e044315SEugene (jno) Dvurechenski } 16785e044315SEugene (jno) Dvurechenski 16795e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 16805e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 16815e044315SEugene (jno) Dvurechenski { 16825e044315SEugene (jno) Dvurechenski d->sda = s->sda; 16835e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 16845e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 16855e044315SEugene (jno) Dvurechenski } 16865e044315SEugene (jno) Dvurechenski 16875e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 16885e044315SEugene (jno) Dvurechenski { 16895e044315SEugene (jno) Dvurechenski int i; 16905e044315SEugene (jno) Dvurechenski 16915e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 16925e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 16935e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 16945e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 16955e044315SEugene (jno) Dvurechenski } 16965e044315SEugene (jno) Dvurechenski 16975e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 16985e044315SEugene (jno) Dvurechenski { 16995e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 17005e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 17015e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 17025e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 17035e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 17045e044315SEugene (jno) Dvurechenski 17055e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 17065e044315SEugene (jno) Dvurechenski if (!new_sca) 17075e044315SEugene (jno) Dvurechenski return -ENOMEM; 17085e044315SEugene (jno) Dvurechenski 17095e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 17105e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 17115e044315SEugene (jno) Dvurechenski 17125e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 17135e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 17145e044315SEugene (jno) Dvurechenski 17155e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 17165e044315SEugene (jno) Dvurechenski 17175e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 17185e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 17195e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 17200c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 17215e044315SEugene (jno) Dvurechenski } 17225e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 17235e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 17245e044315SEugene (jno) Dvurechenski 17255e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 17265e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 17275e044315SEugene (jno) Dvurechenski 17285e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 17295e044315SEugene (jno) Dvurechenski 17308335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 17318335713aSChristian Borntraeger old_sca, kvm->arch.sca); 17325e044315SEugene (jno) Dvurechenski return 0; 17337d43bafcSEugene (jno) Dvurechenski } 1734a6e2f683SEugene (jno) Dvurechenski 1735a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 1736a6e2f683SEugene (jno) Dvurechenski { 17375e044315SEugene (jno) Dvurechenski int rc; 17385e044315SEugene (jno) Dvurechenski 1739a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 1740a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 1741a6940674SDavid Hildenbrand return true; 1742a6940674SDavid Hildenbrand return false; 1743a6940674SDavid Hildenbrand } 17445e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 17455e044315SEugene (jno) Dvurechenski return true; 174676a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 17475e044315SEugene (jno) Dvurechenski return false; 17485e044315SEugene (jno) Dvurechenski 17495e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 17505e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 17515e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 17525e044315SEugene (jno) Dvurechenski 17535e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 1754a6e2f683SEugene (jno) Dvurechenski } 1755a6e2f683SEugene (jno) Dvurechenski 1756dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 1757dafd032aSDominik Dingel { 1758dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 1759dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 176059674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 176159674c1aSChristian Borntraeger KVM_SYNC_GPRS | 17629eed0735SChristian Borntraeger KVM_SYNC_ACRS | 1763b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 1764b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 1765b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 176675a4615cSJulius Niedworok kvm_s390_set_prefix(vcpu, 0); 1767c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 1768c6e5f166SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 1769*4e0b1ab7SFan Zhang if (test_kvm_facility(vcpu->kvm, 133)) 1770*4e0b1ab7SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 1771f6aa6dc4SDavid Hildenbrand /* fprs can be synchronized via vrs, even if the guest has no vx. With 1772f6aa6dc4SDavid Hildenbrand * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 1773f6aa6dc4SDavid Hildenbrand */ 1774f6aa6dc4SDavid Hildenbrand if (MACHINE_HAS_VX) 177568c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 17766fd8e67dSDavid Hildenbrand else 17776fd8e67dSDavid Hildenbrand vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 1778dafd032aSDominik Dingel 1779dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 1780dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 1781dafd032aSDominik Dingel 1782b0c632dbSHeiko Carstens return 0; 1783b0c632dbSHeiko Carstens } 1784b0c632dbSHeiko Carstens 1785db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1786db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1787db0758b2SDavid Hildenbrand { 1788db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 17899c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1790db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 17919c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1792db0758b2SDavid Hildenbrand } 1793db0758b2SDavid Hildenbrand 1794db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1795db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1796db0758b2SDavid Hildenbrand { 1797db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 17989c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1799db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 1800db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 18019c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1802db0758b2SDavid Hildenbrand } 1803db0758b2SDavid Hildenbrand 1804db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1805db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1806db0758b2SDavid Hildenbrand { 1807db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 1808db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 1809db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 1810db0758b2SDavid Hildenbrand } 1811db0758b2SDavid Hildenbrand 1812db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 1813db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1814db0758b2SDavid Hildenbrand { 1815db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 1816db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 1817db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 1818db0758b2SDavid Hildenbrand } 1819db0758b2SDavid Hildenbrand 1820db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1821db0758b2SDavid Hildenbrand { 1822db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 1823db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 1824db0758b2SDavid Hildenbrand preempt_enable(); 1825db0758b2SDavid Hildenbrand } 1826db0758b2SDavid Hildenbrand 1827db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 1828db0758b2SDavid Hildenbrand { 1829db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 1830db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 1831db0758b2SDavid Hildenbrand preempt_enable(); 1832db0758b2SDavid Hildenbrand } 1833db0758b2SDavid Hildenbrand 18344287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 18354287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 18364287f247SDavid Hildenbrand { 1837db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 18389c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 1839db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 1840db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 18414287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 18429c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 1843db0758b2SDavid Hildenbrand preempt_enable(); 18444287f247SDavid Hildenbrand } 18454287f247SDavid Hildenbrand 1846db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 18474287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 18484287f247SDavid Hildenbrand { 18499c23a131SDavid Hildenbrand unsigned int seq; 1850db0758b2SDavid Hildenbrand __u64 value; 1851db0758b2SDavid Hildenbrand 1852db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 18534287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 1854db0758b2SDavid Hildenbrand 18559c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 18569c23a131SDavid Hildenbrand do { 18579c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 18589c23a131SDavid Hildenbrand /* 18599c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 18609c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 18619c23a131SDavid Hildenbrand */ 18629c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 1863db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 18649c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 18659c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 1866db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 18679c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 18689c23a131SDavid Hildenbrand preempt_enable(); 1869db0758b2SDavid Hildenbrand return value; 18704287f247SDavid Hildenbrand } 18714287f247SDavid Hildenbrand 1872b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1873b0c632dbSHeiko Carstens { 18749977e886SHendrik Brueckner 187537d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 1876805de8f4SPeter Zijlstra atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 18775ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 1878db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 187901a745acSDavid Hildenbrand vcpu->cpu = cpu; 1880b0c632dbSHeiko Carstens } 1881b0c632dbSHeiko Carstens 1882b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 1883b0c632dbSHeiko Carstens { 188401a745acSDavid Hildenbrand vcpu->cpu = -1; 18855ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 1886db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 1887805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); 188837d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 188937d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 18909977e886SHendrik Brueckner 1891b0c632dbSHeiko Carstens } 1892b0c632dbSHeiko Carstens 1893b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 1894b0c632dbSHeiko Carstens { 1895b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 1896b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 1897b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 18988d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 18994287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, 0); 1900b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 1901b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 1902b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 1903b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[0] = 0xE0UL; 1904b0c632dbSHeiko Carstens vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; 19059abc2a08SDavid Hildenbrand /* make sure the new fpc will be lazily loaded */ 19069abc2a08SDavid Hildenbrand save_fpu_regs(); 19079abc2a08SDavid Hildenbrand current->thread.fpu.fpc = 0; 1908b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 1909672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 19103c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 19113c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 19126352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 19136852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 19142ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 1915b0c632dbSHeiko Carstens } 1916b0c632dbSHeiko Carstens 191731928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 191842897d86SMarcelo Tosatti { 191972f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 1920fdf03650SFan Zhang preempt_disable(); 192172f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 1922fdf03650SFan Zhang preempt_enable(); 192372f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 192425508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 1925dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 1926eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 192725508824SDavid Hildenbrand } 19286502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 19296502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 193037d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 193137d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 193242897d86SMarcelo Tosatti } 193342897d86SMarcelo Tosatti 19345102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 19355102ee87STony Krowiak { 19369d8d5786SMichael Mueller if (!test_kvm_facility(vcpu->kvm, 76)) 19375102ee87STony Krowiak return; 19385102ee87STony Krowiak 1939a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 1940a374e892STony Krowiak 1941a374e892STony Krowiak if (vcpu->kvm->arch.crypto.aes_kw) 1942a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 1943a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 1944a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 1945a374e892STony Krowiak 19465102ee87STony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 19475102ee87STony Krowiak } 19485102ee87STony Krowiak 1949b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 1950b31605c1SDominik Dingel { 1951b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 1952b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 1953b31605c1SDominik Dingel } 1954b31605c1SDominik Dingel 1955b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 1956b31605c1SDominik Dingel { 1957b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 1958b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 1959b31605c1SDominik Dingel return -ENOMEM; 1960b31605c1SDominik Dingel 19610c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 19620c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI; 1963b31605c1SDominik Dingel return 0; 1964b31605c1SDominik Dingel } 1965b31605c1SDominik Dingel 196691520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 196791520f1aSMichael Mueller { 196891520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 196991520f1aSMichael Mueller 197091520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 197180bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 1972c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 197391520f1aSMichael Mueller } 197491520f1aSMichael Mueller 1975b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 1976b0c632dbSHeiko Carstens { 1977b31605c1SDominik Dingel int rc = 0; 1978b31288faSKonstantin Weitz 19799e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 19809e6dabefSCornelia Huck CPUSTAT_SM | 1981a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 1982a4a4f191SGuenther Hutzl 198353df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 1984805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags); 198553df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 1986805de8f4SPeter Zijlstra atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags); 1987a4a4f191SGuenther Hutzl 198891520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 198991520f1aSMichael Mueller 1990bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 1991bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 19920c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 1993bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 19940c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 1995f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 19960c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 19977feb6bb8SMichael Mueller 1998873b425eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi) 19990c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 2000cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 20010c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 20020c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 200348ee7d3aSDavid Hildenbrand if (sclp.has_cei) 20040c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 200511ad65b7SDavid Hildenbrand if (sclp.has_ib) 20060c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 200737c5f6c8SDavid Hildenbrand if (sclp.has_siif) 20080c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 200937c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 20100c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 201118280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 20120c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 20130c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 201413211ea7SEric Farman } 2015*4e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 2016*4e0b1ab7SFan Zhang | SDNXC; 2017c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 2018492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 20195a5e6536SMatthew Rosato 2020e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 2021b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 2022b31605c1SDominik Dingel if (rc) 2023b31605c1SDominik Dingel return rc; 2024b31288faSKonstantin Weitz } 20250ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2026ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 20279d8d5786SMichael Mueller 20285102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 20295102ee87STony Krowiak 2030b31605c1SDominik Dingel return rc; 2031b0c632dbSHeiko Carstens } 2032b0c632dbSHeiko Carstens 2033b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 2034b0c632dbSHeiko Carstens unsigned int id) 2035b0c632dbSHeiko Carstens { 20364d47555aSCarsten Otte struct kvm_vcpu *vcpu; 20377feb6bb8SMichael Mueller struct sie_page *sie_page; 20384d47555aSCarsten Otte int rc = -EINVAL; 2039b0c632dbSHeiko Carstens 20404215825eSDavid Hildenbrand if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 20414d47555aSCarsten Otte goto out; 20424d47555aSCarsten Otte 20434d47555aSCarsten Otte rc = -ENOMEM; 20444d47555aSCarsten Otte 2045b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 2046b0c632dbSHeiko Carstens if (!vcpu) 20474d47555aSCarsten Otte goto out; 2048b0c632dbSHeiko Carstens 20497feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 20507feb6bb8SMichael Mueller if (!sie_page) 2051b0c632dbSHeiko Carstens goto out_free_cpu; 2052b0c632dbSHeiko Carstens 20537feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 20547feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 20557feb6bb8SMichael Mueller 2056efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 2057efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 2058efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 2059efed1104SDavid Hildenbrand 2060b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 2061ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 2062ba5c1e9bSCarsten Otte vcpu->arch.local_int.float_int = &kvm->arch.float_int; 2063d0321a24SChristian Borntraeger vcpu->arch.local_int.wq = &vcpu->wq; 20645288fbf0SChristian Borntraeger vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; 20659c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 2066ba5c1e9bSCarsten Otte 2067b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 2068b0c632dbSHeiko Carstens if (rc) 20699abc2a08SDavid Hildenbrand goto out_free_sie_block; 20708335713aSChristian Borntraeger VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu, 2071b0c632dbSHeiko Carstens vcpu->arch.sie_block); 2072ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 2073b0c632dbSHeiko Carstens 2074b0c632dbSHeiko Carstens return vcpu; 20757b06bf2fSWei Yongjun out_free_sie_block: 20767b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 2077b0c632dbSHeiko Carstens out_free_cpu: 2078b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 20794d47555aSCarsten Otte out: 2080b0c632dbSHeiko Carstens return ERR_PTR(rc); 2081b0c632dbSHeiko Carstens } 2082b0c632dbSHeiko Carstens 2083b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 2084b0c632dbSHeiko Carstens { 20859a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 2086b0c632dbSHeiko Carstens } 2087b0c632dbSHeiko Carstens 208827406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 208949b99e1eSChristian Borntraeger { 2090805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 209161a6df54SDavid Hildenbrand exit_sie(vcpu); 209249b99e1eSChristian Borntraeger } 209349b99e1eSChristian Borntraeger 209427406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 209549b99e1eSChristian Borntraeger { 2096805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 209749b99e1eSChristian Borntraeger } 209849b99e1eSChristian Borntraeger 20998e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 21008e236546SChristian Borntraeger { 2101805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 210261a6df54SDavid Hildenbrand exit_sie(vcpu); 21038e236546SChristian Borntraeger } 21048e236546SChristian Borntraeger 21058e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 21068e236546SChristian Borntraeger { 21079bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 21088e236546SChristian Borntraeger } 21098e236546SChristian Borntraeger 211049b99e1eSChristian Borntraeger /* 211149b99e1eSChristian Borntraeger * Kick a guest cpu out of SIE and wait until SIE is not running. 211249b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 211349b99e1eSChristian Borntraeger * return immediately. */ 211449b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 211549b99e1eSChristian Borntraeger { 2116805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); 211749b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 211849b99e1eSChristian Borntraeger cpu_relax(); 211949b99e1eSChristian Borntraeger } 212049b99e1eSChristian Borntraeger 21218e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 21228e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 212349b99e1eSChristian Borntraeger { 21248e236546SChristian Borntraeger kvm_make_request(req, vcpu); 21258e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 212649b99e1eSChristian Borntraeger } 212749b99e1eSChristian Borntraeger 2128414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 2129414d3b07SMartin Schwidefsky unsigned long end) 21302c70fe44SChristian Borntraeger { 21312c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 21322c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 2133414d3b07SMartin Schwidefsky unsigned long prefix; 2134414d3b07SMartin Schwidefsky int i; 21352c70fe44SChristian Borntraeger 213665d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 213765d0b0d4SDavid Hildenbrand return; 2138414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 2139414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 2140414d3b07SMartin Schwidefsky return; 21412c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 21422c70fe44SChristian Borntraeger /* match against both prefix pages */ 2143414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 2144414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 2145414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 2146414d3b07SMartin Schwidefsky start, end); 21478e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 21482c70fe44SChristian Borntraeger } 21492c70fe44SChristian Borntraeger } 21502c70fe44SChristian Borntraeger } 21512c70fe44SChristian Borntraeger 2152b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 2153b6d33834SChristoffer Dall { 2154b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 2155b6d33834SChristoffer Dall BUG(); 2156b6d33834SChristoffer Dall return 0; 2157b6d33834SChristoffer Dall } 2158b6d33834SChristoffer Dall 215914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 216014eebd91SCarsten Otte struct kvm_one_reg *reg) 216114eebd91SCarsten Otte { 216214eebd91SCarsten Otte int r = -EINVAL; 216314eebd91SCarsten Otte 216414eebd91SCarsten Otte switch (reg->id) { 216529b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 216629b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 216729b7c71bSCarsten Otte (u32 __user *)reg->addr); 216829b7c71bSCarsten Otte break; 216929b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 217029b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 217129b7c71bSCarsten Otte (u64 __user *)reg->addr); 217229b7c71bSCarsten Otte break; 217346a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 21744287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 217546a6dd1cSJason J. herne (u64 __user *)reg->addr); 217646a6dd1cSJason J. herne break; 217746a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 217846a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 217946a6dd1cSJason J. herne (u64 __user *)reg->addr); 218046a6dd1cSJason J. herne break; 2181536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2182536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 2183536336c2SDominik Dingel (u64 __user *)reg->addr); 2184536336c2SDominik Dingel break; 2185536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2186536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 2187536336c2SDominik Dingel (u64 __user *)reg->addr); 2188536336c2SDominik Dingel break; 2189536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2190536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 2191536336c2SDominik Dingel (u64 __user *)reg->addr); 2192536336c2SDominik Dingel break; 2193672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2194672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 2195672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2196672550fbSChristian Borntraeger break; 2197afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2198afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 2199afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2200afa45ff5SChristian Borntraeger break; 220114eebd91SCarsten Otte default: 220214eebd91SCarsten Otte break; 220314eebd91SCarsten Otte } 220414eebd91SCarsten Otte 220514eebd91SCarsten Otte return r; 220614eebd91SCarsten Otte } 220714eebd91SCarsten Otte 220814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 220914eebd91SCarsten Otte struct kvm_one_reg *reg) 221014eebd91SCarsten Otte { 221114eebd91SCarsten Otte int r = -EINVAL; 22124287f247SDavid Hildenbrand __u64 val; 221314eebd91SCarsten Otte 221414eebd91SCarsten Otte switch (reg->id) { 221529b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 221629b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 221729b7c71bSCarsten Otte (u32 __user *)reg->addr); 221829b7c71bSCarsten Otte break; 221929b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 222029b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 222129b7c71bSCarsten Otte (u64 __user *)reg->addr); 222229b7c71bSCarsten Otte break; 222346a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 22244287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 22254287f247SDavid Hildenbrand if (!r) 22264287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 222746a6dd1cSJason J. herne break; 222846a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 222946a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 223046a6dd1cSJason J. herne (u64 __user *)reg->addr); 223146a6dd1cSJason J. herne break; 2232536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 2233536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 2234536336c2SDominik Dingel (u64 __user *)reg->addr); 22359fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 22369fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2237536336c2SDominik Dingel break; 2238536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 2239536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 2240536336c2SDominik Dingel (u64 __user *)reg->addr); 2241536336c2SDominik Dingel break; 2242536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 2243536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 2244536336c2SDominik Dingel (u64 __user *)reg->addr); 2245536336c2SDominik Dingel break; 2246672550fbSChristian Borntraeger case KVM_REG_S390_PP: 2247672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 2248672550fbSChristian Borntraeger (u64 __user *)reg->addr); 2249672550fbSChristian Borntraeger break; 2250afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 2251afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 2252afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 2253afa45ff5SChristian Borntraeger break; 225414eebd91SCarsten Otte default: 225514eebd91SCarsten Otte break; 225614eebd91SCarsten Otte } 225714eebd91SCarsten Otte 225814eebd91SCarsten Otte return r; 225914eebd91SCarsten Otte } 2260b6d33834SChristoffer Dall 2261b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 2262b0c632dbSHeiko Carstens { 2263b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 2264b0c632dbSHeiko Carstens return 0; 2265b0c632dbSHeiko Carstens } 2266b0c632dbSHeiko Carstens 2267b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2268b0c632dbSHeiko Carstens { 22695a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 2270b0c632dbSHeiko Carstens return 0; 2271b0c632dbSHeiko Carstens } 2272b0c632dbSHeiko Carstens 2273b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 2274b0c632dbSHeiko Carstens { 22755a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 2276b0c632dbSHeiko Carstens return 0; 2277b0c632dbSHeiko Carstens } 2278b0c632dbSHeiko Carstens 2279b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 2280b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2281b0c632dbSHeiko Carstens { 228259674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 2283b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 2284b0c632dbSHeiko Carstens return 0; 2285b0c632dbSHeiko Carstens } 2286b0c632dbSHeiko Carstens 2287b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 2288b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 2289b0c632dbSHeiko Carstens { 229059674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 2291b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 2292b0c632dbSHeiko Carstens return 0; 2293b0c632dbSHeiko Carstens } 2294b0c632dbSHeiko Carstens 2295b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2296b0c632dbSHeiko Carstens { 22974725c860SMartin Schwidefsky if (test_fp_ctl(fpu->fpc)) 22984725c860SMartin Schwidefsky return -EINVAL; 2299e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 23009abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 2301a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 2302a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 23039abc2a08SDavid Hildenbrand else 2304a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 2305b0c632dbSHeiko Carstens return 0; 2306b0c632dbSHeiko Carstens } 2307b0c632dbSHeiko Carstens 2308b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 2309b0c632dbSHeiko Carstens { 23109abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 23119abc2a08SDavid Hildenbrand save_fpu_regs(); 23129abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 2313a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 2314a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 23159abc2a08SDavid Hildenbrand else 2316a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 2317e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 2318b0c632dbSHeiko Carstens return 0; 2319b0c632dbSHeiko Carstens } 2320b0c632dbSHeiko Carstens 2321b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 2322b0c632dbSHeiko Carstens { 2323b0c632dbSHeiko Carstens int rc = 0; 2324b0c632dbSHeiko Carstens 23257a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 2326b0c632dbSHeiko Carstens rc = -EBUSY; 2327d7b0b5ebSCarsten Otte else { 2328d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 2329d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 2330d7b0b5ebSCarsten Otte } 2331b0c632dbSHeiko Carstens return rc; 2332b0c632dbSHeiko Carstens } 2333b0c632dbSHeiko Carstens 2334b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 2335b0c632dbSHeiko Carstens struct kvm_translation *tr) 2336b0c632dbSHeiko Carstens { 2337b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 2338b0c632dbSHeiko Carstens } 2339b0c632dbSHeiko Carstens 234027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 234127291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 234227291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 234327291e21SDavid Hildenbrand 2344d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 2345d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 2346b0c632dbSHeiko Carstens { 234727291e21SDavid Hildenbrand int rc = 0; 234827291e21SDavid Hildenbrand 234927291e21SDavid Hildenbrand vcpu->guest_debug = 0; 235027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 235127291e21SDavid Hildenbrand 23522de3bfc2SDavid Hildenbrand if (dbg->control & ~VALID_GUESTDBG_FLAGS) 235327291e21SDavid Hildenbrand return -EINVAL; 235489b5b4deSDavid Hildenbrand if (!sclp.has_gpere) 235589b5b4deSDavid Hildenbrand return -EINVAL; 235627291e21SDavid Hildenbrand 235727291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 235827291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 235927291e21SDavid Hildenbrand /* enforce guest PER */ 2360805de8f4SPeter Zijlstra atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 236127291e21SDavid Hildenbrand 236227291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 236327291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 236427291e21SDavid Hildenbrand } else { 2365805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 236627291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 236727291e21SDavid Hildenbrand } 236827291e21SDavid Hildenbrand 236927291e21SDavid Hildenbrand if (rc) { 237027291e21SDavid Hildenbrand vcpu->guest_debug = 0; 237127291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 2372805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); 237327291e21SDavid Hildenbrand } 237427291e21SDavid Hildenbrand 237527291e21SDavid Hildenbrand return rc; 2376b0c632dbSHeiko Carstens } 2377b0c632dbSHeiko Carstens 237862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 237962d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 238062d9f0dbSMarcelo Tosatti { 23816352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 23826352e4d2SDavid Hildenbrand return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 23836352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 238462d9f0dbSMarcelo Tosatti } 238562d9f0dbSMarcelo Tosatti 238662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 238762d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 238862d9f0dbSMarcelo Tosatti { 23896352e4d2SDavid Hildenbrand int rc = 0; 23906352e4d2SDavid Hildenbrand 23916352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 23926352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 23936352e4d2SDavid Hildenbrand 23946352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 23956352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 23966352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 23976352e4d2SDavid Hildenbrand break; 23986352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 23996352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 24006352e4d2SDavid Hildenbrand break; 24016352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 24026352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 24036352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 24046352e4d2SDavid Hildenbrand default: 24056352e4d2SDavid Hildenbrand rc = -ENXIO; 24066352e4d2SDavid Hildenbrand } 24076352e4d2SDavid Hildenbrand 24086352e4d2SDavid Hildenbrand return rc; 240962d9f0dbSMarcelo Tosatti } 241062d9f0dbSMarcelo Tosatti 24118ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 24128ad35755SDavid Hildenbrand { 24138ad35755SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS; 24148ad35755SDavid Hildenbrand } 24158ad35755SDavid Hildenbrand 24162c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 24172c70fe44SChristian Borntraeger { 24188ad35755SDavid Hildenbrand retry: 24198e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 2420586b7ccdSChristian Borntraeger if (!vcpu->requests) 2421586b7ccdSChristian Borntraeger return 0; 24222c70fe44SChristian Borntraeger /* 24232c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 2424b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 24252c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 24262c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 24272c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 24282c70fe44SChristian Borntraeger */ 24298ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 24302c70fe44SChristian Borntraeger int rc; 2431b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 2432fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 2433b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 2434aca411a4SJulius Niedworok if (rc) { 2435aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 24362c70fe44SChristian Borntraeger return rc; 2437aca411a4SJulius Niedworok } 24388ad35755SDavid Hildenbrand goto retry; 24392c70fe44SChristian Borntraeger } 24408ad35755SDavid Hildenbrand 2441d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 2442d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 2443d3d692c8SDavid Hildenbrand goto retry; 2444d3d692c8SDavid Hildenbrand } 2445d3d692c8SDavid Hildenbrand 24468ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 24478ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 24488ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 2449805de8f4SPeter Zijlstra atomic_or(CPUSTAT_IBS, 24508ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 24518ad35755SDavid Hildenbrand } 24528ad35755SDavid Hildenbrand goto retry; 24538ad35755SDavid Hildenbrand } 24548ad35755SDavid Hildenbrand 24558ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 24568ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 24578ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 2458805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_IBS, 24598ad35755SDavid Hildenbrand &vcpu->arch.sie_block->cpuflags); 24608ad35755SDavid Hildenbrand } 24618ad35755SDavid Hildenbrand goto retry; 24628ad35755SDavid Hildenbrand } 24638ad35755SDavid Hildenbrand 24646502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 24656502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 24666502a34cSDavid Hildenbrand goto retry; 24676502a34cSDavid Hildenbrand } 24686502a34cSDavid Hildenbrand 24690759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 24700759d068SDavid Hildenbrand clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 24710759d068SDavid Hildenbrand 24722c70fe44SChristian Borntraeger return 0; 24732c70fe44SChristian Borntraeger } 24742c70fe44SChristian Borntraeger 247525ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod) 247625ed1675SDavid Hildenbrand { 247725ed1675SDavid Hildenbrand struct kvm_vcpu *vcpu; 247825ed1675SDavid Hildenbrand int i; 247925ed1675SDavid Hildenbrand 248025ed1675SDavid Hildenbrand mutex_lock(&kvm->lock); 248125ed1675SDavid Hildenbrand preempt_disable(); 248225ed1675SDavid Hildenbrand kvm->arch.epoch = tod - get_tod_clock(); 248325ed1675SDavid Hildenbrand kvm_s390_vcpu_block_all(kvm); 248425ed1675SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) 248525ed1675SDavid Hildenbrand vcpu->arch.sie_block->epoch = kvm->arch.epoch; 248625ed1675SDavid Hildenbrand kvm_s390_vcpu_unblock_all(kvm); 248725ed1675SDavid Hildenbrand preempt_enable(); 248825ed1675SDavid Hildenbrand mutex_unlock(&kvm->lock); 248925ed1675SDavid Hildenbrand } 249025ed1675SDavid Hildenbrand 2491fa576c58SThomas Huth /** 2492fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 2493fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 2494fa576c58SThomas Huth * @gpa: Guest physical address 2495fa576c58SThomas Huth * @writable: Whether the page should be writable or not 2496fa576c58SThomas Huth * 2497fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 2498fa576c58SThomas Huth * 2499fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 2500fa576c58SThomas Huth */ 2501fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 250224eb3a82SDominik Dingel { 2503527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 2504527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 250524eb3a82SDominik Dingel } 250624eb3a82SDominik Dingel 25073c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 25083c038e6bSDominik Dingel unsigned long token) 25093c038e6bSDominik Dingel { 25103c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 2511383d0b05SJens Freimann struct kvm_s390_irq irq; 25123c038e6bSDominik Dingel 25133c038e6bSDominik Dingel if (start_token) { 2514383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 2515383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 2516383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 25173c038e6bSDominik Dingel } else { 25183c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 2519383d0b05SJens Freimann inti.parm64 = token; 25203c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 25213c038e6bSDominik Dingel } 25223c038e6bSDominik Dingel } 25233c038e6bSDominik Dingel 25243c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 25253c038e6bSDominik Dingel struct kvm_async_pf *work) 25263c038e6bSDominik Dingel { 25273c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 25283c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 25293c038e6bSDominik Dingel } 25303c038e6bSDominik Dingel 25313c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 25323c038e6bSDominik Dingel struct kvm_async_pf *work) 25333c038e6bSDominik Dingel { 25343c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 25353c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 25363c038e6bSDominik Dingel } 25373c038e6bSDominik Dingel 25383c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 25393c038e6bSDominik Dingel struct kvm_async_pf *work) 25403c038e6bSDominik Dingel { 25413c038e6bSDominik Dingel /* s390 will always inject the page directly */ 25423c038e6bSDominik Dingel } 25433c038e6bSDominik Dingel 25443c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 25453c038e6bSDominik Dingel { 25463c038e6bSDominik Dingel /* 25473c038e6bSDominik Dingel * s390 will always inject the page directly, 25483c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 25493c038e6bSDominik Dingel */ 25503c038e6bSDominik Dingel return true; 25513c038e6bSDominik Dingel } 25523c038e6bSDominik Dingel 25533c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 25543c038e6bSDominik Dingel { 25553c038e6bSDominik Dingel hva_t hva; 25563c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 25573c038e6bSDominik Dingel int rc; 25583c038e6bSDominik Dingel 25593c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 25603c038e6bSDominik Dingel return 0; 25613c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 25623c038e6bSDominik Dingel vcpu->arch.pfault_compare) 25633c038e6bSDominik Dingel return 0; 25643c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 25653c038e6bSDominik Dingel return 0; 25669a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 25673c038e6bSDominik Dingel return 0; 25683c038e6bSDominik Dingel if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) 25693c038e6bSDominik Dingel return 0; 25703c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 25713c038e6bSDominik Dingel return 0; 25723c038e6bSDominik Dingel 257381480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 257481480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 257581480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 25763c038e6bSDominik Dingel return 0; 25773c038e6bSDominik Dingel 25783c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 25793c038e6bSDominik Dingel return rc; 25803c038e6bSDominik Dingel } 25813c038e6bSDominik Dingel 25823fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 2583b0c632dbSHeiko Carstens { 25843fb4c40fSThomas Huth int rc, cpuflags; 2585e168bf8dSCarsten Otte 25863c038e6bSDominik Dingel /* 25873c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 25883c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 25893c038e6bSDominik Dingel * handled outside the worker. 25903c038e6bSDominik Dingel */ 25913c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 25923c038e6bSDominik Dingel 25937ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 25947ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 2595b0c632dbSHeiko Carstens 2596b0c632dbSHeiko Carstens if (need_resched()) 2597b0c632dbSHeiko Carstens schedule(); 2598b0c632dbSHeiko Carstens 2599d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 260071cde587SChristian Borntraeger s390_handle_mcck(); 260171cde587SChristian Borntraeger 260279395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 260379395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 260479395031SJens Freimann if (rc) 260579395031SJens Freimann return rc; 260679395031SJens Freimann } 26070ff31867SCarsten Otte 26082c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 26092c70fe44SChristian Borntraeger if (rc) 26102c70fe44SChristian Borntraeger return rc; 26112c70fe44SChristian Borntraeger 261227291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 261327291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 261427291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 261527291e21SDavid Hildenbrand } 261627291e21SDavid Hildenbrand 2617b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 26183fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 26193fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 26203fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 26212b29a9fdSDominik Dingel 26223fb4c40fSThomas Huth return 0; 26233fb4c40fSThomas Huth } 26243fb4c40fSThomas Huth 2625492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 2626492d8642SThomas Huth { 262756317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 262856317920SDavid Hildenbrand .code = PGM_ADDRESSING, 262956317920SDavid Hildenbrand }; 263056317920SDavid Hildenbrand u8 opcode, ilen; 2631492d8642SThomas Huth int rc; 2632492d8642SThomas Huth 2633492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 2634492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 2635492d8642SThomas Huth 2636492d8642SThomas Huth /* 2637492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 2638492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 2639492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 2640492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 2641492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 2642492d8642SThomas Huth * to be able to forward the PSW. 2643492d8642SThomas Huth */ 26443fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 264556317920SDavid Hildenbrand ilen = insn_length(opcode); 26469b0d721aSDavid Hildenbrand if (rc < 0) { 26479b0d721aSDavid Hildenbrand return rc; 26489b0d721aSDavid Hildenbrand } else if (rc) { 26499b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 26509b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 26519b0d721aSDavid Hildenbrand * nullification if necessary. 26529b0d721aSDavid Hildenbrand */ 26539b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 26549b0d721aSDavid Hildenbrand ilen = 4; 26559b0d721aSDavid Hildenbrand } 265656317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 265756317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 265856317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 2659492d8642SThomas Huth } 2660492d8642SThomas Huth 26613fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 26623fb4c40fSThomas Huth { 26632b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 26642b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 26652b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 26662b29a9fdSDominik Dingel 266727291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 266827291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 266927291e21SDavid Hildenbrand 26707ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 26717ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 267271f116bfSDavid Hildenbrand 267371f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 267471f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 267571f116bfSDavid Hildenbrand 267671f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 267771f116bfSDavid Hildenbrand return rc; 267871f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 267971f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 268071f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 268171f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 268271f116bfSDavid Hildenbrand return -EREMOTE; 268371f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 268471f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 268571f116bfSDavid Hildenbrand return 0; 2686210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 2687210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 2688210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 2689210b1607SThomas Huth current->thread.gmap_addr; 2690210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 269171f116bfSDavid Hildenbrand return -EREMOTE; 269224eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 26933c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 269424eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 269571f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 269671f116bfSDavid Hildenbrand return 0; 269771f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 2698fa576c58SThomas Huth } 269971f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 27003fb4c40fSThomas Huth } 27013fb4c40fSThomas Huth 27023fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 27033fb4c40fSThomas Huth { 27043fb4c40fSThomas Huth int rc, exit_reason; 27053fb4c40fSThomas Huth 2706800c1065SThomas Huth /* 2707800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 2708800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 2709800c1065SThomas Huth */ 2710800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 2711800c1065SThomas Huth 2712a76ccff6SThomas Huth do { 27133fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 27143fb4c40fSThomas Huth if (rc) 2715a76ccff6SThomas Huth break; 27163fb4c40fSThomas Huth 2717800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 27183fb4c40fSThomas Huth /* 2719a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 2720a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 27213fb4c40fSThomas Huth */ 27220097d12eSChristian Borntraeger local_irq_disable(); 27236edaa530SPaolo Bonzini guest_enter_irqoff(); 2724db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 27250097d12eSChristian Borntraeger local_irq_enable(); 2726a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 2727a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 27280097d12eSChristian Borntraeger local_irq_disable(); 2729db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 27306edaa530SPaolo Bonzini guest_exit_irqoff(); 27310097d12eSChristian Borntraeger local_irq_enable(); 2732800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 27333fb4c40fSThomas Huth 27343fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 273527291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 27363fb4c40fSThomas Huth 2737800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 2738e168bf8dSCarsten Otte return rc; 2739b0c632dbSHeiko Carstens } 2740b0c632dbSHeiko Carstens 2741b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2742b028ee3eSDavid Hildenbrand { 27434d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 2744*4e0b1ab7SFan Zhang struct gs_cb *gscb; 27454d5f2c04SChristian Borntraeger 27464d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 2747*4e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 2748b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 2749b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 2750b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 2751b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 2752b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 2753b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 2754d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 2755d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 2756b028ee3eSDavid Hildenbrand } 2757b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 27584287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 2759b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 2760b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 2761b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 2762b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 2763b028ee3eSDavid Hildenbrand } 2764b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 2765b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 2766b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 2767b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 27689fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 27699fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 2770b028ee3eSDavid Hildenbrand } 277180cd8763SFan Zhang /* 277280cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 277380cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 277480cd8763SFan Zhang */ 277580cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 27764d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 27774d5f2c04SChristian Borntraeger riccb->valid && 27780c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 27794d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 27800c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 278180cd8763SFan Zhang } 2782*4e0b1ab7SFan Zhang /* 2783*4e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 2784*4e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 2785*4e0b1ab7SFan Zhang */ 2786*4e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 2787*4e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 2788*4e0b1ab7SFan Zhang gscb->gssm && 2789*4e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 2790*4e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 2791*4e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 2792*4e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 2793*4e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 2794*4e0b1ab7SFan Zhang } 279531d8b8d4SChristian Borntraeger save_access_regs(vcpu->arch.host_acrs); 279631d8b8d4SChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 2797e1788bb9SChristian Borntraeger /* save host (userspace) fprs/vrs */ 2798e1788bb9SChristian Borntraeger save_fpu_regs(); 2799e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 2800e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 2801e1788bb9SChristian Borntraeger if (MACHINE_HAS_VX) 2802e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.vrs; 2803e1788bb9SChristian Borntraeger else 2804e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.fprs; 2805e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 2806e1788bb9SChristian Borntraeger if (test_fp_ctl(current->thread.fpu.fpc)) 2807e1788bb9SChristian Borntraeger /* User space provided an invalid FPC, let's clear it */ 2808e1788bb9SChristian Borntraeger current->thread.fpu.fpc = 0; 2809*4e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 2810*4e0b1ab7SFan Zhang preempt_disable(); 2811*4e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 2812*4e0b1ab7SFan Zhang if (current->thread.gs_cb) { 2813*4e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 2814*4e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 2815*4e0b1ab7SFan Zhang } 2816*4e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 2817*4e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 2818*4e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 2819*4e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 2820*4e0b1ab7SFan Zhang } 2821*4e0b1ab7SFan Zhang preempt_enable(); 2822*4e0b1ab7SFan Zhang } 282380cd8763SFan Zhang 2824b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 2825b028ee3eSDavid Hildenbrand } 2826b028ee3eSDavid Hildenbrand 2827b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2828b028ee3eSDavid Hildenbrand { 2829b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 2830b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 2831b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 2832b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 28334287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 2834b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 2835b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 2836b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 2837b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 2838b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 2839b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 2840b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 284131d8b8d4SChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 284231d8b8d4SChristian Borntraeger restore_access_regs(vcpu->arch.host_acrs); 2843e1788bb9SChristian Borntraeger /* Save guest register state */ 2844e1788bb9SChristian Borntraeger save_fpu_regs(); 2845e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 2846e1788bb9SChristian Borntraeger /* Restore will be done lazily at return */ 2847e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 2848e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 2849*4e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 2850*4e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 2851*4e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 2852*4e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 2853*4e0b1ab7SFan Zhang preempt_disable(); 2854*4e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 2855*4e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 2856*4e0b1ab7SFan Zhang preempt_enable(); 2857*4e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 2858*4e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 2859*4e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 2860*4e0b1ab7SFan Zhang } 2861e1788bb9SChristian Borntraeger 2862b028ee3eSDavid Hildenbrand } 2863b028ee3eSDavid Hildenbrand 2864b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 2865b0c632dbSHeiko Carstens { 28668f2abe6aSChristian Borntraeger int rc; 2867b0c632dbSHeiko Carstens sigset_t sigsaved; 2868b0c632dbSHeiko Carstens 2869460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 2870460df4c1SPaolo Bonzini return -EINTR; 2871460df4c1SPaolo Bonzini 287227291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 287327291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 287427291e21SDavid Hildenbrand return 0; 287527291e21SDavid Hildenbrand } 287627291e21SDavid Hildenbrand 2877b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2878b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 2879b0c632dbSHeiko Carstens 28806352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 28816852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 28826352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 2883ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 28846352e4d2SDavid Hildenbrand vcpu->vcpu_id); 28856352e4d2SDavid Hildenbrand return -EINVAL; 28866352e4d2SDavid Hildenbrand } 2887b0c632dbSHeiko Carstens 2888b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 2889db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 2890d7b0b5ebSCarsten Otte 2891dab4079dSHeiko Carstens might_fault(); 2892e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 28939ace903dSChristian Ehrhardt 2894b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 2895b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 28968f2abe6aSChristian Borntraeger rc = -EINTR; 2897b1d16c49SChristian Ehrhardt } 28988f2abe6aSChristian Borntraeger 289927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 290027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 290127291e21SDavid Hildenbrand rc = 0; 290227291e21SDavid Hildenbrand } 290327291e21SDavid Hildenbrand 29048f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 290571f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 29068f2abe6aSChristian Borntraeger rc = 0; 29078f2abe6aSChristian Borntraeger } 29088f2abe6aSChristian Borntraeger 2909db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 2910b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 2911d7b0b5ebSCarsten Otte 2912b0c632dbSHeiko Carstens if (vcpu->sigset_active) 2913b0c632dbSHeiko Carstens sigprocmask(SIG_SETMASK, &sigsaved, NULL); 2914b0c632dbSHeiko Carstens 2915b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 29167e8e6ab4SHeiko Carstens return rc; 2917b0c632dbSHeiko Carstens } 2918b0c632dbSHeiko Carstens 2919b0c632dbSHeiko Carstens /* 2920b0c632dbSHeiko Carstens * store status at address 2921b0c632dbSHeiko Carstens * we use have two special cases: 2922b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 2923b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 2924b0c632dbSHeiko Carstens */ 2925d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 2926b0c632dbSHeiko Carstens { 2927092670cdSCarsten Otte unsigned char archmode = 1; 29289abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 2929fda902cbSMichael Mueller unsigned int px; 29304287f247SDavid Hildenbrand u64 clkcomp, cputm; 2931d0bce605SHeiko Carstens int rc; 2932b0c632dbSHeiko Carstens 2933d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 2934d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 2935d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 2936b0c632dbSHeiko Carstens return -EFAULT; 2937d9a3a09aSMartin Schwidefsky gpa = 0; 2938d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 2939d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 2940b0c632dbSHeiko Carstens return -EFAULT; 2941d9a3a09aSMartin Schwidefsky gpa = px; 2942d9a3a09aSMartin Schwidefsky } else 2943d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 29449abc2a08SDavid Hildenbrand 29459abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 29469abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 29479522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 2948d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 29499abc2a08SDavid Hildenbrand fprs, 128); 29509abc2a08SDavid Hildenbrand } else { 29519abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 29526fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 29539abc2a08SDavid Hildenbrand } 2954d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 2955d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 2956d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 2957d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 2958d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 2959fda902cbSMichael Mueller &px, 4); 2960d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 29619abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 2962d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 2963d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 29644287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 2965d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 29664287f247SDavid Hildenbrand &cputm, 8); 2967178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 2968d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 2969d0bce605SHeiko Carstens &clkcomp, 8); 2970d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 2971d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 2972d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 2973d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 2974d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 2975b0c632dbSHeiko Carstens } 2976b0c632dbSHeiko Carstens 2977e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 2978e879892cSThomas Huth { 2979e879892cSThomas Huth /* 2980e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 298131d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 2982e879892cSThomas Huth * it into the save area 2983e879892cSThomas Huth */ 2984d0164ee2SHendrik Brueckner save_fpu_regs(); 29859abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 2986e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 2987e879892cSThomas Huth 2988e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 2989e879892cSThomas Huth } 2990e879892cSThomas Huth 29918ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 29928ad35755SDavid Hildenbrand { 29938ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 29948e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 29958ad35755SDavid Hildenbrand } 29968ad35755SDavid Hildenbrand 29978ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 29988ad35755SDavid Hildenbrand { 29998ad35755SDavid Hildenbrand unsigned int i; 30008ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 30018ad35755SDavid Hildenbrand 30028ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 30038ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 30048ad35755SDavid Hildenbrand } 30058ad35755SDavid Hildenbrand } 30068ad35755SDavid Hildenbrand 30078ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 30088ad35755SDavid Hildenbrand { 300909a400e7SDavid Hildenbrand if (!sclp.has_ibs) 301009a400e7SDavid Hildenbrand return; 30118ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 30128e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 30138ad35755SDavid Hildenbrand } 30148ad35755SDavid Hildenbrand 30156852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 30166852d7b6SDavid Hildenbrand { 30178ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 30188ad35755SDavid Hildenbrand 30198ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 30208ad35755SDavid Hildenbrand return; 30218ad35755SDavid Hildenbrand 30226852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 30238ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 3024433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 30258ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 30268ad35755SDavid Hildenbrand 30278ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 30288ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 30298ad35755SDavid Hildenbrand started_vcpus++; 30308ad35755SDavid Hildenbrand } 30318ad35755SDavid Hildenbrand 30328ad35755SDavid Hildenbrand if (started_vcpus == 0) { 30338ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 30348ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 30358ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 30368ad35755SDavid Hildenbrand /* 30378ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 30388ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 30398ad35755SDavid Hildenbrand * oustanding ENABLE requests. 30408ad35755SDavid Hildenbrand */ 30418ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 30428ad35755SDavid Hildenbrand } 30438ad35755SDavid Hildenbrand 3044805de8f4SPeter Zijlstra atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 30458ad35755SDavid Hildenbrand /* 30468ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 30478ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 30488ad35755SDavid Hildenbrand */ 3049d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 3050433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 30518ad35755SDavid Hildenbrand return; 30526852d7b6SDavid Hildenbrand } 30536852d7b6SDavid Hildenbrand 30546852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 30556852d7b6SDavid Hildenbrand { 30568ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 30578ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 30588ad35755SDavid Hildenbrand 30598ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 30608ad35755SDavid Hildenbrand return; 30618ad35755SDavid Hildenbrand 30626852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 30638ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 3064433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 30658ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 30668ad35755SDavid Hildenbrand 306732f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 30686cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 306932f5ff63SDavid Hildenbrand 3070805de8f4SPeter Zijlstra atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); 30718ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 30728ad35755SDavid Hildenbrand 30738ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 30748ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 30758ad35755SDavid Hildenbrand started_vcpus++; 30768ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 30778ad35755SDavid Hildenbrand } 30788ad35755SDavid Hildenbrand } 30798ad35755SDavid Hildenbrand 30808ad35755SDavid Hildenbrand if (started_vcpus == 1) { 30818ad35755SDavid Hildenbrand /* 30828ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 30838ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 30848ad35755SDavid Hildenbrand */ 30858ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 30868ad35755SDavid Hildenbrand } 30878ad35755SDavid Hildenbrand 3088433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 30898ad35755SDavid Hildenbrand return; 30906852d7b6SDavid Hildenbrand } 30916852d7b6SDavid Hildenbrand 3092d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 3093d6712df9SCornelia Huck struct kvm_enable_cap *cap) 3094d6712df9SCornelia Huck { 3095d6712df9SCornelia Huck int r; 3096d6712df9SCornelia Huck 3097d6712df9SCornelia Huck if (cap->flags) 3098d6712df9SCornelia Huck return -EINVAL; 3099d6712df9SCornelia Huck 3100d6712df9SCornelia Huck switch (cap->cap) { 3101fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 3102fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 3103fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 3104c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 3105fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 3106fa6b7fe9SCornelia Huck } 3107fa6b7fe9SCornelia Huck r = 0; 3108fa6b7fe9SCornelia Huck break; 3109d6712df9SCornelia Huck default: 3110d6712df9SCornelia Huck r = -EINVAL; 3111d6712df9SCornelia Huck break; 3112d6712df9SCornelia Huck } 3113d6712df9SCornelia Huck return r; 3114d6712df9SCornelia Huck } 3115d6712df9SCornelia Huck 311641408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 311741408c28SThomas Huth struct kvm_s390_mem_op *mop) 311841408c28SThomas Huth { 311941408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 312041408c28SThomas Huth void *tmpbuf = NULL; 312141408c28SThomas Huth int r, srcu_idx; 312241408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 312341408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 312441408c28SThomas Huth 312541408c28SThomas Huth if (mop->flags & ~supported_flags) 312641408c28SThomas Huth return -EINVAL; 312741408c28SThomas Huth 312841408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 312941408c28SThomas Huth return -E2BIG; 313041408c28SThomas Huth 313141408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 313241408c28SThomas Huth tmpbuf = vmalloc(mop->size); 313341408c28SThomas Huth if (!tmpbuf) 313441408c28SThomas Huth return -ENOMEM; 313541408c28SThomas Huth } 313641408c28SThomas Huth 313741408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 313841408c28SThomas Huth 313941408c28SThomas Huth switch (mop->op) { 314041408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 314141408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 314292c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 314392c96321SDavid Hildenbrand mop->size, GACC_FETCH); 314441408c28SThomas Huth break; 314541408c28SThomas Huth } 314641408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 314741408c28SThomas Huth if (r == 0) { 314841408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 314941408c28SThomas Huth r = -EFAULT; 315041408c28SThomas Huth } 315141408c28SThomas Huth break; 315241408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 315341408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 315492c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 315592c96321SDavid Hildenbrand mop->size, GACC_STORE); 315641408c28SThomas Huth break; 315741408c28SThomas Huth } 315841408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 315941408c28SThomas Huth r = -EFAULT; 316041408c28SThomas Huth break; 316141408c28SThomas Huth } 316241408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 316341408c28SThomas Huth break; 316441408c28SThomas Huth default: 316541408c28SThomas Huth r = -EINVAL; 316641408c28SThomas Huth } 316741408c28SThomas Huth 316841408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 316941408c28SThomas Huth 317041408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 317141408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 317241408c28SThomas Huth 317341408c28SThomas Huth vfree(tmpbuf); 317441408c28SThomas Huth return r; 317541408c28SThomas Huth } 317641408c28SThomas Huth 3177b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp, 3178b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 3179b0c632dbSHeiko Carstens { 3180b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 3181b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 3182800c1065SThomas Huth int idx; 3183bc923cc9SAvi Kivity long r; 3184b0c632dbSHeiko Carstens 318593736624SAvi Kivity switch (ioctl) { 318647b43c52SJens Freimann case KVM_S390_IRQ: { 318747b43c52SJens Freimann struct kvm_s390_irq s390irq; 318847b43c52SJens Freimann 318947b43c52SJens Freimann r = -EFAULT; 319047b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 319147b43c52SJens Freimann break; 319247b43c52SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 319347b43c52SJens Freimann break; 319447b43c52SJens Freimann } 319593736624SAvi Kivity case KVM_S390_INTERRUPT: { 3196ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 3197383d0b05SJens Freimann struct kvm_s390_irq s390irq; 3198ba5c1e9bSCarsten Otte 319993736624SAvi Kivity r = -EFAULT; 3200ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 320193736624SAvi Kivity break; 3202383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 3203383d0b05SJens Freimann return -EINVAL; 3204383d0b05SJens Freimann r = kvm_s390_inject_vcpu(vcpu, &s390irq); 320593736624SAvi Kivity break; 3206ba5c1e9bSCarsten Otte } 3207b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 3208800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 3209bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 3210800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 3211bc923cc9SAvi Kivity break; 3212b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 3213b0c632dbSHeiko Carstens psw_t psw; 3214b0c632dbSHeiko Carstens 3215bc923cc9SAvi Kivity r = -EFAULT; 3216b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 3217bc923cc9SAvi Kivity break; 3218bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 3219bc923cc9SAvi Kivity break; 3220b0c632dbSHeiko Carstens } 3221b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 3222bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 3223bc923cc9SAvi Kivity break; 322414eebd91SCarsten Otte case KVM_SET_ONE_REG: 322514eebd91SCarsten Otte case KVM_GET_ONE_REG: { 322614eebd91SCarsten Otte struct kvm_one_reg reg; 322714eebd91SCarsten Otte r = -EFAULT; 322814eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 322914eebd91SCarsten Otte break; 323014eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 323114eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 323214eebd91SCarsten Otte else 323314eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 323414eebd91SCarsten Otte break; 323514eebd91SCarsten Otte } 323627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 323727e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 323827e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 323927e0393fSCarsten Otte 324027e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 324127e0393fSCarsten Otte r = -EFAULT; 324227e0393fSCarsten Otte break; 324327e0393fSCarsten Otte } 324427e0393fSCarsten Otte 324527e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 324627e0393fSCarsten Otte r = -EINVAL; 324727e0393fSCarsten Otte break; 324827e0393fSCarsten Otte } 324927e0393fSCarsten Otte 325027e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 325127e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 325227e0393fSCarsten Otte break; 325327e0393fSCarsten Otte } 325427e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 325527e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 325627e0393fSCarsten Otte 325727e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 325827e0393fSCarsten Otte r = -EFAULT; 325927e0393fSCarsten Otte break; 326027e0393fSCarsten Otte } 326127e0393fSCarsten Otte 326227e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 326327e0393fSCarsten Otte r = -EINVAL; 326427e0393fSCarsten Otte break; 326527e0393fSCarsten Otte } 326627e0393fSCarsten Otte 326727e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 326827e0393fSCarsten Otte ucasmap.length); 326927e0393fSCarsten Otte break; 327027e0393fSCarsten Otte } 327127e0393fSCarsten Otte #endif 3272ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 3273527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 3274ccc7910fSCarsten Otte break; 3275ccc7910fSCarsten Otte } 3276d6712df9SCornelia Huck case KVM_ENABLE_CAP: 3277d6712df9SCornelia Huck { 3278d6712df9SCornelia Huck struct kvm_enable_cap cap; 3279d6712df9SCornelia Huck r = -EFAULT; 3280d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 3281d6712df9SCornelia Huck break; 3282d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 3283d6712df9SCornelia Huck break; 3284d6712df9SCornelia Huck } 328541408c28SThomas Huth case KVM_S390_MEM_OP: { 328641408c28SThomas Huth struct kvm_s390_mem_op mem_op; 328741408c28SThomas Huth 328841408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 328941408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 329041408c28SThomas Huth else 329141408c28SThomas Huth r = -EFAULT; 329241408c28SThomas Huth break; 329341408c28SThomas Huth } 3294816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 3295816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3296816c7667SJens Freimann 3297816c7667SJens Freimann r = -EFAULT; 3298816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3299816c7667SJens Freimann break; 3300816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 3301816c7667SJens Freimann irq_state.len == 0 || 3302816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 3303816c7667SJens Freimann r = -EINVAL; 3304816c7667SJens Freimann break; 3305816c7667SJens Freimann } 3306816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 3307816c7667SJens Freimann (void __user *) irq_state.buf, 3308816c7667SJens Freimann irq_state.len); 3309816c7667SJens Freimann break; 3310816c7667SJens Freimann } 3311816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 3312816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 3313816c7667SJens Freimann 3314816c7667SJens Freimann r = -EFAULT; 3315816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 3316816c7667SJens Freimann break; 3317816c7667SJens Freimann if (irq_state.len == 0) { 3318816c7667SJens Freimann r = -EINVAL; 3319816c7667SJens Freimann break; 3320816c7667SJens Freimann } 3321816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 3322816c7667SJens Freimann (__u8 __user *) irq_state.buf, 3323816c7667SJens Freimann irq_state.len); 3324816c7667SJens Freimann break; 3325816c7667SJens Freimann } 3326b0c632dbSHeiko Carstens default: 33273e6afcf1SCarsten Otte r = -ENOTTY; 3328b0c632dbSHeiko Carstens } 3329bc923cc9SAvi Kivity return r; 3330b0c632dbSHeiko Carstens } 3331b0c632dbSHeiko Carstens 33325b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 33335b1c1493SCarsten Otte { 33345b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 33355b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 33365b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 33375b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 33385b1c1493SCarsten Otte get_page(vmf->page); 33395b1c1493SCarsten Otte return 0; 33405b1c1493SCarsten Otte } 33415b1c1493SCarsten Otte #endif 33425b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 33435b1c1493SCarsten Otte } 33445b1c1493SCarsten Otte 33455587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 33465587027cSAneesh Kumar K.V unsigned long npages) 3347db3fe4ebSTakuya Yoshikawa { 3348db3fe4ebSTakuya Yoshikawa return 0; 3349db3fe4ebSTakuya Yoshikawa } 3350db3fe4ebSTakuya Yoshikawa 3351b0c632dbSHeiko Carstens /* Section: memory related */ 3352f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 3353f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 335409170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 33557b6195a9STakuya Yoshikawa enum kvm_mr_change change) 3356b0c632dbSHeiko Carstens { 3357dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 3358dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 3359dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 3360dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 3361b0c632dbSHeiko Carstens 3362598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 3363b0c632dbSHeiko Carstens return -EINVAL; 3364b0c632dbSHeiko Carstens 3365598841caSCarsten Otte if (mem->memory_size & 0xffffful) 3366b0c632dbSHeiko Carstens return -EINVAL; 3367b0c632dbSHeiko Carstens 3368a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 3369a3a92c31SDominik Dingel return -EINVAL; 3370a3a92c31SDominik Dingel 3371f7784b8eSMarcelo Tosatti return 0; 3372f7784b8eSMarcelo Tosatti } 3373f7784b8eSMarcelo Tosatti 3374f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 337509170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 33768482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 3377f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 33788482644aSTakuya Yoshikawa enum kvm_mr_change change) 3379f7784b8eSMarcelo Tosatti { 3380f7850c92SCarsten Otte int rc; 3381f7784b8eSMarcelo Tosatti 33822cef4debSChristian Borntraeger /* If the basics of the memslot do not change, we do not want 33832cef4debSChristian Borntraeger * to update the gmap. Every update causes several unnecessary 33842cef4debSChristian Borntraeger * segment translation exceptions. This is usually handled just 33852cef4debSChristian Borntraeger * fine by the normal fault handler + gmap, but it will also 33862cef4debSChristian Borntraeger * cause faults on the prefix page of running guest CPUs. 33872cef4debSChristian Borntraeger */ 33882cef4debSChristian Borntraeger if (old->userspace_addr == mem->userspace_addr && 33892cef4debSChristian Borntraeger old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && 33902cef4debSChristian Borntraeger old->npages * PAGE_SIZE == mem->memory_size) 33912cef4debSChristian Borntraeger return; 3392598841caSCarsten Otte 3393598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 3394598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 3395598841caSCarsten Otte if (rc) 3396ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 3397598841caSCarsten Otte return; 3398b0c632dbSHeiko Carstens } 3399b0c632dbSHeiko Carstens 340060a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 340160a37709SAlexander Yarygin { 340260a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 340360a37709SAlexander Yarygin 340460a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 340560a37709SAlexander Yarygin } 340660a37709SAlexander Yarygin 34073491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 34083491caf2SChristian Borntraeger { 34093491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 34103491caf2SChristian Borntraeger } 34113491caf2SChristian Borntraeger 3412b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 3413b0c632dbSHeiko Carstens { 341460a37709SAlexander Yarygin int i; 341560a37709SAlexander Yarygin 341607197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 341707197fd0SDavid Hildenbrand pr_info("SIE not available\n"); 341807197fd0SDavid Hildenbrand return -ENODEV; 341907197fd0SDavid Hildenbrand } 342007197fd0SDavid Hildenbrand 342160a37709SAlexander Yarygin for (i = 0; i < 16; i++) 342260a37709SAlexander Yarygin kvm_s390_fac_list_mask[i] |= 342360a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 342460a37709SAlexander Yarygin 34259d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 3426b0c632dbSHeiko Carstens } 3427b0c632dbSHeiko Carstens 3428b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 3429b0c632dbSHeiko Carstens { 3430b0c632dbSHeiko Carstens kvm_exit(); 3431b0c632dbSHeiko Carstens } 3432b0c632dbSHeiko Carstens 3433b0c632dbSHeiko Carstens module_init(kvm_s390_init); 3434b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 3435566af940SCornelia Huck 3436566af940SCornelia Huck /* 3437566af940SCornelia Huck * Enable autoloading of the kvm module. 3438566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 3439566af940SCornelia Huck * since x86 takes a different approach. 3440566af940SCornelia Huck */ 3441566af940SCornelia Huck #include <linux/miscdevice.h> 3442566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 3443566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 3444