psci.c 12 KB

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