44x_tlb.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #ifdef DEBUG
  31. void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
  32. {
  33. struct kvmppc_44x_tlbe *tlbe;
  34. int i;
  35. printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
  36. printk("| %2s | %3s | %8s | %8s | %8s |\n",
  37. "nr", "tid", "word0", "word1", "word2");
  38. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  39. tlbe = &vcpu->arch.guest_tlb[i];
  40. if (tlbe->word0 & PPC44x_TLB_VALID)
  41. printk(" G%2d | %02X | %08X | %08X | %08X |\n",
  42. i, tlbe->tid, tlbe->word0, tlbe->word1,
  43. tlbe->word2);
  44. }
  45. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  46. tlbe = &vcpu->arch.shadow_tlb[i];
  47. if (tlbe->word0 & PPC44x_TLB_VALID)
  48. printk(" S%2d | %02X | %08X | %08X | %08X |\n",
  49. i, tlbe->tid, tlbe->word0, tlbe->word1,
  50. tlbe->word2);
  51. }
  52. }
  53. #endif
  54. static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
  55. {
  56. /* Mask off reserved bits. */
  57. attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_ATTR_MASK;
  58. if (!usermode) {
  59. /* Guest is in supervisor mode, so we need to translate guest
  60. * supervisor permissions into user permissions. */
  61. attrib &= ~PPC44x_TLB_USER_PERM_MASK;
  62. attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
  63. }
  64. /* Make sure host can always access this memory. */
  65. attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
  66. return attrib;
  67. }
  68. /* Search the guest TLB for a matching entry. */
  69. int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
  70. unsigned int as)
  71. {
  72. int i;
  73. /* XXX Replace loop with fancy data structures. */
  74. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  75. struct tlbe *tlbe = &vcpu->arch.guest_tlb[i];
  76. unsigned int tid;
  77. if (eaddr < get_tlb_eaddr(tlbe))
  78. continue;
  79. if (eaddr > get_tlb_end(tlbe))
  80. continue;
  81. tid = get_tlb_tid(tlbe);
  82. if (tid && (tid != pid))
  83. continue;
  84. if (!get_tlb_v(tlbe))
  85. continue;
  86. if (get_tlb_ts(tlbe) != as)
  87. continue;
  88. return i;
  89. }
  90. return -1;
  91. }
  92. struct tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
  93. {
  94. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  95. unsigned int index;
  96. index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  97. if (index == -1)
  98. return NULL;
  99. return &vcpu->arch.guest_tlb[index];
  100. }
  101. struct tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
  102. {
  103. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  104. unsigned int index;
  105. index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  106. if (index == -1)
  107. return NULL;
  108. return &vcpu->arch.guest_tlb[index];
  109. }
  110. static int kvmppc_44x_tlbe_is_writable(struct tlbe *tlbe)
  111. {
  112. return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW);
  113. }
  114. static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu,
  115. unsigned int index)
  116. {
  117. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[index];
  118. struct page *page = vcpu->arch.shadow_pages[index];
  119. if (get_tlb_v(stlbe)) {
  120. if (kvmppc_44x_tlbe_is_writable(stlbe))
  121. kvm_release_page_dirty(page);
  122. else
  123. kvm_release_page_clean(page);
  124. }
  125. }
  126. void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
  127. {
  128. int i;
  129. for (i = 0; i <= tlb_44x_hwater; i++)
  130. kvmppc_44x_shadow_release(vcpu, i);
  131. }
  132. void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i)
  133. {
  134. vcpu->arch.shadow_tlb_mod[i] = 1;
  135. }
  136. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  137. * the shadow TLB. */
  138. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
  139. u32 flags)
  140. {
  141. struct page *new_page;
  142. struct tlbe *stlbe;
  143. hpa_t hpaddr;
  144. unsigned int victim;
  145. /* Future optimization: don't overwrite the TLB entry containing the
  146. * current PC (or stack?). */
  147. victim = kvmppc_tlb_44x_pos++;
  148. if (kvmppc_tlb_44x_pos > tlb_44x_hwater)
  149. kvmppc_tlb_44x_pos = 0;
  150. stlbe = &vcpu->arch.shadow_tlb[victim];
  151. /* Get reference to new page. */
  152. new_page = gfn_to_page(vcpu->kvm, gfn);
  153. if (is_error_page(new_page)) {
  154. printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
  155. kvm_release_page_clean(new_page);
  156. return;
  157. }
  158. hpaddr = page_to_phys(new_page);
  159. /* Drop reference to old page. */
  160. kvmppc_44x_shadow_release(vcpu, victim);
  161. vcpu->arch.shadow_pages[victim] = new_page;
  162. /* XXX Make sure (va, size) doesn't overlap any other
  163. * entries. 440x6 user manual says the result would be
  164. * "undefined." */
  165. /* XXX what about AS? */
  166. stlbe->tid = !(asid & 0xff);
  167. /* Force TS=1 for all guest mappings. */
  168. /* For now we hardcode 4KB mappings, but it will be important to
  169. * use host large pages in the future. */
  170. stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
  171. | PPC44x_TLB_4K;
  172. stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
  173. stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
  174. vcpu->arch.msr & MSR_PR);
  175. kvmppc_tlbe_set_modified(vcpu, victim);
  176. KVMTRACE_5D(STLB_WRITE, vcpu, victim,
  177. stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
  178. handler);
  179. }
  180. static void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
  181. gva_t eend, u32 asid)
  182. {
  183. unsigned int pid = !(asid & 0xff);
  184. int i;
  185. /* XXX Replace loop with fancy data structures. */
  186. for (i = 0; i <= tlb_44x_hwater; i++) {
  187. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
  188. unsigned int tid;
  189. if (!get_tlb_v(stlbe))
  190. continue;
  191. if (eend < get_tlb_eaddr(stlbe))
  192. continue;
  193. if (eaddr > get_tlb_end(stlbe))
  194. continue;
  195. tid = get_tlb_tid(stlbe);
  196. if (tid && (tid != pid))
  197. continue;
  198. kvmppc_44x_shadow_release(vcpu, i);
  199. stlbe->word0 = 0;
  200. kvmppc_tlbe_set_modified(vcpu, i);
  201. KVMTRACE_5D(STLB_INVAL, vcpu, i,
  202. stlbe->tid, stlbe->word0, stlbe->word1,
  203. stlbe->word2, handler);
  204. }
  205. }
  206. /* Invalidate all mappings on the privilege switch after PID has been changed.
  207. * The guest always runs with PID=1, so we must clear the entire TLB when
  208. * switching address spaces. */
  209. void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
  210. {
  211. int i;
  212. if (vcpu->arch.swap_pid) {
  213. /* XXX Replace loop with fancy data structures. */
  214. for (i = 0; i <= tlb_44x_hwater; i++) {
  215. struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
  216. /* Future optimization: clear only userspace mappings. */
  217. kvmppc_44x_shadow_release(vcpu, i);
  218. stlbe->word0 = 0;
  219. kvmppc_tlbe_set_modified(vcpu, i);
  220. KVMTRACE_5D(STLB_INVAL, vcpu, i,
  221. stlbe->tid, stlbe->word0, stlbe->word1,
  222. stlbe->word2, handler);
  223. }
  224. vcpu->arch.swap_pid = 0;
  225. }
  226. vcpu->arch.shadow_pid = !usermode;
  227. }
  228. static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
  229. const struct tlbe *tlbe)
  230. {
  231. gpa_t gpa;
  232. if (!get_tlb_v(tlbe))
  233. return 0;
  234. /* Does it match current guest AS? */
  235. /* XXX what about IS != DS? */
  236. if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
  237. return 0;
  238. gpa = get_tlb_raddr(tlbe);
  239. if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
  240. /* Mapping is not for RAM. */
  241. return 0;
  242. return 1;
  243. }
  244. int kvmppc_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
  245. {
  246. u64 eaddr;
  247. u64 raddr;
  248. u64 asid;
  249. u32 flags;
  250. struct tlbe *tlbe;
  251. unsigned int index;
  252. index = vcpu->arch.gpr[ra];
  253. if (index > PPC44x_TLB_SIZE) {
  254. printk("%s: index %d\n", __func__, index);
  255. kvmppc_dump_vcpu(vcpu);
  256. return EMULATE_FAIL;
  257. }
  258. tlbe = &vcpu->arch.guest_tlb[index];
  259. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  260. if (tlbe->word0 & PPC44x_TLB_VALID) {
  261. eaddr = get_tlb_eaddr(tlbe);
  262. asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
  263. kvmppc_mmu_invalidate(vcpu, eaddr, get_tlb_end(tlbe), asid);
  264. }
  265. switch (ws) {
  266. case PPC44x_TLB_PAGEID:
  267. tlbe->tid = vcpu->arch.mmucr & 0xff;
  268. tlbe->word0 = vcpu->arch.gpr[rs];
  269. break;
  270. case PPC44x_TLB_XLAT:
  271. tlbe->word1 = vcpu->arch.gpr[rs];
  272. break;
  273. case PPC44x_TLB_ATTRIB:
  274. tlbe->word2 = vcpu->arch.gpr[rs];
  275. break;
  276. default:
  277. return EMULATE_FAIL;
  278. }
  279. if (tlbe_is_host_safe(vcpu, tlbe)) {
  280. eaddr = get_tlb_eaddr(tlbe);
  281. raddr = get_tlb_raddr(tlbe);
  282. asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
  283. flags = tlbe->word2 & 0xffff;
  284. /* Create a 4KB mapping on the host. If the guest wanted a
  285. * large page, only the first 4KB is mapped here and the rest
  286. * are mapped on the fly. */
  287. kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags);
  288. }
  289. KVMTRACE_5D(GTLB_WRITE, vcpu, index,
  290. tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2,
  291. handler);
  292. return EMULATE_DONE;
  293. }
  294. int kvmppc_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
  295. {
  296. u32 ea;
  297. int index;
  298. unsigned int as = get_mmucr_sts(vcpu);
  299. unsigned int pid = get_mmucr_stid(vcpu);
  300. ea = vcpu->arch.gpr[rb];
  301. if (ra)
  302. ea += vcpu->arch.gpr[ra];
  303. index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
  304. if (rc) {
  305. if (index < 0)
  306. vcpu->arch.cr &= ~0x20000000;
  307. else
  308. vcpu->arch.cr |= 0x20000000;
  309. }
  310. vcpu->arch.gpr[rt] = index;
  311. return EMULATE_DONE;
  312. }