Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / gdb / cstub.c
bloba5c0c4316545b76ab899a1ef96dfd1fb3299bb6d
1 /* cstub.c - machine independent portion of remote GDB stub */
2 /*
3 * Copyright (C) 2006 Lubomir Kundrak
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <grub/misc.h>
21 #include <grub/cpu/gdb.h>
22 #include <grub/gdb.h>
23 #include <grub/serial.h>
24 #include <grub/backtrace.h>
26 static const char hexchars[] = "0123456789abcdef";
27 int grub_gdb_regs[GRUB_MACHINE_NR_REGS];
29 #define GRUB_GDB_COMBUF_SIZE 400 /* At least sizeof(grub_gdb_regs)*2 are needed for
30 register packets. */
31 static char grub_gdb_inbuf[GRUB_GDB_COMBUF_SIZE + 1];
32 static char grub_gdb_outbuf[GRUB_GDB_COMBUF_SIZE + 1];
34 struct grub_serial_port *grub_gdb_port;
36 static int
37 hex (char ch)
39 if ((ch >= 'a') && (ch <= 'f'))
40 return (ch - 'a' + 10);
41 if ((ch >= '0') && (ch <= '9'))
42 return (ch - '0');
43 if ((ch >= 'A') && (ch <= 'F'))
44 return (ch - 'A' + 10);
45 return (-1);
48 /* Scan for the sequence $<data>#<checksum>. */
49 static char *
50 grub_gdb_getpacket (void)
52 char *buffer = &grub_gdb_inbuf[0];
53 unsigned char checksum;
54 unsigned char xmitcsum;
55 int count;
56 int ch;
58 while (1)
60 /* Wait around for the start character, ignore all other
61 characters. */
62 while ((ch = grub_serial_port_fetch (grub_gdb_port)) != '$');
64 retry:
65 checksum = 0;
66 xmitcsum = -1;
67 count = 0;
69 /* Now read until a # or end of buffer is found. */
70 while (count < GRUB_GDB_COMBUF_SIZE)
73 ch = grub_serial_port_fetch (grub_gdb_port);
74 while (ch < 0);
75 if (ch == '$')
76 goto retry;
77 if (ch == '#')
78 break;
79 checksum += ch;
80 buffer[count] = ch;
81 count = count + 1;
83 buffer[count] = 0;
84 if (ch == '#')
87 ch = grub_serial_port_fetch (grub_gdb_port);
88 while (ch < 0);
89 xmitcsum = hex (ch) << 4;
91 ch = grub_serial_port_fetch (grub_gdb_port);
92 while (ch < 0);
93 xmitcsum += hex (ch);
95 if (checksum != xmitcsum)
96 grub_serial_port_put (grub_gdb_port, '-'); /* Failed checksum. */
97 else
99 grub_serial_port_put (grub_gdb_port, '+'); /* Successful transfer. */
101 /* If a sequence char is present, reply the sequence ID. */
102 if (buffer[2] == ':')
104 grub_serial_port_put (grub_gdb_port, buffer[0]);
105 grub_serial_port_put (grub_gdb_port, buffer[1]);
107 return &buffer[3];
109 return &buffer[0];
115 /* Send the packet in buffer. */
116 static void
117 grub_gdb_putpacket (char *buffer)
119 grub_uint8_t checksum;
121 /* $<packet info>#<checksum>. */
124 char *ptr;
125 grub_serial_port_put (grub_gdb_port, '$');
126 checksum = 0;
128 for (ptr = buffer; *ptr; ptr++)
130 grub_serial_port_put (grub_gdb_port, *ptr);
131 checksum += *ptr;
134 grub_serial_port_put (grub_gdb_port, '#');
135 grub_serial_port_put (grub_gdb_port, hexchars[checksum >> 4]);
136 grub_serial_port_put (grub_gdb_port, hexchars[checksum & 0xf]);
138 while (grub_serial_port_fetch (grub_gdb_port) != '+');
141 /* Convert the memory pointed to by mem into hex, placing result in buf.
142 Return a pointer to the last char put in buf (NULL). */
143 static char *
144 grub_gdb_mem2hex (char *mem, char *buf, grub_size_t count)
146 grub_size_t i;
147 unsigned char ch;
149 for (i = 0; i < count; i++)
151 ch = *mem++;
152 *buf++ = hexchars[ch >> 4];
153 *buf++ = hexchars[ch % 16];
155 *buf = 0;
156 return (buf);
159 /* Convert the hex array pointed to by buf into binary to be placed in mem.
160 Return a pointer to the character after the last byte written. */
161 static char *
162 grub_gdb_hex2mem (char *buf, char *mem, int count)
164 int i;
165 unsigned char ch;
167 for (i = 0; i < count; i++)
169 ch = hex (*buf++) << 4;
170 ch = ch + hex (*buf++);
171 *mem++ = ch;
173 return (mem);
176 /* Convert hex characters to int and return the number of characters
177 processed. */
178 static int
179 grub_gdb_hex2int (char **ptr, grub_uint64_t *int_value)
181 int num_chars = 0;
182 int hex_value;
184 *int_value = 0;
186 while (**ptr)
188 hex_value = hex (**ptr);
189 if (hex_value >= 0)
191 *int_value = (*int_value << 4) | hex_value;
192 num_chars++;
194 else
195 break;
197 (*ptr)++;
200 return (num_chars);
203 /* This function does all command procesing for interfacing to gdb. */
204 void
205 grub_gdb_trap (int trap_no)
207 int sig_no;
208 int stepping;
209 grub_uint64_t addr;
210 grub_uint64_t length;
211 char *ptr;
213 if (!grub_gdb_port)
215 grub_printf ("Unhandled exception 0x%x at ", trap_no);
216 grub_backtrace_print_address ((void *) grub_gdb_regs[PC]);
217 grub_printf ("\n");
218 grub_backtrace_pointer ((void *) grub_gdb_regs[EBP]);
219 grub_abort ();
222 sig_no = grub_gdb_trap2sig (trap_no);
224 ptr = grub_gdb_outbuf;
226 /* Reply to host that an exception has occurred. */
228 *ptr++ = 'T'; /* Notify gdb with signo, PC, FP and SP. */
230 *ptr++ = hexchars[sig_no >> 4];
231 *ptr++ = hexchars[sig_no & 0xf];
233 /* Stack pointer. */
234 *ptr++ = hexchars[SP];
235 *ptr++ = ':';
236 ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[ESP], ptr, 4);
237 *ptr++ = ';';
239 /* Frame pointer. */
240 *ptr++ = hexchars[FP];
241 *ptr++ = ':';
242 ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[EBP], ptr, 4);
243 *ptr++ = ';';
245 /* Program counter. */
246 *ptr++ = hexchars[PC];
247 *ptr++ = ':';
248 ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[PC], ptr, 4);
249 *ptr++ = ';';
251 *ptr = '\0';
253 grub_gdb_putpacket (grub_gdb_outbuf);
255 stepping = 0;
257 while (1)
259 grub_gdb_outbuf[0] = 0;
260 ptr = grub_gdb_getpacket ();
262 switch (*ptr++)
264 case '?':
265 grub_gdb_outbuf[0] = 'S';
266 grub_gdb_outbuf[1] = hexchars[sig_no >> 4];
267 grub_gdb_outbuf[2] = hexchars[sig_no % 16];
268 grub_gdb_outbuf[3] = 0;
269 break;
271 /* Return values of the CPU registers. */
272 case 'g':
273 grub_gdb_mem2hex ((char *) grub_gdb_regs, grub_gdb_outbuf,
274 sizeof (grub_gdb_regs));
275 break;
277 /* Set values of the CPU registers -- return OK. */
278 case 'G':
279 grub_gdb_hex2mem (ptr, (char *) grub_gdb_regs,
280 sizeof (grub_gdb_regs));
281 grub_strcpy (grub_gdb_outbuf, "OK");
282 break;
284 /* Set the value of a single CPU register -- return OK. */
285 case 'P':
287 grub_uint64_t regno;
289 if (grub_gdb_hex2int (&ptr, &regno) && *ptr++ == '=')
290 if (regno < GRUB_MACHINE_NR_REGS)
292 grub_gdb_hex2mem (ptr, (char *) &grub_gdb_regs[regno], 4);
293 grub_strcpy (grub_gdb_outbuf, "OK");
294 break;
296 /* FIXME: GDB requires setting orig_eax. I don't know what's
297 this register is about. For now just simulate setting any
298 registers. */
299 grub_strcpy (grub_gdb_outbuf, /*"E01"*/ "OK");
300 break;
303 /* mAA..AA,LLLL: Read LLLL bytes at address AA..AA. */
304 case 'm':
305 /* Try to read %x,%x. Set ptr = 0 if successful. */
306 if (grub_gdb_hex2int (&ptr, &addr))
307 if (*(ptr++) == ',')
308 if (grub_gdb_hex2int (&ptr, &length))
310 ptr = 0;
311 grub_gdb_mem2hex ((char *) (grub_addr_t) addr,
312 grub_gdb_outbuf, length);
314 if (ptr)
315 grub_strcpy (grub_gdb_outbuf, "E01");
316 break;
318 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA -- return OK. */
319 case 'M':
320 /* Try to read %x,%x. Set ptr = 0 if successful. */
321 if (grub_gdb_hex2int (&ptr, &addr))
322 if (*(ptr++) == ',')
323 if (grub_gdb_hex2int (&ptr, &length))
324 if (*(ptr++) == ':')
326 grub_gdb_hex2mem (ptr, (char *) (grub_addr_t) addr, length);
327 grub_strcpy (grub_gdb_outbuf, "OK");
328 ptr = 0;
330 if (ptr)
332 grub_strcpy (grub_gdb_outbuf, "E02");
334 break;
336 /* sAA..AA: Step one instruction from AA..AA(optional). */
337 case 's':
338 stepping = 1;
340 /* cAA..AA: Continue at address AA..AA(optional). */
341 case 'c':
342 /* try to read optional parameter, pc unchanged if no parm */
343 if (grub_gdb_hex2int (&ptr, &addr))
344 grub_gdb_regs[PC] = addr;
346 /* Clear the trace bit. */
347 grub_gdb_regs[PS] &= 0xfffffeff;
349 /* Set the trace bit if we're stepping. */
350 if (stepping)
351 grub_gdb_regs[PS] |= 0x100;
353 return;
355 /* Kill the program. */
356 case 'k':
357 /* Do nothing. */
358 return;
361 /* Reply to the request. */
362 grub_gdb_putpacket (grub_gdb_outbuf);