Merge tag 'linux-watchdog-4.20-rc1' of git://www.linux-watchdog.org/linux-watchdog
[sfrench/cifs-2.6.git] / arch / powerpc / mm / slb.c
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
17 #include <asm/asm-prototypes.h>
18 #include <asm/pgtable.h>
19 #include <asm/mmu.h>
20 #include <asm/mmu_context.h>
21 #include <asm/paca.h>
22 #include <asm/cputable.h>
23 #include <asm/cacheflush.h>
24 #include <asm/smp.h>
25 #include <linux/compiler.h>
26 #include <linux/context_tracking.h>
27 #include <linux/mm_types.h>
28
29 #include <asm/udbg.h>
30 #include <asm/code-patching.h>
31
32 enum slb_index {
33         LINEAR_INDEX    = 0, /* Kernel linear map  (0xc000000000000000) */
34         KSTACK_INDEX    = 1, /* Kernel stack map */
35 };
36
37 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea);
38
39 #define slb_esid_mask(ssize)    \
40         (((ssize) == MMU_SEGSIZE_256M)? ESID_MASK: ESID_MASK_1T)
41
42 static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
43                                          enum slb_index index)
44 {
45         return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
46 }
47
48 static inline unsigned long __mk_vsid_data(unsigned long vsid, int ssize,
49                                          unsigned long flags)
50 {
51         return (vsid << slb_vsid_shift(ssize)) | flags |
52                 ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
53 }
54
55 static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
56                                          unsigned long flags)
57 {
58         return __mk_vsid_data(get_kernel_vsid(ea, ssize), ssize, flags);
59 }
60
61 static void assert_slb_exists(unsigned long ea)
62 {
63 #ifdef CONFIG_DEBUG_VM
64         unsigned long tmp;
65
66         WARN_ON_ONCE(mfmsr() & MSR_EE);
67
68         asm volatile("slbfee. %0, %1" : "=r"(tmp) : "r"(ea) : "cr0");
69         WARN_ON(tmp == 0);
70 #endif
71 }
72
73 static void assert_slb_notexists(unsigned long ea)
74 {
75 #ifdef CONFIG_DEBUG_VM
76         unsigned long tmp;
77
78         WARN_ON_ONCE(mfmsr() & MSR_EE);
79
80         asm volatile("slbfee. %0, %1" : "=r"(tmp) : "r"(ea) : "cr0");
81         WARN_ON(tmp != 0);
82 #endif
83 }
84
85 static inline void slb_shadow_update(unsigned long ea, int ssize,
86                                      unsigned long flags,
87                                      enum slb_index index)
88 {
89         struct slb_shadow *p = get_slb_shadow();
90
91         /*
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.
95          */
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)));
99 }
100
101 static inline void slb_shadow_clear(enum slb_index index)
102 {
103         WRITE_ONCE(get_slb_shadow()->save_area[index].esid, cpu_to_be64(index));
104 }
105
106 static inline void create_shadowed_slbe(unsigned long ea, int ssize,
107                                         unsigned long flags,
108                                         enum slb_index index)
109 {
110         /*
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.
114          */
115         slb_shadow_update(ea, ssize, flags, index);
116
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))
121                      : "memory" );
122 }
123
124 /*
125  * Insert bolted entries into SLB (which may not be empty, so don't clear
126  * slb_cache_ptr).
127  */
128 void __slb_restore_bolted_realmode(void)
129 {
130         struct slb_shadow *p = get_slb_shadow();
131         enum slb_index index;
132
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)));
138         }
139
140         assert_slb_exists(local_paca->kstack);
141 }
142
143 /*
144  * Insert the bolted entries into an empty SLB.
145  */
146 void slb_restore_bolted_realmode(void)
147 {
148         __slb_restore_bolted_realmode();
149         get_paca()->slb_cache_ptr = 0;
150
151         get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
152         get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
153 }
154
155 /*
156  * This flushes all SLB entries including 0, so it must be realmode.
157  */
158 void slb_flush_all_realmode(void)
159 {
160         asm volatile("slbmte %0,%0; slbia" : : "r" (0));
161 }
162
163 /*
164  * This flushes non-bolted entries, it can be run in virtual mode. Must
165  * be called with interrupts disabled.
166  */
167 void slb_flush_and_restore_bolted(void)
168 {
169         struct slb_shadow *p = get_slb_shadow();
170
171         BUILD_BUG_ON(SLB_NUM_BOLTED != 2);
172
173         WARN_ON(!irqs_disabled());
174
175         /*
176          * We can't take a PMU exception in the following code, so hard
177          * disable interrupts.
178          */
179         hard_irq_disable();
180
181         asm volatile("isync\n"
182                      "slbia\n"
183                      "slbmte  %0, %1\n"
184                      "isync\n"
185                      :: "r" (be64_to_cpu(p->save_area[KSTACK_INDEX].vsid)),
186                         "r" (be64_to_cpu(p->save_area[KSTACK_INDEX].esid))
187                      : "memory");
188         assert_slb_exists(get_paca()->kstack);
189
190         get_paca()->slb_cache_ptr = 0;
191
192         get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
193         get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
194 }
195
196 void slb_save_contents(struct slb_entry *slb_ptr)
197 {
198         int i;
199         unsigned long e, v;
200
201         /* Save slb_cache_ptr value. */
202         get_paca()->slb_save_cache_ptr = get_paca()->slb_cache_ptr;
203
204         if (!slb_ptr)
205                 return;
206
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));
210                 slb_ptr->esid = e;
211                 slb_ptr->vsid = v;
212                 slb_ptr++;
213         }
214 }
215
216 void slb_dump_contents(struct slb_entry *slb_ptr)
217 {
218         int i, n;
219         unsigned long e, v;
220         unsigned long llp;
221
222         if (!slb_ptr)
223                 return;
224
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);
227
228         for (i = 0; i < mmu_slb_size; i++) {
229                 e = slb_ptr->esid;
230                 v = slb_ptr->vsid;
231                 slb_ptr++;
232
233                 if (!e && !v)
234                         continue;
235
236                 pr_err("%02d %016lx %016lx\n", i, e, v);
237
238                 if (!(e & SLB_ESID_V)) {
239                         pr_err("\n");
240                         continue;
241                 }
242                 llp = v & SLB_VSID_LLP;
243                 if (v & SLB_VSID_B_1T) {
244                         pr_err("  1T  ESID=%9lx  VSID=%13lx LLP:%3lx\n",
245                                GET_ESID_1T(e),
246                                (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T, llp);
247                 } else {
248                         pr_err(" 256M ESID=%9lx  VSID=%13lx LLP:%3lx\n",
249                                GET_ESID(e),
250                                (v & ~SLB_VSID_B) >> SLB_VSID_SHIFT, llp);
251                 }
252         }
253         pr_err("----------------------------------\n");
254
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]);
264 }
265
266 void slb_vmalloc_update(void)
267 {
268         /*
269          * vmalloc is not bolted, so just have to flush non-bolted.
270          */
271         slb_flush_and_restore_bolted();
272 }
273
274 static bool preload_hit(struct thread_info *ti, unsigned long esid)
275 {
276         unsigned char i;
277
278         for (i = 0; i < ti->slb_preload_nr; i++) {
279                 unsigned char idx;
280
281                 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
282                 if (esid == ti->slb_preload_esid[idx])
283                         return true;
284         }
285         return false;
286 }
287
288 static bool preload_add(struct thread_info *ti, unsigned long ea)
289 {
290         unsigned char idx;
291         unsigned long esid;
292
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)
296                         ea &= ESID_MASK_1T;
297         }
298
299         esid = ea >> SID_SHIFT;
300
301         if (preload_hit(ti, esid))
302                 return false;
303
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;
308         else
309                 ti->slb_preload_nr++;
310
311         return true;
312 }
313
314 static void preload_age(struct thread_info *ti)
315 {
316         if (!ti->slb_preload_nr)
317                 return;
318         ti->slb_preload_nr--;
319         ti->slb_preload_tail = (ti->slb_preload_tail + 1) % SLB_PRELOAD_NR;
320 }
321
322 void slb_setup_new_exec(void)
323 {
324         struct thread_info *ti = current_thread_info();
325         struct mm_struct *mm = current->mm;
326         unsigned long exec = 0x10000000;
327
328         WARN_ON(irqs_disabled());
329
330         /*
331          * preload cache can only be used to determine whether a SLB
332          * entry exists if it does not start to overflow.
333          */
334         if (ti->slb_preload_nr + 2 > SLB_PRELOAD_NR)
335                 return;
336
337         hard_irq_disable();
338
339         /*
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.
343          *
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,
347          * however.
348          */
349
350         /*
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.
354          */
355         if (!is_kernel_addr(exec)) {
356                 if (preload_add(ti, exec))
357                         slb_allocate_user(mm, exec);
358         }
359
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);
364         }
365
366         /* see switch_slb */
367         asm volatile("isync" : : : "memory");
368
369         local_irq_enable();
370 }
371
372 void preload_new_slb_context(unsigned long start, unsigned long sp)
373 {
374         struct thread_info *ti = current_thread_info();
375         struct mm_struct *mm = current->mm;
376         unsigned long heap = mm->start_brk;
377
378         WARN_ON(irqs_disabled());
379
380         /* see above */
381         if (ti->slb_preload_nr + 3 > SLB_PRELOAD_NR)
382                 return;
383
384         hard_irq_disable();
385
386         /* Userspace entry address. */
387         if (!is_kernel_addr(start)) {
388                 if (preload_add(ti, start))
389                         slb_allocate_user(mm, start);
390         }
391
392         /* Top of stack, grows down. */
393         if (!is_kernel_addr(sp)) {
394                 if (preload_add(ti, sp))
395                         slb_allocate_user(mm, sp);
396         }
397
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);
402         }
403
404         /* see switch_slb */
405         asm volatile("isync" : : : "memory");
406
407         local_irq_enable();
408 }
409
410
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)
413 {
414         struct thread_info *ti = task_thread_info(tsk);
415         unsigned char i;
416
417         /*
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.
422          */
423         hard_irq_disable();
424         asm volatile("isync" : : : "memory");
425         if (cpu_has_feature(CPU_FTR_ARCH_300)) {
426                 /*
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
430                  * cache.
431                  */
432                 asm volatile(PPC_SLBIA(3));
433         } else {
434                 unsigned long offset = get_paca()->slb_cache_ptr;
435
436                 if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
437                     offset <= SLB_CACHE_ENTRIES) {
438                         unsigned long slbie_data = 0;
439
440                         for (i = 0; i < offset; i++) {
441                                 unsigned long ea;
442
443                                 ea = (unsigned long)
444                                         get_paca()->slb_cache[i] << SID_SHIFT;
445                                 /*
446                                  * Could assert_slb_exists here, but hypervisor
447                                  * or machine check could have come in and
448                                  * removed the entry at this point.
449                                  */
450
451                                 slbie_data = ea;
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));
456                         }
457
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));
461
462                 } else {
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);
468
469                         asm volatile(PPC_SLBIA(1) "\n"
470                                      "slbmte    %0,%1\n"
471                                      "isync"
472                                      :: "r"(ksp_vsid_data),
473                                         "r"(ksp_esid_data));
474
475                         get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
476                 }
477
478                 get_paca()->slb_cache_ptr = 0;
479         }
480         get_paca()->slb_used_bitmap = get_paca()->slb_kern_bitmap;
481
482         copy_mm_to_paca(mm);
483
484         /*
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
488          * SLB preload cache.
489          */
490         tsk->thread.load_slb++;
491         if (!tsk->thread.load_slb) {
492                 unsigned long pc = KSTK_EIP(tsk);
493
494                 preload_age(ti);
495                 preload_add(ti, pc);
496         }
497
498         for (i = 0; i < ti->slb_preload_nr; i++) {
499                 unsigned char idx;
500                 unsigned long ea;
501
502                 idx = (ti->slb_preload_tail + i) % SLB_PRELOAD_NR;
503                 ea = (unsigned long)ti->slb_preload_esid[idx] << SID_SHIFT;
504
505                 slb_allocate_user(mm, ea);
506         }
507
508         /*
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).
512          */
513         asm volatile("isync" : : : "memory");
514 }
515
516 void slb_set_size(u16 size)
517 {
518         mmu_slb_size = size;
519 }
520
521 void slb_initialize(void)
522 {
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;
528 #endif
529
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;
537 #endif
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);
544 #endif
545         }
546
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;
550
551         lflags = SLB_VSID_KERNEL | linear_llp;
552
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);
558
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.
563          */
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);
569
570         asm volatile("isync":::"memory");
571 }
572
573 static void slb_cache_update(unsigned long esid_data)
574 {
575         int slb_cache_index;
576
577         if (cpu_has_feature(CPU_FTR_ARCH_300))
578                 return; /* ISAv3.0B and later does not use slb_cache */
579
580         /*
581          * Now update slb cache entries
582          */
583         slb_cache_index = local_paca->slb_cache_ptr;
584         if (slb_cache_index < SLB_CACHE_ENTRIES) {
585                 /*
586                  * We have space in slb cache for optimized switch_slb().
587                  * Top 36 bits from esid_data as per ISA
588                  */
589                 local_paca->slb_cache[slb_cache_index++] = esid_data >> 28;
590                 local_paca->slb_cache_ptr++;
591         } else {
592                 /*
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.
596                  */
597                 local_paca->slb_cache_ptr = SLB_CACHE_ENTRIES + 1;
598         }
599 }
600
601 static enum slb_index alloc_slb_index(bool kernel)
602 {
603         enum slb_index index;
604
605         /*
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.
614          */
615
616         /*
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.
620          */
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;
624                 if (kernel)
625                         local_paca->slb_kern_bitmap |= 1U << index;
626         } else {
627                 /* round-robin replacement of slb starting at SLB_NUM_BOLTED. */
628                 index = local_paca->stab_rr;
629                 if (index < (mmu_slb_size - 1))
630                         index++;
631                 else
632                         index = SLB_NUM_BOLTED;
633                 local_paca->stab_rr = index;
634                 if (index < 32) {
635                         if (kernel)
636                                 local_paca->slb_kern_bitmap |= 1U << index;
637                         else
638                                 local_paca->slb_kern_bitmap &= ~(1U << index);
639                 }
640         }
641         BUG_ON(index < SLB_NUM_BOLTED);
642
643         return index;
644 }
645
646 static long slb_insert_entry(unsigned long ea, unsigned long context,
647                                 unsigned long flags, int ssize, bool kernel)
648 {
649         unsigned long vsid;
650         unsigned long vsid_data, esid_data;
651         enum slb_index index;
652
653         vsid = get_vsid(context, ea, ssize);
654         if (!vsid)
655                 return -EFAULT;
656
657         /*
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
660          * the SLB.
661          *
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.
665          */
666         barrier();
667
668         index = alloc_slb_index(kernel);
669
670         vsid_data = __mk_vsid_data(vsid, ssize, flags);
671         esid_data = mk_esid_data(ea, ssize, index);
672
673         /*
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.
678          */
679         assert_slb_notexists(ea);
680         asm volatile("slbmte %0, %1" : : "r" (vsid_data), "r" (esid_data));
681
682         barrier();
683
684         if (!kernel)
685                 slb_cache_update(esid_data);
686
687         return 0;
688 }
689
690 static long slb_allocate_kernel(unsigned long ea, unsigned long id)
691 {
692         unsigned long context;
693         unsigned long flags;
694         int ssize;
695
696         if (id == KERNEL_REGION_ID) {
697
698                 /* We only support upto MAX_PHYSMEM_BITS */
699                 if ((ea & ~REGION_MASK) > (1UL << MAX_PHYSMEM_BITS))
700                         return -EFAULT;
701
702                 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp;
703
704 #ifdef CONFIG_SPARSEMEM_VMEMMAP
705         } else if (id == VMEMMAP_REGION_ID) {
706
707                 if ((ea & ~REGION_MASK) >= (1ULL << MAX_EA_BITS_PER_CONTEXT))
708                         return -EFAULT;
709
710                 flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmemmap_psize].sllp;
711 #endif
712         } else if (id == VMALLOC_REGION_ID) {
713
714                 if ((ea & ~REGION_MASK) >= (1ULL << MAX_EA_BITS_PER_CONTEXT))
715                         return -EFAULT;
716
717                 if (ea < H_VMALLOC_END)
718                         flags = get_paca()->vmalloc_sllp;
719                 else
720                         flags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_io_psize].sllp;
721         } else {
722                 return -EFAULT;
723         }
724
725         ssize = MMU_SEGSIZE_1T;
726         if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
727                 ssize = MMU_SEGSIZE_256M;
728
729         context = get_kernel_context(ea);
730         return slb_insert_entry(ea, context, flags, ssize, true);
731 }
732
733 static long slb_allocate_user(struct mm_struct *mm, unsigned long ea)
734 {
735         unsigned long context;
736         unsigned long flags;
737         int bpsize;
738         int ssize;
739
740         /*
741          * consider this as bad access if we take a SLB miss
742          * on an address above addr limit.
743          */
744         if (ea >= mm->context.slb_addr_limit)
745                 return -EFAULT;
746
747         context = get_user_context(&mm->context, ea);
748         if (!context)
749                 return -EFAULT;
750
751         if (unlikely(ea >= H_PGTABLE_RANGE)) {
752                 WARN_ON(1);
753                 return -EFAULT;
754         }
755
756         ssize = user_segment_size(ea);
757
758         bpsize = get_slice_psize(mm, ea);
759         flags = SLB_VSID_USER | mmu_psize_defs[bpsize].sllp;
760
761         return slb_insert_entry(ea, context, flags, ssize, false);
762 }
763
764 long do_slb_fault(struct pt_regs *regs, unsigned long ea)
765 {
766         unsigned long id = REGION_ID(ea);
767
768         /* IRQs are not reconciled here, so can't check irqs_disabled */
769         VM_WARN_ON(mfmsr() & MSR_EE);
770
771         if (unlikely(!(regs->msr & MSR_RI)))
772                 return -EINVAL;
773
774         /*
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.
778          *
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.
788          */
789         if (id >= KERNEL_REGION_ID) {
790                 long err;
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;
795 #endif
796                 err = slb_allocate_kernel(ea, id);
797 #ifdef CONFIG_DEBUG_VM
798                 local_paca->in_kernel_slb_handler = 0;
799 #endif
800                 return err;
801         } else {
802                 struct mm_struct *mm = current->mm;
803                 long err;
804
805                 if (unlikely(!mm))
806                         return -EFAULT;
807
808                 err = slb_allocate_user(mm, ea);
809                 if (!err)
810                         preload_add(current_thread_info(), ea);
811
812                 return err;
813         }
814 }
815
816 void do_bad_slb_fault(struct pt_regs *regs, unsigned long ea, long err)
817 {
818         if (err == -EFAULT) {
819                 if (user_mode(regs))
820                         _exception(SIGSEGV, regs, SEGV_BNDERR, ea);
821                 else
822                         bad_page_fault(regs, ea, SIGSEGV);
823         } else if (err == -EINVAL) {
824                 unrecoverable_exception(regs);
825         } else {
826                 BUG();
827         }
828 }