1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
4 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 *
6 * SPDX-License-Identifier: MIT
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12 * the Software, and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * https://www.FreeRTOS.org
26 * https://github.com/FreeRTOS
27 *
28 */
29
30 /*
31 * Xtensa-specific interrupt and exception functions for RTOS ports.
32 * Also see xtensa_intr_asm.S.
33 */
34
35 #include <stdlib.h>
36
37 #include <xtensa/config/core.h>
38
39 #include "xtensa_api.h"
40
41
42 #if XCHAL_HAVE_EXCEPTIONS
43
44 /* Handler table is in xtensa_intr_asm.S */
45
46 extern xt_exc_handler _xt_exception_table[XCHAL_EXCCAUSE_NUM];
47
48
49 /*
50 Default handler for unhandled exceptions.
51 */
xt_unhandled_exception(XtExcFrame * frame)52 void xt_unhandled_exception(XtExcFrame *frame)
53 {
54 exit(-1);
55 }
56
57
58 /*
59 This function registers a handler for the specified exception.
60 The function returns the address of the previous handler.
61 On error, it returns 0.
62 */
xt_set_exception_handler(int n,xt_exc_handler f)63 xt_exc_handler xt_set_exception_handler(int n, xt_exc_handler f)
64 {
65 xt_exc_handler old;
66
67 if( n < 0 || n >= XCHAL_EXCCAUSE_NUM )
68 return 0; /* invalid exception number */
69
70 old = _xt_exception_table[n];
71
72 if (f) {
73 _xt_exception_table[n] = f;
74 }
75 else {
76 _xt_exception_table[n] = &xt_unhandled_exception;
77 }
78
79 return ((old == &xt_unhandled_exception) ? 0 : old);
80 }
81
82 #endif
83
84 #if XCHAL_HAVE_INTERRUPTS
85
86 /* Handler table is in xtensa_intr_asm.S */
87
88 typedef struct xt_handler_table_entry {
89 void * handler;
90 void * arg;
91 } xt_handler_table_entry;
92
93 extern xt_handler_table_entry _xt_interrupt_table[XCHAL_NUM_INTERRUPTS];
94
95
96 /*
97 Default handler for unhandled interrupts.
98 */
xt_unhandled_interrupt(void * arg)99 void xt_unhandled_interrupt(void * arg)
100 {
101 exit(-1);
102 }
103
104
105 /*
106 This function registers a handler for the specified interrupt. The "arg"
107 parameter specifies the argument to be passed to the handler when it is
108 invoked. The function returns the address of the previous handler.
109 On error, it returns 0.
110 */
xt_set_interrupt_handler(int n,xt_handler f,void * arg)111 xt_handler xt_set_interrupt_handler(int n, xt_handler f, void * arg)
112 {
113 xt_handler_table_entry * entry;
114 xt_handler old;
115
116 if( n < 0 || n >= XCHAL_NUM_INTERRUPTS )
117 return 0; /* invalid interrupt number */
118 if( Xthal_intlevel[n] > XCHAL_EXCM_LEVEL )
119 return 0; /* priority level too high to safely handle in C */
120
121 entry = _xt_interrupt_table + n;
122 old = entry->handler;
123
124 if (f) {
125 entry->handler = f;
126 entry->arg = arg;
127 }
128 else {
129 entry->handler = &xt_unhandled_interrupt;
130 entry->arg = (void*)n;
131 }
132
133 return ((old == &xt_unhandled_interrupt) ? 0 : old);
134 }
135
136
137 #endif /* XCHAL_HAVE_INTERRUPTS */
138