1 /* MN10300 GDB stub
2  *
3  * Originally written by Glenn Engel, Lake Stevens Instrument Division
4  *
5  * Contributed by HP Systems
6  *
7  * Modified for SPARC by Stu Grossman, Cygnus Support.
8  *
9  * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
10  * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
11  *
12  * Copyright (C) 1995 Andreas Busse
13  *
14  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
15  * Modified for Linux/mn10300 by David Howells <dhowells@redhat.com>
16  */
17 
18 /*
19  *  To enable debugger support, two things need to happen.  One, a
20  *  call to set_debug_traps() is necessary in order to allow any breakpoints
21  *  or error conditions to be properly intercepted and reported to gdb.
22  *  Two, a breakpoint needs to be generated to begin communication.  This
23  *  is most easily accomplished by a call to breakpoint().  Breakpoint()
24  *  simulates a breakpoint by executing a BREAK instruction.
25  *
26  *
27  *    The following gdb commands are supported:
28  *
29  * command          function                               Return value
30  *
31  *    g             return the value of the CPU registers  hex data or ENN
32  *    G             set the value of the CPU registers     OK or ENN
33  *
34  *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
35  *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
36  *
37  *    c             Resume at current address              SNN   ( signal NN)
38  *    cAA..AA       Continue at address AA..AA             SNN
39  *
40  *    s             Step one instruction                   SNN
41  *    sAA..AA       Step one instruction from AA..AA       SNN
42  *
43  *    k             kill
44  *
45  *    ?             What was the last sigval ?             SNN   (signal NN)
46  *
47  *    bBB..BB	    Set baud rate to BB..BB		   OK or BNN, then sets
48  *							   baud rate
49  *
50  * All commands and responses are sent with a packet which includes a
51  * checksum.  A packet consists of
52  *
53  * $<packet info>#<checksum>.
54  *
55  * where
56  * <packet info> :: <characters representing the command or response>
57  * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
58  *
59  * When a packet is received, it is first acknowledged with either '+' or '-'.
60  * '+' indicates a successful transfer.  '-' indicates a failed transfer.
61  *
62  * Example:
63  *
64  * Host:                  Reply:
65  * $m0,10#2a               +$00010203040506070809101112131415#42
66  *
67  *
68  *  ==============
69  *  MORE EXAMPLES:
70  *  ==============
71  *
72  *  For reference -- the following are the steps that one
73  *  company took (RidgeRun Inc) to get remote gdb debugging
74  *  going. In this scenario the host machine was a PC and the
75  *  target platform was a Galileo EVB64120A MIPS evaluation
76  *  board.
77  *
78  *  Step 1:
79  *  First download gdb-5.0.tar.gz from the internet.
80  *  and then build/install the package.
81  *
82  *  Example:
83  *    $ tar zxf gdb-5.0.tar.gz
84  *    $ cd gdb-5.0
85  *    $ ./configure --target=am33_2.0-linux-gnu
86  *    $ make
87  *    $ install
88  *    am33_2.0-linux-gnu-gdb
89  *
90  *  Step 2:
91  *  Configure linux for remote debugging and build it.
92  *
93  *  Example:
94  *    $ cd ~/linux
95  *    $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
96  *    $ make dep; make vmlinux
97  *
98  *  Step 3:
99  *  Download the kernel to the remote target and start
100  *  the kernel running. It will promptly halt and wait
101  *  for the host gdb session to connect. It does this
102  *  since the "Kernel Hacking" option has defined
103  *  CONFIG_REMOTE_DEBUG which in turn enables your calls
104  *  to:
105  *     set_debug_traps();
106  *     breakpoint();
107  *
108  *  Step 4:
109  *  Start the gdb session on the host.
110  *
111  *  Example:
112  *    $ am33_2.0-linux-gnu-gdb vmlinux
113  *    (gdb) set remotebaud 115200
114  *    (gdb) target remote /dev/ttyS1
115  *    ...at this point you are connected to
116  *       the remote target and can use gdb
117  *       in the normal fasion. Setting
118  *       breakpoints, single stepping,
119  *       printing variables, etc.
120  *
121  */
122 
123 #include <linux/string.h>
124 #include <linux/kernel.h>
125 #include <linux/signal.h>
126 #include <linux/sched.h>
127 #include <linux/mm.h>
128 #include <linux/console.h>
129 #include <linux/init.h>
130 #include <linux/bug.h>
131 
132 #include <asm/pgtable.h>
133 #include <asm/gdb-stub.h>
134 #include <asm/exceptions.h>
135 #include <asm/debugger.h>
136 #include <asm/serial-regs.h>
137 #include <asm/busctl-regs.h>
138 #include <unit/leds.h>
139 #include <unit/serial.h>
140 
141 /* define to use F7F7 rather than FF which is subverted by JTAG debugger */
142 #undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
143 
144 /*
145  * BUFMAX defines the maximum number of characters in inbound/outbound buffers
146  * at least NUMREGBYTES*2 are needed for register packets
147  */
148 #define BUFMAX 2048
149 
150 static const char gdbstub_banner[] =
151 	"Linux/MN10300 GDB Stub (c) RedHat 2007\n";
152 
153 u8	gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
154 u32	gdbstub_rx_inp;
155 u32	gdbstub_rx_outp;
156 u8	gdbstub_busy;
157 u8	gdbstub_rx_overflow;
158 u8	gdbstub_rx_unget;
159 
160 static u8	gdbstub_flush_caches;
161 static char	input_buffer[BUFMAX];
162 static char	output_buffer[BUFMAX];
163 static char	trans_buffer[BUFMAX];
164 
165 struct gdbstub_bkpt {
166 	u8	*addr;		/* address of breakpoint */
167 	u8	len;		/* size of breakpoint */
168 	u8	origbytes[7];	/* original bytes */
169 };
170 
171 static struct gdbstub_bkpt gdbstub_bkpts[256];
172 
173 /*
174  * local prototypes
175  */
176 static void getpacket(char *buffer);
177 static int putpacket(char *buffer);
178 static int computeSignal(enum exception_code excep);
179 static int hex(unsigned char ch);
180 static int hexToInt(char **ptr, int *intValue);
181 static unsigned char *mem2hex(const void *mem, char *buf, int count,
182 			      int may_fault);
183 static const char *hex2mem(const char *buf, void *_mem, int count,
184 			   int may_fault);
185 
186 /*
187  * Convert ch from a hex digit to an int
188  */
hex(unsigned char ch)189 static int hex(unsigned char ch)
190 {
191 	if (ch >= 'a' && ch <= 'f')
192 		return ch - 'a' + 10;
193 	if (ch >= '0' && ch <= '9')
194 		return ch - '0';
195 	if (ch >= 'A' && ch <= 'F')
196 		return ch - 'A' + 10;
197 	return -1;
198 }
199 
200 #ifdef CONFIG_GDBSTUB_DEBUGGING
201 
debug_to_serial(const char * p,int n)202 void debug_to_serial(const char *p, int n)
203 {
204 	__debug_to_serial(p, n);
205 	/* gdbstub_console_write(NULL, p, n); */
206 }
207 
gdbstub_printk(const char * fmt,...)208 void gdbstub_printk(const char *fmt, ...)
209 {
210 	va_list args;
211 	int len;
212 
213 	/* Emit the output into the temporary buffer */
214 	va_start(args, fmt);
215 	len = vsnprintf(trans_buffer, sizeof(trans_buffer), fmt, args);
216 	va_end(args);
217 	debug_to_serial(trans_buffer, len);
218 }
219 
220 #endif
221 
gdbstub_strcpy(char * dst,const char * src)222 static inline char *gdbstub_strcpy(char *dst, const char *src)
223 {
224 	int loop = 0;
225 	while ((dst[loop] = src[loop]))
226 	       loop++;
227 	return dst;
228 }
229 
230 /*
231  * scan for the sequence $<data>#<checksum>
232  */
getpacket(char * buffer)233 static void getpacket(char *buffer)
234 {
235 	unsigned char checksum;
236 	unsigned char xmitcsum;
237 	unsigned char ch;
238 	int count, i, ret, error;
239 
240 	for (;;) {
241 		/*
242 		 * wait around for the start character,
243 		 * ignore all other characters
244 		 */
245 		do {
246 			gdbstub_io_rx_char(&ch, 0);
247 		} while (ch != '$');
248 
249 		checksum = 0;
250 		xmitcsum = -1;
251 		count = 0;
252 		error = 0;
253 
254 		/*
255 		 * now, read until a # or end of buffer is found
256 		 */
257 		while (count < BUFMAX) {
258 			ret = gdbstub_io_rx_char(&ch, 0);
259 			if (ret < 0)
260 				error = ret;
261 
262 			if (ch == '#')
263 				break;
264 			checksum += ch;
265 			buffer[count] = ch;
266 			count++;
267 		}
268 
269 		if (error == -EIO) {
270 			gdbstub_proto("### GDB Rx Error - Skipping packet"
271 				      " ###\n");
272 			gdbstub_proto("### GDB Tx NAK\n");
273 			gdbstub_io_tx_char('-');
274 			continue;
275 		}
276 
277 		if (count >= BUFMAX || error)
278 			continue;
279 
280 		buffer[count] = 0;
281 
282 		/* read the checksum */
283 		ret = gdbstub_io_rx_char(&ch, 0);
284 		if (ret < 0)
285 			error = ret;
286 		xmitcsum = hex(ch) << 4;
287 
288 		ret = gdbstub_io_rx_char(&ch, 0);
289 		if (ret < 0)
290 			error = ret;
291 		xmitcsum |= hex(ch);
292 
293 		if (error) {
294 			if (error == -EIO)
295 				gdbstub_io("### GDB Rx Error -"
296 					   " Skipping packet\n");
297 			gdbstub_io("### GDB Tx NAK\n");
298 			gdbstub_io_tx_char('-');
299 			continue;
300 		}
301 
302 		/* check the checksum */
303 		if (checksum != xmitcsum) {
304 			gdbstub_io("### GDB Tx NAK\n");
305 			gdbstub_io_tx_char('-');	/* failed checksum */
306 			continue;
307 		}
308 
309 		gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
310 		gdbstub_io("### GDB Tx ACK\n");
311 		gdbstub_io_tx_char('+'); /* successful transfer */
312 
313 		/*
314 		 * if a sequence char is present,
315 		 * reply the sequence ID
316 		 */
317 		if (buffer[2] == ':') {
318 			gdbstub_io_tx_char(buffer[0]);
319 			gdbstub_io_tx_char(buffer[1]);
320 
321 			/*
322 			 * remove sequence chars from buffer
323 			 */
324 			count = 0;
325 			while (buffer[count])
326 				count++;
327 			for (i = 3; i <= count; i++)
328 				buffer[i - 3] = buffer[i];
329 		}
330 
331 		break;
332 	}
333 }
334 
335 /*
336  * send the packet in buffer.
337  * - return 0 if successfully ACK'd
338  * - return 1 if abandoned due to new incoming packet
339  */
putpacket(char * buffer)340 static int putpacket(char *buffer)
341 {
342 	unsigned char checksum;
343 	unsigned char ch;
344 	int count;
345 
346 	/*
347 	 * $<packet info>#<checksum>.
348 	 */
349 	gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
350 
351 	do {
352 		gdbstub_io_tx_char('$');
353 		checksum = 0;
354 		count = 0;
355 
356 		while ((ch = buffer[count]) != 0) {
357 			gdbstub_io_tx_char(ch);
358 			checksum += ch;
359 			count += 1;
360 		}
361 
362 		gdbstub_io_tx_char('#');
363 		gdbstub_io_tx_char(hex_asc_hi(checksum));
364 		gdbstub_io_tx_char(hex_asc_lo(checksum));
365 
366 	} while (gdbstub_io_rx_char(&ch, 0),
367 		 ch == '-' && (gdbstub_io("### GDB Rx NAK\n"), 0),
368 		 ch != '-' && ch != '+' &&
369 		 (gdbstub_io("### GDB Rx ??? %02x\n", ch), 0),
370 		 ch != '+' && ch != '$');
371 
372 	if (ch == '+') {
373 		gdbstub_io("### GDB Rx ACK\n");
374 		return 0;
375 	}
376 
377 	gdbstub_io("### GDB Tx Abandoned\n");
378 	gdbstub_rx_unget = ch;
379 	return 1;
380 }
381 
382 /*
383  * While we find nice hex chars, build an int.
384  * Return number of chars processed.
385  */
hexToInt(char ** ptr,int * intValue)386 static int hexToInt(char **ptr, int *intValue)
387 {
388 	int numChars = 0;
389 	int hexValue;
390 
391 	*intValue = 0;
392 
393 	while (**ptr) {
394 		hexValue = hex(**ptr);
395 		if (hexValue < 0)
396 			break;
397 
398 		*intValue = (*intValue << 4) | hexValue;
399 		numChars++;
400 
401 		(*ptr)++;
402 	}
403 
404 	return (numChars);
405 }
406 
407 #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
408 /*
409  * We single-step by setting breakpoints. When an exception
410  * is handled, we need to restore the instructions hoisted
411  * when the breakpoints were set.
412  *
413  * This is where we save the original instructions.
414  */
415 static struct gdb_bp_save {
416 	u8	*addr;
417 	u8	opcode[2];
418 } step_bp[2];
419 
420 static const unsigned char gdbstub_insn_sizes[256] =
421 {
422 	/* 1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
423 	1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3,	/* 0 */
424 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
425 	2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
426 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
427 	1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
428 	1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
429 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
430 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
431 	2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
432 	2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
433 	2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
434 	2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
435 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
436 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
437 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
438 	0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1  /* f */
439 };
440 
__gdbstub_mark_bp(u8 * addr,int ix)441 static int __gdbstub_mark_bp(u8 *addr, int ix)
442 {
443 	/* vmalloc area */
444 	if (((u8 *) VMALLOC_START <= addr) && (addr < (u8 *) VMALLOC_END))
445 		goto okay;
446 	/* SRAM, SDRAM */
447 	if (((u8 *) 0x80000000UL <= addr) && (addr < (u8 *) 0xa0000000UL))
448 		goto okay;
449 	return 0;
450 
451 okay:
452 	if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
453 	    gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
454 		return 0;
455 
456 	step_bp[ix].addr = addr;
457 	return 1;
458 }
459 
__gdbstub_restore_bp(void)460 static inline void __gdbstub_restore_bp(void)
461 {
462 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
463 	if (step_bp[0].addr) {
464 		gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
465 		gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
466 	}
467 	if (step_bp[1].addr) {
468 		gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
469 		gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
470 	}
471 #else
472 	if (step_bp[0].addr)
473 		gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
474 	if (step_bp[1].addr)
475 		gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
476 #endif
477 
478 	gdbstub_flush_caches = 1;
479 
480 	step_bp[0].addr		= NULL;
481 	step_bp[0].opcode[0]	= 0;
482 	step_bp[0].opcode[1]	= 0;
483 	step_bp[1].addr		= NULL;
484 	step_bp[1].opcode[0]	= 0;
485 	step_bp[1].opcode[1]	= 0;
486 }
487 
488 /*
489  * emulate single stepping by means of breakpoint instructions
490  */
gdbstub_single_step(struct pt_regs * regs)491 static int gdbstub_single_step(struct pt_regs *regs)
492 {
493 	unsigned size;
494 	uint32_t x;
495 	uint8_t cur, *pc, *sp;
496 
497 	step_bp[0].addr		= NULL;
498 	step_bp[0].opcode[0]	= 0;
499 	step_bp[0].opcode[1]	= 0;
500 	step_bp[1].addr		= NULL;
501 	step_bp[1].opcode[0]	= 0;
502 	step_bp[1].opcode[1]	= 0;
503 	x = 0;
504 
505 	pc = (u8 *) regs->pc;
506 	sp = (u8 *) (regs + 1);
507 	if (gdbstub_read_byte(pc, &cur) < 0)
508 		return -EFAULT;
509 
510 	gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
511 
512 	gdbstub_flush_caches = 1;
513 
514 	size = gdbstub_insn_sizes[cur];
515 	if (size > 0) {
516 		if (!__gdbstub_mark_bp(pc + size, 0))
517 			goto fault;
518 	} else {
519 		switch (cur) {
520 			/* Bxx (d8,PC) */
521 		case 0xc0 ... 0xca:
522 			if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
523 				goto fault;
524 			if (!__gdbstub_mark_bp(pc + 2, 0))
525 				goto fault;
526 			if ((x < 0 || x > 2) &&
527 			    !__gdbstub_mark_bp(pc + (s8) x, 1))
528 				goto fault;
529 			break;
530 
531 			/* LXX (d8,PC) */
532 		case 0xd0 ... 0xda:
533 			if (!__gdbstub_mark_bp(pc + 1, 0))
534 				goto fault;
535 			if (regs->pc != regs->lar &&
536 			    !__gdbstub_mark_bp((u8 *) regs->lar, 1))
537 				goto fault;
538 			break;
539 
540 			/* SETLB - loads the next for bytes into the LIR
541 			 * register */
542 		case 0xdb:
543 			if (!__gdbstub_mark_bp(pc + 1, 0))
544 				goto fault;
545 			break;
546 
547 			/* JMP (d16,PC) or CALL (d16,PC) */
548 		case 0xcc:
549 		case 0xcd:
550 			if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
551 			    gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
552 				goto fault;
553 			if (!__gdbstub_mark_bp(pc + (s16) x, 0))
554 				goto fault;
555 			break;
556 
557 			/* JMP (d32,PC) or CALL (d32,PC) */
558 		case 0xdc:
559 		case 0xdd:
560 			if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
561 			    gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
562 			    gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
563 			    gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
564 				goto fault;
565 			if (!__gdbstub_mark_bp(pc + (s32) x, 0))
566 				goto fault;
567 			break;
568 
569 			/* RETF */
570 		case 0xde:
571 			if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
572 				goto fault;
573 			break;
574 
575 			/* RET */
576 		case 0xdf:
577 			if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
578 				goto fault;
579 			sp += (s8)x;
580 			if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
581 			    gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
582 			    gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
583 			    gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
584 				goto fault;
585 			if (!__gdbstub_mark_bp((u8 *) x, 0))
586 				goto fault;
587 			break;
588 
589 		case 0xf0:
590 			if (gdbstub_read_byte(pc + 1, &cur) < 0)
591 				goto fault;
592 
593 			if (cur >= 0xf0 && cur <= 0xf7) {
594 				/* JMP (An) / CALLS (An) */
595 				switch (cur & 3) {
596 				case 0: x = regs->a0; break;
597 				case 1: x = regs->a1; break;
598 				case 2: x = regs->a2; break;
599 				case 3: x = regs->a3; break;
600 				}
601 				if (!__gdbstub_mark_bp((u8 *) x, 0))
602 					goto fault;
603 			} else if (cur == 0xfc) {
604 				/* RETS */
605 				if (gdbstub_read_byte(
606 					    sp + 0, ((u8 *) &x) + 0) < 0 ||
607 				    gdbstub_read_byte(
608 					    sp + 1, ((u8 *) &x) + 1) < 0 ||
609 				    gdbstub_read_byte(
610 					    sp + 2, ((u8 *) &x) + 2) < 0 ||
611 				    gdbstub_read_byte(
612 					    sp + 3, ((u8 *) &x) + 3) < 0)
613 					goto fault;
614 				if (!__gdbstub_mark_bp((u8 *) x, 0))
615 					goto fault;
616 			} else if (cur == 0xfd) {
617 				/* RTI */
618 				if (gdbstub_read_byte(
619 					    sp + 4, ((u8 *) &x) + 0) < 0 ||
620 				    gdbstub_read_byte(
621 					    sp + 5, ((u8 *) &x) + 1) < 0 ||
622 				    gdbstub_read_byte(
623 					    sp + 6, ((u8 *) &x) + 2) < 0 ||
624 				    gdbstub_read_byte(
625 					    sp + 7, ((u8 *) &x) + 3) < 0)
626 					goto fault;
627 				if (!__gdbstub_mark_bp((u8 *) x, 0))
628 					goto fault;
629 			} else {
630 				if (!__gdbstub_mark_bp(pc + 2, 0))
631 					goto fault;
632 			}
633 
634 			break;
635 
636 			/* potential 3-byte conditional branches */
637 		case 0xf8:
638 			if (gdbstub_read_byte(pc + 1, &cur) < 0)
639 				goto fault;
640 			if (!__gdbstub_mark_bp(pc + 3, 0))
641 				goto fault;
642 
643 			if (cur >= 0xe8 && cur <= 0xeb) {
644 				if (gdbstub_read_byte(
645 					    pc + 2, ((u8 *) &x) + 0) < 0)
646 					goto fault;
647 				if ((x < 0 || x > 3) &&
648 				    !__gdbstub_mark_bp(pc + (s8) x, 1))
649 					goto fault;
650 			}
651 			break;
652 
653 		case 0xfa:
654 			if (gdbstub_read_byte(pc + 1, &cur) < 0)
655 				goto fault;
656 
657 			if (cur == 0xff) {
658 				/* CALLS (d16,PC) */
659 				if (gdbstub_read_byte(
660 					    pc + 2, ((u8 *) &x) + 0) < 0 ||
661 				    gdbstub_read_byte(
662 					    pc + 3, ((u8 *) &x) + 1) < 0)
663 					goto fault;
664 				if (!__gdbstub_mark_bp(pc + (s16) x, 0))
665 					goto fault;
666 			} else {
667 				if (!__gdbstub_mark_bp(pc + 4, 0))
668 					goto fault;
669 			}
670 			break;
671 
672 		case 0xfc:
673 			if (gdbstub_read_byte(pc + 1, &cur) < 0)
674 				goto fault;
675 			if (cur == 0xff) {
676 				/* CALLS (d32,PC) */
677 				if (gdbstub_read_byte(
678 					    pc + 2, ((u8 *) &x) + 0) < 0 ||
679 				    gdbstub_read_byte(
680 					    pc + 3, ((u8 *) &x) + 1) < 0 ||
681 				    gdbstub_read_byte(
682 					    pc + 4, ((u8 *) &x) + 2) < 0 ||
683 				    gdbstub_read_byte(
684 					    pc + 5, ((u8 *) &x) + 3) < 0)
685 					goto fault;
686 				if (!__gdbstub_mark_bp(
687 					    pc + (s32) x, 0))
688 					goto fault;
689 			} else {
690 				if (!__gdbstub_mark_bp(
691 					    pc + 6, 0))
692 					goto fault;
693 			}
694 			break;
695 
696 		}
697 	}
698 
699 	gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
700 		     step_bp[0].opcode[0], step_bp[0].addr,
701 		     step_bp[1].opcode[0], step_bp[1].addr);
702 
703 	if (step_bp[0].addr) {
704 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
705 		if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
706 		    gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
707 			goto fault;
708 #else
709 		if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
710 			goto fault;
711 #endif
712 	}
713 
714 	if (step_bp[1].addr) {
715 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
716 		if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
717 		    gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
718 			goto fault;
719 #else
720 		if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
721 			goto fault;
722 #endif
723 	}
724 
725 	return 0;
726 
727  fault:
728 	/* uh-oh - silly address alert, try and restore things */
729 	__gdbstub_restore_bp();
730 	return -EFAULT;
731 }
732 #endif /* CONFIG_GDBSTUB_ALLOW_SINGLE_STEP */
733 
734 #ifdef CONFIG_GDBSTUB_CONSOLE
735 
gdbstub_console_write(struct console * con,const char * p,unsigned n)736 void gdbstub_console_write(struct console *con, const char *p, unsigned n)
737 {
738 	static const char gdbstub_cr[] = { 0x0d };
739 	char outbuf[26];
740 	int qty;
741 	u8 busy;
742 
743 	busy = gdbstub_busy;
744 	gdbstub_busy = 1;
745 
746 	outbuf[0] = 'O';
747 
748 	while (n > 0) {
749 		qty = 1;
750 
751 		while (n > 0 && qty < 20) {
752 			mem2hex(p, outbuf + qty, 2, 0);
753 			qty += 2;
754 			if (*p == 0x0a) {
755 				mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
756 				qty += 2;
757 			}
758 			p++;
759 			n--;
760 		}
761 
762 		outbuf[qty] = 0;
763 		putpacket(outbuf);
764 	}
765 
766 	gdbstub_busy = busy;
767 }
768 
gdbstub_console_dev(struct console * con)769 static kdev_t gdbstub_console_dev(struct console *con)
770 {
771 	return MKDEV(1, 3); /* /dev/null */
772 }
773 
774 static struct console gdbstub_console = {
775 	.name	= "gdb",
776 	.write	= gdbstub_console_write,
777 	.device	= gdbstub_console_dev,
778 	.flags	= CON_PRINTBUFFER,
779 	.index	= -1,
780 };
781 
782 #endif
783 
784 /*
785  * Convert the memory pointed to by mem into hex, placing result in buf.
786  * - if successful, return a pointer to the last char put in buf (NUL)
787  * - in case of mem fault, return NULL
788  * may_fault is non-zero if we are reading from arbitrary memory, but is
789  * currently not used.
790  */
791 static
mem2hex(const void * _mem,char * buf,int count,int may_fault)792 unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
793 {
794 	const u8 *mem = _mem;
795 	u8 ch[4];
796 
797 	if ((u32) mem & 1 && count >= 1) {
798 		if (gdbstub_read_byte(mem, ch) != 0)
799 			return 0;
800 		buf = hex_byte_pack(buf, ch[0]);
801 		mem++;
802 		count--;
803 	}
804 
805 	if ((u32) mem & 3 && count >= 2) {
806 		if (gdbstub_read_word(mem, ch) != 0)
807 			return 0;
808 		buf = hex_byte_pack(buf, ch[0]);
809 		buf = hex_byte_pack(buf, ch[1]);
810 		mem += 2;
811 		count -= 2;
812 	}
813 
814 	while (count >= 4) {
815 		if (gdbstub_read_dword(mem, ch) != 0)
816 			return 0;
817 		buf = hex_byte_pack(buf, ch[0]);
818 		buf = hex_byte_pack(buf, ch[1]);
819 		buf = hex_byte_pack(buf, ch[2]);
820 		buf = hex_byte_pack(buf, ch[3]);
821 		mem += 4;
822 		count -= 4;
823 	}
824 
825 	if (count >= 2) {
826 		if (gdbstub_read_word(mem, ch) != 0)
827 			return 0;
828 		buf = hex_byte_pack(buf, ch[0]);
829 		buf = hex_byte_pack(buf, ch[1]);
830 		mem += 2;
831 		count -= 2;
832 	}
833 
834 	if (count >= 1) {
835 		if (gdbstub_read_byte(mem, ch) != 0)
836 			return 0;
837 		buf = hex_byte_pack(buf, ch[0]);
838 	}
839 
840 	*buf = 0;
841 	return buf;
842 }
843 
844 /*
845  * convert the hex array pointed to by buf into binary to be placed in mem
846  * return a pointer to the character AFTER the last byte written
847  * may_fault is non-zero if we are reading from arbitrary memory, but is
848  * currently not used.
849  */
850 static
hex2mem(const char * buf,void * _mem,int count,int may_fault)851 const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
852 {
853 	u8 *mem = _mem;
854 	union {
855 		u32 val;
856 		u8 b[4];
857 	} ch;
858 
859 	if ((u32) mem & 1 && count >= 1) {
860 		ch.b[0]  = hex(*buf++) << 4;
861 		ch.b[0] |= hex(*buf++);
862 		if (gdbstub_write_byte(ch.val, mem) != 0)
863 			return 0;
864 		mem++;
865 		count--;
866 	}
867 
868 	if ((u32) mem & 3 && count >= 2) {
869 		ch.b[0]  = hex(*buf++) << 4;
870 		ch.b[0] |= hex(*buf++);
871 		ch.b[1]  = hex(*buf++) << 4;
872 		ch.b[1] |= hex(*buf++);
873 		if (gdbstub_write_word(ch.val, mem) != 0)
874 			return 0;
875 		mem += 2;
876 		count -= 2;
877 	}
878 
879 	while (count >= 4) {
880 		ch.b[0]  = hex(*buf++) << 4;
881 		ch.b[0] |= hex(*buf++);
882 		ch.b[1]  = hex(*buf++) << 4;
883 		ch.b[1] |= hex(*buf++);
884 		ch.b[2]  = hex(*buf++) << 4;
885 		ch.b[2] |= hex(*buf++);
886 		ch.b[3]  = hex(*buf++) << 4;
887 		ch.b[3] |= hex(*buf++);
888 		if (gdbstub_write_dword(ch.val, mem) != 0)
889 			return 0;
890 		mem += 4;
891 		count -= 4;
892 	}
893 
894 	if (count >= 2) {
895 		ch.b[0]  = hex(*buf++) << 4;
896 		ch.b[0] |= hex(*buf++);
897 		ch.b[1]  = hex(*buf++) << 4;
898 		ch.b[1] |= hex(*buf++);
899 		if (gdbstub_write_word(ch.val, mem) != 0)
900 			return 0;
901 		mem += 2;
902 		count -= 2;
903 	}
904 
905 	if (count >= 1) {
906 		ch.b[0]  = hex(*buf++) << 4;
907 		ch.b[0] |= hex(*buf++);
908 		if (gdbstub_write_byte(ch.val, mem) != 0)
909 			return 0;
910 	}
911 
912 	return buf;
913 }
914 
915 /*
916  * This table contains the mapping between MN10300 exception codes, and
917  * signals, which are primarily what GDB understands.  It also indicates
918  * which hardware traps we need to commandeer when initializing the stub.
919  */
920 static const struct excep_to_sig_map {
921 	enum exception_code	excep;	/* MN10300 exception code */
922 	unsigned char		signo;	/* Signal that we map this into */
923 } excep_to_sig_map[] = {
924 	{ EXCEP_ITLBMISS,	SIGSEGV		},
925 	{ EXCEP_DTLBMISS,	SIGSEGV		},
926 	{ EXCEP_TRAP,		SIGTRAP		},
927 	{ EXCEP_ISTEP,		SIGTRAP		},
928 	{ EXCEP_IBREAK,		SIGTRAP		},
929 	{ EXCEP_OBREAK,		SIGTRAP		},
930 	{ EXCEP_UNIMPINS,	SIGILL		},
931 	{ EXCEP_UNIMPEXINS,	SIGILL		},
932 	{ EXCEP_MEMERR,		SIGSEGV		},
933 	{ EXCEP_MISALIGN,	SIGSEGV		},
934 	{ EXCEP_BUSERROR,	SIGBUS		},
935 	{ EXCEP_ILLINSACC,	SIGSEGV		},
936 	{ EXCEP_ILLDATACC,	SIGSEGV		},
937 	{ EXCEP_IOINSACC,	SIGSEGV		},
938 	{ EXCEP_PRIVINSACC,	SIGSEGV		},
939 	{ EXCEP_PRIVDATACC,	SIGSEGV		},
940 	{ EXCEP_FPU_DISABLED,	SIGFPE		},
941 	{ EXCEP_FPU_UNIMPINS,	SIGFPE		},
942 	{ EXCEP_FPU_OPERATION,	SIGFPE		},
943 	{ EXCEP_WDT,		SIGALRM		},
944 	{ EXCEP_NMI,		SIGQUIT		},
945 	{ EXCEP_IRQ_LEVEL0,	SIGINT		},
946 	{ EXCEP_IRQ_LEVEL1,	SIGINT		},
947 	{ EXCEP_IRQ_LEVEL2,	SIGINT		},
948 	{ EXCEP_IRQ_LEVEL3,	SIGINT		},
949 	{ EXCEP_IRQ_LEVEL4,	SIGINT		},
950 	{ EXCEP_IRQ_LEVEL5,	SIGINT		},
951 	{ EXCEP_IRQ_LEVEL6,	SIGINT		},
952 	{ 0, 0}
953 };
954 
955 /*
956  * convert the MN10300 exception code into a UNIX signal number
957  */
computeSignal(enum exception_code excep)958 static int computeSignal(enum exception_code excep)
959 {
960 	const struct excep_to_sig_map *map;
961 
962 	for (map = excep_to_sig_map; map->signo; map++)
963 		if (map->excep == excep)
964 			return map->signo;
965 
966 	return SIGHUP; /* default for things we don't know about */
967 }
968 
969 static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
970 
971 /*
972  *
973  */
gdbstub_store_fpu(void)974 static void gdbstub_store_fpu(void)
975 {
976 #ifdef CONFIG_FPU
977 
978 	asm volatile(
979 		"or %2,epsw\n"
980 #ifdef CONFIG_MN10300_PROC_MN103E010
981 		"nop\n"
982 		"nop\n"
983 #endif
984 		"mov %1, a1\n"
985 		"fmov fs0,  (a1+)\n"
986 		"fmov fs1,  (a1+)\n"
987 		"fmov fs2,  (a1+)\n"
988 		"fmov fs3,  (a1+)\n"
989 		"fmov fs4,  (a1+)\n"
990 		"fmov fs5,  (a1+)\n"
991 		"fmov fs6,  (a1+)\n"
992 		"fmov fs7,  (a1+)\n"
993 		"fmov fs8,  (a1+)\n"
994 		"fmov fs9,  (a1+)\n"
995 		"fmov fs10, (a1+)\n"
996 		"fmov fs11, (a1+)\n"
997 		"fmov fs12, (a1+)\n"
998 		"fmov fs13, (a1+)\n"
999 		"fmov fs14, (a1+)\n"
1000 		"fmov fs15, (a1+)\n"
1001 		"fmov fs16, (a1+)\n"
1002 		"fmov fs17, (a1+)\n"
1003 		"fmov fs18, (a1+)\n"
1004 		"fmov fs19, (a1+)\n"
1005 		"fmov fs20, (a1+)\n"
1006 		"fmov fs21, (a1+)\n"
1007 		"fmov fs22, (a1+)\n"
1008 		"fmov fs23, (a1+)\n"
1009 		"fmov fs24, (a1+)\n"
1010 		"fmov fs25, (a1+)\n"
1011 		"fmov fs26, (a1+)\n"
1012 		"fmov fs27, (a1+)\n"
1013 		"fmov fs28, (a1+)\n"
1014 		"fmov fs29, (a1+)\n"
1015 		"fmov fs30, (a1+)\n"
1016 		"fmov fs31, (a1+)\n"
1017 		"fmov fpcr, %0\n"
1018 		: "=d"(gdbstub_fpcr)
1019 		: "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
1020 		: "a1"
1021 		);
1022 #endif
1023 }
1024 
1025 /*
1026  *
1027  */
gdbstub_load_fpu(void)1028 static void gdbstub_load_fpu(void)
1029 {
1030 #ifdef CONFIG_FPU
1031 
1032 	asm volatile(
1033 		"or %1,epsw\n"
1034 #ifdef CONFIG_MN10300_PROC_MN103E010
1035 		"nop\n"
1036 		"nop\n"
1037 #endif
1038 		"mov %0, a1\n"
1039 		"fmov (a1+), fs0\n"
1040 		"fmov (a1+), fs1\n"
1041 		"fmov (a1+), fs2\n"
1042 		"fmov (a1+), fs3\n"
1043 		"fmov (a1+), fs4\n"
1044 		"fmov (a1+), fs5\n"
1045 		"fmov (a1+), fs6\n"
1046 		"fmov (a1+), fs7\n"
1047 		"fmov (a1+), fs8\n"
1048 		"fmov (a1+), fs9\n"
1049 		"fmov (a1+), fs10\n"
1050 		"fmov (a1+), fs11\n"
1051 		"fmov (a1+), fs12\n"
1052 		"fmov (a1+), fs13\n"
1053 		"fmov (a1+), fs14\n"
1054 		"fmov (a1+), fs15\n"
1055 		"fmov (a1+), fs16\n"
1056 		"fmov (a1+), fs17\n"
1057 		"fmov (a1+), fs18\n"
1058 		"fmov (a1+), fs19\n"
1059 		"fmov (a1+), fs20\n"
1060 		"fmov (a1+), fs21\n"
1061 		"fmov (a1+), fs22\n"
1062 		"fmov (a1+), fs23\n"
1063 		"fmov (a1+), fs24\n"
1064 		"fmov (a1+), fs25\n"
1065 		"fmov (a1+), fs26\n"
1066 		"fmov (a1+), fs27\n"
1067 		"fmov (a1+), fs28\n"
1068 		"fmov (a1+), fs29\n"
1069 		"fmov (a1+), fs30\n"
1070 		"fmov (a1+), fs31\n"
1071 		"fmov %2, fpcr\n"
1072 		:
1073 		: "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
1074 		: "a1"
1075 	);
1076 #endif
1077 }
1078 
1079 /*
1080  * set a software breakpoint
1081  */
gdbstub_set_breakpoint(u8 * addr,int len)1082 int gdbstub_set_breakpoint(u8 *addr, int len)
1083 {
1084 	int bkpt, loop, xloop;
1085 
1086 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1087 	len = (len + 1) & ~1;
1088 #endif
1089 
1090 	gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
1091 
1092 	for (bkpt = 255; bkpt >= 0; bkpt--)
1093 		if (!gdbstub_bkpts[bkpt].addr)
1094 			break;
1095 	if (bkpt < 0)
1096 		return -ENOSPC;
1097 
1098 	for (loop = 0; loop < len; loop++)
1099 		if (gdbstub_read_byte(&addr[loop],
1100 				      &gdbstub_bkpts[bkpt].origbytes[loop]
1101 				      ) < 0)
1102 			return -EFAULT;
1103 
1104 	gdbstub_flush_caches = 1;
1105 
1106 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1107 	for (loop = 0; loop < len; loop++)
1108 		if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
1109 			goto restore;
1110 #else
1111 	for (loop = 0; loop < len; loop++)
1112 		if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
1113 			goto restore;
1114 #endif
1115 
1116 	gdbstub_bkpts[bkpt].addr = addr;
1117 	gdbstub_bkpts[bkpt].len = len;
1118 
1119 	gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
1120 		     bkpt,
1121 		     gdbstub_bkpts[bkpt].addr,
1122 		     gdbstub_bkpts[bkpt].addr + gdbstub_bkpts[bkpt].len - 1,
1123 		     gdbstub_bkpts[bkpt].origbytes[0],
1124 		     gdbstub_bkpts[bkpt].origbytes[1],
1125 		     gdbstub_bkpts[bkpt].origbytes[2],
1126 		     gdbstub_bkpts[bkpt].origbytes[3],
1127 		     gdbstub_bkpts[bkpt].origbytes[4],
1128 		     gdbstub_bkpts[bkpt].origbytes[5],
1129 		     gdbstub_bkpts[bkpt].origbytes[6]
1130 		     );
1131 
1132 	return 0;
1133 
1134 restore:
1135 	for (xloop = 0; xloop < loop; xloop++)
1136 		gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
1137 				   addr + xloop);
1138 	return -EFAULT;
1139 }
1140 
1141 /*
1142  * clear a software breakpoint
1143  */
gdbstub_clear_breakpoint(u8 * addr,int len)1144 int gdbstub_clear_breakpoint(u8 *addr, int len)
1145 {
1146 	int bkpt, loop;
1147 
1148 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1149 	len = (len + 1) & ~1;
1150 #endif
1151 
1152 	gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
1153 
1154 	for (bkpt = 255; bkpt >= 0; bkpt--)
1155 		if (gdbstub_bkpts[bkpt].addr == addr &&
1156 		    gdbstub_bkpts[bkpt].len == len)
1157 			break;
1158 	if (bkpt < 0)
1159 		return -ENOENT;
1160 
1161 	gdbstub_bkpts[bkpt].addr = NULL;
1162 
1163 	gdbstub_flush_caches = 1;
1164 
1165 	for (loop = 0; loop < len; loop++)
1166 		if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
1167 				       addr + loop) < 0)
1168 			return -EFAULT;
1169 
1170 	return 0;
1171 }
1172 
1173 /*
1174  * This function does all command processing for interfacing to gdb
1175  * - returns 0 if the exception should be skipped, -ERROR otherwise.
1176  */
gdbstub(struct pt_regs * regs,enum exception_code excep)1177 static int gdbstub(struct pt_regs *regs, enum exception_code excep)
1178 {
1179 	unsigned long *stack;
1180 	unsigned long epsw, mdr;
1181 	uint32_t zero, ssp;
1182 	uint8_t broke;
1183 	char *ptr;
1184 	int sigval;
1185 	int addr;
1186 	int length;
1187 	int loop;
1188 
1189 	if (excep == EXCEP_FPU_DISABLED)
1190 		return -ENOTSUPP;
1191 
1192 	gdbstub_flush_caches = 0;
1193 
1194 	mn10300_set_gdbleds(1);
1195 
1196 	asm volatile("mov mdr,%0" : "=d"(mdr));
1197 	local_save_flags(epsw);
1198 	arch_local_change_intr_mask_level(
1199 		NUM2EPSW_IM(CONFIG_DEBUGGER_IRQ_LEVEL + 1));
1200 
1201 	gdbstub_store_fpu();
1202 
1203 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1204 	/* skip the initial pause loop */
1205 	if (regs->pc == (unsigned long) __gdbstub_pause)
1206 		regs->pc = (unsigned long) start_kernel;
1207 #endif
1208 
1209 	/* if we were single stepping, restore the opcodes hoisted for the
1210 	 * breakpoint[s] */
1211 	broke = 0;
1212 #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
1213 	if ((step_bp[0].addr && step_bp[0].addr == (u8 *) regs->pc) ||
1214 	    (step_bp[1].addr && step_bp[1].addr == (u8 *) regs->pc))
1215 		broke = 1;
1216 
1217 	__gdbstub_restore_bp();
1218 #endif
1219 
1220 	if (gdbstub_rx_unget) {
1221 		sigval = SIGINT;
1222 		if (gdbstub_rx_unget != 3)
1223 			goto packet_waiting;
1224 		gdbstub_rx_unget = 0;
1225 	}
1226 
1227 	stack = (unsigned long *) regs->sp;
1228 	sigval = broke ? SIGTRAP : computeSignal(excep);
1229 
1230 	/* send information about a BUG() */
1231 	if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
1232 		const struct bug_entry *bug;
1233 
1234 		bug = find_bug(regs->pc);
1235 		if (bug)
1236 			goto found_bug;
1237 		length = snprintf(trans_buffer, sizeof(trans_buffer),
1238 				  "BUG() at address %lx\n", regs->pc);
1239 		goto send_bug_pkt;
1240 
1241 	found_bug:
1242 		length = snprintf(trans_buffer, sizeof(trans_buffer),
1243 				  "BUG() at address %lx (%s:%d)\n",
1244 				  regs->pc, bug->file, bug->line);
1245 
1246 	send_bug_pkt:
1247 		ptr = output_buffer;
1248 		*ptr++ = 'O';
1249 		ptr = mem2hex(trans_buffer, ptr, length, 0);
1250 		*ptr = 0;
1251 		putpacket(output_buffer);
1252 
1253 		regs->pc -= 2;
1254 		sigval = SIGABRT;
1255 	} else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
1256 		regs->pc = regs->mdr;
1257 		sigval = SIGABRT;
1258 	}
1259 
1260 	/*
1261 	 * send a message to the debugger's user saying what happened if it may
1262 	 * not be clear cut (we can't map exceptions onto signals properly)
1263 	 */
1264 	if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
1265 		static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
1266 		static const char crlf[] = "\r\n";
1267 		char hx;
1268 		u32 bcberr = BCBERR;
1269 
1270 		ptr = output_buffer;
1271 		*ptr++ = 'O';
1272 		ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
1273 
1274 		hx = hex_asc_hi(excep >> 8);
1275 		ptr = hex_byte_pack(ptr, hx);
1276 		hx = hex_asc_lo(excep >> 8);
1277 		ptr = hex_byte_pack(ptr, hx);
1278 		hx = hex_asc_hi(excep);
1279 		ptr = hex_byte_pack(ptr, hx);
1280 		hx = hex_asc_lo(excep);
1281 		ptr = hex_byte_pack(ptr, hx);
1282 
1283 		ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1284 		*ptr = 0;
1285 		putpacket(output_buffer);	/* send it off... */
1286 
1287 		/* BCBERR */
1288 		ptr = output_buffer;
1289 		*ptr++ = 'O';
1290 		ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
1291 
1292 		hx = hex_asc_hi(bcberr >> 24);
1293 		ptr = hex_byte_pack(ptr, hx);
1294 		hx = hex_asc_lo(bcberr >> 24);
1295 		ptr = hex_byte_pack(ptr, hx);
1296 		hx = hex_asc_hi(bcberr >> 16);
1297 		ptr = hex_byte_pack(ptr, hx);
1298 		hx = hex_asc_lo(bcberr >> 16);
1299 		ptr = hex_byte_pack(ptr, hx);
1300 		hx = hex_asc_hi(bcberr >> 8);
1301 		ptr = hex_byte_pack(ptr, hx);
1302 		hx = hex_asc_lo(bcberr >> 8);
1303 		ptr = hex_byte_pack(ptr, hx);
1304 		hx = hex_asc_hi(bcberr);
1305 		ptr = hex_byte_pack(ptr, hx);
1306 		hx = hex_asc_lo(bcberr);
1307 		ptr = hex_byte_pack(ptr, hx);
1308 
1309 		ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1310 		*ptr = 0;
1311 		putpacket(output_buffer);	/* send it off... */
1312 	}
1313 
1314 	/*
1315 	 * tell the debugger that an exception has occurred
1316 	 */
1317 	ptr = output_buffer;
1318 
1319 	/*
1320 	 * Send trap type (converted to signal)
1321 	 */
1322 	*ptr++ = 'T';
1323 	ptr = hex_byte_pack(ptr, sigval);
1324 
1325 	/*
1326 	 * Send Error PC
1327 	 */
1328 	ptr = hex_byte_pack(ptr, GDB_REGID_PC);
1329 	*ptr++ = ':';
1330 	ptr = mem2hex(&regs->pc, ptr, 4, 0);
1331 	*ptr++ = ';';
1332 
1333 	/*
1334 	 * Send frame pointer
1335 	 */
1336 	ptr = hex_byte_pack(ptr, GDB_REGID_FP);
1337 	*ptr++ = ':';
1338 	ptr = mem2hex(&regs->a3, ptr, 4, 0);
1339 	*ptr++ = ';';
1340 
1341 	/*
1342 	 * Send stack pointer
1343 	 */
1344 	ssp = (unsigned long) (regs + 1);
1345 	ptr = hex_byte_pack(ptr, GDB_REGID_SP);
1346 	*ptr++ = ':';
1347 	ptr = mem2hex(&ssp, ptr, 4, 0);
1348 	*ptr++ = ';';
1349 
1350 	*ptr++ = 0;
1351 	putpacket(output_buffer);	/* send it off... */
1352 
1353 packet_waiting:
1354 	/*
1355 	 * Wait for input from remote GDB
1356 	 */
1357 	while (1) {
1358 		output_buffer[0] = 0;
1359 		getpacket(input_buffer);
1360 
1361 		switch (input_buffer[0]) {
1362 			/* request repeat of last signal number */
1363 		case '?':
1364 			output_buffer[0] = 'S';
1365 			output_buffer[1] = hex_asc_hi(sigval);
1366 			output_buffer[2] = hex_asc_lo(sigval);
1367 			output_buffer[3] = 0;
1368 			break;
1369 
1370 		case 'd':
1371 			/* toggle debug flag */
1372 			break;
1373 
1374 			/*
1375 			 * Return the value of the CPU registers
1376 			 */
1377 		case 'g':
1378 			zero = 0;
1379 			ssp = (u32) (regs + 1);
1380 			ptr = output_buffer;
1381 			ptr = mem2hex(&regs->d0, ptr, 4, 0);
1382 			ptr = mem2hex(&regs->d1, ptr, 4, 0);
1383 			ptr = mem2hex(&regs->d2, ptr, 4, 0);
1384 			ptr = mem2hex(&regs->d3, ptr, 4, 0);
1385 			ptr = mem2hex(&regs->a0, ptr, 4, 0);
1386 			ptr = mem2hex(&regs->a1, ptr, 4, 0);
1387 			ptr = mem2hex(&regs->a2, ptr, 4, 0);
1388 			ptr = mem2hex(&regs->a3, ptr, 4, 0);
1389 
1390 			ptr = mem2hex(&ssp, ptr, 4, 0);		/* 8 */
1391 			ptr = mem2hex(&regs->pc, ptr, 4, 0);
1392 			ptr = mem2hex(&regs->mdr, ptr, 4, 0);
1393 			ptr = mem2hex(&regs->epsw, ptr, 4, 0);
1394 			ptr = mem2hex(&regs->lir, ptr, 4, 0);
1395 			ptr = mem2hex(&regs->lar, ptr, 4, 0);
1396 			ptr = mem2hex(&regs->mdrq, ptr, 4, 0);
1397 
1398 			ptr = mem2hex(&regs->e0, ptr, 4, 0);	/* 15 */
1399 			ptr = mem2hex(&regs->e1, ptr, 4, 0);
1400 			ptr = mem2hex(&regs->e2, ptr, 4, 0);
1401 			ptr = mem2hex(&regs->e3, ptr, 4, 0);
1402 			ptr = mem2hex(&regs->e4, ptr, 4, 0);
1403 			ptr = mem2hex(&regs->e5, ptr, 4, 0);
1404 			ptr = mem2hex(&regs->e6, ptr, 4, 0);
1405 			ptr = mem2hex(&regs->e7, ptr, 4, 0);
1406 
1407 			ptr = mem2hex(&ssp, ptr, 4, 0);
1408 			ptr = mem2hex(&regs, ptr, 4, 0);
1409 			ptr = mem2hex(&regs->sp, ptr, 4, 0);
1410 			ptr = mem2hex(&regs->mcrh, ptr, 4, 0);	/* 26 */
1411 			ptr = mem2hex(&regs->mcrl, ptr, 4, 0);
1412 			ptr = mem2hex(&regs->mcvf, ptr, 4, 0);
1413 
1414 			ptr = mem2hex(&gdbstub_fpcr, ptr, 4, 0); /* 29 - FPCR */
1415 			ptr = mem2hex(&zero, ptr, 4, 0);
1416 			ptr = mem2hex(&zero, ptr, 4, 0);
1417 			for (loop = 0; loop < 32; loop++)
1418 				ptr = mem2hex(&gdbstub_fpufs_array[loop],
1419 					      ptr, 4, 0); /* 32 - FS0-31 */
1420 
1421 			break;
1422 
1423 			/*
1424 			 * set the value of the CPU registers - return OK
1425 			 */
1426 		case 'G':
1427 		{
1428 			const char *ptr;
1429 
1430 			ptr = &input_buffer[1];
1431 			ptr = hex2mem(ptr, &regs->d0, 4, 0);
1432 			ptr = hex2mem(ptr, &regs->d1, 4, 0);
1433 			ptr = hex2mem(ptr, &regs->d2, 4, 0);
1434 			ptr = hex2mem(ptr, &regs->d3, 4, 0);
1435 			ptr = hex2mem(ptr, &regs->a0, 4, 0);
1436 			ptr = hex2mem(ptr, &regs->a1, 4, 0);
1437 			ptr = hex2mem(ptr, &regs->a2, 4, 0);
1438 			ptr = hex2mem(ptr, &regs->a3, 4, 0);
1439 
1440 			ptr = hex2mem(ptr, &ssp, 4, 0);		/* 8 */
1441 			ptr = hex2mem(ptr, &regs->pc, 4, 0);
1442 			ptr = hex2mem(ptr, &regs->mdr, 4, 0);
1443 			ptr = hex2mem(ptr, &regs->epsw, 4, 0);
1444 			ptr = hex2mem(ptr, &regs->lir, 4, 0);
1445 			ptr = hex2mem(ptr, &regs->lar, 4, 0);
1446 			ptr = hex2mem(ptr, &regs->mdrq, 4, 0);
1447 
1448 			ptr = hex2mem(ptr, &regs->e0, 4, 0);	/* 15 */
1449 			ptr = hex2mem(ptr, &regs->e1, 4, 0);
1450 			ptr = hex2mem(ptr, &regs->e2, 4, 0);
1451 			ptr = hex2mem(ptr, &regs->e3, 4, 0);
1452 			ptr = hex2mem(ptr, &regs->e4, 4, 0);
1453 			ptr = hex2mem(ptr, &regs->e5, 4, 0);
1454 			ptr = hex2mem(ptr, &regs->e6, 4, 0);
1455 			ptr = hex2mem(ptr, &regs->e7, 4, 0);
1456 
1457 			ptr = hex2mem(ptr, &ssp, 4, 0);
1458 			ptr = hex2mem(ptr, &zero, 4, 0);
1459 			ptr = hex2mem(ptr, &regs->sp, 4, 0);
1460 			ptr = hex2mem(ptr, &regs->mcrh, 4, 0);	/* 26 */
1461 			ptr = hex2mem(ptr, &regs->mcrl, 4, 0);
1462 			ptr = hex2mem(ptr, &regs->mcvf, 4, 0);
1463 
1464 			ptr = hex2mem(ptr, &zero, 4, 0);	/* 29 - FPCR */
1465 			ptr = hex2mem(ptr, &zero, 4, 0);
1466 			ptr = hex2mem(ptr, &zero, 4, 0);
1467 			for (loop = 0; loop < 32; loop++)     /* 32 - FS0-31 */
1468 				ptr = hex2mem(ptr, &zero, 4, 0);
1469 
1470 #if 0
1471 			/*
1472 			 * See if the stack pointer has moved. If so, then copy
1473 			 * the saved locals and ins to the new location.
1474 			 */
1475 			unsigned long *newsp = (unsigned long *) registers[SP];
1476 			if (sp != newsp)
1477 				sp = memcpy(newsp, sp, 16 * 4);
1478 #endif
1479 
1480 			gdbstub_strcpy(output_buffer, "OK");
1481 		}
1482 		break;
1483 
1484 		/*
1485 		 * mAA..AA,LLLL  Read LLLL bytes at address AA..AA
1486 		 */
1487 		case 'm':
1488 			ptr = &input_buffer[1];
1489 
1490 			if (hexToInt(&ptr, &addr) &&
1491 			    *ptr++ == ',' &&
1492 			    hexToInt(&ptr, &length)
1493 			    ) {
1494 				if (mem2hex((char *) addr, output_buffer,
1495 					    length, 1))
1496 					break;
1497 				gdbstub_strcpy(output_buffer, "E03");
1498 			} else {
1499 				gdbstub_strcpy(output_buffer, "E01");
1500 			}
1501 			break;
1502 
1503 			/*
1504 			 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
1505 			 * return OK
1506 			 */
1507 		case 'M':
1508 			ptr = &input_buffer[1];
1509 
1510 			if (hexToInt(&ptr, &addr) &&
1511 			    *ptr++ == ',' &&
1512 			    hexToInt(&ptr, &length) &&
1513 			    *ptr++ == ':'
1514 			    ) {
1515 				if (hex2mem(ptr, (char *) addr, length, 1))
1516 					gdbstub_strcpy(output_buffer, "OK");
1517 				else
1518 					gdbstub_strcpy(output_buffer, "E03");
1519 
1520 				gdbstub_flush_caches = 1;
1521 			} else {
1522 				gdbstub_strcpy(output_buffer, "E02");
1523 			}
1524 			break;
1525 
1526 			/*
1527 			 * cAA..AA    Continue at address AA..AA(optional)
1528 			 */
1529 		case 'c':
1530 			/* try to read optional parameter, pc unchanged if no
1531 			 * parm */
1532 
1533 			ptr = &input_buffer[1];
1534 			if (hexToInt(&ptr, &addr))
1535 				regs->pc = addr;
1536 			goto done;
1537 
1538 			/*
1539 			 * kill the program
1540 			 */
1541 		case 'k' :
1542 			goto done;	/* just continue */
1543 
1544 			/*
1545 			 * Reset the whole machine (FIXME: system dependent)
1546 			 */
1547 		case 'r':
1548 			break;
1549 
1550 			/*
1551 			 * Step to next instruction
1552 			 */
1553 		case 's':
1554 			/* Using the T flag doesn't seem to perform single
1555 			 * stepping (it seems to wind up being caught by the
1556 			 * JTAG unit), so we have to use breakpoints and
1557 			 * continue instead.
1558 			 */
1559 #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
1560 			if (gdbstub_single_step(regs) < 0)
1561 				/* ignore any fault error for now */
1562 				gdbstub_printk("unable to set single-step"
1563 					       " bp\n");
1564 			goto done;
1565 #else
1566 			gdbstub_strcpy(output_buffer, "E01");
1567 			break;
1568 #endif
1569 
1570 			/*
1571 			 * Set baud rate (bBB)
1572 			 */
1573 		case 'b':
1574 			do {
1575 				int baudrate;
1576 
1577 				ptr = &input_buffer[1];
1578 				if (!hexToInt(&ptr, &baudrate)) {
1579 					gdbstub_strcpy(output_buffer, "B01");
1580 					break;
1581 				}
1582 
1583 				if (baudrate) {
1584 					/* ACK before changing speed */
1585 					putpacket("OK");
1586 					gdbstub_io_set_baud(baudrate);
1587 				}
1588 			} while (0);
1589 			break;
1590 
1591 			/*
1592 			 * Set breakpoint
1593 			 */
1594 		case 'Z':
1595 			ptr = &input_buffer[1];
1596 
1597 			if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1598 			    !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1599 			    !hexToInt(&ptr, &length)
1600 			    ) {
1601 				gdbstub_strcpy(output_buffer, "E01");
1602 				break;
1603 			}
1604 
1605 			/* only support software breakpoints */
1606 			gdbstub_strcpy(output_buffer, "E03");
1607 			if (loop != 0 ||
1608 			    length < 1 ||
1609 			    length > 7 ||
1610 			    (unsigned long) addr < 4096)
1611 				break;
1612 
1613 			if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
1614 				break;
1615 
1616 			gdbstub_strcpy(output_buffer, "OK");
1617 			break;
1618 
1619 			/*
1620 			 * Clear breakpoint
1621 			 */
1622 		case 'z':
1623 			ptr = &input_buffer[1];
1624 
1625 			if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1626 			    !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1627 			    !hexToInt(&ptr, &length)
1628 			    ) {
1629 				gdbstub_strcpy(output_buffer, "E01");
1630 				break;
1631 			}
1632 
1633 			/* only support software breakpoints */
1634 			gdbstub_strcpy(output_buffer, "E03");
1635 			if (loop != 0 ||
1636 			    length < 1 ||
1637 			    length > 7 ||
1638 			    (unsigned long) addr < 4096)
1639 				break;
1640 
1641 			if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
1642 				break;
1643 
1644 			gdbstub_strcpy(output_buffer, "OK");
1645 			break;
1646 
1647 		default:
1648 			gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
1649 				      input_buffer);
1650 			break;
1651 		}
1652 
1653 		/* reply to the request */
1654 		putpacket(output_buffer);
1655 	}
1656 
1657 done:
1658 	/*
1659 	 * Need to flush the instruction cache here, as we may
1660 	 * have deposited a breakpoint, and the icache probably
1661 	 * has no way of knowing that a data ref to some location
1662 	 * may have changed something that is in the instruction
1663 	 * cache.
1664 	 * NB: We flush both caches, just to be sure...
1665 	 */
1666 	if (gdbstub_flush_caches)
1667 		debugger_local_cache_flushinv();
1668 
1669 	gdbstub_load_fpu();
1670 	mn10300_set_gdbleds(0);
1671 	if (excep == EXCEP_NMI)
1672 		NMICR = NMICR_NMIF;
1673 
1674 	touch_softlockup_watchdog();
1675 
1676 	local_irq_restore(epsw);
1677 	return 0;
1678 }
1679 
1680 /*
1681  * Determine if we hit a debugger special breakpoint that needs skipping over
1682  * automatically.
1683  */
at_debugger_breakpoint(struct pt_regs * regs)1684 int at_debugger_breakpoint(struct pt_regs *regs)
1685 {
1686 	return 0;
1687 }
1688 
1689 /*
1690  * handle event interception
1691  */
debugger_intercept(enum exception_code excep,int signo,int si_code,struct pt_regs * regs)1692 asmlinkage int debugger_intercept(enum exception_code excep,
1693 				  int signo, int si_code, struct pt_regs *regs)
1694 {
1695 	static u8 notfirst = 1;
1696 	int ret;
1697 
1698 	if (gdbstub_busy)
1699 		gdbstub_printk("--> gdbstub reentered itself\n");
1700 	gdbstub_busy = 1;
1701 
1702 	if (notfirst) {
1703 		unsigned long mdr;
1704 		asm("mov mdr,%0" : "=d"(mdr));
1705 
1706 		gdbstub_entry(
1707 			"--> debugger_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
1708 			regs, excep, mdr, regs->pc);
1709 
1710 		gdbstub_entry(
1711 			"PC:  %08lx EPSW:  %08lx  SSP: %08lx mode: %s\n",
1712 			regs->pc, regs->epsw, (unsigned long) &ret,
1713 			user_mode(regs) ? "User" : "Super");
1714 		gdbstub_entry(
1715 			"d0:  %08lx   d1:  %08lx   d2: %08lx   d3: %08lx\n",
1716 			regs->d0, regs->d1, regs->d2, regs->d3);
1717 		gdbstub_entry(
1718 			"a0:  %08lx   a1:  %08lx   a2: %08lx   a3: %08lx\n",
1719 			regs->a0, regs->a1, regs->a2, regs->a3);
1720 		gdbstub_entry(
1721 			"e0:  %08lx   e1:  %08lx   e2: %08lx   e3: %08lx\n",
1722 			regs->e0, regs->e1, regs->e2, regs->e3);
1723 		gdbstub_entry(
1724 			"e4:  %08lx   e5:  %08lx   e6: %08lx   e7: %08lx\n",
1725 			regs->e4, regs->e5, regs->e6, regs->e7);
1726 		gdbstub_entry(
1727 			"lar: %08lx   lir: %08lx  mdr: %08lx  usp: %08lx\n",
1728 			regs->lar, regs->lir, regs->mdr, regs->sp);
1729 		gdbstub_entry(
1730 			"cvf: %08lx   crl: %08lx  crh: %08lx  drq: %08lx\n",
1731 			regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
1732 		gdbstub_entry(
1733 			"threadinfo=%p task=%p)\n",
1734 			current_thread_info(), current);
1735 	} else {
1736 		notfirst = 1;
1737 	}
1738 
1739 	ret = gdbstub(regs, excep);
1740 
1741 	gdbstub_entry("<-- debugger_intercept()\n");
1742 	gdbstub_busy = 0;
1743 	return ret;
1744 }
1745 
1746 /*
1747  * handle the GDB stub itself causing an exception
1748  */
gdbstub_exception(struct pt_regs * regs,enum exception_code excep)1749 asmlinkage void gdbstub_exception(struct pt_regs *regs,
1750 				  enum exception_code excep)
1751 {
1752 	unsigned long mdr;
1753 
1754 	asm("mov mdr,%0" : "=d"(mdr));
1755 	gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
1756 		      regs, excep, mdr);
1757 
1758 	while ((unsigned long) regs == 0xffffffff) {}
1759 
1760 	/* handle guarded memory accesses where we know it might fault */
1761 	if (regs->pc == (unsigned) gdbstub_read_byte_guard) {
1762 		regs->pc = (unsigned) gdbstub_read_byte_cont;
1763 		goto fault;
1764 	}
1765 
1766 	if (regs->pc == (unsigned) gdbstub_read_word_guard) {
1767 		regs->pc = (unsigned) gdbstub_read_word_cont;
1768 		goto fault;
1769 	}
1770 
1771 	if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
1772 		regs->pc = (unsigned) gdbstub_read_dword_cont;
1773 		goto fault;
1774 	}
1775 
1776 	if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
1777 		regs->pc = (unsigned) gdbstub_write_byte_cont;
1778 		goto fault;
1779 	}
1780 
1781 	if (regs->pc == (unsigned) gdbstub_write_word_guard) {
1782 		regs->pc = (unsigned) gdbstub_write_word_cont;
1783 		goto fault;
1784 	}
1785 
1786 	if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
1787 		regs->pc = (unsigned) gdbstub_write_dword_cont;
1788 		goto fault;
1789 	}
1790 
1791 	gdbstub_printk("\n### GDB stub caused an exception ###\n");
1792 
1793 	/* something went horribly wrong */
1794 	console_verbose();
1795 	show_registers(regs);
1796 
1797 	panic("GDB Stub caused an unexpected exception - can't continue\n");
1798 
1799 	/* we caught an attempt by the stub to access silly memory */
1800 fault:
1801 	gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
1802 	regs->d0 = -EFAULT;
1803 	return;
1804 }
1805 
1806 /*
1807  * send an exit message to GDB
1808  */
gdbstub_exit(int status)1809 void gdbstub_exit(int status)
1810 {
1811 	unsigned char checksum;
1812 	unsigned char ch;
1813 	int count;
1814 
1815 	gdbstub_busy = 1;
1816 	output_buffer[0] = 'W';
1817 	output_buffer[1] = hex_asc_hi(status);
1818 	output_buffer[2] = hex_asc_lo(status);
1819 	output_buffer[3] = 0;
1820 
1821 	gdbstub_io_tx_char('$');
1822 	checksum = 0;
1823 	count = 0;
1824 
1825 	while ((ch = output_buffer[count]) != 0) {
1826 		gdbstub_io_tx_char(ch);
1827 		checksum += ch;
1828 		count += 1;
1829 	}
1830 
1831 	gdbstub_io_tx_char('#');
1832 	gdbstub_io_tx_char(hex_asc_hi(checksum));
1833 	gdbstub_io_tx_char(hex_asc_lo(checksum));
1834 
1835 	/* make sure the output is flushed, or else RedBoot might clobber it */
1836 	gdbstub_io_tx_flush();
1837 
1838 	gdbstub_busy = 0;
1839 }
1840 
1841 /*
1842  * initialise the GDB stub
1843  */
gdbstub_init(void)1844 asmlinkage void __init gdbstub_init(void)
1845 {
1846 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1847 	unsigned char ch;
1848 	int ret;
1849 #endif
1850 
1851 	gdbstub_busy = 1;
1852 
1853 	printk(KERN_INFO "%s", gdbstub_banner);
1854 
1855 	gdbstub_io_init();
1856 
1857 	gdbstub_entry("--> gdbstub_init\n");
1858 
1859 	/* try to talk to GDB (or anyone insane enough to want to type GDB
1860 	 * protocol by hand) */
1861 	gdbstub_io("### GDB Tx ACK\n");
1862 	gdbstub_io_tx_char('+'); /* 'hello world' */
1863 
1864 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1865 	gdbstub_printk("GDB Stub waiting for packet\n");
1866 
1867 	/* in case GDB is started before us, ACK any packets that are already
1868 	 * sitting there (presumably "$?#xx")
1869 	 */
1870 	do { gdbstub_io_rx_char(&ch, 0); } while (ch != '$');
1871 	do { gdbstub_io_rx_char(&ch, 0); } while (ch != '#');
1872 	/* eat first csum byte */
1873 	do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1874 	/* eat second csum byte */
1875 	do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1876 
1877 	gdbstub_io("### GDB Tx NAK\n");
1878 	gdbstub_io_tx_char('-'); /* NAK it */
1879 
1880 #else
1881 	printk("GDB Stub ready\n");
1882 #endif
1883 
1884 	gdbstub_busy = 0;
1885 	gdbstub_entry("<-- gdbstub_init\n");
1886 }
1887 
1888 /*
1889  * register the console at a more appropriate time
1890  */
1891 #ifdef CONFIG_GDBSTUB_CONSOLE
gdbstub_postinit(void)1892 static int __init gdbstub_postinit(void)
1893 {
1894 	printk(KERN_NOTICE "registering console\n");
1895 	register_console(&gdbstub_console);
1896 	return 0;
1897 }
1898 
1899 __initcall(gdbstub_postinit);
1900 #endif
1901 
1902 /*
1903  * handle character reception on GDB serial port
1904  * - jump into the GDB stub if BREAK is detected on the serial line
1905  */
gdbstub_rx_irq(struct pt_regs * regs,enum exception_code excep)1906 asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
1907 {
1908 	char ch;
1909 	int ret;
1910 
1911 	gdbstub_entry("--> gdbstub_rx_irq\n");
1912 
1913 	do {
1914 		ret = gdbstub_io_rx_char(&ch, 1);
1915 		if (ret != -EIO && ret != -EAGAIN) {
1916 			if (ret != -EINTR)
1917 				gdbstub_rx_unget = ch;
1918 			gdbstub(regs, excep);
1919 		}
1920 	} while (ret != -EAGAIN);
1921 
1922 	gdbstub_entry("<-- gdbstub_rx_irq\n");
1923 }
1924