1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
7  */
8 
9 /*
10  * This file handles the architecture-dependent parts of hardware
11  * exceptions
12  */
13 
14 #include <common.h>
15 #include <asm/global_data.h>
16 #include <asm/ptrace.h>
17 #include <command.h>
18 #include <kgdb.h>
19 #include <asm/processor.h>
20 #include <asm/mpc8349_pci.h>
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
24 /* Returns 0 if exception not found and fixup otherwise.  */
25 extern unsigned long search_exception_table(unsigned long);
26 
27 #define END_OF_MEM	(gd->ram_base + gd->ram_size)
28 
29 /*
30  * Trap & Exception support
31  */
32 
print_backtrace(unsigned long * sp)33 static void print_backtrace(unsigned long *sp)
34 {
35 	int cnt = 0;
36 	unsigned long i;
37 
38 	puts ("Call backtrace: ");
39 	while (sp) {
40 		if ((uint)sp > END_OF_MEM)
41 			break;
42 
43 		i = sp[1];
44 		if (cnt++ % 7 == 0)
45 			putc ('\n');
46 		printf("%08lX ", i);
47 		if (cnt > 32) break;
48 		sp = (unsigned long *)*sp;
49 	}
50 	putc ('\n');
51 }
52 
show_regs(struct pt_regs * regs)53 void show_regs(struct pt_regs *regs)
54 {
55 	int i;
56 
57 	printf("NIP: %08lX XER: %08lX LR: %08lX REGS: %p TRAP: %04lx DAR: %08lX\n",
58 	       regs->nip, regs->xer, regs->link, regs, regs->trap, regs->dar);
59 	printf("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
60 	       regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
61 	       regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
62 	       regs->msr&MSR_IR ? 1 : 0,
63 	       regs->msr&MSR_DR ? 1 : 0);
64 
65 	putc ('\n');
66 	for (i = 0;  i < 32;  i++) {
67 		if ((i % 8) == 0) {
68 			printf("GPR%02d: ", i);
69 		}
70 
71 		printf("%08lX ", regs->gpr[i]);
72 		if ((i % 8) == 7) {
73 			putc ('\n');
74 		}
75 	}
76 }
77 
78 
_exception(int signr,struct pt_regs * regs)79 static void _exception(int signr, struct pt_regs *regs)
80 {
81 	show_regs(regs);
82 	print_backtrace((unsigned long *)regs->gpr[1]);
83 	panic("Exception in kernel pc %lx signal %d",regs->nip,signr);
84 }
85 
86 #ifdef CONFIG_PCI
dump_pci(void)87 void dump_pci (void)
88 {
89 /*
90 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
91 	printf ("PCI: err status %x err mask %x err ctrl %x\n",
92 		le32_to_cpu (immap->im_pci.pci_esr),
93 		le32_to_cpu (immap->im_pci.pci_emr),
94 		le32_to_cpu (immap->im_pci.pci_ecr));
95 	printf ("     error address %x error data %x ctrl %x\n",
96 		le32_to_cpu (immap->im_pci.pci_eacr),
97 		le32_to_cpu (immap->im_pci.pci_edcr),
98 		le32_to_cpu (immap->im_pci.pci_eccr));
99 */
100 }
101 #endif
102 
MachineCheckException(struct pt_regs * regs)103 void MachineCheckException(struct pt_regs *regs)
104 {
105 	unsigned long fixup;
106 
107 	/* Probing PCI using config cycles cause this exception
108 	 * when a device is not present.  Catch it and return to
109 	 * the PCI exception handler.
110 	 */
111 #ifdef CONFIG_PCI
112 #if 0
113 	volatile immap_t *immap  = (immap_t *)CONFIG_SYS_IMMR;
114 #ifdef DEBUG
115 	dump_pci();
116 #endif
117 	/* clear the error in the error status register */
118 	if(immap->im_pci.pci_esr & cpu_to_le32(PCI_ERROR_PCI_NO_RSP)) {
119 		immap->im_pci.pci_esr = cpu_to_le32(PCI_ERROR_PCI_NO_RSP);
120 		return;
121 	}
122 #endif
123 #endif /* CONFIG_PCI */
124 	if ((fixup = search_exception_table(regs->nip)) != 0) {
125 		regs->nip = fixup;
126 		return;
127 	}
128 
129 #if defined(CONFIG_CMD_KGDB)
130 	if (debugger_exception_handler && (*debugger_exception_handler)(regs))
131 		return;
132 #endif
133 
134 	puts ("Machine check in kernel mode.\n"
135 		"Caused by (from msr): ");
136 	printf("regs %p ",regs);
137 	switch( regs->msr & 0x000F0000) {
138 	case (0x80000000>>12):
139 		puts ("Machine check signal - probably due to mm fault\n"
140 			"with mmu off\n");
141 		break;
142 	case (0x80000000>>13):
143 		puts ("Transfer error ack signal\n");
144 		break;
145 	case (0x80000000>>14):
146 		puts ("Data parity signal\n");
147 		break;
148 	case (0x80000000>>15):
149 		puts ("Address parity signal\n");
150 		break;
151 	default:
152 		puts ("Unknown values in msr\n");
153 	}
154 	show_regs(regs);
155 	print_backtrace((unsigned long *)regs->gpr[1]);
156 #ifdef CONFIG_PCI
157 	dump_pci();
158 #endif
159 	panic("machine check");
160 }
161 
AlignmentException(struct pt_regs * regs)162 void AlignmentException(struct pt_regs *regs)
163 {
164 #if defined(CONFIG_CMD_KGDB)
165 	if (debugger_exception_handler && (*debugger_exception_handler)(regs))
166 		return;
167 #endif
168 	show_regs(regs);
169 	print_backtrace((unsigned long *)regs->gpr[1]);
170 	panic("Alignment Exception");
171 }
172 
ProgramCheckException(struct pt_regs * regs)173 void ProgramCheckException(struct pt_regs *regs)
174 {
175 #if defined(CONFIG_CMD_KGDB)
176 	if (debugger_exception_handler && (*debugger_exception_handler)(regs))
177 		return;
178 #endif
179 	show_regs(regs);
180 	print_backtrace((unsigned long *)regs->gpr[1]);
181 	panic("Program Check Exception");
182 }
183 
SoftEmuException(struct pt_regs * regs)184 void SoftEmuException(struct pt_regs *regs)
185 {
186 #if defined(CONFIG_CMD_KGDB)
187 	if (debugger_exception_handler && (*debugger_exception_handler)(regs))
188 		return;
189 #endif
190 	show_regs(regs);
191 	print_backtrace((unsigned long *)regs->gpr[1]);
192 	panic("Software Emulation Exception");
193 }
194 
195 
UnknownException(struct pt_regs * regs)196 void UnknownException(struct pt_regs *regs)
197 {
198 #if defined(CONFIG_CMD_KGDB)
199 	if (debugger_exception_handler && (*debugger_exception_handler)(regs))
200 		return;
201 #endif
202 	printf("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
203 	       regs->nip, regs->msr, regs->trap);
204 	_exception(0, regs);
205 }
206 
207 #if defined(CONFIG_CMD_BEDBUG)
208 extern void do_bedbug_breakpoint(struct pt_regs *);
209 #endif
210 
DebugException(struct pt_regs * regs)211 void DebugException(struct pt_regs *regs)
212 {
213 	printf("Debugger trap at @ %lx\n", regs->nip );
214 	show_regs(regs);
215 #if defined(CONFIG_CMD_BEDBUG)
216 	do_bedbug_breakpoint( regs );
217 #endif
218 }
219