psci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. return PSCI_RET_SUCCESS;
  81. }
  82. static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
  83. {
  84. vcpu->arch.power_off = true;
  85. }
  86. static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  87. {
  88. struct kvm *kvm = source_vcpu->kvm;
  89. struct kvm_vcpu *vcpu = NULL;
  90. struct swait_queue_head *wq;
  91. unsigned long cpu_id;
  92. unsigned long context_id;
  93. phys_addr_t target_pc;
  94. cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
  95. if (vcpu_mode_is_32bit(source_vcpu))
  96. cpu_id &= ~((u32) 0);
  97. vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
  98. /*
  99. * Make sure the caller requested a valid CPU and that the CPU is
  100. * turned off.
  101. */
  102. if (!vcpu)
  103. return PSCI_RET_INVALID_PARAMS;
  104. if (!vcpu->arch.power_off) {
  105. if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
  106. return PSCI_RET_ALREADY_ON;
  107. else
  108. return PSCI_RET_INVALID_PARAMS;
  109. }
  110. target_pc = smccc_get_arg2(source_vcpu);
  111. context_id = smccc_get_arg3(source_vcpu);
  112. kvm_reset_vcpu(vcpu);
  113. /* Gracefully handle Thumb2 entry point */
  114. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  115. target_pc &= ~((phys_addr_t) 1);
  116. vcpu_set_thumb(vcpu);
  117. }
  118. /* Propagate caller endianness */
  119. if (kvm_vcpu_is_be(source_vcpu))
  120. kvm_vcpu_set_be(vcpu);
  121. *vcpu_pc(vcpu) = target_pc;
  122. /*
  123. * NOTE: We always update r0 (or x0) because for PSCI v0.1
  124. * the general puspose registers are undefined upon CPU_ON.
  125. */
  126. smccc_set_retval(vcpu, context_id, 0, 0, 0);
  127. vcpu->arch.power_off = false;
  128. smp_mb(); /* Make sure the above is visible */
  129. wq = kvm_arch_vcpu_wq(vcpu);
  130. swake_up(wq);
  131. return PSCI_RET_SUCCESS;
  132. }
  133. static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
  134. {
  135. int i, matching_cpus = 0;
  136. unsigned long mpidr;
  137. unsigned long target_affinity;
  138. unsigned long target_affinity_mask;
  139. unsigned long lowest_affinity_level;
  140. struct kvm *kvm = vcpu->kvm;
  141. struct kvm_vcpu *tmp;
  142. target_affinity = smccc_get_arg1(vcpu);
  143. lowest_affinity_level = smccc_get_arg2(vcpu);
  144. /* Determine target affinity mask */
  145. target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
  146. if (!target_affinity_mask)
  147. return PSCI_RET_INVALID_PARAMS;
  148. /* Ignore other bits of target affinity */
  149. target_affinity &= target_affinity_mask;
  150. /*
  151. * If one or more VCPU matching target affinity are running
  152. * then ON else OFF
  153. */
  154. kvm_for_each_vcpu(i, tmp, kvm) {
  155. mpidr = kvm_vcpu_get_mpidr_aff(tmp);
  156. if ((mpidr & target_affinity_mask) == target_affinity) {
  157. matching_cpus++;
  158. if (!tmp->arch.power_off)
  159. return PSCI_0_2_AFFINITY_LEVEL_ON;
  160. }
  161. }
  162. if (!matching_cpus)
  163. return PSCI_RET_INVALID_PARAMS;
  164. return PSCI_0_2_AFFINITY_LEVEL_OFF;
  165. }
  166. static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
  167. {
  168. int i;
  169. struct kvm_vcpu *tmp;
  170. /*
  171. * The KVM ABI specifies that a system event exit may call KVM_RUN
  172. * again and may perform shutdown/reboot at a later time that when the
  173. * actual request is made. Since we are implementing PSCI and a
  174. * caller of PSCI reboot and shutdown expects that the system shuts
  175. * down or reboots immediately, let's make sure that VCPUs are not run
  176. * after this call is handled and before the VCPUs have been
  177. * re-initialized.
  178. */
  179. kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
  180. tmp->arch.power_off = true;
  181. kvm_vcpu_kick(tmp);
  182. }
  183. memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
  184. vcpu->run->system_event.type = type;
  185. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  186. }
  187. static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
  188. {
  189. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
  190. }
  191. static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
  192. {
  193. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
  194. }
  195. static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
  196. {
  197. struct kvm *kvm = vcpu->kvm;
  198. unsigned long psci_fn = smccc_get_function(vcpu);
  199. unsigned long val;
  200. int ret = 1;
  201. switch (psci_fn) {
  202. case PSCI_0_2_FN_PSCI_VERSION:
  203. /*
  204. * Bits[31:16] = Major Version = 0
  205. * Bits[15:0] = Minor Version = 2
  206. */
  207. val = KVM_ARM_PSCI_0_2;
  208. break;
  209. case PSCI_0_2_FN_CPU_SUSPEND:
  210. case PSCI_0_2_FN64_CPU_SUSPEND:
  211. val = kvm_psci_vcpu_suspend(vcpu);
  212. break;
  213. case PSCI_0_2_FN_CPU_OFF:
  214. kvm_psci_vcpu_off(vcpu);
  215. val = PSCI_RET_SUCCESS;
  216. break;
  217. case PSCI_0_2_FN_CPU_ON:
  218. case PSCI_0_2_FN64_CPU_ON:
  219. mutex_lock(&kvm->lock);
  220. val = kvm_psci_vcpu_on(vcpu);
  221. mutex_unlock(&kvm->lock);
  222. break;
  223. case PSCI_0_2_FN_AFFINITY_INFO:
  224. case PSCI_0_2_FN64_AFFINITY_INFO:
  225. val = kvm_psci_vcpu_affinity_info(vcpu);
  226. break;
  227. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  228. /*
  229. * Trusted OS is MP hence does not require migration
  230. * or
  231. * Trusted OS is not present
  232. */
  233. val = PSCI_0_2_TOS_MP;
  234. break;
  235. case PSCI_0_2_FN_SYSTEM_OFF:
  236. kvm_psci_system_off(vcpu);
  237. /*
  238. * We should'nt be going back to guest VCPU after
  239. * receiving SYSTEM_OFF request.
  240. *
  241. * If user space accidently/deliberately resumes
  242. * guest VCPU after SYSTEM_OFF request then guest
  243. * VCPU should see internal failure from PSCI return
  244. * value. To achieve this, we preload r0 (or x0) with
  245. * PSCI return value INTERNAL_FAILURE.
  246. */
  247. val = PSCI_RET_INTERNAL_FAILURE;
  248. ret = 0;
  249. break;
  250. case PSCI_0_2_FN_SYSTEM_RESET:
  251. kvm_psci_system_reset(vcpu);
  252. /*
  253. * Same reason as SYSTEM_OFF for preloading r0 (or x0)
  254. * with PSCI return value INTERNAL_FAILURE.
  255. */
  256. val = PSCI_RET_INTERNAL_FAILURE;
  257. ret = 0;
  258. break;
  259. default:
  260. val = PSCI_RET_NOT_SUPPORTED;
  261. break;
  262. }
  263. smccc_set_retval(vcpu, val, 0, 0, 0);
  264. return ret;
  265. }
  266. static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
  267. {
  268. u32 psci_fn = smccc_get_function(vcpu);
  269. u32 feature;
  270. unsigned long val;
  271. int ret = 1;
  272. switch(psci_fn) {
  273. case PSCI_0_2_FN_PSCI_VERSION:
  274. val = KVM_ARM_PSCI_1_0;
  275. break;
  276. case PSCI_1_0_FN_PSCI_FEATURES:
  277. feature = smccc_get_arg1(vcpu);
  278. switch(feature) {
  279. case PSCI_0_2_FN_PSCI_VERSION:
  280. case PSCI_0_2_FN_CPU_SUSPEND:
  281. case PSCI_0_2_FN64_CPU_SUSPEND:
  282. case PSCI_0_2_FN_CPU_OFF:
  283. case PSCI_0_2_FN_CPU_ON:
  284. case PSCI_0_2_FN64_CPU_ON:
  285. case PSCI_0_2_FN_AFFINITY_INFO:
  286. case PSCI_0_2_FN64_AFFINITY_INFO:
  287. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  288. case PSCI_0_2_FN_SYSTEM_OFF:
  289. case PSCI_0_2_FN_SYSTEM_RESET:
  290. case PSCI_1_0_FN_PSCI_FEATURES:
  291. case ARM_SMCCC_VERSION_FUNC_ID:
  292. val = 0;
  293. break;
  294. default:
  295. val = PSCI_RET_NOT_SUPPORTED;
  296. break;
  297. }
  298. break;
  299. default:
  300. return kvm_psci_0_2_call(vcpu);
  301. }
  302. smccc_set_retval(vcpu, val, 0, 0, 0);
  303. return ret;
  304. }
  305. static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
  306. {
  307. struct kvm *kvm = vcpu->kvm;
  308. unsigned long psci_fn = smccc_get_function(vcpu);
  309. unsigned long val;
  310. switch (psci_fn) {
  311. case KVM_PSCI_FN_CPU_OFF:
  312. kvm_psci_vcpu_off(vcpu);
  313. val = PSCI_RET_SUCCESS;
  314. break;
  315. case KVM_PSCI_FN_CPU_ON:
  316. mutex_lock(&kvm->lock);
  317. val = kvm_psci_vcpu_on(vcpu);
  318. mutex_unlock(&kvm->lock);
  319. break;
  320. default:
  321. val = PSCI_RET_NOT_SUPPORTED;
  322. break;
  323. }
  324. smccc_set_retval(vcpu, val, 0, 0, 0);
  325. return 1;
  326. }
  327. /**
  328. * kvm_psci_call - handle PSCI call if r0 value is in range
  329. * @vcpu: Pointer to the VCPU struct
  330. *
  331. * Handle PSCI calls from guests through traps from HVC instructions.
  332. * The calling convention is similar to SMC calls to the secure world
  333. * where the function number is placed in r0.
  334. *
  335. * This function returns: > 0 (success), 0 (success but exit to user
  336. * space), and < 0 (errors)
  337. *
  338. * Errors:
  339. * -EINVAL: Unrecognized PSCI function
  340. */
  341. static int kvm_psci_call(struct kvm_vcpu *vcpu)
  342. {
  343. switch (kvm_psci_version(vcpu, vcpu->kvm)) {
  344. case KVM_ARM_PSCI_1_0:
  345. return kvm_psci_1_0_call(vcpu);
  346. case KVM_ARM_PSCI_0_2:
  347. return kvm_psci_0_2_call(vcpu);
  348. case KVM_ARM_PSCI_0_1:
  349. return kvm_psci_0_1_call(vcpu);
  350. default:
  351. return -EINVAL;
  352. };
  353. }
  354. int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
  355. {
  356. u32 func_id = smccc_get_function(vcpu);
  357. u32 val = SMCCC_RET_NOT_SUPPORTED;
  358. u32 feature;
  359. switch (func_id) {
  360. case ARM_SMCCC_VERSION_FUNC_ID:
  361. val = ARM_SMCCC_VERSION_1_1;
  362. break;
  363. case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
  364. feature = smccc_get_arg1(vcpu);
  365. switch(feature) {
  366. case ARM_SMCCC_ARCH_WORKAROUND_1:
  367. if (kvm_arm_harden_branch_predictor())
  368. val = SMCCC_RET_SUCCESS;
  369. break;
  370. case ARM_SMCCC_ARCH_WORKAROUND_2:
  371. switch (kvm_arm_have_ssbd()) {
  372. case KVM_SSBD_FORCE_DISABLE:
  373. case KVM_SSBD_UNKNOWN:
  374. break;
  375. case KVM_SSBD_KERNEL:
  376. val = SMCCC_RET_SUCCESS;
  377. break;
  378. case KVM_SSBD_FORCE_ENABLE:
  379. case KVM_SSBD_MITIGATED:
  380. val = SMCCC_RET_NOT_REQUIRED;
  381. break;
  382. }
  383. break;
  384. }
  385. break;
  386. default:
  387. return kvm_psci_call(vcpu);
  388. }
  389. smccc_set_retval(vcpu, val, 0, 0, 0);
  390. return 1;
  391. }
  392. int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
  393. {
  394. return 1; /* PSCI version */
  395. }
  396. int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  397. {
  398. if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices))
  399. return -EFAULT;
  400. return 0;
  401. }
  402. int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  403. {
  404. if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
  405. void __user *uaddr = (void __user *)(long)reg->addr;
  406. u64 val;
  407. val = kvm_psci_version(vcpu, vcpu->kvm);
  408. if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
  409. return -EFAULT;
  410. return 0;
  411. }
  412. return -EINVAL;
  413. }
  414. int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  415. {
  416. if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
  417. void __user *uaddr = (void __user *)(long)reg->addr;
  418. bool wants_02;
  419. u64 val;
  420. if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
  421. return -EFAULT;
  422. wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
  423. switch (val) {
  424. case KVM_ARM_PSCI_0_1:
  425. if (wants_02)
  426. return -EINVAL;
  427. vcpu->kvm->arch.psci_version = val;
  428. return 0;
  429. case KVM_ARM_PSCI_0_2:
  430. case KVM_ARM_PSCI_1_0:
  431. if (!wants_02)
  432. return -EINVAL;
  433. vcpu->kvm->arch.psci_version = val;
  434. return 0;
  435. }
  436. }
  437. return -EINVAL;
  438. }