1 /* Remote debugging interface for M32R/SDI.
3 Copyright 2003 Free Software Foundation, Inc.
5 Contributed by Renesas Technology Co.
6 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
31 #include "gdb_string.h"
34 #include <netinet/in.h>
35 #include <sys/types.h>
43 /* Descriptor for I/O to remote machine. */
45 static struct serial *sdi_desc = NULL;
47 #define SDI_TIMEOUT 30
52 static char chip_name[64];
55 static unsigned long last_pc_addr = 0xffffffff;
56 static unsigned char last_pc_addr_data[2];
58 static int mmu_on = 0;
60 static int use_ib_breakpoints = 1;
62 #define MAX_BREAKPOINTS 1024
63 static int max_ib_breakpoints;
64 static unsigned long bp_address[MAX_BREAKPOINTS];
65 static unsigned char bp_data[MAX_BREAKPOINTS][4];
66 static const unsigned char ib_bp_entry_enable[] = {
67 0x00, 0x00, 0x00, 0x06
69 static const unsigned char ib_bp_entry_disable[] = {
70 0x00, 0x00, 0x00, 0x00
74 static const unsigned char dbt_bp_entry[] = {
75 0x10, 0xe0, 0x70, 0x00
78 #define MAX_ACCESS_BREAKS 4
79 static int max_access_breaks;
80 static unsigned long ab_address[MAX_ACCESS_BREAKS];
81 static unsigned int ab_type[MAX_ACCESS_BREAKS];
82 static unsigned int ab_size[MAX_ACCESS_BREAKS];
83 static CORE_ADDR hit_watchpoint_addr = 0;
85 static int interrupted = 0;
87 /* Forward data declarations */
88 extern struct target_ops m32r_ops;
95 #define SDI_READ_CPU_REG 4
96 #define SDI_WRITE_CPU_REG 5
97 #define SDI_READ_MEMORY 6
98 #define SDI_WRITE_MEMORY 7
99 #define SDI_EXEC_CPU 8
100 #define SDI_STOP_CPU 9
101 #define SDI_WAIT_FOR_READY 10
102 #define SDI_GET_ATTR 11
103 #define SDI_SET_ATTR 12
104 #define SDI_STATUS 13
107 #define SDI_ATTR_NAME 1
108 #define SDI_ATTR_BRK 2
109 #define SDI_ATTR_ABRK 3
110 #define SDI_ATTR_CACHE 4
111 #define SDI_CACHE_TYPE_M32102 0
112 #define SDI_CACHE_TYPE_CHAOS 1
113 #define SDI_ATTR_MEM_ACCESS 5
114 #define SDI_MEM_ACCESS_DEBUG_DMA 0
115 #define SDI_MEM_ACCESS_MON_CODE 1
128 #define SDI_REG_R10 10
129 #define SDI_REG_R11 11
130 #define SDI_REG_R12 12
131 #define SDI_REG_FP 13
132 #define SDI_REG_LR 14
133 #define SDI_REG_SP 15
134 #define SDI_REG_PSW 16
135 #define SDI_REG_CBR 17
136 #define SDI_REG_SPI 18
137 #define SDI_REG_SPU 19
138 #define SDI_REG_CR4 20
139 #define SDI_REG_EVB 21
140 #define SDI_REG_BPC 22
141 #define SDI_REG_CR7 23
142 #define SDI_REG_BBPSW 24
143 #define SDI_REG_CR9 25
144 #define SDI_REG_CR10 26
145 #define SDI_REG_CR11 27
146 #define SDI_REG_CR12 28
147 #define SDI_REG_WR 29
148 #define SDI_REG_BBPC 30
149 #define SDI_REG_PBP 31
150 #define SDI_REG_ACCH 32
151 #define SDI_REG_ACCL 33
152 #define SDI_REG_ACC1H 34
153 #define SDI_REG_ACC1L 35
156 /* Low level communication functions */
158 /* Check an ack packet from the target */
167 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
172 if (c != '+') /* error */
178 /* Send data to the target and check an ack packet */
180 send_data (void *buf, int len)
187 if (serial_write (sdi_desc, buf, len) != 0)
190 if (get_ack () == -1)
196 /* Receive data from the target */
198 recv_data (void *buf, int len)
208 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
213 ((unsigned char *) buf)[total++] = c;
219 /* Store unsigned long parameter on packet */
221 store_long_parameter (void *buf, long val)
224 memcpy (buf, &val, 4);
227 /* Check if MMU is on */
229 check_mmu_status (void)
232 unsigned char buf[2];
234 /* Read PC address */
235 buf[0] = SDI_READ_CPU_REG;
236 buf[1] = SDI_REG_BPC;
237 if (send_data (buf, 2) == -1)
241 if ((val & 0xc0000000) == 0x80000000)
247 /* Read EVB address */
248 buf[0] = SDI_READ_CPU_REG;
249 buf[1] = SDI_REG_EVB;
250 if (send_data (buf, 2) == -1)
254 if ((val & 0xc0000000) == 0x80000000)
264 /* This is called not only when we first attach, but also when the
265 user types "run" after having attached. */
267 m32r_create_inferior (char *execfile, char *args, char **env)
272 error ("Cannot pass arguments to remote STDEBUG process");
274 if (execfile == 0 || exec_bfd == 0)
275 error ("No executable file specified");
278 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
281 entry_pt = bfd_get_start_address (exec_bfd);
283 /* The "process" (board) is already stopped awaiting our commands, and
284 the program is already downloaded. We just set its PC and go. */
286 clear_proceed_status ();
288 /* Tell wait_for_inferior that we've started a new process. */
289 init_wait_for_inferior ();
291 /* Set up the "saved terminal modes" of the inferior
292 based on what modes we are starting it with. */
293 target_terminal_init ();
295 /* Install inferior's terminal modes. */
296 target_terminal_inferior ();
298 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
301 /* Open a connection to a remote debugger.
302 NAME is the filename used for communication. */
305 m32r_open (char *args, int from_tty)
307 struct hostent *host_ent;
308 struct sockaddr_in server_addr;
309 char *port_str, hostname[256];
311 unsigned char buf[2];
316 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
318 target_preopen (from_tty);
320 push_target (&m32r_ops);
323 sprintf (hostname, "localhost:%d", SDIPORT);
326 port_str = strchr (args, ':');
327 if (port_str == NULL)
328 sprintf (hostname, "%s:%d", args, SDIPORT);
330 strcpy (hostname, args);
333 sdi_desc = serial_open (hostname);
335 error ("Connection refused\n");
337 if (get_ack () == -1)
338 error ("Cannot connect to SDI target\n");
341 if (send_data (buf, 1) == -1)
342 error ("Cannot connect to SDI target\n");
344 /* Get maximum number of ib breakpoints */
345 buf[0] = SDI_GET_ATTR;
346 buf[1] = SDI_ATTR_BRK;
349 max_ib_breakpoints = buf[0];
351 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
353 /* Initialize breakpoints. */
354 for (i = 0; i < MAX_BREAKPOINTS; i++)
355 bp_address[i] = 0xffffffff;
357 /* Get maximum number of access breaks. */
358 buf[0] = SDI_GET_ATTR;
359 buf[1] = SDI_ATTR_ABRK;
362 max_access_breaks = buf[0];
364 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
366 /* Initialize access breask. */
367 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
368 ab_address[i] = 0x00000000;
372 /* Get the name of chip on target board. */
373 buf[0] = SDI_GET_ATTR;
374 buf[1] = SDI_ATTR_NAME;
376 recv_data (chip_name, 64);
379 printf_filtered ("Remote %s connected to %s\n", target_shortname,
383 /* Close out all files and local state before this target loses control. */
386 m32r_close (int quitting)
388 unsigned char buf[1];
391 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
397 serial_close (sdi_desc);
401 inferior_ptid = null_ptid;
405 /* Tell the remote machine to resume. */
408 m32r_resume (ptid_t ptid, int step, enum target_signal sig)
410 unsigned long pc_addr, bp_addr, ab_addr;
411 unsigned char buf[13];
417 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
419 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
424 pc_addr = read_pc ();
426 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
428 /* At pc address there is a parallel instruction with +2 offset,
429 so we have to make it a serial instruction or avoid it. */
430 if (pc_addr == last_pc_addr)
432 /* Avoid a parallel nop. */
433 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
436 /* Now we can forget this instruction. */
437 last_pc_addr = 0xffffffff;
439 /* Clear a parallel bit. */
442 buf[0] = SDI_WRITE_MEMORY;
443 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
444 store_long_parameter (buf + 1, pc_addr);
446 store_long_parameter (buf + 1, pc_addr - 1);
447 store_long_parameter (buf + 5, 1);
448 buf[9] = last_pc_addr_data[0] & 0x7f;
454 buf[0] = SDI_WRITE_CPU_REG;
455 buf[1] = SDI_REG_BPC;
456 store_long_parameter (buf + 2, pc_addr);
464 buf[0] = SDI_WRITE_CPU_REG;
465 buf[1] = SDI_REG_PBP;
466 store_long_parameter (buf + 2, pc_addr | 1);
473 if (use_ib_breakpoints)
474 ib_breakpoints = max_ib_breakpoints;
478 /* Set ib breakpoints. */
479 for (i = 0; i < ib_breakpoints; i++)
481 bp_addr = bp_address[i];
482 if (bp_addr != 0xffffffff && bp_addr != pc_addr)
485 buf[0] = SDI_WRITE_MEMORY;
486 store_long_parameter (buf + 1, 0xffff8000 + 4 * i);
487 store_long_parameter (buf + 5, 4);
488 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
490 buf[9] = ib_bp_entry_enable[0];
491 buf[10] = ib_bp_entry_enable[1];
492 buf[11] = ib_bp_entry_enable[2];
493 buf[12] = ib_bp_entry_enable[3];
497 buf[9] = ib_bp_entry_enable[3];
498 buf[10] = ib_bp_entry_enable[2];
499 buf[11] = ib_bp_entry_enable[1];
500 buf[12] = ib_bp_entry_enable[0];
504 buf[0] = SDI_WRITE_MEMORY;
505 store_long_parameter (buf + 1, 0xffff8080 + 4 * i);
506 store_long_parameter (buf + 5, 4);
507 store_unsigned_integer (buf + 9, 4, bp_addr);
512 /* Set dbt breakpoints. */
513 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
515 bp_addr = bp_address[i];
516 if (bp_addr != 0xffffffff && bp_addr != pc_addr)
519 bp_addr &= 0x7fffffff;
521 /* Write DBT instruction. */
522 buf[0] = SDI_WRITE_MEMORY;
523 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
525 store_long_parameter (buf + 1, bp_addr);
526 store_long_parameter (buf + 5, 4);
527 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
529 buf[9] = dbt_bp_entry[0];
530 buf[10] = dbt_bp_entry[1];
531 buf[11] = dbt_bp_entry[2];
532 buf[12] = dbt_bp_entry[3];
536 buf[9] = dbt_bp_entry[3];
537 buf[10] = dbt_bp_entry[2];
538 buf[11] = dbt_bp_entry[1];
539 buf[12] = dbt_bp_entry[0];
545 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
546 store_long_parameter (buf + 1, bp_addr);
547 else if ((bp_addr & 2) == 0)
548 store_long_parameter (buf + 1, bp_addr + 2);
550 store_long_parameter (buf + 1, bp_addr - 2);
551 store_long_parameter (buf + 5, 2);
552 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
554 buf[9] = dbt_bp_entry[0];
555 buf[10] = dbt_bp_entry[1];
559 buf[9] = dbt_bp_entry[1];
560 buf[10] = dbt_bp_entry[0];
567 /* Set access breaks. */
568 for (i = 0; i < max_access_breaks; i++)
570 ab_addr = ab_address[i];
571 if (ab_addr != 0x00000000)
574 buf[0] = SDI_WRITE_MEMORY;
575 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
576 store_long_parameter (buf + 5, 4);
577 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
584 case 0: /* write watch */
587 case 1: /* read watch */
590 case 2: /* access watch */
599 case 0: /* write watch */
602 case 1: /* read watch */
605 case 2: /* access watch */
616 buf[0] = SDI_WRITE_MEMORY;
617 store_long_parameter (buf + 1, 0xffff8180 + 4 * i);
618 store_long_parameter (buf + 5, 4);
619 store_unsigned_integer (buf + 9, 4, ab_addr);
623 buf[0] = SDI_WRITE_MEMORY;
624 store_long_parameter (buf + 1, 0xffff8200 + 4 * i);
625 store_long_parameter (buf + 5, 4);
626 store_long_parameter (buf + 9, 0xffffffff);
630 buf[0] = SDI_WRITE_MEMORY;
631 store_long_parameter (buf + 1, 0xffff8280 + 4 * i);
632 store_long_parameter (buf + 5, 4);
633 store_long_parameter (buf + 9, 0x00000000);
637 buf[0] = SDI_WRITE_MEMORY;
638 store_long_parameter (buf + 1, 0xffff8300 + 4 * i);
639 store_long_parameter (buf + 5, 4);
640 store_long_parameter (buf + 9, 0x00000000);
646 buf[0] = SDI_WRITE_CPU_REG;
647 buf[1] = SDI_REG_PBP;
648 store_long_parameter (buf + 2, 0x00000000);
652 buf[0] = SDI_EXEC_CPU;
655 /* Without this, some commands which require an active target (such as kill)
656 won't work. This variable serves (at least) double duty as both the pid
657 of the target process (if it has such), and as a flag indicating that a
658 target is active. These functions should be split out into seperate
659 variables, especially since GDB will someday have a notion of debugging
660 several processes. */
661 inferior_ptid = pid_to_ptid (32);
666 /* Wait until the remote machine stops, then return,
667 storing status in STATUS just as `wait' would. */
670 gdb_cntrl_c (int signo)
673 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
678 m32r_wait (ptid_t ptid, struct target_waitstatus *status)
680 static RETSIGTYPE (*prev_sigint) ();
681 unsigned long bp_addr, pc_addr;
683 unsigned char buf[13];
688 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
690 status->kind = TARGET_WAITKIND_EXITED;
691 status->value.sig = 0;
694 prev_sigint = signal (SIGINT, gdb_cntrl_c);
697 buf[0] = SDI_WAIT_FOR_READY;
698 if (serial_write (sdi_desc, buf, 1) != 0)
699 error ("Remote connection closed");
703 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
705 error ("Remote connection closed");
707 if (c == '-') /* error */
709 status->kind = TARGET_WAITKIND_STOPPED;
710 status->value.sig = TARGET_SIGNAL_HUP;
711 return inferior_ptid;
713 else if (c == '+') /* stopped */
717 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
719 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
721 error ("Remote connection closed");
724 status->kind = TARGET_WAITKIND_STOPPED;
726 status->value.sig = TARGET_SIGNAL_INT;
728 status->value.sig = TARGET_SIGNAL_TRAP;
731 signal (SIGINT, prev_sigint);
735 /* Recover parallel bit. */
736 if (last_pc_addr != 0xffffffff)
738 buf[0] = SDI_WRITE_MEMORY;
739 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
740 store_long_parameter (buf + 1, last_pc_addr);
742 store_long_parameter (buf + 1, last_pc_addr - 1);
743 store_long_parameter (buf + 5, 1);
744 buf[9] = last_pc_addr_data[0];
746 last_pc_addr = 0xffffffff;
749 /* Breakpoints are inserted only for "next" command */
754 if (use_ib_breakpoints)
755 ib_breakpoints = max_ib_breakpoints;
759 /* Set back pc by 2 if m32r is stopped with dbt. */
760 buf[0] = SDI_READ_CPU_REG;
761 buf[1] = SDI_REG_BPC;
764 pc_addr = ntohl (val) - 2;
765 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
767 if (pc_addr == bp_address[i])
769 buf[0] = SDI_WRITE_CPU_REG;
770 buf[1] = SDI_REG_BPC;
771 store_long_parameter (buf + 2, pc_addr);
774 /* If there is a parallel instruction with +2 offset at pc
775 address, we have to take care of it later. */
776 if ((pc_addr & 0x2) != 0)
778 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
780 if ((bp_data[i][2] & 0x80) != 0)
782 last_pc_addr = pc_addr;
783 last_pc_addr_data[0] = bp_data[i][2];
784 last_pc_addr_data[1] = bp_data[i][3];
789 if ((bp_data[i][1] & 0x80) != 0)
791 last_pc_addr = pc_addr;
792 last_pc_addr_data[0] = bp_data[i][1];
793 last_pc_addr_data[1] = bp_data[i][0];
801 /* Remove ib breakpoints. */
802 for (i = 0; i < ib_breakpoints; i++)
804 if (bp_address[i] != 0xffffffff)
806 buf[0] = SDI_WRITE_MEMORY;
807 store_long_parameter (buf + 1, 0xffff8000 + 4 * i);
808 store_long_parameter (buf + 5, 4);
809 buf[9] = ib_bp_entry_disable[0];
810 buf[10] = ib_bp_entry_disable[1];
811 buf[11] = ib_bp_entry_disable[2];
812 buf[12] = ib_bp_entry_disable[3];
816 /* Remove dbt breakpoints. */
817 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
819 bp_addr = bp_address[i];
820 if (bp_addr != 0xffffffff)
823 bp_addr &= 0x7fffffff;
824 buf[0] = SDI_WRITE_MEMORY;
825 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
826 store_long_parameter (buf + 5, 4);
827 buf[9] = bp_data[i][0];
828 buf[10] = bp_data[i][1];
829 buf[11] = bp_data[i][2];
830 buf[12] = bp_data[i][3];
835 /* Remove access breaks. */
836 hit_watchpoint_addr = 0;
837 for (i = 0; i < max_access_breaks; i++)
839 if (ab_address[i] != 0x00000000)
841 buf[0] = SDI_READ_MEMORY;
842 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
843 store_long_parameter (buf + 5, 4);
844 serial_write (sdi_desc, buf, 9);
845 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
846 if (c != '-' && recv_data (buf, 4) != -1)
848 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
850 if ((buf[3] & 0x1) == 0x1)
851 hit_watchpoint_addr = ab_address[i];
855 if ((buf[0] & 0x1) == 0x1)
856 hit_watchpoint_addr = ab_address[i];
860 buf[0] = SDI_WRITE_MEMORY;
861 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
862 store_long_parameter (buf + 5, 4);
863 store_long_parameter (buf + 9, 0x00000000);
869 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
872 last_pc_addr = 0xffffffff;
874 return inferior_ptid;
877 /* Terminate the open connection to the remote debugger.
878 Use this when you want to detach and do something else
881 m32r_detach (char *args, int from_tty)
884 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
886 m32r_resume (inferior_ptid, 0, 0);
888 /* calls m32r_close to do the real work */
891 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
895 /* Return the id of register number REGNO. */
898 get_reg_id (int regno)
917 /* Read the remote registers into the block REGS. */
919 static void m32r_fetch_register (int);
922 m32r_fetch_registers (void)
926 for (regno = 0; regno < NUM_REGS; regno++)
927 m32r_fetch_register (regno);
930 /* Fetch register REGNO, or all registers if REGNO is -1.
931 Returns errno value. */
933 m32r_fetch_register (int regno)
935 unsigned long val, val2, regid;
936 unsigned char buf[2];
939 m32r_fetch_registers ();
942 char buffer[MAX_REGISTER_SIZE];
944 regid = get_reg_id (regno);
945 buf[0] = SDI_READ_CPU_REG;
951 if (regid == SDI_REG_PSW)
953 buf[0] = SDI_READ_CPU_REG;
954 buf[1] = SDI_REG_BBPSW;
956 recv_data (&val2, 4);
958 val = ((0x00c1 & val2) << 8) | ((0xc100 & val) >> 8);
962 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
965 /* We got the number the register holds, but gdb expects to see a
966 value in the target byte ordering. */
967 store_unsigned_integer (buffer, 4, val);
968 supply_register (regno, buffer);
973 /* Store the remote registers from the contents of the block REGS. */
975 static void m32r_store_register (int);
978 m32r_store_registers (void)
982 for (regno = 0; regno < NUM_REGS; regno++)
983 m32r_store_register (regno);
985 registers_changed ();
988 /* Store register REGNO, or all if REGNO == 0.
989 Return errno value. */
991 m32r_store_register (int regno)
994 ULONGEST regval, tmp;
995 unsigned char buf[6];
998 m32r_store_registers ();
1001 regcache_cooked_read_unsigned (current_regcache, regno, ®val);
1002 regid = get_reg_id (regno);
1004 if (regid == SDI_REG_PSW)
1006 unsigned long psw, bbpsw;
1008 buf[0] = SDI_READ_CPU_REG;
1009 buf[1] = SDI_REG_PSW;
1011 recv_data (&psw, 4);
1014 buf[0] = SDI_READ_CPU_REG;
1015 buf[1] = SDI_REG_BBPSW;
1017 recv_data (&bbpsw, 4);
1018 bbpsw = ntohl (bbpsw);
1020 tmp = (0x00c1 & psw) | ((0x00c1 & regval) << 8);
1021 buf[0] = SDI_WRITE_CPU_REG;
1022 buf[1] = SDI_REG_PSW;
1023 store_long_parameter (buf + 2, tmp);
1026 tmp = (0x0030 & bbpsw) | ((0xc100 & regval) >> 8);
1027 buf[0] = SDI_WRITE_CPU_REG;
1028 buf[1] = SDI_REG_BBPSW;
1029 store_long_parameter (buf + 2, tmp);
1034 buf[0] = SDI_WRITE_CPU_REG;
1036 store_long_parameter (buf + 2, regval);
1041 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1042 regno, (unsigned long) regval);
1046 /* Get ready to modify the registers array. On machines which store
1047 individual registers, this doesn't need to do anything. On machines
1048 which store all the registers in one fell swoop, this makes sure
1049 that registers contains all the registers from the program being
1053 m32r_prepare_to_store (void)
1055 /* Do nothing, since we can store individual regs */
1057 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1061 m32r_files_info (struct target_ops *target)
1063 char *file = "nothing";
1067 file = bfd_get_filename (exec_bfd);
1068 printf_filtered ("\tAttached to %s running program %s\n",
1073 /* Read/Write memory. */
1075 m32r_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
1077 struct mem_attrib *attrib, struct target_ops *target)
1079 unsigned long taddr;
1080 unsigned char buf[0x2000];
1087 if ((taddr & 0xa0000000) == 0x80000000)
1088 taddr &= 0x7fffffff;
1094 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,write)\n",
1097 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,read)\n",
1103 buf[0] = SDI_WRITE_MEMORY;
1104 store_long_parameter (buf + 1, taddr);
1105 store_long_parameter (buf + 5, len);
1108 memcpy (buf + 9, myaddr, len);
1109 ret = send_data (buf, len + 9) - 9;
1113 if (serial_write (sdi_desc, buf, 9) != 0)
1116 fprintf_unfiltered (gdb_stdlog,
1117 "m32r_xfer_memory() failed\n");
1120 ret = send_data (myaddr, len);
1125 buf[0] = SDI_READ_MEMORY;
1126 store_long_parameter (buf + 1, taddr);
1127 store_long_parameter (buf + 5, len);
1128 if (serial_write (sdi_desc, buf, 9) != 0)
1131 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1135 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1136 if (c < 0 || c == '-')
1139 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1143 ret = recv_data (myaddr, len);
1149 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1160 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1162 inferior_ptid = null_ptid;
1167 /* Clean up when a program exits.
1169 The program actually lives on in the remote processor's RAM, and may be
1170 run again without a download. Don't leave it full of breakpoint
1174 m32r_mourn_inferior (void)
1177 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1179 remove_breakpoints ();
1180 generic_mourn_inferior ();
1184 m32r_insert_breakpoint (CORE_ADDR addr, char *shadow)
1187 unsigned char buf[13];
1191 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%08lx,\"%s\")\n",
1194 if (use_ib_breakpoints)
1195 ib_breakpoints = max_ib_breakpoints;
1199 for (i = 0; i < MAX_BREAKPOINTS; i++)
1201 if (bp_address[i] == 0xffffffff)
1203 bp_address[i] = addr;
1204 if (i >= ib_breakpoints)
1206 buf[0] = SDI_READ_MEMORY;
1208 store_long_parameter (buf + 1, addr & 0xfffffffc);
1210 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1211 store_long_parameter (buf + 5, 4);
1212 serial_write (sdi_desc, buf, 9);
1213 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1215 recv_data (bp_data[i], 4);
1221 error ("Too many breakpoints");
1226 m32r_remove_breakpoint (CORE_ADDR addr, char *shadow)
1231 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%08lx,\"%s\")\n",
1234 for (i = 0; i < MAX_BREAKPOINTS; i++)
1236 if (bp_address[i] == addr)
1238 bp_address[i] = 0xffffffff;
1247 m32r_load (char *args, int from_tty)
1249 struct cleanup *old_chain;
1256 time_t start_time, end_time; /* Start and end times of download */
1257 unsigned long data_count; /* Number of bytes transferred to memory */
1259 static RETSIGTYPE (*prev_sigint) ();
1261 /* for direct tcp connections, we can do a fast binary download */
1266 while (*args != '\000')
1270 while (isspace (*args))
1275 while ((*args != '\000') && !isspace (*args))
1278 if (*args != '\000')
1283 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1285 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1288 error ("Unknown option `%s'", arg);
1292 filename = get_exec_file (1);
1294 pbfd = bfd_openr (filename, gnutarget);
1297 perror_with_name (filename);
1300 old_chain = make_cleanup_bfd_close (pbfd);
1302 if (!bfd_check_format (pbfd, bfd_object))
1303 error ("\"%s\" is not an object file: %s", filename,
1304 bfd_errmsg (bfd_get_error ()));
1306 start_time = time (NULL);
1310 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1312 for (section = pbfd->sections; section; section = section->next)
1314 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1316 bfd_vma section_address;
1317 bfd_size_type section_size;
1321 section_address = bfd_section_lma (pbfd, section);
1322 section_size = bfd_get_section_size_before_reloc (section);
1326 if ((section_address & 0xa0000000) == 0x80000000)
1327 section_address &= 0x7fffffff;
1331 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1332 bfd_get_section_name (pbfd, section),
1333 section_address, (int) section_size);
1337 data_count += section_size;
1340 while (section_size > 0)
1342 char unsigned buf[0x1000 + 9];
1345 count = min (section_size, 0x1000);
1347 buf[0] = SDI_WRITE_MEMORY;
1348 store_long_parameter (buf + 1, section_address);
1349 store_long_parameter (buf + 5, count);
1351 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1352 if (send_data (buf, count + 9) <= 0)
1353 error ("Error while downloading %s section.",
1354 bfd_get_section_name (pbfd, section));
1358 printf_unfiltered (".");
1361 printf_unfiltered ("\n");
1364 gdb_flush (gdb_stdout);
1367 section_address += count;
1369 section_size -= count;
1375 if (!quiet && !interrupted)
1377 printf_unfiltered ("done.\n");
1378 gdb_flush (gdb_stdout);
1384 printf_unfiltered ("Interrupted.\n");
1390 signal (SIGINT, prev_sigint);
1392 end_time = time (NULL);
1394 /* Make the PC point at the start address */
1396 write_pc (bfd_get_start_address (exec_bfd));
1398 inferior_ptid = null_ptid; /* No process now */
1400 /* This is necessary because many things were based on the PC at the time
1401 that we attached to the monitor, which is no longer valid now that we
1402 have loaded new code (and just changed the PC). Another way to do this
1403 might be to call normal_stop, except that the stack may not be valid,
1404 and things would get horribly confused... */
1406 clear_symtab_users ();
1410 entry = bfd_get_start_address (pbfd);
1413 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename, entry);
1416 print_transfer_performance (gdb_stdout, data_count, 0,
1417 end_time - start_time);
1419 do_cleanups (old_chain);
1425 unsigned char buf[1];
1428 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1430 buf[0] = SDI_STOP_CPU;
1437 /* Tell whether this target can support a hardware breakpoint.
1438 This implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1441 m32r_can_use_hardware_watchpoint (void)
1443 return max_access_breaks;
1446 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1447 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1451 m32r_set_watchpoint (CORE_ADDR addr, int len, int type)
1456 fprintf_unfiltered (gdb_stdlog, "m32r_set_watchpoint(%08lx,%d,%d)\n",
1459 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1461 if (ab_address[i] == 0x00000000)
1463 ab_address[i] = addr;
1470 error ("Too many watchpoints");
1475 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1480 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1483 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1485 if (ab_address[i] == addr)
1487 ab_address[i] = 0x00000000;
1496 m32r_stopped_data_address (void)
1498 return hit_watchpoint_addr;
1502 m32r_stopped_by_watchpoint (void)
1504 return (hit_watchpoint_addr != 0x00000000);
1509 sdireset_command (char *args, int from_tty)
1511 unsigned char buf[1];
1514 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1519 inferior_ptid = null_ptid;
1524 sdistatus_command (char *args, int from_tty)
1526 unsigned char buf[4096];
1530 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1535 buf[0] = SDI_STATUS;
1537 for (i = 0; i < 4096; i++)
1539 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1547 printf_filtered ("%s", buf);
1552 debug_chaos_command (char *args, int from_tty)
1554 unsigned char buf[3];
1556 buf[0] = SDI_SET_ATTR;
1557 buf[1] = SDI_ATTR_CACHE;
1558 buf[2] = SDI_CACHE_TYPE_CHAOS;
1564 use_debug_dma_command (char *args, int from_tty)
1566 unsigned char buf[3];
1568 buf[0] = SDI_SET_ATTR;
1569 buf[1] = SDI_ATTR_MEM_ACCESS;
1570 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1575 use_mon_code_command (char *args, int from_tty)
1577 unsigned char buf[3];
1579 buf[0] = SDI_SET_ATTR;
1580 buf[1] = SDI_ATTR_MEM_ACCESS;
1581 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1587 use_ib_breakpoints_command (char *args, int from_tty)
1589 use_ib_breakpoints = 1;
1593 use_dbt_breakpoints_command (char *args, int from_tty)
1595 use_ib_breakpoints = 0;
1599 /* Define the target subroutine names */
1601 struct target_ops m32r_ops;
1604 init_m32r_ops (void)
1606 m32r_ops.to_shortname = "m32rsdi";
1607 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1608 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1609 m32r_ops.to_open = m32r_open;
1610 m32r_ops.to_close = m32r_close;
1611 m32r_ops.to_detach = m32r_detach;
1612 m32r_ops.to_resume = m32r_resume;
1613 m32r_ops.to_wait = m32r_wait;
1614 m32r_ops.to_fetch_registers = m32r_fetch_register;
1615 m32r_ops.to_store_registers = m32r_store_register;
1616 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
1617 m32r_ops.to_xfer_memory = m32r_xfer_memory;
1618 m32r_ops.to_files_info = m32r_files_info;
1619 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1620 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
1621 m32r_ops.to_kill = m32r_kill;
1622 m32r_ops.to_load = m32r_load;
1623 m32r_ops.to_create_inferior = m32r_create_inferior;
1624 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1625 m32r_ops.to_stop = m32r_stop;
1626 m32r_ops.to_stratum = process_stratum;
1627 m32r_ops.to_has_all_memory = 1;
1628 m32r_ops.to_has_memory = 1;
1629 m32r_ops.to_has_stack = 1;
1630 m32r_ops.to_has_registers = 1;
1631 m32r_ops.to_has_execution = 1;
1632 m32r_ops.to_magic = OPS_MAGIC;
1636 extern initialize_file_ftype _initialize_remote_m32r;
1639 _initialize_remote_m32r (void)
1645 /* Initialize breakpoints. */
1646 for (i = 0; i < MAX_BREAKPOINTS; i++)
1647 bp_address[i] = 0xffffffff;
1649 /* Initialize access breaks. */
1650 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1651 ab_address[i] = 0x00000000;
1653 add_target (&m32r_ops);
1655 add_com ("sdireset", class_obscure, sdireset_command,
1656 "Reset SDI connection.");
1658 add_com ("sdistatus", class_obscure, sdistatus_command,
1659 "Show status of SDI connection.");
1661 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1662 "Debug M32R/Chaos.");
1664 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1665 "Use debug DMA mem access.");
1666 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1667 "Use mon code mem access.");
1669 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1670 "Set breakpoints by IB break.");
1671 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1672 "Set breakpoints by dbt.");