1
2
3
4
5
6
7
8#include <linux/context_tracking.h>
9#include <linux/signal.h>
10#include <linux/sched.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/types.h>
16#include <linux/ptrace.h>
17#include <linux/ratelimit.h>
18#include <linux/mman.h>
19#include <linux/mm.h>
20#include <linux/smp.h>
21#include <linux/kprobes.h>
22#include <linux/perf_event.h>
23#include <linux/uaccess.h>
24
25#include <asm/branch.h>
26#include <asm/mmu_context.h>
27#include <asm/ptrace.h>
28#include <asm/highmem.h>
29#include <linux/kdebug.h>
30
31int show_unhandled_signals = 1;
32
33
34
35
36
37
38static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,
39 unsigned long address)
40{
41 struct vm_area_struct * vma = NULL;
42 struct task_struct *tsk = current;
43 struct mm_struct *mm = tsk->mm;
44 const int field = sizeof(unsigned long) * 2;
45 int si_code;
46 vm_fault_t fault;
47 unsigned int flags = FAULT_FLAG_DEFAULT;
48
49 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
50
51#if 0
52 printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
53 current->comm, current->pid, field, address, write,
54 field, regs->cp0_epc);
55#endif
56
57#ifdef CONFIG_KPROBES
58
59
60
61 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
62 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
63 return;
64#endif
65
66 si_code = SEGV_MAPERR;
67
68
69
70
71
72
73
74
75
76
77#ifdef CONFIG_64BIT
78# define VMALLOC_FAULT_TARGET no_context
79#else
80# define VMALLOC_FAULT_TARGET vmalloc_fault
81#endif
82
83 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
84 goto VMALLOC_FAULT_TARGET;
85#ifdef MODULE_START
86 if (unlikely(address >= MODULE_START && address < MODULE_END))
87 goto VMALLOC_FAULT_TARGET;
88#endif
89
90
91
92
93
94 if (faulthandler_disabled() || !mm)
95 goto bad_area_nosemaphore;
96
97 if (user_mode(regs))
98 flags |= FAULT_FLAG_USER;
99retry:
100 down_read(&mm->mmap_sem);
101 vma = find_vma(mm, address);
102 if (!vma)
103 goto bad_area;
104 if (vma->vm_start <= address)
105 goto good_area;
106 if (!(vma->vm_flags & VM_GROWSDOWN))
107 goto bad_area;
108 if (expand_stack(vma, address))
109 goto bad_area;
110
111
112
113
114good_area:
115 si_code = SEGV_ACCERR;
116
117 if (write) {
118 if (!(vma->vm_flags & VM_WRITE))
119 goto bad_area;
120 flags |= FAULT_FLAG_WRITE;
121 } else {
122 if (cpu_has_rixi) {
123 if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
124#if 0
125 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
126 raw_smp_processor_id(),
127 current->comm, current->pid,
128 field, address, write,
129 field, regs->cp0_epc);
130#endif
131 goto bad_area;
132 }
133 if (!(vma->vm_flags & VM_READ) &&
134 exception_epc(regs) != address) {
135#if 0
136 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
137 raw_smp_processor_id(),
138 current->comm, current->pid,
139 field, address, write,
140 field, regs->cp0_epc);
141#endif
142 goto bad_area;
143 }
144 } else {
145 if (unlikely(!vma_is_accessible(vma)))
146 goto bad_area;
147 }
148 }
149
150
151
152
153
154
155 fault = handle_mm_fault(vma, address, flags);
156
157 if (fault_signal_pending(fault, regs))
158 return;
159
160 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
161 if (unlikely(fault & VM_FAULT_ERROR)) {
162 if (fault & VM_FAULT_OOM)
163 goto out_of_memory;
164 else if (fault & VM_FAULT_SIGSEGV)
165 goto bad_area;
166 else if (fault & VM_FAULT_SIGBUS)
167 goto do_sigbus;
168 BUG();
169 }
170 if (flags & FAULT_FLAG_ALLOW_RETRY) {
171 if (fault & VM_FAULT_MAJOR) {
172 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
173 regs, address);
174 tsk->maj_flt++;
175 } else {
176 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
177 regs, address);
178 tsk->min_flt++;
179 }
180 if (fault & VM_FAULT_RETRY) {
181 flags |= FAULT_FLAG_TRIED;
182
183
184
185
186
187
188
189 goto retry;
190 }
191 }
192
193 up_read(&mm->mmap_sem);
194 return;
195
196
197
198
199
200bad_area:
201 up_read(&mm->mmap_sem);
202
203bad_area_nosemaphore:
204
205 if (user_mode(regs)) {
206 tsk->thread.cp0_badvaddr = address;
207 tsk->thread.error_code = write;
208 if (show_unhandled_signals &&
209 unhandled_signal(tsk, SIGSEGV) &&
210 __ratelimit(&ratelimit_state)) {
211 pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
212 tsk->comm,
213 write ? "write access to" : "read access from",
214 field, address);
215 pr_info("epc = %0*lx in", field,
216 (unsigned long) regs->cp0_epc);
217 print_vma_addr(KERN_CONT " ", regs->cp0_epc);
218 pr_cont("\n");
219 pr_info("ra = %0*lx in", field,
220 (unsigned long) regs->regs[31]);
221 print_vma_addr(KERN_CONT " ", regs->regs[31]);
222 pr_cont("\n");
223 }
224 current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
225 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
226 return;
227 }
228
229no_context:
230
231 if (fixup_exception(regs)) {
232 current->thread.cp0_baduaddr = address;
233 return;
234 }
235
236
237
238
239
240 bust_spinlocks(1);
241
242 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
243 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
244 raw_smp_processor_id(), field, address, field, regs->cp0_epc,
245 field, regs->regs[31]);
246 die("Oops", regs);
247
248out_of_memory:
249
250
251
252
253 up_read(&mm->mmap_sem);
254 if (!user_mode(regs))
255 goto no_context;
256 pagefault_out_of_memory();
257 return;
258
259do_sigbus:
260 up_read(&mm->mmap_sem);
261
262
263 if (!user_mode(regs))
264 goto no_context;
265
266
267
268
269
270#if 0
271 printk("do_page_fault() #3: sending SIGBUS to %s for "
272 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
273 tsk->comm,
274 write ? "write access to" : "read access from",
275 field, address,
276 field, (unsigned long) regs->cp0_epc,
277 field, (unsigned long) regs->regs[31]);
278#endif
279 current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
280 tsk->thread.cp0_badvaddr = address;
281 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
282
283 return;
284#ifndef CONFIG_64BIT
285vmalloc_fault:
286 {
287
288
289
290
291
292
293
294 int offset = pgd_index(address);
295 pgd_t *pgd, *pgd_k;
296 p4d_t *p4d, *p4d_k;
297 pud_t *pud, *pud_k;
298 pmd_t *pmd, *pmd_k;
299 pte_t *pte_k;
300
301 pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
302 pgd_k = init_mm.pgd + offset;
303
304 if (!pgd_present(*pgd_k))
305 goto no_context;
306 set_pgd(pgd, *pgd_k);
307
308 p4d = p4d_offset(pgd, address);
309 p4d_k = p4d_offset(pgd_k, address);
310 if (!p4d_present(*p4d_k))
311 goto no_context;
312
313 pud = pud_offset(p4d, address);
314 pud_k = pud_offset(p4d_k, address);
315 if (!pud_present(*pud_k))
316 goto no_context;
317
318 pmd = pmd_offset(pud, address);
319 pmd_k = pmd_offset(pud_k, address);
320 if (!pmd_present(*pmd_k))
321 goto no_context;
322 set_pmd(pmd, *pmd_k);
323
324 pte_k = pte_offset_kernel(pmd_k, address);
325 if (!pte_present(*pte_k))
326 goto no_context;
327 return;
328 }
329#endif
330}
331
332asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
333 unsigned long write, unsigned long address)
334{
335 enum ctx_state prev_state;
336
337 prev_state = exception_enter();
338 __do_page_fault(regs, write, address);
339 exception_exit(prev_state);
340}
341