| 1 | /* Remote debugging interface for MIPS remote debugging protocol. |
| 2 | Copyright 1993 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. Written by Ian Lance Taylor |
| 4 | <ian@cygnus.com>. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "inferior.h" |
| 24 | #include "bfd.h" |
| 25 | #include "symfile.h" |
| 26 | #include "wait.h" |
| 27 | #include "gdbcmd.h" |
| 28 | #include "gdbcore.h" |
| 29 | #include "serial.h" |
| 30 | #include "target.h" |
| 31 | #include "remote-utils.h" |
| 32 | |
| 33 | #include <signal.h> |
| 34 | #include <varargs.h> |
| 35 | \f |
| 36 | /* Prototypes for local functions. */ |
| 37 | |
| 38 | static int |
| 39 | mips_readchar PARAMS ((int timeout)); |
| 40 | |
| 41 | static int |
| 42 | mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage, int ch, |
| 43 | int timeout)); |
| 44 | |
| 45 | static int |
| 46 | mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage, int *pch, |
| 47 | int timeout)); |
| 48 | |
| 49 | static int mips_cksum PARAMS ((const unsigned char *hdr, |
| 50 | const unsigned char *data, |
| 51 | int len)); |
| 52 | |
| 53 | static void |
| 54 | mips_send_packet PARAMS ((const char *s, int get_ack)); |
| 55 | |
| 56 | static int mips_receive_packet PARAMS ((char *buff, int throw_error, |
| 57 | int timeout)); |
| 58 | |
| 59 | static int |
| 60 | mips_request PARAMS ((char cmd, unsigned int addr, unsigned int data, |
| 61 | int *perr, timeout)); |
| 62 | |
| 63 | static void |
| 64 | mips_initialize PARAMS ((void)); |
| 65 | |
| 66 | static void |
| 67 | mips_open PARAMS ((char *name, int from_tty)); |
| 68 | |
| 69 | static void |
| 70 | mips_close PARAMS ((int quitting)); |
| 71 | |
| 72 | static void |
| 73 | mips_detach PARAMS ((char *args, int from_tty)); |
| 74 | |
| 75 | static void mips_resume PARAMS ((int pid, int step, |
| 76 | enum target_signal siggnal)); |
| 77 | |
| 78 | static int |
| 79 | mips_wait PARAMS ((int pid, WAITTYPE *status)); |
| 80 | |
| 81 | static int |
| 82 | mips_map_regno PARAMS ((int regno)); |
| 83 | |
| 84 | static void |
| 85 | mips_fetch_registers PARAMS ((int regno)); |
| 86 | |
| 87 | static void |
| 88 | mips_prepare_to_store PARAMS ((void)); |
| 89 | |
| 90 | static void |
| 91 | mips_store_registers PARAMS ((int regno)); |
| 92 | |
| 93 | static int |
| 94 | mips_fetch_word PARAMS ((CORE_ADDR addr)); |
| 95 | |
| 96 | static void |
| 97 | mips_store_word PARAMS ((CORE_ADDR addr, int value)); |
| 98 | |
| 99 | static int |
| 100 | mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, |
| 101 | int write, struct target_ops *ignore)); |
| 102 | |
| 103 | static void |
| 104 | mips_files_info PARAMS ((struct target_ops *ignore)); |
| 105 | |
| 106 | static void |
| 107 | mips_load PARAMS ((char *args, int from_tty)); |
| 108 | |
| 109 | static void |
| 110 | mips_create_inferior PARAMS ((char *execfile, char *args, char **env)); |
| 111 | |
| 112 | static void |
| 113 | mips_mourn_inferior PARAMS ((void)); |
| 114 | |
| 115 | /* A forward declaration. */ |
| 116 | extern struct target_ops mips_ops; |
| 117 | \f |
| 118 | /* The MIPS remote debugging interface is built on top of a simple |
| 119 | packet protocol. Each packet is organized as follows: |
| 120 | |
| 121 | SYN The first character is always a SYN (ASCII 026, or ^V). SYN |
| 122 | may not appear anywhere else in the packet. Any time a SYN is |
| 123 | seen, a new packet should be assumed to have begun. |
| 124 | |
| 125 | TYPE_LEN |
| 126 | This byte contains the upper five bits of the logical length |
| 127 | of the data section, plus a single bit indicating whether this |
| 128 | is a data packet or an acknowledgement. The documentation |
| 129 | indicates that this bit is 1 for a data packet, but the actual |
| 130 | board uses 1 for an acknowledgement. The value of the byte is |
| 131 | 0x40 + (ack ? 0x20 : 0) + (len >> 6) |
| 132 | (we always have 0 <= len < 1024). Acknowledgement packets do |
| 133 | not carry data, and must have a data length of 0. |
| 134 | |
| 135 | LEN1 This byte contains the lower six bits of the logical length of |
| 136 | the data section. The value is |
| 137 | 0x40 + (len & 0x3f) |
| 138 | |
| 139 | SEQ This byte contains the six bit sequence number of the packet. |
| 140 | The value is |
| 141 | 0x40 + seq |
| 142 | An acknowlegment packet contains the sequence number of the |
| 143 | packet being acknowledged plus 1 module 64. Data packets are |
| 144 | transmitted in sequence. There may only be one outstanding |
| 145 | unacknowledged data packet at a time. The sequence numbers |
| 146 | are independent in each direction. If an acknowledgement for |
| 147 | the previous packet is received (i.e., an acknowledgement with |
| 148 | the sequence number of the packet just sent) the packet just |
| 149 | sent should be retransmitted. If no acknowledgement is |
| 150 | received within a timeout period, the packet should be |
| 151 | retransmitted. This has an unfortunate failure condition on a |
| 152 | high-latency line, as a delayed acknowledgement may lead to an |
| 153 | endless series of duplicate packets. |
| 154 | |
| 155 | DATA The actual data bytes follow. The following characters are |
| 156 | escaped inline with DLE (ASCII 020, or ^P): |
| 157 | SYN (026) DLE S |
| 158 | DLE (020) DLE D |
| 159 | ^C (003) DLE C |
| 160 | ^S (023) DLE s |
| 161 | ^Q (021) DLE q |
| 162 | The additional DLE characters are not counted in the logical |
| 163 | length stored in the TYPE_LEN and LEN1 bytes. |
| 164 | |
| 165 | CSUM1 |
| 166 | CSUM2 |
| 167 | CSUM3 |
| 168 | These bytes contain an 18 bit checksum of the complete |
| 169 | contents of the packet excluding the SEQ byte and the |
| 170 | CSUM[123] bytes. The checksum is simply the twos complement |
| 171 | addition of all the bytes treated as unsigned characters. The |
| 172 | values of the checksum bytes are: |
| 173 | CSUM1: 0x40 + ((cksum >> 12) & 0x3f) |
| 174 | CSUM2: 0x40 + ((cksum >> 6) & 0x3f) |
| 175 | CSUM3: 0x40 + (cksum & 0x3f) |
| 176 | |
| 177 | It happens that the MIPS remote debugging protocol always |
| 178 | communicates with ASCII strings. Because of this, this |
| 179 | implementation doesn't bother to handle the DLE quoting mechanism, |
| 180 | since it will never be required. */ |
| 181 | |
| 182 | /* The SYN character which starts each packet. */ |
| 183 | #define SYN '\026' |
| 184 | |
| 185 | /* The 0x40 used to offset each packet (this value ensures that all of |
| 186 | the header and trailer bytes, other than SYN, are printable ASCII |
| 187 | characters). */ |
| 188 | #define HDR_OFFSET 0x40 |
| 189 | |
| 190 | /* The indices of the bytes in the packet header. */ |
| 191 | #define HDR_INDX_SYN 0 |
| 192 | #define HDR_INDX_TYPE_LEN 1 |
| 193 | #define HDR_INDX_LEN1 2 |
| 194 | #define HDR_INDX_SEQ 3 |
| 195 | #define HDR_LENGTH 4 |
| 196 | |
| 197 | /* The data/ack bit in the TYPE_LEN header byte. */ |
| 198 | #define TYPE_LEN_DA_BIT 0x20 |
| 199 | #define TYPE_LEN_DATA 0 |
| 200 | #define TYPE_LEN_ACK TYPE_LEN_DA_BIT |
| 201 | |
| 202 | /* How to compute the header bytes. */ |
| 203 | #define HDR_SET_SYN(data, len, seq) (SYN) |
| 204 | #define HDR_SET_TYPE_LEN(data, len, seq) \ |
| 205 | (HDR_OFFSET \ |
| 206 | + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \ |
| 207 | + (((len) >> 6) & 0x1f)) |
| 208 | #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f)) |
| 209 | #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq)) |
| 210 | |
| 211 | /* Check that a header byte is reasonable. */ |
| 212 | #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET) |
| 213 | |
| 214 | /* Get data from the header. These macros evaluate their argument |
| 215 | multiple times. */ |
| 216 | #define HDR_IS_DATA(hdr) \ |
| 217 | (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA) |
| 218 | #define HDR_GET_LEN(hdr) \ |
| 219 | ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f))) |
| 220 | #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f) |
| 221 | |
| 222 | /* The maximum data length. */ |
| 223 | #define DATA_MAXLEN 1023 |
| 224 | |
| 225 | /* The trailer offset. */ |
| 226 | #define TRLR_OFFSET HDR_OFFSET |
| 227 | |
| 228 | /* The indices of the bytes in the packet trailer. */ |
| 229 | #define TRLR_INDX_CSUM1 0 |
| 230 | #define TRLR_INDX_CSUM2 1 |
| 231 | #define TRLR_INDX_CSUM3 2 |
| 232 | #define TRLR_LENGTH 3 |
| 233 | |
| 234 | /* How to compute the trailer bytes. */ |
| 235 | #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f)) |
| 236 | #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f)) |
| 237 | #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f)) |
| 238 | |
| 239 | /* Check that a trailer byte is reasonable. */ |
| 240 | #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET) |
| 241 | |
| 242 | /* Get data from the trailer. This evaluates its argument multiple |
| 243 | times. */ |
| 244 | #define TRLR_GET_CKSUM(trlr) \ |
| 245 | ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \ |
| 246 | + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \ |
| 247 | + ((trlr)[TRLR_INDX_CSUM3] & 0x3f)) |
| 248 | |
| 249 | /* The sequence number modulos. */ |
| 250 | #define SEQ_MODULOS (64) |
| 251 | |
| 252 | /* Set to 1 if the target is open. */ |
| 253 | static int mips_is_open; |
| 254 | |
| 255 | /* Set to 1 while the connection is being initialized. */ |
| 256 | static int mips_initializing; |
| 257 | |
| 258 | /* The next sequence number to send. */ |
| 259 | static int mips_send_seq; |
| 260 | |
| 261 | /* The next sequence number we expect to receive. */ |
| 262 | static int mips_receive_seq; |
| 263 | |
| 264 | /* The time to wait before retransmitting a packet, in seconds. */ |
| 265 | static int mips_retransmit_wait = 3; |
| 266 | |
| 267 | /* The number of times to try retransmitting a packet before giving up. */ |
| 268 | static int mips_send_retries = 10; |
| 269 | |
| 270 | /* The number of garbage characters to accept when looking for an |
| 271 | SYN for the next packet. */ |
| 272 | static int mips_syn_garbage = 1050; |
| 273 | |
| 274 | /* The time to wait for a packet, in seconds. */ |
| 275 | static int mips_receive_wait = 5; |
| 276 | |
| 277 | /* Set if we have sent a packet to the board but have not yet received |
| 278 | a reply. */ |
| 279 | static int mips_need_reply = 0; |
| 280 | |
| 281 | /* Handle used to access serial I/O stream. */ |
| 282 | static serial_t mips_desc; |
| 283 | |
| 284 | /* Handle low-level error that we can't recover from. Note that just |
| 285 | error()ing out from target_wait or some such low-level place will cause |
| 286 | all hell to break loose--the rest of GDB will tend to get left in an |
| 287 | inconsistent state. */ |
| 288 | |
| 289 | static void NORETURN |
| 290 | mips_error (va_alist) |
| 291 | va_dcl |
| 292 | { |
| 293 | va_list args; |
| 294 | char *string; |
| 295 | |
| 296 | va_start (args); |
| 297 | target_terminal_ours (); |
| 298 | wrap_here(""); /* Force out any buffered output */ |
| 299 | gdb_flush (gdb_stdout); |
| 300 | if (error_pre_print) |
| 301 | fprintf_filtered (gdb_stderr, error_pre_print); |
| 302 | string = va_arg (args, char *); |
| 303 | vfprintf_filtered (gdb_stderr, string, args); |
| 304 | fprintf_filtered (gdb_stderr, "\n"); |
| 305 | va_end (args); |
| 306 | |
| 307 | /* Clean up in such a way that mips_close won't try to talk to the |
| 308 | board (it almost surely won't work since we weren't able to talk to |
| 309 | it). */ |
| 310 | mips_is_open = 0; |
| 311 | SERIAL_CLOSE (mips_desc); |
| 312 | |
| 313 | printf_unfiltered ("Ending remote MIPS debugging.\n"); |
| 314 | target_mourn_inferior (); |
| 315 | |
| 316 | return_to_top_level (RETURN_ERROR); |
| 317 | } |
| 318 | |
| 319 | /* Read a character from the remote, aborting on error. Returns |
| 320 | SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR |
| 321 | returns). FIXME: If we see the string "<IDT>" from the board, then |
| 322 | we are debugging on the main console port, and we have somehow |
| 323 | dropped out of remote debugging mode. In this case, we |
| 324 | automatically go back in to remote debugging mode. This is a hack, |
| 325 | put in because I can't find any way for a program running on the |
| 326 | remote board to terminate without also ending remote debugging |
| 327 | mode. I assume users won't have any trouble with this; for one |
| 328 | thing, the IDT documentation generally assumes that the remote |
| 329 | debugging port is not the console port. This is, however, very |
| 330 | convenient for DejaGnu when you only have one connected serial |
| 331 | port. */ |
| 332 | |
| 333 | static int |
| 334 | mips_readchar (timeout) |
| 335 | int timeout; |
| 336 | { |
| 337 | int ch; |
| 338 | static int state = 0; |
| 339 | static char nextstate[5] = { '<', 'I', 'D', 'T', '>' }; |
| 340 | |
| 341 | ch = SERIAL_READCHAR (mips_desc, timeout); |
| 342 | if (ch == SERIAL_EOF) |
| 343 | mips_error ("End of file from remote"); |
| 344 | if (ch == SERIAL_ERROR) |
| 345 | mips_error ("Error reading from remote: %s", safe_strerror (errno)); |
| 346 | if (sr_get_debug () > 1) |
| 347 | { |
| 348 | if (ch != SERIAL_TIMEOUT) |
| 349 | printf_filtered ("Read '%c' %d 0x%x\n", ch, ch, ch); |
| 350 | else |
| 351 | printf_filtered ("Timed out in read\n"); |
| 352 | } |
| 353 | |
| 354 | /* If we have seen <IDT> and we either time out, or we see a @ |
| 355 | (which was echoed from a packet we sent), reset the board as |
| 356 | described above. The first character in a packet after the SYN |
| 357 | (which is not echoed) is always an @ unless the packet is more |
| 358 | than 64 characters long, which ours never are. */ |
| 359 | if ((ch == SERIAL_TIMEOUT || ch == '@') |
| 360 | && state == 5 |
| 361 | && ! mips_initializing) |
| 362 | { |
| 363 | if (sr_get_debug () > 0) |
| 364 | printf_filtered ("Reinitializing MIPS debugging mode\n"); |
| 365 | SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1); |
| 366 | sleep (1); |
| 367 | |
| 368 | mips_need_reply = 0; |
| 369 | mips_initialize (); |
| 370 | |
| 371 | state = 0; |
| 372 | |
| 373 | mips_error ("Remote board reset"); |
| 374 | } |
| 375 | |
| 376 | if (ch == nextstate[state]) |
| 377 | ++state; |
| 378 | else |
| 379 | state = 0; |
| 380 | |
| 381 | return ch; |
| 382 | } |
| 383 | |
| 384 | /* Get a packet header, putting the data in the supplied buffer. |
| 385 | PGARBAGE is a pointer to the number of garbage characters received |
| 386 | so far. CH is the last character received. Returns 0 for success, |
| 387 | or -1 for timeout. */ |
| 388 | |
| 389 | static int |
| 390 | mips_receive_header (hdr, pgarbage, ch, timeout) |
| 391 | unsigned char *hdr; |
| 392 | int *pgarbage; |
| 393 | int ch; |
| 394 | int timeout; |
| 395 | { |
| 396 | int i; |
| 397 | |
| 398 | while (1) |
| 399 | { |
| 400 | /* Wait for a SYN. mips_syn_garbage is intended to prevent |
| 401 | sitting here indefinitely if the board sends us one garbage |
| 402 | character per second. ch may already have a value from the |
| 403 | last time through the loop. */ |
| 404 | while (ch != SYN) |
| 405 | { |
| 406 | ch = mips_readchar (timeout); |
| 407 | if (ch == SERIAL_TIMEOUT) |
| 408 | return -1; |
| 409 | if (ch != SYN) |
| 410 | { |
| 411 | /* Printing the character here lets the user of gdb see |
| 412 | what the program is outputting, if the debugging is |
| 413 | being done on the console port. FIXME: Perhaps this |
| 414 | should be filtered? */ |
| 415 | if (! mips_initializing || sr_get_debug () > 0) |
| 416 | { |
| 417 | putchar_unfiltered (ch); |
| 418 | gdb_flush (gdb_stdout); |
| 419 | } |
| 420 | |
| 421 | ++*pgarbage; |
| 422 | if (*pgarbage > mips_syn_garbage) |
| 423 | mips_error ("Remote debugging protocol failure"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Get the packet header following the SYN. */ |
| 428 | for (i = 1; i < HDR_LENGTH; i++) |
| 429 | { |
| 430 | ch = mips_readchar (timeout); |
| 431 | if (ch == SERIAL_TIMEOUT) |
| 432 | return -1; |
| 433 | |
| 434 | /* Make sure this is a header byte. */ |
| 435 | if (ch == SYN || ! HDR_CHECK (ch)) |
| 436 | break; |
| 437 | |
| 438 | hdr[i] = ch; |
| 439 | } |
| 440 | |
| 441 | /* If we got the complete header, we can return. Otherwise we |
| 442 | loop around and keep looking for SYN. */ |
| 443 | if (i >= HDR_LENGTH) |
| 444 | return 0; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* Get a packet header, putting the data in the supplied buffer. |
| 449 | PGARBAGE is a pointer to the number of garbage characters received |
| 450 | so far. The last character read is returned in *PCH. Returns 0 |
| 451 | for success, -1 for timeout, -2 for error. */ |
| 452 | |
| 453 | static int |
| 454 | mips_receive_trailer (trlr, pgarbage, pch, timeout) |
| 455 | unsigned char *trlr; |
| 456 | int *pgarbage; |
| 457 | int *pch; |
| 458 | int timeout; |
| 459 | { |
| 460 | int i; |
| 461 | int ch; |
| 462 | |
| 463 | for (i = 0; i < TRLR_LENGTH; i++) |
| 464 | { |
| 465 | ch = mips_readchar (timeout); |
| 466 | *pch = ch; |
| 467 | if (ch == SERIAL_TIMEOUT) |
| 468 | return -1; |
| 469 | if (! TRLR_CHECK (ch)) |
| 470 | return -2; |
| 471 | trlr[i] = ch; |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /* Get the checksum of a packet. HDR points to the packet header. |
| 477 | DATA points to the packet data. LEN is the length of DATA. */ |
| 478 | |
| 479 | static int |
| 480 | mips_cksum (hdr, data, len) |
| 481 | const unsigned char *hdr; |
| 482 | const unsigned char *data; |
| 483 | int len; |
| 484 | { |
| 485 | register const unsigned char *p; |
| 486 | register int c; |
| 487 | register int cksum; |
| 488 | |
| 489 | cksum = 0; |
| 490 | |
| 491 | /* The initial SYN is not included in the checksum. */ |
| 492 | c = HDR_LENGTH - 1; |
| 493 | p = hdr + 1; |
| 494 | while (c-- != 0) |
| 495 | cksum += *p++; |
| 496 | |
| 497 | c = len; |
| 498 | p = data; |
| 499 | while (c-- != 0) |
| 500 | cksum += *p++; |
| 501 | |
| 502 | return cksum; |
| 503 | } |
| 504 | |
| 505 | /* Send a packet containing the given ASCII string. */ |
| 506 | |
| 507 | static void |
| 508 | mips_send_packet (s, get_ack) |
| 509 | const char *s; |
| 510 | int get_ack; |
| 511 | { |
| 512 | unsigned int len; |
| 513 | unsigned char *packet; |
| 514 | register int cksum; |
| 515 | int try; |
| 516 | |
| 517 | len = strlen (s); |
| 518 | if (len > DATA_MAXLEN) |
| 519 | mips_error ("MIPS protocol data packet too long: %s", s); |
| 520 | |
| 521 | packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1); |
| 522 | |
| 523 | packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq); |
| 524 | packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq); |
| 525 | packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq); |
| 526 | packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq); |
| 527 | |
| 528 | memcpy (packet + HDR_LENGTH, s, len); |
| 529 | |
| 530 | cksum = mips_cksum (packet, packet + HDR_LENGTH, len); |
| 531 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); |
| 532 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); |
| 533 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); |
| 534 | |
| 535 | /* Increment the sequence number. This will set mips_send_seq to |
| 536 | the sequence number we expect in the acknowledgement. */ |
| 537 | mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS; |
| 538 | |
| 539 | if (! get_ack) |
| 540 | return; |
| 541 | |
| 542 | /* We can only have one outstanding data packet, so we just wait for |
| 543 | the acknowledgement here. Keep retransmitting the packet until |
| 544 | we get one, or until we've tried too many times. */ |
| 545 | for (try = 0; try < mips_send_retries; try++) |
| 546 | { |
| 547 | int garbage; |
| 548 | int ch; |
| 549 | |
| 550 | if (sr_get_debug () > 0) |
| 551 | { |
| 552 | packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0'; |
| 553 | printf_filtered ("Writing \"%s\"\n", packet + 1); |
| 554 | } |
| 555 | |
| 556 | if (SERIAL_WRITE (mips_desc, packet, |
| 557 | HDR_LENGTH + len + TRLR_LENGTH) != 0) |
| 558 | mips_error ("write to target failed: %s", safe_strerror (errno)); |
| 559 | |
| 560 | garbage = 0; |
| 561 | ch = 0; |
| 562 | while (1) |
| 563 | { |
| 564 | unsigned char hdr[HDR_LENGTH + 1]; |
| 565 | unsigned char trlr[TRLR_LENGTH + 1]; |
| 566 | int err; |
| 567 | int seq; |
| 568 | |
| 569 | /* Get the packet header. If we time out, resend the data |
| 570 | packet. */ |
| 571 | err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait); |
| 572 | if (err != 0) |
| 573 | break; |
| 574 | |
| 575 | ch = 0; |
| 576 | |
| 577 | /* If we get a data packet, assume it is a duplicate and |
| 578 | ignore it. FIXME: If the acknowledgement is lost, this |
| 579 | data packet may be the packet the remote sends after the |
| 580 | acknowledgement. */ |
| 581 | if (HDR_IS_DATA (hdr)) |
| 582 | continue; |
| 583 | |
| 584 | /* If the length is not 0, this is a garbled packet. */ |
| 585 | if (HDR_GET_LEN (hdr) != 0) |
| 586 | continue; |
| 587 | |
| 588 | /* Get the packet trailer. */ |
| 589 | err = mips_receive_trailer (trlr, &garbage, &ch, |
| 590 | mips_retransmit_wait); |
| 591 | |
| 592 | /* If we timed out, resend the data packet. */ |
| 593 | if (err == -1) |
| 594 | break; |
| 595 | |
| 596 | /* If we got a bad character, reread the header. */ |
| 597 | if (err != 0) |
| 598 | continue; |
| 599 | |
| 600 | /* If the checksum does not match the trailer checksum, this |
| 601 | is a bad packet; ignore it. */ |
| 602 | if (mips_cksum (hdr, (unsigned char *) NULL, 0) |
| 603 | != TRLR_GET_CKSUM (trlr)) |
| 604 | continue; |
| 605 | |
| 606 | if (sr_get_debug () > 0) |
| 607 | { |
| 608 | hdr[HDR_LENGTH] = '\0'; |
| 609 | trlr[TRLR_LENGTH] = '\0'; |
| 610 | printf_filtered ("Got ack %d \"%s%s\"\n", |
| 611 | HDR_GET_SEQ (hdr), hdr + 1, trlr); |
| 612 | } |
| 613 | |
| 614 | /* If this ack is for the current packet, we're done. */ |
| 615 | seq = HDR_GET_SEQ (hdr); |
| 616 | if (seq == mips_send_seq) |
| 617 | return; |
| 618 | |
| 619 | /* If this ack is for the last packet, resend the current |
| 620 | packet. */ |
| 621 | if ((seq + 1) % SEQ_MODULOS == mips_send_seq) |
| 622 | break; |
| 623 | |
| 624 | /* Otherwise this is a bad ack; ignore it. Increment the |
| 625 | garbage count to ensure that we do not stay in this loop |
| 626 | forever. */ |
| 627 | ++garbage; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | mips_error ("Remote did not acknowledge packet"); |
| 632 | } |
| 633 | |
| 634 | /* Receive and acknowledge a packet, returning the data in BUFF (which |
| 635 | should be DATA_MAXLEN + 1 bytes). The protocol documentation |
| 636 | implies that only the sender retransmits packets, so this code just |
| 637 | waits silently for a packet. It returns the length of the received |
| 638 | packet. If THROW_ERROR is nonzero, call error() on errors. If not, |
| 639 | don't print an error message and return -1. */ |
| 640 | |
| 641 | static int |
| 642 | mips_receive_packet (buff, throw_error, timeout) |
| 643 | char *buff; |
| 644 | int throw_error; |
| 645 | int timeout; |
| 646 | { |
| 647 | int ch; |
| 648 | int garbage; |
| 649 | int len; |
| 650 | unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1]; |
| 651 | int cksum; |
| 652 | |
| 653 | ch = 0; |
| 654 | garbage = 0; |
| 655 | while (1) |
| 656 | { |
| 657 | unsigned char hdr[HDR_LENGTH]; |
| 658 | unsigned char trlr[TRLR_LENGTH]; |
| 659 | int i; |
| 660 | int err; |
| 661 | |
| 662 | if (mips_receive_header (hdr, &garbage, ch, timeout) != 0) |
| 663 | { |
| 664 | if (throw_error) |
| 665 | mips_error ("Timed out waiting for remote packet"); |
| 666 | else |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | ch = 0; |
| 671 | |
| 672 | /* An acknowledgement is probably a duplicate; ignore it. */ |
| 673 | if (! HDR_IS_DATA (hdr)) |
| 674 | { |
| 675 | if (sr_get_debug () > 0) |
| 676 | printf_filtered ("Ignoring unexpected ACK\n"); |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | /* If this is the wrong sequence number, ignore it. */ |
| 681 | if (HDR_GET_SEQ (hdr) != mips_receive_seq) |
| 682 | { |
| 683 | if (sr_get_debug () > 0) |
| 684 | printf_filtered ("Ignoring sequence number %d (want %d)\n", |
| 685 | HDR_GET_SEQ (hdr), mips_receive_seq); |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | len = HDR_GET_LEN (hdr); |
| 690 | |
| 691 | for (i = 0; i < len; i++) |
| 692 | { |
| 693 | int rch; |
| 694 | |
| 695 | rch = mips_readchar (timeout); |
| 696 | if (rch == SYN) |
| 697 | { |
| 698 | ch = SYN; |
| 699 | break; |
| 700 | } |
| 701 | if (rch == SERIAL_TIMEOUT) |
| 702 | { |
| 703 | if (throw_error) |
| 704 | mips_error ("Timed out waiting for remote packet"); |
| 705 | else |
| 706 | return -1; |
| 707 | } |
| 708 | buff[i] = rch; |
| 709 | } |
| 710 | |
| 711 | if (i < len) |
| 712 | { |
| 713 | if (sr_get_debug () > 0) |
| 714 | printf_filtered ("Got new SYN after %d chars (wanted %d)\n", |
| 715 | i, len); |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | err = mips_receive_trailer (trlr, &garbage, &ch, timeout); |
| 720 | if (err == -1) |
| 721 | { |
| 722 | if (throw_error) |
| 723 | mips_error ("Timed out waiting for packet"); |
| 724 | else |
| 725 | return -1; |
| 726 | } |
| 727 | if (err == -2) |
| 728 | { |
| 729 | if (sr_get_debug () > 0) |
| 730 | printf_filtered ("Got SYN when wanted trailer\n"); |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr)) |
| 735 | break; |
| 736 | |
| 737 | if (sr_get_debug () > 0) |
| 738 | printf_filtered ("Bad checksum; data %d, trailer %d\n", |
| 739 | mips_cksum (hdr, buff, len), |
| 740 | TRLR_GET_CKSUM (trlr)); |
| 741 | |
| 742 | /* The checksum failed. Send an acknowledgement for the |
| 743 | previous packet to tell the remote to resend the packet. */ |
| 744 | ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq); |
| 745 | ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq); |
| 746 | ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq); |
| 747 | ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq); |
| 748 | |
| 749 | cksum = mips_cksum (ack, (unsigned char *) NULL, 0); |
| 750 | |
| 751 | ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); |
| 752 | ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); |
| 753 | ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); |
| 754 | |
| 755 | if (sr_get_debug () > 0) |
| 756 | { |
| 757 | ack[HDR_LENGTH + TRLR_LENGTH] = '\0'; |
| 758 | printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq, |
| 759 | ack + 1); |
| 760 | } |
| 761 | |
| 762 | if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0) |
| 763 | { |
| 764 | if (throw_error) |
| 765 | mips_error ("write to target failed: %s", safe_strerror (errno)); |
| 766 | else |
| 767 | return -1; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (sr_get_debug () > 0) |
| 772 | { |
| 773 | buff[len] = '\0'; |
| 774 | printf_filtered ("Got packet \"%s\"\n", buff); |
| 775 | } |
| 776 | |
| 777 | /* We got the packet. Send an acknowledgement. */ |
| 778 | mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS; |
| 779 | |
| 780 | ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq); |
| 781 | ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq); |
| 782 | ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq); |
| 783 | ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq); |
| 784 | |
| 785 | cksum = mips_cksum (ack, (unsigned char *) NULL, 0); |
| 786 | |
| 787 | ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); |
| 788 | ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); |
| 789 | ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); |
| 790 | |
| 791 | if (sr_get_debug () > 0) |
| 792 | { |
| 793 | ack[HDR_LENGTH + TRLR_LENGTH] = '\0'; |
| 794 | printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq, |
| 795 | ack + 1); |
| 796 | } |
| 797 | |
| 798 | if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0) |
| 799 | { |
| 800 | if (throw_error) |
| 801 | mips_error ("write to target failed: %s", safe_strerror (errno)); |
| 802 | else |
| 803 | return -1; |
| 804 | } |
| 805 | |
| 806 | return len; |
| 807 | } |
| 808 | \f |
| 809 | /* Optionally send a request to the remote system and optionally wait |
| 810 | for the reply. This implements the remote debugging protocol, |
| 811 | which is built on top of the packet protocol defined above. Each |
| 812 | request has an ADDR argument and a DATA argument. The following |
| 813 | requests are defined: |
| 814 | |
| 815 | \0 don't send a request; just wait for a reply |
| 816 | i read word from instruction space at ADDR |
| 817 | d read word from data space at ADDR |
| 818 | I write DATA to instruction space at ADDR |
| 819 | D write DATA to data space at ADDR |
| 820 | r read register number ADDR |
| 821 | R set register number ADDR to value DATA |
| 822 | c continue execution (if ADDR != 1, set pc to ADDR) |
| 823 | s single step (if ADDR != 1, set pc to ADDR) |
| 824 | |
| 825 | The read requests return the value requested. The write requests |
| 826 | return the previous value in the changed location. The execution |
| 827 | requests return a UNIX wait value (the approximate signal which |
| 828 | caused execution to stop is in the upper eight bits). |
| 829 | |
| 830 | If PERR is not NULL, this function waits for a reply. If an error |
| 831 | occurs, it sets *PERR to 1 and sets errno according to what the |
| 832 | target board reports. */ |
| 833 | |
| 834 | static int |
| 835 | mips_request (cmd, addr, data, perr, timeout) |
| 836 | char cmd; |
| 837 | unsigned int addr; |
| 838 | unsigned int data; |
| 839 | int *perr; |
| 840 | int timeout; |
| 841 | { |
| 842 | char buff[DATA_MAXLEN + 1]; |
| 843 | int len; |
| 844 | int rpid; |
| 845 | char rcmd; |
| 846 | int rerrflg; |
| 847 | int rresponse; |
| 848 | |
| 849 | if (cmd != '\0') |
| 850 | { |
| 851 | if (mips_need_reply) |
| 852 | fatal ("mips_request: Trying to send command before reply"); |
| 853 | sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data); |
| 854 | mips_send_packet (buff, 1); |
| 855 | mips_need_reply = 1; |
| 856 | } |
| 857 | |
| 858 | if (perr == (int *) NULL) |
| 859 | return 0; |
| 860 | |
| 861 | if (! mips_need_reply) |
| 862 | fatal ("mips_request: Trying to get reply before command"); |
| 863 | |
| 864 | mips_need_reply = 0; |
| 865 | |
| 866 | len = mips_receive_packet (buff, 1, timeout); |
| 867 | buff[len] = '\0'; |
| 868 | |
| 869 | if (sscanf (buff, "0x%x %c 0x%x 0x%x", |
| 870 | &rpid, &rcmd, &rerrflg, &rresponse) != 4 |
| 871 | || (cmd != '\0' && rcmd != cmd)) |
| 872 | mips_error ("Bad response from remote board"); |
| 873 | |
| 874 | if (rerrflg != 0) |
| 875 | { |
| 876 | *perr = 1; |
| 877 | |
| 878 | /* FIXME: This will returns MIPS errno numbers, which may or may |
| 879 | not be the same as errno values used on other systems. If |
| 880 | they stick to common errno values, they will be the same, but |
| 881 | if they don't, they must be translated. */ |
| 882 | errno = rresponse; |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | *perr = 0; |
| 888 | return rresponse; |
| 889 | } |
| 890 | |
| 891 | /* Initialize a new connection to the MIPS board, and make sure we are |
| 892 | really connected. */ |
| 893 | |
| 894 | static void |
| 895 | mips_initialize () |
| 896 | { |
| 897 | char cr; |
| 898 | char buff[DATA_MAXLEN + 1]; |
| 899 | int err; |
| 900 | |
| 901 | if (mips_initializing) |
| 902 | return; |
| 903 | |
| 904 | mips_initializing = 1; |
| 905 | |
| 906 | mips_send_seq = 0; |
| 907 | mips_receive_seq = 0; |
| 908 | |
| 909 | /* The board seems to want to send us a packet. I don't know what |
| 910 | it means. The packet seems to be triggered by a carriage return |
| 911 | character, although perhaps any character would do. */ |
| 912 | cr = '\r'; |
| 913 | SERIAL_WRITE (mips_desc, &cr, 1); |
| 914 | |
| 915 | if (mips_receive_packet (buff, 0, 3) < 0) |
| 916 | { |
| 917 | char cc; |
| 918 | |
| 919 | /* We did not receive the packet we expected; try resetting the |
| 920 | board and trying again. */ |
| 921 | printf_filtered ("Failed to initialize; trying to reset board\n"); |
| 922 | cc = '\003'; |
| 923 | SERIAL_WRITE (mips_desc, &cc, 1); |
| 924 | sleep (2); |
| 925 | SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1); |
| 926 | sleep (1); |
| 927 | cr = '\r'; |
| 928 | SERIAL_WRITE (mips_desc, &cr, 1); |
| 929 | } |
| 930 | mips_receive_packet (buff, 1, 3); |
| 931 | |
| 932 | mips_initializing = 0; |
| 933 | |
| 934 | /* If this doesn't call error, we have connected; we don't care if |
| 935 | the request itself succeeds or fails. */ |
| 936 | mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err, |
| 937 | mips_receive_wait); |
| 938 | } |
| 939 | |
| 940 | /* Open a connection to the remote board. */ |
| 941 | |
| 942 | static void |
| 943 | mips_open (name, from_tty) |
| 944 | char *name; |
| 945 | int from_tty; |
| 946 | { |
| 947 | if (name == 0) |
| 948 | error ( |
| 949 | "To open a MIPS remote debugging connection, you need to specify what serial\n\ |
| 950 | device is attached to the target board (e.g., /dev/ttya)."); |
| 951 | |
| 952 | target_preopen (from_tty); |
| 953 | |
| 954 | if (mips_is_open) |
| 955 | unpush_target (&mips_ops); |
| 956 | |
| 957 | mips_desc = SERIAL_OPEN (name); |
| 958 | if (mips_desc == (serial_t) NULL) |
| 959 | perror_with_name (name); |
| 960 | |
| 961 | SERIAL_RAW (mips_desc); |
| 962 | |
| 963 | mips_is_open = 1; |
| 964 | |
| 965 | mips_initialize (); |
| 966 | |
| 967 | if (from_tty) |
| 968 | printf_unfiltered ("Remote MIPS debugging using %s\n", name); |
| 969 | push_target (&mips_ops); /* Switch to using remote target now */ |
| 970 | |
| 971 | /* FIXME: Should we call start_remote here? */ |
| 972 | } |
| 973 | |
| 974 | /* Close a connection to the remote board. */ |
| 975 | |
| 976 | static void |
| 977 | mips_close (quitting) |
| 978 | int quitting; |
| 979 | { |
| 980 | if (mips_is_open) |
| 981 | { |
| 982 | int err; |
| 983 | |
| 984 | mips_is_open = 0; |
| 985 | |
| 986 | /* Get the board out of remote debugging mode. */ |
| 987 | mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err, |
| 988 | mips_receive_wait); |
| 989 | |
| 990 | SERIAL_CLOSE (mips_desc); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /* Detach from the remote board. */ |
| 995 | |
| 996 | static void |
| 997 | mips_detach (args, from_tty) |
| 998 | char *args; |
| 999 | int from_tty; |
| 1000 | { |
| 1001 | if (args) |
| 1002 | error ("Argument given to \"detach\" when remotely debugging."); |
| 1003 | |
| 1004 | pop_target (); |
| 1005 | if (from_tty) |
| 1006 | printf_unfiltered ("Ending remote MIPS debugging.\n"); |
| 1007 | } |
| 1008 | |
| 1009 | /* Tell the target board to resume. This does not wait for a reply |
| 1010 | from the board. */ |
| 1011 | |
| 1012 | static void |
| 1013 | mips_resume (pid, step, siggnal) |
| 1014 | int pid, step; |
| 1015 | enum target_signal siggnal; |
| 1016 | { |
| 1017 | if (siggnal != TARGET_SIGNAL_0) |
| 1018 | warning |
| 1019 | ("Can't send signals to a remote system. Try `handle %s ignore'.", |
| 1020 | target_signal_to_name (siggnal)); |
| 1021 | |
| 1022 | mips_request (step ? 's' : 'c', |
| 1023 | (unsigned int) 1, |
| 1024 | (unsigned int) 0, |
| 1025 | (int *) NULL, |
| 1026 | mips_receive_wait); |
| 1027 | } |
| 1028 | |
| 1029 | /* Wait until the remote stops, and return a wait status. */ |
| 1030 | |
| 1031 | static int |
| 1032 | mips_wait (pid, status) |
| 1033 | int pid; |
| 1034 | struct target_waitstatus *status; |
| 1035 | { |
| 1036 | int rstatus; |
| 1037 | int err; |
| 1038 | |
| 1039 | /* If we have not sent a single step or continue command, then the |
| 1040 | board is waiting for us to do something. Return a status |
| 1041 | indicating that it is stopped. */ |
| 1042 | if (! mips_need_reply) |
| 1043 | { |
| 1044 | status->kind = TARGET_WAITKIND_STOPPED; |
| 1045 | status->value.sig = TARGET_SIGNAL_TRAP; |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | /* No timeout; we sit here as long as the program continues to execute. */ |
| 1050 | rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err, -1); |
| 1051 | if (err) |
| 1052 | mips_error ("Remote failure: %s", safe_strerror (errno)); |
| 1053 | |
| 1054 | /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG |
| 1055 | and so on, because the constants we want here are determined by the |
| 1056 | MIPS protocol and have nothing to do with what host we are running on. */ |
| 1057 | if ((rstatus & 0x377) == 0) |
| 1058 | { |
| 1059 | status->kind = TARGET_WAITKIND_EXITED; |
| 1060 | status->value.integer = (((rstatus) >> 8) & 0377); |
| 1061 | } |
| 1062 | else if ((rstatus & 0x377) == 0x177) |
| 1063 | { |
| 1064 | status->kind = TARGET_WAIT_KIND_STOPPED; |
| 1065 | /* Don't want to use target_signal_from_host because we are converting |
| 1066 | from MIPS signal numbers, not host ones. Our internal numbers |
| 1067 | match the MIPS numbers for the signals the board can return, which |
| 1068 | are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */ |
| 1069 | status->value.sig = (enum target_signal) (((rstatus) >> 8) & 0377); |
| 1070 | } |
| 1071 | else |
| 1072 | { |
| 1073 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 1074 | /* Don't want to use target_signal_from_host because we are converting |
| 1075 | from MIPS signal numbers, not host ones. Our internal numbers |
| 1076 | match the MIPS numbers for the signals the board can return, which |
| 1077 | are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */ |
| 1078 | status->value.sig = (enum target_signal) (rstatus & 0x177); |
| 1079 | } |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | /* We have to map between the register numbers used by gdb and the |
| 1085 | register numbers used by the debugging protocol. This function |
| 1086 | assumes that we are using tm-mips.h. */ |
| 1087 | |
| 1088 | #define REGNO_OFFSET 96 |
| 1089 | |
| 1090 | static int |
| 1091 | mips_map_regno (regno) |
| 1092 | int regno; |
| 1093 | { |
| 1094 | if (regno < 32) |
| 1095 | return regno; |
| 1096 | if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32) |
| 1097 | return regno - FP0_REGNUM + 32; |
| 1098 | switch (regno) |
| 1099 | { |
| 1100 | case PC_REGNUM: |
| 1101 | return REGNO_OFFSET + 0; |
| 1102 | case CAUSE_REGNUM: |
| 1103 | return REGNO_OFFSET + 1; |
| 1104 | case HI_REGNUM: |
| 1105 | return REGNO_OFFSET + 2; |
| 1106 | case LO_REGNUM: |
| 1107 | return REGNO_OFFSET + 3; |
| 1108 | case FCRCS_REGNUM: |
| 1109 | return REGNO_OFFSET + 4; |
| 1110 | case FCRIR_REGNUM: |
| 1111 | return REGNO_OFFSET + 5; |
| 1112 | default: |
| 1113 | /* FIXME: Is there a way to get the status register? */ |
| 1114 | return 0; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /* Fetch the remote registers. */ |
| 1119 | |
| 1120 | static void |
| 1121 | mips_fetch_registers (regno) |
| 1122 | int regno; |
| 1123 | { |
| 1124 | unsigned LONGEST val; |
| 1125 | int err; |
| 1126 | |
| 1127 | if (regno == -1) |
| 1128 | { |
| 1129 | for (regno = 0; regno < NUM_REGS; regno++) |
| 1130 | mips_fetch_registers (regno); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | val = mips_request ('r', (unsigned int) mips_map_regno (regno), |
| 1135 | (unsigned int) 0, &err, mips_receive_wait); |
| 1136 | if (err) |
| 1137 | mips_error ("Can't read register %d: %s", regno, safe_strerror (errno)); |
| 1138 | |
| 1139 | { |
| 1140 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 1141 | |
| 1142 | /* We got the number the register holds, but gdb expects to see a |
| 1143 | value in the target byte ordering. */ |
| 1144 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val); |
| 1145 | supply_register (regno, buf); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /* Prepare to store registers. The MIPS protocol can store individual |
| 1150 | registers, so this function doesn't have to do anything. */ |
| 1151 | |
| 1152 | static void |
| 1153 | mips_prepare_to_store () |
| 1154 | { |
| 1155 | } |
| 1156 | |
| 1157 | /* Store remote register(s). */ |
| 1158 | |
| 1159 | static void |
| 1160 | mips_store_registers (regno) |
| 1161 | int regno; |
| 1162 | { |
| 1163 | int err; |
| 1164 | |
| 1165 | if (regno == -1) |
| 1166 | { |
| 1167 | for (regno = 0; regno < NUM_REGS; regno++) |
| 1168 | mips_store_registers (regno); |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | mips_request ('R', (unsigned int) mips_map_regno (regno), |
| 1173 | (unsigned int) read_register (regno), |
| 1174 | &err, mips_receive_wait); |
| 1175 | if (err) |
| 1176 | mips_error ("Can't write register %d: %s", regno, safe_strerror (errno)); |
| 1177 | } |
| 1178 | |
| 1179 | /* Fetch a word from the target board. */ |
| 1180 | |
| 1181 | static int |
| 1182 | mips_fetch_word (addr) |
| 1183 | CORE_ADDR addr; |
| 1184 | { |
| 1185 | int val; |
| 1186 | int err; |
| 1187 | |
| 1188 | val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err, |
| 1189 | mips_receive_wait); |
| 1190 | if (err) |
| 1191 | { |
| 1192 | /* Data space failed; try instruction space. */ |
| 1193 | val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err, |
| 1194 | mips_receive_wait); |
| 1195 | if (err) |
| 1196 | mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno)); |
| 1197 | } |
| 1198 | return val; |
| 1199 | } |
| 1200 | |
| 1201 | /* Store a word to the target board. */ |
| 1202 | |
| 1203 | static void |
| 1204 | mips_store_word (addr, val) |
| 1205 | CORE_ADDR addr; |
| 1206 | int val; |
| 1207 | { |
| 1208 | int err; |
| 1209 | |
| 1210 | mips_request ('D', (unsigned int) addr, (unsigned int) val, &err, |
| 1211 | mips_receive_wait); |
| 1212 | if (err) |
| 1213 | { |
| 1214 | /* Data space failed; try instruction space. */ |
| 1215 | mips_request ('I', (unsigned int) addr, (unsigned int) val, &err, |
| 1216 | mips_receive_wait); |
| 1217 | if (err) |
| 1218 | mips_error ("Can't write address 0x%x: %s", addr, safe_strerror (errno)); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* Read or write LEN bytes from inferior memory at MEMADDR, |
| 1223 | transferring to or from debugger address MYADDR. Write to inferior |
| 1224 | if SHOULD_WRITE is nonzero. Returns length of data written or |
| 1225 | read; 0 for error. Note that protocol gives us the correct value |
| 1226 | for a longword, since it transfers values in ASCII. We want the |
| 1227 | byte values, so we have to swap the longword values. */ |
| 1228 | |
| 1229 | static int |
| 1230 | mips_xfer_memory (memaddr, myaddr, len, write, ignore) |
| 1231 | CORE_ADDR memaddr; |
| 1232 | char *myaddr; |
| 1233 | int len; |
| 1234 | int write; |
| 1235 | struct target_ops *ignore; |
| 1236 | { |
| 1237 | register int i; |
| 1238 | /* Round starting address down to longword boundary. */ |
| 1239 | register CORE_ADDR addr = memaddr &~ 3; |
| 1240 | /* Round ending address up; get number of longwords that makes. */ |
| 1241 | register int count = (((memaddr + len) - addr) + 3) / 4; |
| 1242 | /* Allocate buffer of that many longwords. */ |
| 1243 | register char *buffer = alloca (count * 4); |
| 1244 | |
| 1245 | if (write) |
| 1246 | { |
| 1247 | /* Fill start and end extra bytes of buffer with existing data. */ |
| 1248 | if (addr != memaddr || len < 4) |
| 1249 | { |
| 1250 | /* Need part of initial word -- fetch it. */ |
| 1251 | store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr)); |
| 1252 | } |
| 1253 | |
| 1254 | if (count > 1) |
| 1255 | { |
| 1256 | /* Need part of last word -- fetch it. FIXME: we do this even |
| 1257 | if we don't need it. */ |
| 1258 | store_unsigned_integer (&buffer[(count - 1) * 4], 4, |
| 1259 | mips_fetch_word (addr + (count - 1) * 4)); |
| 1260 | } |
| 1261 | |
| 1262 | /* Copy data to be written over corresponding part of buffer */ |
| 1263 | |
| 1264 | memcpy ((char *) buffer + (memaddr & 3), myaddr, len); |
| 1265 | |
| 1266 | /* Write the entire buffer. */ |
| 1267 | |
| 1268 | for (i = 0; i < count; i++, addr += 4) |
| 1269 | { |
| 1270 | mips_store_word (addr, extract_unsigned_integer (&buffer[i*4], 4)); |
| 1271 | /* FIXME: Do we want a QUIT here? */ |
| 1272 | } |
| 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | /* Read all the longwords */ |
| 1277 | for (i = 0; i < count; i++, addr += 4) |
| 1278 | { |
| 1279 | store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr)); |
| 1280 | QUIT; |
| 1281 | } |
| 1282 | |
| 1283 | /* Copy appropriate bytes out of the buffer. */ |
| 1284 | memcpy (myaddr, buffer + (memaddr & 3), len); |
| 1285 | } |
| 1286 | return len; |
| 1287 | } |
| 1288 | |
| 1289 | /* Print info on this target. */ |
| 1290 | |
| 1291 | static void |
| 1292 | mips_files_info (ignore) |
| 1293 | struct target_ops *ignore; |
| 1294 | { |
| 1295 | printf_unfiltered ("Debugging a MIPS board over a serial line.\n"); |
| 1296 | } |
| 1297 | |
| 1298 | /* Kill the process running on the board. This will actually only |
| 1299 | work if we are doing remote debugging over the console input. I |
| 1300 | think that if IDT/sim had the remote debug interrupt enabled on the |
| 1301 | right port, we could interrupt the process with a break signal. */ |
| 1302 | |
| 1303 | static void |
| 1304 | mips_kill () |
| 1305 | { |
| 1306 | #if 0 |
| 1307 | if (mips_is_open) |
| 1308 | { |
| 1309 | char cc; |
| 1310 | |
| 1311 | /* Send a ^C. */ |
| 1312 | cc = '\003'; |
| 1313 | SERIAL_WRITE (mips_desc, &cc, 1); |
| 1314 | sleep (1); |
| 1315 | target_mourn_inferior (); |
| 1316 | } |
| 1317 | #endif |
| 1318 | } |
| 1319 | |
| 1320 | /* Start running on the target board. */ |
| 1321 | |
| 1322 | static void |
| 1323 | mips_create_inferior (execfile, args, env) |
| 1324 | char *execfile; |
| 1325 | char *args; |
| 1326 | char **env; |
| 1327 | { |
| 1328 | CORE_ADDR entry_pt; |
| 1329 | |
| 1330 | if (args && *args) |
| 1331 | mips_error ("Can't pass arguments to remote MIPS board."); |
| 1332 | |
| 1333 | if (execfile == 0 || exec_bfd == 0) |
| 1334 | mips_error ("No exec file specified"); |
| 1335 | |
| 1336 | entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd); |
| 1337 | |
| 1338 | init_wait_for_inferior (); |
| 1339 | |
| 1340 | /* FIXME: Should we set inferior_pid here? */ |
| 1341 | |
| 1342 | proceed (entry_pt, -1, 0); |
| 1343 | } |
| 1344 | |
| 1345 | /* Clean up after a process. Actually nothing to do. */ |
| 1346 | |
| 1347 | static void |
| 1348 | mips_mourn_inferior () |
| 1349 | { |
| 1350 | unpush_target (&mips_ops); |
| 1351 | generic_mourn_inferior (); |
| 1352 | } |
| 1353 | \f |
| 1354 | /* The target vector. */ |
| 1355 | |
| 1356 | struct target_ops mips_ops = |
| 1357 | { |
| 1358 | "mips", /* to_shortname */ |
| 1359 | "Remote MIPS debugging over serial line", /* to_longname */ |
| 1360 | "Debug a board using the MIPS remote debugging protocol over a serial line.\n\ |
| 1361 | Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */ |
| 1362 | mips_open, /* to_open */ |
| 1363 | mips_close, /* to_close */ |
| 1364 | NULL, /* to_attach */ |
| 1365 | mips_detach, /* to_detach */ |
| 1366 | mips_resume, /* to_resume */ |
| 1367 | mips_wait, /* to_wait */ |
| 1368 | mips_fetch_registers, /* to_fetch_registers */ |
| 1369 | mips_store_registers, /* to_store_registers */ |
| 1370 | mips_prepare_to_store, /* to_prepare_to_store */ |
| 1371 | mips_xfer_memory, /* to_xfer_memory */ |
| 1372 | mips_files_info, /* to_files_info */ |
| 1373 | NULL, /* to_insert_breakpoint */ |
| 1374 | NULL, /* to_remove_breakpoint */ |
| 1375 | NULL, /* to_terminal_init */ |
| 1376 | NULL, /* to_terminal_inferior */ |
| 1377 | NULL, /* to_terminal_ours_for_output */ |
| 1378 | NULL, /* to_terminal_ours */ |
| 1379 | NULL, /* to_terminal_info */ |
| 1380 | mips_kill, /* to_kill */ |
| 1381 | generic_load, /* to_load */ |
| 1382 | NULL, /* to_lookup_symbol */ |
| 1383 | mips_create_inferior, /* to_create_inferior */ |
| 1384 | mips_mourn_inferior, /* to_mourn_inferior */ |
| 1385 | NULL, /* to_can_run */ |
| 1386 | NULL, /* to_notice_signals */ |
| 1387 | process_stratum, /* to_stratum */ |
| 1388 | NULL, /* to_next */ |
| 1389 | 1, /* to_has_all_memory */ |
| 1390 | 1, /* to_has_memory */ |
| 1391 | 1, /* to_has_stack */ |
| 1392 | 1, /* to_has_registers */ |
| 1393 | 1, /* to_has_execution */ |
| 1394 | NULL, /* sections */ |
| 1395 | NULL, /* sections_end */ |
| 1396 | OPS_MAGIC /* to_magic */ |
| 1397 | }; |
| 1398 | \f |
| 1399 | void |
| 1400 | _initialize_remote_mips () |
| 1401 | { |
| 1402 | add_target (&mips_ops); |
| 1403 | |
| 1404 | add_show_from_set ( |
| 1405 | add_set_cmd ("timeout", no_class, var_zinteger, |
| 1406 | (char *) &mips_receive_wait, |
| 1407 | "Set timeout in seconds for remote MIPS serial I/O.", |
| 1408 | &setlist), |
| 1409 | &showlist); |
| 1410 | |
| 1411 | add_show_from_set ( |
| 1412 | add_set_cmd ("retransmit-timeout", no_class, var_zinteger, |
| 1413 | (char *) &mips_retransmit_wait, |
| 1414 | "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\ |
| 1415 | This is the number of seconds to wait for an acknowledgement to a packet\n\ |
| 1416 | before resending the packet.", &setlist), |
| 1417 | &showlist); |
| 1418 | } |