blob: c7725f1de05d03868fd04edea558f671c6a8ad55 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68k/mm/fault.c
3 *
4 * Copyright (C) 1995 Hamish Macdonald
5 */
6
7#include <linux/mman.h>
8#include <linux/mm.h>
9#include <linux/kernel.h>
10#include <linux/ptrace.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13
14#include <asm/setup.h>
15#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/pgalloc.h>
18
19extern void die_if_kernel(char *, struct pt_regs *, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21int send_fault_sig(struct pt_regs *regs)
22{
23 siginfo_t siginfo = { 0, 0, 0, };
24
25 siginfo.si_signo = current->thread.signo;
26 siginfo.si_code = current->thread.code;
27 siginfo.si_addr = (void *)current->thread.faddr;
28#ifdef DEBUG
29 printk("send_fault_sig: %p,%d,%d\n", siginfo.si_addr, siginfo.si_signo, siginfo.si_code);
30#endif
31
32 if (user_mode(regs)) {
33 force_sig_info(siginfo.si_signo,
34 &siginfo, current);
35 } else {
Roman Zippeldcdf3a22008-11-18 21:25:17 +010036 if (handle_kernel_fault(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 //if (siginfo.si_signo == SIGBUS)
40 // force_sig_info(siginfo.si_signo,
41 // &siginfo, current);
42
43 /*
44 * Oops. The kernel tried to access some bad page. We'll have to
45 * terminate things with extreme prejudice.
46 */
47 if ((unsigned long)siginfo.si_addr < PAGE_SIZE)
48 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
49 else
50 printk(KERN_ALERT "Unable to handle kernel access");
51 printk(" at virtual address %p\n", siginfo.si_addr);
52 die_if_kernel("Oops", regs, 0 /*error_code*/);
53 do_exit(SIGKILL);
54 }
55
56 return 1;
57}
58
59/*
60 * This routine handles page faults. It determines the problem, and
61 * then passes it off to one of the appropriate routines.
62 *
63 * error_code:
64 * bit 0 == 0 means no page found, 1 means protection fault
65 * bit 1 == 0 means read, 1 means write
66 *
67 * If this routine detects a bad access, it returns 1, otherwise it
68 * returns 0.
69 */
70int do_page_fault(struct pt_regs *regs, unsigned long address,
71 unsigned long error_code)
72{
73 struct mm_struct *mm = current->mm;
74 struct vm_area_struct * vma;
Kautuk Consulb637a6b2012-03-25 06:37:48 -040075 int fault;
76 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#ifdef DEBUG
79 printk ("do page fault:\nregs->sr=%#x, regs->pc=%#lx, address=%#lx, %ld, %p\n",
Geert Uytterhoeven4e25c0e2013-12-10 11:35:47 +010080 regs->sr, regs->pc, address, error_code, mm ? mm->pgd : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83 /*
84 * If we're in an interrupt or have no user
85 * context, we must not take the fault..
86 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -080087 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto no_context;
89
Johannes Weiner759496b2013-09-12 15:13:39 -070090 if (user_mode(regs))
91 flags |= FAULT_FLAG_USER;
Kautuk Consulb637a6b2012-03-25 06:37:48 -040092retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 down_read(&mm->mmap_sem);
94
95 vma = find_vma(mm, address);
96 if (!vma)
97 goto map_err;
98 if (vma->vm_flags & VM_IO)
99 goto acc_err;
100 if (vma->vm_start <= address)
101 goto good_area;
102 if (!(vma->vm_flags & VM_GROWSDOWN))
103 goto map_err;
104 if (user_mode(regs)) {
105 /* Accessing the stack below usp is always a bug. The
106 "+ 256" is there due to some instructions doing
107 pre-decrement on the stack and that doesn't show up
108 until later. */
109 if (address + 256 < rdusp())
110 goto map_err;
111 }
112 if (expand_stack(vma, address))
113 goto map_err;
114
115/*
116 * Ok, we have a good vm_area for this memory access, so
117 * we can handle it..
118 */
119good_area:
120#ifdef DEBUG
121 printk("do_page_fault: good_area\n");
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 switch (error_code & 3) {
124 default: /* 3: write, present */
125 /* fall through */
126 case 2: /* write, not present */
127 if (!(vma->vm_flags & VM_WRITE))
128 goto acc_err;
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400129 flags |= FAULT_FLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 case 1: /* read, present */
132 goto acc_err;
133 case 0: /* read, not present */
Jason Barondf67b3d2006-09-29 01:58:58 -0700134 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto acc_err;
136 }
137
138 /*
139 * If for any reason at all we couldn't handle the fault,
140 * make sure we exit gracefully rather than endlessly redo
141 * the fault.
142 */
143
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400144 fault = handle_mm_fault(mm, vma, address, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifdef DEBUG
146 printk("handle_mm_fault returns %d\n",fault);
147#endif
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400148
149 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
150 return 0;
151
Nick Piggin83c54072007-07-19 01:47:05 -0700152 if (unlikely(fault & VM_FAULT_ERROR)) {
153 if (fault & VM_FAULT_OOM)
154 goto out_of_memory;
155 else if (fault & VM_FAULT_SIGBUS)
156 goto bus_err;
157 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400159
160 /*
161 * Major/minor page fault accounting is only done on the
162 * initial attempt. If we go through a retry, it is extremely
163 * likely that the page will be found in page cache at that point.
164 */
165 if (flags & FAULT_FLAG_ALLOW_RETRY) {
166 if (fault & VM_FAULT_MAJOR)
167 current->maj_flt++;
168 else
169 current->min_flt++;
170 if (fault & VM_FAULT_RETRY) {
171 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
172 * of starvation. */
173 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700174 flags |= FAULT_FLAG_TRIED;
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400175
176 /*
177 * No need to up_read(&mm->mmap_sem) as we would
178 * have already released it in __lock_page_or_retry
179 * in mm/filemap.c.
180 */
181
182 goto retry;
183 }
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 up_read(&mm->mmap_sem);
187 return 0;
188
189/*
190 * We ran out of memory, or some other thing happened to us that made
191 * us unable to handle the page fault gracefully.
192 */
193out_of_memory:
194 up_read(&mm->mmap_sem);
Nick Pigginadbf6e62010-04-23 02:06:20 +1000195 if (!user_mode(regs))
196 goto no_context;
197 pagefault_out_of_memory();
198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200no_context:
201 current->thread.signo = SIGBUS;
202 current->thread.faddr = address;
203 return send_fault_sig(regs);
204
205bus_err:
206 current->thread.signo = SIGBUS;
207 current->thread.code = BUS_ADRERR;
208 current->thread.faddr = address;
209 goto send_sig;
210
211map_err:
212 current->thread.signo = SIGSEGV;
213 current->thread.code = SEGV_MAPERR;
214 current->thread.faddr = address;
215 goto send_sig;
216
217acc_err:
218 current->thread.signo = SIGSEGV;
219 current->thread.code = SEGV_ACCERR;
220 current->thread.faddr = address;
221
222send_sig:
223 up_read(&mm->mmap_sem);
224 return send_fault_sig(regs);
225}