psci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (C) 2012 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/arm-smccc.h>
  18. #include <linux/preempt.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/wait.h>
  22. #include <asm/cputype.h>
  23. #include <asm/kvm_emulate.h>
  24. #include <asm/kvm_host.h>
  25. #include <kvm/arm_psci.h>
  26. /*
  27. * This is an implementation of the Power State Coordination Interface
  28. * as described in ARM document number ARM DEN 0022A.
  29. */
  30. #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
  31. static u32 smccc_get_function(struct kvm_vcpu *vcpu)
  32. {
  33. return vcpu_get_reg(vcpu, 0);
  34. }
  35. static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
  36. {
  37. return vcpu_get_reg(vcpu, 1);
  38. }
  39. static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
  40. {
  41. return vcpu_get_reg(vcpu, 2);
  42. }
  43. static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
  44. {
  45. return vcpu_get_reg(vcpu, 3);
  46. }
  47. static void smccc_set_retval(struct kvm_vcpu *vcpu,
  48. unsigned long a0,
  49. unsigned long a1,
  50. unsigned long a2,
  51. unsigned long a3)
  52. {
  53. vcpu_set_reg(vcpu, 0, a0);
  54. vcpu_set_reg(vcpu, 1, a1);
  55. vcpu_set_reg(vcpu, 2, a2);
  56. vcpu_set_reg(vcpu, 3, a3);
  57. }
  58. static unsigned long psci_affinity_mask(unsigned long affinity_level)
  59. {
  60. if (affinity_level <= 3)
  61. return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
  62. return 0;
  63. }
  64. static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
  65. {
  66. /*
  67. * NOTE: For simplicity, we make VCPU suspend emulation to be
  68. * same-as WFI (Wait-for-interrupt) emulation.
  69. *
  70. * This means for KVM the wakeup events are interrupts and
  71. * this is consistent with intended use of StateID as described
  72. * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
  73. *
  74. * Further, we also treat power-down request to be same as
  75. * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
  76. * specification (ARM DEN 0022A). This means all suspend states
  77. * for KVM will preserve the register state.
  78. */
  79. kvm_vcpu_block(vcpu);
  80. kvm_clear_request(KVM_REQ_UNHALT, vcpu);
  81. return PSCI_RET_SUCCESS;
  82. }
  83. static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
  84. {
  85. vcpu->arch.power_off = true;
  86. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  87. kvm_vcpu_kick(vcpu);
  88. }
  89. static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  90. {
  91. struct vcpu_reset_state *reset_state;
  92. struct kvm *kvm = source_vcpu->kvm;
  93. struct kvm_vcpu *vcpu = NULL;
  94. unsigned long cpu_id;
  95. cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
  96. if (vcpu_mode_is_32bit(source_vcpu))
  97. cpu_id &= ~((u32) 0);
  98. vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
  99. /*
  100. * Make sure the caller requested a valid CPU and that the CPU is
  101. * turned off.
  102. */
  103. if (!vcpu)
  104. return PSCI_RET_INVALID_PARAMS;
  105. if (!vcpu->arch.power_off) {
  106. if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
  107. return PSCI_RET_ALREADY_ON;
  108. else
  109. return PSCI_RET_INVALID_PARAMS;
  110. }
  111. reset_state = &vcpu->arch.reset_state;
  112. reset_state->pc = smccc_get_arg2(source_vcpu);
  113. /* Propagate caller endianness */
  114. reset_state->be = kvm_vcpu_is_be(source_vcpu);
  115. /*
  116. * NOTE: We always update r0 (or x0) because for PSCI v0.1
  117. * the general puspose registers are undefined upon CPU_ON.
  118. */
  119. reset_state->r0 = smccc_get_arg3(source_vcpu);
  120. WRITE_ONCE(reset_state->reset, true);
  121. kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
  122. /*
  123. * Make sure the reset request is observed if the change to
  124. * power_state is observed.
  125. */
  126. smp_wmb();
  127. vcpu->arch.power_off = false;
  128. kvm_vcpu_wake_up(vcpu);
  129. return PSCI_RET_SUCCESS;
  130. }
  131. static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
  132. {
  133. int i, matching_cpus = 0;
  134. unsigned long mpidr;
  135. unsigned long target_affinity;
  136. unsigned long target_affinity_mask;
  137. unsigned long lowest_affinity_level;
  138. struct kvm *kvm = vcpu->kvm;
  139. struct kvm_vcpu *tmp;
  140. target_affinity = smccc_get_arg1(vcpu);
  141. lowest_affinity_level = smccc_get_arg2(vcpu);
  142. /* Determine target affinity mask */
  143. target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
  144. if (!target_affinity_mask)
  145. return PSCI_RET_INVALID_PARAMS;
  146. /* Ignore other bits of target affinity */
  147. target_affinity &= target_affinity_mask;
  148. /*
  149. * If one or more VCPU matching target affinity are running
  150. * then ON else OFF
  151. */
  152. kvm_for_each_vcpu(i, tmp, kvm) {
  153. mpidr = kvm_vcpu_get_mpidr_aff(tmp);
  154. if ((mpidr & target_affinity_mask) == target_affinity) {
  155. matching_cpus++;
  156. if (!tmp->arch.power_off)
  157. return PSCI_0_2_AFFINITY_LEVEL_ON;
  158. }
  159. }
  160. if (!matching_cpus)
  161. return PSCI_RET_INVALID_PARAMS;
  162. return PSCI_0_2_AFFINITY_LEVEL_OFF;
  163. }
  164. static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
  165. {
  166. int i;
  167. struct kvm_vcpu *tmp;
  168. /*
  169. * The KVM ABI specifies that a system event exit may call KVM_RUN
  170. * again and may perform shutdown/reboot at a later time that when the
  171. * actual request is made. Since we are implementing PSCI and a
  172. * caller of PSCI reboot and shutdown expects that the system shuts
  173. * down or reboots immediately, let's make sure that VCPUs are not run
  174. * after this call is handled and before the VCPUs have been
  175. * re-initialized.
  176. */
  177. kvm_for_each_vcpu(i, tmp, vcpu->kvm)
  178. tmp->arch.power_off = true;
  179. kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
  180. memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
  181. vcpu->run->system_event.type = type;
  182. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  183. }
  184. static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
  185. {
  186. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
  187. }
  188. static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
  189. {
  190. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
  191. }
  192. static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
  193. {
  194. struct kvm *kvm = vcpu->kvm;
  195. u32 psci_fn = smccc_get_function(vcpu);
  196. unsigned long val;
  197. int ret = 1;
  198. switch (psci_fn) {
  199. case PSCI_0_2_FN_PSCI_VERSION:
  200. /*
  201. * Bits[31:16] = Major Version = 0
  202. * Bits[15:0] = Minor Version = 2
  203. */
  204. val = KVM_ARM_PSCI_0_2;
  205. break;
  206. case PSCI_0_2_FN_CPU_SUSPEND:
  207. case PSCI_0_2_FN64_CPU_SUSPEND:
  208. val = kvm_psci_vcpu_suspend(vcpu);
  209. break;
  210. case PSCI_0_2_FN_CPU_OFF:
  211. kvm_psci_vcpu_off(vcpu);
  212. val = PSCI_RET_SUCCESS;
  213. break;
  214. case PSCI_0_2_FN_CPU_ON:
  215. case PSCI_0_2_FN64_CPU_ON:
  216. mutex_lock(&kvm->lock);
  217. val = kvm_psci_vcpu_on(vcpu);
  218. mutex_unlock(&kvm->lock);
  219. break;
  220. case PSCI_0_2_FN_AFFINITY_INFO:
  221. case PSCI_0_2_FN64_AFFINITY_INFO:
  222. val = kvm_psci_vcpu_affinity_info(vcpu);
  223. break;
  224. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  225. /*
  226. * Trusted OS is MP hence does not require migration
  227. * or
  228. * Trusted OS is not present
  229. */
  230. val = PSCI_0_2_TOS_MP;
  231. break;
  232. case PSCI_0_2_FN_SYSTEM_OFF:
  233. kvm_psci_system_off(vcpu);
  234. /*
  235. * We should'nt be going back to guest VCPU after
  236. * receiving SYSTEM_OFF request.
  237. *
  238. * If user space accidently/deliberately resumes
  239. * guest VCPU after SYSTEM_OFF request then guest
  240. * VCPU should see internal failure from PSCI return
  241. * value. To achieve this, we preload r0 (or x0) with
  242. * PSCI return value INTERNAL_FAILURE.
  243. */
  244. val = PSCI_RET_INTERNAL_FAILURE;
  245. ret = 0;
  246. break;
  247. case PSCI_0_2_FN_SYSTEM_RESET:
  248. kvm_psci_system_reset(vcpu);
  249. /*
  250. * Same reason as SYSTEM_OFF for preloading r0 (or x0)
  251. * with PSCI return value INTERNAL_FAILURE.
  252. */
  253. val = PSCI_RET_INTERNAL_FAILURE;
  254. ret = 0;
  255. break;
  256. default:
  257. val = PSCI_RET_NOT_SUPPORTED;
  258. break;
  259. }
  260. smccc_set_retval(vcpu, val, 0, 0, 0);
  261. return ret;
  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. switch(psci_fn) {
  270. case PSCI_0_2_FN_PSCI_VERSION:
  271. val = KVM_ARM_PSCI_1_0;
  272. break;
  273. case PSCI_1_0_FN_PSCI_FEATURES:
  274. feature = smccc_get_arg1(vcpu);
  275. switch(feature) {
  276. case PSCI_0_2_FN_PSCI_VERSION:
  277. case PSCI_0_2_FN_CPU_SUSPEND:
  278. case PSCI_0_2_FN64_CPU_SUSPEND:
  279. case PSCI_0_2_FN_CPU_OFF:
  280. case PSCI_0_2_FN_CPU_ON:
  281. case PSCI_0_2_FN64_CPU_ON:
  282. case PSCI_0_2_FN_AFFINITY_INFO:
  283. case PSCI_0_2_FN64_AFFINITY_INFO:
  284. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  285. case PSCI_0_2_FN_SYSTEM_OFF:
  286. case PSCI_0_2_FN_SYSTEM_RESET:
  287. case PSCI_1_0_FN_PSCI_FEATURES:
  288. case ARM_SMCCC_VERSION_FUNC_ID:
  289. val = 0;
  290. break;
  291. default:
  292. val = PSCI_RET_NOT_SUPPORTED;
  293. break;
  294. }
  295. break;
  296. default:
  297. return kvm_psci_0_2_call(vcpu);
  298. }
  299. smccc_set_retval(vcpu, val, 0, 0, 0);
  300. return ret;
  301. }
  302. static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
  303. {
  304. struct kvm *kvm = vcpu->kvm;
  305. u32 psci_fn = smccc_get_function(vcpu);
  306. unsigned long val;
  307. switch (psci_fn) {
  308. case KVM_PSCI_FN_CPU_OFF:
  309. kvm_psci_vcpu_off(vcpu);
  310. val = PSCI_RET_SUCCESS;
  311. break;
  312. case KVM_PSCI_FN_CPU_ON:
  313. mutex_lock(&kvm->lock);
  314. val = kvm_psci_vcpu_on(vcpu);
  315. mutex_unlock(&kvm->lock);
  316. break;
  317. default:
  318. val = PSCI_RET_NOT_SUPPORTED;
  319. break;
  320. }
  321. smccc_set_retval(vcpu, val, 0, 0, 0);
  322. return 1;
  323. }
  324. /**
  325. * kvm_psci_call - handle PSCI call if r0 value is in range
  326. * @vcpu: Pointer to the VCPU struct
  327. *
  328. * Handle PSCI calls from guests through traps from HVC instructions.
  329. * The calling convention is similar to SMC calls to the secure world
  330. * where the function number is placed in r0.
  331. *
  332. * This function returns: > 0 (success), 0 (success but exit to user
  333. * space), and < 0 (errors)
  334. *
  335. * Errors:
  336. * -EINVAL: Unrecognized PSCI function
  337. */
  338. static int kvm_psci_call(struct kvm_vcpu *vcpu)
  339. {
  340. switch (kvm_psci_version(vcpu, vcpu->kvm)) {
  341. case KVM_ARM_PSCI_1_0:
  342. return kvm_psci_1_0_call(vcpu);
  343. case KVM_ARM_PSCI_0_2:
  344. return kvm_psci_0_2_call(vcpu);
  345. case KVM_ARM_PSCI_0_1:
  346. return kvm_psci_0_1_call(vcpu);
  347. default:
  348. return -EINVAL;
  349. };
  350. }
  351. int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
  352. {
  353. u32 func_id = smccc_get_function(vcpu);
  354. u32 val = SMCCC_RET_NOT_SUPPORTED;
  355. u32 feature;
  356. switch (func_id) {
  357. case ARM_SMCCC_VERSION_FUNC_ID:
  358. val = ARM_SMCCC_VERSION_1_1;
  359. break;
  360. case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
  361. feature = smccc_get_arg1(vcpu);
  362. switch(feature) {
  363. case ARM_SMCCC_ARCH_WORKAROUND_1:
  364. if (kvm_arm_harden_branch_predictor())
  365. val = SMCCC_RET_SUCCESS;
  366. break;
  367. case ARM_SMCCC_ARCH_WORKAROUND_2:
  368. switch (kvm_arm_have_ssbd()) {
  369. case KVM_SSBD_FORCE_DISABLE:
  370. case KVM_SSBD_UNKNOWN:
  371. break;
  372. case KVM_SSBD_KERNEL:
  373. val = SMCCC_RET_SUCCESS;
  374. break;
  375. case KVM_SSBD_FORCE_ENABLE:
  376. case KVM_SSBD_MITIGATED:
  377. val = SMCCC_RET_NOT_REQUIRED;
  378. break;
  379. }
  380. break;
  381. }
  382. break;
  383. default:
  384. return kvm_psci_call(vcpu);
  385. }
  386. smccc_set_retval(vcpu, val, 0, 0, 0);
  387. return 1;
  388. }
  389. int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
  390. {
  391. return 1; /* PSCI version */
  392. }
  393. int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  394. {
  395. if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices))
  396. return -EFAULT;
  397. return 0;
  398. }
  399. int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  400. {
  401. if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
  402. void __user *uaddr = (void __user *)(long)reg->addr;
  403. u64 val;
  404. val = kvm_psci_version(vcpu, vcpu->kvm);
  405. if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
  406. return -EFAULT;
  407. return 0;
  408. }
  409. return -EINVAL;
  410. }
  411. int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  412. {
  413. if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
  414. void __user *uaddr = (void __user *)(long)reg->addr;
  415. bool wants_02;
  416. u64 val;
  417. if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
  418. return -EFAULT;
  419. wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
  420. switch (val) {
  421. case KVM_ARM_PSCI_0_1:
  422. if (wants_02)
  423. return -EINVAL;
  424. vcpu->kvm->arch.psci_version = val;
  425. return 0;
  426. case KVM_ARM_PSCI_0_2:
  427. case KVM_ARM_PSCI_1_0:
  428. if (!wants_02)
  429. return -EINVAL;
  430. vcpu->kvm->arch.psci_version = val;
  431. return 0;
  432. }
  433. }
  434. return -EINVAL;
  435. }