xref: /linux/arch/um/kernel/trap.c (revision 5c48b108ecbf6505d929e64d50dace13ac2bdf34)
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #include <linux/mm.h>
7 #include <linux/sched.h>
8 #include <linux/hardirq.h>
9 #include <asm/current.h>
10 #include <asm/pgtable.h>
11 #include <asm/tlbflush.h>
12 #include "arch.h"
13 #include "as-layout.h"
14 #include "kern_util.h"
15 #include "os.h"
16 #include "skas.h"
17 
18 /*
19  * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
20  * segv().
21  */
22 int handle_page_fault(unsigned long address, unsigned long ip,
23 		      int is_write, int is_user, int *code_out)
24 {
25 	struct mm_struct *mm = current->mm;
26 	struct vm_area_struct *vma;
27 	pgd_t *pgd;
28 	pud_t *pud;
29 	pmd_t *pmd;
30 	pte_t *pte;
31 	int err = -EFAULT;
32 
33 	*code_out = SEGV_MAPERR;
34 
35 	/*
36 	 * If the fault was during atomic operation, don't take the fault, just
37 	 * fail.
38 	 */
39 	if (in_atomic())
40 		goto out_nosemaphore;
41 
42 	down_read(&mm->mmap_sem);
43 	vma = find_vma(mm, address);
44 	if (!vma)
45 		goto out;
46 	else if (vma->vm_start <= address)
47 		goto good_area;
48 	else if (!(vma->vm_flags & VM_GROWSDOWN))
49 		goto out;
50 	else if (is_user && !ARCH_IS_STACKGROW(address))
51 		goto out;
52 	else if (expand_stack(vma, address))
53 		goto out;
54 
55 good_area:
56 	*code_out = SEGV_ACCERR;
57 	if (is_write && !(vma->vm_flags & VM_WRITE))
58 		goto out;
59 
60 	/* Don't require VM_READ|VM_EXEC for write faults! */
61 	if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
62 		goto out;
63 
64 	do {
65 		int fault;
66 
67 		fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
68 		if (unlikely(fault & VM_FAULT_ERROR)) {
69 			if (fault & VM_FAULT_OOM) {
70 				goto out_of_memory;
71 			} else if (fault & VM_FAULT_SIGBUS) {
72 				err = -EACCES;
73 				goto out;
74 			}
75 			BUG();
76 		}
77 		if (fault & VM_FAULT_MAJOR)
78 			current->maj_flt++;
79 		else
80 			current->min_flt++;
81 
82 		pgd = pgd_offset(mm, address);
83 		pud = pud_offset(pgd, address);
84 		pmd = pmd_offset(pud, address);
85 		pte = pte_offset_kernel(pmd, address);
86 	} while (!pte_present(*pte));
87 	err = 0;
88 	/*
89 	 * The below warning was added in place of
90 	 *	pte_mkyoung(); if (is_write) pte_mkdirty();
91 	 * If it's triggered, we'd see normally a hang here (a clean pte is
92 	 * marked read-only to emulate the dirty bit).
93 	 * However, the generic code can mark a PTE writable but clean on a
94 	 * concurrent read fault, triggering this harmlessly. So comment it out.
95 	 */
96 #if 0
97 	WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
98 #endif
99 	flush_tlb_page(vma, address);
100 out:
101 	up_read(&mm->mmap_sem);
102 out_nosemaphore:
103 	return err;
104 
105 out_of_memory:
106 	/*
107 	 * We ran out of memory, call the OOM killer, and return the userspace
108 	 * (which will retry the fault, or kill us if we got oom-killed).
109 	 */
110 	up_read(&mm->mmap_sem);
111 	pagefault_out_of_memory();
112 	return 0;
113 }
114 
115 static void show_segv_info(struct uml_pt_regs *regs)
116 {
117 	struct task_struct *tsk = current;
118 	struct faultinfo *fi = UPT_FAULTINFO(regs);
119 
120 	if (!unhandled_signal(tsk, SIGSEGV))
121 		return;
122 
123 	if (!printk_ratelimit())
124 		return;
125 
126 	printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
127 		task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
128 		tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
129 		(void *)UPT_IP(regs), (void *)UPT_SP(regs),
130 		fi->error_code);
131 
132 	print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
133 	printk(KERN_CONT "\n");
134 }
135 
136 static void bad_segv(struct faultinfo fi, unsigned long ip)
137 {
138 	struct siginfo si;
139 
140 	si.si_signo = SIGSEGV;
141 	si.si_code = SEGV_ACCERR;
142 	si.si_addr = (void __user *) FAULT_ADDRESS(fi);
143 	current->thread.arch.faultinfo = fi;
144 	force_sig_info(SIGSEGV, &si, current);
145 }
146 
147 void fatal_sigsegv(void)
148 {
149 	force_sigsegv(SIGSEGV, current);
150 	do_signal();
151 	/*
152 	 * This is to tell gcc that we're not returning - do_signal
153 	 * can, in general, return, but in this case, it's not, since
154 	 * we just got a fatal SIGSEGV queued.
155 	 */
156 	os_dump_core();
157 }
158 
159 void segv_handler(int sig, struct uml_pt_regs *regs)
160 {
161 	struct faultinfo * fi = UPT_FAULTINFO(regs);
162 
163 	if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
164 		show_segv_info(regs);
165 		bad_segv(*fi, UPT_IP(regs));
166 		return;
167 	}
168 	segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
169 }
170 
171 /*
172  * We give a *copy* of the faultinfo in the regs to segv.
173  * This must be done, since nesting SEGVs could overwrite
174  * the info in the regs. A pointer to the info then would
175  * give us bad data!
176  */
177 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
178 		   struct uml_pt_regs *regs)
179 {
180 	struct siginfo si;
181 	jmp_buf *catcher;
182 	int err;
183 	int is_write = FAULT_WRITE(fi);
184 	unsigned long address = FAULT_ADDRESS(fi);
185 
186 	if (!is_user && (address >= start_vm) && (address < end_vm)) {
187 		flush_tlb_kernel_vm();
188 		return 0;
189 	}
190 	else if (current->mm == NULL) {
191 		show_regs(container_of(regs, struct pt_regs, regs));
192 		panic("Segfault with no mm");
193 	}
194 
195 	if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
196 		err = handle_page_fault(address, ip, is_write, is_user,
197 					&si.si_code);
198 	else {
199 		err = -EFAULT;
200 		/*
201 		 * A thread accessed NULL, we get a fault, but CR2 is invalid.
202 		 * This code is used in __do_copy_from_user() of TT mode.
203 		 * XXX tt mode is gone, so maybe this isn't needed any more
204 		 */
205 		address = 0;
206 	}
207 
208 	catcher = current->thread.fault_catcher;
209 	if (!err)
210 		return 0;
211 	else if (catcher != NULL) {
212 		current->thread.fault_addr = (void *) address;
213 		UML_LONGJMP(catcher, 1);
214 	}
215 	else if (current->thread.fault_addr != NULL)
216 		panic("fault_addr set but no fault catcher");
217 	else if (!is_user && arch_fixup(ip, regs))
218 		return 0;
219 
220 	if (!is_user) {
221 		show_regs(container_of(regs, struct pt_regs, regs));
222 		panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
223 		      address, ip);
224 	}
225 
226 	show_segv_info(regs);
227 
228 	if (err == -EACCES) {
229 		si.si_signo = SIGBUS;
230 		si.si_errno = 0;
231 		si.si_code = BUS_ADRERR;
232 		si.si_addr = (void __user *)address;
233 		current->thread.arch.faultinfo = fi;
234 		force_sig_info(SIGBUS, &si, current);
235 	} else {
236 		BUG_ON(err != -EFAULT);
237 		si.si_signo = SIGSEGV;
238 		si.si_addr = (void __user *) address;
239 		current->thread.arch.faultinfo = fi;
240 		force_sig_info(SIGSEGV, &si, current);
241 	}
242 	return 0;
243 }
244 
245 void relay_signal(int sig, struct uml_pt_regs *regs)
246 {
247 	if (!UPT_IS_USER(regs)) {
248 		if (sig == SIGBUS)
249 			printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
250 			       "mount likely just ran out of space\n");
251 		panic("Kernel mode signal %d", sig);
252 	}
253 
254 	arch_examine_signal(sig, regs);
255 
256 	current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
257 	force_sig(sig, current);
258 }
259 
260 void bus_handler(int sig, struct uml_pt_regs *regs)
261 {
262 	if (current->thread.fault_catcher != NULL)
263 		UML_LONGJMP(current->thread.fault_catcher, 1);
264 	else relay_signal(sig, regs);
265 }
266 
267 void winch(int sig, struct uml_pt_regs *regs)
268 {
269 	do_IRQ(WINCH_IRQ, regs);
270 }
271 
272 void trap_init(void)
273 {
274 }
275