xref: /linux/arch/arm64/kvm/psci.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
25 {
26 	/*
27 	 * NOTE: For simplicity, we make VCPU suspend emulation to be
28 	 * same-as WFI (Wait-for-interrupt) emulation.
29 	 *
30 	 * This means for KVM the wakeup events are interrupts and
31 	 * this is consistent with intended use of StateID as described
32 	 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
33 	 *
34 	 * Further, we also treat power-down request to be same as
35 	 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
36 	 * specification (ARM DEN 0022A). This means all suspend states
37 	 * for KVM will preserve the register state.
38 	 */
39 	kvm_vcpu_wfi(vcpu);
40 
41 	return PSCI_RET_SUCCESS;
42 }
43 
44 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
45 {
46 	struct vcpu_reset_state *reset_state;
47 	struct kvm *kvm = source_vcpu->kvm;
48 	struct kvm_vcpu *vcpu = NULL;
49 	int ret = PSCI_RET_SUCCESS;
50 	unsigned long cpu_id;
51 
52 	cpu_id = smccc_get_arg1(source_vcpu);
53 	if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
54 		return PSCI_RET_INVALID_PARAMS;
55 
56 	vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
57 
58 	/*
59 	 * Make sure the caller requested a valid CPU and that the CPU is
60 	 * turned off.
61 	 */
62 	if (!vcpu)
63 		return PSCI_RET_INVALID_PARAMS;
64 
65 	spin_lock(&vcpu->arch.mp_state_lock);
66 	if (!kvm_arm_vcpu_stopped(vcpu)) {
67 		if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
68 			ret = PSCI_RET_ALREADY_ON;
69 		else
70 			ret = PSCI_RET_INVALID_PARAMS;
71 
72 		goto out_unlock;
73 	}
74 
75 	reset_state = &vcpu->arch.reset_state;
76 
77 	reset_state->pc = smccc_get_arg2(source_vcpu);
78 
79 	/* Propagate caller endianness */
80 	reset_state->be = kvm_vcpu_is_be(source_vcpu);
81 
82 	/*
83 	 * NOTE: We always update r0 (or x0) because for PSCI v0.1
84 	 * the general purpose registers are undefined upon CPU_ON.
85 	 */
86 	reset_state->r0 = smccc_get_arg3(source_vcpu);
87 
88 	reset_state->reset = true;
89 	kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
90 
91 	/*
92 	 * Make sure the reset request is observed if the RUNNABLE mp_state is
93 	 * observed.
94 	 */
95 	smp_wmb();
96 
97 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
98 	kvm_vcpu_wake_up(vcpu);
99 
100 out_unlock:
101 	spin_unlock(&vcpu->arch.mp_state_lock);
102 	return ret;
103 }
104 
105 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
106 {
107 	int matching_cpus = 0;
108 	unsigned long i, mpidr;
109 	unsigned long target_affinity;
110 	unsigned long target_affinity_mask;
111 	unsigned long lowest_affinity_level;
112 	struct kvm *kvm = vcpu->kvm;
113 	struct kvm_vcpu *tmp;
114 
115 	target_affinity = smccc_get_arg1(vcpu);
116 	lowest_affinity_level = smccc_get_arg2(vcpu);
117 
118 	if (!kvm_psci_valid_affinity(vcpu, target_affinity))
119 		return PSCI_RET_INVALID_PARAMS;
120 
121 	/* Determine target affinity mask */
122 	target_affinity_mask = kvm_psci_affinity_mask(lowest_affinity_level);
123 	if (!target_affinity_mask)
124 		return PSCI_RET_INVALID_PARAMS;
125 
126 	/* Ignore other bits of target affinity */
127 	target_affinity &= target_affinity_mask;
128 
129 	/*
130 	 * If one or more VCPU matching target affinity are running
131 	 * then ON else OFF
132 	 */
133 	kvm_for_each_vcpu(i, tmp, kvm) {
134 		mpidr = kvm_vcpu_get_mpidr_aff(tmp);
135 		if ((mpidr & target_affinity_mask) == target_affinity) {
136 			matching_cpus++;
137 			if (!kvm_arm_vcpu_stopped(tmp))
138 				return PSCI_0_2_AFFINITY_LEVEL_ON;
139 		}
140 	}
141 
142 	if (!matching_cpus)
143 		return PSCI_RET_INVALID_PARAMS;
144 
145 	return PSCI_0_2_AFFINITY_LEVEL_OFF;
146 }
147 
148 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
149 {
150 	unsigned long i;
151 	struct kvm_vcpu *tmp;
152 
153 	/*
154 	 * The KVM ABI specifies that a system event exit may call KVM_RUN
155 	 * again and may perform shutdown/reboot at a later time that when the
156 	 * actual request is made.  Since we are implementing PSCI and a
157 	 * caller of PSCI reboot and shutdown expects that the system shuts
158 	 * down or reboots immediately, let's make sure that VCPUs are not run
159 	 * after this call is handled and before the VCPUs have been
160 	 * re-initialized.
161 	 */
162 	kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
163 		spin_lock(&tmp->arch.mp_state_lock);
164 		WRITE_ONCE(tmp->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
165 		spin_unlock(&tmp->arch.mp_state_lock);
166 	}
167 	kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
168 
169 	memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
170 	vcpu->run->system_event.type = type;
171 	vcpu->run->system_event.ndata = 1;
172 	vcpu->run->system_event.data[0] = flags;
173 	vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
174 }
175 
176 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
177 {
178 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
179 }
180 
181 static void kvm_psci_system_off2(struct kvm_vcpu *vcpu)
182 {
183 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN,
184 				 KVM_SYSTEM_EVENT_SHUTDOWN_FLAG_PSCI_OFF2);
185 }
186 
187 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
188 {
189 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
190 }
191 
192 static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
193 {
194 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
195 				 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
196 }
197 
198 static void kvm_psci_system_suspend(struct kvm_vcpu *vcpu)
199 {
200 	struct kvm_run *run = vcpu->run;
201 
202 	memset(&run->system_event, 0, sizeof(vcpu->run->system_event));
203 	run->system_event.type = KVM_SYSTEM_EVENT_SUSPEND;
204 	run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
205 }
206 
207 static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
208 {
209 	/*
210 	 * Prevent 32 bit guests from calling 64 bit PSCI functions.
211 	 */
212 	if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
213 		return PSCI_RET_NOT_SUPPORTED;
214 
215 	return 0;
216 }
217 
218 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
219 {
220 	u32 psci_fn = smccc_get_function(vcpu);
221 	unsigned long val;
222 	int ret = 1;
223 
224 	switch (psci_fn) {
225 	case PSCI_0_2_FN_PSCI_VERSION:
226 		/*
227 		 * Bits[31:16] = Major Version = 0
228 		 * Bits[15:0] = Minor Version = 2
229 		 */
230 		val = KVM_ARM_PSCI_0_2;
231 		break;
232 	case PSCI_0_2_FN_CPU_SUSPEND:
233 	case PSCI_0_2_FN64_CPU_SUSPEND:
234 		val = kvm_psci_vcpu_suspend(vcpu);
235 		break;
236 	case PSCI_0_2_FN_CPU_OFF:
237 		kvm_arm_vcpu_power_off(vcpu);
238 		val = PSCI_RET_SUCCESS;
239 		break;
240 	case PSCI_0_2_FN_CPU_ON:
241 		kvm_psci_narrow_to_32bit(vcpu);
242 		fallthrough;
243 	case PSCI_0_2_FN64_CPU_ON:
244 		val = kvm_psci_vcpu_on(vcpu);
245 		break;
246 	case PSCI_0_2_FN_AFFINITY_INFO:
247 		kvm_psci_narrow_to_32bit(vcpu);
248 		fallthrough;
249 	case PSCI_0_2_FN64_AFFINITY_INFO:
250 		val = kvm_psci_vcpu_affinity_info(vcpu);
251 		break;
252 	case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
253 		/*
254 		 * Trusted OS is MP hence does not require migration
255 	         * or
256 		 * Trusted OS is not present
257 		 */
258 		val = PSCI_0_2_TOS_MP;
259 		break;
260 	case PSCI_0_2_FN_SYSTEM_OFF:
261 		kvm_psci_system_off(vcpu);
262 		/*
263 		 * We shouldn't be going back to guest VCPU after
264 		 * receiving SYSTEM_OFF request.
265 		 *
266 		 * If user space accidentally/deliberately resumes
267 		 * guest VCPU after SYSTEM_OFF request then guest
268 		 * VCPU should see internal failure from PSCI return
269 		 * value. To achieve this, we preload r0 (or x0) with
270 		 * PSCI return value INTERNAL_FAILURE.
271 		 */
272 		val = PSCI_RET_INTERNAL_FAILURE;
273 		ret = 0;
274 		break;
275 	case PSCI_0_2_FN_SYSTEM_RESET:
276 		kvm_psci_system_reset(vcpu);
277 		/*
278 		 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
279 		 * with PSCI return value INTERNAL_FAILURE.
280 		 */
281 		val = PSCI_RET_INTERNAL_FAILURE;
282 		ret = 0;
283 		break;
284 	default:
285 		val = PSCI_RET_NOT_SUPPORTED;
286 		break;
287 	}
288 
289 	smccc_set_retval(vcpu, val, 0, 0, 0);
290 	return ret;
291 }
292 
293 static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
294 {
295 	unsigned long val = PSCI_RET_NOT_SUPPORTED;
296 	u32 psci_fn = smccc_get_function(vcpu);
297 	struct kvm *kvm = vcpu->kvm;
298 	u32 arg;
299 	int ret = 1;
300 
301 	switch(psci_fn) {
302 	case PSCI_0_2_FN_PSCI_VERSION:
303 		val = PSCI_VERSION(1, minor);
304 		break;
305 	case PSCI_1_0_FN_PSCI_FEATURES:
306 		arg = smccc_get_arg1(vcpu);
307 		val = kvm_psci_check_allowed_function(vcpu, arg);
308 		if (val)
309 			break;
310 
311 		val = PSCI_RET_NOT_SUPPORTED;
312 
313 		switch(arg) {
314 		case PSCI_0_2_FN_PSCI_VERSION:
315 		case PSCI_0_2_FN_CPU_SUSPEND:
316 		case PSCI_0_2_FN64_CPU_SUSPEND:
317 		case PSCI_0_2_FN_CPU_OFF:
318 		case PSCI_0_2_FN_CPU_ON:
319 		case PSCI_0_2_FN64_CPU_ON:
320 		case PSCI_0_2_FN_AFFINITY_INFO:
321 		case PSCI_0_2_FN64_AFFINITY_INFO:
322 		case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
323 		case PSCI_0_2_FN_SYSTEM_OFF:
324 		case PSCI_0_2_FN_SYSTEM_RESET:
325 		case PSCI_1_0_FN_PSCI_FEATURES:
326 		case ARM_SMCCC_VERSION_FUNC_ID:
327 			val = 0;
328 			break;
329 		case PSCI_1_0_FN_SYSTEM_SUSPEND:
330 		case PSCI_1_0_FN64_SYSTEM_SUSPEND:
331 			if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags))
332 				val = 0;
333 			break;
334 		case PSCI_1_1_FN_SYSTEM_RESET2:
335 		case PSCI_1_1_FN64_SYSTEM_RESET2:
336 			if (minor >= 1)
337 				val = 0;
338 			break;
339 		case PSCI_1_3_FN_SYSTEM_OFF2:
340 		case PSCI_1_3_FN64_SYSTEM_OFF2:
341 			if (minor >= 3)
342 				val = PSCI_1_3_OFF_TYPE_HIBERNATE_OFF;
343 			break;
344 		}
345 		break;
346 	case PSCI_1_0_FN_SYSTEM_SUSPEND:
347 		kvm_psci_narrow_to_32bit(vcpu);
348 		fallthrough;
349 	case PSCI_1_0_FN64_SYSTEM_SUSPEND:
350 		/*
351 		 * Return directly to userspace without changing the vCPU's
352 		 * registers. Userspace depends on reading the SMCCC parameters
353 		 * to implement SYSTEM_SUSPEND.
354 		 */
355 		if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags)) {
356 			kvm_psci_system_suspend(vcpu);
357 			return 0;
358 		}
359 		break;
360 	case PSCI_1_1_FN_SYSTEM_RESET2:
361 		kvm_psci_narrow_to_32bit(vcpu);
362 		fallthrough;
363 	case PSCI_1_1_FN64_SYSTEM_RESET2:
364 		if (minor >= 1) {
365 			arg = smccc_get_arg1(vcpu);
366 
367 			if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
368 			    arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
369 				kvm_psci_system_reset2(vcpu);
370 				vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
371 				return 0;
372 			}
373 
374 			val = PSCI_RET_INVALID_PARAMS;
375 			break;
376 		}
377 		break;
378 	case PSCI_1_3_FN_SYSTEM_OFF2:
379 		kvm_psci_narrow_to_32bit(vcpu);
380 		fallthrough;
381 	case PSCI_1_3_FN64_SYSTEM_OFF2:
382 		if (minor < 3)
383 			break;
384 
385 		arg = smccc_get_arg1(vcpu);
386 		/*
387 		 * SYSTEM_OFF2 defaults to HIBERNATE_OFF if arg1 is zero. arg2
388 		 * must be zero.
389 		 */
390 		if ((arg && arg != PSCI_1_3_OFF_TYPE_HIBERNATE_OFF) ||
391 		    smccc_get_arg2(vcpu) != 0) {
392 			val = PSCI_RET_INVALID_PARAMS;
393 			break;
394 		}
395 		kvm_psci_system_off2(vcpu);
396 		/*
397 		 * We shouldn't be going back to the guest after receiving a
398 		 * SYSTEM_OFF2 request. Preload a return value of
399 		 * INTERNAL_FAILURE should userspace ignore the exit and resume
400 		 * the vCPU.
401 		 */
402 		val = PSCI_RET_INTERNAL_FAILURE;
403 		ret = 0;
404 		break;
405 	default:
406 		return kvm_psci_0_2_call(vcpu);
407 	}
408 
409 	smccc_set_retval(vcpu, val, 0, 0, 0);
410 	return ret;
411 }
412 
413 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
414 {
415 	u32 psci_fn = smccc_get_function(vcpu);
416 	unsigned long val;
417 
418 	switch (psci_fn) {
419 	case KVM_PSCI_FN_CPU_OFF:
420 		kvm_arm_vcpu_power_off(vcpu);
421 		val = PSCI_RET_SUCCESS;
422 		break;
423 	case KVM_PSCI_FN_CPU_ON:
424 		val = kvm_psci_vcpu_on(vcpu);
425 		break;
426 	default:
427 		val = PSCI_RET_NOT_SUPPORTED;
428 		break;
429 	}
430 
431 	smccc_set_retval(vcpu, val, 0, 0, 0);
432 	return 1;
433 }
434 
435 /**
436  * kvm_psci_call - handle PSCI call if r0 value is in range
437  * @vcpu: Pointer to the VCPU struct
438  *
439  * Handle PSCI calls from guests through traps from HVC instructions.
440  * The calling convention is similar to SMC calls to the secure world
441  * where the function number is placed in r0.
442  *
443  * This function returns: > 0 (success), 0 (success but exit to user
444  * space), and < 0 (errors)
445  *
446  * Errors:
447  * -EINVAL: Unrecognized PSCI function
448  */
449 int kvm_psci_call(struct kvm_vcpu *vcpu)
450 {
451 	u32 psci_fn = smccc_get_function(vcpu);
452 	int version = kvm_psci_version(vcpu);
453 	unsigned long val;
454 
455 	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
456 	if (val) {
457 		smccc_set_retval(vcpu, val, 0, 0, 0);
458 		return 1;
459 	}
460 
461 	switch (version) {
462 	case KVM_ARM_PSCI_1_3:
463 		return kvm_psci_1_x_call(vcpu, 3);
464 	case KVM_ARM_PSCI_1_2:
465 		return kvm_psci_1_x_call(vcpu, 2);
466 	case KVM_ARM_PSCI_1_1:
467 		return kvm_psci_1_x_call(vcpu, 1);
468 	case KVM_ARM_PSCI_1_0:
469 		return kvm_psci_1_x_call(vcpu, 0);
470 	case KVM_ARM_PSCI_0_2:
471 		return kvm_psci_0_2_call(vcpu);
472 	case KVM_ARM_PSCI_0_1:
473 		return kvm_psci_0_1_call(vcpu);
474 	default:
475 		WARN_ONCE(1, "Unknown PSCI version %d", version);
476 		smccc_set_retval(vcpu, SMCCC_RET_NOT_SUPPORTED, 0, 0, 0);
477 		return 1;
478 	}
479 }
480