blob: 716ebf568af20d2e2e7e303161d11304886ff794 [file] [log] [blame]
Paul Mundt26ff6c12006-09-27 15:13:36 +09001/*
2 * Page fault handler for SH with an MMU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1999 Niibe Yutaka
5 * Copyright (C) 2003 Paul Mundt
6 *
7 * Based on linux/arch/i386/mm/fault.c:
8 * Copyright (C) 1995 Linus Torvalds
Paul Mundt26ff6c12006-09-27 15:13:36 +09009 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
12 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
Paul Mundt0f08f332006-09-27 17:03:56 +090016#include <linux/hardirq.h>
17#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/kgdb.h>
21
22extern void die(const char *,struct pt_regs *,long);
23
24/*
25 * This routine handles page faults. It determines the address,
26 * and the problem, and then passes it off to one of the appropriate
27 * routines.
28 */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090029asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
30 unsigned long writeaccess,
31 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct task_struct *tsk;
34 struct mm_struct *mm;
35 struct vm_area_struct * vma;
36 unsigned long page;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090037 int si_code;
38 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Paul Mundtafbfb522006-12-04 18:17:28 +090040 trace_hardirqs_on();
41 local_irq_enable();
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#ifdef CONFIG_SH_KGDB
44 if (kgdb_nofault && kgdb_bus_err_hook)
45 kgdb_bus_err_hook();
46#endif
47
48 tsk = current;
49 mm = tsk->mm;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090050 si_code = SEGV_MAPERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stuart Menefy99a596f2006-11-21 15:38:05 +090052 if (unlikely(address >= TASK_SIZE)) {
53 /*
54 * Synchronize this task's top level page-table
55 * with the 'reference' page table.
56 *
57 * Do _not_ use "tsk" here. We might be inside
58 * an interrupt in the middle of a task switch..
59 */
60 int offset = pgd_index(address);
61 pgd_t *pgd, *pgd_k;
62 pud_t *pud, *pud_k;
63 pmd_t *pmd, *pmd_k;
64
65 pgd = get_TTB() + offset;
66 pgd_k = swapper_pg_dir + offset;
67
68 /* This will never happen with the folded page table. */
69 if (!pgd_present(*pgd)) {
70 if (!pgd_present(*pgd_k))
71 goto bad_area_nosemaphore;
72 set_pgd(pgd, *pgd_k);
73 return;
74 }
75
76 pud = pud_offset(pgd, address);
77 pud_k = pud_offset(pgd_k, address);
78 if (pud_present(*pud) || !pud_present(*pud_k))
79 goto bad_area_nosemaphore;
80 set_pud(pud, *pud_k);
81
82 pmd = pmd_offset(pud, address);
83 pmd_k = pmd_offset(pud_k, address);
84 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
85 goto bad_area_nosemaphore;
86 set_pmd(pmd, *pmd_k);
87
88 return;
89 }
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /*
92 * If we're in an interrupt or have no user
93 * context, we must not take the fault..
94 */
95 if (in_atomic() || !mm)
96 goto no_context;
97
98 down_read(&mm->mmap_sem);
99
100 vma = find_vma(mm, address);
101 if (!vma)
102 goto bad_area;
103 if (vma->vm_start <= address)
104 goto good_area;
105 if (!(vma->vm_flags & VM_GROWSDOWN))
106 goto bad_area;
107 if (expand_stack(vma, address))
108 goto bad_area;
109/*
110 * Ok, we have a good vm_area for this memory access, so
111 * we can handle it..
112 */
113good_area:
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900114 si_code = SEGV_ACCERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (writeaccess) {
116 if (!(vma->vm_flags & VM_WRITE))
117 goto bad_area;
118 } else {
Jason Barondf67b3d2006-09-29 01:58:58 -0700119 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 goto bad_area;
121 }
122
123 /*
124 * If for any reason at all we couldn't handle the fault,
125 * make sure we exit gracefully rather than endlessly redo
126 * the fault.
127 */
128survive:
129 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
130 case VM_FAULT_MINOR:
131 tsk->min_flt++;
132 break;
133 case VM_FAULT_MAJOR:
134 tsk->maj_flt++;
135 break;
136 case VM_FAULT_SIGBUS:
137 goto do_sigbus;
138 case VM_FAULT_OOM:
139 goto out_of_memory;
140 default:
141 BUG();
142 }
143
144 up_read(&mm->mmap_sem);
145 return;
146
147/*
148 * Something tried to access memory that isn't in our memory map..
149 * Fix it, but check if it's kernel or user first..
150 */
151bad_area:
152 up_read(&mm->mmap_sem);
153
Stuart Menefy99a596f2006-11-21 15:38:05 +0900154bad_area_nosemaphore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900156 info.si_signo = SIGSEGV;
157 info.si_errno = 0;
158 info.si_code = si_code;
159 info.si_addr = (void *) address;
160 force_sig_info(SIGSEGV, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return;
162 }
163
164no_context:
165 /* Are we prepared to handle this kernel fault? */
166 if (fixup_exception(regs))
167 return;
168
169/*
170 * Oops. The kernel tried to access some bad page. We'll have to
171 * terminate things with extreme prejudice.
172 *
173 */
174 if (address < PAGE_SIZE)
175 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
176 else
177 printk(KERN_ALERT "Unable to handle kernel paging request");
178 printk(" at virtual address %08lx\n", address);
179 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
Paul Mundtbca7c202006-12-01 12:14:11 +0900180 page = (unsigned long)get_TTB();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (page) {
Paul Mundtbca7c202006-12-01 12:14:11 +0900182 page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 printk(KERN_ALERT "*pde = %08lx\n", page);
184 if (page & _PAGE_PRESENT) {
185 page &= PAGE_MASK;
186 address &= 0x003ff000;
187 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
188 printk(KERN_ALERT "*pte = %08lx\n", page);
189 }
190 }
191 die("Oops", regs, writeaccess);
192 do_exit(SIGKILL);
193
194/*
195 * We ran out of memory, or some other thing happened to us that made
196 * us unable to handle the page fault gracefully.
197 */
198out_of_memory:
199 up_read(&mm->mmap_sem);
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700200 if (is_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 yield();
202 down_read(&mm->mmap_sem);
203 goto survive;
204 }
205 printk("VM: killing process %s\n", tsk->comm);
206 if (user_mode(regs))
207 do_exit(SIGKILL);
208 goto no_context;
209
210do_sigbus:
211 up_read(&mm->mmap_sem);
212
213 /*
214 * Send a sigbus, regardless of whether we were in kernel
215 * or user mode.
216 */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900217 info.si_signo = SIGBUS;
218 info.si_errno = 0;
219 info.si_code = BUS_ADRERR;
220 info.si_addr = (void *)address;
221 force_sig_info(SIGBUS, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Kernel mode? Handle exceptions or die */
224 if (!user_mode(regs))
225 goto no_context;
226}