book3s_64_mmu_host.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. * Kevin Wolf <mail@kevin-wolf.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License, version 2, as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/kvm_host.h>
  22. #include <asm/kvm_ppc.h>
  23. #include <asm/kvm_book3s.h>
  24. #include <asm/mmu-hash64.h>
  25. #include <asm/machdep.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/hw_irq.h>
  28. #include "trace_pr.h"
  29. #include "book3s.h"
  30. #define PTE_SIZE 12
  31. void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  32. {
  33. ppc_md.hpte_invalidate(pte->slot, pte->host_vpn,
  34. pte->pagesize, pte->pagesize, MMU_SEGSIZE_256M,
  35. false);
  36. }
  37. /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
  38. * a hash, so we don't waste cycles on looping */
  39. static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
  40. {
  41. return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
  42. ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
  43. ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
  44. ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
  45. ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
  46. ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
  47. ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
  48. ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
  49. }
  50. static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
  51. {
  52. struct kvmppc_sid_map *map;
  53. u16 sid_map_mask;
  54. if (kvmppc_get_msr(vcpu) & MSR_PR)
  55. gvsid |= VSID_PR;
  56. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  57. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  58. if (map->valid && (map->guest_vsid == gvsid)) {
  59. trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
  60. return map;
  61. }
  62. map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
  63. if (map->valid && (map->guest_vsid == gvsid)) {
  64. trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
  65. return map;
  66. }
  67. trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
  68. return NULL;
  69. }
  70. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
  71. bool iswrite)
  72. {
  73. unsigned long vpn;
  74. pfn_t hpaddr;
  75. ulong hash, hpteg;
  76. u64 vsid;
  77. int ret;
  78. int rflags = 0x192;
  79. int vflags = 0;
  80. int attempt = 0;
  81. struct kvmppc_sid_map *map;
  82. int r = 0;
  83. int hpsize = MMU_PAGE_4K;
  84. bool writable;
  85. unsigned long mmu_seq;
  86. struct kvm *kvm = vcpu->kvm;
  87. struct hpte_cache *cpte;
  88. unsigned long gfn = orig_pte->raddr >> PAGE_SHIFT;
  89. unsigned long pfn;
  90. /* used to check for invalidations in progress */
  91. mmu_seq = kvm->mmu_notifier_seq;
  92. smp_rmb();
  93. /* Get host physical address for gpa */
  94. pfn = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable);
  95. if (is_error_noslot_pfn(pfn)) {
  96. printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n",
  97. orig_pte->raddr);
  98. r = -EINVAL;
  99. goto out;
  100. }
  101. hpaddr = pfn << PAGE_SHIFT;
  102. /* and write the mapping ea -> hpa into the pt */
  103. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  104. map = find_sid_vsid(vcpu, vsid);
  105. if (!map) {
  106. ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
  107. WARN_ON(ret < 0);
  108. map = find_sid_vsid(vcpu, vsid);
  109. }
  110. if (!map) {
  111. printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
  112. vsid, orig_pte->eaddr);
  113. WARN_ON(true);
  114. r = -EINVAL;
  115. goto out;
  116. }
  117. vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M);
  118. kvm_set_pfn_accessed(pfn);
  119. if (!orig_pte->may_write || !writable)
  120. rflags |= PP_RXRX;
  121. else {
  122. mark_page_dirty(vcpu->kvm, gfn);
  123. kvm_set_pfn_dirty(pfn);
  124. }
  125. if (!orig_pte->may_execute)
  126. rflags |= HPTE_R_N;
  127. else
  128. kvmppc_mmu_flush_icache(pfn);
  129. /*
  130. * Use 64K pages if possible; otherwise, on 64K page kernels,
  131. * we need to transfer 4 more bits from guest real to host real addr.
  132. */
  133. if (vsid & VSID_64K)
  134. hpsize = MMU_PAGE_64K;
  135. else
  136. hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
  137. hash = hpt_hash(vpn, mmu_psize_defs[hpsize].shift, MMU_SEGSIZE_256M);
  138. cpte = kvmppc_mmu_hpte_cache_next(vcpu);
  139. spin_lock(&kvm->mmu_lock);
  140. if (!cpte || mmu_notifier_retry(kvm, mmu_seq)) {
  141. r = -EAGAIN;
  142. goto out_unlock;
  143. }
  144. map_again:
  145. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  146. /* In case we tried normal mapping already, let's nuke old entries */
  147. if (attempt > 1)
  148. if (ppc_md.hpte_remove(hpteg) < 0) {
  149. r = -1;
  150. goto out_unlock;
  151. }
  152. ret = ppc_md.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags,
  153. hpsize, hpsize, MMU_SEGSIZE_256M);
  154. if (ret == -1) {
  155. /* If we couldn't map a primary PTE, try a secondary */
  156. hash = ~hash;
  157. vflags ^= HPTE_V_SECONDARY;
  158. attempt++;
  159. goto map_again;
  160. } else if (ret < 0) {
  161. r = -EIO;
  162. goto out_unlock;
  163. } else {
  164. trace_kvm_book3s_64_mmu_map(rflags, hpteg,
  165. vpn, hpaddr, orig_pte);
  166. /* The ppc_md code may give us a secondary entry even though we
  167. asked for a primary. Fix up. */
  168. if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
  169. hash = ~hash;
  170. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  171. }
  172. cpte->slot = hpteg + (ret & 7);
  173. cpte->host_vpn = vpn;
  174. cpte->pte = *orig_pte;
  175. cpte->pfn = pfn;
  176. cpte->pagesize = hpsize;
  177. kvmppc_mmu_hpte_cache_map(vcpu, cpte);
  178. cpte = NULL;
  179. }
  180. out_unlock:
  181. spin_unlock(&kvm->mmu_lock);
  182. kvm_release_pfn_clean(pfn);
  183. if (cpte)
  184. kvmppc_mmu_hpte_cache_free(cpte);
  185. out:
  186. return r;
  187. }
  188. void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
  189. {
  190. u64 mask = 0xfffffffffULL;
  191. u64 vsid;
  192. vcpu->arch.mmu.esid_to_vsid(vcpu, pte->eaddr >> SID_SHIFT, &vsid);
  193. if (vsid & VSID_64K)
  194. mask = 0xffffffff0ULL;
  195. kvmppc_mmu_pte_vflush(vcpu, pte->vpage, mask);
  196. }
  197. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  198. {
  199. struct kvmppc_sid_map *map;
  200. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  201. u16 sid_map_mask;
  202. static int backwards_map = 0;
  203. if (kvmppc_get_msr(vcpu) & MSR_PR)
  204. gvsid |= VSID_PR;
  205. /* We might get collisions that trap in preceding order, so let's
  206. map them differently */
  207. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  208. if (backwards_map)
  209. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  210. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  211. /* Make sure we're taking the other map next time */
  212. backwards_map = !backwards_map;
  213. /* Uh-oh ... out of mappings. Let's flush! */
  214. if (vcpu_book3s->proto_vsid_next == vcpu_book3s->proto_vsid_max) {
  215. vcpu_book3s->proto_vsid_next = vcpu_book3s->proto_vsid_first;
  216. memset(vcpu_book3s->sid_map, 0,
  217. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  218. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  219. kvmppc_mmu_flush_segments(vcpu);
  220. }
  221. map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++, 256M);
  222. map->guest_vsid = gvsid;
  223. map->valid = true;
  224. trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
  225. return map;
  226. }
  227. static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
  228. {
  229. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  230. int i;
  231. int max_slb_size = 64;
  232. int found_inval = -1;
  233. int r;
  234. /* Are we overwriting? */
  235. for (i = 0; i < svcpu->slb_max; i++) {
  236. if (!(svcpu->slb[i].esid & SLB_ESID_V))
  237. found_inval = i;
  238. else if ((svcpu->slb[i].esid & ESID_MASK) == esid) {
  239. r = i;
  240. goto out;
  241. }
  242. }
  243. /* Found a spare entry that was invalidated before */
  244. if (found_inval >= 0) {
  245. r = found_inval;
  246. goto out;
  247. }
  248. /* No spare invalid entry, so create one */
  249. if (mmu_slb_size < 64)
  250. max_slb_size = mmu_slb_size;
  251. /* Overflowing -> purge */
  252. if ((svcpu->slb_max) == max_slb_size)
  253. kvmppc_mmu_flush_segments(vcpu);
  254. r = svcpu->slb_max;
  255. svcpu->slb_max++;
  256. out:
  257. svcpu_put(svcpu);
  258. return r;
  259. }
  260. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  261. {
  262. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  263. u64 esid = eaddr >> SID_SHIFT;
  264. u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
  265. u64 slb_vsid = SLB_VSID_USER;
  266. u64 gvsid;
  267. int slb_index;
  268. struct kvmppc_sid_map *map;
  269. int r = 0;
  270. slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
  271. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  272. /* Invalidate an entry */
  273. svcpu->slb[slb_index].esid = 0;
  274. r = -ENOENT;
  275. goto out;
  276. }
  277. map = find_sid_vsid(vcpu, gvsid);
  278. if (!map)
  279. map = create_sid_map(vcpu, gvsid);
  280. map->guest_esid = esid;
  281. slb_vsid |= (map->host_vsid << 12);
  282. slb_vsid &= ~SLB_VSID_KP;
  283. slb_esid |= slb_index;
  284. #ifdef CONFIG_PPC_64K_PAGES
  285. /* Set host segment base page size to 64K if possible */
  286. if (gvsid & VSID_64K)
  287. slb_vsid |= mmu_psize_defs[MMU_PAGE_64K].sllp;
  288. #endif
  289. svcpu->slb[slb_index].esid = slb_esid;
  290. svcpu->slb[slb_index].vsid = slb_vsid;
  291. trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
  292. out:
  293. svcpu_put(svcpu);
  294. return r;
  295. }
  296. void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong ea, ulong seg_size)
  297. {
  298. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  299. ulong seg_mask = -seg_size;
  300. int i;
  301. for (i = 0; i < svcpu->slb_max; i++) {
  302. if ((svcpu->slb[i].esid & SLB_ESID_V) &&
  303. (svcpu->slb[i].esid & seg_mask) == ea) {
  304. /* Invalidate this entry */
  305. svcpu->slb[i].esid = 0;
  306. }
  307. }
  308. svcpu_put(svcpu);
  309. }
  310. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  311. {
  312. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  313. svcpu->slb_max = 0;
  314. svcpu->slb[0].esid = 0;
  315. svcpu_put(svcpu);
  316. }
  317. void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
  318. {
  319. kvmppc_mmu_hpte_destroy(vcpu);
  320. __destroy_context(to_book3s(vcpu)->context_id[0]);
  321. }
  322. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  323. {
  324. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  325. int err;
  326. err = __init_new_context();
  327. if (err < 0)
  328. return -1;
  329. vcpu3s->context_id[0] = err;
  330. vcpu3s->proto_vsid_max = ((u64)(vcpu3s->context_id[0] + 1)
  331. << ESID_BITS) - 1;
  332. vcpu3s->proto_vsid_first = (u64)vcpu3s->context_id[0] << ESID_BITS;
  333. vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first;
  334. kvmppc_mmu_hpte_init(vcpu);
  335. return 0;
  336. }