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