1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
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>
13 #include <asm/cputype.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_host.h>
17 #include <kvm/arm_psci.h>
18 #include <kvm/arm_hypercalls.h>
21 * This is an implementation of the Power State Coordination Interface
22 * as described in ARM document number ARM DEN 0022A.
25 #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
27 static unsigned long psci_affinity_mask(unsigned long affinity_level)
29 if (affinity_level <= 3)
30 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
35 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
38 * NOTE: For simplicity, we make VCPU suspend emulation to be
39 * same-as WFI (Wait-for-interrupt) emulation.
41 * This means for KVM the wakeup events are interrupts and
42 * this is consistent with intended use of StateID as described
43 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
45 * Further, we also treat power-down request to be same as
46 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
47 * specification (ARM DEN 0022A). This means all suspend states
48 * for KVM will preserve the register state.
51 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
53 return PSCI_RET_SUCCESS;
56 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
58 vcpu->arch.power_off = true;
59 kvm_make_request(KVM_REQ_SLEEP, vcpu);
63 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
65 struct vcpu_reset_state *reset_state;
66 struct kvm *kvm = source_vcpu->kvm;
67 struct kvm_vcpu *vcpu = NULL;
70 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
71 if (vcpu_mode_is_32bit(source_vcpu))
74 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
77 * Make sure the caller requested a valid CPU and that the CPU is
81 return PSCI_RET_INVALID_PARAMS;
82 if (!vcpu->arch.power_off) {
83 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
84 return PSCI_RET_ALREADY_ON;
86 return PSCI_RET_INVALID_PARAMS;
89 reset_state = &vcpu->arch.reset_state;
91 reset_state->pc = smccc_get_arg2(source_vcpu);
93 /* Propagate caller endianness */
94 reset_state->be = kvm_vcpu_is_be(source_vcpu);
97 * NOTE: We always update r0 (or x0) because for PSCI v0.1
98 * the general puspose registers are undefined upon CPU_ON.
100 reset_state->r0 = smccc_get_arg3(source_vcpu);
102 WRITE_ONCE(reset_state->reset, true);
103 kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
106 * Make sure the reset request is observed if the change to
107 * power_state is observed.
111 vcpu->arch.power_off = false;
112 kvm_vcpu_wake_up(vcpu);
114 return PSCI_RET_SUCCESS;
117 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
119 int i, matching_cpus = 0;
121 unsigned long target_affinity;
122 unsigned long target_affinity_mask;
123 unsigned long lowest_affinity_level;
124 struct kvm *kvm = vcpu->kvm;
125 struct kvm_vcpu *tmp;
127 target_affinity = smccc_get_arg1(vcpu);
128 lowest_affinity_level = smccc_get_arg2(vcpu);
130 /* Determine target affinity mask */
131 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
132 if (!target_affinity_mask)
133 return PSCI_RET_INVALID_PARAMS;
135 /* Ignore other bits of target affinity */
136 target_affinity &= target_affinity_mask;
139 * If one or more VCPU matching target affinity are running
142 kvm_for_each_vcpu(i, tmp, kvm) {
143 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
144 if ((mpidr & target_affinity_mask) == target_affinity) {
146 if (!tmp->arch.power_off)
147 return PSCI_0_2_AFFINITY_LEVEL_ON;
152 return PSCI_RET_INVALID_PARAMS;
154 return PSCI_0_2_AFFINITY_LEVEL_OFF;
157 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
160 struct kvm_vcpu *tmp;
163 * The KVM ABI specifies that a system event exit may call KVM_RUN
164 * again and may perform shutdown/reboot at a later time that when the
165 * actual request is made. Since we are implementing PSCI and a
166 * caller of PSCI reboot and shutdown expects that the system shuts
167 * down or reboots immediately, let's make sure that VCPUs are not run
168 * after this call is handled and before the VCPUs have been
171 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
172 tmp->arch.power_off = true;
173 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
175 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
176 vcpu->run->system_event.type = type;
177 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
180 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
182 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
185 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
187 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
190 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
192 struct kvm *kvm = vcpu->kvm;
193 u32 psci_fn = smccc_get_function(vcpu);
198 case PSCI_0_2_FN_PSCI_VERSION:
200 * Bits[31:16] = Major Version = 0
201 * Bits[15:0] = Minor Version = 2
203 val = KVM_ARM_PSCI_0_2;
205 case PSCI_0_2_FN_CPU_SUSPEND:
206 case PSCI_0_2_FN64_CPU_SUSPEND:
207 val = kvm_psci_vcpu_suspend(vcpu);
209 case PSCI_0_2_FN_CPU_OFF:
210 kvm_psci_vcpu_off(vcpu);
211 val = PSCI_RET_SUCCESS;
213 case PSCI_0_2_FN_CPU_ON:
214 case PSCI_0_2_FN64_CPU_ON:
215 mutex_lock(&kvm->lock);
216 val = kvm_psci_vcpu_on(vcpu);
217 mutex_unlock(&kvm->lock);
219 case PSCI_0_2_FN_AFFINITY_INFO:
220 case PSCI_0_2_FN64_AFFINITY_INFO:
221 val = kvm_psci_vcpu_affinity_info(vcpu);
223 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
225 * Trusted OS is MP hence does not require migration
227 * Trusted OS is not present
229 val = PSCI_0_2_TOS_MP;
231 case PSCI_0_2_FN_SYSTEM_OFF:
232 kvm_psci_system_off(vcpu);
234 * We should'nt be going back to guest VCPU after
235 * receiving SYSTEM_OFF request.
237 * If user space accidently/deliberately resumes
238 * guest VCPU after SYSTEM_OFF request then guest
239 * VCPU should see internal failure from PSCI return
240 * value. To achieve this, we preload r0 (or x0) with
241 * PSCI return value INTERNAL_FAILURE.
243 val = PSCI_RET_INTERNAL_FAILURE;
246 case PSCI_0_2_FN_SYSTEM_RESET:
247 kvm_psci_system_reset(vcpu);
249 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
250 * with PSCI return value INTERNAL_FAILURE.
252 val = PSCI_RET_INTERNAL_FAILURE;
256 val = PSCI_RET_NOT_SUPPORTED;
260 smccc_set_retval(vcpu, val, 0, 0, 0);
264 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
266 u32 psci_fn = smccc_get_function(vcpu);
272 case PSCI_0_2_FN_PSCI_VERSION:
273 val = KVM_ARM_PSCI_1_0;
275 case PSCI_1_0_FN_PSCI_FEATURES:
276 feature = smccc_get_arg1(vcpu);
278 case PSCI_0_2_FN_PSCI_VERSION:
279 case PSCI_0_2_FN_CPU_SUSPEND:
280 case PSCI_0_2_FN64_CPU_SUSPEND:
281 case PSCI_0_2_FN_CPU_OFF:
282 case PSCI_0_2_FN_CPU_ON:
283 case PSCI_0_2_FN64_CPU_ON:
284 case PSCI_0_2_FN_AFFINITY_INFO:
285 case PSCI_0_2_FN64_AFFINITY_INFO:
286 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
287 case PSCI_0_2_FN_SYSTEM_OFF:
288 case PSCI_0_2_FN_SYSTEM_RESET:
289 case PSCI_1_0_FN_PSCI_FEATURES:
290 case ARM_SMCCC_VERSION_FUNC_ID:
294 val = PSCI_RET_NOT_SUPPORTED;
299 return kvm_psci_0_2_call(vcpu);
302 smccc_set_retval(vcpu, val, 0, 0, 0);
306 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
308 struct kvm *kvm = vcpu->kvm;
309 u32 psci_fn = smccc_get_function(vcpu);
313 case KVM_PSCI_FN_CPU_OFF:
314 kvm_psci_vcpu_off(vcpu);
315 val = PSCI_RET_SUCCESS;
317 case KVM_PSCI_FN_CPU_ON:
318 mutex_lock(&kvm->lock);
319 val = kvm_psci_vcpu_on(vcpu);
320 mutex_unlock(&kvm->lock);
323 val = PSCI_RET_NOT_SUPPORTED;
327 smccc_set_retval(vcpu, val, 0, 0, 0);
332 * kvm_psci_call - handle PSCI call if r0 value is in range
333 * @vcpu: Pointer to the VCPU struct
335 * Handle PSCI calls from guests through traps from HVC instructions.
336 * The calling convention is similar to SMC calls to the secure world
337 * where the function number is placed in r0.
339 * This function returns: > 0 (success), 0 (success but exit to user
340 * space), and < 0 (errors)
343 * -EINVAL: Unrecognized PSCI function
345 int kvm_psci_call(struct kvm_vcpu *vcpu)
347 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
348 case KVM_ARM_PSCI_1_0:
349 return kvm_psci_1_0_call(vcpu);
350 case KVM_ARM_PSCI_0_2:
351 return kvm_psci_0_2_call(vcpu);
352 case KVM_ARM_PSCI_0_1:
353 return kvm_psci_0_1_call(vcpu);
359 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
361 return 3; /* PSCI version and two workaround registers */
364 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
366 if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
369 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
372 if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
378 #define KVM_REG_FEATURE_LEVEL_WIDTH 4
379 #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
382 * Convert the workaround level into an easy-to-compare number, where higher
383 * values mean better protection.
385 static int get_kernel_wa_level(u64 regid)
388 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
389 switch (kvm_arm_harden_branch_predictor()) {
390 case KVM_BP_HARDEN_UNKNOWN:
391 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
392 case KVM_BP_HARDEN_WA_NEEDED:
393 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
394 case KVM_BP_HARDEN_NOT_REQUIRED:
395 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
397 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
398 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
399 switch (kvm_arm_have_ssbd()) {
400 case KVM_SSBD_FORCE_DISABLE:
401 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
402 case KVM_SSBD_KERNEL:
403 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL;
404 case KVM_SSBD_FORCE_ENABLE:
405 case KVM_SSBD_MITIGATED:
406 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
407 case KVM_SSBD_UNKNOWN:
409 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN;
416 int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
418 void __user *uaddr = (void __user *)(long)reg->addr;
422 case KVM_REG_ARM_PSCI_VERSION:
423 val = kvm_psci_version(vcpu, vcpu->kvm);
425 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
426 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
428 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
429 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
431 if (val == KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
432 kvm_arm_get_vcpu_workaround_2_flag(vcpu))
433 val |= KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED;
439 if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
445 int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
447 void __user *uaddr = (void __user *)(long)reg->addr;
451 if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
455 case KVM_REG_ARM_PSCI_VERSION:
459 wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
462 case KVM_ARM_PSCI_0_1:
465 vcpu->kvm->arch.psci_version = val;
467 case KVM_ARM_PSCI_0_2:
468 case KVM_ARM_PSCI_1_0:
471 vcpu->kvm->arch.psci_version = val;
477 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
478 if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
481 if (get_kernel_wa_level(reg->id) < val)
486 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
487 if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
488 KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
491 wa_level = val & KVM_REG_FEATURE_LEVEL_MASK;
493 if (get_kernel_wa_level(reg->id) < wa_level)
496 /* The enabled bit must not be set unless the level is AVAIL. */
497 if (wa_level != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
501 /* Are we finished or do we need to check the enable bit ? */
502 if (kvm_arm_have_ssbd() != KVM_SSBD_KERNEL)
506 * If this kernel supports the workaround to be switched on
507 * or off, make sure it matches the requested setting.
510 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
511 kvm_arm_set_vcpu_workaround_2_flag(vcpu,
512 val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED);
514 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
515 kvm_arm_set_vcpu_workaround_2_flag(vcpu, true);