Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 - ARM Ltd |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Marc Zyngier | 09e6be1 | 2018-02-06 17:56:12 +0000 | [diff] [blame] | 7 | #include <linux/arm-smccc.h> |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 8 | #include <linux/preempt.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 9 | #include <linux/kvm_host.h> |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 10 | #include <linux/uaccess.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 11 | #include <linux/wait.h> |
| 12 | |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 13 | #include <asm/cputype.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 14 | #include <asm/kvm_emulate.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 15 | |
Marc Zyngier | 1a2fb94 | 2018-02-06 17:56:08 +0000 | [diff] [blame] | 16 | #include <kvm/arm_psci.h> |
Christoffer Dall | 55009c6 | 2019-10-21 16:28:15 +0100 | [diff] [blame] | 17 | #include <kvm/arm_hypercalls.h> |
Marc Zyngier | 1a2fb94 | 2018-02-06 17:56:08 +0000 | [diff] [blame] | 18 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 19 | /* |
| 20 | * This is an implementation of the Power State Coordination Interface |
| 21 | * as described in ARM document number ARM DEN 0022A. |
| 22 | */ |
| 23 | |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 24 | #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1) |
| 25 | |
| 26 | static unsigned long psci_affinity_mask(unsigned long affinity_level) |
| 27 | { |
| 28 | if (affinity_level <= 3) |
| 29 | return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Anup Patel | b376d02 | 2014-04-29 11:24:24 +0530 | [diff] [blame] | 34 | static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu) |
| 35 | { |
| 36 | /* |
| 37 | * NOTE: For simplicity, we make VCPU suspend emulation to be |
| 38 | * same-as WFI (Wait-for-interrupt) emulation. |
| 39 | * |
| 40 | * This means for KVM the wakeup events are interrupts and |
| 41 | * this is consistent with intended use of StateID as described |
| 42 | * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A). |
| 43 | * |
| 44 | * Further, we also treat power-down request to be same as |
| 45 | * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2 |
| 46 | * specification (ARM DEN 0022A). This means all suspend states |
| 47 | * for KVM will preserve the register state. |
| 48 | */ |
| 49 | kvm_vcpu_block(vcpu); |
Andrew Jones | 6a6d73b | 2017-06-04 14:43:54 +0200 | [diff] [blame] | 50 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
Anup Patel | b376d02 | 2014-04-29 11:24:24 +0530 | [diff] [blame] | 51 | |
| 52 | return PSCI_RET_SUCCESS; |
| 53 | } |
| 54 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 55 | static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) |
| 56 | { |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 57 | vcpu->arch.power_off = true; |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 58 | kvm_make_request(KVM_REQ_SLEEP, vcpu); |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 59 | kvm_vcpu_kick(vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Oliver Upton | e10ecb4 | 2021-08-18 20:21:32 +0000 | [diff] [blame] | 62 | static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu, |
| 63 | unsigned long affinity) |
| 64 | { |
| 65 | return !(affinity & ~MPIDR_HWID_BITMASK); |
| 66 | } |
| 67 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 68 | static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) |
| 69 | { |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 70 | struct vcpu_reset_state *reset_state; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 71 | struct kvm *kvm = source_vcpu->kvm; |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 72 | struct kvm_vcpu *vcpu = NULL; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 73 | unsigned long cpu_id; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 74 | |
Oliver Upton | e10ecb4 | 2021-08-18 20:21:32 +0000 | [diff] [blame] | 75 | cpu_id = smccc_get_arg1(source_vcpu); |
| 76 | if (!kvm_psci_valid_affinity(source_vcpu, cpu_id)) |
| 77 | return PSCI_RET_INVALID_PARAMS; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 78 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 79 | vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id); |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 80 | |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Make sure the caller requested a valid CPU and that the CPU is |
| 83 | * turned off. |
| 84 | */ |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 85 | if (!vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 86 | return PSCI_RET_INVALID_PARAMS; |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 87 | if (!vcpu->arch.power_off) { |
Marc Zyngier | a4097b3 | 2018-02-06 17:56:13 +0000 | [diff] [blame] | 88 | if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1) |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 89 | return PSCI_RET_ALREADY_ON; |
| 90 | else |
| 91 | return PSCI_RET_INVALID_PARAMS; |
| 92 | } |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 93 | |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 94 | reset_state = &vcpu->arch.reset_state; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 95 | |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 96 | reset_state->pc = smccc_get_arg2(source_vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 97 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 98 | /* Propagate caller endianness */ |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 99 | reset_state->be = kvm_vcpu_is_be(source_vcpu); |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 100 | |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 101 | /* |
| 102 | * NOTE: We always update r0 (or x0) because for PSCI v0.1 |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 103 | * the general purpose registers are undefined upon CPU_ON. |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 104 | */ |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 105 | reset_state->r0 = smccc_get_arg3(source_vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 106 | |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 107 | WRITE_ONCE(reset_state->reset, true); |
| 108 | kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); |
| 109 | |
| 110 | /* |
| 111 | * Make sure the reset request is observed if the change to |
| 112 | * power_state is observed. |
| 113 | */ |
| 114 | smp_wmb(); |
| 115 | |
| 116 | vcpu->arch.power_off = false; |
| 117 | kvm_vcpu_wake_up(vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 118 | |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 119 | return PSCI_RET_SUCCESS; |
| 120 | } |
| 121 | |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 122 | static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu) |
| 123 | { |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 124 | int i, matching_cpus = 0; |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 125 | unsigned long mpidr; |
| 126 | unsigned long target_affinity; |
| 127 | unsigned long target_affinity_mask; |
| 128 | unsigned long lowest_affinity_level; |
| 129 | struct kvm *kvm = vcpu->kvm; |
| 130 | struct kvm_vcpu *tmp; |
| 131 | |
Marc Zyngier | 84684fe | 2018-02-06 17:56:10 +0000 | [diff] [blame] | 132 | target_affinity = smccc_get_arg1(vcpu); |
| 133 | lowest_affinity_level = smccc_get_arg2(vcpu); |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 134 | |
Oliver Upton | e10ecb4 | 2021-08-18 20:21:32 +0000 | [diff] [blame] | 135 | if (!kvm_psci_valid_affinity(vcpu, target_affinity)) |
| 136 | return PSCI_RET_INVALID_PARAMS; |
| 137 | |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 138 | /* Determine target affinity mask */ |
| 139 | target_affinity_mask = psci_affinity_mask(lowest_affinity_level); |
| 140 | if (!target_affinity_mask) |
| 141 | return PSCI_RET_INVALID_PARAMS; |
| 142 | |
| 143 | /* Ignore other bits of target affinity */ |
| 144 | target_affinity &= target_affinity_mask; |
| 145 | |
| 146 | /* |
| 147 | * If one or more VCPU matching target affinity are running |
| 148 | * then ON else OFF |
| 149 | */ |
| 150 | kvm_for_each_vcpu(i, tmp, kvm) { |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 151 | mpidr = kvm_vcpu_get_mpidr_aff(tmp); |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 152 | if ((mpidr & target_affinity_mask) == target_affinity) { |
| 153 | matching_cpus++; |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 154 | if (!tmp->arch.power_off) |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 155 | return PSCI_0_2_AFFINITY_LEVEL_ON; |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 159 | if (!matching_cpus) |
| 160 | return PSCI_RET_INVALID_PARAMS; |
| 161 | |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 162 | return PSCI_0_2_AFFINITY_LEVEL_OFF; |
| 163 | } |
| 164 | |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 165 | static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type) |
| 166 | { |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 167 | int i; |
| 168 | struct kvm_vcpu *tmp; |
| 169 | |
| 170 | /* |
| 171 | * The KVM ABI specifies that a system event exit may call KVM_RUN |
| 172 | * again and may perform shutdown/reboot at a later time that when the |
| 173 | * actual request is made. Since we are implementing PSCI and a |
| 174 | * caller of PSCI reboot and shutdown expects that the system shuts |
| 175 | * down or reboots immediately, let's make sure that VCPUs are not run |
| 176 | * after this call is handled and before the VCPUs have been |
| 177 | * re-initialized. |
| 178 | */ |
Andrew Jones | cc9b43f | 2017-06-04 14:43:56 +0200 | [diff] [blame] | 179 | kvm_for_each_vcpu(i, tmp, vcpu->kvm) |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 180 | tmp->arch.power_off = true; |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 181 | kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 182 | |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 183 | memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); |
| 184 | vcpu->run->system_event.type = type; |
| 185 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 186 | } |
| 187 | |
| 188 | static void kvm_psci_system_off(struct kvm_vcpu *vcpu) |
| 189 | { |
| 190 | kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN); |
| 191 | } |
| 192 | |
| 193 | static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) |
| 194 | { |
| 195 | kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET); |
| 196 | } |
| 197 | |
Marc Zyngier | 2890ac9 | 2020-04-01 12:25:05 +0100 | [diff] [blame] | 198 | static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu) |
| 199 | { |
| 200 | int i; |
| 201 | |
| 202 | /* |
| 203 | * Zero the input registers' upper 32 bits. They will be fully |
| 204 | * zeroed on exit, so we're fine changing them in place. |
| 205 | */ |
| 206 | for (i = 1; i < 4; i++) |
| 207 | vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i))); |
| 208 | } |
| 209 | |
Marc Zyngier | fdc9999 | 2020-04-01 12:38:49 +0100 | [diff] [blame] | 210 | static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn) |
| 211 | { |
| 212 | switch(fn) { |
| 213 | case PSCI_0_2_FN64_CPU_SUSPEND: |
| 214 | case PSCI_0_2_FN64_CPU_ON: |
| 215 | case PSCI_0_2_FN64_AFFINITY_INFO: |
| 216 | /* Disallow these functions for 32bit guests */ |
| 217 | if (vcpu_mode_is_32bit(vcpu)) |
| 218 | return PSCI_RET_NOT_SUPPORTED; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 225 | static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 226 | { |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 227 | struct kvm *kvm = vcpu->kvm; |
Marc Zyngier | 84684fe | 2018-02-06 17:56:10 +0000 | [diff] [blame] | 228 | u32 psci_fn = smccc_get_function(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 229 | unsigned long val; |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 230 | int ret = 1; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 231 | |
Marc Zyngier | fdc9999 | 2020-04-01 12:38:49 +0100 | [diff] [blame] | 232 | val = kvm_psci_check_allowed_function(vcpu, psci_fn); |
| 233 | if (val) |
| 234 | goto out; |
| 235 | |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 236 | switch (psci_fn) { |
| 237 | case PSCI_0_2_FN_PSCI_VERSION: |
| 238 | /* |
| 239 | * Bits[31:16] = Major Version = 0 |
| 240 | * Bits[15:0] = Minor Version = 2 |
| 241 | */ |
Marc Zyngier | d0a144f | 2018-02-06 17:56:09 +0000 | [diff] [blame] | 242 | val = KVM_ARM_PSCI_0_2; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 243 | break; |
Anup Patel | b376d02 | 2014-04-29 11:24:24 +0530 | [diff] [blame] | 244 | case PSCI_0_2_FN_CPU_SUSPEND: |
| 245 | case PSCI_0_2_FN64_CPU_SUSPEND: |
| 246 | val = kvm_psci_vcpu_suspend(vcpu); |
| 247 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 248 | case PSCI_0_2_FN_CPU_OFF: |
| 249 | kvm_psci_vcpu_off(vcpu); |
| 250 | val = PSCI_RET_SUCCESS; |
| 251 | break; |
| 252 | case PSCI_0_2_FN_CPU_ON: |
Marc Zyngier | 2890ac9 | 2020-04-01 12:25:05 +0100 | [diff] [blame] | 253 | kvm_psci_narrow_to_32bit(vcpu); |
| 254 | fallthrough; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 255 | case PSCI_0_2_FN64_CPU_ON: |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 256 | mutex_lock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 257 | val = kvm_psci_vcpu_on(vcpu); |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 258 | mutex_unlock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 259 | break; |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 260 | case PSCI_0_2_FN_AFFINITY_INFO: |
Marc Zyngier | 2890ac9 | 2020-04-01 12:25:05 +0100 | [diff] [blame] | 261 | kvm_psci_narrow_to_32bit(vcpu); |
| 262 | fallthrough; |
Anup Patel | e6bc13c8 | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 263 | case PSCI_0_2_FN64_AFFINITY_INFO: |
| 264 | val = kvm_psci_vcpu_affinity_info(vcpu); |
| 265 | break; |
Anup Patel | bab0b430 | 2014-04-29 11:24:22 +0530 | [diff] [blame] | 266 | case PSCI_0_2_FN_MIGRATE_INFO_TYPE: |
| 267 | /* |
| 268 | * Trusted OS is MP hence does not require migration |
| 269 | * or |
| 270 | * Trusted OS is not present |
| 271 | */ |
| 272 | val = PSCI_0_2_TOS_MP; |
| 273 | break; |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 274 | case PSCI_0_2_FN_SYSTEM_OFF: |
| 275 | kvm_psci_system_off(vcpu); |
| 276 | /* |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 277 | * We shouldn't be going back to guest VCPU after |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 278 | * receiving SYSTEM_OFF request. |
| 279 | * |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 280 | * If user space accidentally/deliberately resumes |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 281 | * guest VCPU after SYSTEM_OFF request then guest |
| 282 | * VCPU should see internal failure from PSCI return |
| 283 | * value. To achieve this, we preload r0 (or x0) with |
| 284 | * PSCI return value INTERNAL_FAILURE. |
| 285 | */ |
| 286 | val = PSCI_RET_INTERNAL_FAILURE; |
| 287 | ret = 0; |
| 288 | break; |
| 289 | case PSCI_0_2_FN_SYSTEM_RESET: |
| 290 | kvm_psci_system_reset(vcpu); |
| 291 | /* |
| 292 | * Same reason as SYSTEM_OFF for preloading r0 (or x0) |
| 293 | * with PSCI return value INTERNAL_FAILURE. |
| 294 | */ |
| 295 | val = PSCI_RET_INTERNAL_FAILURE; |
| 296 | ret = 0; |
| 297 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 298 | default: |
Lorenzo Pieralisi | e2d9973 | 2015-06-10 15:19:24 +0100 | [diff] [blame] | 299 | val = PSCI_RET_NOT_SUPPORTED; |
| 300 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 301 | } |
| 302 | |
Marc Zyngier | fdc9999 | 2020-04-01 12:38:49 +0100 | [diff] [blame] | 303 | out: |
Marc Zyngier | 84684fe | 2018-02-06 17:56:10 +0000 | [diff] [blame] | 304 | smccc_set_retval(vcpu, val, 0, 0, 0); |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 305 | return ret; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 306 | } |
| 307 | |
Marc Zyngier | 58e0b22 | 2018-02-06 17:56:11 +0000 | [diff] [blame] | 308 | static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) |
| 309 | { |
| 310 | u32 psci_fn = smccc_get_function(vcpu); |
| 311 | u32 feature; |
| 312 | unsigned long val; |
| 313 | int ret = 1; |
| 314 | |
| 315 | switch(psci_fn) { |
| 316 | case PSCI_0_2_FN_PSCI_VERSION: |
| 317 | val = KVM_ARM_PSCI_1_0; |
| 318 | break; |
| 319 | case PSCI_1_0_FN_PSCI_FEATURES: |
| 320 | feature = smccc_get_arg1(vcpu); |
Marc Zyngier | fdc9999 | 2020-04-01 12:38:49 +0100 | [diff] [blame] | 321 | val = kvm_psci_check_allowed_function(vcpu, feature); |
| 322 | if (val) |
| 323 | break; |
| 324 | |
Marc Zyngier | 58e0b22 | 2018-02-06 17:56:11 +0000 | [diff] [blame] | 325 | switch(feature) { |
| 326 | case PSCI_0_2_FN_PSCI_VERSION: |
| 327 | case PSCI_0_2_FN_CPU_SUSPEND: |
| 328 | case PSCI_0_2_FN64_CPU_SUSPEND: |
| 329 | case PSCI_0_2_FN_CPU_OFF: |
| 330 | case PSCI_0_2_FN_CPU_ON: |
| 331 | case PSCI_0_2_FN64_CPU_ON: |
| 332 | case PSCI_0_2_FN_AFFINITY_INFO: |
| 333 | case PSCI_0_2_FN64_AFFINITY_INFO: |
| 334 | case PSCI_0_2_FN_MIGRATE_INFO_TYPE: |
| 335 | case PSCI_0_2_FN_SYSTEM_OFF: |
| 336 | case PSCI_0_2_FN_SYSTEM_RESET: |
| 337 | case PSCI_1_0_FN_PSCI_FEATURES: |
Marc Zyngier | 09e6be1 | 2018-02-06 17:56:12 +0000 | [diff] [blame] | 338 | case ARM_SMCCC_VERSION_FUNC_ID: |
Marc Zyngier | 58e0b22 | 2018-02-06 17:56:11 +0000 | [diff] [blame] | 339 | val = 0; |
| 340 | break; |
| 341 | default: |
| 342 | val = PSCI_RET_NOT_SUPPORTED; |
| 343 | break; |
| 344 | } |
| 345 | break; |
| 346 | default: |
| 347 | return kvm_psci_0_2_call(vcpu); |
| 348 | } |
| 349 | |
| 350 | smccc_set_retval(vcpu, val, 0, 0, 0); |
| 351 | return ret; |
| 352 | } |
| 353 | |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 354 | static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 355 | { |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 356 | struct kvm *kvm = vcpu->kvm; |
Marc Zyngier | 84684fe | 2018-02-06 17:56:10 +0000 | [diff] [blame] | 357 | u32 psci_fn = smccc_get_function(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 358 | unsigned long val; |
| 359 | |
| 360 | switch (psci_fn) { |
| 361 | case KVM_PSCI_FN_CPU_OFF: |
| 362 | kvm_psci_vcpu_off(vcpu); |
| 363 | val = PSCI_RET_SUCCESS; |
| 364 | break; |
| 365 | case KVM_PSCI_FN_CPU_ON: |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 366 | mutex_lock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 367 | val = kvm_psci_vcpu_on(vcpu); |
Andrew Jones | 6c7a5dc | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 368 | mutex_unlock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 369 | break; |
Lorenzo Pieralisi | e2d9973 | 2015-06-10 15:19:24 +0100 | [diff] [blame] | 370 | default: |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 371 | val = PSCI_RET_NOT_SUPPORTED; |
| 372 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 373 | } |
| 374 | |
Marc Zyngier | 84684fe | 2018-02-06 17:56:10 +0000 | [diff] [blame] | 375 | smccc_set_retval(vcpu, val, 0, 0, 0); |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 376 | return 1; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * kvm_psci_call - handle PSCI call if r0 value is in range |
| 381 | * @vcpu: Pointer to the VCPU struct |
| 382 | * |
Dave P Martin | 24a7f67 | 2013-05-01 17:49:28 +0100 | [diff] [blame] | 383 | * Handle PSCI calls from guests through traps from HVC instructions. |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 384 | * The calling convention is similar to SMC calls to the secure world |
| 385 | * where the function number is placed in r0. |
| 386 | * |
| 387 | * This function returns: > 0 (success), 0 (success but exit to user |
| 388 | * space), and < 0 (errors) |
| 389 | * |
| 390 | * Errors: |
| 391 | * -EINVAL: Unrecognized PSCI function |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 392 | */ |
Christoffer Dall | 55009c6 | 2019-10-21 16:28:15 +0100 | [diff] [blame] | 393 | int kvm_psci_call(struct kvm_vcpu *vcpu) |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 394 | { |
Marc Zyngier | a4097b3 | 2018-02-06 17:56:13 +0000 | [diff] [blame] | 395 | switch (kvm_psci_version(vcpu, vcpu->kvm)) { |
Marc Zyngier | 58e0b22 | 2018-02-06 17:56:11 +0000 | [diff] [blame] | 396 | case KVM_ARM_PSCI_1_0: |
| 397 | return kvm_psci_1_0_call(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 398 | case KVM_ARM_PSCI_0_2: |
| 399 | return kvm_psci_0_2_call(vcpu); |
| 400 | case KVM_ARM_PSCI_0_1: |
| 401 | return kvm_psci_0_1_call(vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 402 | default: |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 403 | return -EINVAL; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 404 | }; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 405 | } |
Marc Zyngier | 09e6be1 | 2018-02-06 17:56:12 +0000 | [diff] [blame] | 406 | |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 407 | int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) |
| 408 | { |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 409 | return 3; /* PSCI version and two workaround registers */ |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) |
| 413 | { |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 414 | if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++)) |
| 415 | return -EFAULT; |
| 416 | |
| 417 | if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++)) |
| 418 | return -EFAULT; |
| 419 | |
| 420 | if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++)) |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 421 | return -EFAULT; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 426 | #define KVM_REG_FEATURE_LEVEL_WIDTH 4 |
| 427 | #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) |
| 428 | |
| 429 | /* |
| 430 | * Convert the workaround level into an easy-to-compare number, where higher |
| 431 | * values mean better protection. |
| 432 | */ |
| 433 | static int get_kernel_wa_level(u64 regid) |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 434 | { |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 435 | switch (regid) { |
| 436 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: |
Will Deacon | d4647f0 | 2020-09-15 23:30:17 +0100 | [diff] [blame] | 437 | switch (arm64_get_spectre_v2_state()) { |
| 438 | case SPECTRE_VULNERABLE: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 439 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; |
Will Deacon | d4647f0 | 2020-09-15 23:30:17 +0100 | [diff] [blame] | 440 | case SPECTRE_MITIGATED: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 441 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL; |
Will Deacon | d4647f0 | 2020-09-15 23:30:17 +0100 | [diff] [blame] | 442 | case SPECTRE_UNAFFECTED: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 443 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED; |
| 444 | } |
| 445 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; |
| 446 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: |
Marc Zyngier | d63d975 | 2020-09-18 14:08:54 +0100 | [diff] [blame] | 447 | switch (arm64_get_spectre_v4_state()) { |
| 448 | case SPECTRE_MITIGATED: |
| 449 | /* |
| 450 | * As for the hypercall discovery, we pretend we |
| 451 | * don't have any FW mitigation if SSBS is there at |
| 452 | * all times. |
| 453 | */ |
| 454 | if (cpus_have_final_cap(ARM64_SSBS)) |
| 455 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; |
| 456 | fallthrough; |
| 457 | case SPECTRE_UNAFFECTED: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 458 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; |
Marc Zyngier | d63d975 | 2020-09-18 14:08:54 +0100 | [diff] [blame] | 459 | case SPECTRE_VULNERABLE: |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 460 | return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 461 | } |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 467 | int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) |
| 468 | { |
| 469 | void __user *uaddr = (void __user *)(long)reg->addr; |
| 470 | u64 val; |
| 471 | |
| 472 | switch (reg->id) { |
| 473 | case KVM_REG_ARM_PSCI_VERSION: |
| 474 | val = kvm_psci_version(vcpu, vcpu->kvm); |
| 475 | break; |
| 476 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 477 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: |
| 478 | val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | return -ENOENT; |
| 482 | } |
| 483 | |
| 484 | if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) |
| 485 | return -EFAULT; |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 490 | int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) |
| 491 | { |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 492 | void __user *uaddr = (void __user *)(long)reg->addr; |
| 493 | u64 val; |
| 494 | int wa_level; |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 495 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 496 | if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) |
| 497 | return -EFAULT; |
| 498 | |
| 499 | switch (reg->id) { |
| 500 | case KVM_REG_ARM_PSCI_VERSION: |
| 501 | { |
| 502 | bool wants_02; |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 503 | |
| 504 | wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features); |
| 505 | |
| 506 | switch (val) { |
| 507 | case KVM_ARM_PSCI_0_1: |
| 508 | if (wants_02) |
| 509 | return -EINVAL; |
| 510 | vcpu->kvm->arch.psci_version = val; |
| 511 | return 0; |
| 512 | case KVM_ARM_PSCI_0_2: |
| 513 | case KVM_ARM_PSCI_1_0: |
| 514 | if (!wants_02) |
| 515 | return -EINVAL; |
| 516 | vcpu->kvm->arch.psci_version = val; |
| 517 | return 0; |
| 518 | } |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | |
| 522 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: |
| 523 | if (val & ~KVM_REG_FEATURE_LEVEL_MASK) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | if (get_kernel_wa_level(reg->id) < val) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | return 0; |
| 530 | |
| 531 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: |
| 532 | if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | |
| 533 | KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED)) |
| 534 | return -EINVAL; |
| 535 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 536 | /* The enabled bit must not be set unless the level is AVAIL. */ |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 537 | if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) && |
| 538 | (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL) |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 541 | /* |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 542 | * Map all the possible incoming states to the only two we |
| 543 | * really want to deal with. |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 544 | */ |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 545 | switch (val & KVM_REG_FEATURE_LEVEL_MASK) { |
| 546 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL: |
| 547 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN: |
| 548 | wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; |
| 549 | break; |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 550 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL: |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 551 | case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED: |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 552 | wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 553 | break; |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 554 | default: |
| 555 | return -EINVAL; |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Marc Zyngier | 29e8910 | 2020-09-18 12:25:40 +0100 | [diff] [blame] | 558 | /* |
| 559 | * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the |
| 560 | * other way around. |
| 561 | */ |
| 562 | if (get_kernel_wa_level(reg->id) < wa_level) |
| 563 | return -EINVAL; |
| 564 | |
Andre Przywara | 99adb567 | 2019-05-03 15:27:49 +0100 | [diff] [blame] | 565 | return 0; |
| 566 | default: |
| 567 | return -ENOENT; |
Marc Zyngier | 85bd0ba | 2018-01-21 16:42:56 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | return -EINVAL; |
| 571 | } |