1 /*
2  * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * GDB stub for remote debugging
30  *
31  */
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <byteswap.h>
38 #include <ipxe/gdbstub.h>
39 #include "gdbmach.h"
40 
41 enum {
42 	POSIX_EINVAL = 0x1c,  /* used to report bad arguments to GDB */
43 	SIZEOF_PAYLOAD = 512, /* buffer size of GDB payload data */
44 };
45 
46 struct gdbstub {
47 	struct gdb_transport *trans;
48 	int exit_handler; /* leave interrupt handler */
49 
50 	int signo;
51 	gdbreg_t *regs;
52 
53 	void ( * parse ) ( struct gdbstub *stub, char ch );
54 	uint8_t cksum1;
55 
56 	/* Buffer for payload data when parsing a packet.  Once the
57 	 * packet has been received, this buffer is used to hold
58 	 * the reply payload. */
59 	char buf [ SIZEOF_PAYLOAD + 4 ]; /* $...PAYLOAD...#XX */
60 	char *payload;                   /* start of payload */
61 	int len;                         /* length of payload */
62 };
63 
64 /* Packet parser states */
65 static void gdbstub_state_new ( struct gdbstub *stub, char ch );
66 static void gdbstub_state_data ( struct gdbstub *stub, char ch );
67 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch );
68 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch );
69 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch );
70 
gdbstub_from_hex_digit(char ch)71 static uint8_t gdbstub_from_hex_digit ( char ch ) {
72 	return ( isdigit ( ch ) ? ch - '0' : tolower ( ch ) - 'a' + 0xa ) & 0xf;
73 }
74 
gdbstub_to_hex_digit(uint8_t b)75 static uint8_t gdbstub_to_hex_digit ( uint8_t b ) {
76 	b &= 0xf;
77 	return ( b < 0xa ? '0' : 'a' - 0xa ) + b;
78 }
79 
80 /*
81  * To make reading/writing device memory atomic, we check for
82  * 2- or 4-byte aligned operations and handle them specially.
83  */
84 
gdbstub_from_hex_buf(char * dst,char * src,int lenbytes)85 static void gdbstub_from_hex_buf ( char *dst, char *src, int lenbytes ) {
86 	if ( lenbytes == 2 && ( ( unsigned long ) dst & 0x1 ) == 0 ) {
87 		uint16_t i = gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
88 			gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
89 			gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
90 			gdbstub_from_hex_digit ( src [ 1 ] );
91 		* ( uint16_t * ) dst = cpu_to_le16 ( i );
92 	} else if ( lenbytes == 4 && ( ( unsigned long ) dst & 0x3 ) == 0 ) {
93 		uint32_t i = gdbstub_from_hex_digit ( src [ 6 ] ) << 28 |
94 			gdbstub_from_hex_digit ( src [ 7 ] ) << 24 |
95 			gdbstub_from_hex_digit ( src [ 4 ] ) << 20 |
96 			gdbstub_from_hex_digit ( src [ 5 ] ) << 16 |
97 			gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
98 			gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
99 			gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
100 			gdbstub_from_hex_digit ( src [ 1 ] );
101 		* ( uint32_t * ) dst = cpu_to_le32 ( i );
102 	} else {
103 		while ( lenbytes-- > 0 ) {
104 			*dst++ = gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
105 				gdbstub_from_hex_digit ( src [ 1 ] );
106 			src += 2;
107 		}
108 	}
109 }
110 
gdbstub_to_hex_buf(char * dst,char * src,int lenbytes)111 static void gdbstub_to_hex_buf ( char *dst, char *src, int lenbytes ) {
112 	if ( lenbytes == 2 && ( ( unsigned long ) src & 0x1 ) == 0 ) {
113 		uint16_t i = cpu_to_le16 ( * ( uint16_t * ) src );
114 		dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
115 		dst [ 1 ] = gdbstub_to_hex_digit ( i );
116 		dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
117 		dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
118 	} else if ( lenbytes == 4 && ( ( unsigned long ) src & 0x3 ) == 0 ) {
119 		uint32_t i = cpu_to_le32 ( * ( uint32_t * ) src );
120 		dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
121 		dst [ 1 ] = gdbstub_to_hex_digit ( i );
122 		dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
123 		dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
124 		dst [ 4 ] = gdbstub_to_hex_digit ( i >> 20 );
125 		dst [ 5 ] = gdbstub_to_hex_digit ( i >> 16);
126 		dst [ 6 ] = gdbstub_to_hex_digit ( i >> 28 );
127 		dst [ 7 ] = gdbstub_to_hex_digit ( i >> 24 );
128 	} else {
129 		while ( lenbytes-- > 0 ) {
130 			*dst++ = gdbstub_to_hex_digit ( *src >> 4 );
131 			*dst++ = gdbstub_to_hex_digit ( *src );
132 			src++;
133 		}
134 	}
135 }
136 
gdbstub_cksum(char * data,int len)137 static uint8_t gdbstub_cksum ( char *data, int len ) {
138 	uint8_t cksum = 0;
139 	while ( len-- > 0 ) {
140 		cksum += ( uint8_t ) *data++;
141 	}
142 	return cksum;
143 }
144 
gdbstub_tx_packet(struct gdbstub * stub)145 static void gdbstub_tx_packet ( struct gdbstub *stub ) {
146 	uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
147 	stub->buf [ 0 ] = '$';
148 	stub->buf [ stub->len + 1 ] = '#';
149 	stub->buf [ stub->len + 2 ] = gdbstub_to_hex_digit ( cksum >> 4 );
150 	stub->buf [ stub->len + 3 ] = gdbstub_to_hex_digit ( cksum );
151 	stub->trans->send ( stub->buf, stub->len + 4 );
152 	stub->parse = gdbstub_state_wait_ack;
153 }
154 
155 /* GDB commands */
gdbstub_send_ok(struct gdbstub * stub)156 static void gdbstub_send_ok ( struct gdbstub *stub ) {
157 	stub->payload [ 0 ] = 'O';
158 	stub->payload [ 1 ] = 'K';
159 	stub->len = 2;
160 	gdbstub_tx_packet ( stub );
161 }
162 
gdbstub_send_num_packet(struct gdbstub * stub,char reply,int num)163 static void gdbstub_send_num_packet ( struct gdbstub *stub, char reply, int num ) {
164 	stub->payload [ 0 ] = reply;
165 	stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
166 	stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
167 	stub->len = 3;
168 	gdbstub_tx_packet ( stub );
169 }
170 
171 /* Format is arg1,arg2,...,argn:data where argn are hex integers and data is not an argument */
gdbstub_get_packet_args(struct gdbstub * stub,unsigned long * args,int nargs,int * stop_idx)172 static int gdbstub_get_packet_args ( struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx ) {
173 	int i;
174 	char ch = 0;
175 	int argc = 0;
176 	unsigned long val = 0;
177 	for ( i = 1; i < stub->len && argc < nargs; i++ ) {
178 		ch = stub->payload [ i ];
179 		if ( ch == ':' ) {
180 			break;
181 		} else if ( ch == ',' ) {
182 			args [ argc++ ] = val;
183 			val = 0;
184 		} else {
185 			val = ( val << 4 ) | gdbstub_from_hex_digit ( ch );
186 		}
187 	}
188 	if ( stop_idx ) {
189 		*stop_idx = i;
190 	}
191 	if ( argc < nargs ) {
192 		args [ argc++ ] = val;
193 	}
194 	return ( ( i == stub->len || ch == ':' ) && argc == nargs );
195 }
196 
gdbstub_send_errno(struct gdbstub * stub,int errno)197 static void gdbstub_send_errno ( struct gdbstub *stub, int errno ) {
198 	gdbstub_send_num_packet ( stub, 'E', errno );
199 }
200 
gdbstub_report_signal(struct gdbstub * stub)201 static void gdbstub_report_signal ( struct gdbstub *stub ) {
202 	gdbstub_send_num_packet ( stub, 'S', stub->signo );
203 }
204 
gdbstub_read_regs(struct gdbstub * stub)205 static void gdbstub_read_regs ( struct gdbstub *stub ) {
206 	gdbstub_to_hex_buf ( stub->payload, ( char * ) stub->regs, GDBMACH_SIZEOF_REGS );
207 	stub->len = GDBMACH_SIZEOF_REGS * 2;
208 	gdbstub_tx_packet ( stub );
209 }
210 
gdbstub_write_regs(struct gdbstub * stub)211 static void gdbstub_write_regs ( struct gdbstub *stub ) {
212 	if ( stub->len != 1 + GDBMACH_SIZEOF_REGS * 2 ) {
213 		gdbstub_send_errno ( stub, POSIX_EINVAL );
214 		return;
215 	}
216 	gdbstub_from_hex_buf ( ( char * ) stub->regs, &stub->payload [ 1 ], GDBMACH_SIZEOF_REGS );
217 	gdbstub_send_ok ( stub );
218 }
219 
gdbstub_read_mem(struct gdbstub * stub)220 static void gdbstub_read_mem ( struct gdbstub *stub ) {
221 	unsigned long args [ 2 ];
222 	if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
223 		gdbstub_send_errno ( stub, POSIX_EINVAL );
224 		return;
225 	}
226 	args [ 1 ] = ( args [ 1 ] < SIZEOF_PAYLOAD / 2 ) ? args [ 1 ] : SIZEOF_PAYLOAD / 2;
227 	gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
228 	stub->len = args [ 1 ] * 2;
229 	gdbstub_tx_packet ( stub );
230 }
231 
gdbstub_write_mem(struct gdbstub * stub)232 static void gdbstub_write_mem ( struct gdbstub *stub ) {
233 	unsigned long args [ 2 ];
234 	int colon;
235 	if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
236 			colon >= stub->len || stub->payload [ colon ] != ':' ||
237 			( stub->len - colon - 1 ) % 2 != 0 ) {
238 		gdbstub_send_errno ( stub, POSIX_EINVAL );
239 		return;
240 	}
241 	gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], ( stub->len - colon - 1 ) / 2 );
242 	gdbstub_send_ok ( stub );
243 }
244 
gdbstub_continue(struct gdbstub * stub,int single_step)245 static void gdbstub_continue ( struct gdbstub *stub, int single_step ) {
246 	gdbreg_t pc;
247 	if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
248 		gdbmach_set_pc ( stub->regs, pc );
249 	}
250 	gdbmach_set_single_step ( stub->regs, single_step );
251 	stub->exit_handler = 1;
252 	/* Reply will be sent when we hit the next breakpoint or interrupt */
253 }
254 
gdbstub_breakpoint(struct gdbstub * stub)255 static void gdbstub_breakpoint ( struct gdbstub *stub ) {
256 	unsigned long args [ 3 ];
257 	int enable = stub->payload [ 0 ] == 'Z' ? 1 : 0;
258 	int rc;
259 
260 	if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
261 		gdbstub_send_errno ( stub, POSIX_EINVAL );
262 		return;
263 	}
264 	if ( ( rc = gdbmach_set_breakpoint ( args [ 0 ], args [ 1 ],
265 					     args [ 2 ], enable ) ) != 0 ) {
266 		/* Not supported */
267 		stub->len = 0;
268 		gdbstub_tx_packet ( stub );
269 		return;
270 	}
271 	gdbstub_send_ok ( stub );
272 }
273 
gdbstub_rx_packet(struct gdbstub * stub)274 static void gdbstub_rx_packet ( struct gdbstub *stub ) {
275 	switch ( stub->payload [ 0 ] ) {
276 		case '?':
277 			gdbstub_report_signal ( stub );
278 			break;
279 		case 'g':
280 			gdbstub_read_regs ( stub );
281 			break;
282 		case 'G':
283 			gdbstub_write_regs ( stub );
284 			break;
285 		case 'm':
286 			gdbstub_read_mem ( stub );
287 			break;
288 		case 'M':
289 			gdbstub_write_mem ( stub );
290 			break;
291 		case 'c': /* Continue */
292 		case 'k': /* Kill */
293 		case 's': /* Step */
294 		case 'D': /* Detach */
295 			gdbstub_continue ( stub, stub->payload [ 0 ] == 's' );
296 			if ( stub->payload [ 0 ] == 'D' ) {
297 				gdbstub_send_ok ( stub );
298 			}
299 			break;
300 		case 'Z': /* Insert breakpoint */
301 		case 'z': /* Remove breakpoint */
302 			gdbstub_breakpoint ( stub );
303 			break;
304 		default:
305 			stub->len = 0;
306 			gdbstub_tx_packet ( stub );
307 			break;
308 	}
309 }
310 
311 /* GDB packet parser */
gdbstub_state_new(struct gdbstub * stub,char ch)312 static void gdbstub_state_new ( struct gdbstub *stub, char ch ) {
313 	if ( ch == '$' ) {
314 		stub->len = 0;
315 		stub->parse = gdbstub_state_data;
316 	}
317 }
318 
gdbstub_state_data(struct gdbstub * stub,char ch)319 static void gdbstub_state_data ( struct gdbstub *stub, char ch ) {
320 	if ( ch == '#' ) {
321 		stub->parse = gdbstub_state_cksum1;
322 	} else if ( ch == '$' ) {
323 		stub->len = 0; /* retry new packet */
324 	} else {
325 		/* If the length exceeds our buffer, let the checksum fail */
326 		if ( stub->len < SIZEOF_PAYLOAD ) {
327 			stub->payload [ stub->len++ ] = ch;
328 		}
329 	}
330 }
331 
gdbstub_state_cksum1(struct gdbstub * stub,char ch)332 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch ) {
333 	stub->cksum1 = gdbstub_from_hex_digit ( ch ) << 4;
334 	stub->parse = gdbstub_state_cksum2;
335 }
336 
gdbstub_state_cksum2(struct gdbstub * stub,char ch)337 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch ) {
338 	uint8_t their_cksum;
339 	uint8_t our_cksum;
340 
341 	stub->parse = gdbstub_state_new;
342 	their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
343 	our_cksum = gdbstub_cksum ( stub->payload, stub->len );
344 	if ( their_cksum == our_cksum ) {
345 		stub->trans->send ( "+", 1 );
346 		if ( stub->len > 0 ) {
347 			gdbstub_rx_packet ( stub );
348 		}
349 	} else {
350 		stub->trans->send ( "-", 1 );
351 	}
352 }
353 
gdbstub_state_wait_ack(struct gdbstub * stub,char ch)354 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch ) {
355 	if ( ch == '+' ) {
356 		stub->parse = gdbstub_state_new;
357 	} else {
358 		/* This retransmit is very aggressive but necessary to keep
359 		 * in sync with GDB. */
360 		gdbstub_tx_packet ( stub );
361 	}
362 }
363 
gdbstub_parse(struct gdbstub * stub,char ch)364 static void gdbstub_parse ( struct gdbstub *stub, char ch ) {
365 	stub->parse ( stub, ch );
366 }
367 
368 static struct gdbstub stub = {
369 	.parse = gdbstub_state_new
370 };
371 
gdbstub_handler(int signo,gdbreg_t * regs)372 void gdbstub_handler ( int signo, gdbreg_t *regs ) {
373 	char packet [ SIZEOF_PAYLOAD + 4 ];
374 	size_t len, i;
375 
376 	/* A transport must be set up */
377 	if ( !stub.trans ) {
378 		return;
379 	}
380 
381 	stub.signo = signo;
382 	stub.regs = regs;
383 	stub.exit_handler = 0;
384 	gdbstub_report_signal ( &stub );
385 	while ( !stub.exit_handler && ( len = stub.trans->recv ( packet, sizeof ( packet ) ) ) > 0 ) {
386 		for ( i = 0; i < len; i++ ) {
387 			gdbstub_parse ( &stub, packet [ i ] );
388 		}
389 	}
390 }
391 
find_gdb_transport(const char * name)392 struct gdb_transport *find_gdb_transport ( const char *name ) {
393 	struct gdb_transport *trans;
394 
395 	for_each_table_entry ( trans, GDB_TRANSPORTS ) {
396 		if ( strcmp ( trans->name, name ) == 0 ) {
397 			return trans;
398 		}
399 	}
400 	return NULL;
401 }
402 
gdbstub_start(struct gdb_transport * trans)403 void gdbstub_start ( struct gdb_transport *trans ) {
404 	stub.trans = trans;
405 	stub.payload = &stub.buf [ 1 ];
406 	gdbmach_init();
407 	gdbmach_breakpoint();
408 }
409