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
,
273 error ("Cannot pass arguments to remote STDEBUG process");
275 if (execfile
== 0 || exec_bfd
== 0)
276 error ("No executable file specified");
279 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
282 entry_pt
= bfd_get_start_address (exec_bfd
);
284 /* The "process" (board) is already stopped awaiting our commands, and
285 the program is already downloaded. We just set its PC and go. */
287 clear_proceed_status ();
289 /* Tell wait_for_inferior that we've started a new process. */
290 init_wait_for_inferior ();
292 /* Set up the "saved terminal modes" of the inferior
293 based on what modes we are starting it with. */
294 target_terminal_init ();
296 /* Install inferior's terminal modes. */
297 target_terminal_inferior ();
299 proceed (entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
302 /* Open a connection to a remote debugger.
303 NAME is the filename used for communication. */
306 m32r_open (char *args
, int from_tty
)
308 struct hostent
*host_ent
;
309 struct sockaddr_in server_addr
;
310 char *port_str
, hostname
[256];
312 unsigned char buf
[2];
317 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
319 target_preopen (from_tty
);
321 push_target (&m32r_ops
);
324 sprintf (hostname
, "localhost:%d", SDIPORT
);
327 port_str
= strchr (args
, ':');
328 if (port_str
== NULL
)
329 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
331 strcpy (hostname
, args
);
334 sdi_desc
= serial_open (hostname
);
336 error ("Connection refused\n");
338 if (get_ack () == -1)
339 error ("Cannot connect to SDI target\n");
342 if (send_data (buf
, 1) == -1)
343 error ("Cannot connect to SDI target\n");
345 /* Get maximum number of ib breakpoints */
346 buf
[0] = SDI_GET_ATTR
;
347 buf
[1] = SDI_ATTR_BRK
;
350 max_ib_breakpoints
= buf
[0];
352 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
354 /* Initialize breakpoints. */
355 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
356 bp_address
[i
] = 0xffffffff;
358 /* Get maximum number of access breaks. */
359 buf
[0] = SDI_GET_ATTR
;
360 buf
[1] = SDI_ATTR_ABRK
;
363 max_access_breaks
= buf
[0];
365 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
367 /* Initialize access breask. */
368 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
369 ab_address
[i
] = 0x00000000;
373 /* Get the name of chip on target board. */
374 buf
[0] = SDI_GET_ATTR
;
375 buf
[1] = SDI_ATTR_NAME
;
377 recv_data (chip_name
, 64);
380 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
384 /* Close out all files and local state before this target loses control. */
387 m32r_close (int quitting
)
389 unsigned char buf
[1];
392 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
398 serial_close (sdi_desc
);
402 inferior_ptid
= null_ptid
;
406 /* Tell the remote machine to resume. */
409 m32r_resume (ptid_t ptid
, int step
, enum target_signal sig
)
411 unsigned long pc_addr
, bp_addr
, ab_addr
;
412 unsigned char buf
[13];
418 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
420 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
425 pc_addr
= read_pc ();
427 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
429 /* At pc address there is a parallel instruction with +2 offset,
430 so we have to make it a serial instruction or avoid it. */
431 if (pc_addr
== last_pc_addr
)
433 /* Avoid a parallel nop. */
434 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
437 /* Now we can forget this instruction. */
438 last_pc_addr
= 0xffffffff;
440 /* Clear a parallel bit. */
443 buf
[0] = SDI_WRITE_MEMORY
;
444 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
445 store_long_parameter (buf
+ 1, pc_addr
);
447 store_long_parameter (buf
+ 1, pc_addr
- 1);
448 store_long_parameter (buf
+ 5, 1);
449 buf
[9] = last_pc_addr_data
[0] & 0x7f;
455 buf
[0] = SDI_WRITE_CPU_REG
;
456 buf
[1] = SDI_REG_BPC
;
457 store_long_parameter (buf
+ 2, pc_addr
);
465 buf
[0] = SDI_WRITE_CPU_REG
;
466 buf
[1] = SDI_REG_PBP
;
467 store_long_parameter (buf
+ 2, pc_addr
| 1);
474 if (use_ib_breakpoints
)
475 ib_breakpoints
= max_ib_breakpoints
;
479 /* Set ib breakpoints. */
480 for (i
= 0; i
< ib_breakpoints
; i
++)
482 bp_addr
= bp_address
[i
];
483 if (bp_addr
!= 0xffffffff && bp_addr
!= pc_addr
)
486 buf
[0] = SDI_WRITE_MEMORY
;
487 store_long_parameter (buf
+ 1, 0xffff8000 + 4 * i
);
488 store_long_parameter (buf
+ 5, 4);
489 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
491 buf
[9] = ib_bp_entry_enable
[0];
492 buf
[10] = ib_bp_entry_enable
[1];
493 buf
[11] = ib_bp_entry_enable
[2];
494 buf
[12] = ib_bp_entry_enable
[3];
498 buf
[9] = ib_bp_entry_enable
[3];
499 buf
[10] = ib_bp_entry_enable
[2];
500 buf
[11] = ib_bp_entry_enable
[1];
501 buf
[12] = ib_bp_entry_enable
[0];
505 buf
[0] = SDI_WRITE_MEMORY
;
506 store_long_parameter (buf
+ 1, 0xffff8080 + 4 * i
);
507 store_long_parameter (buf
+ 5, 4);
508 store_unsigned_integer (buf
+ 9, 4, bp_addr
);
513 /* Set dbt breakpoints. */
514 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
516 bp_addr
= bp_address
[i
];
517 if (bp_addr
!= 0xffffffff && bp_addr
!= pc_addr
)
520 bp_addr
&= 0x7fffffff;
522 /* Write DBT instruction. */
523 buf
[0] = SDI_WRITE_MEMORY
;
524 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
526 store_long_parameter (buf
+ 1, bp_addr
);
527 store_long_parameter (buf
+ 5, 4);
528 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
530 buf
[9] = dbt_bp_entry
[0];
531 buf
[10] = dbt_bp_entry
[1];
532 buf
[11] = dbt_bp_entry
[2];
533 buf
[12] = dbt_bp_entry
[3];
537 buf
[9] = dbt_bp_entry
[3];
538 buf
[10] = dbt_bp_entry
[2];
539 buf
[11] = dbt_bp_entry
[1];
540 buf
[12] = dbt_bp_entry
[0];
546 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
547 store_long_parameter (buf
+ 1, bp_addr
);
548 else if ((bp_addr
& 2) == 0)
549 store_long_parameter (buf
+ 1, bp_addr
+ 2);
551 store_long_parameter (buf
+ 1, bp_addr
- 2);
552 store_long_parameter (buf
+ 5, 2);
553 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
555 buf
[9] = dbt_bp_entry
[0];
556 buf
[10] = dbt_bp_entry
[1];
560 buf
[9] = dbt_bp_entry
[1];
561 buf
[10] = dbt_bp_entry
[0];
568 /* Set access breaks. */
569 for (i
= 0; i
< max_access_breaks
; i
++)
571 ab_addr
= ab_address
[i
];
572 if (ab_addr
!= 0x00000000)
575 buf
[0] = SDI_WRITE_MEMORY
;
576 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
577 store_long_parameter (buf
+ 5, 4);
578 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
585 case 0: /* write watch */
588 case 1: /* read watch */
591 case 2: /* access watch */
600 case 0: /* write watch */
603 case 1: /* read watch */
606 case 2: /* access watch */
617 buf
[0] = SDI_WRITE_MEMORY
;
618 store_long_parameter (buf
+ 1, 0xffff8180 + 4 * i
);
619 store_long_parameter (buf
+ 5, 4);
620 store_unsigned_integer (buf
+ 9, 4, ab_addr
);
624 buf
[0] = SDI_WRITE_MEMORY
;
625 store_long_parameter (buf
+ 1, 0xffff8200 + 4 * i
);
626 store_long_parameter (buf
+ 5, 4);
627 store_long_parameter (buf
+ 9, 0xffffffff);
631 buf
[0] = SDI_WRITE_MEMORY
;
632 store_long_parameter (buf
+ 1, 0xffff8280 + 4 * i
);
633 store_long_parameter (buf
+ 5, 4);
634 store_long_parameter (buf
+ 9, 0x00000000);
638 buf
[0] = SDI_WRITE_MEMORY
;
639 store_long_parameter (buf
+ 1, 0xffff8300 + 4 * i
);
640 store_long_parameter (buf
+ 5, 4);
641 store_long_parameter (buf
+ 9, 0x00000000);
647 buf
[0] = SDI_WRITE_CPU_REG
;
648 buf
[1] = SDI_REG_PBP
;
649 store_long_parameter (buf
+ 2, 0x00000000);
653 buf
[0] = SDI_EXEC_CPU
;
656 /* Without this, some commands which require an active target (such as kill)
657 won't work. This variable serves (at least) double duty as both the pid
658 of the target process (if it has such), and as a flag indicating that a
659 target is active. These functions should be split out into seperate
660 variables, especially since GDB will someday have a notion of debugging
661 several processes. */
662 inferior_ptid
= pid_to_ptid (32);
667 /* Wait until the remote machine stops, then return,
668 storing status in STATUS just as `wait' would. */
671 gdb_cntrl_c (int signo
)
674 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
679 m32r_wait (ptid_t ptid
, struct target_waitstatus
*status
)
681 static RETSIGTYPE (*prev_sigint
) ();
682 unsigned long bp_addr
, pc_addr
;
684 unsigned char buf
[13];
689 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
691 status
->kind
= TARGET_WAITKIND_EXITED
;
692 status
->value
.sig
= 0;
695 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
698 buf
[0] = SDI_WAIT_FOR_READY
;
699 if (serial_write (sdi_desc
, buf
, 1) != 0)
700 error ("Remote connection closed");
704 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
706 error ("Remote connection closed");
708 if (c
== '-') /* error */
710 status
->kind
= TARGET_WAITKIND_STOPPED
;
711 status
->value
.sig
= TARGET_SIGNAL_HUP
;
712 return inferior_ptid
;
714 else if (c
== '+') /* stopped */
718 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
720 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
722 error ("Remote connection closed");
725 status
->kind
= TARGET_WAITKIND_STOPPED
;
727 status
->value
.sig
= TARGET_SIGNAL_INT
;
729 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
732 signal (SIGINT
, prev_sigint
);
736 /* Recover parallel bit. */
737 if (last_pc_addr
!= 0xffffffff)
739 buf
[0] = SDI_WRITE_MEMORY
;
740 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
741 store_long_parameter (buf
+ 1, last_pc_addr
);
743 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
744 store_long_parameter (buf
+ 5, 1);
745 buf
[9] = last_pc_addr_data
[0];
747 last_pc_addr
= 0xffffffff;
750 /* Breakpoints are inserted only for "next" command */
755 if (use_ib_breakpoints
)
756 ib_breakpoints
= max_ib_breakpoints
;
760 /* Set back pc by 2 if m32r is stopped with dbt. */
761 buf
[0] = SDI_READ_CPU_REG
;
762 buf
[1] = SDI_REG_BPC
;
765 pc_addr
= ntohl (val
) - 2;
766 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
768 if (pc_addr
== bp_address
[i
])
770 buf
[0] = SDI_WRITE_CPU_REG
;
771 buf
[1] = SDI_REG_BPC
;
772 store_long_parameter (buf
+ 2, pc_addr
);
775 /* If there is a parallel instruction with +2 offset at pc
776 address, we have to take care of it later. */
777 if ((pc_addr
& 0x2) != 0)
779 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
781 if ((bp_data
[i
][2] & 0x80) != 0)
783 last_pc_addr
= pc_addr
;
784 last_pc_addr_data
[0] = bp_data
[i
][2];
785 last_pc_addr_data
[1] = bp_data
[i
][3];
790 if ((bp_data
[i
][1] & 0x80) != 0)
792 last_pc_addr
= pc_addr
;
793 last_pc_addr_data
[0] = bp_data
[i
][1];
794 last_pc_addr_data
[1] = bp_data
[i
][0];
802 /* Remove ib breakpoints. */
803 for (i
= 0; i
< ib_breakpoints
; i
++)
805 if (bp_address
[i
] != 0xffffffff)
807 buf
[0] = SDI_WRITE_MEMORY
;
808 store_long_parameter (buf
+ 1, 0xffff8000 + 4 * i
);
809 store_long_parameter (buf
+ 5, 4);
810 buf
[9] = ib_bp_entry_disable
[0];
811 buf
[10] = ib_bp_entry_disable
[1];
812 buf
[11] = ib_bp_entry_disable
[2];
813 buf
[12] = ib_bp_entry_disable
[3];
817 /* Remove dbt breakpoints. */
818 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
820 bp_addr
= bp_address
[i
];
821 if (bp_addr
!= 0xffffffff)
824 bp_addr
&= 0x7fffffff;
825 buf
[0] = SDI_WRITE_MEMORY
;
826 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
827 store_long_parameter (buf
+ 5, 4);
828 buf
[9] = bp_data
[i
][0];
829 buf
[10] = bp_data
[i
][1];
830 buf
[11] = bp_data
[i
][2];
831 buf
[12] = bp_data
[i
][3];
836 /* Remove access breaks. */
837 hit_watchpoint_addr
= 0;
838 for (i
= 0; i
< max_access_breaks
; i
++)
840 if (ab_address
[i
] != 0x00000000)
842 buf
[0] = SDI_READ_MEMORY
;
843 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
844 store_long_parameter (buf
+ 5, 4);
845 serial_write (sdi_desc
, buf
, 9);
846 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
847 if (c
!= '-' && recv_data (buf
, 4) != -1)
849 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
851 if ((buf
[3] & 0x1) == 0x1)
852 hit_watchpoint_addr
= ab_address
[i
];
856 if ((buf
[0] & 0x1) == 0x1)
857 hit_watchpoint_addr
= ab_address
[i
];
861 buf
[0] = SDI_WRITE_MEMORY
;
862 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
863 store_long_parameter (buf
+ 5, 4);
864 store_long_parameter (buf
+ 9, 0x00000000);
870 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
873 last_pc_addr
= 0xffffffff;
875 return inferior_ptid
;
878 /* Terminate the open connection to the remote debugger.
879 Use this when you want to detach and do something else
882 m32r_detach (char *args
, int from_tty
)
885 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
887 m32r_resume (inferior_ptid
, 0, 0);
889 /* calls m32r_close to do the real work */
892 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
896 /* Return the id of register number REGNO. */
899 get_reg_id (int regno
)
918 /* Read the remote registers into the block REGS. */
920 static void m32r_fetch_register (int);
923 m32r_fetch_registers (void)
927 for (regno
= 0; regno
< NUM_REGS
; regno
++)
928 m32r_fetch_register (regno
);
931 /* Fetch register REGNO, or all registers if REGNO is -1.
932 Returns errno value. */
934 m32r_fetch_register (int regno
)
936 unsigned long val
, val2
, regid
;
937 unsigned char buf
[2];
940 m32r_fetch_registers ();
943 char buffer
[MAX_REGISTER_SIZE
];
945 regid
= get_reg_id (regno
);
946 buf
[0] = SDI_READ_CPU_REG
;
952 if (regid
== SDI_REG_PSW
)
954 buf
[0] = SDI_READ_CPU_REG
;
955 buf
[1] = SDI_REG_BBPSW
;
957 recv_data (&val2
, 4);
959 val
= ((0x00c1 & val2
) << 8) | ((0xc100 & val
) >> 8);
963 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
966 /* We got the number the register holds, but gdb expects to see a
967 value in the target byte ordering. */
968 store_unsigned_integer (buffer
, 4, val
);
969 supply_register (regno
, buffer
);
974 /* Store the remote registers from the contents of the block REGS. */
976 static void m32r_store_register (int);
979 m32r_store_registers (void)
983 for (regno
= 0; regno
< NUM_REGS
; regno
++)
984 m32r_store_register (regno
);
986 registers_changed ();
989 /* Store register REGNO, or all if REGNO == 0.
990 Return errno value. */
992 m32r_store_register (int regno
)
995 ULONGEST regval
, tmp
;
996 unsigned char buf
[6];
999 m32r_store_registers ();
1002 regcache_cooked_read_unsigned (current_regcache
, regno
, ®val
);
1003 regid
= get_reg_id (regno
);
1005 if (regid
== SDI_REG_PSW
)
1007 unsigned long psw
, bbpsw
;
1009 buf
[0] = SDI_READ_CPU_REG
;
1010 buf
[1] = SDI_REG_PSW
;
1012 recv_data (&psw
, 4);
1015 buf
[0] = SDI_READ_CPU_REG
;
1016 buf
[1] = SDI_REG_BBPSW
;
1018 recv_data (&bbpsw
, 4);
1019 bbpsw
= ntohl (bbpsw
);
1021 tmp
= (0x00c1 & psw
) | ((0x00c1 & regval
) << 8);
1022 buf
[0] = SDI_WRITE_CPU_REG
;
1023 buf
[1] = SDI_REG_PSW
;
1024 store_long_parameter (buf
+ 2, tmp
);
1027 tmp
= (0x0030 & bbpsw
) | ((0xc100 & regval
) >> 8);
1028 buf
[0] = SDI_WRITE_CPU_REG
;
1029 buf
[1] = SDI_REG_BBPSW
;
1030 store_long_parameter (buf
+ 2, tmp
);
1035 buf
[0] = SDI_WRITE_CPU_REG
;
1037 store_long_parameter (buf
+ 2, regval
);
1042 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1043 regno
, (unsigned long) regval
);
1047 /* Get ready to modify the registers array. On machines which store
1048 individual registers, this doesn't need to do anything. On machines
1049 which store all the registers in one fell swoop, this makes sure
1050 that registers contains all the registers from the program being
1054 m32r_prepare_to_store (void)
1056 /* Do nothing, since we can store individual regs */
1058 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1062 m32r_files_info (struct target_ops
*target
)
1064 char *file
= "nothing";
1068 file
= bfd_get_filename (exec_bfd
);
1069 printf_filtered ("\tAttached to %s running program %s\n",
1074 /* Read/Write memory. */
1076 m32r_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
1078 struct mem_attrib
*attrib
, struct target_ops
*target
)
1080 unsigned long taddr
;
1081 unsigned char buf
[0x2000];
1088 if ((taddr
& 0xa0000000) == 0x80000000)
1089 taddr
&= 0x7fffffff;
1095 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,write)\n",
1098 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,read)\n",
1104 buf
[0] = SDI_WRITE_MEMORY
;
1105 store_long_parameter (buf
+ 1, taddr
);
1106 store_long_parameter (buf
+ 5, len
);
1109 memcpy (buf
+ 9, myaddr
, len
);
1110 ret
= send_data (buf
, len
+ 9) - 9;
1114 if (serial_write (sdi_desc
, buf
, 9) != 0)
1117 fprintf_unfiltered (gdb_stdlog
,
1118 "m32r_xfer_memory() failed\n");
1121 ret
= send_data (myaddr
, len
);
1126 buf
[0] = SDI_READ_MEMORY
;
1127 store_long_parameter (buf
+ 1, taddr
);
1128 store_long_parameter (buf
+ 5, len
);
1129 if (serial_write (sdi_desc
, buf
, 9) != 0)
1132 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1136 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1137 if (c
< 0 || c
== '-')
1140 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1144 ret
= recv_data (myaddr
, len
);
1150 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1161 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1163 inferior_ptid
= null_ptid
;
1168 /* Clean up when a program exits.
1170 The program actually lives on in the remote processor's RAM, and may be
1171 run again without a download. Don't leave it full of breakpoint
1175 m32r_mourn_inferior (void)
1178 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1180 remove_breakpoints ();
1181 generic_mourn_inferior ();
1185 m32r_insert_breakpoint (CORE_ADDR addr
, char *shadow
)
1188 unsigned char buf
[13];
1192 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%08lx,\"%s\")\n",
1195 if (use_ib_breakpoints
)
1196 ib_breakpoints
= max_ib_breakpoints
;
1200 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1202 if (bp_address
[i
] == 0xffffffff)
1204 bp_address
[i
] = addr
;
1205 if (i
>= ib_breakpoints
)
1207 buf
[0] = SDI_READ_MEMORY
;
1209 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1211 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1212 store_long_parameter (buf
+ 5, 4);
1213 serial_write (sdi_desc
, buf
, 9);
1214 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1216 recv_data (bp_data
[i
], 4);
1222 error ("Too many breakpoints");
1227 m32r_remove_breakpoint (CORE_ADDR addr
, char *shadow
)
1232 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%08lx,\"%s\")\n",
1235 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1237 if (bp_address
[i
] == addr
)
1239 bp_address
[i
] = 0xffffffff;
1248 m32r_load (char *args
, int from_tty
)
1250 struct cleanup
*old_chain
;
1257 time_t start_time
, end_time
; /* Start and end times of download */
1258 unsigned long data_count
; /* Number of bytes transferred to memory */
1260 static RETSIGTYPE (*prev_sigint
) ();
1262 /* for direct tcp connections, we can do a fast binary download */
1267 while (*args
!= '\000')
1271 while (isspace (*args
))
1276 while ((*args
!= '\000') && !isspace (*args
))
1279 if (*args
!= '\000')
1284 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1286 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1289 error ("Unknown option `%s'", arg
);
1293 filename
= get_exec_file (1);
1295 pbfd
= bfd_openr (filename
, gnutarget
);
1298 perror_with_name (filename
);
1301 old_chain
= make_cleanup_bfd_close (pbfd
);
1303 if (!bfd_check_format (pbfd
, bfd_object
))
1304 error ("\"%s\" is not an object file: %s", filename
,
1305 bfd_errmsg (bfd_get_error ()));
1307 start_time
= time (NULL
);
1311 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1313 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1315 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1317 bfd_vma section_address
;
1318 bfd_size_type section_size
;
1322 section_address
= bfd_section_lma (pbfd
, section
);
1323 section_size
= bfd_get_section_size_before_reloc (section
);
1327 if ((section_address
& 0xa0000000) == 0x80000000)
1328 section_address
&= 0x7fffffff;
1332 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1333 bfd_get_section_name (pbfd
, section
),
1334 section_address
, (int) section_size
);
1338 data_count
+= section_size
;
1341 while (section_size
> 0)
1343 char unsigned buf
[0x1000 + 9];
1346 count
= min (section_size
, 0x1000);
1348 buf
[0] = SDI_WRITE_MEMORY
;
1349 store_long_parameter (buf
+ 1, section_address
);
1350 store_long_parameter (buf
+ 5, count
);
1352 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1353 if (send_data (buf
, count
+ 9) <= 0)
1354 error ("Error while downloading %s section.",
1355 bfd_get_section_name (pbfd
, section
));
1359 printf_unfiltered (".");
1362 printf_unfiltered ("\n");
1365 gdb_flush (gdb_stdout
);
1368 section_address
+= count
;
1370 section_size
-= count
;
1376 if (!quiet
&& !interrupted
)
1378 printf_unfiltered ("done.\n");
1379 gdb_flush (gdb_stdout
);
1385 printf_unfiltered ("Interrupted.\n");
1391 signal (SIGINT
, prev_sigint
);
1393 end_time
= time (NULL
);
1395 /* Make the PC point at the start address */
1397 write_pc (bfd_get_start_address (exec_bfd
));
1399 inferior_ptid
= null_ptid
; /* No process now */
1401 /* This is necessary because many things were based on the PC at the time
1402 that we attached to the monitor, which is no longer valid now that we
1403 have loaded new code (and just changed the PC). Another way to do this
1404 might be to call normal_stop, except that the stack may not be valid,
1405 and things would get horribly confused... */
1407 clear_symtab_users ();
1411 entry
= bfd_get_start_address (pbfd
);
1414 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
, entry
);
1417 print_transfer_performance (gdb_stdout
, data_count
, 0,
1418 end_time
- start_time
);
1420 do_cleanups (old_chain
);
1426 unsigned char buf
[1];
1429 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1431 buf
[0] = SDI_STOP_CPU
;
1438 /* Tell whether this target can support a hardware breakpoint. CNT
1439 is the number of hardware breakpoints already installed. This
1440 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1443 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1445 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1448 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1449 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1453 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
1458 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%08lx,%d,%d)\n",
1461 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1463 if (ab_address
[i
] == 0x00000000)
1465 ab_address
[i
] = addr
;
1472 error ("Too many watchpoints");
1477 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
1482 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1485 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1487 if (ab_address
[i
] == addr
)
1489 ab_address
[i
] = 0x00000000;
1498 m32r_stopped_data_address (void)
1500 return hit_watchpoint_addr
;
1504 m32r_stopped_by_watchpoint (void)
1506 return (hit_watchpoint_addr
!= 0x00000000);
1511 sdireset_command (char *args
, int from_tty
)
1513 unsigned char buf
[1];
1516 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1521 inferior_ptid
= null_ptid
;
1526 sdistatus_command (char *args
, int from_tty
)
1528 unsigned char buf
[4096];
1532 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1537 buf
[0] = SDI_STATUS
;
1539 for (i
= 0; i
< 4096; i
++)
1541 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1549 printf_filtered ("%s", buf
);
1554 debug_chaos_command (char *args
, int from_tty
)
1556 unsigned char buf
[3];
1558 buf
[0] = SDI_SET_ATTR
;
1559 buf
[1] = SDI_ATTR_CACHE
;
1560 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1566 use_debug_dma_command (char *args
, int from_tty
)
1568 unsigned char buf
[3];
1570 buf
[0] = SDI_SET_ATTR
;
1571 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1572 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1577 use_mon_code_command (char *args
, int from_tty
)
1579 unsigned char buf
[3];
1581 buf
[0] = SDI_SET_ATTR
;
1582 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1583 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1589 use_ib_breakpoints_command (char *args
, int from_tty
)
1591 use_ib_breakpoints
= 1;
1595 use_dbt_breakpoints_command (char *args
, int from_tty
)
1597 use_ib_breakpoints
= 0;
1601 /* Define the target subroutine names */
1603 struct target_ops m32r_ops
;
1606 init_m32r_ops (void)
1608 m32r_ops
.to_shortname
= "m32rsdi";
1609 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1610 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1611 m32r_ops
.to_open
= m32r_open
;
1612 m32r_ops
.to_close
= m32r_close
;
1613 m32r_ops
.to_detach
= m32r_detach
;
1614 m32r_ops
.to_resume
= m32r_resume
;
1615 m32r_ops
.to_wait
= m32r_wait
;
1616 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1617 m32r_ops
.to_store_registers
= m32r_store_register
;
1618 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1619 m32r_ops
.to_xfer_memory
= m32r_xfer_memory
;
1620 m32r_ops
.to_files_info
= m32r_files_info
;
1621 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1622 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1623 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1624 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1625 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1626 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1627 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1628 m32r_ops
.to_kill
= m32r_kill
;
1629 m32r_ops
.to_load
= m32r_load
;
1630 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1631 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1632 m32r_ops
.to_stop
= m32r_stop
;
1633 m32r_ops
.to_stratum
= process_stratum
;
1634 m32r_ops
.to_has_all_memory
= 1;
1635 m32r_ops
.to_has_memory
= 1;
1636 m32r_ops
.to_has_stack
= 1;
1637 m32r_ops
.to_has_registers
= 1;
1638 m32r_ops
.to_has_execution
= 1;
1639 m32r_ops
.to_magic
= OPS_MAGIC
;
1643 extern initialize_file_ftype _initialize_remote_m32r
;
1646 _initialize_remote_m32r (void)
1652 /* Initialize breakpoints. */
1653 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1654 bp_address
[i
] = 0xffffffff;
1656 /* Initialize access breaks. */
1657 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1658 ab_address
[i
] = 0x00000000;
1660 add_target (&m32r_ops
);
1662 add_com ("sdireset", class_obscure
, sdireset_command
,
1663 "Reset SDI connection.");
1665 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1666 "Show status of SDI connection.");
1668 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1669 "Debug M32R/Chaos.");
1671 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1672 "Use debug DMA mem access.");
1673 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1674 "Use mon code mem access.");
1676 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1677 "Set breakpoints by IB break.");
1678 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1679 "Set breakpoints by dbt.");