mm: don't include asm/pgtable.h if linux/mm.h is already included
[platform/kernel/linux-starfive.git] / arch / unicore32 / mm / fault.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/unicore32/mm/fault.c
4  *
5  * Code specific to PKUnity SoC and UniCore ISA
6  *
7  * Copyright (C) 2001-2010 GUAN Xue-tao
8  */
9 #include <linux/extable.h>
10 #include <linux/signal.h>
11 #include <linux/mm.h>
12 #include <linux/hardirq.h>
13 #include <linux/init.h>
14 #include <linux/kprobes.h>
15 #include <linux/uaccess.h>
16 #include <linux/page-flags.h>
17 #include <linux/sched/signal.h>
18 #include <linux/io.h>
19
20 #include <asm/tlbflush.h>
21
22 /*
23  * Fault status register encodings.  We steal bit 31 for our own purposes.
24  */
25 #define FSR_LNX_PF              (1 << 31)
26
27 static inline int fsr_fs(unsigned int fsr)
28 {
29         /* xyabcde will be abcde+xy */
30         return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
31 }
32
33 /*
34  * This is useful to dump out the page tables associated with
35  * 'addr' in mm 'mm'.
36  */
37 void show_pte(struct mm_struct *mm, unsigned long addr)
38 {
39         pgd_t *pgd;
40
41         if (!mm)
42                 mm = &init_mm;
43
44         printk(KERN_ALERT "pgd = %p\n", mm->pgd);
45         pgd = pgd_offset(mm, addr);
46         printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
47
48         do {
49                 pmd_t *pmd;
50                 pte_t *pte;
51
52                 if (pgd_none(*pgd))
53                         break;
54
55                 if (pgd_bad(*pgd)) {
56                         printk("(bad)");
57                         break;
58                 }
59
60                 pmd = pmd_offset((pud_t *) pgd, addr);
61                 if (PTRS_PER_PMD != 1)
62                         printk(", *pmd=%08lx", pmd_val(*pmd));
63
64                 if (pmd_none(*pmd))
65                         break;
66
67                 if (pmd_bad(*pmd)) {
68                         printk("(bad)");
69                         break;
70                 }
71
72                 /* We must not map this if we have highmem enabled */
73                 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
74                         break;
75
76                 pte = pte_offset_map(pmd, addr);
77                 printk(", *pte=%08lx", pte_val(*pte));
78                 pte_unmap(pte);
79         } while (0);
80
81         printk("\n");
82 }
83
84 /*
85  * Oops.  The kernel tried to access some page that wasn't present.
86  */
87 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
88                 unsigned int fsr, struct pt_regs *regs)
89 {
90         /*
91          * Are we prepared to handle this kernel fault?
92          */
93         if (fixup_exception(regs))
94                 return;
95
96         /*
97          * No handler, we'll have to terminate things with extreme prejudice.
98          */
99         bust_spinlocks(1);
100         printk(KERN_ALERT
101                "Unable to handle kernel %s at virtual address %08lx\n",
102                (addr < PAGE_SIZE) ? "NULL pointer dereference" :
103                "paging request", addr);
104
105         show_pte(mm, addr);
106         die("Oops", regs, fsr);
107         bust_spinlocks(0);
108         do_exit(SIGKILL);
109 }
110
111 /*
112  * Something tried to access memory that isn't in our memory map..
113  * User mode accesses just cause a SIGSEGV
114  */
115 static void __do_user_fault(unsigned long addr, unsigned int fsr,
116                             unsigned int sig, int code, struct pt_regs *regs)
117 {
118         struct task_struct *tsk = current;
119
120         tsk->thread.address = addr;
121         tsk->thread.error_code = fsr;
122         tsk->thread.trap_no = 14;
123         force_sig_fault(sig, code, (void __user *)addr);
124 }
125
126 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
127 {
128         struct task_struct *tsk = current;
129         struct mm_struct *mm = tsk->active_mm;
130
131         /*
132          * If we are in kernel mode at this point, we
133          * have no context to handle this fault with.
134          */
135         if (user_mode(regs))
136                 __do_user_fault(addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
137         else
138                 __do_kernel_fault(mm, addr, fsr, regs);
139 }
140
141 #define VM_FAULT_BADMAP         0x010000
142 #define VM_FAULT_BADACCESS      0x020000
143
144 /*
145  * Check that the permissions on the VMA allow for the fault which occurred.
146  * If we encountered a write fault, we must have write permission, otherwise
147  * we allow any permission.
148  */
149 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
150 {
151         unsigned int mask = VM_ACCESS_FLAGS;
152
153         if (!(fsr ^ 0x12))      /* write? */
154                 mask = VM_WRITE;
155         if (fsr & FSR_LNX_PF)
156                 mask = VM_EXEC;
157
158         return vma->vm_flags & mask ? false : true;
159 }
160
161 static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
162                 unsigned int fsr, unsigned int flags, struct task_struct *tsk)
163 {
164         struct vm_area_struct *vma;
165         vm_fault_t fault;
166
167         vma = find_vma(mm, addr);
168         fault = VM_FAULT_BADMAP;
169         if (unlikely(!vma))
170                 goto out;
171         if (unlikely(vma->vm_start > addr))
172                 goto check_stack;
173
174         /*
175          * Ok, we have a good vm_area for this
176          * memory access, so we can handle it.
177          */
178 good_area:
179         if (access_error(fsr, vma)) {
180                 fault = VM_FAULT_BADACCESS;
181                 goto out;
182         }
183
184         /*
185          * If for any reason at all we couldn't handle the fault, make
186          * sure we exit gracefully rather than endlessly redo the fault.
187          */
188         fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
189         return fault;
190
191 check_stack:
192         if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
193                 goto good_area;
194 out:
195         return fault;
196 }
197
198 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
199 {
200         struct task_struct *tsk;
201         struct mm_struct *mm;
202         int sig, code;
203         vm_fault_t fault;
204         unsigned int flags = FAULT_FLAG_DEFAULT;
205
206         tsk = current;
207         mm = tsk->mm;
208
209         /*
210          * If we're in an interrupt or have no user
211          * context, we must not take the fault..
212          */
213         if (faulthandler_disabled() || !mm)
214                 goto no_context;
215
216         if (user_mode(regs))
217                 flags |= FAULT_FLAG_USER;
218         if (!(fsr ^ 0x12))
219                 flags |= FAULT_FLAG_WRITE;
220
221         /*
222          * As per x86, we may deadlock here.  However, since the kernel only
223          * validly references user space from well defined areas of the code,
224          * we can bug out early if this is from code which shouldn't.
225          */
226         if (!down_read_trylock(&mm->mmap_sem)) {
227                 if (!user_mode(regs)
228                     && !search_exception_tables(regs->UCreg_pc))
229                         goto no_context;
230 retry:
231                 down_read(&mm->mmap_sem);
232         } else {
233                 /*
234                  * The above down_read_trylock() might have succeeded in
235                  * which case, we'll have missed the might_sleep() from
236                  * down_read()
237                  */
238                 might_sleep();
239 #ifdef CONFIG_DEBUG_VM
240                 if (!user_mode(regs) &&
241                     !search_exception_tables(regs->UCreg_pc))
242                         goto no_context;
243 #endif
244         }
245
246         fault = __do_pf(mm, addr, fsr, flags, tsk);
247
248         /* If we need to retry but a fatal signal is pending, handle the
249          * signal first. We do not need to release the mmap_sem because
250          * it would already be released in __lock_page_or_retry in
251          * mm/filemap.c. */
252         if (fault_signal_pending(fault, regs))
253                 return 0;
254
255         if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
256                 if (fault & VM_FAULT_MAJOR)
257                         tsk->maj_flt++;
258                 else
259                         tsk->min_flt++;
260                 if (fault & VM_FAULT_RETRY) {
261                         flags |= FAULT_FLAG_TRIED;
262                         goto retry;
263                 }
264         }
265
266         up_read(&mm->mmap_sem);
267
268         /*
269          * Handle the "normal" case first - VM_FAULT_MAJOR
270          */
271         if (likely(!(fault &
272                (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
273                 return 0;
274
275         /*
276          * If we are in kernel mode at this point, we
277          * have no context to handle this fault with.
278          */
279         if (!user_mode(regs))
280                 goto no_context;
281
282         if (fault & VM_FAULT_OOM) {
283                 /*
284                  * We ran out of memory, call the OOM killer, and return to
285                  * userspace (which will retry the fault, or kill us if we
286                  * got oom-killed)
287                  */
288                 pagefault_out_of_memory();
289                 return 0;
290         }
291
292         if (fault & VM_FAULT_SIGBUS) {
293                 /*
294                  * We had some memory, but were unable to
295                  * successfully fix up this page fault.
296                  */
297                 sig = SIGBUS;
298                 code = BUS_ADRERR;
299         } else {
300                 /*
301                  * Something tried to access memory that
302                  * isn't in our memory map..
303                  */
304                 sig = SIGSEGV;
305                 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
306         }
307
308         __do_user_fault(addr, fsr, sig, code, regs);
309         return 0;
310
311 no_context:
312         __do_kernel_fault(mm, addr, fsr, regs);
313         return 0;
314 }
315
316 /*
317  * First Level Translation Fault Handler
318  *
319  * We enter here because the first level page table doesn't contain
320  * a valid entry for the address.
321  *
322  * If the address is in kernel space (>= TASK_SIZE), then we are
323  * probably faulting in the vmalloc() area.
324  *
325  * If the init_task's first level page tables contains the relevant
326  * entry, we copy the it to this task.  If not, we send the process
327  * a signal, fixup the exception, or oops the kernel.
328  *
329  * NOTE! We MUST NOT take any locks for this case. We may be in an
330  * interrupt or a critical region, and should only copy the information
331  * from the master page table, nothing more.
332  */
333 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
334 {
335         unsigned int index;
336         pgd_t *pgd, *pgd_k;
337         pmd_t *pmd, *pmd_k;
338
339         if (addr < TASK_SIZE)
340                 return do_pf(addr, fsr, regs);
341
342         if (user_mode(regs))
343                 goto bad_area;
344
345         index = pgd_index(addr);
346
347         pgd = cpu_get_pgd() + index;
348         pgd_k = init_mm.pgd + index;
349
350         if (pgd_none(*pgd_k))
351                 goto bad_area;
352
353         pmd_k = pmd_offset((pud_t *) pgd_k, addr);
354         pmd = pmd_offset((pud_t *) pgd, addr);
355
356         if (pmd_none(*pmd_k))
357                 goto bad_area;
358
359         set_pmd(pmd, *pmd_k);
360         flush_pmd_entry(pmd);
361         return 0;
362
363 bad_area:
364         do_bad_area(addr, fsr, regs);
365         return 0;
366 }
367
368 /*
369  * This abort handler always returns "fault".
370  */
371 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
372 {
373         return 1;
374 }
375
376 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
377 {
378         unsigned int res1, res2;
379
380         printk("dabt exception but no error!\n");
381
382         __asm__ __volatile__(
383                         "mff %0,f0\n"
384                         "mff %1,f1\n"
385                         : "=r"(res1), "=r"(res2)
386                         :
387                         : "memory");
388
389         printk(KERN_EMERG "r0 :%08x  r1 :%08x\n", res1, res2);
390         panic("shut up\n");
391         return 0;
392 }
393
394 static struct fsr_info {
395         int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
396         int sig;
397         int code;
398         const char *name;
399 } fsr_info[] = {
400         /*
401          * The following are the standard Unicore-I and UniCore-II aborts.
402          */
403         { do_good,      SIGBUS,  0,             "no error"              },
404         { do_bad,       SIGBUS,  BUS_ADRALN,    "alignment exception"   },
405         { do_bad,       SIGBUS,  BUS_OBJERR,    "external exception"    },
406         { do_bad,       SIGBUS,  0,             "burst operation"       },
407         { do_bad,       SIGBUS,  0,             "unknown 00100"         },
408         { do_ifault,    SIGSEGV, SEGV_MAPERR,   "2nd level pt non-exist"},
409         { do_bad,       SIGBUS,  0,             "2nd lvl large pt non-exist" },
410         { do_bad,       SIGBUS,  0,             "invalid pte"           },
411         { do_pf,        SIGSEGV, SEGV_MAPERR,   "page miss"             },
412         { do_bad,       SIGBUS,  0,             "middle page miss"      },
413         { do_bad,       SIGBUS,  0,             "large page miss"       },
414         { do_pf,        SIGSEGV, SEGV_MAPERR,   "super page (section) miss" },
415         { do_bad,       SIGBUS,  0,             "unknown 01100"         },
416         { do_bad,       SIGBUS,  0,             "unknown 01101"         },
417         { do_bad,       SIGBUS,  0,             "unknown 01110"         },
418         { do_bad,       SIGBUS,  0,             "unknown 01111"         },
419         { do_bad,       SIGBUS,  0,             "addr: up 3G or IO"     },
420         { do_pf,        SIGSEGV, SEGV_ACCERR,   "read unreadable addr"  },
421         { do_pf,        SIGSEGV, SEGV_ACCERR,   "write unwriteable addr"},
422         { do_pf,        SIGSEGV, SEGV_ACCERR,   "exec unexecutable addr"},
423         { do_bad,       SIGBUS,  0,             "unknown 10100"         },
424         { do_bad,       SIGBUS,  0,             "unknown 10101"         },
425         { do_bad,       SIGBUS,  0,             "unknown 10110"         },
426         { do_bad,       SIGBUS,  0,             "unknown 10111"         },
427         { do_bad,       SIGBUS,  0,             "unknown 11000"         },
428         { do_bad,       SIGBUS,  0,             "unknown 11001"         },
429         { do_bad,       SIGBUS,  0,             "unknown 11010"         },
430         { do_bad,       SIGBUS,  0,             "unknown 11011"         },
431         { do_bad,       SIGBUS,  0,             "unknown 11100"         },
432         { do_bad,       SIGBUS,  0,             "unknown 11101"         },
433         { do_bad,       SIGBUS,  0,             "unknown 11110"         },
434         { do_bad,       SIGBUS,  0,             "unknown 11111"         }
435 };
436
437 void __init hook_fault_code(int nr,
438                 int (*fn) (unsigned long, unsigned int, struct pt_regs *),
439                 int sig, int code, const char *name)
440 {
441         if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
442                 BUG();
443
444         fsr_info[nr].fn   = fn;
445         fsr_info[nr].sig  = sig;
446         fsr_info[nr].code = code;
447         fsr_info[nr].name = name;
448 }
449
450 /*
451  * Dispatch a data abort to the relevant handler.
452  */
453 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
454                         struct pt_regs *regs)
455 {
456         const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
457
458         if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
459                 return;
460
461         printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
462                inf->name, fsr, addr);
463
464         uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
465                         fsr, 0);
466 }
467
468 asmlinkage void do_PrefetchAbort(unsigned long addr,
469                         unsigned int ifsr, struct pt_regs *regs)
470 {
471         const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
472
473         if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
474                 return;
475
476         printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
477                inf->name, ifsr, addr);
478
479         uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
480                         ifsr, 0);
481 }