book3s_64_mmu_host.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <linux/hash.h>
  23. #include <asm/kvm_ppc.h>
  24. #include <asm/kvm_book3s.h>
  25. #include <asm/mmu-hash64.h>
  26. #include <asm/machdep.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/hw_irq.h>
  29. #define PTE_SIZE 12
  30. #define VSID_ALL 0
  31. /* #define DEBUG_MMU */
  32. /* #define DEBUG_SLB */
  33. #ifdef DEBUG_MMU
  34. #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  35. #else
  36. #define dprintk_mmu(a, ...) do { } while(0)
  37. #endif
  38. #ifdef DEBUG_SLB
  39. #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  40. #else
  41. #define dprintk_slb(a, ...) do { } while(0)
  42. #endif
  43. void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  44. {
  45. ppc_md.hpte_invalidate(pte->slot, pte->host_va,
  46. MMU_PAGE_4K, MMU_SEGSIZE_256M,
  47. false);
  48. }
  49. /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
  50. * a hash, so we don't waste cycles on looping */
  51. static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
  52. {
  53. return hash_64(gvsid, SID_MAP_BITS);
  54. }
  55. static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
  56. {
  57. struct kvmppc_sid_map *map;
  58. u16 sid_map_mask;
  59. if (vcpu->arch.msr & MSR_PR)
  60. gvsid |= VSID_PR;
  61. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  62. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  63. if (map->guest_vsid == gvsid) {
  64. dprintk_slb("SLB: Searching: 0x%llx -> 0x%llx\n",
  65. gvsid, map->host_vsid);
  66. return map;
  67. }
  68. map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
  69. if (map->guest_vsid == gvsid) {
  70. dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
  71. gvsid, map->host_vsid);
  72. return map;
  73. }
  74. dprintk_slb("SLB: Searching %d/%d: 0x%llx -> not found\n",
  75. sid_map_mask, SID_MAP_MASK - sid_map_mask, gvsid);
  76. return NULL;
  77. }
  78. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
  79. {
  80. pfn_t hpaddr;
  81. ulong hash, hpteg, va;
  82. u64 vsid;
  83. int ret;
  84. int rflags = 0x192;
  85. int vflags = 0;
  86. int attempt = 0;
  87. struct kvmppc_sid_map *map;
  88. /* Get host physical address for gpa */
  89. hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  90. if (kvm_is_error_hva(hpaddr)) {
  91. printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
  92. return -EINVAL;
  93. }
  94. hpaddr <<= PAGE_SHIFT;
  95. #if PAGE_SHIFT == 12
  96. #elif PAGE_SHIFT == 16
  97. hpaddr |= orig_pte->raddr & 0xf000;
  98. #else
  99. #error Unknown page size
  100. #endif
  101. /* and write the mapping ea -> hpa into the pt */
  102. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  103. map = find_sid_vsid(vcpu, vsid);
  104. if (!map) {
  105. ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
  106. WARN_ON(ret < 0);
  107. map = find_sid_vsid(vcpu, vsid);
  108. }
  109. if (!map) {
  110. printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
  111. vsid, orig_pte->eaddr);
  112. WARN_ON(true);
  113. return -EINVAL;
  114. }
  115. vsid = map->host_vsid;
  116. va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
  117. if (!orig_pte->may_write)
  118. rflags |= HPTE_R_PP;
  119. else
  120. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  121. if (!orig_pte->may_execute)
  122. rflags |= HPTE_R_N;
  123. hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
  124. map_again:
  125. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  126. /* In case we tried normal mapping already, let's nuke old entries */
  127. if (attempt > 1)
  128. if (ppc_md.hpte_remove(hpteg) < 0)
  129. return -1;
  130. ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
  131. if (ret < 0) {
  132. /* If we couldn't map a primary PTE, try a secondary */
  133. hash = ~hash;
  134. vflags ^= HPTE_V_SECONDARY;
  135. attempt++;
  136. goto map_again;
  137. } else {
  138. struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
  139. dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
  140. ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
  141. (rflags & HPTE_R_N) ? '-' : 'x',
  142. orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
  143. /* The ppc_md code may give us a secondary entry even though we
  144. asked for a primary. Fix up. */
  145. if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
  146. hash = ~hash;
  147. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  148. }
  149. pte->slot = hpteg + (ret & 7);
  150. pte->host_va = va;
  151. pte->pte = *orig_pte;
  152. pte->pfn = hpaddr >> PAGE_SHIFT;
  153. kvmppc_mmu_hpte_cache_map(vcpu, pte);
  154. }
  155. return 0;
  156. }
  157. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  158. {
  159. struct kvmppc_sid_map *map;
  160. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  161. u16 sid_map_mask;
  162. static int backwards_map = 0;
  163. if (vcpu->arch.msr & MSR_PR)
  164. gvsid |= VSID_PR;
  165. /* We might get collisions that trap in preceding order, so let's
  166. map them differently */
  167. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  168. if (backwards_map)
  169. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  170. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  171. /* Make sure we're taking the other map next time */
  172. backwards_map = !backwards_map;
  173. /* Uh-oh ... out of mappings. Let's flush! */
  174. if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
  175. vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
  176. memset(vcpu_book3s->sid_map, 0,
  177. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  178. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  179. kvmppc_mmu_flush_segments(vcpu);
  180. }
  181. map->host_vsid = vcpu_book3s->vsid_next++;
  182. map->guest_vsid = gvsid;
  183. map->valid = true;
  184. dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
  185. sid_map_mask, gvsid, map->host_vsid);
  186. return map;
  187. }
  188. static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
  189. {
  190. int i;
  191. int max_slb_size = 64;
  192. int found_inval = -1;
  193. int r;
  194. if (!to_svcpu(vcpu)->slb_max)
  195. to_svcpu(vcpu)->slb_max = 1;
  196. /* Are we overwriting? */
  197. for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
  198. if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
  199. found_inval = i;
  200. else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
  201. return i;
  202. }
  203. /* Found a spare entry that was invalidated before */
  204. if (found_inval > 0)
  205. return found_inval;
  206. /* No spare invalid entry, so create one */
  207. if (mmu_slb_size < 64)
  208. max_slb_size = mmu_slb_size;
  209. /* Overflowing -> purge */
  210. if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
  211. kvmppc_mmu_flush_segments(vcpu);
  212. r = to_svcpu(vcpu)->slb_max;
  213. to_svcpu(vcpu)->slb_max++;
  214. return r;
  215. }
  216. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  217. {
  218. u64 esid = eaddr >> SID_SHIFT;
  219. u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
  220. u64 slb_vsid = SLB_VSID_USER;
  221. u64 gvsid;
  222. int slb_index;
  223. struct kvmppc_sid_map *map;
  224. slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
  225. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  226. /* Invalidate an entry */
  227. to_svcpu(vcpu)->slb[slb_index].esid = 0;
  228. return -ENOENT;
  229. }
  230. map = find_sid_vsid(vcpu, gvsid);
  231. if (!map)
  232. map = create_sid_map(vcpu, gvsid);
  233. map->guest_esid = esid;
  234. slb_vsid |= (map->host_vsid << 12);
  235. slb_vsid &= ~SLB_VSID_KP;
  236. slb_esid |= slb_index;
  237. to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
  238. to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
  239. dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
  240. return 0;
  241. }
  242. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  243. {
  244. to_svcpu(vcpu)->slb_max = 1;
  245. to_svcpu(vcpu)->slb[0].esid = 0;
  246. }
  247. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  248. {
  249. kvmppc_mmu_hpte_destroy(vcpu);
  250. __destroy_context(to_book3s(vcpu)->context_id);
  251. }
  252. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  253. {
  254. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  255. int err;
  256. err = __init_new_context();
  257. if (err < 0)
  258. return -1;
  259. vcpu3s->context_id = err;
  260. vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
  261. vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
  262. vcpu3s->vsid_next = vcpu3s->vsid_first;
  263. kvmppc_mmu_hpte_init(vcpu);
  264. return 0;
  265. }