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 |
| 5 | * Copyright (C) 2003 Paul Mundt |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/kgdb.h> |
| 21 | |
| 22 | extern 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 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; |
| 36 | unsigned long page; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 37 | int si_code; |
| 38 | siginfo_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #ifdef CONFIG_SH_KGDB |
| 41 | if (kgdb_nofault && kgdb_bus_err_hook) |
| 42 | kgdb_bus_err_hook(); |
| 43 | #endif |
| 44 | |
| 45 | tsk = current; |
| 46 | mm = tsk->mm; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 47 | si_code = SEGV_MAPERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame^] | 49 | if (unlikely(address >= TASK_SIZE)) { |
| 50 | /* |
| 51 | * Synchronize this task's top level page-table |
| 52 | * with the 'reference' page table. |
| 53 | * |
| 54 | * Do _not_ use "tsk" here. We might be inside |
| 55 | * an interrupt in the middle of a task switch.. |
| 56 | */ |
| 57 | int offset = pgd_index(address); |
| 58 | pgd_t *pgd, *pgd_k; |
| 59 | pud_t *pud, *pud_k; |
| 60 | pmd_t *pmd, *pmd_k; |
| 61 | |
| 62 | pgd = get_TTB() + offset; |
| 63 | pgd_k = swapper_pg_dir + offset; |
| 64 | |
| 65 | /* This will never happen with the folded page table. */ |
| 66 | if (!pgd_present(*pgd)) { |
| 67 | if (!pgd_present(*pgd_k)) |
| 68 | goto bad_area_nosemaphore; |
| 69 | set_pgd(pgd, *pgd_k); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | pud = pud_offset(pgd, address); |
| 74 | pud_k = pud_offset(pgd_k, address); |
| 75 | if (pud_present(*pud) || !pud_present(*pud_k)) |
| 76 | goto bad_area_nosemaphore; |
| 77 | set_pud(pud, *pud_k); |
| 78 | |
| 79 | pmd = pmd_offset(pud, address); |
| 80 | pmd_k = pmd_offset(pud_k, address); |
| 81 | if (pmd_present(*pmd) || !pmd_present(*pmd_k)) |
| 82 | goto bad_area_nosemaphore; |
| 83 | set_pmd(pmd, *pmd_k); |
| 84 | |
| 85 | return; |
| 86 | } |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* |
| 89 | * If we're in an interrupt or have no user |
| 90 | * context, we must not take the fault.. |
| 91 | */ |
| 92 | if (in_atomic() || !mm) |
| 93 | goto no_context; |
| 94 | |
| 95 | down_read(&mm->mmap_sem); |
| 96 | |
| 97 | vma = find_vma(mm, address); |
| 98 | if (!vma) |
| 99 | goto bad_area; |
| 100 | if (vma->vm_start <= address) |
| 101 | goto good_area; |
| 102 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 103 | goto bad_area; |
| 104 | if (expand_stack(vma, address)) |
| 105 | goto bad_area; |
| 106 | /* |
| 107 | * Ok, we have a good vm_area for this memory access, so |
| 108 | * we can handle it.. |
| 109 | */ |
| 110 | good_area: |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 111 | si_code = SEGV_ACCERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (writeaccess) { |
| 113 | if (!(vma->vm_flags & VM_WRITE)) |
| 114 | goto bad_area; |
| 115 | } else { |
Jason Baron | df67b3d | 2006-09-29 01:58:58 -0700 | [diff] [blame] | 116 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | goto bad_area; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * If for any reason at all we couldn't handle the fault, |
| 122 | * make sure we exit gracefully rather than endlessly redo |
| 123 | * the fault. |
| 124 | */ |
| 125 | survive: |
| 126 | switch (handle_mm_fault(mm, vma, address, writeaccess)) { |
| 127 | case VM_FAULT_MINOR: |
| 128 | tsk->min_flt++; |
| 129 | break; |
| 130 | case VM_FAULT_MAJOR: |
| 131 | tsk->maj_flt++; |
| 132 | break; |
| 133 | case VM_FAULT_SIGBUS: |
| 134 | goto do_sigbus; |
| 135 | case VM_FAULT_OOM: |
| 136 | goto out_of_memory; |
| 137 | default: |
| 138 | BUG(); |
| 139 | } |
| 140 | |
| 141 | up_read(&mm->mmap_sem); |
| 142 | return; |
| 143 | |
| 144 | /* |
| 145 | * Something tried to access memory that isn't in our memory map.. |
| 146 | * Fix it, but check if it's kernel or user first.. |
| 147 | */ |
| 148 | bad_area: |
| 149 | up_read(&mm->mmap_sem); |
| 150 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame^] | 151 | bad_area_nosemaphore: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (user_mode(regs)) { |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 153 | info.si_signo = SIGSEGV; |
| 154 | info.si_errno = 0; |
| 155 | info.si_code = si_code; |
| 156 | info.si_addr = (void *) address; |
| 157 | force_sig_info(SIGSEGV, &info, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | no_context: |
| 162 | /* Are we prepared to handle this kernel fault? */ |
| 163 | if (fixup_exception(regs)) |
| 164 | return; |
| 165 | |
| 166 | /* |
| 167 | * Oops. The kernel tried to access some bad page. We'll have to |
| 168 | * terminate things with extreme prejudice. |
| 169 | * |
| 170 | */ |
| 171 | if (address < PAGE_SIZE) |
| 172 | printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference"); |
| 173 | else |
| 174 | printk(KERN_ALERT "Unable to handle kernel paging request"); |
| 175 | printk(" at virtual address %08lx\n", address); |
| 176 | printk(KERN_ALERT "pc = %08lx\n", regs->pc); |
| 177 | asm volatile("mov.l %1, %0" |
| 178 | : "=r" (page) |
| 179 | : "m" (__m(MMU_TTB))); |
| 180 | if (page) { |
| 181 | page = ((unsigned long *) page)[address >> 22]; |
| 182 | printk(KERN_ALERT "*pde = %08lx\n", page); |
| 183 | if (page & _PAGE_PRESENT) { |
| 184 | page &= PAGE_MASK; |
| 185 | address &= 0x003ff000; |
| 186 | page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT]; |
| 187 | printk(KERN_ALERT "*pte = %08lx\n", page); |
| 188 | } |
| 189 | } |
| 190 | die("Oops", regs, writeaccess); |
| 191 | do_exit(SIGKILL); |
| 192 | |
| 193 | /* |
| 194 | * We ran out of memory, or some other thing happened to us that made |
| 195 | * us unable to handle the page fault gracefully. |
| 196 | */ |
| 197 | out_of_memory: |
| 198 | up_read(&mm->mmap_sem); |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 199 | if (is_init(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | yield(); |
| 201 | down_read(&mm->mmap_sem); |
| 202 | goto survive; |
| 203 | } |
| 204 | printk("VM: killing process %s\n", tsk->comm); |
| 205 | if (user_mode(regs)) |
| 206 | do_exit(SIGKILL); |
| 207 | goto no_context; |
| 208 | |
| 209 | do_sigbus: |
| 210 | up_read(&mm->mmap_sem); |
| 211 | |
| 212 | /* |
| 213 | * Send a sigbus, regardless of whether we were in kernel |
| 214 | * or user mode. |
| 215 | */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 216 | info.si_signo = SIGBUS; |
| 217 | info.si_errno = 0; |
| 218 | info.si_code = BUS_ADRERR; |
| 219 | info.si_addr = (void *)address; |
| 220 | force_sig_info(SIGBUS, &info, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /* Kernel mode? Handle exceptions or die */ |
| 223 | if (!user_mode(regs)) |
| 224 | goto no_context; |
| 225 | } |
| 226 | |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 227 | #ifdef CONFIG_SH_STORE_QUEUES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 229 | * This is a special case for the SH-4 store queues, as pages for this |
| 230 | * space still need to be faulted in before it's possible to flush the |
| 231 | * store queue cache for writeout to the remapped region. |
| 232 | */ |
| 233 | #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000) |
| 234 | #else |
| 235 | #define P3_ADDR_MAX P4SEG |
| 236 | #endif |
| 237 | |
| 238 | /* |
| 239 | * Called with interrupts disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 241 | asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs, |
| 242 | unsigned long writeaccess, |
| 243 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 245 | pgd_t *pgd; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 246 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | pmd_t *pmd; |
| 248 | pte_t *pte; |
| 249 | pte_t entry; |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 250 | struct mm_struct *mm = current->mm; |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 251 | spinlock_t *ptl; |
| 252 | int ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | #ifdef CONFIG_SH_KGDB |
| 255 | if (kgdb_nofault && kgdb_bus_err_hook) |
| 256 | kgdb_bus_err_hook(); |
| 257 | #endif |
| 258 | |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 259 | /* |
| 260 | * We don't take page faults for P1, P2, and parts of P4, these |
| 261 | * are always mapped, whether it be due to legacy behaviour in |
| 262 | * 29-bit mode, or due to PMB configuration in 32-bit mode. |
| 263 | */ |
Paul Mundt | f647d33 | 2006-09-27 15:30:24 +0900 | [diff] [blame] | 264 | if (address >= P3SEG && address < P3_ADDR_MAX) { |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 265 | pgd = pgd_offset_k(address); |
Paul Mundt | f647d33 | 2006-09-27 15:30:24 +0900 | [diff] [blame] | 266 | mm = NULL; |
| 267 | } else { |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 268 | if (unlikely(address >= TASK_SIZE || !mm)) |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 269 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 271 | pgd = pgd_offset(mm, address); |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | pud = pud_offset(pgd, address); |
| 275 | if (pud_none_or_clear_bad(pud)) |
| 276 | return 1; |
| 277 | pmd = pmd_offset(pud, address); |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 278 | if (pmd_none_or_clear_bad(pmd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return 1; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 280 | |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 281 | if (mm) |
| 282 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 283 | else |
| 284 | pte = pte_offset_kernel(pmd, address); |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | entry = *pte; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 287 | if (unlikely(pte_none(entry) || pte_not_present(entry))) |
| 288 | goto unlock; |
| 289 | if (unlikely(writeaccess && !pte_write(entry))) |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 290 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | if (writeaccess) |
| 293 | entry = pte_mkdirty(entry); |
| 294 | entry = pte_mkyoung(entry); |
| 295 | |
| 296 | #ifdef CONFIG_CPU_SH4 |
| 297 | /* |
| 298 | * ITLB is not affected by "ldtlb" instruction. |
| 299 | * So, we need to flush the entry by ourselves. |
| 300 | */ |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 301 | __flush_tlb_page(get_asid(), address & PAGE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | set_pte(pte, entry); |
| 305 | update_mmu_cache(NULL, address, entry); |
Hugh Dickins | 60ec558 | 2005-10-29 18:16:34 -0700 | [diff] [blame] | 306 | ret = 0; |
| 307 | unlock: |
| 308 | if (mm) |
| 309 | pte_unmap_unlock(pte, ptl); |
| 310 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |