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