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