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