blob: 57235e5c0cea9ad91707b2f406d6f92a104ac5bd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Vineet Gupta054419e2013-01-18 15:12:18 +05302/*
3 * Traps/Non-MMU Exception handling for ARC
4 *
5 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 *
Vineet Gupta2e651ea2013-01-23 16:30:36 +05307 * vineetg: May 2011
8 * -user-space unaligned access emulation
9 *
Vineet Gupta054419e2013-01-18 15:12:18 +053010 * Rahul Trivedi: Codito Technologies 2004
11 */
12
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Vineet Gupta054419e2013-01-18 15:12:18 +053014#include <linux/kdebug.h>
15#include <linux/uaccess.h>
Sachin Kamat1ec9db12013-03-06 16:53:44 +053016#include <linux/ptrace.h>
17#include <linux/kprobes.h>
18#include <linux/kgdb.h>
Vineet Gupta054419e2013-01-18 15:12:18 +053019#include <asm/setup.h>
Vineet Gupta2e651ea2013-01-23 16:30:36 +053020#include <asm/unaligned.h>
Christian Ruppert5b000292013-04-09 11:50:45 +020021#include <asm/kprobes.h>
Vineet Gupta054419e2013-01-18 15:12:18 +053022
23void __init trap_init(void)
24{
25 return;
26}
27
Vineet Gupta38a9ff62013-06-12 15:13:40 +053028void die(const char *str, struct pt_regs *regs, unsigned long address)
Vineet Gupta054419e2013-01-18 15:12:18 +053029{
Vineet Gupta38a9ff62013-06-12 15:13:40 +053030 show_kernel_fault_diag(str, regs, address);
Vineet Gupta054419e2013-01-18 15:12:18 +053031
32 /* DEAD END */
33 __asm__("flag 1");
34}
35
36/*
37 * Helper called for bulk of exceptions NOT needing specific handling
38 * -for user faults enqueues requested signal
39 * -for kernel, chk if due to copy_(to|from)_user, otherwise die()
40 */
Vineet Gupta38a9ff62013-06-12 15:13:40 +053041static noinline int
Eric W. Biederman44452292018-04-19 19:20:58 -050042unhandled_exception(const char *str, struct pt_regs *regs,
43 int signo, int si_code, void __user *addr)
Vineet Gupta054419e2013-01-18 15:12:18 +053044{
45 if (user_mode(regs)) {
46 struct task_struct *tsk = current;
47
Eric W. Biederman44452292018-04-19 19:20:58 -050048 tsk->thread.fault_address = (__force unsigned int)addr;
Vineet Gupta054419e2013-01-18 15:12:18 +053049
Eric W. Biederman2e1661d22019-05-23 11:04:24 -050050 force_sig_fault(signo, si_code, addr);
Vineet Gupta054419e2013-01-18 15:12:18 +053051
52 } else {
53 /* If not due to copy_(to|from)_user, we are doomed */
54 if (fixup_exception(regs))
55 return 0;
56
Eric W. Biederman44452292018-04-19 19:20:58 -050057 die(str, regs, (unsigned long)addr);
Vineet Gupta054419e2013-01-18 15:12:18 +053058 }
59
60 return 1;
61}
62
63#define DO_ERROR_INFO(signr, str, name, sicode) \
Vineet Gupta38a9ff62013-06-12 15:13:40 +053064int name(unsigned long address, struct pt_regs *regs) \
Eric W. Biederman44452292018-04-19 19:20:58 -050065{ \
66 return unhandled_exception(str, regs, signr, sicode, \
67 (void __user *)address); \
Vineet Gupta054419e2013-01-18 15:12:18 +053068}
69
70/*
71 * Entry points for exceptions NOT needing specific handling
72 */
73DO_ERROR_INFO(SIGILL, "Priv Op/Disabled Extn", do_privilege_fault, ILL_PRVOPC)
74DO_ERROR_INFO(SIGILL, "Invalid Extn Insn", do_extension_fault, ILL_ILLOPC)
75DO_ERROR_INFO(SIGILL, "Illegal Insn (or Seq)", insterror_is_error, ILL_ILLOPC)
Noam Camus98339492017-06-13 17:03:45 +030076DO_ERROR_INFO(SIGBUS, "Invalid Mem Access", __weak do_memory_error, BUS_ADRERR)
Vineet Gupta054419e2013-01-18 15:12:18 +053077DO_ERROR_INFO(SIGTRAP, "Breakpoint Set", trap_is_brkpt, TRAP_BRKPT)
Vineet Guptac723ea42013-04-04 14:37:52 +053078DO_ERROR_INFO(SIGBUS, "Misaligned Access", do_misaligned_error, BUS_ADRALN)
Vineet Guptaf5a16b92017-12-20 12:37:54 -080079DO_ERROR_INFO(SIGSEGV, "gcc generated __builtin_trap", do_trap5_error, 0)
Vineet Gupta054419e2013-01-18 15:12:18 +053080
Vineet Gupta2e651ea2013-01-23 16:30:36 +053081/*
82 * Entry Point for Misaligned Data access Exception, for emulating in software
83 */
Vineet Gupta38a9ff62013-06-12 15:13:40 +053084int do_misaligned_access(unsigned long address, struct pt_regs *regs,
85 struct callee_regs *cregs)
Vineet Gupta2e651ea2013-01-23 16:30:36 +053086{
Vineet Gupta07ba69a2013-09-18 18:08:01 +053087 /* If emulation not enabled, or failed, kill the task */
Vineet Gupta38a9ff62013-06-12 15:13:40 +053088 if (misaligned_fixup(address, regs, cregs) != 0)
89 return do_misaligned_error(address, regs);
Vineet Gupta2e651ea2013-01-23 16:30:36 +053090
Vineet Gupta2e651ea2013-01-23 16:30:36 +053091 return 0;
92}
Vineet Gupta054419e2013-01-18 15:12:18 +053093
94/*
95 * Entry point for miscll errors such as Nested Exceptions
96 * -Duplicate TLB entry is handled seperately though
97 */
Vineet Gupta38a9ff62013-06-12 15:13:40 +053098void do_machine_check_fault(unsigned long address, struct pt_regs *regs)
Vineet Gupta054419e2013-01-18 15:12:18 +053099{
Jose Abreuaa7e3a52017-08-29 10:14:20 +0100100 die("Unhandled Machine Check Exception", regs, address);
Vineet Gupta054419e2013-01-18 15:12:18 +0530101}
102
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530103
Vineet Gupta054419e2013-01-18 15:12:18 +0530104/*
105 * Entry point for traps induced by ARCompact TRAP_S <n> insn
106 * This is same family as TRAP0/SWI insn (use the same vector).
107 * The only difference being SWI insn take no operand, while TRAP_S does
108 * which reflects in ECR Reg as 8 bit param.
109 * Thus TRAP_S <n> can be used for specific purpose
110 * -1 used for software breakpointing (gdb)
111 * -2 used by kprobes
Vineet Guptaf5a16b92017-12-20 12:37:54 -0800112 * -5 __builtin_trap() generated by gcc (2018.03 onwards) for toggle such as
113 * -fno-isolate-erroneous-paths-dereference
Vineet Gupta054419e2013-01-18 15:12:18 +0530114 */
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530115void do_non_swi_trap(unsigned long address, struct pt_regs *regs)
Vineet Gupta054419e2013-01-18 15:12:18 +0530116{
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530117 unsigned int param = regs->ecr_param;
Vineet Gupta054419e2013-01-18 15:12:18 +0530118
119 switch (param) {
120 case 1:
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530121 trap_is_brkpt(address, regs);
Vineet Gupta054419e2013-01-18 15:12:18 +0530122 break;
123
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530124 case 2:
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530125 trap_is_kprobe(address, regs);
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530126 break;
127
Mischa Jonkerf46121b2013-01-18 15:12:24 +0530128 case 3:
129 case 4:
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530130 kgdb_trap(regs);
Mischa Jonkerf46121b2013-01-18 15:12:24 +0530131 break;
132
Vineet Guptaf5a16b92017-12-20 12:37:54 -0800133 case 5:
134 do_trap5_error(address, regs);
135 break;
Vineet Gupta054419e2013-01-18 15:12:18 +0530136 default:
137 break;
138 }
139}
140
141/*
142 * Entry point for Instruction Error Exception
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530143 * -For a corner case, ARC kprobes implementation resorts to using
144 * this exception, hence the check
Vineet Gupta054419e2013-01-18 15:12:18 +0530145 */
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530146void do_insterror_or_kprobe(unsigned long address, struct pt_regs *regs)
Vineet Gupta054419e2013-01-18 15:12:18 +0530147{
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530148 int rc;
149
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530150 /* Check if this exception is caused by kprobes */
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530151 rc = notify_die(DIE_IERR, "kprobe_ierr", regs, address, 0, SIGILL);
152 if (rc == NOTIFY_STOP)
Vineet Gupta4d86dfb2013-01-22 17:03:59 +0530153 return;
154
Vineet Gupta38a9ff62013-06-12 15:13:40 +0530155 insterror_is_error(address, regs);
Vineet Gupta054419e2013-01-18 15:12:18 +0530156}
Vineet Guptaaf1be2e2017-12-08 08:45:57 -0800157
158/*
159 * abort() call generated by older gcc for __builtin_trap()
160 */
161void abort(void)
162{
163 __asm__ __volatile__("trap_s 5\n");
164}