tlb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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/export.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. #include <asm/tlbdebug.h>
  26. #undef CONFIG_MIPS_MT
  27. #include <asm/r4kcache.h>
  28. #define CONFIG_MIPS_MT
  29. #define KVM_GUEST_PC_TLB 0
  30. #define KVM_GUEST_SP_TLB 1
  31. #ifdef CONFIG_KVM_MIPS_VZ
  32. unsigned long GUESTID_MASK;
  33. EXPORT_SYMBOL_GPL(GUESTID_MASK);
  34. unsigned long GUESTID_FIRST_VERSION;
  35. EXPORT_SYMBOL_GPL(GUESTID_FIRST_VERSION);
  36. unsigned long GUESTID_VERSION_MASK;
  37. EXPORT_SYMBOL_GPL(GUESTID_VERSION_MASK);
  38. static u32 kvm_mips_get_root_asid(struct kvm_vcpu *vcpu)
  39. {
  40. struct mm_struct *gpa_mm = &vcpu->kvm->arch.gpa_mm;
  41. if (cpu_has_guestid)
  42. return 0;
  43. else
  44. return cpu_asid(smp_processor_id(), gpa_mm);
  45. }
  46. #endif
  47. static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
  48. {
  49. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  50. int cpu = smp_processor_id();
  51. return cpu_asid(cpu, kern_mm);
  52. }
  53. static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
  54. {
  55. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  56. int cpu = smp_processor_id();
  57. return cpu_asid(cpu, user_mm);
  58. }
  59. /* Structure defining an tlb entry data set. */
  60. void kvm_mips_dump_host_tlbs(void)
  61. {
  62. unsigned long flags;
  63. local_irq_save(flags);
  64. kvm_info("HOST TLBs:\n");
  65. dump_tlb_regs();
  66. pr_info("\n");
  67. dump_tlb_all();
  68. local_irq_restore(flags);
  69. }
  70. EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs);
  71. void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
  72. {
  73. struct mips_coproc *cop0 = vcpu->arch.cop0;
  74. struct kvm_mips_tlb tlb;
  75. int i;
  76. kvm_info("Guest TLBs:\n");
  77. kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
  78. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  79. tlb = vcpu->arch.guest_tlb[i];
  80. kvm_info("TLB%c%3d Hi 0x%08lx ",
  81. (tlb.tlb_lo[0] | tlb.tlb_lo[1]) & ENTRYLO_V
  82. ? ' ' : '*',
  83. i, tlb.tlb_hi);
  84. kvm_info("Lo0=0x%09llx %c%c attr %lx ",
  85. (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[0]),
  86. (tlb.tlb_lo[0] & ENTRYLO_D) ? 'D' : ' ',
  87. (tlb.tlb_lo[0] & ENTRYLO_G) ? 'G' : ' ',
  88. (tlb.tlb_lo[0] & ENTRYLO_C) >> ENTRYLO_C_SHIFT);
  89. kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
  90. (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[1]),
  91. (tlb.tlb_lo[1] & ENTRYLO_D) ? 'D' : ' ',
  92. (tlb.tlb_lo[1] & ENTRYLO_G) ? 'G' : ' ',
  93. (tlb.tlb_lo[1] & ENTRYLO_C) >> ENTRYLO_C_SHIFT,
  94. tlb.tlb_mask);
  95. }
  96. }
  97. EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
  98. int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
  99. {
  100. int i;
  101. int index = -1;
  102. struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
  103. for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
  104. if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
  105. TLB_HI_ASID_HIT(tlb[i], entryhi)) {
  106. index = i;
  107. break;
  108. }
  109. }
  110. kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
  111. __func__, entryhi, index, tlb[i].tlb_lo[0], tlb[i].tlb_lo[1]);
  112. return index;
  113. }
  114. EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
  115. static int _kvm_mips_host_tlb_inv(unsigned long entryhi)
  116. {
  117. int idx;
  118. write_c0_entryhi(entryhi);
  119. mtc0_tlbw_hazard();
  120. tlb_probe();
  121. tlb_probe_hazard();
  122. idx = read_c0_index();
  123. if (idx >= current_cpu_data.tlbsize)
  124. BUG();
  125. if (idx >= 0) {
  126. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  127. write_c0_entrylo0(0);
  128. write_c0_entrylo1(0);
  129. mtc0_tlbw_hazard();
  130. tlb_write_indexed();
  131. tlbw_use_hazard();
  132. }
  133. return idx;
  134. }
  135. int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va,
  136. bool user, bool kernel)
  137. {
  138. /*
  139. * Initialize idx_user and idx_kernel to workaround bogus
  140. * maybe-initialized warning when using GCC 6.
  141. */
  142. int idx_user = 0, idx_kernel = 0;
  143. unsigned long flags, old_entryhi;
  144. local_irq_save(flags);
  145. old_entryhi = read_c0_entryhi();
  146. if (user)
  147. idx_user = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
  148. kvm_mips_get_user_asid(vcpu));
  149. if (kernel)
  150. idx_kernel = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
  151. kvm_mips_get_kernel_asid(vcpu));
  152. write_c0_entryhi(old_entryhi);
  153. mtc0_tlbw_hazard();
  154. local_irq_restore(flags);
  155. /*
  156. * We don't want to get reserved instruction exceptions for missing tlb
  157. * entries.
  158. */
  159. if (cpu_has_vtag_icache)
  160. flush_icache_all();
  161. if (user && idx_user >= 0)
  162. kvm_debug("%s: Invalidated guest user entryhi %#lx @ idx %d\n",
  163. __func__, (va & VPN2_MASK) |
  164. kvm_mips_get_user_asid(vcpu), idx_user);
  165. if (kernel && idx_kernel >= 0)
  166. kvm_debug("%s: Invalidated guest kernel entryhi %#lx @ idx %d\n",
  167. __func__, (va & VPN2_MASK) |
  168. kvm_mips_get_kernel_asid(vcpu), idx_kernel);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
  172. #ifdef CONFIG_KVM_MIPS_VZ
  173. /* GuestID management */
  174. /**
  175. * clear_root_gid() - Set GuestCtl1.RID for normal root operation.
  176. */
  177. static inline void clear_root_gid(void)
  178. {
  179. if (cpu_has_guestid) {
  180. clear_c0_guestctl1(MIPS_GCTL1_RID);
  181. mtc0_tlbw_hazard();
  182. }
  183. }
  184. /**
  185. * set_root_gid_to_guest_gid() - Set GuestCtl1.RID to match GuestCtl1.ID.
  186. *
  187. * Sets the root GuestID to match the current guest GuestID, for TLB operation
  188. * on the GPA->RPA mappings in the root TLB.
  189. *
  190. * The caller must be sure to disable HTW while the root GID is set, and
  191. * possibly longer if TLB registers are modified.
  192. */
  193. static inline void set_root_gid_to_guest_gid(void)
  194. {
  195. unsigned int guestctl1;
  196. if (cpu_has_guestid) {
  197. back_to_back_c0_hazard();
  198. guestctl1 = read_c0_guestctl1();
  199. guestctl1 = (guestctl1 & ~MIPS_GCTL1_RID) |
  200. ((guestctl1 & MIPS_GCTL1_ID) >> MIPS_GCTL1_ID_SHIFT)
  201. << MIPS_GCTL1_RID_SHIFT;
  202. write_c0_guestctl1(guestctl1);
  203. mtc0_tlbw_hazard();
  204. }
  205. }
  206. int kvm_vz_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
  207. {
  208. int idx;
  209. unsigned long flags, old_entryhi;
  210. local_irq_save(flags);
  211. htw_stop();
  212. /* Set root GuestID for root probe and write of guest TLB entry */
  213. set_root_gid_to_guest_gid();
  214. old_entryhi = read_c0_entryhi();
  215. idx = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
  216. kvm_mips_get_root_asid(vcpu));
  217. write_c0_entryhi(old_entryhi);
  218. clear_root_gid();
  219. mtc0_tlbw_hazard();
  220. htw_start();
  221. local_irq_restore(flags);
  222. /*
  223. * We don't want to get reserved instruction exceptions for missing tlb
  224. * entries.
  225. */
  226. if (cpu_has_vtag_icache)
  227. flush_icache_all();
  228. if (idx > 0)
  229. kvm_debug("%s: Invalidated root entryhi %#lx @ idx %d\n",
  230. __func__, (va & VPN2_MASK) |
  231. kvm_mips_get_root_asid(vcpu), idx);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(kvm_vz_host_tlb_inv);
  235. /**
  236. * kvm_vz_guest_tlb_lookup() - Lookup a guest VZ TLB mapping.
  237. * @vcpu: KVM VCPU pointer.
  238. * @gpa: Guest virtual address in a TLB mapped guest segment.
  239. * @gpa: Ponter to output guest physical address it maps to.
  240. *
  241. * Converts a guest virtual address in a guest TLB mapped segment to a guest
  242. * physical address, by probing the guest TLB.
  243. *
  244. * Returns: 0 if guest TLB mapping exists for @gva. *@gpa will have been
  245. * written.
  246. * -EFAULT if no guest TLB mapping exists for @gva. *@gpa may not
  247. * have been written.
  248. */
  249. int kvm_vz_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long gva,
  250. unsigned long *gpa)
  251. {
  252. unsigned long o_entryhi, o_entrylo[2], o_pagemask;
  253. unsigned int o_index;
  254. unsigned long entrylo[2], pagemask, pagemaskbit, pa;
  255. unsigned long flags;
  256. int index;
  257. /* Probe the guest TLB for a mapping */
  258. local_irq_save(flags);
  259. /* Set root GuestID for root probe of guest TLB entry */
  260. htw_stop();
  261. set_root_gid_to_guest_gid();
  262. o_entryhi = read_gc0_entryhi();
  263. o_index = read_gc0_index();
  264. write_gc0_entryhi((o_entryhi & 0x3ff) | (gva & ~0xfffl));
  265. mtc0_tlbw_hazard();
  266. guest_tlb_probe();
  267. tlb_probe_hazard();
  268. index = read_gc0_index();
  269. if (index < 0) {
  270. /* No match, fail */
  271. write_gc0_entryhi(o_entryhi);
  272. write_gc0_index(o_index);
  273. clear_root_gid();
  274. htw_start();
  275. local_irq_restore(flags);
  276. return -EFAULT;
  277. }
  278. /* Match! read the TLB entry */
  279. o_entrylo[0] = read_gc0_entrylo0();
  280. o_entrylo[1] = read_gc0_entrylo1();
  281. o_pagemask = read_gc0_pagemask();
  282. mtc0_tlbr_hazard();
  283. guest_tlb_read();
  284. tlb_read_hazard();
  285. entrylo[0] = read_gc0_entrylo0();
  286. entrylo[1] = read_gc0_entrylo1();
  287. pagemask = ~read_gc0_pagemask() & ~0x1fffl;
  288. write_gc0_entryhi(o_entryhi);
  289. write_gc0_index(o_index);
  290. write_gc0_entrylo0(o_entrylo[0]);
  291. write_gc0_entrylo1(o_entrylo[1]);
  292. write_gc0_pagemask(o_pagemask);
  293. clear_root_gid();
  294. htw_start();
  295. local_irq_restore(flags);
  296. /* Select one of the EntryLo values and interpret the GPA */
  297. pagemaskbit = (pagemask ^ (pagemask & (pagemask - 1))) >> 1;
  298. pa = entrylo[!!(gva & pagemaskbit)];
  299. /*
  300. * TLB entry may have become invalid since TLB probe if physical FTLB
  301. * entries are shared between threads (e.g. I6400).
  302. */
  303. if (!(pa & ENTRYLO_V))
  304. return -EFAULT;
  305. /*
  306. * Note, this doesn't take guest MIPS32 XPA into account, where PFN is
  307. * split with XI/RI in the middle.
  308. */
  309. pa = (pa << 6) & ~0xfffl;
  310. pa |= gva & ~(pagemask | pagemaskbit);
  311. *gpa = pa;
  312. return 0;
  313. }
  314. EXPORT_SYMBOL_GPL(kvm_vz_guest_tlb_lookup);
  315. /**
  316. * kvm_vz_local_flush_roottlb_all_guests() - Flush all root TLB entries for
  317. * guests.
  318. *
  319. * Invalidate all entries in root tlb which are GPA mappings.
  320. */
  321. void kvm_vz_local_flush_roottlb_all_guests(void)
  322. {
  323. unsigned long flags;
  324. unsigned long old_entryhi, old_pagemask, old_guestctl1;
  325. int entry;
  326. if (WARN_ON(!cpu_has_guestid))
  327. return;
  328. local_irq_save(flags);
  329. htw_stop();
  330. /* TLBR may clobber EntryHi.ASID, PageMask, and GuestCtl1.RID */
  331. old_entryhi = read_c0_entryhi();
  332. old_pagemask = read_c0_pagemask();
  333. old_guestctl1 = read_c0_guestctl1();
  334. /*
  335. * Invalidate guest entries in root TLB while leaving root entries
  336. * intact when possible.
  337. */
  338. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  339. write_c0_index(entry);
  340. mtc0_tlbw_hazard();
  341. tlb_read();
  342. tlb_read_hazard();
  343. /* Don't invalidate non-guest (RVA) mappings in the root TLB */
  344. if (!(read_c0_guestctl1() & MIPS_GCTL1_RID))
  345. continue;
  346. /* Make sure all entries differ. */
  347. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  348. write_c0_entrylo0(0);
  349. write_c0_entrylo1(0);
  350. write_c0_guestctl1(0);
  351. mtc0_tlbw_hazard();
  352. tlb_write_indexed();
  353. }
  354. write_c0_entryhi(old_entryhi);
  355. write_c0_pagemask(old_pagemask);
  356. write_c0_guestctl1(old_guestctl1);
  357. tlbw_use_hazard();
  358. htw_start();
  359. local_irq_restore(flags);
  360. }
  361. EXPORT_SYMBOL_GPL(kvm_vz_local_flush_roottlb_all_guests);
  362. /**
  363. * kvm_vz_local_flush_guesttlb_all() - Flush all guest TLB entries.
  364. *
  365. * Invalidate all entries in guest tlb irrespective of guestid.
  366. */
  367. void kvm_vz_local_flush_guesttlb_all(void)
  368. {
  369. unsigned long flags;
  370. unsigned long old_index;
  371. unsigned long old_entryhi;
  372. unsigned long old_entrylo[2];
  373. unsigned long old_pagemask;
  374. int entry;
  375. u64 cvmmemctl2 = 0;
  376. local_irq_save(flags);
  377. /* Preserve all clobbered guest registers */
  378. old_index = read_gc0_index();
  379. old_entryhi = read_gc0_entryhi();
  380. old_entrylo[0] = read_gc0_entrylo0();
  381. old_entrylo[1] = read_gc0_entrylo1();
  382. old_pagemask = read_gc0_pagemask();
  383. switch (current_cpu_type()) {
  384. case CPU_CAVIUM_OCTEON3:
  385. /* Inhibit machine check due to multiple matching TLB entries */
  386. cvmmemctl2 = read_c0_cvmmemctl2();
  387. cvmmemctl2 |= CVMMEMCTL2_INHIBITTS;
  388. write_c0_cvmmemctl2(cvmmemctl2);
  389. break;
  390. };
  391. /* Invalidate guest entries in guest TLB */
  392. write_gc0_entrylo0(0);
  393. write_gc0_entrylo1(0);
  394. write_gc0_pagemask(0);
  395. for (entry = 0; entry < current_cpu_data.guest.tlbsize; entry++) {
  396. /* Make sure all entries differ. */
  397. write_gc0_index(entry);
  398. write_gc0_entryhi(UNIQUE_GUEST_ENTRYHI(entry));
  399. mtc0_tlbw_hazard();
  400. guest_tlb_write_indexed();
  401. }
  402. if (cvmmemctl2) {
  403. cvmmemctl2 &= ~CVMMEMCTL2_INHIBITTS;
  404. write_c0_cvmmemctl2(cvmmemctl2);
  405. };
  406. write_gc0_index(old_index);
  407. write_gc0_entryhi(old_entryhi);
  408. write_gc0_entrylo0(old_entrylo[0]);
  409. write_gc0_entrylo1(old_entrylo[1]);
  410. write_gc0_pagemask(old_pagemask);
  411. tlbw_use_hazard();
  412. local_irq_restore(flags);
  413. }
  414. EXPORT_SYMBOL_GPL(kvm_vz_local_flush_guesttlb_all);
  415. /**
  416. * kvm_vz_save_guesttlb() - Save a range of guest TLB entries.
  417. * @buf: Buffer to write TLB entries into.
  418. * @index: Start index.
  419. * @count: Number of entries to save.
  420. *
  421. * Save a range of guest TLB entries. The caller must ensure interrupts are
  422. * disabled.
  423. */
  424. void kvm_vz_save_guesttlb(struct kvm_mips_tlb *buf, unsigned int index,
  425. unsigned int count)
  426. {
  427. unsigned int end = index + count;
  428. unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
  429. unsigned int guestctl1 = 0;
  430. int old_index, i;
  431. /* Save registers we're about to clobber */
  432. old_index = read_gc0_index();
  433. old_entryhi = read_gc0_entryhi();
  434. old_entrylo0 = read_gc0_entrylo0();
  435. old_entrylo1 = read_gc0_entrylo1();
  436. old_pagemask = read_gc0_pagemask();
  437. /* Set root GuestID for root probe */
  438. htw_stop();
  439. set_root_gid_to_guest_gid();
  440. if (cpu_has_guestid)
  441. guestctl1 = read_c0_guestctl1();
  442. /* Read each entry from guest TLB */
  443. for (i = index; i < end; ++i, ++buf) {
  444. write_gc0_index(i);
  445. mtc0_tlbr_hazard();
  446. guest_tlb_read();
  447. tlb_read_hazard();
  448. if (cpu_has_guestid &&
  449. (read_c0_guestctl1() ^ guestctl1) & MIPS_GCTL1_RID) {
  450. /* Entry invalid or belongs to another guest */
  451. buf->tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
  452. buf->tlb_lo[0] = 0;
  453. buf->tlb_lo[1] = 0;
  454. buf->tlb_mask = 0;
  455. } else {
  456. /* Entry belongs to the right guest */
  457. buf->tlb_hi = read_gc0_entryhi();
  458. buf->tlb_lo[0] = read_gc0_entrylo0();
  459. buf->tlb_lo[1] = read_gc0_entrylo1();
  460. buf->tlb_mask = read_gc0_pagemask();
  461. }
  462. }
  463. /* Clear root GuestID again */
  464. clear_root_gid();
  465. htw_start();
  466. /* Restore clobbered registers */
  467. write_gc0_index(old_index);
  468. write_gc0_entryhi(old_entryhi);
  469. write_gc0_entrylo0(old_entrylo0);
  470. write_gc0_entrylo1(old_entrylo1);
  471. write_gc0_pagemask(old_pagemask);
  472. tlbw_use_hazard();
  473. }
  474. EXPORT_SYMBOL_GPL(kvm_vz_save_guesttlb);
  475. /**
  476. * kvm_vz_load_guesttlb() - Save a range of guest TLB entries.
  477. * @buf: Buffer to read TLB entries from.
  478. * @index: Start index.
  479. * @count: Number of entries to load.
  480. *
  481. * Load a range of guest TLB entries. The caller must ensure interrupts are
  482. * disabled.
  483. */
  484. void kvm_vz_load_guesttlb(const struct kvm_mips_tlb *buf, unsigned int index,
  485. unsigned int count)
  486. {
  487. unsigned int end = index + count;
  488. unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
  489. int old_index, i;
  490. /* Save registers we're about to clobber */
  491. old_index = read_gc0_index();
  492. old_entryhi = read_gc0_entryhi();
  493. old_entrylo0 = read_gc0_entrylo0();
  494. old_entrylo1 = read_gc0_entrylo1();
  495. old_pagemask = read_gc0_pagemask();
  496. /* Set root GuestID for root probe */
  497. htw_stop();
  498. set_root_gid_to_guest_gid();
  499. /* Write each entry to guest TLB */
  500. for (i = index; i < end; ++i, ++buf) {
  501. write_gc0_index(i);
  502. write_gc0_entryhi(buf->tlb_hi);
  503. write_gc0_entrylo0(buf->tlb_lo[0]);
  504. write_gc0_entrylo1(buf->tlb_lo[1]);
  505. write_gc0_pagemask(buf->tlb_mask);
  506. mtc0_tlbw_hazard();
  507. guest_tlb_write_indexed();
  508. }
  509. /* Clear root GuestID again */
  510. clear_root_gid();
  511. htw_start();
  512. /* Restore clobbered registers */
  513. write_gc0_index(old_index);
  514. write_gc0_entryhi(old_entryhi);
  515. write_gc0_entrylo0(old_entrylo0);
  516. write_gc0_entrylo1(old_entrylo1);
  517. write_gc0_pagemask(old_pagemask);
  518. tlbw_use_hazard();
  519. }
  520. EXPORT_SYMBOL_GPL(kvm_vz_load_guesttlb);
  521. #endif
  522. /**
  523. * kvm_mips_suspend_mm() - Suspend the active mm.
  524. * @cpu The CPU we're running on.
  525. *
  526. * Suspend the active_mm, ready for a switch to a KVM guest virtual address
  527. * space. This is left active for the duration of guest context, including time
  528. * with interrupts enabled, so we need to be careful not to confuse e.g. cache
  529. * management IPIs.
  530. *
  531. * kvm_mips_resume_mm() should be called before context switching to a different
  532. * process so we don't need to worry about reference counting.
  533. *
  534. * This needs to be in static kernel code to avoid exporting init_mm.
  535. */
  536. void kvm_mips_suspend_mm(int cpu)
  537. {
  538. cpumask_clear_cpu(cpu, mm_cpumask(current->active_mm));
  539. current->active_mm = &init_mm;
  540. }
  541. EXPORT_SYMBOL_GPL(kvm_mips_suspend_mm);
  542. /**
  543. * kvm_mips_resume_mm() - Resume the current process mm.
  544. * @cpu The CPU we're running on.
  545. *
  546. * Resume the mm of the current process, after a switch back from a KVM guest
  547. * virtual address space (see kvm_mips_suspend_mm()).
  548. */
  549. void kvm_mips_resume_mm(int cpu)
  550. {
  551. cpumask_set_cpu(cpu, mm_cpumask(current->mm));
  552. current->active_mm = current->mm;
  553. }
  554. EXPORT_SYMBOL_GPL(kvm_mips_resume_mm);