2 * PowerPC64 SLB support.
5 * Based on earlier code written by:
6 * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
7 * Copyright (c) 2001 Dave Engebretsen
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.
17 #include <asm/asm-prototypes.h>
18 #include <asm/pgtable.h>
20 #include <asm/mmu_context.h>
22 #include <asm/cputable.h>
23 #include <asm/cacheflush.h>
25 #include <linux/compiler.h>
26 #include <linux/context_tracking.h>
27 #include <linux/mm_types.h>
30 #include <asm/code-patching.h>
33 LINEAR_INDEX = 0, /* Kernel linear map (0xc000000000000000) */
34 KSTACK_INDEX = 1, /* Kernel stack map */
37 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea);
39 #define slb_esid_mask(ssize) \
40 (((ssize) == MMU_SEGSIZE_256M)? ESID_MASK: ESID_MASK_1T)
42 static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
45 return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
48 static inline unsigned long __mk_vsid_data(unsigned long vsid, int ssize,
51 return (vsid << slb_vsid_shift(ssize)) | flags |
52 ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
55 static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
58 return __mk_vsid_data(get_kernel_vsid(ea, ssize), ssize, flags);
61 static void assert_slb_exists(unsigned long ea)
63 #ifdef CONFIG_DEBUG_VM
66 WARN_ON_ONCE(mfmsr() & MSR_EE);
68 asm volatile("slbfee. %0, %1" : "=r"(tmp) : "r"(ea) : "cr0");
73 static void assert_slb_notexists(unsigned long ea)
75 #ifdef CONFIG_DEBUG_VM
78 WARN_ON_ONCE(mfmsr() & MSR_EE);
80 asm volatile("slbfee. %0, %1" : "=r"(tmp) : "r"(ea) : "cr0");
85 static inline void slb_shadow_update(unsigned long ea, int ssize,
89 struct slb_shadow *p = get_slb_shadow();
92 * Clear the ESID first so the entry is not valid while we are
93 * updating it. No write barriers are needed here, provided
94 * we only update the current CPU's SLB shadow buffer.
96 WRITE_ONCE(p->save_area[index].esid, 0);
97 WRITE_ONCE(p->save_area[index].vsid, cpu_to_be64(mk_vsid_data(ea, ssize, flags)));
98 WRITE_ONCE(p->save_area[index].esid, cpu_to_be64(mk_esid_data(ea, ssize, index)));
101 static inline void slb_shadow_clear(enum slb_index index)
103 WRITE_ONCE(get_slb_shadow()->save_area[index].esid, cpu_to_be64(index));
106 static inline void create_shadowed_slbe(unsigned long ea, int ssize,
108 enum slb_index index)
111 * Updating the shadow buffer before writing the SLB ensures
112 * we don't get a stale entry here if we get preempted by PHYP
113 * between these two statements.
115 slb_shadow_update(ea, ssize, flags, index);
117 assert_slb_notexists(ea);
118 asm volatile("slbmte %0,%1" :
119 : "r" (mk_vsid_data(ea, ssize, flags)),
120 "r" (mk_esid_data(ea, ssize, index))
125 * Insert bolted entries into SLB (which may not be empty, so don't clear
128 void __slb_restore_bolted_realmode(void)
130 struct slb_shadow *p = get_slb_shadow();
131 enum slb_index index;
133 /* No isync needed because realmode. */
134 for (index = 0; index < SLB_NUM_BOLTED; index++) {
135 asm volatile("slbmte %0,%1" :
136 : "r" (be64_to_cpu(p->save_area[index].vsid)),
137 "r" (be64_to_cpu(p->save_area[index].esid)));
140 assert_slb_exists(local_paca->kstack);
144 * Insert the bolted entries into an empty SLB.
146 void slb_restore_bolted_realmode(void)
148 __slb_restore_bolted_realmode();
149 get_paca()->slb_cache_ptr = 0;
151 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
152 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
156 * This flushes all SLB entries including 0, so it must be realmode.
158 void slb_flush_all_realmode(void)
160 asm volatile("slbmte %0,%0; slbia" : : "r" (0));
164 * This flushes non-bolted entries, it can be run in virtual mode. Must
165 * be called with interrupts disabled.
167 void slb_flush_and_restore_bolted(void)
169 struct slb_shadow *p = get_slb_shadow();
171 BUILD_BUG_ON(SLB_NUM_BOLTED != 2);
173 WARN_ON(!irqs_disabled());
176 * We can't take a PMU exception in the following code, so hard
177 * disable interrupts.
181 asm volatile("isync\n"
185 :: "r" (be64_to_cpu(p->save_area[KSTACK_INDEX].vsid)),
186 "r" (be64_to_cpu(p->save_area[KSTACK_INDEX].esid))
188 assert_slb_exists(get_paca()->kstack);
190 get_paca()->slb_cache_ptr = 0;
192 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
193 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
196 void slb_save_contents(struct slb_entry *slb_ptr)
201 /* Save slb_cache_ptr value. */
202 get_paca()->slb_save_cache_ptr = get_paca()->slb_cache_ptr;
207 for (i = 0; i < mmu_slb_size; i++) {
208 asm volatile("slbmfee %0,%1" : "=r" (e) : "r" (i));
209 asm volatile("slbmfev %0,%1" : "=r" (v) : "r" (i));
216 void slb_dump_contents(struct slb_entry *slb_ptr)
225 pr_err("SLB contents of cpu 0x%x\n", smp_processor_id());
226 pr_err("Last SLB entry inserted at slot %d\n", get_paca()->stab_rr);
228 for (i = 0; i < mmu_slb_size; i++) {
236 pr_err("%02d %016lx %016lx\n", i, e, v);
238 if (!(e & SLB_ESID_V)) {
242 llp = v & SLB_VSID_LLP;
243 if (v & SLB_VSID_B_1T) {
244 pr_err(" 1T ESID=%9lx VSID=%13lx LLP:%3lx\n",
246 (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T, llp);
248 pr_err(" 256M ESID=%9lx VSID=%13lx LLP:%3lx\n",
250 (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT, llp);
253 pr_err("----------------------------------\n");
255 /* Dump slb cache entires as well. */
256 pr_err("SLB cache ptr value = %d\n", get_paca()->slb_save_cache_ptr);
257 pr_err("Valid SLB cache entries:\n");
258 n = min_t(int, get_paca()->slb_save_cache_ptr, SLB_CACHE_ENTRIES);
259 for (i = 0; i < n; i++)
260 pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
261 pr_err("Rest of SLB cache entries:\n");
262 for (i = n; i < SLB_CACHE_ENTRIES; i++)
263 pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
266 void slb_vmalloc_update(void)
269 * vmalloc is not bolted, so just have to flush non-bolted.
271 slb_flush_and_restore_bolted();
274 static bool preload_hit(struct thread_info *ti, unsigned long esid)
278 for (i = 0; i < ti->slb_preload_nr; i++) {
281 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
282 if (esid == ti->slb_preload_esid[idx])
288 static bool preload_add(struct thread_info *ti, unsigned long ea)
293 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
294 /* EAs are stored >> 28 so 256MB segments don't need clearing */
295 if (ea & ESID_MASK_1T)
299 esid = ea >> SID_SHIFT;
301 if (preload_hit(ti, esid))
304 idx = (ti->slb_preload_tail + ti->slb_preload_nr) % SLB_PRELOAD_NR;
305 ti->slb_preload_esid[idx] = esid;
306 if (ti->slb_preload_nr == SLB_PRELOAD_NR)
307 ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
309 ti->slb_preload_nr++;
314 static void preload_age(struct thread_info *ti)
316 if (!ti->slb_preload_nr)
318 ti->slb_preload_nr--;
319 ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
322 void slb_setup_new_exec(void)
324 struct thread_info *ti = current_thread_info();
325 struct mm_struct *mm = current->mm;
326 unsigned long exec = 0x10000000;
328 WARN_ON(irqs_disabled());
331 * preload cache can only be used to determine whether a SLB
332 * entry exists if it does not start to overflow.
334 if (ti->slb_preload_nr + 2 > SLB_PRELOAD_NR)
340 * We have no good place to clear the slb preload cache on exec,
341 * flush_thread is about the earliest arch hook but that happens
342 * after we switch to the mm and have aleady preloaded the SLBEs.
344 * For the most part that's probably okay to use entries from the
345 * previous exec, they will age out if unused. It may turn out to
346 * be an advantage to clear the cache before switching to it,
351 * preload some userspace segments into the SLB.
352 * Almost all 32 and 64bit PowerPC executables are linked at
353 * 0x10000000 so it makes sense to preload this segment.
355 if (!is_kernel_addr(exec)) {
356 if (preload_add(ti, exec))
357 slb_allocate_user(mm, exec);
360 /* Libraries and mmaps. */
361 if (!is_kernel_addr(mm->mmap_base)) {
362 if (preload_add(ti, mm->mmap_base))
363 slb_allocate_user(mm, mm->mmap_base);
367 asm volatile("isync" : : : "memory");
372 void preload_new_slb_context(unsigned long start, unsigned long sp)
374 struct thread_info *ti = current_thread_info();
375 struct mm_struct *mm = current->mm;
376 unsigned long heap = mm->start_brk;
378 WARN_ON(irqs_disabled());
381 if (ti->slb_preload_nr + 3 > SLB_PRELOAD_NR)
386 /* Userspace entry address. */
387 if (!is_kernel_addr(start)) {
388 if (preload_add(ti, start))
389 slb_allocate_user(mm, start);
392 /* Top of stack, grows down. */
393 if (!is_kernel_addr(sp)) {
394 if (preload_add(ti, sp))
395 slb_allocate_user(mm, sp);
398 /* Bottom of heap, grows up. */
399 if (heap && !is_kernel_addr(heap)) {
400 if (preload_add(ti, heap))
401 slb_allocate_user(mm, heap);
405 asm volatile("isync" : : : "memory");
411 /* Flush all user entries from the segment table of the current processor. */
412 void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
414 struct thread_info *ti = task_thread_info(tsk);
418 * We need interrupts hard-disabled here, not just soft-disabled,
419 * so that a PMU interrupt can't occur, which might try to access
420 * user memory (to get a stack trace) and possible cause an SLB miss
421 * which would update the slb_cache/slb_cache_ptr fields in the PACA.
424 asm volatile("isync" : : : "memory");
425 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
427 * SLBIA IH=3 invalidates all Class=1 SLBEs and their
428 * associated lookaside structures, which matches what
429 * switch_slb wants. So ARCH_300 does not use the slb
432 asm volatile(PPC_SLBIA(3));
434 unsigned long offset = get_paca()->slb_cache_ptr;
436 if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
437 offset <= SLB_CACHE_ENTRIES) {
438 unsigned long slbie_data = 0;
440 for (i = 0; i < offset; i++) {
444 get_paca()->slb_cache[i] << SID_SHIFT;
446 * Could assert_slb_exists here, but hypervisor
447 * or machine check could have come in and
448 * removed the entry at this point.
452 slbie_data |= user_segment_size(slbie_data)
453 << SLBIE_SSIZE_SHIFT;
454 slbie_data |= SLBIE_C; /* user slbs have C=1 */
455 asm volatile("slbie %0" : : "r" (slbie_data));
458 /* Workaround POWER5 < DD2.1 issue */
459 if (!cpu_has_feature(CPU_FTR_ARCH_207S) && offset == 1)
460 asm volatile("slbie %0" : : "r" (slbie_data));
463 struct slb_shadow *p = get_slb_shadow();
464 unsigned long ksp_esid_data =
465 be64_to_cpu(p->save_area[KSTACK_INDEX].esid);
466 unsigned long ksp_vsid_data =
467 be64_to_cpu(p->save_area[KSTACK_INDEX].vsid);
469 asm volatile(PPC_SLBIA(1) "\n"
472 :: "r"(ksp_vsid_data),
475 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
478 get_paca()->slb_cache_ptr = 0;
480 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
485 * We gradually age out SLBs after a number of context switches to
486 * reduce reload overhead of unused entries (like we do with FP/VEC
487 * reload). Each time we wrap 256 switches, take an entry out of the
490 tsk->thread.load_slb++;
491 if (!tsk->thread.load_slb) {
492 unsigned long pc = KSTK_EIP(tsk);
498 for (i = 0; i < ti->slb_preload_nr; i++) {
502 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
503 ea = (unsigned long)ti->slb_preload_esid[idx] << SID_SHIFT;
505 slb_allocate_user(mm, ea);
509 * Synchronize slbmte preloads with possible subsequent user memory
510 * address accesses by the kernel (user mode won't happen until
511 * rfid, which is safe).
513 asm volatile("isync" : : : "memory");
516 void slb_set_size(u16 size)
521 void slb_initialize(void)
523 unsigned long linear_llp, vmalloc_llp, io_llp;
524 unsigned long lflags;
525 static int slb_encoding_inited;
526 #ifdef CONFIG_SPARSEMEM_VMEMMAP
527 unsigned long vmemmap_llp;
530 /* Prepare our SLB miss handler based on our page size */
531 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
532 io_llp = mmu_psize_defs[mmu_io_psize].sllp;
533 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
534 get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
535 #ifdef CONFIG_SPARSEMEM_VMEMMAP
536 vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
538 if (!slb_encoding_inited) {
539 slb_encoding_inited = 1;
540 pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
541 pr_devel("SLB: io LLP = %04lx\n", io_llp);
542 #ifdef CONFIG_SPARSEMEM_VMEMMAP
543 pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
547 get_paca()->stab_rr = SLB_NUM_BOLTED - 1;
548 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
549 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
551 lflags = SLB_VSID_KERNEL | linear_llp;
553 /* Invalidate the entire SLB (even entry 0) & all the ERATS */
554 asm volatile("isync":::"memory");
555 asm volatile("slbmte %0,%0"::"r" (0) : "memory");
556 asm volatile("isync; slbia; isync":::"memory");
557 create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
559 /* For the boot cpu, we're running on the stack in init_thread_union,
560 * which is in the first segment of the linear mapping, and also
561 * get_paca()->kstack hasn't been initialized yet.
562 * For secondary cpus, we need to bolt the kernel stack entry now.
564 slb_shadow_clear(KSTACK_INDEX);
565 if (raw_smp_processor_id() != boot_cpuid &&
566 (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
567 create_shadowed_slbe(get_paca()->kstack,
568 mmu_kernel_ssize, lflags, KSTACK_INDEX);
570 asm volatile("isync":::"memory");
573 static void slb_cache_update(unsigned long esid_data)
577 if (cpu_has_feature(CPU_FTR_ARCH_300))
578 return; /* ISAv3.0B and later does not use slb_cache */
581 * Now update slb cache entries
583 slb_cache_index = local_paca->slb_cache_ptr;
584 if (slb_cache_index < SLB_CACHE_ENTRIES) {
586 * We have space in slb cache for optimized switch_slb().
587 * Top 36 bits from esid_data as per ISA
589 local_paca->slb_cache[slb_cache_index++] = esid_data >> 28;
590 local_paca->slb_cache_ptr++;
593 * Our cache is full and the current cache content strictly
594 * doesn't indicate the active SLB conents. Bump the ptr
595 * so that switch_slb() will ignore the cache.
597 local_paca->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
601 static enum slb_index alloc_slb_index(bool kernel)
603 enum slb_index index;
606 * The allocation bitmaps can become out of synch with the SLB
607 * when the _switch code does slbie when bolting a new stack
608 * segment and it must not be anywhere else in the SLB. This leaves
609 * a kernel allocated entry that is unused in the SLB. With very
610 * large systems or small segment sizes, the bitmaps could slowly
611 * fill with these entries. They will eventually be cleared out
612 * by the round robin allocator in that case, so it's probably not
613 * worth accounting for.
617 * SLBs beyond 32 entries are allocated with stab_rr only
618 * POWER7/8/9 have 32 SLB entries, this could be expanded if a
619 * future CPU has more.
621 if (local_paca->slb_used_bitmap != U32_MAX) {
622 index = ffz(local_paca->slb_used_bitmap);
623 local_paca->slb_used_bitmap |= 1U << index;
625 local_paca->slb_kern_bitmap |= 1U << index;
627 /* round-robin replacement of slb starting at SLB_NUM_BOLTED. */
628 index = local_paca->stab_rr;
629 if (index < (mmu_slb_size - 1))
632 index = SLB_NUM_BOLTED;
633 local_paca->stab_rr = index;
636 local_paca->slb_kern_bitmap |= 1U << index;
638 local_paca->slb_kern_bitmap &= ~(1U << index);
641 BUG_ON(index < SLB_NUM_BOLTED);
646 static long slb_insert_entry(unsigned long ea, unsigned long context,
647 unsigned long flags, int ssize, bool kernel)
650 unsigned long vsid_data, esid_data;
651 enum slb_index index;
653 vsid = get_vsid(context, ea, ssize);
658 * There must not be a kernel SLB fault in alloc_slb_index or before
659 * slbmte here or the allocation bitmaps could get out of whack with
662 * User SLB faults or preloads take this path which might get inlined
663 * into the caller, so add compiler barriers here to ensure unsafe
664 * memory accesses do not come between.
668 index = alloc_slb_index(kernel);
670 vsid_data = __mk_vsid_data(vsid, ssize, flags);
671 esid_data = mk_esid_data(ea, ssize, index);
674 * No need for an isync before or after this slbmte. The exception
675 * we enter with and the rfid we exit with are context synchronizing.
676 * User preloads should add isync afterwards in case the kernel
677 * accesses user memory before it returns to userspace with rfid.
679 assert_slb_notexists(ea);
680 asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data));
685 slb_cache_update(esid_data);
690 static long slb_allocate_kernel(unsigned long ea, unsigned long id)
692 unsigned long context;
696 if (id == KERNEL_REGION_ID) {
698 /* We only support upto MAX_PHYSMEM_BITS */
699 if ((ea & ~REGION_MASK) > (1UL << MAX_PHYSMEM_BITS))
702 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp;
704 #ifdef CONFIG_SPARSEMEM_VMEMMAP
705 } else if (id == VMEMMAP_REGION_ID) {
707 if ((ea & ~REGION_MASK) >= (1ULL << MAX_EA_BITS_PER_CONTEXT))
710 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmemmap_psize].sllp;
712 } else if (id == VMALLOC_REGION_ID) {
714 if ((ea & ~REGION_MASK) >= (1ULL << MAX_EA_BITS_PER_CONTEXT))
717 if (ea < H_VMALLOC_END)
718 flags = get_paca()->vmalloc_sllp;
720 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_io_psize].sllp;
725 ssize = MMU_SEGSIZE_1T;
726 if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
727 ssize = MMU_SEGSIZE_256M;
729 context = get_kernel_context(ea);
730 return slb_insert_entry(ea, context, flags, ssize, true);
733 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea)
735 unsigned long context;
741 * consider this as bad access if we take a SLB miss
742 * on an address above addr limit.
744 if (ea >= mm->context.slb_addr_limit)
747 context = get_user_context(&mm->context, ea);
751 if (unlikely(ea >= H_PGTABLE_RANGE)) {
756 ssize = user_segment_size(ea);
758 bpsize = get_slice_psize(mm, ea);
759 flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
761 return slb_insert_entry(ea, context, flags, ssize, false);
764 long do_slb_fault(struct pt_regs *regs, unsigned long ea)
766 unsigned long id = REGION_ID(ea);
768 /* IRQs are not reconciled here, so can't check irqs_disabled */
769 VM_WARN_ON(mfmsr() & MSR_EE);
771 if (unlikely(!(regs->msr & MSR_RI)))
775 * SLB kernel faults must be very careful not to touch anything
776 * that is not bolted. E.g., PACA and global variables are okay,
777 * mm->context stuff is not.
779 * SLB user faults can access all of kernel memory, but must be
780 * careful not to touch things like IRQ state because it is not
781 * "reconciled" here. The difficulty is that we must use
782 * fast_exception_return to return from kernel SLB faults without
783 * looking at possible non-bolted memory. We could test user vs
784 * kernel faults in the interrupt handler asm and do a full fault,
785 * reconcile, ret_from_except for user faults which would make them
786 * first class kernel code. But for performance it's probably nicer
787 * if they go via fast_exception_return too.
789 if (id >= KERNEL_REGION_ID) {
791 #ifdef CONFIG_DEBUG_VM
792 /* Catch recursive kernel SLB faults. */
793 BUG_ON(local_paca->in_kernel_slb_handler);
794 local_paca->in_kernel_slb_handler = 1;
796 err = slb_allocate_kernel(ea, id);
797 #ifdef CONFIG_DEBUG_VM
798 local_paca->in_kernel_slb_handler = 0;
802 struct mm_struct *mm = current->mm;
808 err = slb_allocate_user(mm, ea);
810 preload_add(current_thread_info(), ea);
816 void do_bad_slb_fault(struct pt_regs *regs, unsigned long ea, long err)
818 if (err == -EFAULT) {
820 _exception(SIGSEGV, regs, SEGV_BNDERR, ea);
822 bad_page_fault(regs, ea, SIGSEGV);
823 } else if (err == -EINVAL) {
824 unrecoverable_exception(regs);