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