traps.c 22 KB

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