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