44x_tlb.c 10 KB

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