Update FSF address.
[deliverable/binutils-gdb.git] / gdb / remote-mm.c
1 /* Remote debugging interface for Am290*0 running MiniMON monitor, for GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Originally written by Daniel Mann at AMD.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* This is like remote.c but ecpects MiniMON to be running on the Am29000
22 target hardware.
23 - David Wood (wood@lab.ultra.nyu.edu) at New York University adapted this
24 file to gdb 3.95. I was unable to get this working on sun3os4
25 with termio, only with sgtty. Because we are only attempting to
26 use this module to debug our kernel, which is already loaded when
27 gdb is started up, I did not code up the file downloading facilities.
28 As a result this module has only the stubs to download files.
29 You should get tagged at compile time if you need to make any
30 changes/additions. */
31
32 #include "defs.h"
33 #include "inferior.h"
34 #include "wait.h"
35 #include "value.h"
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include "gdb_string.h"
41 #include "terminal.h"
42 #include "minimon.h"
43 #include "target.h"
44
45 /* Offset of member MEMBER in a struct of type TYPE. */
46 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
47
48 #define DRAIN_INPUT() (msg_recv_serial((union msg_t*)0))
49
50 extern int stop_soon_quietly; /* for wait_for_inferior */
51
52 static void mm_resume();
53 static void mm_fetch_registers ();
54 static int fetch_register ();
55 static void mm_store_registers ();
56 static int store_register ();
57 static int regnum_to_srnum();
58 static void mm_close ();
59 static char* msg_str();
60 static char* error_msg_str();
61 static int expect_msg();
62 static void init_target_mm();
63 static int mm_memory_space();
64
65 #define FREEZE_MODE (read_register(CPS_REGNUM) && 0x400)
66 #define USE_SHADOW_PC ((processor_type == a29k_freeze_mode) && FREEZE_MODE)
67
68 /* FIXME: Replace with `set remotedebug'. */
69 #define LLOG_FILE "minimon.log"
70 #if defined (LOG_FILE)
71 FILE *log_file;
72 #endif
73
74 /*
75 * Size of message buffers. I couldn't get memory reads to work when
76 * the byte_count was larger than 512 (it may be a baud rate problem).
77 */
78 #define BUFER_SIZE 512
79 /*
80 * Size of data area in message buffer on the TARGET (remote system).
81 */
82 #define MAXDATA_T (target_config.max_msg_size - \
83 offsetof(struct write_r_msg_t,data[0]))
84 /*
85 * Size of data area in message buffer on the HOST (gdb).
86 */
87 #define MAXDATA_H (BUFER_SIZE - offsetof(struct write_r_msg_t,data[0]))
88 /*
89 * Defined as the minimum size of data areas of the two message buffers
90 */
91 #define MAXDATA (MAXDATA_H < MAXDATA_T ? MAXDATA_H : MAXDATA_T)
92
93 static char out_buf[BUFER_SIZE];
94 static char in_buf[BUFER_SIZE];
95
96 int msg_recv_serial();
97 int msg_send_serial();
98
99 #define MAX_RETRIES 5000
100 extern struct target_ops mm_ops; /* Forward declaration */
101 struct config_msg_t target_config; /* HIF needs this */
102 union msg_t *out_msg_buf = (union msg_t*)out_buf;
103 union msg_t *in_msg_buf = (union msg_t*)in_buf;
104
105 static int timeout = 5;
106
107 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
108 mm_open knows that we don't have a file open when the program
109 starts. */
110 int mm_desc = -1;
111
112 /* stream which is fdopen'd from mm_desc. Only valid when
113 mm_desc != -1. */
114 FILE *mm_stream;
115
116 /* Called when SIGALRM signal sent due to alarm() timeout. */
117 #ifndef HAVE_TERMIO
118
119 #ifndef __STDC__
120 # ifndef volatile
121 # define volatile /**/
122 # endif
123 #endif
124 volatile int n_alarms;
125
126 static void
127 mm_timer ()
128 {
129 #if 0
130 if (kiodebug)
131 printf ("mm_timer called\n");
132 #endif
133 n_alarms++;
134 }
135 #endif /* HAVE_TERMIO */
136
137 /* malloc'd name of the program on the remote system. */
138 static char *prog_name = NULL;
139
140
141 /* Number of SIGTRAPs we need to simulate. That is, the next
142 NEED_ARTIFICIAL_TRAP calls to mm_wait should just return
143 SIGTRAP without actually waiting for anything. */
144
145 /**************************************************** REMOTE_CREATE_INFERIOR */
146 /* This is called not only when we first attach, but also when the
147 user types "run" after having attached. */
148 static void
149 mm_create_inferior (execfile, args, env)
150 char *execfile;
151 char *args;
152 char **env;
153 {
154 #define MAX_TOKENS 25
155 #define BUFFER_SIZE 256
156 int token_count;
157 int result;
158 char *token[MAX_TOKENS];
159 char cmd_line[BUFFER_SIZE];
160
161 if (args && *args)
162 error ("Can't pass arguments to remote mm process (yet).");
163
164 if (execfile == 0 /* || exec_bfd == 0 */ )
165 error ("No exec file specified");
166
167 if (!mm_stream) {
168 printf("Minimon not open yet.\n");
169 return;
170 }
171
172 /* On ultra3 (NYU) we assume the kernel is already running so there is
173 no file to download.
174 FIXME: Fixed required here -> load your program, possibly with mm_load().
175 */
176 printf_filtered ("\n\
177 Assuming you are at NYU debuging a kernel, i.e., no need to download.\n\n");
178
179 /* We will get a task spawn event immediately. */
180 init_wait_for_inferior ();
181 clear_proceed_status ();
182 stop_soon_quietly = 1;
183 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
184 normal_stop ();
185 }
186 /**************************************************** REMOTE_MOURN_INFERIOR */
187 static void
188 mm_mourn()
189 {
190 pop_target (); /* Pop back to no-child state */
191 generic_mourn_inferior ();
192 }
193
194 /********************************************************************** damn_b
195 */
196 /* Translate baud rates from integers to damn B_codes. Unix should
197 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
198
199 #ifndef B19200
200 #define B19200 EXTA
201 #endif
202 #ifndef B38400
203 #define B38400 EXTB
204 #endif
205
206 static struct {int rate, damn_b;} baudtab[] = {
207 {0, B0},
208 {50, B50},
209 {75, B75},
210 {110, B110},
211 {134, B134},
212 {150, B150},
213 {200, B200},
214 {300, B300},
215 {600, B600},
216 {1200, B1200},
217 {1800, B1800},
218 {2400, B2400},
219 {4800, B4800},
220 {9600, B9600},
221 {19200, B19200},
222 {38400, B38400},
223 {-1, -1},
224 };
225
226 static int damn_b (rate)
227 int rate;
228 {
229 int i;
230
231 for (i = 0; baudtab[i].rate != -1; i++)
232 if (rate == baudtab[i].rate) return baudtab[i].damn_b;
233 return B38400; /* Random */
234 }
235
236
237 /***************************************************************** REMOTE_OPEN
238 ** Open a connection to remote minimon.
239 NAME is the filename used for communication, then a space,
240 then the baud rate.
241 'target adapt /dev/ttya 9600 [prognam]' for example.
242 */
243
244 static char *dev_name;
245 int baudrate = 9600;
246 static void
247 mm_open (name, from_tty)
248 char *name;
249 int from_tty;
250 {
251 TERMINAL sg;
252 unsigned int prl;
253 char *p;
254
255 /* Find the first whitespace character, it separates dev_name from
256 prog_name. */
257 for (p = name;
258 p && *p && !isspace (*p); p++)
259 ;
260 if (p == 0 || *p == '\0')
261 erroid:
262 error ("Usage : <command> <serial-device> <baud-rate> [progname]");
263 dev_name = (char*)malloc (p - name + 1);
264 strncpy (dev_name, name, p - name);
265 dev_name[p - name] = '\0';
266
267 /* Skip over the whitespace after dev_name */
268 for (; isspace (*p); p++)
269 /*EMPTY*/;
270
271 if (1 != sscanf (p, "%d ", &baudrate))
272 goto erroid;
273
274 /* Skip the number and then the spaces */
275 for (; isdigit (*p); p++)
276 /*EMPTY*/;
277 for (; isspace (*p); p++)
278 /*EMPTY*/;
279
280 if (prog_name != NULL)
281 free (prog_name);
282 prog_name = savestring (p, strlen (p));
283
284
285 if (mm_desc >= 0)
286 close (mm_desc);
287
288 mm_desc = open (dev_name, O_RDWR);
289 if (mm_desc < 0)
290 perror_with_name (dev_name);
291 ioctl (mm_desc, TIOCGETP, &sg);
292 #ifdef HAVE_TERMIO
293 sg.c_cc[VMIN] = 0; /* read with timeout. */
294 sg.c_cc[VTIME] = timeout * 10;
295 sg.c_lflag &= ~(ICANON | ECHO);
296 sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
297 #else
298 sg.sg_ispeed = damn_b (baudrate);
299 sg.sg_ospeed = damn_b (baudrate);
300 sg.sg_flags |= RAW;
301 sg.sg_flags |= ANYP;
302 sg.sg_flags &= ~ECHO;
303 #endif
304
305
306 ioctl (mm_desc, TIOCSETP, &sg);
307 mm_stream = fdopen (mm_desc, "r+");
308
309 push_target (&mm_ops);
310
311 #ifndef HAVE_TERMIO
312 #ifndef NO_SIGINTERRUPT
313 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
314 the read. */
315 if (siginterrupt (SIGALRM, 1) != 0)
316 perror ("mm_open: error in siginterrupt");
317 #endif
318
319 /* Set up read timeout timer. */
320 if ((void (*)) signal (SIGALRM, mm_timer) == (void (*)) -1)
321 perror ("mm_open: error in signal");
322 #endif
323
324 #if defined (LOG_FILE)
325 log_file = fopen (LOG_FILE, "w");
326 if (log_file == NULL)
327 perror_with_name (LOG_FILE);
328 #endif
329 /*
330 ** Initialize target configuration structure (global)
331 */
332 DRAIN_INPUT();
333 out_msg_buf->config_req_msg.code = CONFIG_REQ;
334 out_msg_buf->config_req_msg.length = 4*0;
335 msg_send_serial(out_msg_buf); /* send config request message */
336
337 expect_msg(CONFIG,in_msg_buf,1);
338
339 a29k_get_processor_type ();
340
341 /* Print out some stuff, letting the user now what's going on */
342 printf_filtered("Connected to MiniMon via %s.\n", dev_name);
343 /* FIXME: can this restriction be removed? */
344 printf_filtered("Remote debugging using virtual addresses works only\n");
345 printf_filtered("\twhen virtual addresses map 1:1 to physical addresses.\n")
346 ;
347 if (processor_type != a29k_freeze_mode) {
348 fprintf_filtered(stderr,
349 "Freeze-mode debugging not available, and can only be done on an A29050.\n");
350 }
351
352 target_config.code = CONFIG;
353 target_config.length = 0;
354 target_config.processor_id = in_msg_buf->config_msg.processor_id;
355 target_config.version = in_msg_buf->config_msg.version;
356 target_config.I_mem_start = in_msg_buf->config_msg.I_mem_start;
357 target_config.I_mem_size = in_msg_buf->config_msg.I_mem_size;
358 target_config.D_mem_start = in_msg_buf->config_msg.D_mem_start;
359 target_config.D_mem_size = in_msg_buf->config_msg.D_mem_size;
360 target_config.ROM_start = in_msg_buf->config_msg.ROM_start;
361 target_config.ROM_size = in_msg_buf->config_msg.ROM_size;
362 target_config.max_msg_size = in_msg_buf->config_msg.max_msg_size;
363 target_config.max_bkpts = in_msg_buf->config_msg.max_bkpts;
364 target_config.coprocessor = in_msg_buf->config_msg.coprocessor;
365 target_config.reserved = in_msg_buf->config_msg.reserved;
366 if (from_tty) {
367 printf("Connected to MiniMON :\n");
368 printf(" Debugcore version %d.%d\n",
369 0x0f & (target_config.version >> 4),
370 0x0f & (target_config.version ) );
371 printf(" Configuration version %d.%d\n",
372 0x0f & (target_config.version >> 12),
373 0x0f & (target_config.version >> 8) );
374 printf(" Message system version %d.%d\n",
375 0x0f & (target_config.version >> 20),
376 0x0f & (target_config.version >> 16) );
377 printf(" Communication driver version %d.%d\n",
378 0x0f & (target_config.version >> 28),
379 0x0f & (target_config.version >> 24) );
380 }
381
382 /* Leave the target running...
383 * The above message stopped the target in the dbg core (MiniMon),
384 * so restart the target out of MiniMon,
385 */
386 out_msg_buf->go_msg.code = GO;
387 out_msg_buf->go_msg.length = 0;
388 msg_send_serial(out_msg_buf);
389 /* No message to expect after a GO */
390 }
391
392 /**************************************************************** REMOTE_CLOSE
393 ** Close the open connection to the minimon debugger.
394 Use this when you want to detach and do something else
395 with your gdb. */
396 static void
397 mm_close (quitting) /*FIXME: how is quitting used */
398 int quitting;
399 {
400 if (mm_desc < 0)
401 error ("Can't close remote connection: not debugging remotely.");
402
403 /* We should never get here if there isn't something valid in
404 mm_desc and mm_stream.
405
406 Due to a bug in Unix, fclose closes not only the stdio stream,
407 but also the file descriptor. So we don't actually close
408 mm_desc. */
409 DRAIN_INPUT();
410 fclose (mm_stream);
411 /* close (mm_desc); */
412
413 /* Do not try to close mm_desc again, later in the program. */
414 mm_stream = NULL;
415 mm_desc = -1;
416
417 #if defined (LOG_FILE)
418 if (ferror (log_file))
419 printf ("Error writing log file.\n");
420 if (fclose (log_file) != 0)
421 printf ("Error closing log file.\n");
422 #endif
423
424 printf ("Ending remote debugging\n");
425 }
426
427 /************************************************************* REMOTE_ATACH */
428 /* Attach to a program that is already loaded and running
429 * Upon exiting the process's execution is stopped.
430 */
431 static void
432 mm_attach (args, from_tty)
433 char *args;
434 int from_tty;
435 {
436
437 if (!mm_stream)
438 error ("MiniMon not opened yet, use the 'target minimon' command.\n");
439
440 if (from_tty)
441 printf ("Attaching to remote program %s...\n", prog_name);
442
443 /* Make sure the target is currently running, it is supposed to be. */
444 /* FIXME: is it ok to send MiniMon a BREAK if it is already stopped in
445 * the dbg core. If so, we don't need to send this GO.
446 */
447 out_msg_buf->go_msg.code = GO;
448 out_msg_buf->go_msg.length = 0;
449 msg_send_serial(out_msg_buf);
450 sleep(2); /* At the worst it will stop, receive a message, continue */
451
452 /* Send the mm a break. */
453 out_msg_buf->break_msg.code = BREAK;
454 out_msg_buf->break_msg.length = 0;
455 msg_send_serial(out_msg_buf);
456 }
457 /********************************************************** REMOTE_DETACH */
458 /* Terminate the open connection to the remote debugger.
459 Use this when you want to detach and do something else
460 with your gdb. Leave remote process running (with no breakpoints set). */
461 static void
462 mm_detach (args,from_tty)
463 char *args;
464 int from_tty;
465 {
466 remove_breakpoints(); /* Just in case there were any left in */
467 out_msg_buf->go_msg.code = GO;
468 out_msg_buf->go_msg.length = 0;
469 msg_send_serial(out_msg_buf);
470 pop_target(); /* calls mm_close to do the real work */
471 }
472
473
474 /*************************************************************** REMOTE_RESUME
475 ** Tell the remote machine to resume. */
476
477 static void
478 mm_resume (pid, step, sig)
479 int pid, step;
480 enum target_signal sig;
481 {
482 if (sig != TARGET_SIGNAL_0)
483 warning ("Can't send signals to a remote MiniMon system.");
484
485 if (step) {
486 out_msg_buf->step_msg.code= STEP;
487 out_msg_buf->step_msg.length = 1*4;
488 out_msg_buf->step_msg.count = 1; /* step 1 instruction */
489 msg_send_serial(out_msg_buf);
490 } else {
491 out_msg_buf->go_msg.code= GO;
492 out_msg_buf->go_msg.length = 0;
493 msg_send_serial(out_msg_buf);
494 }
495 }
496
497 /***************************************************************** REMOTE_WAIT
498 ** Wait until the remote machine stops, then return,
499 storing status in STATUS just as `wait' would. */
500
501 static int
502 mm_wait (status)
503 struct target_waitstatus *status;
504 {
505 int i, result;
506 int old_timeout = timeout;
507 int old_immediate_quit = immediate_quit;
508
509 status->kind = TARGET_WAITKIND_EXITED;
510 status->value.integer = 0;
511
512 /* wait for message to arrive. It should be:
513 - A HIF service request.
514 - A HIF exit service request.
515 - A CHANNEL0_ACK.
516 - A CHANNEL1 request.
517 - a debugcore HALT message.
518 HIF services must be responded too, and while-looping continued.
519 If the target stops executing, mm_wait() should return.
520 */
521 timeout = 0; /* Wait indefinetly for a message */
522 immediate_quit = 1; /* Helps ability to QUIT */
523 while(1)
524 {
525 while(msg_recv_serial(in_msg_buf)) {
526 QUIT; /* Let user quit if they want */
527 }
528 switch (in_msg_buf->halt_msg.code)
529 {
530 case HIF_CALL:
531 i = in_msg_buf->hif_call_rtn_msg.service_number;
532 result=service_HIF(in_msg_buf);
533 if(i == 1) /* EXIT */
534 goto exit;
535 if(result)
536 printf("Warning: failure during HIF service %d\n", i);
537 break;
538 case CHANNEL0_ACK:
539 service_HIF(in_msg_buf);
540 break;
541 case CHANNEL1:
542 i=in_msg_buf->channel1_msg.length;
543 in_msg_buf->channel1_msg.data[i] = '\0';
544 printf("%s", in_msg_buf->channel1_msg.data);
545 gdb_flush(stdout);
546 /* Send CHANNEL1_ACK message */
547 out_msg_buf->channel1_ack_msg.code = CHANNEL1_ACK;
548 out_msg_buf->channel1_ack_msg.length = 0;
549 result = msg_send_serial(out_msg_buf);
550 break;
551 case HALT:
552 goto halted;
553 default:
554 goto halted;
555 }
556 }
557 halted:
558 /* FIXME, these printfs should not be here. This is a source level
559 debugger, guys! */
560 if (in_msg_buf->halt_msg.trap_number== 0)
561 { printf("Am290*0 received vector number %d (break point)\n",
562 in_msg_buf->halt_msg.trap_number);
563 status->kind = TARGET_WAITKIND_STOPPED;
564 status->value.sig = TARGET_SIGNAL_TRAP;
565 }
566 else if (in_msg_buf->halt_msg.trap_number== 1)
567 {
568 printf("Am290*0 received vector number %d\n",
569 in_msg_buf->halt_msg.trap_number);
570 status->kind = TARGET_WAITKIND_STOPPED;
571 status->value.sig = TARGET_SIGNAL_BUS;
572 }
573 else if (in_msg_buf->halt_msg.trap_number== 3
574 || in_msg_buf->halt_msg.trap_number== 4)
575 { printf("Am290*0 received vector number %d\n",
576 in_msg_buf->halt_msg.trap_number);
577 status->kind = TARGET_WAITKIND_STOPPED;
578 status->value.sig = TARGET_SIGNAL_FPE;
579 }
580 else if (in_msg_buf->halt_msg.trap_number== 5)
581 { printf("Am290*0 received vector number %d\n",
582 in_msg_buf->halt_msg.trap_number);
583 status->kind = TARGET_WAITKIND_STOPPED;
584 status->value.sig = TARGET_SIGNAL_ILL;
585 }
586 else if (in_msg_buf->halt_msg.trap_number >= 6
587 && in_msg_buf->halt_msg.trap_number <= 11)
588 { printf("Am290*0 received vector number %d\n",
589 in_msg_buf->halt_msg.trap_number);
590 status->kind = TARGET_WAITKIND_STOPPED;
591 status->value.sig = TARGET_SIGNAL_SEGV;
592 }
593 else if (in_msg_buf->halt_msg.trap_number== 12
594 || in_msg_buf->halt_msg.trap_number== 13)
595 { printf("Am290*0 received vector number %d\n",
596 in_msg_buf->halt_msg.trap_number);
597 status->kind = TARGET_WAITKIND_STOPPED;
598 status->value.sig = TARGET_SIGNAL_ILL;
599 }
600 else if (in_msg_buf->halt_msg.trap_number== 14)
601 { printf("Am290*0 received vector number %d\n",
602 in_msg_buf->halt_msg.trap_number);
603 status->kind = TARGET_WAITKIND_STOPPED;
604 status->value.sig = TARGET_SIGNAL_ALRM;
605 }
606 else if (in_msg_buf->halt_msg.trap_number== 15)
607 {
608 status->kind = TARGET_WAITKIND_STOPPED;
609 status->value.sig = TARGET_SIGNAL_TRAP;
610 }
611 else if (in_msg_buf->halt_msg.trap_number >= 16
612 && in_msg_buf->halt_msg.trap_number <= 21)
613 { printf("Am290*0 received vector number %d\n",
614 in_msg_buf->halt_msg.trap_number);
615 status->kind = TARGET_WAITKIND_STOPPED;
616 status->value.sig = TARGET_SIGNAL_INT;
617 }
618 else if (in_msg_buf->halt_msg.trap_number== 22)
619 { printf("Am290*0 received vector number %d\n",
620 in_msg_buf->halt_msg.trap_number);
621 status->kind = TARGET_WAITKIND_STOPPED;
622 status->value.sig = TARGET_SIGNAL_ILL;
623 } /* BREAK message was sent */
624 else if (in_msg_buf->halt_msg.trap_number== 75)
625 {
626 status->kind = TARGET_WAITKIND_STOPPED;
627 status->value.sig = TARGET_SIGNAL_TRAP;
628 }
629 else
630 exit:
631 {
632 status->kind = TARGET_WAITKIND_EXITED;
633 status->value.integer = 0;
634 }
635
636 timeout = old_timeout; /* Restore original timeout value */
637 immediate_quit = old_immediate_quit;
638 return 0;
639 }
640
641 /******************************************************* REMOTE_FETCH_REGISTERS
642 * Read a remote register 'regno'.
643 * If regno==-1 then read all the registers.
644 */
645 static void
646 mm_fetch_registers (regno)
647 int regno;
648 {
649 INT32 *data_p;
650
651 if (regno >= 0) {
652 fetch_register(regno);
653 return;
654 }
655
656 /* Gr1/rsp */
657 out_msg_buf->read_req_msg.byte_count = 4*1;
658 out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
659 out_msg_buf->read_req_msg.address = 1;
660 msg_send_serial(out_msg_buf);
661 expect_msg(READ_ACK,in_msg_buf,1);
662 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
663 supply_register (GR1_REGNUM , data_p);
664
665 #if defined(GR64_REGNUM) /* Read gr64-127 */
666 /* Global Registers gr64-gr95 */
667 out_msg_buf->read_req_msg.code= READ_REQ;
668 out_msg_buf->read_req_msg.length = 4*3;
669 out_msg_buf->read_req_msg.byte_count = 4*32;
670 out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
671 out_msg_buf->read_req_msg.address = 64;
672 msg_send_serial(out_msg_buf);
673 expect_msg(READ_ACK,in_msg_buf,1);
674 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
675
676 for (regno=GR64_REGNUM; regno<GR64_REGNUM+32; regno++) {
677 supply_register (regno, data_p++);
678 }
679 #endif /* GR64_REGNUM */
680
681 /* Global Registers gr96-gr127 */
682 out_msg_buf->read_req_msg.code= READ_REQ;
683 out_msg_buf->read_req_msg.length = 4*3;
684 out_msg_buf->read_req_msg.byte_count = 4 * 32;
685 out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
686 out_msg_buf->read_req_msg.address = 96;
687 msg_send_serial(out_msg_buf);
688 expect_msg(READ_ACK,in_msg_buf,1);
689 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
690
691 for (regno=GR96_REGNUM; regno<GR96_REGNUM+32; regno++) {
692 supply_register (regno, data_p++);
693 }
694
695 /* Local Registers */
696 out_msg_buf->read_req_msg.byte_count = 4 * (128);
697 out_msg_buf->read_req_msg.memory_space = LOCAL_REG;
698 out_msg_buf->read_req_msg.address = 0;
699 msg_send_serial(out_msg_buf);
700 expect_msg(READ_ACK,in_msg_buf,1);
701 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
702
703 for (regno=LR0_REGNUM; regno<LR0_REGNUM+128; regno++) {
704 supply_register (regno, data_p++);
705 }
706
707 /* Protected Special Registers */
708 out_msg_buf->read_req_msg.byte_count = 4*15;
709 out_msg_buf->read_req_msg.memory_space = SPECIAL_REG;
710 out_msg_buf->read_req_msg.address = 0;
711 msg_send_serial( out_msg_buf);
712 expect_msg(READ_ACK,in_msg_buf,1);
713 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
714
715 for (regno=0; regno<=14; regno++) {
716 supply_register (SR_REGNUM(regno), data_p++);
717 }
718 if (USE_SHADOW_PC) { /* Let regno_to_srnum() handle the register number */
719 fetch_register(NPC_REGNUM);
720 fetch_register(PC_REGNUM);
721 fetch_register(PC2_REGNUM);
722 }
723
724 /* Unprotected Special Registers */
725 out_msg_buf->read_req_msg.byte_count = 4*8;
726 out_msg_buf->read_req_msg.memory_space = SPECIAL_REG;
727 out_msg_buf->read_req_msg.address = 128;
728 msg_send_serial( out_msg_buf);
729 expect_msg(READ_ACK,in_msg_buf,1);
730 data_p = &(in_msg_buf->read_r_ack_msg.data[0]);
731
732 for (regno=128; regno<=135; regno++) {
733 supply_register (SR_REGNUM(regno), data_p++);
734 }
735
736 /* There doesn't seem to be any way to get these. */
737 {
738 int val = -1;
739 supply_register (FPE_REGNUM, &val);
740 supply_register (INTE_REGNUM, &val);
741 supply_register (FPS_REGNUM, &val);
742 supply_register (EXO_REGNUM, &val);
743 }
744 }
745
746
747 /****************************************************** REMOTE_STORE_REGISTERS
748 * Store register regno into the target.
749 * If regno==-1 then store all the registers.
750 * Result is 0 for success, -1 for failure.
751 */
752
753 static void
754 mm_store_registers (regno)
755 int regno;
756 {
757 int result;
758
759 if (regno >= 0) {
760 store_register(regno);
761 return;
762 }
763
764 result = 0;
765
766 out_msg_buf->write_r_msg.code= WRITE_REQ;
767
768 /* Gr1/rsp */
769 out_msg_buf->write_r_msg.byte_count = 4*1;
770 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
771 out_msg_buf->write_r_msg.memory_space = GLOBAL_REG;
772 out_msg_buf->write_r_msg.address = 1;
773 out_msg_buf->write_r_msg.data[0] = read_register (GR1_REGNUM);
774
775 msg_send_serial( out_msg_buf);
776 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
777 result = -1;
778 }
779
780 #if defined(GR64_REGNUM)
781 /* Global registers gr64-gr95 */
782 out_msg_buf->write_r_msg.byte_count = 4* (32);
783 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
784 out_msg_buf->write_r_msg.address = 64;
785
786 for (regno=GR64_REGNUM ; regno<GR64_REGNUM+32 ; regno++)
787 {
788 out_msg_buf->write_r_msg.data[regno-GR64_REGNUM] = read_register (regno);
789 }
790 msg_send_serial(out_msg_buf);
791 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
792 result = -1;
793 }
794 #endif /* GR64_REGNUM */
795
796 /* Global registers gr96-gr127 */
797 out_msg_buf->write_r_msg.byte_count = 4* (32);
798 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
799 out_msg_buf->write_r_msg.address = 96;
800 for (regno=GR96_REGNUM ; regno<GR96_REGNUM+32 ; regno++)
801 {
802 out_msg_buf->write_r_msg.data[regno-GR96_REGNUM] = read_register (regno);
803 }
804 msg_send_serial( out_msg_buf);
805 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
806 result = -1;
807 }
808
809 /* Local Registers */
810 out_msg_buf->write_r_msg.memory_space = LOCAL_REG;
811 out_msg_buf->write_r_msg.byte_count = 4*128;
812 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
813 out_msg_buf->write_r_msg.address = 0;
814
815 for (regno = LR0_REGNUM ; regno < LR0_REGNUM+128 ; regno++)
816 {
817 out_msg_buf->write_r_msg.data[regno-LR0_REGNUM] = read_register (regno);
818 }
819 msg_send_serial( out_msg_buf);
820 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
821 result = -1;
822 }
823
824 /* Protected Special Registers */
825 /* VAB through TMR */
826 out_msg_buf->write_r_msg.memory_space = SPECIAL_REG;
827 out_msg_buf->write_r_msg.byte_count = 4* 10;
828 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
829 out_msg_buf->write_r_msg.address = 0;
830 for (regno = 0 ; regno<=9 ; regno++) /* VAB through TMR */
831 out_msg_buf->write_r_msg.data[regno] = read_register (SR_REGNUM(regno));
832 msg_send_serial( out_msg_buf);
833 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
834 result = -1;
835 }
836
837 /* PC0, PC1, PC2 possibly as shadow registers */
838 out_msg_buf->write_r_msg.byte_count = 4* 3;
839 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
840 for (regno=10 ; regno<=12 ; regno++) /* LRU and MMU */
841 out_msg_buf->write_r_msg.data[regno-10] = read_register (SR_REGNUM(regno));
842 if (USE_SHADOW_PC)
843 out_msg_buf->write_r_msg.address = 20; /* SPC0 */
844 else
845 out_msg_buf->write_r_msg.address = 10; /* PC0 */
846 msg_send_serial( out_msg_buf);
847 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
848 result = -1;
849 }
850
851 /* LRU and MMU */
852 out_msg_buf->write_r_msg.byte_count = 4* 2;
853 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
854 out_msg_buf->write_r_msg.address = 13;
855 for (regno=13 ; regno<=14 ; regno++) /* LRU and MMU */
856 out_msg_buf->write_r_msg.data[regno-13] = read_register (SR_REGNUM(regno));
857 msg_send_serial( out_msg_buf);
858 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
859 result = -1;
860 }
861
862 /* Unprotected Special Registers */
863 out_msg_buf->write_r_msg.byte_count = 4*8;
864 out_msg_buf->write_r_msg.length = 3*4 + out_msg_buf->write_r_msg.byte_count;
865 out_msg_buf->write_r_msg.address = 128;
866 for (regno = 128 ; regno<=135 ; regno++)
867 out_msg_buf->write_r_msg.data[regno-128] = read_register(SR_REGNUM(regno));
868 msg_send_serial( out_msg_buf);
869 if (!expect_msg(WRITE_ACK,in_msg_buf,1)) {
870 result = -1;
871 }
872
873 registers_changed ();
874 }
875
876 /*************************************************** REMOTE_PREPARE_TO_STORE */
877 /* Get ready to modify the registers array. On machines which store
878 individual registers, this doesn't need to do anything. On machines
879 which store all the registers in one fell swoop, this makes sure
880 that registers contains all the registers from the program being
881 debugged. */
882
883 static void
884 mm_prepare_to_store ()
885 {
886 /* Do nothing, since we can store individual regs */
887 }
888
889 /******************************************************* REMOTE_XFER_MEMORY */
890 static CORE_ADDR
891 translate_addr(addr)
892 CORE_ADDR addr;
893 {
894 #if defined(KERNEL_DEBUGGING)
895 /* Check for a virtual address in the kernel */
896 /* Assume physical address of ublock is in paddr_u register */
897 /* FIXME: doesn't work for user virtual addresses */
898 if (addr >= UVADDR) {
899 /* PADDR_U register holds the physical address of the ublock */
900 CORE_ADDR i = (CORE_ADDR)read_register(PADDR_U_REGNUM);
901 return(i + addr - (CORE_ADDR)UVADDR);
902 } else {
903 return(addr);
904 }
905 #else
906 return(addr);
907 #endif
908 }
909
910 /******************************************************* REMOTE_FILES_INFO */
911 static void
912 mm_files_info ()
913 {
914 printf ("\tAttached to %s at %d baud and running program %s.\n",
915 dev_name, baudrate, prog_name);
916 }
917
918 /************************************************* REMOTE_INSERT_BREAKPOINT */
919 static int
920 mm_insert_breakpoint (addr, contents_cache)
921 CORE_ADDR addr;
922 char *contents_cache;
923 {
924 out_msg_buf->bkpt_set_msg.code = BKPT_SET;
925 out_msg_buf->bkpt_set_msg.length = 4*4;
926 out_msg_buf->bkpt_set_msg.memory_space = I_MEM;
927 out_msg_buf->bkpt_set_msg.bkpt_addr = (ADDR32) addr;
928 out_msg_buf->bkpt_set_msg.pass_count = 1;
929 out_msg_buf->bkpt_set_msg.bkpt_type = -1; /* use illop for 29000 */
930 msg_send_serial( out_msg_buf);
931 if (expect_msg(BKPT_SET_ACK,in_msg_buf,1)) {
932 return 0; /* Success */
933 } else {
934 return 1; /* Failure */
935 }
936 }
937
938 /************************************************* REMOTE_DELETE_BREAKPOINT */
939 static int
940 mm_remove_breakpoint (addr, contents_cache)
941 CORE_ADDR addr;
942 char *contents_cache;
943 {
944 out_msg_buf->bkpt_rm_msg.code = BKPT_RM;
945 out_msg_buf->bkpt_rm_msg.length = 4*3;
946 out_msg_buf->bkpt_rm_msg.memory_space = I_MEM;
947 out_msg_buf->bkpt_rm_msg.bkpt_addr = (ADDR32) addr;
948 msg_send_serial( out_msg_buf);
949 if (expect_msg(BKPT_RM_ACK,in_msg_buf,1)) {
950 return 0; /* Success */
951 } else {
952 return 1; /* Failure */
953 }
954 }
955
956
957 /******************************************************* REMOTE_KILL */
958 static void
959 mm_kill(arg,from_tty)
960 char *arg;
961 int from_tty;
962 {
963 char buf[4];
964
965 #if defined(KERNEL_DEBUGGING)
966 /* We don't ever kill the kernel */
967 if (from_tty) {
968 printf("Kernel not killed, but left in current state.\n");
969 printf("Use detach to leave kernel running.\n");
970 }
971 #else
972 out_msg_buf->break_msg.code = BREAK;
973 out_msg_buf->bkpt_set_msg.length = 4*0;
974 expect_msg(HALT,in_msg_buf,from_tty);
975 if (from_tty) {
976 printf("Target has been stopped.");
977 printf("Would you like to do a hardware reset (y/n) [n] ");
978 fgets(buf,3,stdin);
979 if (buf[0] == 'y') {
980 out_msg_buf->reset_msg.code = RESET;
981 out_msg_buf->bkpt_set_msg.length = 4*0;
982 expect_msg(RESET_ACK,in_msg_buf,from_tty);
983 printf("Target has been reset.");
984 }
985 }
986 pop_target();
987 #endif
988 }
989
990
991
992 /***************************************************************************/
993 /*
994 * Load a program into the target.
995 */
996 static void
997 mm_load(arg_string,from_tty)
998 char *arg_string;
999 int from_tty;
1000 {
1001 dont_repeat ();
1002
1003 #if defined(KERNEL_DEBUGGING)
1004 printf("The kernel had better be loaded already! Loading not done.\n");
1005 #else
1006 if (arg_string == 0)
1007 error ("The load command takes a file name");
1008
1009 arg_string = tilde_expand (arg_string);
1010 make_cleanup (free, arg_string);
1011 QUIT;
1012 immediate_quit++;
1013 error("File loading is not yet supported for MiniMon.");
1014 /* FIXME, code to load your file here... */
1015 /* You may need to do an init_target_mm() */
1016 /* init_target_mm(?,?,?,?,?,?,?,?); */
1017 immediate_quit--;
1018 /* symbol_file_add (arg_string, from_tty, text_addr, 0, 0); */
1019 #endif
1020
1021 }
1022
1023 /************************************************ REMOTE_WRITE_INFERIOR_MEMORY
1024 ** Copy LEN bytes of data from debugger memory at MYADDR
1025 to inferior's memory at MEMADDR. Returns number of bytes written. */
1026 static int
1027 mm_write_inferior_memory (memaddr, myaddr, len)
1028 CORE_ADDR memaddr;
1029 char *myaddr;
1030 int len;
1031 {
1032 int i,nwritten;
1033
1034 out_msg_buf->write_req_msg.code= WRITE_REQ;
1035 out_msg_buf->write_req_msg.memory_space = mm_memory_space(memaddr);
1036
1037 nwritten=0;
1038 while (nwritten < len) {
1039 int num_to_write = len - nwritten;
1040 if (num_to_write > MAXDATA) num_to_write = MAXDATA;
1041 for (i=0 ; i < num_to_write ; i++)
1042 out_msg_buf->write_req_msg.data[i] = myaddr[i+nwritten];
1043 out_msg_buf->write_req_msg.byte_count = num_to_write;
1044 out_msg_buf->write_req_msg.length = 3*4 + num_to_write;
1045 out_msg_buf->write_req_msg.address = memaddr + nwritten;
1046 msg_send_serial(out_msg_buf);
1047
1048 if (expect_msg(WRITE_ACK,in_msg_buf,1)) {
1049 nwritten += in_msg_buf->write_ack_msg.byte_count;
1050 } else {
1051 break;
1052 }
1053 }
1054 return(nwritten);
1055 }
1056
1057 /************************************************* REMOTE_READ_INFERIOR_MEMORY
1058 ** Read LEN bytes from inferior memory at MEMADDR. Put the result
1059 at debugger address MYADDR. Returns number of bytes read. */
1060 static int
1061 mm_read_inferior_memory(memaddr, myaddr, len)
1062 CORE_ADDR memaddr;
1063 char *myaddr;
1064 int len;
1065 {
1066 int i,nread;
1067
1068 out_msg_buf->read_req_msg.code= READ_REQ;
1069 out_msg_buf->read_req_msg.memory_space = mm_memory_space(memaddr);
1070
1071 nread=0;
1072 while (nread < len) {
1073 int num_to_read = (len - nread);
1074 if (num_to_read > MAXDATA) num_to_read = MAXDATA;
1075 out_msg_buf->read_req_msg.byte_count = num_to_read;
1076 out_msg_buf->read_req_msg.length = 3*4 + num_to_read;
1077 out_msg_buf->read_req_msg.address = memaddr + nread;
1078 msg_send_serial(out_msg_buf);
1079
1080 if (expect_msg(READ_ACK,in_msg_buf,1)) {
1081 for (i=0 ; i<in_msg_buf->read_ack_msg.byte_count ; i++)
1082 myaddr[i+nread] = in_msg_buf->read_ack_msg.data[i];
1083 nread += in_msg_buf->read_ack_msg.byte_count;
1084 } else {
1085 break;
1086 }
1087 }
1088 return(nread);
1089 }
1090
1091 /* FIXME! Merge these two. */
1092 static int
1093 mm_xfer_inferior_memory (memaddr, myaddr, len, write)
1094 CORE_ADDR memaddr;
1095 char *myaddr;
1096 int len;
1097 int write;
1098 {
1099
1100 memaddr = translate_addr(memaddr);
1101
1102 if (write)
1103 return mm_write_inferior_memory (memaddr, myaddr, len);
1104 else
1105 return mm_read_inferior_memory (memaddr, myaddr, len);
1106 }
1107
1108
1109 /********************************************************** MSG_SEND_SERIAL
1110 ** This function is used to send a message over the
1111 ** serial line.
1112 **
1113 ** If the message is successfully sent, a zero is
1114 ** returned. If the message was not sendable, a -1
1115 ** is returned. This function blocks. That is, it
1116 ** does not return until the message is completely
1117 ** sent, or until an error is encountered.
1118 **
1119 */
1120
1121 int
1122 msg_send_serial(msg_ptr)
1123 union msg_t *msg_ptr;
1124 {
1125 INT32 message_size;
1126 int byte_count;
1127 int result;
1128 char c;
1129
1130 /* Send message header */
1131 byte_count = 0;
1132 message_size = msg_ptr->generic_msg.length + (2 * sizeof(INT32));
1133 do {
1134 c = *((char *)msg_ptr+byte_count);
1135 result = write(mm_desc, &c, 1);
1136 if (result == 1) {
1137 byte_count = byte_count + 1;
1138 }
1139 } while ((byte_count < message_size) );
1140
1141 return(0);
1142 } /* end msg_send_serial() */
1143
1144 /********************************************************** MSG_RECV_SERIAL
1145 ** This function is used to receive a message over a
1146 ** serial line.
1147 **
1148 ** If the message is waiting in the buffer, a zero is
1149 ** returned and the buffer pointed to by msg_ptr is filled
1150 ** in. If no message was available, a -1 is returned.
1151 ** If timeout==0, wait indefinetly for a character.
1152 **
1153 */
1154
1155 int
1156 msg_recv_serial(msg_ptr)
1157 union msg_t *msg_ptr;
1158 {
1159 static INT32 length=0;
1160 static INT32 byte_count=0;
1161 int result;
1162 char c;
1163 if(msg_ptr == 0) /* re-sync request */
1164 { length=0;
1165 byte_count=0;
1166 #ifdef HAVE_TERMIO
1167 /* The timeout here is the prevailing timeout set with VTIME */
1168 ->"timeout==0 semantics not supported"
1169 read(mm_desc, in_buf, BUFER_SIZE);
1170 #else
1171 alarm (1);
1172 read(mm_desc, in_buf, BUFER_SIZE);
1173 alarm (0);
1174 #endif
1175 return(0);
1176 }
1177 /* Receive message */
1178 #ifdef HAVE_TERMIO
1179 /* Timeout==0, help support the mm_wait() routine */
1180 ->"timeout==0 semantics not supported (and its nice if they are)"
1181 result = read(mm_desc, &c, 1);
1182 #else
1183 alarm(timeout);
1184 result = read(mm_desc, &c, 1);
1185 alarm (0);
1186 #endif
1187 if ( result < 0) {
1188 if (errno == EINTR) {
1189 error ("Timeout reading from remote system.");
1190 } else
1191 perror_with_name ("remote");
1192 } else if (result == 1) {
1193 *((char *)msg_ptr+byte_count) = c;
1194 byte_count = byte_count + 1;
1195 }
1196
1197 /* Message header received. Save message length. */
1198 if (byte_count == (2 * sizeof(INT32)))
1199 length = msg_ptr->generic_msg.length;
1200
1201 if (byte_count >= (length + (2 * sizeof(INT32)))) {
1202 /* Message received */
1203 byte_count = 0;
1204 return(0);
1205 } else
1206 return (-1);
1207
1208 } /* end msg_recv_serial() */
1209
1210 /********************************************************************* KBD_RAW
1211 ** This function is used to put the keyboard in "raw"
1212 ** mode for BSD Unix. The original status is saved
1213 ** so that it may be restored later.
1214 */
1215 TERMINAL kbd_tbuf;
1216
1217 int
1218 kbd_raw() {
1219 int result;
1220 TERMINAL tbuf;
1221
1222 /* Get keyboard termio (to save to restore original modes) */
1223 #ifdef HAVE_TERMIO
1224 result = ioctl(0, TCGETA, &kbd_tbuf);
1225 #else
1226 result = ioctl(0, TIOCGETP, &kbd_tbuf);
1227 #endif
1228 if (result == -1)
1229 return (errno);
1230
1231 /* Get keyboard TERMINAL (for modification) */
1232 #ifdef HAVE_TERMIO
1233 result = ioctl(0, TCGETA, &tbuf);
1234 #else
1235 result = ioctl(0, TIOCGETP, &tbuf);
1236 #endif
1237 if (result == -1)
1238 return (errno);
1239
1240 /* Set up new parameters */
1241 #ifdef HAVE_TERMIO
1242 tbuf.c_iflag = tbuf.c_iflag &
1243 ~(INLCR | ICRNL | IUCLC | ISTRIP | IXON | BRKINT);
1244 tbuf.c_lflag = tbuf.c_lflag & ~(ICANON | ISIG | ECHO);
1245 tbuf.c_cc[4] = 0; /* MIN */
1246 tbuf.c_cc[5] = 0; /* TIME */
1247 #else
1248 /* FIXME: not sure if this is correct (matches HAVE_TERMIO). */
1249 tbuf.sg_flags |= RAW;
1250 tbuf.sg_flags |= ANYP;
1251 tbuf.sg_flags &= ~ECHO;
1252 #endif
1253
1254 /* Set keyboard termio to new mode (RAW) */
1255 #ifdef HAVE_TERMIO
1256 result = ioctl(0, TCSETAF, &tbuf);
1257 #else
1258 result = ioctl(0, TIOCSETP, &tbuf);
1259 #endif
1260 if (result == -1)
1261 return (errno);
1262
1263 return (0);
1264 } /* end kbd_raw() */
1265
1266
1267
1268 /***************************************************************** KBD_RESTORE
1269 ** This function is used to put the keyboard back in the
1270 ** mode it was in before kbk_raw was called. Note that
1271 ** kbk_raw() must have been called at least once before
1272 ** kbd_restore() is called.
1273 */
1274
1275 int
1276 kbd_restore() {
1277 int result;
1278
1279 /* Set keyboard termio to original mode */
1280 #ifdef HAVE_TERMIO
1281 result = ioctl(0, TCSETAF, &kbd_tbuf);
1282 #else
1283 result = ioctl(0, TIOCGETP, &kbd_tbuf);
1284 #endif
1285
1286 if (result == -1)
1287 return (errno);
1288
1289 return(0);
1290 } /* end kbd_cooked() */
1291
1292
1293 /*****************************************************************************/
1294 /* Fetch a single register indicatated by 'regno'.
1295 * Returns 0/-1 on success/failure.
1296 */
1297 static int
1298 fetch_register (regno)
1299 int regno;
1300 {
1301 int result;
1302 out_msg_buf->read_req_msg.code= READ_REQ;
1303 out_msg_buf->read_req_msg.length = 4*3;
1304 out_msg_buf->read_req_msg.byte_count = 4;
1305
1306 if (regno == GR1_REGNUM)
1307 { out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
1308 out_msg_buf->read_req_msg.address = 1;
1309 }
1310 else if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
1311 { out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
1312 out_msg_buf->read_req_msg.address = (regno - GR96_REGNUM) + 96;
1313 }
1314 #if defined(GR64_REGNUM)
1315 else if (regno >= GR64_REGNUM && regno < GR64_REGNUM + 32 )
1316 { out_msg_buf->read_req_msg.memory_space = GLOBAL_REG;
1317 out_msg_buf->read_req_msg.address = (regno - GR64_REGNUM) + 64;
1318 }
1319 #endif /* GR64_REGNUM */
1320 else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
1321 { out_msg_buf->read_req_msg.memory_space = LOCAL_REG;
1322 out_msg_buf->read_req_msg.address = (regno - LR0_REGNUM);
1323 }
1324 else if (regno>=FPE_REGNUM && regno<=EXO_REGNUM)
1325 { int val = -1;
1326 supply_register(160 + (regno - FPE_REGNUM),&val);
1327 return 0; /* Pretend Success */
1328 }
1329 else
1330 { out_msg_buf->read_req_msg.memory_space = SPECIAL_REG;
1331 out_msg_buf->read_req_msg.address = regnum_to_srnum(regno);
1332 }
1333
1334 msg_send_serial(out_msg_buf);
1335
1336 if (expect_msg(READ_ACK,in_msg_buf,1)) {
1337 supply_register (regno, &(in_msg_buf->read_r_ack_msg.data[0]));
1338 result = 0;
1339 } else {
1340 result = -1;
1341 }
1342 return result;
1343 }
1344 /*****************************************************************************/
1345 /* Store a single register indicated by 'regno'.
1346 * Returns 0/-1 on success/failure.
1347 */
1348 static int
1349 store_register (regno)
1350 int regno;
1351 {
1352 int result;
1353
1354 out_msg_buf->write_req_msg.code= WRITE_REQ;
1355 out_msg_buf->write_req_msg.length = 4*4;
1356 out_msg_buf->write_req_msg.byte_count = 4;
1357 out_msg_buf->write_r_msg.data[0] = read_register (regno);
1358
1359 if (regno == GR1_REGNUM)
1360 { out_msg_buf->write_req_msg.memory_space = GLOBAL_REG;
1361 out_msg_buf->write_req_msg.address = 1;
1362 /* Setting GR1 changes the numbers of all the locals, so invalidate the
1363 * register cache. Do this *after* calling read_register, because we want
1364 * read_register to return the value that write_register has just stuffed
1365 * into the registers array, not the value of the register fetched from
1366 * the inferior.
1367 */
1368 registers_changed ();
1369 }
1370 #if defined(GR64_REGNUM)
1371 else if (regno >= GR64_REGNUM && regno < GR64_REGNUM + 32 )
1372 { out_msg_buf->write_req_msg.memory_space = GLOBAL_REG;
1373 out_msg_buf->write_req_msg.address = (regno - GR64_REGNUM) + 64;
1374 }
1375 #endif /* GR64_REGNUM */
1376 else if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
1377 { out_msg_buf->write_req_msg.memory_space = GLOBAL_REG;
1378 out_msg_buf->write_req_msg.address = (regno - GR96_REGNUM) + 96;
1379 }
1380 else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
1381 { out_msg_buf->write_req_msg.memory_space = LOCAL_REG;
1382 out_msg_buf->write_req_msg.address = (regno - LR0_REGNUM);
1383 }
1384 else if (regno>=FPE_REGNUM && regno<=EXO_REGNUM)
1385 {
1386 return 0; /* Pretend Success */
1387 }
1388 else /* An unprotected or protected special register */
1389 { out_msg_buf->write_req_msg.memory_space = SPECIAL_REG;
1390 out_msg_buf->write_req_msg.address = regnum_to_srnum(regno);
1391 }
1392
1393 msg_send_serial(out_msg_buf);
1394
1395 if (expect_msg(WRITE_ACK,in_msg_buf,1)) {
1396 result = 0;
1397 } else {
1398 result = -1;
1399 }
1400 return result;
1401 }
1402 /****************************************************************************/
1403 /*
1404 * Convert a gdb special register number to a 29000 special register number.
1405 */
1406 static int
1407 regnum_to_srnum(regno)
1408 int regno;
1409 {
1410 switch(regno) {
1411 case VAB_REGNUM: return(0);
1412 case OPS_REGNUM: return(1);
1413 case CPS_REGNUM: return(2);
1414 case CFG_REGNUM: return(3);
1415 case CHA_REGNUM: return(4);
1416 case CHD_REGNUM: return(5);
1417 case CHC_REGNUM: return(6);
1418 case RBP_REGNUM: return(7);
1419 case TMC_REGNUM: return(8);
1420 case TMR_REGNUM: return(9);
1421 case NPC_REGNUM: return(USE_SHADOW_PC ? (20) : (10));
1422 case PC_REGNUM: return(USE_SHADOW_PC ? (21) : (11));
1423 case PC2_REGNUM: return(USE_SHADOW_PC ? (22) : (12));
1424 case MMU_REGNUM: return(13);
1425 case LRU_REGNUM: return(14);
1426 case IPC_REGNUM: return(128);
1427 case IPA_REGNUM: return(129);
1428 case IPB_REGNUM: return(130);
1429 case Q_REGNUM: return(131);
1430 case ALU_REGNUM: return(132);
1431 case BP_REGNUM: return(133);
1432 case FC_REGNUM: return(134);
1433 case CR_REGNUM: return(135);
1434 case FPE_REGNUM: return(160);
1435 case INTE_REGNUM: return(161);
1436 case FPS_REGNUM: return(162);
1437 case EXO_REGNUM:return(164);
1438 default:
1439 return(255); /* Failure ? */
1440 }
1441 }
1442 /****************************************************************************/
1443 /*
1444 * Initialize the target debugger (minimon only).
1445 */
1446 static void
1447 init_target_mm(tstart,tend,dstart,dend,entry,ms_size,rs_size,arg_start)
1448 ADDR32 tstart,tend,dstart,dend,entry;
1449 INT32 ms_size,rs_size;
1450 ADDR32 arg_start;
1451 {
1452 out_msg_buf->init_msg.code = INIT;
1453 out_msg_buf->init_msg.length= sizeof(struct init_msg_t)-2*sizeof(INT32);
1454 out_msg_buf->init_msg.text_start = tstart;
1455 out_msg_buf->init_msg.text_end = tend;
1456 out_msg_buf->init_msg.data_start = dstart;
1457 out_msg_buf->init_msg.data_end = dend;
1458 out_msg_buf->init_msg.entry_point = entry;
1459 out_msg_buf->init_msg.mem_stack_size = ms_size;
1460 out_msg_buf->init_msg.reg_stack_size = rs_size;
1461 out_msg_buf->init_msg.arg_start = arg_start;
1462 msg_send_serial(out_msg_buf);
1463 expect_msg(INIT_ACK,in_msg_buf,1);
1464 }
1465 /****************************************************************************/
1466 /*
1467 * Return a pointer to a string representing the given message code.
1468 * Not all messages are represented here, only the ones that we expect
1469 * to be called with.
1470 */
1471 static char*
1472 msg_str(code)
1473 INT32 code;
1474 {
1475 static char cbuf[32];
1476
1477 switch (code) {
1478 case BKPT_SET_ACK: sprintf(cbuf,"%s (%d)","BKPT_SET_ACK",code); break;
1479 case BKPT_RM_ACK: sprintf(cbuf,"%s (%d)","BKPT_RM_ACK",code); break;
1480 case INIT_ACK: sprintf(cbuf,"%s (%d)","INIT_ACK",code); break;
1481 case READ_ACK: sprintf(cbuf,"%s (%d)","READ_ACK",code); break;
1482 case WRITE_ACK: sprintf(cbuf,"%s (%d)","WRITE_ACK",code); break;
1483 case ERROR: sprintf(cbuf,"%s (%d)","ERROR",code); break;
1484 case HALT: sprintf(cbuf,"%s (%d)","HALT",code); break;
1485 default: sprintf(cbuf,"UNKNOWN (%d)",code); break;
1486 }
1487 return(cbuf);
1488 }
1489 /****************************************************************************/
1490 /*
1491 * Selected (not all of them) error codes that we might get.
1492 */
1493 static char*
1494 error_msg_str(code)
1495 INT32 code;
1496 {
1497 static char cbuf[50];
1498
1499 switch (code) {
1500 case EMFAIL: return("EMFAIL: unrecoverable error");
1501 case EMBADADDR: return("EMBADADDR: Illegal address");
1502 case EMBADREG: return("EMBADREG: Illegal register ");
1503 case EMACCESS: return("EMACCESS: Could not access memory");
1504 case EMBADMSG: return("EMBADMSG: Unknown message type");
1505 case EMMSG2BIG: return("EMMSG2BIG: Message to large");
1506 case EMNOSEND: return("EMNOSEND: Could not send message");
1507 case EMNORECV: return("EMNORECV: Could not recv message");
1508 case EMRESET: return("EMRESET: Could not RESET target");
1509 case EMCONFIG: return("EMCONFIG: Could not get target CONFIG");
1510 case EMSTATUS: return("EMSTATUS: Could not get target STATUS");
1511 case EMREAD: return("EMREAD: Could not READ target memory");
1512 case EMWRITE: return("EMWRITE: Could not WRITE target memory");
1513 case EMBKPTSET: return("EMBKPTSET: Could not set breakpoint");
1514 case EMBKPTRM: return("EMBKPTRM: Could not remove breakpoint");
1515 case EMBKPTSTAT:return("EMBKPTSTAT: Could not get breakpoint status");
1516 case EMBKPTNONE:return("EMBKPTNONE: All breakpoints in use");
1517 case EMBKPTUSED:return("EMBKPTUSED: Breakpoints already in use");
1518 case EMINIT: return("EMINIT: Could not init target memory");
1519 case EMGO: return("EMGO: Could not start execution");
1520 case EMSTEP: return("EMSTEP: Could not single step");
1521 case EMBREAK: return("EMBREAK: Could not BREAK");
1522 case EMCOMMERR: return("EMCOMMERR: Communication error");
1523 default: sprintf(cbuf,"error number %d",code); break;
1524 } /* end switch */
1525
1526 return (cbuf);
1527 }
1528 /****************************************************************************/
1529 /*
1530 * Receive a message and expect it to be of type msgcode.
1531 * Returns 0/1 on failure/success.
1532 */
1533 static int
1534 expect_msg(msgcode,msg_buf,from_tty)
1535 INT32 msgcode; /* Msg code we expect */
1536 union msg_t *msg_buf; /* Where to put the message received */
1537 int from_tty; /* Print message on error if non-zero */
1538 {
1539 int retries=0;
1540 while(msg_recv_serial(msg_buf) && (retries++<MAX_RETRIES));
1541 if (retries >= MAX_RETRIES) {
1542 printf("Expected msg %s, ",msg_str(msgcode));
1543 printf("no message received!\n");
1544 return(0); /* Failure */
1545 }
1546
1547 if (msg_buf->generic_msg.code != msgcode) {
1548 if (from_tty) {
1549 printf("Expected msg %s, ",msg_str(msgcode));
1550 printf("got msg %s\n",msg_str(msg_buf->generic_msg.code));
1551 if (msg_buf->generic_msg.code == ERROR)
1552 printf("%s\n",error_msg_str(msg_buf->error_msg.error_code));
1553 }
1554 return(0); /* Failure */
1555 }
1556 return(1); /* Success */
1557 }
1558 /****************************************************************************/
1559 /*
1560 * Determine the MiniMon memory space qualifier based on the addr.
1561 * FIXME: Can't distinguis I_ROM/D_ROM.
1562 * FIXME: Doesn't know anything about I_CACHE/D_CACHE.
1563 */
1564 static int
1565 mm_memory_space(addr)
1566 CORE_ADDR *addr;
1567 {
1568 ADDR32 tstart = target_config.I_mem_start;
1569 ADDR32 tend = tstart + target_config.I_mem_size;
1570 ADDR32 dstart = target_config.D_mem_start;
1571 ADDR32 dend = tstart + target_config.D_mem_size;
1572 ADDR32 rstart = target_config.ROM_start;
1573 ADDR32 rend = tstart + target_config.ROM_size;
1574
1575 if (((ADDR32)addr >= tstart) && ((ADDR32)addr < tend)) {
1576 return I_MEM;
1577 } else if (((ADDR32)addr >= dstart) && ((ADDR32)addr < dend)) {
1578 return D_MEM;
1579 } else if (((ADDR32)addr >= rstart) && ((ADDR32)addr < rend)) {
1580 /* FIXME: how do we determine between D_ROM and I_ROM */
1581 return D_ROM;
1582 } else /* FIXME: what do me do now? */
1583 return D_MEM; /* Hmmm! */
1584 }
1585
1586 /****************************************************************************/
1587 /*
1588 * Define the target subroutine names
1589 */
1590 struct target_ops mm_ops = {
1591 "minimon", "Remote AMD/Minimon target",
1592 "Remote debug an AMD 290*0 using the MiniMon dbg core on the target",
1593 mm_open, mm_close,
1594 mm_attach, mm_detach, mm_resume, mm_wait,
1595 mm_fetch_registers, mm_store_registers,
1596 mm_prepare_to_store,
1597 mm_xfer_inferior_memory,
1598 mm_files_info,
1599 mm_insert_breakpoint, mm_remove_breakpoint, /* Breakpoints */
1600 0, 0, 0, 0, 0, /* Terminal handling */
1601 mm_kill, /* FIXME, kill */
1602 mm_load,
1603 0, /* lookup_symbol */
1604 mm_create_inferior, /* create_inferior */
1605 mm_mourn, /* mourn_inferior FIXME */
1606 0, /* can_run */
1607 0, /* notice_signals */
1608 0, /* to_stop */
1609 process_stratum, 0, /* next */
1610 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1611 0,0, /* sections, sections_end */
1612 OPS_MAGIC, /* Always the last thing */
1613 };
1614
1615 void
1616 _initialize_remote_mm()
1617 {
1618 add_target (&mm_ops);
1619 }
1620
1621 #ifdef NO_HIF_SUPPORT
1622 service_HIF(msg)
1623 union msg_t *msg;
1624 {
1625 return(0); /* Emulate a failure */
1626 }
1627 #endif
This page took 0.063156 seconds and 4 git commands to generate.