slb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * PowerPC64 SLB support.
  3. *
  4. * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, IBM
  5. * Based on earlier code written by:
  6. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  7. * Copyright (c) 2001 Dave Engebretsen
  8. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <asm/pgtable.h>
  17. #include <asm/mmu.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/paca.h>
  20. #include <asm/cputable.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/smp.h>
  23. #include <linux/compiler.h>
  24. #include <linux/context_tracking.h>
  25. #include <linux/mm_types.h>
  26. #include <asm/udbg.h>
  27. #include <asm/code-patching.h>
  28. enum slb_index {
  29. LINEAR_INDEX = 0, /* Kernel linear map (0xc000000000000000) */
  30. VMALLOC_INDEX = 1, /* Kernel virtual map (0xd000000000000000) */
  31. KSTACK_INDEX = 2, /* Kernel stack map */
  32. };
  33. extern void slb_allocate(unsigned long ea);
  34. #define slb_esid_mask(ssize) \
  35. (((ssize) == MMU_SEGSIZE_256M)? ESID_MASK: ESID_MASK_1T)
  36. static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
  37. enum slb_index index)
  38. {
  39. return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
  40. }
  41. static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
  42. unsigned long flags)
  43. {
  44. return (get_kernel_vsid(ea, ssize) << slb_vsid_shift(ssize)) | flags |
  45. ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
  46. }
  47. static inline void slb_shadow_update(unsigned long ea, int ssize,
  48. unsigned long flags,
  49. enum slb_index index)
  50. {
  51. struct slb_shadow *p = get_slb_shadow();
  52. /*
  53. * Clear the ESID first so the entry is not valid while we are
  54. * updating it. No write barriers are needed here, provided
  55. * we only update the current CPU's SLB shadow buffer.
  56. */
  57. WRITE_ONCE(p->save_area[index].esid, 0);
  58. WRITE_ONCE(p->save_area[index].vsid, cpu_to_be64(mk_vsid_data(ea, ssize, flags)));
  59. WRITE_ONCE(p->save_area[index].esid, cpu_to_be64(mk_esid_data(ea, ssize, index)));
  60. }
  61. static inline void slb_shadow_clear(enum slb_index index)
  62. {
  63. WRITE_ONCE(get_slb_shadow()->save_area[index].esid, cpu_to_be64(index));
  64. }
  65. static inline void create_shadowed_slbe(unsigned long ea, int ssize,
  66. unsigned long flags,
  67. enum slb_index index)
  68. {
  69. /*
  70. * Updating the shadow buffer before writing the SLB ensures
  71. * we don't get a stale entry here if we get preempted by PHYP
  72. * between these two statements.
  73. */
  74. slb_shadow_update(ea, ssize, flags, index);
  75. asm volatile("slbmte %0,%1" :
  76. : "r" (mk_vsid_data(ea, ssize, flags)),
  77. "r" (mk_esid_data(ea, ssize, index))
  78. : "memory" );
  79. }
  80. /*
  81. * Insert bolted entries into SLB (which may not be empty, so don't clear
  82. * slb_cache_ptr).
  83. */
  84. void __slb_restore_bolted_realmode(void)
  85. {
  86. struct slb_shadow *p = get_slb_shadow();
  87. enum slb_index index;
  88. /* No isync needed because realmode. */
  89. for (index = 0; index < SLB_NUM_BOLTED; index++) {
  90. asm volatile("slbmte %0,%1" :
  91. : "r" (be64_to_cpu(p->save_area[index].vsid)),
  92. "r" (be64_to_cpu(p->save_area[index].esid)));
  93. }
  94. }
  95. /*
  96. * Insert the bolted entries into an empty SLB.
  97. * This is not the same as rebolt because the bolted segments are not
  98. * changed, just loaded from the shadow area.
  99. */
  100. void slb_restore_bolted_realmode(void)
  101. {
  102. __slb_restore_bolted_realmode();
  103. get_paca()->slb_cache_ptr = 0;
  104. }
  105. /*
  106. * This flushes all SLB entries including 0, so it must be realmode.
  107. */
  108. void slb_flush_all_realmode(void)
  109. {
  110. /*
  111. * This flushes all SLB entries including 0, so it must be realmode.
  112. */
  113. asm volatile("slbmte %0,%0; slbia" : : "r" (0));
  114. }
  115. static void __slb_flush_and_rebolt(void)
  116. {
  117. /* If you change this make sure you change SLB_NUM_BOLTED
  118. * and PR KVM appropriately too. */
  119. unsigned long linear_llp, vmalloc_llp, lflags, vflags;
  120. unsigned long ksp_esid_data, ksp_vsid_data;
  121. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  122. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  123. lflags = SLB_VSID_KERNEL | linear_llp;
  124. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  125. ksp_esid_data = mk_esid_data(get_paca()->kstack, mmu_kernel_ssize, KSTACK_INDEX);
  126. if ((ksp_esid_data & ~0xfffffffUL) <= PAGE_OFFSET) {
  127. ksp_esid_data &= ~SLB_ESID_V;
  128. ksp_vsid_data = 0;
  129. slb_shadow_clear(KSTACK_INDEX);
  130. } else {
  131. /* Update stack entry; others don't change */
  132. slb_shadow_update(get_paca()->kstack, mmu_kernel_ssize, lflags, KSTACK_INDEX);
  133. ksp_vsid_data =
  134. be64_to_cpu(get_slb_shadow()->save_area[KSTACK_INDEX].vsid);
  135. }
  136. /* We need to do this all in asm, so we're sure we don't touch
  137. * the stack between the slbia and rebolting it. */
  138. asm volatile("isync\n"
  139. "slbia\n"
  140. /* Slot 1 - first VMALLOC segment */
  141. "slbmte %0,%1\n"
  142. /* Slot 2 - kernel stack */
  143. "slbmte %2,%3\n"
  144. "isync"
  145. :: "r"(mk_vsid_data(VMALLOC_START, mmu_kernel_ssize, vflags)),
  146. "r"(mk_esid_data(VMALLOC_START, mmu_kernel_ssize, VMALLOC_INDEX)),
  147. "r"(ksp_vsid_data),
  148. "r"(ksp_esid_data)
  149. : "memory");
  150. }
  151. void slb_flush_and_rebolt(void)
  152. {
  153. WARN_ON(!irqs_disabled());
  154. /*
  155. * We can't take a PMU exception in the following code, so hard
  156. * disable interrupts.
  157. */
  158. hard_irq_disable();
  159. __slb_flush_and_rebolt();
  160. get_paca()->slb_cache_ptr = 0;
  161. }
  162. void slb_vmalloc_update(void)
  163. {
  164. unsigned long vflags;
  165. vflags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmalloc_psize].sllp;
  166. slb_shadow_update(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  167. slb_flush_and_rebolt();
  168. }
  169. /* Helper function to compare esids. There are four cases to handle.
  170. * 1. The system is not 1T segment size capable. Use the GET_ESID compare.
  171. * 2. The system is 1T capable, both addresses are < 1T, use the GET_ESID compare.
  172. * 3. The system is 1T capable, only one of the two addresses is > 1T. This is not a match.
  173. * 4. The system is 1T capable, both addresses are > 1T, use the GET_ESID_1T macro to compare.
  174. */
  175. static inline int esids_match(unsigned long addr1, unsigned long addr2)
  176. {
  177. int esid_1t_count;
  178. /* System is not 1T segment size capable. */
  179. if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
  180. return (GET_ESID(addr1) == GET_ESID(addr2));
  181. esid_1t_count = (((addr1 >> SID_SHIFT_1T) != 0) +
  182. ((addr2 >> SID_SHIFT_1T) != 0));
  183. /* both addresses are < 1T */
  184. if (esid_1t_count == 0)
  185. return (GET_ESID(addr1) == GET_ESID(addr2));
  186. /* One address < 1T, the other > 1T. Not a match */
  187. if (esid_1t_count == 1)
  188. return 0;
  189. /* Both addresses are > 1T. */
  190. return (GET_ESID_1T(addr1) == GET_ESID_1T(addr2));
  191. }
  192. /* Flush all user entries from the segment table of the current processor. */
  193. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  194. {
  195. unsigned long offset;
  196. unsigned long slbie_data = 0;
  197. unsigned long pc = KSTK_EIP(tsk);
  198. unsigned long stack = KSTK_ESP(tsk);
  199. unsigned long exec_base;
  200. /*
  201. * We need interrupts hard-disabled here, not just soft-disabled,
  202. * so that a PMU interrupt can't occur, which might try to access
  203. * user memory (to get a stack trace) and possible cause an SLB miss
  204. * which would update the slb_cache/slb_cache_ptr fields in the PACA.
  205. */
  206. hard_irq_disable();
  207. offset = get_paca()->slb_cache_ptr;
  208. if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
  209. offset <= SLB_CACHE_ENTRIES) {
  210. int i;
  211. asm volatile("isync" : : : "memory");
  212. for (i = 0; i < offset; i++) {
  213. slbie_data = (unsigned long)get_paca()->slb_cache[i]
  214. << SID_SHIFT; /* EA */
  215. slbie_data |= user_segment_size(slbie_data)
  216. << SLBIE_SSIZE_SHIFT;
  217. slbie_data |= SLBIE_C; /* C set for user addresses */
  218. asm volatile("slbie %0" : : "r" (slbie_data));
  219. }
  220. asm volatile("isync" : : : "memory");
  221. } else {
  222. __slb_flush_and_rebolt();
  223. }
  224. /* Workaround POWER5 < DD2.1 issue */
  225. if (offset == 1 || offset > SLB_CACHE_ENTRIES)
  226. asm volatile("slbie %0" : : "r" (slbie_data));
  227. get_paca()->slb_cache_ptr = 0;
  228. copy_mm_to_paca(mm);
  229. /*
  230. * preload some userspace segments into the SLB.
  231. * Almost all 32 and 64bit PowerPC executables are linked at
  232. * 0x10000000 so it makes sense to preload this segment.
  233. */
  234. exec_base = 0x10000000;
  235. if (is_kernel_addr(pc) || is_kernel_addr(stack) ||
  236. is_kernel_addr(exec_base))
  237. return;
  238. slb_allocate(pc);
  239. if (!esids_match(pc, stack))
  240. slb_allocate(stack);
  241. if (!esids_match(pc, exec_base) &&
  242. !esids_match(stack, exec_base))
  243. slb_allocate(exec_base);
  244. }
  245. static inline void patch_slb_encoding(unsigned int *insn_addr,
  246. unsigned int immed)
  247. {
  248. /*
  249. * This function patches either an li or a cmpldi instruction with
  250. * a new immediate value. This relies on the fact that both li
  251. * (which is actually addi) and cmpldi both take a 16-bit immediate
  252. * value, and it is situated in the same location in the instruction,
  253. * ie. bits 16-31 (Big endian bit order) or the lower 16 bits.
  254. * The signedness of the immediate operand differs between the two
  255. * instructions however this code is only ever patching a small value,
  256. * much less than 1 << 15, so we can get away with it.
  257. * To patch the value we read the existing instruction, clear the
  258. * immediate value, and or in our new value, then write the instruction
  259. * back.
  260. */
  261. unsigned int insn = (*insn_addr & 0xffff0000) | immed;
  262. patch_instruction(insn_addr, insn);
  263. }
  264. extern u32 slb_miss_kernel_load_linear[];
  265. extern u32 slb_miss_kernel_load_io[];
  266. extern u32 slb_compare_rr_to_size[];
  267. extern u32 slb_miss_kernel_load_vmemmap[];
  268. void slb_set_size(u16 size)
  269. {
  270. if (mmu_slb_size == size)
  271. return;
  272. mmu_slb_size = size;
  273. patch_slb_encoding(slb_compare_rr_to_size, mmu_slb_size);
  274. }
  275. void slb_initialize(void)
  276. {
  277. unsigned long linear_llp, vmalloc_llp, io_llp;
  278. unsigned long lflags, vflags;
  279. static int slb_encoding_inited;
  280. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  281. unsigned long vmemmap_llp;
  282. #endif
  283. /* Prepare our SLB miss handler based on our page size */
  284. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  285. io_llp = mmu_psize_defs[mmu_io_psize].sllp;
  286. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  287. get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
  288. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  289. vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
  290. #endif
  291. if (!slb_encoding_inited) {
  292. slb_encoding_inited = 1;
  293. patch_slb_encoding(slb_miss_kernel_load_linear,
  294. SLB_VSID_KERNEL | linear_llp);
  295. patch_slb_encoding(slb_miss_kernel_load_io,
  296. SLB_VSID_KERNEL | io_llp);
  297. patch_slb_encoding(slb_compare_rr_to_size,
  298. mmu_slb_size);
  299. pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
  300. pr_devel("SLB: io LLP = %04lx\n", io_llp);
  301. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  302. patch_slb_encoding(slb_miss_kernel_load_vmemmap,
  303. SLB_VSID_KERNEL | vmemmap_llp);
  304. pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
  305. #endif
  306. }
  307. get_paca()->stab_rr = SLB_NUM_BOLTED - 1;
  308. lflags = SLB_VSID_KERNEL | linear_llp;
  309. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  310. /* Invalidate the entire SLB (even entry 0) & all the ERATS */
  311. asm volatile("isync":::"memory");
  312. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  313. asm volatile("isync; slbia; isync":::"memory");
  314. create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
  315. create_shadowed_slbe(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  316. /* For the boot cpu, we're running on the stack in init_thread_union,
  317. * which is in the first segment of the linear mapping, and also
  318. * get_paca()->kstack hasn't been initialized yet.
  319. * For secondary cpus, we need to bolt the kernel stack entry now.
  320. */
  321. slb_shadow_clear(KSTACK_INDEX);
  322. if (raw_smp_processor_id() != boot_cpuid &&
  323. (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
  324. create_shadowed_slbe(get_paca()->kstack,
  325. mmu_kernel_ssize, lflags, KSTACK_INDEX);
  326. asm volatile("isync":::"memory");
  327. }
  328. static void insert_slb_entry(unsigned long vsid, unsigned long ea,
  329. int bpsize, int ssize)
  330. {
  331. unsigned long flags, vsid_data, esid_data;
  332. enum slb_index index;
  333. int slb_cache_index;
  334. /*
  335. * We are irq disabled, hence should be safe to access PACA.
  336. */
  337. VM_WARN_ON(!irqs_disabled());
  338. /*
  339. * We can't take a PMU exception in the following code, so hard
  340. * disable interrupts.
  341. */
  342. hard_irq_disable();
  343. index = get_paca()->stab_rr;
  344. /*
  345. * simple round-robin replacement of slb starting at SLB_NUM_BOLTED.
  346. */
  347. if (index < (mmu_slb_size - 1))
  348. index++;
  349. else
  350. index = SLB_NUM_BOLTED;
  351. get_paca()->stab_rr = index;
  352. flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
  353. vsid_data = (vsid << slb_vsid_shift(ssize)) | flags |
  354. ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
  355. esid_data = mk_esid_data(ea, ssize, index);
  356. /*
  357. * No need for an isync before or after this slbmte. The exception
  358. * we enter with and the rfid we exit with are context synchronizing.
  359. * Also we only handle user segments here.
  360. */
  361. asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data)
  362. : "memory");
  363. /*
  364. * Now update slb cache entries
  365. */
  366. slb_cache_index = get_paca()->slb_cache_ptr;
  367. if (slb_cache_index < SLB_CACHE_ENTRIES) {
  368. /*
  369. * We have space in slb cache for optimized switch_slb().
  370. * Top 36 bits from esid_data as per ISA
  371. */
  372. get_paca()->slb_cache[slb_cache_index++] = esid_data >> 28;
  373. get_paca()->slb_cache_ptr++;
  374. } else {
  375. /*
  376. * Our cache is full and the current cache content strictly
  377. * doesn't indicate the active SLB conents. Bump the ptr
  378. * so that switch_slb() will ignore the cache.
  379. */
  380. get_paca()->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
  381. }
  382. }
  383. static void handle_multi_context_slb_miss(int context_id, unsigned long ea)
  384. {
  385. struct mm_struct *mm = current->mm;
  386. unsigned long vsid;
  387. int bpsize;
  388. /*
  389. * We are always above 1TB, hence use high user segment size.
  390. */
  391. vsid = get_vsid(context_id, ea, mmu_highuser_ssize);
  392. bpsize = get_slice_psize(mm, ea);
  393. insert_slb_entry(vsid, ea, bpsize, mmu_highuser_ssize);
  394. }
  395. void slb_miss_large_addr(struct pt_regs *regs)
  396. {
  397. enum ctx_state prev_state = exception_enter();
  398. unsigned long ea = regs->dar;
  399. int context;
  400. if (REGION_ID(ea) != USER_REGION_ID)
  401. goto slb_bad_addr;
  402. /*
  403. * Are we beyound what the page table layout supports ?
  404. */
  405. if ((ea & ~REGION_MASK) >= H_PGTABLE_RANGE)
  406. goto slb_bad_addr;
  407. /* Lower address should have been handled by asm code */
  408. if (ea < (1UL << MAX_EA_BITS_PER_CONTEXT))
  409. goto slb_bad_addr;
  410. /*
  411. * consider this as bad access if we take a SLB miss
  412. * on an address above addr limit.
  413. */
  414. if (ea >= current->mm->context.slb_addr_limit)
  415. goto slb_bad_addr;
  416. context = get_ea_context(&current->mm->context, ea);
  417. if (!context)
  418. goto slb_bad_addr;
  419. handle_multi_context_slb_miss(context, ea);
  420. exception_exit(prev_state);
  421. return;
  422. slb_bad_addr:
  423. if (user_mode(regs))
  424. _exception(SIGSEGV, regs, SEGV_BNDERR, ea);
  425. else
  426. bad_page_fault(regs, ea, SIGSEGV);
  427. exception_exit(prev_state);
  428. }