book3s_32_mmu_host.c 10 KB

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