traps.c 22 KB

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