1 /*
2  * Copyright (c) 2020 Antony Pavlov <antonynpavlov@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
11 
z_mips_fatal_error(unsigned int reason,const z_arch_esf_t * esf)12 FUNC_NORETURN void z_mips_fatal_error(unsigned int reason,
13 					  const z_arch_esf_t *esf)
14 {
15 	if (esf != NULL) {
16 		printk("$ 0   :         (ze) %08lx(at) %08lx(v0) %08lx(v1)\n",
17 			esf->at, esf->v0, esf->v1);
18 		printk("$ 4   : %08lx(a0) %08lx(a1) %08lx(a2) %08lx(a3)\n",
19 			esf->a0, esf->a1, esf->a2, esf->a3);
20 		printk("$ 8   : %08lx(t0) %08lx(t1) %08lx(t2) %08lx(t3)\n",
21 			esf->t0, esf->t1, esf->t2, esf->t3);
22 		printk("$12   : %08lx(t4) %08lx(t5) %08lx(t6) %08lx(t7)\n",
23 			esf->t4, esf->t5, esf->t6, esf->t7);
24 		printk("...\n");
25 		printk("$24   : %08lx(t8) %08lx(t9)\n",
26 			esf->t8, esf->t9);
27 		printk("$28   : %08lx(gp)         (sp)         (s8) %08lx(ra)\n",
28 			esf->gp, esf->ra);
29 
30 		printk("EPC   : %08lx\n", esf->epc);
31 
32 		printk("Status: %08lx\n", esf->status);
33 		printk("Cause : %08lx\n", esf->cause);
34 		printk("BadVA : %08lx\n", esf->badvaddr);
35 	}
36 
37 	z_fatal_error(reason, esf);
38 	CODE_UNREACHABLE;
39 }
40 
cause_str(unsigned long cause)41 static char *cause_str(unsigned long cause)
42 {
43 	switch (cause) {
44 	case 0:
45 		return "interrupt pending";
46 	case 1:
47 		return "TLB modified";
48 	case 2:
49 		return "TLB miss on load or ifetch";
50 	case 3:
51 		return "TLB miss on store";
52 	case 4:
53 		return "address error on load or ifetch";
54 	case 5:
55 		return "address error on store";
56 	case 6:
57 		return "bus error on ifetch";
58 	case 7:
59 		return "bus error on load or store";
60 	case 8:
61 		return "system call";
62 	case 9:
63 		return "breakpoint";
64 	case 10:
65 		return "reserved instruction";
66 	case 11:
67 		return "coprocessor unusable";
68 	case 12:
69 		return "arithmetic overflow";
70 	case 13:
71 		return "trap instruction";
72 	case 14:
73 		return "virtual coherency instruction";
74 	case 15:
75 		return "floating point";
76 	case 16:
77 		return "iwatch";
78 	case 23:
79 		return "dwatch";
80 	case 31:
81 		return "virtual coherency data";
82 	default:
83 		return "unknown";
84 	}
85 }
86 
_Fault(z_arch_esf_t * esf)87 void _Fault(z_arch_esf_t *esf)
88 {
89 	unsigned long cause;
90 
91 	cause = (read_c0_cause() & CAUSE_EXP_MASK) >> CAUSE_EXP_SHIFT;
92 
93 	LOG_ERR("");
94 	LOG_ERR(" cause: %ld, %s", cause, cause_str(cause));
95 
96 	z_mips_fatal_error(K_ERR_CPU_EXCEPTION, esf);
97 }
98