Thu May 21 13:14:25 1998 John Metzler <jmetzler@cygnus.com>
[external/binutils.git] / gdb / remote-rdp.c
1 /* Remote debugging for the ARM RDP interface.
2    Copyright 1994, 1995 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
19
20
21  */
22
23
24 /* 
25    Much of this file (in particular the SWI stuff) is based on code by
26    David Taylor (djt1000@uk.ac.cam.hermes).
27
28    I hacked on and simplified it by removing a lot of sexy features he
29    had added, and some of the (unix specific) workarounds he'd done
30    for other GDB problems - which if they still exist should be fixed
31    in GDB, not in a remote-foo thing .  I also made it conform more to
32    the doc I have; which may be wrong.
33
34    Steve Chamberlain (sac@cygnus.com).
35  */
36
37
38 #include "defs.h"
39 #include "inferior.h"
40 #include "wait.h"
41 #include "value.h"
42 #include "callback.h"
43 #include "command.h"
44 #ifdef ANSI_PROTOTYPES
45 #include <stdarg.h>
46 #else
47 #include <varargs.h>
48 #endif
49 #include <ctype.h>
50 #include <fcntl.h>
51 #include "symfile.h"
52 #include "remote-utils.h"
53 #include "gdb_string.h"
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 #include "gdbcore.h"
58
59
60 extern struct target_ops remote_rdp_ops;
61 static serial_t io;
62 static host_callback *callback = &default_callback;
63
64 struct
65   {
66     int step_info;
67     int break_info;
68     int model_info;
69     int target_info;
70     int can_step;
71     char command_line[10];
72     int rdi_level;
73     int rdi_stopped_status;
74   }
75 ds;
76
77
78
79 /* Definitions for the RDP protocol. */
80
81 #define RDP_MOUTHFULL                   (1<<6)
82 #define FPU_COPRO_NUMBER                1
83
84 #define RDP_OPEN                        0
85 #define RDP_OPEN_TYPE_COLD              0
86 #define RDP_OPEN_TYPE_WARM              1
87 #define RDP_OPEN_TYPE_BAUDRATE          2
88
89 #define RDP_OPEN_BAUDRATE_9600          1
90 #define RDP_OPEN_BAUDRATE_19200         2
91 #define RDP_OPEN_BAUDRATE_38400         3
92
93 #define RDP_OPEN_TYPE_RETURN_SEX        (1<<3)
94
95 #define RDP_CLOSE                       1
96
97 #define RDP_MEM_READ                    2
98
99 #define RDP_MEM_WRITE                   3
100
101 #define RDP_CPU_READ                    4
102 #define RDP_CPU_WRITE                   5
103 #define RDP_CPU_READWRITE_MODE_CURRENT 255
104 #define RDP_CPU_READWRITE_MASK_PC       (1<<16)
105 #define RDP_CPU_READWRITE_MASK_CPSR     (1<<17)
106 #define RDP_CPU_READWRITE_MASK_SPSR     (1<<18)
107
108 #define RDP_COPRO_READ                  6
109 #define RDP_COPRO_WRITE                 7
110 #define RDP_FPU_READWRITE_MASK_FPS      (1<<8)
111
112 #define RDP_SET_BREAK                   0xa
113 #define RDP_SET_BREAK_TYPE_PC_EQUAL     0
114 #define RDP_SET_BREAK_TYPE_GET_HANDLE   (0x10)
115
116 #define RDP_CLEAR_BREAK                 0xb
117
118 #define RDP_EXEC                        0x10
119 #define RDP_EXEC_TYPE_SYNC              0
120
121 #define RDP_STEP                        0x11
122
123 #define RDP_INFO                        0x12
124 #define RDP_INFO_ABOUT_STEP             2
125 #define RDP_INFO_ABOUT_STEP_GT_1        1
126 #define RDP_INFO_ABOUT_STEP_TO_JMP      2
127 #define RDP_INFO_ABOUT_STEP_1           4
128 #define RDP_INFO_ABOUT_TARGET           0
129 #define RDP_INFO_ABOUT_BREAK            1
130 #define RDP_INFO_ABOUT_BREAK_COMP       1
131 #define RDP_INFO_ABOUT_BREAK_RANGE      2
132 #define RDP_INFO_ABOUT_BREAK_BYTE_READ  4
133 #define RDP_INFO_ABOUT_BREAK_HALFWORD_READ 8
134 #define RDP_INFO_ABOUT_BREAK_WORD_READ (1<<4)
135 #define RDP_INFO_ABOUT_BREAK_BYTE_WRITE (1<<5)
136 #define RDP_INFO_ABOUT_BREAK_HALFWORD_WRITE (1<<6)
137 #define RDP_INFO_ABOUT_BREAK_WORD_WRITE (1<<7)
138 #define RDP_INFO_ABOUT_BREAK_MASK       (1<<8)
139 #define RDP_INFO_ABOUT_BREAK_THREAD_BREAK (1<<9)
140 #define RDP_INFO_ABOUT_BREAK_THREAD_WATCH (1<<10)
141 #define RDP_INFO_ABOUT_BREAK_COND       (1<<11)
142 #define RDP_INFO_VECTOR_CATCH           (0x180)
143 #define RDP_INFO_ICEBREAKER             (7)
144 #define RDP_INFO_SET_CMDLINE            (0x300)
145
146 #define RDP_SELECT_CONFIG               (0x16)
147 #define RDI_ConfigCPU                   0
148 #define RDI_ConfigSystem                1
149 #define RDI_MatchAny                    0
150 #define RDI_MatchExactly                1
151 #define RDI_MatchNoEarlier              2
152
153 #define RDP_RESET                       0x7f
154
155 /* Returns from RDP */
156 #define RDP_RES_STOPPED                 0x20
157 #define RDP_RES_SWI                     0x21
158 #define RDP_RES_FATAL                   0x5e
159 #define RDP_RES_VALUE                   0x5f
160 #define RDP_RES_VALUE_LITTLE_ENDIAN     240
161 #define RDP_RES_VALUE_BIG_ENDIAN        241
162 #define RDP_RES_RESET                   0x7f
163 #define RDP_RES_AT_BREAKPOINT           143
164 #define RDP_RES_IDUNNO                  0xe6
165 #define RDP_OSOpReply                   0x13
166 #define RDP_OSOpWord                    2
167 #define RDP_OSOpNothing                 0
168
169 static int timeout = 2;
170
171 static char * commandline = NULL;
172
173 static int
174 remote_rdp_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
175                                          char *myaddr,
176                                          int len,
177                                          int write,
178                                          struct target_ops * target));
179
180
181 /* Stuff for talking to the serial layer. */
182
183 static unsigned char
184 get_byte ()
185 {
186   int c = SERIAL_READCHAR (io, timeout);
187
188   if (remote_debug)
189     printf ("[%02x]\n", c);
190
191   if (c == SERIAL_TIMEOUT)
192     {
193       if (timeout == 0)
194         return (unsigned char) c;
195
196       error ("Timeout reading from remote_system");
197     }
198
199   return c;
200 }
201
202 /* Note that the target always speaks little-endian to us,
203    even if it's a big endian machine. */
204 static unsigned int
205 get_word ()
206 {
207   unsigned int val = 0;
208   unsigned int c;
209   int n;
210   for (n = 0; n < 4; n++)
211     {
212       c = get_byte ();
213       val |= c << (n * 8);
214     }
215   return val;
216 }
217
218 static void
219 put_byte (val)
220      char val;
221 {
222   if (remote_debug)
223     printf ("(%02x)\n", val);
224   SERIAL_WRITE (io, &val, 1);
225 }
226
227 static void
228 put_word (val)
229      int val;
230 {
231   /* We always send in little endian */
232   unsigned char b[4];
233   b[0] = val;
234   b[1] = val >> 8;
235   b[2] = val >> 16;
236   b[3] = val >> 24;
237
238   if (remote_debug)
239     printf ("(%04x)", val);
240
241   SERIAL_WRITE (io, b, 4);
242 }
243
244
245
246 /* Stuff for talking to the RDP layer. */
247
248 /* This is a bit more fancy that need be so that it syncs even in nasty cases.
249
250    I'be been unable to make it reliably sync up with the change
251    baudrate open command.  It likes to sit and say it's been reset,
252    with no more action.  So I took all that code out.  I'd rather sync
253    reliably at 9600 than wait forever for a possible 19200 connection.
254
255  */
256 static void
257 rdp_init (cold, tty)
258      int cold;
259      int tty;
260 {
261   int sync = 0;
262   int type = cold ? RDP_OPEN_TYPE_COLD : RDP_OPEN_TYPE_WARM;
263   int baudtry = 9600;
264
265   time_t now = time (0);
266   time_t stop_time = now + 10;  /* Try and sync for 10 seconds, then give up */
267
268
269   while (time (0) < stop_time && !sync)
270     {
271       int restype;
272       QUIT;
273
274       SERIAL_FLUSH_INPUT (io);
275       SERIAL_FLUSH_OUTPUT (io);
276
277       if (tty)
278         printf_unfiltered ("Trying to connect at %d baud.\n", baudtry);
279
280       /*
281       ** It seems necessary to reset an EmbeddedICE to get it going.
282       ** This has the side benefit of displaying the startup banner.
283       */
284       if (cold)
285         {
286           put_byte (RDP_RESET);
287           while ((restype = SERIAL_READCHAR (io, 1)) > 0)
288             {
289               switch (restype)
290                 {
291                 case SERIAL_TIMEOUT:
292                   break;
293                 case RDP_RESET:
294                   /* Sent at start of reset process: ignore */
295                   break;
296                 default:
297                   printf_unfiltered ("%c", isgraph (restype) ? restype : ' ');
298                   break;
299                 }
300             }
301
302           if (restype == 0)
303             {
304               /* Got end-of-banner mark */
305               printf_filtered ("\n");
306             }
307         }
308
309       put_byte (RDP_OPEN);
310
311       put_byte (type | RDP_OPEN_TYPE_RETURN_SEX );
312       put_word (0);
313
314       while (!sync && (restype = SERIAL_READCHAR (io, 1)) > 0)
315         {
316           if (remote_debug)
317             printf_unfiltered ("[%02x]\n", restype);
318
319           switch (restype)
320             {
321             case SERIAL_TIMEOUT:
322               break;
323             case RDP_RESET:
324               while ((restype = SERIAL_READCHAR (io, 1)) == RDP_RESET)
325                 ;
326               while ((restype = SERIAL_READCHAR (io, 1)) > 0)
327                 {
328                   printf_unfiltered ("%c", isgraph (restype) ? restype : ' ');
329                 }
330               while ((restype = SERIAL_READCHAR (io, 1)) > 0)
331                 ;
332               if (tty)
333                 {
334                   printf_unfiltered ("\nThe board has sent notification that it was reset.\n");
335                   printf_unfiltered ("Waiting for it to settle down...\n");
336                 }
337               sleep (3);
338               if (tty)
339                 printf_unfiltered ("\nTrying again.\n");
340               break;
341             default:
342               break;
343             case RDP_RES_VALUE:
344               {
345                 int resval = SERIAL_READCHAR (io, 1);
346
347                 if (remote_debug)
348                   printf_unfiltered ("[%02x]\n", resval);
349
350                 switch (resval)
351                   {
352                   case SERIAL_TIMEOUT:
353                     break;
354                   case RDP_RES_VALUE_LITTLE_ENDIAN:
355                     target_byte_order = LITTLE_ENDIAN;
356                     sync = 1;
357                     break;
358                   case RDP_RES_VALUE_BIG_ENDIAN:
359                     target_byte_order = BIG_ENDIAN;
360                     sync = 1;
361                     break;
362                   default:
363                     break;
364                   }
365               }
366             }
367         }
368     }
369
370   if (!sync)
371     {
372       error ("Couldn't reset the board, try pressing the reset button");
373     }
374 }
375
376
377 #ifdef ANSI_PROTOTYPES
378 void
379 send_rdp (char *template,...)
380 #else
381 void
382 send_rdp (char *template, va_alist)
383      va_dcl
384 #endif
385 {
386   char buf[200];
387   char *dst = buf;
388   va_list alist;
389 #ifdef ANSI_PROTOTYPES
390   va_start (alist, template);
391 #else
392   va_start (alist);
393 #endif
394
395   while (*template)
396     {
397       unsigned int val;
398       int *pi;
399       int *pstat;
400       char *pc;
401       int i;
402       switch (*template++)
403         {
404         case 'b':
405           val = va_arg (alist, int);
406           *dst++ = val;
407           break;
408         case 'w':
409           val = va_arg (alist, int);
410           *dst++ = val;
411           *dst++ = val >> 8;
412           *dst++ = val >> 16;
413           *dst++ = val >> 24;
414           break;
415         case 'S':
416           val = get_byte ();
417           if (val != RDP_RES_VALUE)
418             {
419               printf_unfiltered ("got bad res value of %d, %x\n", val, val);
420             }
421           break;
422         case 'V':
423           pstat = va_arg (alist, int *);
424           pi = va_arg (alist, int *);
425
426           *pstat = get_byte ();
427           /* Check the result was zero, if not read the syndrome */
428           if (*pstat)
429             {
430               *pi = get_word ();
431             }
432           break;
433         case 'Z':
434           /* Check the result code */
435           switch (get_byte ())
436             {
437             case 0:
438               /* Success */
439               break;
440             case 253:
441               /* Target can't do it; never mind */
442               printf_unfiltered ("RDP: Insufficient privilege\n");
443               return;
444             case 254:
445               /* Target can't do it; never mind */
446               printf_unfiltered ("RDP: Unimplemented message\n");
447               return;
448             case 255:
449               error ("Command garbled");
450               break;
451             default:
452               error ("Corrupt reply from target");
453               break;
454             }
455           break;
456         case 'W':
457           /* Read a word from the target */
458           pi = va_arg (alist, int *);
459           *pi = get_word ();
460           break;
461         case 'P':
462           /* Read in some bytes from the target. */
463           pc = va_arg (alist, char *);
464           val = va_arg (alist, int);
465           for (i = 0; i < val; i++)
466             {
467               pc[i] = get_byte ();
468             }
469           break;
470         case 'p':
471           /* send what's being pointed at */
472           pc = va_arg (alist, char *);
473           val = va_arg (alist, int);
474           dst = buf;
475           SERIAL_WRITE (io, pc, val);
476           break;
477         case '-':
478           /* Send whats in the queue */
479           if (dst != buf)
480             {
481               SERIAL_WRITE (io, buf, dst - buf);
482               dst = buf;
483             }
484           break;
485         case 'B':
486           pi = va_arg (alist, int *);
487           *pi = get_byte ();
488           break;
489         default:
490           abort ();
491         }
492     }
493   va_end (args);
494
495   if (dst != buf)
496     abort ();
497 }
498
499
500 static int
501 rdp_write (memaddr, buf, len)
502      CORE_ADDR memaddr;
503      char *buf;
504      int len;
505 {
506   int res;
507   int val;
508
509   send_rdp ("bww-p-SV", RDP_MEM_WRITE, memaddr, len, buf, len, &res, &val);
510
511   if (res)
512     {
513       return val;
514     }
515   return len;
516 }
517
518
519 static int
520 rdp_read (memaddr, buf, len)
521      CORE_ADDR memaddr;
522      char *buf;
523      int len;
524 {
525   int res;
526   int val;
527   send_rdp ("bww-S-P-V",
528             RDP_MEM_READ, memaddr, len,
529             buf, len,
530             &res, &val);
531   if (res)
532     {
533       return val;
534     }
535   return len;
536 }
537
538 static void
539 rdp_fetch_one_register (mask, buf)
540      int mask;
541      char *buf;
542 {
543   int val;
544   send_rdp ("bbw-SWZ", RDP_CPU_READ, RDP_CPU_READWRITE_MODE_CURRENT, mask, &val);
545   store_signed_integer (buf, 4, val);
546 }
547
548 static void
549 rdp_fetch_one_fpu_register (mask, buf)
550      int mask;
551      char *buf;
552 {
553 #if 0
554   /* !!! Since the PIE board doesn't work as documented,
555      and it doesn't have FPU hardware anyway and since it
556      slows everything down, I've disabled this. */
557   int val;
558   if (mask == RDP_FPU_READWRITE_MASK_FPS)
559     {
560       /* this guy is only a word */
561       send_rdp ("bbw-SWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, &val);
562       store_signed_integer (buf, 4, val);
563     }
564   else
565     {
566       /* There are 12 bytes long 
567          !! fixme about endianness 
568        */
569       int dummy;                /* I've seen these come back as four words !! */
570       send_rdp ("bbw-SWWWWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, buf + 0, buf + 4, buf + 8, &dummy);
571     }
572 #endif
573   memset (buf, 0, MAX_REGISTER_RAW_SIZE);
574 }
575
576
577 static void
578 rdp_store_one_register (mask, buf)
579      int mask;
580      char *buf;
581 {
582   int val = extract_unsigned_integer (buf, 4);
583
584   send_rdp ("bbww-SZ",
585             RDP_CPU_WRITE, RDP_CPU_READWRITE_MODE_CURRENT, mask, val);
586 }
587
588
589 static void
590 rdp_store_one_fpu_register (mask, buf)
591      int mask;
592      char *buf;
593 {
594 #if 0
595   /* See comment in fetch_one_fpu_register */
596   if (mask == RDP_FPU_READWRITE_MASK_FPS)
597     {
598       int val = extract_unsigned_integer (buf, 4);
599       /* this guy is only a word */
600       send_rdp ("bbww-SZ", RDP_COPRO_WRITE,
601                 FPU_COPRO_NUMBER,
602                 mask, val);
603     }
604   else
605     {
606       /* There are 12 bytes long 
607          !! fixme about endianness 
608        */
609       int dummy = 0;
610       /* I've seen these come as four words, not the three advertized !! */
611       printf ("Sending mask %x\n", mask);
612       send_rdp ("bbwwwww-SZ",
613                 RDP_COPRO_WRITE,
614                 FPU_COPRO_NUMBER,
615                 mask,
616                 *(int *) (buf + 0),
617                 *(int *) (buf + 4),
618                 *(int *) (buf + 8),
619                 0);
620
621       printf ("done mask %x\n", mask);
622     }
623 #endif
624 }
625 \f
626
627 /* Convert between GDB requests and the RDP layer. */
628
629 static void
630 remote_rdp_fetch_register (regno)
631      int regno;
632 {
633   if (regno == -1)
634     {
635       for (regno = 0; regno < NUM_REGS; regno++)
636         remote_rdp_fetch_register (regno);
637     }
638   else
639     {
640       char buf[MAX_REGISTER_RAW_SIZE];
641       if (regno < 15)
642         rdp_fetch_one_register (1 << regno, buf);
643       else if (regno == PC_REGNUM)
644         rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_PC, buf);
645       else if (regno == PS_REGNUM)
646         rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_CPSR, buf);
647       else if (regno == FPS_REGNUM)
648         rdp_fetch_one_fpu_register (RDP_FPU_READWRITE_MASK_FPS, buf);
649       else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
650         rdp_fetch_one_fpu_register (1 << (regno - F0_REGNUM), buf);
651       else
652         {
653           printf ("Help me with fetch reg %d\n", regno);
654         }
655       supply_register (regno, buf);
656     }
657 }
658
659
660 static void
661 remote_rdp_store_register (regno)
662      int regno;
663 {
664   if (regno == -1)
665     {
666       for (regno = 0; regno < NUM_REGS; regno++)
667         remote_rdp_store_register (regno);
668     }
669   else
670     {
671       char tmp[MAX_REGISTER_RAW_SIZE];
672       read_register_gen (regno, tmp);
673       if (regno < 15)
674         rdp_store_one_register (1 << regno, tmp);
675       else if (regno == PC_REGNUM)
676         rdp_store_one_register (RDP_CPU_READWRITE_MASK_PC, tmp);
677       else if (regno == PS_REGNUM)
678         rdp_store_one_register (RDP_CPU_READWRITE_MASK_CPSR, tmp);
679       else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
680         rdp_store_one_fpu_register (1 << (regno - F0_REGNUM), tmp);
681       else
682         {
683           printf ("Help me with reg %d\n", regno);
684         }
685     }
686 }
687
688 static void
689 remote_rdp_kill ()
690 {
691   callback->shutdown (callback);
692 }
693
694
695 static void
696 rdp_info ()
697 {
698   send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_STEP,
699             &ds.step_info);
700   send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_BREAK,
701             &ds.break_info);
702   send_rdp ("bw-S-WW-Z", RDP_INFO, RDP_INFO_ABOUT_TARGET,
703             &ds.target_info,
704             &ds.model_info);
705
706   ds.can_step = ds.step_info & RDP_INFO_ABOUT_STEP_1;
707
708   ds.rdi_level = (ds.target_info >> 5) & 3;
709 }
710
711
712 static void
713 rdp_execute_start ()
714 {
715   /* Start it off, but don't wait for it */
716   send_rdp ("bb-", RDP_EXEC, RDP_EXEC_TYPE_SYNC);
717 }
718
719
720 static void
721 rdp_set_command_line (command, args)
722      char * command;
723      char * args;
724 {
725   /*
726   ** We could use RDP_INFO_SET_CMDLINE to send this, but EmbeddedICE systems
727   ** don't implement that, and get all confused at the unexpected text.
728   ** Instead, just keep a copy, and send it when the target does a SWI_GetEnv
729   */
730
731   if (commandline != NULL)
732     free (commandline);
733
734   commandline = malloc (strlen (command) + strlen (args) + 2);
735   if (commandline != NULL)
736     {
737       strcpy (commandline, command);
738       strcat (commandline, " ");
739       strcat (commandline, args);
740     }
741 }
742
743 static void
744 rdp_catch_vectors ()
745 {
746   /*
747   ** We want the target monitor to intercept the abort vectors
748   ** i.e. stop the program if any of these are used.
749   */
750   send_rdp ("bww-SZ", RDP_INFO, RDP_INFO_VECTOR_CATCH,
751             /*
752             ** Specify a bitmask including
753             **  the reset vector
754             **  the undefined instruction vector
755             **  the prefetch abort vector
756             **  the data abort vector
757             **  the address exception vector
758             */
759             (1<<0)|(1<<1)|(1<<3)|(1<<4)|(1<<5)
760             );
761 }
762   
763
764
765 #define a_byte 1
766 #define a_word 2
767 #define a_string 3
768
769
770 typedef struct
771 {
772   CORE_ADDR n;
773   const char *s;
774 }
775 argsin;
776
777 #define ABYTE 1
778 #define AWORD 2
779 #define ASTRING 3
780 #define ADDRLEN 4
781
782 #define SWI_WriteC                      0x0
783 #define SWI_Write0                      0x2
784 #define SWI_ReadC                       0x4
785 #define SWI_CLI                         0x5
786 #define SWI_GetEnv                      0x10
787 #define SWI_Exit                        0x11
788 #define SWI_EnterOS                     0x16
789
790 #define SWI_GetErrno                    0x60
791 #define SWI_Clock                       0x61
792
793 #define SWI_Time                        0x63
794 #define SWI_Remove                      0x64
795 #define SWI_Rename                      0x65
796 #define SWI_Open                        0x66
797
798 #define SWI_Close                       0x68
799 #define SWI_Write                       0x69
800 #define SWI_Read                        0x6a
801 #define SWI_Seek                        0x6b
802 #define SWI_Flen                        0x6c
803
804 #define SWI_IsTTY                       0x6e
805 #define SWI_TmpNam                      0x6f
806 #define SWI_InstallHandler              0x70
807 #define SWI_GenerateError               0x71
808
809
810 static int
811 exec_swi (swi, args)
812      int swi;
813      argsin *args;
814 {
815   int i;
816   char c;
817   switch (swi)
818     {
819     case SWI_WriteC:
820       callback->write_stdout (callback, &c, 1);
821       return 0;
822     case SWI_Write0:
823       for (i = 0; i < args->n; i++)
824         callback->write_stdout (callback, args->s, strlen (args->s));
825       return 0;
826     case SWI_ReadC:
827       callback->read_stdin (callback, &c, 1);
828       args->n = c;
829       return 1;
830     case SWI_CLI:
831       args->n = callback->system (callback, args->s);
832       return 1;
833     case SWI_GetErrno:
834       args->n = callback->get_errno (callback);
835       return 1;
836     case SWI_Time:
837       args->n = callback->time (callback, NULL);
838       return 1;
839     case SWI_Remove:
840       args->n = callback->unlink (callback, args->s);
841       return 1;
842     case SWI_Rename:
843       args->n = callback->rename (callback, args[0].s, args[1].s);
844       return 1;
845     case SWI_Open:
846       i = 0;
847
848 #ifdef O_BINARY
849       if (args[1].n & 1)
850         i |= O_BINARY;
851 #endif
852       if (args[1].n & 2)
853         i |= O_RDWR;
854
855       if (args[1].n & 4)
856         {
857           i |= O_CREAT;
858         }
859
860       if (args[1].n & 8)
861         i |= O_APPEND;
862
863       args->n = callback->open (callback, args->s, i);
864       return 1;
865
866     case SWI_Close:
867       args->n = callback->close (callback, args->n);
868       return 1;
869
870     case SWI_Write:
871       args->n = callback->write (callback, args[0].n, args[1].s, args[1].n);
872       return 1;
873     case SWI_Read:
874       {
875         char *copy = alloca (args[2].n);
876         int done = callback->read (callback, args[0].n, copy, args[2].n);
877         if (done > 0)
878           remote_rdp_xfer_inferior_memory (args[0].n, copy, done, 1, 0);
879         args->n -= done;
880         return 1;
881       }
882
883     case SWI_Seek:
884       args->n = callback->lseek (callback, args[0].n, args[1].n, 0) >= 0;
885       return 1;
886     case SWI_Flen:
887       {
888         long old = callback->lseek (callback, args->n, 1, 1);
889         args->n = callback->lseek (callback, args->n, 2, 0);
890         callback->lseek (callback, args->n, old, 0);
891         return 1;
892       }
893
894     case SWI_IsTTY:
895       args->n = callback->isatty (callback, args->n);
896       return 1;
897
898     case SWI_GetEnv:
899       if (commandline != NULL)
900         {
901           int len = strlen (commandline);
902           if (len > 255)
903             {
904               len = 255;
905               commandline [255]='\0';
906             }
907           remote_rdp_xfer_inferior_memory (args[0].n,
908                                            commandline, len+1, 1, 0);
909         }
910       else
911         remote_rdp_xfer_inferior_memory (args[0].n, "", 1, 1, 0);
912       return 1;
913       
914     default:
915       return 0;
916     }
917 }
918
919
920 static void
921 handle_swi ()
922 {
923   argsin args[3];
924   char *buf;
925   int len;
926   int count = 0;
927
928   int swino = get_word ();
929   int type = get_byte ();
930   while (type != 0)
931     {
932       switch (type & 0x3)
933         {
934         case ABYTE:
935           args[count].n = get_byte ();
936           break;
937
938         case AWORD:
939           args[count].n = get_word ();
940           break;
941
942         case ASTRING:
943           /* If the word is under 32 bytes it will be sent otherwise
944              an address to it is passed. Also: Special case of 255 */
945
946           len = get_byte ();
947           if (len > 32)
948             {
949               if (len == 255)
950                 {
951                   len = get_word ();
952                 }
953               buf = alloca (len);
954               remote_rdp_xfer_inferior_memory (get_word (),
955                                                buf,
956                                                len,
957                                                0,
958                                                0);
959             }
960           else
961             {
962               int i;
963               buf = alloca (len + 1);
964               for (i = 0; i < len; i++)
965                 buf[i] = get_byte ();
966               buf[i] = 0;
967             }
968           args[count].n = len;
969           args[count].s = buf;
970           break;
971
972         default:
973           error ("Unimplented SWI argument");
974         }
975
976       type = type >> 2;
977       count++;
978     }
979
980   if (exec_swi (swino, args))
981     {
982       /* We have two options here reply with either a byte or a word
983          which is stored in args[0].n. There is no harm in replying with
984          a word all the time, so thats what I do! */
985       send_rdp ("bbw-", RDP_OSOpReply, RDP_OSOpWord, args[0].n);
986     }
987   else
988     {
989       send_rdp ("bb-", RDP_OSOpReply, RDP_OSOpNothing);
990     }
991 }
992
993 static void
994 rdp_execute_finish ()
995 {
996   int running = 1;
997
998   while (running)
999     {
1000       int res;
1001       res = SERIAL_READCHAR (io, 1);
1002       while (res == SERIAL_TIMEOUT)
1003         {
1004           QUIT;
1005           printf_filtered ("Waiting for target..\n");
1006           res = SERIAL_READCHAR (io, 1);
1007         }
1008
1009       switch (res)
1010         {
1011         case RDP_RES_SWI:
1012           handle_swi ();
1013           break;
1014         case RDP_RES_VALUE:
1015           send_rdp ("B", &ds.rdi_stopped_status);
1016           running = 0;
1017           break;
1018         case RDP_RESET:
1019           printf_filtered ("Target reset\n");
1020           running = 0;
1021           break;
1022         default:
1023           printf_filtered ("Ignoring %x\n", res);
1024           break;
1025         }
1026     }
1027 }
1028
1029
1030 static void
1031 rdp_execute ()
1032 {
1033   rdp_execute_start ();
1034   rdp_execute_finish ();
1035 }
1036
1037 static int
1038 remote_rdp_insert_breakpoint (addr, save)
1039      CORE_ADDR addr;
1040      char *save;
1041 {
1042   int res;
1043   if (ds.rdi_level > 0)
1044     {
1045       send_rdp ("bwb-SWB",
1046                 RDP_SET_BREAK,
1047                 addr,
1048                 RDP_SET_BREAK_TYPE_PC_EQUAL | RDP_SET_BREAK_TYPE_GET_HANDLE,
1049                 save,
1050                 &res);
1051     }
1052   else
1053     {
1054       send_rdp ("bwb-SB",
1055                 RDP_SET_BREAK,
1056                 addr,
1057                 RDP_SET_BREAK_TYPE_PC_EQUAL,
1058                 &res);
1059     }
1060   return res;
1061 }
1062
1063 static int
1064 remote_rdp_remove_breakpoint (addr, save)
1065      CORE_ADDR addr;
1066      char *save;
1067 {
1068   int res;
1069   if (ds.rdi_level > 0)
1070     {
1071       send_rdp ("b-p-S-B",
1072                 RDP_CLEAR_BREAK,
1073                 save, 4,
1074                 &res);
1075     }
1076   else
1077     {
1078       send_rdp ("bw-S-B",
1079                 RDP_CLEAR_BREAK,
1080                 addr,
1081                 &res);
1082     }
1083   return res;
1084 }
1085
1086 static void
1087 rdp_step ()
1088 {
1089   if (ds.can_step && 0)
1090     {
1091       /* The pie board can't do steps so I can't test this, and
1092          the other code will always work. */
1093       int status;
1094       send_rdp ("bbw-S-B",
1095                 RDP_STEP, 0, 1,
1096                 &status);
1097     }
1098   else
1099     {
1100       char handle[4];
1101       CORE_ADDR pc = read_register (PC_REGNUM);
1102       pc = arm_get_next_pc (pc);
1103       remote_rdp_insert_breakpoint (pc, &handle);
1104       rdp_execute ();
1105       remote_rdp_remove_breakpoint (pc, &handle);
1106     }
1107 }
1108
1109 static void
1110 remote_rdp_open (args, from_tty)
1111      char *args;
1112      int from_tty;
1113 {
1114   int not_icebreaker;
1115
1116   if (!args)
1117     error_no_arg ("serial port device name");
1118
1119   baud_rate = 9600;
1120
1121   target_preopen (from_tty);
1122
1123   io = SERIAL_OPEN (args);
1124
1125   if (!io)
1126     perror_with_name (args);
1127
1128   SERIAL_RAW (io);
1129
1130   rdp_init (1, from_tty);
1131
1132
1133   if (from_tty)
1134     {
1135       printf_unfiltered ("Remote RDP debugging using %s at %d baud\n", args, baud_rate);
1136     }
1137
1138   rdp_info ();
1139
1140   /* Need to set up the vector interception state */
1141   rdp_catch_vectors();
1142
1143   /*
1144   ** If it's an EmbeddedICE, we need to set the processor config.
1145   ** Assume we can always have ARM7TDI...
1146   */
1147   send_rdp ("bw-SB", RDP_INFO, RDP_INFO_ICEBREAKER, & not_icebreaker);
1148   if (!not_icebreaker)
1149     {
1150       const char * CPU = "ARM7TDI";
1151       int ICEversion;
1152       int len = strlen (CPU);
1153       
1154       send_rdp ("bbbbw-p-SWZ",
1155                 RDP_SELECT_CONFIG,
1156                 RDI_ConfigCPU,  /* Aspect: set the CPU */
1157                 len,            /* The number of bytes in the name */
1158                 RDI_MatchAny,   /* We'll take whatever we get */
1159                 0,              /* We'll take whatever version's there */
1160                 CPU,len,
1161                 & ICEversion);
1162     }
1163
1164   /* command line initialised on 'run'*/
1165
1166   push_target (& remote_rdp_ops);
1167
1168   callback->init (callback);
1169   flush_cached_frames ();
1170   registers_changed ();
1171   stop_pc = read_pc ();
1172   set_current_frame (create_new_frame (read_fp (), stop_pc));
1173   select_frame (get_current_frame (), 0);
1174   print_stack_frame (selected_frame, -1, 1);
1175 }
1176
1177
1178
1179 /* Close out all files and local state before this target loses control. */
1180
1181 static void
1182 remote_rdp_close (quitting)
1183      int quitting;
1184 {
1185   callback->shutdown (callback);
1186   if (io)
1187     SERIAL_CLOSE (io);
1188   io = 0;
1189 }
1190
1191
1192 /* Resume execution of the target process.  STEP says whether to single-step
1193    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
1194    to the target, or zero for no signal.  */
1195
1196 static void
1197 remote_rdp_resume (pid, step, siggnal)
1198      int pid, step;
1199      enum target_signal siggnal;
1200 {
1201   if (step)
1202     rdp_step ();
1203   else
1204     rdp_execute ();
1205 }
1206
1207 /* Wait for inferior process to do something.  Return pid of child,
1208    or -1 in case of error; store status through argument pointer STATUS,
1209    just as `wait' would.  */
1210
1211 static int
1212 remote_rdp_wait (pid, status)
1213      int pid;
1214      struct target_waitstatus *status;
1215 {
1216   switch (ds.rdi_stopped_status)
1217     {
1218     default:
1219     case RDP_RES_RESET:
1220     case RDP_RES_SWI:
1221       status->kind = TARGET_WAITKIND_EXITED;
1222       status->value.integer = read_register (0);
1223       break;
1224     case RDP_RES_AT_BREAKPOINT:
1225       status->kind = TARGET_WAITKIND_STOPPED;
1226       /* The signal in sigrc is a host signal.  That probably
1227          should be fixed.  */
1228       status->value.sig = TARGET_SIGNAL_TRAP;
1229       break;
1230 #if 0
1231     case rdp_signalled:
1232       status->kind = TARGET_WAITKIND_SIGNALLED;
1233       /* The signal in sigrc is a host signal.  That probably
1234          should be fixed.  */
1235       status->value.sig = target_signal_from_host (sigrc);
1236       break;
1237 #endif
1238     }
1239
1240   return inferior_pid;
1241 }
1242
1243 /* Get ready to modify the registers array.  On machines which store
1244    individual registers, this doesn't need to do anything.  On machines
1245    which store all the registers in one fell swoop, this makes sure
1246    that registers contains all the registers from the program being
1247    debugged.  */
1248
1249 static void
1250 remote_rdp_prepare_to_store ()
1251 {
1252   /* Do nothing, since we can store individual regs */
1253 }
1254
1255 static int
1256 remote_rdp_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1257      CORE_ADDR memaddr;
1258      char *myaddr;
1259      int len;
1260      int write;
1261      struct target_ops *target; /* ignored */
1262 {
1263   /* I infer from D Taylor's code that there's a limit on the amount
1264      we can transfer in one chunk.. */
1265   int done = 0;
1266   while (done < len)
1267     {
1268       int justdone;
1269       int thisbite = len - done;
1270       if (thisbite > RDP_MOUTHFULL)
1271         thisbite = RDP_MOUTHFULL;
1272
1273       QUIT;
1274
1275       if (write)
1276         {
1277           justdone = rdp_write (memaddr + done, myaddr + done, thisbite);
1278         }
1279       else
1280         {
1281           justdone = rdp_read (memaddr + done, myaddr + done, thisbite);
1282         }
1283
1284       done += justdone;
1285
1286       if (justdone != thisbite)
1287         break;
1288     }
1289   return done;
1290 }
1291
1292
1293
1294 struct yn
1295 {
1296   const char *name;
1297   int bit;
1298 };
1299 static struct yn stepinfo[] =
1300 {
1301   {"Step more than one instruction", RDP_INFO_ABOUT_STEP_GT_1},
1302   {"Step to jump", RDP_INFO_ABOUT_STEP_TO_JMP},
1303   {"Step one instruction", RDP_INFO_ABOUT_STEP_1},
1304   {0}
1305 };
1306
1307 static struct yn breakinfo[] =
1308 {
1309   {"comparison breakpoints supported", RDP_INFO_ABOUT_BREAK_COMP},
1310   {"range breakpoints supported", RDP_INFO_ABOUT_BREAK_RANGE},
1311   {"watchpoints for byte reads supported", RDP_INFO_ABOUT_BREAK_BYTE_READ},
1312   {"watchpoints for half-word reads supported", RDP_INFO_ABOUT_BREAK_HALFWORD_READ},
1313   {"watchpoints for word reads supported", RDP_INFO_ABOUT_BREAK_WORD_READ},
1314   {"watchpoints for byte writes supported", RDP_INFO_ABOUT_BREAK_BYTE_WRITE},
1315   {"watchpoints for half-word writes supported", RDP_INFO_ABOUT_BREAK_HALFWORD_WRITE},
1316   {"watchpoints for word writes supported", RDP_INFO_ABOUT_BREAK_WORD_WRITE},
1317   {"mask break/watch-points supported", RDP_INFO_ABOUT_BREAK_MASK},
1318 {"thread-specific breakpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_BREAK},
1319 {"thread-specific watchpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_WATCH},
1320   {"conditional breakpoints supported", RDP_INFO_ABOUT_BREAK_COND},
1321   {0}
1322 };
1323
1324
1325 static void
1326 dump_bits (t, info)
1327      struct yn *t;
1328      int info;
1329 {
1330   while (t->name)
1331     {
1332       printf_unfiltered ("  %-45s : %s\n", t->name, (info & t->bit) ? "Yes" : "No");
1333       t++;
1334     }
1335 }
1336
1337 static void
1338 remote_rdp_files_info (target)
1339      struct target_ops *target;
1340 {
1341   printf_filtered ("Target capabilities:\n");
1342   dump_bits (stepinfo, ds.step_info);
1343   dump_bits (breakinfo, ds.break_info);
1344   printf_unfiltered ("target level RDI %x\n", (ds.target_info >> 5) & 3);
1345 }
1346
1347
1348 static void
1349 remote_rdp_create_inferior (exec_file, allargs, env)
1350      char *  exec_file;
1351      char *  allargs;
1352      char ** env;
1353 {
1354   CORE_ADDR entry_point;
1355
1356   if (exec_file == 0 || exec_bfd == 0)
1357    error ("No exec file specified.");
1358
1359   entry_point = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1360
1361   remote_rdp_kill ();    
1362   remove_breakpoints ();
1363   init_wait_for_inferior ();
1364
1365   /* This gives us a chance to set up the command line */
1366   rdp_set_command_line (exec_file, allargs);
1367
1368   inferior_pid = 42;
1369   insert_breakpoints ();  /* Needed to get correct instruction in cache */
1370
1371   /*
1372   ** RDP targets don't provide any facility to set the top of memory,
1373   ** so we don't bother to look for MEMSIZE in the environment.
1374   */
1375
1376   /* Let's go! */
1377   proceed (entry_point, TARGET_SIGNAL_DEFAULT, 0);
1378 }
1379
1380 /* Accept any stray run/attach commands */
1381 static int
1382 remote_rdp_can_run()
1383 {
1384   return 1;
1385 }
1386
1387 /* Attach doesn't need to do anything */
1388 static void
1389 remote_rdp_attach(args, from_tty)
1390      char * args;
1391      int from_tty;
1392 {
1393   return;
1394 }
1395   
1396 /* Define the target subroutine names */
1397
1398 struct target_ops remote_rdp_ops ;
1399
1400 static void init_remote_rdp_ops(void)
1401 {
1402   remote_rdp_ops.to_shortname =   "rdp";
1403   remote_rdp_ops.to_longname =   "Remote Target using the RDProtocol";
1404   remote_rdp_ops.to_doc =   "Use a remote ARM system which uses the ARM Remote Debugging Protocol";
1405   remote_rdp_ops.to_open =   remote_rdp_open;   
1406   remote_rdp_ops.to_close =   remote_rdp_close; 
1407   remote_rdp_ops.to_attach =   remote_rdp_attach;
1408   remote_rdp_ops.to_detach =   NULL;            
1409   remote_rdp_ops.to_resume =   remote_rdp_resume;
1410   remote_rdp_ops.to_wait  =   remote_rdp_wait;  
1411   remote_rdp_ops.to_fetch_registers  =   remote_rdp_fetch_register;
1412   remote_rdp_ops.to_store_registers  =   remote_rdp_store_register;
1413   remote_rdp_ops.to_prepare_to_store =   remote_rdp_prepare_to_store;
1414   remote_rdp_ops.to_xfer_memory  =   remote_rdp_xfer_inferior_memory;
1415   remote_rdp_ops.to_files_info  =   remote_rdp_files_info;
1416   remote_rdp_ops.to_insert_breakpoint =   remote_rdp_insert_breakpoint;
1417   remote_rdp_ops.to_remove_breakpoint =   remote_rdp_remove_breakpoint;
1418   remote_rdp_ops.to_terminal_init  =   NULL;            
1419   remote_rdp_ops.to_terminal_inferior =   NULL;         
1420   remote_rdp_ops.to_terminal_ours_for_output =   NULL;
1421   remote_rdp_ops.to_terminal_ours  =   NULL;    
1422   remote_rdp_ops.to_terminal_info  =   NULL;    
1423   remote_rdp_ops.to_kill  =   remote_rdp_kill;  
1424   remote_rdp_ops.to_load  =   generic_load;     
1425   remote_rdp_ops.to_lookup_symbol =   NULL;                             
1426   remote_rdp_ops.to_create_inferior =   remote_rdp_create_inferior;
1427   remote_rdp_ops.to_mourn_inferior =   generic_mourn_inferior;
1428   remote_rdp_ops.to_can_run  =   remote_rdp_can_run;
1429   remote_rdp_ops.to_notice_signals =   0;       
1430   remote_rdp_ops.to_thread_alive  =   0;        
1431   remote_rdp_ops.to_stop  =   0;                
1432   remote_rdp_ops.to_stratum =   process_stratum;
1433   remote_rdp_ops.DONT_USE =   NULL;             
1434   remote_rdp_ops.to_has_all_memory =   1;       
1435   remote_rdp_ops.to_has_memory =   1;           
1436   remote_rdp_ops.to_has_stack =   1;            
1437   remote_rdp_ops.to_has_registers =   1;        
1438   remote_rdp_ops.to_has_execution =   1;        
1439   remote_rdp_ops.to_sections =   NULL;          
1440   remote_rdp_ops.to_sections_end =   NULL;
1441   remote_rdp_ops.to_magic =   OPS_MAGIC;
1442 }
1443
1444 void
1445 _initialize_remote_rdp ()
1446 {
1447   init_remote_rdp_ops() ;
1448   add_target (&remote_rdp_ops);
1449 }