]> git.ipfire.org Git - thirdparty/linux.git/blob - virt/kvm/arm/psci.c
io_uring: reset -EBUSY error when io sq thread is waken up
[thirdparty/linux.git] / virt / kvm / arm / psci.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 */
6
7 #include <linux/arm-smccc.h>
8 #include <linux/preempt.h>
9 #include <linux/kvm_host.h>
10 #include <linux/uaccess.h>
11 #include <linux/wait.h>
12
13 #include <asm/cputype.h>
14 #include <asm/kvm_emulate.h>
15
16 #include <kvm/arm_psci.h>
17 #include <kvm/arm_hypercalls.h>
18
19 /*
20 * This is an implementation of the Power State Coordination Interface
21 * as described in ARM document number ARM DEN 0022A.
22 */
23
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
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);
50 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
51
52 return PSCI_RET_SUCCESS;
53 }
54
55 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
56 {
57 vcpu->arch.power_off = true;
58 kvm_make_request(KVM_REQ_SLEEP, vcpu);
59 kvm_vcpu_kick(vcpu);
60 }
61
62 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
63 {
64 struct vcpu_reset_state *reset_state;
65 struct kvm *kvm = source_vcpu->kvm;
66 struct kvm_vcpu *vcpu = NULL;
67 unsigned long cpu_id;
68
69 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
70 if (vcpu_mode_is_32bit(source_vcpu))
71 cpu_id &= ~((u32) 0);
72
73 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
74
75 /*
76 * Make sure the caller requested a valid CPU and that the CPU is
77 * turned off.
78 */
79 if (!vcpu)
80 return PSCI_RET_INVALID_PARAMS;
81 if (!vcpu->arch.power_off) {
82 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
83 return PSCI_RET_ALREADY_ON;
84 else
85 return PSCI_RET_INVALID_PARAMS;
86 }
87
88 reset_state = &vcpu->arch.reset_state;
89
90 reset_state->pc = smccc_get_arg2(source_vcpu);
91
92 /* Propagate caller endianness */
93 reset_state->be = kvm_vcpu_is_be(source_vcpu);
94
95 /*
96 * NOTE: We always update r0 (or x0) because for PSCI v0.1
97 * the general puspose registers are undefined upon CPU_ON.
98 */
99 reset_state->r0 = smccc_get_arg3(source_vcpu);
100
101 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);
112
113 return PSCI_RET_SUCCESS;
114 }
115
116 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
117 {
118 int i, matching_cpus = 0;
119 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
126 target_affinity = smccc_get_arg1(vcpu);
127 lowest_affinity_level = smccc_get_arg2(vcpu);
128
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) {
142 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
143 if ((mpidr & target_affinity_mask) == target_affinity) {
144 matching_cpus++;
145 if (!tmp->arch.power_off)
146 return PSCI_0_2_AFFINITY_LEVEL_ON;
147 }
148 }
149
150 if (!matching_cpus)
151 return PSCI_RET_INVALID_PARAMS;
152
153 return PSCI_0_2_AFFINITY_LEVEL_OFF;
154 }
155
156 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
157 {
158 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 */
170 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
171 tmp->arch.power_off = true;
172 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
173
174 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
179 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
180 {
181 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
182 }
183
184 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
185 {
186 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
187 }
188
189 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
190 {
191 struct kvm *kvm = vcpu->kvm;
192 u32 psci_fn = smccc_get_function(vcpu);
193 unsigned long val;
194 int ret = 1;
195
196 switch (psci_fn) {
197 case PSCI_0_2_FN_PSCI_VERSION:
198 /*
199 * Bits[31:16] = Major Version = 0
200 * Bits[15:0] = Minor Version = 2
201 */
202 val = KVM_ARM_PSCI_0_2;
203 break;
204 case PSCI_0_2_FN_CPU_SUSPEND:
205 case PSCI_0_2_FN64_CPU_SUSPEND:
206 val = kvm_psci_vcpu_suspend(vcpu);
207 break;
208 case PSCI_0_2_FN_CPU_OFF:
209 kvm_psci_vcpu_off(vcpu);
210 val = PSCI_RET_SUCCESS;
211 break;
212 case PSCI_0_2_FN_CPU_ON:
213 case PSCI_0_2_FN64_CPU_ON:
214 mutex_lock(&kvm->lock);
215 val = kvm_psci_vcpu_on(vcpu);
216 mutex_unlock(&kvm->lock);
217 break;
218 case PSCI_0_2_FN_AFFINITY_INFO:
219 case PSCI_0_2_FN64_AFFINITY_INFO:
220 val = kvm_psci_vcpu_affinity_info(vcpu);
221 break;
222 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
223 /*
224 * Trusted OS is MP hence does not require migration
225 * or
226 * Trusted OS is not present
227 */
228 val = PSCI_0_2_TOS_MP;
229 break;
230 case PSCI_0_2_FN_SYSTEM_OFF:
231 kvm_psci_system_off(vcpu);
232 /*
233 * We should'nt be going back to guest VCPU after
234 * receiving SYSTEM_OFF request.
235 *
236 * If user space accidently/deliberately resumes
237 * guest VCPU after SYSTEM_OFF request then guest
238 * VCPU should see internal failure from PSCI return
239 * value. To achieve this, we preload r0 (or x0) with
240 * PSCI return value INTERNAL_FAILURE.
241 */
242 val = PSCI_RET_INTERNAL_FAILURE;
243 ret = 0;
244 break;
245 case PSCI_0_2_FN_SYSTEM_RESET:
246 kvm_psci_system_reset(vcpu);
247 /*
248 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
249 * with PSCI return value INTERNAL_FAILURE.
250 */
251 val = PSCI_RET_INTERNAL_FAILURE;
252 ret = 0;
253 break;
254 default:
255 val = PSCI_RET_NOT_SUPPORTED;
256 break;
257 }
258
259 smccc_set_retval(vcpu, val, 0, 0, 0);
260 return ret;
261 }
262
263 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
264 {
265 u32 psci_fn = smccc_get_function(vcpu);
266 u32 feature;
267 unsigned long val;
268 int ret = 1;
269
270 switch(psci_fn) {
271 case PSCI_0_2_FN_PSCI_VERSION:
272 val = KVM_ARM_PSCI_1_0;
273 break;
274 case PSCI_1_0_FN_PSCI_FEATURES:
275 feature = smccc_get_arg1(vcpu);
276 switch(feature) {
277 case PSCI_0_2_FN_PSCI_VERSION:
278 case PSCI_0_2_FN_CPU_SUSPEND:
279 case PSCI_0_2_FN64_CPU_SUSPEND:
280 case PSCI_0_2_FN_CPU_OFF:
281 case PSCI_0_2_FN_CPU_ON:
282 case PSCI_0_2_FN64_CPU_ON:
283 case PSCI_0_2_FN_AFFINITY_INFO:
284 case PSCI_0_2_FN64_AFFINITY_INFO:
285 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
286 case PSCI_0_2_FN_SYSTEM_OFF:
287 case PSCI_0_2_FN_SYSTEM_RESET:
288 case PSCI_1_0_FN_PSCI_FEATURES:
289 case ARM_SMCCC_VERSION_FUNC_ID:
290 val = 0;
291 break;
292 default:
293 val = PSCI_RET_NOT_SUPPORTED;
294 break;
295 }
296 break;
297 default:
298 return kvm_psci_0_2_call(vcpu);
299 }
300
301 smccc_set_retval(vcpu, val, 0, 0, 0);
302 return ret;
303 }
304
305 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
306 {
307 struct kvm *kvm = vcpu->kvm;
308 u32 psci_fn = smccc_get_function(vcpu);
309 unsigned long val;
310
311 switch (psci_fn) {
312 case KVM_PSCI_FN_CPU_OFF:
313 kvm_psci_vcpu_off(vcpu);
314 val = PSCI_RET_SUCCESS;
315 break;
316 case KVM_PSCI_FN_CPU_ON:
317 mutex_lock(&kvm->lock);
318 val = kvm_psci_vcpu_on(vcpu);
319 mutex_unlock(&kvm->lock);
320 break;
321 default:
322 val = PSCI_RET_NOT_SUPPORTED;
323 break;
324 }
325
326 smccc_set_retval(vcpu, val, 0, 0, 0);
327 return 1;
328 }
329
330 /**
331 * kvm_psci_call - handle PSCI call if r0 value is in range
332 * @vcpu: Pointer to the VCPU struct
333 *
334 * Handle PSCI calls from guests through traps from HVC instructions.
335 * The calling convention is similar to SMC calls to the secure world
336 * where the function number is placed in r0.
337 *
338 * This function returns: > 0 (success), 0 (success but exit to user
339 * space), and < 0 (errors)
340 *
341 * Errors:
342 * -EINVAL: Unrecognized PSCI function
343 */
344 int kvm_psci_call(struct kvm_vcpu *vcpu)
345 {
346 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
347 case KVM_ARM_PSCI_1_0:
348 return kvm_psci_1_0_call(vcpu);
349 case KVM_ARM_PSCI_0_2:
350 return kvm_psci_0_2_call(vcpu);
351 case KVM_ARM_PSCI_0_1:
352 return kvm_psci_0_1_call(vcpu);
353 default:
354 return -EINVAL;
355 };
356 }
357
358 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
359 {
360 return 3; /* PSCI version and two workaround registers */
361 }
362
363 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
364 {
365 if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
366 return -EFAULT;
367
368 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
369 return -EFAULT;
370
371 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
372 return -EFAULT;
373
374 return 0;
375 }
376
377 #define KVM_REG_FEATURE_LEVEL_WIDTH 4
378 #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
379
380 /*
381 * Convert the workaround level into an easy-to-compare number, where higher
382 * values mean better protection.
383 */
384 static int get_kernel_wa_level(u64 regid)
385 {
386 switch (regid) {
387 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
388 switch (kvm_arm_harden_branch_predictor()) {
389 case KVM_BP_HARDEN_UNKNOWN:
390 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
391 case KVM_BP_HARDEN_WA_NEEDED:
392 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
393 case KVM_BP_HARDEN_NOT_REQUIRED:
394 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
395 }
396 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
397 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
398 switch (kvm_arm_have_ssbd()) {
399 case KVM_SSBD_FORCE_DISABLE:
400 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
401 case KVM_SSBD_KERNEL:
402 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL;
403 case KVM_SSBD_FORCE_ENABLE:
404 case KVM_SSBD_MITIGATED:
405 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
406 case KVM_SSBD_UNKNOWN:
407 default:
408 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN;
409 }
410 }
411
412 return -EINVAL;
413 }
414
415 int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
416 {
417 void __user *uaddr = (void __user *)(long)reg->addr;
418 u64 val;
419
420 switch (reg->id) {
421 case KVM_REG_ARM_PSCI_VERSION:
422 val = kvm_psci_version(vcpu, vcpu->kvm);
423 break;
424 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
425 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
426 break;
427 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
428 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
429
430 if (val == KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
431 kvm_arm_get_vcpu_workaround_2_flag(vcpu))
432 val |= KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED;
433 break;
434 default:
435 return -ENOENT;
436 }
437
438 if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
439 return -EFAULT;
440
441 return 0;
442 }
443
444 int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
445 {
446 void __user *uaddr = (void __user *)(long)reg->addr;
447 u64 val;
448 int wa_level;
449
450 if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
451 return -EFAULT;
452
453 switch (reg->id) {
454 case KVM_REG_ARM_PSCI_VERSION:
455 {
456 bool wants_02;
457
458 wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
459
460 switch (val) {
461 case KVM_ARM_PSCI_0_1:
462 if (wants_02)
463 return -EINVAL;
464 vcpu->kvm->arch.psci_version = val;
465 return 0;
466 case KVM_ARM_PSCI_0_2:
467 case KVM_ARM_PSCI_1_0:
468 if (!wants_02)
469 return -EINVAL;
470 vcpu->kvm->arch.psci_version = val;
471 return 0;
472 }
473 break;
474 }
475
476 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
477 if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
478 return -EINVAL;
479
480 if (get_kernel_wa_level(reg->id) < val)
481 return -EINVAL;
482
483 return 0;
484
485 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
486 if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
487 KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
488 return -EINVAL;
489
490 wa_level = val & KVM_REG_FEATURE_LEVEL_MASK;
491
492 if (get_kernel_wa_level(reg->id) < wa_level)
493 return -EINVAL;
494
495 /* The enabled bit must not be set unless the level is AVAIL. */
496 if (wa_level != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
497 wa_level != val)
498 return -EINVAL;
499
500 /* Are we finished or do we need to check the enable bit ? */
501 if (kvm_arm_have_ssbd() != KVM_SSBD_KERNEL)
502 return 0;
503
504 /*
505 * If this kernel supports the workaround to be switched on
506 * or off, make sure it matches the requested setting.
507 */
508 switch (wa_level) {
509 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
510 kvm_arm_set_vcpu_workaround_2_flag(vcpu,
511 val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED);
512 break;
513 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
514 kvm_arm_set_vcpu_workaround_2_flag(vcpu, true);
515 break;
516 }
517
518 return 0;
519 default:
520 return -ENOENT;
521 }
522
523 return -EINVAL;
524 }