44x_tlb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/tlbflush.h>
  25. #include <asm/mmu-44x.h>
  26. #include <asm/kvm_ppc.h>
  27. #include <asm/kvm_44x.h>
  28. #include "44x_tlb.h"
  29. #ifndef PPC44x_TLBE_SIZE
  30. #define PPC44x_TLBE_SIZE PPC44x_TLB_4K
  31. #endif
  32. #define PAGE_SIZE_4K (1<<12)
  33. #define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1))
  34. #define PPC44x_TLB_UATTR_MASK \
  35. (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
  36. #define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
  37. #define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
  38. #ifdef DEBUG
  39. void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
  40. {
  41. struct kvmppc_44x_tlbe *tlbe;
  42. int i;
  43. printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
  44. printk("| %2s | %3s | %8s | %8s | %8s |\n",
  45. "nr", "tid", "word0", "word1", "word2");
  46. for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
  47. tlbe = &vcpu_44x->guest_tlb[i];
  48. if (tlbe->word0 & PPC44x_TLB_VALID)
  49. printk(" G%2d | %02X | %08X | %08X | %08X |\n",
  50. i, tlbe->tid, tlbe->word0, tlbe->word1,
  51. tlbe->word2);
  52. }
  53. }
  54. #endif
  55. static inline void kvmppc_44x_tlbie(unsigned int index)
  56. {
  57. /* 0 <= index < 64, so the V bit is clear and we can use the index as
  58. * word0. */
  59. asm volatile(
  60. "tlbwe %[index], %[index], 0\n"
  61. :
  62. : [index] "r"(index)
  63. );
  64. }
  65. static inline void kvmppc_44x_tlbwe(unsigned int index,
  66. struct kvmppc_44x_tlbe *stlbe)
  67. {
  68. unsigned long tmp;
  69. asm volatile(
  70. "mfspr %[tmp], %[sprn_mmucr]\n"
  71. "rlwimi %[tmp], %[tid], 0, 0xff\n"
  72. "mtspr %[sprn_mmucr], %[tmp]\n"
  73. "tlbwe %[word0], %[index], 0\n"
  74. "tlbwe %[word1], %[index], 1\n"
  75. "tlbwe %[word2], %[index], 2\n"
  76. : [tmp] "=&r"(tmp)
  77. : [word0] "r"(stlbe->word0),
  78. [word1] "r"(stlbe->word1),
  79. [word2] "r"(stlbe->word2),
  80. [tid] "r"(stlbe->tid),
  81. [index] "r"(index),
  82. [sprn_mmucr] "i"(SPRN_MMUCR)
  83. );
  84. }
  85. static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
  86. {
  87. /* We only care about the guest's permission and user bits. */
  88. attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
  89. if (!usermode) {
  90. /* Guest is in supervisor mode, so we need to translate guest
  91. * supervisor permissions into user permissions. */
  92. attrib &= ~PPC44x_TLB_USER_PERM_MASK;
  93. attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
  94. }
  95. /* Make sure host can always access this memory. */
  96. attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
  97. /* WIMGE = 0b00100 */
  98. attrib |= PPC44x_TLB_M;
  99. return attrib;
  100. }
  101. /* Search the guest TLB for a matching entry. */
  102. int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
  103. unsigned int as)
  104. {
  105. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  106. int i;
  107. /* XXX Replace loop with fancy data structures. */
  108. for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
  109. struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
  110. unsigned int tid;
  111. if (eaddr < get_tlb_eaddr(tlbe))
  112. continue;
  113. if (eaddr > get_tlb_end(tlbe))
  114. continue;
  115. tid = get_tlb_tid(tlbe);
  116. if (tid && (tid != pid))
  117. continue;
  118. if (!get_tlb_v(tlbe))
  119. continue;
  120. if (get_tlb_ts(tlbe) != as)
  121. continue;
  122. return i;
  123. }
  124. return -1;
  125. }
  126. int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  127. {
  128. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  129. return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  130. }
  131. int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  132. {
  133. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  134. return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
  135. }
  136. static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x,
  137. unsigned int stlb_index)
  138. {
  139. struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index];
  140. if (!ref->page)
  141. return;
  142. /* Discard from the TLB. */
  143. /* Note: we could actually invalidate a host mapping, if the host overwrote
  144. * this TLB entry since we inserted a guest mapping. */
  145. kvmppc_44x_tlbie(stlb_index);
  146. /* Now release the page. */
  147. if (ref->writeable)
  148. kvm_release_page_dirty(ref->page);
  149. else
  150. kvm_release_page_clean(ref->page);
  151. ref->page = NULL;
  152. /* XXX set tlb_44x_index to stlb_index? */
  153. KVMTRACE_1D(STLB_INVAL, &vcpu_44x->vcpu, stlb_index, handler);
  154. }
  155. void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
  156. {
  157. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  158. int i;
  159. for (i = 0; i <= tlb_44x_hwater; i++)
  160. kvmppc_44x_shadow_release(vcpu_44x, i);
  161. }
  162. /**
  163. * kvmppc_mmu_map -- create a host mapping for guest memory
  164. *
  165. * If the guest wanted a larger page than the host supports, only the first
  166. * host page is mapped here and the rest are demand faulted.
  167. *
  168. * If the guest wanted a smaller page than the host page size, we map only the
  169. * guest-size page (i.e. not a full host page mapping).
  170. *
  171. * Caller must ensure that the specified guest TLB entry is safe to insert into
  172. * the shadow TLB.
  173. */
  174. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, u64 asid,
  175. u32 flags, u32 max_bytes, unsigned int gtlb_index)
  176. {
  177. struct kvmppc_44x_tlbe stlbe;
  178. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  179. struct kvmppc_44x_shadow_ref *ref;
  180. struct page *new_page;
  181. hpa_t hpaddr;
  182. gfn_t gfn;
  183. unsigned int victim;
  184. /* Select TLB entry to clobber. Indirectly guard against races with the TLB
  185. * miss handler by disabling interrupts. */
  186. local_irq_disable();
  187. victim = ++tlb_44x_index;
  188. if (victim > tlb_44x_hwater)
  189. victim = 0;
  190. tlb_44x_index = victim;
  191. local_irq_enable();
  192. /* Get reference to new page. */
  193. gfn = gpaddr >> PAGE_SHIFT;
  194. new_page = gfn_to_page(vcpu->kvm, gfn);
  195. if (is_error_page(new_page)) {
  196. printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
  197. kvm_release_page_clean(new_page);
  198. return;
  199. }
  200. hpaddr = page_to_phys(new_page);
  201. /* Invalidate any previous shadow mappings. */
  202. kvmppc_44x_shadow_release(vcpu_44x, victim);
  203. /* XXX Make sure (va, size) doesn't overlap any other
  204. * entries. 440x6 user manual says the result would be
  205. * "undefined." */
  206. /* XXX what about AS? */
  207. /* Force TS=1 for all guest mappings. */
  208. stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS;
  209. if (max_bytes >= PAGE_SIZE) {
  210. /* Guest mapping is larger than or equal to host page size. We can use
  211. * a "native" host mapping. */
  212. stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE;
  213. } else {
  214. /* Guest mapping is smaller than host page size. We must restrict the
  215. * size of the mapping to be at most the smaller of the two, but for
  216. * simplicity we fall back to a 4K mapping (this is probably what the
  217. * guest is using anyways). */
  218. stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K;
  219. /* 'hpaddr' is a host page, which is larger than the mapping we're
  220. * inserting here. To compensate, we must add the in-page offset to the
  221. * sub-page. */
  222. hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K);
  223. }
  224. stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
  225. stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags,
  226. vcpu->arch.msr & MSR_PR);
  227. stlbe.tid = !(asid & 0xff);
  228. /* Keep track of the reference so we can properly release it later. */
  229. ref = &vcpu_44x->shadow_refs[victim];
  230. ref->page = new_page;
  231. ref->gtlb_index = gtlb_index;
  232. ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW);
  233. ref->tid = stlbe.tid;
  234. /* Insert shadow mapping into hardware TLB. */
  235. kvmppc_44x_tlbwe(victim, &stlbe);
  236. KVMTRACE_5D(STLB_WRITE, vcpu, victim, stlbe.tid, stlbe.word0, stlbe.word1,
  237. stlbe.word2, handler);
  238. }
  239. /* For a particular guest TLB entry, invalidate the corresponding host TLB
  240. * mappings and release the host pages. */
  241. static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu,
  242. unsigned int gtlb_index)
  243. {
  244. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  245. int i;
  246. for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
  247. struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
  248. if (ref->gtlb_index == gtlb_index)
  249. kvmppc_44x_shadow_release(vcpu_44x, i);
  250. }
  251. }
  252. void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
  253. {
  254. vcpu->arch.shadow_pid = !usermode;
  255. }
  256. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
  257. {
  258. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  259. int i;
  260. if (unlikely(vcpu->arch.pid == new_pid))
  261. return;
  262. vcpu->arch.pid = new_pid;
  263. /* Guest userspace runs with TID=0 mappings and PID=0, to make sure it
  264. * can't access guest kernel mappings (TID=1). When we switch to a new
  265. * guest PID, which will also use host PID=0, we must discard the old guest
  266. * userspace mappings. */
  267. for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
  268. struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
  269. if (ref->tid == 0)
  270. kvmppc_44x_shadow_release(vcpu_44x, i);
  271. }
  272. }
  273. static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
  274. const struct kvmppc_44x_tlbe *tlbe)
  275. {
  276. gpa_t gpa;
  277. if (!get_tlb_v(tlbe))
  278. return 0;
  279. /* Does it match current guest AS? */
  280. /* XXX what about IS != DS? */
  281. if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
  282. return 0;
  283. gpa = get_tlb_raddr(tlbe);
  284. if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
  285. /* Mapping is not for RAM. */
  286. return 0;
  287. return 1;
  288. }
  289. int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
  290. {
  291. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  292. struct kvmppc_44x_tlbe *tlbe;
  293. unsigned int gtlb_index;
  294. gtlb_index = vcpu->arch.gpr[ra];
  295. if (gtlb_index > KVM44x_GUEST_TLB_SIZE) {
  296. printk("%s: index %d\n", __func__, gtlb_index);
  297. kvmppc_dump_vcpu(vcpu);
  298. return EMULATE_FAIL;
  299. }
  300. tlbe = &vcpu_44x->guest_tlb[gtlb_index];
  301. /* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */
  302. if (tlbe->word0 & PPC44x_TLB_VALID)
  303. kvmppc_44x_invalidate(vcpu, gtlb_index);
  304. switch (ws) {
  305. case PPC44x_TLB_PAGEID:
  306. tlbe->tid = get_mmucr_stid(vcpu);
  307. tlbe->word0 = vcpu->arch.gpr[rs];
  308. break;
  309. case PPC44x_TLB_XLAT:
  310. tlbe->word1 = vcpu->arch.gpr[rs];
  311. break;
  312. case PPC44x_TLB_ATTRIB:
  313. tlbe->word2 = vcpu->arch.gpr[rs];
  314. break;
  315. default:
  316. return EMULATE_FAIL;
  317. }
  318. if (tlbe_is_host_safe(vcpu, tlbe)) {
  319. u64 asid;
  320. gva_t eaddr;
  321. gpa_t gpaddr;
  322. u32 flags;
  323. u32 bytes;
  324. eaddr = get_tlb_eaddr(tlbe);
  325. gpaddr = get_tlb_raddr(tlbe);
  326. /* Use the advertised page size to mask effective and real addrs. */
  327. bytes = get_tlb_bytes(tlbe);
  328. eaddr &= ~(bytes - 1);
  329. gpaddr &= ~(bytes - 1);
  330. asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
  331. flags = tlbe->word2 & 0xffff;
  332. kvmppc_mmu_map(vcpu, eaddr, gpaddr, asid, flags, bytes, gtlb_index);
  333. }
  334. KVMTRACE_5D(GTLB_WRITE, vcpu, gtlb_index, tlbe->tid, tlbe->word0,
  335. tlbe->word1, tlbe->word2, handler);
  336. return EMULATE_DONE;
  337. }
  338. int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
  339. {
  340. u32 ea;
  341. int gtlb_index;
  342. unsigned int as = get_mmucr_sts(vcpu);
  343. unsigned int pid = get_mmucr_stid(vcpu);
  344. ea = vcpu->arch.gpr[rb];
  345. if (ra)
  346. ea += vcpu->arch.gpr[ra];
  347. gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
  348. if (rc) {
  349. if (gtlb_index < 0)
  350. vcpu->arch.cr &= ~0x20000000;
  351. else
  352. vcpu->arch.cr |= 0x20000000;
  353. }
  354. vcpu->arch.gpr[rt] = gtlb_index;
  355. return EMULATE_DONE;
  356. }