xref: /openbmc/linux/arch/arc/kernel/traps.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2054419edSVineet Gupta /*
3054419edSVineet Gupta  * Traps/Non-MMU Exception handling for ARC
4054419edSVineet Gupta  *
5054419edSVineet Gupta  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6054419edSVineet Gupta  *
72e651ea1SVineet Gupta  * vineetg: May 2011
82e651ea1SVineet Gupta  *  -user-space unaligned access emulation
92e651ea1SVineet Gupta  *
10054419edSVineet Gupta  * Rahul Trivedi: Codito Technologies 2004
11054419edSVineet Gupta  */
12054419edSVineet Gupta 
133f07c014SIngo Molnar #include <linux/sched/signal.h>
14054419edSVineet Gupta #include <linux/kdebug.h>
15054419edSVineet Gupta #include <linux/uaccess.h>
161ec9db10SSachin Kamat #include <linux/ptrace.h>
171ec9db10SSachin Kamat #include <linux/kprobes.h>
181ec9db10SSachin Kamat #include <linux/kgdb.h>
194d369680SVineet Gupta #include <asm/entry.h>
20054419edSVineet Gupta #include <asm/setup.h>
212e651ea1SVineet Gupta #include <asm/unaligned.h>
225b00029eSChristian Ruppert #include <asm/kprobes.h>
23054419edSVineet Gupta 
die(const char * str,struct pt_regs * regs,unsigned long address)2438a9ff6dSVineet Gupta void die(const char *str, struct pt_regs *regs, unsigned long address)
25054419edSVineet Gupta {
2638a9ff6dSVineet Gupta 	show_kernel_fault_diag(str, regs, address);
27054419edSVineet Gupta 
28054419edSVineet Gupta 	/* DEAD END */
29054419edSVineet Gupta 	__asm__("flag 1");
30054419edSVineet Gupta }
31054419edSVineet Gupta 
32054419edSVineet Gupta /*
33054419edSVineet Gupta  * Helper called for bulk of exceptions NOT needing specific handling
34054419edSVineet Gupta  *  -for user faults enqueues requested signal
35054419edSVineet Gupta  *  -for kernel, chk if due to copy_(to|from)_user, otherwise die()
36054419edSVineet Gupta  */
3738a9ff6dSVineet Gupta static noinline int
unhandled_exception(const char * str,struct pt_regs * regs,int signo,int si_code,void __user * addr)3844452294SEric W. Biederman unhandled_exception(const char *str, struct pt_regs *regs,
3944452294SEric W. Biederman 		    int signo, int si_code, void __user *addr)
40054419edSVineet Gupta {
41054419edSVineet Gupta 	if (user_mode(regs)) {
42054419edSVineet Gupta 		struct task_struct *tsk = current;
43054419edSVineet Gupta 
4444452294SEric W. Biederman 		tsk->thread.fault_address = (__force unsigned int)addr;
45054419edSVineet Gupta 
462e1661d2SEric W. Biederman 		force_sig_fault(signo, si_code, addr);
47054419edSVineet Gupta 
48054419edSVineet Gupta 	} else {
49054419edSVineet Gupta 		/* If not due to copy_(to|from)_user, we are doomed */
50054419edSVineet Gupta 		if (fixup_exception(regs))
51054419edSVineet Gupta 			return 0;
52054419edSVineet Gupta 
5344452294SEric W. Biederman 		die(str, regs, (unsigned long)addr);
54054419edSVineet Gupta 	}
55054419edSVineet Gupta 
56054419edSVineet Gupta 	return 1;
57054419edSVineet Gupta }
58054419edSVineet Gupta 
59054419edSVineet Gupta #define DO_ERROR_INFO(signr, str, name, sicode) \
6038a9ff6dSVineet Gupta int name(unsigned long address, struct pt_regs *regs) \
61054419edSVineet Gupta {								\
6244452294SEric W. Biederman 	return unhandled_exception(str, regs, signr, sicode,	\
6344452294SEric W. Biederman 				   (void __user *)address);	\
64054419edSVineet Gupta }
65054419edSVineet Gupta 
66054419edSVineet Gupta /*
67054419edSVineet Gupta  * Entry points for exceptions NOT needing specific handling
68054419edSVineet Gupta  */
69054419edSVineet Gupta DO_ERROR_INFO(SIGILL, "Priv Op/Disabled Extn", do_privilege_fault, ILL_PRVOPC)
70054419edSVineet Gupta DO_ERROR_INFO(SIGILL, "Invalid Extn Insn", do_extension_fault, ILL_ILLOPC)
71054419edSVineet Gupta DO_ERROR_INFO(SIGILL, "Illegal Insn (or Seq)", insterror_is_error, ILL_ILLOPC)
7298339495SNoam Camus DO_ERROR_INFO(SIGBUS, "Invalid Mem Access", __weak do_memory_error, BUS_ADRERR)
73054419edSVineet Gupta DO_ERROR_INFO(SIGTRAP, "Breakpoint Set", trap_is_brkpt, TRAP_BRKPT)
74c723ea46SVineet Gupta DO_ERROR_INFO(SIGBUS, "Misaligned Access", do_misaligned_error, BUS_ADRALN)
75f5a16b93SVineet Gupta DO_ERROR_INFO(SIGSEGV, "gcc generated __builtin_trap", do_trap5_error, 0)
76054419edSVineet Gupta 
772e651ea1SVineet Gupta /*
782e651ea1SVineet Gupta  * Entry Point for Misaligned Data access Exception, for emulating in software
792e651ea1SVineet Gupta  */
do_misaligned_access(unsigned long address,struct pt_regs * regs,struct callee_regs * cregs)8038a9ff6dSVineet Gupta int do_misaligned_access(unsigned long address, struct pt_regs *regs,
8138a9ff6dSVineet Gupta 			 struct callee_regs *cregs)
822e651ea1SVineet Gupta {
8307ba69a4SVineet Gupta 	/* If emulation not enabled, or failed, kill the task */
8438a9ff6dSVineet Gupta 	if (misaligned_fixup(address, regs, cregs) != 0)
8538a9ff6dSVineet Gupta 		return do_misaligned_error(address, regs);
862e651ea1SVineet Gupta 
872e651ea1SVineet Gupta 	return 0;
882e651ea1SVineet Gupta }
89054419edSVineet Gupta 
90054419edSVineet Gupta /*
91054419edSVineet Gupta  * Entry point for miscll errors such as Nested Exceptions
92054419edSVineet Gupta  *  -Duplicate TLB entry is handled seperately though
93054419edSVineet Gupta  */
do_machine_check_fault(unsigned long address,struct pt_regs * regs)9438a9ff6dSVineet Gupta void do_machine_check_fault(unsigned long address, struct pt_regs *regs)
95054419edSVineet Gupta {
96aa7e3a5eSJose Abreu 	die("Unhandled Machine Check Exception", regs, address);
97054419edSVineet Gupta }
98054419edSVineet Gupta 
994d86dfbbSVineet Gupta 
100054419edSVineet Gupta /*
101054419edSVineet Gupta  * Entry point for traps induced by ARCompact TRAP_S <n> insn
102054419edSVineet Gupta  * This is same family as TRAP0/SWI insn (use the same vector).
103054419edSVineet Gupta  * The only difference being SWI insn take no operand, while TRAP_S does
104054419edSVineet Gupta  * which reflects in ECR Reg as 8 bit param.
105054419edSVineet Gupta  * Thus TRAP_S <n> can be used for specific purpose
106054419edSVineet Gupta  *  -1 used for software breakpointing (gdb)
107054419edSVineet Gupta  *  -2 used by kprobes
108f5a16b93SVineet Gupta  *  -5 __builtin_trap() generated by gcc (2018.03 onwards) for toggle such as
109f5a16b93SVineet Gupta  *     -fno-isolate-erroneous-paths-dereference
110054419edSVineet Gupta  */
do_non_swi_trap(unsigned long address,struct pt_regs * regs)11138a9ff6dSVineet Gupta void do_non_swi_trap(unsigned long address, struct pt_regs *regs)
112054419edSVineet Gupta {
113*58d9ceb7SVineet Gupta 	switch (regs->ecr.param) {
114054419edSVineet Gupta 	case 1:
11538a9ff6dSVineet Gupta 		trap_is_brkpt(address, regs);
116054419edSVineet Gupta 		break;
117054419edSVineet Gupta 
1184d86dfbbSVineet Gupta 	case 2:
11938a9ff6dSVineet Gupta 		trap_is_kprobe(address, regs);
1204d86dfbbSVineet Gupta 		break;
1214d86dfbbSVineet Gupta 
122f46121bdSMischa Jonker 	case 3:
123f46121bdSMischa Jonker 	case 4:
12438a9ff6dSVineet Gupta 		kgdb_trap(regs);
125f46121bdSMischa Jonker 		break;
126f46121bdSMischa Jonker 
127f5a16b93SVineet Gupta 	case 5:
128f5a16b93SVineet Gupta 		do_trap5_error(address, regs);
129f5a16b93SVineet Gupta 		break;
130054419edSVineet Gupta 	default:
131054419edSVineet Gupta 		break;
132054419edSVineet Gupta 	}
133054419edSVineet Gupta }
134054419edSVineet Gupta 
135054419edSVineet Gupta /*
136054419edSVineet Gupta  * Entry point for Instruction Error Exception
1374d86dfbbSVineet Gupta  *  -For a corner case, ARC kprobes implementation resorts to using
1384d86dfbbSVineet Gupta  *   this exception, hence the check
139054419edSVineet Gupta  */
do_insterror_or_kprobe(unsigned long address,struct pt_regs * regs)14038a9ff6dSVineet Gupta void do_insterror_or_kprobe(unsigned long address, struct pt_regs *regs)
141054419edSVineet Gupta {
14238a9ff6dSVineet Gupta 	int rc;
14338a9ff6dSVineet Gupta 
1444d86dfbbSVineet Gupta 	/* Check if this exception is caused by kprobes */
14538a9ff6dSVineet Gupta 	rc = notify_die(DIE_IERR, "kprobe_ierr", regs, address, 0, SIGILL);
14638a9ff6dSVineet Gupta 	if (rc == NOTIFY_STOP)
1474d86dfbbSVineet Gupta 		return;
1484d86dfbbSVineet Gupta 
14938a9ff6dSVineet Gupta 	insterror_is_error(address, regs);
150054419edSVineet Gupta }
151af1be2e2SVineet Gupta 
152af1be2e2SVineet Gupta /*
153af1be2e2SVineet Gupta  * abort() call generated by older gcc for __builtin_trap()
154af1be2e2SVineet Gupta  */
abort(void)155af1be2e2SVineet Gupta void abort(void)
156af1be2e2SVineet Gupta {
157af1be2e2SVineet Gupta 	__asm__ __volatile__("trap_s  5\n");
158af1be2e2SVineet Gupta }
159