1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "hardware/exception.h"
8 #include "hardware/sync.h"
9 #include "pico/platform/cpu_regs.h"
10 
11 #ifndef exception_is_compile_time_default
exception_is_compile_time_default(exception_handler_t handler)12 static bool exception_is_compile_time_default(exception_handler_t handler) {
13 #ifdef __riscv
14     extern char __unhandled_exception;
15     return (uintptr_t)handler == (uintptr_t)__unhandled_exception;
16 #else
17     extern char __default_isrs_start;
18     extern char __default_isrs_end;
19     return ((uintptr_t)handler) >= (uintptr_t)&__default_isrs_start &&
20             ((uintptr_t)handler) < (uintptr_t)&__default_isrs_end;
21 #endif
22 }
23 #endif
24 
get_exception_table(void)25 static inline exception_handler_t *get_exception_table(void) {
26 #ifdef __riscv
27     extern uintptr_t __riscv_exception_table;
28     return (exception_handler_t *) &__riscv_exception_table;
29 #else
30     return (exception_handler_t *) scb_hw->vtor;
31 #endif
32 }
33 
set_raw_exception_handler_and_restore_interrupts(enum exception_number num,exception_handler_t handler,uint32_t save)34 static void set_raw_exception_handler_and_restore_interrupts(enum exception_number num, exception_handler_t handler, uint32_t save) {
35     // update vtable (vtable_handler may be same or updated depending on cases, but we do it anyway for compactness)
36     get_exception_table()[num] = handler;
37     __dmb();
38     restore_interrupts_from_disabled(save);
39 }
40 
check_exception_param(__unused enum exception_number num)41 static inline void check_exception_param(__unused enum exception_number num) {
42     invalid_params_if(HARDWARE_EXCEPTION, num < MIN_EXCEPTION_NUM || num > MAX_EXCEPTION_NUM);
43 }
44 
exception_get_vtable_handler(enum exception_number num)45 exception_handler_t exception_get_vtable_handler(enum exception_number num) {
46     check_exception_param(num);
47     return get_exception_table()[num];
48 }
49 
exception_set_exclusive_handler(enum exception_number num,exception_handler_t handler)50 exception_handler_t exception_set_exclusive_handler(enum exception_number num, exception_handler_t handler) {
51     check_exception_param(num);
52 #if !PICO_NO_RAM_VECTOR_TABLE
53     uint32_t save = save_and_disable_interrupts();
54     exception_handler_t current = exception_get_vtable_handler(num);
55     hard_assert(handler == current || exception_is_compile_time_default(current));
56     set_raw_exception_handler_and_restore_interrupts(num, handler, save);
57 #else
58     panic_unsupported();
59 #endif
60     return current;
61 }
62 
exception_restore_handler(enum exception_number num,exception_handler_t original_handler)63 void exception_restore_handler(enum exception_number num, exception_handler_t original_handler) {
64     hard_assert(exception_is_compile_time_default(original_handler));
65 #if !PICO_NO_RAM_VECTOR_TABLE
66     uint32_t save = save_and_disable_interrupts();
67     set_raw_exception_handler_and_restore_interrupts(num, original_handler, save);
68 #else
69     panic_unsupported();
70 #endif
71 }
72 
73 #ifndef __riscv
74 
get_shpr0(uint num)75 static io_rw_32 *get_shpr0(uint num) {
76     io_rw_32 *shpr0 = NULL;
77 #if __ARM_ARCH_6M__
78     // only has shpr2-3
79     if (num >= 8 && num < 16) shpr0 = (io_rw_32 *) (PPB_BASE + ARM_CPU_PREFIXED(SHPR2_OFFSET) - 8);
80 #elif __ARM_ARCH_8M_MAIN__
81     // only has shpr1-3
82     if (num >= 4 && num < 16) shpr0 = (io_rw_32 *)(PPB_BASE + ARM_CPU_PREFIXED(SHPR1_OFFSET) - 4);
83 #endif
84     return shpr0;
85 }
86 
exception_set_priority(uint num,uint8_t hardware_priority)87 bool exception_set_priority(uint num, uint8_t hardware_priority) {
88     io_rw_32 *shpr0 = get_shpr0(num);
89     if (shpr0) {
90         // note that only 32 bit writes are supported
91         shpr0[num / 4] = (shpr0[num/4] & ~(0xffu << (8 * (num & 3u)))) | (((uint32_t) hardware_priority) << (8 * (num & 3u)));
92         return true;
93     }
94     return false;
95 }
96 
exception_get_priority(uint num)97 uint exception_get_priority(uint num) {
98     io_rw_32 *shpr0 = get_shpr0(num);
99     if (shpr0) {
100         // note that only 32 bit writes are supported
101         return (uint8_t) (shpr0[num/4] >> (8 * (num & 3)));
102     }
103     return PICO_LOWEST_EXCEPTION_PRIORITY;
104 }
105 #endif