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