blob: 0c776fdfbddae6800480dc0a3dc7be54103509b3 [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
Paul Mundt3a2e1172007-05-01 16:33:10 +09005 * Copyright (C) 2003 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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>
Magnus Damme7cc9a72008-02-07 20:18:21 +090018#include <asm/io_trapped.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/mmu_context.h>
Paul Mundtdb2e1fa2007-02-14 14:13:10 +090021#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/kgdb.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
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;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090036 int si_code;
Nick Piggin83c54072007-07-19 01:47:05 -070037 int fault;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090038 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#ifdef CONFIG_SH_KGDB
41 if (kgdb_nofault && kgdb_bus_err_hook)
42 kgdb_bus_err_hook();
43#endif
44
45 tsk = current;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090046 si_code = SEGV_MAPERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Stuart Menefy99a596f2006-11-21 15:38:05 +090048 if (unlikely(address >= TASK_SIZE)) {
49 /*
50 * Synchronize this task's top level page-table
51 * with the 'reference' page table.
52 *
53 * Do _not_ use "tsk" here. We might be inside
54 * an interrupt in the middle of a task switch..
55 */
56 int offset = pgd_index(address);
57 pgd_t *pgd, *pgd_k;
58 pud_t *pud, *pud_k;
59 pmd_t *pmd, *pmd_k;
60
61 pgd = get_TTB() + offset;
62 pgd_k = swapper_pg_dir + offset;
63
64 /* This will never happen with the folded page table. */
65 if (!pgd_present(*pgd)) {
66 if (!pgd_present(*pgd_k))
67 goto bad_area_nosemaphore;
68 set_pgd(pgd, *pgd_k);
69 return;
70 }
71
72 pud = pud_offset(pgd, address);
73 pud_k = pud_offset(pgd_k, address);
74 if (pud_present(*pud) || !pud_present(*pud_k))
75 goto bad_area_nosemaphore;
76 set_pud(pud, *pud_k);
77
78 pmd = pmd_offset(pud, address);
79 pmd_k = pmd_offset(pud_k, address);
80 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
81 goto bad_area_nosemaphore;
82 set_pmd(pmd, *pmd_k);
83
84 return;
85 }
86
Stuart Menefyf2fb4e42008-07-02 17:51:23 +090087 /* Only enable interrupts if they were on before the fault */
88 if ((regs->sr & SR_IMASK) != SR_IMASK) {
89 trace_hardirqs_on();
90 local_irq_enable();
91 }
92
93 mm = tsk->mm;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
96 * If we're in an interrupt or have no user
97 * context, we must not take the fault..
98 */
99 if (in_atomic() || !mm)
100 goto no_context;
101
102 down_read(&mm->mmap_sem);
103
104 vma = find_vma(mm, address);
105 if (!vma)
106 goto bad_area;
107 if (vma->vm_start <= address)
108 goto good_area;
109 if (!(vma->vm_flags & VM_GROWSDOWN))
110 goto bad_area;
111 if (expand_stack(vma, address))
112 goto bad_area;
113/*
114 * Ok, we have a good vm_area for this memory access, so
115 * we can handle it..
116 */
117good_area:
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900118 si_code = SEGV_ACCERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (writeaccess) {
120 if (!(vma->vm_flags & VM_WRITE))
121 goto bad_area;
122 } else {
Jason Barondf67b3d2006-09-29 01:58:58 -0700123 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto bad_area;
125 }
126
127 /*
128 * If for any reason at all we couldn't handle the fault,
129 * make sure we exit gracefully rather than endlessly redo
130 * the fault.
131 */
132survive:
Nick Piggin83c54072007-07-19 01:47:05 -0700133 fault = handle_mm_fault(mm, vma, address, writeaccess);
134 if (unlikely(fault & VM_FAULT_ERROR)) {
135 if (fault & VM_FAULT_OOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto out_of_memory;
Nick Piggin83c54072007-07-19 01:47:05 -0700137 else if (fault & VM_FAULT_SIGBUS)
138 goto do_sigbus;
139 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Nick Piggin83c54072007-07-19 01:47:05 -0700141 if (fault & VM_FAULT_MAJOR)
142 tsk->maj_flt++;
143 else
144 tsk->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 up_read(&mm->mmap_sem);
147 return;
148
149/*
150 * Something tried to access memory that isn't in our memory map..
151 * Fix it, but check if it's kernel or user first..
152 */
153bad_area:
154 up_read(&mm->mmap_sem);
155
Stuart Menefy99a596f2006-11-21 15:38:05 +0900156bad_area_nosemaphore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900158 info.si_signo = SIGSEGV;
159 info.si_errno = 0;
160 info.si_code = si_code;
161 info.si_addr = (void *) address;
162 force_sig_info(SIGSEGV, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return;
164 }
165
166no_context:
167 /* Are we prepared to handle this kernel fault? */
168 if (fixup_exception(regs))
169 return;
170
Magnus Damme7cc9a72008-02-07 20:18:21 +0900171 if (handle_trapped_io(regs, address))
172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * Oops. The kernel tried to access some bad page. We'll have to
175 * terminate things with extreme prejudice.
176 *
177 */
Paul Mundt0630e452007-06-18 19:02:47 +0900178
179 bust_spinlocks(1);
180
181 if (oops_may_print()) {
Paul Mundtb62ad832008-01-10 14:07:03 +0900182 unsigned long page;
Paul Mundt0630e452007-06-18 19:02:47 +0900183
184 if (address < PAGE_SIZE)
185 printk(KERN_ALERT "Unable to handle kernel NULL "
186 "pointer dereference");
187 else
188 printk(KERN_ALERT "Unable to handle kernel paging "
189 "request");
190 printk(" at virtual address %08lx\n", address);
191 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
192 page = (unsigned long)get_TTB();
193 if (page) {
Paul Mundt06f862c2007-08-01 16:39:51 +0900194 page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
Paul Mundt0630e452007-06-18 19:02:47 +0900195 printk(KERN_ALERT "*pde = %08lx\n", page);
196 if (page & _PAGE_PRESENT) {
197 page &= PAGE_MASK;
198 address &= 0x003ff000;
199 page = ((__typeof__(page) *)
200 __va(page))[address >>
201 PAGE_SHIFT];
202 printk(KERN_ALERT "*pte = %08lx\n", page);
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 }
Paul Mundt0630e452007-06-18 19:02:47 +0900206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 die("Oops", regs, writeaccess);
Paul Mundt0630e452007-06-18 19:02:47 +0900208 bust_spinlocks(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 do_exit(SIGKILL);
210
211/*
212 * We ran out of memory, or some other thing happened to us that made
213 * us unable to handle the page fault gracefully.
214 */
215out_of_memory:
216 up_read(&mm->mmap_sem);
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700217 if (is_global_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 yield();
219 down_read(&mm->mmap_sem);
220 goto survive;
221 }
222 printk("VM: killing process %s\n", tsk->comm);
223 if (user_mode(regs))
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700224 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto no_context;
226
227do_sigbus:
228 up_read(&mm->mmap_sem);
229
230 /*
231 * Send a sigbus, regardless of whether we were in kernel
232 * or user mode.
233 */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900234 info.si_signo = SIGBUS;
235 info.si_errno = 0;
236 info.si_code = BUS_ADRERR;
237 info.si_addr = (void *)address;
238 force_sig_info(SIGBUS, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* Kernel mode? Handle exceptions or die */
241 if (!user_mode(regs))
242 goto no_context;
243}
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900244
245#ifdef CONFIG_SH_STORE_QUEUES
246/*
247 * This is a special case for the SH-4 store queues, as pages for this
248 * space still need to be faulted in before it's possible to flush the
249 * store queue cache for writeout to the remapped region.
250 */
251#define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
252#else
253#define P3_ADDR_MAX P4SEG
254#endif
255
256/*
257 * Called with interrupts disabled.
258 */
259asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
260 unsigned long writeaccess,
261 unsigned long address)
262{
263 pgd_t *pgd;
264 pud_t *pud;
265 pmd_t *pmd;
266 pte_t *pte;
267 pte_t entry;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900268
269#ifdef CONFIG_SH_KGDB
270 if (kgdb_nofault && kgdb_bus_err_hook)
271 kgdb_bus_err_hook();
272#endif
273
274 /*
275 * We don't take page faults for P1, P2, and parts of P4, these
276 * are always mapped, whether it be due to legacy behaviour in
277 * 29-bit mode, or due to PMB configuration in 32-bit mode.
278 */
279 if (address >= P3SEG && address < P3_ADDR_MAX) {
280 pgd = pgd_offset_k(address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900281 } else {
Paul Mundt0f1a3942007-11-19 13:05:18 +0900282 if (unlikely(address >= TASK_SIZE || !current->mm))
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900283 return 1;
284
Paul Mundt0f1a3942007-11-19 13:05:18 +0900285 pgd = pgd_offset(current->mm, address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900286 }
287
288 pud = pud_offset(pgd, address);
289 if (pud_none_or_clear_bad(pud))
290 return 1;
291 pmd = pmd_offset(pud, address);
292 if (pmd_none_or_clear_bad(pmd))
293 return 1;
294
Paul Mundt0f1a3942007-11-19 13:05:18 +0900295 pte = pte_offset_kernel(pmd, address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900296 entry = *pte;
297 if (unlikely(pte_none(entry) || pte_not_present(entry)))
Paul Mundt0f1a3942007-11-19 13:05:18 +0900298 return 1;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900299 if (unlikely(writeaccess && !pte_write(entry)))
Paul Mundt0f1a3942007-11-19 13:05:18 +0900300 return 1;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900301
302 if (writeaccess)
303 entry = pte_mkdirty(entry);
304 entry = pte_mkyoung(entry);
305
Hideo Saitoa602cc02008-02-14 14:45:08 +0900306#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
307 /*
308 * ITLB is not affected by "ldtlb" instruction.
309 * So, we need to flush the entry by ourselves.
310 */
311 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
312#endif
313
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900314 set_pte(pte, entry);
315 update_mmu_cache(NULL, address, entry);
Paul Mundt0f1a3942007-11-19 13:05:18 +0900316
317 return 0;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900318}