Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[sfrench/cifs-2.6.git] / arch / frv / mm / fault.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/frv/mm/fault.c
4  *
5  * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
6  * - Written by David Howells (dhowells@redhat.com)
7  * - Derived from arch/m68knommu/mm/fault.c
8  *   - Copyright (C) 1998  D. Jeff Dionne <jeff@lineo.ca>,
9  *   - Copyright (C) 2000  Lineo, Inc.  (www.lineo.com)
10  *
11  *  Based on:
12  *
13  *  linux/arch/m68k/mm/fault.c
14  *
15  *  Copyright (C) 1995  Hamish Macdonald
16  */
17
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20 #include <linux/kernel.h>
21 #include <linux/ptrace.h>
22 #include <linux/hardirq.h>
23 #include <linux/uaccess.h>
24
25 #include <asm/pgtable.h>
26 #include <asm/gdb-stub.h>
27
28 /*****************************************************************************/
29 /*
30  * This routine handles page faults.  It determines the problem, and
31  * then passes it off to one of the appropriate routines.
32  */
33 asmlinkage void do_page_fault(int datammu, unsigned long esr0, unsigned long ear0)
34 {
35         struct vm_area_struct *vma;
36         struct mm_struct *mm;
37         unsigned long _pme, lrai, lrad;
38         unsigned long flags = 0;
39         siginfo_t info;
40         pgd_t *pge;
41         pud_t *pue;
42         pte_t *pte;
43         int fault;
44
45 #if 0
46         const char *atxc[16] = {
47                 [0x0] = "mmu-miss", [0x8] = "multi-dat", [0x9] = "multi-sat",
48                 [0xa] = "tlb-miss", [0xc] = "privilege", [0xd] = "write-prot",
49         };
50
51         printk("do_page_fault(%d,%lx [%s],%lx)\n",
52                datammu, esr0, atxc[esr0 >> 20 & 0xf], ear0);
53 #endif
54
55         mm = current->mm;
56
57         /*
58          * We fault-in kernel-space virtual memory on-demand. The
59          * 'reference' page table is init_mm.pgd.
60          *
61          * NOTE! We MUST NOT take any locks for this case. We may
62          * be in an interrupt or a critical region, and should
63          * only copy the information from the master page table,
64          * nothing more.
65          *
66          * This verifies that the fault happens in kernel space
67          * and that the fault was a page not present (invalid) error
68          */
69         if (!user_mode(__frame) && (esr0 & ESR0_ATXC) == ESR0_ATXC_AMRTLB_MISS) {
70                 if (ear0 >= VMALLOC_START && ear0 < VMALLOC_END)
71                         goto kernel_pte_fault;
72                 if (ear0 >= PKMAP_BASE && ear0 < PKMAP_END)
73                         goto kernel_pte_fault;
74         }
75
76         info.si_code = SEGV_MAPERR;
77
78         /*
79          * If we're in an interrupt or have no user
80          * context, we must not take the fault..
81          */
82         if (faulthandler_disabled() || !mm)
83                 goto no_context;
84
85         if (user_mode(__frame))
86                 flags |= FAULT_FLAG_USER;
87
88         down_read(&mm->mmap_sem);
89
90         vma = find_vma(mm, ear0);
91         if (!vma)
92                 goto bad_area;
93         if (vma->vm_start <= ear0)
94                 goto good_area;
95         if (!(vma->vm_flags & VM_GROWSDOWN))
96                 goto bad_area;
97
98         if (user_mode(__frame)) {
99                 /*
100                  * accessing the stack below %esp is always a bug.
101                  * The "+ 32" is there due to some instructions (like
102                  * pusha) doing post-decrement on the stack and that
103                  * doesn't show up until later..
104                  */
105                 if ((ear0 & PAGE_MASK) + 2 * PAGE_SIZE < __frame->sp) {
106 #if 0
107                         printk("[%d] ### Access below stack @%lx (sp=%lx)\n",
108                                current->pid, ear0, __frame->sp);
109                         show_registers(__frame);
110                         printk("[%d] ### Code: [%08lx] %02x %02x %02x %02x %02x %02x %02x %02x\n",
111                                current->pid,
112                                __frame->pc,
113                                ((u8*)__frame->pc)[0],
114                                ((u8*)__frame->pc)[1],
115                                ((u8*)__frame->pc)[2],
116                                ((u8*)__frame->pc)[3],
117                                ((u8*)__frame->pc)[4],
118                                ((u8*)__frame->pc)[5],
119                                ((u8*)__frame->pc)[6],
120                                ((u8*)__frame->pc)[7]
121                                );
122 #endif
123                         goto bad_area;
124                 }
125         }
126
127         if (expand_stack(vma, ear0))
128                 goto bad_area;
129
130 /*
131  * Ok, we have a good vm_area for this memory access, so
132  * we can handle it..
133  */
134  good_area:
135         info.si_code = SEGV_ACCERR;
136         switch (esr0 & ESR0_ATXC) {
137         default:
138                 /* handle write to write protected page */
139         case ESR0_ATXC_WP_EXCEP:
140 #ifdef TEST_VERIFY_AREA
141                 if (!(user_mode(__frame)))
142                         printk("WP fault at %08lx\n", __frame->pc);
143 #endif
144                 if (!(vma->vm_flags & VM_WRITE))
145                         goto bad_area;
146                 flags |= FAULT_FLAG_WRITE;
147                 break;
148
149                  /* handle read from protected page */
150         case ESR0_ATXC_PRIV_EXCEP:
151                 goto bad_area;
152
153                  /* handle read, write or exec on absent page
154                   * - can't support write without permitting read
155                   * - don't support execute without permitting read and vice-versa
156                   */
157         case ESR0_ATXC_AMRTLB_MISS:
158                 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
159                         goto bad_area;
160                 break;
161         }
162
163         /*
164          * If for any reason at all we couldn't handle the fault,
165          * make sure we exit gracefully rather than endlessly redo
166          * the fault.
167          */
168         fault = handle_mm_fault(vma, ear0, flags);
169         if (unlikely(fault & VM_FAULT_ERROR)) {
170                 if (fault & VM_FAULT_OOM)
171                         goto out_of_memory;
172                 else if (fault & VM_FAULT_SIGSEGV)
173                         goto bad_area;
174                 else if (fault & VM_FAULT_SIGBUS)
175                         goto do_sigbus;
176                 BUG();
177         }
178         if (fault & VM_FAULT_MAJOR)
179                 current->maj_flt++;
180         else
181                 current->min_flt++;
182
183         up_read(&mm->mmap_sem);
184         return;
185
186 /*
187  * Something tried to access memory that isn't in our memory map..
188  * Fix it, but check if it's kernel or user first..
189  */
190  bad_area:
191         up_read(&mm->mmap_sem);
192
193         /* User mode accesses just cause a SIGSEGV */
194         if (user_mode(__frame)) {
195                 info.si_signo = SIGSEGV;
196                 info.si_errno = 0;
197                 /* info.si_code has been set above */
198                 info.si_addr = (void *) ear0;
199                 force_sig_info(SIGSEGV, &info, current);
200                 return;
201         }
202
203  no_context:
204         /* are we prepared to handle this kernel fault? */
205         if (fixup_exception(__frame))
206                 return;
207
208 /*
209  * Oops. The kernel tried to access some bad page. We'll have to
210  * terminate things with extreme prejudice.
211  */
212
213         bust_spinlocks(1);
214
215         if (ear0 < PAGE_SIZE)
216                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
217         else
218                 printk(KERN_ALERT "Unable to handle kernel paging request");
219         printk(" at virtual addr %08lx\n", ear0);
220         printk("  PC  : %08lx\n", __frame->pc);
221         printk("  EXC : esr0=%08lx ear0=%08lx\n", esr0, ear0);
222
223         asm("lrai %1,%0,#1,#0,#0" : "=&r"(lrai) : "r"(ear0));
224         asm("lrad %1,%0,#1,#0,#0" : "=&r"(lrad) : "r"(ear0));
225
226         printk(KERN_ALERT "  LRAI: %08lx\n", lrai);
227         printk(KERN_ALERT "  LRAD: %08lx\n", lrad);
228
229         __break_hijack_kernel_event();
230
231         pge = pgd_offset(current->mm, ear0);
232         pue = pud_offset(pge, ear0);
233         _pme = pue->pue[0].ste[0];
234
235         printk(KERN_ALERT "  PGE : %8p { PME %08lx }\n", pge, _pme);
236
237         if (_pme & xAMPRx_V) {
238                 unsigned long dampr, damlr, val;
239
240                 asm volatile("movsg dampr2,%0 ! movgs %2,dampr2 ! movsg damlr2,%1"
241                              : "=&r"(dampr), "=r"(damlr)
242                              : "r" (_pme | xAMPRx_L|xAMPRx_SS_16Kb|xAMPRx_S|xAMPRx_C|xAMPRx_V)
243                              );
244
245                 pte = (pte_t *) damlr + __pte_index(ear0);
246                 val = pte_val(*pte);
247
248                 asm volatile("movgs %0,dampr2" :: "r" (dampr));
249
250                 printk(KERN_ALERT "  PTE : %8p { %08lx }\n", pte, val);
251         }
252
253         die_if_kernel("Oops\n");
254         do_exit(SIGKILL);
255
256 /*
257  * We ran out of memory, or some other thing happened to us that made
258  * us unable to handle the page fault gracefully.
259  */
260  out_of_memory:
261         up_read(&mm->mmap_sem);
262         if (!user_mode(__frame))
263                 goto no_context;
264         pagefault_out_of_memory();
265         return;
266
267  do_sigbus:
268         up_read(&mm->mmap_sem);
269
270         /*
271          * Send a sigbus, regardless of whether we were in kernel
272          * or user mode.
273          */
274         info.si_signo = SIGBUS;
275         info.si_errno = 0;
276         info.si_code = BUS_ADRERR;
277         info.si_addr = (void *) ear0;
278         force_sig_info(SIGBUS, &info, current);
279
280         /* Kernel mode? Handle exceptions or die */
281         if (!user_mode(__frame))
282                 goto no_context;
283         return;
284
285 /*
286  * The fault was caused by a kernel PTE (such as installed by vmalloc or kmap)
287  */
288  kernel_pte_fault:
289         {
290                 /*
291                  * Synchronize this task's top level page-table
292                  * with the 'reference' page table.
293                  *
294                  * Do _not_ use "tsk" here. We might be inside
295                  * an interrupt in the middle of a task switch..
296                  */
297                 int index = pgd_index(ear0);
298                 pgd_t *pgd, *pgd_k;
299                 pud_t *pud, *pud_k;
300                 pmd_t *pmd, *pmd_k;
301                 pte_t *pte_k;
302
303                 pgd = (pgd_t *) __get_TTBR();
304                 pgd = (pgd_t *)__va(pgd) + index;
305                 pgd_k = ((pgd_t *)(init_mm.pgd)) + index;
306
307                 if (!pgd_present(*pgd_k))
308                         goto no_context;
309                 //set_pgd(pgd, *pgd_k); /////// gcc ICE's on this line
310
311                 pud_k = pud_offset(pgd_k, ear0);
312                 if (!pud_present(*pud_k))
313                         goto no_context;
314
315                 pmd_k = pmd_offset(pud_k, ear0);
316                 if (!pmd_present(*pmd_k))
317                         goto no_context;
318
319                 pud = pud_offset(pgd, ear0);
320                 pmd = pmd_offset(pud, ear0);
321                 set_pmd(pmd, *pmd_k);
322
323                 pte_k = pte_offset_kernel(pmd_k, ear0);
324                 if (!pte_present(*pte_k))
325                         goto no_context;
326                 return;
327         }
328 } /* end do_page_fault() */