book3s_mmu_hpte.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <linux/hash.h>
  22. #include <linux/slab.h>
  23. #include <asm/kvm_ppc.h>
  24. #include <asm/kvm_book3s.h>
  25. #include <asm/machdep.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/hw_irq.h>
  28. #include "trace_pr.h"
  29. #define PTE_SIZE 12
  30. static struct kmem_cache *hpte_cache;
  31. static inline u64 kvmppc_mmu_hash_pte(u64 eaddr)
  32. {
  33. return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE);
  34. }
  35. static inline u64 kvmppc_mmu_hash_pte_long(u64 eaddr)
  36. {
  37. return hash_64((eaddr & 0x0ffff000) >> PTE_SIZE,
  38. HPTEG_HASH_BITS_PTE_LONG);
  39. }
  40. static inline u64 kvmppc_mmu_hash_vpte(u64 vpage)
  41. {
  42. return hash_64(vpage & 0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
  43. }
  44. static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage)
  45. {
  46. return hash_64((vpage & 0xffffff000ULL) >> 12,
  47. HPTEG_HASH_BITS_VPTE_LONG);
  48. }
  49. #ifdef CONFIG_PPC_BOOK3S_64
  50. static inline u64 kvmppc_mmu_hash_vpte_64k(u64 vpage)
  51. {
  52. return hash_64((vpage & 0xffffffff0ULL) >> 4,
  53. HPTEG_HASH_BITS_VPTE_64K);
  54. }
  55. #endif
  56. void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  57. {
  58. u64 index;
  59. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  60. trace_kvm_book3s_mmu_map(pte);
  61. spin_lock(&vcpu3s->mmu_lock);
  62. /* Add to ePTE list */
  63. index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
  64. hlist_add_head_rcu(&pte->list_pte, &vcpu3s->hpte_hash_pte[index]);
  65. /* Add to ePTE_long list */
  66. index = kvmppc_mmu_hash_pte_long(pte->pte.eaddr);
  67. hlist_add_head_rcu(&pte->list_pte_long,
  68. &vcpu3s->hpte_hash_pte_long[index]);
  69. /* Add to vPTE list */
  70. index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
  71. hlist_add_head_rcu(&pte->list_vpte, &vcpu3s->hpte_hash_vpte[index]);
  72. /* Add to vPTE_long list */
  73. index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
  74. hlist_add_head_rcu(&pte->list_vpte_long,
  75. &vcpu3s->hpte_hash_vpte_long[index]);
  76. #ifdef CONFIG_PPC_BOOK3S_64
  77. /* Add to vPTE_64k list */
  78. index = kvmppc_mmu_hash_vpte_64k(pte->pte.vpage);
  79. hlist_add_head_rcu(&pte->list_vpte_64k,
  80. &vcpu3s->hpte_hash_vpte_64k[index]);
  81. #endif
  82. vcpu3s->hpte_cache_count++;
  83. spin_unlock(&vcpu3s->mmu_lock);
  84. }
  85. static void free_pte_rcu(struct rcu_head *head)
  86. {
  87. struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head);
  88. kmem_cache_free(hpte_cache, pte);
  89. }
  90. static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  91. {
  92. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  93. trace_kvm_book3s_mmu_invalidate(pte);
  94. /* Different for 32 and 64 bit */
  95. kvmppc_mmu_invalidate_pte(vcpu, pte);
  96. spin_lock(&vcpu3s->mmu_lock);
  97. /* pte already invalidated in between? */
  98. if (hlist_unhashed(&pte->list_pte)) {
  99. spin_unlock(&vcpu3s->mmu_lock);
  100. return;
  101. }
  102. hlist_del_init_rcu(&pte->list_pte);
  103. hlist_del_init_rcu(&pte->list_pte_long);
  104. hlist_del_init_rcu(&pte->list_vpte);
  105. hlist_del_init_rcu(&pte->list_vpte_long);
  106. #ifdef CONFIG_PPC_BOOK3S_64
  107. hlist_del_init_rcu(&pte->list_vpte_64k);
  108. #endif
  109. vcpu3s->hpte_cache_count--;
  110. spin_unlock(&vcpu3s->mmu_lock);
  111. call_rcu(&pte->rcu_head, free_pte_rcu);
  112. }
  113. static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
  114. {
  115. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  116. struct hpte_cache *pte;
  117. int i;
  118. rcu_read_lock();
  119. for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
  120. struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
  121. hlist_for_each_entry_rcu(pte, list, list_vpte_long)
  122. invalidate_pte(vcpu, pte);
  123. }
  124. rcu_read_unlock();
  125. }
  126. static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea)
  127. {
  128. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  129. struct hlist_head *list;
  130. struct hpte_cache *pte;
  131. /* Find the list of entries in the map */
  132. list = &vcpu3s->hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
  133. rcu_read_lock();
  134. /* Check the list for matching entries and invalidate */
  135. hlist_for_each_entry_rcu(pte, list, list_pte)
  136. if ((pte->pte.eaddr & ~0xfffUL) == guest_ea)
  137. invalidate_pte(vcpu, pte);
  138. rcu_read_unlock();
  139. }
  140. static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu *vcpu, ulong guest_ea)
  141. {
  142. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  143. struct hlist_head *list;
  144. struct hpte_cache *pte;
  145. /* Find the list of entries in the map */
  146. list = &vcpu3s->hpte_hash_pte_long[
  147. kvmppc_mmu_hash_pte_long(guest_ea)];
  148. rcu_read_lock();
  149. /* Check the list for matching entries and invalidate */
  150. hlist_for_each_entry_rcu(pte, list, list_pte_long)
  151. if ((pte->pte.eaddr & 0x0ffff000UL) == guest_ea)
  152. invalidate_pte(vcpu, pte);
  153. rcu_read_unlock();
  154. }
  155. void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
  156. {
  157. trace_kvm_book3s_mmu_flush("", vcpu, guest_ea, ea_mask);
  158. guest_ea &= ea_mask;
  159. switch (ea_mask) {
  160. case ~0xfffUL:
  161. kvmppc_mmu_pte_flush_page(vcpu, guest_ea);
  162. break;
  163. case 0x0ffff000:
  164. kvmppc_mmu_pte_flush_long(vcpu, guest_ea);
  165. break;
  166. case 0:
  167. /* Doing a complete flush -> start from scratch */
  168. kvmppc_mmu_pte_flush_all(vcpu);
  169. break;
  170. default:
  171. WARN_ON(1);
  172. break;
  173. }
  174. }
  175. /* Flush with mask 0xfffffffff */
  176. static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
  177. {
  178. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  179. struct hlist_head *list;
  180. struct hpte_cache *pte;
  181. u64 vp_mask = 0xfffffffffULL;
  182. list = &vcpu3s->hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
  183. rcu_read_lock();
  184. /* Check the list for matching entries and invalidate */
  185. hlist_for_each_entry_rcu(pte, list, list_vpte)
  186. if ((pte->pte.vpage & vp_mask) == guest_vp)
  187. invalidate_pte(vcpu, pte);
  188. rcu_read_unlock();
  189. }
  190. #ifdef CONFIG_PPC_BOOK3S_64
  191. /* Flush with mask 0xffffffff0 */
  192. static void kvmppc_mmu_pte_vflush_64k(struct kvm_vcpu *vcpu, u64 guest_vp)
  193. {
  194. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  195. struct hlist_head *list;
  196. struct hpte_cache *pte;
  197. u64 vp_mask = 0xffffffff0ULL;
  198. list = &vcpu3s->hpte_hash_vpte_64k[
  199. kvmppc_mmu_hash_vpte_64k(guest_vp)];
  200. rcu_read_lock();
  201. /* Check the list for matching entries and invalidate */
  202. hlist_for_each_entry_rcu(pte, list, list_vpte_64k)
  203. if ((pte->pte.vpage & vp_mask) == guest_vp)
  204. invalidate_pte(vcpu, pte);
  205. rcu_read_unlock();
  206. }
  207. #endif
  208. /* Flush with mask 0xffffff000 */
  209. static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
  210. {
  211. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  212. struct hlist_head *list;
  213. struct hpte_cache *pte;
  214. u64 vp_mask = 0xffffff000ULL;
  215. list = &vcpu3s->hpte_hash_vpte_long[
  216. kvmppc_mmu_hash_vpte_long(guest_vp)];
  217. rcu_read_lock();
  218. /* Check the list for matching entries and invalidate */
  219. hlist_for_each_entry_rcu(pte, list, list_vpte_long)
  220. if ((pte->pte.vpage & vp_mask) == guest_vp)
  221. invalidate_pte(vcpu, pte);
  222. rcu_read_unlock();
  223. }
  224. void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
  225. {
  226. trace_kvm_book3s_mmu_flush("v", vcpu, guest_vp, vp_mask);
  227. guest_vp &= vp_mask;
  228. switch(vp_mask) {
  229. case 0xfffffffffULL:
  230. kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
  231. break;
  232. #ifdef CONFIG_PPC_BOOK3S_64
  233. case 0xffffffff0ULL:
  234. kvmppc_mmu_pte_vflush_64k(vcpu, guest_vp);
  235. break;
  236. #endif
  237. case 0xffffff000ULL:
  238. kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
  239. break;
  240. default:
  241. WARN_ON(1);
  242. return;
  243. }
  244. }
  245. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  246. {
  247. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  248. struct hpte_cache *pte;
  249. int i;
  250. trace_kvm_book3s_mmu_flush("p", vcpu, pa_start, pa_end);
  251. rcu_read_lock();
  252. for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
  253. struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
  254. hlist_for_each_entry_rcu(pte, list, list_vpte_long)
  255. if ((pte->pte.raddr >= pa_start) &&
  256. (pte->pte.raddr < pa_end))
  257. invalidate_pte(vcpu, pte);
  258. }
  259. rcu_read_unlock();
  260. }
  261. struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
  262. {
  263. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  264. struct hpte_cache *pte;
  265. if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM)
  266. kvmppc_mmu_pte_flush_all(vcpu);
  267. pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
  268. return pte;
  269. }
  270. void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte)
  271. {
  272. kmem_cache_free(hpte_cache, pte);
  273. }
  274. void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
  275. {
  276. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  277. }
  278. static void kvmppc_mmu_hpte_init_hash(struct hlist_head *hash_list, int len)
  279. {
  280. int i;
  281. for (i = 0; i < len; i++)
  282. INIT_HLIST_HEAD(&hash_list[i]);
  283. }
  284. int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
  285. {
  286. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  287. /* init hpte lookup hashes */
  288. kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte,
  289. ARRAY_SIZE(vcpu3s->hpte_hash_pte));
  290. kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte_long,
  291. ARRAY_SIZE(vcpu3s->hpte_hash_pte_long));
  292. kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte,
  293. ARRAY_SIZE(vcpu3s->hpte_hash_vpte));
  294. kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_long,
  295. ARRAY_SIZE(vcpu3s->hpte_hash_vpte_long));
  296. #ifdef CONFIG_PPC_BOOK3S_64
  297. kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_64k,
  298. ARRAY_SIZE(vcpu3s->hpte_hash_vpte_64k));
  299. #endif
  300. spin_lock_init(&vcpu3s->mmu_lock);
  301. return 0;
  302. }
  303. int kvmppc_mmu_hpte_sysinit(void)
  304. {
  305. /* init hpte slab cache */
  306. hpte_cache = kmem_cache_create("kvm-spt", sizeof(struct hpte_cache),
  307. sizeof(struct hpte_cache), 0, NULL);
  308. return 0;
  309. }
  310. void kvmppc_mmu_hpte_sysexit(void)
  311. {
  312. kmem_cache_destroy(hpte_cache);
  313. }