book3s_pr_papr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (C) 2011. Freescale Inc. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. * Paul Mackerras <paulus@samba.org>
  7. *
  8. * Description:
  9. *
  10. * Hypercall handling for running PAPR guests in PR KVM on Book 3S
  11. * processors.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License, version 2, as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/anon_inodes.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/kvm_ppc.h>
  20. #include <asm/kvm_book3s.h>
  21. #define HPTE_SIZE 16 /* bytes per HPT entry */
  22. static unsigned long get_pteg_addr(struct kvm_vcpu *vcpu, long pte_index)
  23. {
  24. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  25. unsigned long pteg_addr;
  26. pte_index <<= 4;
  27. pte_index &= ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1) << 7 | 0x70;
  28. pteg_addr = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
  29. pteg_addr |= pte_index;
  30. return pteg_addr;
  31. }
  32. static int kvmppc_h_pr_enter(struct kvm_vcpu *vcpu)
  33. {
  34. long flags = kvmppc_get_gpr(vcpu, 4);
  35. long pte_index = kvmppc_get_gpr(vcpu, 5);
  36. __be64 pteg[2 * 8];
  37. __be64 *hpte;
  38. unsigned long pteg_addr, i;
  39. long int ret;
  40. i = pte_index & 7;
  41. pte_index &= ~7UL;
  42. pteg_addr = get_pteg_addr(vcpu, pte_index);
  43. mutex_lock(&vcpu->kvm->arch.hpt_mutex);
  44. ret = H_FUNCTION;
  45. if (copy_from_user(pteg, (void __user *)pteg_addr, sizeof(pteg)))
  46. goto done;
  47. hpte = pteg;
  48. ret = H_PTEG_FULL;
  49. if (likely((flags & H_EXACT) == 0)) {
  50. for (i = 0; ; ++i) {
  51. if (i == 8)
  52. goto done;
  53. if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0)
  54. break;
  55. hpte += 2;
  56. }
  57. } else {
  58. hpte += i * 2;
  59. if (*hpte & HPTE_V_VALID)
  60. goto done;
  61. }
  62. hpte[0] = cpu_to_be64(kvmppc_get_gpr(vcpu, 6));
  63. hpte[1] = cpu_to_be64(kvmppc_get_gpr(vcpu, 7));
  64. pteg_addr += i * HPTE_SIZE;
  65. ret = H_FUNCTION;
  66. if (copy_to_user((void __user *)pteg_addr, hpte, HPTE_SIZE))
  67. goto done;
  68. kvmppc_set_gpr(vcpu, 4, pte_index | i);
  69. ret = H_SUCCESS;
  70. done:
  71. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  72. kvmppc_set_gpr(vcpu, 3, ret);
  73. return EMULATE_DONE;
  74. }
  75. static int kvmppc_h_pr_remove(struct kvm_vcpu *vcpu)
  76. {
  77. unsigned long flags= kvmppc_get_gpr(vcpu, 4);
  78. unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
  79. unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
  80. unsigned long v = 0, pteg, rb;
  81. unsigned long pte[2];
  82. long int ret;
  83. pteg = get_pteg_addr(vcpu, pte_index);
  84. mutex_lock(&vcpu->kvm->arch.hpt_mutex);
  85. ret = H_FUNCTION;
  86. if (copy_from_user(pte, (void __user *)pteg, sizeof(pte)))
  87. goto done;
  88. pte[0] = be64_to_cpu((__force __be64)pte[0]);
  89. pte[1] = be64_to_cpu((__force __be64)pte[1]);
  90. ret = H_NOT_FOUND;
  91. if ((pte[0] & HPTE_V_VALID) == 0 ||
  92. ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn) ||
  93. ((flags & H_ANDCOND) && (pte[0] & avpn) != 0))
  94. goto done;
  95. ret = H_FUNCTION;
  96. if (copy_to_user((void __user *)pteg, &v, sizeof(v)))
  97. goto done;
  98. rb = compute_tlbie_rb(pte[0], pte[1], pte_index);
  99. vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
  100. ret = H_SUCCESS;
  101. kvmppc_set_gpr(vcpu, 4, pte[0]);
  102. kvmppc_set_gpr(vcpu, 5, pte[1]);
  103. done:
  104. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  105. kvmppc_set_gpr(vcpu, 3, ret);
  106. return EMULATE_DONE;
  107. }
  108. /* Request defs for kvmppc_h_pr_bulk_remove() */
  109. #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
  110. #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
  111. #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
  112. #define H_BULK_REMOVE_END 0xc000000000000000ULL
  113. #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
  114. #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
  115. #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
  116. #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
  117. #define H_BULK_REMOVE_HW 0x3000000000000000ULL
  118. #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
  119. #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
  120. #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
  121. #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
  122. #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
  123. #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
  124. #define H_BULK_REMOVE_MAX_BATCH 4
  125. static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu)
  126. {
  127. int i;
  128. int paramnr = 4;
  129. int ret = H_SUCCESS;
  130. mutex_lock(&vcpu->kvm->arch.hpt_mutex);
  131. for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
  132. unsigned long tsh = kvmppc_get_gpr(vcpu, paramnr+(2*i));
  133. unsigned long tsl = kvmppc_get_gpr(vcpu, paramnr+(2*i)+1);
  134. unsigned long pteg, rb, flags;
  135. unsigned long pte[2];
  136. unsigned long v = 0;
  137. if ((tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
  138. break; /* Exit success */
  139. } else if ((tsh & H_BULK_REMOVE_TYPE) !=
  140. H_BULK_REMOVE_REQUEST) {
  141. ret = H_PARAMETER;
  142. break; /* Exit fail */
  143. }
  144. tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
  145. tsh |= H_BULK_REMOVE_RESPONSE;
  146. if ((tsh & H_BULK_REMOVE_ANDCOND) &&
  147. (tsh & H_BULK_REMOVE_AVPN)) {
  148. tsh |= H_BULK_REMOVE_PARM;
  149. kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
  150. ret = H_PARAMETER;
  151. break; /* Exit fail */
  152. }
  153. pteg = get_pteg_addr(vcpu, tsh & H_BULK_REMOVE_PTEX);
  154. if (copy_from_user(pte, (void __user *)pteg, sizeof(pte))) {
  155. ret = H_FUNCTION;
  156. break;
  157. }
  158. pte[0] = be64_to_cpu((__force __be64)pte[0]);
  159. pte[1] = be64_to_cpu((__force __be64)pte[1]);
  160. /* tsl = AVPN */
  161. flags = (tsh & H_BULK_REMOVE_FLAGS) >> 26;
  162. if ((pte[0] & HPTE_V_VALID) == 0 ||
  163. ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != tsl) ||
  164. ((flags & H_ANDCOND) && (pte[0] & tsl) != 0)) {
  165. tsh |= H_BULK_REMOVE_NOT_FOUND;
  166. } else {
  167. /* Splat the pteg in (userland) hpt */
  168. if (copy_to_user((void __user *)pteg, &v, sizeof(v))) {
  169. ret = H_FUNCTION;
  170. break;
  171. }
  172. rb = compute_tlbie_rb(pte[0], pte[1],
  173. tsh & H_BULK_REMOVE_PTEX);
  174. vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
  175. tsh |= H_BULK_REMOVE_SUCCESS;
  176. tsh |= (pte[1] & (HPTE_R_C | HPTE_R_R)) << 43;
  177. }
  178. kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
  179. }
  180. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  181. kvmppc_set_gpr(vcpu, 3, ret);
  182. return EMULATE_DONE;
  183. }
  184. static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu)
  185. {
  186. unsigned long flags = kvmppc_get_gpr(vcpu, 4);
  187. unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
  188. unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
  189. unsigned long rb, pteg, r, v;
  190. unsigned long pte[2];
  191. long int ret;
  192. pteg = get_pteg_addr(vcpu, pte_index);
  193. mutex_lock(&vcpu->kvm->arch.hpt_mutex);
  194. ret = H_FUNCTION;
  195. if (copy_from_user(pte, (void __user *)pteg, sizeof(pte)))
  196. goto done;
  197. pte[0] = be64_to_cpu((__force __be64)pte[0]);
  198. pte[1] = be64_to_cpu((__force __be64)pte[1]);
  199. ret = H_NOT_FOUND;
  200. if ((pte[0] & HPTE_V_VALID) == 0 ||
  201. ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn))
  202. goto done;
  203. v = pte[0];
  204. r = pte[1];
  205. r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_HI |
  206. HPTE_R_KEY_LO);
  207. r |= (flags << 55) & HPTE_R_PP0;
  208. r |= (flags << 48) & HPTE_R_KEY_HI;
  209. r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
  210. pte[1] = r;
  211. rb = compute_tlbie_rb(v, r, pte_index);
  212. vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
  213. pte[0] = (__force u64)cpu_to_be64(pte[0]);
  214. pte[1] = (__force u64)cpu_to_be64(pte[1]);
  215. ret = H_FUNCTION;
  216. if (copy_to_user((void __user *)pteg, pte, sizeof(pte)))
  217. goto done;
  218. ret = H_SUCCESS;
  219. done:
  220. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  221. kvmppc_set_gpr(vcpu, 3, ret);
  222. return EMULATE_DONE;
  223. }
  224. static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)
  225. {
  226. unsigned long liobn = kvmppc_get_gpr(vcpu, 4);
  227. unsigned long ioba = kvmppc_get_gpr(vcpu, 5);
  228. unsigned long tce = kvmppc_get_gpr(vcpu, 6);
  229. long rc;
  230. rc = kvmppc_h_put_tce(vcpu, liobn, ioba, tce);
  231. if (rc == H_TOO_HARD)
  232. return EMULATE_FAIL;
  233. kvmppc_set_gpr(vcpu, 3, rc);
  234. return EMULATE_DONE;
  235. }
  236. static int kvmppc_h_pr_logical_ci_load(struct kvm_vcpu *vcpu)
  237. {
  238. long rc;
  239. rc = kvmppc_h_logical_ci_load(vcpu);
  240. if (rc == H_TOO_HARD)
  241. return EMULATE_FAIL;
  242. kvmppc_set_gpr(vcpu, 3, rc);
  243. return EMULATE_DONE;
  244. }
  245. static int kvmppc_h_pr_logical_ci_store(struct kvm_vcpu *vcpu)
  246. {
  247. long rc;
  248. rc = kvmppc_h_logical_ci_store(vcpu);
  249. if (rc == H_TOO_HARD)
  250. return EMULATE_FAIL;
  251. kvmppc_set_gpr(vcpu, 3, rc);
  252. return EMULATE_DONE;
  253. }
  254. static int kvmppc_h_pr_put_tce_indirect(struct kvm_vcpu *vcpu)
  255. {
  256. unsigned long liobn = kvmppc_get_gpr(vcpu, 4);
  257. unsigned long ioba = kvmppc_get_gpr(vcpu, 5);
  258. unsigned long tce = kvmppc_get_gpr(vcpu, 6);
  259. unsigned long npages = kvmppc_get_gpr(vcpu, 7);
  260. long rc;
  261. rc = kvmppc_h_put_tce_indirect(vcpu, liobn, ioba,
  262. tce, npages);
  263. if (rc == H_TOO_HARD)
  264. return EMULATE_FAIL;
  265. kvmppc_set_gpr(vcpu, 3, rc);
  266. return EMULATE_DONE;
  267. }
  268. static int kvmppc_h_pr_stuff_tce(struct kvm_vcpu *vcpu)
  269. {
  270. unsigned long liobn = kvmppc_get_gpr(vcpu, 4);
  271. unsigned long ioba = kvmppc_get_gpr(vcpu, 5);
  272. unsigned long tce_value = kvmppc_get_gpr(vcpu, 6);
  273. unsigned long npages = kvmppc_get_gpr(vcpu, 7);
  274. long rc;
  275. rc = kvmppc_h_stuff_tce(vcpu, liobn, ioba, tce_value, npages);
  276. if (rc == H_TOO_HARD)
  277. return EMULATE_FAIL;
  278. kvmppc_set_gpr(vcpu, 3, rc);
  279. return EMULATE_DONE;
  280. }
  281. static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
  282. {
  283. long rc = kvmppc_xics_hcall(vcpu, cmd);
  284. kvmppc_set_gpr(vcpu, 3, rc);
  285. return EMULATE_DONE;
  286. }
  287. int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
  288. {
  289. int rc, idx;
  290. if (cmd <= MAX_HCALL_OPCODE &&
  291. !test_bit(cmd/4, vcpu->kvm->arch.enabled_hcalls))
  292. return EMULATE_FAIL;
  293. switch (cmd) {
  294. case H_ENTER:
  295. return kvmppc_h_pr_enter(vcpu);
  296. case H_REMOVE:
  297. return kvmppc_h_pr_remove(vcpu);
  298. case H_PROTECT:
  299. return kvmppc_h_pr_protect(vcpu);
  300. case H_BULK_REMOVE:
  301. return kvmppc_h_pr_bulk_remove(vcpu);
  302. case H_PUT_TCE:
  303. return kvmppc_h_pr_put_tce(vcpu);
  304. case H_PUT_TCE_INDIRECT:
  305. return kvmppc_h_pr_put_tce_indirect(vcpu);
  306. case H_STUFF_TCE:
  307. return kvmppc_h_pr_stuff_tce(vcpu);
  308. case H_CEDE:
  309. kvmppc_set_msr_fast(vcpu, kvmppc_get_msr(vcpu) | MSR_EE);
  310. kvm_vcpu_block(vcpu);
  311. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  312. vcpu->stat.halt_wakeup++;
  313. return EMULATE_DONE;
  314. case H_LOGICAL_CI_LOAD:
  315. return kvmppc_h_pr_logical_ci_load(vcpu);
  316. case H_LOGICAL_CI_STORE:
  317. return kvmppc_h_pr_logical_ci_store(vcpu);
  318. case H_XIRR:
  319. case H_CPPR:
  320. case H_EOI:
  321. case H_IPI:
  322. case H_IPOLL:
  323. case H_XIRR_X:
  324. if (kvmppc_xics_enabled(vcpu))
  325. return kvmppc_h_pr_xics_hcall(vcpu, cmd);
  326. break;
  327. case H_RTAS:
  328. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  329. break;
  330. idx = srcu_read_lock(&vcpu->kvm->srcu);
  331. rc = kvmppc_rtas_hcall(vcpu);
  332. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  333. if (rc)
  334. break;
  335. kvmppc_set_gpr(vcpu, 3, 0);
  336. return EMULATE_DONE;
  337. }
  338. return EMULATE_FAIL;
  339. }
  340. int kvmppc_hcall_impl_pr(unsigned long cmd)
  341. {
  342. switch (cmd) {
  343. case H_ENTER:
  344. case H_REMOVE:
  345. case H_PROTECT:
  346. case H_BULK_REMOVE:
  347. case H_PUT_TCE:
  348. case H_CEDE:
  349. case H_LOGICAL_CI_LOAD:
  350. case H_LOGICAL_CI_STORE:
  351. #ifdef CONFIG_KVM_XICS
  352. case H_XIRR:
  353. case H_CPPR:
  354. case H_EOI:
  355. case H_IPI:
  356. case H_IPOLL:
  357. case H_XIRR_X:
  358. #endif
  359. return 1;
  360. }
  361. return 0;
  362. }
  363. /*
  364. * List of hcall numbers to enable by default.
  365. * For compatibility with old userspace, we enable by default
  366. * all hcalls that were implemented before the hcall-enabling
  367. * facility was added. Note this list should not include H_RTAS.
  368. */
  369. static unsigned int default_hcall_list[] = {
  370. H_ENTER,
  371. H_REMOVE,
  372. H_PROTECT,
  373. H_BULK_REMOVE,
  374. H_PUT_TCE,
  375. H_CEDE,
  376. #ifdef CONFIG_KVM_XICS
  377. H_XIRR,
  378. H_CPPR,
  379. H_EOI,
  380. H_IPI,
  381. H_IPOLL,
  382. H_XIRR_X,
  383. #endif
  384. 0
  385. };
  386. void kvmppc_pr_init_default_hcalls(struct kvm *kvm)
  387. {
  388. int i;
  389. unsigned int hcall;
  390. for (i = 0; default_hcall_list[i]; ++i) {
  391. hcall = default_hcall_list[i];
  392. WARN_ON(!kvmppc_hcall_impl_pr(hcall));
  393. __set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  394. }
  395. }