e500_tlb.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. *
  6. * Description:
  7. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  8. * by Hollis Blanchard <hollisb@us.ibm.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_host.h>
  19. #include <linux/highmem.h>
  20. #include <asm/kvm_ppc.h>
  21. #include <asm/kvm_e500.h>
  22. #include "../mm/mmu_decl.h"
  23. #include "e500_tlb.h"
  24. #include "trace.h"
  25. #include "timing.h"
  26. #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
  27. struct id {
  28. unsigned long val;
  29. struct id **pentry;
  30. };
  31. #define NUM_TIDS 256
  32. /*
  33. * This table provide mappings from:
  34. * (guestAS,guestTID,guestPR) --> ID of physical cpu
  35. * guestAS [0..1]
  36. * guestTID [0..255]
  37. * guestPR [0..1]
  38. * ID [1..255]
  39. * Each vcpu keeps one vcpu_id_table.
  40. */
  41. struct vcpu_id_table {
  42. struct id id[2][NUM_TIDS][2];
  43. };
  44. /*
  45. * This table provide reversed mappings of vcpu_id_table:
  46. * ID --> address of vcpu_id_table item.
  47. * Each physical core has one pcpu_id_table.
  48. */
  49. struct pcpu_id_table {
  50. struct id *entry[NUM_TIDS];
  51. };
  52. static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
  53. /* This variable keeps last used shadow ID on local core.
  54. * The valid range of shadow ID is [1..255] */
  55. static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
  56. static unsigned int tlb1_entry_num;
  57. /*
  58. * Allocate a free shadow id and setup a valid sid mapping in given entry.
  59. * A mapping is only valid when vcpu_id_table and pcpu_id_table are match.
  60. *
  61. * The caller must have preemption disabled, and keep it that way until
  62. * it has finished with the returned shadow id (either written into the
  63. * TLB or arch.shadow_pid, or discarded).
  64. */
  65. static inline int local_sid_setup_one(struct id *entry)
  66. {
  67. unsigned long sid;
  68. int ret = -1;
  69. sid = ++(__get_cpu_var(pcpu_last_used_sid));
  70. if (sid < NUM_TIDS) {
  71. __get_cpu_var(pcpu_sids).entry[sid] = entry;
  72. entry->val = sid;
  73. entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
  74. ret = sid;
  75. }
  76. /*
  77. * If sid == NUM_TIDS, we've run out of sids. We return -1, and
  78. * the caller will invalidate everything and start over.
  79. *
  80. * sid > NUM_TIDS indicates a race, which we disable preemption to
  81. * avoid.
  82. */
  83. WARN_ON(sid > NUM_TIDS);
  84. return ret;
  85. }
  86. /*
  87. * Check if given entry contain a valid shadow id mapping.
  88. * An ID mapping is considered valid only if
  89. * both vcpu and pcpu know this mapping.
  90. *
  91. * The caller must have preemption disabled, and keep it that way until
  92. * it has finished with the returned shadow id (either written into the
  93. * TLB or arch.shadow_pid, or discarded).
  94. */
  95. static inline int local_sid_lookup(struct id *entry)
  96. {
  97. if (entry && entry->val != 0 &&
  98. __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
  99. entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
  100. return entry->val;
  101. return -1;
  102. }
  103. /* Invalidate all id mappings on local core */
  104. static inline void local_sid_destroy_all(void)
  105. {
  106. preempt_disable();
  107. __get_cpu_var(pcpu_last_used_sid) = 0;
  108. memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
  109. preempt_enable();
  110. }
  111. static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
  112. {
  113. vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
  114. return vcpu_e500->idt;
  115. }
  116. static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
  117. {
  118. kfree(vcpu_e500->idt);
  119. }
  120. /* Invalidate all mappings on vcpu */
  121. static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
  122. {
  123. memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
  124. /* Update shadow pid when mappings are changed */
  125. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  126. }
  127. /* Invalidate one ID mapping on vcpu */
  128. static inline void kvmppc_e500_id_table_reset_one(
  129. struct kvmppc_vcpu_e500 *vcpu_e500,
  130. int as, int pid, int pr)
  131. {
  132. struct vcpu_id_table *idt = vcpu_e500->idt;
  133. BUG_ON(as >= 2);
  134. BUG_ON(pid >= NUM_TIDS);
  135. BUG_ON(pr >= 2);
  136. idt->id[as][pid][pr].val = 0;
  137. idt->id[as][pid][pr].pentry = NULL;
  138. /* Update shadow pid when mappings are changed */
  139. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  140. }
  141. /*
  142. * Map guest (vcpu,AS,ID,PR) to physical core shadow id.
  143. * This function first lookup if a valid mapping exists,
  144. * if not, then creates a new one.
  145. *
  146. * The caller must have preemption disabled, and keep it that way until
  147. * it has finished with the returned shadow id (either written into the
  148. * TLB or arch.shadow_pid, or discarded).
  149. */
  150. static unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
  151. unsigned int as, unsigned int gid,
  152. unsigned int pr, int avoid_recursion)
  153. {
  154. struct vcpu_id_table *idt = vcpu_e500->idt;
  155. int sid;
  156. BUG_ON(as >= 2);
  157. BUG_ON(gid >= NUM_TIDS);
  158. BUG_ON(pr >= 2);
  159. sid = local_sid_lookup(&idt->id[as][gid][pr]);
  160. while (sid <= 0) {
  161. /* No mapping yet */
  162. sid = local_sid_setup_one(&idt->id[as][gid][pr]);
  163. if (sid <= 0) {
  164. _tlbil_all();
  165. local_sid_destroy_all();
  166. }
  167. /* Update shadow pid when mappings are changed */
  168. if (!avoid_recursion)
  169. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  170. }
  171. return sid;
  172. }
  173. /* Map guest pid to shadow.
  174. * We use PID to keep shadow of current guest non-zero PID,
  175. * and use PID1 to keep shadow of guest zero PID.
  176. * So that guest tlbe with TID=0 can be accessed at any time */
  177. void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
  178. {
  179. preempt_disable();
  180. vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
  181. get_cur_as(&vcpu_e500->vcpu),
  182. get_cur_pid(&vcpu_e500->vcpu),
  183. get_cur_pr(&vcpu_e500->vcpu), 1);
  184. vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
  185. get_cur_as(&vcpu_e500->vcpu), 0,
  186. get_cur_pr(&vcpu_e500->vcpu), 1);
  187. preempt_enable();
  188. }
  189. void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
  190. {
  191. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  192. struct tlbe *tlbe;
  193. int i, tlbsel;
  194. printk("| %8s | %8s | %8s | %8s | %8s |\n",
  195. "nr", "mas1", "mas2", "mas3", "mas7");
  196. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  197. printk("Guest TLB%d:\n", tlbsel);
  198. for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) {
  199. tlbe = &vcpu_e500->gtlb_arch[tlbsel][i];
  200. if (tlbe->mas1 & MAS1_VALID)
  201. printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
  202. tlbsel, i, tlbe->mas1, tlbe->mas2,
  203. tlbe->mas3, tlbe->mas7);
  204. }
  205. }
  206. }
  207. static inline unsigned int tlb0_get_next_victim(
  208. struct kvmppc_vcpu_e500 *vcpu_e500)
  209. {
  210. unsigned int victim;
  211. victim = vcpu_e500->gtlb_nv[0]++;
  212. if (unlikely(vcpu_e500->gtlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
  213. vcpu_e500->gtlb_nv[0] = 0;
  214. return victim;
  215. }
  216. static inline unsigned int tlb1_max_shadow_size(void)
  217. {
  218. /* reserve one entry for magic page */
  219. return tlb1_entry_num - tlbcam_index - 1;
  220. }
  221. static inline int tlbe_is_writable(struct tlbe *tlbe)
  222. {
  223. return tlbe->mas3 & (MAS3_SW|MAS3_UW);
  224. }
  225. static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
  226. {
  227. /* Mask off reserved bits. */
  228. mas3 &= MAS3_ATTRIB_MASK;
  229. if (!usermode) {
  230. /* Guest is in supervisor mode,
  231. * so we need to translate guest
  232. * supervisor permissions into user permissions. */
  233. mas3 &= ~E500_TLB_USER_PERM_MASK;
  234. mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
  235. }
  236. return mas3 | E500_TLB_SUPER_PERM_MASK;
  237. }
  238. static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
  239. {
  240. #ifdef CONFIG_SMP
  241. return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
  242. #else
  243. return mas2 & MAS2_ATTRIB_MASK;
  244. #endif
  245. }
  246. /*
  247. * writing shadow tlb entry to host TLB
  248. */
  249. static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0)
  250. {
  251. unsigned long flags;
  252. local_irq_save(flags);
  253. mtspr(SPRN_MAS0, mas0);
  254. mtspr(SPRN_MAS1, stlbe->mas1);
  255. mtspr(SPRN_MAS2, stlbe->mas2);
  256. mtspr(SPRN_MAS3, stlbe->mas3);
  257. mtspr(SPRN_MAS7, stlbe->mas7);
  258. asm volatile("isync; tlbwe" : : : "memory");
  259. local_irq_restore(flags);
  260. }
  261. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  262. int tlbsel, int esel, struct tlbe *stlbe)
  263. {
  264. if (tlbsel == 0) {
  265. __write_host_tlbe(stlbe,
  266. MAS0_TLBSEL(0) |
  267. MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1)));
  268. } else {
  269. __write_host_tlbe(stlbe,
  270. MAS0_TLBSEL(1) |
  271. MAS0_ESEL(to_htlb1_esel(esel)));
  272. }
  273. trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
  274. stlbe->mas3, stlbe->mas7);
  275. }
  276. void kvmppc_map_magic(struct kvm_vcpu *vcpu)
  277. {
  278. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  279. struct tlbe magic;
  280. ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
  281. unsigned int stid;
  282. pfn_t pfn;
  283. pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
  284. get_page(pfn_to_page(pfn));
  285. preempt_disable();
  286. stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
  287. magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
  288. MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  289. magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
  290. magic.mas3 = (pfn << PAGE_SHIFT) |
  291. MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
  292. magic.mas7 = pfn >> (32 - PAGE_SHIFT);
  293. __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
  294. preempt_enable();
  295. }
  296. void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
  297. {
  298. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  299. /* Shadow PID may be expired on local core */
  300. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  301. }
  302. void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
  303. {
  304. }
  305. static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
  306. int tlbsel, int esel)
  307. {
  308. struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  309. struct vcpu_id_table *idt = vcpu_e500->idt;
  310. unsigned int pr, tid, ts, pid;
  311. u32 val, eaddr;
  312. unsigned long flags;
  313. ts = get_tlb_ts(gtlbe);
  314. tid = get_tlb_tid(gtlbe);
  315. preempt_disable();
  316. /* One guest ID may be mapped to two shadow IDs */
  317. for (pr = 0; pr < 2; pr++) {
  318. /*
  319. * The shadow PID can have a valid mapping on at most one
  320. * host CPU. In the common case, it will be valid on this
  321. * CPU, in which case (for TLB0) we do a local invalidation
  322. * of the specific address.
  323. *
  324. * If the shadow PID is not valid on the current host CPU, or
  325. * if we're invalidating a TLB1 entry, we invalidate the
  326. * entire shadow PID.
  327. */
  328. if (tlbsel == 1 ||
  329. (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) {
  330. kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
  331. continue;
  332. }
  333. /*
  334. * The guest is invalidating a TLB0 entry which is in a PID
  335. * that has a valid shadow mapping on this host CPU. We
  336. * search host TLB0 to invalidate it's shadow TLB entry,
  337. * similar to __tlbil_va except that we need to look in AS1.
  338. */
  339. val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
  340. eaddr = get_tlb_eaddr(gtlbe);
  341. local_irq_save(flags);
  342. mtspr(SPRN_MAS6, val);
  343. asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
  344. val = mfspr(SPRN_MAS1);
  345. if (val & MAS1_VALID) {
  346. mtspr(SPRN_MAS1, val & ~MAS1_VALID);
  347. asm volatile("tlbwe");
  348. }
  349. local_irq_restore(flags);
  350. }
  351. preempt_enable();
  352. }
  353. /* Search the guest TLB for a matching entry. */
  354. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  355. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  356. {
  357. int i;
  358. /* XXX Replace loop with fancy data structures. */
  359. for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) {
  360. struct tlbe *tlbe = &vcpu_e500->gtlb_arch[tlbsel][i];
  361. unsigned int tid;
  362. if (eaddr < get_tlb_eaddr(tlbe))
  363. continue;
  364. if (eaddr > get_tlb_end(tlbe))
  365. continue;
  366. tid = get_tlb_tid(tlbe);
  367. if (tid && (tid != pid))
  368. continue;
  369. if (!get_tlb_v(tlbe))
  370. continue;
  371. if (get_tlb_ts(tlbe) != as && as != -1)
  372. continue;
  373. return i;
  374. }
  375. return -1;
  376. }
  377. static inline void kvmppc_e500_priv_setup(struct tlbe_priv *priv,
  378. struct tlbe *gtlbe,
  379. pfn_t pfn)
  380. {
  381. priv->pfn = pfn;
  382. priv->flags = E500_TLB_VALID;
  383. if (tlbe_is_writable(gtlbe))
  384. priv->flags |= E500_TLB_DIRTY;
  385. }
  386. static inline void kvmppc_e500_priv_release(struct tlbe_priv *priv)
  387. {
  388. if (priv->flags & E500_TLB_VALID) {
  389. if (priv->flags & E500_TLB_DIRTY)
  390. kvm_release_pfn_dirty(priv->pfn);
  391. else
  392. kvm_release_pfn_clean(priv->pfn);
  393. priv->flags = 0;
  394. }
  395. }
  396. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  397. unsigned int eaddr, int as)
  398. {
  399. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  400. unsigned int victim, pidsel, tsized;
  401. int tlbsel;
  402. /* since we only have two TLBs, only lower bit is used. */
  403. tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
  404. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  405. pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
  406. tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
  407. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  408. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  409. vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  410. | MAS1_TID(vcpu_e500->pid[pidsel])
  411. | MAS1_TSIZE(tsized);
  412. vcpu_e500->mas2 = (eaddr & MAS2_EPN)
  413. | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
  414. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  415. vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
  416. | (get_cur_pid(vcpu) << 16)
  417. | (as ? MAS6_SAS : 0);
  418. vcpu_e500->mas7 = 0;
  419. }
  420. static inline void kvmppc_e500_setup_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  421. struct tlbe *gtlbe, int tsize,
  422. struct tlbe_priv *priv,
  423. u64 gvaddr, struct tlbe *stlbe)
  424. {
  425. pfn_t pfn = priv->pfn;
  426. unsigned int stid;
  427. stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
  428. get_tlb_tid(gtlbe),
  429. get_cur_pr(&vcpu_e500->vcpu), 0);
  430. /* Force TS=1 IPROT=0 for all guest mappings. */
  431. stlbe->mas1 = MAS1_TSIZE(tsize)
  432. | MAS1_TID(stid) | MAS1_TS | MAS1_VALID;
  433. stlbe->mas2 = (gvaddr & MAS2_EPN)
  434. | e500_shadow_mas2_attrib(gtlbe->mas2,
  435. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  436. stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
  437. | e500_shadow_mas3_attrib(gtlbe->mas3,
  438. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  439. stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
  440. }
  441. static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  442. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel,
  443. struct tlbe *stlbe)
  444. {
  445. struct kvm_memory_slot *slot;
  446. unsigned long pfn, hva;
  447. int pfnmap = 0;
  448. int tsize = BOOK3E_PAGESZ_4K;
  449. struct tlbe_priv *priv;
  450. /*
  451. * Translate guest physical to true physical, acquiring
  452. * a page reference if it is normal, non-reserved memory.
  453. *
  454. * gfn_to_memslot() must succeed because otherwise we wouldn't
  455. * have gotten this far. Eventually we should just pass the slot
  456. * pointer through from the first lookup.
  457. */
  458. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  459. hva = gfn_to_hva_memslot(slot, gfn);
  460. if (tlbsel == 1) {
  461. struct vm_area_struct *vma;
  462. down_read(&current->mm->mmap_sem);
  463. vma = find_vma(current->mm, hva);
  464. if (vma && hva >= vma->vm_start &&
  465. (vma->vm_flags & VM_PFNMAP)) {
  466. /*
  467. * This VMA is a physically contiguous region (e.g.
  468. * /dev/mem) that bypasses normal Linux page
  469. * management. Find the overlap between the
  470. * vma and the memslot.
  471. */
  472. unsigned long start, end;
  473. unsigned long slot_start, slot_end;
  474. pfnmap = 1;
  475. start = vma->vm_pgoff;
  476. end = start +
  477. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  478. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  479. slot_start = pfn - (gfn - slot->base_gfn);
  480. slot_end = slot_start + slot->npages;
  481. if (start < slot_start)
  482. start = slot_start;
  483. if (end > slot_end)
  484. end = slot_end;
  485. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  486. MAS1_TSIZE_SHIFT;
  487. /*
  488. * e500 doesn't implement the lowest tsize bit,
  489. * or 1K pages.
  490. */
  491. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  492. /*
  493. * Now find the largest tsize (up to what the guest
  494. * requested) that will cover gfn, stay within the
  495. * range, and for which gfn and pfn are mutually
  496. * aligned.
  497. */
  498. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  499. unsigned long gfn_start, gfn_end, tsize_pages;
  500. tsize_pages = 1 << (tsize - 2);
  501. gfn_start = gfn & ~(tsize_pages - 1);
  502. gfn_end = gfn_start + tsize_pages;
  503. if (gfn_start + pfn - gfn < start)
  504. continue;
  505. if (gfn_end + pfn - gfn > end)
  506. continue;
  507. if ((gfn & (tsize_pages - 1)) !=
  508. (pfn & (tsize_pages - 1)))
  509. continue;
  510. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  511. pfn &= ~(tsize_pages - 1);
  512. break;
  513. }
  514. }
  515. up_read(&current->mm->mmap_sem);
  516. }
  517. if (likely(!pfnmap)) {
  518. pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
  519. if (is_error_pfn(pfn)) {
  520. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  521. (long)gfn);
  522. kvm_release_pfn_clean(pfn);
  523. return;
  524. }
  525. }
  526. /* Drop old priv and setup new one. */
  527. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  528. kvmppc_e500_priv_release(priv);
  529. kvmppc_e500_priv_setup(priv, gtlbe, pfn);
  530. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, priv, gvaddr, stlbe);
  531. }
  532. /* XXX only map the one-one case, for now use TLB0 */
  533. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  534. int esel, struct tlbe *stlbe)
  535. {
  536. struct tlbe *gtlbe;
  537. gtlbe = &vcpu_e500->gtlb_arch[0][esel];
  538. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  539. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  540. gtlbe, 0, esel, stlbe);
  541. return esel;
  542. }
  543. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  544. * the shadow TLB. */
  545. /* XXX for both one-one and one-to-many , for now use TLB1 */
  546. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  547. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, struct tlbe *stlbe)
  548. {
  549. unsigned int victim;
  550. victim = vcpu_e500->gtlb_nv[1]++;
  551. if (unlikely(vcpu_e500->gtlb_nv[1] >= tlb1_max_shadow_size()))
  552. vcpu_e500->gtlb_nv[1] = 0;
  553. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim, stlbe);
  554. return victim;
  555. }
  556. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  557. {
  558. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  559. /* Recalc shadow pid since MSR changes */
  560. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  561. }
  562. static inline int kvmppc_e500_gtlbe_invalidate(
  563. struct kvmppc_vcpu_e500 *vcpu_e500,
  564. int tlbsel, int esel)
  565. {
  566. struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  567. if (unlikely(get_tlb_iprot(gtlbe)))
  568. return -1;
  569. gtlbe->mas1 = 0;
  570. return 0;
  571. }
  572. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  573. {
  574. int esel;
  575. if (value & MMUCSR0_TLB0FI)
  576. for (esel = 0; esel < vcpu_e500->gtlb_size[0]; esel++)
  577. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  578. if (value & MMUCSR0_TLB1FI)
  579. for (esel = 0; esel < vcpu_e500->gtlb_size[1]; esel++)
  580. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  581. /* Invalidate all vcpu id mappings */
  582. kvmppc_e500_id_table_reset_all(vcpu_e500);
  583. return EMULATE_DONE;
  584. }
  585. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  586. {
  587. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  588. unsigned int ia;
  589. int esel, tlbsel;
  590. gva_t ea;
  591. ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
  592. ia = (ea >> 2) & 0x1;
  593. /* since we only have two TLBs, only lower bit is used. */
  594. tlbsel = (ea >> 3) & 0x1;
  595. if (ia) {
  596. /* invalidate all entries */
  597. for (esel = 0; esel < vcpu_e500->gtlb_size[tlbsel]; esel++)
  598. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  599. } else {
  600. ea &= 0xfffff000;
  601. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  602. get_cur_pid(vcpu), -1);
  603. if (esel >= 0)
  604. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  605. }
  606. /* Invalidate all vcpu id mappings */
  607. kvmppc_e500_id_table_reset_all(vcpu_e500);
  608. return EMULATE_DONE;
  609. }
  610. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  611. {
  612. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  613. int tlbsel, esel;
  614. struct tlbe *gtlbe;
  615. tlbsel = get_tlb_tlbsel(vcpu_e500);
  616. esel = get_tlb_esel(vcpu_e500, tlbsel);
  617. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  618. vcpu_e500->mas0 &= ~MAS0_NV(~0);
  619. vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  620. vcpu_e500->mas1 = gtlbe->mas1;
  621. vcpu_e500->mas2 = gtlbe->mas2;
  622. vcpu_e500->mas3 = gtlbe->mas3;
  623. vcpu_e500->mas7 = gtlbe->mas7;
  624. return EMULATE_DONE;
  625. }
  626. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  627. {
  628. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  629. int as = !!get_cur_sas(vcpu_e500);
  630. unsigned int pid = get_cur_spid(vcpu_e500);
  631. int esel, tlbsel;
  632. struct tlbe *gtlbe = NULL;
  633. gva_t ea;
  634. ea = kvmppc_get_gpr(vcpu, rb);
  635. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  636. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  637. if (esel >= 0) {
  638. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  639. break;
  640. }
  641. }
  642. if (gtlbe) {
  643. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  644. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  645. vcpu_e500->mas1 = gtlbe->mas1;
  646. vcpu_e500->mas2 = gtlbe->mas2;
  647. vcpu_e500->mas3 = gtlbe->mas3;
  648. vcpu_e500->mas7 = gtlbe->mas7;
  649. } else {
  650. int victim;
  651. /* since we only have two TLBs, only lower bit is used. */
  652. tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
  653. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  654. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  655. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  656. vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
  657. | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  658. | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
  659. vcpu_e500->mas2 &= MAS2_EPN;
  660. vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
  661. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  662. vcpu_e500->mas7 = 0;
  663. }
  664. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  665. return EMULATE_DONE;
  666. }
  667. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  668. {
  669. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  670. struct tlbe *gtlbe;
  671. int tlbsel, esel;
  672. tlbsel = get_tlb_tlbsel(vcpu_e500);
  673. esel = get_tlb_esel(vcpu_e500, tlbsel);
  674. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  675. if (get_tlb_v(gtlbe))
  676. kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
  677. gtlbe->mas1 = vcpu_e500->mas1;
  678. gtlbe->mas2 = vcpu_e500->mas2;
  679. gtlbe->mas3 = vcpu_e500->mas3;
  680. gtlbe->mas7 = vcpu_e500->mas7;
  681. trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
  682. gtlbe->mas3, gtlbe->mas7);
  683. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  684. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  685. struct tlbe stlbe;
  686. int stlbsel, sesel;
  687. u64 eaddr;
  688. u64 raddr;
  689. preempt_disable();
  690. switch (tlbsel) {
  691. case 0:
  692. /* TLB0 */
  693. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  694. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  695. stlbsel = 0;
  696. sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  697. break;
  698. case 1:
  699. /* TLB1 */
  700. eaddr = get_tlb_eaddr(gtlbe);
  701. raddr = get_tlb_raddr(gtlbe);
  702. /* Create a 4KB mapping on the host.
  703. * If the guest wanted a large page,
  704. * only the first 4KB is mapped here and the rest
  705. * are mapped on the fly. */
  706. stlbsel = 1;
  707. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  708. raddr >> PAGE_SHIFT, gtlbe, &stlbe);
  709. break;
  710. default:
  711. BUG();
  712. }
  713. write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe);
  714. preempt_enable();
  715. }
  716. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  717. return EMULATE_DONE;
  718. }
  719. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  720. {
  721. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  722. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  723. }
  724. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  725. {
  726. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  727. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  728. }
  729. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  730. {
  731. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  732. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  733. }
  734. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  735. {
  736. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  737. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  738. }
  739. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  740. gva_t eaddr)
  741. {
  742. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  743. struct tlbe *gtlbe =
  744. &vcpu_e500->gtlb_arch[tlbsel_of(index)][esel_of(index)];
  745. u64 pgmask = get_tlb_bytes(gtlbe) - 1;
  746. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  747. }
  748. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  749. {
  750. }
  751. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  752. unsigned int index)
  753. {
  754. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  755. struct tlbe_priv *priv;
  756. struct tlbe *gtlbe, stlbe;
  757. int tlbsel = tlbsel_of(index);
  758. int esel = esel_of(index);
  759. int stlbsel, sesel;
  760. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  761. preempt_disable();
  762. switch (tlbsel) {
  763. case 0:
  764. stlbsel = 0;
  765. sesel = esel;
  766. priv = &vcpu_e500->gtlb_priv[stlbsel][sesel];
  767. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
  768. priv, eaddr, &stlbe);
  769. break;
  770. case 1: {
  771. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  772. stlbsel = 1;
  773. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
  774. gtlbe, &stlbe);
  775. break;
  776. }
  777. default:
  778. BUG();
  779. break;
  780. }
  781. write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe);
  782. preempt_enable();
  783. }
  784. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  785. gva_t eaddr, unsigned int pid, int as)
  786. {
  787. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  788. int esel, tlbsel;
  789. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  790. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  791. if (esel >= 0)
  792. return index_of(tlbsel, esel);
  793. }
  794. return -1;
  795. }
  796. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  797. {
  798. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  799. if (vcpu->arch.pid != pid) {
  800. vcpu_e500->pid[0] = vcpu->arch.pid = pid;
  801. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  802. }
  803. }
  804. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  805. {
  806. struct tlbe *tlbe;
  807. /* Insert large initial mapping for guest. */
  808. tlbe = &vcpu_e500->gtlb_arch[1][0];
  809. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
  810. tlbe->mas2 = 0;
  811. tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
  812. tlbe->mas7 = 0;
  813. /* 4K map for serial output. Used by kernel wrapper. */
  814. tlbe = &vcpu_e500->gtlb_arch[1][1];
  815. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  816. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  817. tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  818. tlbe->mas7 = 0;
  819. }
  820. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  821. {
  822. tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
  823. vcpu_e500->gtlb_size[0] = KVM_E500_TLB0_SIZE;
  824. vcpu_e500->gtlb_arch[0] =
  825. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  826. if (vcpu_e500->gtlb_arch[0] == NULL)
  827. goto err_out;
  828. vcpu_e500->gtlb_size[1] = KVM_E500_TLB1_SIZE;
  829. vcpu_e500->gtlb_arch[1] =
  830. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  831. if (vcpu_e500->gtlb_arch[1] == NULL)
  832. goto err_out_guest0;
  833. vcpu_e500->gtlb_priv[0] = (struct tlbe_priv *)
  834. kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  835. if (vcpu_e500->gtlb_priv[0] == NULL)
  836. goto err_out_guest1;
  837. vcpu_e500->gtlb_priv[1] = (struct tlbe_priv *)
  838. kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  839. if (vcpu_e500->gtlb_priv[1] == NULL)
  840. goto err_out_priv0;
  841. if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
  842. goto err_out_priv1;
  843. /* Init TLB configuration register */
  844. vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
  845. vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_size[0];
  846. vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
  847. vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_size[1];
  848. return 0;
  849. err_out_priv1:
  850. kfree(vcpu_e500->gtlb_priv[1]);
  851. err_out_priv0:
  852. kfree(vcpu_e500->gtlb_priv[0]);
  853. err_out_guest1:
  854. kfree(vcpu_e500->gtlb_arch[1]);
  855. err_out_guest0:
  856. kfree(vcpu_e500->gtlb_arch[0]);
  857. err_out:
  858. return -1;
  859. }
  860. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  861. {
  862. int stlbsel, i;
  863. /* release all privs */
  864. for (stlbsel = 0; stlbsel < 2; stlbsel++)
  865. for (i = 0; i < vcpu_e500->gtlb_size[stlbsel]; i++) {
  866. struct tlbe_priv *priv =
  867. &vcpu_e500->gtlb_priv[stlbsel][i];
  868. kvmppc_e500_priv_release(priv);
  869. }
  870. kvmppc_e500_id_table_free(vcpu_e500);
  871. kfree(vcpu_e500->gtlb_arch[1]);
  872. kfree(vcpu_e500->gtlb_arch[0]);
  873. }