tlb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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/sched.h>
  13. #include <linux/smp.h>
  14. #include <linux/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/srcu.h>
  19. #include <asm/cpu.h>
  20. #include <asm/bootinfo.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlb.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. atomic_t kvm_mips_instance;
  32. EXPORT_SYMBOL_GPL(kvm_mips_instance);
  33. /* These function pointers are initialized once the KVM module is loaded */
  34. kvm_pfn_t (*kvm_mips_gfn_to_pfn)(struct kvm *kvm, gfn_t gfn);
  35. EXPORT_SYMBOL_GPL(kvm_mips_gfn_to_pfn);
  36. void (*kvm_mips_release_pfn_clean)(kvm_pfn_t pfn);
  37. EXPORT_SYMBOL_GPL(kvm_mips_release_pfn_clean);
  38. bool (*kvm_mips_is_error_pfn)(kvm_pfn_t pfn);
  39. EXPORT_SYMBOL_GPL(kvm_mips_is_error_pfn);
  40. uint32_t kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
  41. {
  42. int cpu = smp_processor_id();
  43. return vcpu->arch.guest_kernel_asid[cpu] &
  44. cpu_asid_mask(&cpu_data[cpu]);
  45. }
  46. uint32_t kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
  47. {
  48. int cpu = smp_processor_id();
  49. return vcpu->arch.guest_user_asid[cpu] &
  50. cpu_asid_mask(&cpu_data[cpu]);
  51. }
  52. inline uint32_t kvm_mips_get_commpage_asid(struct kvm_vcpu *vcpu)
  53. {
  54. return vcpu->kvm->arch.commpage_tlb;
  55. }
  56. /* Structure defining an tlb entry data set. */
  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. kvm_info("HOST TLBs:\n");
  68. kvm_info("ASID: %#lx\n", read_c0_entryhi() &
  69. cpu_asid_mask(&current_cpu_data));
  70. for (i = 0; i < current_cpu_data.tlbsize; i++) {
  71. write_c0_index(i);
  72. mtc0_tlbw_hazard();
  73. tlb_read();
  74. tlbw_use_hazard();
  75. tlb.tlb_hi = read_c0_entryhi();
  76. tlb.tlb_lo0 = read_c0_entrylo0();
  77. tlb.tlb_lo1 = read_c0_entrylo1();
  78. tlb.tlb_mask = read_c0_pagemask();
  79. kvm_info("TLB%c%3d Hi 0x%08lx ",
  80. (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
  81. i, tlb.tlb_hi);
  82. kvm_info("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
  83. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
  84. (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
  85. (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
  86. (tlb.tlb_lo0 >> 3) & 7);
  87. kvm_info("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
  88. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
  89. (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
  90. (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
  91. (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
  92. }
  93. write_c0_entryhi(old_entryhi);
  94. write_c0_pagemask(old_pagemask);
  95. mtc0_tlbw_hazard();
  96. local_irq_restore(flags);
  97. }
  98. EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs);
  99. void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
  100. {
  101. struct mips_coproc *cop0 = vcpu->arch.cop0;
  102. struct kvm_mips_tlb tlb;
  103. int i;
  104. kvm_info("Guest TLBs:\n");
  105. kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
  106. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  107. tlb = vcpu->arch.guest_tlb[i];
  108. kvm_info("TLB%c%3d Hi 0x%08lx ",
  109. (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
  110. i, tlb.tlb_hi);
  111. kvm_info("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
  112. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
  113. (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
  114. (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
  115. (tlb.tlb_lo0 >> 3) & 7);
  116. kvm_info("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
  117. (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
  118. (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
  119. (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
  120. (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
  121. }
  122. }
  123. EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
  124. static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn)
  125. {
  126. int srcu_idx, err = 0;
  127. kvm_pfn_t pfn;
  128. if (kvm->arch.guest_pmap[gfn] != KVM_INVALID_PAGE)
  129. return 0;
  130. srcu_idx = srcu_read_lock(&kvm->srcu);
  131. pfn = kvm_mips_gfn_to_pfn(kvm, gfn);
  132. if (kvm_mips_is_error_pfn(pfn)) {
  133. kvm_err("Couldn't get pfn for gfn %#" PRIx64 "!\n", gfn);
  134. err = -EFAULT;
  135. goto out;
  136. }
  137. kvm->arch.guest_pmap[gfn] = pfn;
  138. out:
  139. srcu_read_unlock(&kvm->srcu, srcu_idx);
  140. return err;
  141. }
  142. /* Translate guest KSEG0 addresses to Host PA */
  143. unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
  144. unsigned long gva)
  145. {
  146. gfn_t gfn;
  147. uint32_t offset = gva & ~PAGE_MASK;
  148. struct kvm *kvm = vcpu->kvm;
  149. if (KVM_GUEST_KSEGX(gva) != KVM_GUEST_KSEG0) {
  150. kvm_err("%s/%p: Invalid gva: %#lx\n", __func__,
  151. __builtin_return_address(0), gva);
  152. return KVM_INVALID_PAGE;
  153. }
  154. gfn = (KVM_GUEST_CPHYSADDR(gva) >> PAGE_SHIFT);
  155. if (gfn >= kvm->arch.guest_pmap_npages) {
  156. kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__, gfn,
  157. gva);
  158. return KVM_INVALID_PAGE;
  159. }
  160. if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
  161. return KVM_INVALID_ADDR;
  162. return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
  163. }
  164. EXPORT_SYMBOL_GPL(kvm_mips_translate_guest_kseg0_to_hpa);
  165. /* XXXKYMA: Must be called with interrupts disabled */
  166. /* set flush_dcache_mask == 0 if no dcache flush required */
  167. int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
  168. unsigned long entrylo0, unsigned long entrylo1,
  169. int flush_dcache_mask)
  170. {
  171. unsigned long flags;
  172. unsigned long old_entryhi;
  173. int idx;
  174. local_irq_save(flags);
  175. old_entryhi = read_c0_entryhi();
  176. write_c0_entryhi(entryhi);
  177. mtc0_tlbw_hazard();
  178. tlb_probe();
  179. tlb_probe_hazard();
  180. idx = read_c0_index();
  181. if (idx > current_cpu_data.tlbsize) {
  182. kvm_err("%s: Invalid Index: %d\n", __func__, idx);
  183. kvm_mips_dump_host_tlbs();
  184. local_irq_restore(flags);
  185. return -1;
  186. }
  187. write_c0_entrylo0(entrylo0);
  188. write_c0_entrylo1(entrylo1);
  189. mtc0_tlbw_hazard();
  190. if (idx < 0)
  191. tlb_write_random();
  192. else
  193. tlb_write_indexed();
  194. tlbw_use_hazard();
  195. kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
  196. vcpu->arch.pc, idx, read_c0_entryhi(),
  197. read_c0_entrylo0(), read_c0_entrylo1());
  198. /* Flush D-cache */
  199. if (flush_dcache_mask) {
  200. if (entrylo0 & MIPS3_PG_V) {
  201. ++vcpu->stat.flush_dcache_exits;
  202. flush_data_cache_page((entryhi & VPN2_MASK) &
  203. ~flush_dcache_mask);
  204. }
  205. if (entrylo1 & MIPS3_PG_V) {
  206. ++vcpu->stat.flush_dcache_exits;
  207. flush_data_cache_page(((entryhi & VPN2_MASK) &
  208. ~flush_dcache_mask) |
  209. (0x1 << PAGE_SHIFT));
  210. }
  211. }
  212. /* Restore old ASID */
  213. write_c0_entryhi(old_entryhi);
  214. mtc0_tlbw_hazard();
  215. tlbw_use_hazard();
  216. local_irq_restore(flags);
  217. return 0;
  218. }
  219. /* XXXKYMA: Must be called with interrupts disabled */
  220. int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
  221. struct kvm_vcpu *vcpu)
  222. {
  223. gfn_t gfn;
  224. kvm_pfn_t pfn0, pfn1;
  225. unsigned long vaddr = 0;
  226. unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
  227. int even;
  228. struct kvm *kvm = vcpu->kvm;
  229. const int flush_dcache_mask = 0;
  230. if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
  231. kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
  232. kvm_mips_dump_host_tlbs();
  233. return -1;
  234. }
  235. gfn = (KVM_GUEST_CPHYSADDR(badvaddr) >> PAGE_SHIFT);
  236. if (gfn >= kvm->arch.guest_pmap_npages) {
  237. kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__,
  238. gfn, badvaddr);
  239. kvm_mips_dump_host_tlbs();
  240. return -1;
  241. }
  242. even = !(gfn & 0x1);
  243. vaddr = badvaddr & (PAGE_MASK << 1);
  244. if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
  245. return -1;
  246. if (kvm_mips_map_page(vcpu->kvm, gfn ^ 0x1) < 0)
  247. return -1;
  248. if (even) {
  249. pfn0 = kvm->arch.guest_pmap[gfn];
  250. pfn1 = kvm->arch.guest_pmap[gfn ^ 0x1];
  251. } else {
  252. pfn0 = kvm->arch.guest_pmap[gfn ^ 0x1];
  253. pfn1 = kvm->arch.guest_pmap[gfn];
  254. }
  255. entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
  256. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
  257. (1 << 2) | (0x1 << 1);
  258. entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
  259. (1 << 2) | (0x1 << 1);
  260. return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
  261. flush_dcache_mask);
  262. }
  263. EXPORT_SYMBOL_GPL(kvm_mips_handle_kseg0_tlb_fault);
  264. int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
  265. struct kvm_vcpu *vcpu)
  266. {
  267. kvm_pfn_t pfn0, pfn1;
  268. unsigned long flags, old_entryhi = 0, vaddr = 0;
  269. unsigned long entrylo0 = 0, entrylo1 = 0;
  270. pfn0 = CPHYSADDR(vcpu->arch.kseg0_commpage) >> PAGE_SHIFT;
  271. pfn1 = 0;
  272. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
  273. (1 << 2) | (0x1 << 1);
  274. entrylo1 = 0;
  275. local_irq_save(flags);
  276. old_entryhi = read_c0_entryhi();
  277. vaddr = badvaddr & (PAGE_MASK << 1);
  278. write_c0_entryhi(vaddr | kvm_mips_get_kernel_asid(vcpu));
  279. mtc0_tlbw_hazard();
  280. write_c0_entrylo0(entrylo0);
  281. mtc0_tlbw_hazard();
  282. write_c0_entrylo1(entrylo1);
  283. mtc0_tlbw_hazard();
  284. write_c0_index(kvm_mips_get_commpage_asid(vcpu));
  285. mtc0_tlbw_hazard();
  286. tlb_write_indexed();
  287. mtc0_tlbw_hazard();
  288. tlbw_use_hazard();
  289. kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
  290. vcpu->arch.pc, read_c0_index(), read_c0_entryhi(),
  291. read_c0_entrylo0(), read_c0_entrylo1());
  292. /* Restore old ASID */
  293. write_c0_entryhi(old_entryhi);
  294. mtc0_tlbw_hazard();
  295. tlbw_use_hazard();
  296. local_irq_restore(flags);
  297. return 0;
  298. }
  299. EXPORT_SYMBOL_GPL(kvm_mips_handle_commpage_tlb_fault);
  300. int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
  301. struct kvm_mips_tlb *tlb,
  302. unsigned long *hpa0,
  303. unsigned long *hpa1)
  304. {
  305. unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
  306. struct kvm *kvm = vcpu->kvm;
  307. kvm_pfn_t pfn0, pfn1;
  308. if ((tlb->tlb_hi & VPN2_MASK) == 0) {
  309. pfn0 = 0;
  310. pfn1 = 0;
  311. } else {
  312. if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
  313. >> PAGE_SHIFT) < 0)
  314. return -1;
  315. if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
  316. >> PAGE_SHIFT) < 0)
  317. return -1;
  318. pfn0 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
  319. >> PAGE_SHIFT];
  320. pfn1 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
  321. >> PAGE_SHIFT];
  322. }
  323. if (hpa0)
  324. *hpa0 = pfn0 << PAGE_SHIFT;
  325. if (hpa1)
  326. *hpa1 = pfn1 << PAGE_SHIFT;
  327. /* Get attributes from the Guest TLB */
  328. entryhi = (tlb->tlb_hi & VPN2_MASK) | (KVM_GUEST_KERNEL_MODE(vcpu) ?
  329. kvm_mips_get_kernel_asid(vcpu) :
  330. kvm_mips_get_user_asid(vcpu));
  331. entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
  332. (tlb->tlb_lo0 & MIPS3_PG_D) | (tlb->tlb_lo0 & MIPS3_PG_V);
  333. entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
  334. (tlb->tlb_lo1 & MIPS3_PG_D) | (tlb->tlb_lo1 & MIPS3_PG_V);
  335. kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
  336. tlb->tlb_lo0, tlb->tlb_lo1);
  337. return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
  338. tlb->tlb_mask);
  339. }
  340. EXPORT_SYMBOL_GPL(kvm_mips_handle_mapped_seg_tlb_fault);
  341. int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
  342. {
  343. int i;
  344. int index = -1;
  345. struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
  346. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  347. if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
  348. TLB_HI_ASID_HIT(tlb[i], entryhi)) {
  349. index = i;
  350. break;
  351. }
  352. }
  353. kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
  354. __func__, entryhi, index, tlb[i].tlb_lo0, tlb[i].tlb_lo1);
  355. return index;
  356. }
  357. EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
  358. int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr)
  359. {
  360. unsigned long old_entryhi, flags;
  361. int idx;
  362. local_irq_save(flags);
  363. old_entryhi = read_c0_entryhi();
  364. if (KVM_GUEST_KERNEL_MODE(vcpu))
  365. write_c0_entryhi((vaddr & VPN2_MASK) |
  366. kvm_mips_get_kernel_asid(vcpu));
  367. else {
  368. write_c0_entryhi((vaddr & VPN2_MASK) |
  369. kvm_mips_get_user_asid(vcpu));
  370. }
  371. mtc0_tlbw_hazard();
  372. tlb_probe();
  373. tlb_probe_hazard();
  374. idx = read_c0_index();
  375. /* Restore old ASID */
  376. write_c0_entryhi(old_entryhi);
  377. mtc0_tlbw_hazard();
  378. tlbw_use_hazard();
  379. local_irq_restore(flags);
  380. kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr, idx);
  381. return idx;
  382. }
  383. EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_lookup);
  384. int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
  385. {
  386. int idx;
  387. unsigned long flags, old_entryhi;
  388. local_irq_save(flags);
  389. old_entryhi = read_c0_entryhi();
  390. write_c0_entryhi((va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
  391. mtc0_tlbw_hazard();
  392. tlb_probe();
  393. tlb_probe_hazard();
  394. idx = read_c0_index();
  395. if (idx >= current_cpu_data.tlbsize)
  396. BUG();
  397. if (idx > 0) {
  398. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  399. mtc0_tlbw_hazard();
  400. write_c0_entrylo0(0);
  401. mtc0_tlbw_hazard();
  402. write_c0_entrylo1(0);
  403. mtc0_tlbw_hazard();
  404. tlb_write_indexed();
  405. mtc0_tlbw_hazard();
  406. }
  407. write_c0_entryhi(old_entryhi);
  408. mtc0_tlbw_hazard();
  409. tlbw_use_hazard();
  410. local_irq_restore(flags);
  411. if (idx > 0)
  412. kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__,
  413. (va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu), idx);
  414. return 0;
  415. }
  416. EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
  417. void kvm_mips_flush_host_tlb(int skip_kseg0)
  418. {
  419. unsigned long flags;
  420. unsigned long old_entryhi, entryhi;
  421. unsigned long old_pagemask;
  422. int entry = 0;
  423. int maxentry = current_cpu_data.tlbsize;
  424. local_irq_save(flags);
  425. old_entryhi = read_c0_entryhi();
  426. old_pagemask = read_c0_pagemask();
  427. /* Blast 'em all away. */
  428. for (entry = 0; entry < maxentry; entry++) {
  429. write_c0_index(entry);
  430. mtc0_tlbw_hazard();
  431. if (skip_kseg0) {
  432. tlb_read();
  433. tlbw_use_hazard();
  434. entryhi = read_c0_entryhi();
  435. /* Don't blow away guest kernel entries */
  436. if (KVM_GUEST_KSEGX(entryhi) == KVM_GUEST_KSEG0)
  437. continue;
  438. }
  439. /* Make sure all entries differ. */
  440. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  441. mtc0_tlbw_hazard();
  442. write_c0_entrylo0(0);
  443. mtc0_tlbw_hazard();
  444. write_c0_entrylo1(0);
  445. mtc0_tlbw_hazard();
  446. tlb_write_indexed();
  447. mtc0_tlbw_hazard();
  448. }
  449. tlbw_use_hazard();
  450. write_c0_entryhi(old_entryhi);
  451. write_c0_pagemask(old_pagemask);
  452. mtc0_tlbw_hazard();
  453. tlbw_use_hazard();
  454. local_irq_restore(flags);
  455. }
  456. EXPORT_SYMBOL_GPL(kvm_mips_flush_host_tlb);
  457. void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
  458. struct kvm_vcpu *vcpu)
  459. {
  460. unsigned long asid = asid_cache(cpu);
  461. asid += cpu_asid_inc();
  462. if (!(asid & cpu_asid_mask(&cpu_data[cpu]))) {
  463. if (cpu_has_vtag_icache)
  464. flush_icache_all();
  465. kvm_local_flush_tlb_all(); /* start new asid cycle */
  466. if (!asid) /* fix version if needed */
  467. asid = asid_first_version(cpu);
  468. }
  469. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  470. }
  471. void kvm_local_flush_tlb_all(void)
  472. {
  473. unsigned long flags;
  474. unsigned long old_ctx;
  475. int entry = 0;
  476. local_irq_save(flags);
  477. /* Save old context and create impossible VPN2 value */
  478. old_ctx = read_c0_entryhi();
  479. write_c0_entrylo0(0);
  480. write_c0_entrylo1(0);
  481. /* Blast 'em all away. */
  482. while (entry < current_cpu_data.tlbsize) {
  483. /* Make sure all entries differ. */
  484. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  485. write_c0_index(entry);
  486. mtc0_tlbw_hazard();
  487. tlb_write_indexed();
  488. entry++;
  489. }
  490. tlbw_use_hazard();
  491. write_c0_entryhi(old_ctx);
  492. mtc0_tlbw_hazard();
  493. local_irq_restore(flags);
  494. }
  495. EXPORT_SYMBOL_GPL(kvm_local_flush_tlb_all);
  496. /**
  497. * kvm_mips_migrate_count() - Migrate timer.
  498. * @vcpu: Virtual CPU.
  499. *
  500. * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
  501. * if it was running prior to being cancelled.
  502. *
  503. * Must be called when the VCPU is migrated to a different CPU to ensure that
  504. * timer expiry during guest execution interrupts the guest and causes the
  505. * interrupt to be delivered in a timely manner.
  506. */
  507. static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
  508. {
  509. if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
  510. hrtimer_restart(&vcpu->arch.comparecount_timer);
  511. }
  512. /* Restore ASID once we are scheduled back after preemption */
  513. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  514. {
  515. unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
  516. unsigned long flags;
  517. int newasid = 0;
  518. kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
  519. /* Allocate new kernel and user ASIDs if needed */
  520. local_irq_save(flags);
  521. if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) &
  522. asid_version_mask(cpu)) {
  523. kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
  524. vcpu->arch.guest_kernel_asid[cpu] =
  525. vcpu->arch.guest_kernel_mm.context.asid[cpu];
  526. kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
  527. vcpu->arch.guest_user_asid[cpu] =
  528. vcpu->arch.guest_user_mm.context.asid[cpu];
  529. newasid++;
  530. kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
  531. cpu_context(cpu, current->mm));
  532. kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
  533. cpu, vcpu->arch.guest_kernel_asid[cpu]);
  534. kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
  535. vcpu->arch.guest_user_asid[cpu]);
  536. }
  537. if (vcpu->arch.last_sched_cpu != cpu) {
  538. kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
  539. vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
  540. /*
  541. * Migrate the timer interrupt to the current CPU so that it
  542. * always interrupts the guest and synchronously triggers a
  543. * guest timer interrupt.
  544. */
  545. kvm_mips_migrate_count(vcpu);
  546. }
  547. if (!newasid) {
  548. /*
  549. * If we preempted while the guest was executing, then reload
  550. * the pre-empted ASID
  551. */
  552. if (current->flags & PF_VCPU) {
  553. write_c0_entryhi(vcpu->arch.
  554. preempt_entryhi & asid_mask);
  555. ehb();
  556. }
  557. } else {
  558. /* New ASIDs were allocated for the VM */
  559. /*
  560. * Were we in guest context? If so then the pre-empted ASID is
  561. * no longer valid, we need to set it to what it should be based
  562. * on the mode of the Guest (Kernel/User)
  563. */
  564. if (current->flags & PF_VCPU) {
  565. if (KVM_GUEST_KERNEL_MODE(vcpu))
  566. write_c0_entryhi(vcpu->arch.
  567. guest_kernel_asid[cpu] &
  568. asid_mask);
  569. else
  570. write_c0_entryhi(vcpu->arch.
  571. guest_user_asid[cpu] &
  572. asid_mask);
  573. ehb();
  574. }
  575. }
  576. /* restore guest state to registers */
  577. kvm_mips_callbacks->vcpu_set_regs(vcpu);
  578. local_irq_restore(flags);
  579. }
  580. EXPORT_SYMBOL_GPL(kvm_arch_vcpu_load);
  581. /* ASID can change if another task is scheduled during preemption */
  582. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  583. {
  584. unsigned long flags;
  585. uint32_t cpu;
  586. local_irq_save(flags);
  587. cpu = smp_processor_id();
  588. vcpu->arch.preempt_entryhi = read_c0_entryhi();
  589. vcpu->arch.last_sched_cpu = cpu;
  590. /* save guest state in registers */
  591. kvm_mips_callbacks->vcpu_get_regs(vcpu);
  592. if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
  593. asid_version_mask(cpu))) {
  594. kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
  595. cpu_context(cpu, current->mm));
  596. drop_mmu_context(current->mm, cpu);
  597. }
  598. write_c0_entryhi(cpu_asid(cpu, current->mm));
  599. ehb();
  600. local_irq_restore(flags);
  601. }
  602. EXPORT_SYMBOL_GPL(kvm_arch_vcpu_put);
  603. uint32_t kvm_get_inst(uint32_t *opc, struct kvm_vcpu *vcpu)
  604. {
  605. struct mips_coproc *cop0 = vcpu->arch.cop0;
  606. unsigned long paddr, flags, vpn2, asid;
  607. uint32_t inst;
  608. int index;
  609. if (KVM_GUEST_KSEGX((unsigned long) opc) < KVM_GUEST_KSEG0 ||
  610. KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  611. local_irq_save(flags);
  612. index = kvm_mips_host_tlb_lookup(vcpu, (unsigned long) opc);
  613. if (index >= 0) {
  614. inst = *(opc);
  615. } else {
  616. vpn2 = (unsigned long) opc & VPN2_MASK;
  617. asid = kvm_read_c0_guest_entryhi(cop0) &
  618. KVM_ENTRYHI_ASID;
  619. index = kvm_mips_guest_tlb_lookup(vcpu, vpn2 | asid);
  620. if (index < 0) {
  621. kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
  622. __func__, opc, vcpu, read_c0_entryhi());
  623. kvm_mips_dump_host_tlbs();
  624. local_irq_restore(flags);
  625. return KVM_INVALID_INST;
  626. }
  627. kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
  628. &vcpu->arch.
  629. guest_tlb[index],
  630. NULL, NULL);
  631. inst = *(opc);
  632. }
  633. local_irq_restore(flags);
  634. } else if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  635. paddr =
  636. kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
  637. (unsigned long) opc);
  638. inst = *(uint32_t *) CKSEG0ADDR(paddr);
  639. } else {
  640. kvm_err("%s: illegal address: %p\n", __func__, opc);
  641. return KVM_INVALID_INST;
  642. }
  643. return inst;
  644. }
  645. EXPORT_SYMBOL_GPL(kvm_get_inst);