xref: /nrf52832-nimble/rt-thread/libcpu/mips/loongson_1b/exception.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * File      : cpu.c
3  * This file is part of RT-Thread RTOS
4  * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
5  *
6  * The license and distribution terms for this file may be
7  * found in the file LICENSE in this distribution or at
8  * http://www.rt-thread.org/license/LICENSE
9  *
10  * Change Logs:
11  * Date           Author       Notes
12  * 2010-05-17     swkyer       first version
13  */
14 #include <rtthread.h>
15 #include <rthw.h>
16 #include "../common/exception.h"
17 #include "../common/mipsregs.h"
18 
19 /**
20  * @addtogroup Loongson
21  */
22 
23 /*@{*/
24 
25 /**
26  * exception handle table
27  */
28 #define RT_EXCEPTION_MAX	8
29 exception_func_t sys_exception_handlers[RT_EXCEPTION_MAX];
30 
31 /**
32  * setup the exception handle
33  */
rt_set_except_vector(int n,exception_func_t func)34 exception_func_t rt_set_except_vector(int n, exception_func_t func)
35 {
36     exception_func_t old_handler = sys_exception_handlers[n];
37 
38     if ((n == 0) || (n > RT_EXCEPTION_MAX) || (!func))
39     {
40         return 0;
41     }
42 
43     sys_exception_handlers[n] = func;
44 
45     return old_handler;
46 }
47 
tlb_refill_handler(void)48 void tlb_refill_handler(void)
49 {
50 	rt_kprintf("tlb-miss happens, epc: 0x%08x\n", read_c0_epc());
51 	rt_hw_cpu_shutdown();
52 }
53 
cache_error_handler(void)54 void cache_error_handler(void)
55 {
56 	rt_kprintf("cache exception happens, epc: 0x%08x\n", read_c0_epc());
57 	rt_hw_cpu_shutdown();
58 }
59 
unhandled_exception_handle(pt_regs_t * regs)60 static void unhandled_exception_handle(pt_regs_t *regs)
61 {
62 	rt_kprintf("exception happens, epc: 0x%08x, cause: 0x%08x\n", regs->cp0_epc, read_c0_cause());
63 }
64 
install_default_execpt_handle(void)65 void install_default_execpt_handle(void)
66 {
67 	rt_int32_t i;
68 
69 	for (i=0; i<RT_EXCEPTION_MAX; i++)
70 		sys_exception_handlers[i] = (exception_func_t)unhandled_exception_handle;
71 }
72 
exception_handler(pt_regs_t * regs)73 void exception_handler(pt_regs_t *regs)
74 {
75 	rt_uint32_t cause;
76 	rt_uint32_t index;
77 
78 	cause = (read_c0_cause() & read_c0_config());
79 	cause = (cause & 0xfc00) >> 8;
80 
81 	for (index = RT_EXCEPTION_MAX; index > 0; index --)
82 	{
83 		if (cause & (1 << index))
84 		{
85 			sys_exception_handlers[index](regs);
86 			cause &= ~(1 << index);
87 		}
88 	}
89 }
90 
91 /*@}*/
92