1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/sched/debug.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/string.h>
21#include <linux/types.h>
22#include <linux/ptrace.h>
23#include <linux/mman.h>
24#include <linux/mm.h>
25#include <linux/extable.h>
26#include <linux/uaccess.h>
27#include <linux/ptrace.h>
28
29#include <asm/mmu_context.h>
30#include <asm/traps.h>
31
32#define EXC_SUPERV_INSN_ACCESS 9
33#define EXC_SUPERV_DATA_ACCESS 11
34#define EXC_X_PROTECTION_FAULT 13
35#define EXC_R_PROTECTION_FAULT 14
36#define EXC_W_PROTECTION_FAULT 15
37
38
39
40
41
42
43asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
44 unsigned long address)
45{
46 struct vm_area_struct *vma = NULL;
47 struct task_struct *tsk = current;
48 struct mm_struct *mm = tsk->mm;
49 int code = SEGV_MAPERR;
50 int fault;
51 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
52
53 cause >>= 2;
54
55
56 regs->ea -= 4;
57
58
59
60
61
62
63
64
65
66
67 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {
68 if (user_mode(regs))
69 goto bad_area_nosemaphore;
70 else
71 goto vmalloc_fault;
72 }
73
74 if (unlikely(address >= TASK_SIZE))
75 goto bad_area_nosemaphore;
76
77
78
79
80
81 if (faulthandler_disabled() || !mm)
82 goto bad_area_nosemaphore;
83
84 if (user_mode(regs))
85 flags |= FAULT_FLAG_USER;
86
87 if (!down_read_trylock(&mm->mmap_sem)) {
88 if (!user_mode(regs) && !search_exception_tables(regs->ea))
89 goto bad_area_nosemaphore;
90retry:
91 down_read(&mm->mmap_sem);
92 }
93
94 vma = find_vma(mm, address);
95 if (!vma)
96 goto bad_area;
97 if (vma->vm_start <= address)
98 goto good_area;
99 if (!(vma->vm_flags & VM_GROWSDOWN))
100 goto bad_area;
101 if (expand_stack(vma, address))
102 goto bad_area;
103
104
105
106
107good_area:
108 code = SEGV_ACCERR;
109
110 switch (cause) {
111 case EXC_SUPERV_INSN_ACCESS:
112 goto bad_area;
113 case EXC_SUPERV_DATA_ACCESS:
114 goto bad_area;
115 case EXC_X_PROTECTION_FAULT:
116 if (!(vma->vm_flags & VM_EXEC))
117 goto bad_area;
118 break;
119 case EXC_R_PROTECTION_FAULT:
120 if (!(vma->vm_flags & VM_READ))
121 goto bad_area;
122 break;
123 case EXC_W_PROTECTION_FAULT:
124 if (!(vma->vm_flags & VM_WRITE))
125 goto bad_area;
126 flags = FAULT_FLAG_WRITE;
127 break;
128 }
129
130
131
132
133
134
135 fault = handle_mm_fault(vma, address, flags);
136
137 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
138 return;
139
140 if (unlikely(fault & VM_FAULT_ERROR)) {
141 if (fault & VM_FAULT_OOM)
142 goto out_of_memory;
143 else if (fault & VM_FAULT_SIGSEGV)
144 goto bad_area;
145 else if (fault & VM_FAULT_SIGBUS)
146 goto do_sigbus;
147 BUG();
148 }
149
150
151
152
153
154
155 if (flags & FAULT_FLAG_ALLOW_RETRY) {
156 if (fault & VM_FAULT_MAJOR)
157 current->maj_flt++;
158 else
159 current->min_flt++;
160 if (fault & VM_FAULT_RETRY) {
161
162
163 flags &= ~FAULT_FLAG_ALLOW_RETRY;
164 flags |= FAULT_FLAG_TRIED;
165
166
167
168
169
170
171
172 goto retry;
173 }
174 }
175
176 up_read(&mm->mmap_sem);
177 return;
178
179
180
181
182
183bad_area:
184 up_read(&mm->mmap_sem);
185
186bad_area_nosemaphore:
187
188 if (user_mode(regs)) {
189 if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
190 pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
191 "cause %ld\n", current->comm, SIGSEGV, address, cause);
192 show_regs(regs);
193 }
194 _exception(SIGSEGV, regs, code, address);
195 return;
196 }
197
198no_context:
199
200 if (fixup_exception(regs))
201 return;
202
203
204
205
206
207 bust_spinlocks(1);
208
209 pr_alert("Unable to handle kernel %s at virtual address %08lx",
210 address < PAGE_SIZE ? "NULL pointer dereference" :
211 "paging request", address);
212 pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
213 cause);
214 panic("Oops");
215 return;
216
217
218
219
220
221out_of_memory:
222 up_read(&mm->mmap_sem);
223 if (!user_mode(regs))
224 goto no_context;
225 pagefault_out_of_memory();
226 return;
227
228do_sigbus:
229 up_read(&mm->mmap_sem);
230
231
232 if (!user_mode(regs))
233 goto no_context;
234
235 _exception(SIGBUS, regs, BUS_ADRERR, address);
236 return;
237
238vmalloc_fault:
239 {
240
241
242
243
244
245
246
247 int offset = pgd_index(address);
248 pgd_t *pgd, *pgd_k;
249 pud_t *pud, *pud_k;
250 pmd_t *pmd, *pmd_k;
251 pte_t *pte_k;
252
253 pgd = pgd_current + offset;
254 pgd_k = init_mm.pgd + offset;
255
256 if (!pgd_present(*pgd_k))
257 goto no_context;
258 set_pgd(pgd, *pgd_k);
259
260 pud = pud_offset(pgd, address);
261 pud_k = pud_offset(pgd_k, address);
262 if (!pud_present(*pud_k))
263 goto no_context;
264 pmd = pmd_offset(pud, address);
265 pmd_k = pmd_offset(pud_k, address);
266 if (!pmd_present(*pmd_k))
267 goto no_context;
268 set_pmd(pmd, *pmd_k);
269
270 pte_k = pte_offset_kernel(pmd_k, address);
271 if (!pte_present(*pte_k))
272 goto no_context;
273
274 flush_tlb_one(address);
275 return;
276 }
277}
278