Merge tag 'linux-kselftest-kunit-fixes-5.11-rc3' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / arch / powerpc / mm / book3s64 / slb.c
blob584567970c116e3e82ede9b4f08edc008eb3283e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PowerPC64 SLB support.
5 * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, 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 <anton@au.ibm.com>, IBM
12 #include <asm/asm-prototypes.h>
13 #include <asm/mmu.h>
14 #include <asm/mmu_context.h>
15 #include <asm/paca.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>
25 #include <asm/udbg.h>
26 #include <asm/code-patching.h>
28 #include "internal.h"
31 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea);
33 bool stress_slb_enabled __initdata;
35 static int __init parse_stress_slb(char *p)
37 stress_slb_enabled = true;
38 return 0;
40 early_param("stress_slb", parse_stress_slb);
42 __ro_after_init DEFINE_STATIC_KEY_FALSE(stress_slb_key);
44 static void assert_slb_presence(bool present, unsigned long ea)
46 #ifdef CONFIG_DEBUG_VM
47 unsigned long tmp;
49 WARN_ON_ONCE(mfmsr() & MSR_EE);
51 if (!cpu_has_feature(CPU_FTR_ARCH_206))
52 return;
55 * slbfee. requires bit 24 (PPC bit 39) be clear in RB. Hardware
56 * ignores all other bits from 0-27, so just clear them all.
58 ea &= ~((1UL << SID_SHIFT) - 1);
59 asm volatile(__PPC_SLBFEE_DOT(%0, %1) : "=r"(tmp) : "r"(ea) : "cr0");
61 WARN_ON(present == (tmp == 0));
62 #endif
65 static inline void slb_shadow_update(unsigned long ea, int ssize,
66 unsigned long flags,
67 enum slb_index index)
69 struct slb_shadow *p = get_slb_shadow();
72 * Clear the ESID first so the entry is not valid while we are
73 * updating it. No write barriers are needed here, provided
74 * we only update the current CPU's SLB shadow buffer.
76 WRITE_ONCE(p->save_area[index].esid, 0);
77 WRITE_ONCE(p->save_area[index].vsid, cpu_to_be64(mk_vsid_data(ea, ssize, flags)));
78 WRITE_ONCE(p->save_area[index].esid, cpu_to_be64(mk_esid_data(ea, ssize, index)));
81 static inline void slb_shadow_clear(enum slb_index index)
83 WRITE_ONCE(get_slb_shadow()->save_area[index].esid, cpu_to_be64(index));
86 static inline void create_shadowed_slbe(unsigned long ea, int ssize,
87 unsigned long flags,
88 enum slb_index index)
91 * Updating the shadow buffer before writing the SLB ensures
92 * we don't get a stale entry here if we get preempted by PHYP
93 * between these two statements.
95 slb_shadow_update(ea, ssize, flags, index);
97 assert_slb_presence(false, ea);
98 asm volatile("slbmte %0,%1" :
99 : "r" (mk_vsid_data(ea, ssize, flags)),
100 "r" (mk_esid_data(ea, ssize, index))
101 : "memory" );
105 * Insert bolted entries into SLB (which may not be empty, so don't clear
106 * slb_cache_ptr).
108 void __slb_restore_bolted_realmode(void)
110 struct slb_shadow *p = get_slb_shadow();
111 enum slb_index index;
113 /* No isync needed because realmode. */
114 for (index = 0; index < SLB_NUM_BOLTED; index++) {
115 asm volatile("slbmte %0,%1" :
116 : "r" (be64_to_cpu(p->save_area[index].vsid)),
117 "r" (be64_to_cpu(p->save_area[index].esid)));
120 assert_slb_presence(true, local_paca->kstack);
124 * Insert the bolted entries into an empty SLB.
126 void slb_restore_bolted_realmode(void)
128 __slb_restore_bolted_realmode();
129 get_paca()->slb_cache_ptr = 0;
131 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
132 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
136 * This flushes all SLB entries including 0, so it must be realmode.
138 void slb_flush_all_realmode(void)
140 asm volatile("slbmte %0,%0; slbia" : : "r" (0));
143 static __always_inline void __slb_flush_and_restore_bolted(bool preserve_kernel_lookaside)
145 struct slb_shadow *p = get_slb_shadow();
146 unsigned long ksp_esid_data, ksp_vsid_data;
147 u32 ih;
150 * SLBIA IH=1 on ISA v2.05 and newer processors may preserve lookaside
151 * information created with Class=0 entries, which we use for kernel
152 * SLB entries (the SLB entries themselves are still invalidated).
154 * Older processors will ignore this optimisation. Over-invalidation
155 * is fine because we never rely on lookaside information existing.
157 if (preserve_kernel_lookaside)
158 ih = 1;
159 else
160 ih = 0;
162 ksp_esid_data = be64_to_cpu(p->save_area[KSTACK_INDEX].esid);
163 ksp_vsid_data = be64_to_cpu(p->save_area[KSTACK_INDEX].vsid);
165 asm volatile(PPC_SLBIA(%0)" \n"
166 "slbmte %1, %2 \n"
167 :: "i" (ih),
168 "r" (ksp_vsid_data),
169 "r" (ksp_esid_data)
170 : "memory");
174 * This flushes non-bolted entries, it can be run in virtual mode. Must
175 * be called with interrupts disabled.
177 void slb_flush_and_restore_bolted(void)
179 BUILD_BUG_ON(SLB_NUM_BOLTED != 2);
181 WARN_ON(!irqs_disabled());
184 * We can't take a PMU exception in the following code, so hard
185 * disable interrupts.
187 hard_irq_disable();
189 isync();
190 __slb_flush_and_restore_bolted(false);
191 isync();
193 assert_slb_presence(true, get_paca()->kstack);
195 get_paca()->slb_cache_ptr = 0;
197 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
198 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
201 void slb_save_contents(struct slb_entry *slb_ptr)
203 int i;
204 unsigned long e, v;
206 /* Save slb_cache_ptr value. */
207 get_paca()->slb_save_cache_ptr = get_paca()->slb_cache_ptr;
209 if (!slb_ptr)
210 return;
212 for (i = 0; i < mmu_slb_size; i++) {
213 asm volatile("slbmfee %0,%1" : "=r" (e) : "r" (i));
214 asm volatile("slbmfev %0,%1" : "=r" (v) : "r" (i));
215 slb_ptr->esid = e;
216 slb_ptr->vsid = v;
217 slb_ptr++;
221 void slb_dump_contents(struct slb_entry *slb_ptr)
223 int i, n;
224 unsigned long e, v;
225 unsigned long llp;
227 if (!slb_ptr)
228 return;
230 pr_err("SLB contents of cpu 0x%x\n", smp_processor_id());
232 for (i = 0; i < mmu_slb_size; i++) {
233 e = slb_ptr->esid;
234 v = slb_ptr->vsid;
235 slb_ptr++;
237 if (!e && !v)
238 continue;
240 pr_err("%02d %016lx %016lx %s\n", i, e, v,
241 (e & SLB_ESID_V) ? "VALID" : "NOT VALID");
243 if (!(e & SLB_ESID_V))
244 continue;
246 llp = v & SLB_VSID_LLP;
247 if (v & SLB_VSID_B_1T) {
248 pr_err(" 1T ESID=%9lx VSID=%13lx LLP:%3lx\n",
249 GET_ESID_1T(e),
250 (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T, llp);
251 } else {
252 pr_err(" 256M ESID=%9lx VSID=%13lx LLP:%3lx\n",
253 GET_ESID(e),
254 (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT, llp);
258 if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
259 /* RR is not so useful as it's often not used for allocation */
260 pr_err("SLB RR allocator index %d\n", get_paca()->stab_rr);
262 /* Dump slb cache entires as well. */
263 pr_err("SLB cache ptr value = %d\n", get_paca()->slb_save_cache_ptr);
264 pr_err("Valid SLB cache entries:\n");
265 n = min_t(int, get_paca()->slb_save_cache_ptr, SLB_CACHE_ENTRIES);
266 for (i = 0; i < n; i++)
267 pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
268 pr_err("Rest of SLB cache entries:\n");
269 for (i = n; i < SLB_CACHE_ENTRIES; i++)
270 pr_err("%02d EA[0-35]=%9x\n", i, get_paca()->slb_cache[i]);
274 void slb_vmalloc_update(void)
277 * vmalloc is not bolted, so just have to flush non-bolted.
279 slb_flush_and_restore_bolted();
282 static bool preload_hit(struct thread_info *ti, unsigned long esid)
284 unsigned char i;
286 for (i = 0; i < ti->slb_preload_nr; i++) {
287 unsigned char idx;
289 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
290 if (esid == ti->slb_preload_esid[idx])
291 return true;
293 return false;
296 static bool preload_add(struct thread_info *ti, unsigned long ea)
298 unsigned char idx;
299 unsigned long esid;
301 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
302 /* EAs are stored >> 28 so 256MB segments don't need clearing */
303 if (ea & ESID_MASK_1T)
304 ea &= ESID_MASK_1T;
307 esid = ea >> SID_SHIFT;
309 if (preload_hit(ti, esid))
310 return false;
312 idx = (ti->slb_preload_tail + ti->slb_preload_nr) % SLB_PRELOAD_NR;
313 ti->slb_preload_esid[idx] = esid;
314 if (ti->slb_preload_nr == SLB_PRELOAD_NR)
315 ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
316 else
317 ti->slb_preload_nr++;
319 return true;
322 static void preload_age(struct thread_info *ti)
324 if (!ti->slb_preload_nr)
325 return;
326 ti->slb_preload_nr--;
327 ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
330 void slb_setup_new_exec(void)
332 struct thread_info *ti = current_thread_info();
333 struct mm_struct *mm = current->mm;
334 unsigned long exec = 0x10000000;
336 WARN_ON(irqs_disabled());
339 * preload cache can only be used to determine whether a SLB
340 * entry exists if it does not start to overflow.
342 if (ti->slb_preload_nr + 2 > SLB_PRELOAD_NR)
343 return;
345 hard_irq_disable();
348 * We have no good place to clear the slb preload cache on exec,
349 * flush_thread is about the earliest arch hook but that happens
350 * after we switch to the mm and have aleady preloaded the SLBEs.
352 * For the most part that's probably okay to use entries from the
353 * previous exec, they will age out if unused. It may turn out to
354 * be an advantage to clear the cache before switching to it,
355 * however.
359 * preload some userspace segments into the SLB.
360 * Almost all 32 and 64bit PowerPC executables are linked at
361 * 0x10000000 so it makes sense to preload this segment.
363 if (!is_kernel_addr(exec)) {
364 if (preload_add(ti, exec))
365 slb_allocate_user(mm, exec);
368 /* Libraries and mmaps. */
369 if (!is_kernel_addr(mm->mmap_base)) {
370 if (preload_add(ti, mm->mmap_base))
371 slb_allocate_user(mm, mm->mmap_base);
374 /* see switch_slb */
375 asm volatile("isync" : : : "memory");
377 local_irq_enable();
380 void preload_new_slb_context(unsigned long start, unsigned long sp)
382 struct thread_info *ti = current_thread_info();
383 struct mm_struct *mm = current->mm;
384 unsigned long heap = mm->start_brk;
386 WARN_ON(irqs_disabled());
388 /* see above */
389 if (ti->slb_preload_nr + 3 > SLB_PRELOAD_NR)
390 return;
392 hard_irq_disable();
394 /* Userspace entry address. */
395 if (!is_kernel_addr(start)) {
396 if (preload_add(ti, start))
397 slb_allocate_user(mm, start);
400 /* Top of stack, grows down. */
401 if (!is_kernel_addr(sp)) {
402 if (preload_add(ti, sp))
403 slb_allocate_user(mm, sp);
406 /* Bottom of heap, grows up. */
407 if (heap && !is_kernel_addr(heap)) {
408 if (preload_add(ti, heap))
409 slb_allocate_user(mm, heap);
412 /* see switch_slb */
413 asm volatile("isync" : : : "memory");
415 local_irq_enable();
418 static void slb_cache_slbie_kernel(unsigned int index)
420 unsigned long slbie_data = get_paca()->slb_cache[index];
421 unsigned long ksp = get_paca()->kstack;
423 slbie_data <<= SID_SHIFT;
424 slbie_data |= 0xc000000000000000ULL;
425 if ((ksp & slb_esid_mask(mmu_kernel_ssize)) == slbie_data)
426 return;
427 slbie_data |= mmu_kernel_ssize << SLBIE_SSIZE_SHIFT;
429 asm volatile("slbie %0" : : "r" (slbie_data));
432 static void slb_cache_slbie_user(unsigned int index)
434 unsigned long slbie_data = get_paca()->slb_cache[index];
436 slbie_data <<= SID_SHIFT;
437 slbie_data |= user_segment_size(slbie_data) << SLBIE_SSIZE_SHIFT;
438 slbie_data |= SLBIE_C; /* user slbs have C=1 */
440 asm volatile("slbie %0" : : "r" (slbie_data));
443 /* Flush all user entries from the segment table of the current processor. */
444 void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
446 struct thread_info *ti = task_thread_info(tsk);
447 unsigned char i;
450 * We need interrupts hard-disabled here, not just soft-disabled,
451 * so that a PMU interrupt can't occur, which might try to access
452 * user memory (to get a stack trace) and possible cause an SLB miss
453 * which would update the slb_cache/slb_cache_ptr fields in the PACA.
455 hard_irq_disable();
456 isync();
457 if (stress_slb()) {
458 __slb_flush_and_restore_bolted(false);
459 isync();
460 get_paca()->slb_cache_ptr = 0;
461 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
463 } else if (cpu_has_feature(CPU_FTR_ARCH_300)) {
465 * SLBIA IH=3 invalidates all Class=1 SLBEs and their
466 * associated lookaside structures, which matches what
467 * switch_slb wants. So ARCH_300 does not use the slb
468 * cache.
470 asm volatile(PPC_SLBIA(3));
472 } else {
473 unsigned long offset = get_paca()->slb_cache_ptr;
475 if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
476 offset <= SLB_CACHE_ENTRIES) {
478 * Could assert_slb_presence(true) here, but
479 * hypervisor or machine check could have come
480 * in and removed the entry at this point.
483 for (i = 0; i < offset; i++)
484 slb_cache_slbie_user(i);
486 /* Workaround POWER5 < DD2.1 issue */
487 if (!cpu_has_feature(CPU_FTR_ARCH_207S) && offset == 1)
488 slb_cache_slbie_user(0);
490 } else {
491 /* Flush but retain kernel lookaside information */
492 __slb_flush_and_restore_bolted(true);
493 isync();
495 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
498 get_paca()->slb_cache_ptr = 0;
500 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
502 copy_mm_to_paca(mm);
505 * We gradually age out SLBs after a number of context switches to
506 * reduce reload overhead of unused entries (like we do with FP/VEC
507 * reload). Each time we wrap 256 switches, take an entry out of the
508 * SLB preload cache.
510 tsk->thread.load_slb++;
511 if (!tsk->thread.load_slb) {
512 unsigned long pc = KSTK_EIP(tsk);
514 preload_age(ti);
515 preload_add(ti, pc);
518 for (i = 0; i < ti->slb_preload_nr; i++) {
519 unsigned char idx;
520 unsigned long ea;
522 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
523 ea = (unsigned long)ti->slb_preload_esid[idx] << SID_SHIFT;
525 slb_allocate_user(mm, ea);
529 * Synchronize slbmte preloads with possible subsequent user memory
530 * address accesses by the kernel (user mode won't happen until
531 * rfid, which is safe).
533 isync();
536 void slb_set_size(u16 size)
538 mmu_slb_size = size;
541 void slb_initialize(void)
543 unsigned long linear_llp, vmalloc_llp, io_llp;
544 unsigned long lflags;
545 static int slb_encoding_inited;
546 #ifdef CONFIG_SPARSEMEM_VMEMMAP
547 unsigned long vmemmap_llp;
548 #endif
550 /* Prepare our SLB miss handler based on our page size */
551 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
552 io_llp = mmu_psize_defs[mmu_io_psize].sllp;
553 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
554 get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
555 #ifdef CONFIG_SPARSEMEM_VMEMMAP
556 vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
557 #endif
558 if (!slb_encoding_inited) {
559 slb_encoding_inited = 1;
560 pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
561 pr_devel("SLB: io LLP = %04lx\n", io_llp);
562 #ifdef CONFIG_SPARSEMEM_VMEMMAP
563 pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
564 #endif
567 get_paca()->stab_rr = SLB_NUM_BOLTED - 1;
568 get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
569 get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
571 lflags = SLB_VSID_KERNEL | linear_llp;
573 /* Invalidate the entire SLB (even entry 0) & all the ERATS */
574 asm volatile("isync":::"memory");
575 asm volatile("slbmte %0,%0"::"r" (0) : "memory");
576 asm volatile("isync; slbia; isync":::"memory");
577 create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
580 * For the boot cpu, we're running on the stack in init_thread_union,
581 * which is in the first segment of the linear mapping, and also
582 * get_paca()->kstack hasn't been initialized yet.
583 * For secondary cpus, we need to bolt the kernel stack entry now.
585 slb_shadow_clear(KSTACK_INDEX);
586 if (raw_smp_processor_id() != boot_cpuid &&
587 (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
588 create_shadowed_slbe(get_paca()->kstack,
589 mmu_kernel_ssize, lflags, KSTACK_INDEX);
591 asm volatile("isync":::"memory");
594 static void slb_cache_update(unsigned long esid_data)
596 int slb_cache_index;
598 if (cpu_has_feature(CPU_FTR_ARCH_300))
599 return; /* ISAv3.0B and later does not use slb_cache */
601 if (stress_slb())
602 return;
605 * Now update slb cache entries
607 slb_cache_index = local_paca->slb_cache_ptr;
608 if (slb_cache_index < SLB_CACHE_ENTRIES) {
610 * We have space in slb cache for optimized switch_slb().
611 * Top 36 bits from esid_data as per ISA
613 local_paca->slb_cache[slb_cache_index++] = esid_data >> SID_SHIFT;
614 local_paca->slb_cache_ptr++;
615 } else {
617 * Our cache is full and the current cache content strictly
618 * doesn't indicate the active SLB conents. Bump the ptr
619 * so that switch_slb() will ignore the cache.
621 local_paca->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
625 static enum slb_index alloc_slb_index(bool kernel)
627 enum slb_index index;
630 * The allocation bitmaps can become out of synch with the SLB
631 * when the _switch code does slbie when bolting a new stack
632 * segment and it must not be anywhere else in the SLB. This leaves
633 * a kernel allocated entry that is unused in the SLB. With very
634 * large systems or small segment sizes, the bitmaps could slowly
635 * fill with these entries. They will eventually be cleared out
636 * by the round robin allocator in that case, so it's probably not
637 * worth accounting for.
641 * SLBs beyond 32 entries are allocated with stab_rr only
642 * POWER7/8/9 have 32 SLB entries, this could be expanded if a
643 * future CPU has more.
645 if (local_paca->slb_used_bitmap != U32_MAX) {
646 index = ffz(local_paca->slb_used_bitmap);
647 local_paca->slb_used_bitmap |= 1U << index;
648 if (kernel)
649 local_paca->slb_kern_bitmap |= 1U << index;
650 } else {
651 /* round-robin replacement of slb starting at SLB_NUM_BOLTED. */
652 index = local_paca->stab_rr;
653 if (index < (mmu_slb_size - 1))
654 index++;
655 else
656 index = SLB_NUM_BOLTED;
657 local_paca->stab_rr = index;
658 if (index < 32) {
659 if (kernel)
660 local_paca->slb_kern_bitmap |= 1U << index;
661 else
662 local_paca->slb_kern_bitmap &= ~(1U << index);
665 BUG_ON(index < SLB_NUM_BOLTED);
667 return index;
670 static long slb_insert_entry(unsigned long ea, unsigned long context,
671 unsigned long flags, int ssize, bool kernel)
673 unsigned long vsid;
674 unsigned long vsid_data, esid_data;
675 enum slb_index index;
677 vsid = get_vsid(context, ea, ssize);
678 if (!vsid)
679 return -EFAULT;
682 * There must not be a kernel SLB fault in alloc_slb_index or before
683 * slbmte here or the allocation bitmaps could get out of whack with
684 * the SLB.
686 * User SLB faults or preloads take this path which might get inlined
687 * into the caller, so add compiler barriers here to ensure unsafe
688 * memory accesses do not come between.
690 barrier();
692 index = alloc_slb_index(kernel);
694 vsid_data = __mk_vsid_data(vsid, ssize, flags);
695 esid_data = mk_esid_data(ea, ssize, index);
698 * No need for an isync before or after this slbmte. The exception
699 * we enter with and the rfid we exit with are context synchronizing.
700 * User preloads should add isync afterwards in case the kernel
701 * accesses user memory before it returns to userspace with rfid.
703 assert_slb_presence(false, ea);
704 if (stress_slb()) {
705 int slb_cache_index = local_paca->slb_cache_ptr;
708 * stress_slb() does not use slb cache, repurpose as a
709 * cache of inserted (non-bolted) kernel SLB entries. All
710 * non-bolted kernel entries are flushed on any user fault,
711 * or if there are already 3 non-boled kernel entries.
713 BUILD_BUG_ON(SLB_CACHE_ENTRIES < 3);
714 if (!kernel || slb_cache_index == 3) {
715 int i;
717 for (i = 0; i < slb_cache_index; i++)
718 slb_cache_slbie_kernel(i);
719 slb_cache_index = 0;
722 if (kernel)
723 local_paca->slb_cache[slb_cache_index++] = esid_data >> SID_SHIFT;
724 local_paca->slb_cache_ptr = slb_cache_index;
726 asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data));
728 barrier();
730 if (!kernel)
731 slb_cache_update(esid_data);
733 return 0;
736 static long slb_allocate_kernel(unsigned long ea, unsigned long id)
738 unsigned long context;
739 unsigned long flags;
740 int ssize;
742 if (id == LINEAR_MAP_REGION_ID) {
744 /* We only support upto H_MAX_PHYSMEM_BITS */
745 if ((ea & EA_MASK) > (1UL << H_MAX_PHYSMEM_BITS))
746 return -EFAULT;
748 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp;
750 #ifdef CONFIG_SPARSEMEM_VMEMMAP
751 } else if (id == VMEMMAP_REGION_ID) {
753 if (ea >= H_VMEMMAP_END)
754 return -EFAULT;
756 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmemmap_psize].sllp;
757 #endif
758 } else if (id == VMALLOC_REGION_ID) {
760 if (ea >= H_VMALLOC_END)
761 return -EFAULT;
763 flags = local_paca->vmalloc_sllp;
765 } else if (id == IO_REGION_ID) {
767 if (ea >= H_KERN_IO_END)
768 return -EFAULT;
770 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_io_psize].sllp;
772 } else {
773 return -EFAULT;
776 ssize = MMU_SEGSIZE_1T;
777 if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
778 ssize = MMU_SEGSIZE_256M;
780 context = get_kernel_context(ea);
782 return slb_insert_entry(ea, context, flags, ssize, true);
785 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea)
787 unsigned long context;
788 unsigned long flags;
789 int bpsize;
790 int ssize;
793 * consider this as bad access if we take a SLB miss
794 * on an address above addr limit.
796 if (ea >= mm_ctx_slb_addr_limit(&mm->context))
797 return -EFAULT;
799 context = get_user_context(&mm->context, ea);
800 if (!context)
801 return -EFAULT;
803 if (unlikely(ea >= H_PGTABLE_RANGE)) {
804 WARN_ON(1);
805 return -EFAULT;
808 ssize = user_segment_size(ea);
810 bpsize = get_slice_psize(mm, ea);
811 flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
813 return slb_insert_entry(ea, context, flags, ssize, false);
816 long do_slb_fault(struct pt_regs *regs, unsigned long ea)
818 unsigned long id = get_region_id(ea);
820 /* IRQs are not reconciled here, so can't check irqs_disabled */
821 VM_WARN_ON(mfmsr() & MSR_EE);
823 if (unlikely(!(regs->msr & MSR_RI)))
824 return -EINVAL;
827 * SLB kernel faults must be very careful not to touch anything
828 * that is not bolted. E.g., PACA and global variables are okay,
829 * mm->context stuff is not.
831 * SLB user faults can access all of kernel memory, but must be
832 * careful not to touch things like IRQ state because it is not
833 * "reconciled" here. The difficulty is that we must use
834 * fast_exception_return to return from kernel SLB faults without
835 * looking at possible non-bolted memory. We could test user vs
836 * kernel faults in the interrupt handler asm and do a full fault,
837 * reconcile, ret_from_except for user faults which would make them
838 * first class kernel code. But for performance it's probably nicer
839 * if they go via fast_exception_return too.
841 if (id >= LINEAR_MAP_REGION_ID) {
842 long err;
843 #ifdef CONFIG_DEBUG_VM
844 /* Catch recursive kernel SLB faults. */
845 BUG_ON(local_paca->in_kernel_slb_handler);
846 local_paca->in_kernel_slb_handler = 1;
847 #endif
848 err = slb_allocate_kernel(ea, id);
849 #ifdef CONFIG_DEBUG_VM
850 local_paca->in_kernel_slb_handler = 0;
851 #endif
852 return err;
853 } else {
854 struct mm_struct *mm = current->mm;
855 long err;
857 if (unlikely(!mm))
858 return -EFAULT;
860 err = slb_allocate_user(mm, ea);
861 if (!err)
862 preload_add(current_thread_info(), ea);
864 return err;
868 void do_bad_slb_fault(struct pt_regs *regs, unsigned long ea, long err)
870 if (err == -EFAULT) {
871 if (user_mode(regs))
872 _exception(SIGSEGV, regs, SEGV_BNDERR, ea);
873 else
874 bad_page_fault(regs, ea, SIGSEGV);
875 } else if (err == -EINVAL) {
876 unrecoverable_exception(regs);
877 } else {
878 BUG();