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