traps.c 23 KB

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