87f4281a4b6439b7a6c3097303798db6807eb5e2
[firefly-linux-kernel-4.4.55.git] / arch / arc / mm / fault.c
1 /* Page Fault Handling for ARC (TLB Miss / ProtV)
2  *
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/signal.h>
11 #include <linux/interrupt.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/uaccess.h>
16 #include <linux/kdebug.h>
17 #include <asm/pgalloc.h>
18
19 static int handle_vmalloc_fault(unsigned long address)
20 {
21         /*
22          * Synchronize this task's top level page-table
23          * with the 'reference' page table.
24          */
25         pgd_t *pgd, *pgd_k;
26         pud_t *pud, *pud_k;
27         pmd_t *pmd, *pmd_k;
28
29         pgd = pgd_offset_fast(current->active_mm, address);
30         pgd_k = pgd_offset_k(address);
31
32         if (!pgd_present(*pgd_k))
33                 goto bad_area;
34
35         pud = pud_offset(pgd, address);
36         pud_k = pud_offset(pgd_k, address);
37         if (!pud_present(*pud_k))
38                 goto bad_area;
39
40         pmd = pmd_offset(pud, address);
41         pmd_k = pmd_offset(pud_k, address);
42         if (!pmd_present(*pmd_k))
43                 goto bad_area;
44
45         set_pmd(pmd, *pmd_k);
46
47         /* XXX: create the TLB entry here */
48         return 0;
49
50 bad_area:
51         return 1;
52 }
53
54 void do_page_fault(struct pt_regs *regs, int write, unsigned long address,
55                    unsigned long cause_code)
56 {
57         struct vm_area_struct *vma = NULL;
58         struct task_struct *tsk = current;
59         struct mm_struct *mm = tsk->mm;
60         siginfo_t info;
61         int fault, ret;
62         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
63                                 (write ? FAULT_FLAG_WRITE : 0);
64
65         /*
66          * We fault-in kernel-space virtual memory on-demand. The
67          * 'reference' page table is init_mm.pgd.
68          *
69          * NOTE! We MUST NOT take any locks for this case. We may
70          * be in an interrupt or a critical region, and should
71          * only copy the information from the master page table,
72          * nothing more.
73          */
74         if (address >= VMALLOC_START && address <= VMALLOC_END) {
75                 ret = handle_vmalloc_fault(address);
76                 if (unlikely(ret))
77                         goto bad_area_nosemaphore;
78                 else
79                         return;
80         }
81
82         info.si_code = SEGV_MAPERR;
83
84         /*
85          * If we're in an interrupt or have no user
86          * context, we must not take the fault..
87          */
88         if (in_atomic() || !mm)
89                 goto no_context;
90
91 retry:
92         down_read(&mm->mmap_sem);
93         vma = find_vma(mm, address);
94         if (!vma)
95                 goto bad_area;
96         if (vma->vm_start <= address)
97                 goto good_area;
98         if (!(vma->vm_flags & VM_GROWSDOWN))
99                 goto bad_area;
100         if (expand_stack(vma, address))
101                 goto bad_area;
102
103         /*
104          * Ok, we have a good vm_area for this memory access, so
105          * we can handle it..
106          */
107 good_area:
108         info.si_code = SEGV_ACCERR;
109
110         /* Handle protection violation, execute on heap or stack */
111
112         if (cause_code == ((ECR_V_PROTV << 16) | ECR_C_PROTV_INST_FETCH))
113                 goto bad_area;
114
115         if (write) {
116                 if (!(vma->vm_flags & VM_WRITE))
117                         goto bad_area;
118         } else {
119                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
120                         goto bad_area;
121         }
122
123         /*
124          * If for any reason at all we couldn't handle the fault,
125          * make sure we exit gracefully rather than endlessly redo
126          * the fault.
127          */
128         fault = handle_mm_fault(mm, vma, address, flags);
129
130         /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
131         if (unlikely(fatal_signal_pending(current))) {
132                 if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY))
133                         up_read(&mm->mmap_sem);
134                 if (user_mode(regs))
135                         return;
136         }
137
138         if (likely(!(fault & VM_FAULT_ERROR))) {
139                 if (flags & FAULT_FLAG_ALLOW_RETRY) {
140                         /* To avoid updating stats twice for retry case */
141                         if (fault & VM_FAULT_MAJOR)
142                                 tsk->maj_flt++;
143                         else
144                                 tsk->min_flt++;
145
146                         if (fault & VM_FAULT_RETRY) {
147                                 flags &= ~FAULT_FLAG_ALLOW_RETRY;
148                                 flags |= FAULT_FLAG_TRIED;
149                                 goto retry;
150                         }
151                 }
152
153                 /* Fault Handled Gracefully */
154                 up_read(&mm->mmap_sem);
155                 return;
156         }
157
158         /* TBD: switch to pagefault_out_of_memory() */
159         if (fault & VM_FAULT_OOM)
160                 goto out_of_memory;
161         else if (fault & VM_FAULT_SIGBUS)
162                 goto do_sigbus;
163
164         /* no man's land */
165         BUG();
166
167         /*
168          * Something tried to access memory that isn't in our memory map..
169          * Fix it, but check if it's kernel or user first..
170          */
171 bad_area:
172         up_read(&mm->mmap_sem);
173
174 bad_area_nosemaphore:
175         /* User mode accesses just cause a SIGSEGV */
176         if (user_mode(regs)) {
177                 tsk->thread.fault_address = address;
178                 tsk->thread.cause_code = cause_code;
179                 info.si_signo = SIGSEGV;
180                 info.si_errno = 0;
181                 /* info.si_code has been set above */
182                 info.si_addr = (void __user *)address;
183                 force_sig_info(SIGSEGV, &info, tsk);
184                 return;
185         }
186
187 no_context:
188         /* Are we prepared to handle this kernel fault?
189          *
190          * (The kernel has valid exception-points in the source
191          *  when it acesses user-memory. When it fails in one
192          *  of those points, we find it in a table and do a jump
193          *  to some fixup code that loads an appropriate error
194          *  code)
195          */
196         if (fixup_exception(regs))
197                 return;
198
199         die("Oops", regs, address, cause_code);
200
201 out_of_memory:
202         up_read(&mm->mmap_sem);
203
204         if (user_mode(regs)) {
205                 pagefault_out_of_memory();
206                 return;
207         }
208
209         goto no_context;
210
211 do_sigbus:
212         up_read(&mm->mmap_sem);
213
214         if (!user_mode(regs))
215                 goto no_context;
216
217         tsk->thread.fault_address = address;
218         tsk->thread.cause_code = cause_code;
219         info.si_signo = SIGBUS;
220         info.si_errno = 0;
221         info.si_code = BUS_ADRERR;
222         info.si_addr = (void __user *)address;
223         force_sig_info(SIGBUS, &info, tsk);
224 }