| Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 1 | /* | 
 | 2 |  * Page fault handler for SH with an MMU. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  *  Copyright (C) 1999  Niibe Yutaka | 
| Paul Mundt | 3a2e117 | 2007-05-01 16:33:10 +0900 | [diff] [blame] | 5 |  *  Copyright (C) 2003 - 2007  Paul Mundt | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  * | 
 | 7 |  *  Based on linux/arch/i386/mm/fault.c: | 
 | 8 |  *   Copyright (C) 1995  Linus Torvalds | 
| Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 9 |  * | 
 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> | 
| Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 16 | #include <linux/hardirq.h> | 
 | 17 | #include <linux/kprobes.h> | 
| Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 18 | #include <asm/io_trapped.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/system.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/mmu_context.h> | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 21 | #include <asm/tlbflush.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/kgdb.h> | 
 | 23 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 29 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, | 
 | 30 | 					unsigned long writeaccess, | 
 | 31 | 					unsigned long address) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { | 
 | 33 | 	struct task_struct *tsk; | 
 | 34 | 	struct mm_struct *mm; | 
 | 35 | 	struct vm_area_struct * vma; | 
| Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 36 | 	int si_code; | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 37 | 	int fault; | 
| Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 38 | 	siginfo_t info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| Paul Mundt | afbfb52 | 2006-12-04 18:17:28 +0900 | [diff] [blame] | 40 | 	trace_hardirqs_on(); | 
 | 41 | 	local_irq_enable(); | 
 | 42 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #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 Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 50 | 	si_code = SEGV_MAPERR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
| Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 52 | 	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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | 	/* | 
 | 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 |  */ | 
 | 113 | good_area: | 
| Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 114 | 	si_code = SEGV_ACCERR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | 	if (writeaccess) { | 
 | 116 | 		if (!(vma->vm_flags & VM_WRITE)) | 
 | 117 | 			goto bad_area; | 
 | 118 | 	} else { | 
| Jason Baron | df67b3d | 2006-09-29 01:58:58 -0700 | [diff] [blame] | 119 | 		if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | 			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 | 	 */ | 
 | 128 | survive: | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 129 | 	fault = handle_mm_fault(mm, vma, address, writeaccess); | 
 | 130 | 	if (unlikely(fault & VM_FAULT_ERROR)) { | 
 | 131 | 		if (fault & VM_FAULT_OOM) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 			goto out_of_memory; | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 133 | 		else if (fault & VM_FAULT_SIGBUS) | 
 | 134 | 			goto do_sigbus; | 
 | 135 | 		BUG(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 	} | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 137 | 	if (fault & VM_FAULT_MAJOR) | 
 | 138 | 		tsk->maj_flt++; | 
 | 139 | 	else | 
 | 140 | 		tsk->min_flt++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 |  | 
 | 142 | 	up_read(&mm->mmap_sem); | 
 | 143 | 	return; | 
 | 144 |  | 
 | 145 | /* | 
 | 146 |  * Something tried to access memory that isn't in our memory map.. | 
 | 147 |  * Fix it, but check if it's kernel or user first.. | 
 | 148 |  */ | 
 | 149 | bad_area: | 
 | 150 | 	up_read(&mm->mmap_sem); | 
 | 151 |  | 
| Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 152 | bad_area_nosemaphore: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | 	if (user_mode(regs)) { | 
| Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 154 | 		info.si_signo = SIGSEGV; | 
 | 155 | 		info.si_errno = 0; | 
 | 156 | 		info.si_code = si_code; | 
 | 157 | 		info.si_addr = (void *) address; | 
 | 158 | 		force_sig_info(SIGSEGV, &info, tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | 		return; | 
 | 160 | 	} | 
 | 161 |  | 
 | 162 | no_context: | 
 | 163 | 	/* Are we prepared to handle this kernel fault?  */ | 
 | 164 | 	if (fixup_exception(regs)) | 
 | 165 | 		return; | 
 | 166 |  | 
| Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 167 | 	if (handle_trapped_io(regs, address)) | 
 | 168 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* | 
 | 170 |  * Oops. The kernel tried to access some bad page. We'll have to | 
 | 171 |  * terminate things with extreme prejudice. | 
 | 172 |  * | 
 | 173 |  */ | 
| Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 174 |  | 
 | 175 | 	bust_spinlocks(1); | 
 | 176 |  | 
 | 177 | 	if (oops_may_print()) { | 
| Paul Mundt | b62ad83 | 2008-01-10 14:07:03 +0900 | [diff] [blame] | 178 | 		unsigned long page; | 
| Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 179 |  | 
 | 180 | 		if (address < PAGE_SIZE) | 
 | 181 | 			printk(KERN_ALERT "Unable to handle kernel NULL " | 
 | 182 | 					  "pointer dereference"); | 
 | 183 | 		else | 
 | 184 | 			printk(KERN_ALERT "Unable to handle kernel paging " | 
 | 185 | 					  "request"); | 
 | 186 | 		printk(" at virtual address %08lx\n", address); | 
 | 187 | 		printk(KERN_ALERT "pc = %08lx\n", regs->pc); | 
 | 188 | 		page = (unsigned long)get_TTB(); | 
 | 189 | 		if (page) { | 
| Paul Mundt | 06f862c | 2007-08-01 16:39:51 +0900 | [diff] [blame] | 190 | 			page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT]; | 
| Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 191 | 			printk(KERN_ALERT "*pde = %08lx\n", page); | 
 | 192 | 			if (page & _PAGE_PRESENT) { | 
 | 193 | 				page &= PAGE_MASK; | 
 | 194 | 				address &= 0x003ff000; | 
 | 195 | 				page = ((__typeof__(page) *) | 
 | 196 | 						__va(page))[address >> | 
 | 197 | 							    PAGE_SHIFT]; | 
 | 198 | 				printk(KERN_ALERT "*pte = %08lx\n", page); | 
 | 199 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | 		} | 
 | 201 | 	} | 
| Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 202 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 	die("Oops", regs, writeaccess); | 
| Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 204 | 	bust_spinlocks(0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | 	do_exit(SIGKILL); | 
 | 206 |  | 
 | 207 | /* | 
 | 208 |  * We ran out of memory, or some other thing happened to us that made | 
 | 209 |  * us unable to handle the page fault gracefully. | 
 | 210 |  */ | 
 | 211 | out_of_memory: | 
 | 212 | 	up_read(&mm->mmap_sem); | 
| Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 213 | 	if (is_global_init(current)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | 		yield(); | 
 | 215 | 		down_read(&mm->mmap_sem); | 
 | 216 | 		goto survive; | 
 | 217 | 	} | 
 | 218 | 	printk("VM: killing process %s\n", tsk->comm); | 
 | 219 | 	if (user_mode(regs)) | 
| Will Schmidt | dcca2bd | 2007-10-16 01:24:18 -0700 | [diff] [blame] | 220 | 		do_group_exit(SIGKILL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	goto no_context; | 
 | 222 |  | 
 | 223 | do_sigbus: | 
 | 224 | 	up_read(&mm->mmap_sem); | 
 | 225 |  | 
 | 226 | 	/* | 
 | 227 | 	 * Send a sigbus, regardless of whether we were in kernel | 
 | 228 | 	 * or user mode. | 
 | 229 | 	 */ | 
| Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 230 | 	info.si_signo = SIGBUS; | 
 | 231 | 	info.si_errno = 0; | 
 | 232 | 	info.si_code = BUS_ADRERR; | 
 | 233 | 	info.si_addr = (void *)address; | 
 | 234 | 	force_sig_info(SIGBUS, &info, tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 |  | 
 | 236 | 	/* Kernel mode? Handle exceptions or die */ | 
 | 237 | 	if (!user_mode(regs)) | 
 | 238 | 		goto no_context; | 
 | 239 | } | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 240 |  | 
 | 241 | #ifdef CONFIG_SH_STORE_QUEUES | 
 | 242 | /* | 
 | 243 |  * This is a special case for the SH-4 store queues, as pages for this | 
 | 244 |  * space still need to be faulted in before it's possible to flush the | 
 | 245 |  * store queue cache for writeout to the remapped region. | 
 | 246 |  */ | 
 | 247 | #define P3_ADDR_MAX		(P4SEG_STORE_QUE + 0x04000000) | 
 | 248 | #else | 
 | 249 | #define P3_ADDR_MAX		P4SEG | 
 | 250 | #endif | 
 | 251 |  | 
 | 252 | /* | 
 | 253 |  * Called with interrupts disabled. | 
 | 254 |  */ | 
 | 255 | asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs, | 
 | 256 | 					 unsigned long writeaccess, | 
 | 257 | 					 unsigned long address) | 
 | 258 | { | 
 | 259 | 	pgd_t *pgd; | 
 | 260 | 	pud_t *pud; | 
 | 261 | 	pmd_t *pmd; | 
 | 262 | 	pte_t *pte; | 
 | 263 | 	pte_t entry; | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 264 |  | 
 | 265 | #ifdef CONFIG_SH_KGDB | 
 | 266 | 	if (kgdb_nofault && kgdb_bus_err_hook) | 
 | 267 | 		kgdb_bus_err_hook(); | 
 | 268 | #endif | 
 | 269 |  | 
 | 270 | 	/* | 
 | 271 | 	 * We don't take page faults for P1, P2, and parts of P4, these | 
 | 272 | 	 * are always mapped, whether it be due to legacy behaviour in | 
 | 273 | 	 * 29-bit mode, or due to PMB configuration in 32-bit mode. | 
 | 274 | 	 */ | 
 | 275 | 	if (address >= P3SEG && address < P3_ADDR_MAX) { | 
 | 276 | 		pgd = pgd_offset_k(address); | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 277 | 	} else { | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 278 | 		if (unlikely(address >= TASK_SIZE || !current->mm)) | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 279 | 			return 1; | 
 | 280 |  | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 281 | 		pgd = pgd_offset(current->mm, address); | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 282 | 	} | 
 | 283 |  | 
 | 284 | 	pud = pud_offset(pgd, address); | 
 | 285 | 	if (pud_none_or_clear_bad(pud)) | 
 | 286 | 		return 1; | 
 | 287 | 	pmd = pmd_offset(pud, address); | 
 | 288 | 	if (pmd_none_or_clear_bad(pmd)) | 
 | 289 | 		return 1; | 
 | 290 |  | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 291 | 	pte = pte_offset_kernel(pmd, address); | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 292 | 	entry = *pte; | 
 | 293 | 	if (unlikely(pte_none(entry) || pte_not_present(entry))) | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 294 | 		return 1; | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 295 | 	if (unlikely(writeaccess && !pte_write(entry))) | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 296 | 		return 1; | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 297 |  | 
 | 298 | 	if (writeaccess) | 
 | 299 | 		entry = pte_mkdirty(entry); | 
 | 300 | 	entry = pte_mkyoung(entry); | 
 | 301 |  | 
| Hideo Saito | a602cc0 | 2008-02-14 14:45:08 +0900 | [diff] [blame] | 302 | #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP) | 
 | 303 | 	/* | 
 | 304 | 	 * ITLB is not affected by "ldtlb" instruction. | 
 | 305 | 	 * So, we need to flush the entry by ourselves. | 
 | 306 | 	 */ | 
 | 307 | 	local_flush_tlb_one(get_asid(), address & PAGE_MASK); | 
 | 308 | #endif | 
 | 309 |  | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 310 | 	set_pte(pte, entry); | 
 | 311 | 	update_mmu_cache(NULL, address, entry); | 
| Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 312 |  | 
 | 313 | 	return 0; | 
| Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 314 | } |