book3s_64_mmu_host.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #define PTE_SIZE 12
  29. #define VSID_ALL 0
  30. /* #define DEBUG_MMU */
  31. /* #define DEBUG_SLB */
  32. #ifdef DEBUG_MMU
  33. #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  34. #else
  35. #define dprintk_mmu(a, ...) do { } while(0)
  36. #endif
  37. #ifdef DEBUG_SLB
  38. #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  39. #else
  40. #define dprintk_slb(a, ...) do { } while(0)
  41. #endif
  42. static void invalidate_pte(struct hpte_cache *pte)
  43. {
  44. dprintk_mmu("KVM: Flushing SPT %d: 0x%llx (0x%llx) -> 0x%llx\n",
  45. i, pte->pte.eaddr, pte->pte.vpage, pte->host_va);
  46. ppc_md.hpte_invalidate(pte->slot, pte->host_va,
  47. MMU_PAGE_4K, MMU_SEGSIZE_256M,
  48. false);
  49. pte->host_va = 0;
  50. kvm_release_pfn_dirty(pte->pfn);
  51. }
  52. void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, u64 guest_ea, u64 ea_mask)
  53. {
  54. int i;
  55. dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%llx & 0x%llx\n",
  56. vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
  57. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  58. guest_ea &= ea_mask;
  59. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  60. struct hpte_cache *pte;
  61. pte = &vcpu->arch.hpte_cache[i];
  62. if (!pte->host_va)
  63. continue;
  64. if ((pte->pte.eaddr & ea_mask) == guest_ea) {
  65. invalidate_pte(pte);
  66. }
  67. }
  68. /* Doing a complete flush -> start from scratch */
  69. if (!ea_mask)
  70. vcpu->arch.hpte_cache_offset = 0;
  71. }
  72. void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
  73. {
  74. int i;
  75. dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
  76. vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
  77. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  78. guest_vp &= vp_mask;
  79. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  80. struct hpte_cache *pte;
  81. pte = &vcpu->arch.hpte_cache[i];
  82. if (!pte->host_va)
  83. continue;
  84. if ((pte->pte.vpage & vp_mask) == guest_vp) {
  85. invalidate_pte(pte);
  86. }
  87. }
  88. }
  89. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, u64 pa_start, u64 pa_end)
  90. {
  91. int i;
  92. dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
  93. vcpu->arch.hpte_cache_offset, guest_pa, pa_mask);
  94. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  95. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  96. struct hpte_cache *pte;
  97. pte = &vcpu->arch.hpte_cache[i];
  98. if (!pte->host_va)
  99. continue;
  100. if ((pte->pte.raddr >= pa_start) &&
  101. (pte->pte.raddr < pa_end)) {
  102. invalidate_pte(pte);
  103. }
  104. }
  105. }
  106. struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
  107. {
  108. int i;
  109. u64 guest_vp;
  110. guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
  111. for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
  112. struct hpte_cache *pte;
  113. pte = &vcpu->arch.hpte_cache[i];
  114. if (!pte->host_va)
  115. continue;
  116. if (pte->pte.vpage == guest_vp)
  117. return &pte->pte;
  118. }
  119. return NULL;
  120. }
  121. static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
  122. {
  123. if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
  124. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  125. return vcpu->arch.hpte_cache_offset++;
  126. }
  127. /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
  128. * a hash, so we don't waste cycles on looping */
  129. static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
  130. {
  131. return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
  132. ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
  133. ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
  134. ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
  135. ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
  136. ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
  137. ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
  138. ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
  139. }
  140. static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
  141. {
  142. struct kvmppc_sid_map *map;
  143. u16 sid_map_mask;
  144. if (vcpu->arch.msr & MSR_PR)
  145. gvsid |= VSID_PR;
  146. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  147. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  148. if (map->guest_vsid == gvsid) {
  149. dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
  150. gvsid, map->host_vsid);
  151. return map;
  152. }
  153. map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
  154. if (map->guest_vsid == gvsid) {
  155. dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
  156. gvsid, map->host_vsid);
  157. return map;
  158. }
  159. dprintk_slb("SLB: Searching 0x%llx -> not found\n", gvsid);
  160. return NULL;
  161. }
  162. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
  163. {
  164. pfn_t hpaddr;
  165. ulong hash, hpteg, va;
  166. u64 vsid;
  167. int ret;
  168. int rflags = 0x192;
  169. int vflags = 0;
  170. int attempt = 0;
  171. struct kvmppc_sid_map *map;
  172. /* Get host physical address for gpa */
  173. hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  174. if (kvm_is_error_hva(hpaddr)) {
  175. printk(KERN_INFO "Couldn't get guest page for gfn %llx!\n", orig_pte->eaddr);
  176. return -EINVAL;
  177. }
  178. hpaddr <<= PAGE_SHIFT;
  179. #if PAGE_SHIFT == 12
  180. #elif PAGE_SHIFT == 16
  181. hpaddr |= orig_pte->raddr & 0xf000;
  182. #else
  183. #error Unknown page size
  184. #endif
  185. /* and write the mapping ea -> hpa into the pt */
  186. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  187. map = find_sid_vsid(vcpu, vsid);
  188. if (!map) {
  189. kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
  190. map = find_sid_vsid(vcpu, vsid);
  191. }
  192. BUG_ON(!map);
  193. vsid = map->host_vsid;
  194. va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
  195. if (!orig_pte->may_write)
  196. rflags |= HPTE_R_PP;
  197. else
  198. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  199. if (!orig_pte->may_execute)
  200. rflags |= HPTE_R_N;
  201. hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
  202. map_again:
  203. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  204. /* In case we tried normal mapping already, let's nuke old entries */
  205. if (attempt > 1)
  206. if (ppc_md.hpte_remove(hpteg) < 0)
  207. return -1;
  208. ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
  209. if (ret < 0) {
  210. /* If we couldn't map a primary PTE, try a secondary */
  211. #ifdef USE_SECONDARY
  212. hash = ~hash;
  213. attempt++;
  214. if (attempt % 2)
  215. vflags = HPTE_V_SECONDARY;
  216. else
  217. vflags = 0;
  218. #else
  219. attempt = 2;
  220. #endif
  221. goto map_again;
  222. } else {
  223. int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
  224. struct hpte_cache *pte = &vcpu->arch.hpte_cache[hpte_id];
  225. dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%lx (0x%llx) -> %lx\n",
  226. ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
  227. (rflags & HPTE_R_N) ? '-' : 'x',
  228. orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
  229. pte->slot = hpteg + (ret & 7);
  230. pte->host_va = va;
  231. pte->pte = *orig_pte;
  232. pte->pfn = hpaddr >> PAGE_SHIFT;
  233. }
  234. return 0;
  235. }
  236. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  237. {
  238. struct kvmppc_sid_map *map;
  239. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  240. u16 sid_map_mask;
  241. static int backwards_map = 0;
  242. if (vcpu->arch.msr & MSR_PR)
  243. gvsid |= VSID_PR;
  244. /* We might get collisions that trap in preceding order, so let's
  245. map them differently */
  246. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  247. if (backwards_map)
  248. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  249. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  250. /* Make sure we're taking the other map next time */
  251. backwards_map = !backwards_map;
  252. /* Uh-oh ... out of mappings. Let's flush! */
  253. if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
  254. vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
  255. memset(vcpu_book3s->sid_map, 0,
  256. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  257. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  258. kvmppc_mmu_flush_segments(vcpu);
  259. }
  260. map->host_vsid = vcpu_book3s->vsid_next++;
  261. map->guest_vsid = gvsid;
  262. map->valid = true;
  263. return map;
  264. }
  265. static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
  266. {
  267. int i;
  268. int max_slb_size = 64;
  269. int found_inval = -1;
  270. int r;
  271. if (!get_paca()->kvm_slb_max)
  272. get_paca()->kvm_slb_max = 1;
  273. /* Are we overwriting? */
  274. for (i = 1; i < get_paca()->kvm_slb_max; i++) {
  275. if (!(get_paca()->kvm_slb[i].esid & SLB_ESID_V))
  276. found_inval = i;
  277. else if ((get_paca()->kvm_slb[i].esid & ESID_MASK) == esid)
  278. return i;
  279. }
  280. /* Found a spare entry that was invalidated before */
  281. if (found_inval > 0)
  282. return found_inval;
  283. /* No spare invalid entry, so create one */
  284. if (mmu_slb_size < 64)
  285. max_slb_size = mmu_slb_size;
  286. /* Overflowing -> purge */
  287. if ((get_paca()->kvm_slb_max) == max_slb_size)
  288. kvmppc_mmu_flush_segments(vcpu);
  289. r = get_paca()->kvm_slb_max;
  290. get_paca()->kvm_slb_max++;
  291. return r;
  292. }
  293. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  294. {
  295. u64 esid = eaddr >> SID_SHIFT;
  296. u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
  297. u64 slb_vsid = SLB_VSID_USER;
  298. u64 gvsid;
  299. int slb_index;
  300. struct kvmppc_sid_map *map;
  301. slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
  302. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  303. /* Invalidate an entry */
  304. get_paca()->kvm_slb[slb_index].esid = 0;
  305. return -ENOENT;
  306. }
  307. map = find_sid_vsid(vcpu, gvsid);
  308. if (!map)
  309. map = create_sid_map(vcpu, gvsid);
  310. map->guest_esid = esid;
  311. slb_vsid |= (map->host_vsid << 12);
  312. slb_vsid &= ~SLB_VSID_KP;
  313. slb_esid |= slb_index;
  314. get_paca()->kvm_slb[slb_index].esid = slb_esid;
  315. get_paca()->kvm_slb[slb_index].vsid = slb_vsid;
  316. dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
  317. return 0;
  318. }
  319. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  320. {
  321. get_paca()->kvm_slb_max = 1;
  322. get_paca()->kvm_slb[0].esid = 0;
  323. }
  324. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  325. {
  326. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  327. }