blob: 74c47d420253443c8c116e5f4fab182e37d3019f [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngieraa024c22013-01-20 18:28:13 -05002/*
3 * Copyright (C) 2012 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
Marc Zyngieraa024c22013-01-20 18:28:13 -05005 */
6
Marc Zyngier09e6be12018-02-06 17:56:12 +00007#include <linux/arm-smccc.h>
Christoffer Dallcf5d31882014-10-16 17:00:18 +02008#include <linux/preempt.h>
Marc Zyngieraa024c22013-01-20 18:28:13 -05009#include <linux/kvm_host.h>
Marc Zyngier85bd0ba2018-01-21 16:42:56 +000010#include <linux/uaccess.h>
Marc Zyngieraa024c22013-01-20 18:28:13 -050011#include <linux/wait.h>
12
Marc Zyngier79c64882013-10-18 18:19:03 +010013#include <asm/cputype.h>
Marc Zyngieraa024c22013-01-20 18:28:13 -050014#include <asm/kvm_emulate.h>
Marc Zyngieraa024c22013-01-20 18:28:13 -050015
Marc Zyngier1a2fb942018-02-06 17:56:08 +000016#include <kvm/arm_psci.h>
Christoffer Dall55009c62019-10-21 16:28:15 +010017#include <kvm/arm_hypercalls.h>
Marc Zyngier1a2fb942018-02-06 17:56:08 +000018
Marc Zyngieraa024c22013-01-20 18:28:13 -050019/*
20 * This is an implementation of the Power State Coordination Interface
21 * as described in ARM document number ARM DEN 0022A.
22 */
23
Anup Patele6bc13c82014-04-29 11:24:21 +053024#define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
25
26static 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 Patelb376d022014-04-29 11:24:24 +053034static 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 Jones6a6d73b2017-06-04 14:43:54 +020050 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Anup Patelb376d022014-04-29 11:24:24 +053051
52 return PSCI_RET_SUCCESS;
53}
54
Marc Zyngieraa024c22013-01-20 18:28:13 -050055static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
56{
Eric Auger37815282015-09-25 23:41:14 +020057 vcpu->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +020058 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones424c9892017-06-04 14:43:57 +020059 kvm_vcpu_kick(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -050060}
61
Oliver Uptone10ecb42021-08-18 20:21:32 +000062static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu,
63 unsigned long affinity)
64{
65 return !(affinity & ~MPIDR_HWID_BITMASK);
66}
67
Marc Zyngieraa024c22013-01-20 18:28:13 -050068static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
69{
Marc Zyngier358b28f2018-12-20 11:36:07 +000070 struct vcpu_reset_state *reset_state;
Marc Zyngieraa024c22013-01-20 18:28:13 -050071 struct kvm *kvm = source_vcpu->kvm;
Andre Przywara4429fc62014-06-02 15:37:13 +020072 struct kvm_vcpu *vcpu = NULL;
Marc Zyngieraa024c22013-01-20 18:28:13 -050073 unsigned long cpu_id;
Marc Zyngieraa024c22013-01-20 18:28:13 -050074
Oliver Uptone10ecb42021-08-18 20:21:32 +000075 cpu_id = smccc_get_arg1(source_vcpu);
76 if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
77 return PSCI_RET_INVALID_PARAMS;
Marc Zyngieraa024c22013-01-20 18:28:13 -050078
Andre Przywara4429fc62014-06-02 15:37:13 +020079 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
Marc Zyngier79c64882013-10-18 18:19:03 +010080
Christoffer Dall478a8232013-11-19 17:43:19 -080081 /*
82 * Make sure the caller requested a valid CPU and that the CPU is
83 * turned off.
84 */
Anup Patelaa8aeef2014-04-29 11:24:23 +053085 if (!vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +053086 return PSCI_RET_INVALID_PARAMS;
Eric Auger37815282015-09-25 23:41:14 +020087 if (!vcpu->arch.power_off) {
Marc Zyngiera4097b32018-02-06 17:56:13 +000088 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
Anup Patelaa8aeef2014-04-29 11:24:23 +053089 return PSCI_RET_ALREADY_ON;
90 else
91 return PSCI_RET_INVALID_PARAMS;
92 }
Marc Zyngieraa024c22013-01-20 18:28:13 -050093
Marc Zyngier358b28f2018-12-20 11:36:07 +000094 reset_state = &vcpu->arch.reset_state;
Marc Zyngieraa024c22013-01-20 18:28:13 -050095
Marc Zyngier358b28f2018-12-20 11:36:07 +000096 reset_state->pc = smccc_get_arg2(source_vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -050097
Marc Zyngierce94fe92013-11-05 14:12:15 +000098 /* Propagate caller endianness */
Marc Zyngier358b28f2018-12-20 11:36:07 +000099 reset_state->be = kvm_vcpu_is_be(source_vcpu);
Marc Zyngierce94fe92013-11-05 14:12:15 +0000100
Anup Patelaa8aeef2014-04-29 11:24:23 +0530101 /*
102 * NOTE: We always update r0 (or x0) because for PSCI v0.1
Fuad Tabba656012c2020-04-01 15:03:10 +0100103 * the general purpose registers are undefined upon CPU_ON.
Anup Patelaa8aeef2014-04-29 11:24:23 +0530104 */
Marc Zyngier358b28f2018-12-20 11:36:07 +0000105 reset_state->r0 = smccc_get_arg3(source_vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500106
Marc Zyngier358b28f2018-12-20 11:36:07 +0000107 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 Zyngieraa024c22013-01-20 18:28:13 -0500118
Anup Patel7d0f84a2014-04-29 11:24:16 +0530119 return PSCI_RET_SUCCESS;
120}
121
Anup Patele6bc13c82014-04-29 11:24:21 +0530122static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
123{
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200124 int i, matching_cpus = 0;
Anup Patele6bc13c82014-04-29 11:24:21 +0530125 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 Zyngier84684fe2018-02-06 17:56:10 +0000132 target_affinity = smccc_get_arg1(vcpu);
133 lowest_affinity_level = smccc_get_arg2(vcpu);
Anup Patele6bc13c82014-04-29 11:24:21 +0530134
Oliver Uptone10ecb42021-08-18 20:21:32 +0000135 if (!kvm_psci_valid_affinity(vcpu, target_affinity))
136 return PSCI_RET_INVALID_PARAMS;
137
Anup Patele6bc13c82014-04-29 11:24:21 +0530138 /* 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 Przywara4429fc62014-06-02 15:37:13 +0200151 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200152 if ((mpidr & target_affinity_mask) == target_affinity) {
153 matching_cpus++;
Eric Auger37815282015-09-25 23:41:14 +0200154 if (!tmp->arch.power_off)
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200155 return PSCI_0_2_AFFINITY_LEVEL_ON;
Anup Patele6bc13c82014-04-29 11:24:21 +0530156 }
157 }
158
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200159 if (!matching_cpus)
160 return PSCI_RET_INVALID_PARAMS;
161
Anup Patele6bc13c82014-04-29 11:24:21 +0530162 return PSCI_0_2_AFFINITY_LEVEL_OFF;
163}
164
Anup Patel4b123822014-04-29 11:24:20 +0530165static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
166{
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200167 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 Jonescc9b43f2017-06-04 14:43:56 +0200179 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
Eric Auger37815282015-09-25 23:41:14 +0200180 tmp->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200181 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200182
Anup Patel4b123822014-04-29 11:24:20 +0530183 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
188static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
189{
190 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
191}
192
193static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
194{
195 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
196}
197
Marc Zyngier2890ac92020-04-01 12:25:05 +0100198static 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 Zyngierfdc99992020-04-01 12:38:49 +0100210static 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 Patele8e7fcc2014-04-29 11:24:18 +0530225static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530226{
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200227 struct kvm *kvm = vcpu->kvm;
Marc Zyngier84684fe2018-02-06 17:56:10 +0000228 u32 psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530229 unsigned long val;
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200230 int ret = 1;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530231
Marc Zyngierfdc99992020-04-01 12:38:49 +0100232 val = kvm_psci_check_allowed_function(vcpu, psci_fn);
233 if (val)
234 goto out;
235
Anup Patel7d0f84a2014-04-29 11:24:16 +0530236 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 Zyngierd0a144f2018-02-06 17:56:09 +0000242 val = KVM_ARM_PSCI_0_2;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530243 break;
Anup Patelb376d022014-04-29 11:24:24 +0530244 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 Patel7d0f84a2014-04-29 11:24:16 +0530248 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 Zyngier2890ac92020-04-01 12:25:05 +0100253 kvm_psci_narrow_to_32bit(vcpu);
254 fallthrough;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530255 case PSCI_0_2_FN64_CPU_ON:
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200256 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530257 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200258 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530259 break;
Anup Patele6bc13c82014-04-29 11:24:21 +0530260 case PSCI_0_2_FN_AFFINITY_INFO:
Marc Zyngier2890ac92020-04-01 12:25:05 +0100261 kvm_psci_narrow_to_32bit(vcpu);
262 fallthrough;
Anup Patele6bc13c82014-04-29 11:24:21 +0530263 case PSCI_0_2_FN64_AFFINITY_INFO:
264 val = kvm_psci_vcpu_affinity_info(vcpu);
265 break;
Anup Patelbab0b4302014-04-29 11:24:22 +0530266 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 Patel4b123822014-04-29 11:24:20 +0530274 case PSCI_0_2_FN_SYSTEM_OFF:
275 kvm_psci_system_off(vcpu);
276 /*
Fuad Tabba656012c2020-04-01 15:03:10 +0100277 * We shouldn't be going back to guest VCPU after
Anup Patel4b123822014-04-29 11:24:20 +0530278 * receiving SYSTEM_OFF request.
279 *
Fuad Tabba656012c2020-04-01 15:03:10 +0100280 * If user space accidentally/deliberately resumes
Anup Patel4b123822014-04-29 11:24:20 +0530281 * 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 Patel7d0f84a2014-04-29 11:24:16 +0530298 default:
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100299 val = PSCI_RET_NOT_SUPPORTED;
300 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530301 }
302
Marc Zyngierfdc99992020-04-01 12:38:49 +0100303out:
Marc Zyngier84684fe2018-02-06 17:56:10 +0000304 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patel4b123822014-04-29 11:24:20 +0530305 return ret;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530306}
307
Marc Zyngier58e0b222018-02-06 17:56:11 +0000308static 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 Zyngierfdc99992020-04-01 12:38:49 +0100321 val = kvm_psci_check_allowed_function(vcpu, feature);
322 if (val)
323 break;
324
Marc Zyngier58e0b222018-02-06 17:56:11 +0000325 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 Zyngier09e6be12018-02-06 17:56:12 +0000338 case ARM_SMCCC_VERSION_FUNC_ID:
Marc Zyngier58e0b222018-02-06 17:56:11 +0000339 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 Patele8e7fcc2014-04-29 11:24:18 +0530354static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530355{
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200356 struct kvm *kvm = vcpu->kvm;
Marc Zyngier84684fe2018-02-06 17:56:10 +0000357 u32 psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530358 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 Jones6c7a5dc2017-04-18 17:59:58 +0200366 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530367 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200368 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530369 break;
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100370 default:
Anup Patel7d0f84a2014-04-29 11:24:16 +0530371 val = PSCI_RET_NOT_SUPPORTED;
372 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530373 }
374
Marc Zyngier84684fe2018-02-06 17:56:10 +0000375 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patele8e7fcc2014-04-29 11:24:18 +0530376 return 1;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500377}
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 Martin24a7f672013-05-01 17:49:28 +0100383 * Handle PSCI calls from guests through traps from HVC instructions.
Anup Patele8e7fcc2014-04-29 11:24:18 +0530384 * 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 Zyngieraa024c22013-01-20 18:28:13 -0500392 */
Christoffer Dall55009c62019-10-21 16:28:15 +0100393int kvm_psci_call(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500394{
Marc Zyngiera4097b32018-02-06 17:56:13 +0000395 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
Marc Zyngier58e0b222018-02-06 17:56:11 +0000396 case KVM_ARM_PSCI_1_0:
397 return kvm_psci_1_0_call(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530398 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 Zyngieraa024c22013-01-20 18:28:13 -0500402 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530403 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530404 };
Marc Zyngieraa024c22013-01-20 18:28:13 -0500405}
Marc Zyngier09e6be12018-02-06 17:56:12 +0000406
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000407int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
408{
Andre Przywara99adb5672019-05-03 15:27:49 +0100409 return 3; /* PSCI version and two workaround registers */
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000410}
411
412int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
413{
Andre Przywara99adb5672019-05-03 15:27:49 +0100414 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 Zyngier85bd0ba2018-01-21 16:42:56 +0000421 return -EFAULT;
422
423 return 0;
424}
425
Andre Przywara99adb5672019-05-03 15:27:49 +0100426#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 */
433static int get_kernel_wa_level(u64 regid)
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000434{
Andre Przywara99adb5672019-05-03 15:27:49 +0100435 switch (regid) {
436 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
Will Deacond4647f02020-09-15 23:30:17 +0100437 switch (arm64_get_spectre_v2_state()) {
438 case SPECTRE_VULNERABLE:
Andre Przywara99adb5672019-05-03 15:27:49 +0100439 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
Will Deacond4647f02020-09-15 23:30:17 +0100440 case SPECTRE_MITIGATED:
Andre Przywara99adb5672019-05-03 15:27:49 +0100441 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
Will Deacond4647f02020-09-15 23:30:17 +0100442 case SPECTRE_UNAFFECTED:
Andre Przywara99adb5672019-05-03 15:27:49 +0100443 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 Zyngierd63d9752020-09-18 14:08:54 +0100447 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 Przywara99adb5672019-05-03 15:27:49 +0100458 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
Marc Zyngierd63d9752020-09-18 14:08:54 +0100459 case SPECTRE_VULNERABLE:
Marc Zyngier29e89102020-09-18 12:25:40 +0100460 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
Andre Przywara99adb5672019-05-03 15:27:49 +0100461 }
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000462 }
463
464 return -EINVAL;
465}
466
Andre Przywara99adb5672019-05-03 15:27:49 +0100467int 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 Przywara99adb5672019-05-03 15:27:49 +0100477 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
478 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
Andre Przywara99adb5672019-05-03 15:27:49 +0100479 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 Zyngier85bd0ba2018-01-21 16:42:56 +0000490int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
491{
Andre Przywara99adb5672019-05-03 15:27:49 +0100492 void __user *uaddr = (void __user *)(long)reg->addr;
493 u64 val;
494 int wa_level;
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000495
Andre Przywara99adb5672019-05-03 15:27:49 +0100496 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 Zyngier85bd0ba2018-01-21 16:42:56 +0000503
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 Przywara99adb5672019-05-03 15:27:49 +0100519 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 Przywara99adb5672019-05-03 15:27:49 +0100536 /* The enabled bit must not be set unless the level is AVAIL. */
Marc Zyngier29e89102020-09-18 12:25:40 +0100537 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 Przywara99adb5672019-05-03 15:27:49 +0100539 return -EINVAL;
540
Andre Przywara99adb5672019-05-03 15:27:49 +0100541 /*
Marc Zyngier29e89102020-09-18 12:25:40 +0100542 * Map all the possible incoming states to the only two we
543 * really want to deal with.
Andre Przywara99adb5672019-05-03 15:27:49 +0100544 */
Marc Zyngier29e89102020-09-18 12:25:40 +0100545 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 Przywara99adb5672019-05-03 15:27:49 +0100550 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
Andre Przywara99adb5672019-05-03 15:27:49 +0100551 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
Marc Zyngier29e89102020-09-18 12:25:40 +0100552 wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
Andre Przywara99adb5672019-05-03 15:27:49 +0100553 break;
Marc Zyngier29e89102020-09-18 12:25:40 +0100554 default:
555 return -EINVAL;
Andre Przywara99adb5672019-05-03 15:27:49 +0100556 }
557
Marc Zyngier29e89102020-09-18 12:25:40 +0100558 /*
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 Przywara99adb5672019-05-03 15:27:49 +0100565 return 0;
566 default:
567 return -ENOENT;
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000568 }
569
570 return -EINVAL;
571}