xref: /rk3399_rockchip-uboot/cmd/bedbug.c (revision 5b8e76c35ec312a3f73126bd1a2d2c0965b98a9f)
1 /*
2  * BedBug Functions
3  */
4 
5 #include <common.h>
6 #include <cli.h>
7 #include <command.h>
8 #include <console.h>
9 #include <linux/ctype.h>
10 #include <net.h>
11 #include <bedbug/type.h>
12 #include <bedbug/bedbug.h>
13 #include <bedbug/regs.h>
14 #include <bedbug/ppc.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 extern void show_regs __P ((struct pt_regs *));
19 extern int run_command __P ((const char *, int));
20 
21 ulong dis_last_addr = 0;	/* Last address disassembled   */
22 ulong dis_last_len = 20;	/* Default disassembler length */
23 CPU_DEBUG_CTX bug_ctx;		/* Bedbug context structure    */
24 
25 
26 /* ======================================================================
27  * U-Boot's puts function does not append a newline, so the bedbug stuff
28  * will use this for the output of the dis/assembler.
29  * ====================================================================== */
30 
31 int bedbug_puts (const char *str)
32 {
33 	/* -------------------------------------------------- */
34 
35 	printf ("%s\r\n", str);
36 	return 0;
37 }				/* bedbug_puts */
38 
39 
40 
41 /* ======================================================================
42  * Initialize the bug_ctx structure used by the bedbug debugger.  This is
43  * specific to the CPU since each has different debug registers and
44  * settings.
45  * ====================================================================== */
46 
47 void bedbug_init (void)
48 {
49 	/* -------------------------------------------------- */
50 
51 #if defined(CONFIG_4xx)
52 	void bedbug405_init (void);
53 
54 	bedbug405_init ();
55 #endif
56 
57 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
58 	/* Processors that are 603e core based */
59 	void bedbug603e_init (void);
60 
61 	bedbug603e_init ();
62 #endif
63 
64 	return;
65 }				/* bedbug_init */
66 
67 
68 
69 /* ======================================================================
70  * Entry point from the interpreter to the disassembler.  Repeated calls
71  * will resume from the last disassembled address.
72  * ====================================================================== */
73 int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
74 {
75 	ulong addr;		/* Address to start disassembly from    */
76 	ulong len;		/* # of instructions to disassemble     */
77 
78 	/* -------------------------------------------------- */
79 
80 	/* Setup to go from the last address if none is given */
81 	addr = dis_last_addr;
82 	len = dis_last_len;
83 
84 	if (argc < 2)
85 		return CMD_RET_USAGE;
86 
87 	if ((flag & CMD_FLAG_REPEAT) == 0) {
88 		/* New command */
89 		addr = simple_strtoul (argv[1], NULL, 16);
90 
91 		/* If an extra param is given then it is the length */
92 		if (argc > 2)
93 			len = simple_strtoul (argv[2], NULL, 16);
94 	}
95 
96 	/* Run the disassembler */
97 	disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
98 
99 	dis_last_addr = addr + (len * 4);
100 	dis_last_len = len;
101 	return 0;
102 }				/* do_bedbug_dis */
103 
104 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
105 	    "disassemble memory",
106 	    "ds <address> [# instructions]");
107 
108 /* ======================================================================
109  * Entry point from the interpreter to the assembler.  Assembles
110  * instructions in consecutive memory locations until a '.' (period) is
111  * entered on a line by itself.
112  * ====================================================================== */
113 int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
114 {
115 	long mem_addr;		/* Address to assemble into     */
116 	unsigned long instr;	/* Machine code for text        */
117 	char prompt[15];	/* Prompt string for user input */
118 	int asm_err;		/* Error code from the assembler */
119 
120 	/* -------------------------------------------------- */
121 	int rcode = 0;
122 
123 	if (argc < 2)
124 		return CMD_RET_USAGE;
125 
126 	printf ("\nEnter '.' when done\n");
127 	mem_addr = simple_strtoul (argv[1], NULL, 16);
128 
129 	while (1) {
130 		putc ('\n');
131 		disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
132 			F_RADHEX);
133 
134 		sprintf (prompt, "%08lx:    ", mem_addr);
135 		cli_readline(prompt);
136 
137 		if (console_buffer[0] && strcmp (console_buffer, ".")) {
138 			if ((instr =
139 			     asmppc (mem_addr, console_buffer,
140 				     &asm_err)) != 0) {
141 				*(unsigned long *) mem_addr = instr;
142 				mem_addr += 4;
143 			} else {
144 				printf ("*** Error: %s ***\n",
145 					asm_error_str (asm_err));
146 				rcode = 1;
147 			}
148 		} else {
149 			break;
150 		}
151 	}
152 	return rcode;
153 }				/* do_bedbug_asm */
154 
155 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
156 	    "assemble memory", "as <address>");
157 
158 /* ======================================================================
159  * Used to set a break point from the interpreter.  Simply calls into the
160  * CPU-specific break point set routine.
161  * ====================================================================== */
162 
163 int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
164 {
165 	/* -------------------------------------------------- */
166 	if (bug_ctx.do_break)
167 		(*bug_ctx.do_break) (cmdtp, flag, argc, argv);
168 	return 0;
169 
170 }				/* do_bedbug_break */
171 
172 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
173 	    "set or clear a breakpoint",
174 	    " - Set or clear a breakpoint\n"
175 	    "break <address> - Break at an address\n"
176 	    "break off <bp#> - Disable breakpoint.\n"
177 	    "break show      - List breakpoints.");
178 
179 /* ======================================================================
180  * Called from the debug interrupt routine.  Simply calls the CPU-specific
181  * breakpoint handling routine.
182  * ====================================================================== */
183 
184 void do_bedbug_breakpoint (struct pt_regs *regs)
185 {
186 	/* -------------------------------------------------- */
187 
188 	if (bug_ctx.break_isr)
189 		(*bug_ctx.break_isr) (regs);
190 
191 	return;
192 }				/* do_bedbug_breakpoint */
193 
194 
195 
196 /* ======================================================================
197  * Called from the CPU-specific breakpoint handling routine.  Enter a
198  * mini main loop until the stopped flag is cleared from the breakpoint
199  * context.
200  *
201  * This handles the parts of the debugger that are common to all CPU's.
202  * ====================================================================== */
203 
204 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
205 {
206 	int len;		/* Length of command line */
207 	int flag;		/* Command flags          */
208 	int rc = 0;		/* Result from run_command */
209 	char prompt_str[20];	/* Prompt string          */
210 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 };	/* previous command */
211 	/* -------------------------------------------------- */
212 
213 	if (bug_ctx.clear)
214 		(*bug_ctx.clear) (bug_ctx.current_bp);
215 
216 	printf ("Breakpoint %d: ", bug_ctx.current_bp);
217 	disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
218 
219 	bug_ctx.stopped = 1;
220 	bug_ctx.regs = regs;
221 
222 	sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
223 
224 	/* A miniature main loop */
225 	while (bug_ctx.stopped) {
226 		len = cli_readline(prompt_str);
227 
228 		flag = 0;	/* assume no special flags for now */
229 
230 		if (len > 0)
231 			strcpy (lastcommand, console_buffer);
232 		else if (len == 0)
233 			flag |= CMD_FLAG_REPEAT;
234 
235 		if (len == -1)
236 			printf ("<INTERRUPT>\n");
237 		else
238 			rc = run_command_repeatable(lastcommand, flag);
239 
240 		if (rc <= 0) {
241 			/* invalid command or not repeatable, forget it */
242 			lastcommand[0] = 0;
243 		}
244 	}
245 
246 	bug_ctx.regs = NULL;
247 	bug_ctx.current_bp = 0;
248 
249 	return;
250 }				/* bedbug_main_loop */
251 
252 
253 
254 /* ======================================================================
255  * Interpreter command to continue from a breakpoint.  Just clears the
256  * stopped flag in the context so that the breakpoint routine will
257  * return.
258  * ====================================================================== */
259 int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
260 {
261 	/* -------------------------------------------------- */
262 
263 	if (!bug_ctx.stopped) {
264 		printf ("Not at a breakpoint\n");
265 		return 1;
266 	}
267 
268 	bug_ctx.stopped = 0;
269 	return 0;
270 }				/* do_bedbug_continue */
271 
272 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
273 	    "continue from a breakpoint",
274 	    "");
275 
276 /* ======================================================================
277  * Interpreter command to continue to the next instruction, stepping into
278  * subroutines.  Works by calling the find_next_addr() routine to compute
279  * the address passes control to the CPU-specific set breakpoint routine
280  * for the current breakpoint number.
281  * ====================================================================== */
282 int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
283 {
284 	unsigned long addr;	/* Address to stop at */
285 
286 	/* -------------------------------------------------- */
287 
288 	if (!bug_ctx.stopped) {
289 		printf ("Not at a breakpoint\n");
290 		return 1;
291 	}
292 
293 	if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
294 		return 1;
295 
296 	if (bug_ctx.set)
297 		(*bug_ctx.set) (bug_ctx.current_bp, addr);
298 
299 	bug_ctx.stopped = 0;
300 	return 0;
301 }				/* do_bedbug_step */
302 
303 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
304 	    "single step execution.",
305 	    "");
306 
307 /* ======================================================================
308  * Interpreter command to continue to the next instruction, stepping over
309  * subroutines.  Works by calling the find_next_addr() routine to compute
310  * the address passes control to the CPU-specific set breakpoint routine
311  * for the current breakpoint number.
312  * ====================================================================== */
313 int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
314 {
315 	unsigned long addr;	/* Address to stop at */
316 
317 	/* -------------------------------------------------- */
318 
319 	if (!bug_ctx.stopped) {
320 		printf ("Not at a breakpoint\n");
321 		return 1;
322 	}
323 
324 	if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
325 		return 1;
326 
327 	if (bug_ctx.set)
328 		(*bug_ctx.set) (bug_ctx.current_bp, addr);
329 
330 	bug_ctx.stopped = 0;
331 	return 0;
332 }				/* do_bedbug_next */
333 
334 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
335 	    "single step execution, stepping over subroutines.",
336 	    "");
337 
338 /* ======================================================================
339  * Interpreter command to print the current stack.  This assumes an EABI
340  * architecture, so it starts with GPR R1 and works back up the stack.
341  * ====================================================================== */
342 int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
343 {
344 	unsigned long sp;	/* Stack pointer                */
345 	unsigned long func;	/* LR from stack                */
346 	int depth;		/* Stack iteration level        */
347 	int skip = 1;		/* Flag to skip the first entry */
348 	unsigned long top;	/* Top of memory address        */
349 
350 	/* -------------------------------------------------- */
351 
352 	if (!bug_ctx.stopped) {
353 		printf ("Not at a breakpoint\n");
354 		return 1;
355 	}
356 
357 	top = gd->bd->bi_memstart + gd->bd->bi_memsize;
358 	depth = 0;
359 
360 	printf ("Depth     PC\n");
361 	printf ("-----  --------\n");
362 	printf ("%5d  %08lx\n", depth++, bug_ctx.regs->nip);
363 
364 	sp = bug_ctx.regs->gpr[1];
365 	func = *(unsigned long *) (sp + 4);
366 
367 	while ((func < top) && (sp < top)) {
368 		if (!skip)
369 			printf ("%5d  %08lx\n", depth++, func);
370 		else
371 			--skip;
372 
373 		sp = *(unsigned long *) sp;
374 		func = *(unsigned long *) (sp + 4);
375 	}
376 	return 0;
377 }				/* do_bedbug_stack */
378 
379 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
380 	    "Print the running stack.",
381 	    "");
382 
383 /* ======================================================================
384  * Interpreter command to dump the registers.  Calls the CPU-specific
385  * show registers routine.
386  * ====================================================================== */
387 int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
388 {
389 	/* -------------------------------------------------- */
390 
391 	if (!bug_ctx.stopped) {
392 		printf ("Not at a breakpoint\n");
393 		return 1;
394 	}
395 
396 	show_regs (bug_ctx.regs);
397 	return 0;
398 }				/* do_bedbug_rdump */
399 
400 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
401 	    "Show registers.", "");
402 /* ====================================================================== */
403 
404 
405 /*
406  * Copyright (c) 2001 William L. Pitts
407  * All rights reserved.
408  *
409  * Redistribution and use in source and binary forms are freely
410  * permitted provided that the above copyright notice and this
411  * paragraph and the following disclaimer are duplicated in all
412  * such forms.
413  *
414  * This software is provided "AS IS" and without any express or
415  * implied warranties, including, without limitation, the implied
416  * warranties of merchantability and fitness for a particular
417  * purpose.
418  */
419