book3s_32_mmu_host.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <asm/kvm_ppc.h>
  22. #include <asm/kvm_book3s.h>
  23. #include <asm/mmu-hash32.h>
  24. #include <asm/machdep.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/hw_irq.h>
  27. /* #define DEBUG_MMU */
  28. /* #define DEBUG_SR */
  29. #ifdef DEBUG_MMU
  30. #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  31. #else
  32. #define dprintk_mmu(a, ...) do { } while(0)
  33. #endif
  34. #ifdef DEBUG_SR
  35. #define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  36. #else
  37. #define dprintk_sr(a, ...) do { } while(0)
  38. #endif
  39. #if PAGE_SHIFT != 12
  40. #error Unknown page size
  41. #endif
  42. #ifdef CONFIG_SMP
  43. #error XXX need to grab mmu_hash_lock
  44. #endif
  45. #ifdef CONFIG_PTE_64BIT
  46. #error Only 32 bit pages are supported for now
  47. #endif
  48. static ulong htab;
  49. static u32 htabmask;
  50. void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  51. {
  52. volatile u32 *pteg;
  53. /* Remove from host HTAB */
  54. pteg = (u32*)pte->slot;
  55. pteg[0] = 0;
  56. /* And make sure it's gone from the TLB too */
  57. asm volatile ("sync");
  58. asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
  59. asm volatile ("sync");
  60. asm volatile ("tlbsync");
  61. }
  62. /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
  63. * a hash, so we don't waste cycles on looping */
  64. static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
  65. {
  66. return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
  67. ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
  68. ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
  69. ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
  70. ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
  71. ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
  72. ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
  73. ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
  74. }
  75. static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
  76. {
  77. struct kvmppc_sid_map *map;
  78. u16 sid_map_mask;
  79. if (vcpu->arch.shared->msr & MSR_PR)
  80. gvsid |= VSID_PR;
  81. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  82. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  83. if (map->guest_vsid == gvsid) {
  84. dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
  85. gvsid, map->host_vsid);
  86. return map;
  87. }
  88. map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
  89. if (map->guest_vsid == gvsid) {
  90. dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
  91. gvsid, map->host_vsid);
  92. return map;
  93. }
  94. dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);
  95. return NULL;
  96. }
  97. static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
  98. bool primary)
  99. {
  100. u32 page, hash;
  101. ulong pteg = htab;
  102. page = (eaddr & ~ESID_MASK) >> 12;
  103. hash = ((vsid ^ page) << 6);
  104. if (!primary)
  105. hash = ~hash;
  106. hash &= htabmask;
  107. pteg |= hash;
  108. dprintk_mmu("htab: %lx | hash: %x | htabmask: %x | pteg: %lx\n",
  109. htab, hash, htabmask, pteg);
  110. return (u32*)pteg;
  111. }
  112. extern char etext[];
  113. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
  114. {
  115. pfn_t hpaddr;
  116. u64 va;
  117. u64 vsid;
  118. struct kvmppc_sid_map *map;
  119. volatile u32 *pteg;
  120. u32 eaddr = orig_pte->eaddr;
  121. u32 pteg0, pteg1;
  122. register int rr = 0;
  123. bool primary = false;
  124. bool evict = false;
  125. struct hpte_cache *pte;
  126. /* Get host physical address for gpa */
  127. hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
  128. if (is_error_pfn(hpaddr)) {
  129. printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",
  130. orig_pte->eaddr);
  131. return -EINVAL;
  132. }
  133. hpaddr <<= PAGE_SHIFT;
  134. /* and write the mapping ea -> hpa into the pt */
  135. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  136. map = find_sid_vsid(vcpu, vsid);
  137. if (!map) {
  138. kvmppc_mmu_map_segment(vcpu, eaddr);
  139. map = find_sid_vsid(vcpu, vsid);
  140. }
  141. BUG_ON(!map);
  142. vsid = map->host_vsid;
  143. va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);
  144. next_pteg:
  145. if (rr == 16) {
  146. primary = !primary;
  147. evict = true;
  148. rr = 0;
  149. }
  150. pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
  151. /* not evicting yet */
  152. if (!evict && (pteg[rr] & PTE_V)) {
  153. rr += 2;
  154. goto next_pteg;
  155. }
  156. dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
  157. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  158. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  159. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  160. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  161. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  162. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  163. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  164. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  165. pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
  166. (primary ? 0 : PTE_SEC);
  167. pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
  168. if (orig_pte->may_write) {
  169. pteg1 |= PP_RWRW;
  170. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  171. } else {
  172. pteg1 |= PP_RWRX;
  173. }
  174. local_irq_disable();
  175. if (pteg[rr]) {
  176. pteg[rr] = 0;
  177. asm volatile ("sync");
  178. }
  179. pteg[rr + 1] = pteg1;
  180. pteg[rr] = pteg0;
  181. asm volatile ("sync");
  182. local_irq_enable();
  183. dprintk_mmu("KVM: new PTEG: %p\n", pteg);
  184. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  185. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  186. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  187. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  188. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  189. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  190. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  191. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  192. /* Now tell our Shadow PTE code about the new page */
  193. pte = kvmppc_mmu_hpte_cache_next(vcpu);
  194. dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
  195. orig_pte->may_write ? 'w' : '-',
  196. orig_pte->may_execute ? 'x' : '-',
  197. orig_pte->eaddr, (ulong)pteg, va,
  198. orig_pte->vpage, hpaddr);
  199. pte->slot = (ulong)&pteg[rr];
  200. pte->host_va = va;
  201. pte->pte = *orig_pte;
  202. pte->pfn = hpaddr >> PAGE_SHIFT;
  203. kvmppc_mmu_hpte_cache_map(vcpu, pte);
  204. return 0;
  205. }
  206. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  207. {
  208. struct kvmppc_sid_map *map;
  209. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  210. u16 sid_map_mask;
  211. static int backwards_map = 0;
  212. if (vcpu->arch.shared->msr & MSR_PR)
  213. gvsid |= VSID_PR;
  214. /* We might get collisions that trap in preceding order, so let's
  215. map them differently */
  216. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  217. if (backwards_map)
  218. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  219. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  220. /* Make sure we're taking the other map next time */
  221. backwards_map = !backwards_map;
  222. /* Uh-oh ... out of mappings. Let's flush! */
  223. if (vcpu_book3s->vsid_next >= VSID_POOL_SIZE) {
  224. vcpu_book3s->vsid_next = 0;
  225. memset(vcpu_book3s->sid_map, 0,
  226. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  227. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  228. kvmppc_mmu_flush_segments(vcpu);
  229. }
  230. map->host_vsid = vcpu_book3s->vsid_pool[vcpu_book3s->vsid_next];
  231. vcpu_book3s->vsid_next++;
  232. map->guest_vsid = gvsid;
  233. map->valid = true;
  234. return map;
  235. }
  236. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  237. {
  238. u32 esid = eaddr >> SID_SHIFT;
  239. u64 gvsid;
  240. u32 sr;
  241. struct kvmppc_sid_map *map;
  242. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  243. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  244. /* Invalidate an entry */
  245. svcpu->sr[esid] = SR_INVALID;
  246. return -ENOENT;
  247. }
  248. map = find_sid_vsid(vcpu, gvsid);
  249. if (!map)
  250. map = create_sid_map(vcpu, gvsid);
  251. map->guest_esid = esid;
  252. sr = map->host_vsid | SR_KP;
  253. svcpu->sr[esid] = sr;
  254. dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
  255. return 0;
  256. }
  257. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  258. {
  259. int i;
  260. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  261. dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
  262. for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
  263. svcpu->sr[i] = SR_INVALID;
  264. }
  265. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  266. {
  267. int i;
  268. kvmppc_mmu_hpte_destroy(vcpu);
  269. preempt_disable();
  270. for (i = 0; i < SID_CONTEXTS; i++)
  271. __destroy_context(to_book3s(vcpu)->context_id[i]);
  272. preempt_enable();
  273. }
  274. /* From mm/mmu_context_hash32.c */
  275. #define CTX_TO_VSID(c, id) ((((c) * (897 * 16)) + (id * 0x111)) & 0xffffff)
  276. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  277. {
  278. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  279. int err;
  280. ulong sdr1;
  281. int i;
  282. int j;
  283. for (i = 0; i < SID_CONTEXTS; i++) {
  284. err = __init_new_context();
  285. if (err < 0)
  286. goto init_fail;
  287. vcpu3s->context_id[i] = err;
  288. /* Remember context id for this combination */
  289. for (j = 0; j < 16; j++)
  290. vcpu3s->vsid_pool[(i * 16) + j] = CTX_TO_VSID(err, j);
  291. }
  292. vcpu3s->vsid_next = 0;
  293. /* Remember where the HTAB is */
  294. asm ( "mfsdr1 %0" : "=r"(sdr1) );
  295. htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;
  296. htab = (ulong)__va(sdr1 & 0xffff0000);
  297. kvmppc_mmu_hpte_init(vcpu);
  298. return 0;
  299. init_fail:
  300. for (j = 0; j < i; j++) {
  301. if (!vcpu3s->context_id[j])
  302. continue;
  303. __destroy_context(to_book3s(vcpu)->context_id[j]);
  304. }
  305. return -1;
  306. }