1 /* Remote debugging interface for Densan DVE-R3900 ROM monitor for
2 GDB, the GNU debugger.
3 Copyright 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "target.h"
25 #include "monitor.h"
26 #include "serial.h"
27 #include "inferior.h"
28 #include "command.h"
29 #include "gdb_string.h"
30 #include <time.h>
31 #include "regcache.h"
32 #include "mips-tdep.h"
33
34 /* Type of function passed to bfd_map_over_sections. */
35
36 typedef void (*section_map_func) (bfd * abfd, asection * sect, void *obj);
37
38 /* Packet escape character used by Densan monitor. */
39
40 #define PESC 0xdc
41
42 /* Maximum packet size. This is actually smaller than necessary
43 just to be safe. */
44
45 #define MAXPSIZE 1024
46
47 /* External functions. */
48
49 extern void report_transfer_performance (unsigned long, time_t, time_t);
50
51 /* Certain registers are "bitmapped", in that the monitor can only display
52 them or let the user modify them as a series of named bitfields.
53 This structure describes a field in a bitmapped register. */
54
55 struct bit_field
56 {
57 char *prefix; /* string appearing before the value */
58 char *suffix; /* string appearing after the value */
59 char *user_name; /* name used by human when entering field value */
60 int length; /* number of bits in the field */
61 int start; /* starting (least significant) bit number of field */
62 };
63
64 /* Local functions for register manipulation. */
65
66 static void r3900_supply_register (char *regname, int regnamelen,
67 char *val, int vallen);
68 static void fetch_bad_vaddr (void);
69 static unsigned long fetch_fields (struct bit_field *bf);
70 static void fetch_bitmapped_register (int regno, struct bit_field *bf);
71 static void r3900_fetch_registers (int regno);
72 static void store_bitmapped_register (int regno, struct bit_field *bf);
73 static void r3900_store_registers (int regno);
74
75 /* Local functions for fast binary loading. */
76
77 static void write_long (char *buf, long n);
78 static void write_long_le (char *buf, long n);
79 static int debug_readchar (int hex);
80 static void debug_write (unsigned char *buf, int buflen);
81 static void ignore_packet (void);
82 static void send_packet (char type, unsigned char *buf, int buflen, int seq);
83 static void process_read_request (unsigned char *buf, int buflen);
84 static void count_section (bfd * abfd, asection * s,
85 unsigned int *section_count);
86 static void load_section (bfd * abfd, asection * s, unsigned int *data_count);
87 static void r3900_load (char *filename, int from_tty);
88
89 /* Miscellaneous local functions. */
90
91 static void r3900_open (char *args, int from_tty);
92
93
94 /* Pointers to static functions in monitor.c for fetching and storing
95 registers. We can't use these function in certain cases where the Densan
96 monitor acts perversely: for registers that it displays in bit-map
97 format, and those that can't be modified at all. In those cases
98 we have to use our own functions to fetch and store their values. */
99
100 static void (*orig_monitor_fetch_registers) (int regno);
101 static void (*orig_monitor_store_registers) (int regno);
102
103 /* Pointer to static function in monitor. for loading programs.
104 We use this function for loading S-records via the serial link. */
105
106 static void (*orig_monitor_load) (char *file, int from_tty);
107
108 /* This flag is set if a fast ethernet download should be used. */
109
110 static int ethernet = 0;
111
112 /* This array of registers needs to match the indexes used by GDB. The
113 whole reason this exists is because the various ROM monitors use
114 different names than GDB does, and don't support all the registers
115 either. */
116
117 static char *r3900_regnames[] =
118 {
119 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
120 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
121 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
122 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
123
124 "S", /* PS_REGNUM */
125 "l", /* MIPS_EMBED_LO_REGNUM */
126 "h", /* MIPS_EMBED_HI_REGNUM */
127 "B", /* MIPS_EMBED_BADVADDR_REGNUM */
128 "Pcause", /* MIPS_EMBED_CAUSE_REGNUM */
129 "p" /* MIPS_EMBED_PC_REGNUM */
130 };
131
132
133 /* Table of register names produced by monitor's register dump command. */
134
135 static struct reg_entry
136 {
137 char *name;
138 int regno;
139 }
140 reg_table[] =
141 {
142 {
143 "r0_zero", 0
144 }
145 ,
146 {
147 "r1_at", 1
148 }
149 ,
150 {
151 "r2_v0", 2
152 }
153 ,
154 {
155 "r3_v1", 3
156 }
157 ,
158 {
159 "r4_a0", 4
160 }
161 ,
162 {
163 "r5_a1", 5
164 }
165 ,
166 {
167 "r6_a2", 6
168 }
169 ,
170 {
171 "r7_a3", 7
172 }
173 ,
174 {
175 "r8_t0", 8
176 }
177 ,
178 {
179 "r9_t1", 9
180 }
181 ,
182 {
183 "r10_t2", 10
184 }
185 ,
186 {
187 "r11_t3", 11
188 }
189 ,
190 {
191 "r12_t4", 12
192 }
193 ,
194 {
195 "r13_t5", 13
196 }
197 ,
198 {
199 "r14_t6", 14
200 }
201 ,
202 {
203 "r15_t7", 15
204 }
205 ,
206 {
207 "r16_s0", 16
208 }
209 ,
210 {
211 "r17_s1", 17
212 }
213 ,
214 {
215 "r18_s2", 18
216 }
217 ,
218 {
219 "r19_s3", 19
220 }
221 ,
222 {
223 "r20_s4", 20
224 }
225 ,
226 {
227 "r21_s5", 21
228 }
229 ,
230 {
231 "r22_s6", 22
232 }
233 ,
234 {
235 "r23_s7", 23
236 }
237 ,
238 {
239 "r24_t8", 24
240 }
241 ,
242 {
243 "r25_t9", 25
244 }
245 ,
246 {
247 "r26_k0", 26
248 }
249 ,
250 {
251 "r27_k1", 27
252 }
253 ,
254 {
255 "r28_gp", 28
256 }
257 ,
258 {
259 "r29_sp", 29
260 }
261 ,
262 {
263 "r30_fp", 30
264 }
265 ,
266 {
267 "r31_ra", 31
268 }
269 ,
270 {
271 "HI", MIPS_EMBED_HI_REGNUM
272 }
273 ,
274 {
275 "LO", MIPS_EMBED_LO_REGNUM
276 }
277 ,
278 {
279 "PC", MIPS_EMBED_PC_REGNUM
280 }
281 ,
282 {
283 "BadV", MIPS_EMBED_BADVADDR_REGNUM
284 }
285 ,
286 {
287 NULL, 0
288 }
289 };
290
291
292 /* The monitor displays the cache register along with the status register,
293 as if they were a single register. So when we want to fetch the
294 status register, parse but otherwise ignore the fields of the
295 cache register that the monitor displays. Register fields that should
296 be ignored have a length of zero in the tables below. */
297
298 static struct bit_field status_fields[] =
299 {
300 /* Status register portion */
301 {"SR[<CU=", " ", "cu", 4, 28},
302 {"RE=", " ", "re", 1, 25},
303 {"BEV=", " ", "bev", 1, 22},
304 {"TS=", " ", "ts", 1, 21},
305 {"Nmi=", " ", "nmi", 1, 20},
306 {"INT=", " ", "int", 6, 10},
307 {"SW=", ">]", "sw", 2, 8},
308 {"[<KUO=", " ", "kuo", 1, 5},
309 {"IEO=", " ", "ieo", 1, 4},
310 {"KUP=", " ", "kup", 1, 3},
311 {"IEP=", " ", "iep", 1, 2},
312 {"KUC=", " ", "kuc", 1, 1},
313 {"IEC=", ">]", "iec", 1, 0},
314
315 /* Cache register portion (dummy for parsing only) */
316 {"CR[<IalO=", " ", "ialo", 0, 13},
317 {"DalO=", " ", "dalo", 0, 12},
318 {"IalP=", " ", "ialp", 0, 11},
319 {"DalP=", " ", "dalp", 0, 10},
320 {"IalC=", " ", "ialc", 0, 9},
321 {"DalC=", ">] ", "dalc", 0, 8},
322
323 {NULL, NULL, 0, 0} /* end of table marker */
324 };
325
326
327 #if 0 /* FIXME: Enable when we add support for modifying cache register. */
328 static struct bit_field cache_fields[] =
329 {
330 /* Status register portion (dummy for parsing only) */
331 {"SR[<CU=", " ", "cu", 0, 28},
332 {"RE=", " ", "re", 0, 25},
333 {"BEV=", " ", "bev", 0, 22},
334 {"TS=", " ", "ts", 0, 21},
335 {"Nmi=", " ", "nmi", 0, 20},
336 {"INT=", " ", "int", 0, 10},
337 {"SW=", ">]", "sw", 0, 8},
338 {"[<KUO=", " ", "kuo", 0, 5},
339 {"IEO=", " ", "ieo", 0, 4},
340 {"KUP=", " ", "kup", 0, 3},
341 {"IEP=", " ", "iep", 0, 2},
342 {"KUC=", " ", "kuc", 0, 1},
343 {"IEC=", ">]", "iec", 0, 0},
344
345 /* Cache register portion */
346 {"CR[<IalO=", " ", "ialo", 1, 13},
347 {"DalO=", " ", "dalo", 1, 12},
348 {"IalP=", " ", "ialp", 1, 11},
349 {"DalP=", " ", "dalp", 1, 10},
350 {"IalC=", " ", "ialc", 1, 9},
351 {"DalC=", ">] ", "dalc", 1, 8},
352
353 {NULL, NULL, NULL, 0, 0} /* end of table marker */
354 };
355 #endif
356
357
358 static struct bit_field cause_fields[] =
359 {
360 {"<BD=", " ", "bd", 1, 31},
361 {"CE=", " ", "ce", 2, 28},
362 {"IP=", " ", "ip", 6, 10},
363 {"SW=", " ", "sw", 2, 8},
364 {"EC=", ">]", "ec", 5, 2},
365
366 {NULL, NULL, NULL, 0, 0} /* end of table marker */
367 };
368
369
370 /* The monitor prints register values in the form
371
372 regname = xxxx xxxx
373
374 We look up the register name in a table, and remove the embedded space in
375 the hex value before passing it to monitor_supply_register. */
376
377 static void
r3900_supply_register(char * regname,int regnamelen,char * val,int vallen)378 r3900_supply_register (char *regname, int regnamelen, char *val, int vallen)
379 {
380 int regno = -1;
381 int i;
382 char valbuf[10];
383 char *p;
384
385 /* Perform some sanity checks on the register name and value. */
386 if (regnamelen < 2 || regnamelen > 7 || vallen != 9)
387 return;
388
389 /* Look up the register name. */
390 for (i = 0; reg_table[i].name != NULL; i++)
391 {
392 int rlen = strlen (reg_table[i].name);
393 if (rlen == regnamelen && strncmp (regname, reg_table[i].name, rlen) == 0)
394 {
395 regno = reg_table[i].regno;
396 break;
397 }
398 }
399 if (regno == -1)
400 return;
401
402 /* Copy the hex value to a buffer and eliminate the embedded space. */
403 for (i = 0, p = valbuf; i < vallen; i++)
404 if (val[i] != ' ')
405 *p++ = val[i];
406 *p = '\0';
407
408 monitor_supply_register (regno, valbuf);
409 }
410
411
412 /* Fetch the BadVaddr register. Unlike the other registers, this
413 one can't be modified, and the monitor won't even prompt to let
414 you modify it. */
415
416 static void
fetch_bad_vaddr(void)417 fetch_bad_vaddr (void)
418 {
419 char buf[20];
420
421 monitor_printf ("xB\r");
422 monitor_expect ("BadV=", NULL, 0);
423 monitor_expect_prompt (buf, sizeof (buf));
424 monitor_supply_register (mips_regnum (current_gdbarch)->badvaddr, buf);
425 }
426
427
428 /* Read a series of bit fields from the monitor, and return their
429 combined binary value. */
430
431 static unsigned long
fetch_fields(struct bit_field * bf)432 fetch_fields (struct bit_field *bf)
433 {
434 char buf[20];
435 unsigned long val = 0;
436 unsigned long bits;
437
438 for (; bf->prefix != NULL; bf++)
439 {
440 monitor_expect (bf->prefix, NULL, 0); /* get prefix */
441 monitor_expect (bf->suffix, buf, sizeof (buf)); /* hex value, suffix */
442 if (bf->length != 0)
443 {
444 bits = strtoul (buf, NULL, 16); /* get field value */
445 bits &= ((1 << bf->length) - 1); /* mask out useless bits */
446 val |= bits << bf->start; /* insert into register */
447 }
448
449 }
450
451 return val;
452 }
453
454
455 static void
fetch_bitmapped_register(int regno,struct bit_field * bf)456 fetch_bitmapped_register (int regno, struct bit_field *bf)
457 {
458 unsigned long val;
459 unsigned char regbuf[MAX_REGISTER_SIZE];
460 char *regname = NULL;
461
462 if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0]))
463 internal_error (__FILE__, __LINE__,
464 "fetch_bitmapped_register: regno out of bounds");
465 else
466 regname = r3900_regnames[regno];
467
468 monitor_printf ("x%s\r", regname);
469 val = fetch_fields (bf);
470 monitor_printf (".\r");
471 monitor_expect_prompt (NULL, 0);
472
473 /* supply register stores in target byte order, so swap here */
474
475 store_unsigned_integer (regbuf, register_size (current_gdbarch, regno), val);
476 regcache_raw_supply (current_regcache, regno, regbuf);
477
478 }
479
480
481 /* Fetch all registers (if regno is -1), or one register from the
482 monitor. For most registers, we can use the generic monitor_
483 monitor_fetch_registers function. But others are displayed in
484 a very unusual fashion by the monitor, and must be handled specially. */
485
486 static void
r3900_fetch_registers(int regno)487 r3900_fetch_registers (int regno)
488 {
489 if (regno == mips_regnum (current_gdbarch)->badvaddr)
490 fetch_bad_vaddr ();
491 else if (regno == PS_REGNUM)
492 fetch_bitmapped_register (PS_REGNUM, status_fields);
493 else if (regno == mips_regnum (current_gdbarch)->cause)
494 fetch_bitmapped_register (mips_regnum (current_gdbarch)->cause,
495 cause_fields);
496 else
497 orig_monitor_fetch_registers (regno);
498 }
499
500
501 /* Write the new value of the bitmapped register to the monitor. */
502
503 static void
store_bitmapped_register(int regno,struct bit_field * bf)504 store_bitmapped_register (int regno, struct bit_field *bf)
505 {
506 unsigned long oldval, newval;
507 char *regname = NULL;
508
509 if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0]))
510 internal_error (__FILE__, __LINE__,
511 "fetch_bitmapped_register: regno out of bounds");
512 else
513 regname = r3900_regnames[regno];
514
515 /* Fetch the current value of the register. */
516 monitor_printf ("x%s\r", regname);
517 oldval = fetch_fields (bf);
518 newval = read_register (regno);
519
520 /* To save time, write just the fields that have changed. */
521 for (; bf->prefix != NULL; bf++)
522 {
523 if (bf->length != 0)
524 {
525 unsigned long oldbits, newbits, mask;
526
527 mask = (1 << bf->length) - 1;
528 oldbits = (oldval >> bf->start) & mask;
529 newbits = (newval >> bf->start) & mask;
530 if (oldbits != newbits)
531 monitor_printf ("%s %lx ", bf->user_name, newbits);
532 }
533 }
534
535 monitor_printf (".\r");
536 monitor_expect_prompt (NULL, 0);
537 }
538
539
540 static void
r3900_store_registers(int regno)541 r3900_store_registers (int regno)
542 {
543 if (regno == PS_REGNUM)
544 store_bitmapped_register (PS_REGNUM, status_fields);
545 else if (regno == mips_regnum (current_gdbarch)->cause)
546 store_bitmapped_register (mips_regnum (current_gdbarch)->cause,
547 cause_fields);
548 else
549 orig_monitor_store_registers (regno);
550 }
551
552
553 /* Write a 4-byte integer to the buffer in big-endian order. */
554
555 static void
write_long(char * buf,long n)556 write_long (char *buf, long n)
557 {
558 buf[0] = (n >> 24) & 0xff;
559 buf[1] = (n >> 16) & 0xff;
560 buf[2] = (n >> 8) & 0xff;
561 buf[3] = n & 0xff;
562 }
563
564
565 /* Write a 4-byte integer to the buffer in little-endian order. */
566
567 static void
write_long_le(char * buf,long n)568 write_long_le (char *buf, long n)
569 {
570 buf[0] = n & 0xff;
571 buf[1] = (n >> 8) & 0xff;
572 buf[2] = (n >> 16) & 0xff;
573 buf[3] = (n >> 24) & 0xff;
574 }
575
576
577 /* Read a character from the monitor. If remote debugging is on,
578 print the received character. If HEX is non-zero, print the
579 character in hexadecimal; otherwise, print it in ASCII. */
580
581 static int
debug_readchar(int hex)582 debug_readchar (int hex)
583 {
584 char buf[10];
585 int c = monitor_readchar ();
586
587 if (remote_debug > 0)
588 {
589 if (hex)
590 sprintf (buf, "[%02x]", c & 0xff);
591 else if (c == '\0')
592 strcpy (buf, "\\0");
593 else
594 {
595 buf[0] = c;
596 buf[1] = '\0';
597 }
598 puts_debug ("Read -->", buf, "<--");
599 }
600 return c;
601 }
602
603
604 /* Send a buffer of characters to the monitor. If remote debugging is on,
605 print the sent buffer in hex. */
606
607 static void
debug_write(unsigned char * buf,int buflen)608 debug_write (unsigned char *buf, int buflen)
609 {
610 char s[10];
611
612 monitor_write (buf, buflen);
613
614 if (remote_debug > 0)
615 {
616 while (buflen-- > 0)
617 {
618 sprintf (s, "[%02x]", *buf & 0xff);
619 puts_debug ("Sent -->", s, "<--");
620 buf++;
621 }
622 }
623 }
624
625
626 /* Ignore a packet sent to us by the monitor. It send packets
627 when its console is in "communications interface" mode. A packet
628 is of this form:
629
630 start of packet flag (one byte: 0xdc)
631 packet type (one byte)
632 length (low byte)
633 length (high byte)
634 data (length bytes)
635
636 The last two bytes of the data field are a checksum, but we don't
637 bother to verify it.
638 */
639
640 static void
ignore_packet(void)641 ignore_packet (void)
642 {
643 int c = -1;
644 int len;
645
646 /* Ignore lots of trash (messages about section addresses, for example)
647 until we see the start of a packet. */
648 for (len = 0; len < 256; len++)
649 {
650 c = debug_readchar (0);
651 if (c == PESC)
652 break;
653 }
654 if (len == 8)
655 error ("Packet header byte not found; %02x seen instead.", c);
656
657 /* Read the packet type and length. */
658 c = debug_readchar (1); /* type */
659
660 c = debug_readchar (1); /* low byte of length */
661 len = c & 0xff;
662
663 c = debug_readchar (1); /* high byte of length */
664 len += (c & 0xff) << 8;
665
666 /* Ignore the rest of the packet. */
667 while (len-- > 0)
668 c = debug_readchar (1);
669 }
670
671
672 /* Encapsulate some data into a packet and send it to the monitor.
673
674 The 'p' packet is a special case. This is a packet we send
675 in response to a read ('r') packet from the monitor. This function
676 appends a one-byte sequence number to the data field of such a packet.
677 */
678
679 static void
send_packet(char type,unsigned char * buf,int buflen,int seq)680 send_packet (char type, unsigned char *buf, int buflen, int seq)
681 {
682 unsigned char hdr[4];
683 int len = buflen;
684 int sum, i;
685
686 /* If this is a 'p' packet, add one byte for a sequence number. */
687 if (type == 'p')
688 len++;
689
690 /* If the buffer has a non-zero length, add two bytes for a checksum. */
691 if (len > 0)
692 len += 2;
693
694 /* Write the packet header. */
695 hdr[0] = PESC;
696 hdr[1] = type;
697 hdr[2] = len & 0xff;
698 hdr[3] = (len >> 8) & 0xff;
699 debug_write (hdr, sizeof (hdr));
700
701 if (len)
702 {
703 /* Write the packet data. */
704 debug_write (buf, buflen);
705
706 /* Write the sequence number if this is a 'p' packet. */
707 if (type == 'p')
708 {
709 hdr[0] = seq;
710 debug_write (hdr, 1);
711 }
712
713 /* Write the checksum. */
714 sum = 0;
715 for (i = 0; i < buflen; i++)
716 {
717 int tmp = (buf[i] & 0xff);
718 if (i & 1)
719 sum += tmp;
720 else
721 sum += tmp << 8;
722 }
723 if (type == 'p')
724 {
725 if (buflen & 1)
726 sum += (seq & 0xff);
727 else
728 sum += (seq & 0xff) << 8;
729 }
730 sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
731 sum += (sum >> 16) & 1;
732 sum = ~sum;
733
734 hdr[0] = (sum >> 8) & 0xff;
735 hdr[1] = sum & 0xff;
736 debug_write (hdr, 2);
737 }
738 }
739
740
741 /* Respond to an expected read request from the monitor by sending
742 data in chunks. Handle all acknowledgements and handshaking packets.
743
744 The monitor expects a response consisting of a one or more 'p' packets,
745 each followed by a portion of the data requested. The 'p' packet
746 contains only a four-byte integer, the value of which is the number
747 of bytes of data we are about to send. Following the 'p' packet,
748 the monitor expects the data bytes themselves in raw, unpacketized,
749 form, without even a checksum.
750 */
751
752 static void
process_read_request(unsigned char * buf,int buflen)753 process_read_request (unsigned char *buf, int buflen)
754 {
755 unsigned char len[4];
756 int i, chunk;
757 unsigned char seq;
758
759 /* Discard the read request. FIXME: we have to hope it's for
760 the exact number of bytes we want to send; should check for this. */
761 ignore_packet ();
762
763 for (i = chunk = 0, seq = 0; i < buflen; i += chunk, seq++)
764 {
765 /* Don't send more than MAXPSIZE bytes at a time. */
766 chunk = buflen - i;
767 if (chunk > MAXPSIZE)
768 chunk = MAXPSIZE;
769
770 /* Write a packet containing the number of bytes we are sending. */
771 write_long_le (len, chunk);
772 send_packet ('p', len, sizeof (len), seq);
773
774 /* Write the data in raw form following the packet. */
775 debug_write (&buf[i], chunk);
776
777 /* Discard the ACK packet. */
778 ignore_packet ();
779 }
780
781 /* Send an "end of data" packet. */
782 send_packet ('e', "", 0, 0);
783 }
784
785
786 /* Count loadable sections (helper function for r3900_load). */
787
788 static void
count_section(bfd * abfd,asection * s,unsigned int * section_count)789 count_section (bfd *abfd, asection *s, unsigned int *section_count)
790 {
791 if (s->flags & SEC_LOAD && bfd_section_size (abfd, s) != 0)
792 (*section_count)++;
793 }
794
795
796 /* Load a single BFD section (helper function for r3900_load).
797
798 WARNING: this code is filled with assumptions about how
799 the Densan monitor loads programs. The monitor issues
800 packets containing read requests, but rather than respond
801 to them in an general way, we expect them to following
802 a certain pattern.
803
804 For example, we know that the monitor will start loading by
805 issuing an 8-byte read request for the binary file header.
806 We know this is coming and ignore the actual contents
807 of the read request packet.
808 */
809
810 static void
load_section(bfd * abfd,asection * s,unsigned int * data_count)811 load_section (bfd *abfd, asection *s, unsigned int *data_count)
812 {
813 if (s->flags & SEC_LOAD)
814 {
815 bfd_size_type section_size = bfd_section_size (abfd, s);
816 bfd_vma section_base = bfd_section_lma (abfd, s);
817 unsigned char *buffer;
818 unsigned char header[8];
819
820 /* Don't output zero-length sections. */
821 if (section_size == 0)
822 return;
823 if (data_count)
824 *data_count += section_size;
825
826 /* Print some fluff about the section being loaded. */
827 printf_filtered ("Loading section %s, size 0x%lx lma ",
828 bfd_section_name (abfd, s), (long) section_size);
829 print_address_numeric (section_base, 1, gdb_stdout);
830 printf_filtered ("\n");
831 gdb_flush (gdb_stdout);
832
833 /* Write the section header (location and size). */
834 write_long (&header[0], (long) section_base);
835 write_long (&header[4], (long) section_size);
836 process_read_request (header, sizeof (header));
837
838 /* Read the section contents into a buffer, write it out,
839 then free the buffer. */
840 buffer = (unsigned char *) xmalloc (section_size);
841 bfd_get_section_contents (abfd, s, buffer, 0, section_size);
842 process_read_request (buffer, section_size);
843 xfree (buffer);
844 }
845 }
846
847
848 /* When the ethernet is used as the console port on the Densan board,
849 we can use the "Rm" command to do a fast binary load. The format
850 of the download data is:
851
852 number of sections (4 bytes)
853 starting address (4 bytes)
854 repeat for each section:
855 location address (4 bytes)
856 section size (4 bytes)
857 binary data
858
859 The 4-byte fields are all in big-endian order.
860
861 Using this command is tricky because we have to put the monitor
862 into a special funky "communications interface" mode, in which
863 it sends and receives packets of data along with the normal prompt.
864 */
865
866 static void
r3900_load(char * filename,int from_tty)867 r3900_load (char *filename, int from_tty)
868 {
869 bfd *abfd;
870 unsigned int data_count = 0;
871 time_t start_time, end_time; /* for timing of download */
872 int section_count = 0;
873 unsigned char buffer[8];
874
875 /* If we are not using the ethernet, use the normal monitor load,
876 which sends S-records over the serial link. */
877 if (!ethernet)
878 {
879 orig_monitor_load (filename, from_tty);
880 return;
881 }
882
883 /* Open the file. */
884 if (filename == NULL || filename[0] == 0)
885 filename = get_exec_file (1);
886 abfd = bfd_openr (filename, 0);
887 if (!abfd)
888 error ("Unable to open file %s\n", filename);
889 if (bfd_check_format (abfd, bfd_object) == 0)
890 error ("File is not an object file\n");
891
892 /* Output the "vconsi" command to get the monitor in the communication
893 state where it will accept a load command. This will cause
894 the monitor to emit a packet before each prompt, so ignore the packet. */
895 monitor_printf ("vconsi\r");
896 ignore_packet ();
897 monitor_expect_prompt (NULL, 0);
898
899 /* Output the "Rm" (load) command and respond to the subsequent "open"
900 packet by sending an ACK packet. */
901 monitor_printf ("Rm\r");
902 ignore_packet ();
903 send_packet ('a', "", 0, 0);
904
905 /* Output the fast load header (number of sections and starting address). */
906 bfd_map_over_sections ((bfd *) abfd, (section_map_func) count_section,
907 §ion_count);
908 write_long (&buffer[0], (long) section_count);
909 if (exec_bfd)
910 write_long (&buffer[4], (long) bfd_get_start_address (exec_bfd));
911 else
912 write_long (&buffer[4], 0);
913 process_read_request (buffer, sizeof (buffer));
914
915 /* Output the section data. */
916 start_time = time (NULL);
917 bfd_map_over_sections (abfd, (section_map_func) load_section, &data_count);
918 end_time = time (NULL);
919
920 /* Acknowledge the close packet and put the monitor back into
921 "normal" mode so it won't send packets any more. */
922 ignore_packet ();
923 send_packet ('a', "", 0, 0);
924 monitor_expect_prompt (NULL, 0);
925 monitor_printf ("vconsx\r");
926 monitor_expect_prompt (NULL, 0);
927
928 /* Print start address and download performance information. */
929 printf_filtered ("Start address 0x%lx\n", (long) bfd_get_start_address (abfd));
930 report_transfer_performance (data_count, start_time, end_time);
931
932 /* Finally, make the PC point at the start address */
933 if (exec_bfd)
934 write_pc (bfd_get_start_address (exec_bfd));
935
936 inferior_ptid = null_ptid; /* No process now */
937
938 /* This is necessary because many things were based on the PC at the
939 time that we attached to the monitor, which is no longer valid
940 now that we have loaded new code (and just changed the PC).
941 Another way to do this might be to call normal_stop, except that
942 the stack may not be valid, and things would get horribly
943 confused... */
944 clear_symtab_users ();
945 }
946
947
948 /* Commands to send to the monitor when first connecting:
949 * The bare carriage return forces a prompt from the monitor
950 (monitor doesn't prompt immediately after a reset).
951 * The "vconsx" switches the monitor back to interactive mode
952 in case an aborted download had left it in packet mode.
953 * The "Xtr" command causes subsequent "t" (trace) commands to display
954 the general registers only.
955 * The "Xxr" command does the same thing for the "x" (examine
956 registers) command.
957 * The "bx" command clears all breakpoints.
958 */
959
960 static char *r3900_inits[] =
961 {"\r", "vconsx\r", "Xtr\r", "Xxr\r", "bx\r", NULL};
962 static char *dummy_inits[] =
963 {NULL};
964
965 static struct target_ops r3900_ops;
966 static struct monitor_ops r3900_cmds;
967
968 static void
r3900_open(char * args,int from_tty)969 r3900_open (char *args, int from_tty)
970 {
971 char buf[64];
972 int i;
973
974 monitor_open (args, &r3900_cmds, from_tty);
975
976 /* We have to handle sending the init strings ourselves, because
977 the first two strings we send (carriage returns) may not be echoed
978 by the monitor, but the rest will be. */
979 monitor_printf_noecho ("\r\r");
980 for (i = 0; r3900_inits[i] != NULL; i++)
981 {
982 monitor_printf (r3900_inits[i]);
983 monitor_expect_prompt (NULL, 0);
984 }
985
986 /* Attempt to determine whether the console device is ethernet or serial.
987 This will tell us which kind of load to use (S-records over a serial
988 link, or the Densan fast binary multi-section format over the net). */
989
990 ethernet = 0;
991 monitor_printf ("v\r");
992 if (monitor_expect ("console device :", NULL, 0) != -1)
993 if (monitor_expect ("\n", buf, sizeof (buf)) != -1)
994 if (strstr (buf, "ethernet") != NULL)
995 ethernet = 1;
996 monitor_expect_prompt (NULL, 0);
997 }
998
999 void
_initialize_r3900_rom(void)1000 _initialize_r3900_rom (void)
1001 {
1002 r3900_cmds.flags = MO_NO_ECHO_ON_OPEN |
1003 MO_ADDR_BITS_REMOVE |
1004 MO_CLR_BREAK_USES_ADDR |
1005 MO_GETMEM_READ_SINGLE |
1006 MO_PRINT_PROGRAM_OUTPUT;
1007
1008 r3900_cmds.init = dummy_inits;
1009 r3900_cmds.cont = "g\r";
1010 r3900_cmds.step = "t\r";
1011 r3900_cmds.set_break = "b %A\r"; /* COREADDR */
1012 r3900_cmds.clr_break = "b %A,0\r"; /* COREADDR */
1013 r3900_cmds.fill = "fx %A s %x %x\r"; /* COREADDR, len, val */
1014
1015 r3900_cmds.setmem.cmdb = "sx %A %x\r"; /* COREADDR, val */
1016 r3900_cmds.setmem.cmdw = "sh %A %x\r"; /* COREADDR, val */
1017 r3900_cmds.setmem.cmdl = "sw %A %x\r"; /* COREADDR, val */
1018
1019 r3900_cmds.getmem.cmdb = "sx %A\r"; /* COREADDR */
1020 r3900_cmds.getmem.cmdw = "sh %A\r"; /* COREADDR */
1021 r3900_cmds.getmem.cmdl = "sw %A\r"; /* COREADDR */
1022 r3900_cmds.getmem.resp_delim = " : ";
1023 r3900_cmds.getmem.term = " ";
1024 r3900_cmds.getmem.term_cmd = ".\r";
1025
1026 r3900_cmds.setreg.cmd = "x%s %x\r"; /* regname, val */
1027
1028 r3900_cmds.getreg.cmd = "x%s\r"; /* regname */
1029 r3900_cmds.getreg.resp_delim = "=";
1030 r3900_cmds.getreg.term = " ";
1031 r3900_cmds.getreg.term_cmd = ".\r";
1032
1033 r3900_cmds.dump_registers = "x\r";
1034 r3900_cmds.register_pattern =
1035 "\\([a-zA-Z0-9_]+\\) *=\\([0-9a-f]+ [0-9a-f]+\\b\\)";
1036 r3900_cmds.supply_register = r3900_supply_register;
1037 /* S-record download, via "keyboard port". */
1038 r3900_cmds.load = "r0\r";
1039 r3900_cmds.prompt = "#";
1040 r3900_cmds.line_term = "\r";
1041 r3900_cmds.target = &r3900_ops;
1042 r3900_cmds.stopbits = SERIAL_1_STOPBITS;
1043 r3900_cmds.regnames = r3900_regnames;
1044 r3900_cmds.magic = MONITOR_OPS_MAGIC;
1045
1046 init_monitor_ops (&r3900_ops);
1047
1048 r3900_ops.to_shortname = "r3900";
1049 r3900_ops.to_longname = "R3900 monitor";
1050 r3900_ops.to_doc = "Debug using the DVE R3900 monitor.\n\
1051 Specify the serial device it is connected to (e.g. /dev/ttya).";
1052 r3900_ops.to_open = r3900_open;
1053
1054 /* Override the functions to fetch and store registers. But save the
1055 addresses of the default functions, because we will use those functions
1056 for "normal" registers. */
1057
1058 orig_monitor_fetch_registers = r3900_ops.to_fetch_registers;
1059 orig_monitor_store_registers = r3900_ops.to_store_registers;
1060 r3900_ops.to_fetch_registers = r3900_fetch_registers;
1061 r3900_ops.to_store_registers = r3900_store_registers;
1062
1063 /* Override the load function, but save the address of the default
1064 function to use when loading S-records over a serial link. */
1065 orig_monitor_load = r3900_ops.to_load;
1066 r3900_ops.to_load = r3900_load;
1067
1068 add_target (&r3900_ops);
1069 }
1070