xref: /openbmc/linux/arch/arc/kernel/traps.c (revision 750afb08)
1 /*
2  * Traps/Non-MMU Exception handling for ARC
3  *
4  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * vineetg: May 2011
11  *  -user-space unaligned access emulation
12  *
13  * Rahul Trivedi: Codito Technologies 2004
14  */
15 
16 #include <linux/sched/signal.h>
17 #include <linux/kdebug.h>
18 #include <linux/uaccess.h>
19 #include <linux/ptrace.h>
20 #include <linux/kprobes.h>
21 #include <linux/kgdb.h>
22 #include <asm/setup.h>
23 #include <asm/unaligned.h>
24 #include <asm/kprobes.h>
25 
26 void __init trap_init(void)
27 {
28 	return;
29 }
30 
31 void die(const char *str, struct pt_regs *regs, unsigned long address)
32 {
33 	show_kernel_fault_diag(str, regs, address);
34 
35 	/* DEAD END */
36 	__asm__("flag 1");
37 }
38 
39 /*
40  * Helper called for bulk of exceptions NOT needing specific handling
41  *  -for user faults enqueues requested signal
42  *  -for kernel, chk if due to copy_(to|from)_user, otherwise die()
43  */
44 static noinline int
45 unhandled_exception(const char *str, struct pt_regs *regs,
46 		    int signo, int si_code, void __user *addr)
47 {
48 	if (user_mode(regs)) {
49 		struct task_struct *tsk = current;
50 
51 		tsk->thread.fault_address = (__force unsigned int)addr;
52 
53 		force_sig_fault(signo, si_code, addr, tsk);
54 
55 	} else {
56 		/* If not due to copy_(to|from)_user, we are doomed */
57 		if (fixup_exception(regs))
58 			return 0;
59 
60 		die(str, regs, (unsigned long)addr);
61 	}
62 
63 	return 1;
64 }
65 
66 #define DO_ERROR_INFO(signr, str, name, sicode) \
67 int name(unsigned long address, struct pt_regs *regs) \
68 {								\
69 	return unhandled_exception(str, regs, signr, sicode,	\
70 				   (void __user *)address);	\
71 }
72 
73 /*
74  * Entry points for exceptions NOT needing specific handling
75  */
76 DO_ERROR_INFO(SIGILL, "Priv Op/Disabled Extn", do_privilege_fault, ILL_PRVOPC)
77 DO_ERROR_INFO(SIGILL, "Invalid Extn Insn", do_extension_fault, ILL_ILLOPC)
78 DO_ERROR_INFO(SIGILL, "Illegal Insn (or Seq)", insterror_is_error, ILL_ILLOPC)
79 DO_ERROR_INFO(SIGBUS, "Invalid Mem Access", __weak do_memory_error, BUS_ADRERR)
80 DO_ERROR_INFO(SIGTRAP, "Breakpoint Set", trap_is_brkpt, TRAP_BRKPT)
81 DO_ERROR_INFO(SIGBUS, "Misaligned Access", do_misaligned_error, BUS_ADRALN)
82 DO_ERROR_INFO(SIGSEGV, "gcc generated __builtin_trap", do_trap5_error, 0)
83 
84 /*
85  * Entry Point for Misaligned Data access Exception, for emulating in software
86  */
87 int do_misaligned_access(unsigned long address, struct pt_regs *regs,
88 			 struct callee_regs *cregs)
89 {
90 	/* If emulation not enabled, or failed, kill the task */
91 	if (misaligned_fixup(address, regs, cregs) != 0)
92 		return do_misaligned_error(address, regs);
93 
94 	return 0;
95 }
96 
97 /*
98  * Entry point for miscll errors such as Nested Exceptions
99  *  -Duplicate TLB entry is handled seperately though
100  */
101 void do_machine_check_fault(unsigned long address, struct pt_regs *regs)
102 {
103 	die("Unhandled Machine Check Exception", regs, address);
104 }
105 
106 
107 /*
108  * Entry point for traps induced by ARCompact TRAP_S <n> insn
109  * This is same family as TRAP0/SWI insn (use the same vector).
110  * The only difference being SWI insn take no operand, while TRAP_S does
111  * which reflects in ECR Reg as 8 bit param.
112  * Thus TRAP_S <n> can be used for specific purpose
113  *  -1 used for software breakpointing (gdb)
114  *  -2 used by kprobes
115  *  -5 __builtin_trap() generated by gcc (2018.03 onwards) for toggle such as
116  *     -fno-isolate-erroneous-paths-dereference
117  */
118 void do_non_swi_trap(unsigned long address, struct pt_regs *regs)
119 {
120 	unsigned int param = regs->ecr_param;
121 
122 	switch (param) {
123 	case 1:
124 		trap_is_brkpt(address, regs);
125 		break;
126 
127 	case 2:
128 		trap_is_kprobe(address, regs);
129 		break;
130 
131 	case 3:
132 	case 4:
133 		kgdb_trap(regs);
134 		break;
135 
136 	case 5:
137 		do_trap5_error(address, regs);
138 		break;
139 	default:
140 		break;
141 	}
142 }
143 
144 /*
145  * Entry point for Instruction Error Exception
146  *  -For a corner case, ARC kprobes implementation resorts to using
147  *   this exception, hence the check
148  */
149 void do_insterror_or_kprobe(unsigned long address, struct pt_regs *regs)
150 {
151 	int rc;
152 
153 	/* Check if this exception is caused by kprobes */
154 	rc = notify_die(DIE_IERR, "kprobe_ierr", regs, address, 0, SIGILL);
155 	if (rc == NOTIFY_STOP)
156 		return;
157 
158 	insterror_is_error(address, regs);
159 }
160 
161 /*
162  * abort() call generated by older gcc for __builtin_trap()
163  */
164 void abort(void)
165 {
166 	__asm__ __volatile__("trap_s  5\n");
167 }
168