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