kvm_tlb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * KVM/MIPS TLB handling, this file is part of the Linux host kernel so that
  7. * TLB handlers run from KSEG0
  8. *
  9. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  10. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/kvm_host.h>
  19. #include <linux/srcu.h>
  20. #include <asm/cpu.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/cacheflush.h>
  25. #undef CONFIG_MIPS_MT
  26. #include <asm/r4kcache.h>
  27. #define CONFIG_MIPS_MT
  28. #define KVM_GUEST_PC_TLB 0
  29. #define KVM_GUEST_SP_TLB 1
  30. #define PRIx64 "llx"
  31. /* Use VZ EntryHi.EHINV to invalidate TLB entries */
  32. #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
  33. atomic_t kvm_mips_instance;
  34. EXPORT_SYMBOL(kvm_mips_instance);
  35. /* These function pointers are initialized once the KVM module is loaded */
  36. pfn_t(*kvm_mips_gfn_to_pfn) (struct kvm *kvm, gfn_t gfn);
  37. EXPORT_SYMBOL(kvm_mips_gfn_to_pfn);
  38. void (*kvm_mips_release_pfn_clean) (pfn_t pfn);
  39. EXPORT_SYMBOL(kvm_mips_release_pfn_clean);
  40. bool(*kvm_mips_is_error_pfn) (pfn_t pfn);
  41. EXPORT_SYMBOL(kvm_mips_is_error_pfn);
  42. uint32_t kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
  43. {
  44. return vcpu->arch.guest_kernel_asid[smp_processor_id()] & ASID_MASK;
  45. }
  46. uint32_t kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
  47. {
  48. return vcpu->arch.guest_user_asid[smp_processor_id()] & ASID_MASK;
  49. }
  50. inline uint32_t kvm_mips_get_commpage_asid (struct kvm_vcpu *vcpu)
  51. {
  52. return vcpu->kvm->arch.commpage_tlb;
  53. }
  54. /*
  55. * Structure defining an tlb entry data set.
  56. */
  57. void kvm_mips_dump_host_tlbs(void)
  58. {
  59. unsigned long old_entryhi;
  60. unsigned long old_pagemask;
  61. struct kvm_mips_tlb tlb;
  62. unsigned long flags;
  63. int i;
  64. local_irq_save(flags);
  65. old_entryhi = read_c0_entryhi();
  66. old_pagemask = read_c0_pagemask();
  67. printk("HOST TLBs:\n");
  68. printk("ASID: %#lx\n", read_c0_entryhi() & ASID_MASK);
  69. for (i = 0; i < current_cpu_data.tlbsize; i++) {
  70. write_c0_index(i);
  71. mtc0_tlbw_hazard();
  72. tlb_read();
  73. tlbw_use_hazard();
  74. tlb.tlb_hi = read_c0_entryhi();
  75. tlb.tlb_lo0 = read_c0_entrylo0();
  76. tlb.tlb_lo1 = read_c0_entrylo1();
  77. tlb.tlb_mask = read_c0_pagemask();
  78. printk("TLB%c%3d Hi 0x%08lx ",
  79. (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
  80. i, tlb.tlb_hi);
  81. printk("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
  82. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
  83. (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
  84. (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
  85. (tlb.tlb_lo0 >> 3) & 7);
  86. printk("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
  87. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
  88. (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
  89. (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
  90. (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
  91. }
  92. write_c0_entryhi(old_entryhi);
  93. write_c0_pagemask(old_pagemask);
  94. mtc0_tlbw_hazard();
  95. local_irq_restore(flags);
  96. }
  97. void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
  98. {
  99. struct mips_coproc *cop0 = vcpu->arch.cop0;
  100. struct kvm_mips_tlb tlb;
  101. int i;
  102. printk("Guest TLBs:\n");
  103. printk("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
  104. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  105. tlb = vcpu->arch.guest_tlb[i];
  106. printk("TLB%c%3d Hi 0x%08lx ",
  107. (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
  108. i, tlb.tlb_hi);
  109. printk("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
  110. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
  111. (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
  112. (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
  113. (tlb.tlb_lo0 >> 3) & 7);
  114. printk("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
  115. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
  116. (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
  117. (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
  118. (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
  119. }
  120. }
  121. void kvm_mips_dump_shadow_tlbs(struct kvm_vcpu *vcpu)
  122. {
  123. int i;
  124. volatile struct kvm_mips_tlb tlb;
  125. printk("Shadow TLBs:\n");
  126. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  127. tlb = vcpu->arch.shadow_tlb[smp_processor_id()][i];
  128. printk("TLB%c%3d Hi 0x%08lx ",
  129. (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
  130. i, tlb.tlb_hi);
  131. printk("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
  132. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
  133. (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
  134. (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
  135. (tlb.tlb_lo0 >> 3) & 7);
  136. printk("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
  137. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
  138. (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
  139. (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
  140. (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
  141. }
  142. }
  143. static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn)
  144. {
  145. int srcu_idx, err = 0;
  146. pfn_t pfn;
  147. if (kvm->arch.guest_pmap[gfn] != KVM_INVALID_PAGE)
  148. return 0;
  149. srcu_idx = srcu_read_lock(&kvm->srcu);
  150. pfn = kvm_mips_gfn_to_pfn(kvm, gfn);
  151. if (kvm_mips_is_error_pfn(pfn)) {
  152. kvm_err("Couldn't get pfn for gfn %#" PRIx64 "!\n", gfn);
  153. err = -EFAULT;
  154. goto out;
  155. }
  156. kvm->arch.guest_pmap[gfn] = pfn;
  157. out:
  158. srcu_read_unlock(&kvm->srcu, srcu_idx);
  159. return err;
  160. }
  161. /* Translate guest KSEG0 addresses to Host PA */
  162. unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
  163. unsigned long gva)
  164. {
  165. gfn_t gfn;
  166. uint32_t offset = gva & ~PAGE_MASK;
  167. struct kvm *kvm = vcpu->kvm;
  168. if (KVM_GUEST_KSEGX(gva) != KVM_GUEST_KSEG0) {
  169. kvm_err("%s/%p: Invalid gva: %#lx\n", __func__,
  170. __builtin_return_address(0), gva);
  171. return KVM_INVALID_PAGE;
  172. }
  173. gfn = (KVM_GUEST_CPHYSADDR(gva) >> PAGE_SHIFT);
  174. if (gfn >= kvm->arch.guest_pmap_npages) {
  175. kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__, gfn,
  176. gva);
  177. return KVM_INVALID_PAGE;
  178. }
  179. if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
  180. return KVM_INVALID_ADDR;
  181. return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
  182. }
  183. /* XXXKYMA: Must be called with interrupts disabled */
  184. /* set flush_dcache_mask == 0 if no dcache flush required */
  185. int
  186. kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
  187. unsigned long entrylo0, unsigned long entrylo1, int flush_dcache_mask)
  188. {
  189. unsigned long flags;
  190. unsigned long old_entryhi;
  191. volatile int idx;
  192. local_irq_save(flags);
  193. old_entryhi = read_c0_entryhi();
  194. write_c0_entryhi(entryhi);
  195. mtc0_tlbw_hazard();
  196. tlb_probe();
  197. tlb_probe_hazard();
  198. idx = read_c0_index();
  199. if (idx > current_cpu_data.tlbsize) {
  200. kvm_err("%s: Invalid Index: %d\n", __func__, idx);
  201. kvm_mips_dump_host_tlbs();
  202. return -1;
  203. }
  204. if (idx < 0) {
  205. idx = read_c0_random() % current_cpu_data.tlbsize;
  206. write_c0_index(idx);
  207. mtc0_tlbw_hazard();
  208. }
  209. write_c0_entrylo0(entrylo0);
  210. write_c0_entrylo1(entrylo1);
  211. mtc0_tlbw_hazard();
  212. tlb_write_indexed();
  213. tlbw_use_hazard();
  214. #ifdef DEBUG
  215. if (debug) {
  216. kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] "
  217. "entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
  218. vcpu->arch.pc, idx, read_c0_entryhi(),
  219. read_c0_entrylo0(), read_c0_entrylo1());
  220. }
  221. #endif
  222. /* Flush D-cache */
  223. if (flush_dcache_mask) {
  224. if (entrylo0 & MIPS3_PG_V) {
  225. ++vcpu->stat.flush_dcache_exits;
  226. flush_data_cache_page((entryhi & VPN2_MASK) & ~flush_dcache_mask);
  227. }
  228. if (entrylo1 & MIPS3_PG_V) {
  229. ++vcpu->stat.flush_dcache_exits;
  230. flush_data_cache_page(((entryhi & VPN2_MASK) & ~flush_dcache_mask) |
  231. (0x1 << PAGE_SHIFT));
  232. }
  233. }
  234. /* Restore old ASID */
  235. write_c0_entryhi(old_entryhi);
  236. mtc0_tlbw_hazard();
  237. tlbw_use_hazard();
  238. local_irq_restore(flags);
  239. return 0;
  240. }
  241. /* XXXKYMA: Must be called with interrupts disabled */
  242. int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
  243. struct kvm_vcpu *vcpu)
  244. {
  245. gfn_t gfn;
  246. pfn_t pfn0, pfn1;
  247. unsigned long vaddr = 0;
  248. unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
  249. int even;
  250. struct kvm *kvm = vcpu->kvm;
  251. const int flush_dcache_mask = 0;
  252. if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
  253. kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
  254. kvm_mips_dump_host_tlbs();
  255. return -1;
  256. }
  257. gfn = (KVM_GUEST_CPHYSADDR(badvaddr) >> PAGE_SHIFT);
  258. if (gfn >= kvm->arch.guest_pmap_npages) {
  259. kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__,
  260. gfn, badvaddr);
  261. kvm_mips_dump_host_tlbs();
  262. return -1;
  263. }
  264. even = !(gfn & 0x1);
  265. vaddr = badvaddr & (PAGE_MASK << 1);
  266. if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
  267. return -1;
  268. if (kvm_mips_map_page(vcpu->kvm, gfn ^ 0x1) < 0)
  269. return -1;
  270. if (even) {
  271. pfn0 = kvm->arch.guest_pmap[gfn];
  272. pfn1 = kvm->arch.guest_pmap[gfn ^ 0x1];
  273. } else {
  274. pfn0 = kvm->arch.guest_pmap[gfn ^ 0x1];
  275. pfn1 = kvm->arch.guest_pmap[gfn];
  276. }
  277. entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
  278. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) | (1 << 2) |
  279. (0x1 << 1);
  280. entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) | (1 << 2) |
  281. (0x1 << 1);
  282. return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
  283. flush_dcache_mask);
  284. }
  285. int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
  286. struct kvm_vcpu *vcpu)
  287. {
  288. pfn_t pfn0, pfn1;
  289. unsigned long flags, old_entryhi = 0, vaddr = 0;
  290. unsigned long entrylo0 = 0, entrylo1 = 0;
  291. pfn0 = CPHYSADDR(vcpu->arch.kseg0_commpage) >> PAGE_SHIFT;
  292. pfn1 = 0;
  293. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) | (1 << 2) |
  294. (0x1 << 1);
  295. entrylo1 = 0;
  296. local_irq_save(flags);
  297. old_entryhi = read_c0_entryhi();
  298. vaddr = badvaddr & (PAGE_MASK << 1);
  299. write_c0_entryhi(vaddr | kvm_mips_get_kernel_asid(vcpu));
  300. mtc0_tlbw_hazard();
  301. write_c0_entrylo0(entrylo0);
  302. mtc0_tlbw_hazard();
  303. write_c0_entrylo1(entrylo1);
  304. mtc0_tlbw_hazard();
  305. write_c0_index(kvm_mips_get_commpage_asid(vcpu));
  306. mtc0_tlbw_hazard();
  307. tlb_write_indexed();
  308. mtc0_tlbw_hazard();
  309. tlbw_use_hazard();
  310. #ifdef DEBUG
  311. kvm_debug ("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
  312. vcpu->arch.pc, read_c0_index(), read_c0_entryhi(),
  313. read_c0_entrylo0(), read_c0_entrylo1());
  314. #endif
  315. /* Restore old ASID */
  316. write_c0_entryhi(old_entryhi);
  317. mtc0_tlbw_hazard();
  318. tlbw_use_hazard();
  319. local_irq_restore(flags);
  320. return 0;
  321. }
  322. int
  323. kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
  324. struct kvm_mips_tlb *tlb, unsigned long *hpa0, unsigned long *hpa1)
  325. {
  326. unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
  327. struct kvm *kvm = vcpu->kvm;
  328. pfn_t pfn0, pfn1;
  329. if ((tlb->tlb_hi & VPN2_MASK) == 0) {
  330. pfn0 = 0;
  331. pfn1 = 0;
  332. } else {
  333. if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo0) >> PAGE_SHIFT) < 0)
  334. return -1;
  335. if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo1) >> PAGE_SHIFT) < 0)
  336. return -1;
  337. pfn0 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo0) >> PAGE_SHIFT];
  338. pfn1 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo1) >> PAGE_SHIFT];
  339. }
  340. if (hpa0)
  341. *hpa0 = pfn0 << PAGE_SHIFT;
  342. if (hpa1)
  343. *hpa1 = pfn1 << PAGE_SHIFT;
  344. /* Get attributes from the Guest TLB */
  345. entryhi = (tlb->tlb_hi & VPN2_MASK) | (KVM_GUEST_KERNEL_MODE(vcpu) ?
  346. kvm_mips_get_kernel_asid(vcpu) : kvm_mips_get_user_asid(vcpu));
  347. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
  348. (tlb->tlb_lo0 & MIPS3_PG_D) | (tlb->tlb_lo0 & MIPS3_PG_V);
  349. entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
  350. (tlb->tlb_lo1 & MIPS3_PG_D) | (tlb->tlb_lo1 & MIPS3_PG_V);
  351. #ifdef DEBUG
  352. kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
  353. tlb->tlb_lo0, tlb->tlb_lo1);
  354. #endif
  355. return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
  356. tlb->tlb_mask);
  357. }
  358. int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
  359. {
  360. int i;
  361. int index = -1;
  362. struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
  363. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  364. if (((TLB_VPN2(tlb[i]) & ~tlb[i].tlb_mask) == ((entryhi & VPN2_MASK) & ~tlb[i].tlb_mask)) &&
  365. (TLB_IS_GLOBAL(tlb[i]) || (TLB_ASID(tlb[i]) == (entryhi & ASID_MASK)))) {
  366. index = i;
  367. break;
  368. }
  369. }
  370. #ifdef DEBUG
  371. kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
  372. __func__, entryhi, index, tlb[i].tlb_lo0, tlb[i].tlb_lo1);
  373. #endif
  374. return index;
  375. }
  376. int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr)
  377. {
  378. unsigned long old_entryhi, flags;
  379. volatile int idx;
  380. local_irq_save(flags);
  381. old_entryhi = read_c0_entryhi();
  382. if (KVM_GUEST_KERNEL_MODE(vcpu))
  383. write_c0_entryhi((vaddr & VPN2_MASK) | kvm_mips_get_kernel_asid(vcpu));
  384. else {
  385. write_c0_entryhi((vaddr & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
  386. }
  387. mtc0_tlbw_hazard();
  388. tlb_probe();
  389. tlb_probe_hazard();
  390. idx = read_c0_index();
  391. /* Restore old ASID */
  392. write_c0_entryhi(old_entryhi);
  393. mtc0_tlbw_hazard();
  394. tlbw_use_hazard();
  395. local_irq_restore(flags);
  396. #ifdef DEBUG
  397. kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr, idx);
  398. #endif
  399. return idx;
  400. }
  401. int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
  402. {
  403. int idx;
  404. unsigned long flags, old_entryhi;
  405. local_irq_save(flags);
  406. old_entryhi = read_c0_entryhi();
  407. write_c0_entryhi((va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
  408. mtc0_tlbw_hazard();
  409. tlb_probe();
  410. tlb_probe_hazard();
  411. idx = read_c0_index();
  412. if (idx >= current_cpu_data.tlbsize)
  413. BUG();
  414. if (idx > 0) {
  415. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  416. mtc0_tlbw_hazard();
  417. write_c0_entrylo0(0);
  418. mtc0_tlbw_hazard();
  419. write_c0_entrylo1(0);
  420. mtc0_tlbw_hazard();
  421. tlb_write_indexed();
  422. mtc0_tlbw_hazard();
  423. }
  424. write_c0_entryhi(old_entryhi);
  425. mtc0_tlbw_hazard();
  426. tlbw_use_hazard();
  427. local_irq_restore(flags);
  428. #ifdef DEBUG
  429. if (idx > 0) {
  430. kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__,
  431. (va & VPN2_MASK) | (vcpu->arch.asid_map[va & ASID_MASK] & ASID_MASK), idx);
  432. }
  433. #endif
  434. return 0;
  435. }
  436. /* XXXKYMA: Fix Guest USER/KERNEL no longer share the same ASID*/
  437. int kvm_mips_host_tlb_inv_index(struct kvm_vcpu *vcpu, int index)
  438. {
  439. unsigned long flags, old_entryhi;
  440. if (index >= current_cpu_data.tlbsize)
  441. BUG();
  442. local_irq_save(flags);
  443. old_entryhi = read_c0_entryhi();
  444. write_c0_entryhi(UNIQUE_ENTRYHI(index));
  445. mtc0_tlbw_hazard();
  446. write_c0_index(index);
  447. mtc0_tlbw_hazard();
  448. write_c0_entrylo0(0);
  449. mtc0_tlbw_hazard();
  450. write_c0_entrylo1(0);
  451. mtc0_tlbw_hazard();
  452. tlb_write_indexed();
  453. mtc0_tlbw_hazard();
  454. tlbw_use_hazard();
  455. write_c0_entryhi(old_entryhi);
  456. mtc0_tlbw_hazard();
  457. tlbw_use_hazard();
  458. local_irq_restore(flags);
  459. return 0;
  460. }
  461. void kvm_mips_flush_host_tlb(int skip_kseg0)
  462. {
  463. unsigned long flags;
  464. unsigned long old_entryhi, entryhi;
  465. unsigned long old_pagemask;
  466. int entry = 0;
  467. int maxentry = current_cpu_data.tlbsize;
  468. local_irq_save(flags);
  469. old_entryhi = read_c0_entryhi();
  470. old_pagemask = read_c0_pagemask();
  471. /* Blast 'em all away. */
  472. for (entry = 0; entry < maxentry; entry++) {
  473. write_c0_index(entry);
  474. mtc0_tlbw_hazard();
  475. if (skip_kseg0) {
  476. tlb_read();
  477. tlbw_use_hazard();
  478. entryhi = read_c0_entryhi();
  479. /* Don't blow away guest kernel entries */
  480. if (KVM_GUEST_KSEGX(entryhi) == KVM_GUEST_KSEG0) {
  481. continue;
  482. }
  483. }
  484. /* Make sure all entries differ. */
  485. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  486. mtc0_tlbw_hazard();
  487. write_c0_entrylo0(0);
  488. mtc0_tlbw_hazard();
  489. write_c0_entrylo1(0);
  490. mtc0_tlbw_hazard();
  491. tlb_write_indexed();
  492. mtc0_tlbw_hazard();
  493. }
  494. tlbw_use_hazard();
  495. write_c0_entryhi(old_entryhi);
  496. write_c0_pagemask(old_pagemask);
  497. mtc0_tlbw_hazard();
  498. tlbw_use_hazard();
  499. local_irq_restore(flags);
  500. }
  501. void
  502. kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
  503. struct kvm_vcpu *vcpu)
  504. {
  505. unsigned long asid = asid_cache(cpu);
  506. if (!((asid += ASID_INC) & ASID_MASK)) {
  507. if (cpu_has_vtag_icache) {
  508. flush_icache_all();
  509. }
  510. kvm_local_flush_tlb_all(); /* start new asid cycle */
  511. if (!asid) /* fix version if needed */
  512. asid = ASID_FIRST_VERSION;
  513. }
  514. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  515. }
  516. void kvm_shadow_tlb_put(struct kvm_vcpu *vcpu)
  517. {
  518. unsigned long flags;
  519. unsigned long old_entryhi;
  520. unsigned long old_pagemask;
  521. int entry = 0;
  522. int cpu = smp_processor_id();
  523. local_irq_save(flags);
  524. old_entryhi = read_c0_entryhi();
  525. old_pagemask = read_c0_pagemask();
  526. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  527. write_c0_index(entry);
  528. mtc0_tlbw_hazard();
  529. tlb_read();
  530. tlbw_use_hazard();
  531. vcpu->arch.shadow_tlb[cpu][entry].tlb_hi = read_c0_entryhi();
  532. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo0 = read_c0_entrylo0();
  533. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo1 = read_c0_entrylo1();
  534. vcpu->arch.shadow_tlb[cpu][entry].tlb_mask = read_c0_pagemask();
  535. }
  536. write_c0_entryhi(old_entryhi);
  537. write_c0_pagemask(old_pagemask);
  538. mtc0_tlbw_hazard();
  539. local_irq_restore(flags);
  540. }
  541. void kvm_shadow_tlb_load(struct kvm_vcpu *vcpu)
  542. {
  543. unsigned long flags;
  544. unsigned long old_ctx;
  545. int entry;
  546. int cpu = smp_processor_id();
  547. local_irq_save(flags);
  548. old_ctx = read_c0_entryhi();
  549. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  550. write_c0_entryhi(vcpu->arch.shadow_tlb[cpu][entry].tlb_hi);
  551. mtc0_tlbw_hazard();
  552. write_c0_entrylo0(vcpu->arch.shadow_tlb[cpu][entry].tlb_lo0);
  553. write_c0_entrylo1(vcpu->arch.shadow_tlb[cpu][entry].tlb_lo1);
  554. write_c0_index(entry);
  555. mtc0_tlbw_hazard();
  556. tlb_write_indexed();
  557. tlbw_use_hazard();
  558. }
  559. tlbw_use_hazard();
  560. write_c0_entryhi(old_ctx);
  561. mtc0_tlbw_hazard();
  562. local_irq_restore(flags);
  563. }
  564. void kvm_local_flush_tlb_all(void)
  565. {
  566. unsigned long flags;
  567. unsigned long old_ctx;
  568. int entry = 0;
  569. local_irq_save(flags);
  570. /* Save old context and create impossible VPN2 value */
  571. old_ctx = read_c0_entryhi();
  572. write_c0_entrylo0(0);
  573. write_c0_entrylo1(0);
  574. /* Blast 'em all away. */
  575. while (entry < current_cpu_data.tlbsize) {
  576. /* Make sure all entries differ. */
  577. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  578. write_c0_index(entry);
  579. mtc0_tlbw_hazard();
  580. tlb_write_indexed();
  581. entry++;
  582. }
  583. tlbw_use_hazard();
  584. write_c0_entryhi(old_ctx);
  585. mtc0_tlbw_hazard();
  586. local_irq_restore(flags);
  587. }
  588. void kvm_mips_init_shadow_tlb(struct kvm_vcpu *vcpu)
  589. {
  590. int cpu, entry;
  591. for_each_possible_cpu(cpu) {
  592. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  593. vcpu->arch.shadow_tlb[cpu][entry].tlb_hi =
  594. UNIQUE_ENTRYHI(entry);
  595. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo0 = 0x0;
  596. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo1 = 0x0;
  597. vcpu->arch.shadow_tlb[cpu][entry].tlb_mask =
  598. read_c0_pagemask();
  599. #ifdef DEBUG
  600. kvm_debug
  601. ("shadow_tlb[%d][%d]: tlb_hi: %#lx, lo0: %#lx, lo1: %#lx\n",
  602. cpu, entry,
  603. vcpu->arch.shadow_tlb[cpu][entry].tlb_hi,
  604. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo0,
  605. vcpu->arch.shadow_tlb[cpu][entry].tlb_lo1);
  606. #endif
  607. }
  608. }
  609. }
  610. /* Restore ASID once we are scheduled back after preemption */
  611. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  612. {
  613. unsigned long flags;
  614. int newasid = 0;
  615. #ifdef DEBUG
  616. kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
  617. #endif
  618. /* Alocate new kernel and user ASIDs if needed */
  619. local_irq_save(flags);
  620. if (((vcpu->arch.
  621. guest_kernel_asid[cpu] ^ asid_cache(cpu)) & ASID_VERSION_MASK)) {
  622. kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
  623. vcpu->arch.guest_kernel_asid[cpu] =
  624. vcpu->arch.guest_kernel_mm.context.asid[cpu];
  625. kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
  626. vcpu->arch.guest_user_asid[cpu] =
  627. vcpu->arch.guest_user_mm.context.asid[cpu];
  628. newasid++;
  629. kvm_info("[%d]: cpu_context: %#lx\n", cpu,
  630. cpu_context(cpu, current->mm));
  631. kvm_info("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
  632. cpu, vcpu->arch.guest_kernel_asid[cpu]);
  633. kvm_info("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
  634. vcpu->arch.guest_user_asid[cpu]);
  635. }
  636. if (vcpu->arch.last_sched_cpu != cpu) {
  637. kvm_info("[%d->%d]KVM VCPU[%d] switch\n",
  638. vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
  639. }
  640. /* Only reload shadow host TLB if new ASIDs haven't been allocated */
  641. #if 0
  642. if ((atomic_read(&kvm_mips_instance) > 1) && !newasid) {
  643. kvm_mips_flush_host_tlb(0);
  644. kvm_shadow_tlb_load(vcpu);
  645. }
  646. #endif
  647. if (!newasid) {
  648. /* If we preempted while the guest was executing, then reload the pre-empted ASID */
  649. if (current->flags & PF_VCPU) {
  650. write_c0_entryhi(vcpu->arch.
  651. preempt_entryhi & ASID_MASK);
  652. ehb();
  653. }
  654. } else {
  655. /* New ASIDs were allocated for the VM */
  656. /* Were we in guest context? If so then the pre-empted ASID is no longer
  657. * valid, we need to set it to what it should be based on the mode of
  658. * the Guest (Kernel/User)
  659. */
  660. if (current->flags & PF_VCPU) {
  661. if (KVM_GUEST_KERNEL_MODE(vcpu))
  662. write_c0_entryhi(vcpu->arch.
  663. guest_kernel_asid[cpu] &
  664. ASID_MASK);
  665. else
  666. write_c0_entryhi(vcpu->arch.
  667. guest_user_asid[cpu] &
  668. ASID_MASK);
  669. ehb();
  670. }
  671. }
  672. local_irq_restore(flags);
  673. }
  674. /* ASID can change if another task is scheduled during preemption */
  675. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  676. {
  677. unsigned long flags;
  678. uint32_t cpu;
  679. local_irq_save(flags);
  680. cpu = smp_processor_id();
  681. vcpu->arch.preempt_entryhi = read_c0_entryhi();
  682. vcpu->arch.last_sched_cpu = cpu;
  683. #if 0
  684. if ((atomic_read(&kvm_mips_instance) > 1)) {
  685. kvm_shadow_tlb_put(vcpu);
  686. }
  687. #endif
  688. if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
  689. ASID_VERSION_MASK)) {
  690. kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
  691. cpu_context(cpu, current->mm));
  692. drop_mmu_context(current->mm, cpu);
  693. }
  694. write_c0_entryhi(cpu_asid(cpu, current->mm));
  695. ehb();
  696. local_irq_restore(flags);
  697. }
  698. uint32_t kvm_get_inst(uint32_t *opc, struct kvm_vcpu *vcpu)
  699. {
  700. struct mips_coproc *cop0 = vcpu->arch.cop0;
  701. unsigned long paddr, flags;
  702. uint32_t inst;
  703. int index;
  704. if (KVM_GUEST_KSEGX((unsigned long) opc) < KVM_GUEST_KSEG0 ||
  705. KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  706. local_irq_save(flags);
  707. index = kvm_mips_host_tlb_lookup(vcpu, (unsigned long) opc);
  708. if (index >= 0) {
  709. inst = *(opc);
  710. } else {
  711. index =
  712. kvm_mips_guest_tlb_lookup(vcpu,
  713. ((unsigned long) opc & VPN2_MASK)
  714. |
  715. (kvm_read_c0_guest_entryhi
  716. (cop0) & ASID_MASK));
  717. if (index < 0) {
  718. kvm_err
  719. ("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
  720. __func__, opc, vcpu, read_c0_entryhi());
  721. kvm_mips_dump_host_tlbs();
  722. local_irq_restore(flags);
  723. return KVM_INVALID_INST;
  724. }
  725. kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
  726. &vcpu->arch.
  727. guest_tlb[index],
  728. NULL, NULL);
  729. inst = *(opc);
  730. }
  731. local_irq_restore(flags);
  732. } else if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  733. paddr =
  734. kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
  735. (unsigned long) opc);
  736. inst = *(uint32_t *) CKSEG0ADDR(paddr);
  737. } else {
  738. kvm_err("%s: illegal address: %p\n", __func__, opc);
  739. return KVM_INVALID_INST;
  740. }
  741. return inst;
  742. }
  743. EXPORT_SYMBOL(kvm_local_flush_tlb_all);
  744. EXPORT_SYMBOL(kvm_shadow_tlb_put);
  745. EXPORT_SYMBOL(kvm_mips_handle_mapped_seg_tlb_fault);
  746. EXPORT_SYMBOL(kvm_mips_handle_commpage_tlb_fault);
  747. EXPORT_SYMBOL(kvm_mips_init_shadow_tlb);
  748. EXPORT_SYMBOL(kvm_mips_dump_host_tlbs);
  749. EXPORT_SYMBOL(kvm_mips_handle_kseg0_tlb_fault);
  750. EXPORT_SYMBOL(kvm_mips_host_tlb_lookup);
  751. EXPORT_SYMBOL(kvm_mips_flush_host_tlb);
  752. EXPORT_SYMBOL(kvm_mips_guest_tlb_lookup);
  753. EXPORT_SYMBOL(kvm_mips_host_tlb_inv);
  754. EXPORT_SYMBOL(kvm_mips_translate_guest_kseg0_to_hpa);
  755. EXPORT_SYMBOL(kvm_shadow_tlb_load);
  756. EXPORT_SYMBOL(kvm_mips_dump_shadow_tlbs);
  757. EXPORT_SYMBOL(kvm_mips_dump_guest_tlbs);
  758. EXPORT_SYMBOL(kvm_get_inst);
  759. EXPORT_SYMBOL(kvm_arch_vcpu_load);
  760. EXPORT_SYMBOL(kvm_arch_vcpu_put);