yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/kernel/traps.c |
| 3 | * |
| 4 | * Copyright (C) 1995-2009 Russell King |
| 5 | * Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * 'traps.c' handles hardware exceptions after we have saved some state in |
| 12 | * 'linux/arch/arm/lib/traps.S'. Mostly a debugging aid, but will probably |
| 13 | * kill the offending process. |
| 14 | */ |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/personality.h> |
| 17 | #include <linux/kallsyms.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/hardirq.h> |
| 21 | #include <linux/kdebug.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kexec.h> |
| 24 | #include <linux/bug.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/sched.h> |
| 28 | |
| 29 | #include <linux/atomic.h> |
| 30 | #include <asm/cacheflush.h> |
| 31 | #include <asm/exception.h> |
| 32 | #include <asm/unistd.h> |
| 33 | #include <asm/traps.h> |
| 34 | #include <asm/unwind.h> |
| 35 | #include <asm/tls.h> |
| 36 | #include <asm/system_misc.h> |
| 37 | |
| 38 | #include "signal.h" |
| 39 | |
| 40 | static const char *handler[]= { |
| 41 | "prefetch abort", |
| 42 | "data abort", |
| 43 | "address exception", |
| 44 | "interrupt", |
| 45 | "undefined instruction", |
| 46 | }; |
| 47 | |
| 48 | void *vectors_page; |
| 49 | |
| 50 | #ifdef CONFIG_DEBUG_USER |
| 51 | unsigned int user_debug; |
| 52 | |
| 53 | static int __init user_debug_setup(char *str) |
| 54 | { |
| 55 | get_option(&str, &user_debug); |
| 56 | return 1; |
| 57 | } |
| 58 | __setup("user_debug=", user_debug_setup); |
| 59 | #endif |
| 60 | |
| 61 | static void dump_mem(const char *, const char *, unsigned long, unsigned long); |
| 62 | |
| 63 | void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame) |
| 64 | { |
| 65 | #ifdef CONFIG_KALLSYMS |
| 66 | printk("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from); |
| 67 | #else |
| 68 | printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from); |
| 69 | #endif |
| 70 | |
| 71 | if (in_exception_text(where)) |
| 72 | dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs)); |
| 73 | } |
| 74 | |
| 75 | #ifndef CONFIG_ARM_UNWIND |
| 76 | /* |
| 77 | * Stack pointers should always be within the kernels view of |
| 78 | * physical memory. If it is not there, then we can't dump |
| 79 | * out any information relating to the stack. |
| 80 | */ |
| 81 | static int verify_stack(unsigned long sp) |
| 82 | { |
| 83 | if (sp < PAGE_OFFSET || |
| 84 | (sp > (unsigned long)high_memory && high_memory != NULL)) |
| 85 | return -EFAULT; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | /* |
| 92 | * Dump out the contents of some memory nicely... |
| 93 | */ |
| 94 | static void dump_mem(const char *lvl, const char *str, unsigned long bottom, |
| 95 | unsigned long top) |
| 96 | { |
| 97 | unsigned long first; |
| 98 | mm_segment_t fs; |
| 99 | int i; |
| 100 | |
| 101 | /* |
| 102 | * We need to switch to kernel mode so that we can use __get_user |
| 103 | * to safely read from kernel space. Note that we now dump the |
| 104 | * code first, just in case the backtrace kills us. |
| 105 | */ |
| 106 | fs = get_fs(); |
| 107 | set_fs(KERNEL_DS); |
| 108 | |
| 109 | printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top); |
| 110 | |
| 111 | for (first = bottom & ~31; first < top; first += 32) { |
| 112 | unsigned long p; |
| 113 | char str[sizeof(" 12345678") * 8 + 1]; |
| 114 | |
| 115 | memset(str, ' ', sizeof(str)); |
| 116 | str[sizeof(str) - 1] = '\0'; |
| 117 | |
| 118 | for (p = first, i = 0; i < 8 && p < top; i++, p += 4) { |
| 119 | if (p >= bottom && p < top) { |
| 120 | unsigned long val; |
| 121 | if (__get_user(val, (unsigned long *)p) == 0) |
| 122 | sprintf(str + i * 9, " %08lx", val); |
| 123 | else |
| 124 | sprintf(str + i * 9, " ????????"); |
| 125 | } |
| 126 | } |
| 127 | printk("%s%04lx:%s\n", lvl, first & 0xffff, str); |
| 128 | } |
| 129 | |
| 130 | set_fs(fs); |
| 131 | } |
| 132 | |
| 133 | static void dump_instr(const char *lvl, struct pt_regs *regs) |
| 134 | { |
| 135 | unsigned long addr = instruction_pointer(regs); |
| 136 | const int thumb = thumb_mode(regs); |
| 137 | const int width = thumb ? 4 : 8; |
| 138 | mm_segment_t fs; |
| 139 | char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str; |
| 140 | int i; |
| 141 | |
| 142 | /* |
| 143 | * We need to switch to kernel mode so that we can use __get_user |
| 144 | * to safely read from kernel space. Note that we now dump the |
| 145 | * code first, just in case the backtrace kills us. |
| 146 | */ |
| 147 | fs = get_fs(); |
| 148 | set_fs(KERNEL_DS); |
| 149 | |
| 150 | for (i = -4; i < 1 + !!thumb; i++) { |
| 151 | unsigned int val, bad; |
| 152 | |
| 153 | if (thumb) |
| 154 | bad = __get_user(val, &((u16 *)addr)[i]); |
| 155 | else |
| 156 | bad = __get_user(val, &((u32 *)addr)[i]); |
| 157 | |
| 158 | if (!bad) |
| 159 | p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ", |
| 160 | width, val); |
| 161 | else { |
| 162 | p += sprintf(p, "bad PC value"); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | printk("%sCode: %s\n", lvl, str); |
| 167 | |
| 168 | set_fs(fs); |
| 169 | } |
| 170 | |
| 171 | #ifdef CONFIG_ARM_UNWIND |
| 172 | static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk) |
| 173 | { |
| 174 | unwind_backtrace(regs, tsk); |
| 175 | } |
| 176 | #else |
| 177 | static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk) |
| 178 | { |
| 179 | unsigned int fp, mode; |
| 180 | int ok = 1; |
| 181 | |
| 182 | printk("Backtrace: "); |
| 183 | |
| 184 | if (!tsk) |
| 185 | tsk = current; |
| 186 | |
| 187 | if (regs) { |
| 188 | fp = regs->ARM_fp; |
| 189 | mode = processor_mode(regs); |
| 190 | } else if (tsk != current) { |
| 191 | fp = thread_saved_fp(tsk); |
| 192 | mode = 0x10; |
| 193 | } else { |
| 194 | asm("mov %0, fp" : "=r" (fp) : : "cc"); |
| 195 | mode = 0x10; |
| 196 | } |
| 197 | |
| 198 | if (!fp) { |
| 199 | printk("no frame pointer"); |
| 200 | ok = 0; |
| 201 | } else if (verify_stack(fp)) { |
| 202 | printk("invalid frame pointer 0x%08x", fp); |
| 203 | ok = 0; |
| 204 | } else if (fp < (unsigned long)end_of_stack(tsk)) |
| 205 | printk("frame pointer underflow"); |
| 206 | printk("\n"); |
| 207 | |
| 208 | if (ok) |
| 209 | c_backtrace(fp, mode); |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | void dump_stack(void) |
| 214 | { |
| 215 | #ifndef CONFIG_MIN_8M_VERSION |
| 216 | dump_backtrace(NULL, NULL); |
| 217 | #endif |
| 218 | } |
| 219 | |
| 220 | EXPORT_SYMBOL(dump_stack); |
| 221 | |
| 222 | void show_stack(struct task_struct *tsk, unsigned long *sp) |
| 223 | { |
| 224 | #ifndef CONFIG_MIN_8M_VERSION |
| 225 | dump_backtrace(NULL, tsk); |
| 226 | barrier(); |
| 227 | #endif |
| 228 | } |
| 229 | |
| 230 | #ifdef CONFIG_PREEMPT |
| 231 | #define S_PREEMPT " PREEMPT" |
| 232 | #else |
| 233 | #define S_PREEMPT "" |
| 234 | #endif |
| 235 | #ifdef CONFIG_SMP |
| 236 | #define S_SMP " SMP" |
| 237 | #else |
| 238 | #define S_SMP "" |
| 239 | #endif |
| 240 | #ifdef CONFIG_THUMB2_KERNEL |
| 241 | #define S_ISA " THUMB2" |
| 242 | #else |
| 243 | #define S_ISA " ARM" |
| 244 | #endif |
| 245 | |
| 246 | static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs) |
| 247 | { |
| 248 | struct task_struct *tsk = thread->task; |
| 249 | static int die_counter; |
| 250 | int ret; |
| 251 | |
| 252 | printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP |
| 253 | S_ISA "\n", str, err, ++die_counter); |
| 254 | |
| 255 | /* trap and error numbers are mostly meaningless on ARM */ |
| 256 | ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV); |
| 257 | if (ret == NOTIFY_STOP) |
| 258 | return ret; |
| 259 | |
| 260 | print_modules(); |
| 261 | __show_regs(regs); |
| 262 | printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n", |
| 263 | TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1); |
| 264 | |
| 265 | if (!user_mode(regs) || in_interrupt()) { |
| 266 | dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp, |
| 267 | THREAD_SIZE + (unsigned long)task_stack_page(tsk)); |
| 268 | #ifndef CONFIG_MIN_8M_VERSION |
| 269 | dump_backtrace(regs, tsk); |
| 270 | #endif |
| 271 | dump_instr(KERN_EMERG, regs); |
| 272 | } |
| 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static DEFINE_RAW_SPINLOCK(die_lock); |
| 278 | |
| 279 | /* |
| 280 | * This function is protected against re-entrancy. |
| 281 | */ |
| 282 | void die(const char *str, struct pt_regs *regs, int err) |
| 283 | { |
| 284 | struct thread_info *thread = current_thread_info(); |
| 285 | int ret; |
| 286 | enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE; |
| 287 | |
| 288 | oops_enter(); |
| 289 | |
| 290 | raw_spin_lock_irq(&die_lock); |
| 291 | console_verbose(); |
| 292 | bust_spinlocks(1); |
| 293 | if (!user_mode(regs)) |
| 294 | bug_type = report_bug(regs->ARM_pc, regs); |
| 295 | if (bug_type != BUG_TRAP_TYPE_NONE) |
| 296 | str = "Oops - BUG"; |
| 297 | ret = __die(str, err, thread, regs); |
| 298 | |
| 299 | if (regs && kexec_should_crash(thread->task)) |
| 300 | crash_kexec(regs); |
| 301 | |
| 302 | bust_spinlocks(0); |
| 303 | add_taint(TAINT_DIE); |
| 304 | raw_spin_unlock_irq(&die_lock); |
| 305 | oops_exit(); |
| 306 | |
| 307 | if (in_interrupt()) |
| 308 | panic("Fatal exception in interrupt"); |
| 309 | if (panic_on_oops) |
| 310 | panic("Fatal exception"); |
| 311 | if (ret != NOTIFY_STOP) |
| 312 | do_exit(SIGSEGV); |
| 313 | } |
| 314 | |
| 315 | void arm_notify_die(const char *str, struct pt_regs *regs, |
| 316 | struct siginfo *info, unsigned long err, unsigned long trap) |
| 317 | { |
| 318 | if (user_mode(regs)) { |
| 319 | current->thread.error_code = err; |
| 320 | current->thread.trap_no = trap; |
| 321 | |
| 322 | force_sig_info(info->si_signo, info, current); |
| 323 | } else { |
| 324 | die(str, regs, err); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | #ifdef CONFIG_GENERIC_BUG |
| 329 | |
| 330 | int is_valid_bugaddr(unsigned long pc) |
| 331 | { |
| 332 | #ifdef CONFIG_THUMB2_KERNEL |
| 333 | unsigned short bkpt; |
| 334 | #else |
| 335 | unsigned long bkpt; |
| 336 | #endif |
| 337 | |
| 338 | if (probe_kernel_address((unsigned *)pc, bkpt)) |
| 339 | return 0; |
| 340 | |
| 341 | return bkpt == BUG_INSTR_VALUE; |
| 342 | } |
| 343 | |
| 344 | #endif |
| 345 | |
| 346 | static LIST_HEAD(undef_hook); |
| 347 | static DEFINE_RAW_SPINLOCK(undef_lock); |
| 348 | |
| 349 | void register_undef_hook(struct undef_hook *hook) |
| 350 | { |
| 351 | unsigned long flags; |
| 352 | |
| 353 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 354 | list_add(&hook->node, &undef_hook); |
| 355 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 356 | } |
| 357 | |
| 358 | void unregister_undef_hook(struct undef_hook *hook) |
| 359 | { |
| 360 | unsigned long flags; |
| 361 | |
| 362 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 363 | list_del(&hook->node); |
| 364 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 365 | } |
| 366 | |
| 367 | static int call_undef_hook(struct pt_regs *regs, unsigned int instr) |
| 368 | { |
| 369 | struct undef_hook *hook; |
| 370 | unsigned long flags; |
| 371 | int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL; |
| 372 | |
| 373 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 374 | list_for_each_entry(hook, &undef_hook, node) |
| 375 | if ((instr & hook->instr_mask) == hook->instr_val && |
| 376 | (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val) |
| 377 | fn = hook->fn; |
| 378 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 379 | |
| 380 | return fn ? fn(regs, instr) : 1; |
| 381 | } |
| 382 | |
| 383 | asmlinkage void __exception do_undefinstr(struct pt_regs *regs) |
| 384 | { |
| 385 | unsigned int instr; |
| 386 | siginfo_t info; |
| 387 | void __user *pc; |
| 388 | |
| 389 | pc = (void __user *)instruction_pointer(regs); |
| 390 | |
| 391 | if (processor_mode(regs) == SVC_MODE) { |
| 392 | #ifdef CONFIG_THUMB2_KERNEL |
| 393 | if (thumb_mode(regs)) { |
| 394 | instr = ((u16 *)pc)[0]; |
| 395 | if (is_wide_instruction(instr)) { |
| 396 | instr <<= 16; |
| 397 | instr |= ((u16 *)pc)[1]; |
| 398 | } |
| 399 | } else |
| 400 | #endif |
| 401 | instr = *(u32 *) pc; |
| 402 | } else if (thumb_mode(regs)) { |
| 403 | if (get_user(instr, (u16 __user *)pc)) |
| 404 | goto die_sig; |
| 405 | if (is_wide_instruction(instr)) { |
| 406 | unsigned int instr2; |
| 407 | if (get_user(instr2, (u16 __user *)pc+1)) |
| 408 | goto die_sig; |
| 409 | instr <<= 16; |
| 410 | instr |= instr2; |
| 411 | } |
| 412 | } else if (get_user(instr, (u32 __user *)pc)) { |
| 413 | goto die_sig; |
| 414 | } |
| 415 | |
| 416 | if (call_undef_hook(regs, instr) == 0) |
| 417 | return; |
| 418 | |
| 419 | die_sig: |
| 420 | #ifdef CONFIG_DEBUG_USER |
| 421 | if (user_debug & UDBG_UNDEFINED) { |
| 422 | printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n", |
| 423 | current->comm, task_pid_nr(current), pc); |
| 424 | dump_instr(KERN_INFO, regs); |
| 425 | } |
| 426 | #endif |
| 427 | |
| 428 | info.si_signo = SIGILL; |
| 429 | info.si_errno = 0; |
| 430 | info.si_code = ILL_ILLOPC; |
| 431 | info.si_addr = pc; |
| 432 | |
| 433 | arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6); |
| 434 | } |
| 435 | |
| 436 | asmlinkage void do_unexp_fiq (struct pt_regs *regs) |
| 437 | { |
| 438 | printk("Hmm. Unexpected FIQ received, but trying to continue\n"); |
| 439 | printk("You may have a hardware problem...\n"); |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * bad_mode handles the impossible case in the vectors. If you see one of |
| 444 | * these, then it's extremely serious, and could mean you have buggy hardware. |
| 445 | * It never returns, and never tries to sync. We hope that we can at least |
| 446 | * dump out some state information... |
| 447 | */ |
| 448 | asmlinkage void bad_mode(struct pt_regs *regs, int reason) |
| 449 | { |
| 450 | console_verbose(); |
| 451 | |
| 452 | printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]); |
| 453 | |
| 454 | die("Oops - bad mode", regs, 0); |
| 455 | local_irq_disable(); |
| 456 | panic("bad mode"); |
| 457 | } |
| 458 | |
| 459 | static int bad_syscall(int n, struct pt_regs *regs) |
| 460 | { |
| 461 | struct thread_info *thread = current_thread_info(); |
| 462 | siginfo_t info; |
| 463 | |
| 464 | if ((current->personality & PER_MASK) != PER_LINUX && |
| 465 | thread->exec_domain->handler) { |
| 466 | thread->exec_domain->handler(n, regs); |
| 467 | return regs->ARM_r0; |
| 468 | } |
| 469 | |
| 470 | #ifdef CONFIG_DEBUG_USER |
| 471 | if (user_debug & UDBG_SYSCALL) { |
| 472 | printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n", |
| 473 | task_pid_nr(current), current->comm, n); |
| 474 | dump_instr(KERN_ERR, regs); |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | info.si_signo = SIGILL; |
| 479 | info.si_errno = 0; |
| 480 | info.si_code = ILL_ILLTRP; |
| 481 | info.si_addr = (void __user *)instruction_pointer(regs) - |
| 482 | (thumb_mode(regs) ? 2 : 4); |
| 483 | |
| 484 | arm_notify_die("Oops - bad syscall", regs, &info, n, 0); |
| 485 | |
| 486 | return regs->ARM_r0; |
| 487 | } |
| 488 | |
| 489 | static inline void |
| 490 | do_cache_op(unsigned long start, unsigned long end, int flags) |
| 491 | { |
| 492 | struct mm_struct *mm = current->active_mm; |
| 493 | struct vm_area_struct *vma; |
| 494 | |
| 495 | if (end < start || flags) |
| 496 | return; |
| 497 | |
| 498 | down_read(&mm->mmap_sem); |
| 499 | vma = find_vma(mm, start); |
| 500 | if (vma && vma->vm_start < end) { |
| 501 | if (start < vma->vm_start) |
| 502 | start = vma->vm_start; |
| 503 | if (end > vma->vm_end) |
| 504 | end = vma->vm_end; |
| 505 | |
| 506 | up_read(&mm->mmap_sem); |
| 507 | flush_cache_user_range(start, end); |
| 508 | return; |
| 509 | } |
| 510 | up_read(&mm->mmap_sem); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Handle all unrecognised system calls. |
| 515 | * 0x9f0000 - 0x9fffff are some more esoteric system calls |
| 516 | */ |
| 517 | #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE) |
| 518 | asmlinkage int arm_syscall(int no, struct pt_regs *regs) |
| 519 | { |
| 520 | struct thread_info *thread = current_thread_info(); |
| 521 | siginfo_t info; |
| 522 | |
| 523 | if ((no >> 16) != (__ARM_NR_BASE>> 16)) |
| 524 | return bad_syscall(no, regs); |
| 525 | |
| 526 | switch (no & 0xffff) { |
| 527 | case 0: /* branch through 0 */ |
| 528 | info.si_signo = SIGSEGV; |
| 529 | info.si_errno = 0; |
| 530 | info.si_code = SEGV_MAPERR; |
| 531 | info.si_addr = NULL; |
| 532 | |
| 533 | arm_notify_die("branch through zero", regs, &info, 0, 0); |
| 534 | return 0; |
| 535 | |
| 536 | case NR(breakpoint): /* SWI BREAK_POINT */ |
| 537 | regs->ARM_pc -= thumb_mode(regs) ? 2 : 4; |
| 538 | ptrace_break(current, regs); |
| 539 | return regs->ARM_r0; |
| 540 | |
| 541 | /* |
| 542 | * Flush a region from virtual address 'r0' to virtual address 'r1' |
| 543 | * _exclusive_. There is no alignment requirement on either address; |
| 544 | * user space does not need to know the hardware cache layout. |
| 545 | * |
| 546 | * r2 contains flags. It should ALWAYS be passed as ZERO until it |
| 547 | * is defined to be something else. For now we ignore it, but may |
| 548 | * the fires of hell burn in your belly if you break this rule. ;) |
| 549 | * |
| 550 | * (at a later date, we may want to allow this call to not flush |
| 551 | * various aspects of the cache. Passing '0' will guarantee that |
| 552 | * everything necessary gets flushed to maintain consistency in |
| 553 | * the specified region). |
| 554 | */ |
| 555 | case NR(cacheflush): |
| 556 | do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2); |
| 557 | return 0; |
| 558 | |
| 559 | case NR(usr26): |
| 560 | if (!(elf_hwcap & HWCAP_26BIT)) |
| 561 | break; |
| 562 | regs->ARM_cpsr &= ~MODE32_BIT; |
| 563 | return regs->ARM_r0; |
| 564 | |
| 565 | case NR(usr32): |
| 566 | if (!(elf_hwcap & HWCAP_26BIT)) |
| 567 | break; |
| 568 | regs->ARM_cpsr |= MODE32_BIT; |
| 569 | return regs->ARM_r0; |
| 570 | |
| 571 | case NR(set_tls): |
| 572 | /*Fix for HUB: CVE-2014-9870*/ |
| 573 | thread->tp_value[0] = regs->ARM_r0; |
| 574 | if (tls_emu) |
| 575 | return 0; |
| 576 | if (has_tls_reg) { |
| 577 | asm ("mcr p15, 0, %0, c13, c0, 3" |
| 578 | : : "r" (regs->ARM_r0)); |
| 579 | } else { |
| 580 | /* |
| 581 | * User space must never try to access this directly. |
| 582 | * Expect your app to break eventually if you do so. |
| 583 | * The user helper at 0xffff0fe0 must be used instead. |
| 584 | * (see entry-armv.S for details) |
| 585 | */ |
| 586 | *((unsigned int *)0xffff0ff0) = regs->ARM_r0; |
| 587 | } |
| 588 | return 0; |
| 589 | |
| 590 | #ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG |
| 591 | /* |
| 592 | * Atomically store r1 in *r2 if *r2 is equal to r0 for user space. |
| 593 | * Return zero in r0 if *MEM was changed or non-zero if no exchange |
| 594 | * happened. Also set the user C flag accordingly. |
| 595 | * If access permissions have to be fixed up then non-zero is |
| 596 | * returned and the operation has to be re-attempted. |
| 597 | * |
| 598 | * *NOTE*: This is a ghost syscall private to the kernel. Only the |
| 599 | * __kuser_cmpxchg code in entry-armv.S should be aware of its |
| 600 | * existence. Don't ever use this from user code. |
| 601 | */ |
| 602 | case NR(cmpxchg): |
| 603 | for (;;) { |
| 604 | extern void do_DataAbort(unsigned long addr, unsigned int fsr, |
| 605 | struct pt_regs *regs); |
| 606 | unsigned long val; |
| 607 | unsigned long addr = regs->ARM_r2; |
| 608 | struct mm_struct *mm = current->mm; |
| 609 | pgd_t *pgd; pmd_t *pmd; pte_t *pte; |
| 610 | spinlock_t *ptl; |
| 611 | |
| 612 | regs->ARM_cpsr &= ~PSR_C_BIT; |
| 613 | down_read(&mm->mmap_sem); |
| 614 | pgd = pgd_offset(mm, addr); |
| 615 | if (!pgd_present(*pgd)) |
| 616 | goto bad_access; |
| 617 | pmd = pmd_offset(pgd, addr); |
| 618 | if (!pmd_present(*pmd)) |
| 619 | goto bad_access; |
| 620 | pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 621 | if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) { |
| 622 | pte_unmap_unlock(pte, ptl); |
| 623 | goto bad_access; |
| 624 | } |
| 625 | val = *(unsigned long *)addr; |
| 626 | val -= regs->ARM_r0; |
| 627 | if (val == 0) { |
| 628 | *(unsigned long *)addr = regs->ARM_r1; |
| 629 | regs->ARM_cpsr |= PSR_C_BIT; |
| 630 | } |
| 631 | pte_unmap_unlock(pte, ptl); |
| 632 | up_read(&mm->mmap_sem); |
| 633 | return val; |
| 634 | |
| 635 | bad_access: |
| 636 | up_read(&mm->mmap_sem); |
| 637 | /* simulate a write access fault */ |
| 638 | do_DataAbort(addr, 15 + (1 << 11), regs); |
| 639 | } |
| 640 | #endif |
| 641 | |
| 642 | default: |
| 643 | /* Calls 9f00xx..9f07ff are defined to return -ENOSYS |
| 644 | if not implemented, rather than raising SIGILL. This |
| 645 | way the calling program can gracefully determine whether |
| 646 | a feature is supported. */ |
| 647 | if ((no & 0xffff) <= 0x7ff) |
| 648 | return -ENOSYS; |
| 649 | break; |
| 650 | } |
| 651 | #ifdef CONFIG_DEBUG_USER |
| 652 | /* |
| 653 | * experience shows that these seem to indicate that |
| 654 | * something catastrophic has happened |
| 655 | */ |
| 656 | if (user_debug & UDBG_SYSCALL) { |
| 657 | printk("[%d] %s: arm syscall %d\n", |
| 658 | task_pid_nr(current), current->comm, no); |
| 659 | dump_instr("", regs); |
| 660 | if (user_mode(regs)) { |
| 661 | __show_regs(regs); |
| 662 | c_backtrace(regs->ARM_fp, processor_mode(regs)); |
| 663 | } |
| 664 | } |
| 665 | #endif |
| 666 | info.si_signo = SIGILL; |
| 667 | info.si_errno = 0; |
| 668 | info.si_code = ILL_ILLTRP; |
| 669 | info.si_addr = (void __user *)instruction_pointer(regs) - |
| 670 | (thumb_mode(regs) ? 2 : 4); |
| 671 | |
| 672 | arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | #ifdef CONFIG_TLS_REG_EMUL |
| 677 | |
| 678 | /* |
| 679 | * We might be running on an ARMv6+ processor which should have the TLS |
| 680 | * register but for some reason we can't use it, or maybe an SMP system |
| 681 | * using a pre-ARMv6 processor (there are apparently a few prototypes like |
| 682 | * that in existence) and therefore access to that register must be |
| 683 | * emulated. |
| 684 | */ |
| 685 | |
| 686 | static int get_tp_trap(struct pt_regs *regs, unsigned int instr) |
| 687 | { |
| 688 | int reg = (instr >> 12) & 15; |
| 689 | if (reg == 15) |
| 690 | return 1; |
| 691 | |
| 692 | /*Fix for HUB: CVE-2014-9870*/ |
| 693 | regs->uregs[reg] = current_thread_info()->tp_value[0]; |
| 694 | regs->ARM_pc += 4; |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static struct undef_hook arm_mrc_hook = { |
| 699 | .instr_mask = 0x0fff0fff, |
| 700 | .instr_val = 0x0e1d0f70, |
| 701 | .cpsr_mask = PSR_T_BIT, |
| 702 | .cpsr_val = 0, |
| 703 | .fn = get_tp_trap, |
| 704 | }; |
| 705 | |
| 706 | static int __init arm_mrc_hook_init(void) |
| 707 | { |
| 708 | register_undef_hook(&arm_mrc_hook); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | late_initcall(arm_mrc_hook_init); |
| 713 | |
| 714 | #endif |
| 715 | |
| 716 | void __bad_xchg(volatile void *ptr, int size) |
| 717 | { |
| 718 | printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n", |
| 719 | __builtin_return_address(0), ptr, size); |
| 720 | BUG(); |
| 721 | } |
| 722 | EXPORT_SYMBOL(__bad_xchg); |
| 723 | |
| 724 | /* |
| 725 | * A data abort trap was taken, but we did not handle the instruction. |
| 726 | * Try to abort the user program, or panic if it was the kernel. |
| 727 | */ |
| 728 | asmlinkage void |
| 729 | baddataabort(int code, unsigned long instr, struct pt_regs *regs) |
| 730 | { |
| 731 | unsigned long addr = instruction_pointer(regs); |
| 732 | siginfo_t info; |
| 733 | |
| 734 | #ifdef CONFIG_DEBUG_USER |
| 735 | if (user_debug & UDBG_BADABORT) { |
| 736 | printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n", |
| 737 | task_pid_nr(current), current->comm, code, instr); |
| 738 | dump_instr(KERN_ERR, regs); |
| 739 | show_pte(current->mm, addr); |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | info.si_signo = SIGILL; |
| 744 | info.si_errno = 0; |
| 745 | info.si_code = ILL_ILLOPC; |
| 746 | info.si_addr = (void __user *)addr; |
| 747 | |
| 748 | arm_notify_die("unknown data abort code", regs, &info, instr, 0); |
| 749 | } |
| 750 | |
| 751 | void __readwrite_bug(const char *fn) |
| 752 | { |
| 753 | printk("%s called, but not implemented\n", fn); |
| 754 | BUG(); |
| 755 | } |
| 756 | EXPORT_SYMBOL(__readwrite_bug); |
| 757 | |
| 758 | void __pte_error(const char *file, int line, pte_t pte) |
| 759 | { |
| 760 | printk("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte)); |
| 761 | } |
| 762 | |
| 763 | void __pmd_error(const char *file, int line, pmd_t pmd) |
| 764 | { |
| 765 | printk("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd)); |
| 766 | } |
| 767 | |
| 768 | void __pgd_error(const char *file, int line, pgd_t pgd) |
| 769 | { |
| 770 | printk("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd)); |
| 771 | } |
| 772 | |
| 773 | asmlinkage void __div0(void) |
| 774 | { |
| 775 | printk("Division by zero in kernel.\n"); |
| 776 | dump_stack(); |
| 777 | } |
| 778 | EXPORT_SYMBOL(__div0); |
| 779 | |
| 780 | void abort(void) |
| 781 | { |
| 782 | BUG(); |
| 783 | |
| 784 | /* if that doesn't kill us, halt */ |
| 785 | panic("Oops failed to kill thread"); |
| 786 | } |
| 787 | EXPORT_SYMBOL(abort); |
| 788 | |
| 789 | void __init trap_init(void) |
| 790 | { |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | static void __init kuser_get_tls_init(unsigned long vectors) |
| 795 | { |
| 796 | /* |
| 797 | * vectors + 0xfe0 = __kuser_get_tls |
| 798 | * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8 |
| 799 | */ |
| 800 | if (tls_emu || has_tls_reg) |
| 801 | memcpy((void *)vectors + 0xfe0, (void *)vectors + 0xfe8, 4); |
| 802 | } |
| 803 | |
| 804 | void __init early_trap_init(void *vectors_base) |
| 805 | { |
| 806 | unsigned long vectors = (unsigned long)vectors_base; |
| 807 | extern char __stubs_start[], __stubs_end[]; |
| 808 | extern char __vectors_start[], __vectors_end[]; |
| 809 | extern char __kuser_helper_start[], __kuser_helper_end[]; |
| 810 | int kuser_sz = __kuser_helper_end - __kuser_helper_start; |
| 811 | |
| 812 | vectors_page = vectors_base; |
| 813 | |
| 814 | /* |
| 815 | * Copy the vectors, stubs and kuser helpers (in entry-armv.S) |
| 816 | * into the vector page, mapped at 0xffff0000, and ensure these |
| 817 | * are visible to the instruction stream. |
| 818 | */ |
| 819 | memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start); |
| 820 | memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start); |
| 821 | memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz); |
| 822 | |
| 823 | /* |
| 824 | * Do processor specific fixups for the kuser helpers |
| 825 | */ |
| 826 | kuser_get_tls_init(vectors); |
| 827 | |
| 828 | /* |
| 829 | * Copy signal return handlers into the vector page, and |
| 830 | * set sigreturn to be a pointer to these. |
| 831 | */ |
| 832 | memcpy((void *)(vectors + KERN_SIGRETURN_CODE - CONFIG_VECTORS_BASE), |
| 833 | sigreturn_codes, sizeof(sigreturn_codes)); |
| 834 | memcpy((void *)(vectors + KERN_RESTART_CODE - CONFIG_VECTORS_BASE), |
| 835 | syscall_restart_code, sizeof(syscall_restart_code)); |
| 836 | |
| 837 | flush_icache_range(vectors, vectors + PAGE_SIZE); |
| 838 | modify_domain(DOMAIN_USER, DOMAIN_CLIENT); |
| 839 | } |