blob: 48086a62809d34ba4e6aaf824b95b48e0c26a2af [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
2 * BedBug Functions
3 */
4
5#include <common.h>
6#include <command.h>
7#include <linux/ctype.h>
8#include <net.h>
wdenk57b2d802003-06-27 21:31:46 +00009#include <bedbug/type.h>
wdenk47d1a6e2002-11-03 00:01:44 +000010#include <bedbug/bedbug.h>
11#include <bedbug/regs.h>
12#include <bedbug/ppc.h>
wdenk47d1a6e2002-11-03 00:01:44 +000013
Wolfgang Denk6405a152006-03-31 18:32:53 +020014DECLARE_GLOBAL_DATA_PTR;
15
wdenk47d1a6e2002-11-03 00:01:44 +000016#if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
17
18#ifndef MAX
19#define MAX(a,b) ((a) > (b) ? (a) : (b))
20#endif
21
Wolfgang Denk6405a152006-03-31 18:32:53 +020022extern void show_regs __P ((struct pt_regs *));
23extern int run_command __P ((const char *, int));
wdenk47d1a6e2002-11-03 00:01:44 +000024extern char console_buffer[];
25
Wolfgang Denk6405a152006-03-31 18:32:53 +020026ulong dis_last_addr = 0; /* Last address disassembled */
27ulong dis_last_len = 20; /* Default disassembler length */
28CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
wdenk47d1a6e2002-11-03 00:01:44 +000029
Wolfgang Denk6405a152006-03-31 18:32:53 +020030
wdenk47d1a6e2002-11-03 00:01:44 +000031/* ======================================================================
32 * U-Boot's puts function does not append a newline, so the bedbug stuff
33 * will use this for the output of the dis/assembler.
34 * ====================================================================== */
35
Wolfgang Denk6405a152006-03-31 18:32:53 +020036int bedbug_puts (const char *str)
wdenk47d1a6e2002-11-03 00:01:44 +000037{
Wolfgang Denk6405a152006-03-31 18:32:53 +020038 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +000039
Wolfgang Denk6405a152006-03-31 18:32:53 +020040 printf ("%s\r\n", str);
41 return 0;
42} /* bedbug_puts */
43
wdenk47d1a6e2002-11-03 00:01:44 +000044
45
wdenk47d1a6e2002-11-03 00:01:44 +000046/* ======================================================================
47 * Initialize the bug_ctx structure used by the bedbug debugger. This is
48 * specific to the CPU since each has different debug registers and
49 * settings.
50 * ====================================================================== */
51
Wolfgang Denk6405a152006-03-31 18:32:53 +020052void bedbug_init (void)
wdenk47d1a6e2002-11-03 00:01:44 +000053{
Wolfgang Denk6405a152006-03-31 18:32:53 +020054 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +000055
56#if defined(CONFIG_4xx)
Wolfgang Denk6405a152006-03-31 18:32:53 +020057 void bedbug405_init (void);
58
59 bedbug405_init ();
wdenk6bd14892003-04-10 11:18:18 +000060#elif defined(CONFIG_8xx)
Wolfgang Denk6405a152006-03-31 18:32:53 +020061 void bedbug860_init (void);
62
63 bedbug860_init ();
wdenk47d1a6e2002-11-03 00:01:44 +000064#endif
65
66#if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
Wolfgang Denk6405a152006-03-31 18:32:53 +020067 /* Processors that are 603e core based */
68 void bedbug603e_init (void);
wdenk47d1a6e2002-11-03 00:01:44 +000069
Wolfgang Denk6405a152006-03-31 18:32:53 +020070 bedbug603e_init ();
wdenk47d1a6e2002-11-03 00:01:44 +000071#endif
72
Wolfgang Denk6405a152006-03-31 18:32:53 +020073 return;
74} /* bedbug_init */
75
wdenk47d1a6e2002-11-03 00:01:44 +000076
77
wdenk47d1a6e2002-11-03 00:01:44 +000078/* ======================================================================
79 * Entry point from the interpreter to the disassembler. Repeated calls
80 * will resume from the last disassembled address.
81 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +020082int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +000083{
Wolfgang Denk6405a152006-03-31 18:32:53 +020084 ulong addr; /* Address to start disassembly from */
85 ulong len; /* # of instructions to disassemble */
86
87 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +000088
Wolfgang Denk6405a152006-03-31 18:32:53 +020089 /* Setup to go from the last address if none is given */
90 addr = dis_last_addr;
91 len = dis_last_len;
wdenk47d1a6e2002-11-03 00:01:44 +000092
Wolfgang Denk6405a152006-03-31 18:32:53 +020093 if (argc < 2) {
94 printf ("Usage:\n%s\n", cmdtp->usage);
95 return 1;
96 }
wdenk47d1a6e2002-11-03 00:01:44 +000097
Wolfgang Denk6405a152006-03-31 18:32:53 +020098 if ((flag & CMD_FLAG_REPEAT) == 0) {
99 /* New command */
100 addr = simple_strtoul (argv[1], NULL, 16);
wdenk47d1a6e2002-11-03 00:01:44 +0000101
Wolfgang Denk6405a152006-03-31 18:32:53 +0200102 /* If an extra param is given then it is the length */
103 if (argc > 2)
104 len = simple_strtoul (argv[2], NULL, 16);
105 }
wdenk47d1a6e2002-11-03 00:01:44 +0000106
Wolfgang Denk6405a152006-03-31 18:32:53 +0200107 /* Run the disassembler */
108 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
wdenk47d1a6e2002-11-03 00:01:44 +0000109
Wolfgang Denk6405a152006-03-31 18:32:53 +0200110 dis_last_addr = addr + (len * 4);
111 dis_last_len = len;
112 return 0;
113} /* do_bedbug_dis */
114
115U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
116 "ds - disassemble memory\n",
117 "ds <address> [# instructions]\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000118
119/* ======================================================================
120 * Entry point from the interpreter to the assembler. Assembles
121 * instructions in consecutive memory locations until a '.' (period) is
122 * entered on a line by itself.
123 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200124int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000125{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200126 long mem_addr; /* Address to assemble into */
127 unsigned long instr; /* Machine code for text */
128 char prompt[15]; /* Prompt string for user input */
129 int asm_err; /* Error code from the assembler */
130
131 /* -------------------------------------------------- */
132 int rcode = 0;
133
134 if (argc < 2) {
135 printf ("Usage:\n%s\n", cmdtp->usage);
136 return 1;
137 }
wdenk47d1a6e2002-11-03 00:01:44 +0000138
Wolfgang Denk6405a152006-03-31 18:32:53 +0200139 printf ("\nEnter '.' when done\n");
140 mem_addr = simple_strtoul (argv[1], NULL, 16);
wdenk47d1a6e2002-11-03 00:01:44 +0000141
Wolfgang Denk6405a152006-03-31 18:32:53 +0200142 while (1) {
143 putc ('\n');
144 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
145 F_RADHEX);
wdenk47d1a6e2002-11-03 00:01:44 +0000146
Wolfgang Denk6405a152006-03-31 18:32:53 +0200147 sprintf (prompt, "%08lx: ", mem_addr);
148 readline (prompt);
wdenk47d1a6e2002-11-03 00:01:44 +0000149
Wolfgang Denk6405a152006-03-31 18:32:53 +0200150 if (console_buffer[0] && strcmp (console_buffer, ".")) {
151 if ((instr =
152 asmppc (mem_addr, console_buffer,
153 &asm_err)) != 0) {
154 *(unsigned long *) mem_addr = instr;
155 mem_addr += 4;
156 } else {
157 printf ("*** Error: %s ***\n",
158 asm_error_str (asm_err));
159 rcode = 1;
160 }
161 } else {
162 break;
163 }
164 }
165 return rcode;
166} /* do_bedbug_asm */
wdenk47d1a6e2002-11-03 00:01:44 +0000167
Wolfgang Denk6405a152006-03-31 18:32:53 +0200168U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
169 "as - assemble memory\n", "as <address>\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000170
171/* ======================================================================
172 * Used to set a break point from the interpreter. Simply calls into the
173 * CPU-specific break point set routine.
174 * ====================================================================== */
175
Wolfgang Denk6405a152006-03-31 18:32:53 +0200176int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000177{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200178 /* -------------------------------------------------- */
179 if (bug_ctx.do_break)
180 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
181 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000182
Wolfgang Denk6405a152006-03-31 18:32:53 +0200183} /* do_bedbug_break */
184
185U_BOOT_CMD (break, 3, 0, do_bedbug_break,
186 "break - set or clear a breakpoint\n",
187 " - Set or clear a breakpoint\n"
188 "break <address> - Break at an address\n"
189 "break off <bp#> - Disable breakpoint.\n"
190 "break show - List breakpoints.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000191
192/* ======================================================================
193 * Called from the debug interrupt routine. Simply calls the CPU-specific
194 * breakpoint handling routine.
195 * ====================================================================== */
196
197void do_bedbug_breakpoint (struct pt_regs *regs)
198{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200199 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000200
Wolfgang Denk6405a152006-03-31 18:32:53 +0200201 if (bug_ctx.break_isr)
202 (*bug_ctx.break_isr) (regs);
wdenk47d1a6e2002-11-03 00:01:44 +0000203
Wolfgang Denk6405a152006-03-31 18:32:53 +0200204 return;
205} /* do_bedbug_breakpoint */
206
wdenk47d1a6e2002-11-03 00:01:44 +0000207
208
wdenk47d1a6e2002-11-03 00:01:44 +0000209/* ======================================================================
210 * Called from the CPU-specific breakpoint handling routine. Enter a
211 * mini main loop until the stopped flag is cleared from the breakpoint
212 * context.
213 *
214 * This handles the parts of the debugger that are common to all CPU's.
215 * ====================================================================== */
216
Wolfgang Denk6405a152006-03-31 18:32:53 +0200217void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
wdenk47d1a6e2002-11-03 00:01:44 +0000218{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200219 int len; /* Length of command line */
220 int flag; /* Command flags */
221 int rc = 0; /* Result from run_command */
222 char prompt_str[20]; /* Prompt string */
223 static char lastcommand[CFG_CBSIZE] = { 0 }; /* previous command */
224 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000225
Wolfgang Denk6405a152006-03-31 18:32:53 +0200226 if (bug_ctx.clear)
227 (*bug_ctx.clear) (bug_ctx.current_bp);
wdenk47d1a6e2002-11-03 00:01:44 +0000228
Wolfgang Denk6405a152006-03-31 18:32:53 +0200229 printf ("Breakpoint %d: ", bug_ctx.current_bp);
230 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
wdenk47d1a6e2002-11-03 00:01:44 +0000231
Wolfgang Denk6405a152006-03-31 18:32:53 +0200232 bug_ctx.stopped = 1;
233 bug_ctx.regs = regs;
wdenk47d1a6e2002-11-03 00:01:44 +0000234
Wolfgang Denk6405a152006-03-31 18:32:53 +0200235 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
wdenk47d1a6e2002-11-03 00:01:44 +0000236
Wolfgang Denk6405a152006-03-31 18:32:53 +0200237 /* A miniature main loop */
238 while (bug_ctx.stopped) {
239 len = readline (prompt_str);
wdenk47d1a6e2002-11-03 00:01:44 +0000240
Wolfgang Denk6405a152006-03-31 18:32:53 +0200241 flag = 0; /* assume no special flags for now */
wdenk47d1a6e2002-11-03 00:01:44 +0000242
Wolfgang Denk6405a152006-03-31 18:32:53 +0200243 if (len > 0)
244 strcpy (lastcommand, console_buffer);
245 else if (len == 0)
246 flag |= CMD_FLAG_REPEAT;
wdenk47d1a6e2002-11-03 00:01:44 +0000247
Wolfgang Denk6405a152006-03-31 18:32:53 +0200248 if (len == -1)
249 printf ("<INTERRUPT>\n");
250 else
251 rc = run_command (lastcommand, flag);
wdenk47d1a6e2002-11-03 00:01:44 +0000252
Wolfgang Denk6405a152006-03-31 18:32:53 +0200253 if (rc <= 0) {
254 /* invalid command or not repeatable, forget it */
255 lastcommand[0] = 0;
256 }
257 }
wdenk47d1a6e2002-11-03 00:01:44 +0000258
Wolfgang Denk6405a152006-03-31 18:32:53 +0200259 bug_ctx.regs = NULL;
260 bug_ctx.current_bp = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000261
Wolfgang Denk6405a152006-03-31 18:32:53 +0200262 return;
263} /* bedbug_main_loop */
264
wdenk47d1a6e2002-11-03 00:01:44 +0000265
266
wdenk47d1a6e2002-11-03 00:01:44 +0000267/* ======================================================================
268 * Interpreter command to continue from a breakpoint. Just clears the
269 * stopped flag in the context so that the breakpoint routine will
270 * return.
271 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200272int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000273{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200274 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000275
Wolfgang Denk6405a152006-03-31 18:32:53 +0200276 if (!bug_ctx.stopped) {
277 printf ("Not at a breakpoint\n");
278 return 1;
279 }
wdenk47d1a6e2002-11-03 00:01:44 +0000280
Wolfgang Denk6405a152006-03-31 18:32:53 +0200281 bug_ctx.stopped = 0;
282 return 0;
283} /* do_bedbug_continue */
284
285U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
286 "continue- continue from a breakpoint\n",
287 " - continue from a breakpoint.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000288
289/* ======================================================================
290 * Interpreter command to continue to the next instruction, stepping into
291 * subroutines. Works by calling the find_next_addr() routine to compute
292 * the address passes control to the CPU-specific set breakpoint routine
293 * for the current breakpoint number.
294 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200295int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000296{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200297 unsigned long addr; /* Address to stop at */
wdenk47d1a6e2002-11-03 00:01:44 +0000298
Wolfgang Denk6405a152006-03-31 18:32:53 +0200299 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000300
Wolfgang Denk6405a152006-03-31 18:32:53 +0200301 if (!bug_ctx.stopped) {
302 printf ("Not at a breakpoint\n");
303 return 1;
304 }
wdenk47d1a6e2002-11-03 00:01:44 +0000305
Wolfgang Denk6405a152006-03-31 18:32:53 +0200306 if (!find_next_address ((unsigned char *) &addr, FALSE, bug_ctx.regs))
307 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000308
Wolfgang Denk6405a152006-03-31 18:32:53 +0200309 if (bug_ctx.set)
310 (*bug_ctx.set) (bug_ctx.current_bp, addr);
311
312 bug_ctx.stopped = 0;
313 return 0;
314} /* do_bedbug_step */
315
316U_BOOT_CMD (step, 1, 1, do_bedbug_step,
317 "step - single step execution.\n",
318 " - single step execution.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000319
320/* ======================================================================
321 * Interpreter command to continue to the next instruction, stepping over
322 * subroutines. Works by calling the find_next_addr() routine to compute
323 * the address passes control to the CPU-specific set breakpoint routine
324 * for the current breakpoint number.
325 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200326int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000327{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200328 unsigned long addr; /* Address to stop at */
329
330 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000331
Wolfgang Denk6405a152006-03-31 18:32:53 +0200332 if (!bug_ctx.stopped) {
333 printf ("Not at a breakpoint\n");
334 return 1;
335 }
wdenk47d1a6e2002-11-03 00:01:44 +0000336
Wolfgang Denk6405a152006-03-31 18:32:53 +0200337 if (!find_next_address ((unsigned char *) &addr, TRUE, bug_ctx.regs))
338 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000339
Wolfgang Denk6405a152006-03-31 18:32:53 +0200340 if (bug_ctx.set)
341 (*bug_ctx.set) (bug_ctx.current_bp, addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000342
Wolfgang Denk6405a152006-03-31 18:32:53 +0200343 bug_ctx.stopped = 0;
344 return 0;
345} /* do_bedbug_next */
346
347U_BOOT_CMD (next, 1, 1, do_bedbug_next,
348 "next - single step execution, stepping over subroutines.\n",
349 " - single step execution, stepping over subroutines.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000350
351/* ======================================================================
352 * Interpreter command to print the current stack. This assumes an EABI
353 * architecture, so it starts with GPR R1 and works back up the stack.
354 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200355int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000356{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200357 unsigned long sp; /* Stack pointer */
358 unsigned long func; /* LR from stack */
359 int depth; /* Stack iteration level */
360 int skip = 1; /* Flag to skip the first entry */
361 unsigned long top; /* Top of memory address */
wdenk47d1a6e2002-11-03 00:01:44 +0000362
Wolfgang Denk6405a152006-03-31 18:32:53 +0200363 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000364
Wolfgang Denk6405a152006-03-31 18:32:53 +0200365 if (!bug_ctx.stopped) {
366 printf ("Not at a breakpoint\n");
367 return 1;
368 }
wdenk47d1a6e2002-11-03 00:01:44 +0000369
Wolfgang Denk6405a152006-03-31 18:32:53 +0200370 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
371 depth = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000372
Wolfgang Denk6405a152006-03-31 18:32:53 +0200373 printf ("Depth PC\n");
374 printf ("----- --------\n");
375 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
wdenk47d1a6e2002-11-03 00:01:44 +0000376
Wolfgang Denk6405a152006-03-31 18:32:53 +0200377 sp = bug_ctx.regs->gpr[1];
378 func = *(unsigned long *) (sp + 4);
wdenk47d1a6e2002-11-03 00:01:44 +0000379
Wolfgang Denk6405a152006-03-31 18:32:53 +0200380 while ((func < top) && (sp < top)) {
381 if (!skip)
382 printf ("%5d %08lx\n", depth++, func);
383 else
384 --skip;
wdenk47d1a6e2002-11-03 00:01:44 +0000385
Wolfgang Denk6405a152006-03-31 18:32:53 +0200386 sp = *(unsigned long *) sp;
387 func = *(unsigned long *) (sp + 4);
388 }
389 return 0;
390} /* do_bedbug_stack */
391
392U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
393 "where - Print the running stack.\n",
394 " - Print the running stack.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000395
396/* ======================================================================
397 * Interpreter command to dump the registers. Calls the CPU-specific
398 * show registers routine.
399 * ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200400int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000401{
Wolfgang Denk6405a152006-03-31 18:32:53 +0200402 /* -------------------------------------------------- */
wdenk47d1a6e2002-11-03 00:01:44 +0000403
Wolfgang Denk6405a152006-03-31 18:32:53 +0200404 if (!bug_ctx.stopped) {
405 printf ("Not at a breakpoint\n");
406 return 1;
407 }
wdenk47d1a6e2002-11-03 00:01:44 +0000408
Wolfgang Denk6405a152006-03-31 18:32:53 +0200409 show_regs (bug_ctx.regs);
410 return 0;
411} /* do_bedbug_rdump */
412
413U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
414 "rdump - Show registers.\n", " - Show registers.\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000415/* ====================================================================== */
Wolfgang Denk6405a152006-03-31 18:32:53 +0200416#endif /* CFG_CMD_BEDBUG */
wdenk47d1a6e2002-11-03 00:01:44 +0000417
418
419/*
420 * Copyright (c) 2001 William L. Pitts
421 * All rights reserved.
422 *
423 * Redistribution and use in source and binary forms are freely
424 * permitted provided that the above copyright notice and this
425 * paragraph and the following disclaimer are duplicated in all
426 * such forms.
427 *
428 * This software is provided "AS IS" and without any express or
429 * implied warranties, including, without limitation, the implied
430 * warranties of merchantability and fitness for a particular
431 * purpose.
432 */