slb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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, 0);
  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. static void __slb_flush_and_rebolt(void)
  81. {
  82. /* If you change this make sure you change SLB_NUM_BOLTED
  83. * and PR KVM appropriately too. */
  84. unsigned long linear_llp, vmalloc_llp, lflags, vflags;
  85. unsigned long ksp_esid_data, ksp_vsid_data;
  86. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  87. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  88. lflags = SLB_VSID_KERNEL | linear_llp;
  89. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  90. ksp_esid_data = mk_esid_data(get_paca()->kstack, mmu_kernel_ssize, KSTACK_INDEX);
  91. if ((ksp_esid_data & ~0xfffffffUL) <= PAGE_OFFSET) {
  92. ksp_esid_data &= ~SLB_ESID_V;
  93. ksp_vsid_data = 0;
  94. slb_shadow_clear(KSTACK_INDEX);
  95. } else {
  96. /* Update stack entry; others don't change */
  97. slb_shadow_update(get_paca()->kstack, mmu_kernel_ssize, lflags, KSTACK_INDEX);
  98. ksp_vsid_data =
  99. be64_to_cpu(get_slb_shadow()->save_area[KSTACK_INDEX].vsid);
  100. }
  101. /* We need to do this all in asm, so we're sure we don't touch
  102. * the stack between the slbia and rebolting it. */
  103. asm volatile("isync\n"
  104. "slbia\n"
  105. /* Slot 1 - first VMALLOC segment */
  106. "slbmte %0,%1\n"
  107. /* Slot 2 - kernel stack */
  108. "slbmte %2,%3\n"
  109. "isync"
  110. :: "r"(mk_vsid_data(VMALLOC_START, mmu_kernel_ssize, vflags)),
  111. "r"(mk_esid_data(VMALLOC_START, mmu_kernel_ssize, VMALLOC_INDEX)),
  112. "r"(ksp_vsid_data),
  113. "r"(ksp_esid_data)
  114. : "memory");
  115. }
  116. void slb_flush_and_rebolt(void)
  117. {
  118. WARN_ON(!irqs_disabled());
  119. /*
  120. * We can't take a PMU exception in the following code, so hard
  121. * disable interrupts.
  122. */
  123. hard_irq_disable();
  124. __slb_flush_and_rebolt();
  125. get_paca()->slb_cache_ptr = 0;
  126. }
  127. void slb_vmalloc_update(void)
  128. {
  129. unsigned long vflags;
  130. vflags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmalloc_psize].sllp;
  131. slb_shadow_update(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  132. slb_flush_and_rebolt();
  133. }
  134. /* Helper function to compare esids. There are four cases to handle.
  135. * 1. The system is not 1T segment size capable. Use the GET_ESID compare.
  136. * 2. The system is 1T capable, both addresses are < 1T, use the GET_ESID compare.
  137. * 3. The system is 1T capable, only one of the two addresses is > 1T. This is not a match.
  138. * 4. The system is 1T capable, both addresses are > 1T, use the GET_ESID_1T macro to compare.
  139. */
  140. static inline int esids_match(unsigned long addr1, unsigned long addr2)
  141. {
  142. int esid_1t_count;
  143. /* System is not 1T segment size capable. */
  144. if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
  145. return (GET_ESID(addr1) == GET_ESID(addr2));
  146. esid_1t_count = (((addr1 >> SID_SHIFT_1T) != 0) +
  147. ((addr2 >> SID_SHIFT_1T) != 0));
  148. /* both addresses are < 1T */
  149. if (esid_1t_count == 0)
  150. return (GET_ESID(addr1) == GET_ESID(addr2));
  151. /* One address < 1T, the other > 1T. Not a match */
  152. if (esid_1t_count == 1)
  153. return 0;
  154. /* Both addresses are > 1T. */
  155. return (GET_ESID_1T(addr1) == GET_ESID_1T(addr2));
  156. }
  157. /* Flush all user entries from the segment table of the current processor. */
  158. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  159. {
  160. unsigned long offset;
  161. unsigned long slbie_data = 0;
  162. unsigned long pc = KSTK_EIP(tsk);
  163. unsigned long stack = KSTK_ESP(tsk);
  164. unsigned long exec_base;
  165. /*
  166. * We need interrupts hard-disabled here, not just soft-disabled,
  167. * so that a PMU interrupt can't occur, which might try to access
  168. * user memory (to get a stack trace) and possible cause an SLB miss
  169. * which would update the slb_cache/slb_cache_ptr fields in the PACA.
  170. */
  171. hard_irq_disable();
  172. offset = get_paca()->slb_cache_ptr;
  173. if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
  174. offset <= SLB_CACHE_ENTRIES) {
  175. int i;
  176. asm volatile("isync" : : : "memory");
  177. for (i = 0; i < offset; i++) {
  178. slbie_data = (unsigned long)get_paca()->slb_cache[i]
  179. << SID_SHIFT; /* EA */
  180. slbie_data |= user_segment_size(slbie_data)
  181. << SLBIE_SSIZE_SHIFT;
  182. slbie_data |= SLBIE_C; /* C set for user addresses */
  183. asm volatile("slbie %0" : : "r" (slbie_data));
  184. }
  185. asm volatile("isync" : : : "memory");
  186. } else {
  187. __slb_flush_and_rebolt();
  188. }
  189. /* Workaround POWER5 < DD2.1 issue */
  190. if (offset == 1 || offset > SLB_CACHE_ENTRIES)
  191. asm volatile("slbie %0" : : "r" (slbie_data));
  192. get_paca()->slb_cache_ptr = 0;
  193. copy_mm_to_paca(mm);
  194. /*
  195. * preload some userspace segments into the SLB.
  196. * Almost all 32 and 64bit PowerPC executables are linked at
  197. * 0x10000000 so it makes sense to preload this segment.
  198. */
  199. exec_base = 0x10000000;
  200. if (is_kernel_addr(pc) || is_kernel_addr(stack) ||
  201. is_kernel_addr(exec_base))
  202. return;
  203. slb_allocate(pc);
  204. if (!esids_match(pc, stack))
  205. slb_allocate(stack);
  206. if (!esids_match(pc, exec_base) &&
  207. !esids_match(stack, exec_base))
  208. slb_allocate(exec_base);
  209. }
  210. static inline void patch_slb_encoding(unsigned int *insn_addr,
  211. unsigned int immed)
  212. {
  213. /*
  214. * This function patches either an li or a cmpldi instruction with
  215. * a new immediate value. This relies on the fact that both li
  216. * (which is actually addi) and cmpldi both take a 16-bit immediate
  217. * value, and it is situated in the same location in the instruction,
  218. * ie. bits 16-31 (Big endian bit order) or the lower 16 bits.
  219. * The signedness of the immediate operand differs between the two
  220. * instructions however this code is only ever patching a small value,
  221. * much less than 1 << 15, so we can get away with it.
  222. * To patch the value we read the existing instruction, clear the
  223. * immediate value, and or in our new value, then write the instruction
  224. * back.
  225. */
  226. unsigned int insn = (*insn_addr & 0xffff0000) | immed;
  227. patch_instruction(insn_addr, insn);
  228. }
  229. extern u32 slb_miss_kernel_load_linear[];
  230. extern u32 slb_miss_kernel_load_io[];
  231. extern u32 slb_compare_rr_to_size[];
  232. extern u32 slb_miss_kernel_load_vmemmap[];
  233. void slb_set_size(u16 size)
  234. {
  235. if (mmu_slb_size == size)
  236. return;
  237. mmu_slb_size = size;
  238. patch_slb_encoding(slb_compare_rr_to_size, mmu_slb_size);
  239. }
  240. void slb_initialize(void)
  241. {
  242. unsigned long linear_llp, vmalloc_llp, io_llp;
  243. unsigned long lflags, vflags;
  244. static int slb_encoding_inited;
  245. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  246. unsigned long vmemmap_llp;
  247. #endif
  248. /* Prepare our SLB miss handler based on our page size */
  249. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  250. io_llp = mmu_psize_defs[mmu_io_psize].sllp;
  251. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  252. get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
  253. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  254. vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
  255. #endif
  256. if (!slb_encoding_inited) {
  257. slb_encoding_inited = 1;
  258. patch_slb_encoding(slb_miss_kernel_load_linear,
  259. SLB_VSID_KERNEL | linear_llp);
  260. patch_slb_encoding(slb_miss_kernel_load_io,
  261. SLB_VSID_KERNEL | io_llp);
  262. patch_slb_encoding(slb_compare_rr_to_size,
  263. mmu_slb_size);
  264. pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
  265. pr_devel("SLB: io LLP = %04lx\n", io_llp);
  266. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  267. patch_slb_encoding(slb_miss_kernel_load_vmemmap,
  268. SLB_VSID_KERNEL | vmemmap_llp);
  269. pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
  270. #endif
  271. }
  272. get_paca()->stab_rr = SLB_NUM_BOLTED;
  273. lflags = SLB_VSID_KERNEL | linear_llp;
  274. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  275. /* Invalidate the entire SLB (even entry 0) & all the ERATS */
  276. asm volatile("isync":::"memory");
  277. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  278. asm volatile("isync; slbia; isync":::"memory");
  279. create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
  280. create_shadowed_slbe(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  281. /* For the boot cpu, we're running on the stack in init_thread_union,
  282. * which is in the first segment of the linear mapping, and also
  283. * get_paca()->kstack hasn't been initialized yet.
  284. * For secondary cpus, we need to bolt the kernel stack entry now.
  285. */
  286. slb_shadow_clear(KSTACK_INDEX);
  287. if (raw_smp_processor_id() != boot_cpuid &&
  288. (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
  289. create_shadowed_slbe(get_paca()->kstack,
  290. mmu_kernel_ssize, lflags, KSTACK_INDEX);
  291. asm volatile("isync":::"memory");
  292. }
  293. static void insert_slb_entry(unsigned long vsid, unsigned long ea,
  294. int bpsize, int ssize)
  295. {
  296. unsigned long flags, vsid_data, esid_data;
  297. enum slb_index index;
  298. int slb_cache_index;
  299. /*
  300. * We are irq disabled, hence should be safe to access PACA.
  301. */
  302. VM_WARN_ON(!irqs_disabled());
  303. /*
  304. * We can't take a PMU exception in the following code, so hard
  305. * disable interrupts.
  306. */
  307. hard_irq_disable();
  308. index = get_paca()->stab_rr;
  309. /*
  310. * simple round-robin replacement of slb starting at SLB_NUM_BOLTED.
  311. */
  312. if (index < (mmu_slb_size - 1))
  313. index++;
  314. else
  315. index = SLB_NUM_BOLTED;
  316. get_paca()->stab_rr = index;
  317. flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
  318. vsid_data = (vsid << slb_vsid_shift(ssize)) | flags |
  319. ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
  320. esid_data = mk_esid_data(ea, ssize, index);
  321. /*
  322. * No need for an isync before or after this slbmte. The exception
  323. * we enter with and the rfid we exit with are context synchronizing.
  324. * Also we only handle user segments here.
  325. */
  326. asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data)
  327. : "memory");
  328. /*
  329. * Now update slb cache entries
  330. */
  331. slb_cache_index = get_paca()->slb_cache_ptr;
  332. if (slb_cache_index < SLB_CACHE_ENTRIES) {
  333. /*
  334. * We have space in slb cache for optimized switch_slb().
  335. * Top 36 bits from esid_data as per ISA
  336. */
  337. get_paca()->slb_cache[slb_cache_index++] = esid_data >> 28;
  338. get_paca()->slb_cache_ptr++;
  339. } else {
  340. /*
  341. * Our cache is full and the current cache content strictly
  342. * doesn't indicate the active SLB conents. Bump the ptr
  343. * so that switch_slb() will ignore the cache.
  344. */
  345. get_paca()->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
  346. }
  347. }
  348. static void handle_multi_context_slb_miss(int context_id, unsigned long ea)
  349. {
  350. struct mm_struct *mm = current->mm;
  351. unsigned long vsid;
  352. int bpsize;
  353. /*
  354. * We are always above 1TB, hence use high user segment size.
  355. */
  356. vsid = get_vsid(context_id, ea, mmu_highuser_ssize);
  357. bpsize = get_slice_psize(mm, ea);
  358. insert_slb_entry(vsid, ea, bpsize, mmu_highuser_ssize);
  359. }
  360. void slb_miss_large_addr(struct pt_regs *regs)
  361. {
  362. enum ctx_state prev_state = exception_enter();
  363. unsigned long ea = regs->dar;
  364. int context;
  365. if (REGION_ID(ea) != USER_REGION_ID)
  366. goto slb_bad_addr;
  367. /*
  368. * Are we beyound what the page table layout supports ?
  369. */
  370. if ((ea & ~REGION_MASK) >= H_PGTABLE_RANGE)
  371. goto slb_bad_addr;
  372. /* Lower address should have been handled by asm code */
  373. if (ea < (1UL << MAX_EA_BITS_PER_CONTEXT))
  374. goto slb_bad_addr;
  375. /*
  376. * consider this as bad access if we take a SLB miss
  377. * on an address above addr limit.
  378. */
  379. if (ea >= current->mm->context.slb_addr_limit)
  380. goto slb_bad_addr;
  381. context = get_ea_context(&current->mm->context, ea);
  382. if (!context)
  383. goto slb_bad_addr;
  384. handle_multi_context_slb_miss(context, ea);
  385. exception_exit(prev_state);
  386. return;
  387. slb_bad_addr:
  388. if (user_mode(regs))
  389. _exception(SIGSEGV, regs, SEGV_BNDERR, ea);
  390. else
  391. bad_page_fault(regs, ea, SIGSEGV);
  392. exception_exit(prev_state);
  393. }