]>
Commit | Line | Data |
---|---|---|
0d1fb0a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ea2ba7dc | 2 | /* |
4c9e1385 | 3 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
4 | */ |
5 | ||
4c9e1385 | 6 | #include <linux/mm.h> |
3f07c014 | 7 | #include <linux/sched/signal.h> |
4c9e1385 | 8 | #include <linux/hardirq.h> |
73395a00 | 9 | #include <linux/module.h> |
fbc9f16a | 10 | #include <linux/uaccess.h> |
b17b0153 | 11 | #include <linux/sched/debug.h> |
4c9e1385 | 12 | #include <asm/current.h> |
4c9e1385 | 13 | #include <asm/tlbflush.h> |
37185b33 AV |
14 | #include <arch.h> |
15 | #include <as-layout.h> | |
16 | #include <kern_util.h> | |
17 | #include <os.h> | |
18 | #include <skas.h> | |
1da177e4 | 19 | |
4c9e1385 | 20 | /* |
59fdf91d | 21 | * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by |
4c9e1385 JD |
22 | * segv(). |
23 | */ | |
1d3468a6 | 24 | int handle_page_fault(unsigned long address, unsigned long ip, |
1da177e4 LT |
25 | int is_write, int is_user, int *code_out) |
26 | { | |
27 | struct mm_struct *mm = current->mm; | |
28 | struct vm_area_struct *vma; | |
1da177e4 LT |
29 | pmd_t *pmd; |
30 | pte_t *pte; | |
1da177e4 | 31 | int err = -EFAULT; |
dde16072 | 32 | unsigned int flags = FAULT_FLAG_DEFAULT; |
1da177e4 LT |
33 | |
34 | *code_out = SEGV_MAPERR; | |
fea03cb4 | 35 | |
4c9e1385 | 36 | /* |
70ffdb93 | 37 | * If the fault was with pagefaults disabled, don't take the fault, just |
4c9e1385 JD |
38 | * fail. |
39 | */ | |
70ffdb93 | 40 | if (faulthandler_disabled()) |
fea03cb4 PBG |
41 | goto out_nosemaphore; |
42 | ||
759496ba JW |
43 | if (is_user) |
44 | flags |= FAULT_FLAG_USER; | |
1cefe28f | 45 | retry: |
d8ed45c5 | 46 | mmap_read_lock(mm); |
1da177e4 | 47 | vma = find_vma(mm, address); |
4c9e1385 | 48 | if (!vma) |
1da177e4 | 49 | goto out; |
4c9e1385 | 50 | else if (vma->vm_start <= address) |
1da177e4 | 51 | goto good_area; |
4c9e1385 | 52 | else if (!(vma->vm_flags & VM_GROWSDOWN)) |
1da177e4 | 53 | goto out; |
4c9e1385 | 54 | else if (is_user && !ARCH_IS_STACKGROW(address)) |
1da177e4 | 55 | goto out; |
4c9e1385 | 56 | else if (expand_stack(vma, address)) |
1da177e4 LT |
57 | goto out; |
58 | ||
3b52166c | 59 | good_area: |
1da177e4 | 60 | *code_out = SEGV_ACCERR; |
759496ba JW |
61 | if (is_write) { |
62 | if (!(vma->vm_flags & VM_WRITE)) | |
63 | goto out; | |
64 | flags |= FAULT_FLAG_WRITE; | |
65 | } else { | |
66 | /* Don't require VM_READ|VM_EXEC for write faults! */ | |
67 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) | |
68 | goto out; | |
69 | } | |
13479d52 | 70 | |
1da177e4 | 71 | do { |
50a7ca3c | 72 | vm_fault_t fault; |
1c0fe6e3 | 73 | |
dcddffd4 | 74 | fault = handle_mm_fault(vma, address, flags); |
1cefe28f KC |
75 | |
76 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) | |
77 | goto out_nosemaphore; | |
78 | ||
83c54070 NP |
79 | if (unlikely(fault & VM_FAULT_ERROR)) { |
80 | if (fault & VM_FAULT_OOM) { | |
83c54070 | 81 | goto out_of_memory; |
33692f27 LT |
82 | } else if (fault & VM_FAULT_SIGSEGV) { |
83 | goto out; | |
83c54070 NP |
84 | } else if (fault & VM_FAULT_SIGBUS) { |
85 | err = -EACCES; | |
86 | goto out; | |
87 | } | |
1da177e4 LT |
88 | BUG(); |
89 | } | |
1cefe28f KC |
90 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
91 | if (fault & VM_FAULT_MAJOR) | |
92 | current->maj_flt++; | |
93 | else | |
94 | current->min_flt++; | |
95 | if (fault & VM_FAULT_RETRY) { | |
45cac65b | 96 | flags |= FAULT_FLAG_TRIED; |
1cefe28f KC |
97 | |
98 | goto retry; | |
99 | } | |
100 | } | |
83c54070 | 101 | |
e05c7b1f | 102 | pmd = pmd_off(mm, address); |
3b52166c | 103 | pte = pte_offset_kernel(pmd, address); |
4c9e1385 | 104 | } while (!pte_present(*pte)); |
1da177e4 | 105 | err = 0; |
4c9e1385 JD |
106 | /* |
107 | * The below warning was added in place of | |
cbc24afa PBG |
108 | * pte_mkyoung(); if (is_write) pte_mkdirty(); |
109 | * If it's triggered, we'd see normally a hang here (a clean pte is | |
110 | * marked read-only to emulate the dirty bit). | |
111 | * However, the generic code can mark a PTE writable but clean on a | |
112 | * concurrent read fault, triggering this harmlessly. So comment it out. | |
113 | */ | |
114 | #if 0 | |
16b03678 | 115 | WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte))); |
cbc24afa | 116 | #endif |
3b52166c PBG |
117 | flush_tlb_page(vma, address); |
118 | out: | |
d8ed45c5 | 119 | mmap_read_unlock(mm); |
fea03cb4 | 120 | out_nosemaphore: |
4c9e1385 | 121 | return err; |
1da177e4 | 122 | |
1da177e4 | 123 | out_of_memory: |
1c0fe6e3 NP |
124 | /* |
125 | * We ran out of memory, call the OOM killer, and return the userspace | |
126 | * (which will retry the fault, or kill us if we got oom-killed). | |
127 | */ | |
d8ed45c5 | 128 | mmap_read_unlock(mm); |
87134102 JW |
129 | if (!is_user) |
130 | goto out_nosemaphore; | |
1c0fe6e3 NP |
131 | pagefault_out_of_memory(); |
132 | return 0; | |
1da177e4 | 133 | } |
73395a00 | 134 | EXPORT_SYMBOL(handle_page_fault); |
1da177e4 | 135 | |
3ef6130a RW |
136 | static void show_segv_info(struct uml_pt_regs *regs) |
137 | { | |
138 | struct task_struct *tsk = current; | |
139 | struct faultinfo *fi = UPT_FAULTINFO(regs); | |
140 | ||
141 | if (!unhandled_signal(tsk, SIGSEGV)) | |
142 | return; | |
143 | ||
144 | if (!printk_ratelimit()) | |
145 | return; | |
146 | ||
10a7e9d8 | 147 | printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x", |
3ef6130a RW |
148 | task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, |
149 | tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi), | |
150 | (void *)UPT_IP(regs), (void *)UPT_SP(regs), | |
151 | fi->error_code); | |
152 | ||
153 | print_vma_addr(KERN_CONT " in ", UPT_IP(regs)); | |
154 | printk(KERN_CONT "\n"); | |
155 | } | |
156 | ||
27aa6ef3 JD |
157 | static void bad_segv(struct faultinfo fi, unsigned long ip) |
158 | { | |
27aa6ef3 | 159 | current->thread.arch.faultinfo = fi; |
2e1661d2 | 160 | force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi)); |
27aa6ef3 JD |
161 | } |
162 | ||
3e6f2ac4 JD |
163 | void fatal_sigsegv(void) |
164 | { | |
cb44c9a0 | 165 | force_sigsegv(SIGSEGV); |
ccaee5f8 | 166 | do_signal(¤t->thread.regs); |
3e6f2ac4 JD |
167 | /* |
168 | * This is to tell gcc that we're not returning - do_signal | |
169 | * can, in general, return, but in this case, it's not, since | |
170 | * we just got a fatal SIGSEGV queued. | |
171 | */ | |
172 | os_dump_core(); | |
173 | } | |
174 | ||
88af2338 TM |
175 | /** |
176 | * segv_handler() - the SIGSEGV handler | |
177 | * @sig: the signal number | |
178 | * @unused_si: the signal info struct; unused in this handler | |
179 | * @regs: the ptrace register information | |
180 | * | |
181 | * The handler first extracts the faultinfo from the UML ptrace regs struct. | |
182 | * If the userfault did not happen in an UML userspace process, bad_segv is called. | |
183 | * Otherwise the signal did happen in a cloned userspace process, handle it. | |
184 | */ | |
d3c1cfcd | 185 | void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs) |
c66fdd5e GS |
186 | { |
187 | struct faultinfo * fi = UPT_FAULTINFO(regs); | |
188 | ||
4c9e1385 | 189 | if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) { |
3ef6130a | 190 | show_segv_info(regs); |
c66fdd5e GS |
191 | bad_segv(*fi, UPT_IP(regs)); |
192 | return; | |
193 | } | |
194 | segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs); | |
195 | } | |
196 | ||
c578455a BS |
197 | /* |
198 | * We give a *copy* of the faultinfo in the regs to segv. | |
199 | * This must be done, since nesting SEGVs could overwrite | |
200 | * the info in the regs. A pointer to the info then would | |
201 | * give us bad data! | |
202 | */ | |
5d86456d | 203 | unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, |
77bf4400 | 204 | struct uml_pt_regs *regs) |
1da177e4 | 205 | { |
fab95c55 | 206 | jmp_buf *catcher; |
bc08c078 | 207 | int si_code; |
1da177e4 | 208 | int err; |
5d86456d JD |
209 | int is_write = FAULT_WRITE(fi); |
210 | unsigned long address = FAULT_ADDRESS(fi); | |
1da177e4 | 211 | |
bb6a1b2e | 212 | if (!is_user && regs) |
f72c22e4 RW |
213 | current->thread.segv_regs = container_of(regs, struct pt_regs, regs); |
214 | ||
4c9e1385 | 215 | if (!is_user && (address >= start_vm) && (address < end_vm)) { |
5d86456d | 216 | flush_tlb_kernel_vm(); |
f72c22e4 | 217 | goto out; |
5d86456d | 218 | } |
4c9e1385 | 219 | else if (current->mm == NULL) { |
377fad3a | 220 | show_regs(container_of(regs, struct pt_regs, regs)); |
4c9e1385 | 221 | panic("Segfault with no mm"); |
377fad3a | 222 | } |
56b88a3b | 223 | else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) { |
d2313084 RW |
224 | show_regs(container_of(regs, struct pt_regs, regs)); |
225 | panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx", | |
226 | address, ip); | |
227 | } | |
546fe1cb | 228 | |
d0b5e15f | 229 | if (SEGV_IS_FIXABLE(&fi)) |
4c9e1385 | 230 | err = handle_page_fault(address, ip, is_write, is_user, |
bc08c078 | 231 | &si_code); |
546fe1cb PBG |
232 | else { |
233 | err = -EFAULT; | |
4c9e1385 JD |
234 | /* |
235 | * A thread accessed NULL, we get a fault, but CR2 is invalid. | |
236 | * This code is used in __do_copy_from_user() of TT mode. | |
237 | * XXX tt mode is gone, so maybe this isn't needed any more | |
238 | */ | |
546fe1cb PBG |
239 | address = 0; |
240 | } | |
1da177e4 LT |
241 | |
242 | catcher = current->thread.fault_catcher; | |
4c9e1385 | 243 | if (!err) |
f72c22e4 | 244 | goto out; |
4c9e1385 | 245 | else if (catcher != NULL) { |
1da177e4 | 246 | current->thread.fault_addr = (void *) address; |
fab95c55 | 247 | UML_LONGJMP(catcher, 1); |
1d3468a6 | 248 | } |
4c9e1385 | 249 | else if (current->thread.fault_addr != NULL) |
1da177e4 | 250 | panic("fault_addr set but no fault catcher"); |
4c9e1385 | 251 | else if (!is_user && arch_fixup(ip, regs)) |
f72c22e4 | 252 | goto out; |
1da177e4 | 253 | |
4c9e1385 | 254 | if (!is_user) { |
377fad3a | 255 | show_regs(container_of(regs, struct pt_regs, regs)); |
1d3468a6 | 256 | panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", |
1da177e4 | 257 | address, ip); |
377fad3a | 258 | } |
1da177e4 | 259 | |
3ef6130a RW |
260 | show_segv_info(regs); |
261 | ||
3b52166c | 262 | if (err == -EACCES) { |
5d86456d | 263 | current->thread.arch.faultinfo = fi; |
2e1661d2 | 264 | force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); |
3b52166c PBG |
265 | } else { |
266 | BUG_ON(err != -EFAULT); | |
5d86456d | 267 | current->thread.arch.faultinfo = fi; |
2e1661d2 | 268 | force_sig_fault(SIGSEGV, si_code, (void __user *) address); |
1da177e4 | 269 | } |
f72c22e4 RW |
270 | |
271 | out: | |
272 | if (regs) | |
273 | current->thread.segv_regs = NULL; | |
274 | ||
5d86456d | 275 | return 0; |
1da177e4 LT |
276 | } |
277 | ||
d3c1cfcd | 278 | void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs) |
1da177e4 | 279 | { |
530621b7 | 280 | int code, err; |
4c9e1385 JD |
281 | if (!UPT_IS_USER(regs)) { |
282 | if (sig == SIGBUS) | |
283 | printk(KERN_ERR "Bus error - the host /dev/shm or /tmp " | |
284 | "mount likely just ran out of space\n"); | |
1da177e4 | 285 | panic("Kernel mode signal %d", sig); |
6edf428e JD |
286 | } |
287 | ||
9226b838 JD |
288 | arch_examine_signal(sig, regs); |
289 | ||
530621b7 EB |
290 | /* Is the signal layout for the signal known? |
291 | * Signal data must be scrubbed to prevent information leaks. | |
292 | */ | |
293 | code = si->si_code; | |
294 | err = si->si_errno; | |
295 | if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) { | |
296 | struct faultinfo *fi = UPT_FAULTINFO(regs); | |
d3c1cfcd | 297 | current->thread.arch.faultinfo = *fi; |
2e1661d2 | 298 | force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi)); |
530621b7 EB |
299 | } else { |
300 | printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n", | |
301 | sig, code, err); | |
3cf5d076 | 302 | force_sig(sig); |
d3c1cfcd | 303 | } |
1da177e4 LT |
304 | } |
305 | ||
d3c1cfcd | 306 | void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs) |
1da177e4 | 307 | { |
4c9e1385 | 308 | if (current->thread.fault_catcher != NULL) |
fab95c55 | 309 | UML_LONGJMP(current->thread.fault_catcher, 1); |
d3c1cfcd MP |
310 | else |
311 | relay_signal(sig, si, regs); | |
1da177e4 LT |
312 | } |
313 | ||
d3c1cfcd | 314 | void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs) |
1da177e4 LT |
315 | { |
316 | do_IRQ(WINCH_IRQ, regs); | |
317 | } | |
318 | ||
319 | void trap_init(void) | |
320 | { | |
321 | } |