44x_tlb.c 9.9 KB

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