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