1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2b0c632dbSHeiko Carstens /* 3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x) 4b0c632dbSHeiko Carstens * 5a37cb07aSChristian Borntraeger * Copyright IBM Corp. 2008, 2018 6b0c632dbSHeiko Carstens * 7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 9b0c632dbSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 10628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1115f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 12b0c632dbSHeiko Carstens */ 13b0c632dbSHeiko Carstens 147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 167aedd9d4SMichael Mueller 17b0c632dbSHeiko Carstens #include <linux/compiler.h> 18b0c632dbSHeiko Carstens #include <linux/err.h> 19b0c632dbSHeiko Carstens #include <linux/fs.h> 20ca872302SChristian Borntraeger #include <linux/hrtimer.h> 21b0c632dbSHeiko Carstens #include <linux/init.h> 22b0c632dbSHeiko Carstens #include <linux/kvm.h> 23b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 24b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 25b0c632dbSHeiko Carstens #include <linux/module.h> 26d3217967SPaul Gortmaker #include <linux/moduleparam.h> 27a374e892STony Krowiak #include <linux/random.h> 28b0c632dbSHeiko Carstens #include <linux/slab.h> 29ba5c1e9bSCarsten Otte #include <linux/timer.h> 3041408c28SThomas Huth #include <linux/vmalloc.h> 3115c9705fSDavid Hildenbrand #include <linux/bitmap.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33190df4a2SClaudio Imbrenda #include <linux/string.h> 34174cd4b1SIngo Molnar 35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 36b0c632dbSHeiko Carstens #include <asm/lowcore.h> 37fd5ada04SMartin Schwidefsky #include <asm/stp.h> 38b0c632dbSHeiko Carstens #include <asm/pgtable.h> 391e133ab2SMartin Schwidefsky #include <asm/gmap.h> 40f5daba1dSHeiko Carstens #include <asm/nmi.h> 41a0616cdeSDavid Howells #include <asm/switch_to.h> 426d3da241SJens Freimann #include <asm/isc.h> 431526bf9cSChristian Borntraeger #include <asm/sclp.h> 440a763c78SDavid Hildenbrand #include <asm/cpacf.h> 45221bb8a4SLinus Torvalds #include <asm/timex.h> 46e585b24aSTony Krowiak #include <asm/ap.h> 478f2abe6aSChristian Borntraeger #include "kvm-s390.h" 48b0c632dbSHeiko Carstens #include "gaccess.h" 49b0c632dbSHeiko Carstens 505786fffaSCornelia Huck #define CREATE_TRACE_POINTS 515786fffaSCornelia Huck #include "trace.h" 52ade38c31SCornelia Huck #include "trace-s390.h" 535786fffaSCornelia Huck 5441408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 55816c7667SJens Freimann #define LOCAL_IRQS 32 56816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 57816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 5841408c28SThomas Huth 59b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 60ccc40c53SChristian Borntraeger #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM 61b0c632dbSHeiko Carstens 62b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = { 63b0c632dbSHeiko Carstens { "userspace_handled", VCPU_STAT(exit_userspace) }, 640eaeafa1SChristian Borntraeger { "exit_null", VCPU_STAT(exit_null) }, 658f2abe6aSChristian Borntraeger { "exit_validity", VCPU_STAT(exit_validity) }, 668f2abe6aSChristian Borntraeger { "exit_stop_request", VCPU_STAT(exit_stop_request) }, 678f2abe6aSChristian Borntraeger { "exit_external_request", VCPU_STAT(exit_external_request) }, 68a5e0aceaSChristian Borntraeger { "exit_io_request", VCPU_STAT(exit_io_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) }, 788b905d28SChristian Borntraeger { "halt_no_poll_steal", VCPU_STAT(halt_no_poll_steal) }, 79ce2e4f0bSDavid Hildenbrand { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 80f5e10b09SChristian Borntraeger { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, 81ba5c1e9bSCarsten Otte { "instruction_lctl", VCPU_STAT(instruction_lctl) }, 82aba07508SDavid Hildenbrand { "instruction_stctl", VCPU_STAT(instruction_stctl) }, 83aba07508SDavid Hildenbrand { "instruction_stctg", VCPU_STAT(instruction_stctg) }, 84ccc40c53SChristian Borntraeger { "deliver_ckc", VCPU_STAT(deliver_ckc) }, 85ccc40c53SChristian Borntraeger { "deliver_cputm", VCPU_STAT(deliver_cputm) }, 86ba5c1e9bSCarsten Otte { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, 877697e71fSChristian Ehrhardt { "deliver_external_call", VCPU_STAT(deliver_external_call) }, 88ba5c1e9bSCarsten Otte { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, 89ccc40c53SChristian Borntraeger { "deliver_virtio", VCPU_STAT(deliver_virtio) }, 90ba5c1e9bSCarsten Otte { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, 91ba5c1e9bSCarsten Otte { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, 92ba5c1e9bSCarsten Otte { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, 93ccc40c53SChristian Borntraeger { "deliver_program", VCPU_STAT(deliver_program) }, 94ccc40c53SChristian Borntraeger { "deliver_io", VCPU_STAT(deliver_io) }, 9532de0749SQingFeng Hao { "deliver_machine_check", VCPU_STAT(deliver_machine_check) }, 96ba5c1e9bSCarsten Otte { "exit_wait_state", VCPU_STAT(exit_wait_state) }, 97ccc40c53SChristian Borntraeger { "inject_ckc", VCPU_STAT(inject_ckc) }, 98ccc40c53SChristian Borntraeger { "inject_cputm", VCPU_STAT(inject_cputm) }, 99ccc40c53SChristian Borntraeger { "inject_external_call", VCPU_STAT(inject_external_call) }, 100ccc40c53SChristian Borntraeger { "inject_float_mchk", VM_STAT(inject_float_mchk) }, 101ccc40c53SChristian Borntraeger { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) }, 102ccc40c53SChristian Borntraeger { "inject_io", VM_STAT(inject_io) }, 103ccc40c53SChristian Borntraeger { "inject_mchk", VCPU_STAT(inject_mchk) }, 104ccc40c53SChristian Borntraeger { "inject_pfault_done", VM_STAT(inject_pfault_done) }, 105ccc40c53SChristian Borntraeger { "inject_program", VCPU_STAT(inject_program) }, 106ccc40c53SChristian Borntraeger { "inject_restart", VCPU_STAT(inject_restart) }, 107ccc40c53SChristian Borntraeger { "inject_service_signal", VM_STAT(inject_service_signal) }, 108ccc40c53SChristian Borntraeger { "inject_set_prefix", VCPU_STAT(inject_set_prefix) }, 109ccc40c53SChristian Borntraeger { "inject_stop_signal", VCPU_STAT(inject_stop_signal) }, 110ccc40c53SChristian Borntraeger { "inject_pfault_init", VCPU_STAT(inject_pfault_init) }, 111ccc40c53SChristian Borntraeger { "inject_virtio", VM_STAT(inject_virtio) }, 112a37cb07aSChristian Borntraeger { "instruction_epsw", VCPU_STAT(instruction_epsw) }, 113a37cb07aSChristian Borntraeger { "instruction_gs", VCPU_STAT(instruction_gs) }, 114a37cb07aSChristian Borntraeger { "instruction_io_other", VCPU_STAT(instruction_io_other) }, 115a37cb07aSChristian Borntraeger { "instruction_lpsw", VCPU_STAT(instruction_lpsw) }, 116a37cb07aSChristian Borntraeger { "instruction_lpswe", VCPU_STAT(instruction_lpswe) }, 11769d0d3a3SChristian Borntraeger { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, 118a37cb07aSChristian Borntraeger { "instruction_ptff", VCPU_STAT(instruction_ptff) }, 119453423dcSChristian Borntraeger { "instruction_stidp", VCPU_STAT(instruction_stidp) }, 120a37cb07aSChristian Borntraeger { "instruction_sck", VCPU_STAT(instruction_sck) }, 121a37cb07aSChristian Borntraeger { "instruction_sckpf", VCPU_STAT(instruction_sckpf) }, 122453423dcSChristian Borntraeger { "instruction_spx", VCPU_STAT(instruction_spx) }, 123453423dcSChristian Borntraeger { "instruction_stpx", VCPU_STAT(instruction_stpx) }, 124453423dcSChristian Borntraeger { "instruction_stap", VCPU_STAT(instruction_stap) }, 125a37cb07aSChristian Borntraeger { "instruction_iske", VCPU_STAT(instruction_iske) }, 126a37cb07aSChristian Borntraeger { "instruction_ri", VCPU_STAT(instruction_ri) }, 127a37cb07aSChristian Borntraeger { "instruction_rrbe", VCPU_STAT(instruction_rrbe) }, 128a37cb07aSChristian Borntraeger { "instruction_sske", VCPU_STAT(instruction_sske) }, 1298a242234SHeiko Carstens { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, 130b31288faSKonstantin Weitz { "instruction_essa", VCPU_STAT(instruction_essa) }, 131453423dcSChristian Borntraeger { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 132453423dcSChristian Borntraeger { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 133a37cb07aSChristian Borntraeger { "instruction_tb", VCPU_STAT(instruction_tb) }, 134a37cb07aSChristian Borntraeger { "instruction_tpi", VCPU_STAT(instruction_tpi) }, 135bb25b9baSChristian Borntraeger { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 136a37cb07aSChristian Borntraeger { "instruction_tsch", VCPU_STAT(instruction_tsch) }, 13795ca2cb5SJanosch Frank { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, 138a3508fbeSDavid Hildenbrand { "instruction_sie", VCPU_STAT(instruction_sie) }, 1395288fbf0SChristian Borntraeger { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 140bd59d3a4SCornelia Huck { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 1417697e71fSChristian Ehrhardt { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 1425288fbf0SChristian Borntraeger { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, 14342cb0c9fSDavid Hildenbrand { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, 14442cb0c9fSDavid Hildenbrand { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, 1455288fbf0SChristian Borntraeger { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, 14642cb0c9fSDavid Hildenbrand { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, 14742cb0c9fSDavid Hildenbrand { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, 148cd7b4b61SEric Farman { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, 1495288fbf0SChristian Borntraeger { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, 1505288fbf0SChristian Borntraeger { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, 1515288fbf0SChristian Borntraeger { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, 15242cb0c9fSDavid Hildenbrand { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, 15342cb0c9fSDavid Hildenbrand { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, 15442cb0c9fSDavid Hildenbrand { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, 155866c138cSChristian Borntraeger { "instruction_diag_10", VCPU_STAT(diagnose_10) }, 156866c138cSChristian Borntraeger { "instruction_diag_44", VCPU_STAT(diagnose_44) }, 157866c138cSChristian Borntraeger { "instruction_diag_9c", VCPU_STAT(diagnose_9c) }, 158866c138cSChristian Borntraeger { "instruction_diag_258", VCPU_STAT(diagnose_258) }, 159866c138cSChristian Borntraeger { "instruction_diag_308", VCPU_STAT(diagnose_308) }, 160866c138cSChristian Borntraeger { "instruction_diag_500", VCPU_STAT(diagnose_500) }, 161a37cb07aSChristian Borntraeger { "instruction_diag_other", VCPU_STAT(diagnose_other) }, 162b0c632dbSHeiko Carstens { NULL } 163b0c632dbSHeiko Carstens }; 164b0c632dbSHeiko Carstens 1658fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext { 1668fa1696eSCollin L. Walling __u8 epoch_idx; 1678fa1696eSCollin L. Walling __u64 tod; 1688fa1696eSCollin L. Walling __u8 reserved[7]; 1698fa1696eSCollin L. Walling } __packed; 1708fa1696eSCollin L. Walling 171a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 172a411edf1SDavid Hildenbrand static int nested; 173a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 174a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 175a411edf1SDavid Hildenbrand 176a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 177a4499382SJanosch Frank static int hpage; 178a4499382SJanosch Frank module_param(hpage, int, 0444); 179a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 180b0c632dbSHeiko Carstens 1818b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */ 1828b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10; 1838b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644); 184b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); 1858b905d28SChristian Borntraeger 186c3b9e3e1SChristian Borntraeger /* 187c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 188c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 189c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 190c3b9e3e1SChristian Borntraeger */ 191c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 192c3b9e3e1SChristian Borntraeger 193c3b9e3e1SChristian Borntraeger /* 194c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 195c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 196c3b9e3e1SChristian Borntraeger */ 197c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 198c3b9e3e1SChristian Borntraeger /* 199c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 200c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 201c3b9e3e1SChristian Borntraeger */ 202c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 203c3b9e3e1SChristian Borntraeger 204c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 20578c4b59fSMichael Mueller { 206c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 207c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 208c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 209c3b9e3e1SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 210c3b9e3e1SChristian Borntraeger 211c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 21278c4b59fSMichael Mueller } 21378c4b59fSMichael Mueller 21415c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 21515c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2160a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2170a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 21815c9705fSDavid Hildenbrand 2199d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 220a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 22178f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2229d8d5786SMichael Mueller 223b0c632dbSHeiko Carstens /* Section: not file related */ 22413a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 225b0c632dbSHeiko Carstens { 226b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 22710474ae8SAlexander Graf return 0; 228b0c632dbSHeiko Carstens } 229b0c632dbSHeiko Carstens 230f257d6dcSSean Christopherson int kvm_arch_check_processor_compat(void) 231f257d6dcSSean Christopherson { 232f257d6dcSSean Christopherson return 0; 233f257d6dcSSean Christopherson } 234f257d6dcSSean Christopherson 235414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 236414d3b07SMartin Schwidefsky unsigned long end); 2372c70fe44SChristian Borntraeger 2381575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2391575767eSDavid Hildenbrand { 2401575767eSDavid Hildenbrand u8 delta_idx = 0; 2411575767eSDavid Hildenbrand 2421575767eSDavid Hildenbrand /* 2431575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2441575767eSDavid Hildenbrand * -delta to the epoch. 2451575767eSDavid Hildenbrand */ 2461575767eSDavid Hildenbrand delta = -delta; 2471575767eSDavid Hildenbrand 2481575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2491575767eSDavid Hildenbrand if ((s64)delta < 0) 2501575767eSDavid Hildenbrand delta_idx = -1; 2511575767eSDavid Hildenbrand 2521575767eSDavid Hildenbrand scb->epoch += delta; 2531575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2541575767eSDavid Hildenbrand scb->epdx += delta_idx; 2551575767eSDavid Hildenbrand if (scb->epoch < delta) 2561575767eSDavid Hildenbrand scb->epdx += 1; 2571575767eSDavid Hildenbrand } 2581575767eSDavid Hildenbrand } 2591575767eSDavid Hildenbrand 260fdf03650SFan Zhang /* 261fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 262fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 263fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 264fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 265fdf03650SFan Zhang */ 266fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 267fdf03650SFan Zhang void *v) 268fdf03650SFan Zhang { 269fdf03650SFan Zhang struct kvm *kvm; 270fdf03650SFan Zhang struct kvm_vcpu *vcpu; 271fdf03650SFan Zhang int i; 272fdf03650SFan Zhang unsigned long long *delta = v; 273fdf03650SFan Zhang 274fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 275fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 2761575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 2771575767eSDavid Hildenbrand if (i == 0) { 2781575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 2791575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 2801575767eSDavid Hildenbrand } 281db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 282db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 28391473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 2841575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 2851575767eSDavid Hildenbrand *delta); 286fdf03650SFan Zhang } 287fdf03650SFan Zhang } 288fdf03650SFan Zhang return NOTIFY_OK; 289fdf03650SFan Zhang } 290fdf03650SFan Zhang 291fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 292fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 293fdf03650SFan Zhang }; 294fdf03650SFan Zhang 295b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void) 296b0c632dbSHeiko Carstens { 2972c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 298b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 299a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 300a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 301fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 302fdf03650SFan Zhang &kvm_clock_notifier); 303b0c632dbSHeiko Carstens return 0; 304b0c632dbSHeiko Carstens } 305b0c632dbSHeiko Carstens 306b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 307b0c632dbSHeiko Carstens { 308b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 309a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 310fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 311fdf03650SFan Zhang &kvm_clock_notifier); 312b0c632dbSHeiko Carstens } 313b0c632dbSHeiko Carstens 31422be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 31522be5a13SDavid Hildenbrand { 31622be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 31722be5a13SDavid Hildenbrand } 31822be5a13SDavid Hildenbrand 3190a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3200a763c78SDavid Hildenbrand { 3210a763c78SDavid Hildenbrand register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; 322d051ae53SHeiko Carstens int cc; 3230a763c78SDavid Hildenbrand 3240a763c78SDavid Hildenbrand asm volatile( 3250a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3260a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3270a763c78SDavid Hildenbrand " ipm %0\n" 3280a763c78SDavid Hildenbrand " srl %0,28\n" 3290a763c78SDavid Hildenbrand : "=d" (cc) 3300a763c78SDavid Hildenbrand : "d" (r0) 3310a763c78SDavid Hildenbrand : "cc"); 3320a763c78SDavid Hildenbrand return cc == 0; 3330a763c78SDavid Hildenbrand } 3340a763c78SDavid Hildenbrand 335*d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 336d6681397SChristian Borntraeger { 337d6681397SChristian Borntraeger register unsigned long r0 asm("0") = 0; /* query function */ 338d6681397SChristian Borntraeger register unsigned long r1 asm("1") = (unsigned long) query; 339d6681397SChristian Borntraeger 340d6681397SChristian Borntraeger asm volatile( 341d6681397SChristian Borntraeger /* Parameter regs are ignored */ 342d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 343b1c41ac3SHeiko Carstens : 344d6681397SChristian Borntraeger : "d" (r0), "a" (r1), [opc] "i" (opcode) 345b1c41ac3SHeiko Carstens : "cc", "memory"); 346d6681397SChristian Borntraeger } 347d6681397SChristian Borntraeger 348173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3494f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 350173aec2dSChristian Borntraeger 35122be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 35222be5a13SDavid Hildenbrand { 3530a763c78SDavid Hildenbrand int i; 3540a763c78SDavid Hildenbrand 3550a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3560a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3570a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3580a763c78SDavid Hildenbrand } 3590a763c78SDavid Hildenbrand 3600a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 361221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 362221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 363221bb8a4SLinus Torvalds PTFF_QAF); 3640a763c78SDavid Hildenbrand 3650a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 36669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 36769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 36869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 36969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 37069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 37169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 37269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 37369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 37469c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 37569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 3760a763c78SDavid Hildenbrand } 3770a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 37869c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 37969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 3800a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 38169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 38269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 38369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 38469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 38569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 38669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 38769c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 38869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 3890a763c78SDavid Hildenbrand } 3900a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 391985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 39269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 3930a763c78SDavid Hildenbrand 394e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 395e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 396e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 397e000b8e0SJason J. Herne 39813209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 39913209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 40013209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 40113209ad0SChristian Borntraeger 402173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 403173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 404173aec2dSChristian Borntraeger 4054f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4064f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4074f45b90eSChristian Borntraeger 40822be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 40922be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 410a3508fbeSDavid Hildenbrand /* 411a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 412a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 413a3508fbeSDavid Hildenbrand */ 414a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 415a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 416a3508fbeSDavid Hildenbrand return; 417a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 41819c439b5SDavid Hildenbrand if (sclp.has_64bscao) 41919c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4200615a326SDavid Hildenbrand if (sclp.has_siif) 4210615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 42277d18f6dSDavid Hildenbrand if (sclp.has_gpere) 42377d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 424a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 425a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4265630a8e8SDavid Hildenbrand if (sclp.has_ib) 4275630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 42813ee3f67SDavid Hildenbrand if (sclp.has_cei) 42913ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4307fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4317fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 432730cd632SFarhan Ali if (sclp.has_kss) 433730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4345d3876a8SDavid Hildenbrand /* 4355d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4365d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4375d3876a8SDavid Hildenbrand * instead of the real storage key. 4385d3876a8SDavid Hildenbrand * 4395d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4405d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4415d3876a8SDavid Hildenbrand * 4425d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4435d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4445d3876a8SDavid Hildenbrand * 4455d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4465d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4475d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4485d3876a8SDavid Hildenbrand * 4495d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4505d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4515d3876a8SDavid Hildenbrand */ 45222be5a13SDavid Hildenbrand } 45322be5a13SDavid Hildenbrand 454b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 455b0c632dbSHeiko Carstens { 456308c3e66SMichael Mueller int rc; 457308c3e66SMichael Mueller 45878f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 45978f26131SChristian Borntraeger if (!kvm_s390_dbf) 46078f26131SChristian Borntraeger return -ENOMEM; 46178f26131SChristian Borntraeger 46278f26131SChristian Borntraeger if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { 463308c3e66SMichael Mueller rc = -ENOMEM; 464308c3e66SMichael Mueller goto out_debug_unreg; 46578f26131SChristian Borntraeger } 46678f26131SChristian Borntraeger 46722be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 46822be5a13SDavid Hildenbrand 46984877d93SCornelia Huck /* Register floating interrupt controller interface. */ 470308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 471308c3e66SMichael Mueller if (rc) { 4728d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 473308c3e66SMichael Mueller goto out_debug_unreg; 474308c3e66SMichael Mueller } 475b1d1e76eSMichael Mueller 476b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 477b1d1e76eSMichael Mueller if (rc) 478b1d1e76eSMichael Mueller goto out_gib_destroy; 479b1d1e76eSMichael Mueller 480308c3e66SMichael Mueller return 0; 481308c3e66SMichael Mueller 482b1d1e76eSMichael Mueller out_gib_destroy: 483b1d1e76eSMichael Mueller kvm_s390_gib_destroy(); 484308c3e66SMichael Mueller out_debug_unreg: 485308c3e66SMichael Mueller debug_unregister(kvm_s390_dbf); 486308c3e66SMichael Mueller return rc; 487b0c632dbSHeiko Carstens } 488b0c632dbSHeiko Carstens 48978f26131SChristian Borntraeger void kvm_arch_exit(void) 49078f26131SChristian Borntraeger { 4911282c21eSMichael Mueller kvm_s390_gib_destroy(); 49278f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 49378f26131SChristian Borntraeger } 49478f26131SChristian Borntraeger 495b0c632dbSHeiko Carstens /* Section: device related */ 496b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 497b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 498b0c632dbSHeiko Carstens { 499b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 500b0c632dbSHeiko Carstens return s390_enable_sie(); 501b0c632dbSHeiko Carstens return -EINVAL; 502b0c632dbSHeiko Carstens } 503b0c632dbSHeiko Carstens 504784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 505b0c632dbSHeiko Carstens { 506d7b0b5ebSCarsten Otte int r; 507d7b0b5ebSCarsten Otte 5082bd0ac4eSCarsten Otte switch (ext) { 509d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 510b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 51152e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5121efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5131efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5141efd0f59SCarsten Otte #endif 5153c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 51660b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 51714eebd91SCarsten Otte case KVM_CAP_ONE_REG: 518d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 519fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 52010ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 521c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 52278599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 523f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5246352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 525460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 52647b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5272444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 528e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 52930ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 530816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5316502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5324036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 53347a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 534da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 535d7b0b5ebSCarsten Otte r = 1; 536d7b0b5ebSCarsten Otte break; 537a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 538a4499382SJanosch Frank r = 0; 53940ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 540a4499382SJanosch Frank r = 1; 541a4499382SJanosch Frank break; 54241408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 54341408c28SThomas Huth r = MEM_OP_MAX_SIZE; 54441408c28SThomas Huth break; 545e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 546e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 547a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 54876a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 549a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 550a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 551a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 55276a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 553e726b1bdSChristian Borntraeger break; 5541526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 555abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 5561526bf9cSChristian Borntraeger break; 55768c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 55868c55750SEric Farman r = MACHINE_HAS_VX; 55968c55750SEric Farman break; 560c6e5f166SFan Zhang case KVM_CAP_S390_RI: 561c6e5f166SFan Zhang r = test_facility(64); 562c6e5f166SFan Zhang break; 5634e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 5644e0b1ab7SFan Zhang r = test_facility(133); 5654e0b1ab7SFan Zhang break; 56635b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 56735b3fde6SChristian Borntraeger r = test_facility(82); 56835b3fde6SChristian Borntraeger break; 5692bd0ac4eSCarsten Otte default: 570d7b0b5ebSCarsten Otte r = 0; 571b0c632dbSHeiko Carstens } 572d7b0b5ebSCarsten Otte return r; 5732bd0ac4eSCarsten Otte } 574b0c632dbSHeiko Carstens 57515f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm, 57615f36ebdSJason J. Herne struct kvm_memory_slot *memslot) 57715f36ebdSJason J. Herne { 5780959e168SJanosch Frank int i; 57915f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 5800959e168SJanosch Frank unsigned long gaddr, vmaddr; 58115f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 5820959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 58315f36ebdSJason J. Herne 5840959e168SJanosch Frank /* Loop over all guest segments */ 5850959e168SJanosch Frank cur_gfn = memslot->base_gfn; 58615f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 5870959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 5880959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 5890959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 5900959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 5910959e168SJanosch Frank continue; 59215f36ebdSJason J. Herne 5930959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 5940959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 5950959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 5960959e168SJanosch Frank if (test_bit(i, bitmap)) 5970959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 5980959e168SJanosch Frank } 5990959e168SJanosch Frank 6001763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6011763f8d0SChristian Borntraeger return; 60270c88a00SChristian Borntraeger cond_resched(); 60315f36ebdSJason J. Herne } 60415f36ebdSJason J. Herne } 60515f36ebdSJason J. Herne 606b0c632dbSHeiko Carstens /* Section: vm related */ 607a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 608a6e2f683SEugene (jno) Dvurechenski 609b0c632dbSHeiko Carstens /* 610b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 611b0c632dbSHeiko Carstens */ 612b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 613b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 614b0c632dbSHeiko Carstens { 61515f36ebdSJason J. Herne int r; 61615f36ebdSJason J. Herne unsigned long n; 6179f6b8029SPaolo Bonzini struct kvm_memslots *slots; 61815f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 61915f36ebdSJason J. Herne int is_dirty = 0; 62015f36ebdSJason J. Herne 621e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 622e1e8a962SJanosch Frank return -EINVAL; 623e1e8a962SJanosch Frank 62415f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 62515f36ebdSJason J. Herne 62615f36ebdSJason J. Herne r = -EINVAL; 62715f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 62815f36ebdSJason J. Herne goto out; 62915f36ebdSJason J. Herne 6309f6b8029SPaolo Bonzini slots = kvm_memslots(kvm); 6319f6b8029SPaolo Bonzini memslot = id_to_memslot(slots, log->slot); 63215f36ebdSJason J. Herne r = -ENOENT; 63315f36ebdSJason J. Herne if (!memslot->dirty_bitmap) 63415f36ebdSJason J. Herne goto out; 63515f36ebdSJason J. Herne 63615f36ebdSJason J. Herne kvm_s390_sync_dirty_log(kvm, memslot); 63715f36ebdSJason J. Herne r = kvm_get_dirty_log(kvm, log, &is_dirty); 63815f36ebdSJason J. Herne if (r) 63915f36ebdSJason J. Herne goto out; 64015f36ebdSJason J. Herne 64115f36ebdSJason J. Herne /* Clear the dirty log */ 64215f36ebdSJason J. Herne if (is_dirty) { 64315f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 64415f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 64515f36ebdSJason J. Herne } 64615f36ebdSJason J. Herne r = 0; 64715f36ebdSJason J. Herne out: 64815f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 64915f36ebdSJason J. Herne return r; 650b0c632dbSHeiko Carstens } 651b0c632dbSHeiko Carstens 6526502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 6536502a34cSDavid Hildenbrand { 6546502a34cSDavid Hildenbrand unsigned int i; 6556502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 6566502a34cSDavid Hildenbrand 6576502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 6586502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 6596502a34cSDavid Hildenbrand } 6606502a34cSDavid Hildenbrand } 6616502a34cSDavid Hildenbrand 662e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 663d938dc55SCornelia Huck { 664d938dc55SCornelia Huck int r; 665d938dc55SCornelia Huck 666d938dc55SCornelia Huck if (cap->flags) 667d938dc55SCornelia Huck return -EINVAL; 668d938dc55SCornelia Huck 669d938dc55SCornelia Huck switch (cap->cap) { 67084223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 671c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 67284223598SCornelia Huck kvm->arch.use_irqchip = 1; 67384223598SCornelia Huck r = 0; 67484223598SCornelia Huck break; 6752444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 676c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 6772444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 6782444b352SDavid Hildenbrand r = 0; 6792444b352SDavid Hildenbrand break; 68068c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 6815967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 682a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 6835967c17bSDavid Hildenbrand r = -EBUSY; 6845967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 685c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 686c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 6872f87d942SGuenther Hutzl if (test_facility(134)) { 6882f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 6892f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 6902f87d942SGuenther Hutzl } 69153743aa7SMaxim Samoylov if (test_facility(135)) { 69253743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 69353743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 69453743aa7SMaxim Samoylov } 6957832e91cSChristian Borntraeger if (test_facility(148)) { 6967832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 6977832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 6987832e91cSChristian Borntraeger } 699d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 700d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 701d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 702d5cb6ab1SChristian Borntraeger } 70318280d8bSMichael Mueller r = 0; 70418280d8bSMichael Mueller } else 70518280d8bSMichael Mueller r = -EINVAL; 7065967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 707c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 708c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 70968c55750SEric Farman break; 710c6e5f166SFan Zhang case KVM_CAP_S390_RI: 711c6e5f166SFan Zhang r = -EINVAL; 712c6e5f166SFan Zhang mutex_lock(&kvm->lock); 713a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 714c6e5f166SFan Zhang r = -EBUSY; 715c6e5f166SFan Zhang } else if (test_facility(64)) { 716c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 717c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 718c6e5f166SFan Zhang r = 0; 719c6e5f166SFan Zhang } 720c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 721c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 722c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 723c6e5f166SFan Zhang break; 72447a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 72547a4693eSYi Min Zhao mutex_lock(&kvm->lock); 72647a4693eSYi Min Zhao if (kvm->created_vcpus) { 72747a4693eSYi Min Zhao r = -EBUSY; 72847a4693eSYi Min Zhao } else { 72947a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 73047a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 73147a4693eSYi Min Zhao r = 0; 73247a4693eSYi Min Zhao } 73347a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 73447a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 73547a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 73647a4693eSYi Min Zhao break; 7374e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 7384e0b1ab7SFan Zhang r = -EINVAL; 7394e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 740241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 7414e0b1ab7SFan Zhang r = -EBUSY; 7424e0b1ab7SFan Zhang } else if (test_facility(133)) { 7434e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 7444e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 7454e0b1ab7SFan Zhang r = 0; 7464e0b1ab7SFan Zhang } 7474e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 7484e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 7494e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 7504e0b1ab7SFan Zhang break; 751a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 752a4499382SJanosch Frank mutex_lock(&kvm->lock); 753a4499382SJanosch Frank if (kvm->created_vcpus) 754a4499382SJanosch Frank r = -EBUSY; 75540ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 756a4499382SJanosch Frank r = -EINVAL; 757a4499382SJanosch Frank else { 758a4499382SJanosch Frank r = 0; 759df88f318SJanosch Frank down_write(&kvm->mm->mmap_sem); 760a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 761df88f318SJanosch Frank up_write(&kvm->mm->mmap_sem); 762a4499382SJanosch Frank /* 763a4499382SJanosch Frank * We might have to create fake 4k page 764a4499382SJanosch Frank * tables. To avoid that the hardware works on 765a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 766a4499382SJanosch Frank */ 767a4499382SJanosch Frank kvm->arch.use_skf = 0; 768a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 769a4499382SJanosch Frank } 770a4499382SJanosch Frank mutex_unlock(&kvm->lock); 771a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 772a4499382SJanosch Frank r ? "(not available)" : "(success)"); 773a4499382SJanosch Frank break; 774e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 775c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 776e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 777e44fc8c9SEkaterina Tumanova r = 0; 778e44fc8c9SEkaterina Tumanova break; 7796502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 7806502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 7816502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 7826502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 7836502a34cSDavid Hildenbrand r = 0; 7846502a34cSDavid Hildenbrand break; 785d938dc55SCornelia Huck default: 786d938dc55SCornelia Huck r = -EINVAL; 787d938dc55SCornelia Huck break; 788d938dc55SCornelia Huck } 789d938dc55SCornelia Huck return r; 790d938dc55SCornelia Huck } 791d938dc55SCornelia Huck 7928c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 7938c0a7ce6SDominik Dingel { 7948c0a7ce6SDominik Dingel int ret; 7958c0a7ce6SDominik Dingel 7968c0a7ce6SDominik Dingel switch (attr->attr) { 7978c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 7988c0a7ce6SDominik Dingel ret = 0; 799c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 800a3a92c31SDominik Dingel kvm->arch.mem_limit); 801a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8028c0a7ce6SDominik Dingel ret = -EFAULT; 8038c0a7ce6SDominik Dingel break; 8048c0a7ce6SDominik Dingel default: 8058c0a7ce6SDominik Dingel ret = -ENXIO; 8068c0a7ce6SDominik Dingel break; 8078c0a7ce6SDominik Dingel } 8088c0a7ce6SDominik Dingel return ret; 8098c0a7ce6SDominik Dingel } 8108c0a7ce6SDominik Dingel 8118c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8124f718eabSDominik Dingel { 8134f718eabSDominik Dingel int ret; 8144f718eabSDominik Dingel unsigned int idx; 8154f718eabSDominik Dingel switch (attr->attr) { 8164f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 817f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 818c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 819e6db1d61SDominik Dingel break; 820e6db1d61SDominik Dingel 821c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 8224f718eabSDominik Dingel mutex_lock(&kvm->lock); 823a4499382SJanosch Frank if (kvm->created_vcpus) 824a4499382SJanosch Frank ret = -EBUSY; 825a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 826a4499382SJanosch Frank ret = -EINVAL; 827a4499382SJanosch Frank else { 8284f718eabSDominik Dingel kvm->arch.use_cmma = 1; 829c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 830c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 8314f718eabSDominik Dingel ret = 0; 8324f718eabSDominik Dingel } 8334f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8344f718eabSDominik Dingel break; 8354f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 836f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 837f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 838f9cbd9b0SDavid Hildenbrand break; 839c3489155SDominik Dingel ret = -EINVAL; 840c3489155SDominik Dingel if (!kvm->arch.use_cmma) 841c3489155SDominik Dingel break; 842c3489155SDominik Dingel 843c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 8444f718eabSDominik Dingel mutex_lock(&kvm->lock); 8454f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 846a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 8474f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 8484f718eabSDominik Dingel mutex_unlock(&kvm->lock); 8494f718eabSDominik Dingel ret = 0; 8504f718eabSDominik Dingel break; 8518c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 8528c0a7ce6SDominik Dingel unsigned long new_limit; 8538c0a7ce6SDominik Dingel 8548c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 8558c0a7ce6SDominik Dingel return -EINVAL; 8568c0a7ce6SDominik Dingel 8578c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 8588c0a7ce6SDominik Dingel return -EFAULT; 8598c0a7ce6SDominik Dingel 860a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 861a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 8628c0a7ce6SDominik Dingel return -E2BIG; 8638c0a7ce6SDominik Dingel 864a3a92c31SDominik Dingel if (!new_limit) 865a3a92c31SDominik Dingel return -EINVAL; 866a3a92c31SDominik Dingel 8676ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 868a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 869a3a92c31SDominik Dingel new_limit -= 1; 870a3a92c31SDominik Dingel 8718c0a7ce6SDominik Dingel ret = -EBUSY; 8728c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 873a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 8746ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 8756ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 8768c0a7ce6SDominik Dingel 8778c0a7ce6SDominik Dingel if (!new) { 8788c0a7ce6SDominik Dingel ret = -ENOMEM; 8798c0a7ce6SDominik Dingel } else { 8806ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 8818c0a7ce6SDominik Dingel new->private = kvm; 8828c0a7ce6SDominik Dingel kvm->arch.gmap = new; 8838c0a7ce6SDominik Dingel ret = 0; 8848c0a7ce6SDominik Dingel } 8858c0a7ce6SDominik Dingel } 8868c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 887a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 888a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 889a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 8908c0a7ce6SDominik Dingel break; 8918c0a7ce6SDominik Dingel } 8924f718eabSDominik Dingel default: 8934f718eabSDominik Dingel ret = -ENXIO; 8944f718eabSDominik Dingel break; 8954f718eabSDominik Dingel } 8964f718eabSDominik Dingel return ret; 8974f718eabSDominik Dingel } 8984f718eabSDominik Dingel 899a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 900a374e892STony Krowiak 90120c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 902a374e892STony Krowiak { 903a374e892STony Krowiak struct kvm_vcpu *vcpu; 904a374e892STony Krowiak int i; 905a374e892STony Krowiak 90620c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 90720c922f0STony Krowiak 9083194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 90920c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 9103194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 9113194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 9123194cdb7SDavid Hildenbrand } 91320c922f0STony Krowiak 91420c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 91520c922f0STony Krowiak } 91620c922f0STony Krowiak 91720c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 91820c922f0STony Krowiak { 919a374e892STony Krowiak mutex_lock(&kvm->lock); 920a374e892STony Krowiak switch (attr->attr) { 921a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 9228e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9238e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 92437940fb0STony Krowiak return -EINVAL; 9258e41bd54SChristian Borntraeger } 926a374e892STony Krowiak get_random_bytes( 927a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 928a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 929a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 930c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 931a374e892STony Krowiak break; 932a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 9338e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9348e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 93537940fb0STony Krowiak return -EINVAL; 9368e41bd54SChristian Borntraeger } 937a374e892STony Krowiak get_random_bytes( 938a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 939a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 940a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 941c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 942a374e892STony Krowiak break; 943a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 9448e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9458e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 94637940fb0STony Krowiak return -EINVAL; 9478e41bd54SChristian Borntraeger } 948a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 949a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 950a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 951c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 952a374e892STony Krowiak break; 953a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 9548e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 9558e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 95637940fb0STony Krowiak return -EINVAL; 9578e41bd54SChristian Borntraeger } 958a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 959a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 960a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 961c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 962a374e892STony Krowiak break; 96337940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 96437940fb0STony Krowiak if (!ap_instructions_available()) { 96537940fb0STony Krowiak mutex_unlock(&kvm->lock); 96637940fb0STony Krowiak return -EOPNOTSUPP; 96737940fb0STony Krowiak } 96837940fb0STony Krowiak kvm->arch.crypto.apie = 1; 96937940fb0STony Krowiak break; 97037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 97137940fb0STony Krowiak if (!ap_instructions_available()) { 97237940fb0STony Krowiak mutex_unlock(&kvm->lock); 97337940fb0STony Krowiak return -EOPNOTSUPP; 97437940fb0STony Krowiak } 97537940fb0STony Krowiak kvm->arch.crypto.apie = 0; 97637940fb0STony Krowiak break; 977a374e892STony Krowiak default: 978a374e892STony Krowiak mutex_unlock(&kvm->lock); 979a374e892STony Krowiak return -ENXIO; 980a374e892STony Krowiak } 981a374e892STony Krowiak 98220c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 983a374e892STony Krowiak mutex_unlock(&kvm->lock); 984a374e892STony Krowiak return 0; 985a374e892STony Krowiak } 986a374e892STony Krowiak 987190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 988190df4a2SClaudio Imbrenda { 989190df4a2SClaudio Imbrenda int cx; 990190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 991190df4a2SClaudio Imbrenda 992190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 993190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 994190df4a2SClaudio Imbrenda } 995190df4a2SClaudio Imbrenda 996190df4a2SClaudio Imbrenda /* 997190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 9981de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 999190df4a2SClaudio Imbrenda */ 1000190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1001190df4a2SClaudio Imbrenda { 1002190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1003190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1004afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1005190df4a2SClaudio Imbrenda int slotnr; 1006190df4a2SClaudio Imbrenda 1007190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1008afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1009190df4a2SClaudio Imbrenda return 0; 1010190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1011190df4a2SClaudio Imbrenda if (!slots || !slots->used_slots) 1012190df4a2SClaudio Imbrenda return -EINVAL; 1013190df4a2SClaudio Imbrenda 1014afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1015afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1016afdad616SClaudio Imbrenda return 0; 1017190df4a2SClaudio Imbrenda } 1018190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1019190df4a2SClaudio Imbrenda for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { 1020190df4a2SClaudio Imbrenda ms = slots->memslots + slotnr; 102113a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 102213a17cc0SIgor Mammedov return -EINVAL; 1023afdad616SClaudio Imbrenda /* 1024afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1025afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1026afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1027afdad616SClaudio Imbrenda * attributes. 1028afdad616SClaudio Imbrenda */ 1029afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1030afdad616SClaudio Imbrenda ram_pages += ms->npages; 1031190df4a2SClaudio Imbrenda } 1032afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1033afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1034190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1035190df4a2SClaudio Imbrenda return 0; 1036190df4a2SClaudio Imbrenda } 1037190df4a2SClaudio Imbrenda 1038190df4a2SClaudio Imbrenda /* 10391de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1040190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1041190df4a2SClaudio Imbrenda */ 1042190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1043190df4a2SClaudio Imbrenda { 1044190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1045afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1046190df4a2SClaudio Imbrenda return 0; 1047afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1048afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1049190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1050190df4a2SClaudio Imbrenda return 0; 1051190df4a2SClaudio Imbrenda } 1052190df4a2SClaudio Imbrenda 1053190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1054190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1055190df4a2SClaudio Imbrenda { 10561de1ea7eSChristian Borntraeger int res = -ENXIO; 1057190df4a2SClaudio Imbrenda 10581de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1059190df4a2SClaudio Imbrenda switch (attr->attr) { 1060190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1061190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1062190df4a2SClaudio Imbrenda break; 1063190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1064190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1065190df4a2SClaudio Imbrenda break; 1066190df4a2SClaudio Imbrenda default: 1067190df4a2SClaudio Imbrenda break; 1068190df4a2SClaudio Imbrenda } 10691de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1070190df4a2SClaudio Imbrenda 1071190df4a2SClaudio Imbrenda return res; 1072190df4a2SClaudio Imbrenda } 1073190df4a2SClaudio Imbrenda 1074190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1075190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1076190df4a2SClaudio Imbrenda { 1077afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1078190df4a2SClaudio Imbrenda 1079190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1080190df4a2SClaudio Imbrenda return -ENXIO; 1081190df4a2SClaudio Imbrenda 1082190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1083190df4a2SClaudio Imbrenda return -EFAULT; 1084190df4a2SClaudio Imbrenda return 0; 1085190df4a2SClaudio Imbrenda } 1086190df4a2SClaudio Imbrenda 10878fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 10888fa1696eSCollin L. Walling { 10898fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 10908fa1696eSCollin L. Walling 10918fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 10928fa1696eSCollin L. Walling return -EFAULT; 10938fa1696eSCollin L. Walling 10940e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 10958fa1696eSCollin L. Walling return -EINVAL; 10960e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 10978fa1696eSCollin L. Walling 10988fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 10998fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11008fa1696eSCollin L. Walling 11018fa1696eSCollin L. Walling return 0; 11028fa1696eSCollin L. Walling } 11038fa1696eSCollin L. Walling 110472f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 110572f25020SJason J. Herne { 110672f25020SJason J. Herne u8 gtod_high; 110772f25020SJason J. Herne 110872f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 110972f25020SJason J. Herne sizeof(gtod_high))) 111072f25020SJason J. Herne return -EFAULT; 111172f25020SJason J. Herne 111272f25020SJason J. Herne if (gtod_high != 0) 111372f25020SJason J. Herne return -EINVAL; 111458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 111572f25020SJason J. Herne 111672f25020SJason J. Herne return 0; 111772f25020SJason J. Herne } 111872f25020SJason J. Herne 111972f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 112072f25020SJason J. Herne { 11210e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 112272f25020SJason J. Herne 11230e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 11240e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 112572f25020SJason J. Herne return -EFAULT; 112672f25020SJason J. Herne 11270e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 11280e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 112972f25020SJason J. Herne return 0; 113072f25020SJason J. Herne } 113172f25020SJason J. Herne 113272f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 113372f25020SJason J. Herne { 113472f25020SJason J. Herne int ret; 113572f25020SJason J. Herne 113672f25020SJason J. Herne if (attr->flags) 113772f25020SJason J. Herne return -EINVAL; 113872f25020SJason J. Herne 113972f25020SJason J. Herne switch (attr->attr) { 11408fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 11418fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 11428fa1696eSCollin L. Walling break; 114372f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 114472f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 114572f25020SJason J. Herne break; 114672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 114772f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 114872f25020SJason J. Herne break; 114972f25020SJason J. Herne default: 115072f25020SJason J. Herne ret = -ENXIO; 115172f25020SJason J. Herne break; 115272f25020SJason J. Herne } 115372f25020SJason J. Herne return ret; 115472f25020SJason J. Herne } 115572f25020SJason J. Herne 115633d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 11578fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 11588fa1696eSCollin L. Walling { 11598fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 11608fa1696eSCollin L. Walling 11618fa1696eSCollin L. Walling preempt_disable(); 11628fa1696eSCollin L. Walling 11638fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 11648fa1696eSCollin L. Walling 11658fa1696eSCollin L. Walling gtod->tod = htod.tod + kvm->arch.epoch; 116633d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 116733d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 11688fa1696eSCollin L. Walling gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx; 11698fa1696eSCollin L. Walling if (gtod->tod < htod.tod) 11708fa1696eSCollin L. Walling gtod->epoch_idx += 1; 117133d1b272SDavid Hildenbrand } 11728fa1696eSCollin L. Walling 11738fa1696eSCollin L. Walling preempt_enable(); 11748fa1696eSCollin L. Walling } 11758fa1696eSCollin L. Walling 11768fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 11778fa1696eSCollin L. Walling { 11788fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 11798fa1696eSCollin L. Walling 11808fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 118133d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 11828fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 11838fa1696eSCollin L. Walling return -EFAULT; 11848fa1696eSCollin L. Walling 11858fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 11868fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 11878fa1696eSCollin L. Walling return 0; 11888fa1696eSCollin L. Walling } 11898fa1696eSCollin L. Walling 119072f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 119172f25020SJason J. Herne { 119272f25020SJason J. Herne u8 gtod_high = 0; 119372f25020SJason J. Herne 119472f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 119572f25020SJason J. Herne sizeof(gtod_high))) 119672f25020SJason J. Herne return -EFAULT; 119758c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 119872f25020SJason J. Herne 119972f25020SJason J. Herne return 0; 120072f25020SJason J. Herne } 120172f25020SJason J. Herne 120272f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 120372f25020SJason J. Herne { 12045a3d883aSDavid Hildenbrand u64 gtod; 120572f25020SJason J. Herne 120660417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 120772f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 120872f25020SJason J. Herne return -EFAULT; 120958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 121072f25020SJason J. Herne 121172f25020SJason J. Herne return 0; 121272f25020SJason J. Herne } 121372f25020SJason J. Herne 121472f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 121572f25020SJason J. Herne { 121672f25020SJason J. Herne int ret; 121772f25020SJason J. Herne 121872f25020SJason J. Herne if (attr->flags) 121972f25020SJason J. Herne return -EINVAL; 122072f25020SJason J. Herne 122172f25020SJason J. Herne switch (attr->attr) { 12228fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12238fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 12248fa1696eSCollin L. Walling break; 122572f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 122672f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 122772f25020SJason J. Herne break; 122872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 122972f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 123072f25020SJason J. Herne break; 123172f25020SJason J. Herne default: 123272f25020SJason J. Herne ret = -ENXIO; 123372f25020SJason J. Herne break; 123472f25020SJason J. Herne } 123572f25020SJason J. Herne return ret; 123672f25020SJason J. Herne } 123772f25020SJason J. Herne 1238658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1239658b6edaSMichael Mueller { 1240658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1241053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1242658b6edaSMichael Mueller int ret = 0; 1243658b6edaSMichael Mueller 1244658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1245a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1246658b6edaSMichael Mueller ret = -EBUSY; 1247658b6edaSMichael Mueller goto out; 1248658b6edaSMichael Mueller } 1249658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1250658b6edaSMichael Mueller if (!proc) { 1251658b6edaSMichael Mueller ret = -ENOMEM; 1252658b6edaSMichael Mueller goto out; 1253658b6edaSMichael Mueller } 1254658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1255658b6edaSMichael Mueller sizeof(*proc))) { 12569bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1257053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1258053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 12590487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1260053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1261053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1262053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1263053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1264053dd230SDavid Hildenbrand else 1265658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1266053dd230SDavid Hildenbrand } 1267c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1268658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1269a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1270a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1271a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1272a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1273a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1274a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1275a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1276658b6edaSMichael Mueller } else 1277658b6edaSMichael Mueller ret = -EFAULT; 1278658b6edaSMichael Mueller kfree(proc); 1279658b6edaSMichael Mueller out: 1280658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1281658b6edaSMichael Mueller return ret; 1282658b6edaSMichael Mueller } 1283658b6edaSMichael Mueller 128415c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 128515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 128615c9705fSDavid Hildenbrand { 128715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 128815c9705fSDavid Hildenbrand 128915c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 129015c9705fSDavid Hildenbrand return -EFAULT; 129115c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 129215c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 129315c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 129415c9705fSDavid Hildenbrand return -EINVAL; 129515c9705fSDavid Hildenbrand 129615c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 12972f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 12982f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 12992f8311c9SChristian Borntraeger return -EBUSY; 13002f8311c9SChristian Borntraeger } 130115c9705fSDavid Hildenbrand bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, 130215c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 130315c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 13042f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 13052f8311c9SChristian Borntraeger data.feat[0], 13062f8311c9SChristian Borntraeger data.feat[1], 13072f8311c9SChristian Borntraeger data.feat[2]); 13082f8311c9SChristian Borntraeger return 0; 130915c9705fSDavid Hildenbrand } 131015c9705fSDavid Hildenbrand 13110a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 13120a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 13130a763c78SDavid Hildenbrand { 1314346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1315346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1316346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1317346fa2f8SChristian Borntraeger return -EBUSY; 1318346fa2f8SChristian Borntraeger } 1319346fa2f8SChristian Borntraeger 1320346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1321346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1322346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1323346fa2f8SChristian Borntraeger return -EFAULT; 1324346fa2f8SChristian Borntraeger } 1325346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1326346fa2f8SChristian Borntraeger 132711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 132811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 132911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 133011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 133111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 133211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 133311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 133411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 133511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 133611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 133711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 133811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 133911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 134011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 134111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 134211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 134311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 134411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 134511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 134611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 134711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 134811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 134911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 135011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 135111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 135211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 135311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 135411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 135511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 135611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 135711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 135811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 135911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 136011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 136111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 136211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 136311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 136411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 136511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 136611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 136711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 136811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 136911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 137011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 137113209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 137213209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 137313209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1374173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1375173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1376173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1377173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1378173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 13794f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 13804f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 13814f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 13824f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 13834f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 138411ba5961SChristian Borntraeger 1385346fa2f8SChristian Borntraeger return 0; 13860a763c78SDavid Hildenbrand } 13870a763c78SDavid Hildenbrand 1388658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1389658b6edaSMichael Mueller { 1390658b6edaSMichael Mueller int ret = -ENXIO; 1391658b6edaSMichael Mueller 1392658b6edaSMichael Mueller switch (attr->attr) { 1393658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1394658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1395658b6edaSMichael Mueller break; 139615c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 139715c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 139815c9705fSDavid Hildenbrand break; 13990a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 14000a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 14010a763c78SDavid Hildenbrand break; 1402658b6edaSMichael Mueller } 1403658b6edaSMichael Mueller return ret; 1404658b6edaSMichael Mueller } 1405658b6edaSMichael Mueller 1406658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1407658b6edaSMichael Mueller { 1408658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1409658b6edaSMichael Mueller int ret = 0; 1410658b6edaSMichael Mueller 1411658b6edaSMichael Mueller proc = kzalloc(sizeof(*proc), GFP_KERNEL); 1412658b6edaSMichael Mueller if (!proc) { 1413658b6edaSMichael Mueller ret = -ENOMEM; 1414658b6edaSMichael Mueller goto out; 1415658b6edaSMichael Mueller } 14169bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1417658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1418c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1419c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1420a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1421a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1422a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1423a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1424a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1425a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1426a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1427658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1428658b6edaSMichael Mueller ret = -EFAULT; 1429658b6edaSMichael Mueller kfree(proc); 1430658b6edaSMichael Mueller out: 1431658b6edaSMichael Mueller return ret; 1432658b6edaSMichael Mueller } 1433658b6edaSMichael Mueller 1434658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1435658b6edaSMichael Mueller { 1436658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1437658b6edaSMichael Mueller int ret = 0; 1438658b6edaSMichael Mueller 1439658b6edaSMichael Mueller mach = kzalloc(sizeof(*mach), GFP_KERNEL); 1440658b6edaSMichael Mueller if (!mach) { 1441658b6edaSMichael Mueller ret = -ENOMEM; 1442658b6edaSMichael Mueller goto out; 1443658b6edaSMichael Mueller } 1444658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 144537c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1446c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1447981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1448658b6edaSMichael Mueller memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, 144904478197SChristian Borntraeger sizeof(S390_lowcore.stfle_fac_list)); 1450a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1451a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1452a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1453a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1454a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1455a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1456a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1457a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1458a8c39dd7SChristian Borntraeger mach->fac_list[0], 1459a8c39dd7SChristian Borntraeger mach->fac_list[1], 1460a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1461658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1462658b6edaSMichael Mueller ret = -EFAULT; 1463658b6edaSMichael Mueller kfree(mach); 1464658b6edaSMichael Mueller out: 1465658b6edaSMichael Mueller return ret; 1466658b6edaSMichael Mueller } 1467658b6edaSMichael Mueller 146815c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 146915c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 147015c9705fSDavid Hildenbrand { 147115c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 147215c9705fSDavid Hildenbrand 147315c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, 147415c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 147515c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 147615c9705fSDavid Hildenbrand return -EFAULT; 14772f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14782f8311c9SChristian Borntraeger data.feat[0], 14792f8311c9SChristian Borntraeger data.feat[1], 14802f8311c9SChristian Borntraeger data.feat[2]); 148115c9705fSDavid Hildenbrand return 0; 148215c9705fSDavid Hildenbrand } 148315c9705fSDavid Hildenbrand 148415c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 148515c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 148615c9705fSDavid Hildenbrand { 148715c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 148815c9705fSDavid Hildenbrand 148915c9705fSDavid Hildenbrand bitmap_copy((unsigned long *) data.feat, 149015c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 149115c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS); 149215c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 149315c9705fSDavid Hildenbrand return -EFAULT; 14942f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14952f8311c9SChristian Borntraeger data.feat[0], 14962f8311c9SChristian Borntraeger data.feat[1], 14972f8311c9SChristian Borntraeger data.feat[2]); 149815c9705fSDavid Hildenbrand return 0; 149915c9705fSDavid Hildenbrand } 150015c9705fSDavid Hildenbrand 15010a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 15020a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15030a763c78SDavid Hildenbrand { 1504346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1505346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1506346fa2f8SChristian Borntraeger return -EFAULT; 1507346fa2f8SChristian Borntraeger 150811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 150911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 151011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 151111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 151211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 151311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 151411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 151511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 151611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 151711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 151811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 151911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 152011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 152111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 152211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 152311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 152411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 152511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 152611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 152711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 152811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 152911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 153011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 153111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 153211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 153311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 153411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 153511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 153611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 153711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 153811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 153911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 154011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 154111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 154211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 154311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 154411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 154511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 154611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 154711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 154811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 154911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 155011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 155111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 155213209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 155313209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 155413209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1555173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1556173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1557173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1558173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1559173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15604f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15614f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15624f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15634f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15644f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 156511ba5961SChristian Borntraeger 1566346fa2f8SChristian Borntraeger return 0; 15670a763c78SDavid Hildenbrand } 15680a763c78SDavid Hildenbrand 15690a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 15700a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 15710a763c78SDavid Hildenbrand { 15720a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 15730a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 15740a763c78SDavid Hildenbrand return -EFAULT; 157511ba5961SChristian Borntraeger 157611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 157711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 157811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 157911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 158011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 158111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 158211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 158311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 158411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 158511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 158611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 158711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 158811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 158911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 159011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 159111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 159211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 159311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 159411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 159511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 159611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 159711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 159811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 159911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 160011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 160111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 160211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 160311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 160411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 160511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 160611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 160711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 160811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 160911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 161011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 161111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 161211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 161311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 161411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 161511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 161611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 161711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 161811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 161911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 162013209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 162113209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 162213209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1623173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1624173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1625173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1626173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1627173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 16284f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 16294f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 16304f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 16314f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 16324f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 163311ba5961SChristian Borntraeger 16340a763c78SDavid Hildenbrand return 0; 16350a763c78SDavid Hildenbrand } 1636346fa2f8SChristian Borntraeger 1637658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1638658b6edaSMichael Mueller { 1639658b6edaSMichael Mueller int ret = -ENXIO; 1640658b6edaSMichael Mueller 1641658b6edaSMichael Mueller switch (attr->attr) { 1642658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1643658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1644658b6edaSMichael Mueller break; 1645658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1646658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1647658b6edaSMichael Mueller break; 164815c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 164915c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 165015c9705fSDavid Hildenbrand break; 165115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 165215c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 165315c9705fSDavid Hildenbrand break; 16540a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 16550a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 16560a763c78SDavid Hildenbrand break; 16570a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 16580a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 16590a763c78SDavid Hildenbrand break; 1660658b6edaSMichael Mueller } 1661658b6edaSMichael Mueller return ret; 1662658b6edaSMichael Mueller } 1663658b6edaSMichael Mueller 1664f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1665f2061656SDominik Dingel { 1666f2061656SDominik Dingel int ret; 1667f2061656SDominik Dingel 1668f2061656SDominik Dingel switch (attr->group) { 16694f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 16708c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 16714f718eabSDominik Dingel break; 167272f25020SJason J. Herne case KVM_S390_VM_TOD: 167372f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 167472f25020SJason J. Herne break; 1675658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1676658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1677658b6edaSMichael Mueller break; 1678a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1679a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1680a374e892STony Krowiak break; 1681190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1682190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1683190df4a2SClaudio Imbrenda break; 1684f2061656SDominik Dingel default: 1685f2061656SDominik Dingel ret = -ENXIO; 1686f2061656SDominik Dingel break; 1687f2061656SDominik Dingel } 1688f2061656SDominik Dingel 1689f2061656SDominik Dingel return ret; 1690f2061656SDominik Dingel } 1691f2061656SDominik Dingel 1692f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1693f2061656SDominik Dingel { 16948c0a7ce6SDominik Dingel int ret; 16958c0a7ce6SDominik Dingel 16968c0a7ce6SDominik Dingel switch (attr->group) { 16978c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 16988c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 16998c0a7ce6SDominik Dingel break; 170072f25020SJason J. Herne case KVM_S390_VM_TOD: 170172f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 170272f25020SJason J. Herne break; 1703658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1704658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1705658b6edaSMichael Mueller break; 1706190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1707190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1708190df4a2SClaudio Imbrenda break; 17098c0a7ce6SDominik Dingel default: 17108c0a7ce6SDominik Dingel ret = -ENXIO; 17118c0a7ce6SDominik Dingel break; 17128c0a7ce6SDominik Dingel } 17138c0a7ce6SDominik Dingel 17148c0a7ce6SDominik Dingel return ret; 1715f2061656SDominik Dingel } 1716f2061656SDominik Dingel 1717f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1718f2061656SDominik Dingel { 1719f2061656SDominik Dingel int ret; 1720f2061656SDominik Dingel 1721f2061656SDominik Dingel switch (attr->group) { 17224f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 17234f718eabSDominik Dingel switch (attr->attr) { 17244f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 17254f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1726f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1727f9cbd9b0SDavid Hildenbrand break; 17288c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 17294f718eabSDominik Dingel ret = 0; 17304f718eabSDominik Dingel break; 17314f718eabSDominik Dingel default: 17324f718eabSDominik Dingel ret = -ENXIO; 17334f718eabSDominik Dingel break; 17344f718eabSDominik Dingel } 17354f718eabSDominik Dingel break; 173672f25020SJason J. Herne case KVM_S390_VM_TOD: 173772f25020SJason J. Herne switch (attr->attr) { 173872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 173972f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 174072f25020SJason J. Herne ret = 0; 174172f25020SJason J. Herne break; 174272f25020SJason J. Herne default: 174372f25020SJason J. Herne ret = -ENXIO; 174472f25020SJason J. Herne break; 174572f25020SJason J. Herne } 174672f25020SJason J. Herne break; 1747658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1748658b6edaSMichael Mueller switch (attr->attr) { 1749658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1750658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 175115c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 175215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 17530a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1754346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1755658b6edaSMichael Mueller ret = 0; 1756658b6edaSMichael Mueller break; 1757658b6edaSMichael Mueller default: 1758658b6edaSMichael Mueller ret = -ENXIO; 1759658b6edaSMichael Mueller break; 1760658b6edaSMichael Mueller } 1761658b6edaSMichael Mueller break; 1762a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1763a374e892STony Krowiak switch (attr->attr) { 1764a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1765a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1766a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1767a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1768a374e892STony Krowiak ret = 0; 1769a374e892STony Krowiak break; 177037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 177137940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 177237940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 177337940fb0STony Krowiak break; 1774a374e892STony Krowiak default: 1775a374e892STony Krowiak ret = -ENXIO; 1776a374e892STony Krowiak break; 1777a374e892STony Krowiak } 1778a374e892STony Krowiak break; 1779190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1780190df4a2SClaudio Imbrenda ret = 0; 1781190df4a2SClaudio Imbrenda break; 1782f2061656SDominik Dingel default: 1783f2061656SDominik Dingel ret = -ENXIO; 1784f2061656SDominik Dingel break; 1785f2061656SDominik Dingel } 1786f2061656SDominik Dingel 1787f2061656SDominik Dingel return ret; 1788f2061656SDominik Dingel } 1789f2061656SDominik Dingel 179030ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 179130ee2a98SJason J. Herne { 179230ee2a98SJason J. Herne uint8_t *keys; 179330ee2a98SJason J. Herne uint64_t hva; 17944f899147SChristian Borntraeger int srcu_idx, i, r = 0; 179530ee2a98SJason J. Herne 179630ee2a98SJason J. Herne if (args->flags != 0) 179730ee2a98SJason J. Herne return -EINVAL; 179830ee2a98SJason J. Herne 179930ee2a98SJason J. Herne /* Is this guest using storage keys? */ 180055531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 180130ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 180230ee2a98SJason J. Herne 180330ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 180430ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 180530ee2a98SJason J. Herne return -EINVAL; 180630ee2a98SJason J. Herne 1807752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 180830ee2a98SJason J. Herne if (!keys) 180930ee2a98SJason J. Herne return -ENOMEM; 181030ee2a98SJason J. Herne 1811d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 18124f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 181330ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 181430ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 181530ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 181630ee2a98SJason J. Herne r = -EFAULT; 1817d3ed1ceeSMartin Schwidefsky break; 181830ee2a98SJason J. Herne } 181930ee2a98SJason J. Herne 1820154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 1821154c8c19SDavid Hildenbrand if (r) 1822d3ed1ceeSMartin Schwidefsky break; 182330ee2a98SJason J. Herne } 18244f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1825d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 182630ee2a98SJason J. Herne 1827d3ed1ceeSMartin Schwidefsky if (!r) { 182830ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 182930ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 183030ee2a98SJason J. Herne if (r) 183130ee2a98SJason J. Herne r = -EFAULT; 1832d3ed1ceeSMartin Schwidefsky } 1833d3ed1ceeSMartin Schwidefsky 183430ee2a98SJason J. Herne kvfree(keys); 183530ee2a98SJason J. Herne return r; 183630ee2a98SJason J. Herne } 183730ee2a98SJason J. Herne 183830ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 183930ee2a98SJason J. Herne { 184030ee2a98SJason J. Herne uint8_t *keys; 184130ee2a98SJason J. Herne uint64_t hva; 18424f899147SChristian Borntraeger int srcu_idx, i, r = 0; 1843bd096f64SJanosch Frank bool unlocked; 184430ee2a98SJason J. Herne 184530ee2a98SJason J. Herne if (args->flags != 0) 184630ee2a98SJason J. Herne return -EINVAL; 184730ee2a98SJason J. Herne 184830ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 184930ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 185030ee2a98SJason J. Herne return -EINVAL; 185130ee2a98SJason J. Herne 1852752ade68SMichal Hocko keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); 185330ee2a98SJason J. Herne if (!keys) 185430ee2a98SJason J. Herne return -ENOMEM; 185530ee2a98SJason J. Herne 185630ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 185730ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 185830ee2a98SJason J. Herne if (r) { 185930ee2a98SJason J. Herne r = -EFAULT; 186030ee2a98SJason J. Herne goto out; 186130ee2a98SJason J. Herne } 186230ee2a98SJason J. Herne 186330ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 186414d4a425SDominik Dingel r = s390_enable_skey(); 186514d4a425SDominik Dingel if (r) 186614d4a425SDominik Dingel goto out; 186730ee2a98SJason J. Herne 1868bd096f64SJanosch Frank i = 0; 1869d3ed1ceeSMartin Schwidefsky down_read(¤t->mm->mmap_sem); 18704f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 1871bd096f64SJanosch Frank while (i < args->count) { 1872bd096f64SJanosch Frank unlocked = false; 187330ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 187430ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 187530ee2a98SJason J. Herne r = -EFAULT; 1876d3ed1ceeSMartin Schwidefsky break; 187730ee2a98SJason J. Herne } 187830ee2a98SJason J. Herne 187930ee2a98SJason J. Herne /* Lowest order bit is reserved */ 188030ee2a98SJason J. Herne if (keys[i] & 0x01) { 188130ee2a98SJason J. Herne r = -EINVAL; 1882d3ed1ceeSMartin Schwidefsky break; 188330ee2a98SJason J. Herne } 188430ee2a98SJason J. Herne 1885fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 1886bd096f64SJanosch Frank if (r) { 1887bd096f64SJanosch Frank r = fixup_user_fault(current, current->mm, hva, 1888bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 188930ee2a98SJason J. Herne if (r) 1890d3ed1ceeSMartin Schwidefsky break; 189130ee2a98SJason J. Herne } 1892bd096f64SJanosch Frank if (!r) 1893bd096f64SJanosch Frank i++; 1894bd096f64SJanosch Frank } 18954f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 1896d3ed1ceeSMartin Schwidefsky up_read(¤t->mm->mmap_sem); 189730ee2a98SJason J. Herne out: 189830ee2a98SJason J. Herne kvfree(keys); 189930ee2a98SJason J. Herne return r; 190030ee2a98SJason J. Herne } 190130ee2a98SJason J. Herne 19024036e387SClaudio Imbrenda /* 19034036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 19044036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 19054036e387SClaudio Imbrenda * two longs. 19064036e387SClaudio Imbrenda */ 19074036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 19084036e387SClaudio Imbrenda /* for consistency */ 19094036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 19104036e387SClaudio Imbrenda 19114036e387SClaudio Imbrenda /* 1912afdad616SClaudio Imbrenda * Similar to gfn_to_memslot, but returns the index of a memslot also when the 1913afdad616SClaudio Imbrenda * address falls in a hole. In that case the index of one of the memslots 1914afdad616SClaudio Imbrenda * bordering the hole is returned. 1915afdad616SClaudio Imbrenda */ 1916afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) 1917afdad616SClaudio Imbrenda { 1918afdad616SClaudio Imbrenda int start = 0, end = slots->used_slots; 1919afdad616SClaudio Imbrenda int slot = atomic_read(&slots->lru_slot); 1920afdad616SClaudio Imbrenda struct kvm_memory_slot *memslots = slots->memslots; 1921afdad616SClaudio Imbrenda 1922afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn && 1923afdad616SClaudio Imbrenda gfn < memslots[slot].base_gfn + memslots[slot].npages) 1924afdad616SClaudio Imbrenda return slot; 1925afdad616SClaudio Imbrenda 1926afdad616SClaudio Imbrenda while (start < end) { 1927afdad616SClaudio Imbrenda slot = start + (end - start) / 2; 1928afdad616SClaudio Imbrenda 1929afdad616SClaudio Imbrenda if (gfn >= memslots[slot].base_gfn) 1930afdad616SClaudio Imbrenda end = slot; 1931afdad616SClaudio Imbrenda else 1932afdad616SClaudio Imbrenda start = slot + 1; 1933afdad616SClaudio Imbrenda } 1934afdad616SClaudio Imbrenda 1935afdad616SClaudio Imbrenda if (gfn >= memslots[start].base_gfn && 1936afdad616SClaudio Imbrenda gfn < memslots[start].base_gfn + memslots[start].npages) { 1937afdad616SClaudio Imbrenda atomic_set(&slots->lru_slot, start); 1938afdad616SClaudio Imbrenda } 1939afdad616SClaudio Imbrenda 1940afdad616SClaudio Imbrenda return start; 1941afdad616SClaudio Imbrenda } 1942afdad616SClaudio Imbrenda 1943afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1944afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1945afdad616SClaudio Imbrenda { 1946afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 1947afdad616SClaudio Imbrenda 1948afdad616SClaudio Imbrenda args->count = 0; 1949afdad616SClaudio Imbrenda while (args->count < bufsize) { 1950afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 1951afdad616SClaudio Imbrenda /* 1952afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 1953afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 1954afdad616SClaudio Imbrenda */ 1955afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 1956afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 1957afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 1958afdad616SClaudio Imbrenda pgstev = 0; 1959afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 1960afdad616SClaudio Imbrenda cur_gfn++; 1961afdad616SClaudio Imbrenda } 1962afdad616SClaudio Imbrenda 1963afdad616SClaudio Imbrenda return 0; 1964afdad616SClaudio Imbrenda } 1965afdad616SClaudio Imbrenda 1966afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 1967afdad616SClaudio Imbrenda unsigned long cur_gfn) 1968afdad616SClaudio Imbrenda { 1969afdad616SClaudio Imbrenda int slotidx = gfn_to_memslot_approx(slots, cur_gfn); 1970afdad616SClaudio Imbrenda struct kvm_memory_slot *ms = slots->memslots + slotidx; 1971afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 1972afdad616SClaudio Imbrenda 1973afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 1974afdad616SClaudio Imbrenda slotidx--; 1975afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 1976afdad616SClaudio Imbrenda if (slotidx < 0) 1977afdad616SClaudio Imbrenda slotidx = slots->used_slots - 1; 1978afdad616SClaudio Imbrenda 1979afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1980afdad616SClaudio Imbrenda ofs = 0; 1981afdad616SClaudio Imbrenda } 1982afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 1983afdad616SClaudio Imbrenda while ((slotidx > 0) && (ofs >= ms->npages)) { 1984afdad616SClaudio Imbrenda slotidx--; 1985afdad616SClaudio Imbrenda ms = slots->memslots + slotidx; 1986afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); 1987afdad616SClaudio Imbrenda } 1988afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 1989afdad616SClaudio Imbrenda } 1990afdad616SClaudio Imbrenda 1991afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 1992afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 1993afdad616SClaudio Imbrenda { 1994afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 1995afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 1996afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 1997afdad616SClaudio Imbrenda 1998afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 1999afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2000afdad616SClaudio Imbrenda args->count = 0; 2001afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2002afdad616SClaudio Imbrenda if (!ms) 2003afdad616SClaudio Imbrenda return 0; 2004afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2005afdad616SClaudio Imbrenda mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; 2006afdad616SClaudio Imbrenda 2007afdad616SClaudio Imbrenda while (args->count < bufsize) { 2008afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2009afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2010afdad616SClaudio Imbrenda return 0; 2011afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2012afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2013afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2014afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2015afdad616SClaudio Imbrenda pgstev = 0; 2016afdad616SClaudio Imbrenda /* Save the value */ 2017afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2018afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2019afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2020afdad616SClaudio Imbrenda return 0; 2021afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2022afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2023afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2024afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2025afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2026afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2027afdad616SClaudio Imbrenda return 0; 2028afdad616SClaudio Imbrenda cur_gfn++; 2029afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2030afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2031afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2032afdad616SClaudio Imbrenda if (!ms) 2033afdad616SClaudio Imbrenda return 0; 2034afdad616SClaudio Imbrenda } 2035afdad616SClaudio Imbrenda } 2036afdad616SClaudio Imbrenda return 0; 2037afdad616SClaudio Imbrenda } 2038afdad616SClaudio Imbrenda 2039afdad616SClaudio Imbrenda /* 20404036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 20414036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 20424036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 20434036e387SClaudio Imbrenda * no trailing clean bytes are saved. 20444036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 20454036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 20464036e387SClaudio Imbrenda */ 20474036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 20484036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 20494036e387SClaudio Imbrenda { 2050afdad616SClaudio Imbrenda unsigned long bufsize; 2051afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2052afdad616SClaudio Imbrenda u8 *values; 20534036e387SClaudio Imbrenda 2054afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 20554036e387SClaudio Imbrenda return -ENXIO; 20564036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 20574036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 20584036e387SClaudio Imbrenda return -EINVAL; 20594036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 20604036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2061afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 20624036e387SClaudio Imbrenda return -EINVAL; 20634036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 20644036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2065c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 20664036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20674036e387SClaudio Imbrenda return 0; 20684036e387SClaudio Imbrenda } 20694036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2070afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 20714036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 20724036e387SClaudio Imbrenda return 0; 20734036e387SClaudio Imbrenda } 20744036e387SClaudio Imbrenda 2075afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2076afdad616SClaudio Imbrenda if (!values) 20774036e387SClaudio Imbrenda return -ENOMEM; 20784036e387SClaudio Imbrenda 20794036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 20804036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2081afdad616SClaudio Imbrenda if (peek) 2082afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2083afdad616SClaudio Imbrenda else 2084afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 20854036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 20864036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 20874036e387SClaudio Imbrenda 2088afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2089afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2090afdad616SClaudio Imbrenda else 2091afdad616SClaudio Imbrenda args->remaining = 0; 20924036e387SClaudio Imbrenda 2093afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2094afdad616SClaudio Imbrenda ret = -EFAULT; 2095afdad616SClaudio Imbrenda 2096afdad616SClaudio Imbrenda vfree(values); 2097afdad616SClaudio Imbrenda return ret; 20984036e387SClaudio Imbrenda } 20994036e387SClaudio Imbrenda 21004036e387SClaudio Imbrenda /* 21014036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 21024036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2103c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 21044036e387SClaudio Imbrenda */ 21054036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 21064036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 21074036e387SClaudio Imbrenda { 21084036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 21094036e387SClaudio Imbrenda uint8_t *bits; 21104036e387SClaudio Imbrenda int srcu_idx, r = 0; 21114036e387SClaudio Imbrenda 21124036e387SClaudio Imbrenda mask = args->mask; 21134036e387SClaudio Imbrenda 21144036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 21154036e387SClaudio Imbrenda return -ENXIO; 21164036e387SClaudio Imbrenda /* invalid/unsupported flags */ 21174036e387SClaudio Imbrenda if (args->flags != 0) 21184036e387SClaudio Imbrenda return -EINVAL; 21194036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 21204036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 21214036e387SClaudio Imbrenda return -EINVAL; 21224036e387SClaudio Imbrenda /* Nothing to do */ 21234036e387SClaudio Imbrenda if (args->count == 0) 21244036e387SClaudio Imbrenda return 0; 21254036e387SClaudio Imbrenda 212642bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 21274036e387SClaudio Imbrenda if (!bits) 21284036e387SClaudio Imbrenda return -ENOMEM; 21294036e387SClaudio Imbrenda 21304036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 21314036e387SClaudio Imbrenda if (r) { 21324036e387SClaudio Imbrenda r = -EFAULT; 21334036e387SClaudio Imbrenda goto out; 21344036e387SClaudio Imbrenda } 21354036e387SClaudio Imbrenda 21364036e387SClaudio Imbrenda down_read(&kvm->mm->mmap_sem); 21374036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 21384036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 21394036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 21404036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 21414036e387SClaudio Imbrenda r = -EFAULT; 21424036e387SClaudio Imbrenda break; 21434036e387SClaudio Imbrenda } 21444036e387SClaudio Imbrenda 21454036e387SClaudio Imbrenda pgstev = bits[i]; 21464036e387SClaudio Imbrenda pgstev = pgstev << 24; 21471bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 21484036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 21494036e387SClaudio Imbrenda } 21504036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 21514036e387SClaudio Imbrenda up_read(&kvm->mm->mmap_sem); 21524036e387SClaudio Imbrenda 2153c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 21544036e387SClaudio Imbrenda down_write(&kvm->mm->mmap_sem); 2155c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 21564036e387SClaudio Imbrenda up_write(&kvm->mm->mmap_sem); 21574036e387SClaudio Imbrenda } 21584036e387SClaudio Imbrenda out: 21594036e387SClaudio Imbrenda vfree(bits); 21604036e387SClaudio Imbrenda return r; 21614036e387SClaudio Imbrenda } 21624036e387SClaudio Imbrenda 2163b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2164b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2165b0c632dbSHeiko Carstens { 2166b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2167b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2168f2061656SDominik Dingel struct kvm_device_attr attr; 2169b0c632dbSHeiko Carstens int r; 2170b0c632dbSHeiko Carstens 2171b0c632dbSHeiko Carstens switch (ioctl) { 2172ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2173ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2174ba5c1e9bSCarsten Otte 2175ba5c1e9bSCarsten Otte r = -EFAULT; 2176ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2177ba5c1e9bSCarsten Otte break; 2178ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2179ba5c1e9bSCarsten Otte break; 2180ba5c1e9bSCarsten Otte } 218184223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 218284223598SCornelia Huck struct kvm_irq_routing_entry routing; 218384223598SCornelia Huck 218484223598SCornelia Huck r = -EINVAL; 218584223598SCornelia Huck if (kvm->arch.use_irqchip) { 218684223598SCornelia Huck /* Set up dummy routing. */ 218784223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2188152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 218984223598SCornelia Huck } 219084223598SCornelia Huck break; 219184223598SCornelia Huck } 2192f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2193f2061656SDominik Dingel r = -EFAULT; 2194f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2195f2061656SDominik Dingel break; 2196f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2197f2061656SDominik Dingel break; 2198f2061656SDominik Dingel } 2199f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2200f2061656SDominik Dingel r = -EFAULT; 2201f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2202f2061656SDominik Dingel break; 2203f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2204f2061656SDominik Dingel break; 2205f2061656SDominik Dingel } 2206f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2207f2061656SDominik Dingel r = -EFAULT; 2208f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2209f2061656SDominik Dingel break; 2210f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2211f2061656SDominik Dingel break; 2212f2061656SDominik Dingel } 221330ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 221430ee2a98SJason J. Herne struct kvm_s390_skeys args; 221530ee2a98SJason J. Herne 221630ee2a98SJason J. Herne r = -EFAULT; 221730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 221830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 221930ee2a98SJason J. Herne break; 222030ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 222130ee2a98SJason J. Herne break; 222230ee2a98SJason J. Herne } 222330ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 222430ee2a98SJason J. Herne struct kvm_s390_skeys args; 222530ee2a98SJason J. Herne 222630ee2a98SJason J. Herne r = -EFAULT; 222730ee2a98SJason J. Herne if (copy_from_user(&args, argp, 222830ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 222930ee2a98SJason J. Herne break; 223030ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 223130ee2a98SJason J. Herne break; 223230ee2a98SJason J. Herne } 22334036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 22344036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 22354036e387SClaudio Imbrenda 22364036e387SClaudio Imbrenda r = -EFAULT; 22374036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 22384036e387SClaudio Imbrenda break; 22391de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 22404036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 22411de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 22424036e387SClaudio Imbrenda if (!r) { 22434036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 22444036e387SClaudio Imbrenda if (r) 22454036e387SClaudio Imbrenda r = -EFAULT; 22464036e387SClaudio Imbrenda } 22474036e387SClaudio Imbrenda break; 22484036e387SClaudio Imbrenda } 22494036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 22504036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 22514036e387SClaudio Imbrenda 22524036e387SClaudio Imbrenda r = -EFAULT; 22534036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 22544036e387SClaudio Imbrenda break; 22551de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 22564036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 22571de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 22584036e387SClaudio Imbrenda break; 22594036e387SClaudio Imbrenda } 2260b0c632dbSHeiko Carstens default: 2261367e1319SAvi Kivity r = -ENOTTY; 2262b0c632dbSHeiko Carstens } 2263b0c632dbSHeiko Carstens 2264b0c632dbSHeiko Carstens return r; 2265b0c632dbSHeiko Carstens } 2266b0c632dbSHeiko Carstens 226745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 226845c9b47cSTony Krowiak { 2269e585b24aSTony Krowiak struct ap_config_info info; 227045c9b47cSTony Krowiak 2271e585b24aSTony Krowiak if (ap_instructions_available()) { 2272e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2273e585b24aSTony Krowiak return info.apxa; 227445c9b47cSTony Krowiak } 227545c9b47cSTony Krowiak 227645c9b47cSTony Krowiak return 0; 227745c9b47cSTony Krowiak } 227845c9b47cSTony Krowiak 2279e585b24aSTony Krowiak /* 2280e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2281e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2282e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2283e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2284e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2285e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 2286e585b24aSTony Krowiak */ 228745c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 228845c9b47cSTony Krowiak { 228945c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 229045c9b47cSTony Krowiak 2291e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 2292e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 2293e585b24aSTony Krowiak 2294e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 2295e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2296e585b24aSTony Krowiak return; 2297e585b24aSTony Krowiak 229845c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 229945c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 230045c9b47cSTony Krowiak else 230145c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 230245c9b47cSTony Krowiak } 230345c9b47cSTony Krowiak 23040e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 23050e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 23060e237e44SPierre Morel { 23070e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 23080e237e44SPierre Morel 23090e237e44SPierre Morel mutex_lock(&kvm->lock); 23100e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 23110e237e44SPierre Morel 23120e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 23130e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 23140e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 23150e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 23160e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 23170e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 23180e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 23190e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 23200e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 23210e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 23220e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 23230e237e44SPierre Morel break; 23240e237e44SPierre Morel case CRYCB_FORMAT1: 23250e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 23260e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 23270e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 23280e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 23290e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 23300e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 23310e237e44SPierre Morel *((unsigned short *)adm)); 23320e237e44SPierre Morel break; 23330e237e44SPierre Morel default: /* Can not happen */ 23340e237e44SPierre Morel break; 23350e237e44SPierre Morel } 23360e237e44SPierre Morel 23370e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 23380e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 23390e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 23400e237e44SPierre Morel mutex_unlock(&kvm->lock); 23410e237e44SPierre Morel } 23420e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 23430e237e44SPierre Morel 234442104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 234542104598STony Krowiak { 234642104598STony Krowiak mutex_lock(&kvm->lock); 234742104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 234842104598STony Krowiak 234942104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 235042104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 235142104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 235242104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 235342104598STony Krowiak 23540e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 23556cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 23566cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 235742104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 235842104598STony Krowiak mutex_unlock(&kvm->lock); 235942104598STony Krowiak } 236042104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 236142104598STony Krowiak 23629bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 23639d8d5786SMichael Mueller { 23649bb0ec09SDavid Hildenbrand struct cpuid cpuid; 23659bb0ec09SDavid Hildenbrand 23669bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 23679bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 23689bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 23699d8d5786SMichael Mueller } 23709d8d5786SMichael Mueller 2371c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 23725102ee87STony Krowiak { 2373c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 237445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 23755102ee87STony Krowiak 2376e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 2377e585b24aSTony Krowiak return; 2378e585b24aSTony Krowiak 2379ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 2380ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 2381ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 2382ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 2383ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 2384ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 2385ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 23865102ee87STony Krowiak } 23875102ee87STony Krowiak 23887d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 23897d43bafcSEugene (jno) Dvurechenski { 23907d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 23915e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 23927d43bafcSEugene (jno) Dvurechenski else 23937d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 23947d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 23957d43bafcSEugene (jno) Dvurechenski } 23967d43bafcSEugene (jno) Dvurechenski 2397e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 2398b0c632dbSHeiko Carstens { 239976a6dd72SDavid Hildenbrand gfp_t alloc_flags = GFP_KERNEL; 24009d8d5786SMichael Mueller int i, rc; 2401b0c632dbSHeiko Carstens char debug_name[16]; 2402f6c137ffSChristian Borntraeger static unsigned long sca_offset; 2403b0c632dbSHeiko Carstens 2404e08b9637SCarsten Otte rc = -EINVAL; 2405e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 2406e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 2407e08b9637SCarsten Otte goto out_err; 2408e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 2409e08b9637SCarsten Otte goto out_err; 2410e08b9637SCarsten Otte #else 2411e08b9637SCarsten Otte if (type) 2412e08b9637SCarsten Otte goto out_err; 2413e08b9637SCarsten Otte #endif 2414e08b9637SCarsten Otte 2415b0c632dbSHeiko Carstens rc = s390_enable_sie(); 2416b0c632dbSHeiko Carstens if (rc) 2417d89f5effSJan Kiszka goto out_err; 2418b0c632dbSHeiko Carstens 2419b290411aSCarsten Otte rc = -ENOMEM; 2420b290411aSCarsten Otte 242176a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 242276a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 24235e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 24249ac96d75SDavid Hildenbrand /* start with basic SCA */ 242576a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 2426b0c632dbSHeiko Carstens if (!kvm->arch.sca) 2427d89f5effSJan Kiszka goto out_err; 24280d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 2429c5c2c393SDavid Hildenbrand sca_offset += 16; 2430bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 2431c5c2c393SDavid Hildenbrand sca_offset = 0; 2432bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 2433bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 24340d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 2435b0c632dbSHeiko Carstens 2436b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 2437b0c632dbSHeiko Carstens 24381cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 2439b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 244040f5b735SDominik Dingel goto out_err; 2441b0c632dbSHeiko Carstens 244219114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 2443c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 2444c54f0d6aSDavid Hildenbrand (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 2445c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 244640f5b735SDominik Dingel goto out_err; 24479d8d5786SMichael Mueller 244825c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 2449c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 2450c3b9e3e1SChristian Borntraeger 2451c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 2452c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] & 2453c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 2454c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 2455c3b9e3e1SChristian Borntraeger kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] & 2456c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 2457c3b9e3e1SChristian Borntraeger } 2458346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 2459981467c9SMichael Mueller 24601935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 24611935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 24621935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 24631935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 246495ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 246595ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 24661bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 24671bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 24681bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 24691bab1c02SClaudio Imbrenda } 247095ca2cb5SJanosch Frank 247105f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 247205f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 247305f31e3bSPierre Morel 24749bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 247537c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 24769d8d5786SMichael Mueller 2477c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 24785102ee87STony Krowiak 247951978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 2480ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 24816d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 24826d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 24838a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 2484a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 2485ba5c1e9bSCarsten Otte 2486b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 248778f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 2488b0c632dbSHeiko Carstens 2489e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 2490e08b9637SCarsten Otte kvm->arch.gmap = NULL; 2491a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 2492e08b9637SCarsten Otte } else { 249332e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 2494ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 249532e6b236SGuenther Hutzl else 2496ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 249732e6b236SGuenther Hutzl sclp.hamax + 1); 24986ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 2499598841caSCarsten Otte if (!kvm->arch.gmap) 250040f5b735SDominik Dingel goto out_err; 25012c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 250224eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 2503e08b9637SCarsten Otte } 2504fa6b7fe9SCornelia Huck 2505c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 250655531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 25078ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 2508a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 2509d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 25108335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 25118ad35755SDavid Hildenbrand 2512d89f5effSJan Kiszka return 0; 2513d89f5effSJan Kiszka out_err: 2514c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 251540f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 25167d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 251778f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 2518d89f5effSJan Kiszka return rc; 2519b0c632dbSHeiko Carstens } 2520b0c632dbSHeiko Carstens 2521d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 2522d329c035SChristian Borntraeger { 2523d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 2524ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 252567335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 25263c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 2527bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 2528a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 252927e0393fSCarsten Otte 253027e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 25316ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 253227e0393fSCarsten Otte 2533e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 2534b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 2535d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 2536b31288faSKonstantin Weitz 25376692cef3SChristian Borntraeger kvm_vcpu_uninit(vcpu); 2538b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 2539d329c035SChristian Borntraeger } 2540d329c035SChristian Borntraeger 2541d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm) 2542d329c035SChristian Borntraeger { 2543d329c035SChristian Borntraeger unsigned int i; 2544988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 2545d329c035SChristian Borntraeger 2546988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 2547988a2caeSGleb Natapov kvm_arch_vcpu_destroy(vcpu); 2548988a2caeSGleb Natapov 2549988a2caeSGleb Natapov mutex_lock(&kvm->lock); 2550988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 2551d329c035SChristian Borntraeger kvm->vcpus[i] = NULL; 2552988a2caeSGleb Natapov 2553988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 2554988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 2555d329c035SChristian Borntraeger } 2556d329c035SChristian Borntraeger 2557b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 2558b0c632dbSHeiko Carstens { 2559d329c035SChristian Borntraeger kvm_free_vcpus(kvm); 25607d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 2561d329c035SChristian Borntraeger debug_unregister(kvm->arch.dbf); 2562d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 2563c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 256427e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 25656ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 2566841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 256767335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 2568a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 25698335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 2570b0c632dbSHeiko Carstens } 2571b0c632dbSHeiko Carstens 2572b0c632dbSHeiko Carstens /* Section: vcpu related */ 2573dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 2574b0c632dbSHeiko Carstens { 25756ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 257627e0393fSCarsten Otte if (!vcpu->arch.gmap) 257727e0393fSCarsten Otte return -ENOMEM; 25782c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 2579dafd032aSDominik Dingel 258027e0393fSCarsten Otte return 0; 258127e0393fSCarsten Otte } 258227e0393fSCarsten Otte 2583a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 2584a6e2f683SEugene (jno) Dvurechenski { 2585a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 2586a6940674SDavid Hildenbrand return; 25875e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 25887d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 25897d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 25907d43bafcSEugene (jno) Dvurechenski 25917d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 25927d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 25937d43bafcSEugene (jno) Dvurechenski } else { 2594bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 2595a6e2f683SEugene (jno) Dvurechenski 2596a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2597a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 2598a6e2f683SEugene (jno) Dvurechenski } 25995e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 26007d43bafcSEugene (jno) Dvurechenski } 2601a6e2f683SEugene (jno) Dvurechenski 2602eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 2603a6e2f683SEugene (jno) Dvurechenski { 2604a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2605a6940674SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2606a6940674SDavid Hildenbrand 2607a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 2608a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2609a6940674SDavid Hildenbrand vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2610f07afa04SDavid Hildenbrand return; 2611a6940674SDavid Hildenbrand } 2612eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 2613eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 2614eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 26157d43bafcSEugene (jno) Dvurechenski 2616eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 26177d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 26187d43bafcSEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; 26190c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 2620eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 26217d43bafcSEugene (jno) Dvurechenski } else { 2622eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 2623a6e2f683SEugene (jno) Dvurechenski 2624eaa78f34SDavid Hildenbrand sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; 2625a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); 2626a6e2f683SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; 2627eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 2628a6e2f683SEugene (jno) Dvurechenski } 2629eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 26305e044315SEugene (jno) Dvurechenski } 26315e044315SEugene (jno) Dvurechenski 26325e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 26335e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 26345e044315SEugene (jno) Dvurechenski { 26355e044315SEugene (jno) Dvurechenski d->sda = s->sda; 26365e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 26375e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 26385e044315SEugene (jno) Dvurechenski } 26395e044315SEugene (jno) Dvurechenski 26405e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 26415e044315SEugene (jno) Dvurechenski { 26425e044315SEugene (jno) Dvurechenski int i; 26435e044315SEugene (jno) Dvurechenski 26445e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 26455e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 26465e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 26475e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 26485e044315SEugene (jno) Dvurechenski } 26495e044315SEugene (jno) Dvurechenski 26505e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 26515e044315SEugene (jno) Dvurechenski { 26525e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 26535e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 26545e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 26555e044315SEugene (jno) Dvurechenski unsigned int vcpu_idx; 26565e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 26575e044315SEugene (jno) Dvurechenski 26585e044315SEugene (jno) Dvurechenski new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); 26595e044315SEugene (jno) Dvurechenski if (!new_sca) 26605e044315SEugene (jno) Dvurechenski return -ENOMEM; 26615e044315SEugene (jno) Dvurechenski 26625e044315SEugene (jno) Dvurechenski scaoh = (u32)((u64)(new_sca) >> 32); 26635e044315SEugene (jno) Dvurechenski scaol = (u32)(u64)(new_sca) & ~0x3fU; 26645e044315SEugene (jno) Dvurechenski 26655e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 26665e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 26675e044315SEugene (jno) Dvurechenski 26685e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 26695e044315SEugene (jno) Dvurechenski 26705e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 26715e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 26725e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 26730c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 26745e044315SEugene (jno) Dvurechenski } 26755e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 26765e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 26775e044315SEugene (jno) Dvurechenski 26785e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 26795e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 26805e044315SEugene (jno) Dvurechenski 26815e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 26825e044315SEugene (jno) Dvurechenski 26838335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 26848335713aSChristian Borntraeger old_sca, kvm->arch.sca); 26855e044315SEugene (jno) Dvurechenski return 0; 26867d43bafcSEugene (jno) Dvurechenski } 2687a6e2f683SEugene (jno) Dvurechenski 2688a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 2689a6e2f683SEugene (jno) Dvurechenski { 26905e044315SEugene (jno) Dvurechenski int rc; 26915e044315SEugene (jno) Dvurechenski 2692a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 2693a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 2694a6940674SDavid Hildenbrand return true; 2695a6940674SDavid Hildenbrand return false; 2696a6940674SDavid Hildenbrand } 26975e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 26985e044315SEugene (jno) Dvurechenski return true; 269976a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 27005e044315SEugene (jno) Dvurechenski return false; 27015e044315SEugene (jno) Dvurechenski 27025e044315SEugene (jno) Dvurechenski mutex_lock(&kvm->lock); 27035e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 27045e044315SEugene (jno) Dvurechenski mutex_unlock(&kvm->lock); 27055e044315SEugene (jno) Dvurechenski 27065e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 2707a6e2f683SEugene (jno) Dvurechenski } 2708a6e2f683SEugene (jno) Dvurechenski 2709dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 2710dafd032aSDominik Dingel { 2711dafd032aSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 2712dafd032aSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 271359674c1aSChristian Borntraeger vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 271459674c1aSChristian Borntraeger KVM_SYNC_GPRS | 27159eed0735SChristian Borntraeger KVM_SYNC_ACRS | 2716b028ee3eSDavid Hildenbrand KVM_SYNC_CRS | 2717b028ee3eSDavid Hildenbrand KVM_SYNC_ARCH0 | 2718b028ee3eSDavid Hildenbrand KVM_SYNC_PFAULT; 271975a4615cSJulius Niedworok kvm_s390_set_prefix(vcpu, 0); 2720c6e5f166SFan Zhang if (test_kvm_facility(vcpu->kvm, 64)) 2721c6e5f166SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 272235b3fde6SChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 82)) 272335b3fde6SChristian Borntraeger vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 27244e0b1ab7SFan Zhang if (test_kvm_facility(vcpu->kvm, 133)) 27254e0b1ab7SFan Zhang vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 2726a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 2727a3da7b4aSChristian Borntraeger vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 2728f6aa6dc4SDavid Hildenbrand /* fprs can be synchronized via vrs, even if the guest has no vx. With 2729f6aa6dc4SDavid Hildenbrand * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 2730f6aa6dc4SDavid Hildenbrand */ 2731f6aa6dc4SDavid Hildenbrand if (MACHINE_HAS_VX) 273268c55750SEric Farman vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 27336fd8e67dSDavid Hildenbrand else 27346fd8e67dSDavid Hildenbrand vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 2735dafd032aSDominik Dingel 2736dafd032aSDominik Dingel if (kvm_is_ucontrol(vcpu->kvm)) 2737dafd032aSDominik Dingel return __kvm_ucontrol_vcpu_init(vcpu); 2738dafd032aSDominik Dingel 2739b0c632dbSHeiko Carstens return 0; 2740b0c632dbSHeiko Carstens } 2741b0c632dbSHeiko Carstens 2742db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2743db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2744db0758b2SDavid Hildenbrand { 2745db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 27469c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2747db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 27489c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2749db0758b2SDavid Hildenbrand } 2750db0758b2SDavid Hildenbrand 2751db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2752db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2753db0758b2SDavid Hildenbrand { 2754db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 27559c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2756db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 2757db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 27589c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2759db0758b2SDavid Hildenbrand } 2760db0758b2SDavid Hildenbrand 2761db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2762db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2763db0758b2SDavid Hildenbrand { 2764db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 2765db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 2766db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 2767db0758b2SDavid Hildenbrand } 2768db0758b2SDavid Hildenbrand 2769db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 2770db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2771db0758b2SDavid Hildenbrand { 2772db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 2773db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 2774db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 2775db0758b2SDavid Hildenbrand } 2776db0758b2SDavid Hildenbrand 2777db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2778db0758b2SDavid Hildenbrand { 2779db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2780db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 2781db0758b2SDavid Hildenbrand preempt_enable(); 2782db0758b2SDavid Hildenbrand } 2783db0758b2SDavid Hildenbrand 2784db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 2785db0758b2SDavid Hildenbrand { 2786db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 2787db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 2788db0758b2SDavid Hildenbrand preempt_enable(); 2789db0758b2SDavid Hildenbrand } 2790db0758b2SDavid Hildenbrand 27914287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 27924287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 27934287f247SDavid Hildenbrand { 2794db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 27959c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 2796db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 2797db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 27984287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 27999c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 2800db0758b2SDavid Hildenbrand preempt_enable(); 28014287f247SDavid Hildenbrand } 28024287f247SDavid Hildenbrand 2803db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 28044287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 28054287f247SDavid Hildenbrand { 28069c23a131SDavid Hildenbrand unsigned int seq; 2807db0758b2SDavid Hildenbrand __u64 value; 2808db0758b2SDavid Hildenbrand 2809db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 28104287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 2811db0758b2SDavid Hildenbrand 28129c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 28139c23a131SDavid Hildenbrand do { 28149c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 28159c23a131SDavid Hildenbrand /* 28169c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 28179c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 28189c23a131SDavid Hildenbrand */ 28199c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 2820db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 28219c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 28229c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 2823db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 28249c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 28259c23a131SDavid Hildenbrand preempt_enable(); 2826db0758b2SDavid Hildenbrand return value; 28274287f247SDavid Hildenbrand } 28284287f247SDavid Hildenbrand 2829b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 2830b0c632dbSHeiko Carstens { 28319977e886SHendrik Brueckner 283237d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 2833ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 28345ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2835db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 283601a745acSDavid Hildenbrand vcpu->cpu = cpu; 2837b0c632dbSHeiko Carstens } 2838b0c632dbSHeiko Carstens 2839b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 2840b0c632dbSHeiko Carstens { 284101a745acSDavid Hildenbrand vcpu->cpu = -1; 28425ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 2843db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 28449daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 284537d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 284637d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 28479977e886SHendrik Brueckner 2848b0c632dbSHeiko Carstens } 2849b0c632dbSHeiko Carstens 2850b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) 2851b0c632dbSHeiko Carstens { 2852b0c632dbSHeiko Carstens /* this equals initial cpu reset in pop, but we don't switch to ESA */ 2853b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.mask = 0UL; 2854b0c632dbSHeiko Carstens vcpu->arch.sie_block->gpsw.addr = 0UL; 28558d26cf7bSChristian Borntraeger kvm_s390_set_prefix(vcpu, 0); 28564287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, 0); 2857b0c632dbSHeiko Carstens vcpu->arch.sie_block->ckc = 0UL; 2858b0c632dbSHeiko Carstens vcpu->arch.sie_block->todpr = 0; 2859b0c632dbSHeiko Carstens memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); 2860b9224cd7SDavid Hildenbrand vcpu->arch.sie_block->gcr[0] = CR0_UNUSED_56 | 2861b9224cd7SDavid Hildenbrand CR0_INTERRUPT_KEY_SUBMASK | 2862b9224cd7SDavid Hildenbrand CR0_MEASUREMENT_ALERT_SUBMASK; 2863b9224cd7SDavid Hildenbrand vcpu->arch.sie_block->gcr[14] = CR14_UNUSED_32 | 2864b9224cd7SDavid Hildenbrand CR14_UNUSED_33 | 2865b9224cd7SDavid Hildenbrand CR14_EXTERNAL_DAMAGE_SUBMASK; 28669abc2a08SDavid Hildenbrand /* make sure the new fpc will be lazily loaded */ 28679abc2a08SDavid Hildenbrand save_fpu_regs(); 28689abc2a08SDavid Hildenbrand current->thread.fpu.fpc = 0; 2869b0c632dbSHeiko Carstens vcpu->arch.sie_block->gbea = 1; 2870672550fbSChristian Borntraeger vcpu->arch.sie_block->pp = 0; 287135b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 28723c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 28733c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 28746352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 28756852d7b6SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 28762ed10cc1SJens Freimann kvm_s390_clear_local_irqs(vcpu); 2877b0c632dbSHeiko Carstens } 2878b0c632dbSHeiko Carstens 287931928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 288042897d86SMarcelo Tosatti { 288172f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 2882fdf03650SFan Zhang preempt_disable(); 288372f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 2884d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 2885fdf03650SFan Zhang preempt_enable(); 288672f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 288725508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 2888dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 2889eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 289025508824SDavid Hildenbrand } 28916502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 28926502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 289337d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 289437d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 289542897d86SMarcelo Tosatti } 289642897d86SMarcelo Tosatti 28978ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 28988ec2fa52SChristian Borntraeger { 28998ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 29008ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 29018ec2fa52SChristian Borntraeger return true; 29028ec2fa52SChristian Borntraeger return false; 29038ec2fa52SChristian Borntraeger } 29048ec2fa52SChristian Borntraeger 29058ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 29068ec2fa52SChristian Borntraeger { 29078ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 29088ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 29098ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 29108ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 29118ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 29128ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 29138ec2fa52SChristian Borntraeger 29148ec2fa52SChristian Borntraeger } 29158ec2fa52SChristian Borntraeger 29165102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 29175102ee87STony Krowiak { 2918e585b24aSTony Krowiak /* 2919e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 2920e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 2921e585b24aSTony Krowiak */ 2922e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 29235102ee87STony Krowiak return; 29245102ee87STony Krowiak 2925e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 2926a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 292737940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 29288ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 2929a374e892STony Krowiak 2930e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 2931e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 2932e585b24aSTony Krowiak 2933e585b24aSTony Krowiak /* Set up protected key support */ 29348ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 2935a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 29368ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 29378ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 29388ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 29398ec2fa52SChristian Borntraeger } 29408ec2fa52SChristian Borntraeger 2941a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 2942a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 29435102ee87STony Krowiak } 29445102ee87STony Krowiak 2945b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 2946b31605c1SDominik Dingel { 2947b31605c1SDominik Dingel free_page(vcpu->arch.sie_block->cbrlo); 2948b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 2949b31605c1SDominik Dingel } 2950b31605c1SDominik Dingel 2951b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 2952b31605c1SDominik Dingel { 2953b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); 2954b31605c1SDominik Dingel if (!vcpu->arch.sie_block->cbrlo) 2955b31605c1SDominik Dingel return -ENOMEM; 2956b31605c1SDominik Dingel return 0; 2957b31605c1SDominik Dingel } 2958b31605c1SDominik Dingel 295991520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 296091520f1aSMichael Mueller { 296191520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 296291520f1aSMichael Mueller 296391520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 296480bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 2965c54f0d6aSDavid Hildenbrand vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; 296691520f1aSMichael Mueller } 296791520f1aSMichael Mueller 2968b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 2969b0c632dbSHeiko Carstens { 2970b31605c1SDominik Dingel int rc = 0; 2971b31288faSKonstantin Weitz 29729e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 29739e6dabefSCornelia Huck CPUSTAT_SM | 2974a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 2975a4a4f191SGuenther Hutzl 297653df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 2977ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 297853df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 2979ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 2980a4a4f191SGuenther Hutzl 298191520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 298291520f1aSMichael Mueller 2983bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 2984bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 29850c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 2986bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 29870c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 2988f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 29890c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 29907feb6bb8SMichael Mueller 2991c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 29920c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 2993cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 29940c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 29950c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 299648ee7d3aSDavid Hildenbrand if (sclp.has_cei) 29970c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 299811ad65b7SDavid Hildenbrand if (sclp.has_ib) 29990c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 300037c5f6c8SDavid Hildenbrand if (sclp.has_siif) 30010c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 300237c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 30030c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 300418280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 30050c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 30060c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 300713211ea7SEric Farman } 30088fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 30098fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3010a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3011a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3012d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3013d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3014d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3015d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3016d7c5cb01SMichael Mueller } 30174e0b1ab7SFan Zhang vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) 30184e0b1ab7SFan Zhang | SDNXC; 3019c6e5f166SFan Zhang vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 3020730cd632SFarhan Ali 3021730cd632SFarhan Ali if (sclp.has_kss) 3022ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3023730cd632SFarhan Ali else 3024492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 30255a5e6536SMatthew Rosato 3026e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3027b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3028b31605c1SDominik Dingel if (rc) 3029b31605c1SDominik Dingel return rc; 3030b31288faSKonstantin Weitz } 30310ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3032ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 30339d8d5786SMichael Mueller 303467d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 303567d49d52SCollin Walling 30365102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 30375102ee87STony Krowiak 3038b31605c1SDominik Dingel return rc; 3039b0c632dbSHeiko Carstens } 3040b0c632dbSHeiko Carstens 3041b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, 3042b0c632dbSHeiko Carstens unsigned int id) 3043b0c632dbSHeiko Carstens { 30444d47555aSCarsten Otte struct kvm_vcpu *vcpu; 30457feb6bb8SMichael Mueller struct sie_page *sie_page; 30464d47555aSCarsten Otte int rc = -EINVAL; 3047b0c632dbSHeiko Carstens 30484215825eSDavid Hildenbrand if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 30494d47555aSCarsten Otte goto out; 30504d47555aSCarsten Otte 30514d47555aSCarsten Otte rc = -ENOMEM; 30524d47555aSCarsten Otte 3053b110feafSMichael Mueller vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); 3054b0c632dbSHeiko Carstens if (!vcpu) 30554d47555aSCarsten Otte goto out; 3056b0c632dbSHeiko Carstens 3057da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 30587feb6bb8SMichael Mueller sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); 30597feb6bb8SMichael Mueller if (!sie_page) 3060b0c632dbSHeiko Carstens goto out_free_cpu; 3061b0c632dbSHeiko Carstens 30627feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 30637feb6bb8SMichael Mueller vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 30647feb6bb8SMichael Mueller 3065efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3066efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3067efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3068efed1104SDavid Hildenbrand 3069b0c632dbSHeiko Carstens vcpu->arch.sie_block->icpua = id; 3070ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3071982cff42SMichael Mueller vcpu->arch.sie_block->gd = (u32)(u64)kvm->arch.gisa_int.origin; 30724b9f9525SMichael Mueller if (vcpu->arch.sie_block->gd && sclp.has_gisaf) 30734b9f9525SMichael Mueller vcpu->arch.sie_block->gd |= GISA_FORMAT1; 30749c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3075ba5c1e9bSCarsten Otte 3076b0c632dbSHeiko Carstens rc = kvm_vcpu_init(vcpu, kvm, id); 3077b0c632dbSHeiko Carstens if (rc) 30789abc2a08SDavid Hildenbrand goto out_free_sie_block; 30798335713aSChristian Borntraeger VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu, 3080b0c632dbSHeiko Carstens vcpu->arch.sie_block); 3081ade38c31SCornelia Huck trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); 3082b0c632dbSHeiko Carstens 3083b0c632dbSHeiko Carstens return vcpu; 30847b06bf2fSWei Yongjun out_free_sie_block: 30857b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3086b0c632dbSHeiko Carstens out_free_cpu: 3087b110feafSMichael Mueller kmem_cache_free(kvm_vcpu_cache, vcpu); 30884d47555aSCarsten Otte out: 3089b0c632dbSHeiko Carstens return ERR_PTR(rc); 3090b0c632dbSHeiko Carstens } 3091b0c632dbSHeiko Carstens 3092b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3093b0c632dbSHeiko Carstens { 30949a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3095b0c632dbSHeiko Carstens } 3096b0c632dbSHeiko Carstens 3097199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3098199b5763SLongpeng(Mike) { 30990546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3100199b5763SLongpeng(Mike) } 3101199b5763SLongpeng(Mike) 310227406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 310349b99e1eSChristian Borntraeger { 3104805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 310561a6df54SDavid Hildenbrand exit_sie(vcpu); 310649b99e1eSChristian Borntraeger } 310749b99e1eSChristian Borntraeger 310827406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 310949b99e1eSChristian Borntraeger { 3110805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 311149b99e1eSChristian Borntraeger } 311249b99e1eSChristian Borntraeger 31138e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 31148e236546SChristian Borntraeger { 3115805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 311661a6df54SDavid Hildenbrand exit_sie(vcpu); 31178e236546SChristian Borntraeger } 31188e236546SChristian Borntraeger 31199ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 31209ea59728SDavid Hildenbrand { 31219ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 31229ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 31239ea59728SDavid Hildenbrand } 31249ea59728SDavid Hildenbrand 31258e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 31268e236546SChristian Borntraeger { 31279bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 31288e236546SChristian Borntraeger } 31298e236546SChristian Borntraeger 313049b99e1eSChristian Borntraeger /* 31319ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 313249b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 313349b99e1eSChristian Borntraeger * return immediately. */ 313449b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 313549b99e1eSChristian Borntraeger { 3136ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 31379ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 313849b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 313949b99e1eSChristian Borntraeger cpu_relax(); 314049b99e1eSChristian Borntraeger } 314149b99e1eSChristian Borntraeger 31428e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 31438e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 314449b99e1eSChristian Borntraeger { 31458e236546SChristian Borntraeger kvm_make_request(req, vcpu); 31468e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 314749b99e1eSChristian Borntraeger } 314849b99e1eSChristian Borntraeger 3149414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3150414d3b07SMartin Schwidefsky unsigned long end) 31512c70fe44SChristian Borntraeger { 31522c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 31532c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3154414d3b07SMartin Schwidefsky unsigned long prefix; 3155414d3b07SMartin Schwidefsky int i; 31562c70fe44SChristian Borntraeger 315765d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 315865d0b0d4SDavid Hildenbrand return; 3159414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3160414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3161414d3b07SMartin Schwidefsky return; 31622c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 31632c70fe44SChristian Borntraeger /* match against both prefix pages */ 3164414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3165414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3166414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3167414d3b07SMartin Schwidefsky start, end); 31688e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); 31692c70fe44SChristian Borntraeger } 31702c70fe44SChristian Borntraeger } 31712c70fe44SChristian Borntraeger } 31722c70fe44SChristian Borntraeger 31738b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 31748b905d28SChristian Borntraeger { 31758b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 31768b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 31778b905d28SChristian Borntraeger halt_poll_max_steal) { 31788b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 31798b905d28SChristian Borntraeger return true; 31808b905d28SChristian Borntraeger } 31818b905d28SChristian Borntraeger return false; 31828b905d28SChristian Borntraeger } 31838b905d28SChristian Borntraeger 3184b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3185b6d33834SChristoffer Dall { 3186b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3187b6d33834SChristoffer Dall BUG(); 3188b6d33834SChristoffer Dall return 0; 3189b6d33834SChristoffer Dall } 3190b6d33834SChristoffer Dall 319114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 319214eebd91SCarsten Otte struct kvm_one_reg *reg) 319314eebd91SCarsten Otte { 319414eebd91SCarsten Otte int r = -EINVAL; 319514eebd91SCarsten Otte 319614eebd91SCarsten Otte switch (reg->id) { 319729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 319829b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 319929b7c71bSCarsten Otte (u32 __user *)reg->addr); 320029b7c71bSCarsten Otte break; 320129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 320229b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 320329b7c71bSCarsten Otte (u64 __user *)reg->addr); 320429b7c71bSCarsten Otte break; 320546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 32064287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 320746a6dd1cSJason J. herne (u64 __user *)reg->addr); 320846a6dd1cSJason J. herne break; 320946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 321046a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 321146a6dd1cSJason J. herne (u64 __user *)reg->addr); 321246a6dd1cSJason J. herne break; 3213536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3214536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3215536336c2SDominik Dingel (u64 __user *)reg->addr); 3216536336c2SDominik Dingel break; 3217536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3218536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3219536336c2SDominik Dingel (u64 __user *)reg->addr); 3220536336c2SDominik Dingel break; 3221536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3222536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3223536336c2SDominik Dingel (u64 __user *)reg->addr); 3224536336c2SDominik Dingel break; 3225672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3226672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3227672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3228672550fbSChristian Borntraeger break; 3229afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3230afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3231afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3232afa45ff5SChristian Borntraeger break; 323314eebd91SCarsten Otte default: 323414eebd91SCarsten Otte break; 323514eebd91SCarsten Otte } 323614eebd91SCarsten Otte 323714eebd91SCarsten Otte return r; 323814eebd91SCarsten Otte } 323914eebd91SCarsten Otte 324014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 324114eebd91SCarsten Otte struct kvm_one_reg *reg) 324214eebd91SCarsten Otte { 324314eebd91SCarsten Otte int r = -EINVAL; 32444287f247SDavid Hildenbrand __u64 val; 324514eebd91SCarsten Otte 324614eebd91SCarsten Otte switch (reg->id) { 324729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 324829b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 324929b7c71bSCarsten Otte (u32 __user *)reg->addr); 325029b7c71bSCarsten Otte break; 325129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 325229b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 325329b7c71bSCarsten Otte (u64 __user *)reg->addr); 325429b7c71bSCarsten Otte break; 325546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 32564287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 32574287f247SDavid Hildenbrand if (!r) 32584287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 325946a6dd1cSJason J. herne break; 326046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 326146a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 326246a6dd1cSJason J. herne (u64 __user *)reg->addr); 326346a6dd1cSJason J. herne break; 3264536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3265536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 3266536336c2SDominik Dingel (u64 __user *)reg->addr); 32679fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 32689fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3269536336c2SDominik Dingel break; 3270536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3271536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 3272536336c2SDominik Dingel (u64 __user *)reg->addr); 3273536336c2SDominik Dingel break; 3274536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3275536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 3276536336c2SDominik Dingel (u64 __user *)reg->addr); 3277536336c2SDominik Dingel break; 3278672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3279672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 3280672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3281672550fbSChristian Borntraeger break; 3282afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3283afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 3284afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3285afa45ff5SChristian Borntraeger break; 328614eebd91SCarsten Otte default: 328714eebd91SCarsten Otte break; 328814eebd91SCarsten Otte } 328914eebd91SCarsten Otte 329014eebd91SCarsten Otte return r; 329114eebd91SCarsten Otte } 3292b6d33834SChristoffer Dall 3293b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 3294b0c632dbSHeiko Carstens { 3295b0c632dbSHeiko Carstens kvm_s390_vcpu_initial_reset(vcpu); 3296b0c632dbSHeiko Carstens return 0; 3297b0c632dbSHeiko Carstens } 3298b0c632dbSHeiko Carstens 3299b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3300b0c632dbSHeiko Carstens { 3301875656feSChristoffer Dall vcpu_load(vcpu); 33025a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 3303875656feSChristoffer Dall vcpu_put(vcpu); 3304b0c632dbSHeiko Carstens return 0; 3305b0c632dbSHeiko Carstens } 3306b0c632dbSHeiko Carstens 3307b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 3308b0c632dbSHeiko Carstens { 33091fc9b76bSChristoffer Dall vcpu_load(vcpu); 33105a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 33111fc9b76bSChristoffer Dall vcpu_put(vcpu); 3312b0c632dbSHeiko Carstens return 0; 3313b0c632dbSHeiko Carstens } 3314b0c632dbSHeiko Carstens 3315b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 3316b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3317b0c632dbSHeiko Carstens { 3318b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 3319b4ef9d4eSChristoffer Dall 332059674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 3321b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 3322b4ef9d4eSChristoffer Dall 3323b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 3324b0c632dbSHeiko Carstens return 0; 3325b0c632dbSHeiko Carstens } 3326b0c632dbSHeiko Carstens 3327b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 3328b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 3329b0c632dbSHeiko Carstens { 3330bcdec41cSChristoffer Dall vcpu_load(vcpu); 3331bcdec41cSChristoffer Dall 333259674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 3333b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 3334bcdec41cSChristoffer Dall 3335bcdec41cSChristoffer Dall vcpu_put(vcpu); 3336b0c632dbSHeiko Carstens return 0; 3337b0c632dbSHeiko Carstens } 3338b0c632dbSHeiko Carstens 3339b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3340b0c632dbSHeiko Carstens { 33416a96bc7fSChristoffer Dall int ret = 0; 33426a96bc7fSChristoffer Dall 33436a96bc7fSChristoffer Dall vcpu_load(vcpu); 33446a96bc7fSChristoffer Dall 33456a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 33466a96bc7fSChristoffer Dall ret = -EINVAL; 33476a96bc7fSChristoffer Dall goto out; 33486a96bc7fSChristoffer Dall } 3349e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 33509abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3351a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 3352a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 33539abc2a08SDavid Hildenbrand else 3354a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 33556a96bc7fSChristoffer Dall 33566a96bc7fSChristoffer Dall out: 33576a96bc7fSChristoffer Dall vcpu_put(vcpu); 33586a96bc7fSChristoffer Dall return ret; 3359b0c632dbSHeiko Carstens } 3360b0c632dbSHeiko Carstens 3361b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 3362b0c632dbSHeiko Carstens { 33631393123eSChristoffer Dall vcpu_load(vcpu); 33641393123eSChristoffer Dall 33659abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 33669abc2a08SDavid Hildenbrand save_fpu_regs(); 33679abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 3368a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 3369a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 33709abc2a08SDavid Hildenbrand else 3371a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 3372e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 33731393123eSChristoffer Dall 33741393123eSChristoffer Dall vcpu_put(vcpu); 3375b0c632dbSHeiko Carstens return 0; 3376b0c632dbSHeiko Carstens } 3377b0c632dbSHeiko Carstens 3378b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 3379b0c632dbSHeiko Carstens { 3380b0c632dbSHeiko Carstens int rc = 0; 3381b0c632dbSHeiko Carstens 33827a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 3383b0c632dbSHeiko Carstens rc = -EBUSY; 3384d7b0b5ebSCarsten Otte else { 3385d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 3386d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 3387d7b0b5ebSCarsten Otte } 3388b0c632dbSHeiko Carstens return rc; 3389b0c632dbSHeiko Carstens } 3390b0c632dbSHeiko Carstens 3391b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 3392b0c632dbSHeiko Carstens struct kvm_translation *tr) 3393b0c632dbSHeiko Carstens { 3394b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 3395b0c632dbSHeiko Carstens } 3396b0c632dbSHeiko Carstens 339727291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 339827291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 339927291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 340027291e21SDavid Hildenbrand 3401d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 3402d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 3403b0c632dbSHeiko Carstens { 340427291e21SDavid Hildenbrand int rc = 0; 340527291e21SDavid Hildenbrand 340666b56562SChristoffer Dall vcpu_load(vcpu); 340766b56562SChristoffer Dall 340827291e21SDavid Hildenbrand vcpu->guest_debug = 0; 340927291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 341027291e21SDavid Hildenbrand 341166b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 341266b56562SChristoffer Dall rc = -EINVAL; 341366b56562SChristoffer Dall goto out; 341466b56562SChristoffer Dall } 341566b56562SChristoffer Dall if (!sclp.has_gpere) { 341666b56562SChristoffer Dall rc = -EINVAL; 341766b56562SChristoffer Dall goto out; 341866b56562SChristoffer Dall } 341927291e21SDavid Hildenbrand 342027291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 342127291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 342227291e21SDavid Hildenbrand /* enforce guest PER */ 3423ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 342427291e21SDavid Hildenbrand 342527291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 342627291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 342727291e21SDavid Hildenbrand } else { 34289daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 342927291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 343027291e21SDavid Hildenbrand } 343127291e21SDavid Hildenbrand 343227291e21SDavid Hildenbrand if (rc) { 343327291e21SDavid Hildenbrand vcpu->guest_debug = 0; 343427291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 34359daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 343627291e21SDavid Hildenbrand } 343727291e21SDavid Hildenbrand 343866b56562SChristoffer Dall out: 343966b56562SChristoffer Dall vcpu_put(vcpu); 344027291e21SDavid Hildenbrand return rc; 3441b0c632dbSHeiko Carstens } 3442b0c632dbSHeiko Carstens 344362d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 344462d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 344562d9f0dbSMarcelo Tosatti { 3446fd232561SChristoffer Dall int ret; 3447fd232561SChristoffer Dall 3448fd232561SChristoffer Dall vcpu_load(vcpu); 3449fd232561SChristoffer Dall 34506352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 3451fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 34526352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 3453fd232561SChristoffer Dall 3454fd232561SChristoffer Dall vcpu_put(vcpu); 3455fd232561SChristoffer Dall return ret; 345662d9f0dbSMarcelo Tosatti } 345762d9f0dbSMarcelo Tosatti 345862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 345962d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 346062d9f0dbSMarcelo Tosatti { 34616352e4d2SDavid Hildenbrand int rc = 0; 34626352e4d2SDavid Hildenbrand 3463e83dff5eSChristoffer Dall vcpu_load(vcpu); 3464e83dff5eSChristoffer Dall 34656352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 34666352e4d2SDavid Hildenbrand vcpu->kvm->arch.user_cpu_state_ctrl = 1; 34676352e4d2SDavid Hildenbrand 34686352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 34696352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 34706352e4d2SDavid Hildenbrand kvm_s390_vcpu_stop(vcpu); 34716352e4d2SDavid Hildenbrand break; 34726352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 34736352e4d2SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 34746352e4d2SDavid Hildenbrand break; 34756352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 34766352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 34776352e4d2SDavid Hildenbrand /* fall through - CHECK_STOP and LOAD are not supported yet */ 34786352e4d2SDavid Hildenbrand default: 34796352e4d2SDavid Hildenbrand rc = -ENXIO; 34806352e4d2SDavid Hildenbrand } 34816352e4d2SDavid Hildenbrand 3482e83dff5eSChristoffer Dall vcpu_put(vcpu); 34836352e4d2SDavid Hildenbrand return rc; 348462d9f0dbSMarcelo Tosatti } 348562d9f0dbSMarcelo Tosatti 34868ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 34878ad35755SDavid Hildenbrand { 34888d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 34898ad35755SDavid Hildenbrand } 34908ad35755SDavid Hildenbrand 34912c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 34922c70fe44SChristian Borntraeger { 34938ad35755SDavid Hildenbrand retry: 34948e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 34952fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 3496586b7ccdSChristian Borntraeger return 0; 34972c70fe44SChristian Borntraeger /* 34982c70fe44SChristian Borntraeger * We use MMU_RELOAD just to re-arm the ipte notifier for the 3499b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 35002c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 35012c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 35022c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 35032c70fe44SChristian Borntraeger */ 35048ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { 35052c70fe44SChristian Borntraeger int rc; 3506b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 3507fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 3508b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 3509aca411a4SJulius Niedworok if (rc) { 3510aca411a4SJulius Niedworok kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); 35112c70fe44SChristian Borntraeger return rc; 3512aca411a4SJulius Niedworok } 35138ad35755SDavid Hildenbrand goto retry; 35142c70fe44SChristian Borntraeger } 35158ad35755SDavid Hildenbrand 3516d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 3517d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 3518d3d692c8SDavid Hildenbrand goto retry; 3519d3d692c8SDavid Hildenbrand } 3520d3d692c8SDavid Hildenbrand 35218ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 35228ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 35238ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 3524ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 35258ad35755SDavid Hildenbrand } 35268ad35755SDavid Hildenbrand goto retry; 35278ad35755SDavid Hildenbrand } 35288ad35755SDavid Hildenbrand 35298ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 35308ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 35318ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 35329daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 35338ad35755SDavid Hildenbrand } 35348ad35755SDavid Hildenbrand goto retry; 35358ad35755SDavid Hildenbrand } 35368ad35755SDavid Hildenbrand 35376502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 35386502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 35396502a34cSDavid Hildenbrand goto retry; 35406502a34cSDavid Hildenbrand } 35416502a34cSDavid Hildenbrand 3542190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 3543190df4a2SClaudio Imbrenda /* 3544c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 3545190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 3546190df4a2SClaudio Imbrenda * functionalities needed for live migration. 3547190df4a2SClaudio Imbrenda */ 3548190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 3549190df4a2SClaudio Imbrenda goto retry; 3550190df4a2SClaudio Imbrenda } 3551190df4a2SClaudio Imbrenda 3552190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 3553190df4a2SClaudio Imbrenda /* 3554c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 3555c9f0a2b8SJanosch Frank * CMM has been used. 3556190df4a2SClaudio Imbrenda */ 3557190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 3558c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 3559190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 3560190df4a2SClaudio Imbrenda goto retry; 3561190df4a2SClaudio Imbrenda } 3562190df4a2SClaudio Imbrenda 35630759d068SDavid Hildenbrand /* nothing to do, just clear the request */ 356472875d8aSRadim Krčmář kvm_clear_request(KVM_REQ_UNHALT, vcpu); 35653194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 35663194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 35670759d068SDavid Hildenbrand 35682c70fe44SChristian Borntraeger return 0; 35692c70fe44SChristian Borntraeger } 35702c70fe44SChristian Borntraeger 35710e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, 35728fa1696eSCollin L. Walling const struct kvm_s390_vm_tod_clock *gtod) 35738fa1696eSCollin L. Walling { 35748fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 35758fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext htod; 35768fa1696eSCollin L. Walling int i; 35778fa1696eSCollin L. Walling 35788fa1696eSCollin L. Walling mutex_lock(&kvm->lock); 35798fa1696eSCollin L. Walling preempt_disable(); 35808fa1696eSCollin L. Walling 35818fa1696eSCollin L. Walling get_tod_clock_ext((char *)&htod); 35828fa1696eSCollin L. Walling 35838fa1696eSCollin L. Walling kvm->arch.epoch = gtod->tod - htod.tod; 35840e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 35850e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 35868fa1696eSCollin L. Walling kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx; 35878fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 35888fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 35890e7def5fSDavid Hildenbrand } 35908fa1696eSCollin L. Walling 35918fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 35928fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 35938fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 35948fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 35958fa1696eSCollin L. Walling } 35968fa1696eSCollin L. Walling 35978fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 35988fa1696eSCollin L. Walling preempt_enable(); 35998fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 36008fa1696eSCollin L. Walling } 36018fa1696eSCollin L. Walling 3602fa576c58SThomas Huth /** 3603fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 3604fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 3605fa576c58SThomas Huth * @gpa: Guest physical address 3606fa576c58SThomas Huth * @writable: Whether the page should be writable or not 3607fa576c58SThomas Huth * 3608fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 3609fa576c58SThomas Huth * 3610fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 3611fa576c58SThomas Huth */ 3612fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 361324eb3a82SDominik Dingel { 3614527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 3615527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 361624eb3a82SDominik Dingel } 361724eb3a82SDominik Dingel 36183c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 36193c038e6bSDominik Dingel unsigned long token) 36203c038e6bSDominik Dingel { 36213c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 3622383d0b05SJens Freimann struct kvm_s390_irq irq; 36233c038e6bSDominik Dingel 36243c038e6bSDominik Dingel if (start_token) { 3625383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 3626383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 3627383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 36283c038e6bSDominik Dingel } else { 36293c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 3630383d0b05SJens Freimann inti.parm64 = token; 36313c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 36323c038e6bSDominik Dingel } 36333c038e6bSDominik Dingel } 36343c038e6bSDominik Dingel 36353c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 36363c038e6bSDominik Dingel struct kvm_async_pf *work) 36373c038e6bSDominik Dingel { 36383c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 36393c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 36403c038e6bSDominik Dingel } 36413c038e6bSDominik Dingel 36423c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 36433c038e6bSDominik Dingel struct kvm_async_pf *work) 36443c038e6bSDominik Dingel { 36453c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 36463c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 36473c038e6bSDominik Dingel } 36483c038e6bSDominik Dingel 36493c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 36503c038e6bSDominik Dingel struct kvm_async_pf *work) 36513c038e6bSDominik Dingel { 36523c038e6bSDominik Dingel /* s390 will always inject the page directly */ 36533c038e6bSDominik Dingel } 36543c038e6bSDominik Dingel 36553c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) 36563c038e6bSDominik Dingel { 36573c038e6bSDominik Dingel /* 36583c038e6bSDominik Dingel * s390 will always inject the page directly, 36593c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 36603c038e6bSDominik Dingel */ 36613c038e6bSDominik Dingel return true; 36623c038e6bSDominik Dingel } 36633c038e6bSDominik Dingel 36643c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 36653c038e6bSDominik Dingel { 36663c038e6bSDominik Dingel hva_t hva; 36673c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 36683c038e6bSDominik Dingel int rc; 36693c038e6bSDominik Dingel 36703c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 36713c038e6bSDominik Dingel return 0; 36723c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 36733c038e6bSDominik Dingel vcpu->arch.pfault_compare) 36743c038e6bSDominik Dingel return 0; 36753c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 36763c038e6bSDominik Dingel return 0; 36779a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 36783c038e6bSDominik Dingel return 0; 3679b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 36803c038e6bSDominik Dingel return 0; 36813c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 36823c038e6bSDominik Dingel return 0; 36833c038e6bSDominik Dingel 368481480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 368581480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 368681480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 36873c038e6bSDominik Dingel return 0; 36883c038e6bSDominik Dingel 36893c038e6bSDominik Dingel rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 36903c038e6bSDominik Dingel return rc; 36913c038e6bSDominik Dingel } 36923c038e6bSDominik Dingel 36933fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 3694b0c632dbSHeiko Carstens { 36953fb4c40fSThomas Huth int rc, cpuflags; 3696e168bf8dSCarsten Otte 36973c038e6bSDominik Dingel /* 36983c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 36993c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 37003c038e6bSDominik Dingel * handled outside the worker. 37013c038e6bSDominik Dingel */ 37023c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 37033c038e6bSDominik Dingel 37047ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 37057ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 3706b0c632dbSHeiko Carstens 3707b0c632dbSHeiko Carstens if (need_resched()) 3708b0c632dbSHeiko Carstens schedule(); 3709b0c632dbSHeiko Carstens 3710d3a73acbSMartin Schwidefsky if (test_cpu_flag(CIF_MCCK_PENDING)) 371171cde587SChristian Borntraeger s390_handle_mcck(); 371271cde587SChristian Borntraeger 371379395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 371479395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 371579395031SJens Freimann if (rc) 371679395031SJens Freimann return rc; 371779395031SJens Freimann } 37180ff31867SCarsten Otte 37192c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 37202c70fe44SChristian Borntraeger if (rc) 37212c70fe44SChristian Borntraeger return rc; 37222c70fe44SChristian Borntraeger 372327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 372427291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 372527291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 372627291e21SDavid Hildenbrand } 372727291e21SDavid Hildenbrand 37289f30f621SMichael Mueller clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask); 37299f30f621SMichael Mueller 3730b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 37313fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 37323fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 37333fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 37342b29a9fdSDominik Dingel 37353fb4c40fSThomas Huth return 0; 37363fb4c40fSThomas Huth } 37373fb4c40fSThomas Huth 3738492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 3739492d8642SThomas Huth { 374056317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 374156317920SDavid Hildenbrand .code = PGM_ADDRESSING, 374256317920SDavid Hildenbrand }; 374356317920SDavid Hildenbrand u8 opcode, ilen; 3744492d8642SThomas Huth int rc; 3745492d8642SThomas Huth 3746492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 3747492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 3748492d8642SThomas Huth 3749492d8642SThomas Huth /* 3750492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 3751492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 3752492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 3753492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 3754492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 3755492d8642SThomas Huth * to be able to forward the PSW. 3756492d8642SThomas Huth */ 37573fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 375856317920SDavid Hildenbrand ilen = insn_length(opcode); 37599b0d721aSDavid Hildenbrand if (rc < 0) { 37609b0d721aSDavid Hildenbrand return rc; 37619b0d721aSDavid Hildenbrand } else if (rc) { 37629b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 37639b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 37649b0d721aSDavid Hildenbrand * nullification if necessary. 37659b0d721aSDavid Hildenbrand */ 37669b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 37679b0d721aSDavid Hildenbrand ilen = 4; 37689b0d721aSDavid Hildenbrand } 376956317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 377056317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 377156317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 3772492d8642SThomas Huth } 3773492d8642SThomas Huth 37743fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 37753fb4c40fSThomas Huth { 37764d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 37774d62fcc0SQingFeng Hao struct sie_page *sie_page; 37784d62fcc0SQingFeng Hao 37792b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 37802b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 37812b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 37822b29a9fdSDominik Dingel 378327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 378427291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 378527291e21SDavid Hildenbrand 37867ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 37877ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 378871f116bfSDavid Hildenbrand 37894d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 37904d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 37914d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 37924d62fcc0SQingFeng Hao struct sie_page, sie_block); 37934d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 37944d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 37954d62fcc0SQingFeng Hao return 0; 37964d62fcc0SQingFeng Hao } 37974d62fcc0SQingFeng Hao 379871f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 379971f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 380071f116bfSDavid Hildenbrand 380171f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 380271f116bfSDavid Hildenbrand return rc; 380371f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 380471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 380571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 380671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 380771f116bfSDavid Hildenbrand return -EREMOTE; 380871f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 380971f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 381071f116bfSDavid Hildenbrand return 0; 3811210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 3812210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 3813210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 3814210b1607SThomas Huth current->thread.gmap_addr; 3815210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 381671f116bfSDavid Hildenbrand return -EREMOTE; 381724eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 38183c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 381924eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 382071f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 382171f116bfSDavid Hildenbrand return 0; 382271f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 3823fa576c58SThomas Huth } 382471f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 38253fb4c40fSThomas Huth } 38263fb4c40fSThomas Huth 38273fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 38283fb4c40fSThomas Huth { 38293fb4c40fSThomas Huth int rc, exit_reason; 38303fb4c40fSThomas Huth 3831800c1065SThomas Huth /* 3832800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 3833800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 3834800c1065SThomas Huth */ 3835800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 3836800c1065SThomas Huth 3837a76ccff6SThomas Huth do { 38383fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 38393fb4c40fSThomas Huth if (rc) 3840a76ccff6SThomas Huth break; 38413fb4c40fSThomas Huth 3842800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 38433fb4c40fSThomas Huth /* 3844a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 3845a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 38463fb4c40fSThomas Huth */ 38470097d12eSChristian Borntraeger local_irq_disable(); 38486edaa530SPaolo Bonzini guest_enter_irqoff(); 3849db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 38500097d12eSChristian Borntraeger local_irq_enable(); 3851a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 3852a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 38530097d12eSChristian Borntraeger local_irq_disable(); 3854db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 38556edaa530SPaolo Bonzini guest_exit_irqoff(); 38560097d12eSChristian Borntraeger local_irq_enable(); 3857800c1065SThomas Huth vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 38583fb4c40fSThomas Huth 38593fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 386027291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 38613fb4c40fSThomas Huth 3862800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 3863e168bf8dSCarsten Otte return rc; 3864b0c632dbSHeiko Carstens } 3865b0c632dbSHeiko Carstens 3866b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3867b028ee3eSDavid Hildenbrand { 38684d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 38694e0b1ab7SFan Zhang struct gs_cb *gscb; 38704d5f2c04SChristian Borntraeger 38714d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 38724e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 3873b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 3874b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 3875b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 3876b028ee3eSDavid Hildenbrand kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 3877b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 3878b028ee3eSDavid Hildenbrand memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 3879d3d692c8SDavid Hildenbrand /* some control register changes require a tlb flush */ 3880d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 3881b028ee3eSDavid Hildenbrand } 3882b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 38834287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 3884b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 3885b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 3886b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 3887b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 3888b028ee3eSDavid Hildenbrand } 3889b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 3890b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 3891b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 3892b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 38939fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 38949fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 3895b028ee3eSDavid Hildenbrand } 389680cd8763SFan Zhang /* 389780cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 389880cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 389980cd8763SFan Zhang */ 390080cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 39014d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 3902bb59c2daSAlice Frosi riccb->v && 39030c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 39044d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 39050c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 390680cd8763SFan Zhang } 39074e0b1ab7SFan Zhang /* 39084e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 39094e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 39104e0b1ab7SFan Zhang */ 39114e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 39124e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 39134e0b1ab7SFan Zhang gscb->gssm && 39144e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 39154e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 39164e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 39174e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 39184e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 391980cd8763SFan Zhang } 392035b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 392135b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 392235b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 392335b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 392435b3fde6SChristian Borntraeger } 392531d8b8d4SChristian Borntraeger save_access_regs(vcpu->arch.host_acrs); 392631d8b8d4SChristian Borntraeger restore_access_regs(vcpu->run->s.regs.acrs); 3927e1788bb9SChristian Borntraeger /* save host (userspace) fprs/vrs */ 3928e1788bb9SChristian Borntraeger save_fpu_regs(); 3929e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 3930e1788bb9SChristian Borntraeger vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 3931e1788bb9SChristian Borntraeger if (MACHINE_HAS_VX) 3932e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.vrs; 3933e1788bb9SChristian Borntraeger else 3934e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->run->s.regs.fprs; 3935e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 3936e1788bb9SChristian Borntraeger if (test_fp_ctl(current->thread.fpu.fpc)) 3937e1788bb9SChristian Borntraeger /* User space provided an invalid FPC, let's clear it */ 3938e1788bb9SChristian Borntraeger current->thread.fpu.fpc = 0; 39394e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 39404e0b1ab7SFan Zhang preempt_disable(); 39414e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 39424e0b1ab7SFan Zhang if (current->thread.gs_cb) { 39434e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 39444e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 39454e0b1ab7SFan Zhang } 39464e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 39474e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 39484e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 39494e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 39504e0b1ab7SFan Zhang } 39514e0b1ab7SFan Zhang preempt_enable(); 39524e0b1ab7SFan Zhang } 3953a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 395480cd8763SFan Zhang 3955b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 3956b028ee3eSDavid Hildenbrand } 3957b028ee3eSDavid Hildenbrand 3958b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3959b028ee3eSDavid Hildenbrand { 3960b028ee3eSDavid Hildenbrand kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 3961b028ee3eSDavid Hildenbrand kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 3962b028ee3eSDavid Hildenbrand kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 3963b028ee3eSDavid Hildenbrand memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 39644287f247SDavid Hildenbrand kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 3965b028ee3eSDavid Hildenbrand kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 3966b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 3967b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 3968b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 3969b028ee3eSDavid Hildenbrand kvm_run->s.regs.pft = vcpu->arch.pfault_token; 3970b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 3971b028ee3eSDavid Hildenbrand kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 397235b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 397331d8b8d4SChristian Borntraeger save_access_regs(vcpu->run->s.regs.acrs); 397431d8b8d4SChristian Borntraeger restore_access_regs(vcpu->arch.host_acrs); 3975e1788bb9SChristian Borntraeger /* Save guest register state */ 3976e1788bb9SChristian Borntraeger save_fpu_regs(); 3977e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 3978e1788bb9SChristian Borntraeger /* Restore will be done lazily at return */ 3979e1788bb9SChristian Borntraeger current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 3980e1788bb9SChristian Borntraeger current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 39814e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 39824e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 39834e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 39844e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 39854e0b1ab7SFan Zhang preempt_disable(); 39864e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 39874e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 39884e0b1ab7SFan Zhang preempt_enable(); 39894e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 39904e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 39914e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 39924e0b1ab7SFan Zhang } 3993a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 3994b028ee3eSDavid Hildenbrand } 3995b028ee3eSDavid Hildenbrand 3996b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) 3997b0c632dbSHeiko Carstens { 39988f2abe6aSChristian Borntraeger int rc; 3999b0c632dbSHeiko Carstens 4000460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4001460df4c1SPaolo Bonzini return -EINTR; 4002460df4c1SPaolo Bonzini 4003200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4004200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4005200824f5SThomas Huth return -EINVAL; 4006200824f5SThomas Huth 4007accb757dSChristoffer Dall vcpu_load(vcpu); 4008accb757dSChristoffer Dall 400927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 401027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4011accb757dSChristoffer Dall rc = 0; 4012accb757dSChristoffer Dall goto out; 401327291e21SDavid Hildenbrand } 401427291e21SDavid Hildenbrand 401520b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4016b0c632dbSHeiko Carstens 40176352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 40186852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 40196352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4020ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 40216352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4022accb757dSChristoffer Dall rc = -EINVAL; 4023accb757dSChristoffer Dall goto out; 40246352e4d2SDavid Hildenbrand } 4025b0c632dbSHeiko Carstens 4026b028ee3eSDavid Hildenbrand sync_regs(vcpu, kvm_run); 4027db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4028d7b0b5ebSCarsten Otte 4029dab4079dSHeiko Carstens might_fault(); 4030e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 40319ace903dSChristian Ehrhardt 4032b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4033b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 40348f2abe6aSChristian Borntraeger rc = -EINTR; 4035b1d16c49SChristian Ehrhardt } 40368f2abe6aSChristian Borntraeger 403727291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 403827291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 403927291e21SDavid Hildenbrand rc = 0; 404027291e21SDavid Hildenbrand } 404127291e21SDavid Hildenbrand 40428f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 404371f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 40448f2abe6aSChristian Borntraeger rc = 0; 40458f2abe6aSChristian Borntraeger } 40468f2abe6aSChristian Borntraeger 4047db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 4048b028ee3eSDavid Hildenbrand store_regs(vcpu, kvm_run); 4049d7b0b5ebSCarsten Otte 405020b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4051b0c632dbSHeiko Carstens 4052b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4053accb757dSChristoffer Dall out: 4054accb757dSChristoffer Dall vcpu_put(vcpu); 40557e8e6ab4SHeiko Carstens return rc; 4056b0c632dbSHeiko Carstens } 4057b0c632dbSHeiko Carstens 4058b0c632dbSHeiko Carstens /* 4059b0c632dbSHeiko Carstens * store status at address 4060b0c632dbSHeiko Carstens * we use have two special cases: 4061b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4062b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4063b0c632dbSHeiko Carstens */ 4064d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4065b0c632dbSHeiko Carstens { 4066092670cdSCarsten Otte unsigned char archmode = 1; 40679abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4068fda902cbSMichael Mueller unsigned int px; 40694287f247SDavid Hildenbrand u64 clkcomp, cputm; 4070d0bce605SHeiko Carstens int rc; 4071b0c632dbSHeiko Carstens 4072d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4073d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4074d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4075b0c632dbSHeiko Carstens return -EFAULT; 4076d9a3a09aSMartin Schwidefsky gpa = 0; 4077d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4078d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4079b0c632dbSHeiko Carstens return -EFAULT; 4080d9a3a09aSMartin Schwidefsky gpa = px; 4081d9a3a09aSMartin Schwidefsky } else 4082d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 40839abc2a08SDavid Hildenbrand 40849abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 40859abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 40869522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4087d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 40889abc2a08SDavid Hildenbrand fprs, 128); 40899abc2a08SDavid Hildenbrand } else { 40909abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 40916fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 40929abc2a08SDavid Hildenbrand } 4093d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 4094d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 4095d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 4096d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 4097d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 4098fda902cbSMichael Mueller &px, 4); 4099d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 41009abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 4101d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 4102d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 41034287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 4104d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 41054287f247SDavid Hildenbrand &cputm, 8); 4106178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 4107d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 4108d0bce605SHeiko Carstens &clkcomp, 8); 4109d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 4110d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 4111d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 4112d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 4113d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 4114b0c632dbSHeiko Carstens } 4115b0c632dbSHeiko Carstens 4116e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 4117e879892cSThomas Huth { 4118e879892cSThomas Huth /* 4119e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 412031d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 4121e879892cSThomas Huth * it into the save area 4122e879892cSThomas Huth */ 4123d0164ee2SHendrik Brueckner save_fpu_regs(); 41249abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4125e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 4126e879892cSThomas Huth 4127e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 4128e879892cSThomas Huth } 4129e879892cSThomas Huth 41308ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 41318ad35755SDavid Hildenbrand { 41328ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 41338e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 41348ad35755SDavid Hildenbrand } 41358ad35755SDavid Hildenbrand 41368ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 41378ad35755SDavid Hildenbrand { 41388ad35755SDavid Hildenbrand unsigned int i; 41398ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 41408ad35755SDavid Hildenbrand 41418ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 41428ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 41438ad35755SDavid Hildenbrand } 41448ad35755SDavid Hildenbrand } 41458ad35755SDavid Hildenbrand 41468ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 41478ad35755SDavid Hildenbrand { 414809a400e7SDavid Hildenbrand if (!sclp.has_ibs) 414909a400e7SDavid Hildenbrand return; 41508ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 41518e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 41528ad35755SDavid Hildenbrand } 41538ad35755SDavid Hildenbrand 41546852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 41556852d7b6SDavid Hildenbrand { 41568ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 41578ad35755SDavid Hildenbrand 41588ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 41598ad35755SDavid Hildenbrand return; 41608ad35755SDavid Hildenbrand 41616852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 41628ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4163433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 41648ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 41658ad35755SDavid Hildenbrand 41668ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 41678ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) 41688ad35755SDavid Hildenbrand started_vcpus++; 41698ad35755SDavid Hildenbrand } 41708ad35755SDavid Hildenbrand 41718ad35755SDavid Hildenbrand if (started_vcpus == 0) { 41728ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 41738ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 41748ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 41758ad35755SDavid Hildenbrand /* 41768ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 41778ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 41788ad35755SDavid Hildenbrand * oustanding ENABLE requests. 41798ad35755SDavid Hildenbrand */ 41808ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 41818ad35755SDavid Hildenbrand } 41828ad35755SDavid Hildenbrand 41839daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 41848ad35755SDavid Hildenbrand /* 41858ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 41868ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 41878ad35755SDavid Hildenbrand */ 4188d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4189433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 41908ad35755SDavid Hildenbrand return; 41916852d7b6SDavid Hildenbrand } 41926852d7b6SDavid Hildenbrand 41936852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 41946852d7b6SDavid Hildenbrand { 41958ad35755SDavid Hildenbrand int i, online_vcpus, started_vcpus = 0; 41968ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 41978ad35755SDavid Hildenbrand 41988ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 41998ad35755SDavid Hildenbrand return; 42008ad35755SDavid Hildenbrand 42016852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 42028ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 4203433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 42048ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 42058ad35755SDavid Hildenbrand 420632f5ff63SDavid Hildenbrand /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ 42076cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 420832f5ff63SDavid Hildenbrand 4209ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 42108ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 42118ad35755SDavid Hildenbrand 42128ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 42138ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { 42148ad35755SDavid Hildenbrand started_vcpus++; 42158ad35755SDavid Hildenbrand started_vcpu = vcpu->kvm->vcpus[i]; 42168ad35755SDavid Hildenbrand } 42178ad35755SDavid Hildenbrand } 42188ad35755SDavid Hildenbrand 42198ad35755SDavid Hildenbrand if (started_vcpus == 1) { 42208ad35755SDavid Hildenbrand /* 42218ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 42228ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 42238ad35755SDavid Hildenbrand */ 42248ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 42258ad35755SDavid Hildenbrand } 42268ad35755SDavid Hildenbrand 4227433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 42288ad35755SDavid Hildenbrand return; 42296852d7b6SDavid Hildenbrand } 42306852d7b6SDavid Hildenbrand 4231d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 4232d6712df9SCornelia Huck struct kvm_enable_cap *cap) 4233d6712df9SCornelia Huck { 4234d6712df9SCornelia Huck int r; 4235d6712df9SCornelia Huck 4236d6712df9SCornelia Huck if (cap->flags) 4237d6712df9SCornelia Huck return -EINVAL; 4238d6712df9SCornelia Huck 4239d6712df9SCornelia Huck switch (cap->cap) { 4240fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 4241fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 4242fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 4243c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 4244fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 4245fa6b7fe9SCornelia Huck } 4246fa6b7fe9SCornelia Huck r = 0; 4247fa6b7fe9SCornelia Huck break; 4248d6712df9SCornelia Huck default: 4249d6712df9SCornelia Huck r = -EINVAL; 4250d6712df9SCornelia Huck break; 4251d6712df9SCornelia Huck } 4252d6712df9SCornelia Huck return r; 4253d6712df9SCornelia Huck } 4254d6712df9SCornelia Huck 425541408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, 425641408c28SThomas Huth struct kvm_s390_mem_op *mop) 425741408c28SThomas Huth { 425841408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 425941408c28SThomas Huth void *tmpbuf = NULL; 426041408c28SThomas Huth int r, srcu_idx; 426141408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 426241408c28SThomas Huth | KVM_S390_MEMOP_F_CHECK_ONLY; 426341408c28SThomas Huth 4264a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 426541408c28SThomas Huth return -EINVAL; 426641408c28SThomas Huth 426741408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 426841408c28SThomas Huth return -E2BIG; 426941408c28SThomas Huth 427041408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 427141408c28SThomas Huth tmpbuf = vmalloc(mop->size); 427241408c28SThomas Huth if (!tmpbuf) 427341408c28SThomas Huth return -ENOMEM; 427441408c28SThomas Huth } 427541408c28SThomas Huth 427641408c28SThomas Huth srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 427741408c28SThomas Huth 427841408c28SThomas Huth switch (mop->op) { 427941408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 428041408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 428192c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 428292c96321SDavid Hildenbrand mop->size, GACC_FETCH); 428341408c28SThomas Huth break; 428441408c28SThomas Huth } 428541408c28SThomas Huth r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 428641408c28SThomas Huth if (r == 0) { 428741408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 428841408c28SThomas Huth r = -EFAULT; 428941408c28SThomas Huth } 429041408c28SThomas Huth break; 429141408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 429241408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 429392c96321SDavid Hildenbrand r = check_gva_range(vcpu, mop->gaddr, mop->ar, 429492c96321SDavid Hildenbrand mop->size, GACC_STORE); 429541408c28SThomas Huth break; 429641408c28SThomas Huth } 429741408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 429841408c28SThomas Huth r = -EFAULT; 429941408c28SThomas Huth break; 430041408c28SThomas Huth } 430141408c28SThomas Huth r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); 430241408c28SThomas Huth break; 430341408c28SThomas Huth default: 430441408c28SThomas Huth r = -EINVAL; 430541408c28SThomas Huth } 430641408c28SThomas Huth 430741408c28SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 430841408c28SThomas Huth 430941408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 431041408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 431141408c28SThomas Huth 431241408c28SThomas Huth vfree(tmpbuf); 431341408c28SThomas Huth return r; 431441408c28SThomas Huth } 431541408c28SThomas Huth 43165cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 4317b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 4318b0c632dbSHeiko Carstens { 4319b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 4320b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 4321b0c632dbSHeiko Carstens 432293736624SAvi Kivity switch (ioctl) { 432347b43c52SJens Freimann case KVM_S390_IRQ: { 432447b43c52SJens Freimann struct kvm_s390_irq s390irq; 432547b43c52SJens Freimann 432647b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 43279b062471SChristoffer Dall return -EFAULT; 43289b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 432947b43c52SJens Freimann } 433093736624SAvi Kivity case KVM_S390_INTERRUPT: { 4331ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 433253936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 4333ba5c1e9bSCarsten Otte 4334ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 43359b062471SChristoffer Dall return -EFAULT; 4336383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 4337383d0b05SJens Freimann return -EINVAL; 43389b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 4339ba5c1e9bSCarsten Otte } 43409b062471SChristoffer Dall } 43415cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 43425cb0944cSPaolo Bonzini } 43435cb0944cSPaolo Bonzini 43445cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 43455cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 43465cb0944cSPaolo Bonzini { 43475cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 43485cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 43495cb0944cSPaolo Bonzini int idx; 43505cb0944cSPaolo Bonzini long r; 43519b062471SChristoffer Dall 43529b062471SChristoffer Dall vcpu_load(vcpu); 43539b062471SChristoffer Dall 43549b062471SChristoffer Dall switch (ioctl) { 4355b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 4356800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 4357bc923cc9SAvi Kivity r = kvm_s390_vcpu_store_status(vcpu, arg); 4358800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 4359bc923cc9SAvi Kivity break; 4360b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 4361b0c632dbSHeiko Carstens psw_t psw; 4362b0c632dbSHeiko Carstens 4363bc923cc9SAvi Kivity r = -EFAULT; 4364b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 4365bc923cc9SAvi Kivity break; 4366bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 4367bc923cc9SAvi Kivity break; 4368b0c632dbSHeiko Carstens } 4369b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 4370bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); 4371bc923cc9SAvi Kivity break; 437214eebd91SCarsten Otte case KVM_SET_ONE_REG: 437314eebd91SCarsten Otte case KVM_GET_ONE_REG: { 437414eebd91SCarsten Otte struct kvm_one_reg reg; 437514eebd91SCarsten Otte r = -EFAULT; 437614eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 437714eebd91SCarsten Otte break; 437814eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 437914eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 438014eebd91SCarsten Otte else 438114eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 438214eebd91SCarsten Otte break; 438314eebd91SCarsten Otte } 438427e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 438527e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 438627e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 438727e0393fSCarsten Otte 438827e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 438927e0393fSCarsten Otte r = -EFAULT; 439027e0393fSCarsten Otte break; 439127e0393fSCarsten Otte } 439227e0393fSCarsten Otte 439327e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 439427e0393fSCarsten Otte r = -EINVAL; 439527e0393fSCarsten Otte break; 439627e0393fSCarsten Otte } 439727e0393fSCarsten Otte 439827e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 439927e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 440027e0393fSCarsten Otte break; 440127e0393fSCarsten Otte } 440227e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 440327e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 440427e0393fSCarsten Otte 440527e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 440627e0393fSCarsten Otte r = -EFAULT; 440727e0393fSCarsten Otte break; 440827e0393fSCarsten Otte } 440927e0393fSCarsten Otte 441027e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 441127e0393fSCarsten Otte r = -EINVAL; 441227e0393fSCarsten Otte break; 441327e0393fSCarsten Otte } 441427e0393fSCarsten Otte 441527e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 441627e0393fSCarsten Otte ucasmap.length); 441727e0393fSCarsten Otte break; 441827e0393fSCarsten Otte } 441927e0393fSCarsten Otte #endif 4420ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 4421527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 4422ccc7910fSCarsten Otte break; 4423ccc7910fSCarsten Otte } 4424d6712df9SCornelia Huck case KVM_ENABLE_CAP: 4425d6712df9SCornelia Huck { 4426d6712df9SCornelia Huck struct kvm_enable_cap cap; 4427d6712df9SCornelia Huck r = -EFAULT; 4428d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 4429d6712df9SCornelia Huck break; 4430d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 4431d6712df9SCornelia Huck break; 4432d6712df9SCornelia Huck } 443341408c28SThomas Huth case KVM_S390_MEM_OP: { 443441408c28SThomas Huth struct kvm_s390_mem_op mem_op; 443541408c28SThomas Huth 443641408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 443741408c28SThomas Huth r = kvm_s390_guest_mem_op(vcpu, &mem_op); 443841408c28SThomas Huth else 443941408c28SThomas Huth r = -EFAULT; 444041408c28SThomas Huth break; 444141408c28SThomas Huth } 4442816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 4443816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4444816c7667SJens Freimann 4445816c7667SJens Freimann r = -EFAULT; 4446816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4447816c7667SJens Freimann break; 4448816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 4449816c7667SJens Freimann irq_state.len == 0 || 4450816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 4451816c7667SJens Freimann r = -EINVAL; 4452816c7667SJens Freimann break; 4453816c7667SJens Freimann } 4454bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4455816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 4456816c7667SJens Freimann (void __user *) irq_state.buf, 4457816c7667SJens Freimann irq_state.len); 4458816c7667SJens Freimann break; 4459816c7667SJens Freimann } 4460816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 4461816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 4462816c7667SJens Freimann 4463816c7667SJens Freimann r = -EFAULT; 4464816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 4465816c7667SJens Freimann break; 4466816c7667SJens Freimann if (irq_state.len == 0) { 4467816c7667SJens Freimann r = -EINVAL; 4468816c7667SJens Freimann break; 4469816c7667SJens Freimann } 4470bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 4471816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 4472816c7667SJens Freimann (__u8 __user *) irq_state.buf, 4473816c7667SJens Freimann irq_state.len); 4474816c7667SJens Freimann break; 4475816c7667SJens Freimann } 4476b0c632dbSHeiko Carstens default: 44773e6afcf1SCarsten Otte r = -ENOTTY; 4478b0c632dbSHeiko Carstens } 44799b062471SChristoffer Dall 44809b062471SChristoffer Dall vcpu_put(vcpu); 4481bc923cc9SAvi Kivity return r; 4482b0c632dbSHeiko Carstens } 4483b0c632dbSHeiko Carstens 44841499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 44855b1c1493SCarsten Otte { 44865b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 44875b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 44885b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 44895b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 44905b1c1493SCarsten Otte get_page(vmf->page); 44915b1c1493SCarsten Otte return 0; 44925b1c1493SCarsten Otte } 44935b1c1493SCarsten Otte #endif 44945b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 44955b1c1493SCarsten Otte } 44965b1c1493SCarsten Otte 44975587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, 44985587027cSAneesh Kumar K.V unsigned long npages) 4499db3fe4ebSTakuya Yoshikawa { 4500db3fe4ebSTakuya Yoshikawa return 0; 4501db3fe4ebSTakuya Yoshikawa } 4502db3fe4ebSTakuya Yoshikawa 4503b0c632dbSHeiko Carstens /* Section: memory related */ 4504f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 4505f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 450609170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 45077b6195a9STakuya Yoshikawa enum kvm_mr_change change) 4508b0c632dbSHeiko Carstens { 4509dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 4510dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 4511dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 4512dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 4513b0c632dbSHeiko Carstens 4514598841caSCarsten Otte if (mem->userspace_addr & 0xffffful) 4515b0c632dbSHeiko Carstens return -EINVAL; 4516b0c632dbSHeiko Carstens 4517598841caSCarsten Otte if (mem->memory_size & 0xffffful) 4518b0c632dbSHeiko Carstens return -EINVAL; 4519b0c632dbSHeiko Carstens 4520a3a92c31SDominik Dingel if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) 4521a3a92c31SDominik Dingel return -EINVAL; 4522a3a92c31SDominik Dingel 4523f7784b8eSMarcelo Tosatti return 0; 4524f7784b8eSMarcelo Tosatti } 4525f7784b8eSMarcelo Tosatti 4526f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 452709170a49SPaolo Bonzini const struct kvm_userspace_memory_region *mem, 45288482644aSTakuya Yoshikawa const struct kvm_memory_slot *old, 4529f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 45308482644aSTakuya Yoshikawa enum kvm_mr_change change) 4531f7784b8eSMarcelo Tosatti { 453219ec166cSChristian Borntraeger int rc = 0; 4533f7784b8eSMarcelo Tosatti 453419ec166cSChristian Borntraeger switch (change) { 453519ec166cSChristian Borntraeger case KVM_MR_DELETE: 453619ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 453719ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 453819ec166cSChristian Borntraeger break; 453919ec166cSChristian Borntraeger case KVM_MR_MOVE: 454019ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 454119ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 454219ec166cSChristian Borntraeger if (rc) 454319ec166cSChristian Borntraeger break; 454419ec166cSChristian Borntraeger /* FALLTHROUGH */ 454519ec166cSChristian Borntraeger case KVM_MR_CREATE: 4546598841caSCarsten Otte rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, 4547598841caSCarsten Otte mem->guest_phys_addr, mem->memory_size); 454819ec166cSChristian Borntraeger break; 454919ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 455019ec166cSChristian Borntraeger break; 455119ec166cSChristian Borntraeger default: 455219ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 455319ec166cSChristian Borntraeger } 4554598841caSCarsten Otte if (rc) 4555ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 4556598841caSCarsten Otte return; 4557b0c632dbSHeiko Carstens } 4558b0c632dbSHeiko Carstens 455960a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 456060a37709SAlexander Yarygin { 456160a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 456260a37709SAlexander Yarygin 456360a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 456460a37709SAlexander Yarygin } 456560a37709SAlexander Yarygin 45663491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) 45673491caf2SChristian Borntraeger { 45683491caf2SChristian Borntraeger vcpu->valid_wakeup = false; 45693491caf2SChristian Borntraeger } 45703491caf2SChristian Borntraeger 4571b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 4572b0c632dbSHeiko Carstens { 457360a37709SAlexander Yarygin int i; 457460a37709SAlexander Yarygin 457507197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 45768d43d570SMichael Mueller pr_info("SIE is not available\n"); 457707197fd0SDavid Hildenbrand return -ENODEV; 457807197fd0SDavid Hildenbrand } 457907197fd0SDavid Hildenbrand 4580a4499382SJanosch Frank if (nested && hpage) { 45818d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 4582a4499382SJanosch Frank return -EINVAL; 4583a4499382SJanosch Frank } 4584a4499382SJanosch Frank 458560a37709SAlexander Yarygin for (i = 0; i < 16; i++) 4586c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 458760a37709SAlexander Yarygin S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); 458860a37709SAlexander Yarygin 45899d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 4590b0c632dbSHeiko Carstens } 4591b0c632dbSHeiko Carstens 4592b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 4593b0c632dbSHeiko Carstens { 4594b0c632dbSHeiko Carstens kvm_exit(); 4595b0c632dbSHeiko Carstens } 4596b0c632dbSHeiko Carstens 4597b0c632dbSHeiko Carstens module_init(kvm_s390_init); 4598b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 4599566af940SCornelia Huck 4600566af940SCornelia Huck /* 4601566af940SCornelia Huck * Enable autoloading of the kvm module. 4602566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 4603566af940SCornelia Huck * since x86 takes a different approach. 4604566af940SCornelia Huck */ 4605566af940SCornelia Huck #include <linux/miscdevice.h> 4606566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 4607566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 4608