44x_tlb.c 14 KB

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