e500_tlb.c 27 KB

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