OSDN Git Service

Initial revision
[pf3gnuchains/pf3gnuchains3x.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
324             case RDP_RESET:
325               while ((restype = SERIAL_READCHAR (io, 1)) == RDP_RESET)
326                 ;
327               do
328                 {
329                   printf_unfiltered ("%c", isgraph (restype) ? restype : ' ');
330                 }
331               while ((restype = SERIAL_READCHAR (io, 1)) > 0);
332
333               if (tty)
334                 {
335                   printf_unfiltered ("\nThe board has sent notification that it was reset.\n");
336                   printf_unfiltered ("Waiting for it to settle down...\n");
337                 }
338               sleep (3);
339               if (tty)
340                 printf_unfiltered ("\nTrying again.\n");
341               cold = 0;
342               break;
343
344             default:
345               break;
346
347             case RDP_RES_VALUE:
348               {
349                 int resval = SERIAL_READCHAR (io, 1);
350
351                 if (remote_debug)
352                   printf_unfiltered ("[%02x]\n", resval);
353
354                 switch (resval)
355                   {
356                   case SERIAL_TIMEOUT:
357                     break;
358                   case RDP_RES_VALUE_LITTLE_ENDIAN:
359                     target_byte_order = LITTLE_ENDIAN;
360                     sync = 1;
361                     break;
362                   case RDP_RES_VALUE_BIG_ENDIAN:
363                     target_byte_order = BIG_ENDIAN;
364                     sync = 1;
365                     break;
366                   default:
367                     break;
368                   }
369               }
370             }
371         }
372     }
373
374   if (!sync)
375     {
376       error ("Couldn't reset the board, try pressing the reset button");
377     }
378 }
379
380
381 #ifdef ANSI_PROTOTYPES
382 void
383 send_rdp (char *template,...)
384 #else
385 void
386 send_rdp (char *template, va_alist)
387      va_dcl
388 #endif
389 {
390   char buf[200];
391   char *dst = buf;
392   va_list alist;
393 #ifdef ANSI_PROTOTYPES
394   va_start (alist, template);
395 #else
396   va_start (alist);
397 #endif
398
399   while (*template)
400     {
401       unsigned int val;
402       int *pi;
403       int *pstat;
404       char *pc;
405       int i;
406       switch (*template++)
407         {
408         case 'b':
409           val = va_arg (alist, int);
410           *dst++ = val;
411           break;
412         case 'w':
413           val = va_arg (alist, int);
414           *dst++ = val;
415           *dst++ = val >> 8;
416           *dst++ = val >> 16;
417           *dst++ = val >> 24;
418           break;
419         case 'S':
420           val = get_byte ();
421           if (val != RDP_RES_VALUE)
422             {
423               printf_unfiltered ("got bad res value of %d, %x\n", val, val);
424             }
425           break;
426         case 'V':
427           pstat = va_arg (alist, int *);
428           pi = va_arg (alist, int *);
429
430           *pstat = get_byte ();
431           /* Check the result was zero, if not read the syndrome */
432           if (*pstat)
433             {
434               *pi = get_word ();
435             }
436           break;
437         case 'Z':
438           /* Check the result code */
439           switch (get_byte ())
440             {
441             case 0:
442               /* Success */
443               break;
444             case 253:
445               /* Target can't do it; never mind */
446               printf_unfiltered ("RDP: Insufficient privilege\n");
447               return;
448             case 254:
449               /* Target can't do it; never mind */
450               printf_unfiltered ("RDP: Unimplemented message\n");
451               return;
452             case 255:
453               error ("Command garbled");
454               break;
455             default:
456               error ("Corrupt reply from target");
457               break;
458             }
459           break;
460         case 'W':
461           /* Read a word from the target */
462           pi = va_arg (alist, int *);
463           *pi = get_word ();
464           break;
465         case 'P':
466           /* Read in some bytes from the target. */
467           pc = va_arg (alist, char *);
468           val = va_arg (alist, int);
469           for (i = 0; i < val; i++)
470             {
471               pc[i] = get_byte ();
472             }
473           break;
474         case 'p':
475           /* send what's being pointed at */
476           pc = va_arg (alist, char *);
477           val = va_arg (alist, int);
478           dst = buf;
479           SERIAL_WRITE (io, pc, val);
480           break;
481         case '-':
482           /* Send whats in the queue */
483           if (dst != buf)
484             {
485               SERIAL_WRITE (io, buf, dst - buf);
486               dst = buf;
487             }
488           break;
489         case 'B':
490           pi = va_arg (alist, int *);
491           *pi = get_byte ();
492           break;
493         default:
494           abort ();
495         }
496     }
497   va_end (args);
498
499   if (dst != buf)
500     abort ();
501 }
502
503
504 static int
505 rdp_write (memaddr, buf, len)
506      CORE_ADDR memaddr;
507      char *buf;
508      int len;
509 {
510   int res;
511   int val;
512
513   send_rdp ("bww-p-SV", RDP_MEM_WRITE, memaddr, len, buf, len, &res, &val);
514
515   if (res)
516     {
517       return val;
518     }
519   return len;
520 }
521
522
523 static int
524 rdp_read (memaddr, buf, len)
525      CORE_ADDR memaddr;
526      char *buf;
527      int len;
528 {
529   int res;
530   int val;
531   send_rdp ("bww-S-P-V",
532             RDP_MEM_READ, memaddr, len,
533             buf, len,
534             &res, &val);
535   if (res)
536     {
537       return val;
538     }
539   return len;
540 }
541
542 static void
543 rdp_fetch_one_register (mask, buf)
544      int mask;
545      char *buf;
546 {
547   int val;
548   send_rdp ("bbw-SWZ", RDP_CPU_READ, RDP_CPU_READWRITE_MODE_CURRENT, mask, &val);
549   store_signed_integer (buf, 4, val);
550 }
551
552 static void
553 rdp_fetch_one_fpu_register (mask, buf)
554      int mask;
555      char *buf;
556 {
557 #if 0
558   /* !!! Since the PIE board doesn't work as documented,
559      and it doesn't have FPU hardware anyway and since it
560      slows everything down, I've disabled this. */
561   int val;
562   if (mask == RDP_FPU_READWRITE_MASK_FPS)
563     {
564       /* this guy is only a word */
565       send_rdp ("bbw-SWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, &val);
566       store_signed_integer (buf, 4, val);
567     }
568   else
569     {
570       /* There are 12 bytes long 
571          !! fixme about endianness 
572        */
573       int dummy;                /* I've seen these come back as four words !! */
574       send_rdp ("bbw-SWWWWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, buf + 0, buf + 4, buf + 8, &dummy);
575     }
576 #endif
577   memset (buf, 0, MAX_REGISTER_RAW_SIZE);
578 }
579
580
581 static void
582 rdp_store_one_register (mask, buf)
583      int mask;
584      char *buf;
585 {
586   int val = extract_unsigned_integer (buf, 4);
587
588   send_rdp ("bbww-SZ",
589             RDP_CPU_WRITE, RDP_CPU_READWRITE_MODE_CURRENT, mask, val);
590 }
591
592
593 static void
594 rdp_store_one_fpu_register (mask, buf)
595      int mask;
596      char *buf;
597 {
598 #if 0
599   /* See comment in fetch_one_fpu_register */
600   if (mask == RDP_FPU_READWRITE_MASK_FPS)
601     {
602       int val = extract_unsigned_integer (buf, 4);
603       /* this guy is only a word */
604       send_rdp ("bbww-SZ", RDP_COPRO_WRITE,
605                 FPU_COPRO_NUMBER,
606                 mask, val);
607     }
608   else
609     {
610       /* There are 12 bytes long 
611          !! fixme about endianness 
612        */
613       int dummy = 0;
614       /* I've seen these come as four words, not the three advertized !! */
615       printf ("Sending mask %x\n", mask);
616       send_rdp ("bbwwwww-SZ",
617                 RDP_COPRO_WRITE,
618                 FPU_COPRO_NUMBER,
619                 mask,
620                 *(int *) (buf + 0),
621                 *(int *) (buf + 4),
622                 *(int *) (buf + 8),
623                 0);
624
625       printf ("done mask %x\n", mask);
626     }
627 #endif
628 }
629 \f
630
631 /* Convert between GDB requests and the RDP layer. */
632
633 static void
634 remote_rdp_fetch_register (regno)
635      int regno;
636 {
637   if (regno == -1)
638     {
639       for (regno = 0; regno < NUM_REGS; regno++)
640         remote_rdp_fetch_register (regno);
641     }
642   else
643     {
644       char buf[MAX_REGISTER_RAW_SIZE];
645       if (regno < 15)
646         rdp_fetch_one_register (1 << regno, buf);
647       else if (regno == PC_REGNUM)
648         rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_PC, buf);
649       else if (regno == PS_REGNUM)
650         rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_CPSR, buf);
651       else if (regno == FPS_REGNUM)
652         rdp_fetch_one_fpu_register (RDP_FPU_READWRITE_MASK_FPS, buf);
653       else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
654         rdp_fetch_one_fpu_register (1 << (regno - F0_REGNUM), buf);
655       else
656         {
657           printf ("Help me with fetch reg %d\n", regno);
658         }
659       supply_register (regno, buf);
660     }
661 }
662
663
664 static void
665 remote_rdp_store_register (regno)
666      int regno;
667 {
668   if (regno == -1)
669     {
670       for (regno = 0; regno < NUM_REGS; regno++)
671         remote_rdp_store_register (regno);
672     }
673   else
674     {
675       char tmp[MAX_REGISTER_RAW_SIZE];
676       read_register_gen (regno, tmp);
677       if (regno < 15)
678         rdp_store_one_register (1 << regno, tmp);
679       else if (regno == PC_REGNUM)
680         rdp_store_one_register (RDP_CPU_READWRITE_MASK_PC, tmp);
681       else if (regno == PS_REGNUM)
682         rdp_store_one_register (RDP_CPU_READWRITE_MASK_CPSR, tmp);
683       else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
684         rdp_store_one_fpu_register (1 << (regno - F0_REGNUM), tmp);
685       else
686         {
687           printf ("Help me with reg %d\n", regno);
688         }
689     }
690 }
691
692 static void
693 remote_rdp_kill ()
694 {
695   callback->shutdown (callback);
696 }
697
698
699 static void
700 rdp_info ()
701 {
702   send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_STEP,
703             &ds.step_info);
704   send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_BREAK,
705             &ds.break_info);
706   send_rdp ("bw-S-WW-Z", RDP_INFO, RDP_INFO_ABOUT_TARGET,
707             &ds.target_info,
708             &ds.model_info);
709
710   ds.can_step = ds.step_info & RDP_INFO_ABOUT_STEP_1;
711
712   ds.rdi_level = (ds.target_info >> 5) & 3;
713 }
714
715
716 static void
717 rdp_execute_start ()
718 {
719   /* Start it off, but don't wait for it */
720   send_rdp ("bb-", RDP_EXEC, RDP_EXEC_TYPE_SYNC);
721 }
722
723
724 static void
725 rdp_set_command_line (command, args)
726      char * command;
727      char * args;
728 {
729   /*
730   ** We could use RDP_INFO_SET_CMDLINE to send this, but EmbeddedICE systems
731   ** don't implement that, and get all confused at the unexpected text.
732   ** Instead, just keep a copy, and send it when the target does a SWI_GetEnv
733   */
734
735   if (commandline != NULL)
736     free (commandline);
737
738   commandline = malloc (strlen (command) + strlen (args) + 2);
739   if (commandline != NULL)
740     {
741       strcpy (commandline, command);
742       strcat (commandline, " ");
743       strcat (commandline, args);
744     }
745 }
746
747 static void
748 rdp_catch_vectors ()
749 {
750   /*
751   ** We want the target monitor to intercept the abort vectors
752   ** i.e. stop the program if any of these are used.
753   */
754   send_rdp ("bww-SZ", RDP_INFO, RDP_INFO_VECTOR_CATCH,
755             /*
756             ** Specify a bitmask including
757             **  the reset vector
758             **  the undefined instruction vector
759             **  the prefetch abort vector
760             **  the data abort vector
761             **  the address exception vector
762             */
763             (1<<0)|(1<<1)|(1<<3)|(1<<4)|(1<<5)
764             );
765 }
766   
767
768
769 #define a_byte 1
770 #define a_word 2
771 #define a_string 3
772
773
774 typedef struct
775 {
776   CORE_ADDR n;
777   const char *s;
778 }
779 argsin;
780
781 #define ABYTE 1
782 #define AWORD 2
783 #define ASTRING 3
784 #define ADDRLEN 4
785
786 #define SWI_WriteC                      0x0
787 #define SWI_Write0                      0x2
788 #define SWI_ReadC                       0x4
789 #define SWI_CLI                         0x5
790 #define SWI_GetEnv                      0x10
791 #define SWI_Exit                        0x11
792 #define SWI_EnterOS                     0x16
793
794 #define SWI_GetErrno                    0x60
795 #define SWI_Clock                       0x61
796
797 #define SWI_Time                        0x63
798 #define SWI_Remove                      0x64
799 #define SWI_Rename                      0x65
800 #define SWI_Open                        0x66
801
802 #define SWI_Close                       0x68
803 #define SWI_Write                       0x69
804 #define SWI_Read                        0x6a
805 #define SWI_Seek                        0x6b
806 #define SWI_Flen                        0x6c
807
808 #define SWI_IsTTY                       0x6e
809 #define SWI_TmpNam                      0x6f
810 #define SWI_InstallHandler              0x70
811 #define SWI_GenerateError               0x71
812
813
814 #ifndef O_BINARY
815 #define O_BINARY 0
816 #endif
817
818 static int translate_open_mode[] =
819 {
820   O_RDONLY,                          /* "r"   */
821   O_RDONLY+O_BINARY,                 /* "rb"  */
822   O_RDWR,                            /* "r+"  */
823   O_RDWR  +O_BINARY,                 /* "r+b" */
824   O_WRONLY         +O_CREAT+O_TRUNC, /* "w"   */
825   O_WRONLY+O_BINARY+O_CREAT+O_TRUNC, /* "wb"  */
826   O_RDWR           +O_CREAT+O_TRUNC, /* "w+"  */
827   O_RDWR  +O_BINARY+O_CREAT+O_TRUNC, /* "w+b" */
828   O_WRONLY         +O_APPEND+O_CREAT,/* "a"   */
829   O_WRONLY+O_BINARY+O_APPEND+O_CREAT,/* "ab"  */
830   O_RDWR           +O_APPEND+O_CREAT,/* "a+"  */
831   O_RDWR  +O_BINARY+O_APPEND+O_CREAT /* "a+b" */
832 };
833
834 static int
835 exec_swi (swi, args)
836      int swi;
837      argsin *args;
838 {
839   int i;
840   char c;
841   switch (swi)
842     {
843     case SWI_WriteC:
844       callback->write_stdout (callback, &c, 1);
845       return 0;
846     case SWI_Write0:
847       for (i = 0; i < args->n; i++)
848         callback->write_stdout (callback, args->s, strlen (args->s));
849       return 0;
850     case SWI_ReadC:
851       callback->read_stdin (callback, &c, 1);
852       args->n = c;
853       return 1;
854     case SWI_CLI:
855       args->n = callback->system (callback, args->s);
856       return 1;
857     case SWI_GetErrno:
858       args->n = callback->get_errno (callback);
859       return 1;
860     case SWI_Time:
861       args->n = callback->time (callback, NULL);
862       return 1;
863
864     case SWI_Clock :
865        /* return number of centi-seconds... */
866        args->n = 
867 #ifdef CLOCKS_PER_SEC
868           (CLOCKS_PER_SEC >= 100)
869              ? (clock() / (CLOCKS_PER_SEC / 100))
870              : ((clock() * 100) / CLOCKS_PER_SEC) ;
871 #else
872      /* presume unix... clock() returns microseconds */
873           clock() / 10000 ;
874 #endif
875        return 1 ;
876
877     case SWI_Remove:
878       args->n = callback->unlink (callback, args->s);
879       return 1;
880     case SWI_Rename:
881       args->n = callback->rename (callback, args[0].s, args[1].s);
882       return 1;
883
884     case SWI_Open:
885         /* Now we need to decode the Demon open mode */
886         i = translate_open_mode[args[1].n];
887
888         /* Filename ":tt" is special: it denotes stdin/out */
889         if (strcmp(args->s,":tt")==0)
890           {
891             if (i == O_RDONLY ) /* opening tty "r" */
892               args->n = 0 /* stdin */ ;
893             else 
894               args->n = 1 /* stdout */ ;
895           }
896         else
897           args->n = callback->open (callback, args->s, i);
898       return 1;
899
900     case SWI_Close:
901       args->n = callback->close (callback, args->n);
902       return 1;
903
904     case SWI_Write:
905       /* Return the number of bytes *not* written */
906       args->n = args[1].n -
907         callback->write (callback, args[0].n, args[1].s, args[1].n);
908       return 1;
909
910     case SWI_Read:
911       {
912         char *copy = alloca (args[2].n);
913         int done = callback->read (callback, args[0].n, copy, args[2].n);
914         if (done > 0)
915           remote_rdp_xfer_inferior_memory (args[1].n, copy, done, 1, 0);
916         args->n = args[2].n-done;
917         return 1;
918       }
919
920     case SWI_Seek:
921       /* Return non-zero on failure */
922       args->n = callback->lseek (callback, args[0].n, args[1].n, 0) < 0;
923       return 1;
924
925     case SWI_Flen:
926       {
927         long old = callback->lseek (callback, args->n, 0, SEEK_CUR);
928         args->n = callback->lseek (callback, args->n, 0, SEEK_END);
929         callback->lseek (callback, args->n, old, 0);
930         return 1;
931       }
932
933     case SWI_IsTTY:
934       args->n = callback->isatty (callback, args->n);
935       return 1;
936
937     case SWI_GetEnv:
938       if (commandline != NULL)
939         {
940           int len = strlen (commandline);
941           if (len > 255)
942             {
943               len = 255;
944               commandline [255]='\0';
945             }
946           remote_rdp_xfer_inferior_memory (args[0].n,
947                                            commandline, len+1, 1, 0);
948         }
949       else
950         remote_rdp_xfer_inferior_memory (args[0].n, "", 1, 1, 0);
951       return 1;
952       
953     default:
954       return 0;
955     }
956 }
957
958
959 static void
960 handle_swi ()
961 {
962   argsin args[3];
963   char *buf;
964   int len;
965   int count = 0;
966
967   int swino = get_word ();
968   int type = get_byte ();
969   while (type != 0)
970     {
971       switch (type & 0x3)
972         {
973         case ABYTE:
974           args[count].n = get_byte ();
975           break;
976
977         case AWORD:
978           args[count].n = get_word ();
979           break;
980
981         case ASTRING:
982           /* If the word is under 32 bytes it will be sent otherwise
983              an address to it is passed. Also: Special case of 255 */
984
985           len = get_byte ();
986           if (len > 32)
987             {
988               if (len == 255)
989                 {
990                   len = get_word ();
991                 }
992               buf = alloca (len);
993               remote_rdp_xfer_inferior_memory (get_word (),
994                                                buf,
995                                                len,
996                                                0,
997                                                0);
998             }
999           else
1000             {
1001               int i;
1002               buf = alloca (len + 1);
1003               for (i = 0; i < len; i++)
1004                 buf[i] = get_byte ();
1005               buf[i] = 0;
1006             }
1007           args[count].n = len;
1008           args[count].s = buf;
1009           break;
1010
1011         default:
1012           error ("Unimplented SWI argument");
1013         }
1014
1015       type = type >> 2;
1016       count++;
1017     }
1018
1019   if (exec_swi (swino, args))
1020     {
1021       /* We have two options here reply with either a byte or a word
1022          which is stored in args[0].n. There is no harm in replying with
1023          a word all the time, so thats what I do! */
1024       send_rdp ("bbw-", RDP_OSOpReply, RDP_OSOpWord, args[0].n);
1025     }
1026   else
1027     {
1028       send_rdp ("bb-", RDP_OSOpReply, RDP_OSOpNothing);
1029     }
1030 }
1031
1032 static void
1033 rdp_execute_finish ()
1034 {
1035   int running = 1;
1036
1037   while (running)
1038     {
1039       int res;
1040       res = SERIAL_READCHAR (io, 1);
1041       while (res == SERIAL_TIMEOUT)
1042         {
1043           QUIT;
1044           printf_filtered ("Waiting for target..\n");
1045           res = SERIAL_READCHAR (io, 1);
1046         }
1047
1048       switch (res)
1049         {
1050         case RDP_RES_SWI:
1051           handle_swi ();
1052           break;
1053         case RDP_RES_VALUE:
1054           send_rdp ("B", &ds.rdi_stopped_status);
1055           running = 0;
1056           break;
1057         case RDP_RESET:
1058           printf_filtered ("Target reset\n");
1059           running = 0;
1060           break;
1061         default:
1062           printf_filtered ("Ignoring %x\n", res);
1063           break;
1064         }
1065     }
1066 }
1067
1068
1069 static void
1070 rdp_execute ()
1071 {
1072   rdp_execute_start ();
1073   rdp_execute_finish ();
1074 }
1075
1076 static int
1077 remote_rdp_insert_breakpoint (addr, save)
1078      CORE_ADDR addr;
1079      char *save;
1080 {
1081   int res;
1082   if (ds.rdi_level > 0)
1083     {
1084       send_rdp ("bwb-SWB",
1085                 RDP_SET_BREAK,
1086                 addr,
1087                 RDP_SET_BREAK_TYPE_PC_EQUAL | RDP_SET_BREAK_TYPE_GET_HANDLE,
1088                 save,
1089                 &res);
1090     }
1091   else
1092     {
1093       send_rdp ("bwb-SB",
1094                 RDP_SET_BREAK,
1095                 addr,
1096                 RDP_SET_BREAK_TYPE_PC_EQUAL,
1097                 &res);
1098     }
1099   return res;
1100 }
1101
1102 static int
1103 remote_rdp_remove_breakpoint (addr, save)
1104      CORE_ADDR addr;
1105      char *save;
1106 {
1107   int res;
1108   if (ds.rdi_level > 0)
1109     {
1110       send_rdp ("b-p-S-B",
1111                 RDP_CLEAR_BREAK,
1112                 save, 4,
1113                 &res);
1114     }
1115   else
1116     {
1117       send_rdp ("bw-S-B",
1118                 RDP_CLEAR_BREAK,
1119                 addr,
1120                 &res);
1121     }
1122   return res;
1123 }
1124
1125 static void
1126 rdp_step ()
1127 {
1128   if (ds.can_step && 0)
1129     {
1130       /* The pie board can't do steps so I can't test this, and
1131          the other code will always work. */
1132       int status;
1133       send_rdp ("bbw-S-B",
1134                 RDP_STEP, 0, 1,
1135                 &status);
1136     }
1137   else
1138     {
1139       char handle[4];
1140       CORE_ADDR pc = read_register (PC_REGNUM);
1141       pc = arm_get_next_pc (pc);
1142       remote_rdp_insert_breakpoint (pc, &handle);
1143       rdp_execute ();
1144       remote_rdp_remove_breakpoint (pc, &handle);
1145     }
1146 }
1147
1148 static void
1149 remote_rdp_open (args, from_tty)
1150      char *args;
1151      int from_tty;
1152 {
1153   int not_icebreaker;
1154
1155   if (!args)
1156     error_no_arg ("serial port device name");
1157
1158   baud_rate = 9600;
1159
1160   target_preopen (from_tty);
1161
1162   io = SERIAL_OPEN (args);
1163
1164   if (!io)
1165     perror_with_name (args);
1166
1167   SERIAL_RAW (io);
1168
1169   rdp_init (1, from_tty);
1170
1171
1172   if (from_tty)
1173     {
1174       printf_unfiltered ("Remote RDP debugging using %s at %d baud\n", args, baud_rate);
1175     }
1176
1177   rdp_info ();
1178
1179   /* Need to set up the vector interception state */
1180   rdp_catch_vectors();
1181
1182   /*
1183   ** If it's an EmbeddedICE, we need to set the processor config.
1184   ** Assume we can always have ARM7TDI...
1185   */
1186   send_rdp ("bw-SB", RDP_INFO, RDP_INFO_ICEBREAKER, & not_icebreaker);
1187   if (!not_icebreaker)
1188     {
1189       const char * CPU = "ARM7TDI";
1190       int ICEversion;
1191       int len = strlen (CPU);
1192       
1193       send_rdp ("bbbbw-p-SWZ",
1194                 RDP_SELECT_CONFIG,
1195                 RDI_ConfigCPU,  /* Aspect: set the CPU */
1196                 len,            /* The number of bytes in the name */
1197                 RDI_MatchAny,   /* We'll take whatever we get */
1198                 0,              /* We'll take whatever version's there */
1199                 CPU,len,
1200                 & ICEversion);
1201     }
1202
1203   /* command line initialised on 'run'*/
1204
1205   push_target (& remote_rdp_ops);
1206
1207   callback->init (callback);
1208   flush_cached_frames ();
1209   registers_changed ();
1210   stop_pc = read_pc ();
1211   set_current_frame (create_new_frame (read_fp (), stop_pc));
1212   select_frame (get_current_frame (), 0);
1213   print_stack_frame (selected_frame, -1, 1);
1214 }
1215
1216
1217
1218 /* Close out all files and local state before this target loses control. */
1219
1220 static void
1221 remote_rdp_close (quitting)
1222      int quitting;
1223 {
1224   callback->shutdown (callback);
1225   if (io)
1226     SERIAL_CLOSE (io);
1227   io = 0;
1228 }
1229
1230
1231 /* Resume execution of the target process.  STEP says whether to single-step
1232    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
1233    to the target, or zero for no signal.  */
1234
1235 static void
1236 remote_rdp_resume (pid, step, siggnal)
1237      int pid, step;
1238      enum target_signal siggnal;
1239 {
1240   if (step)
1241     rdp_step ();
1242   else
1243     rdp_execute ();
1244 }
1245
1246 /* Wait for inferior process to do something.  Return pid of child,
1247    or -1 in case of error; store status through argument pointer STATUS,
1248    just as `wait' would.  */
1249
1250 static int
1251 remote_rdp_wait (pid, status)
1252      int pid;
1253      struct target_waitstatus *status;
1254 {
1255   switch (ds.rdi_stopped_status)
1256     {
1257     default:
1258     case RDP_RES_RESET:
1259     case RDP_RES_SWI:
1260       status->kind = TARGET_WAITKIND_EXITED;
1261       status->value.integer = read_register (0);
1262       break;
1263     case RDP_RES_AT_BREAKPOINT:
1264       status->kind = TARGET_WAITKIND_STOPPED;
1265       /* The signal in sigrc is a host signal.  That probably
1266          should be fixed.  */
1267       status->value.sig = TARGET_SIGNAL_TRAP;
1268       break;
1269 #if 0
1270     case rdp_signalled:
1271       status->kind = TARGET_WAITKIND_SIGNALLED;
1272       /* The signal in sigrc is a host signal.  That probably
1273          should be fixed.  */
1274       status->value.sig = target_signal_from_host (sigrc);
1275       break;
1276 #endif
1277     }
1278
1279   return inferior_pid;
1280 }
1281
1282 /* Get ready to modify the registers array.  On machines which store
1283    individual registers, this doesn't need to do anything.  On machines
1284    which store all the registers in one fell swoop, this makes sure
1285    that registers contains all the registers from the program being
1286    debugged.  */
1287
1288 static void
1289 remote_rdp_prepare_to_store ()
1290 {
1291   /* Do nothing, since we can store individual regs */
1292 }
1293
1294 static int
1295 remote_rdp_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1296      CORE_ADDR memaddr;
1297      char *myaddr;
1298      int len;
1299      int write;
1300      struct target_ops *target; /* ignored */
1301 {
1302   /* I infer from D Taylor's code that there's a limit on the amount
1303      we can transfer in one chunk.. */
1304   int done = 0;
1305   while (done < len)
1306     {
1307       int justdone;
1308       int thisbite = len - done;
1309       if (thisbite > RDP_MOUTHFULL)
1310         thisbite = RDP_MOUTHFULL;
1311
1312       QUIT;
1313
1314       if (write)
1315         {
1316           justdone = rdp_write (memaddr + done, myaddr + done, thisbite);
1317         }
1318       else
1319         {
1320           justdone = rdp_read (memaddr + done, myaddr + done, thisbite);
1321         }
1322
1323       done += justdone;
1324
1325       if (justdone != thisbite)
1326         break;
1327     }
1328   return done;
1329 }
1330
1331
1332
1333 struct yn
1334 {
1335   const char *name;
1336   int bit;
1337 };
1338 static struct yn stepinfo[] =
1339 {
1340   {"Step more than one instruction", RDP_INFO_ABOUT_STEP_GT_1},
1341   {"Step to jump", RDP_INFO_ABOUT_STEP_TO_JMP},
1342   {"Step one instruction", RDP_INFO_ABOUT_STEP_1},
1343   {0}
1344 };
1345
1346 static struct yn breakinfo[] =
1347 {
1348   {"comparison breakpoints supported", RDP_INFO_ABOUT_BREAK_COMP},
1349   {"range breakpoints supported", RDP_INFO_ABOUT_BREAK_RANGE},
1350   {"watchpoints for byte reads supported", RDP_INFO_ABOUT_BREAK_BYTE_READ},
1351   {"watchpoints for half-word reads supported", RDP_INFO_ABOUT_BREAK_HALFWORD_READ},
1352   {"watchpoints for word reads supported", RDP_INFO_ABOUT_BREAK_WORD_READ},
1353   {"watchpoints for byte writes supported", RDP_INFO_ABOUT_BREAK_BYTE_WRITE},
1354   {"watchpoints for half-word writes supported", RDP_INFO_ABOUT_BREAK_HALFWORD_WRITE},
1355   {"watchpoints for word writes supported", RDP_INFO_ABOUT_BREAK_WORD_WRITE},
1356   {"mask break/watch-points supported", RDP_INFO_ABOUT_BREAK_MASK},
1357 {"thread-specific breakpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_BREAK},
1358 {"thread-specific watchpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_WATCH},
1359   {"conditional breakpoints supported", RDP_INFO_ABOUT_BREAK_COND},
1360   {0}
1361 };
1362
1363
1364 static void
1365 dump_bits (t, info)
1366      struct yn *t;
1367      int info;
1368 {
1369   while (t->name)
1370     {
1371       printf_unfiltered ("  %-45s : %s\n", t->name, (info & t->bit) ? "Yes" : "No");
1372       t++;
1373     }
1374 }
1375
1376 static void
1377 remote_rdp_files_info (target)
1378      struct target_ops *target;
1379 {
1380   printf_filtered ("Target capabilities:\n");
1381   dump_bits (stepinfo, ds.step_info);
1382   dump_bits (breakinfo, ds.break_info);
1383   printf_unfiltered ("target level RDI %x\n", (ds.target_info >> 5) & 3);
1384 }
1385
1386
1387 static void
1388 remote_rdp_create_inferior (exec_file, allargs, env)
1389      char *  exec_file;
1390      char *  allargs;
1391      char ** env;
1392 {
1393   CORE_ADDR entry_point;
1394
1395   if (exec_file == 0 || exec_bfd == 0)
1396    error ("No executable file specified.");
1397
1398   entry_point = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1399
1400   remote_rdp_kill ();    
1401   remove_breakpoints ();
1402   init_wait_for_inferior ();
1403
1404   /* This gives us a chance to set up the command line */
1405   rdp_set_command_line (exec_file, allargs);
1406
1407   inferior_pid = 42;
1408   insert_breakpoints ();  /* Needed to get correct instruction in cache */
1409
1410   /*
1411   ** RDP targets don't provide any facility to set the top of memory,
1412   ** so we don't bother to look for MEMSIZE in the environment.
1413   */
1414
1415   /* Let's go! */
1416   proceed (entry_point, TARGET_SIGNAL_DEFAULT, 0);
1417 }
1418
1419 /* Accept any stray run/attach commands */
1420 static int
1421 remote_rdp_can_run()
1422 {
1423   return 1;
1424 }
1425
1426 /* Attach doesn't need to do anything */
1427 static void
1428 remote_rdp_attach(args, from_tty)
1429      char * args;
1430      int from_tty;
1431 {
1432   return;
1433 }
1434   
1435 /* Define the target subroutine names */
1436
1437 struct target_ops remote_rdp_ops ;
1438
1439 static void 
1440 init_remote_rdp_ops(void)
1441 {
1442   remote_rdp_ops.to_shortname =   "rdp";
1443   remote_rdp_ops.to_longname =   "Remote Target using the RDProtocol";
1444   remote_rdp_ops.to_doc =   "Use a remote ARM system which uses the ARM Remote Debugging Protocol";
1445   remote_rdp_ops.to_open =   remote_rdp_open;   
1446   remote_rdp_ops.to_close =   remote_rdp_close; 
1447   remote_rdp_ops.to_attach =   remote_rdp_attach;
1448   remote_rdp_ops.to_post_attach = NULL;
1449   remote_rdp_ops.to_require_attach = NULL;
1450   remote_rdp_ops.to_detach =   NULL;
1451   remote_rdp_ops.to_require_detach = NULL;              
1452   remote_rdp_ops.to_resume =   remote_rdp_resume;
1453   remote_rdp_ops.to_wait  =   remote_rdp_wait;
1454   remote_rdp_ops.to_post_wait = NULL;   
1455   remote_rdp_ops.to_fetch_registers  =   remote_rdp_fetch_register;
1456   remote_rdp_ops.to_store_registers  =   remote_rdp_store_register;
1457   remote_rdp_ops.to_prepare_to_store =   remote_rdp_prepare_to_store;
1458   remote_rdp_ops.to_xfer_memory  =   remote_rdp_xfer_inferior_memory;
1459   remote_rdp_ops.to_files_info  =   remote_rdp_files_info;
1460   remote_rdp_ops.to_insert_breakpoint =   remote_rdp_insert_breakpoint;
1461   remote_rdp_ops.to_remove_breakpoint =   remote_rdp_remove_breakpoint;
1462   remote_rdp_ops.to_terminal_init  =   NULL;            
1463   remote_rdp_ops.to_terminal_inferior =   NULL;         
1464   remote_rdp_ops.to_terminal_ours_for_output =   NULL;
1465   remote_rdp_ops.to_terminal_ours  =   NULL;    
1466   remote_rdp_ops.to_terminal_info  =   NULL;    
1467   remote_rdp_ops.to_kill  =   remote_rdp_kill;  
1468   remote_rdp_ops.to_load  =   generic_load;     
1469   remote_rdp_ops.to_lookup_symbol =   NULL;                             
1470   remote_rdp_ops.to_create_inferior =   remote_rdp_create_inferior;
1471   remote_rdp_ops.to_post_startup_inferior = NULL;
1472   remote_rdp_ops.to_acknowledge_created_inferior = NULL;
1473   remote_rdp_ops.to_clone_and_follow_inferior = NULL;
1474   remote_rdp_ops.to_post_follow_inferior_by_clone = NULL;
1475   remote_rdp_ops.to_insert_fork_catchpoint = NULL;
1476   remote_rdp_ops.to_remove_fork_catchpoint = NULL;
1477   remote_rdp_ops.to_insert_vfork_catchpoint = NULL;
1478   remote_rdp_ops.to_remove_vfork_catchpoint = NULL;
1479   remote_rdp_ops.to_has_forked = NULL;
1480   remote_rdp_ops.to_has_vforked = NULL;
1481   remote_rdp_ops.to_can_follow_vfork_prior_to_exec = NULL;
1482   remote_rdp_ops.to_post_follow_vfork = NULL;
1483   remote_rdp_ops.to_insert_exec_catchpoint = NULL;
1484   remote_rdp_ops.to_remove_exec_catchpoint = NULL;
1485   remote_rdp_ops.to_has_execd = NULL;
1486   remote_rdp_ops.to_reported_exec_events_per_exec_call = NULL;
1487   remote_rdp_ops.to_has_exited = NULL;
1488   remote_rdp_ops.to_mourn_inferior =   generic_mourn_inferior;
1489   remote_rdp_ops.to_can_run  =   remote_rdp_can_run;
1490   remote_rdp_ops.to_notice_signals =   0;       
1491   remote_rdp_ops.to_thread_alive  =   0;        
1492   remote_rdp_ops.to_stop  =   0;
1493   remote_rdp_ops.to_pid_to_exec_file = NULL;
1494   remote_rdp_ops.to_core_file_to_sym_file = NULL;               
1495   remote_rdp_ops.to_stratum =   process_stratum;
1496   remote_rdp_ops.DONT_USE =   NULL;             
1497   remote_rdp_ops.to_has_all_memory =   1;       
1498   remote_rdp_ops.to_has_memory =   1;           
1499   remote_rdp_ops.to_has_stack =   1;            
1500   remote_rdp_ops.to_has_registers =   1;        
1501   remote_rdp_ops.to_has_execution =   1;        
1502   remote_rdp_ops.to_sections =   NULL;          
1503   remote_rdp_ops.to_sections_end =   NULL;
1504   remote_rdp_ops.to_magic =   OPS_MAGIC;
1505 }
1506
1507 void
1508 _initialize_remote_rdp ()
1509 {
1510   init_remote_rdp_ops() ;
1511   add_target (&remote_rdp_ops);
1512 }