tlb.c 20 KB

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