traps.c 22 KB

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