1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2003
4 * Texas Instruments <www.ti.com>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 *
10 * (C) Copyright 2002
11 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Alex Zuepke <azu@sysgo.de>
13 *
14 * (C) Copyright 2002-2004
15 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
16 *
17 * (C) Copyright 2004
18 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
19 */
20
21#include <common.h>
22#include <cpu_func.h>
23#include <efi_loader.h>
24#include <irq_func.h>
25#include <semihosting.h>
26#include <asm/global_data.h>
27#include <asm/proc-armv/ptrace.h>
28#include <asm/ptrace.h>
29#include <asm/u-boot-arm.h>
30
31DECLARE_GLOBAL_DATA_PTR;
32
33int interrupt_init(void)
34{
35	/*
36	 * setup up stacks if necessary
37	 */
38	IRQ_STACK_START_IN = gd->irq_sp + 8;
39
40	enable_interrupts();
41
42	return 0;
43}
44
45void enable_interrupts(void)
46{
47	return;
48}
49int disable_interrupts(void)
50{
51	return 0;
52}
53
54void bad_mode (void)
55{
56	panic ("Resetting CPU ...\n");
57	reset_cpu();
58}
59
60static void show_efi_loaded_images(struct pt_regs *regs)
61{
62	efi_print_image_infos((void *)instruction_pointer(regs));
63}
64
65static void dump_instr(struct pt_regs *regs)
66{
67	unsigned long addr = instruction_pointer(regs);
68	const int thumb = thumb_mode(regs);
69	const int width = thumb ? 4 : 8;
70	int i;
71
72	if (thumb)
73		addr &= ~1L;
74	else
75		addr &= ~3L;
76	printf("Code: ");
77	for (i = -4; i < 1 + !!thumb; i++) {
78		unsigned int val;
79
80		if (thumb)
81			val = ((u16 *)addr)[i];
82		else
83			val = ((u32 *)addr)[i];
84		printf(i == 0 ? "(%0*x) " : "%0*x ", width, val);
85	}
86	printf("\n");
87}
88
89void show_regs (struct pt_regs *regs)
90{
91	unsigned long __maybe_unused flags;
92	const char __maybe_unused *processor_modes[] = {
93	"USER_26",	"FIQ_26",	"IRQ_26",	"SVC_26",
94	"UK4_26",	"UK5_26",	"UK6_26",	"UK7_26",
95	"UK8_26",	"UK9_26",	"UK10_26",	"UK11_26",
96	"UK12_26",	"UK13_26",	"UK14_26",	"UK15_26",
97	"USER_32",	"FIQ_32",	"IRQ_32",	"SVC_32",
98	"UK4_32",	"UK5_32",	"UK6_32",	"ABT_32",
99	"UK8_32",	"UK9_32",	"HYP_32",	"UND_32",
100	"UK12_32",	"UK13_32",	"UK14_32",	"SYS_32",
101	};
102
103	flags = condition_codes (regs);
104
105	printf("pc : [<%08lx>]	   lr : [<%08lx>]\n",
106	       instruction_pointer(regs), regs->ARM_lr);
107	if (gd->flags & GD_FLG_RELOC) {
108		printf("reloc pc : [<%08lx>]	   lr : [<%08lx>]\n",
109		       instruction_pointer(regs) - gd->reloc_off,
110		       regs->ARM_lr - gd->reloc_off);
111	}
112	printf("sp : %08lx  ip : %08lx	 fp : %08lx\n",
113	       regs->ARM_sp, regs->ARM_ip, regs->ARM_fp);
114	printf ("r10: %08lx  r9 : %08lx	 r8 : %08lx\n",
115		regs->ARM_r10, regs->ARM_r9, regs->ARM_r8);
116	printf ("r7 : %08lx  r6 : %08lx	 r5 : %08lx  r4 : %08lx\n",
117		regs->ARM_r7, regs->ARM_r6, regs->ARM_r5, regs->ARM_r4);
118	printf ("r3 : %08lx  r2 : %08lx	 r1 : %08lx  r0 : %08lx\n",
119		regs->ARM_r3, regs->ARM_r2, regs->ARM_r1, regs->ARM_r0);
120	printf ("Flags: %c%c%c%c",
121		flags & CC_N_BIT ? 'N' : 'n',
122		flags & CC_Z_BIT ? 'Z' : 'z',
123		flags & CC_C_BIT ? 'C' : 'c', flags & CC_V_BIT ? 'V' : 'v');
124	printf ("  IRQs %s  FIQs %s  Mode %s%s\n",
125		interrupts_enabled (regs) ? "on" : "off",
126		fast_interrupts_enabled (regs) ? "on" : "off",
127		processor_modes[processor_mode (regs)],
128		thumb_mode (regs) ? " (T)" : "");
129	dump_instr(regs);
130}
131
132/* fixup PC to point to the instruction leading to the exception */
133static inline void fixup_pc(struct pt_regs *regs, int offset)
134{
135	uint32_t pc = instruction_pointer(regs) + offset;
136	regs->ARM_pc = pc | (regs->ARM_pc & PCMASK);
137}
138
139/*
140 * Try to "emulate" a semihosting call in the event that we don't have a
141 * debugger attached.
142 */
143static bool smh_emulate_trap(struct pt_regs *regs)
144{
145	if (regs->ARM_cpsr & T_BIT) {
146		u16 *insn = (u16 *)(regs->ARM_pc - 2);
147
148		if (*insn != SMH_T32_SVC)
149			return false;
150	} else {
151		u32 *insn = (u32 *)(regs->ARM_pc - 4);
152
153		if (*insn != SMH_A32_SVC)
154			return false;
155	}
156
157	/* Avoid future semihosting calls */
158	disable_semihosting();
159
160	/* Just pretend the call failed */
161	regs->ARM_r0 = -1;
162	return true;
163}
164
165void do_undefined_instruction (struct pt_regs *pt_regs)
166{
167	efi_restore_gd();
168	printf ("undefined instruction\n");
169	fixup_pc(pt_regs, -4);
170	show_regs (pt_regs);
171	show_efi_loaded_images(pt_regs);
172	bad_mode ();
173}
174
175void do_software_interrupt (struct pt_regs *pt_regs)
176{
177	if (CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) &&
178	    smh_emulate_trap(pt_regs))
179		return;
180
181	efi_restore_gd();
182	printf ("software interrupt\n");
183	fixup_pc(pt_regs, -4);
184	show_regs (pt_regs);
185	show_efi_loaded_images(pt_regs);
186	bad_mode ();
187}
188
189void do_prefetch_abort (struct pt_regs *pt_regs)
190{
191	efi_restore_gd();
192	printf ("prefetch abort\n");
193	fixup_pc(pt_regs, -8);
194	show_regs (pt_regs);
195	show_efi_loaded_images(pt_regs);
196	bad_mode ();
197}
198
199void do_data_abort (struct pt_regs *pt_regs)
200{
201	efi_restore_gd();
202	printf ("data abort\n");
203	fixup_pc(pt_regs, -8);
204	show_regs (pt_regs);
205	show_efi_loaded_images(pt_regs);
206	bad_mode ();
207}
208
209void do_not_used (struct pt_regs *pt_regs)
210{
211	efi_restore_gd();
212	printf ("not used\n");
213	fixup_pc(pt_regs, -8);
214	show_regs (pt_regs);
215	show_efi_loaded_images(pt_regs);
216	bad_mode ();
217}
218
219void do_fiq (struct pt_regs *pt_regs)
220{
221	efi_restore_gd();
222	printf ("fast interrupt request\n");
223	fixup_pc(pt_regs, -8);
224	show_regs (pt_regs);
225	show_efi_loaded_images(pt_regs);
226	bad_mode ();
227}
228
229void do_irq (struct pt_regs *pt_regs)
230{
231	efi_restore_gd();
232	printf ("interrupt request\n");
233	fixup_pc(pt_regs, -8);
234	show_regs (pt_regs);
235	show_efi_loaded_images(pt_regs);
236	bad_mode ();
237}
238