1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * (C) Copyright 2000
5*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * The exception table consists of pairs of addresses: the first is the
13*4882a593Smuzhiyun * address of an instruction that is allowed to fault, and the second is
14*4882a593Smuzhiyun * the address at which the program should continue. No registers are
15*4882a593Smuzhiyun * modified, so it is entirely up to the continuation code to figure out
16*4882a593Smuzhiyun * what to do.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * All the routines below use bits of fixup code that are out of line
19*4882a593Smuzhiyun * with the main instruction path. This means when everything is well,
20*4882a593Smuzhiyun * we don't even have to jump over them. Further, they do not intrude
21*4882a593Smuzhiyun * on our cache or tlb entries.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun struct exception_table_entry
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun unsigned long insn, fixup;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun extern const struct exception_table_entry __start___ex_table[];
32*4882a593Smuzhiyun extern const struct exception_table_entry __stop___ex_table[];
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static inline unsigned long
search_one_table(const struct exception_table_entry * first,const struct exception_table_entry * last,unsigned long value)35*4882a593Smuzhiyun search_one_table(const struct exception_table_entry *first,
36*4882a593Smuzhiyun const struct exception_table_entry *last,
37*4882a593Smuzhiyun unsigned long value)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun long diff;
40*4882a593Smuzhiyun while (first <= last) {
41*4882a593Smuzhiyun diff = first->insn - value;
42*4882a593Smuzhiyun if (diff == 0)
43*4882a593Smuzhiyun return first->fixup;
44*4882a593Smuzhiyun first++;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun return 0;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun unsigned long
search_exception_table(unsigned long addr)51*4882a593Smuzhiyun search_exception_table(unsigned long addr)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun unsigned long ret;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* There is only the kernel to search. */
56*4882a593Smuzhiyun ret = search_one_table(__start___ex_table, __stop___ex_table-1, addr);
57*4882a593Smuzhiyun /* if the serial port does not hang in exception, printf can be used */
58*4882a593Smuzhiyun #if !defined(CONFIG_SYS_SERIAL_HANG_IN_EXCEPTION)
59*4882a593Smuzhiyun debug("Bus Fault @ 0x%08lx, fixup 0x%08lx\n", addr, ret);
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun if (ret) return ret;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun return 0;
64*4882a593Smuzhiyun }
65