blob: 83415e96b589fff983322b813ee5bf414c8bb1ce [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
62static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
63{
Marc Zyngier358b28f2018-12-20 11:36:07 +000064 struct vcpu_reset_state *reset_state;
Marc Zyngieraa024c22013-01-20 18:28:13 -050065 struct kvm *kvm = source_vcpu->kvm;
Andre Przywara4429fc62014-06-02 15:37:13 +020066 struct kvm_vcpu *vcpu = NULL;
Marc Zyngieraa024c22013-01-20 18:28:13 -050067 unsigned long cpu_id;
Marc Zyngieraa024c22013-01-20 18:28:13 -050068
Marc Zyngier84684fe2018-02-06 17:56:10 +000069 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
Marc Zyngieraa024c22013-01-20 18:28:13 -050070 if (vcpu_mode_is_32bit(source_vcpu))
71 cpu_id &= ~((u32) 0);
72
Andre Przywara4429fc62014-06-02 15:37:13 +020073 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
Marc Zyngier79c64882013-10-18 18:19:03 +010074
Christoffer Dall478a8232013-11-19 17:43:19 -080075 /*
76 * Make sure the caller requested a valid CPU and that the CPU is
77 * turned off.
78 */
Anup Patelaa8aeef2014-04-29 11:24:23 +053079 if (!vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +053080 return PSCI_RET_INVALID_PARAMS;
Eric Auger37815282015-09-25 23:41:14 +020081 if (!vcpu->arch.power_off) {
Marc Zyngiera4097b32018-02-06 17:56:13 +000082 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
Anup Patelaa8aeef2014-04-29 11:24:23 +053083 return PSCI_RET_ALREADY_ON;
84 else
85 return PSCI_RET_INVALID_PARAMS;
86 }
Marc Zyngieraa024c22013-01-20 18:28:13 -050087
Marc Zyngier358b28f2018-12-20 11:36:07 +000088 reset_state = &vcpu->arch.reset_state;
Marc Zyngieraa024c22013-01-20 18:28:13 -050089
Marc Zyngier358b28f2018-12-20 11:36:07 +000090 reset_state->pc = smccc_get_arg2(source_vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -050091
Marc Zyngierce94fe92013-11-05 14:12:15 +000092 /* Propagate caller endianness */
Marc Zyngier358b28f2018-12-20 11:36:07 +000093 reset_state->be = kvm_vcpu_is_be(source_vcpu);
Marc Zyngierce94fe92013-11-05 14:12:15 +000094
Anup Patelaa8aeef2014-04-29 11:24:23 +053095 /*
96 * NOTE: We always update r0 (or x0) because for PSCI v0.1
Fuad Tabba656012c2020-04-01 15:03:10 +010097 * the general purpose registers are undefined upon CPU_ON.
Anup Patelaa8aeef2014-04-29 11:24:23 +053098 */
Marc Zyngier358b28f2018-12-20 11:36:07 +000099 reset_state->r0 = smccc_get_arg3(source_vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500100
Marc Zyngier358b28f2018-12-20 11:36:07 +0000101 WRITE_ONCE(reset_state->reset, true);
102 kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
103
104 /*
105 * Make sure the reset request is observed if the change to
106 * power_state is observed.
107 */
108 smp_wmb();
109
110 vcpu->arch.power_off = false;
111 kvm_vcpu_wake_up(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500112
Anup Patel7d0f84a2014-04-29 11:24:16 +0530113 return PSCI_RET_SUCCESS;
114}
115
Anup Patele6bc13c82014-04-29 11:24:21 +0530116static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
117{
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200118 int i, matching_cpus = 0;
Anup Patele6bc13c82014-04-29 11:24:21 +0530119 unsigned long mpidr;
120 unsigned long target_affinity;
121 unsigned long target_affinity_mask;
122 unsigned long lowest_affinity_level;
123 struct kvm *kvm = vcpu->kvm;
124 struct kvm_vcpu *tmp;
125
Marc Zyngier84684fe2018-02-06 17:56:10 +0000126 target_affinity = smccc_get_arg1(vcpu);
127 lowest_affinity_level = smccc_get_arg2(vcpu);
Anup Patele6bc13c82014-04-29 11:24:21 +0530128
129 /* Determine target affinity mask */
130 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
131 if (!target_affinity_mask)
132 return PSCI_RET_INVALID_PARAMS;
133
134 /* Ignore other bits of target affinity */
135 target_affinity &= target_affinity_mask;
136
137 /*
138 * If one or more VCPU matching target affinity are running
139 * then ON else OFF
140 */
141 kvm_for_each_vcpu(i, tmp, kvm) {
Andre Przywara4429fc62014-06-02 15:37:13 +0200142 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200143 if ((mpidr & target_affinity_mask) == target_affinity) {
144 matching_cpus++;
Eric Auger37815282015-09-25 23:41:14 +0200145 if (!tmp->arch.power_off)
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200146 return PSCI_0_2_AFFINITY_LEVEL_ON;
Anup Patele6bc13c82014-04-29 11:24:21 +0530147 }
148 }
149
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200150 if (!matching_cpus)
151 return PSCI_RET_INVALID_PARAMS;
152
Anup Patele6bc13c82014-04-29 11:24:21 +0530153 return PSCI_0_2_AFFINITY_LEVEL_OFF;
154}
155
Anup Patel4b123822014-04-29 11:24:20 +0530156static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
157{
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200158 int i;
159 struct kvm_vcpu *tmp;
160
161 /*
162 * The KVM ABI specifies that a system event exit may call KVM_RUN
163 * again and may perform shutdown/reboot at a later time that when the
164 * actual request is made. Since we are implementing PSCI and a
165 * caller of PSCI reboot and shutdown expects that the system shuts
166 * down or reboots immediately, let's make sure that VCPUs are not run
167 * after this call is handled and before the VCPUs have been
168 * re-initialized.
169 */
Andrew Jonescc9b43f2017-06-04 14:43:56 +0200170 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
Eric Auger37815282015-09-25 23:41:14 +0200171 tmp->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200172 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200173
Anup Patel4b123822014-04-29 11:24:20 +0530174 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
175 vcpu->run->system_event.type = type;
176 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
177}
178
179static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
180{
181 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
182}
183
184static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
185{
186 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
187}
188
Marc Zyngier2890ac92020-04-01 12:25:05 +0100189static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
190{
191 int i;
192
193 /*
194 * Zero the input registers' upper 32 bits. They will be fully
195 * zeroed on exit, so we're fine changing them in place.
196 */
197 for (i = 1; i < 4; i++)
198 vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i)));
199}
200
Marc Zyngierfdc99992020-04-01 12:38:49 +0100201static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
202{
203 switch(fn) {
204 case PSCI_0_2_FN64_CPU_SUSPEND:
205 case PSCI_0_2_FN64_CPU_ON:
206 case PSCI_0_2_FN64_AFFINITY_INFO:
207 /* Disallow these functions for 32bit guests */
208 if (vcpu_mode_is_32bit(vcpu))
209 return PSCI_RET_NOT_SUPPORTED;
210 break;
211 }
212
213 return 0;
214}
215
Anup Patele8e7fcc2014-04-29 11:24:18 +0530216static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530217{
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200218 struct kvm *kvm = vcpu->kvm;
Marc Zyngier84684fe2018-02-06 17:56:10 +0000219 u32 psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530220 unsigned long val;
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200221 int ret = 1;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530222
Marc Zyngierfdc99992020-04-01 12:38:49 +0100223 val = kvm_psci_check_allowed_function(vcpu, psci_fn);
224 if (val)
225 goto out;
226
Anup Patel7d0f84a2014-04-29 11:24:16 +0530227 switch (psci_fn) {
228 case PSCI_0_2_FN_PSCI_VERSION:
229 /*
230 * Bits[31:16] = Major Version = 0
231 * Bits[15:0] = Minor Version = 2
232 */
Marc Zyngierd0a144f2018-02-06 17:56:09 +0000233 val = KVM_ARM_PSCI_0_2;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530234 break;
Anup Patelb376d022014-04-29 11:24:24 +0530235 case PSCI_0_2_FN_CPU_SUSPEND:
236 case PSCI_0_2_FN64_CPU_SUSPEND:
237 val = kvm_psci_vcpu_suspend(vcpu);
238 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530239 case PSCI_0_2_FN_CPU_OFF:
240 kvm_psci_vcpu_off(vcpu);
241 val = PSCI_RET_SUCCESS;
242 break;
243 case PSCI_0_2_FN_CPU_ON:
Marc Zyngier2890ac92020-04-01 12:25:05 +0100244 kvm_psci_narrow_to_32bit(vcpu);
245 fallthrough;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530246 case PSCI_0_2_FN64_CPU_ON:
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200247 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530248 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200249 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530250 break;
Anup Patele6bc13c82014-04-29 11:24:21 +0530251 case PSCI_0_2_FN_AFFINITY_INFO:
Marc Zyngier2890ac92020-04-01 12:25:05 +0100252 kvm_psci_narrow_to_32bit(vcpu);
253 fallthrough;
Anup Patele6bc13c82014-04-29 11:24:21 +0530254 case PSCI_0_2_FN64_AFFINITY_INFO:
255 val = kvm_psci_vcpu_affinity_info(vcpu);
256 break;
Anup Patelbab0b4302014-04-29 11:24:22 +0530257 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
258 /*
259 * Trusted OS is MP hence does not require migration
260 * or
261 * Trusted OS is not present
262 */
263 val = PSCI_0_2_TOS_MP;
264 break;
Anup Patel4b123822014-04-29 11:24:20 +0530265 case PSCI_0_2_FN_SYSTEM_OFF:
266 kvm_psci_system_off(vcpu);
267 /*
Fuad Tabba656012c2020-04-01 15:03:10 +0100268 * We shouldn't be going back to guest VCPU after
Anup Patel4b123822014-04-29 11:24:20 +0530269 * receiving SYSTEM_OFF request.
270 *
Fuad Tabba656012c2020-04-01 15:03:10 +0100271 * If user space accidentally/deliberately resumes
Anup Patel4b123822014-04-29 11:24:20 +0530272 * guest VCPU after SYSTEM_OFF request then guest
273 * VCPU should see internal failure from PSCI return
274 * value. To achieve this, we preload r0 (or x0) with
275 * PSCI return value INTERNAL_FAILURE.
276 */
277 val = PSCI_RET_INTERNAL_FAILURE;
278 ret = 0;
279 break;
280 case PSCI_0_2_FN_SYSTEM_RESET:
281 kvm_psci_system_reset(vcpu);
282 /*
283 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
284 * with PSCI return value INTERNAL_FAILURE.
285 */
286 val = PSCI_RET_INTERNAL_FAILURE;
287 ret = 0;
288 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530289 default:
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100290 val = PSCI_RET_NOT_SUPPORTED;
291 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530292 }
293
Marc Zyngierfdc99992020-04-01 12:38:49 +0100294out:
Marc Zyngier84684fe2018-02-06 17:56:10 +0000295 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patel4b123822014-04-29 11:24:20 +0530296 return ret;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530297}
298
Marc Zyngier58e0b222018-02-06 17:56:11 +0000299static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
300{
301 u32 psci_fn = smccc_get_function(vcpu);
302 u32 feature;
303 unsigned long val;
304 int ret = 1;
305
306 switch(psci_fn) {
307 case PSCI_0_2_FN_PSCI_VERSION:
308 val = KVM_ARM_PSCI_1_0;
309 break;
310 case PSCI_1_0_FN_PSCI_FEATURES:
311 feature = smccc_get_arg1(vcpu);
Marc Zyngierfdc99992020-04-01 12:38:49 +0100312 val = kvm_psci_check_allowed_function(vcpu, feature);
313 if (val)
314 break;
315
Marc Zyngier58e0b222018-02-06 17:56:11 +0000316 switch(feature) {
317 case PSCI_0_2_FN_PSCI_VERSION:
318 case PSCI_0_2_FN_CPU_SUSPEND:
319 case PSCI_0_2_FN64_CPU_SUSPEND:
320 case PSCI_0_2_FN_CPU_OFF:
321 case PSCI_0_2_FN_CPU_ON:
322 case PSCI_0_2_FN64_CPU_ON:
323 case PSCI_0_2_FN_AFFINITY_INFO:
324 case PSCI_0_2_FN64_AFFINITY_INFO:
325 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
326 case PSCI_0_2_FN_SYSTEM_OFF:
327 case PSCI_0_2_FN_SYSTEM_RESET:
328 case PSCI_1_0_FN_PSCI_FEATURES:
Marc Zyngier09e6be12018-02-06 17:56:12 +0000329 case ARM_SMCCC_VERSION_FUNC_ID:
Marc Zyngier58e0b222018-02-06 17:56:11 +0000330 val = 0;
331 break;
332 default:
333 val = PSCI_RET_NOT_SUPPORTED;
334 break;
335 }
336 break;
337 default:
338 return kvm_psci_0_2_call(vcpu);
339 }
340
341 smccc_set_retval(vcpu, val, 0, 0, 0);
342 return ret;
343}
344
Anup Patele8e7fcc2014-04-29 11:24:18 +0530345static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530346{
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200347 struct kvm *kvm = vcpu->kvm;
Marc Zyngier84684fe2018-02-06 17:56:10 +0000348 u32 psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530349 unsigned long val;
350
351 switch (psci_fn) {
352 case KVM_PSCI_FN_CPU_OFF:
353 kvm_psci_vcpu_off(vcpu);
354 val = PSCI_RET_SUCCESS;
355 break;
356 case KVM_PSCI_FN_CPU_ON:
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200357 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530358 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones6c7a5dc2017-04-18 17:59:58 +0200359 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530360 break;
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100361 default:
Anup Patel7d0f84a2014-04-29 11:24:16 +0530362 val = PSCI_RET_NOT_SUPPORTED;
363 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530364 }
365
Marc Zyngier84684fe2018-02-06 17:56:10 +0000366 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patele8e7fcc2014-04-29 11:24:18 +0530367 return 1;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500368}
369
370/**
371 * kvm_psci_call - handle PSCI call if r0 value is in range
372 * @vcpu: Pointer to the VCPU struct
373 *
Dave P Martin24a7f672013-05-01 17:49:28 +0100374 * Handle PSCI calls from guests through traps from HVC instructions.
Anup Patele8e7fcc2014-04-29 11:24:18 +0530375 * The calling convention is similar to SMC calls to the secure world
376 * where the function number is placed in r0.
377 *
378 * This function returns: > 0 (success), 0 (success but exit to user
379 * space), and < 0 (errors)
380 *
381 * Errors:
382 * -EINVAL: Unrecognized PSCI function
Marc Zyngieraa024c22013-01-20 18:28:13 -0500383 */
Christoffer Dall55009c62019-10-21 16:28:15 +0100384int kvm_psci_call(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500385{
Marc Zyngiera4097b32018-02-06 17:56:13 +0000386 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
Marc Zyngier58e0b222018-02-06 17:56:11 +0000387 case KVM_ARM_PSCI_1_0:
388 return kvm_psci_1_0_call(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530389 case KVM_ARM_PSCI_0_2:
390 return kvm_psci_0_2_call(vcpu);
391 case KVM_ARM_PSCI_0_1:
392 return kvm_psci_0_1_call(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500393 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530394 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530395 };
Marc Zyngieraa024c22013-01-20 18:28:13 -0500396}
Marc Zyngier09e6be12018-02-06 17:56:12 +0000397
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000398int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
399{
Andre Przywara99adb5672019-05-03 15:27:49 +0100400 return 3; /* PSCI version and two workaround registers */
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000401}
402
403int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
404{
Andre Przywara99adb5672019-05-03 15:27:49 +0100405 if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
406 return -EFAULT;
407
408 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
409 return -EFAULT;
410
411 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000412 return -EFAULT;
413
414 return 0;
415}
416
Andre Przywara99adb5672019-05-03 15:27:49 +0100417#define KVM_REG_FEATURE_LEVEL_WIDTH 4
418#define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
419
420/*
421 * Convert the workaround level into an easy-to-compare number, where higher
422 * values mean better protection.
423 */
424static int get_kernel_wa_level(u64 regid)
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000425{
Andre Przywara99adb5672019-05-03 15:27:49 +0100426 switch (regid) {
427 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
428 switch (kvm_arm_harden_branch_predictor()) {
429 case KVM_BP_HARDEN_UNKNOWN:
430 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
431 case KVM_BP_HARDEN_WA_NEEDED:
432 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
433 case KVM_BP_HARDEN_NOT_REQUIRED:
434 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
435 }
436 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
437 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
438 switch (kvm_arm_have_ssbd()) {
439 case KVM_SSBD_FORCE_DISABLE:
440 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
441 case KVM_SSBD_KERNEL:
442 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL;
443 case KVM_SSBD_FORCE_ENABLE:
444 case KVM_SSBD_MITIGATED:
445 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
446 case KVM_SSBD_UNKNOWN:
447 default:
448 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN;
449 }
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000450 }
451
452 return -EINVAL;
453}
454
Andre Przywara99adb5672019-05-03 15:27:49 +0100455int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
456{
457 void __user *uaddr = (void __user *)(long)reg->addr;
458 u64 val;
459
460 switch (reg->id) {
461 case KVM_REG_ARM_PSCI_VERSION:
462 val = kvm_psci_version(vcpu, vcpu->kvm);
463 break;
464 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
465 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
466 break;
467 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
468 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
469
470 if (val == KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
471 kvm_arm_get_vcpu_workaround_2_flag(vcpu))
472 val |= KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED;
473 break;
474 default:
475 return -ENOENT;
476 }
477
478 if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
479 return -EFAULT;
480
481 return 0;
482}
483
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000484int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
485{
Andre Przywara99adb5672019-05-03 15:27:49 +0100486 void __user *uaddr = (void __user *)(long)reg->addr;
487 u64 val;
488 int wa_level;
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000489
Andre Przywara99adb5672019-05-03 15:27:49 +0100490 if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
491 return -EFAULT;
492
493 switch (reg->id) {
494 case KVM_REG_ARM_PSCI_VERSION:
495 {
496 bool wants_02;
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000497
498 wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
499
500 switch (val) {
501 case KVM_ARM_PSCI_0_1:
502 if (wants_02)
503 return -EINVAL;
504 vcpu->kvm->arch.psci_version = val;
505 return 0;
506 case KVM_ARM_PSCI_0_2:
507 case KVM_ARM_PSCI_1_0:
508 if (!wants_02)
509 return -EINVAL;
510 vcpu->kvm->arch.psci_version = val;
511 return 0;
512 }
Andre Przywara99adb5672019-05-03 15:27:49 +0100513 break;
514 }
515
516 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
517 if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
518 return -EINVAL;
519
520 if (get_kernel_wa_level(reg->id) < val)
521 return -EINVAL;
522
523 return 0;
524
525 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
526 if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
527 KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
528 return -EINVAL;
529
530 wa_level = val & KVM_REG_FEATURE_LEVEL_MASK;
531
532 if (get_kernel_wa_level(reg->id) < wa_level)
533 return -EINVAL;
534
535 /* The enabled bit must not be set unless the level is AVAIL. */
536 if (wa_level != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
537 wa_level != val)
538 return -EINVAL;
539
540 /* Are we finished or do we need to check the enable bit ? */
541 if (kvm_arm_have_ssbd() != KVM_SSBD_KERNEL)
542 return 0;
543
544 /*
545 * If this kernel supports the workaround to be switched on
546 * or off, make sure it matches the requested setting.
547 */
548 switch (wa_level) {
549 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
550 kvm_arm_set_vcpu_workaround_2_flag(vcpu,
551 val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED);
552 break;
553 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
554 kvm_arm_set_vcpu_workaround_2_flag(vcpu, true);
555 break;
556 }
557
558 return 0;
559 default:
560 return -ENOENT;
Marc Zyngier85bd0ba2018-01-21 16:42:56 +0000561 }
562
563 return -EINVAL;
564}