44x_tlb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kvm.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/highmem.h>
  24. #include <asm/mmu-44x.h>
  25. #include <asm/kvm_ppc.h>
  26. #include "44x_tlb.h"
  27. #define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
  28. #define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
  29. static unsigned int kvmppc_tlb_44x_pos;
  30. static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
  31. {
  32. /* Mask off reserved bits. */
  33. attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_ATTR_MASK;
  34. if (!usermode) {
  35. /* Guest is in supervisor mode, so we need to translate guest
  36. * supervisor permissions into user permissions. */
  37. attrib &= ~PPC44x_TLB_USER_PERM_MASK;
  38. attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
  39. }
  40. /* Make sure host can always access this memory. */
  41. attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
  42. return attrib;
  43. }
  44. /* Search the guest TLB for a matching entry. */
  45. int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
  46. unsigned int as)
  47. {
  48. int i;
  49. /* XXX Replace loop with fancy data structures. */
  50. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  51. struct tlbe *tlbe = &vcpu->arch.guest_tlb[i];
  52. unsigned int tid;
  53. if (eaddr < get_tlb_eaddr(tlbe))
  54. continue;
  55. if (eaddr > get_tlb_end(tlbe))
  56. continue;
  57. tid = get_tlb_tid(tlbe);
  58. if (tid && (tid != pid))
  59. continue;
  60. if (!get_tlb_v(tlbe))
  61. continue;
  62. if (get_tlb_ts(tlbe) != as)
  63. continue;
  64. return i;
  65. }
  66. return -1;
  67. }
  68. struct tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
  69. {
  70. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  71. unsigned int index;
  72. index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  73. if (index == -1)
  74. return NULL;
  75. return &vcpu->arch.guest_tlb[index];
  76. }
  77. struct tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
  78. {
  79. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  80. unsigned int index;
  81. index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  82. if (index == -1)
  83. return NULL;
  84. return &vcpu->arch.guest_tlb[index];
  85. }
  86. static int kvmppc_44x_tlbe_is_writable(struct tlbe *tlbe)
  87. {
  88. return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW);
  89. }
  90. static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu,
  91. unsigned int index)
  92. {
  93. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[index];
  94. struct page *page = vcpu->arch.shadow_pages[index];
  95. if (get_tlb_v(stlbe)) {
  96. if (kvmppc_44x_tlbe_is_writable(stlbe))
  97. kvm_release_page_dirty(page);
  98. else
  99. kvm_release_page_clean(page);
  100. }
  101. }
  102. void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
  103. {
  104. int i;
  105. for (i = 0; i <= tlb_44x_hwater; i++)
  106. kvmppc_44x_shadow_release(vcpu, i);
  107. }
  108. void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i)
  109. {
  110. vcpu->arch.shadow_tlb_mod[i] = 1;
  111. }
  112. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  113. * the shadow TLB. */
  114. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
  115. u32 flags)
  116. {
  117. struct page *new_page;
  118. struct tlbe *stlbe;
  119. hpa_t hpaddr;
  120. unsigned int victim;
  121. /* Future optimization: don't overwrite the TLB entry containing the
  122. * current PC (or stack?). */
  123. victim = kvmppc_tlb_44x_pos++;
  124. if (kvmppc_tlb_44x_pos > tlb_44x_hwater)
  125. kvmppc_tlb_44x_pos = 0;
  126. stlbe = &vcpu->arch.shadow_tlb[victim];
  127. /* Get reference to new page. */
  128. new_page = gfn_to_page(vcpu->kvm, gfn);
  129. if (is_error_page(new_page)) {
  130. printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
  131. kvm_release_page_clean(new_page);
  132. return;
  133. }
  134. hpaddr = page_to_phys(new_page);
  135. /* Drop reference to old page. */
  136. kvmppc_44x_shadow_release(vcpu, victim);
  137. vcpu->arch.shadow_pages[victim] = new_page;
  138. /* XXX Make sure (va, size) doesn't overlap any other
  139. * entries. 440x6 user manual says the result would be
  140. * "undefined." */
  141. /* XXX what about AS? */
  142. stlbe->tid = !(asid & 0xff);
  143. /* Force TS=1 for all guest mappings. */
  144. /* For now we hardcode 4KB mappings, but it will be important to
  145. * use host large pages in the future. */
  146. stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
  147. | PPC44x_TLB_4K;
  148. stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
  149. stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
  150. vcpu->arch.msr & MSR_PR);
  151. kvmppc_tlbe_set_modified(vcpu, victim);
  152. KVMTRACE_5D(STLB_WRITE, vcpu, victim,
  153. stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
  154. handler);
  155. }
  156. void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
  157. gva_t eend, u32 asid)
  158. {
  159. unsigned int pid = !(asid & 0xff);
  160. int i;
  161. /* XXX Replace loop with fancy data structures. */
  162. for (i = 0; i <= tlb_44x_hwater; i++) {
  163. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
  164. unsigned int tid;
  165. if (!get_tlb_v(stlbe))
  166. continue;
  167. if (eend < get_tlb_eaddr(stlbe))
  168. continue;
  169. if (eaddr > get_tlb_end(stlbe))
  170. continue;
  171. tid = get_tlb_tid(stlbe);
  172. if (tid && (tid != pid))
  173. continue;
  174. kvmppc_44x_shadow_release(vcpu, i);
  175. stlbe->word0 = 0;
  176. kvmppc_tlbe_set_modified(vcpu, i);
  177. KVMTRACE_5D(STLB_INVAL, vcpu, i,
  178. stlbe->tid, stlbe->word0, stlbe->word1,
  179. stlbe->word2, handler);
  180. }
  181. }
  182. /* Invalidate all mappings on the privilege switch after PID has been changed.
  183. * The guest always runs with PID=1, so we must clear the entire TLB when
  184. * switching address spaces. */
  185. void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
  186. {
  187. int i;
  188. if (vcpu->arch.swap_pid) {
  189. /* XXX Replace loop with fancy data structures. */
  190. for (i = 0; i <= tlb_44x_hwater; i++) {
  191. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
  192. /* Future optimization: clear only userspace mappings. */
  193. kvmppc_44x_shadow_release(vcpu, i);
  194. stlbe->word0 = 0;
  195. kvmppc_tlbe_set_modified(vcpu, i);
  196. KVMTRACE_5D(STLB_INVAL, vcpu, i,
  197. stlbe->tid, stlbe->word0, stlbe->word1,
  198. stlbe->word2, handler);
  199. }
  200. vcpu->arch.swap_pid = 0;
  201. }
  202. vcpu->arch.shadow_pid = !usermode;
  203. }