e500_tlb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 size = vcpu_e500->gtlb_size[tlbsel];
  358. int set_base;
  359. int i;
  360. if (tlbsel == 0) {
  361. int mask = size / KVM_E500_TLB0_WAY_NUM - 1;
  362. set_base = (eaddr >> PAGE_SHIFT) & mask;
  363. set_base *= KVM_E500_TLB0_WAY_NUM;
  364. size = KVM_E500_TLB0_WAY_NUM;
  365. } else {
  366. set_base = 0;
  367. }
  368. for (i = 0; i < size; i++) {
  369. struct tlbe *tlbe = &vcpu_e500->gtlb_arch[tlbsel][set_base + i];
  370. unsigned int tid;
  371. if (eaddr < get_tlb_eaddr(tlbe))
  372. continue;
  373. if (eaddr > get_tlb_end(tlbe))
  374. continue;
  375. tid = get_tlb_tid(tlbe);
  376. if (tid && (tid != pid))
  377. continue;
  378. if (!get_tlb_v(tlbe))
  379. continue;
  380. if (get_tlb_ts(tlbe) != as && as != -1)
  381. continue;
  382. return set_base + i;
  383. }
  384. return -1;
  385. }
  386. static inline void kvmppc_e500_priv_setup(struct tlbe_priv *priv,
  387. struct tlbe *gtlbe,
  388. pfn_t pfn)
  389. {
  390. priv->pfn = pfn;
  391. priv->flags = E500_TLB_VALID;
  392. if (tlbe_is_writable(gtlbe))
  393. priv->flags |= E500_TLB_DIRTY;
  394. }
  395. static inline void kvmppc_e500_priv_release(struct tlbe_priv *priv)
  396. {
  397. if (priv->flags & E500_TLB_VALID) {
  398. if (priv->flags & E500_TLB_DIRTY)
  399. kvm_release_pfn_dirty(priv->pfn);
  400. else
  401. kvm_release_pfn_clean(priv->pfn);
  402. priv->flags = 0;
  403. }
  404. }
  405. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  406. unsigned int eaddr, int as)
  407. {
  408. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  409. unsigned int victim, pidsel, tsized;
  410. int tlbsel;
  411. /* since we only have two TLBs, only lower bit is used. */
  412. tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
  413. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  414. pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
  415. tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
  416. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  417. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  418. vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  419. | MAS1_TID(vcpu_e500->pid[pidsel])
  420. | MAS1_TSIZE(tsized);
  421. vcpu_e500->mas2 = (eaddr & MAS2_EPN)
  422. | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
  423. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  424. vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
  425. | (get_cur_pid(vcpu) << 16)
  426. | (as ? MAS6_SAS : 0);
  427. vcpu_e500->mas7 = 0;
  428. }
  429. /* TID must be supplied by the caller */
  430. static inline void kvmppc_e500_setup_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  431. struct tlbe *gtlbe, int tsize,
  432. struct tlbe_priv *priv,
  433. u64 gvaddr, struct tlbe *stlbe)
  434. {
  435. pfn_t pfn = priv->pfn;
  436. /* Force TS=1 IPROT=0 for all guest mappings. */
  437. stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
  438. stlbe->mas2 = (gvaddr & MAS2_EPN)
  439. | e500_shadow_mas2_attrib(gtlbe->mas2,
  440. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  441. stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
  442. | e500_shadow_mas3_attrib(gtlbe->mas3,
  443. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  444. stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
  445. }
  446. static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  447. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel,
  448. struct tlbe *stlbe)
  449. {
  450. struct kvm_memory_slot *slot;
  451. unsigned long pfn, hva;
  452. int pfnmap = 0;
  453. int tsize = BOOK3E_PAGESZ_4K;
  454. struct tlbe_priv *priv;
  455. /*
  456. * Translate guest physical to true physical, acquiring
  457. * a page reference if it is normal, non-reserved memory.
  458. *
  459. * gfn_to_memslot() must succeed because otherwise we wouldn't
  460. * have gotten this far. Eventually we should just pass the slot
  461. * pointer through from the first lookup.
  462. */
  463. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  464. hva = gfn_to_hva_memslot(slot, gfn);
  465. if (tlbsel == 1) {
  466. struct vm_area_struct *vma;
  467. down_read(&current->mm->mmap_sem);
  468. vma = find_vma(current->mm, hva);
  469. if (vma && hva >= vma->vm_start &&
  470. (vma->vm_flags & VM_PFNMAP)) {
  471. /*
  472. * This VMA is a physically contiguous region (e.g.
  473. * /dev/mem) that bypasses normal Linux page
  474. * management. Find the overlap between the
  475. * vma and the memslot.
  476. */
  477. unsigned long start, end;
  478. unsigned long slot_start, slot_end;
  479. pfnmap = 1;
  480. start = vma->vm_pgoff;
  481. end = start +
  482. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  483. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  484. slot_start = pfn - (gfn - slot->base_gfn);
  485. slot_end = slot_start + slot->npages;
  486. if (start < slot_start)
  487. start = slot_start;
  488. if (end > slot_end)
  489. end = slot_end;
  490. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  491. MAS1_TSIZE_SHIFT;
  492. /*
  493. * e500 doesn't implement the lowest tsize bit,
  494. * or 1K pages.
  495. */
  496. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  497. /*
  498. * Now find the largest tsize (up to what the guest
  499. * requested) that will cover gfn, stay within the
  500. * range, and for which gfn and pfn are mutually
  501. * aligned.
  502. */
  503. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  504. unsigned long gfn_start, gfn_end, tsize_pages;
  505. tsize_pages = 1 << (tsize - 2);
  506. gfn_start = gfn & ~(tsize_pages - 1);
  507. gfn_end = gfn_start + tsize_pages;
  508. if (gfn_start + pfn - gfn < start)
  509. continue;
  510. if (gfn_end + pfn - gfn > end)
  511. continue;
  512. if ((gfn & (tsize_pages - 1)) !=
  513. (pfn & (tsize_pages - 1)))
  514. continue;
  515. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  516. pfn &= ~(tsize_pages - 1);
  517. break;
  518. }
  519. }
  520. up_read(&current->mm->mmap_sem);
  521. }
  522. if (likely(!pfnmap)) {
  523. pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
  524. if (is_error_pfn(pfn)) {
  525. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  526. (long)gfn);
  527. kvm_release_pfn_clean(pfn);
  528. return;
  529. }
  530. }
  531. /* Drop old priv and setup new one. */
  532. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  533. kvmppc_e500_priv_release(priv);
  534. kvmppc_e500_priv_setup(priv, gtlbe, pfn);
  535. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, priv, gvaddr, stlbe);
  536. }
  537. /* XXX only map the one-one case, for now use TLB0 */
  538. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  539. int esel, struct tlbe *stlbe)
  540. {
  541. struct tlbe *gtlbe;
  542. gtlbe = &vcpu_e500->gtlb_arch[0][esel];
  543. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  544. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  545. gtlbe, 0, esel, stlbe);
  546. return esel;
  547. }
  548. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  549. * the shadow TLB. */
  550. /* XXX for both one-one and one-to-many , for now use TLB1 */
  551. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  552. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, struct tlbe *stlbe)
  553. {
  554. unsigned int victim;
  555. victim = vcpu_e500->gtlb_nv[1]++;
  556. if (unlikely(vcpu_e500->gtlb_nv[1] >= tlb1_max_shadow_size()))
  557. vcpu_e500->gtlb_nv[1] = 0;
  558. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim, stlbe);
  559. return victim;
  560. }
  561. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  562. {
  563. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  564. /* Recalc shadow pid since MSR changes */
  565. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  566. }
  567. static inline int kvmppc_e500_gtlbe_invalidate(
  568. struct kvmppc_vcpu_e500 *vcpu_e500,
  569. int tlbsel, int esel)
  570. {
  571. struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  572. if (unlikely(get_tlb_iprot(gtlbe)))
  573. return -1;
  574. gtlbe->mas1 = 0;
  575. return 0;
  576. }
  577. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  578. {
  579. int esel;
  580. if (value & MMUCSR0_TLB0FI)
  581. for (esel = 0; esel < vcpu_e500->gtlb_size[0]; esel++)
  582. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  583. if (value & MMUCSR0_TLB1FI)
  584. for (esel = 0; esel < vcpu_e500->gtlb_size[1]; esel++)
  585. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  586. /* Invalidate all vcpu id mappings */
  587. kvmppc_e500_id_table_reset_all(vcpu_e500);
  588. return EMULATE_DONE;
  589. }
  590. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  591. {
  592. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  593. unsigned int ia;
  594. int esel, tlbsel;
  595. gva_t ea;
  596. ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
  597. ia = (ea >> 2) & 0x1;
  598. /* since we only have two TLBs, only lower bit is used. */
  599. tlbsel = (ea >> 3) & 0x1;
  600. if (ia) {
  601. /* invalidate all entries */
  602. for (esel = 0; esel < vcpu_e500->gtlb_size[tlbsel]; esel++)
  603. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  604. } else {
  605. ea &= 0xfffff000;
  606. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  607. get_cur_pid(vcpu), -1);
  608. if (esel >= 0)
  609. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  610. }
  611. /* Invalidate all vcpu id mappings */
  612. kvmppc_e500_id_table_reset_all(vcpu_e500);
  613. return EMULATE_DONE;
  614. }
  615. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  616. {
  617. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  618. int tlbsel, esel;
  619. struct tlbe *gtlbe;
  620. tlbsel = get_tlb_tlbsel(vcpu_e500);
  621. esel = get_tlb_esel(vcpu_e500, tlbsel);
  622. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  623. vcpu_e500->mas0 &= ~MAS0_NV(~0);
  624. vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  625. vcpu_e500->mas1 = gtlbe->mas1;
  626. vcpu_e500->mas2 = gtlbe->mas2;
  627. vcpu_e500->mas3 = gtlbe->mas3;
  628. vcpu_e500->mas7 = gtlbe->mas7;
  629. return EMULATE_DONE;
  630. }
  631. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  632. {
  633. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  634. int as = !!get_cur_sas(vcpu_e500);
  635. unsigned int pid = get_cur_spid(vcpu_e500);
  636. int esel, tlbsel;
  637. struct tlbe *gtlbe = NULL;
  638. gva_t ea;
  639. ea = kvmppc_get_gpr(vcpu, rb);
  640. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  641. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  642. if (esel >= 0) {
  643. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  644. break;
  645. }
  646. }
  647. if (gtlbe) {
  648. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  649. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  650. vcpu_e500->mas1 = gtlbe->mas1;
  651. vcpu_e500->mas2 = gtlbe->mas2;
  652. vcpu_e500->mas3 = gtlbe->mas3;
  653. vcpu_e500->mas7 = gtlbe->mas7;
  654. } else {
  655. int victim;
  656. /* since we only have two TLBs, only lower bit is used. */
  657. tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
  658. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  659. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  660. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  661. vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
  662. | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  663. | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
  664. vcpu_e500->mas2 &= MAS2_EPN;
  665. vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
  666. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  667. vcpu_e500->mas7 = 0;
  668. }
  669. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  670. return EMULATE_DONE;
  671. }
  672. /* sesel is index into the set, not the whole array */
  673. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  674. struct tlbe *gtlbe,
  675. struct tlbe *stlbe,
  676. int stlbsel, int sesel)
  677. {
  678. int stid;
  679. preempt_disable();
  680. stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
  681. get_tlb_tid(gtlbe),
  682. get_cur_pr(&vcpu_e500->vcpu), 0);
  683. stlbe->mas1 |= MAS1_TID(stid);
  684. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  685. preempt_enable();
  686. }
  687. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  688. {
  689. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  690. struct tlbe *gtlbe;
  691. int tlbsel, esel;
  692. tlbsel = get_tlb_tlbsel(vcpu_e500);
  693. esel = get_tlb_esel(vcpu_e500, tlbsel);
  694. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  695. if (get_tlb_v(gtlbe))
  696. kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
  697. gtlbe->mas1 = vcpu_e500->mas1;
  698. gtlbe->mas2 = vcpu_e500->mas2;
  699. gtlbe->mas3 = vcpu_e500->mas3;
  700. gtlbe->mas7 = vcpu_e500->mas7;
  701. trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
  702. gtlbe->mas3, gtlbe->mas7);
  703. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  704. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  705. struct tlbe stlbe;
  706. int stlbsel, sesel;
  707. u64 eaddr;
  708. u64 raddr;
  709. switch (tlbsel) {
  710. case 0:
  711. /* TLB0 */
  712. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  713. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  714. stlbsel = 0;
  715. sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  716. break;
  717. case 1:
  718. /* TLB1 */
  719. eaddr = get_tlb_eaddr(gtlbe);
  720. raddr = get_tlb_raddr(gtlbe);
  721. /* Create a 4KB mapping on the host.
  722. * If the guest wanted a large page,
  723. * only the first 4KB is mapped here and the rest
  724. * are mapped on the fly. */
  725. stlbsel = 1;
  726. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  727. raddr >> PAGE_SHIFT, gtlbe, &stlbe);
  728. break;
  729. default:
  730. BUG();
  731. }
  732. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  733. }
  734. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  735. return EMULATE_DONE;
  736. }
  737. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  738. {
  739. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  740. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  741. }
  742. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  743. {
  744. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  745. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  746. }
  747. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  748. {
  749. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  750. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  751. }
  752. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  753. {
  754. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  755. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  756. }
  757. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  758. gva_t eaddr)
  759. {
  760. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  761. struct tlbe *gtlbe =
  762. &vcpu_e500->gtlb_arch[tlbsel_of(index)][esel_of(index)];
  763. u64 pgmask = get_tlb_bytes(gtlbe) - 1;
  764. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  765. }
  766. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  767. {
  768. }
  769. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  770. unsigned int index)
  771. {
  772. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  773. struct tlbe_priv *priv;
  774. struct tlbe *gtlbe, stlbe;
  775. int tlbsel = tlbsel_of(index);
  776. int esel = esel_of(index);
  777. int stlbsel, sesel;
  778. gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
  779. switch (tlbsel) {
  780. case 0:
  781. stlbsel = 0;
  782. sesel = esel;
  783. priv = &vcpu_e500->gtlb_priv[stlbsel][sesel];
  784. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
  785. priv, eaddr, &stlbe);
  786. break;
  787. case 1: {
  788. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  789. stlbsel = 1;
  790. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
  791. gtlbe, &stlbe);
  792. break;
  793. }
  794. default:
  795. BUG();
  796. break;
  797. }
  798. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  799. }
  800. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  801. gva_t eaddr, unsigned int pid, int as)
  802. {
  803. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  804. int esel, tlbsel;
  805. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  806. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  807. if (esel >= 0)
  808. return index_of(tlbsel, esel);
  809. }
  810. return -1;
  811. }
  812. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  813. {
  814. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  815. if (vcpu->arch.pid != pid) {
  816. vcpu_e500->pid[0] = vcpu->arch.pid = pid;
  817. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  818. }
  819. }
  820. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  821. {
  822. struct tlbe *tlbe;
  823. /* Insert large initial mapping for guest. */
  824. tlbe = &vcpu_e500->gtlb_arch[1][0];
  825. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
  826. tlbe->mas2 = 0;
  827. tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
  828. tlbe->mas7 = 0;
  829. /* 4K map for serial output. Used by kernel wrapper. */
  830. tlbe = &vcpu_e500->gtlb_arch[1][1];
  831. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  832. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  833. tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  834. tlbe->mas7 = 0;
  835. }
  836. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  837. {
  838. tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
  839. vcpu_e500->gtlb_size[0] = KVM_E500_TLB0_SIZE;
  840. vcpu_e500->gtlb_arch[0] =
  841. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  842. if (vcpu_e500->gtlb_arch[0] == NULL)
  843. goto err_out;
  844. vcpu_e500->gtlb_size[1] = KVM_E500_TLB1_SIZE;
  845. vcpu_e500->gtlb_arch[1] =
  846. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  847. if (vcpu_e500->gtlb_arch[1] == NULL)
  848. goto err_out_guest0;
  849. vcpu_e500->gtlb_priv[0] = (struct tlbe_priv *)
  850. kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  851. if (vcpu_e500->gtlb_priv[0] == NULL)
  852. goto err_out_guest1;
  853. vcpu_e500->gtlb_priv[1] = (struct tlbe_priv *)
  854. kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  855. if (vcpu_e500->gtlb_priv[1] == NULL)
  856. goto err_out_priv0;
  857. if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
  858. goto err_out_priv1;
  859. /* Init TLB configuration register */
  860. vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
  861. vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_size[0];
  862. vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
  863. vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_size[1];
  864. return 0;
  865. err_out_priv1:
  866. kfree(vcpu_e500->gtlb_priv[1]);
  867. err_out_priv0:
  868. kfree(vcpu_e500->gtlb_priv[0]);
  869. err_out_guest1:
  870. kfree(vcpu_e500->gtlb_arch[1]);
  871. err_out_guest0:
  872. kfree(vcpu_e500->gtlb_arch[0]);
  873. err_out:
  874. return -1;
  875. }
  876. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  877. {
  878. int stlbsel, i;
  879. /* release all privs */
  880. for (stlbsel = 0; stlbsel < 2; stlbsel++)
  881. for (i = 0; i < vcpu_e500->gtlb_size[stlbsel]; i++) {
  882. struct tlbe_priv *priv =
  883. &vcpu_e500->gtlb_priv[stlbsel][i];
  884. kvmppc_e500_priv_release(priv);
  885. }
  886. kvmppc_e500_id_table_free(vcpu_e500);
  887. kfree(vcpu_e500->gtlb_arch[1]);
  888. kfree(vcpu_e500->gtlb_arch[0]);
  889. }