44x_tlb.c 13 KB

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