Greg Kroah-Hartman | d809aa2 | 2017-11-24 15:00:33 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2 | /* |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 3 | * hosting IBM Z kernel virtual machines (s390x) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4 | * |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 5 | * Copyright IBM Corp. 2008, 2020 |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 8 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 9 | * Heiko Carstens <heiko.carstens@de.ibm.com> |
Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 10 | * Christian Ehrhardt <ehrhardt@de.ibm.com> |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 11 | * Jason J. Herne <jjherne@us.ibm.com> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
Michael Mueller | 7aedd9d | 2018-12-03 10:20:22 +0100 | [diff] [blame] | 14 | #define KMSG_COMPONENT "kvm-s390" |
| 15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 16 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 17 | #include <linux/compiler.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/fs.h> |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 20 | #include <linux/hrtimer.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/kvm.h> |
| 23 | #include <linux/kvm_host.h> |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 24 | #include <linux/mman.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 25 | #include <linux/module.h> |
Paul Gortmaker | d321796 | 2017-02-09 15:20:25 -0500 | [diff] [blame] | 26 | #include <linux/moduleparam.h> |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 27 | #include <linux/random.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 28 | #include <linux/slab.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 29 | #include <linux/timer.h> |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 31 | #include <linux/bitmap.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 33 | #include <linux/string.h> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 34 | #include <linux/pgtable.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 35 | |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 36 | #include <asm/asm-offsets.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 37 | #include <asm/lowcore.h> |
Martin Schwidefsky | fd5ada0 | 2016-05-31 15:06:51 +0200 | [diff] [blame] | 38 | #include <asm/stp.h> |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 39 | #include <asm/gmap.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 40 | #include <asm/nmi.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 41 | #include <asm/switch_to.h> |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 42 | #include <asm/isc.h> |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 43 | #include <asm/sclp.h> |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 44 | #include <asm/cpacf.h> |
Linus Torvalds | 221bb8a | 2016-08-02 16:11:27 -0400 | [diff] [blame] | 45 | #include <asm/timex.h> |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 46 | #include <asm/ap.h> |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 47 | #include <asm/uv.h> |
Sven Schnelle | 56e62a7 | 2020-11-21 11:14:56 +0100 | [diff] [blame] | 48 | #include <asm/fpu/api.h> |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 49 | #include "kvm-s390.h" |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 50 | #include "gaccess.h" |
| 51 | |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 52 | #define CREATE_TRACE_POINTS |
| 53 | #include "trace.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 54 | #include "trace-s390.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 55 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 56 | #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 57 | #define LOCAL_IRQS 32 |
| 58 | #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ |
| 59 | (KVM_MAX_VCPUS + LOCAL_IRQS)) |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 60 | |
Jing Zhang | fcfe1ba | 2021-06-18 22:27:05 +0000 | [diff] [blame] | 61 | const struct _kvm_stats_desc kvm_vm_stats_desc[] = { |
| 62 | KVM_GENERIC_VM_STATS(), |
| 63 | STATS_DESC_COUNTER(VM, inject_io), |
| 64 | STATS_DESC_COUNTER(VM, inject_float_mchk), |
| 65 | STATS_DESC_COUNTER(VM, inject_pfault_done), |
| 66 | STATS_DESC_COUNTER(VM, inject_service_signal), |
| 67 | STATS_DESC_COUNTER(VM, inject_virtio) |
| 68 | }; |
Jing Zhang | fcfe1ba | 2021-06-18 22:27:05 +0000 | [diff] [blame] | 69 | |
| 70 | const struct kvm_stats_header kvm_vm_stats_header = { |
| 71 | .name_size = KVM_STATS_NAME_SIZE, |
| 72 | .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), |
| 73 | .id_offset = sizeof(struct kvm_stats_header), |
| 74 | .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, |
| 75 | .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + |
| 76 | sizeof(kvm_vm_stats_desc), |
| 77 | }; |
| 78 | |
Jing Zhang | ce55c04 | 2021-06-18 22:27:06 +0000 | [diff] [blame] | 79 | const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { |
| 80 | KVM_GENERIC_VCPU_STATS(), |
| 81 | STATS_DESC_COUNTER(VCPU, exit_userspace), |
| 82 | STATS_DESC_COUNTER(VCPU, exit_null), |
| 83 | STATS_DESC_COUNTER(VCPU, exit_external_request), |
| 84 | STATS_DESC_COUNTER(VCPU, exit_io_request), |
| 85 | STATS_DESC_COUNTER(VCPU, exit_external_interrupt), |
| 86 | STATS_DESC_COUNTER(VCPU, exit_stop_request), |
| 87 | STATS_DESC_COUNTER(VCPU, exit_validity), |
| 88 | STATS_DESC_COUNTER(VCPU, exit_instruction), |
| 89 | STATS_DESC_COUNTER(VCPU, exit_pei), |
| 90 | STATS_DESC_COUNTER(VCPU, halt_no_poll_steal), |
| 91 | STATS_DESC_COUNTER(VCPU, instruction_lctl), |
| 92 | STATS_DESC_COUNTER(VCPU, instruction_lctlg), |
| 93 | STATS_DESC_COUNTER(VCPU, instruction_stctl), |
| 94 | STATS_DESC_COUNTER(VCPU, instruction_stctg), |
| 95 | STATS_DESC_COUNTER(VCPU, exit_program_interruption), |
| 96 | STATS_DESC_COUNTER(VCPU, exit_instr_and_program), |
| 97 | STATS_DESC_COUNTER(VCPU, exit_operation_exception), |
| 98 | STATS_DESC_COUNTER(VCPU, deliver_ckc), |
| 99 | STATS_DESC_COUNTER(VCPU, deliver_cputm), |
| 100 | STATS_DESC_COUNTER(VCPU, deliver_external_call), |
| 101 | STATS_DESC_COUNTER(VCPU, deliver_emergency_signal), |
| 102 | STATS_DESC_COUNTER(VCPU, deliver_service_signal), |
| 103 | STATS_DESC_COUNTER(VCPU, deliver_virtio), |
| 104 | STATS_DESC_COUNTER(VCPU, deliver_stop_signal), |
| 105 | STATS_DESC_COUNTER(VCPU, deliver_prefix_signal), |
| 106 | STATS_DESC_COUNTER(VCPU, deliver_restart_signal), |
| 107 | STATS_DESC_COUNTER(VCPU, deliver_program), |
| 108 | STATS_DESC_COUNTER(VCPU, deliver_io), |
| 109 | STATS_DESC_COUNTER(VCPU, deliver_machine_check), |
| 110 | STATS_DESC_COUNTER(VCPU, exit_wait_state), |
| 111 | STATS_DESC_COUNTER(VCPU, inject_ckc), |
| 112 | STATS_DESC_COUNTER(VCPU, inject_cputm), |
| 113 | STATS_DESC_COUNTER(VCPU, inject_external_call), |
| 114 | STATS_DESC_COUNTER(VCPU, inject_emergency_signal), |
| 115 | STATS_DESC_COUNTER(VCPU, inject_mchk), |
| 116 | STATS_DESC_COUNTER(VCPU, inject_pfault_init), |
| 117 | STATS_DESC_COUNTER(VCPU, inject_program), |
| 118 | STATS_DESC_COUNTER(VCPU, inject_restart), |
| 119 | STATS_DESC_COUNTER(VCPU, inject_set_prefix), |
| 120 | STATS_DESC_COUNTER(VCPU, inject_stop_signal), |
| 121 | STATS_DESC_COUNTER(VCPU, instruction_epsw), |
| 122 | STATS_DESC_COUNTER(VCPU, instruction_gs), |
| 123 | STATS_DESC_COUNTER(VCPU, instruction_io_other), |
| 124 | STATS_DESC_COUNTER(VCPU, instruction_lpsw), |
| 125 | STATS_DESC_COUNTER(VCPU, instruction_lpswe), |
| 126 | STATS_DESC_COUNTER(VCPU, instruction_pfmf), |
| 127 | STATS_DESC_COUNTER(VCPU, instruction_ptff), |
| 128 | STATS_DESC_COUNTER(VCPU, instruction_sck), |
| 129 | STATS_DESC_COUNTER(VCPU, instruction_sckpf), |
| 130 | STATS_DESC_COUNTER(VCPU, instruction_stidp), |
| 131 | STATS_DESC_COUNTER(VCPU, instruction_spx), |
| 132 | STATS_DESC_COUNTER(VCPU, instruction_stpx), |
| 133 | STATS_DESC_COUNTER(VCPU, instruction_stap), |
| 134 | STATS_DESC_COUNTER(VCPU, instruction_iske), |
| 135 | STATS_DESC_COUNTER(VCPU, instruction_ri), |
| 136 | STATS_DESC_COUNTER(VCPU, instruction_rrbe), |
| 137 | STATS_DESC_COUNTER(VCPU, instruction_sske), |
| 138 | STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock), |
| 139 | STATS_DESC_COUNTER(VCPU, instruction_stsi), |
| 140 | STATS_DESC_COUNTER(VCPU, instruction_stfl), |
| 141 | STATS_DESC_COUNTER(VCPU, instruction_tb), |
| 142 | STATS_DESC_COUNTER(VCPU, instruction_tpi), |
| 143 | STATS_DESC_COUNTER(VCPU, instruction_tprot), |
| 144 | STATS_DESC_COUNTER(VCPU, instruction_tsch), |
| 145 | STATS_DESC_COUNTER(VCPU, instruction_sie), |
| 146 | STATS_DESC_COUNTER(VCPU, instruction_essa), |
| 147 | STATS_DESC_COUNTER(VCPU, instruction_sthyi), |
| 148 | STATS_DESC_COUNTER(VCPU, instruction_sigp_sense), |
| 149 | STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running), |
| 150 | STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call), |
| 151 | STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency), |
| 152 | STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency), |
| 153 | STATS_DESC_COUNTER(VCPU, instruction_sigp_start), |
| 154 | STATS_DESC_COUNTER(VCPU, instruction_sigp_stop), |
| 155 | STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status), |
| 156 | STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status), |
| 157 | STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status), |
| 158 | STATS_DESC_COUNTER(VCPU, instruction_sigp_arch), |
| 159 | STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix), |
| 160 | STATS_DESC_COUNTER(VCPU, instruction_sigp_restart), |
| 161 | STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset), |
| 162 | STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset), |
| 163 | STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown), |
Christian Borntraeger | bb000f64 | 2021-07-26 17:01:08 +0200 | [diff] [blame] | 164 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_10), |
| 165 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_44), |
| 166 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c), |
| 167 | STATS_DESC_COUNTER(VCPU, diag_9c_ignored), |
| 168 | STATS_DESC_COUNTER(VCPU, diag_9c_forward), |
| 169 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_258), |
| 170 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_308), |
| 171 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_500), |
| 172 | STATS_DESC_COUNTER(VCPU, instruction_diagnose_other), |
Jing Zhang | ce55c04 | 2021-06-18 22:27:06 +0000 | [diff] [blame] | 173 | STATS_DESC_COUNTER(VCPU, pfault_sync) |
| 174 | }; |
Jing Zhang | ce55c04 | 2021-06-18 22:27:06 +0000 | [diff] [blame] | 175 | |
| 176 | const struct kvm_stats_header kvm_vcpu_stats_header = { |
| 177 | .name_size = KVM_STATS_NAME_SIZE, |
| 178 | .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc), |
| 179 | .id_offset = sizeof(struct kvm_stats_header), |
| 180 | .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, |
| 181 | .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + |
| 182 | sizeof(kvm_vcpu_stats_desc), |
| 183 | }; |
| 184 | |
David Hildenbrand | a411edf | 2016-02-02 15:41:22 +0100 | [diff] [blame] | 185 | /* allow nested virtualization in KVM (if enabled by user space) */ |
| 186 | static int nested; |
| 187 | module_param(nested, int, S_IRUGO); |
| 188 | MODULE_PARM_DESC(nested, "Nested virtualization support"); |
| 189 | |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 190 | /* allow 1m huge page guest backing, if !nested */ |
| 191 | static int hpage; |
| 192 | module_param(hpage, int, 0444); |
| 193 | MODULE_PARM_DESC(hpage, "1m huge page backing support"); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 194 | |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 195 | /* maximum percentage of steal time for polling. >100 is treated like 100 */ |
| 196 | static u8 halt_poll_max_steal = 10; |
| 197 | module_param(halt_poll_max_steal, byte, 0644); |
Wei Yongjun | b41fb52 | 2019-05-04 06:51:45 +0000 | [diff] [blame] | 198 | MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 199 | |
Michael Mueller | cc674ef | 2020-02-27 10:10:31 +0100 | [diff] [blame] | 200 | /* if set to true, the GISA will be initialized and used if available */ |
| 201 | static bool use_gisa = true; |
| 202 | module_param(use_gisa, bool, 0644); |
| 203 | MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it."); |
| 204 | |
Pierre Morel | 87e28a1 | 2020-09-07 15:26:07 +0200 | [diff] [blame] | 205 | /* maximum diag9c forwarding per second */ |
| 206 | unsigned int diag9c_forwarding_hz; |
| 207 | module_param(diag9c_forwarding_hz, uint, 0644); |
| 208 | MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off"); |
| 209 | |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 210 | /* |
| 211 | * For now we handle at most 16 double words as this is what the s390 base |
| 212 | * kernel handles and stores in the prefix page. If we ever need to go beyond |
| 213 | * this, this requires changes to code, but the external uapi can stay. |
| 214 | */ |
| 215 | #define SIZE_INTERNAL 16 |
| 216 | |
| 217 | /* |
| 218 | * Base feature mask that defines default mask for facilities. Consists of the |
| 219 | * defines in FACILITIES_KVM and the non-hypervisor managed bits. |
| 220 | */ |
| 221 | static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; |
| 222 | /* |
| 223 | * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL |
| 224 | * and defines the facilities that can be enabled via a cpu model. |
| 225 | */ |
| 226 | static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; |
| 227 | |
| 228 | static unsigned long kvm_s390_fac_size(void) |
Michael Mueller | 78c4b59 | 2013-07-26 15:04:04 +0200 | [diff] [blame] | 229 | { |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 230 | BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); |
| 231 | BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); |
| 232 | BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > |
Sven Schnelle | 17e89e1 | 2021-05-05 22:01:10 +0200 | [diff] [blame] | 233 | sizeof(stfle_fac_list)); |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 234 | |
| 235 | return SIZE_INTERNAL; |
Michael Mueller | 78c4b59 | 2013-07-26 15:04:04 +0200 | [diff] [blame] | 236 | } |
| 237 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 238 | /* available cpu features supported by kvm */ |
| 239 | static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 240 | /* available subfunctions indicated via query / "test bit" */ |
| 241 | static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 242 | |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 243 | static struct gmap_notifier gmap_notifier; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 244 | static struct gmap_notifier vsie_gmap_notifier; |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 245 | debug_info_t *kvm_s390_dbf; |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 246 | debug_info_t *kvm_s390_dbf_uv; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 247 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 248 | /* Section: not file related */ |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 249 | int kvm_arch_hardware_enable(void) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 250 | { |
| 251 | /* every s390 is virtualization enabled ;-) */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 252 | return 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 255 | int kvm_arch_check_processor_compat(void *opaque) |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 260 | /* forward declarations */ |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 261 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 262 | unsigned long end); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 263 | static int sca_switch_to_extended(struct kvm *kvm); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 264 | |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 265 | static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) |
| 266 | { |
| 267 | u8 delta_idx = 0; |
| 268 | |
| 269 | /* |
| 270 | * The TOD jumps by delta, we have to compensate this by adding |
| 271 | * -delta to the epoch. |
| 272 | */ |
| 273 | delta = -delta; |
| 274 | |
| 275 | /* sign-extension - we're adding to signed values below */ |
| 276 | if ((s64)delta < 0) |
| 277 | delta_idx = -1; |
| 278 | |
| 279 | scb->epoch += delta; |
| 280 | if (scb->ecd & ECD_MEF) { |
| 281 | scb->epdx += delta_idx; |
| 282 | if (scb->epoch < delta) |
| 283 | scb->epdx += 1; |
| 284 | } |
| 285 | } |
| 286 | |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 287 | /* |
| 288 | * This callback is executed during stop_machine(). All CPUs are therefore |
| 289 | * temporarily stopped. In order not to change guest behavior, we have to |
| 290 | * disable preemption whenever we touch the epoch of kvm and the VCPUs, |
| 291 | * so a CPU won't be stopped while calculating with the epoch. |
| 292 | */ |
| 293 | static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, |
| 294 | void *v) |
| 295 | { |
| 296 | struct kvm *kvm; |
| 297 | struct kvm_vcpu *vcpu; |
| 298 | int i; |
| 299 | unsigned long long *delta = v; |
| 300 | |
| 301 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 302 | kvm_for_each_vcpu(i, vcpu, kvm) { |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 303 | kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); |
| 304 | if (i == 0) { |
| 305 | kvm->arch.epoch = vcpu->arch.sie_block->epoch; |
| 306 | kvm->arch.epdx = vcpu->arch.sie_block->epdx; |
| 307 | } |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 308 | if (vcpu->arch.cputm_enabled) |
| 309 | vcpu->arch.cputm_start += *delta; |
David Hildenbrand | 91473b4 | 2015-10-29 10:30:36 +0100 | [diff] [blame] | 310 | if (vcpu->arch.vsie_block) |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 311 | kvm_clock_sync_scb(vcpu->arch.vsie_block, |
| 312 | *delta); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | return NOTIFY_OK; |
| 316 | } |
| 317 | |
| 318 | static struct notifier_block kvm_clock_notifier = { |
| 319 | .notifier_call = kvm_clock_sync, |
| 320 | }; |
| 321 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 322 | int kvm_arch_hardware_setup(void *opaque) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 323 | { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 324 | gmap_notifier.notifier_call = kvm_gmap_notifier; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 325 | gmap_register_pte_notifier(&gmap_notifier); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 326 | vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; |
| 327 | gmap_register_pte_notifier(&vsie_gmap_notifier); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 328 | atomic_notifier_chain_register(&s390_epoch_delta_notifier, |
| 329 | &kvm_clock_notifier); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | void kvm_arch_hardware_unsetup(void) |
| 334 | { |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 335 | gmap_unregister_pte_notifier(&gmap_notifier); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 336 | gmap_unregister_pte_notifier(&vsie_gmap_notifier); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 337 | atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, |
| 338 | &kvm_clock_notifier); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 339 | } |
| 340 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 341 | static void allow_cpu_feat(unsigned long nr) |
| 342 | { |
| 343 | set_bit_inv(nr, kvm_s390_available_cpu_feat); |
| 344 | } |
| 345 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 346 | static inline int plo_test_bit(unsigned char nr) |
| 347 | { |
Heiko Carstens | 4fa3b91 | 2021-06-21 16:03:56 +0200 | [diff] [blame] | 348 | unsigned long function = (unsigned long)nr | 0x100; |
Heiko Carstens | d051ae5 | 2016-12-13 14:25:32 +0100 | [diff] [blame] | 349 | int cc; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 350 | |
| 351 | asm volatile( |
Heiko Carstens | 4fa3b91 | 2021-06-21 16:03:56 +0200 | [diff] [blame] | 352 | " lgr 0,%[function]\n" |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 353 | /* Parameter registers are ignored for "test bit" */ |
| 354 | " plo 0,0,0,0(0)\n" |
| 355 | " ipm %0\n" |
| 356 | " srl %0,28\n" |
| 357 | : "=d" (cc) |
Heiko Carstens | 4fa3b91 | 2021-06-21 16:03:56 +0200 | [diff] [blame] | 358 | : [function] "d" (function) |
| 359 | : "cc", "0"); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 360 | return cc == 0; |
| 361 | } |
| 362 | |
Heiko Carstens | d0dea73 | 2019-10-02 14:34:37 +0200 | [diff] [blame] | 363 | static __always_inline void __insn32_query(unsigned int opcode, u8 *query) |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 364 | { |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 365 | asm volatile( |
Heiko Carstens | 4fa3b91 | 2021-06-21 16:03:56 +0200 | [diff] [blame] | 366 | " lghi 0,0\n" |
| 367 | " lgr 1,%[query]\n" |
| 368 | /* Parameter registers are ignored */ |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 369 | " .insn rrf,%[opc] << 16,2,4,6,0\n" |
Heiko Carstens | b1c41ac | 2019-10-02 14:24:47 +0200 | [diff] [blame] | 370 | : |
Heiko Carstens | 4fa3b91 | 2021-06-21 16:03:56 +0200 | [diff] [blame] | 371 | : [query] "d" ((unsigned long)query), [opc] "i" (opcode) |
| 372 | : "cc", "memory", "0", "1"); |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 375 | #define INSN_SORTL 0xb938 |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 376 | #define INSN_DFLTCC 0xb939 |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 377 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 378 | static void kvm_s390_cpu_feat_init(void) |
| 379 | { |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 380 | int i; |
| 381 | |
| 382 | for (i = 0; i < 256; ++i) { |
| 383 | if (plo_test_bit(i)) |
| 384 | kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); |
| 385 | } |
| 386 | |
| 387 | if (test_facility(28)) /* TOD-clock steering */ |
Linus Torvalds | 221bb8a | 2016-08-02 16:11:27 -0400 | [diff] [blame] | 388 | ptff(kvm_s390_available_subfunc.ptff, |
| 389 | sizeof(kvm_s390_available_subfunc.ptff), |
| 390 | PTFF_QAF); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 391 | |
| 392 | if (test_facility(17)) { /* MSA */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 393 | __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) |
| 394 | kvm_s390_available_subfunc.kmac); |
| 395 | __cpacf_query(CPACF_KMC, (cpacf_mask_t *) |
| 396 | kvm_s390_available_subfunc.kmc); |
| 397 | __cpacf_query(CPACF_KM, (cpacf_mask_t *) |
| 398 | kvm_s390_available_subfunc.km); |
| 399 | __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) |
| 400 | kvm_s390_available_subfunc.kimd); |
| 401 | __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) |
| 402 | kvm_s390_available_subfunc.klmd); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 403 | } |
| 404 | if (test_facility(76)) /* MSA3 */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 405 | __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) |
| 406 | kvm_s390_available_subfunc.pckmo); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 407 | if (test_facility(77)) { /* MSA4 */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 408 | __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) |
| 409 | kvm_s390_available_subfunc.kmctr); |
| 410 | __cpacf_query(CPACF_KMF, (cpacf_mask_t *) |
| 411 | kvm_s390_available_subfunc.kmf); |
| 412 | __cpacf_query(CPACF_KMO, (cpacf_mask_t *) |
| 413 | kvm_s390_available_subfunc.kmo); |
| 414 | __cpacf_query(CPACF_PCC, (cpacf_mask_t *) |
| 415 | kvm_s390_available_subfunc.pcc); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 416 | } |
| 417 | if (test_facility(57)) /* MSA5 */ |
Harald Freudenberger | 985a9d2 | 2017-02-24 10:11:54 +0100 | [diff] [blame] | 418 | __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 419 | kvm_s390_available_subfunc.ppno); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 420 | |
Jason J. Herne | e000b8e | 2017-03-20 09:57:42 -0400 | [diff] [blame] | 421 | if (test_facility(146)) /* MSA8 */ |
| 422 | __cpacf_query(CPACF_KMA, (cpacf_mask_t *) |
| 423 | kvm_s390_available_subfunc.kma); |
| 424 | |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 425 | if (test_facility(155)) /* MSA9 */ |
| 426 | __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) |
| 427 | kvm_s390_available_subfunc.kdsa); |
| 428 | |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 429 | if (test_facility(150)) /* SORTL */ |
| 430 | __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); |
| 431 | |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 432 | if (test_facility(151)) /* DFLTCC */ |
| 433 | __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); |
| 434 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 435 | if (MACHINE_HAS_ESOP) |
| 436 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 437 | /* |
| 438 | * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), |
| 439 | * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). |
| 440 | */ |
| 441 | if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || |
David Hildenbrand | a411edf | 2016-02-02 15:41:22 +0100 | [diff] [blame] | 442 | !test_facility(3) || !nested) |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 443 | return; |
| 444 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); |
David Hildenbrand | 19c439b | 2015-11-25 11:02:26 +0100 | [diff] [blame] | 445 | if (sclp.has_64bscao) |
| 446 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); |
David Hildenbrand | 0615a32 | 2015-11-25 09:59:49 +0100 | [diff] [blame] | 447 | if (sclp.has_siif) |
| 448 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); |
David Hildenbrand | 77d18f6 | 2015-11-24 16:32:35 +0100 | [diff] [blame] | 449 | if (sclp.has_gpere) |
| 450 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); |
David Hildenbrand | a1b7b9b | 2015-11-24 16:41:33 +0100 | [diff] [blame] | 451 | if (sclp.has_gsls) |
| 452 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); |
David Hildenbrand | 5630a8e | 2015-11-24 16:53:51 +0100 | [diff] [blame] | 453 | if (sclp.has_ib) |
| 454 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); |
David Hildenbrand | 13ee3f6 | 2015-11-24 16:54:37 +0100 | [diff] [blame] | 455 | if (sclp.has_cei) |
| 456 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); |
David Hildenbrand | 7fd7f39 | 2015-11-24 16:56:23 +0100 | [diff] [blame] | 457 | if (sclp.has_ibs) |
| 458 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 459 | if (sclp.has_kss) |
| 460 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); |
David Hildenbrand | 5d3876a | 2016-04-13 17:06:50 +0200 | [diff] [blame] | 461 | /* |
| 462 | * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make |
| 463 | * all skey handling functions read/set the skey from the PGSTE |
| 464 | * instead of the real storage key. |
| 465 | * |
| 466 | * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make |
| 467 | * pages being detected as preserved although they are resident. |
| 468 | * |
| 469 | * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will |
| 470 | * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. |
| 471 | * |
| 472 | * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and |
| 473 | * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be |
| 474 | * correctly shadowed. We can do that for the PGSTE but not for PTE.I. |
| 475 | * |
| 476 | * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We |
| 477 | * cannot easily shadow the SCA because of the ipte lock. |
| 478 | */ |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 481 | int kvm_arch_init(void *opaque) |
| 482 | { |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 483 | int rc = -ENOMEM; |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 484 | |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 485 | kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); |
| 486 | if (!kvm_s390_dbf) |
| 487 | return -ENOMEM; |
| 488 | |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 489 | kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); |
| 490 | if (!kvm_s390_dbf_uv) |
| 491 | goto out; |
| 492 | |
| 493 | if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || |
| 494 | debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 495 | goto out; |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 496 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 497 | kvm_s390_cpu_feat_init(); |
| 498 | |
Cornelia Huck | 84877d9 | 2014-09-02 10:27:35 +0100 | [diff] [blame] | 499 | /* Register floating interrupt controller interface. */ |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 500 | rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); |
| 501 | if (rc) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 502 | pr_err("A FLIC registration call failed with rc=%d\n", rc); |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 503 | goto out; |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 504 | } |
Michael Mueller | b1d1e76 | 2019-01-31 09:52:45 +0100 | [diff] [blame] | 505 | |
| 506 | rc = kvm_s390_gib_init(GAL_ISC); |
| 507 | if (rc) |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 508 | goto out; |
Michael Mueller | b1d1e76 | 2019-01-31 09:52:45 +0100 | [diff] [blame] | 509 | |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 510 | return 0; |
| 511 | |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 512 | out: |
| 513 | kvm_arch_exit(); |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 514 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 517 | void kvm_arch_exit(void) |
| 518 | { |
Michael Mueller | 1282c21 | 2019-01-31 09:52:40 +0100 | [diff] [blame] | 519 | kvm_s390_gib_destroy(); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 520 | debug_unregister(kvm_s390_dbf); |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 521 | debug_unregister(kvm_s390_dbf_uv); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 524 | /* Section: device related */ |
| 525 | long kvm_arch_dev_ioctl(struct file *filp, |
| 526 | unsigned int ioctl, unsigned long arg) |
| 527 | { |
| 528 | if (ioctl == KVM_S390_ENABLE_SIE) |
| 529 | return s390_enable_sie(); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 533 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 534 | { |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 535 | int r; |
| 536 | |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 537 | switch (ext) { |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 538 | case KVM_CAP_S390_PSW: |
Christian Borntraeger | b6cf878 | 2011-09-20 17:07:29 +0200 | [diff] [blame] | 539 | case KVM_CAP_S390_GMAP: |
Christian Borntraeger | 52e16b1 | 2011-11-17 11:00:44 +0100 | [diff] [blame] | 540 | case KVM_CAP_SYNC_MMU: |
Carsten Otte | 1efd0f5 | 2012-01-04 10:25:29 +0100 | [diff] [blame] | 541 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 542 | case KVM_CAP_S390_UCONTROL: |
| 543 | #endif |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 544 | case KVM_CAP_ASYNC_PF: |
Christian Borntraeger | 60b413c | 2012-01-11 11:20:31 +0100 | [diff] [blame] | 545 | case KVM_CAP_SYNC_REGS: |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 546 | case KVM_CAP_ONE_REG: |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 547 | case KVM_CAP_ENABLE_CAP: |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 548 | case KVM_CAP_S390_CSS_SUPPORT: |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 549 | case KVM_CAP_IOEVENTFD: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 550 | case KVM_CAP_DEVICE_CTRL: |
Cornelia Huck | 78599d9 | 2014-07-15 09:54:39 +0200 | [diff] [blame] | 551 | case KVM_CAP_S390_IRQCHIP: |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 552 | case KVM_CAP_VM_ATTRIBUTES: |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 553 | case KVM_CAP_MP_STATE: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 554 | case KVM_CAP_IMMEDIATE_EXIT: |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 555 | case KVM_CAP_S390_INJECT_IRQ: |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 556 | case KVM_CAP_S390_USER_SIGP: |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 557 | case KVM_CAP_S390_USER_STSI: |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 558 | case KVM_CAP_S390_SKEYS: |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 559 | case KVM_CAP_S390_IRQ_STATE: |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 560 | case KVM_CAP_S390_USER_INSTR0: |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 561 | case KVM_CAP_S390_CMMA_MIGRATION: |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 562 | case KVM_CAP_S390_AIS: |
Christian Borntraeger | da9a144 | 2017-11-09 10:00:45 +0100 | [diff] [blame] | 563 | case KVM_CAP_S390_AIS_MIGRATION: |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 564 | case KVM_CAP_S390_VCPU_RESETS: |
Peter Xu | b9b2782 | 2020-05-05 11:47:50 -0400 | [diff] [blame] | 565 | case KVM_CAP_SET_GUEST_DEBUG: |
Collin Walling | 23a60f8 | 2020-06-22 11:46:36 -0400 | [diff] [blame] | 566 | case KVM_CAP_S390_DIAG318: |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 567 | r = 1; |
| 568 | break; |
Maxim Levitsky | a43b80b | 2021-04-01 16:54:47 +0300 | [diff] [blame] | 569 | case KVM_CAP_SET_GUEST_DEBUG2: |
| 570 | r = KVM_GUESTDBG_VALID_MASK; |
| 571 | break; |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 572 | case KVM_CAP_S390_HPAGE_1M: |
| 573 | r = 0; |
Janosch Frank | 40ebdb8 | 2018-08-01 11:48:28 +0100 | [diff] [blame] | 574 | if (hpage && !kvm_is_ucontrol(kvm)) |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 575 | r = 1; |
| 576 | break; |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 577 | case KVM_CAP_S390_MEM_OP: |
| 578 | r = MEM_OP_MAX_SIZE; |
| 579 | break; |
Christian Borntraeger | e726b1b | 2012-05-02 10:50:38 +0200 | [diff] [blame] | 580 | case KVM_CAP_NR_VCPUS: |
| 581 | case KVM_CAP_MAX_VCPUS: |
Thomas Huth | a86cb41 | 2019-05-23 18:43:08 +0200 | [diff] [blame] | 582 | case KVM_CAP_MAX_VCPU_ID: |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 583 | r = KVM_S390_BSCA_CPU_SLOTS; |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 584 | if (!kvm_s390_use_sca_entries()) |
| 585 | r = KVM_MAX_VCPUS; |
| 586 | else if (sclp.has_esca && sclp.has_64bscao) |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 587 | r = KVM_S390_ESCA_CPU_SLOTS; |
Christian Borntraeger | e726b1b | 2012-05-02 10:50:38 +0200 | [diff] [blame] | 588 | break; |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 589 | case KVM_CAP_S390_COW: |
Martin Schwidefsky | abf09be | 2012-11-07 13:17:37 +0100 | [diff] [blame] | 590 | r = MACHINE_HAS_ESOP; |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 591 | break; |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 592 | case KVM_CAP_S390_VECTOR_REGISTERS: |
| 593 | r = MACHINE_HAS_VX; |
| 594 | break; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 595 | case KVM_CAP_S390_RI: |
| 596 | r = test_facility(64); |
| 597 | break; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 598 | case KVM_CAP_S390_GS: |
| 599 | r = test_facility(133); |
| 600 | break; |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 601 | case KVM_CAP_S390_BPB: |
| 602 | r = test_facility(82); |
| 603 | break; |
Christian Borntraeger | 13da9ae | 2020-02-18 15:08:07 -0500 | [diff] [blame] | 604 | case KVM_CAP_S390_PROTECTED: |
| 605 | r = is_prot_virt_host(); |
| 606 | break; |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 607 | default: |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 608 | r = 0; |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 609 | } |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 610 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 613 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 614 | { |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 615 | int i; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 616 | gfn_t cur_gfn, last_gfn; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 617 | unsigned long gaddr, vmaddr; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 618 | struct gmap *gmap = kvm->arch.gmap; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 619 | DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 620 | |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 621 | /* Loop over all guest segments */ |
| 622 | cur_gfn = memslot->base_gfn; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 623 | last_gfn = memslot->base_gfn + memslot->npages; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 624 | for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { |
| 625 | gaddr = gfn_to_gpa(cur_gfn); |
| 626 | vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); |
| 627 | if (kvm_is_error_hva(vmaddr)) |
| 628 | continue; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 629 | |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 630 | bitmap_zero(bitmap, _PAGE_ENTRIES); |
| 631 | gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); |
| 632 | for (i = 0; i < _PAGE_ENTRIES; i++) { |
| 633 | if (test_bit(i, bitmap)) |
| 634 | mark_page_dirty(kvm, cur_gfn + i); |
| 635 | } |
| 636 | |
Christian Borntraeger | 1763f8d | 2016-02-03 11:12:34 +0100 | [diff] [blame] | 637 | if (fatal_signal_pending(current)) |
| 638 | return; |
Christian Borntraeger | 70c88a0 | 2016-02-02 15:15:56 +0100 | [diff] [blame] | 639 | cond_resched(); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 640 | } |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 641 | } |
| 642 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 643 | /* Section: vm related */ |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 644 | static void sca_del_vcpu(struct kvm_vcpu *vcpu); |
| 645 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 646 | /* |
| 647 | * Get (and clear) the dirty memory log for a memory slot. |
| 648 | */ |
| 649 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 650 | struct kvm_dirty_log *log) |
| 651 | { |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 652 | int r; |
| 653 | unsigned long n; |
| 654 | struct kvm_memory_slot *memslot; |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 655 | int is_dirty; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 656 | |
Janosch Frank | e1e8a96 | 2017-02-02 16:39:31 +0100 | [diff] [blame] | 657 | if (kvm_is_ucontrol(kvm)) |
| 658 | return -EINVAL; |
| 659 | |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 660 | mutex_lock(&kvm->slots_lock); |
| 661 | |
| 662 | r = -EINVAL; |
| 663 | if (log->slot >= KVM_USER_MEM_SLOTS) |
| 664 | goto out; |
| 665 | |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 666 | r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 667 | if (r) |
| 668 | goto out; |
| 669 | |
| 670 | /* Clear the dirty log */ |
| 671 | if (is_dirty) { |
| 672 | n = kvm_dirty_bitmap_bytes(memslot); |
| 673 | memset(memslot->dirty_bitmap, 0, n); |
| 674 | } |
| 675 | r = 0; |
| 676 | out: |
| 677 | mutex_unlock(&kvm->slots_lock); |
| 678 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 679 | } |
| 680 | |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 681 | static void icpt_operexc_on_all_vcpus(struct kvm *kvm) |
| 682 | { |
| 683 | unsigned int i; |
| 684 | struct kvm_vcpu *vcpu; |
| 685 | |
| 686 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 687 | kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); |
| 688 | } |
| 689 | } |
| 690 | |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 691 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) |
Cornelia Huck | d938dc5 | 2013-10-23 18:26:34 +0200 | [diff] [blame] | 692 | { |
| 693 | int r; |
| 694 | |
| 695 | if (cap->flags) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | switch (cap->cap) { |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 699 | case KVM_CAP_S390_IRQCHIP: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 700 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 701 | kvm->arch.use_irqchip = 1; |
| 702 | r = 0; |
| 703 | break; |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 704 | case KVM_CAP_S390_USER_SIGP: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 705 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 706 | kvm->arch.user_sigp = 1; |
| 707 | r = 0; |
| 708 | break; |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 709 | case KVM_CAP_S390_VECTOR_REGISTERS: |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 710 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 711 | if (kvm->created_vcpus) { |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 712 | r = -EBUSY; |
| 713 | } else if (MACHINE_HAS_VX) { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 714 | set_kvm_facility(kvm->arch.model.fac_mask, 129); |
| 715 | set_kvm_facility(kvm->arch.model.fac_list, 129); |
Guenther Hutzl | 2f87d94 | 2016-06-03 14:37:17 +0200 | [diff] [blame] | 716 | if (test_facility(134)) { |
| 717 | set_kvm_facility(kvm->arch.model.fac_mask, 134); |
| 718 | set_kvm_facility(kvm->arch.model.fac_list, 134); |
| 719 | } |
Maxim Samoylov | 53743aa | 2016-02-10 10:31:23 +0100 | [diff] [blame] | 720 | if (test_facility(135)) { |
| 721 | set_kvm_facility(kvm->arch.model.fac_mask, 135); |
| 722 | set_kvm_facility(kvm->arch.model.fac_list, 135); |
| 723 | } |
Christian Borntraeger | 7832e91 | 2018-12-28 09:43:37 +0100 | [diff] [blame] | 724 | if (test_facility(148)) { |
| 725 | set_kvm_facility(kvm->arch.model.fac_mask, 148); |
| 726 | set_kvm_facility(kvm->arch.model.fac_list, 148); |
| 727 | } |
Christian Borntraeger | d5cb6ab | 2018-12-28 09:45:58 +0100 | [diff] [blame] | 728 | if (test_facility(152)) { |
| 729 | set_kvm_facility(kvm->arch.model.fac_mask, 152); |
| 730 | set_kvm_facility(kvm->arch.model.fac_list, 152); |
| 731 | } |
Christian Borntraeger | 1f703d2 | 2021-01-25 13:39:45 +0100 | [diff] [blame] | 732 | if (test_facility(192)) { |
| 733 | set_kvm_facility(kvm->arch.model.fac_mask, 192); |
| 734 | set_kvm_facility(kvm->arch.model.fac_list, 192); |
| 735 | } |
Michael Mueller | 18280d8 | 2015-03-16 16:05:41 +0100 | [diff] [blame] | 736 | r = 0; |
| 737 | } else |
| 738 | r = -EINVAL; |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 739 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 740 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", |
| 741 | r ? "(not available)" : "(success)"); |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 742 | break; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 743 | case KVM_CAP_S390_RI: |
| 744 | r = -EINVAL; |
| 745 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 746 | if (kvm->created_vcpus) { |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 747 | r = -EBUSY; |
| 748 | } else if (test_facility(64)) { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 749 | set_kvm_facility(kvm->arch.model.fac_mask, 64); |
| 750 | set_kvm_facility(kvm->arch.model.fac_list, 64); |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 751 | r = 0; |
| 752 | } |
| 753 | mutex_unlock(&kvm->lock); |
| 754 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", |
| 755 | r ? "(not available)" : "(success)"); |
| 756 | break; |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 757 | case KVM_CAP_S390_AIS: |
| 758 | mutex_lock(&kvm->lock); |
| 759 | if (kvm->created_vcpus) { |
| 760 | r = -EBUSY; |
| 761 | } else { |
| 762 | set_kvm_facility(kvm->arch.model.fac_mask, 72); |
| 763 | set_kvm_facility(kvm->arch.model.fac_list, 72); |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 764 | r = 0; |
| 765 | } |
| 766 | mutex_unlock(&kvm->lock); |
| 767 | VM_EVENT(kvm, 3, "ENABLE: AIS %s", |
| 768 | r ? "(not available)" : "(success)"); |
| 769 | break; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 770 | case KVM_CAP_S390_GS: |
| 771 | r = -EINVAL; |
| 772 | mutex_lock(&kvm->lock); |
Christian Borntraeger | 241e3ec0 | 2017-11-16 15:12:52 +0100 | [diff] [blame] | 773 | if (kvm->created_vcpus) { |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 774 | r = -EBUSY; |
| 775 | } else if (test_facility(133)) { |
| 776 | set_kvm_facility(kvm->arch.model.fac_mask, 133); |
| 777 | set_kvm_facility(kvm->arch.model.fac_list, 133); |
| 778 | r = 0; |
| 779 | } |
| 780 | mutex_unlock(&kvm->lock); |
| 781 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", |
| 782 | r ? "(not available)" : "(success)"); |
| 783 | break; |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 784 | case KVM_CAP_S390_HPAGE_1M: |
| 785 | mutex_lock(&kvm->lock); |
| 786 | if (kvm->created_vcpus) |
| 787 | r = -EBUSY; |
Janosch Frank | 40ebdb8 | 2018-08-01 11:48:28 +0100 | [diff] [blame] | 788 | else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 789 | r = -EINVAL; |
| 790 | else { |
| 791 | r = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 792 | mmap_write_lock(kvm->mm); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 793 | kvm->mm->context.allow_gmap_hpage_1m = 1; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 794 | mmap_write_unlock(kvm->mm); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 795 | /* |
| 796 | * We might have to create fake 4k page |
| 797 | * tables. To avoid that the hardware works on |
| 798 | * stale PGSTEs, we emulate these instructions. |
| 799 | */ |
| 800 | kvm->arch.use_skf = 0; |
| 801 | kvm->arch.use_pfmfi = 0; |
| 802 | } |
| 803 | mutex_unlock(&kvm->lock); |
| 804 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", |
| 805 | r ? "(not available)" : "(success)"); |
| 806 | break; |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 807 | case KVM_CAP_S390_USER_STSI: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 808 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 809 | kvm->arch.user_stsi = 1; |
| 810 | r = 0; |
| 811 | break; |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 812 | case KVM_CAP_S390_USER_INSTR0: |
| 813 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); |
| 814 | kvm->arch.user_instr0 = 1; |
| 815 | icpt_operexc_on_all_vcpus(kvm); |
| 816 | r = 0; |
| 817 | break; |
Cornelia Huck | d938dc5 | 2013-10-23 18:26:34 +0200 | [diff] [blame] | 818 | default: |
| 819 | r = -EINVAL; |
| 820 | break; |
| 821 | } |
| 822 | return r; |
| 823 | } |
| 824 | |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 825 | static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) |
| 826 | { |
| 827 | int ret; |
| 828 | |
| 829 | switch (attr->attr) { |
| 830 | case KVM_S390_VM_MEM_LIMIT_SIZE: |
| 831 | ret = 0; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 832 | VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 833 | kvm->arch.mem_limit); |
| 834 | if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 835 | ret = -EFAULT; |
| 836 | break; |
| 837 | default: |
| 838 | ret = -ENXIO; |
| 839 | break; |
| 840 | } |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 845 | { |
| 846 | int ret; |
| 847 | unsigned int idx; |
| 848 | switch (attr->attr) { |
| 849 | case KVM_S390_VM_MEM_ENABLE_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 850 | ret = -ENXIO; |
David Hildenbrand | c24cc9c | 2015-11-24 13:53:04 +0100 | [diff] [blame] | 851 | if (!sclp.has_cmma) |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 852 | break; |
| 853 | |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 854 | VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 855 | mutex_lock(&kvm->lock); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 856 | if (kvm->created_vcpus) |
| 857 | ret = -EBUSY; |
| 858 | else if (kvm->mm->context.allow_gmap_hpage_1m) |
| 859 | ret = -EINVAL; |
| 860 | else { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 861 | kvm->arch.use_cmma = 1; |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 862 | /* Not compatible with cmma. */ |
| 863 | kvm->arch.use_pfmfi = 0; |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 864 | ret = 0; |
| 865 | } |
| 866 | mutex_unlock(&kvm->lock); |
| 867 | break; |
| 868 | case KVM_S390_VM_MEM_CLR_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 869 | ret = -ENXIO; |
| 870 | if (!sclp.has_cmma) |
| 871 | break; |
Dominik Dingel | c348915 | 2015-06-18 13:17:11 +0200 | [diff] [blame] | 872 | ret = -EINVAL; |
| 873 | if (!kvm->arch.use_cmma) |
| 874 | break; |
| 875 | |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 876 | VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 877 | mutex_lock(&kvm->lock); |
| 878 | idx = srcu_read_lock(&kvm->srcu); |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 879 | s390_reset_cmma(kvm->arch.gmap->mm); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 880 | srcu_read_unlock(&kvm->srcu, idx); |
| 881 | mutex_unlock(&kvm->lock); |
| 882 | ret = 0; |
| 883 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 884 | case KVM_S390_VM_MEM_LIMIT_SIZE: { |
| 885 | unsigned long new_limit; |
| 886 | |
| 887 | if (kvm_is_ucontrol(kvm)) |
| 888 | return -EINVAL; |
| 889 | |
| 890 | if (get_user(new_limit, (u64 __user *)attr->addr)) |
| 891 | return -EFAULT; |
| 892 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 893 | if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && |
| 894 | new_limit > kvm->arch.mem_limit) |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 895 | return -E2BIG; |
| 896 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 897 | if (!new_limit) |
| 898 | return -EINVAL; |
| 899 | |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 900 | /* gmap_create takes last usable address */ |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 901 | if (new_limit != KVM_S390_NO_MEM_LIMIT) |
| 902 | new_limit -= 1; |
| 903 | |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 904 | ret = -EBUSY; |
| 905 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 906 | if (!kvm->created_vcpus) { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 907 | /* gmap_create will round the limit up */ |
| 908 | struct gmap *new = gmap_create(current->mm, new_limit); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 909 | |
| 910 | if (!new) { |
| 911 | ret = -ENOMEM; |
| 912 | } else { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 913 | gmap_remove(kvm->arch.gmap); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 914 | new->private = kvm; |
| 915 | kvm->arch.gmap = new; |
| 916 | ret = 0; |
| 917 | } |
| 918 | } |
| 919 | mutex_unlock(&kvm->lock); |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 920 | VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); |
| 921 | VM_EVENT(kvm, 3, "New guest asce: 0x%pK", |
| 922 | (void *) kvm->arch.gmap->asce); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 923 | break; |
| 924 | } |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 925 | default: |
| 926 | ret = -ENXIO; |
| 927 | break; |
| 928 | } |
| 929 | return ret; |
| 930 | } |
| 931 | |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 932 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); |
| 933 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 934 | void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 935 | { |
| 936 | struct kvm_vcpu *vcpu; |
| 937 | int i; |
| 938 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 939 | kvm_s390_vcpu_block_all(kvm); |
| 940 | |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 941 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 942 | kvm_s390_vcpu_crypto_setup(vcpu); |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 943 | /* recreate the shadow crycb by leaving the VSIE handler */ |
| 944 | kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); |
| 945 | } |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 946 | |
| 947 | kvm_s390_vcpu_unblock_all(kvm); |
| 948 | } |
| 949 | |
| 950 | static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) |
| 951 | { |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 952 | mutex_lock(&kvm->lock); |
| 953 | switch (attr->attr) { |
| 954 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 955 | if (!test_kvm_facility(kvm, 76)) { |
| 956 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 957 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 958 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 959 | get_random_bytes( |
| 960 | kvm->arch.crypto.crycb->aes_wrapping_key_mask, |
| 961 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
| 962 | kvm->arch.crypto.aes_kw = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 963 | VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 964 | break; |
| 965 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 966 | if (!test_kvm_facility(kvm, 76)) { |
| 967 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 968 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 969 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 970 | get_random_bytes( |
| 971 | kvm->arch.crypto.crycb->dea_wrapping_key_mask, |
| 972 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
| 973 | kvm->arch.crypto.dea_kw = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 974 | VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 975 | break; |
| 976 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 977 | if (!test_kvm_facility(kvm, 76)) { |
| 978 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 979 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 980 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 981 | kvm->arch.crypto.aes_kw = 0; |
| 982 | memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, |
| 983 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 984 | VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 985 | break; |
| 986 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 987 | if (!test_kvm_facility(kvm, 76)) { |
| 988 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 989 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 990 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 991 | kvm->arch.crypto.dea_kw = 0; |
| 992 | memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, |
| 993 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 994 | VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 995 | break; |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 996 | case KVM_S390_VM_CRYPTO_ENABLE_APIE: |
| 997 | if (!ap_instructions_available()) { |
| 998 | mutex_unlock(&kvm->lock); |
| 999 | return -EOPNOTSUPP; |
| 1000 | } |
| 1001 | kvm->arch.crypto.apie = 1; |
| 1002 | break; |
| 1003 | case KVM_S390_VM_CRYPTO_DISABLE_APIE: |
| 1004 | if (!ap_instructions_available()) { |
| 1005 | mutex_unlock(&kvm->lock); |
| 1006 | return -EOPNOTSUPP; |
| 1007 | } |
| 1008 | kvm->arch.crypto.apie = 0; |
| 1009 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1010 | default: |
| 1011 | mutex_unlock(&kvm->lock); |
| 1012 | return -ENXIO; |
| 1013 | } |
| 1014 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 1015 | kvm_s390_vcpu_crypto_reset_all(kvm); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1016 | mutex_unlock(&kvm->lock); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1020 | static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) |
| 1021 | { |
| 1022 | int cx; |
| 1023 | struct kvm_vcpu *vcpu; |
| 1024 | |
| 1025 | kvm_for_each_vcpu(cx, vcpu, kvm) |
| 1026 | kvm_s390_sync_request(req, vcpu); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Must be called with kvm->srcu held to avoid races on memslots, and with |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1031 | * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1032 | */ |
| 1033 | static int kvm_s390_vm_start_migration(struct kvm *kvm) |
| 1034 | { |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1035 | struct kvm_memory_slot *ms; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1036 | struct kvm_memslots *slots; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1037 | unsigned long ram_pages = 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1038 | int slotnr; |
| 1039 | |
| 1040 | /* migration mode already enabled */ |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1041 | if (kvm->arch.migration_mode) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1042 | return 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1043 | slots = kvm_memslots(kvm); |
| 1044 | if (!slots || !slots->used_slots) |
| 1045 | return -EINVAL; |
| 1046 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1047 | if (!kvm->arch.use_cmma) { |
| 1048 | kvm->arch.migration_mode = 1; |
| 1049 | return 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1050 | } |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1051 | /* mark all the pages in active slots as dirty */ |
| 1052 | for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { |
| 1053 | ms = slots->memslots + slotnr; |
Igor Mammedov | 13a17cc | 2019-09-11 03:52:18 -0400 | [diff] [blame] | 1054 | if (!ms->dirty_bitmap) |
| 1055 | return -EINVAL; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1056 | /* |
| 1057 | * The second half of the bitmap is only used on x86, |
| 1058 | * and would be wasted otherwise, so we put it to good |
| 1059 | * use here to keep track of the state of the storage |
| 1060 | * attributes. |
| 1061 | */ |
| 1062 | memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); |
| 1063 | ram_pages += ms->npages; |
| 1064 | } |
| 1065 | atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); |
| 1066 | kvm->arch.migration_mode = 1; |
| 1067 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1072 | * Must be called with kvm->slots_lock to avoid races with ourselves and |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1073 | * kvm_s390_vm_start_migration. |
| 1074 | */ |
| 1075 | static int kvm_s390_vm_stop_migration(struct kvm *kvm) |
| 1076 | { |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1077 | /* migration mode already disabled */ |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1078 | if (!kvm->arch.migration_mode) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1079 | return 0; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1080 | kvm->arch.migration_mode = 0; |
| 1081 | if (kvm->arch.use_cmma) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1082 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static int kvm_s390_vm_set_migration(struct kvm *kvm, |
| 1087 | struct kvm_device_attr *attr) |
| 1088 | { |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1089 | int res = -ENXIO; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1090 | |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1091 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1092 | switch (attr->attr) { |
| 1093 | case KVM_S390_VM_MIGRATION_START: |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1094 | res = kvm_s390_vm_start_migration(kvm); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1095 | break; |
| 1096 | case KVM_S390_VM_MIGRATION_STOP: |
| 1097 | res = kvm_s390_vm_stop_migration(kvm); |
| 1098 | break; |
| 1099 | default: |
| 1100 | break; |
| 1101 | } |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1102 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1103 | |
| 1104 | return res; |
| 1105 | } |
| 1106 | |
| 1107 | static int kvm_s390_vm_get_migration(struct kvm *kvm, |
| 1108 | struct kvm_device_attr *attr) |
| 1109 | { |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1110 | u64 mig = kvm->arch.migration_mode; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1111 | |
| 1112 | if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) |
| 1113 | return -ENXIO; |
| 1114 | |
| 1115 | if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) |
| 1116 | return -EFAULT; |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1120 | static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1121 | { |
| 1122 | struct kvm_s390_vm_tod_clock gtod; |
| 1123 | |
| 1124 | if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) |
| 1125 | return -EFAULT; |
| 1126 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1127 | if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1128 | return -EINVAL; |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1129 | kvm_s390_set_tod_clock(kvm, >od); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1130 | |
| 1131 | VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", |
| 1132 | gtod.epoch_idx, gtod.tod); |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1137 | static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1138 | { |
| 1139 | u8 gtod_high; |
| 1140 | |
| 1141 | if (copy_from_user(>od_high, (void __user *)attr->addr, |
| 1142 | sizeof(gtod_high))) |
| 1143 | return -EFAULT; |
| 1144 | |
| 1145 | if (gtod_high != 0) |
| 1146 | return -EINVAL; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1147 | VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1153 | { |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1154 | struct kvm_s390_vm_tod_clock gtod = { 0 }; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1155 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1156 | if (copy_from_user(>od.tod, (void __user *)attr->addr, |
| 1157 | sizeof(gtod.tod))) |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1158 | return -EFAULT; |
| 1159 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1160 | kvm_s390_set_tod_clock(kvm, >od); |
| 1161 | VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1166 | { |
| 1167 | int ret; |
| 1168 | |
| 1169 | if (attr->flags) |
| 1170 | return -EINVAL; |
| 1171 | |
| 1172 | switch (attr->attr) { |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1173 | case KVM_S390_VM_TOD_EXT: |
| 1174 | ret = kvm_s390_set_tod_ext(kvm, attr); |
| 1175 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1176 | case KVM_S390_VM_TOD_HIGH: |
| 1177 | ret = kvm_s390_set_tod_high(kvm, attr); |
| 1178 | break; |
| 1179 | case KVM_S390_VM_TOD_LOW: |
| 1180 | ret = kvm_s390_set_tod_low(kvm, attr); |
| 1181 | break; |
| 1182 | default: |
| 1183 | ret = -ENXIO; |
| 1184 | break; |
| 1185 | } |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1189 | static void kvm_s390_get_tod_clock(struct kvm *kvm, |
| 1190 | struct kvm_s390_vm_tod_clock *gtod) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1191 | { |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 1192 | union tod_clock clk; |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1193 | |
| 1194 | preempt_disable(); |
| 1195 | |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 1196 | store_tod_clock_ext(&clk); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1197 | |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 1198 | gtod->tod = clk.tod + kvm->arch.epoch; |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1199 | gtod->epoch_idx = 0; |
| 1200 | if (test_kvm_facility(kvm, 139)) { |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 1201 | gtod->epoch_idx = clk.ei + kvm->arch.epdx; |
| 1202 | if (gtod->tod < clk.tod) |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1203 | gtod->epoch_idx += 1; |
| 1204 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1205 | |
| 1206 | preempt_enable(); |
| 1207 | } |
| 1208 | |
| 1209 | static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1210 | { |
| 1211 | struct kvm_s390_vm_tod_clock gtod; |
| 1212 | |
| 1213 | memset(>od, 0, sizeof(gtod)); |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1214 | kvm_s390_get_tod_clock(kvm, >od); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1215 | if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) |
| 1216 | return -EFAULT; |
| 1217 | |
| 1218 | VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", |
| 1219 | gtod.epoch_idx, gtod.tod); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1223 | static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1224 | { |
| 1225 | u8 gtod_high = 0; |
| 1226 | |
| 1227 | if (copy_to_user((void __user *)attr->addr, >od_high, |
| 1228 | sizeof(gtod_high))) |
| 1229 | return -EFAULT; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1230 | VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1236 | { |
David Hildenbrand | 5a3d883 | 2015-09-29 16:27:24 +0200 | [diff] [blame] | 1237 | u64 gtod; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1238 | |
David Hildenbrand | 60417fc | 2015-09-29 16:20:36 +0200 | [diff] [blame] | 1239 | gtod = kvm_s390_get_tod_clock_fast(kvm); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1240 | if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) |
| 1241 | return -EFAULT; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1242 | VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1248 | { |
| 1249 | int ret; |
| 1250 | |
| 1251 | if (attr->flags) |
| 1252 | return -EINVAL; |
| 1253 | |
| 1254 | switch (attr->attr) { |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1255 | case KVM_S390_VM_TOD_EXT: |
| 1256 | ret = kvm_s390_get_tod_ext(kvm, attr); |
| 1257 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1258 | case KVM_S390_VM_TOD_HIGH: |
| 1259 | ret = kvm_s390_get_tod_high(kvm, attr); |
| 1260 | break; |
| 1261 | case KVM_S390_VM_TOD_LOW: |
| 1262 | ret = kvm_s390_get_tod_low(kvm, attr); |
| 1263 | break; |
| 1264 | default: |
| 1265 | ret = -ENXIO; |
| 1266 | break; |
| 1267 | } |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1271 | static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1272 | { |
| 1273 | struct kvm_s390_vm_cpu_processor *proc; |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1274 | u16 lowest_ibc, unblocked_ibc; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1275 | int ret = 0; |
| 1276 | |
| 1277 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 1278 | if (kvm->created_vcpus) { |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1279 | ret = -EBUSY; |
| 1280 | goto out; |
| 1281 | } |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 1282 | proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1283 | if (!proc) { |
| 1284 | ret = -ENOMEM; |
| 1285 | goto out; |
| 1286 | } |
| 1287 | if (!copy_from_user(proc, (void __user *)attr->addr, |
| 1288 | sizeof(*proc))) { |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 1289 | kvm->arch.model.cpuid = proc->cpuid; |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1290 | lowest_ibc = sclp.ibc >> 16 & 0xfff; |
| 1291 | unblocked_ibc = sclp.ibc & 0xfff; |
David Hildenbrand | 0487c44 | 2016-06-10 09:22:31 +0200 | [diff] [blame] | 1292 | if (lowest_ibc && proc->ibc) { |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1293 | if (proc->ibc > unblocked_ibc) |
| 1294 | kvm->arch.model.ibc = unblocked_ibc; |
| 1295 | else if (proc->ibc < lowest_ibc) |
| 1296 | kvm->arch.model.ibc = lowest_ibc; |
| 1297 | else |
| 1298 | kvm->arch.model.ibc = proc->ibc; |
| 1299 | } |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1300 | memcpy(kvm->arch.model.fac_list, proc->fac_list, |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1301 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1302 | VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", |
| 1303 | kvm->arch.model.ibc, |
| 1304 | kvm->arch.model.cpuid); |
| 1305 | VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1306 | kvm->arch.model.fac_list[0], |
| 1307 | kvm->arch.model.fac_list[1], |
| 1308 | kvm->arch.model.fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1309 | } else |
| 1310 | ret = -EFAULT; |
| 1311 | kfree(proc); |
| 1312 | out: |
| 1313 | mutex_unlock(&kvm->lock); |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1317 | static int kvm_s390_set_processor_feat(struct kvm *kvm, |
| 1318 | struct kvm_device_attr *attr) |
| 1319 | { |
| 1320 | struct kvm_s390_vm_cpu_feat data; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) |
| 1323 | return -EFAULT; |
| 1324 | if (!bitmap_subset((unsigned long *) data.feat, |
| 1325 | kvm_s390_available_cpu_feat, |
| 1326 | KVM_S390_VM_CPU_FEAT_NR_BITS)) |
| 1327 | return -EINVAL; |
| 1328 | |
| 1329 | mutex_lock(&kvm->lock); |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1330 | if (kvm->created_vcpus) { |
| 1331 | mutex_unlock(&kvm->lock); |
| 1332 | return -EBUSY; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1333 | } |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1334 | bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, |
| 1335 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1336 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1337 | VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1338 | data.feat[0], |
| 1339 | data.feat[1], |
| 1340 | data.feat[2]); |
| 1341 | return 0; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1344 | static int kvm_s390_set_processor_subfunc(struct kvm *kvm, |
| 1345 | struct kvm_device_attr *attr) |
| 1346 | { |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1347 | mutex_lock(&kvm->lock); |
| 1348 | if (kvm->created_vcpus) { |
| 1349 | mutex_unlock(&kvm->lock); |
| 1350 | return -EBUSY; |
| 1351 | } |
| 1352 | |
| 1353 | if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, |
| 1354 | sizeof(struct kvm_s390_vm_cpu_subfunc))) { |
| 1355 | mutex_unlock(&kvm->lock); |
| 1356 | return -EFAULT; |
| 1357 | } |
| 1358 | mutex_unlock(&kvm->lock); |
| 1359 | |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1360 | VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1361 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], |
| 1362 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], |
| 1363 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], |
| 1364 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); |
| 1365 | VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1366 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], |
| 1367 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); |
| 1368 | VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1369 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], |
| 1370 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); |
| 1371 | VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", |
| 1372 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], |
| 1373 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); |
| 1374 | VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", |
| 1375 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], |
| 1376 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); |
| 1377 | VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1378 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], |
| 1379 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); |
| 1380 | VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1381 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], |
| 1382 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); |
| 1383 | VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1384 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], |
| 1385 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); |
| 1386 | VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1387 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], |
| 1388 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); |
| 1389 | VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", |
| 1390 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], |
| 1391 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); |
| 1392 | VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", |
| 1393 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], |
| 1394 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); |
| 1395 | VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", |
| 1396 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], |
| 1397 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); |
| 1398 | VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1399 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], |
| 1400 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); |
| 1401 | VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", |
| 1402 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], |
| 1403 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1404 | VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1405 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], |
| 1406 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1407 | VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1408 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], |
| 1409 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], |
| 1410 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], |
| 1411 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1412 | VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1413 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], |
| 1414 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], |
| 1415 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], |
| 1416 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1417 | |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1418 | return 0; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1421 | static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1422 | { |
| 1423 | int ret = -ENXIO; |
| 1424 | |
| 1425 | switch (attr->attr) { |
| 1426 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1427 | ret = kvm_s390_set_processor(kvm, attr); |
| 1428 | break; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1429 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1430 | ret = kvm_s390_set_processor_feat(kvm, attr); |
| 1431 | break; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1432 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
| 1433 | ret = kvm_s390_set_processor_subfunc(kvm, attr); |
| 1434 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1435 | } |
| 1436 | return ret; |
| 1437 | } |
| 1438 | |
| 1439 | static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1440 | { |
| 1441 | struct kvm_s390_vm_cpu_processor *proc; |
| 1442 | int ret = 0; |
| 1443 | |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 1444 | proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1445 | if (!proc) { |
| 1446 | ret = -ENOMEM; |
| 1447 | goto out; |
| 1448 | } |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 1449 | proc->cpuid = kvm->arch.model.cpuid; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1450 | proc->ibc = kvm->arch.model.ibc; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1451 | memcpy(&proc->fac_list, kvm->arch.model.fac_list, |
| 1452 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1453 | VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", |
| 1454 | kvm->arch.model.ibc, |
| 1455 | kvm->arch.model.cpuid); |
| 1456 | VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1457 | kvm->arch.model.fac_list[0], |
| 1458 | kvm->arch.model.fac_list[1], |
| 1459 | kvm->arch.model.fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1460 | if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) |
| 1461 | ret = -EFAULT; |
| 1462 | kfree(proc); |
| 1463 | out: |
| 1464 | return ret; |
| 1465 | } |
| 1466 | |
| 1467 | static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1468 | { |
| 1469 | struct kvm_s390_vm_cpu_machine *mach; |
| 1470 | int ret = 0; |
| 1471 | |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 1472 | mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1473 | if (!mach) { |
| 1474 | ret = -ENOMEM; |
| 1475 | goto out; |
| 1476 | } |
| 1477 | get_cpu_id((struct cpuid *) &mach->cpuid); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 1478 | mach->ibc = sclp.ibc; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1479 | memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, |
Michael Mueller | 981467c | 2015-02-24 13:51:04 +0100 | [diff] [blame] | 1480 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Sven Schnelle | 17e89e1 | 2021-05-05 22:01:10 +0200 | [diff] [blame] | 1481 | memcpy((unsigned long *)&mach->fac_list, stfle_fac_list, |
| 1482 | sizeof(stfle_fac_list)); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1483 | VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", |
| 1484 | kvm->arch.model.ibc, |
| 1485 | kvm->arch.model.cpuid); |
| 1486 | VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1487 | mach->fac_mask[0], |
| 1488 | mach->fac_mask[1], |
| 1489 | mach->fac_mask[2]); |
| 1490 | VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1491 | mach->fac_list[0], |
| 1492 | mach->fac_list[1], |
| 1493 | mach->fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1494 | if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) |
| 1495 | ret = -EFAULT; |
| 1496 | kfree(mach); |
| 1497 | out: |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1501 | static int kvm_s390_get_processor_feat(struct kvm *kvm, |
| 1502 | struct kvm_device_attr *attr) |
| 1503 | { |
| 1504 | struct kvm_s390_vm_cpu_feat data; |
| 1505 | |
| 1506 | bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, |
| 1507 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
| 1508 | if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) |
| 1509 | return -EFAULT; |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1510 | VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1511 | data.feat[0], |
| 1512 | data.feat[1], |
| 1513 | data.feat[2]); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | static int kvm_s390_get_machine_feat(struct kvm *kvm, |
| 1518 | struct kvm_device_attr *attr) |
| 1519 | { |
| 1520 | struct kvm_s390_vm_cpu_feat data; |
| 1521 | |
| 1522 | bitmap_copy((unsigned long *) data.feat, |
| 1523 | kvm_s390_available_cpu_feat, |
| 1524 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
| 1525 | if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) |
| 1526 | return -EFAULT; |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1527 | VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1528 | data.feat[0], |
| 1529 | data.feat[1], |
| 1530 | data.feat[2]); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1531 | return 0; |
| 1532 | } |
| 1533 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1534 | static int kvm_s390_get_processor_subfunc(struct kvm *kvm, |
| 1535 | struct kvm_device_attr *attr) |
| 1536 | { |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1537 | if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, |
| 1538 | sizeof(struct kvm_s390_vm_cpu_subfunc))) |
| 1539 | return -EFAULT; |
| 1540 | |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1541 | VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1542 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], |
| 1543 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], |
| 1544 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], |
| 1545 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); |
| 1546 | VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1547 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], |
| 1548 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); |
| 1549 | VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1550 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], |
| 1551 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); |
| 1552 | VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", |
| 1553 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], |
| 1554 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); |
| 1555 | VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", |
| 1556 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], |
| 1557 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); |
| 1558 | VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1559 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], |
| 1560 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); |
| 1561 | VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1562 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], |
| 1563 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); |
| 1564 | VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1565 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], |
| 1566 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); |
| 1567 | VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1568 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], |
| 1569 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); |
| 1570 | VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", |
| 1571 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], |
| 1572 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); |
| 1573 | VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", |
| 1574 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], |
| 1575 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); |
| 1576 | VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", |
| 1577 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], |
| 1578 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); |
| 1579 | VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1580 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], |
| 1581 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); |
| 1582 | VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", |
| 1583 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], |
| 1584 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1585 | VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1586 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], |
| 1587 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1588 | VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1589 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], |
| 1590 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], |
| 1591 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], |
| 1592 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1593 | VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1594 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], |
| 1595 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], |
| 1596 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], |
| 1597 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1598 | |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1599 | return 0; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | static int kvm_s390_get_machine_subfunc(struct kvm *kvm, |
| 1603 | struct kvm_device_attr *attr) |
| 1604 | { |
| 1605 | if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, |
| 1606 | sizeof(struct kvm_s390_vm_cpu_subfunc))) |
| 1607 | return -EFAULT; |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1608 | |
| 1609 | VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1610 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], |
| 1611 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], |
| 1612 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], |
| 1613 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); |
| 1614 | VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1615 | ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], |
| 1616 | ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); |
| 1617 | VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1618 | ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], |
| 1619 | ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); |
| 1620 | VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", |
| 1621 | ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], |
| 1622 | ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); |
| 1623 | VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", |
| 1624 | ((unsigned long *) &kvm_s390_available_subfunc.km)[0], |
| 1625 | ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); |
| 1626 | VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1627 | ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], |
| 1628 | ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); |
| 1629 | VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1630 | ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], |
| 1631 | ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); |
| 1632 | VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1633 | ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], |
| 1634 | ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); |
| 1635 | VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1636 | ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], |
| 1637 | ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); |
| 1638 | VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", |
| 1639 | ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], |
| 1640 | ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); |
| 1641 | VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", |
| 1642 | ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], |
| 1643 | ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); |
| 1644 | VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", |
| 1645 | ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], |
| 1646 | ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); |
| 1647 | VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1648 | ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], |
| 1649 | ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); |
| 1650 | VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", |
| 1651 | ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], |
| 1652 | ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1653 | VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1654 | ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], |
| 1655 | ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1656 | VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1657 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], |
| 1658 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], |
| 1659 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], |
| 1660 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1661 | VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1662 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], |
| 1663 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], |
| 1664 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], |
| 1665 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1666 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1667 | return 0; |
| 1668 | } |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1669 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1670 | static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1671 | { |
| 1672 | int ret = -ENXIO; |
| 1673 | |
| 1674 | switch (attr->attr) { |
| 1675 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1676 | ret = kvm_s390_get_processor(kvm, attr); |
| 1677 | break; |
| 1678 | case KVM_S390_VM_CPU_MACHINE: |
| 1679 | ret = kvm_s390_get_machine(kvm, attr); |
| 1680 | break; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1681 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1682 | ret = kvm_s390_get_processor_feat(kvm, attr); |
| 1683 | break; |
| 1684 | case KVM_S390_VM_CPU_MACHINE_FEAT: |
| 1685 | ret = kvm_s390_get_machine_feat(kvm, attr); |
| 1686 | break; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1687 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
| 1688 | ret = kvm_s390_get_processor_subfunc(kvm, attr); |
| 1689 | break; |
| 1690 | case KVM_S390_VM_CPU_MACHINE_SUBFUNC: |
| 1691 | ret = kvm_s390_get_machine_subfunc(kvm, attr); |
| 1692 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1693 | } |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1697 | static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1698 | { |
| 1699 | int ret; |
| 1700 | |
| 1701 | switch (attr->group) { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1702 | case KVM_S390_VM_MEM_CTRL: |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1703 | ret = kvm_s390_set_mem_control(kvm, attr); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1704 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1705 | case KVM_S390_VM_TOD: |
| 1706 | ret = kvm_s390_set_tod(kvm, attr); |
| 1707 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1708 | case KVM_S390_VM_CPU_MODEL: |
| 1709 | ret = kvm_s390_set_cpu_model(kvm, attr); |
| 1710 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1711 | case KVM_S390_VM_CRYPTO: |
| 1712 | ret = kvm_s390_vm_set_crypto(kvm, attr); |
| 1713 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1714 | case KVM_S390_VM_MIGRATION: |
| 1715 | ret = kvm_s390_vm_set_migration(kvm, attr); |
| 1716 | break; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1717 | default: |
| 1718 | ret = -ENXIO; |
| 1719 | break; |
| 1720 | } |
| 1721 | |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1726 | { |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1727 | int ret; |
| 1728 | |
| 1729 | switch (attr->group) { |
| 1730 | case KVM_S390_VM_MEM_CTRL: |
| 1731 | ret = kvm_s390_get_mem_control(kvm, attr); |
| 1732 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1733 | case KVM_S390_VM_TOD: |
| 1734 | ret = kvm_s390_get_tod(kvm, attr); |
| 1735 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1736 | case KVM_S390_VM_CPU_MODEL: |
| 1737 | ret = kvm_s390_get_cpu_model(kvm, attr); |
| 1738 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1739 | case KVM_S390_VM_MIGRATION: |
| 1740 | ret = kvm_s390_vm_get_migration(kvm, attr); |
| 1741 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1742 | default: |
| 1743 | ret = -ENXIO; |
| 1744 | break; |
| 1745 | } |
| 1746 | |
| 1747 | return ret; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1751 | { |
| 1752 | int ret; |
| 1753 | |
| 1754 | switch (attr->group) { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1755 | case KVM_S390_VM_MEM_CTRL: |
| 1756 | switch (attr->attr) { |
| 1757 | case KVM_S390_VM_MEM_ENABLE_CMMA: |
| 1758 | case KVM_S390_VM_MEM_CLR_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 1759 | ret = sclp.has_cmma ? 0 : -ENXIO; |
| 1760 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1761 | case KVM_S390_VM_MEM_LIMIT_SIZE: |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1762 | ret = 0; |
| 1763 | break; |
| 1764 | default: |
| 1765 | ret = -ENXIO; |
| 1766 | break; |
| 1767 | } |
| 1768 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1769 | case KVM_S390_VM_TOD: |
| 1770 | switch (attr->attr) { |
| 1771 | case KVM_S390_VM_TOD_LOW: |
| 1772 | case KVM_S390_VM_TOD_HIGH: |
| 1773 | ret = 0; |
| 1774 | break; |
| 1775 | default: |
| 1776 | ret = -ENXIO; |
| 1777 | break; |
| 1778 | } |
| 1779 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1780 | case KVM_S390_VM_CPU_MODEL: |
| 1781 | switch (attr->attr) { |
| 1782 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1783 | case KVM_S390_VM_CPU_MACHINE: |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1784 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1785 | case KVM_S390_VM_CPU_MACHINE_FEAT: |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1786 | case KVM_S390_VM_CPU_MACHINE_SUBFUNC: |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1787 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1788 | ret = 0; |
| 1789 | break; |
| 1790 | default: |
| 1791 | ret = -ENXIO; |
| 1792 | break; |
| 1793 | } |
| 1794 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1795 | case KVM_S390_VM_CRYPTO: |
| 1796 | switch (attr->attr) { |
| 1797 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: |
| 1798 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: |
| 1799 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: |
| 1800 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: |
| 1801 | ret = 0; |
| 1802 | break; |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 1803 | case KVM_S390_VM_CRYPTO_ENABLE_APIE: |
| 1804 | case KVM_S390_VM_CRYPTO_DISABLE_APIE: |
| 1805 | ret = ap_instructions_available() ? 0 : -ENXIO; |
| 1806 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1807 | default: |
| 1808 | ret = -ENXIO; |
| 1809 | break; |
| 1810 | } |
| 1811 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1812 | case KVM_S390_VM_MIGRATION: |
| 1813 | ret = 0; |
| 1814 | break; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1815 | default: |
| 1816 | ret = -ENXIO; |
| 1817 | break; |
| 1818 | } |
| 1819 | |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1823 | static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) |
| 1824 | { |
| 1825 | uint8_t *keys; |
| 1826 | uint64_t hva; |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1827 | int srcu_idx, i, r = 0; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1828 | |
| 1829 | if (args->flags != 0) |
| 1830 | return -EINVAL; |
| 1831 | |
| 1832 | /* Is this guest using storage keys? */ |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 1833 | if (!mm_uses_skeys(current->mm)) |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1834 | return KVM_S390_GET_SKEYS_NONE; |
| 1835 | |
| 1836 | /* Enforce sane limit on memory allocation */ |
| 1837 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) |
| 1838 | return -EINVAL; |
| 1839 | |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 1840 | keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1841 | if (!keys) |
| 1842 | return -ENOMEM; |
| 1843 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1844 | mmap_read_lock(current->mm); |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1845 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1846 | for (i = 0; i < args->count; i++) { |
| 1847 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 1848 | if (kvm_is_error_hva(hva)) { |
| 1849 | r = -EFAULT; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1850 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 1853 | r = get_guest_storage_key(current->mm, hva, &keys[i]); |
| 1854 | if (r) |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1855 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1856 | } |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1857 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1858 | mmap_read_unlock(current->mm); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1859 | |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1860 | if (!r) { |
| 1861 | r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, |
| 1862 | sizeof(uint8_t) * args->count); |
| 1863 | if (r) |
| 1864 | r = -EFAULT; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1865 | } |
| 1866 | |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1867 | kvfree(keys); |
| 1868 | return r; |
| 1869 | } |
| 1870 | |
| 1871 | static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) |
| 1872 | { |
| 1873 | uint8_t *keys; |
| 1874 | uint64_t hva; |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1875 | int srcu_idx, i, r = 0; |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1876 | bool unlocked; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1877 | |
| 1878 | if (args->flags != 0) |
| 1879 | return -EINVAL; |
| 1880 | |
| 1881 | /* Enforce sane limit on memory allocation */ |
| 1882 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) |
| 1883 | return -EINVAL; |
| 1884 | |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 1885 | keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1886 | if (!keys) |
| 1887 | return -ENOMEM; |
| 1888 | |
| 1889 | r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, |
| 1890 | sizeof(uint8_t) * args->count); |
| 1891 | if (r) { |
| 1892 | r = -EFAULT; |
| 1893 | goto out; |
| 1894 | } |
| 1895 | |
| 1896 | /* Enable storage key handling for the guest */ |
Dominik Dingel | 14d4a42 | 2015-05-07 15:16:13 +0200 | [diff] [blame] | 1897 | r = s390_enable_skey(); |
| 1898 | if (r) |
| 1899 | goto out; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1900 | |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1901 | i = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1902 | mmap_read_lock(current->mm); |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1903 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1904 | while (i < args->count) { |
| 1905 | unlocked = false; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1906 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 1907 | if (kvm_is_error_hva(hva)) { |
| 1908 | r = -EFAULT; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1909 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | /* Lowest order bit is reserved */ |
| 1913 | if (keys[i] & 0x01) { |
| 1914 | r = -EINVAL; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1915 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1916 | } |
| 1917 | |
David Hildenbrand | fe69eab | 2016-05-09 13:08:07 +0200 | [diff] [blame] | 1918 | r = set_guest_storage_key(current->mm, hva, keys[i], 0); |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1919 | if (r) { |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 1920 | r = fixup_user_fault(current->mm, hva, |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1921 | FAULT_FLAG_WRITE, &unlocked); |
| 1922 | if (r) |
| 1923 | break; |
| 1924 | } |
| 1925 | if (!r) |
| 1926 | i++; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1927 | } |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1928 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1929 | mmap_read_unlock(current->mm); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1930 | out: |
| 1931 | kvfree(keys); |
| 1932 | return r; |
| 1933 | } |
| 1934 | |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 1935 | /* |
| 1936 | * Base address and length must be sent at the start of each block, therefore |
| 1937 | * it's cheaper to send some clean data, as long as it's less than the size of |
| 1938 | * two longs. |
| 1939 | */ |
| 1940 | #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) |
| 1941 | /* for consistency */ |
| 1942 | #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) |
| 1943 | |
| 1944 | /* |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1945 | * Similar to gfn_to_memslot, but returns the index of a memslot also when the |
| 1946 | * address falls in a hole. In that case the index of one of the memslots |
| 1947 | * bordering the hole is returned. |
| 1948 | */ |
| 1949 | static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) |
| 1950 | { |
| 1951 | int start = 0, end = slots->used_slots; |
David Matlack | 8768927 | 2021-08-04 22:28:38 +0000 | [diff] [blame] | 1952 | int slot = atomic_read(&slots->last_used_slot); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1953 | struct kvm_memory_slot *memslots = slots->memslots; |
| 1954 | |
| 1955 | if (gfn >= memslots[slot].base_gfn && |
| 1956 | gfn < memslots[slot].base_gfn + memslots[slot].npages) |
| 1957 | return slot; |
| 1958 | |
| 1959 | while (start < end) { |
| 1960 | slot = start + (end - start) / 2; |
| 1961 | |
| 1962 | if (gfn >= memslots[slot].base_gfn) |
| 1963 | end = slot; |
| 1964 | else |
| 1965 | start = slot + 1; |
| 1966 | } |
| 1967 | |
Sean Christopherson | 97daa02 | 2020-04-07 23:40:59 -0700 | [diff] [blame] | 1968 | if (start >= slots->used_slots) |
| 1969 | return slots->used_slots - 1; |
| 1970 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1971 | if (gfn >= memslots[start].base_gfn && |
| 1972 | gfn < memslots[start].base_gfn + memslots[start].npages) { |
David Matlack | 8768927 | 2021-08-04 22:28:38 +0000 | [diff] [blame] | 1973 | atomic_set(&slots->last_used_slot, start); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | return start; |
| 1977 | } |
| 1978 | |
| 1979 | static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, |
| 1980 | u8 *res, unsigned long bufsize) |
| 1981 | { |
| 1982 | unsigned long pgstev, hva, cur_gfn = args->start_gfn; |
| 1983 | |
| 1984 | args->count = 0; |
| 1985 | while (args->count < bufsize) { |
| 1986 | hva = gfn_to_hva(kvm, cur_gfn); |
| 1987 | /* |
| 1988 | * We return an error if the first value was invalid, but we |
| 1989 | * return successfully if at least one value was copied. |
| 1990 | */ |
| 1991 | if (kvm_is_error_hva(hva)) |
| 1992 | return args->count ? 0 : -EFAULT; |
| 1993 | if (get_pgste(kvm->mm, hva, &pgstev) < 0) |
| 1994 | pgstev = 0; |
| 1995 | res[args->count++] = (pgstev >> 24) & 0x43; |
| 1996 | cur_gfn++; |
| 1997 | } |
| 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, |
| 2003 | unsigned long cur_gfn) |
| 2004 | { |
| 2005 | int slotidx = gfn_to_memslot_approx(slots, cur_gfn); |
| 2006 | struct kvm_memory_slot *ms = slots->memslots + slotidx; |
| 2007 | unsigned long ofs = cur_gfn - ms->base_gfn; |
| 2008 | |
| 2009 | if (ms->base_gfn + ms->npages <= cur_gfn) { |
| 2010 | slotidx--; |
| 2011 | /* If we are above the highest slot, wrap around */ |
| 2012 | if (slotidx < 0) |
| 2013 | slotidx = slots->used_slots - 1; |
| 2014 | |
| 2015 | ms = slots->memslots + slotidx; |
| 2016 | ofs = 0; |
| 2017 | } |
| 2018 | ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); |
| 2019 | while ((slotidx > 0) && (ofs >= ms->npages)) { |
| 2020 | slotidx--; |
| 2021 | ms = slots->memslots + slotidx; |
| 2022 | ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); |
| 2023 | } |
| 2024 | return ms->base_gfn + ofs; |
| 2025 | } |
| 2026 | |
| 2027 | static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, |
| 2028 | u8 *res, unsigned long bufsize) |
| 2029 | { |
| 2030 | unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; |
| 2031 | struct kvm_memslots *slots = kvm_memslots(kvm); |
| 2032 | struct kvm_memory_slot *ms; |
| 2033 | |
Sean Christopherson | 0774a96 | 2020-03-20 13:55:40 -0700 | [diff] [blame] | 2034 | if (unlikely(!slots->used_slots)) |
| 2035 | return 0; |
| 2036 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2037 | cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); |
| 2038 | ms = gfn_to_memslot(kvm, cur_gfn); |
| 2039 | args->count = 0; |
| 2040 | args->start_gfn = cur_gfn; |
| 2041 | if (!ms) |
| 2042 | return 0; |
| 2043 | next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); |
| 2044 | mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; |
| 2045 | |
| 2046 | while (args->count < bufsize) { |
| 2047 | hva = gfn_to_hva(kvm, cur_gfn); |
| 2048 | if (kvm_is_error_hva(hva)) |
| 2049 | return 0; |
| 2050 | /* Decrement only if we actually flipped the bit to 0 */ |
| 2051 | if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) |
| 2052 | atomic64_dec(&kvm->arch.cmma_dirty_pages); |
| 2053 | if (get_pgste(kvm->mm, hva, &pgstev) < 0) |
| 2054 | pgstev = 0; |
| 2055 | /* Save the value */ |
| 2056 | res[args->count++] = (pgstev >> 24) & 0x43; |
| 2057 | /* If the next bit is too far away, stop. */ |
| 2058 | if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) |
| 2059 | return 0; |
| 2060 | /* If we reached the previous "next", find the next one */ |
| 2061 | if (cur_gfn == next_gfn) |
| 2062 | next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); |
| 2063 | /* Reached the end of memory or of the buffer, stop */ |
| 2064 | if ((next_gfn >= mem_end) || |
| 2065 | (next_gfn - args->start_gfn >= bufsize)) |
| 2066 | return 0; |
| 2067 | cur_gfn++; |
| 2068 | /* Reached the end of the current memslot, take the next one. */ |
| 2069 | if (cur_gfn - ms->base_gfn >= ms->npages) { |
| 2070 | ms = gfn_to_memslot(kvm, cur_gfn); |
| 2071 | if (!ms) |
| 2072 | return 0; |
| 2073 | } |
| 2074 | } |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | /* |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2079 | * This function searches for the next page with dirty CMMA attributes, and |
| 2080 | * saves the attributes in the buffer up to either the end of the buffer or |
| 2081 | * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; |
| 2082 | * no trailing clean bytes are saved. |
| 2083 | * In case no dirty bits were found, or if CMMA was not enabled or used, the |
| 2084 | * output buffer will indicate 0 as length. |
| 2085 | */ |
| 2086 | static int kvm_s390_get_cmma_bits(struct kvm *kvm, |
| 2087 | struct kvm_s390_cmma_log *args) |
| 2088 | { |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2089 | unsigned long bufsize; |
| 2090 | int srcu_idx, peek, ret; |
| 2091 | u8 *values; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2092 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2093 | if (!kvm->arch.use_cmma) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2094 | return -ENXIO; |
| 2095 | /* Invalid/unsupported flags were specified */ |
| 2096 | if (args->flags & ~KVM_S390_CMMA_PEEK) |
| 2097 | return -EINVAL; |
| 2098 | /* Migration mode query, and we are not doing a migration */ |
| 2099 | peek = !!(args->flags & KVM_S390_CMMA_PEEK); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2100 | if (!peek && !kvm->arch.migration_mode) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2101 | return -EINVAL; |
| 2102 | /* CMMA is disabled or was not used, or the buffer has length zero */ |
| 2103 | bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2104 | if (!bufsize || !kvm->mm->context.uses_cmm) { |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2105 | memset(args, 0, sizeof(*args)); |
| 2106 | return 0; |
| 2107 | } |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2108 | /* We are not peeking, and there are no dirty pages */ |
| 2109 | if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { |
| 2110 | memset(args, 0, sizeof(*args)); |
| 2111 | return 0; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2114 | values = vmalloc(bufsize); |
| 2115 | if (!values) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2116 | return -ENOMEM; |
| 2117 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2118 | mmap_read_lock(kvm->mm); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2119 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2120 | if (peek) |
| 2121 | ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); |
| 2122 | else |
| 2123 | ret = kvm_s390_get_cmma(kvm, args, values, bufsize); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2124 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2125 | mmap_read_unlock(kvm->mm); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2126 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2127 | if (kvm->arch.migration_mode) |
| 2128 | args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); |
| 2129 | else |
| 2130 | args->remaining = 0; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2131 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2132 | if (copy_to_user((void __user *)args->values, values, args->count)) |
| 2133 | ret = -EFAULT; |
| 2134 | |
| 2135 | vfree(values); |
| 2136 | return ret; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * This function sets the CMMA attributes for the given pages. If the input |
| 2141 | * buffer has zero length, no action is taken, otherwise the attributes are |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2142 | * set and the mm->context.uses_cmm flag is set. |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2143 | */ |
| 2144 | static int kvm_s390_set_cmma_bits(struct kvm *kvm, |
| 2145 | const struct kvm_s390_cmma_log *args) |
| 2146 | { |
| 2147 | unsigned long hva, mask, pgstev, i; |
| 2148 | uint8_t *bits; |
| 2149 | int srcu_idx, r = 0; |
| 2150 | |
| 2151 | mask = args->mask; |
| 2152 | |
| 2153 | if (!kvm->arch.use_cmma) |
| 2154 | return -ENXIO; |
| 2155 | /* invalid/unsupported flags */ |
| 2156 | if (args->flags != 0) |
| 2157 | return -EINVAL; |
| 2158 | /* Enforce sane limit on memory allocation */ |
| 2159 | if (args->count > KVM_S390_CMMA_SIZE_MAX) |
| 2160 | return -EINVAL; |
| 2161 | /* Nothing to do */ |
| 2162 | if (args->count == 0) |
| 2163 | return 0; |
| 2164 | |
Kees Cook | 42bc47b | 2018-06-12 14:27:11 -0700 | [diff] [blame] | 2165 | bits = vmalloc(array_size(sizeof(*bits), args->count)); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2166 | if (!bits) |
| 2167 | return -ENOMEM; |
| 2168 | |
| 2169 | r = copy_from_user(bits, (void __user *)args->values, args->count); |
| 2170 | if (r) { |
| 2171 | r = -EFAULT; |
| 2172 | goto out; |
| 2173 | } |
| 2174 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2175 | mmap_read_lock(kvm->mm); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2176 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 2177 | for (i = 0; i < args->count; i++) { |
| 2178 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 2179 | if (kvm_is_error_hva(hva)) { |
| 2180 | r = -EFAULT; |
| 2181 | break; |
| 2182 | } |
| 2183 | |
| 2184 | pgstev = bits[i]; |
| 2185 | pgstev = pgstev << 24; |
Claudio Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 2186 | mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2187 | set_pgste_bits(kvm->mm, hva, mask, pgstev); |
| 2188 | } |
| 2189 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2190 | mmap_read_unlock(kvm->mm); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2191 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2192 | if (!kvm->mm->context.uses_cmm) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2193 | mmap_write_lock(kvm->mm); |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2194 | kvm->mm->context.uses_cmm = 1; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2195 | mmap_write_unlock(kvm->mm); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2196 | } |
| 2197 | out: |
| 2198 | vfree(bits); |
| 2199 | return r; |
| 2200 | } |
| 2201 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2202 | static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp) |
| 2203 | { |
| 2204 | struct kvm_vcpu *vcpu; |
| 2205 | u16 rc, rrc; |
| 2206 | int ret = 0; |
| 2207 | int i; |
| 2208 | |
| 2209 | /* |
| 2210 | * We ignore failures and try to destroy as many CPUs as possible. |
| 2211 | * At the same time we must not free the assigned resources when |
| 2212 | * this fails, as the ultravisor has still access to that memory. |
| 2213 | * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak |
| 2214 | * behind. |
| 2215 | * We want to return the first failure rc and rrc, though. |
| 2216 | */ |
| 2217 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2218 | mutex_lock(&vcpu->mutex); |
| 2219 | if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) { |
| 2220 | *rcp = rc; |
| 2221 | *rrcp = rrc; |
| 2222 | ret = -EIO; |
| 2223 | } |
| 2224 | mutex_unlock(&vcpu->mutex); |
| 2225 | } |
| 2226 | return ret; |
| 2227 | } |
| 2228 | |
| 2229 | static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) |
| 2230 | { |
| 2231 | int i, r = 0; |
| 2232 | u16 dummy; |
| 2233 | |
| 2234 | struct kvm_vcpu *vcpu; |
| 2235 | |
| 2236 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2237 | mutex_lock(&vcpu->mutex); |
| 2238 | r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); |
| 2239 | mutex_unlock(&vcpu->mutex); |
| 2240 | if (r) |
| 2241 | break; |
| 2242 | } |
| 2243 | if (r) |
| 2244 | kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); |
| 2245 | return r; |
| 2246 | } |
| 2247 | |
| 2248 | static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) |
| 2249 | { |
| 2250 | int r = 0; |
| 2251 | u16 dummy; |
| 2252 | void __user *argp = (void __user *)cmd->data; |
| 2253 | |
| 2254 | switch (cmd->cmd) { |
| 2255 | case KVM_PV_ENABLE: { |
| 2256 | r = -EINVAL; |
| 2257 | if (kvm_s390_pv_is_protected(kvm)) |
| 2258 | break; |
| 2259 | |
| 2260 | /* |
| 2261 | * FMT 4 SIE needs esca. As we never switch back to bsca from |
| 2262 | * esca, we need no cleanup in the error cases below |
| 2263 | */ |
| 2264 | r = sca_switch_to_extended(kvm); |
| 2265 | if (r) |
| 2266 | break; |
| 2267 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2268 | mmap_write_lock(current->mm); |
Janosch Frank | fa0c5ea | 2019-07-16 13:08:37 +0200 | [diff] [blame] | 2269 | r = gmap_mark_unmergeable(); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2270 | mmap_write_unlock(current->mm); |
Janosch Frank | fa0c5ea | 2019-07-16 13:08:37 +0200 | [diff] [blame] | 2271 | if (r) |
| 2272 | break; |
| 2273 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2274 | r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); |
| 2275 | if (r) |
| 2276 | break; |
| 2277 | |
| 2278 | r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); |
| 2279 | if (r) |
| 2280 | kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); |
Christian Borntraeger | 0890dde | 2020-02-03 09:13:37 +0100 | [diff] [blame] | 2281 | |
| 2282 | /* we need to block service interrupts from now on */ |
| 2283 | set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2284 | break; |
| 2285 | } |
| 2286 | case KVM_PV_DISABLE: { |
| 2287 | r = -EINVAL; |
| 2288 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2289 | break; |
| 2290 | |
| 2291 | r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); |
| 2292 | /* |
| 2293 | * If a CPU could not be destroyed, destroy VM will also fail. |
| 2294 | * There is no point in trying to destroy it. Instead return |
| 2295 | * the rc and rrc from the first CPU that failed destroying. |
| 2296 | */ |
| 2297 | if (r) |
| 2298 | break; |
| 2299 | r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc); |
Christian Borntraeger | 0890dde | 2020-02-03 09:13:37 +0100 | [diff] [blame] | 2300 | |
| 2301 | /* no need to block service interrupts any more */ |
| 2302 | clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2303 | break; |
| 2304 | } |
| 2305 | case KVM_PV_SET_SEC_PARMS: { |
| 2306 | struct kvm_s390_pv_sec_parm parms = {}; |
| 2307 | void *hdr; |
| 2308 | |
| 2309 | r = -EINVAL; |
| 2310 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2311 | break; |
| 2312 | |
| 2313 | r = -EFAULT; |
| 2314 | if (copy_from_user(&parms, argp, sizeof(parms))) |
| 2315 | break; |
| 2316 | |
| 2317 | /* Currently restricted to 8KB */ |
| 2318 | r = -EINVAL; |
| 2319 | if (parms.length > PAGE_SIZE * 2) |
| 2320 | break; |
| 2321 | |
| 2322 | r = -ENOMEM; |
| 2323 | hdr = vmalloc(parms.length); |
| 2324 | if (!hdr) |
| 2325 | break; |
| 2326 | |
| 2327 | r = -EFAULT; |
| 2328 | if (!copy_from_user(hdr, (void __user *)parms.origin, |
| 2329 | parms.length)) |
| 2330 | r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, |
| 2331 | &cmd->rc, &cmd->rrc); |
| 2332 | |
| 2333 | vfree(hdr); |
| 2334 | break; |
| 2335 | } |
| 2336 | case KVM_PV_UNPACK: { |
| 2337 | struct kvm_s390_pv_unp unp = {}; |
| 2338 | |
| 2339 | r = -EINVAL; |
Janosch Frank | 1ed576a | 2020-10-20 06:12:07 -0400 | [diff] [blame] | 2340 | if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2341 | break; |
| 2342 | |
| 2343 | r = -EFAULT; |
| 2344 | if (copy_from_user(&unp, argp, sizeof(unp))) |
| 2345 | break; |
| 2346 | |
| 2347 | r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, |
| 2348 | &cmd->rc, &cmd->rrc); |
| 2349 | break; |
| 2350 | } |
| 2351 | case KVM_PV_VERIFY: { |
| 2352 | r = -EINVAL; |
| 2353 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2354 | break; |
| 2355 | |
| 2356 | r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), |
| 2357 | UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); |
| 2358 | KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, |
| 2359 | cmd->rrc); |
| 2360 | break; |
| 2361 | } |
Janosch Frank | e0d2773 | 2019-05-09 13:07:21 +0200 | [diff] [blame] | 2362 | case KVM_PV_PREP_RESET: { |
| 2363 | r = -EINVAL; |
| 2364 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2365 | break; |
| 2366 | |
| 2367 | r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), |
| 2368 | UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); |
| 2369 | KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", |
| 2370 | cmd->rc, cmd->rrc); |
| 2371 | break; |
| 2372 | } |
| 2373 | case KVM_PV_UNSHARE_ALL: { |
| 2374 | r = -EINVAL; |
| 2375 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2376 | break; |
| 2377 | |
| 2378 | r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), |
| 2379 | UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); |
| 2380 | KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", |
| 2381 | cmd->rc, cmd->rrc); |
| 2382 | break; |
| 2383 | } |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2384 | default: |
| 2385 | r = -ENOTTY; |
| 2386 | } |
| 2387 | return r; |
| 2388 | } |
| 2389 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2390 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2391 | unsigned int ioctl, unsigned long arg) |
| 2392 | { |
| 2393 | struct kvm *kvm = filp->private_data; |
| 2394 | void __user *argp = (void __user *)arg; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 2395 | struct kvm_device_attr attr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2396 | int r; |
| 2397 | |
| 2398 | switch (ioctl) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2399 | case KVM_S390_INTERRUPT: { |
| 2400 | struct kvm_s390_interrupt s390int; |
| 2401 | |
| 2402 | r = -EFAULT; |
| 2403 | if (copy_from_user(&s390int, argp, sizeof(s390int))) |
| 2404 | break; |
| 2405 | r = kvm_s390_inject_vm(kvm, &s390int); |
| 2406 | break; |
| 2407 | } |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2408 | case KVM_CREATE_IRQCHIP: { |
| 2409 | struct kvm_irq_routing_entry routing; |
| 2410 | |
| 2411 | r = -EINVAL; |
| 2412 | if (kvm->arch.use_irqchip) { |
| 2413 | /* Set up dummy routing. */ |
| 2414 | memset(&routing, 0, sizeof(routing)); |
Nicholas Krause | 152b283 | 2015-08-06 13:05:54 -0400 | [diff] [blame] | 2415 | r = kvm_set_irq_routing(kvm, &routing, 0, 0); |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2416 | } |
| 2417 | break; |
| 2418 | } |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 2419 | case KVM_SET_DEVICE_ATTR: { |
| 2420 | r = -EFAULT; |
| 2421 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2422 | break; |
| 2423 | r = kvm_s390_vm_set_attr(kvm, &attr); |
| 2424 | break; |
| 2425 | } |
| 2426 | case KVM_GET_DEVICE_ATTR: { |
| 2427 | r = -EFAULT; |
| 2428 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2429 | break; |
| 2430 | r = kvm_s390_vm_get_attr(kvm, &attr); |
| 2431 | break; |
| 2432 | } |
| 2433 | case KVM_HAS_DEVICE_ATTR: { |
| 2434 | r = -EFAULT; |
| 2435 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2436 | break; |
| 2437 | r = kvm_s390_vm_has_attr(kvm, &attr); |
| 2438 | break; |
| 2439 | } |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 2440 | case KVM_S390_GET_SKEYS: { |
| 2441 | struct kvm_s390_skeys args; |
| 2442 | |
| 2443 | r = -EFAULT; |
| 2444 | if (copy_from_user(&args, argp, |
| 2445 | sizeof(struct kvm_s390_skeys))) |
| 2446 | break; |
| 2447 | r = kvm_s390_get_skeys(kvm, &args); |
| 2448 | break; |
| 2449 | } |
| 2450 | case KVM_S390_SET_SKEYS: { |
| 2451 | struct kvm_s390_skeys args; |
| 2452 | |
| 2453 | r = -EFAULT; |
| 2454 | if (copy_from_user(&args, argp, |
| 2455 | sizeof(struct kvm_s390_skeys))) |
| 2456 | break; |
| 2457 | r = kvm_s390_set_skeys(kvm, &args); |
| 2458 | break; |
| 2459 | } |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2460 | case KVM_S390_GET_CMMA_BITS: { |
| 2461 | struct kvm_s390_cmma_log args; |
| 2462 | |
| 2463 | r = -EFAULT; |
| 2464 | if (copy_from_user(&args, argp, sizeof(args))) |
| 2465 | break; |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2466 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2467 | r = kvm_s390_get_cmma_bits(kvm, &args); |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2468 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2469 | if (!r) { |
| 2470 | r = copy_to_user(argp, &args, sizeof(args)); |
| 2471 | if (r) |
| 2472 | r = -EFAULT; |
| 2473 | } |
| 2474 | break; |
| 2475 | } |
| 2476 | case KVM_S390_SET_CMMA_BITS: { |
| 2477 | struct kvm_s390_cmma_log args; |
| 2478 | |
| 2479 | r = -EFAULT; |
| 2480 | if (copy_from_user(&args, argp, sizeof(args))) |
| 2481 | break; |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2482 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2483 | r = kvm_s390_set_cmma_bits(kvm, &args); |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2484 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2485 | break; |
| 2486 | } |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2487 | case KVM_S390_PV_COMMAND: { |
| 2488 | struct kvm_pv_cmd args; |
| 2489 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 2490 | /* protvirt means user sigp */ |
| 2491 | kvm->arch.user_cpu_state_ctrl = 1; |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2492 | r = 0; |
| 2493 | if (!is_prot_virt_host()) { |
| 2494 | r = -EINVAL; |
| 2495 | break; |
| 2496 | } |
| 2497 | if (copy_from_user(&args, argp, sizeof(args))) { |
| 2498 | r = -EFAULT; |
| 2499 | break; |
| 2500 | } |
| 2501 | if (args.flags) { |
| 2502 | r = -EINVAL; |
| 2503 | break; |
| 2504 | } |
| 2505 | mutex_lock(&kvm->lock); |
| 2506 | r = kvm_s390_handle_pv(kvm, &args); |
| 2507 | mutex_unlock(&kvm->lock); |
| 2508 | if (copy_to_user(argp, &args, sizeof(args))) { |
| 2509 | r = -EFAULT; |
| 2510 | break; |
| 2511 | } |
| 2512 | break; |
| 2513 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2514 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2515 | r = -ENOTTY; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | return r; |
| 2519 | } |
| 2520 | |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2521 | static int kvm_s390_apxa_installed(void) |
| 2522 | { |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2523 | struct ap_config_info info; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2524 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2525 | if (ap_instructions_available()) { |
| 2526 | if (ap_qci(&info) == 0) |
| 2527 | return info.apxa; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2533 | /* |
| 2534 | * The format of the crypto control block (CRYCB) is specified in the 3 low |
| 2535 | * order bits of the CRYCB designation (CRYCBD) field as follows: |
| 2536 | * Format 0: Neither the message security assist extension 3 (MSAX3) nor the |
| 2537 | * AP extended addressing (APXA) facility are installed. |
| 2538 | * Format 1: The APXA facility is not installed but the MSAX3 facility is. |
| 2539 | * Format 2: Both the APXA and MSAX3 facilities are installed |
| 2540 | */ |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2541 | static void kvm_s390_set_crycb_format(struct kvm *kvm) |
| 2542 | { |
| 2543 | kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; |
| 2544 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2545 | /* Clear the CRYCB format bits - i.e., set format 0 by default */ |
| 2546 | kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); |
| 2547 | |
| 2548 | /* Check whether MSAX3 is installed */ |
| 2549 | if (!test_kvm_facility(kvm, 76)) |
| 2550 | return; |
| 2551 | |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2552 | if (kvm_s390_apxa_installed()) |
| 2553 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; |
| 2554 | else |
| 2555 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; |
| 2556 | } |
| 2557 | |
Tony Krowiak | 86956e7 | 2021-08-23 17:20:47 -0400 | [diff] [blame] | 2558 | /* |
| 2559 | * kvm_arch_crypto_set_masks |
| 2560 | * |
| 2561 | * @kvm: pointer to the target guest's KVM struct containing the crypto masks |
| 2562 | * to be set. |
| 2563 | * @apm: the mask identifying the accessible AP adapters |
| 2564 | * @aqm: the mask identifying the accessible AP domains |
| 2565 | * @adm: the mask identifying the accessible AP control domains |
| 2566 | * |
| 2567 | * Set the masks that identify the adapters, domains and control domains to |
| 2568 | * which the KVM guest is granted access. |
| 2569 | * |
| 2570 | * Note: The kvm->lock mutex must be locked by the caller before invoking this |
| 2571 | * function. |
| 2572 | */ |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2573 | void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, |
| 2574 | unsigned long *aqm, unsigned long *adm) |
| 2575 | { |
| 2576 | struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; |
| 2577 | |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2578 | kvm_s390_vcpu_block_all(kvm); |
| 2579 | |
| 2580 | switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { |
| 2581 | case CRYCB_FORMAT2: /* APCB1 use 256 bits */ |
| 2582 | memcpy(crycb->apcb1.apm, apm, 32); |
| 2583 | VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", |
| 2584 | apm[0], apm[1], apm[2], apm[3]); |
| 2585 | memcpy(crycb->apcb1.aqm, aqm, 32); |
| 2586 | VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", |
| 2587 | aqm[0], aqm[1], aqm[2], aqm[3]); |
| 2588 | memcpy(crycb->apcb1.adm, adm, 32); |
| 2589 | VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", |
| 2590 | adm[0], adm[1], adm[2], adm[3]); |
| 2591 | break; |
| 2592 | case CRYCB_FORMAT1: |
| 2593 | case CRYCB_FORMAT0: /* Fall through both use APCB0 */ |
| 2594 | memcpy(crycb->apcb0.apm, apm, 8); |
| 2595 | memcpy(crycb->apcb0.aqm, aqm, 2); |
| 2596 | memcpy(crycb->apcb0.adm, adm, 2); |
| 2597 | VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", |
| 2598 | apm[0], *((unsigned short *)aqm), |
| 2599 | *((unsigned short *)adm)); |
| 2600 | break; |
| 2601 | default: /* Can not happen */ |
| 2602 | break; |
| 2603 | } |
| 2604 | |
| 2605 | /* recreate the shadow crycb for each vcpu */ |
| 2606 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); |
| 2607 | kvm_s390_vcpu_unblock_all(kvm); |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2608 | } |
| 2609 | EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); |
| 2610 | |
Tony Krowiak | 86956e7 | 2021-08-23 17:20:47 -0400 | [diff] [blame] | 2611 | /* |
| 2612 | * kvm_arch_crypto_clear_masks |
| 2613 | * |
| 2614 | * @kvm: pointer to the target guest's KVM struct containing the crypto masks |
| 2615 | * to be cleared. |
| 2616 | * |
| 2617 | * Clear the masks that identify the adapters, domains and control domains to |
| 2618 | * which the KVM guest is granted access. |
| 2619 | * |
| 2620 | * Note: The kvm->lock mutex must be locked by the caller before invoking this |
| 2621 | * function. |
| 2622 | */ |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2623 | void kvm_arch_crypto_clear_masks(struct kvm *kvm) |
| 2624 | { |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2625 | kvm_s390_vcpu_block_all(kvm); |
| 2626 | |
| 2627 | memset(&kvm->arch.crypto.crycb->apcb0, 0, |
| 2628 | sizeof(kvm->arch.crypto.crycb->apcb0)); |
| 2629 | memset(&kvm->arch.crypto.crycb->apcb1, 0, |
| 2630 | sizeof(kvm->arch.crypto.crycb->apcb1)); |
| 2631 | |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2632 | VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); |
Pierre Morel | 6cc571b | 2018-09-25 19:16:30 -0400 | [diff] [blame] | 2633 | /* recreate the shadow crycb for each vcpu */ |
| 2634 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2635 | kvm_s390_vcpu_unblock_all(kvm); |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2636 | } |
| 2637 | EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); |
| 2638 | |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2639 | static u64 kvm_s390_get_initial_cpuid(void) |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2640 | { |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2641 | struct cpuid cpuid; |
| 2642 | |
| 2643 | get_cpu_id(&cpuid); |
| 2644 | cpuid.version = 0xff; |
| 2645 | return *((u64 *) &cpuid); |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2646 | } |
| 2647 | |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2648 | static void kvm_s390_crypto_init(struct kvm *kvm) |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2649 | { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2650 | kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2651 | kvm_s390_set_crycb_format(kvm); |
Tony Krowiak | 1e75373 | 2021-08-23 17:20:46 -0400 | [diff] [blame] | 2652 | init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2653 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2654 | if (!test_kvm_facility(kvm, 76)) |
| 2655 | return; |
| 2656 | |
Tony Krowiak | ed6f76b | 2015-02-24 14:06:57 -0500 | [diff] [blame] | 2657 | /* Enable AES/DEA protected key functions by default */ |
| 2658 | kvm->arch.crypto.aes_kw = 1; |
| 2659 | kvm->arch.crypto.dea_kw = 1; |
| 2660 | get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, |
| 2661 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
| 2662 | get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, |
| 2663 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2664 | } |
| 2665 | |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2666 | static void sca_dispose(struct kvm *kvm) |
| 2667 | { |
| 2668 | if (kvm->arch.use_esca) |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2669 | free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2670 | else |
| 2671 | free_page((unsigned long)(kvm->arch.sca)); |
| 2672 | kvm->arch.sca = NULL; |
| 2673 | } |
| 2674 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2675 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2676 | { |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 2677 | gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2678 | int i, rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2679 | char debug_name[16]; |
Christian Borntraeger | f6c137f | 2014-03-19 11:18:29 +0100 | [diff] [blame] | 2680 | static unsigned long sca_offset; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2681 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2682 | rc = -EINVAL; |
| 2683 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 2684 | if (type & ~KVM_VM_S390_UCONTROL) |
| 2685 | goto out_err; |
| 2686 | if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) |
| 2687 | goto out_err; |
| 2688 | #else |
| 2689 | if (type) |
| 2690 | goto out_err; |
| 2691 | #endif |
| 2692 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2693 | rc = s390_enable_sie(); |
| 2694 | if (rc) |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2695 | goto out_err; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2696 | |
Carsten Otte | b290411 | 2011-10-18 12:27:13 +0200 | [diff] [blame] | 2697 | rc = -ENOMEM; |
| 2698 | |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2699 | if (!sclp.has_64bscao) |
| 2700 | alloc_flags |= GFP_DMA; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2701 | rwlock_init(&kvm->arch.sca_lock); |
David Hildenbrand | 9ac96d7 | 2018-04-27 14:36:12 +0200 | [diff] [blame] | 2702 | /* start with basic SCA */ |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2703 | kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2704 | if (!kvm->arch.sca) |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2705 | goto out_err; |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 2706 | mutex_lock(&kvm_lock); |
David Hildenbrand | c5c2c39 | 2015-10-26 08:41:29 +0100 | [diff] [blame] | 2707 | sca_offset += 16; |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2708 | if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) |
David Hildenbrand | c5c2c39 | 2015-10-26 08:41:29 +0100 | [diff] [blame] | 2709 | sca_offset = 0; |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2710 | kvm->arch.sca = (struct bsca_block *) |
| 2711 | ((char *) kvm->arch.sca + sca_offset); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 2712 | mutex_unlock(&kvm_lock); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2713 | |
| 2714 | sprintf(debug_name, "kvm-%u", current->pid); |
| 2715 | |
Christian Borntraeger | 1cb9cf7 | 2015-07-20 15:04:48 +0200 | [diff] [blame] | 2716 | kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2717 | if (!kvm->arch.dbf) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2718 | goto out_err; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2719 | |
Michael Mueller | 19114be | 2017-05-30 14:26:02 +0200 | [diff] [blame] | 2720 | BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2721 | kvm->arch.sie_page2 = |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 2722 | (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2723 | if (!kvm->arch.sie_page2) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2724 | goto out_err; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2725 | |
Michael Mueller | 25c84db | 2019-01-31 09:52:41 +0100 | [diff] [blame] | 2726 | kvm->arch.sie_page2->kvm = kvm; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2727 | kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 2728 | |
| 2729 | for (i = 0; i < kvm_s390_fac_size(); i++) { |
Sven Schnelle | 17e89e1 | 2021-05-05 22:01:10 +0200 | [diff] [blame] | 2730 | kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 2731 | (kvm_s390_fac_base[i] | |
| 2732 | kvm_s390_fac_ext[i]); |
Sven Schnelle | 17e89e1 | 2021-05-05 22:01:10 +0200 | [diff] [blame] | 2733 | kvm->arch.model.fac_list[i] = stfle_fac_list[i] & |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 2734 | kvm_s390_fac_base[i]; |
| 2735 | } |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 2736 | kvm->arch.model.subfuncs = kvm_s390_available_subfunc; |
Michael Mueller | 981467c | 2015-02-24 13:51:04 +0100 | [diff] [blame] | 2737 | |
David Hildenbrand | 1935222 | 2017-08-29 16:31:08 +0200 | [diff] [blame] | 2738 | /* we are always in czam mode - even on pre z14 machines */ |
| 2739 | set_kvm_facility(kvm->arch.model.fac_mask, 138); |
| 2740 | set_kvm_facility(kvm->arch.model.fac_list, 138); |
| 2741 | /* we emulate STHYI in kvm */ |
Janosch Frank | 95ca2cb | 2016-05-23 15:11:58 +0200 | [diff] [blame] | 2742 | set_kvm_facility(kvm->arch.model.fac_mask, 74); |
| 2743 | set_kvm_facility(kvm->arch.model.fac_list, 74); |
Claudio Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 2744 | if (MACHINE_HAS_TLB_GUEST) { |
| 2745 | set_kvm_facility(kvm->arch.model.fac_mask, 147); |
| 2746 | set_kvm_facility(kvm->arch.model.fac_list, 147); |
| 2747 | } |
Janosch Frank | 95ca2cb | 2016-05-23 15:11:58 +0200 | [diff] [blame] | 2748 | |
Pierre Morel | 05f31e3 | 2019-05-21 17:34:37 +0200 | [diff] [blame] | 2749 | if (css_general_characteristics.aiv && test_facility(65)) |
| 2750 | set_kvm_facility(kvm->arch.model.fac_mask, 65); |
| 2751 | |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2752 | kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 2753 | kvm->arch.model.ibc = sclp.ibc & 0x0fff; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2754 | |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2755 | kvm_s390_crypto_init(kvm); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2756 | |
Fei Li | 5197839 | 2017-02-17 17:06:26 +0800 | [diff] [blame] | 2757 | mutex_init(&kvm->arch.float_int.ais_lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2758 | spin_lock_init(&kvm->arch.float_int.lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 2759 | for (i = 0; i < FIRQ_LIST_COUNT; i++) |
| 2760 | INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 2761 | init_waitqueue_head(&kvm->arch.ipte_wq); |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 2762 | mutex_init(&kvm->arch.ipte_mutex); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2763 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2764 | debug_register_view(kvm->arch.dbf, &debug_sprintf_view); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 2765 | VM_EVENT(kvm, 3, "vm created with type %lu", type); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2766 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2767 | if (type & KVM_VM_S390_UCONTROL) { |
| 2768 | kvm->arch.gmap = NULL; |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2769 | kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2770 | } else { |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2771 | if (sclp.hamax == U64_MAX) |
Martin Schwidefsky | ee71d16 | 2017-04-20 14:43:51 +0200 | [diff] [blame] | 2772 | kvm->arch.mem_limit = TASK_SIZE_MAX; |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2773 | else |
Martin Schwidefsky | ee71d16 | 2017-04-20 14:43:51 +0200 | [diff] [blame] | 2774 | kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2775 | sclp.hamax + 1); |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2776 | kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2777 | if (!kvm->arch.gmap) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2778 | goto out_err; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 2779 | kvm->arch.gmap->private = kvm; |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 2780 | kvm->arch.gmap->pfault_enabled = 0; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2781 | } |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 2782 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2783 | kvm->arch.use_pfmfi = sclp.has_pfmfi; |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 2784 | kvm->arch.use_skf = sclp.has_skey; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 2785 | spin_lock_init(&kvm->arch.start_stop_lock); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 2786 | kvm_s390_vsie_init(kvm); |
Michael Mueller | cc674ef | 2020-02-27 10:10:31 +0100 | [diff] [blame] | 2787 | if (use_gisa) |
| 2788 | kvm_s390_gisa_init(kvm); |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2789 | KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 2790 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2791 | return 0; |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2792 | out_err: |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2793 | free_page((unsigned long)kvm->arch.sie_page2); |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2794 | debug_unregister(kvm->arch.dbf); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2795 | sca_dispose(kvm); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 2796 | KVM_EVENT(3, "creation of vm failed: %d", rc); |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2797 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2800 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 2801 | { |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2802 | u16 rc, rrc; |
| 2803 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2804 | VCPU_EVENT(vcpu, 3, "%s", "free cpu"); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 2805 | trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); |
Christian Borntraeger | 67335e6 | 2014-03-25 17:09:08 +0100 | [diff] [blame] | 2806 | kvm_s390_clear_local_irqs(vcpu); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 2807 | kvm_clear_async_pf_completion_queue(vcpu); |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2808 | if (!kvm_is_ucontrol(vcpu->kvm)) |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2809 | sca_del_vcpu(vcpu); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2810 | |
| 2811 | if (kvm_is_ucontrol(vcpu->kvm)) |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2812 | gmap_remove(vcpu->arch.gmap); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2813 | |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 2814 | if (vcpu->kvm->arch.use_cmma) |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 2815 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2816 | /* We can not hold the vcpu mutex here, we are already dying */ |
| 2817 | if (kvm_s390_pv_cpu_get_handle(vcpu)) |
| 2818 | kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2819 | free_page((unsigned long)(vcpu->arch.sie_block)); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | static void kvm_free_vcpus(struct kvm *kvm) |
| 2823 | { |
| 2824 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2825 | struct kvm_vcpu *vcpu; |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2826 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2827 | kvm_for_each_vcpu(i, vcpu, kvm) |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 2828 | kvm_vcpu_destroy(vcpu); |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2829 | |
| 2830 | mutex_lock(&kvm->lock); |
| 2831 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 2832 | kvm->vcpus[i] = NULL; |
| 2833 | |
| 2834 | atomic_set(&kvm->online_vcpus, 0); |
| 2835 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2838 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 2839 | { |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2840 | u16 rc, rrc; |
| 2841 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2842 | kvm_free_vcpus(kvm); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2843 | sca_dispose(kvm); |
Michael Mueller | d7c5cb0 | 2017-06-12 14:15:19 +0200 | [diff] [blame] | 2844 | kvm_s390_gisa_destroy(kvm); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2845 | /* |
| 2846 | * We are already at the end of life and kvm->lock is not taken. |
| 2847 | * This is ok as the file descriptor is closed by now and nobody |
| 2848 | * can mess with the pv state. To avoid lockdep_assert_held from |
| 2849 | * complaining we do not use kvm_s390_pv_is_protected. |
| 2850 | */ |
| 2851 | if (kvm_s390_pv_get_handle(kvm)) |
| 2852 | kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); |
| 2853 | debug_unregister(kvm->arch.dbf); |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2854 | free_page((unsigned long)kvm->arch.sie_page2); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2855 | if (!kvm_is_ucontrol(kvm)) |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2856 | gmap_remove(kvm->arch.gmap); |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2857 | kvm_s390_destroy_adapters(kvm); |
Christian Borntraeger | 67335e6 | 2014-03-25 17:09:08 +0100 | [diff] [blame] | 2858 | kvm_s390_clear_float_irqs(kvm); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 2859 | kvm_s390_vsie_destroy(kvm); |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2860 | KVM_EVENT(3, "vm 0x%pK destroyed", kvm); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | /* Section: vcpu related */ |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 2864 | static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) |
| 2865 | { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2866 | vcpu->arch.gmap = gmap_create(current->mm, -1UL); |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 2867 | if (!vcpu->arch.gmap) |
| 2868 | return -ENOMEM; |
| 2869 | vcpu->arch.gmap->private = vcpu->kvm; |
| 2870 | |
| 2871 | return 0; |
| 2872 | } |
| 2873 | |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2874 | static void sca_del_vcpu(struct kvm_vcpu *vcpu) |
| 2875 | { |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2876 | if (!kvm_s390_use_sca_entries()) |
| 2877 | return; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2878 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2879 | if (vcpu->kvm->arch.use_esca) { |
| 2880 | struct esca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2881 | |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2882 | clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); |
David Hildenbrand | 10ce32d | 2015-10-12 12:41:41 +0200 | [diff] [blame] | 2883 | sca->cpu[vcpu->vcpu_id].sda = 0; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2884 | } else { |
| 2885 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
| 2886 | |
| 2887 | clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); |
David Hildenbrand | 10ce32d | 2015-10-12 12:41:41 +0200 | [diff] [blame] | 2888 | sca->cpu[vcpu->vcpu_id].sda = 0; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2889 | } |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2890 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2893 | static void sca_add_vcpu(struct kvm_vcpu *vcpu) |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2894 | { |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2895 | if (!kvm_s390_use_sca_entries()) { |
| 2896 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
| 2897 | |
| 2898 | /* we still need the basic sca for the ipte control */ |
| 2899 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2900 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; |
David Hildenbrand | f07afa0 | 2018-03-06 14:27:58 +0100 | [diff] [blame] | 2901 | return; |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2902 | } |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2903 | read_lock(&vcpu->kvm->arch.sca_lock); |
| 2904 | if (vcpu->kvm->arch.use_esca) { |
| 2905 | struct esca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2906 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2907 | sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2908 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2909 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 2910 | vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2911 | set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2912 | } else { |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2913 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2914 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2915 | sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2916 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2917 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2918 | set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2919 | } |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2920 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | /* Basic SCA to Extended SCA data copy routines */ |
| 2924 | static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) |
| 2925 | { |
| 2926 | d->sda = s->sda; |
| 2927 | d->sigp_ctrl.c = s->sigp_ctrl.c; |
| 2928 | d->sigp_ctrl.scn = s->sigp_ctrl.scn; |
| 2929 | } |
| 2930 | |
| 2931 | static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) |
| 2932 | { |
| 2933 | int i; |
| 2934 | |
| 2935 | d->ipte_control = s->ipte_control; |
| 2936 | d->mcn[0] = s->mcn; |
| 2937 | for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) |
| 2938 | sca_copy_entry(&d->cpu[i], &s->cpu[i]); |
| 2939 | } |
| 2940 | |
| 2941 | static int sca_switch_to_extended(struct kvm *kvm) |
| 2942 | { |
| 2943 | struct bsca_block *old_sca = kvm->arch.sca; |
| 2944 | struct esca_block *new_sca; |
| 2945 | struct kvm_vcpu *vcpu; |
| 2946 | unsigned int vcpu_idx; |
| 2947 | u32 scaol, scaoh; |
| 2948 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2949 | if (kvm->arch.use_esca) |
| 2950 | return 0; |
| 2951 | |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 2952 | new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2953 | if (!new_sca) |
| 2954 | return -ENOMEM; |
| 2955 | |
| 2956 | scaoh = (u32)((u64)(new_sca) >> 32); |
| 2957 | scaol = (u32)(u64)(new_sca) & ~0x3fU; |
| 2958 | |
| 2959 | kvm_s390_vcpu_block_all(kvm); |
| 2960 | write_lock(&kvm->arch.sca_lock); |
| 2961 | |
| 2962 | sca_copy_b_to_e(new_sca, old_sca); |
| 2963 | |
| 2964 | kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { |
| 2965 | vcpu->arch.sie_block->scaoh = scaoh; |
| 2966 | vcpu->arch.sie_block->scaol = scaol; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 2967 | vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2968 | } |
| 2969 | kvm->arch.sca = new_sca; |
| 2970 | kvm->arch.use_esca = 1; |
| 2971 | |
| 2972 | write_unlock(&kvm->arch.sca_lock); |
| 2973 | kvm_s390_vcpu_unblock_all(kvm); |
| 2974 | |
| 2975 | free_page((unsigned long)old_sca); |
| 2976 | |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2977 | VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", |
| 2978 | old_sca, kvm->arch.sca); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2979 | return 0; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) |
| 2983 | { |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2984 | int rc; |
| 2985 | |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2986 | if (!kvm_s390_use_sca_entries()) { |
| 2987 | if (id < KVM_MAX_VCPUS) |
| 2988 | return true; |
| 2989 | return false; |
| 2990 | } |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2991 | if (id < KVM_S390_BSCA_CPU_SLOTS) |
| 2992 | return true; |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2993 | if (!sclp.has_esca || !sclp.has_64bscao) |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2994 | return false; |
| 2995 | |
| 2996 | mutex_lock(&kvm->lock); |
| 2997 | rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); |
| 2998 | mutex_unlock(&kvm->lock); |
| 2999 | |
| 3000 | return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3003 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 3004 | static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3005 | { |
| 3006 | WARN_ON_ONCE(vcpu->arch.cputm_start != 0); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3007 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3008 | vcpu->arch.cputm_start = get_tod_clock_fast(); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3009 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 3013 | static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3014 | { |
| 3015 | WARN_ON_ONCE(vcpu->arch.cputm_start == 0); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3016 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3017 | vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; |
| 3018 | vcpu->arch.cputm_start = 0; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3019 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 3023 | static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3024 | { |
| 3025 | WARN_ON_ONCE(vcpu->arch.cputm_enabled); |
| 3026 | vcpu->arch.cputm_enabled = true; |
| 3027 | __start_cpu_timer_accounting(vcpu); |
| 3028 | } |
| 3029 | |
| 3030 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 3031 | static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3032 | { |
| 3033 | WARN_ON_ONCE(!vcpu->arch.cputm_enabled); |
| 3034 | __stop_cpu_timer_accounting(vcpu); |
| 3035 | vcpu->arch.cputm_enabled = false; |
| 3036 | } |
| 3037 | |
| 3038 | static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3039 | { |
| 3040 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 3041 | __enable_cpu_timer_accounting(vcpu); |
| 3042 | preempt_enable(); |
| 3043 | } |
| 3044 | |
| 3045 | static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 3046 | { |
| 3047 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 3048 | __disable_cpu_timer_accounting(vcpu); |
| 3049 | preempt_enable(); |
| 3050 | } |
| 3051 | |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3052 | /* set the cpu timer - may only be called from the VCPU thread itself */ |
| 3053 | void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) |
| 3054 | { |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3055 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3056 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3057 | if (vcpu->arch.cputm_enabled) |
| 3058 | vcpu->arch.cputm_start = get_tod_clock_fast(); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3059 | vcpu->arch.sie_block->cputm = cputm; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3060 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3061 | preempt_enable(); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3064 | /* update and get the cpu timer - can also be called from other VCPU threads */ |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3065 | __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) |
| 3066 | { |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3067 | unsigned int seq; |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3068 | __u64 value; |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3069 | |
| 3070 | if (unlikely(!vcpu->arch.cputm_enabled)) |
| 3071 | return vcpu->arch.sie_block->cputm; |
| 3072 | |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3073 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 3074 | do { |
| 3075 | seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); |
| 3076 | /* |
| 3077 | * If the writer would ever execute a read in the critical |
| 3078 | * section, e.g. in irq context, we have a deadlock. |
| 3079 | */ |
| 3080 | WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); |
| 3081 | value = vcpu->arch.sie_block->cputm; |
| 3082 | /* if cputm_start is 0, accounting is being started/stopped */ |
| 3083 | if (likely(vcpu->arch.cputm_start)) |
| 3084 | value -= get_tod_clock_fast() - vcpu->arch.cputm_start; |
| 3085 | } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); |
| 3086 | preempt_enable(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3087 | return value; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3088 | } |
| 3089 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3090 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 3091 | { |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 3092 | |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3093 | gmap_enable(vcpu->arch.enabled_gmap); |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3094 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); |
David Hildenbrand | 5ebda31 | 2016-02-22 13:52:27 +0100 | [diff] [blame] | 3095 | if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3096 | __start_cpu_timer_accounting(vcpu); |
David Hildenbrand | 01a745a | 2016-02-12 20:41:56 +0100 | [diff] [blame] | 3097 | vcpu->cpu = cpu; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 3101 | { |
David Hildenbrand | 01a745a | 2016-02-12 20:41:56 +0100 | [diff] [blame] | 3102 | vcpu->cpu = -1; |
David Hildenbrand | 5ebda31 | 2016-02-22 13:52:27 +0100 | [diff] [blame] | 3103 | if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3104 | __stop_cpu_timer_accounting(vcpu); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3105 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3106 | vcpu->arch.enabled_gmap = gmap_get_enabled(); |
| 3107 | gmap_disable(vcpu->arch.enabled_gmap); |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 3108 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 3111 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 3112 | { |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3113 | mutex_lock(&vcpu->kvm->lock); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 3114 | preempt_disable(); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3115 | vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; |
David Hildenbrand | d16b52c | 2018-02-07 12:46:44 +0100 | [diff] [blame] | 3116 | vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 3117 | preempt_enable(); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3118 | mutex_unlock(&vcpu->kvm->lock); |
David Hildenbrand | 2550882 | 2015-10-12 16:27:23 +0200 | [diff] [blame] | 3119 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 3120 | vcpu->arch.gmap = vcpu->kvm->arch.gmap; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 3121 | sca_add_vcpu(vcpu); |
David Hildenbrand | 2550882 | 2015-10-12 16:27:23 +0200 | [diff] [blame] | 3122 | } |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 3123 | if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) |
| 3124 | vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3125 | /* make vcpu_load load the right gmap on the first trigger */ |
| 3126 | vcpu->arch.enabled_gmap = vcpu->arch.gmap; |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 3127 | } |
| 3128 | |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3129 | static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) |
| 3130 | { |
| 3131 | if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && |
| 3132 | test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) |
| 3133 | return true; |
| 3134 | return false; |
| 3135 | } |
| 3136 | |
| 3137 | static bool kvm_has_pckmo_ecc(struct kvm *kvm) |
| 3138 | { |
| 3139 | /* At least one ECC subfunction must be present */ |
| 3140 | return kvm_has_pckmo_subfunc(kvm, 32) || |
| 3141 | kvm_has_pckmo_subfunc(kvm, 33) || |
| 3142 | kvm_has_pckmo_subfunc(kvm, 34) || |
| 3143 | kvm_has_pckmo_subfunc(kvm, 40) || |
| 3144 | kvm_has_pckmo_subfunc(kvm, 41); |
| 3145 | |
| 3146 | } |
| 3147 | |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3148 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) |
| 3149 | { |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3150 | /* |
| 3151 | * If the AP instructions are not being interpreted and the MSAX3 |
| 3152 | * facility is not configured for the guest, there is nothing to set up. |
| 3153 | */ |
| 3154 | if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3155 | return; |
| 3156 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3157 | vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3158 | vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 3159 | vcpu->arch.sie_block->eca &= ~ECA_APIE; |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3160 | vcpu->arch.sie_block->ecd &= ~ECD_ECC; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3161 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3162 | if (vcpu->kvm->arch.crypto.apie) |
| 3163 | vcpu->arch.sie_block->eca |= ECA_APIE; |
| 3164 | |
| 3165 | /* Set up protected key support */ |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3166 | if (vcpu->kvm->arch.crypto.aes_kw) { |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3167 | vcpu->arch.sie_block->ecb3 |= ECB3_AES; |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3168 | /* ecc is also wrapped with AES key */ |
| 3169 | if (kvm_has_pckmo_ecc(vcpu->kvm)) |
| 3170 | vcpu->arch.sie_block->ecd |= ECD_ECC; |
| 3171 | } |
| 3172 | |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3173 | if (vcpu->kvm->arch.crypto.dea_kw) |
| 3174 | vcpu->arch.sie_block->ecb3 |= ECB3_DEA; |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3175 | } |
| 3176 | |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3177 | void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) |
| 3178 | { |
| 3179 | free_page(vcpu->arch.sie_block->cbrlo); |
| 3180 | vcpu->arch.sie_block->cbrlo = 0; |
| 3181 | } |
| 3182 | |
| 3183 | int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) |
| 3184 | { |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 3185 | vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT); |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3186 | if (!vcpu->arch.sie_block->cbrlo) |
| 3187 | return -ENOMEM; |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3188 | return 0; |
| 3189 | } |
| 3190 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3191 | static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) |
| 3192 | { |
| 3193 | struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; |
| 3194 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3195 | vcpu->arch.sie_block->ibc = model->ibc; |
David Hildenbrand | 80bc79d | 2015-12-02 09:43:29 +0100 | [diff] [blame] | 3196 | if (test_kvm_facility(vcpu->kvm, 7)) |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 3197 | vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3198 | } |
| 3199 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3200 | static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) |
| 3201 | { |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3202 | int rc = 0; |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 3203 | u16 uvrc, uvrrc; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 3204 | |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 3205 | atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | |
| 3206 | CPUSTAT_SM | |
Guenther Hutzl | a4a4f19 | 2015-03-31 14:39:49 +0200 | [diff] [blame] | 3207 | CPUSTAT_STOPPED); |
| 3208 | |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 3209 | if (test_kvm_facility(vcpu->kvm, 78)) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3210 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 3211 | else if (test_kvm_facility(vcpu->kvm, 8)) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3212 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); |
Guenther Hutzl | a4a4f19 | 2015-03-31 14:39:49 +0200 | [diff] [blame] | 3213 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3214 | kvm_s390_vcpu_setup_model(vcpu); |
| 3215 | |
David Hildenbrand | bdab09f | 2016-04-12 11:07:49 +0200 | [diff] [blame] | 3216 | /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ |
| 3217 | if (MACHINE_HAS_ESOP) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3218 | vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; |
David Hildenbrand | bd50e8e | 2016-03-04 12:23:55 +0100 | [diff] [blame] | 3219 | if (test_kvm_facility(vcpu->kvm, 9)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3220 | vcpu->arch.sie_block->ecb |= ECB_SRSI; |
David Hildenbrand | f597d24 | 2016-04-22 16:26:49 +0200 | [diff] [blame] | 3221 | if (test_kvm_facility(vcpu->kvm, 73)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3222 | vcpu->arch.sie_block->ecb |= ECB_TE; |
Janis Schoetterl-Glausch | 7119dec | 2021-06-29 10:55:30 +0200 | [diff] [blame] | 3223 | if (!kvm_is_ucontrol(vcpu->kvm)) |
| 3224 | vcpu->arch.sie_block->ecb |= ECB_SPECI; |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3225 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3226 | if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3227 | vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; |
Janosch Frank | cd1836f | 2016-08-04 09:57:36 +0200 | [diff] [blame] | 3228 | if (test_kvm_facility(vcpu->kvm, 130)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3229 | vcpu->arch.sie_block->ecb2 |= ECB2_IEP; |
| 3230 | vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; |
David Hildenbrand | 48ee7d3 | 2016-04-04 15:49:34 +0200 | [diff] [blame] | 3231 | if (sclp.has_cei) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3232 | vcpu->arch.sie_block->eca |= ECA_CEI; |
David Hildenbrand | 11ad65b | 2016-04-04 15:46:26 +0200 | [diff] [blame] | 3233 | if (sclp.has_ib) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3234 | vcpu->arch.sie_block->eca |= ECA_IB; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 3235 | if (sclp.has_siif) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3236 | vcpu->arch.sie_block->eca |= ECA_SII; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 3237 | if (sclp.has_sigpif) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3238 | vcpu->arch.sie_block->eca |= ECA_SIGPI; |
Michael Mueller | 18280d8 | 2015-03-16 16:05:41 +0100 | [diff] [blame] | 3239 | if (test_kvm_facility(vcpu->kvm, 129)) { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3240 | vcpu->arch.sie_block->eca |= ECA_VX; |
| 3241 | vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; |
Eric Farman | 13211ea | 2014-04-30 13:39:46 -0400 | [diff] [blame] | 3242 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3243 | if (test_kvm_facility(vcpu->kvm, 139)) |
| 3244 | vcpu->arch.sie_block->ecd |= ECD_MEF; |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 3245 | if (test_kvm_facility(vcpu->kvm, 156)) |
| 3246 | vcpu->arch.sie_block->ecd |= ECD_ETOKENF; |
Michael Mueller | d7c5cb0 | 2017-06-12 14:15:19 +0200 | [diff] [blame] | 3247 | if (vcpu->arch.sie_block->gd) { |
| 3248 | vcpu->arch.sie_block->eca |= ECA_AIV; |
| 3249 | VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", |
| 3250 | vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); |
| 3251 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 3252 | vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) |
| 3253 | | SDNXC; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 3254 | vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 3255 | |
| 3256 | if (sclp.has_kss) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3257 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 3258 | else |
| 3259 | vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; |
Matthew Rosato | 5a5e653 | 2013-01-29 11:48:20 -0500 | [diff] [blame] | 3260 | |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 3261 | if (vcpu->kvm->arch.use_cmma) { |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3262 | rc = kvm_s390_vcpu_setup_cmma(vcpu); |
| 3263 | if (rc) |
| 3264 | return rc; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 3265 | } |
David Hildenbrand | 0ac96caf | 2014-12-12 15:17:31 +0100 | [diff] [blame] | 3266 | hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 3267 | vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 3268 | |
Collin Walling | 67d49d5 | 2018-08-31 12:51:19 -0400 | [diff] [blame] | 3269 | vcpu->arch.sie_block->hpid = HPID_KVM; |
| 3270 | |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3271 | kvm_s390_vcpu_crypto_setup(vcpu); |
| 3272 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 3273 | mutex_lock(&vcpu->kvm->lock); |
| 3274 | if (kvm_s390_pv_is_protected(vcpu->kvm)) { |
| 3275 | rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); |
| 3276 | if (rc) |
| 3277 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
| 3278 | } |
| 3279 | mutex_unlock(&vcpu->kvm->lock); |
| 3280 | |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3281 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3282 | } |
| 3283 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 3284 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
| 3285 | { |
| 3286 | if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) |
| 3287 | return -EINVAL; |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3291 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3292 | { |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3293 | struct sie_page *sie_page; |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 3294 | int rc; |
Carsten Otte | 4d47555 | 2011-10-18 12:27:12 +0200 | [diff] [blame] | 3295 | |
QingFeng Hao | da72ca4 | 2017-06-07 11:41:19 +0200 | [diff] [blame] | 3296 | BUILD_BUG_ON(sizeof(struct sie_page) != 4096); |
Christian Borntraeger | c419621 | 2020-11-06 08:34:23 +0100 | [diff] [blame] | 3297 | sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3298 | if (!sie_page) |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3299 | return -ENOMEM; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3300 | |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3301 | vcpu->arch.sie_block = &sie_page->sie_block; |
| 3302 | vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; |
| 3303 | |
David Hildenbrand | efed110 | 2015-04-16 12:32:41 +0200 | [diff] [blame] | 3304 | /* the real guest size will always be smaller than msl */ |
| 3305 | vcpu->arch.sie_block->mso = 0; |
| 3306 | vcpu->arch.sie_block->msl = sclp.hamax; |
| 3307 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3308 | vcpu->arch.sie_block->icpua = vcpu->vcpu_id; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 3309 | spin_lock_init(&vcpu->arch.local_int.lock); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3310 | vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin; |
Michael Mueller | 4b9f952 | 2017-06-23 13:51:25 +0200 | [diff] [blame] | 3311 | if (vcpu->arch.sie_block->gd && sclp.has_gisaf) |
| 3312 | vcpu->arch.sie_block->gd |= GISA_FORMAT1; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3313 | seqcount_init(&vcpu->arch.cputm_seqcount); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 3314 | |
Sean Christopherson | 321f8ee | 2019-12-18 13:55:10 -0800 | [diff] [blame] | 3315 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
| 3316 | kvm_clear_async_pf_completion_queue(vcpu); |
| 3317 | vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | |
| 3318 | KVM_SYNC_GPRS | |
| 3319 | KVM_SYNC_ACRS | |
| 3320 | KVM_SYNC_CRS | |
| 3321 | KVM_SYNC_ARCH0 | |
Collin Walling | 23a60f8 | 2020-06-22 11:46:36 -0400 | [diff] [blame] | 3322 | KVM_SYNC_PFAULT | |
| 3323 | KVM_SYNC_DIAG318; |
Sean Christopherson | 321f8ee | 2019-12-18 13:55:10 -0800 | [diff] [blame] | 3324 | kvm_s390_set_prefix(vcpu, 0); |
| 3325 | if (test_kvm_facility(vcpu->kvm, 64)) |
| 3326 | vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; |
| 3327 | if (test_kvm_facility(vcpu->kvm, 82)) |
| 3328 | vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; |
| 3329 | if (test_kvm_facility(vcpu->kvm, 133)) |
| 3330 | vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; |
| 3331 | if (test_kvm_facility(vcpu->kvm, 156)) |
| 3332 | vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; |
| 3333 | /* fprs can be synchronized via vrs, even if the guest has no vx. With |
| 3334 | * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. |
| 3335 | */ |
| 3336 | if (MACHINE_HAS_VX) |
| 3337 | vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; |
| 3338 | else |
| 3339 | vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; |
| 3340 | |
| 3341 | if (kvm_is_ucontrol(vcpu->kvm)) { |
| 3342 | rc = __kvm_ucontrol_vcpu_init(vcpu); |
| 3343 | if (rc) |
Sean Christopherson | a2017f1 | 2019-12-18 13:55:11 -0800 | [diff] [blame] | 3344 | goto out_free_sie_block; |
Sean Christopherson | 321f8ee | 2019-12-18 13:55:10 -0800 | [diff] [blame] | 3345 | } |
| 3346 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3347 | VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", |
| 3348 | vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); |
| 3349 | trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3350 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3351 | rc = kvm_s390_vcpu_setup(vcpu); |
| 3352 | if (rc) |
| 3353 | goto out_ucontrol_uninit; |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3354 | return 0; |
| 3355 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3356 | out_ucontrol_uninit: |
| 3357 | if (kvm_is_ucontrol(vcpu->kvm)) |
| 3358 | gmap_remove(vcpu->arch.gmap); |
Wei Yongjun | 7b06bf2 | 2010-03-09 14:37:53 +0800 | [diff] [blame] | 3359 | out_free_sie_block: |
| 3360 | free_page((unsigned long)(vcpu->arch.sie_block)); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3361 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3362 | } |
| 3363 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3364 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 3365 | { |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 3366 | return kvm_s390_vcpu_has_irq(vcpu, 0); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3367 | } |
| 3368 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 3369 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 3370 | { |
Longpeng(Mike) | 0546c63 | 2017-08-08 12:05:34 +0800 | [diff] [blame] | 3371 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 3372 | } |
| 3373 | |
Christian Borntraeger | 27406cd | 2015-04-14 12:17:34 +0200 | [diff] [blame] | 3374 | void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3375 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3376 | atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); |
David Hildenbrand | 61a6df5 | 2015-05-12 08:41:40 +0200 | [diff] [blame] | 3377 | exit_sie(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
Christian Borntraeger | 27406cd | 2015-04-14 12:17:34 +0200 | [diff] [blame] | 3380 | void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3381 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3382 | atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3383 | } |
| 3384 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3385 | static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) |
| 3386 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3387 | atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); |
David Hildenbrand | 61a6df5 | 2015-05-12 08:41:40 +0200 | [diff] [blame] | 3388 | exit_sie(vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3391 | bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) |
| 3392 | { |
| 3393 | return atomic_read(&vcpu->arch.sie_block->prog20) & |
| 3394 | (PROG_BLOCK_SIE | PROG_REQUEST); |
| 3395 | } |
| 3396 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3397 | static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) |
| 3398 | { |
Jason J. Herne | 9bf9fde | 2015-09-16 09:13:50 -0400 | [diff] [blame] | 3399 | atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3402 | /* |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3403 | * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3404 | * If the CPU is not running (e.g. waiting as idle) the function will |
| 3405 | * return immediately. */ |
| 3406 | void exit_sie(struct kvm_vcpu *vcpu) |
| 3407 | { |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3408 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3409 | kvm_s390_vsie_kick(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3410 | while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) |
| 3411 | cpu_relax(); |
| 3412 | } |
| 3413 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3414 | /* Kick a guest cpu out of SIE to process a request synchronously */ |
| 3415 | void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3416 | { |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3417 | kvm_make_request(req, vcpu); |
| 3418 | kvm_s390_vcpu_request(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3419 | } |
| 3420 | |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3421 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 3422 | unsigned long end) |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3423 | { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3424 | struct kvm *kvm = gmap->private; |
| 3425 | struct kvm_vcpu *vcpu; |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3426 | unsigned long prefix; |
| 3427 | int i; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3428 | |
David Hildenbrand | 65d0b0d | 2015-04-27 16:29:34 +0200 | [diff] [blame] | 3429 | if (gmap_is_shadow(gmap)) |
| 3430 | return; |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3431 | if (start >= 1UL << 31) |
| 3432 | /* We are only interested in prefix pages */ |
| 3433 | return; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3434 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 3435 | /* match against both prefix pages */ |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3436 | prefix = kvm_s390_get_prefix(vcpu); |
| 3437 | if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { |
| 3438 | VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", |
| 3439 | start, end); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3440 | kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | } |
| 3444 | |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 3445 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
| 3446 | { |
| 3447 | /* do not poll with more than halt_poll_max_steal percent of steal time */ |
| 3448 | if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= |
| 3449 | halt_poll_max_steal) { |
| 3450 | vcpu->stat.halt_no_poll_steal++; |
| 3451 | return true; |
| 3452 | } |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 3456 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 3457 | { |
| 3458 | /* kvm common code refers to this, but never calls it */ |
| 3459 | BUG(); |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3463 | static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, |
| 3464 | struct kvm_one_reg *reg) |
| 3465 | { |
| 3466 | int r = -EINVAL; |
| 3467 | |
| 3468 | switch (reg->id) { |
Carsten Otte | 29b7c71 | 2012-05-15 14:15:27 +0200 | [diff] [blame] | 3469 | case KVM_REG_S390_TODPR: |
| 3470 | r = put_user(vcpu->arch.sie_block->todpr, |
| 3471 | (u32 __user *)reg->addr); |
| 3472 | break; |
| 3473 | case KVM_REG_S390_EPOCHDIFF: |
| 3474 | r = put_user(vcpu->arch.sie_block->epoch, |
| 3475 | (u64 __user *)reg->addr); |
| 3476 | break; |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3477 | case KVM_REG_S390_CPU_TIMER: |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3478 | r = put_user(kvm_s390_get_cpu_timer(vcpu), |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3479 | (u64 __user *)reg->addr); |
| 3480 | break; |
| 3481 | case KVM_REG_S390_CLOCK_COMP: |
| 3482 | r = put_user(vcpu->arch.sie_block->ckc, |
| 3483 | (u64 __user *)reg->addr); |
| 3484 | break; |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3485 | case KVM_REG_S390_PFTOKEN: |
| 3486 | r = put_user(vcpu->arch.pfault_token, |
| 3487 | (u64 __user *)reg->addr); |
| 3488 | break; |
| 3489 | case KVM_REG_S390_PFCOMPARE: |
| 3490 | r = put_user(vcpu->arch.pfault_compare, |
| 3491 | (u64 __user *)reg->addr); |
| 3492 | break; |
| 3493 | case KVM_REG_S390_PFSELECT: |
| 3494 | r = put_user(vcpu->arch.pfault_select, |
| 3495 | (u64 __user *)reg->addr); |
| 3496 | break; |
Christian Borntraeger | 672550f | 2014-02-10 15:32:19 +0100 | [diff] [blame] | 3497 | case KVM_REG_S390_PP: |
| 3498 | r = put_user(vcpu->arch.sie_block->pp, |
| 3499 | (u64 __user *)reg->addr); |
| 3500 | break; |
Christian Borntraeger | afa45ff | 2014-02-10 15:39:23 +0100 | [diff] [blame] | 3501 | case KVM_REG_S390_GBEA: |
| 3502 | r = put_user(vcpu->arch.sie_block->gbea, |
| 3503 | (u64 __user *)reg->addr); |
| 3504 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3505 | default: |
| 3506 | break; |
| 3507 | } |
| 3508 | |
| 3509 | return r; |
| 3510 | } |
| 3511 | |
| 3512 | static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, |
| 3513 | struct kvm_one_reg *reg) |
| 3514 | { |
| 3515 | int r = -EINVAL; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3516 | __u64 val; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3517 | |
| 3518 | switch (reg->id) { |
Carsten Otte | 29b7c71 | 2012-05-15 14:15:27 +0200 | [diff] [blame] | 3519 | case KVM_REG_S390_TODPR: |
| 3520 | r = get_user(vcpu->arch.sie_block->todpr, |
| 3521 | (u32 __user *)reg->addr); |
| 3522 | break; |
| 3523 | case KVM_REG_S390_EPOCHDIFF: |
| 3524 | r = get_user(vcpu->arch.sie_block->epoch, |
| 3525 | (u64 __user *)reg->addr); |
| 3526 | break; |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3527 | case KVM_REG_S390_CPU_TIMER: |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3528 | r = get_user(val, (u64 __user *)reg->addr); |
| 3529 | if (!r) |
| 3530 | kvm_s390_set_cpu_timer(vcpu, val); |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3531 | break; |
| 3532 | case KVM_REG_S390_CLOCK_COMP: |
| 3533 | r = get_user(vcpu->arch.sie_block->ckc, |
| 3534 | (u64 __user *)reg->addr); |
| 3535 | break; |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3536 | case KVM_REG_S390_PFTOKEN: |
| 3537 | r = get_user(vcpu->arch.pfault_token, |
| 3538 | (u64 __user *)reg->addr); |
David Hildenbrand | 9fbd808 | 2014-10-09 15:01:38 +0200 | [diff] [blame] | 3539 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 3540 | kvm_clear_async_pf_completion_queue(vcpu); |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3541 | break; |
| 3542 | case KVM_REG_S390_PFCOMPARE: |
| 3543 | r = get_user(vcpu->arch.pfault_compare, |
| 3544 | (u64 __user *)reg->addr); |
| 3545 | break; |
| 3546 | case KVM_REG_S390_PFSELECT: |
| 3547 | r = get_user(vcpu->arch.pfault_select, |
| 3548 | (u64 __user *)reg->addr); |
| 3549 | break; |
Christian Borntraeger | 672550f | 2014-02-10 15:32:19 +0100 | [diff] [blame] | 3550 | case KVM_REG_S390_PP: |
| 3551 | r = get_user(vcpu->arch.sie_block->pp, |
| 3552 | (u64 __user *)reg->addr); |
| 3553 | break; |
Christian Borntraeger | afa45ff | 2014-02-10 15:39:23 +0100 | [diff] [blame] | 3554 | case KVM_REG_S390_GBEA: |
| 3555 | r = get_user(vcpu->arch.sie_block->gbea, |
| 3556 | (u64 __user *)reg->addr); |
| 3557 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3558 | default: |
| 3559 | break; |
| 3560 | } |
| 3561 | |
| 3562 | return r; |
| 3563 | } |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 3564 | |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3565 | static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3566 | { |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3567 | vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; |
| 3568 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
| 3569 | memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); |
| 3570 | |
| 3571 | kvm_clear_async_pf_completion_queue(vcpu); |
| 3572 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) |
| 3573 | kvm_s390_vcpu_stop(vcpu); |
| 3574 | kvm_s390_clear_local_irqs(vcpu); |
| 3575 | } |
| 3576 | |
| 3577 | static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) |
| 3578 | { |
| 3579 | /* Initial reset is a superset of the normal reset */ |
| 3580 | kvm_arch_vcpu_ioctl_normal_reset(vcpu); |
| 3581 | |
Christian Borntraeger | e93fc7b | 2020-03-03 03:10:57 -0500 | [diff] [blame] | 3582 | /* |
| 3583 | * This equals initial cpu reset in pop, but we don't switch to ESA. |
| 3584 | * We do not only reset the internal data, but also ... |
| 3585 | */ |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3586 | vcpu->arch.sie_block->gpsw.mask = 0; |
| 3587 | vcpu->arch.sie_block->gpsw.addr = 0; |
| 3588 | kvm_s390_set_prefix(vcpu, 0); |
| 3589 | kvm_s390_set_cpu_timer(vcpu, 0); |
| 3590 | vcpu->arch.sie_block->ckc = 0; |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3591 | memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); |
| 3592 | vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; |
| 3593 | vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; |
Christian Borntraeger | e93fc7b | 2020-03-03 03:10:57 -0500 | [diff] [blame] | 3594 | |
| 3595 | /* ... the data in sync regs */ |
| 3596 | memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); |
| 3597 | vcpu->run->s.regs.ckc = 0; |
| 3598 | vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; |
| 3599 | vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; |
| 3600 | vcpu->run->psw_addr = 0; |
| 3601 | vcpu->run->psw_mask = 0; |
| 3602 | vcpu->run->s.regs.todpr = 0; |
| 3603 | vcpu->run->s.regs.cputm = 0; |
| 3604 | vcpu->run->s.regs.ckc = 0; |
| 3605 | vcpu->run->s.regs.pp = 0; |
| 3606 | vcpu->run->s.regs.gbea = 1; |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3607 | vcpu->run->s.regs.fpc = 0; |
Janosch Frank | 0f30350 | 2020-02-10 04:27:47 -0500 | [diff] [blame] | 3608 | /* |
| 3609 | * Do not reset these registers in the protected case, as some of |
| 3610 | * them are overlayed and they are not accessible in this case |
| 3611 | * anyway. |
| 3612 | */ |
| 3613 | if (!kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 3614 | vcpu->arch.sie_block->gbea = 1; |
| 3615 | vcpu->arch.sie_block->pp = 0; |
| 3616 | vcpu->arch.sie_block->fpf &= ~FPF_BPBC; |
| 3617 | vcpu->arch.sie_block->todpr = 0; |
| 3618 | } |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) |
| 3622 | { |
| 3623 | struct kvm_sync_regs *regs = &vcpu->run->s.regs; |
| 3624 | |
| 3625 | /* Clear reset is a superset of the initial reset */ |
| 3626 | kvm_arch_vcpu_ioctl_initial_reset(vcpu); |
| 3627 | |
| 3628 | memset(®s->gprs, 0, sizeof(regs->gprs)); |
| 3629 | memset(®s->vrs, 0, sizeof(regs->vrs)); |
| 3630 | memset(®s->acrs, 0, sizeof(regs->acrs)); |
| 3631 | memset(®s->gscb, 0, sizeof(regs->gscb)); |
| 3632 | |
| 3633 | regs->etoken = 0; |
| 3634 | regs->etoken_extension = 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3638 | { |
Christoffer Dall | 875656f | 2017-12-04 21:35:27 +0100 | [diff] [blame] | 3639 | vcpu_load(vcpu); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 3640 | memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); |
Christoffer Dall | 875656f | 2017-12-04 21:35:27 +0100 | [diff] [blame] | 3641 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3642 | return 0; |
| 3643 | } |
| 3644 | |
| 3645 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3646 | { |
Christoffer Dall | 1fc9b76 | 2017-12-04 21:35:26 +0100 | [diff] [blame] | 3647 | vcpu_load(vcpu); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 3648 | memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); |
Christoffer Dall | 1fc9b76 | 2017-12-04 21:35:26 +0100 | [diff] [blame] | 3649 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3650 | return 0; |
| 3651 | } |
| 3652 | |
| 3653 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 3654 | struct kvm_sregs *sregs) |
| 3655 | { |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 3656 | vcpu_load(vcpu); |
| 3657 | |
Christian Borntraeger | 59674c1 | 2012-01-11 11:20:33 +0100 | [diff] [blame] | 3658 | memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3659 | memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 3660 | |
| 3661 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3662 | return 0; |
| 3663 | } |
| 3664 | |
| 3665 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 3666 | struct kvm_sregs *sregs) |
| 3667 | { |
Christoffer Dall | bcdec41 | 2017-12-04 21:35:28 +0100 | [diff] [blame] | 3668 | vcpu_load(vcpu); |
| 3669 | |
Christian Borntraeger | 59674c1 | 2012-01-11 11:20:33 +0100 | [diff] [blame] | 3670 | memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3671 | memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); |
Christoffer Dall | bcdec41 | 2017-12-04 21:35:28 +0100 | [diff] [blame] | 3672 | |
| 3673 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3674 | return 0; |
| 3675 | } |
| 3676 | |
| 3677 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3678 | { |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 3679 | int ret = 0; |
| 3680 | |
| 3681 | vcpu_load(vcpu); |
| 3682 | |
| 3683 | if (test_fp_ctl(fpu->fpc)) { |
| 3684 | ret = -EINVAL; |
| 3685 | goto out; |
| 3686 | } |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 3687 | vcpu->run->s.regs.fpc = fpu->fpc; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3688 | if (MACHINE_HAS_VX) |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3689 | convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, |
| 3690 | (freg_t *) fpu->fprs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3691 | else |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3692 | memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 3693 | |
| 3694 | out: |
| 3695 | vcpu_put(vcpu); |
| 3696 | return ret; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3700 | { |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 3701 | vcpu_load(vcpu); |
| 3702 | |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3703 | /* make sure we have the latest values */ |
| 3704 | save_fpu_regs(); |
| 3705 | if (MACHINE_HAS_VX) |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3706 | convert_vx_to_fp((freg_t *) fpu->fprs, |
| 3707 | (__vector128 *) vcpu->run->s.regs.vrs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3708 | else |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3709 | memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 3710 | fpu->fpc = vcpu->run->s.regs.fpc; |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 3711 | |
| 3712 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3713 | return 0; |
| 3714 | } |
| 3715 | |
| 3716 | static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) |
| 3717 | { |
| 3718 | int rc = 0; |
| 3719 | |
David Hildenbrand | 7a42fdc | 2014-05-05 16:26:19 +0200 | [diff] [blame] | 3720 | if (!is_vcpu_stopped(vcpu)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3721 | rc = -EBUSY; |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 3722 | else { |
| 3723 | vcpu->run->psw_mask = psw.mask; |
| 3724 | vcpu->run->psw_addr = psw.addr; |
| 3725 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3726 | return rc; |
| 3727 | } |
| 3728 | |
| 3729 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 3730 | struct kvm_translation *tr) |
| 3731 | { |
| 3732 | return -EINVAL; /* not implemented yet */ |
| 3733 | } |
| 3734 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3735 | #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ |
| 3736 | KVM_GUESTDBG_USE_HW_BP | \ |
| 3737 | KVM_GUESTDBG_ENABLE) |
| 3738 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3739 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 3740 | struct kvm_guest_debug *dbg) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3741 | { |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3742 | int rc = 0; |
| 3743 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3744 | vcpu_load(vcpu); |
| 3745 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3746 | vcpu->guest_debug = 0; |
| 3747 | kvm_s390_clear_bp_data(vcpu); |
| 3748 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3749 | if (dbg->control & ~VALID_GUESTDBG_FLAGS) { |
| 3750 | rc = -EINVAL; |
| 3751 | goto out; |
| 3752 | } |
| 3753 | if (!sclp.has_gpere) { |
| 3754 | rc = -EINVAL; |
| 3755 | goto out; |
| 3756 | } |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3757 | |
| 3758 | if (dbg->control & KVM_GUESTDBG_ENABLE) { |
| 3759 | vcpu->guest_debug = dbg->control; |
| 3760 | /* enforce guest PER */ |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3761 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3762 | |
| 3763 | if (dbg->control & KVM_GUESTDBG_USE_HW_BP) |
| 3764 | rc = kvm_s390_import_bp_data(vcpu, dbg); |
| 3765 | } else { |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3766 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3767 | vcpu->arch.guestdbg.last_bp = 0; |
| 3768 | } |
| 3769 | |
| 3770 | if (rc) { |
| 3771 | vcpu->guest_debug = 0; |
| 3772 | kvm_s390_clear_bp_data(vcpu); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3773 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3774 | } |
| 3775 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3776 | out: |
| 3777 | vcpu_put(vcpu); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3778 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3781 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 3782 | struct kvm_mp_state *mp_state) |
| 3783 | { |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 3784 | int ret; |
| 3785 | |
| 3786 | vcpu_load(vcpu); |
| 3787 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3788 | /* CHECK_STOP and LOAD are not supported yet */ |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 3789 | ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : |
| 3790 | KVM_MP_STATE_OPERATING; |
| 3791 | |
| 3792 | vcpu_put(vcpu); |
| 3793 | return ret; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 3797 | struct kvm_mp_state *mp_state) |
| 3798 | { |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3799 | int rc = 0; |
| 3800 | |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 3801 | vcpu_load(vcpu); |
| 3802 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3803 | /* user space knows about this interface - let it control the state */ |
| 3804 | vcpu->kvm->arch.user_cpu_state_ctrl = 1; |
| 3805 | |
| 3806 | switch (mp_state->mp_state) { |
| 3807 | case KVM_MP_STATE_STOPPED: |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 3808 | rc = kvm_s390_vcpu_stop(vcpu); |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3809 | break; |
| 3810 | case KVM_MP_STATE_OPERATING: |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 3811 | rc = kvm_s390_vcpu_start(vcpu); |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3812 | break; |
| 3813 | case KVM_MP_STATE_LOAD: |
Janosch Frank | 7c36a3f | 2019-09-02 08:34:44 +0200 | [diff] [blame] | 3814 | if (!kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 3815 | rc = -ENXIO; |
| 3816 | break; |
| 3817 | } |
| 3818 | rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); |
| 3819 | break; |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3820 | case KVM_MP_STATE_CHECK_STOP: |
Joe Perches | 3b684a4 | 2020-03-10 21:51:32 -0700 | [diff] [blame] | 3821 | fallthrough; /* CHECK_STOP and LOAD are not supported yet */ |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3822 | default: |
| 3823 | rc = -ENXIO; |
| 3824 | } |
| 3825 | |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 3826 | vcpu_put(vcpu); |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3827 | return rc; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3828 | } |
| 3829 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3830 | static bool ibs_enabled(struct kvm_vcpu *vcpu) |
| 3831 | { |
David Hildenbrand | 8d5fb0d | 2018-01-23 18:05:31 +0100 | [diff] [blame] | 3832 | return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3835 | static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) |
| 3836 | { |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3837 | retry: |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3838 | kvm_s390_vcpu_request_handled(vcpu); |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 3839 | if (!kvm_request_pending(vcpu)) |
Christian Borntraeger | 586b7cc | 2015-07-28 15:03:05 +0200 | [diff] [blame] | 3840 | return 0; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3841 | /* |
| 3842 | * We use MMU_RELOAD just to re-arm the ipte notifier for the |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 3843 | * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3844 | * This ensures that the ipte instruction for this request has |
| 3845 | * already finished. We might race against a second unmapper that |
| 3846 | * wants to set the blocking bit. Lets just retry the request loop. |
| 3847 | */ |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3848 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3849 | int rc; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 3850 | rc = gmap_mprotect_notify(vcpu->arch.gmap, |
| 3851 | kvm_s390_get_prefix(vcpu), |
| 3852 | PAGE_SIZE * 2, PROT_WRITE); |
Julius Niedworok | aca411a | 2016-08-03 16:39:55 +0200 | [diff] [blame] | 3853 | if (rc) { |
| 3854 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3855 | return rc; |
Julius Niedworok | aca411a | 2016-08-03 16:39:55 +0200 | [diff] [blame] | 3856 | } |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3857 | goto retry; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3858 | } |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3859 | |
David Hildenbrand | d3d692c | 2014-07-29 08:53:36 +0200 | [diff] [blame] | 3860 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
| 3861 | vcpu->arch.sie_block->ihcpu = 0xffff; |
| 3862 | goto retry; |
| 3863 | } |
| 3864 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3865 | if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { |
| 3866 | if (!ibs_enabled(vcpu)) { |
| 3867 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3868 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3869 | } |
| 3870 | goto retry; |
| 3871 | } |
| 3872 | |
| 3873 | if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { |
| 3874 | if (ibs_enabled(vcpu)) { |
| 3875 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3876 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3877 | } |
| 3878 | goto retry; |
| 3879 | } |
| 3880 | |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 3881 | if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { |
| 3882 | vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; |
| 3883 | goto retry; |
| 3884 | } |
| 3885 | |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3886 | if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { |
| 3887 | /* |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3888 | * Disable CMM virtualization; we will emulate the ESSA |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3889 | * instruction manually, in order to provide additional |
| 3890 | * functionalities needed for live migration. |
| 3891 | */ |
| 3892 | vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; |
| 3893 | goto retry; |
| 3894 | } |
| 3895 | |
| 3896 | if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { |
| 3897 | /* |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3898 | * Re-enable CMM virtualization if CMMA is available and |
| 3899 | * CMM has been used. |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3900 | */ |
| 3901 | if ((vcpu->kvm->arch.use_cmma) && |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3902 | (vcpu->kvm->mm->context.uses_cmm)) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3903 | vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; |
| 3904 | goto retry; |
| 3905 | } |
| 3906 | |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 3907 | /* nothing to do, just clear the request */ |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 3908 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 3909 | /* we left the vsie handler, nothing to do, just clear the request */ |
| 3910 | kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 3911 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3912 | return 0; |
| 3913 | } |
| 3914 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 3915 | void kvm_s390_set_tod_clock(struct kvm *kvm, |
| 3916 | const struct kvm_s390_vm_tod_clock *gtod) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3917 | { |
| 3918 | struct kvm_vcpu *vcpu; |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 3919 | union tod_clock clk; |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3920 | int i; |
| 3921 | |
| 3922 | mutex_lock(&kvm->lock); |
| 3923 | preempt_disable(); |
| 3924 | |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 3925 | store_tod_clock_ext(&clk); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3926 | |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 3927 | kvm->arch.epoch = gtod->tod - clk.tod; |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 3928 | kvm->arch.epdx = 0; |
| 3929 | if (test_kvm_facility(kvm, 139)) { |
Heiko Carstens | 2cfd7b7 | 2021-02-08 16:27:33 +0100 | [diff] [blame] | 3930 | kvm->arch.epdx = gtod->epoch_idx - clk.ei; |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 3931 | if (kvm->arch.epoch > gtod->tod) |
| 3932 | kvm->arch.epdx -= 1; |
| 3933 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3934 | |
| 3935 | kvm_s390_vcpu_block_all(kvm); |
| 3936 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 3937 | vcpu->arch.sie_block->epoch = kvm->arch.epoch; |
| 3938 | vcpu->arch.sie_block->epdx = kvm->arch.epdx; |
| 3939 | } |
| 3940 | |
| 3941 | kvm_s390_vcpu_unblock_all(kvm); |
| 3942 | preempt_enable(); |
| 3943 | mutex_unlock(&kvm->lock); |
| 3944 | } |
| 3945 | |
Thomas Huth | fa576c5 | 2014-05-06 17:20:16 +0200 | [diff] [blame] | 3946 | /** |
| 3947 | * kvm_arch_fault_in_page - fault-in guest page if necessary |
| 3948 | * @vcpu: The corresponding virtual cpu |
| 3949 | * @gpa: Guest physical address |
| 3950 | * @writable: Whether the page should be writable or not |
| 3951 | * |
| 3952 | * Make sure that a guest page has been faulted-in on the host. |
| 3953 | * |
| 3954 | * Return: Zero on success, negative error code otherwise. |
| 3955 | */ |
| 3956 | long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 3957 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 3958 | return gmap_fault(vcpu->arch.gmap, gpa, |
| 3959 | writable ? FAULT_FLAG_WRITE : 0); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3962 | static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, |
| 3963 | unsigned long token) |
| 3964 | { |
| 3965 | struct kvm_s390_interrupt inti; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3966 | struct kvm_s390_irq irq; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3967 | |
| 3968 | if (start_token) { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3969 | irq.u.ext.ext_params2 = token; |
| 3970 | irq.type = KVM_S390_INT_PFAULT_INIT; |
| 3971 | WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3972 | } else { |
| 3973 | inti.type = KVM_S390_INT_PFAULT_DONE; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3974 | inti.parm64 = token; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3975 | WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); |
| 3976 | } |
| 3977 | } |
| 3978 | |
Vitaly Kuznetsov | 2a18b7e | 2020-06-10 19:55:32 +0200 | [diff] [blame] | 3979 | bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3980 | struct kvm_async_pf *work) |
| 3981 | { |
| 3982 | trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); |
| 3983 | __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); |
Vitaly Kuznetsov | 2a18b7e | 2020-06-10 19:55:32 +0200 | [diff] [blame] | 3984 | |
| 3985 | return true; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, |
| 3989 | struct kvm_async_pf *work) |
| 3990 | { |
| 3991 | trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); |
| 3992 | __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); |
| 3993 | } |
| 3994 | |
| 3995 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, |
| 3996 | struct kvm_async_pf *work) |
| 3997 | { |
| 3998 | /* s390 will always inject the page directly */ |
| 3999 | } |
| 4000 | |
Vitaly Kuznetsov | 7c0ade6 | 2020-05-25 16:41:18 +0200 | [diff] [blame] | 4001 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4002 | { |
| 4003 | /* |
| 4004 | * s390 will always inject the page directly, |
| 4005 | * but we still want check_async_completion to cleanup |
| 4006 | */ |
| 4007 | return true; |
| 4008 | } |
| 4009 | |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4010 | static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4011 | { |
| 4012 | hva_t hva; |
| 4013 | struct kvm_arch_async_pf arch; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4014 | |
| 4015 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4016 | return false; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4017 | if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != |
| 4018 | vcpu->arch.pfault_compare) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4019 | return false; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4020 | if (psw_extint_disabled(vcpu)) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4021 | return false; |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 4022 | if (kvm_s390_vcpu_has_irq(vcpu, 0)) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4023 | return false; |
David Hildenbrand | b9224cd | 2018-04-30 17:55:24 +0200 | [diff] [blame] | 4024 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4025 | return false; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4026 | if (!vcpu->arch.gmap->pfault_enabled) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4027 | return false; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4028 | |
Heiko Carstens | 81480cc | 2014-01-01 16:36:07 +0100 | [diff] [blame] | 4029 | hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); |
| 4030 | hva += current->thread.gmap_addr & ~PAGE_MASK; |
| 4031 | if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4032 | return false; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4033 | |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 4034 | return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4035 | } |
| 4036 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4037 | static int vcpu_pre_run(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4038 | { |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4039 | int rc, cpuflags; |
Carsten Otte | e168bf8 | 2012-01-04 10:25:22 +0100 | [diff] [blame] | 4040 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4041 | /* |
| 4042 | * On s390 notifications for arriving pages will be delivered directly |
| 4043 | * to the guest but the house keeping for completed pfaults is |
| 4044 | * handled outside the worker. |
| 4045 | */ |
| 4046 | kvm_check_async_pf_completion(vcpu); |
| 4047 | |
Christian Borntraeger | 7ec7c8c | 2015-12-02 14:27:03 +0100 | [diff] [blame] | 4048 | vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; |
| 4049 | vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4050 | |
| 4051 | if (need_resched()) |
| 4052 | schedule(); |
| 4053 | |
Jens Freimann | 7939503 | 2014-04-17 10:10:30 +0200 | [diff] [blame] | 4054 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 4055 | rc = kvm_s390_deliver_pending_interrupts(vcpu); |
| 4056 | if (rc) |
| 4057 | return rc; |
| 4058 | } |
Carsten Otte | 0ff3186 | 2008-05-21 13:37:37 +0200 | [diff] [blame] | 4059 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 4060 | rc = kvm_s390_handle_requests(vcpu); |
| 4061 | if (rc) |
| 4062 | return rc; |
| 4063 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4064 | if (guestdbg_enabled(vcpu)) { |
| 4065 | kvm_s390_backup_guest_per_regs(vcpu); |
| 4066 | kvm_s390_patch_guest_per_regs(vcpu); |
| 4067 | } |
| 4068 | |
Sean Christopherson | 4eeef24 | 2021-09-10 11:32:19 -0700 | [diff] [blame^] | 4069 | clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); |
Michael Mueller | 9f30f62 | 2019-01-31 09:52:44 +0100 | [diff] [blame] | 4070 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4071 | vcpu->arch.sie_block->icptcode = 0; |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4072 | cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); |
| 4073 | VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); |
| 4074 | trace_kvm_s390_sie_enter(vcpu, cpuflags); |
Dominik Dingel | 2b29a9f | 2013-07-26 15:04:00 +0200 | [diff] [blame] | 4075 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4076 | return 0; |
| 4077 | } |
| 4078 | |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 4079 | static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) |
| 4080 | { |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 4081 | struct kvm_s390_pgm_info pgm_info = { |
| 4082 | .code = PGM_ADDRESSING, |
| 4083 | }; |
| 4084 | u8 opcode, ilen; |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 4085 | int rc; |
| 4086 | |
| 4087 | VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); |
| 4088 | trace_kvm_s390_sie_fault(vcpu); |
| 4089 | |
| 4090 | /* |
| 4091 | * We want to inject an addressing exception, which is defined as a |
| 4092 | * suppressing or terminating exception. However, since we came here |
| 4093 | * by a DAT access exception, the PSW still points to the faulting |
| 4094 | * instruction since DAT exceptions are nullifying. So we've got |
| 4095 | * to look up the current opcode to get the length of the instruction |
| 4096 | * to be able to forward the PSW. |
| 4097 | */ |
David Hildenbrand | 3fa8cad7 | 2016-05-24 12:00:49 +0200 | [diff] [blame] | 4098 | rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 4099 | ilen = insn_length(opcode); |
David Hildenbrand | 9b0d721 | 2016-01-12 17:40:54 +0100 | [diff] [blame] | 4100 | if (rc < 0) { |
| 4101 | return rc; |
| 4102 | } else if (rc) { |
| 4103 | /* Instruction-Fetching Exceptions - we can't detect the ilen. |
| 4104 | * Forward by arbitrary ilc, injection will take care of |
| 4105 | * nullification if necessary. |
| 4106 | */ |
| 4107 | pgm_info = vcpu->arch.pgm; |
| 4108 | ilen = 4; |
| 4109 | } |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 4110 | pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; |
| 4111 | kvm_s390_forward_psw(vcpu, ilen); |
| 4112 | return kvm_s390_inject_prog_irq(vcpu, &pgm_info); |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 4113 | } |
| 4114 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4115 | static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) |
| 4116 | { |
QingFeng Hao | 4d62fcc | 2017-06-07 12:03:05 +0200 | [diff] [blame] | 4117 | struct mcck_volatile_info *mcck_info; |
| 4118 | struct sie_page *sie_page; |
| 4119 | |
Dominik Dingel | 2b29a9f | 2013-07-26 15:04:00 +0200 | [diff] [blame] | 4120 | VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", |
| 4121 | vcpu->arch.sie_block->icptcode); |
| 4122 | trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); |
| 4123 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4124 | if (guestdbg_enabled(vcpu)) |
| 4125 | kvm_s390_restore_guest_per_regs(vcpu); |
| 4126 | |
Christian Borntraeger | 7ec7c8c | 2015-12-02 14:27:03 +0100 | [diff] [blame] | 4127 | vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; |
| 4128 | vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4129 | |
QingFeng Hao | 4d62fcc | 2017-06-07 12:03:05 +0200 | [diff] [blame] | 4130 | if (exit_reason == -EINTR) { |
| 4131 | VCPU_EVENT(vcpu, 3, "%s", "machine check"); |
| 4132 | sie_page = container_of(vcpu->arch.sie_block, |
| 4133 | struct sie_page, sie_block); |
| 4134 | mcck_info = &sie_page->mcck_info; |
| 4135 | kvm_s390_reinject_machine_check(vcpu, mcck_info); |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4139 | if (vcpu->arch.sie_block->icptcode > 0) { |
| 4140 | int rc = kvm_handle_sie_intercept(vcpu); |
| 4141 | |
| 4142 | if (rc != -EOPNOTSUPP) |
| 4143 | return rc; |
| 4144 | vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; |
| 4145 | vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; |
| 4146 | vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; |
| 4147 | vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; |
| 4148 | return -EREMOTE; |
| 4149 | } else if (exit_reason != -EFAULT) { |
| 4150 | vcpu->stat.exit_null++; |
| 4151 | return 0; |
Thomas Huth | 210b1607 | 2013-09-19 16:26:18 +0200 | [diff] [blame] | 4152 | } else if (kvm_is_ucontrol(vcpu->kvm)) { |
| 4153 | vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; |
| 4154 | vcpu->run->s390_ucontrol.trans_exc_code = |
| 4155 | current->thread.gmap_addr; |
| 4156 | vcpu->run->s390_ucontrol.pgm_code = 0x10; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4157 | return -EREMOTE; |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4158 | } else if (current->thread.gmap_pfault) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4159 | trace_kvm_s390_major_guest_pfault(vcpu); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4160 | current->thread.gmap_pfault = 0; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4161 | if (kvm_arch_setup_async_pf(vcpu)) |
| 4162 | return 0; |
Christian Borntraeger | 50a05be | 2020-11-25 10:06:58 +0100 | [diff] [blame] | 4163 | vcpu->stat.pfault_sync++; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4164 | return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4165 | } |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4166 | return vcpu_post_run_fault_in_sie(vcpu); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Janosch Frank | 3adae0b | 2019-12-13 08:26:06 -0500 | [diff] [blame] | 4169 | #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4170 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
| 4171 | { |
| 4172 | int rc, exit_reason; |
Janosch Frank | c8aac23 | 2019-05-08 15:52:00 +0200 | [diff] [blame] | 4173 | struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4174 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4175 | /* |
| 4176 | * We try to hold kvm->srcu during most of vcpu_run (except when run- |
| 4177 | * ning the guest), so that memslots (and other stuff) are protected |
| 4178 | */ |
| 4179 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4180 | |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4181 | do { |
| 4182 | rc = vcpu_pre_run(vcpu); |
| 4183 | if (rc) |
| 4184 | break; |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4185 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4186 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4187 | /* |
| 4188 | * As PF_VCPU will be used in fault handler, between |
| 4189 | * guest_enter and guest_exit should be no uaccess. |
| 4190 | */ |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4191 | local_irq_disable(); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 4192 | guest_enter_irqoff(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4193 | __disable_cpu_timer_accounting(vcpu); |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4194 | local_irq_enable(); |
Janosch Frank | c8aac23 | 2019-05-08 15:52:00 +0200 | [diff] [blame] | 4195 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4196 | memcpy(sie_page->pv_grregs, |
| 4197 | vcpu->run->s.regs.gprs, |
| 4198 | sizeof(sie_page->pv_grregs)); |
| 4199 | } |
Sven Schnelle | 56e62a7 | 2020-11-21 11:14:56 +0100 | [diff] [blame] | 4200 | if (test_cpu_flag(CIF_FPU)) |
| 4201 | load_fpu_regs(); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4202 | exit_reason = sie64a(vcpu->arch.sie_block, |
| 4203 | vcpu->run->s.regs.gprs); |
Janosch Frank | c8aac23 | 2019-05-08 15:52:00 +0200 | [diff] [blame] | 4204 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4205 | memcpy(vcpu->run->s.regs.gprs, |
| 4206 | sie_page->pv_grregs, |
| 4207 | sizeof(sie_page->pv_grregs)); |
Janosch Frank | 3adae0b | 2019-12-13 08:26:06 -0500 | [diff] [blame] | 4208 | /* |
| 4209 | * We're not allowed to inject interrupts on intercepts |
| 4210 | * that leave the guest state in an "in-between" state |
| 4211 | * where the next SIE entry will do a continuation. |
| 4212 | * Fence interrupts in our "internal" PSW. |
| 4213 | */ |
| 4214 | if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || |
| 4215 | vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { |
| 4216 | vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; |
| 4217 | } |
Janosch Frank | c8aac23 | 2019-05-08 15:52:00 +0200 | [diff] [blame] | 4218 | } |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4219 | local_irq_disable(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4220 | __enable_cpu_timer_accounting(vcpu); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 4221 | guest_exit_irqoff(); |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4222 | local_irq_enable(); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4223 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4224 | |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4225 | rc = vcpu_post_run(vcpu, exit_reason); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4226 | } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4227 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4228 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Carsten Otte | e168bf8 | 2012-01-04 10:25:22 +0100 | [diff] [blame] | 4229 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4230 | } |
| 4231 | |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4232 | static void sync_regs_fmt2(struct kvm_vcpu *vcpu) |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4233 | { |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4234 | struct kvm_run *kvm_run = vcpu->run; |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4235 | struct runtime_instr_cb *riccb; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4236 | struct gs_cb *gscb; |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4237 | |
| 4238 | riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4239 | gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4240 | vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; |
| 4241 | vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4242 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4243 | vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; |
| 4244 | vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; |
| 4245 | vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; |
| 4246 | } |
| 4247 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { |
| 4248 | vcpu->arch.pfault_token = kvm_run->s.regs.pft; |
| 4249 | vcpu->arch.pfault_select = kvm_run->s.regs.pfs; |
| 4250 | vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; |
David Hildenbrand | 9fbd808 | 2014-10-09 15:01:38 +0200 | [diff] [blame] | 4251 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 4252 | kvm_clear_async_pf_completion_queue(vcpu); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4253 | } |
Collin Walling | 23a60f8 | 2020-06-22 11:46:36 -0400 | [diff] [blame] | 4254 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { |
| 4255 | vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; |
| 4256 | vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; |
| 4257 | } |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4258 | /* |
| 4259 | * If userspace sets the riccb (e.g. after migration) to a valid state, |
| 4260 | * we should enable RI here instead of doing the lazy enablement. |
| 4261 | */ |
| 4262 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4263 | test_kvm_facility(vcpu->kvm, 64) && |
Alice Frosi | bb59c2d | 2017-09-14 12:35:45 +0200 | [diff] [blame] | 4264 | riccb->v && |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 4265 | !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4266 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 4267 | vcpu->arch.sie_block->ecb3 |= ECB3_RI; |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4268 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4269 | /* |
| 4270 | * If userspace sets the gscb (e.g. after migration) to non-zero, |
| 4271 | * we should enable GS here instead of doing the lazy enablement. |
| 4272 | */ |
| 4273 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && |
| 4274 | test_kvm_facility(vcpu->kvm, 133) && |
| 4275 | gscb->gssm && |
| 4276 | !vcpu->arch.gs_enabled) { |
| 4277 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); |
| 4278 | vcpu->arch.sie_block->ecb |= ECB_GS; |
| 4279 | vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; |
| 4280 | vcpu->arch.gs_enabled = 1; |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4281 | } |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 4282 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && |
| 4283 | test_kvm_facility(vcpu->kvm, 82)) { |
| 4284 | vcpu->arch.sie_block->fpf &= ~FPF_BPBC; |
| 4285 | vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; |
| 4286 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4287 | if (MACHINE_HAS_GS) { |
| 4288 | preempt_disable(); |
| 4289 | __ctl_set_bit(2, 4); |
| 4290 | if (current->thread.gs_cb) { |
| 4291 | vcpu->arch.host_gscb = current->thread.gs_cb; |
| 4292 | save_gs_cb(vcpu->arch.host_gscb); |
| 4293 | } |
| 4294 | if (vcpu->arch.gs_enabled) { |
| 4295 | current->thread.gs_cb = (struct gs_cb *) |
| 4296 | &vcpu->run->s.regs.gscb; |
| 4297 | restore_gs_cb(current->thread.gs_cb); |
| 4298 | } |
| 4299 | preempt_enable(); |
| 4300 | } |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 4301 | /* SIE will load etoken directly from SDNX and therefore kvm_run */ |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4302 | } |
| 4303 | |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4304 | static void sync_regs(struct kvm_vcpu *vcpu) |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4305 | { |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4306 | struct kvm_run *kvm_run = vcpu->run; |
| 4307 | |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4308 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) |
| 4309 | kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); |
| 4310 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { |
| 4311 | memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); |
| 4312 | /* some control register changes require a tlb flush */ |
| 4313 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 4314 | } |
| 4315 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { |
| 4316 | kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); |
| 4317 | vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; |
| 4318 | } |
| 4319 | save_access_regs(vcpu->arch.host_acrs); |
| 4320 | restore_access_regs(vcpu->run->s.regs.acrs); |
| 4321 | /* save host (userspace) fprs/vrs */ |
| 4322 | save_fpu_regs(); |
| 4323 | vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; |
| 4324 | vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; |
| 4325 | if (MACHINE_HAS_VX) |
| 4326 | current->thread.fpu.regs = vcpu->run->s.regs.vrs; |
| 4327 | else |
| 4328 | current->thread.fpu.regs = vcpu->run->s.regs.fprs; |
| 4329 | current->thread.fpu.fpc = vcpu->run->s.regs.fpc; |
| 4330 | if (test_fp_ctl(current->thread.fpu.fpc)) |
| 4331 | /* User space provided an invalid FPC, let's clear it */ |
| 4332 | current->thread.fpu.fpc = 0; |
| 4333 | |
| 4334 | /* Sync fmt2 only data */ |
| 4335 | if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4336 | sync_regs_fmt2(vcpu); |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4337 | } else { |
| 4338 | /* |
| 4339 | * In several places we have to modify our internal view to |
| 4340 | * not do things that are disallowed by the ultravisor. For |
| 4341 | * example we must not inject interrupts after specific exits |
| 4342 | * (e.g. 112 prefix page not secure). We do this by turning |
| 4343 | * off the machine check, external and I/O interrupt bits |
| 4344 | * of our PSW copy. To avoid getting validity intercepts, we |
| 4345 | * do only accept the condition code from userspace. |
| 4346 | */ |
| 4347 | vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; |
| 4348 | vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & |
| 4349 | PSW_MASK_CC; |
| 4350 | } |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4351 | |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4352 | kvm_run->kvm_dirty_regs = 0; |
| 4353 | } |
| 4354 | |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4355 | static void store_regs_fmt2(struct kvm_vcpu *vcpu) |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4356 | { |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4357 | struct kvm_run *kvm_run = vcpu->run; |
| 4358 | |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4359 | kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; |
| 4360 | kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; |
| 4361 | kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 4362 | kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; |
Collin Walling | 23a60f8 | 2020-06-22 11:46:36 -0400 | [diff] [blame] | 4363 | kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4364 | if (MACHINE_HAS_GS) { |
Heiko Carstens | 44bada2 | 2021-04-15 10:01:27 +0200 | [diff] [blame] | 4365 | preempt_disable(); |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4366 | __ctl_set_bit(2, 4); |
| 4367 | if (vcpu->arch.gs_enabled) |
| 4368 | save_gs_cb(current->thread.gs_cb); |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4369 | current->thread.gs_cb = vcpu->arch.host_gscb; |
| 4370 | restore_gs_cb(vcpu->arch.host_gscb); |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4371 | if (!vcpu->arch.host_gscb) |
| 4372 | __ctl_clear_bit(2, 4); |
| 4373 | vcpu->arch.host_gscb = NULL; |
Heiko Carstens | 44bada2 | 2021-04-15 10:01:27 +0200 | [diff] [blame] | 4374 | preempt_enable(); |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4375 | } |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 4376 | /* SIE will save etoken directly into SDNX and therefore kvm_run */ |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4377 | } |
| 4378 | |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4379 | static void store_regs(struct kvm_vcpu *vcpu) |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4380 | { |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4381 | struct kvm_run *kvm_run = vcpu->run; |
| 4382 | |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4383 | kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; |
| 4384 | kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; |
| 4385 | kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); |
| 4386 | memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); |
| 4387 | kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); |
| 4388 | kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; |
| 4389 | kvm_run->s.regs.pft = vcpu->arch.pfault_token; |
| 4390 | kvm_run->s.regs.pfs = vcpu->arch.pfault_select; |
| 4391 | kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; |
| 4392 | save_access_regs(vcpu->run->s.regs.acrs); |
| 4393 | restore_access_regs(vcpu->arch.host_acrs); |
| 4394 | /* Save guest register state */ |
| 4395 | save_fpu_regs(); |
| 4396 | vcpu->run->s.regs.fpc = current->thread.fpu.fpc; |
| 4397 | /* Restore will be done lazily at return */ |
| 4398 | current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; |
| 4399 | current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; |
| 4400 | if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4401 | store_regs_fmt2(vcpu); |
Janosch Frank | 811ea79 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4402 | } |
| 4403 | |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 4404 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4405 | { |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 4406 | struct kvm_run *kvm_run = vcpu->run; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4407 | int rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4408 | |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 4409 | if (kvm_run->immediate_exit) |
| 4410 | return -EINTR; |
| 4411 | |
Thomas Huth | 200824f | 2019-09-04 10:51:59 +0200 | [diff] [blame] | 4412 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || |
| 4413 | kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) |
| 4414 | return -EINVAL; |
| 4415 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4416 | vcpu_load(vcpu); |
| 4417 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4418 | if (guestdbg_exit_pending(vcpu)) { |
| 4419 | kvm_s390_prepare_debug_exit(vcpu); |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4420 | rc = 0; |
| 4421 | goto out; |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4422 | } |
| 4423 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 4424 | kvm_sigset_activate(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4425 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4426 | /* |
| 4427 | * no need to check the return value of vcpu_start as it can only have |
| 4428 | * an error for protvirt, but protvirt means user cpu state |
| 4429 | */ |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4430 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { |
| 4431 | kvm_s390_vcpu_start(vcpu); |
| 4432 | } else if (is_vcpu_stopped(vcpu)) { |
David Hildenbrand | ea2cdd2 | 2015-05-20 13:24:02 +0200 | [diff] [blame] | 4433 | pr_err_ratelimited("can't run stopped vcpu %d\n", |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4434 | vcpu->vcpu_id); |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4435 | rc = -EINVAL; |
| 4436 | goto out; |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4437 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4438 | |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4439 | sync_regs(vcpu); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4440 | enable_cpu_timer_accounting(vcpu); |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 4441 | |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 4442 | might_fault(); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4443 | rc = __vcpu_run(vcpu); |
Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 4444 | |
Christian Ehrhardt | b1d16c4 | 2009-05-20 15:34:56 +0200 | [diff] [blame] | 4445 | if (signal_pending(current) && !rc) { |
| 4446 | kvm_run->exit_reason = KVM_EXIT_INTR; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4447 | rc = -EINTR; |
Christian Ehrhardt | b1d16c4 | 2009-05-20 15:34:56 +0200 | [diff] [blame] | 4448 | } |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4449 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4450 | if (guestdbg_exit_pending(vcpu) && !rc) { |
| 4451 | kvm_s390_prepare_debug_exit(vcpu); |
| 4452 | rc = 0; |
| 4453 | } |
| 4454 | |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4455 | if (rc == -EREMOTE) { |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4456 | /* userspace support is needed, kvm_run has been prepared */ |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4457 | rc = 0; |
| 4458 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4459 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4460 | disable_cpu_timer_accounting(vcpu); |
Tianjia Zhang | 2f0a83b | 2020-06-23 21:14:14 +0800 | [diff] [blame] | 4461 | store_regs(vcpu); |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 4462 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 4463 | kvm_sigset_deactivate(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4464 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4465 | vcpu->stat.exit_userspace++; |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4466 | out: |
| 4467 | vcpu_put(vcpu); |
Heiko Carstens | 7e8e6ab | 2008-04-04 15:12:35 +0200 | [diff] [blame] | 4468 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4469 | } |
| 4470 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4471 | /* |
| 4472 | * store status at address |
| 4473 | * we use have two special cases: |
| 4474 | * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit |
| 4475 | * KVM_S390_STORE_STATUS_PREFIXED: -> prefix |
| 4476 | */ |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4477 | int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4478 | { |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 4479 | unsigned char archmode = 1; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4480 | freg_t fprs[NUM_FPRS]; |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 4481 | unsigned int px; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4482 | u64 clkcomp, cputm; |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4483 | int rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4484 | |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4485 | px = kvm_s390_get_prefix(vcpu); |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4486 | if (gpa == KVM_S390_STORE_STATUS_NOADDR) { |
| 4487 | if (write_guest_abs(vcpu, 163, &archmode, 1)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4488 | return -EFAULT; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4489 | gpa = 0; |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4490 | } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { |
| 4491 | if (write_guest_real(vcpu, 163, &archmode, 1)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4492 | return -EFAULT; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4493 | gpa = px; |
| 4494 | } else |
| 4495 | gpa -= __LC_FPREGS_SAVE_AREA; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* manually convert vector registers if necessary */ |
| 4498 | if (MACHINE_HAS_VX) { |
David Hildenbrand | 9522b37 | 2016-03-08 12:24:30 +0100 | [diff] [blame] | 4499 | convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4500 | rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, |
| 4501 | fprs, 128); |
| 4502 | } else { |
| 4503 | rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, |
David Hildenbrand | 6fd8e67 | 2016-01-18 14:46:34 +0100 | [diff] [blame] | 4504 | vcpu->run->s.regs.fprs, 128); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4505 | } |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4506 | rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4507 | vcpu->run->s.regs.gprs, 128); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4508 | rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4509 | &vcpu->arch.sie_block->gpsw, 16); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4510 | rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 4511 | &px, 4); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4512 | rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4513 | &vcpu->run->s.regs.fpc, 4); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4514 | rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4515 | &vcpu->arch.sie_block->todpr, 4); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4516 | cputm = kvm_s390_get_cpu_timer(vcpu); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4517 | rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4518 | &cputm, 8); |
Thomas Huth | 178bd78 | 2013-11-13 20:28:18 +0100 | [diff] [blame] | 4519 | clkcomp = vcpu->arch.sie_block->ckc >> 8; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4520 | rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4521 | &clkcomp, 8); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4522 | rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4523 | &vcpu->run->s.regs.acrs, 64); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4524 | rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4525 | &vcpu->arch.sie_block->gcr, 128); |
| 4526 | return rc ? -EFAULT : 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4527 | } |
| 4528 | |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4529 | int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) |
| 4530 | { |
| 4531 | /* |
| 4532 | * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy |
Christian Borntraeger | 31d8b8d | 2016-11-10 14:22:02 +0100 | [diff] [blame] | 4533 | * switch in the run ioctl. Let's update our copies before we save |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4534 | * it into the save area |
| 4535 | */ |
Hendrik Brueckner | d0164ee | 2015-06-29 16:43:06 +0200 | [diff] [blame] | 4536 | save_fpu_regs(); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4537 | vcpu->run->s.regs.fpc = current->thread.fpu.fpc; |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4538 | save_access_regs(vcpu->run->s.regs.acrs); |
| 4539 | |
| 4540 | return kvm_s390_store_status_unloaded(vcpu, addr); |
| 4541 | } |
| 4542 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4543 | static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) |
| 4544 | { |
| 4545 | kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 4546 | kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | static void __disable_ibs_on_all_vcpus(struct kvm *kvm) |
| 4550 | { |
| 4551 | unsigned int i; |
| 4552 | struct kvm_vcpu *vcpu; |
| 4553 | |
| 4554 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4555 | __disable_ibs_on_vcpu(vcpu); |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) |
| 4560 | { |
David Hildenbrand | 09a400e | 2016-04-04 15:57:08 +0200 | [diff] [blame] | 4561 | if (!sclp.has_ibs) |
| 4562 | return; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4563 | kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 4564 | kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4565 | } |
| 4566 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4567 | int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4568 | { |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4569 | int i, online_vcpus, r = 0, started_vcpus = 0; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4570 | |
| 4571 | if (!is_vcpu_stopped(vcpu)) |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4572 | return 0; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4573 | |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4574 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4575 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4576 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4577 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
| 4578 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4579 | /* Let's tell the UV that we want to change into the operating state */ |
| 4580 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4581 | r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); |
| 4582 | if (r) { |
| 4583 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
| 4584 | return r; |
| 4585 | } |
| 4586 | } |
| 4587 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4588 | for (i = 0; i < online_vcpus; i++) { |
| 4589 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) |
| 4590 | started_vcpus++; |
| 4591 | } |
| 4592 | |
| 4593 | if (started_vcpus == 0) { |
| 4594 | /* we're the only active VCPU -> speed it up */ |
| 4595 | __enable_ibs_on_vcpu(vcpu); |
| 4596 | } else if (started_vcpus == 1) { |
| 4597 | /* |
| 4598 | * As we are starting a second VCPU, we have to disable |
| 4599 | * the IBS facility on all VCPUs to remove potentially |
Bhaskar Chowdhury | 3886075 | 2021-02-13 21:02:27 +0530 | [diff] [blame] | 4600 | * outstanding ENABLE requests. |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4601 | */ |
| 4602 | __disable_ibs_on_all_vcpus(vcpu->kvm); |
| 4603 | } |
| 4604 | |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 4605 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4606 | /* |
Christian Borntraeger | 72f2182 | 2020-01-30 11:18:28 -0500 | [diff] [blame] | 4607 | * The real PSW might have changed due to a RESTART interpreted by the |
| 4608 | * ultravisor. We block all interrupts and let the next sie exit |
| 4609 | * refresh our view. |
| 4610 | */ |
| 4611 | if (kvm_s390_pv_cpu_is_protected(vcpu)) |
| 4612 | vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; |
| 4613 | /* |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4614 | * Another VCPU might have used IBS while we were offline. |
| 4615 | * Let's play safe and flush the VCPU at startup. |
| 4616 | */ |
David Hildenbrand | d3d692c | 2014-07-29 08:53:36 +0200 | [diff] [blame] | 4617 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4618 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4619 | return 0; |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4620 | } |
| 4621 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4622 | int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4623 | { |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4624 | int i, online_vcpus, r = 0, started_vcpus = 0; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4625 | struct kvm_vcpu *started_vcpu = NULL; |
| 4626 | |
| 4627 | if (is_vcpu_stopped(vcpu)) |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4628 | return 0; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4629 | |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4630 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4631 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4632 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4633 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
| 4634 | |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4635 | /* Let's tell the UV that we want to change into the stopped state */ |
| 4636 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4637 | r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); |
| 4638 | if (r) { |
| 4639 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
| 4640 | return r; |
| 4641 | } |
| 4642 | } |
| 4643 | |
David Hildenbrand | 32f5ff63 | 2014-04-14 12:40:03 +0200 | [diff] [blame] | 4644 | /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 4645 | kvm_s390_clear_stop_irq(vcpu); |
David Hildenbrand | 32f5ff63 | 2014-04-14 12:40:03 +0200 | [diff] [blame] | 4646 | |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 4647 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4648 | __disable_ibs_on_vcpu(vcpu); |
| 4649 | |
| 4650 | for (i = 0; i < online_vcpus; i++) { |
| 4651 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { |
| 4652 | started_vcpus++; |
| 4653 | started_vcpu = vcpu->kvm->vcpus[i]; |
| 4654 | } |
| 4655 | } |
| 4656 | |
| 4657 | if (started_vcpus == 1) { |
| 4658 | /* |
| 4659 | * As we only have one VCPU left, we want to enable the |
| 4660 | * IBS facility for that VCPU to speed it up. |
| 4661 | */ |
| 4662 | __enable_ibs_on_vcpu(started_vcpu); |
| 4663 | } |
| 4664 | |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4665 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
Janosch Frank | fe28c786 | 2019-05-15 13:24:30 +0200 | [diff] [blame] | 4666 | return 0; |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4667 | } |
| 4668 | |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4669 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 4670 | struct kvm_enable_cap *cap) |
| 4671 | { |
| 4672 | int r; |
| 4673 | |
| 4674 | if (cap->flags) |
| 4675 | return -EINVAL; |
| 4676 | |
| 4677 | switch (cap->cap) { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 4678 | case KVM_CAP_S390_CSS_SUPPORT: |
| 4679 | if (!vcpu->kvm->arch.css_support) { |
| 4680 | vcpu->kvm->arch.css_support = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 4681 | VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 4682 | trace_kvm_s390_enable_css(vcpu->kvm); |
| 4683 | } |
| 4684 | r = 0; |
| 4685 | break; |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4686 | default: |
| 4687 | r = -EINVAL; |
| 4688 | break; |
| 4689 | } |
| 4690 | return r; |
| 4691 | } |
| 4692 | |
Janosch Frank | 19e1227 | 2019-04-02 09:21:06 +0200 | [diff] [blame] | 4693 | static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu, |
| 4694 | struct kvm_s390_mem_op *mop) |
| 4695 | { |
| 4696 | void __user *uaddr = (void __user *)mop->buf; |
| 4697 | int r = 0; |
| 4698 | |
| 4699 | if (mop->flags || !mop->size) |
| 4700 | return -EINVAL; |
| 4701 | if (mop->size + mop->sida_offset < mop->size) |
| 4702 | return -EINVAL; |
| 4703 | if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) |
| 4704 | return -E2BIG; |
| 4705 | |
| 4706 | switch (mop->op) { |
| 4707 | case KVM_S390_MEMOP_SIDA_READ: |
| 4708 | if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) + |
| 4709 | mop->sida_offset), mop->size)) |
| 4710 | r = -EFAULT; |
| 4711 | |
| 4712 | break; |
| 4713 | case KVM_S390_MEMOP_SIDA_WRITE: |
| 4714 | if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) + |
| 4715 | mop->sida_offset), uaddr, mop->size)) |
| 4716 | r = -EFAULT; |
| 4717 | break; |
| 4718 | } |
| 4719 | return r; |
| 4720 | } |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4721 | static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, |
| 4722 | struct kvm_s390_mem_op *mop) |
| 4723 | { |
| 4724 | void __user *uaddr = (void __user *)mop->buf; |
| 4725 | void *tmpbuf = NULL; |
Janosch Frank | 19e1227 | 2019-04-02 09:21:06 +0200 | [diff] [blame] | 4726 | int r = 0; |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4727 | const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION |
| 4728 | | KVM_S390_MEMOP_F_CHECK_ONLY; |
| 4729 | |
Thomas Huth | a13b03b | 2019-08-29 14:25:17 +0200 | [diff] [blame] | 4730 | if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4731 | return -EINVAL; |
| 4732 | |
| 4733 | if (mop->size > MEM_OP_MAX_SIZE) |
| 4734 | return -E2BIG; |
| 4735 | |
Janosch Frank | 19e1227 | 2019-04-02 09:21:06 +0200 | [diff] [blame] | 4736 | if (kvm_s390_pv_cpu_is_protected(vcpu)) |
| 4737 | return -EINVAL; |
| 4738 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4739 | if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { |
| 4740 | tmpbuf = vmalloc(mop->size); |
| 4741 | if (!tmpbuf) |
| 4742 | return -ENOMEM; |
| 4743 | } |
| 4744 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4745 | switch (mop->op) { |
| 4746 | case KVM_S390_MEMOP_LOGICAL_READ: |
| 4747 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 4748 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, |
| 4749 | mop->size, GACC_FETCH); |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4750 | break; |
| 4751 | } |
| 4752 | r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); |
| 4753 | if (r == 0) { |
| 4754 | if (copy_to_user(uaddr, tmpbuf, mop->size)) |
| 4755 | r = -EFAULT; |
| 4756 | } |
| 4757 | break; |
| 4758 | case KVM_S390_MEMOP_LOGICAL_WRITE: |
| 4759 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 4760 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, |
| 4761 | mop->size, GACC_STORE); |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4762 | break; |
| 4763 | } |
| 4764 | if (copy_from_user(tmpbuf, uaddr, mop->size)) { |
| 4765 | r = -EFAULT; |
| 4766 | break; |
| 4767 | } |
| 4768 | r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); |
| 4769 | break; |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4770 | } |
| 4771 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4772 | if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) |
| 4773 | kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); |
| 4774 | |
| 4775 | vfree(tmpbuf); |
| 4776 | return r; |
| 4777 | } |
| 4778 | |
Janosch Frank | 19e1227 | 2019-04-02 09:21:06 +0200 | [diff] [blame] | 4779 | static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu, |
| 4780 | struct kvm_s390_mem_op *mop) |
| 4781 | { |
| 4782 | int r, srcu_idx; |
| 4783 | |
| 4784 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4785 | |
| 4786 | switch (mop->op) { |
| 4787 | case KVM_S390_MEMOP_LOGICAL_READ: |
| 4788 | case KVM_S390_MEMOP_LOGICAL_WRITE: |
| 4789 | r = kvm_s390_guest_mem_op(vcpu, mop); |
| 4790 | break; |
| 4791 | case KVM_S390_MEMOP_SIDA_READ: |
| 4792 | case KVM_S390_MEMOP_SIDA_WRITE: |
| 4793 | /* we are locked against sida going away by the vcpu->mutex */ |
| 4794 | r = kvm_s390_guest_sida_op(vcpu, mop); |
| 4795 | break; |
| 4796 | default: |
| 4797 | r = -EINVAL; |
| 4798 | } |
| 4799 | |
| 4800 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
| 4801 | return r; |
| 4802 | } |
| 4803 | |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 4804 | long kvm_arch_vcpu_async_ioctl(struct file *filp, |
| 4805 | unsigned int ioctl, unsigned long arg) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4806 | { |
| 4807 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4808 | void __user *argp = (void __user *)arg; |
| 4809 | |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 4810 | switch (ioctl) { |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4811 | case KVM_S390_IRQ: { |
| 4812 | struct kvm_s390_irq s390irq; |
| 4813 | |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4814 | if (copy_from_user(&s390irq, argp, sizeof(s390irq))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4815 | return -EFAULT; |
| 4816 | return kvm_s390_inject_vcpu(vcpu, &s390irq); |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4817 | } |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 4818 | case KVM_S390_INTERRUPT: { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4819 | struct kvm_s390_interrupt s390int; |
Thomas Huth | 53936b5 | 2019-09-12 13:54:38 +0200 | [diff] [blame] | 4820 | struct kvm_s390_irq s390irq = {}; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4821 | |
| 4822 | if (copy_from_user(&s390int, argp, sizeof(s390int))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4823 | return -EFAULT; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 4824 | if (s390int_to_s390irq(&s390int, &s390irq)) |
| 4825 | return -EINVAL; |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4826 | return kvm_s390_inject_vcpu(vcpu, &s390irq); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4827 | } |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4828 | } |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 4829 | return -ENOIOCTLCMD; |
| 4830 | } |
| 4831 | |
| 4832 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 4833 | unsigned int ioctl, unsigned long arg) |
| 4834 | { |
| 4835 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4836 | void __user *argp = (void __user *)arg; |
| 4837 | int idx; |
| 4838 | long r; |
Janosch Frank | 8a8378f | 2020-01-09 04:37:50 -0500 | [diff] [blame] | 4839 | u16 rc, rrc; |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4840 | |
| 4841 | vcpu_load(vcpu); |
| 4842 | |
| 4843 | switch (ioctl) { |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4844 | case KVM_S390_STORE_STATUS: |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4845 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Christian Borntraeger | 5568089 | 2020-01-31 05:02:00 -0500 | [diff] [blame] | 4846 | r = kvm_s390_store_status_unloaded(vcpu, arg); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4847 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4848 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4849 | case KVM_S390_SET_INITIAL_PSW: { |
| 4850 | psw_t psw; |
| 4851 | |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4852 | r = -EFAULT; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4853 | if (copy_from_user(&psw, argp, sizeof(psw))) |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4854 | break; |
| 4855 | r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); |
| 4856 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4857 | } |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4858 | case KVM_S390_CLEAR_RESET: |
| 4859 | r = 0; |
| 4860 | kvm_arch_vcpu_ioctl_clear_reset(vcpu); |
Janosch Frank | 8a8378f | 2020-01-09 04:37:50 -0500 | [diff] [blame] | 4861 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4862 | r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), |
| 4863 | UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); |
| 4864 | VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", |
| 4865 | rc, rrc); |
| 4866 | } |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4867 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4868 | case KVM_S390_INITIAL_RESET: |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4869 | r = 0; |
| 4870 | kvm_arch_vcpu_ioctl_initial_reset(vcpu); |
Janosch Frank | 8a8378f | 2020-01-09 04:37:50 -0500 | [diff] [blame] | 4871 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4872 | r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), |
| 4873 | UVC_CMD_CPU_RESET_INITIAL, |
| 4874 | &rc, &rrc); |
| 4875 | VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", |
| 4876 | rc, rrc); |
| 4877 | } |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4878 | break; |
| 4879 | case KVM_S390_NORMAL_RESET: |
| 4880 | r = 0; |
| 4881 | kvm_arch_vcpu_ioctl_normal_reset(vcpu); |
Janosch Frank | 8a8378f | 2020-01-09 04:37:50 -0500 | [diff] [blame] | 4882 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
| 4883 | r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), |
| 4884 | UVC_CMD_CPU_RESET, &rc, &rrc); |
| 4885 | VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", |
| 4886 | rc, rrc); |
| 4887 | } |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4888 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 4889 | case KVM_SET_ONE_REG: |
| 4890 | case KVM_GET_ONE_REG: { |
| 4891 | struct kvm_one_reg reg; |
Janosch Frank | 68cf7b1 | 2019-06-14 13:11:21 +0200 | [diff] [blame] | 4892 | r = -EINVAL; |
| 4893 | if (kvm_s390_pv_cpu_is_protected(vcpu)) |
| 4894 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 4895 | r = -EFAULT; |
| 4896 | if (copy_from_user(®, argp, sizeof(reg))) |
| 4897 | break; |
| 4898 | if (ioctl == KVM_SET_ONE_REG) |
| 4899 | r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); |
| 4900 | else |
| 4901 | r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); |
| 4902 | break; |
| 4903 | } |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 4904 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 4905 | case KVM_S390_UCAS_MAP: { |
| 4906 | struct kvm_s390_ucas_mapping ucasmap; |
| 4907 | |
| 4908 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { |
| 4909 | r = -EFAULT; |
| 4910 | break; |
| 4911 | } |
| 4912 | |
| 4913 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 4914 | r = -EINVAL; |
| 4915 | break; |
| 4916 | } |
| 4917 | |
| 4918 | r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, |
| 4919 | ucasmap.vcpu_addr, ucasmap.length); |
| 4920 | break; |
| 4921 | } |
| 4922 | case KVM_S390_UCAS_UNMAP: { |
| 4923 | struct kvm_s390_ucas_mapping ucasmap; |
| 4924 | |
| 4925 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { |
| 4926 | r = -EFAULT; |
| 4927 | break; |
| 4928 | } |
| 4929 | |
| 4930 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 4931 | r = -EINVAL; |
| 4932 | break; |
| 4933 | } |
| 4934 | |
| 4935 | r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, |
| 4936 | ucasmap.length); |
| 4937 | break; |
| 4938 | } |
| 4939 | #endif |
Carsten Otte | ccc7910 | 2012-01-04 10:25:26 +0100 | [diff] [blame] | 4940 | case KVM_S390_VCPU_FAULT: { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 4941 | r = gmap_fault(vcpu->arch.gmap, arg, 0); |
Carsten Otte | ccc7910 | 2012-01-04 10:25:26 +0100 | [diff] [blame] | 4942 | break; |
| 4943 | } |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4944 | case KVM_ENABLE_CAP: |
| 4945 | { |
| 4946 | struct kvm_enable_cap cap; |
| 4947 | r = -EFAULT; |
| 4948 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 4949 | break; |
| 4950 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 4951 | break; |
| 4952 | } |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4953 | case KVM_S390_MEM_OP: { |
| 4954 | struct kvm_s390_mem_op mem_op; |
| 4955 | |
| 4956 | if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) |
Janosch Frank | 19e1227 | 2019-04-02 09:21:06 +0200 | [diff] [blame] | 4957 | r = kvm_s390_guest_memsida_op(vcpu, &mem_op); |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4958 | else |
| 4959 | r = -EFAULT; |
| 4960 | break; |
| 4961 | } |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4962 | case KVM_S390_SET_IRQ_STATE: { |
| 4963 | struct kvm_s390_irq_state irq_state; |
| 4964 | |
| 4965 | r = -EFAULT; |
| 4966 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) |
| 4967 | break; |
| 4968 | if (irq_state.len > VCPU_IRQS_MAX_BUF || |
| 4969 | irq_state.len == 0 || |
| 4970 | irq_state.len % sizeof(struct kvm_s390_irq) > 0) { |
| 4971 | r = -EINVAL; |
| 4972 | break; |
| 4973 | } |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 4974 | /* do not use irq_state.flags, it will break old QEMUs */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4975 | r = kvm_s390_set_irq_state(vcpu, |
| 4976 | (void __user *) irq_state.buf, |
| 4977 | irq_state.len); |
| 4978 | break; |
| 4979 | } |
| 4980 | case KVM_S390_GET_IRQ_STATE: { |
| 4981 | struct kvm_s390_irq_state irq_state; |
| 4982 | |
| 4983 | r = -EFAULT; |
| 4984 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) |
| 4985 | break; |
| 4986 | if (irq_state.len == 0) { |
| 4987 | r = -EINVAL; |
| 4988 | break; |
| 4989 | } |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 4990 | /* do not use irq_state.flags, it will break old QEMUs */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4991 | r = kvm_s390_get_irq_state(vcpu, |
| 4992 | (__u8 __user *) irq_state.buf, |
| 4993 | irq_state.len); |
| 4994 | break; |
| 4995 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4996 | default: |
Carsten Otte | 3e6afcf | 2012-01-04 10:25:30 +0100 | [diff] [blame] | 4997 | r = -ENOTTY; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4998 | } |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4999 | |
| 5000 | vcpu_put(vcpu); |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 5001 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5002 | } |
| 5003 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 5004 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 5005 | { |
| 5006 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 5007 | if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) |
| 5008 | && (kvm_is_ucontrol(vcpu->kvm))) { |
| 5009 | vmf->page = virt_to_page(vcpu->arch.sie_block); |
| 5010 | get_page(vmf->page); |
| 5011 | return 0; |
| 5012 | } |
| 5013 | #endif |
| 5014 | return VM_FAULT_SIGBUS; |
| 5015 | } |
| 5016 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5017 | /* Section: memory related */ |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5018 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 5019 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 5020 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 7b6195a | 2013-02-27 19:44:34 +0900 | [diff] [blame] | 5021 | enum kvm_mr_change change) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5022 | { |
Nick Wang | dd2887e | 2013-03-25 17:22:57 +0100 | [diff] [blame] | 5023 | /* A few sanity checks. We can have memory slots which have to be |
| 5024 | located/ended at a segment boundary (1MB). The memory in userland is |
| 5025 | ok to be fragmented into various different vmas. It is okay to mmap() |
| 5026 | and munmap() stuff in this slot after doing this call at any time */ |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5027 | |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 5028 | if (mem->userspace_addr & 0xffffful) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5029 | return -EINVAL; |
| 5030 | |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 5031 | if (mem->memory_size & 0xffffful) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5032 | return -EINVAL; |
| 5033 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 5034 | if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) |
| 5035 | return -EINVAL; |
| 5036 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 5037 | /* When we are protected, we should not change the memory slots */ |
| 5038 | if (kvm_s390_pv_get_handle(kvm)) |
| 5039 | return -EINVAL; |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 5044 | const struct kvm_userspace_memory_region *mem, |
Sean Christopherson | 9d4c197 | 2020-02-18 13:07:24 -0800 | [diff] [blame] | 5045 | struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 5046 | const struct kvm_memory_slot *new, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 5047 | enum kvm_mr_change change) |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5048 | { |
Christian Borntraeger | 19ec166 | 2019-05-24 16:06:23 +0200 | [diff] [blame] | 5049 | int rc = 0; |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5050 | |
Christian Borntraeger | 19ec166 | 2019-05-24 16:06:23 +0200 | [diff] [blame] | 5051 | switch (change) { |
| 5052 | case KVM_MR_DELETE: |
| 5053 | rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, |
| 5054 | old->npages * PAGE_SIZE); |
| 5055 | break; |
| 5056 | case KVM_MR_MOVE: |
| 5057 | rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, |
| 5058 | old->npages * PAGE_SIZE); |
| 5059 | if (rc) |
| 5060 | break; |
Joe Perches | 3b684a4 | 2020-03-10 21:51:32 -0700 | [diff] [blame] | 5061 | fallthrough; |
Christian Borntraeger | 19ec166 | 2019-05-24 16:06:23 +0200 | [diff] [blame] | 5062 | case KVM_MR_CREATE: |
| 5063 | rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, |
| 5064 | mem->guest_phys_addr, mem->memory_size); |
| 5065 | break; |
| 5066 | case KVM_MR_FLAGS_ONLY: |
| 5067 | break; |
| 5068 | default: |
| 5069 | WARN(1, "Unknown KVM MR CHANGE: %d\n", change); |
| 5070 | } |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 5071 | if (rc) |
David Hildenbrand | ea2cdd2 | 2015-05-20 13:24:02 +0200 | [diff] [blame] | 5072 | pr_warn("failed to commit memory region\n"); |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 5073 | return; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5074 | } |
| 5075 | |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 5076 | static inline unsigned long nonhyp_mask(int i) |
| 5077 | { |
| 5078 | unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; |
| 5079 | |
| 5080 | return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); |
| 5081 | } |
| 5082 | |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 5083 | void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) |
| 5084 | { |
| 5085 | vcpu->valid_wakeup = false; |
| 5086 | } |
| 5087 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5088 | static int __init kvm_s390_init(void) |
| 5089 | { |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 5090 | int i; |
| 5091 | |
David Hildenbrand | 07197fd | 2015-01-30 16:01:38 +0100 | [diff] [blame] | 5092 | if (!sclp.has_sief2) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 5093 | pr_info("SIE is not available\n"); |
David Hildenbrand | 07197fd | 2015-01-30 16:01:38 +0100 | [diff] [blame] | 5094 | return -ENODEV; |
| 5095 | } |
| 5096 | |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 5097 | if (nested && hpage) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 5098 | pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 5099 | return -EINVAL; |
| 5100 | } |
| 5101 | |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 5102 | for (i = 0; i < 16; i++) |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 5103 | kvm_s390_fac_base[i] |= |
Sven Schnelle | 17e89e1 | 2021-05-05 22:01:10 +0200 | [diff] [blame] | 5104 | stfle_fac_list[i] & nonhyp_mask(i); |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 5105 | |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 5106 | return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | static void __exit kvm_s390_exit(void) |
| 5110 | { |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 5111 | kvm_exit(); |
| 5112 | } |
| 5113 | |
| 5114 | module_init(kvm_s390_init); |
| 5115 | module_exit(kvm_s390_exit); |
Cornelia Huck | 566af94 | 2013-05-27 18:42:33 +0200 | [diff] [blame] | 5116 | |
| 5117 | /* |
| 5118 | * Enable autoloading of the kvm module. |
| 5119 | * Note that we add the module alias here instead of virt/kvm/kvm_main.c |
| 5120 | * since x86 takes a different approach. |
| 5121 | */ |
| 5122 | #include <linux/miscdevice.h> |
| 5123 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 5124 | MODULE_ALIAS("devname:kvm"); |