1
2
3
4
5
6
7
8
9
10
11
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/pagemap.h>
15#include <linux/kdebug.h>
16#include <linux/kprobes.h>
17
18#include <asm/mmu_context.h>
19#include <asm/sysreg.h>
20#include <asm/tlb.h>
21#include <asm/uaccess.h>
22
23#ifdef CONFIG_KPROBES
24static inline int notify_page_fault(struct pt_regs *regs, int trap)
25{
26 int ret = 0;
27
28 if (!user_mode(regs)) {
29 if (kprobe_running() && kprobe_fault_handler(regs, trap))
30 ret = 1;
31 }
32
33 return ret;
34}
35#else
36static inline int notify_page_fault(struct pt_regs *regs, int trap)
37{
38 return 0;
39}
40#endif
41
42int exception_trace = 1;
43
44
45
46
47
48
49
50
51
52
53
54
55
56asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
57{
58 struct task_struct *tsk;
59 struct mm_struct *mm;
60 struct vm_area_struct *vma;
61 const struct exception_table_entry *fixup;
62 unsigned long address;
63 unsigned long page;
64 int writeaccess;
65 long signr;
66 int code;
67 int fault;
68
69 if (notify_page_fault(regs, ecr))
70 return;
71
72 address = sysreg_read(TLBEAR);
73
74 tsk = current;
75 mm = tsk->mm;
76
77 signr = SIGSEGV;
78 code = SEGV_MAPERR;
79
80
81
82
83
84 if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
85 goto no_context;
86
87 local_irq_enable();
88
89 down_read(&mm->mmap_sem);
90
91 vma = find_vma(mm, address);
92 if (!vma)
93 goto bad_area;
94 if (vma->vm_start <= address)
95 goto good_area;
96 if (!(vma->vm_flags & VM_GROWSDOWN))
97 goto bad_area;
98 if (expand_stack(vma, address))
99 goto bad_area;
100
101
102
103
104
105good_area:
106 code = SEGV_ACCERR;
107 writeaccess = 0;
108
109 switch (ecr) {
110 case ECR_PROTECTION_X:
111 case ECR_TLB_MISS_X:
112 if (!(vma->vm_flags & VM_EXEC))
113 goto bad_area;
114 break;
115 case ECR_PROTECTION_R:
116 case ECR_TLB_MISS_R:
117 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
118 goto bad_area;
119 break;
120 case ECR_PROTECTION_W:
121 case ECR_TLB_MISS_W:
122 if (!(vma->vm_flags & VM_WRITE))
123 goto bad_area;
124 writeaccess = 1;
125 break;
126 default:
127 panic("Unhandled case %lu in do_page_fault!", ecr);
128 }
129
130
131
132
133
134
135survive:
136 fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
137 if (unlikely(fault & VM_FAULT_ERROR)) {
138 if (fault & VM_FAULT_OOM)
139 goto out_of_memory;
140 else if (fault & VM_FAULT_SIGBUS)
141 goto do_sigbus;
142 BUG();
143 }
144 if (fault & VM_FAULT_MAJOR)
145 tsk->maj_flt++;
146 else
147 tsk->min_flt++;
148
149 up_read(&mm->mmap_sem);
150 return;
151
152
153
154
155
156bad_area:
157 up_read(&mm->mmap_sem);
158
159 if (user_mode(regs)) {
160 if (exception_trace && printk_ratelimit())
161 printk("%s%s[%d]: segfault at %08lx pc %08lx "
162 "sp %08lx ecr %lu\n",
163 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
164 tsk->comm, tsk->pid, address, regs->pc,
165 regs->sp, ecr);
166 _exception(SIGSEGV, regs, code, address);
167 return;
168 }
169
170no_context:
171
172 fixup = search_exception_tables(regs->pc);
173 if (fixup) {
174 regs->pc = fixup->fixup;
175 return;
176 }
177
178
179
180
181
182 if (address < PAGE_SIZE)
183 printk(KERN_ALERT
184 "Unable to handle kernel NULL pointer dereference");
185 else
186 printk(KERN_ALERT
187 "Unable to handle kernel paging request");
188 printk(" at virtual address %08lx\n", address);
189
190 page = sysreg_read(PTBR);
191 printk(KERN_ALERT "ptbr = %08lx", page);
192 if (address >= TASK_SIZE)
193 page = (unsigned long)swapper_pg_dir;
194 if (page) {
195 page = ((unsigned long *)page)[address >> 22];
196 printk(" pgd = %08lx", page);
197 if (page & _PAGE_PRESENT) {
198 page &= PAGE_MASK;
199 address &= 0x003ff000;
200 page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
201 printk(" pte = %08lx", page);
202 }
203 }
204 printk("\n");
205 die("Kernel access of bad area", regs, signr);
206 return;
207
208
209
210
211
212out_of_memory:
213 up_read(&mm->mmap_sem);
214 if (is_global_init(current)) {
215 yield();
216 down_read(&mm->mmap_sem);
217 goto survive;
218 }
219 printk("VM: Killing process %s\n", tsk->comm);
220 if (user_mode(regs))
221 do_group_exit(SIGKILL);
222 goto no_context;
223
224do_sigbus:
225 up_read(&mm->mmap_sem);
226
227
228 signr = SIGBUS;
229 code = BUS_ADRERR;
230 if (!user_mode(regs))
231 goto no_context;
232
233 if (exception_trace)
234 printk("%s%s[%d]: bus error at %08lx pc %08lx "
235 "sp %08lx ecr %lu\n",
236 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
237 tsk->comm, tsk->pid, address, regs->pc,
238 regs->sp, ecr);
239
240 _exception(SIGBUS, regs, BUS_ADRERR, address);
241}
242
243asmlinkage void do_bus_error(unsigned long addr, int write_access,
244 struct pt_regs *regs)
245{
246 printk(KERN_ALERT
247 "Bus error at physical address 0x%08lx (%s access)\n",
248 addr, write_access ? "write" : "read");
249 printk(KERN_INFO "DTLB dump:\n");
250 dump_dtlb();
251 die("Bus Error", regs, SIGKILL);
252}
253