psci.c 13 KB

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