blob: a563727806bf922b5b3315559b9b46b5bbb9d7fc [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",
80 regs->sr, regs->pc, address, error_code,
81 current->mm->pgd);
82#endif
83
84 /*
85 * If we're in an interrupt or have no user
86 * context, we must not take the fault..
87 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -080088 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 goto no_context;
90
Kautuk Consulb637a6b2012-03-25 06:37:48 -040091retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 down_read(&mm->mmap_sem);
93
94 vma = find_vma(mm, address);
95 if (!vma)
96 goto map_err;
97 if (vma->vm_flags & VM_IO)
98 goto acc_err;
99 if (vma->vm_start <= address)
100 goto good_area;
101 if (!(vma->vm_flags & VM_GROWSDOWN))
102 goto map_err;
103 if (user_mode(regs)) {
104 /* Accessing the stack below usp is always a bug. The
105 "+ 256" is there due to some instructions doing
106 pre-decrement on the stack and that doesn't show up
107 until later. */
108 if (address + 256 < rdusp())
109 goto map_err;
110 }
111 if (expand_stack(vma, address))
112 goto map_err;
113
114/*
115 * Ok, we have a good vm_area for this memory access, so
116 * we can handle it..
117 */
118good_area:
119#ifdef DEBUG
120 printk("do_page_fault: good_area\n");
121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 switch (error_code & 3) {
123 default: /* 3: write, present */
124 /* fall through */
125 case 2: /* write, not present */
126 if (!(vma->vm_flags & VM_WRITE))
127 goto acc_err;
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400128 flags |= FAULT_FLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130 case 1: /* read, present */
131 goto acc_err;
132 case 0: /* read, not present */
Jason Barondf67b3d2006-09-29 01:58:58 -0700133 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto acc_err;
135 }
136
137 /*
138 * If for any reason at all we couldn't handle the fault,
139 * make sure we exit gracefully rather than endlessly redo
140 * the fault.
141 */
142
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400143 fault = handle_mm_fault(mm, vma, address, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#ifdef DEBUG
145 printk("handle_mm_fault returns %d\n",fault);
146#endif
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400147
148 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
149 return 0;
150
Nick Piggin83c54072007-07-19 01:47:05 -0700151 if (unlikely(fault & VM_FAULT_ERROR)) {
152 if (fault & VM_FAULT_OOM)
153 goto out_of_memory;
154 else if (fault & VM_FAULT_SIGBUS)
155 goto bus_err;
156 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400158
159 /*
160 * Major/minor page fault accounting is only done on the
161 * initial attempt. If we go through a retry, it is extremely
162 * likely that the page will be found in page cache at that point.
163 */
164 if (flags & FAULT_FLAG_ALLOW_RETRY) {
165 if (fault & VM_FAULT_MAJOR)
166 current->maj_flt++;
167 else
168 current->min_flt++;
169 if (fault & VM_FAULT_RETRY) {
170 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
171 * of starvation. */
172 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700173 flags |= FAULT_FLAG_TRIED;
Kautuk Consulb637a6b2012-03-25 06:37:48 -0400174
175 /*
176 * No need to up_read(&mm->mmap_sem) as we would
177 * have already released it in __lock_page_or_retry
178 * in mm/filemap.c.
179 */
180
181 goto retry;
182 }
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 up_read(&mm->mmap_sem);
186 return 0;
187
188/*
189 * We ran out of memory, or some other thing happened to us that made
190 * us unable to handle the page fault gracefully.
191 */
192out_of_memory:
193 up_read(&mm->mmap_sem);
Nick Pigginadbf6e62010-04-23 02:06:20 +1000194 if (!user_mode(regs))
195 goto no_context;
196 pagefault_out_of_memory();
197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199no_context:
200 current->thread.signo = SIGBUS;
201 current->thread.faddr = address;
202 return send_fault_sig(regs);
203
204bus_err:
205 current->thread.signo = SIGBUS;
206 current->thread.code = BUS_ADRERR;
207 current->thread.faddr = address;
208 goto send_sig;
209
210map_err:
211 current->thread.signo = SIGSEGV;
212 current->thread.code = SEGV_MAPERR;
213 current->thread.faddr = address;
214 goto send_sig;
215
216acc_err:
217 current->thread.signo = SIGSEGV;
218 current->thread.code = SEGV_ACCERR;
219 current->thread.faddr = address;
220
221send_sig:
222 up_read(&mm->mmap_sem);
223 return send_fault_sig(regs);
224}