2010-05-16 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / remote-m32r-sdi.c
CommitLineData
b4b4b794
KI
1/* Remote debugging interface for M32R/SDI.
2
4c38e0a4 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
9b254dd1 4 Free Software Foundation, Inc.
b4b4b794
KI
5
6 Contributed by Renesas Technology Co.
7 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
8
9 This file is part of GDB.
10
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
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
b4b4b794
KI
14 (at your option) any later version.
15
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.
20
21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b4b4b794
KI
23
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"
e5ef4d75 31#include "gdbthread.h"
b4b4b794
KI
32#include <ctype.h>
33#include <signal.h>
cbba9205
KI
34#ifdef __MINGW32__
35#include <winsock.h>
36#else
b4b4b794 37#include <netinet/in.h>
cbba9205 38#endif
b4b4b794
KI
39#include <sys/types.h>
40#include <sys/time.h>
41#include <signal.h>
42#include <time.h>
43
44
45#include "serial.h"
46
47/* Descriptor for I/O to remote machine. */
48
49static struct serial *sdi_desc = NULL;
50
51#define SDI_TIMEOUT 30
52
53
54#define SDIPORT 3232
55
56static char chip_name[64];
57
58static int step_mode;
59static unsigned long last_pc_addr = 0xffffffff;
60static unsigned char last_pc_addr_data[2];
61
62static int mmu_on = 0;
63
64static int use_ib_breakpoints = 1;
65
66#define MAX_BREAKPOINTS 1024
67static int max_ib_breakpoints;
68static unsigned long bp_address[MAX_BREAKPOINTS];
69static unsigned char bp_data[MAX_BREAKPOINTS][4];
b4b4b794
KI
70
71/* dbt -> nop */
72static const unsigned char dbt_bp_entry[] = {
73 0x10, 0xe0, 0x70, 0x00
74};
75
76#define MAX_ACCESS_BREAKS 4
77static int max_access_breaks;
78static unsigned long ab_address[MAX_ACCESS_BREAKS];
79static unsigned int ab_type[MAX_ACCESS_BREAKS];
80static unsigned int ab_size[MAX_ACCESS_BREAKS];
81static CORE_ADDR hit_watchpoint_addr = 0;
82
83static int interrupted = 0;
84
85/* Forward data declarations */
86extern struct target_ops m32r_ops;
87
e5ef4d75
PA
88/* This is the ptid we use while we're connected to the remote. Its
89 value is arbitrary, as the target doesn't have a notion of
90 processes or threads, but we need something non-null to place in
91 inferior_ptid. */
92static ptid_t remote_m32r_ptid;
b4b4b794
KI
93
94/* Commands */
95#define SDI_OPEN 1
96#define SDI_CLOSE 2
97#define SDI_RELEASE 3
98#define SDI_READ_CPU_REG 4
99#define SDI_WRITE_CPU_REG 5
100#define SDI_READ_MEMORY 6
101#define SDI_WRITE_MEMORY 7
102#define SDI_EXEC_CPU 8
103#define SDI_STOP_CPU 9
104#define SDI_WAIT_FOR_READY 10
105#define SDI_GET_ATTR 11
106#define SDI_SET_ATTR 12
107#define SDI_STATUS 13
108
109/* Attributes */
110#define SDI_ATTR_NAME 1
111#define SDI_ATTR_BRK 2
112#define SDI_ATTR_ABRK 3
113#define SDI_ATTR_CACHE 4
114#define SDI_CACHE_TYPE_M32102 0
115#define SDI_CACHE_TYPE_CHAOS 1
116#define SDI_ATTR_MEM_ACCESS 5
117#define SDI_MEM_ACCESS_DEBUG_DMA 0
118#define SDI_MEM_ACCESS_MON_CODE 1
119
120/* Registers */
121#define SDI_REG_R0 0
122#define SDI_REG_R1 1
123#define SDI_REG_R2 2
124#define SDI_REG_R3 3
125#define SDI_REG_R4 4
126#define SDI_REG_R5 5
127#define SDI_REG_R6 6
128#define SDI_REG_R7 7
129#define SDI_REG_R8 8
130#define SDI_REG_R9 9
131#define SDI_REG_R10 10
132#define SDI_REG_R11 11
133#define SDI_REG_R12 12
134#define SDI_REG_FP 13
135#define SDI_REG_LR 14
136#define SDI_REG_SP 15
137#define SDI_REG_PSW 16
138#define SDI_REG_CBR 17
139#define SDI_REG_SPI 18
140#define SDI_REG_SPU 19
141#define SDI_REG_CR4 20
142#define SDI_REG_EVB 21
143#define SDI_REG_BPC 22
144#define SDI_REG_CR7 23
145#define SDI_REG_BBPSW 24
146#define SDI_REG_CR9 25
147#define SDI_REG_CR10 26
148#define SDI_REG_CR11 27
149#define SDI_REG_CR12 28
150#define SDI_REG_WR 29
151#define SDI_REG_BBPC 30
152#define SDI_REG_PBP 31
153#define SDI_REG_ACCH 32
154#define SDI_REG_ACCL 33
155#define SDI_REG_ACC1H 34
156#define SDI_REG_ACC1L 35
157
158
159/* Low level communication functions */
160
161/* Check an ack packet from the target */
162static int
163get_ack (void)
164{
165 int c;
166
717eb1cf 167 if (!sdi_desc)
b4b4b794
KI
168 return -1;
169
170 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
171
172 if (c < 0)
173 return -1;
174
717eb1cf 175 if (c != '+') /* error */
b4b4b794
KI
176 return -1;
177
178 return 0;
179}
180
181/* Send data to the target and check an ack packet */
182static int
183send_data (void *buf, int len)
184{
185 int ret;
186
717eb1cf 187 if (!sdi_desc)
b4b4b794
KI
188 return -1;
189
190 if (serial_write (sdi_desc, buf, len) != 0)
191 return -1;
192
193 if (get_ack () == -1)
194 return -1;
195
196 return len;
197}
198
199/* Receive data from the target */
200static int
201recv_data (void *buf, int len)
202{
203 int total = 0;
204 int c;
205
717eb1cf 206 if (!sdi_desc)
b4b4b794
KI
207 return -1;
208
209 while (total < len)
210 {
211 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
212
213 if (c < 0)
214 return -1;
215
216 ((unsigned char *) buf)[total++] = c;
217 }
218
219 return len;
220}
221
222/* Store unsigned long parameter on packet */
223static void
224store_long_parameter (void *buf, long val)
225{
226 val = htonl (val);
227 memcpy (buf, &val, 4);
228}
229
e22f895c
KI
230static int
231send_cmd (unsigned char cmd)
232{
233 unsigned char buf[1];
234 buf[0] = cmd;
235 return send_data (buf, 1);
236}
237
238static int
239send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
240{
241 unsigned char buf[2];
242 buf[0] = cmd;
243 buf[1] = arg1;
244 return send_data (buf, 2);
245}
246
247static int
248send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
249{
250 unsigned char buf[6];
251 buf[0] = cmd;
252 buf[1] = arg1;
253 store_long_parameter (buf + 2, arg2);
254 return send_data (buf, 6);
255}
256
257static int
258send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
259 unsigned long arg3)
260{
261 unsigned char buf[13];
262 buf[0] = cmd;
263 store_long_parameter (buf + 1, arg1);
264 store_long_parameter (buf + 5, arg2);
265 store_long_parameter (buf + 9, arg3);
266 return send_data (buf, 13);
267}
268
269static unsigned char
270recv_char_data (void)
271{
272 unsigned char val;
273 recv_data (&val, 1);
274 return val;
275}
276
277static unsigned long
278recv_long_data (void)
279{
280 unsigned long val;
281 recv_data (&val, 4);
282 return ntohl (val);
283}
284
285
b4b4b794
KI
286/* Check if MMU is on */
287static void
288check_mmu_status (void)
289{
290 unsigned long val;
b4b4b794
KI
291
292 /* Read PC address */
e22f895c 293 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
b4b4b794 294 return;
e22f895c 295 val = recv_long_data ();
b4b4b794
KI
296 if ((val & 0xc0000000) == 0x80000000)
297 {
298 mmu_on = 1;
299 return;
300 }
301
302 /* Read EVB address */
e22f895c 303 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
b4b4b794 304 return;
e22f895c 305 val = recv_long_data ();
b4b4b794
KI
306 if ((val & 0xc0000000) == 0x80000000)
307 {
308 mmu_on = 1;
309 return;
310 }
311
312 mmu_on = 0;
313}
314
315
316/* This is called not only when we first attach, but also when the
317 user types "run" after having attached. */
318static void
136d6dae
VP
319m32r_create_inferior (struct target_ops *ops, char *execfile,
320 char *args, char **env, int from_tty)
b4b4b794
KI
321{
322 CORE_ADDR entry_pt;
323
324 if (args && *args)
8a3fe4f8 325 error (_("Cannot pass arguments to remote STDEBUG process"));
b4b4b794
KI
326
327 if (execfile == 0 || exec_bfd == 0)
8a3fe4f8 328 error (_("No executable file specified"));
b4b4b794
KI
329
330 if (remote_debug)
331 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
332 args);
333
334 entry_pt = bfd_get_start_address (exec_bfd);
335
336 /* The "process" (board) is already stopped awaiting our commands, and
337 the program is already downloaded. We just set its PC and go. */
338
339 clear_proceed_status ();
340
341 /* Tell wait_for_inferior that we've started a new process. */
342 init_wait_for_inferior ();
343
344 /* Set up the "saved terminal modes" of the inferior
345 based on what modes we are starting it with. */
346 target_terminal_init ();
347
348 /* Install inferior's terminal modes. */
349 target_terminal_inferior ();
350
fb14de7b 351 regcache_write_pc (get_current_regcache (), entry_pt);
b4b4b794
KI
352}
353
354/* Open a connection to a remote debugger.
355 NAME is the filename used for communication. */
356
357static void
358m32r_open (char *args, int from_tty)
359{
360 struct hostent *host_ent;
361 struct sockaddr_in server_addr;
362 char *port_str, hostname[256];
363 int port;
b4b4b794
KI
364 int i, n;
365 int yes = 1;
366
367 if (remote_debug)
368 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
369
370 target_preopen (from_tty);
371
372 push_target (&m32r_ops);
373
374 if (args == NULL)
375 sprintf (hostname, "localhost:%d", SDIPORT);
376 else
377 {
378 port_str = strchr (args, ':');
379 if (port_str == NULL)
717eb1cf 380 sprintf (hostname, "%s:%d", args, SDIPORT);
b4b4b794
KI
381 else
382 strcpy (hostname, args);
383 }
384
385 sdi_desc = serial_open (hostname);
386 if (!sdi_desc)
8a3fe4f8 387 error (_("Connection refused."));
b4b4b794
KI
388
389 if (get_ack () == -1)
8a3fe4f8 390 error (_("Cannot connect to SDI target."));
b4b4b794 391
e22f895c 392 if (send_cmd (SDI_OPEN) == -1)
8a3fe4f8 393 error (_("Cannot connect to SDI target."));
b4b4b794
KI
394
395 /* Get maximum number of ib breakpoints */
e22f895c
KI
396 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
397 max_ib_breakpoints = recv_char_data ();
b4b4b794
KI
398 if (remote_debug)
399 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
400
401 /* Initialize breakpoints. */
402 for (i = 0; i < MAX_BREAKPOINTS; i++)
403 bp_address[i] = 0xffffffff;
404
405 /* Get maximum number of access breaks. */
e22f895c
KI
406 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
407 max_access_breaks = recv_char_data ();
b4b4b794
KI
408 if (remote_debug)
409 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
410
411 /* Initialize access breask. */
412 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
413 ab_address[i] = 0x00000000;
414
415 check_mmu_status ();
416
417 /* Get the name of chip on target board. */
e22f895c 418 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
b4b4b794
KI
419 recv_data (chip_name, 64);
420
421 if (from_tty)
422 printf_filtered ("Remote %s connected to %s\n", target_shortname,
423 chip_name);
424}
425
426/* Close out all files and local state before this target loses control. */
427
428static void
429m32r_close (int quitting)
430{
b4b4b794
KI
431 if (remote_debug)
432 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
433
434 if (sdi_desc)
435 {
e22f895c 436 send_cmd (SDI_CLOSE);
b4b4b794
KI
437 serial_close (sdi_desc);
438 sdi_desc = NULL;
439 }
440
441 inferior_ptid = null_ptid;
e5ef4d75 442 delete_thread_silent (remote_m32r_ptid);
b4b4b794
KI
443 return;
444}
445
446/* Tell the remote machine to resume. */
447
448static void
28439f5e
PA
449m32r_resume (struct target_ops *ops,
450 ptid_t ptid, int step, enum target_signal sig)
b4b4b794
KI
451{
452 unsigned long pc_addr, bp_addr, ab_addr;
e22f895c 453 int ib_breakpoints;
b4b4b794
KI
454 unsigned char buf[13];
455 int i;
456
457 if (remote_debug)
458 {
459 if (step)
460 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
461 else
462 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
463 }
464
465 check_mmu_status ();
466
fb14de7b 467 pc_addr = regcache_read_pc (get_current_regcache ());
b4b4b794
KI
468 if (remote_debug)
469 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
470
471 /* At pc address there is a parallel instruction with +2 offset,
472 so we have to make it a serial instruction or avoid it. */
473 if (pc_addr == last_pc_addr)
474 {
475 /* Avoid a parallel nop. */
476 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
477 {
478 pc_addr += 2;
479 /* Now we can forget this instruction. */
480 last_pc_addr = 0xffffffff;
481 }
482 /* Clear a parallel bit. */
483 else
484 {
485 buf[0] = SDI_WRITE_MEMORY;
1cf3db46 486 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794
KI
487 store_long_parameter (buf + 1, pc_addr);
488 else
489 store_long_parameter (buf + 1, pc_addr - 1);
490 store_long_parameter (buf + 5, 1);
491 buf[9] = last_pc_addr_data[0] & 0x7f;
492 send_data (buf, 10);
493 }
494 }
495
496 /* Set PC. */
e22f895c 497 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
b4b4b794
KI
498
499 /* step mode. */
500 step_mode = step;
501 if (step)
502 {
503 /* Set PBP. */
e22f895c 504 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
b4b4b794
KI
505 }
506 else
507 {
e22f895c
KI
508 /* Unset PBP. */
509 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
510 }
511
512 if (use_ib_breakpoints)
513 ib_breakpoints = max_ib_breakpoints;
514 else
515 ib_breakpoints = 0;
516
517 /* Set ib breakpoints. */
518 for (i = 0; i < ib_breakpoints; i++)
519 {
520 bp_addr = bp_address[i];
b4b4b794 521
e22f895c
KI
522 if (bp_addr == 0xffffffff)
523 continue;
524
525 /* Set PBP. */
1cf3db46 526 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
e22f895c
KI
527 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
528 0x00000006);
b4b4b794 529 else
e22f895c
KI
530 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
531 0x06000000);
532
533 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
534 }
535
536 /* Set dbt breakpoints. */
537 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
538 {
539 bp_addr = bp_address[i];
540
541 if (bp_addr == 0xffffffff)
542 continue;
543
544 if (!mmu_on)
545 bp_addr &= 0x7fffffff;
b4b4b794 546
e22f895c
KI
547 /* Write DBT instruction. */
548 buf[0] = SDI_WRITE_MEMORY;
492e5c6b 549 store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
e22f895c
KI
550 store_long_parameter (buf + 5, 4);
551 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
b4b4b794 552 {
1cf3db46 553 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794 554 {
e22f895c
KI
555 buf[9] = dbt_bp_entry[0];
556 buf[10] = dbt_bp_entry[1];
557 buf[11] = dbt_bp_entry[2];
558 buf[12] = dbt_bp_entry[3];
559 }
560 else
561 {
562 buf[9] = dbt_bp_entry[3];
563 buf[10] = dbt_bp_entry[2];
564 buf[11] = dbt_bp_entry[1];
565 buf[12] = dbt_bp_entry[0];
b4b4b794
KI
566 }
567 }
e22f895c 568 else
b4b4b794 569 {
1cf3db46 570 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794 571 {
e22f895c 572 if ((bp_addr & 2) == 0)
b4b4b794 573 {
e22f895c
KI
574 buf[9] = dbt_bp_entry[0];
575 buf[10] = dbt_bp_entry[1];
576 buf[11] = bp_data[i][2] & 0x7f;
577 buf[12] = bp_data[i][3];
b4b4b794
KI
578 }
579 else
580 {
e22f895c
KI
581 buf[9] = bp_data[i][0];
582 buf[10] = bp_data[i][1];
583 buf[11] = dbt_bp_entry[0];
584 buf[12] = dbt_bp_entry[1];
b4b4b794
KI
585 }
586 }
e22f895c 587 else
b4b4b794 588 {
e22f895c 589 if ((bp_addr & 2) == 0)
b4b4b794 590 {
e22f895c
KI
591 buf[9] = bp_data[i][0];
592 buf[10] = bp_data[i][1] & 0x7f;
593 buf[11] = dbt_bp_entry[1];
594 buf[12] = dbt_bp_entry[0];
b4b4b794
KI
595 }
596 else
597 {
e22f895c
KI
598 buf[9] = dbt_bp_entry[1];
599 buf[10] = dbt_bp_entry[0];
600 buf[11] = bp_data[i][2];
601 buf[12] = bp_data[i][3];
b4b4b794 602 }
e22f895c
KI
603 }
604 }
605 send_data (buf, 13);
606 }
b4b4b794 607
e22f895c
KI
608 /* Set access breaks. */
609 for (i = 0; i < max_access_breaks; i++)
610 {
611 ab_addr = ab_address[i];
b4b4b794 612
e22f895c
KI
613 if (ab_addr == 0x00000000)
614 continue;
b4b4b794 615
e22f895c 616 /* DBC register */
1cf3db46 617 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
e22f895c
KI
618 {
619 switch (ab_type[i])
620 {
621 case 0: /* write watch */
622 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
623 0x00000086);
624 break;
625 case 1: /* read watch */
626 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
627 0x00000046);
628 break;
629 case 2: /* access watch */
630 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
631 0x00000006);
632 break;
633 }
634 }
635 else
636 {
637 switch (ab_type[i])
638 {
639 case 0: /* write watch */
640 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
641 0x86000000);
642 break;
643 case 1: /* read watch */
644 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
645 0x46000000);
646 break;
647 case 2: /* access watch */
648 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
649 0x06000000);
650 break;
b4b4b794
KI
651 }
652 }
653
e22f895c
KI
654 /* DBAH register */
655 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
656
657 /* DBAL register */
658 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
659 0xffffffff);
660
661 /* DBD register */
662 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
663 0x00000000);
664
665 /* DBDM register */
666 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
667 0x00000000);
b4b4b794
KI
668 }
669
e22f895c
KI
670 /* Resume program. */
671 send_cmd (SDI_EXEC_CPU);
b4b4b794
KI
672
673 /* Without this, some commands which require an active target (such as kill)
674 won't work. This variable serves (at least) double duty as both the pid
675 of the target process (if it has such), and as a flag indicating that a
676 target is active. These functions should be split out into seperate
677 variables, especially since GDB will someday have a notion of debugging
678 several processes. */
e5ef4d75
PA
679 inferior_ptid = remote_m32r_ptid;
680 add_thread_silent (remote_m32r_ptid);
b4b4b794
KI
681
682 return;
683}
684
685/* Wait until the remote machine stops, then return,
686 storing status in STATUS just as `wait' would. */
687
688static void
689gdb_cntrl_c (int signo)
690{
691 if (remote_debug)
692 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
693 interrupted = 1;
694}
695
696static ptid_t
117de6a9 697m32r_wait (struct target_ops *ops,
61439e34 698 ptid_t ptid, struct target_waitstatus *status, int options)
b4b4b794
KI
699{
700 static RETSIGTYPE (*prev_sigint) ();
701 unsigned long bp_addr, pc_addr;
e22f895c 702 int ib_breakpoints;
b4b4b794
KI
703 long i;
704 unsigned char buf[13];
705 unsigned long val;
706 int ret, c;
707
708 if (remote_debug)
709 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
710
711 status->kind = TARGET_WAITKIND_EXITED;
712 status->value.sig = 0;
713
714 interrupted = 0;
715 prev_sigint = signal (SIGINT, gdb_cntrl_c);
716
717 /* Wait for ready */
718 buf[0] = SDI_WAIT_FOR_READY;
719 if (serial_write (sdi_desc, buf, 1) != 0)
8a3fe4f8 720 error (_("Remote connection closed"));
b4b4b794
KI
721
722 while (1)
723 {
724 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
725 if (c < 0)
8a3fe4f8 726 error (_("Remote connection closed"));
b4b4b794 727
717eb1cf 728 if (c == '-') /* error */
b4b4b794
KI
729 {
730 status->kind = TARGET_WAITKIND_STOPPED;
731 status->value.sig = TARGET_SIGNAL_HUP;
732 return inferior_ptid;
733 }
734 else if (c == '+') /* stopped */
735 break;
736
737 if (interrupted)
738 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
739 else
740 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
741 if (ret != 0)
8a3fe4f8 742 error (_("Remote connection closed"));
b4b4b794
KI
743 }
744
745 status->kind = TARGET_WAITKIND_STOPPED;
746 if (interrupted)
747 status->value.sig = TARGET_SIGNAL_INT;
748 else
749 status->value.sig = TARGET_SIGNAL_TRAP;
750
751 interrupted = 0;
752 signal (SIGINT, prev_sigint);
753
754 check_mmu_status ();
755
756 /* Recover parallel bit. */
757 if (last_pc_addr != 0xffffffff)
758 {
759 buf[0] = SDI_WRITE_MEMORY;
1cf3db46 760 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794
KI
761 store_long_parameter (buf + 1, last_pc_addr);
762 else
763 store_long_parameter (buf + 1, last_pc_addr - 1);
764 store_long_parameter (buf + 5, 1);
765 buf[9] = last_pc_addr_data[0];
766 send_data (buf, 10);
767 last_pc_addr = 0xffffffff;
768 }
769
e22f895c
KI
770 if (use_ib_breakpoints)
771 ib_breakpoints = max_ib_breakpoints;
772 else
773 ib_breakpoints = 0;
b4b4b794 774
e22f895c
KI
775 /* Set back pc by 2 if m32r is stopped with dbt. */
776 last_pc_addr = 0xffffffff;
777 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
778 pc_addr = recv_long_data () - 2;
779 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
780 {
781 if (pc_addr == bp_address[i])
b4b4b794 782 {
e22f895c
KI
783 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
784
785 /* If there is a parallel instruction with +2 offset at pc
786 address, we have to take care of it later. */
787 if ((pc_addr & 0x2) != 0)
b4b4b794 788 {
1cf3db46 789 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794 790 {
e22f895c 791 if ((bp_data[i][2] & 0x80) != 0)
b4b4b794 792 {
e22f895c
KI
793 last_pc_addr = pc_addr;
794 last_pc_addr_data[0] = bp_data[i][2];
795 last_pc_addr_data[1] = bp_data[i][3];
b4b4b794 796 }
e22f895c
KI
797 }
798 else
799 {
800 if ((bp_data[i][1] & 0x80) != 0)
b4b4b794 801 {
e22f895c
KI
802 last_pc_addr = pc_addr;
803 last_pc_addr_data[0] = bp_data[i][1];
804 last_pc_addr_data[1] = bp_data[i][0];
b4b4b794
KI
805 }
806 }
b4b4b794 807 }
e22f895c 808 break;
b4b4b794 809 }
e22f895c 810 }
b4b4b794 811
e22f895c
KI
812 /* Remove ib breakpoints. */
813 for (i = 0; i < ib_breakpoints; i++)
814 {
815 if (bp_address[i] != 0xffffffff)
816 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
817 0x00000000);
818 }
819 /* Remove dbt breakpoints. */
820 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
821 {
822 bp_addr = bp_address[i];
823 if (bp_addr != 0xffffffff)
b4b4b794 824 {
e22f895c
KI
825 if (!mmu_on)
826 bp_addr &= 0x7fffffff;
492e5c6b 827 buf[0] = SDI_WRITE_MEMORY;
e22f895c
KI
828 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
829 store_long_parameter (buf + 5, 4);
830 buf[9] = bp_data[i][0];
831 buf[10] = bp_data[i][1];
832 buf[11] = bp_data[i][2];
833 buf[12] = bp_data[i][3];
834 send_data (buf, 13);
b4b4b794 835 }
e22f895c 836 }
b4b4b794 837
e22f895c
KI
838 /* Remove access breaks. */
839 hit_watchpoint_addr = 0;
840 for (i = 0; i < max_access_breaks; i++)
841 {
842 if (ab_address[i] != 0x00000000)
b4b4b794 843 {
e22f895c
KI
844 buf[0] = SDI_READ_MEMORY;
845 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
846 store_long_parameter (buf + 5, 4);
847 serial_write (sdi_desc, buf, 9);
848 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
849 if (c != '-' && recv_data (buf, 4) != -1)
b4b4b794 850 {
1cf3db46 851 if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
b4b4b794 852 {
e22f895c
KI
853 if ((buf[3] & 0x1) == 0x1)
854 hit_watchpoint_addr = ab_address[i];
855 }
856 else
857 {
858 if ((buf[0] & 0x1) == 0x1)
859 hit_watchpoint_addr = ab_address[i];
b4b4b794 860 }
b4b4b794 861 }
b4b4b794 862
e22f895c
KI
863 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
864 0x00000000);
865 }
b4b4b794 866 }
e22f895c
KI
867
868 if (remote_debug)
869 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
b4b4b794
KI
870
871 return inferior_ptid;
872}
873
874/* Terminate the open connection to the remote debugger.
875 Use this when you want to detach and do something else
876 with your gdb. */
877static void
136d6dae 878m32r_detach (struct target_ops *ops, char *args, int from_tty)
b4b4b794
KI
879{
880 if (remote_debug)
881 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
882
28439f5e 883 m32r_resume (ops, inferior_ptid, 0, 0);
b4b4b794
KI
884
885 /* calls m32r_close to do the real work */
886 pop_target ();
887 if (from_tty)
888 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
889 target_shortname);
890}
891
892/* Return the id of register number REGNO. */
893
894static int
895get_reg_id (int regno)
896{
897 switch (regno)
898 {
899 case 20:
900 return SDI_REG_BBPC;
901 case 21:
902 return SDI_REG_BPC;
903 case 22:
904 return SDI_REG_ACCL;
905 case 23:
906 return SDI_REG_ACCH;
907 case 24:
908 return SDI_REG_EVB;
909 }
910
911 return regno;
912}
913
b4b4b794
KI
914/* Fetch register REGNO, or all registers if REGNO is -1.
915 Returns errno value. */
916static void
28439f5e
PA
917m32r_fetch_register (struct target_ops *ops,
918 struct regcache *regcache, int regno)
b4b4b794 919{
e17a4113
UW
920 struct gdbarch *gdbarch = get_regcache_arch (regcache);
921 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
b4b4b794 922 unsigned long val, val2, regid;
b4b4b794
KI
923
924 if (regno == -1)
28439f5e
PA
925 {
926 for (regno = 0;
927 regno < gdbarch_num_regs (get_regcache_arch (regcache));
928 regno++)
929 m32r_fetch_register (ops, regcache, regno);
930 }
b4b4b794
KI
931 else
932 {
933 char buffer[MAX_REGISTER_SIZE];
934
935 regid = get_reg_id (regno);
e22f895c
KI
936 send_one_arg_cmd (SDI_READ_CPU_REG, regid);
937 val = recv_long_data ();
b4b4b794
KI
938
939 if (regid == SDI_REG_PSW)
940 {
e22f895c
KI
941 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
942 val2 = recv_long_data ();
3e41d55f 943 val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
b4b4b794
KI
944 }
945
946 if (remote_debug)
947 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
948 regno, val);
949
950 /* We got the number the register holds, but gdb expects to see a
951 value in the target byte ordering. */
e17a4113 952 store_unsigned_integer (buffer, 4, byte_order, val);
56be3814 953 regcache_raw_supply (regcache, regno, buffer);
b4b4b794
KI
954 }
955 return;
956}
957
b4b4b794
KI
958/* Store register REGNO, or all if REGNO == 0.
959 Return errno value. */
960static void
28439f5e
PA
961m32r_store_register (struct target_ops *ops,
962 struct regcache *regcache, int regno)
b4b4b794
KI
963{
964 int regid;
965 ULONGEST regval, tmp;
b4b4b794
KI
966
967 if (regno == -1)
28439f5e
PA
968 {
969 for (regno = 0;
970 regno < gdbarch_num_regs (get_regcache_arch (regcache));
971 regno++)
972 m32r_store_register (ops, regcache, regno);
973 }
b4b4b794
KI
974 else
975 {
56be3814 976 regcache_cooked_read_unsigned (regcache, regno, &regval);
b4b4b794
KI
977 regid = get_reg_id (regno);
978
979 if (regid == SDI_REG_PSW)
980 {
981 unsigned long psw, bbpsw;
982
e22f895c
KI
983 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
984 psw = recv_long_data ();
b4b4b794 985
e22f895c
KI
986 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
987 bbpsw = recv_long_data ();
b4b4b794 988
3e41d55f 989 tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
e22f895c 990 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
b4b4b794 991
3e41d55f 992 tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
e22f895c 993 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
b4b4b794
KI
994 }
995 else
996 {
e22f895c 997 send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
b4b4b794
KI
998 }
999
1000 if (remote_debug)
1001 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1002 regno, (unsigned long) regval);
1003 }
1004}
1005
1006/* Get ready to modify the registers array. On machines which store
1007 individual registers, this doesn't need to do anything. On machines
1008 which store all the registers in one fell swoop, this makes sure
1009 that registers contains all the registers from the program being
1010 debugged. */
1011
1012static void
316f2060 1013m32r_prepare_to_store (struct regcache *regcache)
b4b4b794
KI
1014{
1015 /* Do nothing, since we can store individual regs */
1016 if (remote_debug)
1017 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1018}
1019
1020static void
1021m32r_files_info (struct target_ops *target)
1022{
0d18d720 1023 const char *file = "nothing";
b4b4b794
KI
1024
1025 if (exec_bfd)
1026 {
1027 file = bfd_get_filename (exec_bfd);
1028 printf_filtered ("\tAttached to %s running program %s\n",
1029 chip_name, file);
1030 }
1031}
1032
1033/* Read/Write memory. */
1034static int
16ac4ab5 1035m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
b4b4b794
KI
1036 int write,
1037 struct mem_attrib *attrib, struct target_ops *target)
1038{
1039 unsigned long taddr;
1040 unsigned char buf[0x2000];
1041 int ret, c;
1042
1043 taddr = memaddr;
1044
1045 if (!mmu_on)
1046 {
1047 if ((taddr & 0xa0000000) == 0x80000000)
1048 taddr &= 0x7fffffff;
1049 }
1050
1051 if (remote_debug)
1052 {
1053 if (write)
ec20a626 1054 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,write)\n",
5af949e3 1055 paddress (target_gdbarch, memaddr), len);
b4b4b794 1056 else
ec20a626 1057 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,read)\n",
5af949e3 1058 paddress (target_gdbarch, memaddr), len);
b4b4b794
KI
1059 }
1060
1061 if (write)
1062 {
1063 buf[0] = SDI_WRITE_MEMORY;
1064 store_long_parameter (buf + 1, taddr);
1065 store_long_parameter (buf + 5, len);
1066 if (len < 0x1000)
1067 {
1068 memcpy (buf + 9, myaddr, len);
1069 ret = send_data (buf, len + 9) - 9;
1070 }
1071 else
1072 {
1073 if (serial_write (sdi_desc, buf, 9) != 0)
1074 {
1075 if (remote_debug)
1076 fprintf_unfiltered (gdb_stdlog,
1077 "m32r_xfer_memory() failed\n");
1078 return 0;
1079 }
1080 ret = send_data (myaddr, len);
1081 }
1082 }
1083 else
1084 {
1085 buf[0] = SDI_READ_MEMORY;
1086 store_long_parameter (buf + 1, taddr);
1087 store_long_parameter (buf + 5, len);
1088 if (serial_write (sdi_desc, buf, 9) != 0)
1089 {
1090 if (remote_debug)
1091 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1092 return 0;
1093 }
1094
1095 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1096 if (c < 0 || c == '-')
1097 {
1098 if (remote_debug)
1099 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1100 return 0;
1101 }
1102
1103 ret = recv_data (myaddr, len);
1104 }
1105
1106 if (ret <= 0)
1107 {
1108 if (remote_debug)
1109 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1110 return 0;
1111 }
1112
1113 return ret;
1114}
1115
1116static void
7d85a9c0 1117m32r_kill (struct target_ops *ops)
b4b4b794
KI
1118{
1119 if (remote_debug)
1120 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1121
1122 inferior_ptid = null_ptid;
e5ef4d75 1123 delete_thread_silent (remote_m32r_ptid);
b4b4b794
KI
1124
1125 return;
1126}
1127
1128/* Clean up when a program exits.
1129
1130 The program actually lives on in the remote processor's RAM, and may be
1131 run again without a download. Don't leave it full of breakpoint
1132 instructions. */
1133
1134static void
136d6dae 1135m32r_mourn_inferior (struct target_ops *ops)
b4b4b794
KI
1136{
1137 if (remote_debug)
1138 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1139
1140 remove_breakpoints ();
1141 generic_mourn_inferior ();
1142}
1143
1144static int
a6d9a66e
UW
1145m32r_insert_breakpoint (struct gdbarch *gdbarch,
1146 struct bp_target_info *bp_tgt)
b4b4b794 1147{
8181d85f 1148 CORE_ADDR addr = bp_tgt->placed_address;
b4b4b794
KI
1149 int ib_breakpoints;
1150 unsigned char buf[13];
1151 int i, c;
1152
1153 if (remote_debug)
ec20a626 1154 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
5af949e3 1155 paddress (gdbarch, addr));
b4b4b794
KI
1156
1157 if (use_ib_breakpoints)
1158 ib_breakpoints = max_ib_breakpoints;
1159 else
1160 ib_breakpoints = 0;
1161
1162 for (i = 0; i < MAX_BREAKPOINTS; i++)
1163 {
1164 if (bp_address[i] == 0xffffffff)
1165 {
1166 bp_address[i] = addr;
1167 if (i >= ib_breakpoints)
1168 {
1169 buf[0] = SDI_READ_MEMORY;
1170 if (mmu_on)
1171 store_long_parameter (buf + 1, addr & 0xfffffffc);
1172 else
1173 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1174 store_long_parameter (buf + 5, 4);
1175 serial_write (sdi_desc, buf, 9);
1176 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1177 if (c != '-')
1178 recv_data (bp_data[i], 4);
1179 }
1180 return 0;
1181 }
1182 }
1183
8a3fe4f8 1184 error (_("Too many breakpoints"));
b4b4b794
KI
1185 return 1;
1186}
1187
1188static int
a6d9a66e
UW
1189m32r_remove_breakpoint (struct gdbarch *gdbarch,
1190 struct bp_target_info *bp_tgt)
b4b4b794 1191{
8181d85f 1192 CORE_ADDR addr = bp_tgt->placed_address;
b4b4b794
KI
1193 int i;
1194
1195 if (remote_debug)
ec20a626 1196 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
5af949e3 1197 paddress (gdbarch, addr));
b4b4b794
KI
1198
1199 for (i = 0; i < MAX_BREAKPOINTS; i++)
1200 {
1201 if (bp_address[i] == addr)
1202 {
1203 bp_address[i] = 0xffffffff;
1204 break;
1205 }
1206 }
1207
1208 return 0;
1209}
1210
1211static void
1212m32r_load (char *args, int from_tty)
1213{
1214 struct cleanup *old_chain;
1215 asection *section;
1216 bfd *pbfd;
1217 bfd_vma entry;
1218 char *filename;
1219 int quiet;
1220 int nostart;
2b71414d 1221 struct timeval start_time, end_time;
b4b4b794
KI
1222 unsigned long data_count; /* Number of bytes transferred to memory */
1223 int ret;
1224 static RETSIGTYPE (*prev_sigint) ();
1225
1226 /* for direct tcp connections, we can do a fast binary download */
1227 quiet = 0;
1228 nostart = 0;
1229 filename = NULL;
1230
1231 while (*args != '\000')
1232 {
1233 char *arg;
1234
1235 while (isspace (*args))
1236 args++;
1237
1238 arg = args;
1239
1240 while ((*args != '\000') && !isspace (*args))
1241 args++;
1242
1243 if (*args != '\000')
1244 *args++ = '\000';
1245
1246 if (*arg != '-')
1247 filename = arg;
1248 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1249 quiet = 1;
1250 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1251 nostart = 1;
1252 else
8a3fe4f8 1253 error (_("Unknown option `%s'"), arg);
b4b4b794
KI
1254 }
1255
1256 if (!filename)
1257 filename = get_exec_file (1);
1258
1259 pbfd = bfd_openr (filename, gnutarget);
1260 if (pbfd == NULL)
1261 {
1262 perror_with_name (filename);
1263 return;
1264 }
1265 old_chain = make_cleanup_bfd_close (pbfd);
1266
1267 if (!bfd_check_format (pbfd, bfd_object))
8a3fe4f8 1268 error (_("\"%s\" is not an object file: %s"), filename,
b4b4b794
KI
1269 bfd_errmsg (bfd_get_error ()));
1270
2b71414d 1271 gettimeofday (&start_time, NULL);
b4b4b794
KI
1272 data_count = 0;
1273
1274 interrupted = 0;
1275 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1276
1277 for (section = pbfd->sections; section; section = section->next)
1278 {
1279 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1280 {
1281 bfd_vma section_address;
1282 bfd_size_type section_size;
1283 file_ptr fptr;
1284 int n;
1285
1286 section_address = bfd_section_lma (pbfd, section);
2c500098 1287 section_size = bfd_get_section_size (section);
b4b4b794
KI
1288
1289 if (!mmu_on)
1290 {
1291 if ((section_address & 0xa0000000) == 0x80000000)
1292 section_address &= 0x7fffffff;
1293 }
1294
1295 if (!quiet)
1296 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1297 bfd_get_section_name (pbfd, section),
ec20a626
UW
1298 (unsigned long) section_address,
1299 (int) section_size);
b4b4b794
KI
1300
1301 fptr = 0;
1302
1303 data_count += section_size;
1304
1305 n = 0;
1306 while (section_size > 0)
1307 {
1308 char unsigned buf[0x1000 + 9];
1309 int count;
1310
1311 count = min (section_size, 0x1000);
1312
1313 buf[0] = SDI_WRITE_MEMORY;
1314 store_long_parameter (buf + 1, section_address);
1315 store_long_parameter (buf + 5, count);
1316
1317 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1318 if (send_data (buf, count + 9) <= 0)
8a3fe4f8 1319 error (_("Error while downloading %s section."),
b4b4b794
KI
1320 bfd_get_section_name (pbfd, section));
1321
1322 if (!quiet)
1323 {
1324 printf_unfiltered (".");
1325 if (n++ > 60)
1326 {
1327 printf_unfiltered ("\n");
1328 n = 0;
1329 }
1330 gdb_flush (gdb_stdout);
1331 }
1332
1333 section_address += count;
1334 fptr += count;
1335 section_size -= count;
1336
1337 if (interrupted)
1338 break;
1339 }
1340
1341 if (!quiet && !interrupted)
1342 {
1343 printf_unfiltered ("done.\n");
1344 gdb_flush (gdb_stdout);
1345 }
1346 }
1347
1348 if (interrupted)
1349 {
1350 printf_unfiltered ("Interrupted.\n");
1351 break;
1352 }
1353 }
1354
1355 interrupted = 0;
1356 signal (SIGINT, prev_sigint);
1357
2b71414d 1358 gettimeofday (&end_time, NULL);
b4b4b794
KI
1359
1360 /* Make the PC point at the start address */
1361 if (exec_bfd)
fb14de7b
UW
1362 regcache_write_pc (get_current_regcache (),
1363 bfd_get_start_address (exec_bfd));
b4b4b794
KI
1364
1365 inferior_ptid = null_ptid; /* No process now */
e5ef4d75 1366 delete_thread_silent (remote_m32r_ptid);
b4b4b794
KI
1367
1368 /* This is necessary because many things were based on the PC at the time
1369 that we attached to the monitor, which is no longer valid now that we
1370 have loaded new code (and just changed the PC). Another way to do this
1371 might be to call normal_stop, except that the stack may not be valid,
1372 and things would get horribly confused... */
1373
1374 clear_symtab_users ();
1375
1376 if (!nostart)
1377 {
1378 entry = bfd_get_start_address (pbfd);
1379
1380 if (!quiet)
ec20a626
UW
1381 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
1382 (unsigned long) entry);
b4b4b794
KI
1383 }
1384
2b71414d
DJ
1385 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
1386 &end_time);
b4b4b794
KI
1387
1388 do_cleanups (old_chain);
1389}
1390
1391static void
f9c72d52 1392m32r_stop (ptid_t ptid)
b4b4b794 1393{
b4b4b794
KI
1394 if (remote_debug)
1395 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1396
e22f895c 1397 send_cmd (SDI_STOP_CPU);
b4b4b794
KI
1398
1399 return;
1400}
1401
1402
37814c18
KI
1403/* Tell whether this target can support a hardware breakpoint. CNT
1404 is the number of hardware breakpoints already installed. This
d92524f1 1405 implements the target_can_use_hardware_watchpoint macro. */
b4b4b794 1406
63807e1d 1407static int
37814c18 1408m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
b4b4b794 1409{
37814c18 1410 return sdi_desc != NULL && cnt < max_access_breaks;
b4b4b794
KI
1411}
1412
1413/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1414 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1415 watchpoint. */
1416
63807e1d 1417static int
37814c18 1418m32r_insert_watchpoint (CORE_ADDR addr, int len, int type)
b4b4b794
KI
1419{
1420 int i;
1421
1422 if (remote_debug)
ec20a626 1423 fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
5af949e3 1424 paddress (target_gdbarch, addr), len, type);
b4b4b794
KI
1425
1426 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1427 {
1428 if (ab_address[i] == 0x00000000)
1429 {
1430 ab_address[i] = addr;
1431 ab_size[i] = len;
1432 ab_type[i] = type;
1433 return 0;
1434 }
1435 }
1436
8a3fe4f8 1437 error (_("Too many watchpoints"));
b4b4b794
KI
1438 return 1;
1439}
1440
63807e1d 1441static int
b4b4b794
KI
1442m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1443{
1444 int i;
1445
1446 if (remote_debug)
ec20a626 1447 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
5af949e3 1448 paddress (target_gdbarch, addr), len, type);
b4b4b794
KI
1449
1450 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1451 {
1452 if (ab_address[i] == addr)
1453 {
1454 ab_address[i] = 0x00000000;
1455 break;
1456 }
1457 }
1458
1459 return 0;
1460}
1461
63807e1d 1462static int
4aa7a7f5 1463m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
b4b4b794 1464{
4aa7a7f5
JJ
1465 int rc = 0;
1466 if (hit_watchpoint_addr != 0x00000000)
1467 {
1468 *addr_p = hit_watchpoint_addr;
1469 rc = 1;
1470 }
1471 return rc;
b4b4b794
KI
1472}
1473
63807e1d 1474static int
b4b4b794
KI
1475m32r_stopped_by_watchpoint (void)
1476{
4aa7a7f5
JJ
1477 CORE_ADDR addr;
1478 return m32r_stopped_data_address (&current_target, &addr);
b4b4b794
KI
1479}
1480
4d6c6261
PA
1481/* Check to see if a thread is still alive. */
1482
1483static int
28439f5e 1484m32r_thread_alive (struct target_ops *ops, ptid_t ptid)
4d6c6261
PA
1485{
1486 if (ptid_equal (ptid, remote_m32r_ptid))
1487 /* The main task is always alive. */
1488 return 1;
1489
1490 return 0;
1491}
1492
1493/* Convert a thread ID to a string. Returns the string in a static
1494 buffer. */
1495
1496static char *
117de6a9 1497m32r_pid_to_str (struct target_ops *ops, ptid_t ptid)
4d6c6261
PA
1498{
1499 static char buf[64];
1500
1501 if (ptid_equal (remote_m32r_ptid, ptid))
1502 {
1503 xsnprintf (buf, sizeof buf, "Thread <main>");
1504 return buf;
1505 }
1506
1507 return normal_pid_to_str (ptid);
1508}
b4b4b794
KI
1509
1510static void
1511sdireset_command (char *args, int from_tty)
1512{
b4b4b794
KI
1513 if (remote_debug)
1514 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1515
e22f895c 1516 send_cmd (SDI_OPEN);
b4b4b794
KI
1517
1518 inferior_ptid = null_ptid;
e5ef4d75 1519 delete_thread_silent (remote_m32r_ptid);
b4b4b794
KI
1520}
1521
1522
1523static void
1524sdistatus_command (char *args, int from_tty)
1525{
1526 unsigned char buf[4096];
1527 int i, c;
1528
1529 if (remote_debug)
1530 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1531
1532 if (!sdi_desc)
1533 return;
1534
e22f895c 1535 send_cmd (SDI_STATUS);
b4b4b794
KI
1536 for (i = 0; i < 4096; i++)
1537 {
1538 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1539 if (c < 0)
717eb1cf 1540 return;
b4b4b794
KI
1541 buf[i] = c;
1542 if (c == 0)
717eb1cf
AC
1543 break;
1544 }
b4b4b794
KI
1545
1546 printf_filtered ("%s", buf);
1547}
1548
1549
1550static void
1551debug_chaos_command (char *args, int from_tty)
1552{
1553 unsigned char buf[3];
1554
1555 buf[0] = SDI_SET_ATTR;
1556 buf[1] = SDI_ATTR_CACHE;
1557 buf[2] = SDI_CACHE_TYPE_CHAOS;
1558 send_data (buf, 3);
1559}
1560
1561
1562static void
1563use_debug_dma_command (char *args, int from_tty)
1564{
1565 unsigned char buf[3];
1566
1567 buf[0] = SDI_SET_ATTR;
1568 buf[1] = SDI_ATTR_MEM_ACCESS;
1569 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1570 send_data (buf, 3);
1571}
1572
1573static void
1574use_mon_code_command (char *args, int from_tty)
1575{
1576 unsigned char buf[3];
1577
1578 buf[0] = SDI_SET_ATTR;
1579 buf[1] = SDI_ATTR_MEM_ACCESS;
1580 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1581 send_data (buf, 3);
1582}
1583
1584
1585static void
1586use_ib_breakpoints_command (char *args, int from_tty)
1587{
1588 use_ib_breakpoints = 1;
1589}
1590
1591static void
1592use_dbt_breakpoints_command (char *args, int from_tty)
1593{
1594 use_ib_breakpoints = 0;
1595}
1596
c35b1492
PA
1597static int
1598m32r_return_one (struct target_ops *target)
1599{
1600 return 1;
1601}
b4b4b794
KI
1602
1603/* Define the target subroutine names */
1604
1605struct target_ops m32r_ops;
1606
1607static void
1608init_m32r_ops (void)
1609{
1610 m32r_ops.to_shortname = "m32rsdi";
1611 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1612 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1613 m32r_ops.to_open = m32r_open;
1614 m32r_ops.to_close = m32r_close;
1615 m32r_ops.to_detach = m32r_detach;
1616 m32r_ops.to_resume = m32r_resume;
1617 m32r_ops.to_wait = m32r_wait;
1618 m32r_ops.to_fetch_registers = m32r_fetch_register;
1619 m32r_ops.to_store_registers = m32r_store_register;
1620 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
c8e73a31 1621 m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
b4b4b794
KI
1622 m32r_ops.to_files_info = m32r_files_info;
1623 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1624 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
37814c18
KI
1625 m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1626 m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1627 m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1628 m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1629 m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
b4b4b794
KI
1630 m32r_ops.to_kill = m32r_kill;
1631 m32r_ops.to_load = m32r_load;
1632 m32r_ops.to_create_inferior = m32r_create_inferior;
1633 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1634 m32r_ops.to_stop = m32r_stop;
49d03eab 1635 m32r_ops.to_log_command = serial_log_command;
4d6c6261
PA
1636 m32r_ops.to_thread_alive = m32r_thread_alive;
1637 m32r_ops.to_pid_to_str = m32r_pid_to_str;
b4b4b794 1638 m32r_ops.to_stratum = process_stratum;
c35b1492
PA
1639 m32r_ops.to_has_all_memory = m32r_return_one;
1640 m32r_ops.to_has_memory = m32r_return_one;
1641 m32r_ops.to_has_stack = m32r_return_one;
1642 m32r_ops.to_has_registers = m32r_return_one;
1643 m32r_ops.to_has_execution = m32r_return_one;
b4b4b794
KI
1644 m32r_ops.to_magic = OPS_MAGIC;
1645};
1646
1647
1648extern initialize_file_ftype _initialize_remote_m32r;
1649
1650void
1651_initialize_remote_m32r (void)
1652{
1653 int i;
1654
1655 init_m32r_ops ();
1656
1657 /* Initialize breakpoints. */
1658 for (i = 0; i < MAX_BREAKPOINTS; i++)
1659 bp_address[i] = 0xffffffff;
1660
1661 /* Initialize access breaks. */
1662 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1663 ab_address[i] = 0x00000000;
1664
1665 add_target (&m32r_ops);
1666
1667 add_com ("sdireset", class_obscure, sdireset_command,
1bedd215 1668 _("Reset SDI connection."));
b4b4b794
KI
1669
1670 add_com ("sdistatus", class_obscure, sdistatus_command,
1bedd215 1671 _("Show status of SDI connection."));
b4b4b794
KI
1672
1673 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1bedd215 1674 _("Debug M32R/Chaos."));
b4b4b794
KI
1675
1676 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1bedd215 1677 _("Use debug DMA mem access."));
b4b4b794 1678 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1bedd215 1679 _("Use mon code mem access."));
b4b4b794
KI
1680
1681 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1bedd215 1682 _("Set breakpoints by IB break."));
b4b4b794 1683 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1bedd215 1684 _("Set breakpoints by dbt."));
e5ef4d75
PA
1685
1686 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1687 isn't 0. */
1688 remote_m32r_ptid = ptid_build (42000, 0, 42000);
b4b4b794 1689}
This page took 0.946249 seconds and 4 git commands to generate.