slb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC64 SLB support.
  4. *
  5. * Copyright (C) 2004 David Gibson <[email protected]>, IBM
  6. * Based on earlier code written by:
  7. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  8. * Copyright (c) 2001 Dave Engebretsen
  9. * Copyright (C) 2002 Anton Blanchard <[email protected]>, IBM
  10. */
  11. #include <asm/interrupt.h>
  12. #include <asm/mmu.h>
  13. #include <asm/mmu_context.h>
  14. #include <asm/paca.h>
  15. #include <asm/lppaca.h>
  16. #include <asm/ppc-opcode.h>
  17. #include <asm/cputable.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/smp.h>
  20. #include <linux/compiler.h>
  21. #include <linux/context_tracking.h>
  22. #include <linux/mm_types.h>
  23. #include <linux/pgtable.h>
  24. #include <asm/udbg.h>
  25. #include <asm/code-patching.h>
  26. #include "internal.h"
  27. static long slb_allocate_user(struct mm_struct *mm, unsigned long ea);
  28. bool stress_slb_enabled __initdata;
  29. static int __init parse_stress_slb(char *p)
  30. {
  31. stress_slb_enabled = true;
  32. return 0;
  33. }
  34. early_param("stress_slb", parse_stress_slb);
  35. __ro_after_init DEFINE_STATIC_KEY_FALSE(stress_slb_key);
  36. static void assert_slb_presence(bool present, unsigned long ea)
  37. {
  38. #ifdef CONFIG_DEBUG_VM
  39. unsigned long tmp;
  40. WARN_ON_ONCE(mfmsr() & MSR_EE);
  41. if (!cpu_has_feature(CPU_FTR_ARCH_206))
  42. return;
  43. /*
  44. * slbfee. requires bit 24 (PPC bit 39) be clear in RB. Hardware
  45. * ignores all other bits from 0-27, so just clear them all.
  46. */
  47. ea &= ~((1UL << SID_SHIFT) - 1);
  48. asm volatile(__PPC_SLBFEE_DOT(%0, %1) : "=r"(tmp) : "r"(ea) : "cr0");
  49. WARN_ON(present == (tmp == 0));
  50. #endif
  51. }
  52. static inline void slb_shadow_update(unsigned long ea, int ssize,
  53. unsigned long flags,
  54. enum slb_index index)
  55. {
  56. struct slb_shadow *p = get_slb_shadow();
  57. /*
  58. * Clear the ESID first so the entry is not valid while we are
  59. * updating it. No write barriers are needed here, provided
  60. * we only update the current CPU's SLB shadow buffer.
  61. */
  62. WRITE_ONCE(p->save_area[index].esid, 0);
  63. WRITE_ONCE(p->save_area[index].vsid, cpu_to_be64(mk_vsid_data(ea, ssize, flags)));
  64. WRITE_ONCE(p->save_area[index].esid, cpu_to_be64(mk_esid_data(ea, ssize, index)));
  65. }
  66. static inline void slb_shadow_clear(enum slb_index index)
  67. {
  68. WRITE_ONCE(get_slb_shadow()->save_area[index].esid, cpu_to_be64(index));
  69. }
  70. static inline void create_shadowed_slbe(unsigned long ea, int ssize,
  71. unsigned long flags,
  72. enum slb_index index)
  73. {
  74. /*
  75. * Updating the shadow buffer before writing the SLB ensures
  76. * we don't get a stale entry here if we get preempted by PHYP
  77. * between these two statements.
  78. */
  79. slb_shadow_update(ea, ssize, flags, index);
  80. assert_slb_presence(false, ea);
  81. asm volatile("slbmte %0,%1" :
  82. : "r" (mk_vsid_data(ea, ssize, flags)),
  83. "r" (mk_esid_data(ea, ssize, index))
  84. : "memory" );
  85. }
  86. /*
  87. * Insert bolted entries into SLB (which may not be empty, so don't clear
  88. * slb_cache_ptr).
  89. */
  90. void __slb_restore_bolted_realmode(void)
  91. {
  92. struct slb_shadow *p = get_slb_shadow();
  93. enum slb_index index;
  94. /* No isync needed because realmode. */
  95. for (index = 0; index < SLB_NUM_BOLTED; index++) {
  96. asm volatile("slbmte %0,%1" :
  97. : "r" (be64_to_cpu(p->save_area[index].vsid)),
  98. "r" (be64_to_cpu(p->save_area[index].esid)));
  99. }
  100. assert_slb_presence(true, local_paca->kstack);
  101. }
  102. /*
  103. * Insert the bolted entries into an empty SLB.
  104. */
  105. void slb_restore_bolted_realmode(void)
  106. {
  107. __slb_restore_bolted_realmode();
  108. get_paca()->slb_cache_ptr = 0;
  109. get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
  110. get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
  111. }
  112. /*
  113. * This flushes all SLB entries including 0, so it must be realmode.
  114. */
  115. void slb_flush_all_realmode(void)
  116. {
  117. asm volatile("slbmte %0,%0; slbia" : : "r" (0));
  118. }
  119. static __always_inline void __slb_flush_and_restore_bolted(bool preserve_kernel_lookaside)
  120. {
  121. struct slb_shadow *p = get_slb_shadow();
  122. unsigned long ksp_esid_data, ksp_vsid_data;
  123. u32 ih;
  124. /*
  125. * SLBIA IH=1 on ISA v2.05 and newer processors may preserve lookaside
  126. * information created with Class=0 entries, which we use for kernel
  127. * SLB entries (the SLB entries themselves are still invalidated).
  128. *
  129. * Older processors will ignore this optimisation. Over-invalidation
  130. * is fine because we never rely on lookaside information existing.
  131. */
  132. if (preserve_kernel_lookaside)
  133. ih = 1;
  134. else
  135. ih = 0;
  136. ksp_esid_data = be64_to_cpu(p->save_area[KSTACK_INDEX].esid);
  137. ksp_vsid_data = be64_to_cpu(p->save_area[KSTACK_INDEX].vsid);
  138. asm volatile(PPC_SLBIA(%0)" \n"
  139. "slbmte %1, %2 \n"
  140. :: "i" (ih),
  141. "r" (ksp_vsid_data),
  142. "r" (ksp_esid_data)
  143. : "memory");
  144. }
  145. /*
  146. * This flushes non-bolted entries, it can be run in virtual mode. Must
  147. * be called with interrupts disabled.
  148. */
  149. void slb_flush_and_restore_bolted(void)
  150. {
  151. BUILD_BUG_ON(SLB_NUM_BOLTED != 2);
  152. WARN_ON(!irqs_disabled());
  153. /*
  154. * We can't take a PMU exception in the following code, so hard
  155. * disable interrupts.
  156. */
  157. hard_irq_disable();
  158. isync();
  159. __slb_flush_and_restore_bolted(false);
  160. isync();
  161. assert_slb_presence(true, get_paca()->kstack);
  162. get_paca()->slb_cache_ptr = 0;
  163. get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
  164. get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
  165. }
  166. void slb_save_contents(struct slb_entry *slb_ptr)
  167. {
  168. int i;
  169. unsigned long e, v;
  170. /* Save slb_cache_ptr value. */
  171. get_paca()->slb_save_cache_ptr = get_paca()->slb_cache_ptr;
  172. if (!slb_ptr)
  173. return;
  174. for (i = 0; i < mmu_slb_size; i++) {
  175. asm volatile("slbmfee %0,%1" : "=r" (e) : "r" (i));
  176. asm volatile("slbmfev %0,%1" : "=r" (v) : "r" (i));
  177. slb_ptr->esid = e;
  178. slb_ptr->vsid = v;
  179. slb_ptr++;
  180. }
  181. }
  182. void slb_dump_contents(struct slb_entry *slb_ptr)
  183. {
  184. int i, n;
  185. unsigned long e, v;
  186. unsigned long llp;
  187. if (!slb_ptr)
  188. return;
  189. pr_err("SLB contents of cpu 0x%x\n", smp_processor_id());
  190. for (i = 0; i < mmu_slb_size; i++) {
  191. e = slb_ptr->esid;
  192. v = slb_ptr->vsid;
  193. slb_ptr++;
  194. if (!e && !v)
  195. continue;
  196. pr_err("%02d %016lx %016lx %s\n", i, e, v,
  197. (e & SLB_ESID_V) ? "VALID" : "NOT VALID");
  198. if (!(e & SLB_ESID_V))
  199. continue;
  200. llp = v & SLB_VSID_LLP;
  201. if (v & SLB_VSID_B_1T) {
  202. pr_err(" 1T ESID=%9lx VSID=%13lx LLP:%3lx\n",
  203. GET_ESID_1T(e),
  204. (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T, llp);
  205. } else {
  206. pr_err(" 256M ESID=%9lx VSID=%13lx LLP:%3lx\n",
  207. GET_ESID(e),
  208. (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT, llp);
  209. }
  210. }
  211. if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
  212. /* RR is not so useful as it's often not used for allocation */
  213. pr_err("SLB RR allocator index %d\n", get_paca()->stab_rr);
  214. /* Dump slb cache entires as well. */
  215. pr_err("SLB cache ptr value = %d\n", get_paca()->slb_save_cache_ptr);
  216. pr_err("Valid SLB cache entries:\n");
  217. n = min_t(int, get_paca()->slb_save_cache_ptr, SLB_CACHE_ENTRIES);
  218. for (i = 0; i < n; i++)
  219. pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
  220. pr_err("Rest of SLB cache entries:\n");
  221. for (i = n; i < SLB_CACHE_ENTRIES; i++)
  222. pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
  223. }
  224. }
  225. void slb_vmalloc_update(void)
  226. {
  227. /*
  228. * vmalloc is not bolted, so just have to flush non-bolted.
  229. */
  230. slb_flush_and_restore_bolted();
  231. }
  232. static bool preload_hit(struct thread_info *ti, unsigned long esid)
  233. {
  234. unsigned char i;
  235. for (i = 0; i < ti->slb_preload_nr; i++) {
  236. unsigned char idx;
  237. idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
  238. if (esid == ti->slb_preload_esid[idx])
  239. return true;
  240. }
  241. return false;
  242. }
  243. static bool preload_add(struct thread_info *ti, unsigned long ea)
  244. {
  245. unsigned char idx;
  246. unsigned long esid;
  247. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  248. /* EAs are stored >> 28 so 256MB segments don't need clearing */
  249. if (ea & ESID_MASK_1T)
  250. ea &= ESID_MASK_1T;
  251. }
  252. esid = ea >> SID_SHIFT;
  253. if (preload_hit(ti, esid))
  254. return false;
  255. idx = (ti->slb_preload_tail + ti->slb_preload_nr) % SLB_PRELOAD_NR;
  256. ti->slb_preload_esid[idx] = esid;
  257. if (ti->slb_preload_nr == SLB_PRELOAD_NR)
  258. ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
  259. else
  260. ti->slb_preload_nr++;
  261. return true;
  262. }
  263. static void preload_age(struct thread_info *ti)
  264. {
  265. if (!ti->slb_preload_nr)
  266. return;
  267. ti->slb_preload_nr--;
  268. ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
  269. }
  270. void slb_setup_new_exec(void)
  271. {
  272. struct thread_info *ti = current_thread_info();
  273. struct mm_struct *mm = current->mm;
  274. unsigned long exec = 0x10000000;
  275. WARN_ON(irqs_disabled());
  276. /*
  277. * preload cache can only be used to determine whether a SLB
  278. * entry exists if it does not start to overflow.
  279. */
  280. if (ti->slb_preload_nr + 2 > SLB_PRELOAD_NR)
  281. return;
  282. hard_irq_disable();
  283. /*
  284. * We have no good place to clear the slb preload cache on exec,
  285. * flush_thread is about the earliest arch hook but that happens
  286. * after we switch to the mm and have already preloaded the SLBEs.
  287. *
  288. * For the most part that's probably okay to use entries from the
  289. * previous exec, they will age out if unused. It may turn out to
  290. * be an advantage to clear the cache before switching to it,
  291. * however.
  292. */
  293. /*
  294. * preload some userspace segments into the SLB.
  295. * Almost all 32 and 64bit PowerPC executables are linked at
  296. * 0x10000000 so it makes sense to preload this segment.
  297. */
  298. if (!is_kernel_addr(exec)) {
  299. if (preload_add(ti, exec))
  300. slb_allocate_user(mm, exec);
  301. }
  302. /* Libraries and mmaps. */
  303. if (!is_kernel_addr(mm->mmap_base)) {
  304. if (preload_add(ti, mm->mmap_base))
  305. slb_allocate_user(mm, mm->mmap_base);
  306. }
  307. /* see switch_slb */
  308. asm volatile("isync" : : : "memory");
  309. local_irq_enable();
  310. }
  311. void preload_new_slb_context(unsigned long start, unsigned long sp)
  312. {
  313. struct thread_info *ti = current_thread_info();
  314. struct mm_struct *mm = current->mm;
  315. unsigned long heap = mm->start_brk;
  316. WARN_ON(irqs_disabled());
  317. /* see above */
  318. if (ti->slb_preload_nr + 3 > SLB_PRELOAD_NR)
  319. return;
  320. hard_irq_disable();
  321. /* Userspace entry address. */
  322. if (!is_kernel_addr(start)) {
  323. if (preload_add(ti, start))
  324. slb_allocate_user(mm, start);
  325. }
  326. /* Top of stack, grows down. */
  327. if (!is_kernel_addr(sp)) {
  328. if (preload_add(ti, sp))
  329. slb_allocate_user(mm, sp);
  330. }
  331. /* Bottom of heap, grows up. */
  332. if (heap && !is_kernel_addr(heap)) {
  333. if (preload_add(ti, heap))
  334. slb_allocate_user(mm, heap);
  335. }
  336. /* see switch_slb */
  337. asm volatile("isync" : : : "memory");
  338. local_irq_enable();
  339. }
  340. static void slb_cache_slbie_kernel(unsigned int index)
  341. {
  342. unsigned long slbie_data = get_paca()->slb_cache[index];
  343. unsigned long ksp = get_paca()->kstack;
  344. slbie_data <<= SID_SHIFT;
  345. slbie_data |= 0xc000000000000000ULL;
  346. if ((ksp & slb_esid_mask(mmu_kernel_ssize)) == slbie_data)
  347. return;
  348. slbie_data |= mmu_kernel_ssize << SLBIE_SSIZE_SHIFT;
  349. asm volatile("slbie %0" : : "r" (slbie_data));
  350. }
  351. static void slb_cache_slbie_user(unsigned int index)
  352. {
  353. unsigned long slbie_data = get_paca()->slb_cache[index];
  354. slbie_data <<= SID_SHIFT;
  355. slbie_data |= user_segment_size(slbie_data) << SLBIE_SSIZE_SHIFT;
  356. slbie_data |= SLBIE_C; /* user slbs have C=1 */
  357. asm volatile("slbie %0" : : "r" (slbie_data));
  358. }
  359. /* Flush all user entries from the segment table of the current processor. */
  360. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  361. {
  362. struct thread_info *ti = task_thread_info(tsk);
  363. unsigned char i;
  364. /*
  365. * We need interrupts hard-disabled here, not just soft-disabled,
  366. * so that a PMU interrupt can't occur, which might try to access
  367. * user memory (to get a stack trace) and possible cause an SLB miss
  368. * which would update the slb_cache/slb_cache_ptr fields in the PACA.
  369. */
  370. hard_irq_disable();
  371. isync();
  372. if (stress_slb()) {
  373. __slb_flush_and_restore_bolted(false);
  374. isync();
  375. get_paca()->slb_cache_ptr = 0;
  376. get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
  377. } else if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  378. /*
  379. * SLBIA IH=3 invalidates all Class=1 SLBEs and their
  380. * associated lookaside structures, which matches what
  381. * switch_slb wants. So ARCH_300 does not use the slb
  382. * cache.
  383. */
  384. asm volatile(PPC_SLBIA(3));
  385. } else {
  386. unsigned long offset = get_paca()->slb_cache_ptr;
  387. if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
  388. offset <= SLB_CACHE_ENTRIES) {
  389. /*
  390. * Could assert_slb_presence(true) here, but
  391. * hypervisor or machine check could have come
  392. * in and removed the entry at this point.
  393. */
  394. for (i = 0; i < offset; i++)
  395. slb_cache_slbie_user(i);
  396. /* Workaround POWER5 < DD2.1 issue */
  397. if (!cpu_has_feature(CPU_FTR_ARCH_207S) && offset == 1)
  398. slb_cache_slbie_user(0);
  399. } else {
  400. /* Flush but retain kernel lookaside information */
  401. __slb_flush_and_restore_bolted(true);
  402. isync();
  403. get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
  404. }
  405. get_paca()->slb_cache_ptr = 0;
  406. }
  407. get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
  408. copy_mm_to_paca(mm);
  409. /*
  410. * We gradually age out SLBs after a number of context switches to
  411. * reduce reload overhead of unused entries (like we do with FP/VEC
  412. * reload). Each time we wrap 256 switches, take an entry out of the
  413. * SLB preload cache.
  414. */
  415. tsk->thread.load_slb++;
  416. if (!tsk->thread.load_slb) {
  417. unsigned long pc = KSTK_EIP(tsk);
  418. preload_age(ti);
  419. preload_add(ti, pc);
  420. }
  421. for (i = 0; i < ti->slb_preload_nr; i++) {
  422. unsigned char idx;
  423. unsigned long ea;
  424. idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
  425. ea = (unsigned long)ti->slb_preload_esid[idx] << SID_SHIFT;
  426. slb_allocate_user(mm, ea);
  427. }
  428. /*
  429. * Synchronize slbmte preloads with possible subsequent user memory
  430. * address accesses by the kernel (user mode won't happen until
  431. * rfid, which is safe).
  432. */
  433. isync();
  434. }
  435. void slb_set_size(u16 size)
  436. {
  437. mmu_slb_size = size;
  438. }
  439. void slb_initialize(void)
  440. {
  441. unsigned long linear_llp, vmalloc_llp, io_llp;
  442. unsigned long lflags;
  443. static int slb_encoding_inited;
  444. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  445. unsigned long vmemmap_llp;
  446. #endif
  447. /* Prepare our SLB miss handler based on our page size */
  448. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  449. io_llp = mmu_psize_defs[mmu_io_psize].sllp;
  450. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  451. get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
  452. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  453. vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
  454. #endif
  455. if (!slb_encoding_inited) {
  456. slb_encoding_inited = 1;
  457. pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
  458. pr_devel("SLB: io LLP = %04lx\n", io_llp);
  459. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  460. pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
  461. #endif
  462. }
  463. get_paca()->stab_rr = SLB_NUM_BOLTED - 1;
  464. get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
  465. get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
  466. lflags = SLB_VSID_KERNEL | linear_llp;
  467. /* Invalidate the entire SLB (even entry 0) & all the ERATS */
  468. asm volatile("isync":::"memory");
  469. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  470. asm volatile("isync; slbia; isync":::"memory");
  471. create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
  472. /*
  473. * For the boot cpu, we're running on the stack in init_thread_union,
  474. * which is in the first segment of the linear mapping, and also
  475. * get_paca()->kstack hasn't been initialized yet.
  476. * For secondary cpus, we need to bolt the kernel stack entry now.
  477. */
  478. slb_shadow_clear(KSTACK_INDEX);
  479. if (raw_smp_processor_id() != boot_cpuid &&
  480. (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
  481. create_shadowed_slbe(get_paca()->kstack,
  482. mmu_kernel_ssize, lflags, KSTACK_INDEX);
  483. asm volatile("isync":::"memory");
  484. }
  485. static void slb_cache_update(unsigned long esid_data)
  486. {
  487. int slb_cache_index;
  488. if (cpu_has_feature(CPU_FTR_ARCH_300))
  489. return; /* ISAv3.0B and later does not use slb_cache */
  490. if (stress_slb())
  491. return;
  492. /*
  493. * Now update slb cache entries
  494. */
  495. slb_cache_index = local_paca->slb_cache_ptr;
  496. if (slb_cache_index < SLB_CACHE_ENTRIES) {
  497. /*
  498. * We have space in slb cache for optimized switch_slb().
  499. * Top 36 bits from esid_data as per ISA
  500. */
  501. local_paca->slb_cache[slb_cache_index++] = esid_data >> SID_SHIFT;
  502. local_paca->slb_cache_ptr++;
  503. } else {
  504. /*
  505. * Our cache is full and the current cache content strictly
  506. * doesn't indicate the active SLB contents. Bump the ptr
  507. * so that switch_slb() will ignore the cache.
  508. */
  509. local_paca->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
  510. }
  511. }
  512. static enum slb_index alloc_slb_index(bool kernel)
  513. {
  514. enum slb_index index;
  515. /*
  516. * The allocation bitmaps can become out of synch with the SLB
  517. * when the _switch code does slbie when bolting a new stack
  518. * segment and it must not be anywhere else in the SLB. This leaves
  519. * a kernel allocated entry that is unused in the SLB. With very
  520. * large systems or small segment sizes, the bitmaps could slowly
  521. * fill with these entries. They will eventually be cleared out
  522. * by the round robin allocator in that case, so it's probably not
  523. * worth accounting for.
  524. */
  525. /*
  526. * SLBs beyond 32 entries are allocated with stab_rr only
  527. * POWER7/8/9 have 32 SLB entries, this could be expanded if a
  528. * future CPU has more.
  529. */
  530. if (local_paca->slb_used_bitmap != U32_MAX) {
  531. index = ffz(local_paca->slb_used_bitmap);
  532. local_paca->slb_used_bitmap |= 1U << index;
  533. if (kernel)
  534. local_paca->slb_kern_bitmap |= 1U << index;
  535. } else {
  536. /* round-robin replacement of slb starting at SLB_NUM_BOLTED. */
  537. index = local_paca->stab_rr;
  538. if (index < (mmu_slb_size - 1))
  539. index++;
  540. else
  541. index = SLB_NUM_BOLTED;
  542. local_paca->stab_rr = index;
  543. if (index < 32) {
  544. if (kernel)
  545. local_paca->slb_kern_bitmap |= 1U << index;
  546. else
  547. local_paca->slb_kern_bitmap &= ~(1U << index);
  548. }
  549. }
  550. BUG_ON(index < SLB_NUM_BOLTED);
  551. return index;
  552. }
  553. static long slb_insert_entry(unsigned long ea, unsigned long context,
  554. unsigned long flags, int ssize, bool kernel)
  555. {
  556. unsigned long vsid;
  557. unsigned long vsid_data, esid_data;
  558. enum slb_index index;
  559. vsid = get_vsid(context, ea, ssize);
  560. if (!vsid)
  561. return -EFAULT;
  562. /*
  563. * There must not be a kernel SLB fault in alloc_slb_index or before
  564. * slbmte here or the allocation bitmaps could get out of whack with
  565. * the SLB.
  566. *
  567. * User SLB faults or preloads take this path which might get inlined
  568. * into the caller, so add compiler barriers here to ensure unsafe
  569. * memory accesses do not come between.
  570. */
  571. barrier();
  572. index = alloc_slb_index(kernel);
  573. vsid_data = __mk_vsid_data(vsid, ssize, flags);
  574. esid_data = mk_esid_data(ea, ssize, index);
  575. /*
  576. * No need for an isync before or after this slbmte. The exception
  577. * we enter with and the rfid we exit with are context synchronizing.
  578. * User preloads should add isync afterwards in case the kernel
  579. * accesses user memory before it returns to userspace with rfid.
  580. */
  581. assert_slb_presence(false, ea);
  582. if (stress_slb()) {
  583. int slb_cache_index = local_paca->slb_cache_ptr;
  584. /*
  585. * stress_slb() does not use slb cache, repurpose as a
  586. * cache of inserted (non-bolted) kernel SLB entries. All
  587. * non-bolted kernel entries are flushed on any user fault,
  588. * or if there are already 3 non-boled kernel entries.
  589. */
  590. BUILD_BUG_ON(SLB_CACHE_ENTRIES < 3);
  591. if (!kernel || slb_cache_index == 3) {
  592. int i;
  593. for (i = 0; i < slb_cache_index; i++)
  594. slb_cache_slbie_kernel(i);
  595. slb_cache_index = 0;
  596. }
  597. if (kernel)
  598. local_paca->slb_cache[slb_cache_index++] = esid_data >> SID_SHIFT;
  599. local_paca->slb_cache_ptr = slb_cache_index;
  600. }
  601. asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data));
  602. barrier();
  603. if (!kernel)
  604. slb_cache_update(esid_data);
  605. return 0;
  606. }
  607. static long slb_allocate_kernel(unsigned long ea, unsigned long id)
  608. {
  609. unsigned long context;
  610. unsigned long flags;
  611. int ssize;
  612. if (id == LINEAR_MAP_REGION_ID) {
  613. /* We only support upto H_MAX_PHYSMEM_BITS */
  614. if ((ea & EA_MASK) > (1UL << H_MAX_PHYSMEM_BITS))
  615. return -EFAULT;
  616. flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp;
  617. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  618. } else if (id == VMEMMAP_REGION_ID) {
  619. if (ea >= H_VMEMMAP_END)
  620. return -EFAULT;
  621. flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmemmap_psize].sllp;
  622. #endif
  623. } else if (id == VMALLOC_REGION_ID) {
  624. if (ea >= H_VMALLOC_END)
  625. return -EFAULT;
  626. flags = local_paca->vmalloc_sllp;
  627. } else if (id == IO_REGION_ID) {
  628. if (ea >= H_KERN_IO_END)
  629. return -EFAULT;
  630. flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_io_psize].sllp;
  631. } else {
  632. return -EFAULT;
  633. }
  634. ssize = MMU_SEGSIZE_1T;
  635. if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
  636. ssize = MMU_SEGSIZE_256M;
  637. context = get_kernel_context(ea);
  638. return slb_insert_entry(ea, context, flags, ssize, true);
  639. }
  640. static long slb_allocate_user(struct mm_struct *mm, unsigned long ea)
  641. {
  642. unsigned long context;
  643. unsigned long flags;
  644. int bpsize;
  645. int ssize;
  646. /*
  647. * consider this as bad access if we take a SLB miss
  648. * on an address above addr limit.
  649. */
  650. if (ea >= mm_ctx_slb_addr_limit(&mm->context))
  651. return -EFAULT;
  652. context = get_user_context(&mm->context, ea);
  653. if (!context)
  654. return -EFAULT;
  655. if (unlikely(ea >= H_PGTABLE_RANGE)) {
  656. WARN_ON(1);
  657. return -EFAULT;
  658. }
  659. ssize = user_segment_size(ea);
  660. bpsize = get_slice_psize(mm, ea);
  661. flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
  662. return slb_insert_entry(ea, context, flags, ssize, false);
  663. }
  664. DEFINE_INTERRUPT_HANDLER_RAW(do_slb_fault)
  665. {
  666. unsigned long ea = regs->dar;
  667. unsigned long id = get_region_id(ea);
  668. /* IRQs are not reconciled here, so can't check irqs_disabled */
  669. VM_WARN_ON(mfmsr() & MSR_EE);
  670. if (regs_is_unrecoverable(regs))
  671. return -EINVAL;
  672. /*
  673. * SLB kernel faults must be very careful not to touch anything that is
  674. * not bolted. E.g., PACA and global variables are okay, mm->context
  675. * stuff is not. SLB user faults may access all of memory (and induce
  676. * one recursive SLB kernel fault), so the kernel fault must not
  677. * trample on the user fault state at those points.
  678. */
  679. /*
  680. * This is a raw interrupt handler, for performance, so that
  681. * fast_interrupt_return can be used. The handler must not touch local
  682. * irq state, or schedule. We could test for usermode and upgrade to a
  683. * normal process context (synchronous) interrupt for those, which
  684. * would make them first-class kernel code and able to be traced and
  685. * instrumented, although performance would suffer a bit, it would
  686. * probably be a good tradeoff.
  687. */
  688. if (id >= LINEAR_MAP_REGION_ID) {
  689. long err;
  690. #ifdef CONFIG_DEBUG_VM
  691. /* Catch recursive kernel SLB faults. */
  692. BUG_ON(local_paca->in_kernel_slb_handler);
  693. local_paca->in_kernel_slb_handler = 1;
  694. #endif
  695. err = slb_allocate_kernel(ea, id);
  696. #ifdef CONFIG_DEBUG_VM
  697. local_paca->in_kernel_slb_handler = 0;
  698. #endif
  699. return err;
  700. } else {
  701. struct mm_struct *mm = current->mm;
  702. long err;
  703. if (unlikely(!mm))
  704. return -EFAULT;
  705. err = slb_allocate_user(mm, ea);
  706. if (!err)
  707. preload_add(current_thread_info(), ea);
  708. return err;
  709. }
  710. }