book3s_32_mmu_host.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. int r = 0;
  127. /* Get host physical address for gpa */
  128. hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
  129. if (is_error_pfn(hpaddr)) {
  130. printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",
  131. orig_pte->eaddr);
  132. r = -EINVAL;
  133. goto out;
  134. }
  135. hpaddr <<= PAGE_SHIFT;
  136. /* and write the mapping ea -> hpa into the pt */
  137. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  138. map = find_sid_vsid(vcpu, vsid);
  139. if (!map) {
  140. kvmppc_mmu_map_segment(vcpu, eaddr);
  141. map = find_sid_vsid(vcpu, vsid);
  142. }
  143. BUG_ON(!map);
  144. vsid = map->host_vsid;
  145. va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);
  146. next_pteg:
  147. if (rr == 16) {
  148. primary = !primary;
  149. evict = true;
  150. rr = 0;
  151. }
  152. pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
  153. /* not evicting yet */
  154. if (!evict && (pteg[rr] & PTE_V)) {
  155. rr += 2;
  156. goto next_pteg;
  157. }
  158. dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
  159. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  160. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  161. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  162. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  163. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  164. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  165. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  166. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  167. pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
  168. (primary ? 0 : PTE_SEC);
  169. pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
  170. if (orig_pte->may_write) {
  171. pteg1 |= PP_RWRW;
  172. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  173. } else {
  174. pteg1 |= PP_RWRX;
  175. }
  176. local_irq_disable();
  177. if (pteg[rr]) {
  178. pteg[rr] = 0;
  179. asm volatile ("sync");
  180. }
  181. pteg[rr + 1] = pteg1;
  182. pteg[rr] = pteg0;
  183. asm volatile ("sync");
  184. local_irq_enable();
  185. dprintk_mmu("KVM: new PTEG: %p\n", pteg);
  186. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  187. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  188. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  189. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  190. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  191. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  192. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  193. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  194. /* Now tell our Shadow PTE code about the new page */
  195. pte = kvmppc_mmu_hpte_cache_next(vcpu);
  196. dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
  197. orig_pte->may_write ? 'w' : '-',
  198. orig_pte->may_execute ? 'x' : '-',
  199. orig_pte->eaddr, (ulong)pteg, va,
  200. orig_pte->vpage, hpaddr);
  201. pte->slot = (ulong)&pteg[rr];
  202. pte->host_va = va;
  203. pte->pte = *orig_pte;
  204. pte->pfn = hpaddr >> PAGE_SHIFT;
  205. kvmppc_mmu_hpte_cache_map(vcpu, pte);
  206. out:
  207. return r;
  208. }
  209. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  210. {
  211. struct kvmppc_sid_map *map;
  212. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  213. u16 sid_map_mask;
  214. static int backwards_map = 0;
  215. if (vcpu->arch.shared->msr & MSR_PR)
  216. gvsid |= VSID_PR;
  217. /* We might get collisions that trap in preceding order, so let's
  218. map them differently */
  219. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  220. if (backwards_map)
  221. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  222. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  223. /* Make sure we're taking the other map next time */
  224. backwards_map = !backwards_map;
  225. /* Uh-oh ... out of mappings. Let's flush! */
  226. if (vcpu_book3s->vsid_next >= VSID_POOL_SIZE) {
  227. vcpu_book3s->vsid_next = 0;
  228. memset(vcpu_book3s->sid_map, 0,
  229. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  230. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  231. kvmppc_mmu_flush_segments(vcpu);
  232. }
  233. map->host_vsid = vcpu_book3s->vsid_pool[vcpu_book3s->vsid_next];
  234. vcpu_book3s->vsid_next++;
  235. map->guest_vsid = gvsid;
  236. map->valid = true;
  237. return map;
  238. }
  239. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  240. {
  241. u32 esid = eaddr >> SID_SHIFT;
  242. u64 gvsid;
  243. u32 sr;
  244. struct kvmppc_sid_map *map;
  245. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  246. int r = 0;
  247. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  248. /* Invalidate an entry */
  249. svcpu->sr[esid] = SR_INVALID;
  250. r = -ENOENT;
  251. goto out;
  252. }
  253. map = find_sid_vsid(vcpu, gvsid);
  254. if (!map)
  255. map = create_sid_map(vcpu, gvsid);
  256. map->guest_esid = esid;
  257. sr = map->host_vsid | SR_KP;
  258. svcpu->sr[esid] = sr;
  259. dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
  260. out:
  261. svcpu_put(svcpu);
  262. return r;
  263. }
  264. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  265. {
  266. int i;
  267. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  268. dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
  269. for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
  270. svcpu->sr[i] = SR_INVALID;
  271. svcpu_put(svcpu);
  272. }
  273. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  274. {
  275. int i;
  276. kvmppc_mmu_hpte_destroy(vcpu);
  277. preempt_disable();
  278. for (i = 0; i < SID_CONTEXTS; i++)
  279. __destroy_context(to_book3s(vcpu)->context_id[i]);
  280. preempt_enable();
  281. }
  282. /* From mm/mmu_context_hash32.c */
  283. #define CTX_TO_VSID(c, id) ((((c) * (897 * 16)) + (id * 0x111)) & 0xffffff)
  284. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  285. {
  286. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  287. int err;
  288. ulong sdr1;
  289. int i;
  290. int j;
  291. for (i = 0; i < SID_CONTEXTS; i++) {
  292. err = __init_new_context();
  293. if (err < 0)
  294. goto init_fail;
  295. vcpu3s->context_id[i] = err;
  296. /* Remember context id for this combination */
  297. for (j = 0; j < 16; j++)
  298. vcpu3s->vsid_pool[(i * 16) + j] = CTX_TO_VSID(err, j);
  299. }
  300. vcpu3s->vsid_next = 0;
  301. /* Remember where the HTAB is */
  302. asm ( "mfsdr1 %0" : "=r"(sdr1) );
  303. htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;
  304. htab = (ulong)__va(sdr1 & 0xffff0000);
  305. kvmppc_mmu_hpte_init(vcpu);
  306. return 0;
  307. init_fail:
  308. for (j = 0; j < i; j++) {
  309. if (!vcpu3s->context_id[j])
  310. continue;
  311. __destroy_context(to_book3s(vcpu)->context_id[j]);
  312. }
  313. return -1;
  314. }