1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003-2012 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 3 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, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_string.h"
30 #include "gdbthread.h"
36 #include <netinet/in.h>
38 #include <sys/types.h>
46 /* Descriptor for I/O to remote machine. */
48 static struct serial *sdi_desc = NULL;
50 #define SDI_TIMEOUT 30
55 static char chip_name[64];
58 static unsigned long last_pc_addr = 0xffffffff;
59 static unsigned char last_pc_addr_data[2];
61 static int mmu_on = 0;
63 static int use_ib_breakpoints = 1;
65 #define MAX_BREAKPOINTS 1024
66 static int max_ib_breakpoints;
67 static unsigned long bp_address[MAX_BREAKPOINTS];
68 static unsigned char bp_data[MAX_BREAKPOINTS][4];
71 static const unsigned char dbt_bp_entry[] = {
72 0x10, 0xe0, 0x70, 0x00
75 #define MAX_ACCESS_BREAKS 4
76 static int max_access_breaks;
77 static unsigned long ab_address[MAX_ACCESS_BREAKS];
78 static unsigned int ab_type[MAX_ACCESS_BREAKS];
79 static unsigned int ab_size[MAX_ACCESS_BREAKS];
80 static CORE_ADDR hit_watchpoint_addr = 0;
82 static int interrupted = 0;
84 /* Forward data declarations */
85 extern struct target_ops m32r_ops;
87 /* This is the ptid we use while we're connected to the remote. Its
88 value is arbitrary, as the target doesn't have a notion of
89 processes or threads, but we need something non-null to place in
91 static ptid_t remote_m32r_ptid;
97 #define SDI_READ_CPU_REG 4
98 #define SDI_WRITE_CPU_REG 5
99 #define SDI_READ_MEMORY 6
100 #define SDI_WRITE_MEMORY 7
101 #define SDI_EXEC_CPU 8
102 #define SDI_STOP_CPU 9
103 #define SDI_WAIT_FOR_READY 10
104 #define SDI_GET_ATTR 11
105 #define SDI_SET_ATTR 12
106 #define SDI_STATUS 13
109 #define SDI_ATTR_NAME 1
110 #define SDI_ATTR_BRK 2
111 #define SDI_ATTR_ABRK 3
112 #define SDI_ATTR_CACHE 4
113 #define SDI_CACHE_TYPE_M32102 0
114 #define SDI_CACHE_TYPE_CHAOS 1
115 #define SDI_ATTR_MEM_ACCESS 5
116 #define SDI_MEM_ACCESS_DEBUG_DMA 0
117 #define SDI_MEM_ACCESS_MON_CODE 1
130 #define SDI_REG_R10 10
131 #define SDI_REG_R11 11
132 #define SDI_REG_R12 12
133 #define SDI_REG_FP 13
134 #define SDI_REG_LR 14
135 #define SDI_REG_SP 15
136 #define SDI_REG_PSW 16
137 #define SDI_REG_CBR 17
138 #define SDI_REG_SPI 18
139 #define SDI_REG_SPU 19
140 #define SDI_REG_CR4 20
141 #define SDI_REG_EVB 21
142 #define SDI_REG_BPC 22
143 #define SDI_REG_CR7 23
144 #define SDI_REG_BBPSW 24
145 #define SDI_REG_CR9 25
146 #define SDI_REG_CR10 26
147 #define SDI_REG_CR11 27
148 #define SDI_REG_CR12 28
149 #define SDI_REG_WR 29
150 #define SDI_REG_BBPC 30
151 #define SDI_REG_PBP 31
152 #define SDI_REG_ACCH 32
153 #define SDI_REG_ACCL 33
154 #define SDI_REG_ACC1H 34
155 #define SDI_REG_ACC1L 35
158 /* Low level communication functions. */
160 /* Check an ack packet from the target. */
169 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
174 if (c != '+') /* error */
180 /* Send data to the target and check an ack packet. */
182 send_data (void *buf, int len)
189 if (serial_write (sdi_desc, buf, len) != 0)
192 if (get_ack () == -1)
198 /* Receive data from the target. */
200 recv_data (void *buf, int len)
210 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
215 ((unsigned char *) buf)[total++] = c;
221 /* Store unsigned long parameter on packet. */
223 store_long_parameter (void *buf, long val)
226 memcpy (buf, &val, 4);
230 send_cmd (unsigned char cmd)
232 unsigned char buf[1];
235 return send_data (buf, 1);
239 send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
241 unsigned char buf[2];
245 return send_data (buf, 2);
249 send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
251 unsigned char buf[6];
255 store_long_parameter (buf + 2, arg2);
256 return send_data (buf, 6);
260 send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
263 unsigned char buf[13];
266 store_long_parameter (buf + 1, arg1);
267 store_long_parameter (buf + 5, arg2);
268 store_long_parameter (buf + 9, arg3);
269 return send_data (buf, 13);
273 recv_char_data (void)
282 recv_long_data (void)
291 /* Check if MMU is on. */
293 check_mmu_status (void)
297 /* Read PC address. */
298 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
300 val = recv_long_data ();
301 if ((val & 0xc0000000) == 0x80000000)
307 /* Read EVB address. */
308 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
310 val = recv_long_data ();
311 if ((val & 0xc0000000) == 0x80000000)
321 /* This is called not only when we first attach, but also when the
322 user types "run" after having attached. */
324 m32r_create_inferior (struct target_ops *ops, char *execfile,
325 char *args, char **env, int from_tty)
330 error (_("Cannot pass arguments to remote STDEBUG process"));
332 if (execfile == 0 || exec_bfd == 0)
333 error (_("No executable file specified"));
336 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
339 entry_pt = bfd_get_start_address (exec_bfd);
341 /* The "process" (board) is already stopped awaiting our commands, and
342 the program is already downloaded. We just set its PC and go. */
344 clear_proceed_status ();
346 /* Tell wait_for_inferior that we've started a new process. */
347 init_wait_for_inferior ();
349 /* Set up the "saved terminal modes" of the inferior
350 based on what modes we are starting it with. */
351 target_terminal_init ();
353 /* Install inferior's terminal modes. */
354 target_terminal_inferior ();
356 regcache_write_pc (get_current_regcache (), entry_pt);
359 /* Open a connection to a remote debugger.
360 NAME is the filename used for communication. */
363 m32r_open (char *args, int from_tty)
365 struct hostent *host_ent;
366 struct sockaddr_in server_addr;
367 char *port_str, hostname[256];
373 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
375 target_preopen (from_tty);
377 push_target (&m32r_ops);
380 sprintf (hostname, "localhost:%d", SDIPORT);
383 port_str = strchr (args, ':');
384 if (port_str == NULL)
385 sprintf (hostname, "%s:%d", args, SDIPORT);
387 strcpy (hostname, args);
390 sdi_desc = serial_open (hostname);
392 error (_("Connection refused."));
394 if (get_ack () == -1)
395 error (_("Cannot connect to SDI target."));
397 if (send_cmd (SDI_OPEN) == -1)
398 error (_("Cannot connect to SDI target."));
400 /* Get maximum number of ib breakpoints. */
401 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
402 max_ib_breakpoints = recv_char_data ();
404 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
406 /* Initialize breakpoints. */
407 for (i = 0; i < MAX_BREAKPOINTS; i++)
408 bp_address[i] = 0xffffffff;
410 /* Get maximum number of access breaks. */
411 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
412 max_access_breaks = recv_char_data ();
414 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
416 /* Initialize access breask. */
417 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
418 ab_address[i] = 0x00000000;
422 /* Get the name of chip on target board. */
423 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
424 recv_data (chip_name, 64);
427 printf_filtered ("Remote %s connected to %s\n", target_shortname,
431 /* Close out all files and local state before this target loses control. */
434 m32r_close (int quitting)
437 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
441 send_cmd (SDI_CLOSE);
442 serial_close (sdi_desc);
446 inferior_ptid = null_ptid;
447 delete_thread_silent (remote_m32r_ptid);
451 /* Tell the remote machine to resume. */
454 m32r_resume (struct target_ops *ops,
455 ptid_t ptid, int step, enum target_signal sig)
457 unsigned long pc_addr, bp_addr, ab_addr;
459 unsigned char buf[13];
465 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
467 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
472 pc_addr = regcache_read_pc (get_current_regcache ());
474 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
476 /* At pc address there is a parallel instruction with +2 offset,
477 so we have to make it a serial instruction or avoid it. */
478 if (pc_addr == last_pc_addr)
480 /* Avoid a parallel nop. */
481 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
484 /* Now we can forget this instruction. */
485 last_pc_addr = 0xffffffff;
487 /* Clear a parallel bit. */
490 buf[0] = SDI_WRITE_MEMORY;
491 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
492 store_long_parameter (buf + 1, pc_addr);
494 store_long_parameter (buf + 1, pc_addr - 1);
495 store_long_parameter (buf + 5, 1);
496 buf[9] = last_pc_addr_data[0] & 0x7f;
502 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
509 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
514 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
517 if (use_ib_breakpoints)
518 ib_breakpoints = max_ib_breakpoints;
522 /* Set ib breakpoints. */
523 for (i = 0; i < ib_breakpoints; i++)
525 bp_addr = bp_address[i];
527 if (bp_addr == 0xffffffff)
531 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
532 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
535 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
538 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
541 /* Set dbt breakpoints. */
542 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
544 bp_addr = bp_address[i];
546 if (bp_addr == 0xffffffff)
550 bp_addr &= 0x7fffffff;
552 /* Write DBT instruction. */
553 buf[0] = SDI_WRITE_MEMORY;
554 store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
555 store_long_parameter (buf + 5, 4);
556 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
558 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
560 buf[9] = dbt_bp_entry[0];
561 buf[10] = dbt_bp_entry[1];
562 buf[11] = dbt_bp_entry[2];
563 buf[12] = dbt_bp_entry[3];
567 buf[9] = dbt_bp_entry[3];
568 buf[10] = dbt_bp_entry[2];
569 buf[11] = dbt_bp_entry[1];
570 buf[12] = dbt_bp_entry[0];
575 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
577 if ((bp_addr & 2) == 0)
579 buf[9] = dbt_bp_entry[0];
580 buf[10] = dbt_bp_entry[1];
581 buf[11] = bp_data[i][2] & 0x7f;
582 buf[12] = bp_data[i][3];
586 buf[9] = bp_data[i][0];
587 buf[10] = bp_data[i][1];
588 buf[11] = dbt_bp_entry[0];
589 buf[12] = dbt_bp_entry[1];
594 if ((bp_addr & 2) == 0)
596 buf[9] = bp_data[i][0];
597 buf[10] = bp_data[i][1] & 0x7f;
598 buf[11] = dbt_bp_entry[1];
599 buf[12] = dbt_bp_entry[0];
603 buf[9] = dbt_bp_entry[1];
604 buf[10] = dbt_bp_entry[0];
605 buf[11] = bp_data[i][2];
606 buf[12] = bp_data[i][3];
613 /* Set access breaks. */
614 for (i = 0; i < max_access_breaks; i++)
616 ab_addr = ab_address[i];
618 if (ab_addr == 0x00000000)
622 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
626 case 0: /* write watch */
627 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
630 case 1: /* read watch */
631 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
634 case 2: /* access watch */
635 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
644 case 0: /* write watch */
645 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
648 case 1: /* read watch */
649 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
652 case 2: /* access watch */
653 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
660 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
663 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
667 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
671 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
675 /* Resume program. */
676 send_cmd (SDI_EXEC_CPU);
678 /* Without this, some commands which require an active target (such as kill)
679 won't work. This variable serves (at least) double duty as both the pid
680 of the target process (if it has such), and as a flag indicating that a
681 target is active. These functions should be split out into seperate
682 variables, especially since GDB will someday have a notion of debugging
683 several processes. */
684 inferior_ptid = remote_m32r_ptid;
685 add_thread_silent (remote_m32r_ptid);
690 /* Wait until the remote machine stops, then return,
691 storing status in STATUS just as `wait' would. */
694 gdb_cntrl_c (int signo)
697 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
702 m32r_wait (struct target_ops *ops,
703 ptid_t ptid, struct target_waitstatus *status, int options)
705 static RETSIGTYPE (*prev_sigint) ();
706 unsigned long bp_addr, pc_addr;
709 unsigned char buf[13];
714 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
716 status->kind = TARGET_WAITKIND_EXITED;
717 status->value.sig = TARGET_SIGNAL_0;
720 prev_sigint = signal (SIGINT, gdb_cntrl_c);
722 /* Wait for ready. */
723 buf[0] = SDI_WAIT_FOR_READY;
724 if (serial_write (sdi_desc, buf, 1) != 0)
725 error (_("Remote connection closed"));
729 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
731 error (_("Remote connection closed"));
733 if (c == '-') /* error */
735 status->kind = TARGET_WAITKIND_STOPPED;
736 status->value.sig = TARGET_SIGNAL_HUP;
737 return inferior_ptid;
739 else if (c == '+') /* stopped */
743 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
745 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
747 error (_("Remote connection closed"));
750 status->kind = TARGET_WAITKIND_STOPPED;
752 status->value.sig = TARGET_SIGNAL_INT;
754 status->value.sig = TARGET_SIGNAL_TRAP;
757 signal (SIGINT, prev_sigint);
761 /* Recover parallel bit. */
762 if (last_pc_addr != 0xffffffff)
764 buf[0] = SDI_WRITE_MEMORY;
765 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
766 store_long_parameter (buf + 1, last_pc_addr);
768 store_long_parameter (buf + 1, last_pc_addr - 1);
769 store_long_parameter (buf + 5, 1);
770 buf[9] = last_pc_addr_data[0];
772 last_pc_addr = 0xffffffff;
775 if (use_ib_breakpoints)
776 ib_breakpoints = max_ib_breakpoints;
780 /* Set back pc by 2 if m32r is stopped with dbt. */
781 last_pc_addr = 0xffffffff;
782 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
783 pc_addr = recv_long_data () - 2;
784 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
786 if (pc_addr == bp_address[i])
788 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
790 /* If there is a parallel instruction with +2 offset at pc
791 address, we have to take care of it later. */
792 if ((pc_addr & 0x2) != 0)
794 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
796 if ((bp_data[i][2] & 0x80) != 0)
798 last_pc_addr = pc_addr;
799 last_pc_addr_data[0] = bp_data[i][2];
800 last_pc_addr_data[1] = bp_data[i][3];
805 if ((bp_data[i][1] & 0x80) != 0)
807 last_pc_addr = pc_addr;
808 last_pc_addr_data[0] = bp_data[i][1];
809 last_pc_addr_data[1] = bp_data[i][0];
817 /* Remove ib breakpoints. */
818 for (i = 0; i < ib_breakpoints; i++)
820 if (bp_address[i] != 0xffffffff)
821 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
824 /* Remove dbt breakpoints. */
825 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
827 bp_addr = bp_address[i];
828 if (bp_addr != 0xffffffff)
831 bp_addr &= 0x7fffffff;
832 buf[0] = SDI_WRITE_MEMORY;
833 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
834 store_long_parameter (buf + 5, 4);
835 buf[9] = bp_data[i][0];
836 buf[10] = bp_data[i][1];
837 buf[11] = bp_data[i][2];
838 buf[12] = bp_data[i][3];
843 /* Remove access breaks. */
844 hit_watchpoint_addr = 0;
845 for (i = 0; i < max_access_breaks; i++)
847 if (ab_address[i] != 0x00000000)
849 buf[0] = SDI_READ_MEMORY;
850 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
851 store_long_parameter (buf + 5, 4);
852 serial_write (sdi_desc, buf, 9);
853 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
854 if (c != '-' && recv_data (buf, 4) != -1)
856 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
858 if ((buf[3] & 0x1) == 0x1)
859 hit_watchpoint_addr = ab_address[i];
863 if ((buf[0] & 0x1) == 0x1)
864 hit_watchpoint_addr = ab_address[i];
868 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
874 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
876 return inferior_ptid;
879 /* Terminate the open connection to the remote debugger.
880 Use this when you want to detach and do something else
883 m32r_detach (struct target_ops *ops, char *args, int from_tty)
886 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
888 m32r_resume (ops, inferior_ptid, 0, TARGET_SIGNAL_0);
890 /* Calls m32r_close to do the real work. */
893 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
897 /* Return the id of register number REGNO. */
900 get_reg_id (int regno)
919 /* Fetch register REGNO, or all registers if REGNO is -1.
920 Returns errno value. */
922 m32r_fetch_register (struct target_ops *ops,
923 struct regcache *regcache, int regno)
925 struct gdbarch *gdbarch = get_regcache_arch (regcache);
926 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
927 unsigned long val, val2, regid;
932 regno < gdbarch_num_regs (get_regcache_arch (regcache));
934 m32r_fetch_register (ops, regcache, regno);
938 char buffer[MAX_REGISTER_SIZE];
940 regid = get_reg_id (regno);
941 send_one_arg_cmd (SDI_READ_CPU_REG, regid);
942 val = recv_long_data ();
944 if (regid == SDI_REG_PSW)
946 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
947 val2 = recv_long_data ();
948 val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
952 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
955 /* We got the number the register holds, but gdb expects to see a
956 value in the target byte ordering. */
957 store_unsigned_integer (buffer, 4, byte_order, val);
958 regcache_raw_supply (regcache, regno, buffer);
963 /* Store register REGNO, or all if REGNO == 0.
964 Return errno value. */
966 m32r_store_register (struct target_ops *ops,
967 struct regcache *regcache, int regno)
970 ULONGEST regval, tmp;
975 regno < gdbarch_num_regs (get_regcache_arch (regcache));
977 m32r_store_register (ops, regcache, regno);
981 regcache_cooked_read_unsigned (regcache, regno, ®val);
982 regid = get_reg_id (regno);
984 if (regid == SDI_REG_PSW)
986 unsigned long psw, bbpsw;
988 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
989 psw = recv_long_data ();
991 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
992 bbpsw = recv_long_data ();
994 tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
995 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
997 tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
998 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
1002 send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
1006 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1007 regno, (unsigned long) regval);
1011 /* Get ready to modify the registers array. On machines which store
1012 individual registers, this doesn't need to do anything. On machines
1013 which store all the registers in one fell swoop, this makes sure
1014 that registers contains all the registers from the program being
1018 m32r_prepare_to_store (struct regcache *regcache)
1020 /* Do nothing, since we can store individual regs. */
1022 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1026 m32r_files_info (struct target_ops *target)
1028 const char *file = "nothing";
1032 file = bfd_get_filename (exec_bfd);
1033 printf_filtered ("\tAttached to %s running program %s\n",
1038 /* Read/Write memory. */
1040 m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
1042 struct mem_attrib *attrib, struct target_ops *target)
1044 unsigned long taddr;
1045 unsigned char buf[0x2000];
1052 if ((taddr & 0xa0000000) == 0x80000000)
1053 taddr &= 0x7fffffff;
1059 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,write)\n",
1060 paddress (target_gdbarch, memaddr), len);
1062 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,read)\n",
1063 paddress (target_gdbarch, memaddr), len);
1068 buf[0] = SDI_WRITE_MEMORY;
1069 store_long_parameter (buf + 1, taddr);
1070 store_long_parameter (buf + 5, len);
1073 memcpy (buf + 9, myaddr, len);
1074 ret = send_data (buf, len + 9) - 9;
1078 if (serial_write (sdi_desc, buf, 9) != 0)
1081 fprintf_unfiltered (gdb_stdlog,
1082 "m32r_xfer_memory() failed\n");
1085 ret = send_data (myaddr, len);
1090 buf[0] = SDI_READ_MEMORY;
1091 store_long_parameter (buf + 1, taddr);
1092 store_long_parameter (buf + 5, len);
1093 if (serial_write (sdi_desc, buf, 9) != 0)
1096 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1100 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1101 if (c < 0 || c == '-')
1104 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1108 ret = recv_data (myaddr, len);
1114 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1122 m32r_kill (struct target_ops *ops)
1125 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1127 inferior_ptid = null_ptid;
1128 delete_thread_silent (remote_m32r_ptid);
1133 /* Clean up when a program exits.
1135 The program actually lives on in the remote processor's RAM, and may be
1136 run again without a download. Don't leave it full of breakpoint
1140 m32r_mourn_inferior (struct target_ops *ops)
1143 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1145 remove_breakpoints ();
1146 generic_mourn_inferior ();
1150 m32r_insert_breakpoint (struct gdbarch *gdbarch,
1151 struct bp_target_info *bp_tgt)
1153 CORE_ADDR addr = bp_tgt->placed_address;
1155 unsigned char buf[13];
1159 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
1160 paddress (gdbarch, addr));
1162 if (use_ib_breakpoints)
1163 ib_breakpoints = max_ib_breakpoints;
1167 for (i = 0; i < MAX_BREAKPOINTS; i++)
1169 if (bp_address[i] == 0xffffffff)
1171 bp_address[i] = addr;
1172 if (i >= ib_breakpoints)
1174 buf[0] = SDI_READ_MEMORY;
1176 store_long_parameter (buf + 1, addr & 0xfffffffc);
1178 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1179 store_long_parameter (buf + 5, 4);
1180 serial_write (sdi_desc, buf, 9);
1181 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1183 recv_data (bp_data[i], 4);
1189 error (_("Too many breakpoints"));
1194 m32r_remove_breakpoint (struct gdbarch *gdbarch,
1195 struct bp_target_info *bp_tgt)
1197 CORE_ADDR addr = bp_tgt->placed_address;
1201 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
1202 paddress (gdbarch, addr));
1204 for (i = 0; i < MAX_BREAKPOINTS; i++)
1206 if (bp_address[i] == addr)
1208 bp_address[i] = 0xffffffff;
1217 m32r_load (char *args, int from_tty)
1219 struct cleanup *old_chain;
1226 struct timeval start_time, end_time;
1227 unsigned long data_count; /* Number of bytes transferred to memory. */
1229 static RETSIGTYPE (*prev_sigint) ();
1231 /* for direct tcp connections, we can do a fast binary download. */
1236 while (*args != '\000')
1240 while (isspace (*args))
1245 while ((*args != '\000') && !isspace (*args))
1248 if (*args != '\000')
1253 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1255 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1258 error (_("Unknown option `%s'"), arg);
1262 filename = get_exec_file (1);
1264 pbfd = bfd_openr (filename, gnutarget);
1267 perror_with_name (filename);
1270 old_chain = make_cleanup_bfd_close (pbfd);
1272 if (!bfd_check_format (pbfd, bfd_object))
1273 error (_("\"%s\" is not an object file: %s"), filename,
1274 bfd_errmsg (bfd_get_error ()));
1276 gettimeofday (&start_time, NULL);
1280 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1282 for (section = pbfd->sections; section; section = section->next)
1284 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1286 bfd_vma section_address;
1287 bfd_size_type section_size;
1291 section_address = bfd_section_lma (pbfd, section);
1292 section_size = bfd_get_section_size (section);
1296 if ((section_address & 0xa0000000) == 0x80000000)
1297 section_address &= 0x7fffffff;
1301 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1302 bfd_get_section_name (pbfd, section),
1303 (unsigned long) section_address,
1304 (int) section_size);
1308 data_count += section_size;
1311 while (section_size > 0)
1313 char unsigned buf[0x1000 + 9];
1316 count = min (section_size, 0x1000);
1318 buf[0] = SDI_WRITE_MEMORY;
1319 store_long_parameter (buf + 1, section_address);
1320 store_long_parameter (buf + 5, count);
1322 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1323 if (send_data (buf, count + 9) <= 0)
1324 error (_("Error while downloading %s section."),
1325 bfd_get_section_name (pbfd, section));
1329 printf_unfiltered (".");
1332 printf_unfiltered ("\n");
1335 gdb_flush (gdb_stdout);
1338 section_address += count;
1340 section_size -= count;
1346 if (!quiet && !interrupted)
1348 printf_unfiltered ("done.\n");
1349 gdb_flush (gdb_stdout);
1355 printf_unfiltered ("Interrupted.\n");
1361 signal (SIGINT, prev_sigint);
1363 gettimeofday (&end_time, NULL);
1365 /* Make the PC point at the start address. */
1367 regcache_write_pc (get_current_regcache (),
1368 bfd_get_start_address (exec_bfd));
1370 inferior_ptid = null_ptid; /* No process now. */
1371 delete_thread_silent (remote_m32r_ptid);
1373 /* This is necessary because many things were based on the PC at the time
1374 that we attached to the monitor, which is no longer valid now that we
1375 have loaded new code (and just changed the PC). Another way to do this
1376 might be to call normal_stop, except that the stack may not be valid,
1377 and things would get horribly confused... */
1379 clear_symtab_users (0);
1383 entry = bfd_get_start_address (pbfd);
1386 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
1387 (unsigned long) entry);
1390 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
1393 do_cleanups (old_chain);
1397 m32r_stop (ptid_t ptid)
1400 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1402 send_cmd (SDI_STOP_CPU);
1408 /* Tell whether this target can support a hardware breakpoint. CNT
1409 is the number of hardware breakpoints already installed. This
1410 implements the target_can_use_hardware_watchpoint macro. */
1413 m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
1415 return sdi_desc != NULL && cnt < max_access_breaks;
1418 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1419 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1423 m32r_insert_watchpoint (CORE_ADDR addr, int len, int type,
1424 struct expression *cond)
1429 fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
1430 paddress (target_gdbarch, addr), len, type);
1432 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1434 if (ab_address[i] == 0x00000000)
1436 ab_address[i] = addr;
1443 error (_("Too many watchpoints"));
1448 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type,
1449 struct expression *cond)
1454 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
1455 paddress (target_gdbarch, addr), len, type);
1457 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1459 if (ab_address[i] == addr)
1461 ab_address[i] = 0x00000000;
1470 m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1474 if (hit_watchpoint_addr != 0x00000000)
1476 *addr_p = hit_watchpoint_addr;
1483 m32r_stopped_by_watchpoint (void)
1487 return m32r_stopped_data_address (¤t_target, &addr);
1490 /* Check to see if a thread is still alive. */
1493 m32r_thread_alive (struct target_ops *ops, ptid_t ptid)
1495 if (ptid_equal (ptid, remote_m32r_ptid))
1496 /* The main task is always alive. */
1502 /* Convert a thread ID to a string. Returns the string in a static
1506 m32r_pid_to_str (struct target_ops *ops, ptid_t ptid)
1508 static char buf[64];
1510 if (ptid_equal (remote_m32r_ptid, ptid))
1512 xsnprintf (buf, sizeof buf, "Thread <main>");
1516 return normal_pid_to_str (ptid);
1520 sdireset_command (char *args, int from_tty)
1523 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1525 send_cmd (SDI_OPEN);
1527 inferior_ptid = null_ptid;
1528 delete_thread_silent (remote_m32r_ptid);
1533 sdistatus_command (char *args, int from_tty)
1535 unsigned char buf[4096];
1539 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1544 send_cmd (SDI_STATUS);
1545 for (i = 0; i < 4096; i++)
1547 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1555 printf_filtered ("%s", buf);
1560 debug_chaos_command (char *args, int from_tty)
1562 unsigned char buf[3];
1564 buf[0] = SDI_SET_ATTR;
1565 buf[1] = SDI_ATTR_CACHE;
1566 buf[2] = SDI_CACHE_TYPE_CHAOS;
1572 use_debug_dma_command (char *args, int from_tty)
1574 unsigned char buf[3];
1576 buf[0] = SDI_SET_ATTR;
1577 buf[1] = SDI_ATTR_MEM_ACCESS;
1578 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1583 use_mon_code_command (char *args, int from_tty)
1585 unsigned char buf[3];
1587 buf[0] = SDI_SET_ATTR;
1588 buf[1] = SDI_ATTR_MEM_ACCESS;
1589 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1595 use_ib_breakpoints_command (char *args, int from_tty)
1597 use_ib_breakpoints = 1;
1601 use_dbt_breakpoints_command (char *args, int from_tty)
1603 use_ib_breakpoints = 0;
1607 m32r_return_one (struct target_ops *target)
1612 /* Implementation of the to_has_execution method. */
1615 m32r_has_execution (struct target_ops *target, ptid_t the_ptid)
1620 /* Define the target subroutine names. */
1622 struct target_ops m32r_ops;
1625 init_m32r_ops (void)
1627 m32r_ops.to_shortname = "m32rsdi";
1628 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1629 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1630 m32r_ops.to_open = m32r_open;
1631 m32r_ops.to_close = m32r_close;
1632 m32r_ops.to_detach = m32r_detach;
1633 m32r_ops.to_resume = m32r_resume;
1634 m32r_ops.to_wait = m32r_wait;
1635 m32r_ops.to_fetch_registers = m32r_fetch_register;
1636 m32r_ops.to_store_registers = m32r_store_register;
1637 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
1638 m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
1639 m32r_ops.to_files_info = m32r_files_info;
1640 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1641 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
1642 m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1643 m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1644 m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1645 m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1646 m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
1647 m32r_ops.to_kill = m32r_kill;
1648 m32r_ops.to_load = m32r_load;
1649 m32r_ops.to_create_inferior = m32r_create_inferior;
1650 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1651 m32r_ops.to_stop = m32r_stop;
1652 m32r_ops.to_log_command = serial_log_command;
1653 m32r_ops.to_thread_alive = m32r_thread_alive;
1654 m32r_ops.to_pid_to_str = m32r_pid_to_str;
1655 m32r_ops.to_stratum = process_stratum;
1656 m32r_ops.to_has_all_memory = m32r_return_one;
1657 m32r_ops.to_has_memory = m32r_return_one;
1658 m32r_ops.to_has_stack = m32r_return_one;
1659 m32r_ops.to_has_registers = m32r_return_one;
1660 m32r_ops.to_has_execution = m32r_has_execution;
1661 m32r_ops.to_magic = OPS_MAGIC;
1665 extern initialize_file_ftype _initialize_remote_m32r;
1668 _initialize_remote_m32r (void)
1674 /* Initialize breakpoints. */
1675 for (i = 0; i < MAX_BREAKPOINTS; i++)
1676 bp_address[i] = 0xffffffff;
1678 /* Initialize access breaks. */
1679 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1680 ab_address[i] = 0x00000000;
1682 add_target (&m32r_ops);
1684 add_com ("sdireset", class_obscure, sdireset_command,
1685 _("Reset SDI connection."));
1687 add_com ("sdistatus", class_obscure, sdistatus_command,
1688 _("Show status of SDI connection."));
1690 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1691 _("Debug M32R/Chaos."));
1693 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1694 _("Use debug DMA mem access."));
1695 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1696 _("Use mon code mem access."));
1698 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1699 _("Set breakpoints by IB break."));
1700 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1701 _("Set breakpoints by dbt."));
1703 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1705 remote_m32r_ptid = ptid_build (42000, 0, 42000);