1 /* Remote debugging for the ARM RDP interface.
2 Copyright 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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.
25 Much of this file (in particular the SWI stuff) is based on code by
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.
44 #ifdef ANSI_PROTOTYPES
52 #include "remote-utils.h"
54 extern struct target_ops remote_rdp_ops;
56 static host_callback *callback = &default_callback;
65 char command_line[10];
67 int rdi_stopped_status;
72 /* Definitions for the RDP protocol. */
74 #define RDP_MOUTHFULL (1<<6)
75 #define FPU_COPRO_NUMBER 1
78 #define RDP_OPEN_TYPE_COLD 0
79 #define RDP_OPEN_TYPE_WARM 1
80 #define RDP_OPEN_TYPE_RETURN_SEX (1<<3)
84 #define RDP_MEM_READ 2
86 #define RDP_MEM_WRITE 3
88 #define RDP_CPU_READ 4
89 #define RDP_CPU_WRITE 5
90 #define RDP_CPU_READWRITE_MODE_CURRENT 255
91 #define RDP_CPU_READWRITE_MASK_PC (1<<16)
92 #define RDP_CPU_READWRITE_MASK_CPSR (1<<17)
93 #define RDP_CPU_READWRITE_MASK_SPSR (1<<18)
95 #define RDP_COPRO_READ 6
96 #define RDP_COPRO_WRITE 7
97 #define RDP_FPU_READWRITE_MASK_FPS (1<<8)
99 #define RDP_SET_BREAK 0xa
100 #define RDP_SET_BREAK_TYPE_PC_EQUAL 0
101 #define RDP_SET_BREAK_TYPE_GET_HANDLE (0x10)
103 #define RDP_CLEAR_BREAK 0xb
105 #define RDP_EXEC 0x10
106 #define RDP_EXEC_TYPE_SYNC 0
108 #define RDP_STEP 0x11
110 #define RDP_INFO 0x12
111 #define RDP_INFO_ABOUT_STEP 2
112 #define RDP_INFO_ABOUT_STEP_GT_1 1
113 #define RDP_INFO_ABOUT_STEP_TO_JMP 2
114 #define RDP_INFO_ABOUT_STEP_1 4
115 #define RDP_INFO_ABOUT_TARGET 0
116 #define RDP_INFO_ABOUT_BREAK 1
117 #define RDP_INFO_ABOUT_BREAK_COMP 1
118 #define RDP_INFO_ABOUT_BREAK_RANGE 2
119 #define RDP_INFO_ABOUT_BREAK_BYTE_READ 4
120 #define RDP_INFO_ABOUT_BREAK_HALFWORD_READ 8
121 #define RDP_INFO_ABOUT_BREAK_WORD_READ (1<<4)
122 #define RDP_INFO_ABOUT_BREAK_BYTE_WRITE (1<<5)
123 #define RDP_INFO_ABOUT_BREAK_HALFWORD_WRITE (1<<6)
124 #define RDP_INFO_ABOUT_BREAK_WORD_WRITE (1<<7)
125 #define RDP_INFO_ABOUT_BREAK_MASK (1<<8)
126 #define RDP_INFO_ABOUT_BREAK_THREAD_BREAK (1<<9)
127 #define RDP_INFO_ABOUT_BREAK_THREAD_WATCH (1<<10)
128 #define RDP_INFO_ABOUT_BREAK_COND (1<<11)
130 #define RDP_RESET 0x7f
132 /* Returns from RDP */
133 #define RDP_RES_STOPPED 0x20
134 #define RDP_RES_SWI 0x21
135 #define RDP_RES_FATAL 0x5e
136 #define RDP_RES_VALUE 0x5f
137 #define RDP_RES_VALUE_LITTLE_ENDIAN 240
138 #define RDP_RES_VALUE_BIG_ENDIAN 241
139 #define RDP_RES_RESET 0x7f
140 #define RDP_RES_AT_BREAKPOINT 143
142 #define RDP_OSOpReply 0x13
143 #define RDP_OSOpWord 2
144 #define RDP_OSOpNothing 0
146 static int timeout = 2;
149 remote_rdp_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
153 struct target_ops * target));
156 /* Stuff for talking to the serial layer. */
161 int c = SERIAL_READCHAR (io, timeout);
163 if (c == SERIAL_TIMEOUT)
166 return (unsigned char) c;
168 error ("Timeout reading from remote_system");
172 printf ("[%02x]", c);
176 /* Note that the target always speaks little-endian to us,
177 even if it's a big endian machine. */
181 unsigned int val = 0;
184 for (n = 0; n < 4; n++)
196 SERIAL_WRITE (io, &val, 1);
203 /* We always send in little endian */
210 SERIAL_WRITE (io, b, 4);
215 /* Stuff for talking to the RDP layer. */
218 /* This is a bit more fancy that need be so that it syncs even in nasty cases. */
225 for (oloop = 0; oloop < 2; oloop++)
228 SERIAL_FLUSH_INPUT (io);
229 for (longtry = 0; longtry < 2; longtry++)
232 int flush_and_retry = 0;
234 for (try = 0; try < 10 && !flush_and_retry; try++)
239 put_byte (RDP_OPEN_TYPE_COLD | RDP_OPEN_TYPE_RETURN_SEX);
242 restype = SERIAL_READCHAR (io, 1);
252 printf_unfiltered ("Got res return %d\n", restype);
256 int resval = SERIAL_READCHAR (io, 1);
262 case RDP_RES_VALUE_LITTLE_ENDIAN:
263 target_byte_order = LITTLE_ENDIAN;
265 case RDP_RES_VALUE_BIG_ENDIAN:
266 target_byte_order = BIG_ENDIAN;
269 printf_unfiltered ("Trying to sync, got resval %d\n", resval);
276 error ("Couldn't reset the board, try pressing the reset button");
280 #ifdef ANSI_PROTOTYPES
282 send_rdp (char *template,...)
285 send_rdp (char *template, va_alist)
292 #ifdef ANSI_PROTOTYPES
293 va_start (alist, template);
308 val = va_arg (alist, int);
312 val = va_arg (alist, int);
320 if (val != RDP_RES_VALUE)
322 printf_unfiltered ("got bad res value of %d, %x\n", val, val);
326 pstat = va_arg (alist, int *);
327 pi = va_arg (alist, int *);
329 *pstat = get_byte ();
330 /* Check the result was zero, if not read the syndrome */
337 /* Check the result code, error if not zero */
339 error ("Command garbled");
342 /* Read a word from the target */
343 pi = va_arg (alist, int *);
347 /* Read in some bytes from the target. */
348 pc = va_arg (alist, char *);
349 val = va_arg (alist, int);
350 for (i = 0; i < val; i++)
356 /* send what's being pointed at */
357 pc = va_arg (alist, char *);
358 val = va_arg (alist, int);
360 SERIAL_WRITE (io, pc, val);
363 /* Send whats in the queue */
366 SERIAL_WRITE (io, buf, dst - buf);
371 pi = va_arg (alist, int *);
386 rdp_write (memaddr, buf, len)
394 send_rdp ("bww-p-SV", RDP_MEM_WRITE, memaddr, len, buf, len, &res, &val);
405 rdp_read (memaddr, buf, len)
412 send_rdp ("bww-S-P-V",
413 RDP_MEM_READ, memaddr, len,
424 rdp_fetch_one_register (mask, buf)
429 send_rdp ("bbw-SWZ", RDP_CPU_READ, RDP_CPU_READWRITE_MODE_CURRENT, mask, &val);
430 store_signed_integer (buf, 4, val);
434 rdp_fetch_one_fpu_register (mask, buf)
439 /* !!! Since the PIE board doesn't work as documented,
440 and it doesn't have FPU hardware anyway and since it
441 slows everything down, I've disabled this. */
443 if (mask == RDP_FPU_READWRITE_MASK_FPS)
445 /* this guy is only a word */
446 send_rdp ("bbw-SWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, &val);
447 store_signed_integer (buf, 4, val);
451 /* There are 12 bytes long
452 !! fixme about endianness
454 int dummy; /* I've seen these come back as four words !! */
455 send_rdp ("bbw-SWWWWZ", RDP_COPRO_READ, FPU_COPRO_NUMBER, mask, buf + 0, buf + 4, buf + 8, &dummy);
458 memset (buf, 0, MAX_REGISTER_RAW_SIZE);
463 rdp_store_one_register (mask, buf)
467 int val = extract_unsigned_integer (buf, 4);
470 RDP_CPU_WRITE, RDP_CPU_READWRITE_MODE_CURRENT, mask, val);
475 rdp_store_one_fpu_register (mask, buf)
480 /* See comment in fetch_one_fpu_register */
481 if (mask == RDP_FPU_READWRITE_MASK_FPS)
483 int val = extract_unsigned_integer (buf, 4);
484 /* this guy is only a word */
485 send_rdp ("bbww-SZ", RDP_COPRO_WRITE,
491 /* There are 12 bytes long
492 !! fixme about endianness
495 /* I've seen these come as four words, not the three advertized !! */
496 printf ("Sending mask %x\n", mask);
497 send_rdp ("bbwwwww-SZ",
506 printf ("done mask %x\n", mask);
512 /* Convert between GDB requests and the RDP layer. */
515 remote_rdp_fetch_register (regno)
520 for (regno = 0; regno < NUM_REGS; regno++)
521 remote_rdp_fetch_register (regno);
525 char buf[MAX_REGISTER_RAW_SIZE];
527 rdp_fetch_one_register (1 << regno, buf);
528 else if (regno == PC_REGNUM)
529 rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_PC, buf);
530 else if (regno == PS_REGNUM)
531 rdp_fetch_one_register (RDP_CPU_READWRITE_MASK_CPSR, buf);
532 else if (regno == FPS_REGNUM)
533 rdp_fetch_one_fpu_register (RDP_FPU_READWRITE_MASK_FPS, buf);
534 else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
535 rdp_fetch_one_fpu_register (1 << (regno - F0_REGNUM), buf);
538 printf ("Help me with fetch reg %d\n", regno);
540 supply_register (regno, buf);
546 remote_rdp_store_register (regno)
551 for (regno = 0; regno < NUM_REGS; regno++)
552 remote_rdp_store_register (regno);
556 char tmp[MAX_REGISTER_RAW_SIZE];
557 read_register_gen (regno, tmp);
559 rdp_store_one_register (1 << regno, tmp);
560 else if (regno == PC_REGNUM)
561 rdp_store_one_register (RDP_CPU_READWRITE_MASK_PC, tmp);
562 else if (regno == PS_REGNUM)
563 rdp_store_one_register (RDP_CPU_READWRITE_MASK_CPSR, tmp);
564 else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
565 rdp_store_one_fpu_register (1 << (regno - F0_REGNUM), tmp);
568 printf ("Help me with reg %d\n", regno);
576 callback->shutdown (callback);
583 send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_STEP,
585 send_rdp ("bw-S-W-Z", RDP_INFO, RDP_INFO_ABOUT_BREAK,
587 send_rdp ("bw-S-WW-Z", RDP_INFO, RDP_INFO_ABOUT_TARGET,
591 ds.can_step = ds.step_info & RDP_INFO_ABOUT_STEP_1;
593 ds.rdi_level = (ds.target_info >> 5) & 3;
600 /* Start it off, but don't wait for it */
601 send_rdp ("bb-", RDP_EXEC, RDP_EXEC_TYPE_SYNC);
623 #define SWI_WriteC 0x0
624 #define SWI_Write0 0x2
625 #define SWI_ReadC 0x4
627 #define SWI_GetEnv 0x10
628 #define SWI_Exit 0x11
629 #define SWI_EnterOS 0x16
631 #define SWI_GetErrno 0x60
632 #define SWI_Clock 0x61
634 #define SWI_Time 0x63
635 #define SWI_Remove 0x64
636 #define SWI_Rename 0x65
637 #define SWI_Open 0x66
639 #define SWI_Close 0x68
640 #define SWI_Write 0x69
641 #define SWI_Read 0x6a
642 #define SWI_Seek 0x6b
643 #define SWI_Flen 0x6c
645 #define SWI_IsTTY 0x6e
646 #define SWI_TmpNam 0x6f
647 #define SWI_InstallHandler 0x70
648 #define SWI_GenerateError 0x71
661 callback->write_stdout (callback, &c, 1);
664 for (i = 0; i < args->n; i++)
665 callback->write_stdout (callback, args->s, strlen (args->s));
668 callback->read_stdin (callback, &c, 1);
672 args->n = callback->system (callback, args->s);
675 args->n = callback->get_errno (callback);
678 args->n = callback->time (callback, NULL);
681 args->n = callback->unlink (callback, args->s);
684 args->n = callback->rename (callback, args[0].s, args[1].s);
704 args->n = callback->open (callback, args->s, i);
708 args->n = callback->close (callback, args->n);
712 args->n = callback->write (callback, args[0].n, args[1].s, args[1].n);
716 char *copy = alloca (args[2].n);
717 int done = callback->read (callback, args[0].n, copy, args[2].n);
719 remote_rdp_xfer_inferior_memory (args[0].n, copy, done, 1, 0);
725 args->n = callback->lseek (callback, args[0].n, args[1].n, 0) >= 0;
729 long old = callback->lseek (callback, args->n, 1, 1);
730 args->n = callback->lseek (callback, args->n, 2, 0);
731 callback->lseek (callback, args->n, old, 0);
736 args->n = callback->isatty (callback, args->n);
753 int swino = get_word ();
754 int type = get_byte ();
760 args[count].n = get_byte ();
764 args[count].n = get_word ();
768 /* If the word is under 32 bytes it will be sent otherwise
769 an address to it is passed. Also: Special case of 255 */
779 remote_rdp_xfer_inferior_memory (get_word (),
788 buf = alloca (len + 1);
789 for (i = 0; i < len; i++)
790 buf[i] = get_byte ();
798 error ("Unimplented SWI argument");
805 if (exec_swi (swino, args))
807 /* We have two options here reply with either a byte or a word
808 which is stored in args[0].n. There is no harm in replying with
809 a word all the time, so thats what I do! */
810 send_rdp ("bbw-", RDP_OSOpReply, RDP_OSOpWord, args[0].n);
814 send_rdp ("bb-", RDP_OSOpReply, RDP_OSOpNothing);
819 rdp_execute_finish ()
826 res = SERIAL_READCHAR (io, 1);
827 while (res == SERIAL_TIMEOUT)
830 printf_filtered ("Waiting for target..\n");
831 res = SERIAL_READCHAR (io, 1);
840 send_rdp ("B", &ds.rdi_stopped_status);
844 printf_filtered ("Target reset\n");
848 printf_filtered ("Ignoring %x\n", res);
858 rdp_execute_start ();
859 rdp_execute_finish ();
863 remote_rdp_insert_breakpoint (addr, save)
868 if (ds.rdi_level > 0)
873 RDP_SET_BREAK_TYPE_PC_EQUAL | RDP_SET_BREAK_TYPE_GET_HANDLE,
882 RDP_SET_BREAK_TYPE_PC_EQUAL,
889 remote_rdp_remove_breakpoint (addr, save)
894 if (ds.rdi_level > 0)
914 if (ds.can_step && 0)
916 /* The pie board can't do steps so I can't test this, and
917 the other code will always work. */
926 CORE_ADDR pc = read_register (PC_REGNUM);
927 pc = arm_get_next_pc (pc);
928 remote_rdp_insert_breakpoint (pc, &handle);
930 remote_rdp_remove_breakpoint (pc, &handle);
935 remote_rdp_open (args, from_tty)
940 error_no_arg ("serial port device name");
942 target_preopen (from_tty);
944 io = SERIAL_OPEN (args);
947 perror_with_name (args);
956 printf_unfiltered ("Remote RDP debugging using %s\n", args);
959 push_target (&remote_rdp_ops);
961 callback->init (callback);
962 flush_cached_frames ();
963 registers_changed ();
964 stop_pc = read_pc ();
965 set_current_frame (create_new_frame (read_fp (), stop_pc));
966 select_frame (get_current_frame (), 0);
967 print_stack_frame (selected_frame, -1, 1);
972 /* Close out all files and local state before this target loses control. */
975 remote_rdp_close (quitting)
978 callback->shutdown (callback);
982 /* Terminate the open connection to the remote debugger. */
985 remote_rdp_detach (args, from_tty)
992 /* Resume execution of the target process. STEP says whether to single-step
993 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
994 to the target, or zero for no signal. */
997 remote_rdp_resume (pid, step, siggnal)
999 enum target_signal siggnal;
1007 /* Wait for inferior process to do something. Return pid of child,
1008 or -1 in case of error; store status through argument pointer STATUS,
1009 just as `wait' would. */
1012 remote_rdp_wait (pid, status)
1014 struct target_waitstatus *status;
1016 switch (ds.rdi_stopped_status)
1021 status->kind = TARGET_WAITKIND_EXITED;
1022 status->value.integer = read_register (0);
1024 case RDP_RES_AT_BREAKPOINT:
1025 status->kind = TARGET_WAITKIND_STOPPED;
1026 /* The signal in sigrc is a host signal. That probably
1028 status->value.sig = TARGET_SIGNAL_TRAP;
1032 status->kind = TARGET_WAITKIND_SIGNALLED;
1033 /* The signal in sigrc is a host signal. That probably
1035 status->value.sig = target_signal_from_host (sigrc);
1040 return inferior_pid;
1043 /* Get ready to modify the registers array. On machines which store
1044 individual registers, this doesn't need to do anything. On machines
1045 which store all the registers in one fell swoop, this makes sure
1046 that registers contains all the registers from the program being
1050 remote_rdp_prepare_to_store ()
1052 /* Do nothing, since we can store individual regs */
1056 remote_rdp_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1061 struct target_ops *target; /* ignored */
1063 /* I infer from D Taylor's code that there's a limit on the amount
1064 we can transfer in one chunk.. */
1069 int thisbite = len - done;
1070 if (thisbite > RDP_MOUTHFULL)
1071 thisbite = RDP_MOUTHFULL;
1077 justdone = rdp_write (memaddr + done, myaddr + done, thisbite);
1081 justdone = rdp_read (memaddr + done, myaddr + done, thisbite);
1086 if (justdone != thisbite)
1099 static struct yn stepinfo[] =
1101 {"Step more than one instruction", RDP_INFO_ABOUT_STEP_GT_1},
1102 {"Step to jump", RDP_INFO_ABOUT_STEP_TO_JMP},
1103 {"Step one instruction", RDP_INFO_ABOUT_STEP_1},
1107 static struct yn breakinfo[] =
1109 {"comparison breakpoints supported", RDP_INFO_ABOUT_BREAK_COMP},
1110 {"range breakpoints supported", RDP_INFO_ABOUT_BREAK_RANGE},
1111 {"watchpoints for byte reads supported", RDP_INFO_ABOUT_BREAK_BYTE_READ},
1112 {"watchpoints for half-word reads supported", RDP_INFO_ABOUT_BREAK_HALFWORD_READ},
1113 {"watchpoints for word reads supported", RDP_INFO_ABOUT_BREAK_WORD_READ},
1114 {"watchpoints for byte writes supported", RDP_INFO_ABOUT_BREAK_BYTE_WRITE},
1115 {"watchpoints for half-word writes supported", RDP_INFO_ABOUT_BREAK_HALFWORD_WRITE},
1116 {"watchpoints for word writes supported", RDP_INFO_ABOUT_BREAK_WORD_WRITE},
1117 {"mask break/watch-points supported", RDP_INFO_ABOUT_BREAK_MASK},
1118 {"thread-specific breakpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_BREAK},
1119 {"thread-specific watchpoints supported", RDP_INFO_ABOUT_BREAK_THREAD_WATCH},
1120 {"conditional breakpoints supported", RDP_INFO_ABOUT_BREAK_COND},
1132 printf_unfiltered (" %-45s : %s\n", t->name, (info & t->bit) ? "Yes" : "No");
1138 remote_rdp_files_info (target)
1139 struct target_ops *target;
1141 printf_filtered ("Target capabilities:\n");
1142 dump_bits (stepinfo, ds.step_info);
1143 dump_bits (breakinfo, ds.break_info);
1144 printf_unfiltered ("target level RDI %x\n", (ds.target_info >> 5) & 3);
1148 /* Define the target subroutine names */
1150 struct target_ops remote_rdp_ops =
1152 "rdp", /* to_shortname */
1154 "Remote Target using the RDProtocol",
1156 "Use a remote ARM system which uses the ARM Remote Debugging Protocol",
1157 remote_rdp_open, /* to_open */
1158 remote_rdp_close, /* to_close */
1159 NULL, /* to_attach */
1160 NULL, /* to_detach */
1161 remote_rdp_resume, /* to_resume */
1162 remote_rdp_wait, /* to_wait */
1163 remote_rdp_fetch_register, /* to_fetch_registers */
1164 remote_rdp_store_register, /* to_store_registers */
1165 remote_rdp_prepare_to_store, /* to_prepare_to_store */
1166 remote_rdp_xfer_inferior_memory, /* to_xfer_memory */
1167 remote_rdp_files_info, /* to_files_info */
1168 remote_rdp_insert_breakpoint, /* to_insert_breakpoint */
1169 remote_rdp_remove_breakpoint, /* to_remove_breakpoint */
1170 NULL, /* to_terminal_init */
1171 NULL, /* to_terminal_inferior */
1172 NULL, /* to_terminal_ours_for_output */
1173 NULL, /* to_terminal_ours */
1174 NULL, /* to_terminal_info */
1175 remote_rdp_kill, /* to_kill */
1176 generic_load, /* to_load */
1177 NULL, /* to_lookup_symbol */
1178 NULL, /* to_create_inferior */
1179 generic_mourn_inferior, /* to_mourn_inferior */
1181 0, /* to_notice_signals */
1182 0, /* to_thread_alive */
1184 process_stratum, /* to_stratum */
1186 1, /* to_has_all_memory */
1187 1, /* to_has_memory */
1188 1, /* to_has_stack */
1189 1, /* to_has_registers */
1190 1, /* to_has_execution */
1191 NULL, /* sections */
1192 NULL, /* sections_end */
1193 OPS_MAGIC, /* to_magic */
1197 _initialize_remote_rdp ()
1199 add_target (&remote_rdp_ops);