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