Update
[gdb.git] / gdb / remote-m32r-sdi.c
blob5a8e0c752ae1827a3eed3f35f71accb34c27c2ae
1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Renesas Technology Co.
7 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "defs.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "inferior.h"
28 #include "target.h"
29 #include "regcache.h"
30 #include "gdb_string.h"
31 #include <ctype.h>
32 #include <signal.h>
33 #ifdef __MINGW32__
34 #include <winsock.h>
35 #else
36 #include <netinet/in.h>
37 #endif
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <signal.h>
41 #include <time.h>
44 #include "serial.h"
46 /* Descriptor for I/O to remote machine. */
48 static struct serial *sdi_desc = NULL;
50 #define SDI_TIMEOUT 30
53 #define SDIPORT 3232
55 static char chip_name[64];
57 static int step_mode;
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];
70 /* dbt -> nop */
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;
88 /* Commands */
89 #define SDI_OPEN 1
90 #define SDI_CLOSE 2
91 #define SDI_RELEASE 3
92 #define SDI_READ_CPU_REG 4
93 #define SDI_WRITE_CPU_REG 5
94 #define SDI_READ_MEMORY 6
95 #define SDI_WRITE_MEMORY 7
96 #define SDI_EXEC_CPU 8
97 #define SDI_STOP_CPU 9
98 #define SDI_WAIT_FOR_READY 10
99 #define SDI_GET_ATTR 11
100 #define SDI_SET_ATTR 12
101 #define SDI_STATUS 13
103 /* Attributes */
104 #define SDI_ATTR_NAME 1
105 #define SDI_ATTR_BRK 2
106 #define SDI_ATTR_ABRK 3
107 #define SDI_ATTR_CACHE 4
108 #define SDI_CACHE_TYPE_M32102 0
109 #define SDI_CACHE_TYPE_CHAOS 1
110 #define SDI_ATTR_MEM_ACCESS 5
111 #define SDI_MEM_ACCESS_DEBUG_DMA 0
112 #define SDI_MEM_ACCESS_MON_CODE 1
114 /* Registers */
115 #define SDI_REG_R0 0
116 #define SDI_REG_R1 1
117 #define SDI_REG_R2 2
118 #define SDI_REG_R3 3
119 #define SDI_REG_R4 4
120 #define SDI_REG_R5 5
121 #define SDI_REG_R6 6
122 #define SDI_REG_R7 7
123 #define SDI_REG_R8 8
124 #define SDI_REG_R9 9
125 #define SDI_REG_R10 10
126 #define SDI_REG_R11 11
127 #define SDI_REG_R12 12
128 #define SDI_REG_FP 13
129 #define SDI_REG_LR 14
130 #define SDI_REG_SP 15
131 #define SDI_REG_PSW 16
132 #define SDI_REG_CBR 17
133 #define SDI_REG_SPI 18
134 #define SDI_REG_SPU 19
135 #define SDI_REG_CR4 20
136 #define SDI_REG_EVB 21
137 #define SDI_REG_BPC 22
138 #define SDI_REG_CR7 23
139 #define SDI_REG_BBPSW 24
140 #define SDI_REG_CR9 25
141 #define SDI_REG_CR10 26
142 #define SDI_REG_CR11 27
143 #define SDI_REG_CR12 28
144 #define SDI_REG_WR 29
145 #define SDI_REG_BBPC 30
146 #define SDI_REG_PBP 31
147 #define SDI_REG_ACCH 32
148 #define SDI_REG_ACCL 33
149 #define SDI_REG_ACC1H 34
150 #define SDI_REG_ACC1L 35
153 /* Low level communication functions */
155 /* Check an ack packet from the target */
156 static int
157 get_ack (void)
159 int c;
161 if (!sdi_desc)
162 return -1;
164 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
166 if (c < 0)
167 return -1;
169 if (c != '+') /* error */
170 return -1;
172 return 0;
175 /* Send data to the target and check an ack packet */
176 static int
177 send_data (void *buf, int len)
179 int ret;
181 if (!sdi_desc)
182 return -1;
184 if (serial_write (sdi_desc, buf, len) != 0)
185 return -1;
187 if (get_ack () == -1)
188 return -1;
190 return len;
193 /* Receive data from the target */
194 static int
195 recv_data (void *buf, int len)
197 int total = 0;
198 int c;
200 if (!sdi_desc)
201 return -1;
203 while (total < len)
205 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
207 if (c < 0)
208 return -1;
210 ((unsigned char *) buf)[total++] = c;
213 return len;
216 /* Store unsigned long parameter on packet */
217 static void
218 store_long_parameter (void *buf, long val)
220 val = htonl (val);
221 memcpy (buf, &val, 4);
224 static int
225 send_cmd (unsigned char cmd)
227 unsigned char buf[1];
228 buf[0] = cmd;
229 return send_data (buf, 1);
232 static int
233 send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
235 unsigned char buf[2];
236 buf[0] = cmd;
237 buf[1] = arg1;
238 return send_data (buf, 2);
241 static int
242 send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
244 unsigned char buf[6];
245 buf[0] = cmd;
246 buf[1] = arg1;
247 store_long_parameter (buf + 2, arg2);
248 return send_data (buf, 6);
251 static int
252 send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
253 unsigned long arg3)
255 unsigned char buf[13];
256 buf[0] = cmd;
257 store_long_parameter (buf + 1, arg1);
258 store_long_parameter (buf + 5, arg2);
259 store_long_parameter (buf + 9, arg3);
260 return send_data (buf, 13);
263 static unsigned char
264 recv_char_data (void)
266 unsigned char val;
267 recv_data (&val, 1);
268 return val;
271 static unsigned long
272 recv_long_data (void)
274 unsigned long val;
275 recv_data (&val, 4);
276 return ntohl (val);
280 /* Check if MMU is on */
281 static void
282 check_mmu_status (void)
284 unsigned long val;
286 /* Read PC address */
287 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
288 return;
289 val = recv_long_data ();
290 if ((val & 0xc0000000) == 0x80000000)
292 mmu_on = 1;
293 return;
296 /* Read EVB address */
297 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
298 return;
299 val = recv_long_data ();
300 if ((val & 0xc0000000) == 0x80000000)
302 mmu_on = 1;
303 return;
306 mmu_on = 0;
310 /* This is called not only when we first attach, but also when the
311 user types "run" after having attached. */
312 static void
313 m32r_create_inferior (char *execfile, char *args, char **env, int from_tty)
315 CORE_ADDR entry_pt;
317 if (args && *args)
318 error (_("Cannot pass arguments to remote STDEBUG process"));
320 if (execfile == 0 || exec_bfd == 0)
321 error (_("No executable file specified"));
323 if (remote_debug)
324 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
325 args);
327 entry_pt = bfd_get_start_address (exec_bfd);
329 /* The "process" (board) is already stopped awaiting our commands, and
330 the program is already downloaded. We just set its PC and go. */
332 clear_proceed_status ();
334 /* Tell wait_for_inferior that we've started a new process. */
335 init_wait_for_inferior ();
337 /* Set up the "saved terminal modes" of the inferior
338 based on what modes we are starting it with. */
339 target_terminal_init ();
341 /* Install inferior's terminal modes. */
342 target_terminal_inferior ();
344 write_pc (entry_pt);
347 /* Open a connection to a remote debugger.
348 NAME is the filename used for communication. */
350 static void
351 m32r_open (char *args, int from_tty)
353 struct hostent *host_ent;
354 struct sockaddr_in server_addr;
355 char *port_str, hostname[256];
356 int port;
357 int i, n;
358 int yes = 1;
360 if (remote_debug)
361 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
363 target_preopen (from_tty);
365 push_target (&m32r_ops);
367 if (args == NULL)
368 sprintf (hostname, "localhost:%d", SDIPORT);
369 else
371 port_str = strchr (args, ':');
372 if (port_str == NULL)
373 sprintf (hostname, "%s:%d", args, SDIPORT);
374 else
375 strcpy (hostname, args);
378 sdi_desc = serial_open (hostname);
379 if (!sdi_desc)
380 error (_("Connection refused."));
382 if (get_ack () == -1)
383 error (_("Cannot connect to SDI target."));
385 if (send_cmd (SDI_OPEN) == -1)
386 error (_("Cannot connect to SDI target."));
388 /* Get maximum number of ib breakpoints */
389 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
390 max_ib_breakpoints = recv_char_data ();
391 if (remote_debug)
392 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
394 /* Initialize breakpoints. */
395 for (i = 0; i < MAX_BREAKPOINTS; i++)
396 bp_address[i] = 0xffffffff;
398 /* Get maximum number of access breaks. */
399 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
400 max_access_breaks = recv_char_data ();
401 if (remote_debug)
402 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
404 /* Initialize access breask. */
405 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
406 ab_address[i] = 0x00000000;
408 check_mmu_status ();
410 /* Get the name of chip on target board. */
411 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
412 recv_data (chip_name, 64);
414 if (from_tty)
415 printf_filtered ("Remote %s connected to %s\n", target_shortname,
416 chip_name);
419 /* Close out all files and local state before this target loses control. */
421 static void
422 m32r_close (int quitting)
424 if (remote_debug)
425 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
427 if (sdi_desc)
429 send_cmd (SDI_CLOSE);
430 serial_close (sdi_desc);
431 sdi_desc = NULL;
434 inferior_ptid = null_ptid;
435 return;
438 /* Tell the remote machine to resume. */
440 static void
441 m32r_resume (ptid_t ptid, int step, enum target_signal sig)
443 unsigned long pc_addr, bp_addr, ab_addr;
444 int ib_breakpoints;
445 unsigned char buf[13];
446 int i;
448 if (remote_debug)
450 if (step)
451 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
452 else
453 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
456 check_mmu_status ();
458 pc_addr = read_pc ();
459 if (remote_debug)
460 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
462 /* At pc address there is a parallel instruction with +2 offset,
463 so we have to make it a serial instruction or avoid it. */
464 if (pc_addr == last_pc_addr)
466 /* Avoid a parallel nop. */
467 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
469 pc_addr += 2;
470 /* Now we can forget this instruction. */
471 last_pc_addr = 0xffffffff;
473 /* Clear a parallel bit. */
474 else
476 buf[0] = SDI_WRITE_MEMORY;
477 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
478 store_long_parameter (buf + 1, pc_addr);
479 else
480 store_long_parameter (buf + 1, pc_addr - 1);
481 store_long_parameter (buf + 5, 1);
482 buf[9] = last_pc_addr_data[0] & 0x7f;
483 send_data (buf, 10);
487 /* Set PC. */
488 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
490 /* step mode. */
491 step_mode = step;
492 if (step)
494 /* Set PBP. */
495 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
497 else
499 /* Unset PBP. */
500 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
503 if (use_ib_breakpoints)
504 ib_breakpoints = max_ib_breakpoints;
505 else
506 ib_breakpoints = 0;
508 /* Set ib breakpoints. */
509 for (i = 0; i < ib_breakpoints; i++)
511 bp_addr = bp_address[i];
513 if (bp_addr == 0xffffffff)
514 continue;
516 /* Set PBP. */
517 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
518 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
519 0x00000006);
520 else
521 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
522 0x06000000);
524 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
527 /* Set dbt breakpoints. */
528 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
530 bp_addr = bp_address[i];
532 if (bp_addr == 0xffffffff)
533 continue;
535 if (!mmu_on)
536 bp_addr &= 0x7fffffff;
538 /* Write DBT instruction. */
539 buf[0] = SDI_WRITE_MEMORY;
540 store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
541 store_long_parameter (buf + 5, 4);
542 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
544 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
546 buf[9] = dbt_bp_entry[0];
547 buf[10] = dbt_bp_entry[1];
548 buf[11] = dbt_bp_entry[2];
549 buf[12] = dbt_bp_entry[3];
551 else
553 buf[9] = dbt_bp_entry[3];
554 buf[10] = dbt_bp_entry[2];
555 buf[11] = dbt_bp_entry[1];
556 buf[12] = dbt_bp_entry[0];
559 else
561 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
563 if ((bp_addr & 2) == 0)
565 buf[9] = dbt_bp_entry[0];
566 buf[10] = dbt_bp_entry[1];
567 buf[11] = bp_data[i][2] & 0x7f;
568 buf[12] = bp_data[i][3];
570 else
572 buf[9] = bp_data[i][0];
573 buf[10] = bp_data[i][1];
574 buf[11] = dbt_bp_entry[0];
575 buf[12] = dbt_bp_entry[1];
578 else
580 if ((bp_addr & 2) == 0)
582 buf[9] = bp_data[i][0];
583 buf[10] = bp_data[i][1] & 0x7f;
584 buf[11] = dbt_bp_entry[1];
585 buf[12] = dbt_bp_entry[0];
587 else
589 buf[9] = dbt_bp_entry[1];
590 buf[10] = dbt_bp_entry[0];
591 buf[11] = bp_data[i][2];
592 buf[12] = bp_data[i][3];
596 send_data (buf, 13);
599 /* Set access breaks. */
600 for (i = 0; i < max_access_breaks; i++)
602 ab_addr = ab_address[i];
604 if (ab_addr == 0x00000000)
605 continue;
607 /* DBC register */
608 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
610 switch (ab_type[i])
612 case 0: /* write watch */
613 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
614 0x00000086);
615 break;
616 case 1: /* read watch */
617 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
618 0x00000046);
619 break;
620 case 2: /* access watch */
621 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
622 0x00000006);
623 break;
626 else
628 switch (ab_type[i])
630 case 0: /* write watch */
631 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
632 0x86000000);
633 break;
634 case 1: /* read watch */
635 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
636 0x46000000);
637 break;
638 case 2: /* access watch */
639 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
640 0x06000000);
641 break;
645 /* DBAH register */
646 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
648 /* DBAL register */
649 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
650 0xffffffff);
652 /* DBD register */
653 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
654 0x00000000);
656 /* DBDM register */
657 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
658 0x00000000);
661 /* Resume program. */
662 send_cmd (SDI_EXEC_CPU);
664 /* Without this, some commands which require an active target (such as kill)
665 won't work. This variable serves (at least) double duty as both the pid
666 of the target process (if it has such), and as a flag indicating that a
667 target is active. These functions should be split out into seperate
668 variables, especially since GDB will someday have a notion of debugging
669 several processes. */
670 inferior_ptid = pid_to_ptid (32);
672 return;
675 /* Wait until the remote machine stops, then return,
676 storing status in STATUS just as `wait' would. */
678 static void
679 gdb_cntrl_c (int signo)
681 if (remote_debug)
682 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
683 interrupted = 1;
686 static ptid_t
687 m32r_wait (ptid_t ptid, struct target_waitstatus *status)
689 static RETSIGTYPE (*prev_sigint) ();
690 unsigned long bp_addr, pc_addr;
691 int ib_breakpoints;
692 long i;
693 unsigned char buf[13];
694 unsigned long val;
695 int ret, c;
697 if (remote_debug)
698 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
700 status->kind = TARGET_WAITKIND_EXITED;
701 status->value.sig = 0;
703 interrupted = 0;
704 prev_sigint = signal (SIGINT, gdb_cntrl_c);
706 /* Wait for ready */
707 buf[0] = SDI_WAIT_FOR_READY;
708 if (serial_write (sdi_desc, buf, 1) != 0)
709 error (_("Remote connection closed"));
711 while (1)
713 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
714 if (c < 0)
715 error (_("Remote connection closed"));
717 if (c == '-') /* error */
719 status->kind = TARGET_WAITKIND_STOPPED;
720 status->value.sig = TARGET_SIGNAL_HUP;
721 return inferior_ptid;
723 else if (c == '+') /* stopped */
724 break;
726 if (interrupted)
727 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
728 else
729 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
730 if (ret != 0)
731 error (_("Remote connection closed"));
734 status->kind = TARGET_WAITKIND_STOPPED;
735 if (interrupted)
736 status->value.sig = TARGET_SIGNAL_INT;
737 else
738 status->value.sig = TARGET_SIGNAL_TRAP;
740 interrupted = 0;
741 signal (SIGINT, prev_sigint);
743 check_mmu_status ();
745 /* Recover parallel bit. */
746 if (last_pc_addr != 0xffffffff)
748 buf[0] = SDI_WRITE_MEMORY;
749 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
750 store_long_parameter (buf + 1, last_pc_addr);
751 else
752 store_long_parameter (buf + 1, last_pc_addr - 1);
753 store_long_parameter (buf + 5, 1);
754 buf[9] = last_pc_addr_data[0];
755 send_data (buf, 10);
756 last_pc_addr = 0xffffffff;
759 if (use_ib_breakpoints)
760 ib_breakpoints = max_ib_breakpoints;
761 else
762 ib_breakpoints = 0;
764 /* Set back pc by 2 if m32r is stopped with dbt. */
765 last_pc_addr = 0xffffffff;
766 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
767 pc_addr = recv_long_data () - 2;
768 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
770 if (pc_addr == bp_address[i])
772 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, 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 (gdbarch_byte_order (current_gdbarch) == 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];
787 else
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];
797 break;
801 /* Remove ib breakpoints. */
802 for (i = 0; i < ib_breakpoints; i++)
804 if (bp_address[i] != 0xffffffff)
805 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
806 0x00000000);
808 /* Remove dbt breakpoints. */
809 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
811 bp_addr = bp_address[i];
812 if (bp_addr != 0xffffffff)
814 if (!mmu_on)
815 bp_addr &= 0x7fffffff;
816 buf[0] = SDI_WRITE_MEMORY;
817 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
818 store_long_parameter (buf + 5, 4);
819 buf[9] = bp_data[i][0];
820 buf[10] = bp_data[i][1];
821 buf[11] = bp_data[i][2];
822 buf[12] = bp_data[i][3];
823 send_data (buf, 13);
827 /* Remove access breaks. */
828 hit_watchpoint_addr = 0;
829 for (i = 0; i < max_access_breaks; i++)
831 if (ab_address[i] != 0x00000000)
833 buf[0] = SDI_READ_MEMORY;
834 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
835 store_long_parameter (buf + 5, 4);
836 serial_write (sdi_desc, buf, 9);
837 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
838 if (c != '-' && recv_data (buf, 4) != -1)
840 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
842 if ((buf[3] & 0x1) == 0x1)
843 hit_watchpoint_addr = ab_address[i];
845 else
847 if ((buf[0] & 0x1) == 0x1)
848 hit_watchpoint_addr = ab_address[i];
852 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
853 0x00000000);
857 if (remote_debug)
858 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
860 return inferior_ptid;
863 /* Terminate the open connection to the remote debugger.
864 Use this when you want to detach and do something else
865 with your gdb. */
866 static void
867 m32r_detach (char *args, int from_tty)
869 if (remote_debug)
870 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
872 m32r_resume (inferior_ptid, 0, 0);
874 /* calls m32r_close to do the real work */
875 pop_target ();
876 if (from_tty)
877 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
878 target_shortname);
881 /* Return the id of register number REGNO. */
883 static int
884 get_reg_id (int regno)
886 switch (regno)
888 case 20:
889 return SDI_REG_BBPC;
890 case 21:
891 return SDI_REG_BPC;
892 case 22:
893 return SDI_REG_ACCL;
894 case 23:
895 return SDI_REG_ACCH;
896 case 24:
897 return SDI_REG_EVB;
900 return regno;
903 /* Read the remote registers into the block REGS. */
905 static void m32r_fetch_register (struct regcache *, int);
907 static void
908 m32r_fetch_registers (struct regcache *regcache)
910 int regno;
912 for (regno = 0;
913 regno < gdbarch_num_regs (get_regcache_arch (regcache));
914 regno++)
915 m32r_fetch_register (regcache, regno);
918 /* Fetch register REGNO, or all registers if REGNO is -1.
919 Returns errno value. */
920 static void
921 m32r_fetch_register (struct regcache *regcache, int regno)
923 unsigned long val, val2, regid;
925 if (regno == -1)
926 m32r_fetch_registers (regcache);
927 else
929 char buffer[MAX_REGISTER_SIZE];
931 regid = get_reg_id (regno);
932 send_one_arg_cmd (SDI_READ_CPU_REG, regid);
933 val = recv_long_data ();
935 if (regid == SDI_REG_PSW)
937 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
938 val2 = recv_long_data ();
939 val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
942 if (remote_debug)
943 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
944 regno, val);
946 /* We got the number the register holds, but gdb expects to see a
947 value in the target byte ordering. */
948 store_unsigned_integer (buffer, 4, val);
949 regcache_raw_supply (regcache, regno, buffer);
951 return;
954 /* Store the remote registers from the contents of the block REGS. */
956 static void m32r_store_register (struct regcache *, int);
958 static void
959 m32r_store_registers (struct regcache *regcache)
961 int regno;
963 for (regno = 0;
964 regno < gdbarch_num_regs (get_regcache_arch (regcache));
965 regno++)
966 m32r_store_register (regcache, regno);
968 registers_changed ();
971 /* Store register REGNO, or all if REGNO == 0.
972 Return errno value. */
973 static void
974 m32r_store_register (struct regcache *regcache, int regno)
976 int regid;
977 ULONGEST regval, tmp;
979 if (regno == -1)
980 m32r_store_registers (regcache);
981 else
983 regcache_cooked_read_unsigned (regcache, regno, &regval);
984 regid = get_reg_id (regno);
986 if (regid == SDI_REG_PSW)
988 unsigned long psw, bbpsw;
990 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
991 psw = recv_long_data ();
993 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
994 bbpsw = recv_long_data ();
996 tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
997 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
999 tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
1000 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
1002 else
1004 send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
1007 if (remote_debug)
1008 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1009 regno, (unsigned long) regval);
1013 /* Get ready to modify the registers array. On machines which store
1014 individual registers, this doesn't need to do anything. On machines
1015 which store all the registers in one fell swoop, this makes sure
1016 that registers contains all the registers from the program being
1017 debugged. */
1019 static void
1020 m32r_prepare_to_store (struct regcache *regcache)
1022 /* Do nothing, since we can store individual regs */
1023 if (remote_debug)
1024 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1027 static void
1028 m32r_files_info (struct target_ops *target)
1030 char *file = "nothing";
1032 if (exec_bfd)
1034 file = bfd_get_filename (exec_bfd);
1035 printf_filtered ("\tAttached to %s running program %s\n",
1036 chip_name, file);
1040 /* Read/Write memory. */
1041 static int
1042 m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
1043 int write,
1044 struct mem_attrib *attrib, struct target_ops *target)
1046 unsigned long taddr;
1047 unsigned char buf[0x2000];
1048 int ret, c;
1050 taddr = memaddr;
1052 if (!mmu_on)
1054 if ((taddr & 0xa0000000) == 0x80000000)
1055 taddr &= 0x7fffffff;
1058 if (remote_debug)
1060 if (write)
1061 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,write)\n",
1062 paddr (memaddr), len);
1063 else
1064 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,read)\n",
1065 paddr (memaddr), len);
1068 if (write)
1070 buf[0] = SDI_WRITE_MEMORY;
1071 store_long_parameter (buf + 1, taddr);
1072 store_long_parameter (buf + 5, len);
1073 if (len < 0x1000)
1075 memcpy (buf + 9, myaddr, len);
1076 ret = send_data (buf, len + 9) - 9;
1078 else
1080 if (serial_write (sdi_desc, buf, 9) != 0)
1082 if (remote_debug)
1083 fprintf_unfiltered (gdb_stdlog,
1084 "m32r_xfer_memory() failed\n");
1085 return 0;
1087 ret = send_data (myaddr, len);
1090 else
1092 buf[0] = SDI_READ_MEMORY;
1093 store_long_parameter (buf + 1, taddr);
1094 store_long_parameter (buf + 5, len);
1095 if (serial_write (sdi_desc, buf, 9) != 0)
1097 if (remote_debug)
1098 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1099 return 0;
1102 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1103 if (c < 0 || c == '-')
1105 if (remote_debug)
1106 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1107 return 0;
1110 ret = recv_data (myaddr, len);
1113 if (ret <= 0)
1115 if (remote_debug)
1116 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1117 return 0;
1120 return ret;
1123 static void
1124 m32r_kill (void)
1126 if (remote_debug)
1127 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1129 inferior_ptid = null_ptid;
1131 return;
1134 /* Clean up when a program exits.
1136 The program actually lives on in the remote processor's RAM, and may be
1137 run again without a download. Don't leave it full of breakpoint
1138 instructions. */
1140 static void
1141 m32r_mourn_inferior (void)
1143 if (remote_debug)
1144 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1146 remove_breakpoints ();
1147 generic_mourn_inferior ();
1150 static int
1151 m32r_insert_breakpoint (struct bp_target_info *bp_tgt)
1153 CORE_ADDR addr = bp_tgt->placed_address;
1154 int ib_breakpoints;
1155 unsigned char buf[13];
1156 int i, c;
1158 if (remote_debug)
1159 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
1160 paddr (addr));
1162 if (use_ib_breakpoints)
1163 ib_breakpoints = max_ib_breakpoints;
1164 else
1165 ib_breakpoints = 0;
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;
1175 if (mmu_on)
1176 store_long_parameter (buf + 1, addr & 0xfffffffc);
1177 else
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);
1182 if (c != '-')
1183 recv_data (bp_data[i], 4);
1185 return 0;
1189 error (_("Too many breakpoints"));
1190 return 1;
1193 static int
1194 m32r_remove_breakpoint (struct bp_target_info *bp_tgt)
1196 CORE_ADDR addr = bp_tgt->placed_address;
1197 int i;
1199 if (remote_debug)
1200 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
1201 paddr (addr));
1203 for (i = 0; i < MAX_BREAKPOINTS; i++)
1205 if (bp_address[i] == addr)
1207 bp_address[i] = 0xffffffff;
1208 break;
1212 return 0;
1215 static void
1216 m32r_load (char *args, int from_tty)
1218 struct cleanup *old_chain;
1219 asection *section;
1220 bfd *pbfd;
1221 bfd_vma entry;
1222 char *filename;
1223 int quiet;
1224 int nostart;
1225 struct timeval start_time, end_time;
1226 unsigned long data_count; /* Number of bytes transferred to memory */
1227 int ret;
1228 static RETSIGTYPE (*prev_sigint) ();
1230 /* for direct tcp connections, we can do a fast binary download */
1231 quiet = 0;
1232 nostart = 0;
1233 filename = NULL;
1235 while (*args != '\000')
1237 char *arg;
1239 while (isspace (*args))
1240 args++;
1242 arg = args;
1244 while ((*args != '\000') && !isspace (*args))
1245 args++;
1247 if (*args != '\000')
1248 *args++ = '\000';
1250 if (*arg != '-')
1251 filename = arg;
1252 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1253 quiet = 1;
1254 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1255 nostart = 1;
1256 else
1257 error (_("Unknown option `%s'"), arg);
1260 if (!filename)
1261 filename = get_exec_file (1);
1263 pbfd = bfd_openr (filename, gnutarget);
1264 if (pbfd == NULL)
1266 perror_with_name (filename);
1267 return;
1269 old_chain = make_cleanup_bfd_close (pbfd);
1271 if (!bfd_check_format (pbfd, bfd_object))
1272 error (_("\"%s\" is not an object file: %s"), filename,
1273 bfd_errmsg (bfd_get_error ()));
1275 gettimeofday (&start_time, NULL);
1276 data_count = 0;
1278 interrupted = 0;
1279 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1281 for (section = pbfd->sections; section; section = section->next)
1283 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1285 bfd_vma section_address;
1286 bfd_size_type section_size;
1287 file_ptr fptr;
1288 int n;
1290 section_address = bfd_section_lma (pbfd, section);
1291 section_size = bfd_get_section_size (section);
1293 if (!mmu_on)
1295 if ((section_address & 0xa0000000) == 0x80000000)
1296 section_address &= 0x7fffffff;
1299 if (!quiet)
1300 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1301 bfd_get_section_name (pbfd, section),
1302 (unsigned long) section_address,
1303 (int) section_size);
1305 fptr = 0;
1307 data_count += section_size;
1309 n = 0;
1310 while (section_size > 0)
1312 char unsigned buf[0x1000 + 9];
1313 int count;
1315 count = min (section_size, 0x1000);
1317 buf[0] = SDI_WRITE_MEMORY;
1318 store_long_parameter (buf + 1, section_address);
1319 store_long_parameter (buf + 5, count);
1321 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1322 if (send_data (buf, count + 9) <= 0)
1323 error (_("Error while downloading %s section."),
1324 bfd_get_section_name (pbfd, section));
1326 if (!quiet)
1328 printf_unfiltered (".");
1329 if (n++ > 60)
1331 printf_unfiltered ("\n");
1332 n = 0;
1334 gdb_flush (gdb_stdout);
1337 section_address += count;
1338 fptr += count;
1339 section_size -= count;
1341 if (interrupted)
1342 break;
1345 if (!quiet && !interrupted)
1347 printf_unfiltered ("done.\n");
1348 gdb_flush (gdb_stdout);
1352 if (interrupted)
1354 printf_unfiltered ("Interrupted.\n");
1355 break;
1359 interrupted = 0;
1360 signal (SIGINT, prev_sigint);
1362 gettimeofday (&end_time, NULL);
1364 /* Make the PC point at the start address */
1365 if (exec_bfd)
1366 write_pc (bfd_get_start_address (exec_bfd));
1368 inferior_ptid = null_ptid; /* No process now */
1370 /* This is necessary because many things were based on the PC at the time
1371 that we attached to the monitor, which is no longer valid now that we
1372 have loaded new code (and just changed the PC). Another way to do this
1373 might be to call normal_stop, except that the stack may not be valid,
1374 and things would get horribly confused... */
1376 clear_symtab_users ();
1378 if (!nostart)
1380 entry = bfd_get_start_address (pbfd);
1382 if (!quiet)
1383 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
1384 (unsigned long) entry);
1387 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
1388 &end_time);
1390 do_cleanups (old_chain);
1393 static void
1394 m32r_stop (void)
1396 if (remote_debug)
1397 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1399 send_cmd (SDI_STOP_CPU);
1401 return;
1405 /* Tell whether this target can support a hardware breakpoint. CNT
1406 is the number of hardware breakpoints already installed. This
1407 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1410 m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
1412 return sdi_desc != NULL && cnt < max_access_breaks;
1415 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1416 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1417 watchpoint. */
1420 m32r_insert_watchpoint (CORE_ADDR addr, int len, int type)
1422 int i;
1424 if (remote_debug)
1425 fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
1426 paddr (addr), len, type);
1428 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1430 if (ab_address[i] == 0x00000000)
1432 ab_address[i] = addr;
1433 ab_size[i] = len;
1434 ab_type[i] = type;
1435 return 0;
1439 error (_("Too many watchpoints"));
1440 return 1;
1444 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1446 int i;
1448 if (remote_debug)
1449 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
1450 paddr (addr), len, type);
1452 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1454 if (ab_address[i] == addr)
1456 ab_address[i] = 0x00000000;
1457 break;
1461 return 0;
1465 m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1467 int rc = 0;
1468 if (hit_watchpoint_addr != 0x00000000)
1470 *addr_p = hit_watchpoint_addr;
1471 rc = 1;
1473 return rc;
1477 m32r_stopped_by_watchpoint (void)
1479 CORE_ADDR addr;
1480 return m32r_stopped_data_address (&current_target, &addr);
1484 static void
1485 sdireset_command (char *args, int from_tty)
1487 if (remote_debug)
1488 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1490 send_cmd (SDI_OPEN);
1492 inferior_ptid = null_ptid;
1496 static void
1497 sdistatus_command (char *args, int from_tty)
1499 unsigned char buf[4096];
1500 int i, c;
1502 if (remote_debug)
1503 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1505 if (!sdi_desc)
1506 return;
1508 send_cmd (SDI_STATUS);
1509 for (i = 0; i < 4096; i++)
1511 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1512 if (c < 0)
1513 return;
1514 buf[i] = c;
1515 if (c == 0)
1516 break;
1519 printf_filtered ("%s", buf);
1523 static void
1524 debug_chaos_command (char *args, int from_tty)
1526 unsigned char buf[3];
1528 buf[0] = SDI_SET_ATTR;
1529 buf[1] = SDI_ATTR_CACHE;
1530 buf[2] = SDI_CACHE_TYPE_CHAOS;
1531 send_data (buf, 3);
1535 static void
1536 use_debug_dma_command (char *args, int from_tty)
1538 unsigned char buf[3];
1540 buf[0] = SDI_SET_ATTR;
1541 buf[1] = SDI_ATTR_MEM_ACCESS;
1542 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1543 send_data (buf, 3);
1546 static void
1547 use_mon_code_command (char *args, int from_tty)
1549 unsigned char buf[3];
1551 buf[0] = SDI_SET_ATTR;
1552 buf[1] = SDI_ATTR_MEM_ACCESS;
1553 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1554 send_data (buf, 3);
1558 static void
1559 use_ib_breakpoints_command (char *args, int from_tty)
1561 use_ib_breakpoints = 1;
1564 static void
1565 use_dbt_breakpoints_command (char *args, int from_tty)
1567 use_ib_breakpoints = 0;
1571 /* Define the target subroutine names */
1573 struct target_ops m32r_ops;
1575 static void
1576 init_m32r_ops (void)
1578 m32r_ops.to_shortname = "m32rsdi";
1579 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1580 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1581 m32r_ops.to_open = m32r_open;
1582 m32r_ops.to_close = m32r_close;
1583 m32r_ops.to_detach = m32r_detach;
1584 m32r_ops.to_resume = m32r_resume;
1585 m32r_ops.to_wait = m32r_wait;
1586 m32r_ops.to_fetch_registers = m32r_fetch_register;
1587 m32r_ops.to_store_registers = m32r_store_register;
1588 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
1589 m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
1590 m32r_ops.to_files_info = m32r_files_info;
1591 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1592 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
1593 m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1594 m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1595 m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1596 m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1597 m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
1598 m32r_ops.to_kill = m32r_kill;
1599 m32r_ops.to_load = m32r_load;
1600 m32r_ops.to_create_inferior = m32r_create_inferior;
1601 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1602 m32r_ops.to_stop = m32r_stop;
1603 m32r_ops.to_log_command = serial_log_command;
1604 m32r_ops.to_stratum = process_stratum;
1605 m32r_ops.to_has_all_memory = 1;
1606 m32r_ops.to_has_memory = 1;
1607 m32r_ops.to_has_stack = 1;
1608 m32r_ops.to_has_registers = 1;
1609 m32r_ops.to_has_execution = 1;
1610 m32r_ops.to_magic = OPS_MAGIC;
1614 extern initialize_file_ftype _initialize_remote_m32r;
1616 void
1617 _initialize_remote_m32r (void)
1619 int i;
1621 init_m32r_ops ();
1623 /* Initialize breakpoints. */
1624 for (i = 0; i < MAX_BREAKPOINTS; i++)
1625 bp_address[i] = 0xffffffff;
1627 /* Initialize access breaks. */
1628 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1629 ab_address[i] = 0x00000000;
1631 add_target (&m32r_ops);
1633 add_com ("sdireset", class_obscure, sdireset_command,
1634 _("Reset SDI connection."));
1636 add_com ("sdistatus", class_obscure, sdistatus_command,
1637 _("Show status of SDI connection."));
1639 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1640 _("Debug M32R/Chaos."));
1642 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1643 _("Use debug DMA mem access."));
1644 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1645 _("Use mon code mem access."));
1647 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1648 _("Set breakpoints by IB break."));
1649 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1650 _("Set breakpoints by dbt."));