xref: /openbsd-src/gnu/usr.bin/binutils/gdb/remote-mips.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* Remote debugging interface for MIPS remote debugging protocol.
2b725ae77Skettenis 
3b725ae77Skettenis    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4*63addd46Skettenis    2002, 2003, 2004 Free Software Foundation, Inc.
5b725ae77Skettenis 
6e93f7393Sniklas    Contributed by Cygnus Support.  Written by Ian Lance Taylor
7e93f7393Sniklas    <ian@cygnus.com>.
8e93f7393Sniklas 
9e93f7393Sniklas    This file is part of GDB.
10e93f7393Sniklas 
11e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
12e93f7393Sniklas    it under the terms of the GNU General Public License as published by
13e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
14e93f7393Sniklas    (at your option) any later version.
15e93f7393Sniklas 
16e93f7393Sniklas    This program is distributed in the hope that it will be useful,
17e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
18e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19e93f7393Sniklas    GNU General Public License for more details.
20e93f7393Sniklas 
21e93f7393Sniklas    You should have received a copy of the GNU General Public License
22e93f7393Sniklas    along with this program; if not, write to the Free Software
23b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
24b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
25e93f7393Sniklas 
26e93f7393Sniklas #include "defs.h"
27e93f7393Sniklas #include "inferior.h"
28e93f7393Sniklas #include "bfd.h"
29e93f7393Sniklas #include "symfile.h"
30e93f7393Sniklas #include "gdbcmd.h"
31e93f7393Sniklas #include "gdbcore.h"
32e93f7393Sniklas #include "serial.h"
33e93f7393Sniklas #include "target.h"
34e93f7393Sniklas #include "remote-utils.h"
35b725ae77Skettenis #include "gdb_string.h"
36b725ae77Skettenis #include "gdb_stat.h"
37b725ae77Skettenis #include "regcache.h"
38b725ae77Skettenis #include <ctype.h>
39b725ae77Skettenis #include "mips-tdep.h"
40e93f7393Sniklas 
41b725ae77Skettenis 
42b725ae77Skettenis /* Breakpoint types.  Values 0, 1, and 2 must agree with the watch
43b725ae77Skettenis    types passed by breakpoint.c to target_insert_watchpoint.
44b725ae77Skettenis    Value 3 is our own invention, and is used for ordinary instruction
45b725ae77Skettenis    breakpoints.  Value 4 is used to mark an unused watchpoint in tables.  */
46b725ae77Skettenis enum break_type
47b725ae77Skettenis   {
48b725ae77Skettenis     BREAK_WRITE,		/* 0 */
49b725ae77Skettenis     BREAK_READ,			/* 1 */
50b725ae77Skettenis     BREAK_ACCESS,		/* 2 */
51b725ae77Skettenis     BREAK_FETCH,		/* 3 */
52b725ae77Skettenis     BREAK_UNUSED		/* 4 */
53b725ae77Skettenis   };
54b725ae77Skettenis 
55e93f7393Sniklas /* Prototypes for local functions.  */
56e93f7393Sniklas 
57b725ae77Skettenis static int mips_readchar (int timeout);
58e93f7393Sniklas 
59b725ae77Skettenis static int mips_receive_header (unsigned char *hdr, int *pgarbage,
60b725ae77Skettenis 				int ch, int timeout);
61e93f7393Sniklas 
62b725ae77Skettenis static int mips_receive_trailer (unsigned char *trlr, int *pgarbage,
63b725ae77Skettenis 				 int *pch, int timeout);
64e93f7393Sniklas 
65b725ae77Skettenis static int mips_cksum (const unsigned char *hdr,
66b725ae77Skettenis 		       const unsigned char *data, int len);
67e93f7393Sniklas 
68b725ae77Skettenis static void mips_send_packet (const char *s, int get_ack);
69e93f7393Sniklas 
70b725ae77Skettenis static void mips_send_command (const char *cmd, int prompt);
71e93f7393Sniklas 
72b725ae77Skettenis static int mips_receive_packet (char *buff, int throw_error, int timeout);
73e93f7393Sniklas 
74b725ae77Skettenis static ULONGEST mips_request (int cmd, ULONGEST addr, ULONGEST data,
75b725ae77Skettenis 			      int *perr, int timeout, char *buff);
76e93f7393Sniklas 
77b725ae77Skettenis static void mips_initialize (void);
78e93f7393Sniklas 
79b725ae77Skettenis static void mips_open (char *name, int from_tty);
80e93f7393Sniklas 
81b725ae77Skettenis static void pmon_open (char *name, int from_tty);
82e93f7393Sniklas 
83b725ae77Skettenis static void ddb_open (char *name, int from_tty);
84e93f7393Sniklas 
85b725ae77Skettenis static void lsi_open (char *name, int from_tty);
86e93f7393Sniklas 
87b725ae77Skettenis static void mips_close (int quitting);
88e93f7393Sniklas 
89b725ae77Skettenis static void mips_detach (char *args, int from_tty);
90e93f7393Sniklas 
91b725ae77Skettenis static void mips_resume (ptid_t ptid, int step,
92b725ae77Skettenis                          enum target_signal siggnal);
93e93f7393Sniklas 
94b725ae77Skettenis static ptid_t mips_wait (ptid_t ptid,
95b725ae77Skettenis                                struct target_waitstatus *status);
96e93f7393Sniklas 
97b725ae77Skettenis static int mips_map_regno (int regno);
98e93f7393Sniklas 
99b725ae77Skettenis static void mips_fetch_registers (int regno);
100e93f7393Sniklas 
101b725ae77Skettenis static void mips_prepare_to_store (void);
102e93f7393Sniklas 
103b725ae77Skettenis static void mips_store_registers (int regno);
104e93f7393Sniklas 
105b725ae77Skettenis static unsigned int mips_fetch_word (CORE_ADDR addr);
106e93f7393Sniklas 
107b725ae77Skettenis static int mips_store_word (CORE_ADDR addr, unsigned int value,
108b725ae77Skettenis 			    char *old_contents);
109e93f7393Sniklas 
110b725ae77Skettenis static int mips_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
111b725ae77Skettenis 			     int write,
112b725ae77Skettenis 			     struct mem_attrib *attrib,
113b725ae77Skettenis 			     struct target_ops *target);
114e93f7393Sniklas 
115b725ae77Skettenis static void mips_files_info (struct target_ops *ignore);
116e93f7393Sniklas 
117b725ae77Skettenis static void mips_mourn_inferior (void);
118e93f7393Sniklas 
119b725ae77Skettenis static int pmon_makeb64 (unsigned long v, char *p, int n, int *chksum);
120e93f7393Sniklas 
121b725ae77Skettenis static int pmon_zeroset (int recsize, char **buff, int *amount,
122b725ae77Skettenis 			 unsigned int *chksum);
123e93f7393Sniklas 
124b725ae77Skettenis static int pmon_checkset (int recsize, char **buff, int *value);
125e93f7393Sniklas 
126b725ae77Skettenis static void pmon_make_fastrec (char **outbuf, unsigned char *inbuf,
127e93f7393Sniklas 			       int *inptr, int inamount, int *recsize,
128b725ae77Skettenis 			       unsigned int *csum, unsigned int *zerofill);
129e93f7393Sniklas 
130b725ae77Skettenis static int pmon_check_ack (char *mesg);
131e93f7393Sniklas 
132b725ae77Skettenis static void pmon_start_download (void);
133e93f7393Sniklas 
134b725ae77Skettenis static void pmon_end_download (int final, int bintotal);
135e93f7393Sniklas 
136b725ae77Skettenis static void pmon_download (char *buffer, int length);
137e93f7393Sniklas 
138b725ae77Skettenis static void pmon_load_fast (char *file);
139e93f7393Sniklas 
140b725ae77Skettenis static void mips_load (char *file, int from_tty);
141b725ae77Skettenis 
142b725ae77Skettenis static int mips_make_srec (char *buffer, int type, CORE_ADDR memaddr,
143b725ae77Skettenis 			   unsigned char *myaddr, int len);
144b725ae77Skettenis 
145b725ae77Skettenis static int set_breakpoint (CORE_ADDR addr, int len, enum break_type type);
146b725ae77Skettenis 
147b725ae77Skettenis static int clear_breakpoint (CORE_ADDR addr, int len, enum break_type type);
148b725ae77Skettenis 
149b725ae77Skettenis static int common_breakpoint (int set, CORE_ADDR addr, int len,
150b725ae77Skettenis 			      enum break_type type);
151b725ae77Skettenis 
152e93f7393Sniklas /* Forward declarations.  */
153e93f7393Sniklas extern struct target_ops mips_ops;
154e93f7393Sniklas extern struct target_ops pmon_ops;
155e93f7393Sniklas extern struct target_ops ddb_ops;
156b725ae77Skettenis /* *INDENT-OFF* */
157e93f7393Sniklas /* The MIPS remote debugging interface is built on top of a simple
158e93f7393Sniklas    packet protocol.  Each packet is organized as follows:
159e93f7393Sniklas 
160e93f7393Sniklas    SYN  The first character is always a SYN (ASCII 026, or ^V).  SYN
161e93f7393Sniklas    may not appear anywhere else in the packet.  Any time a SYN is
162e93f7393Sniklas    seen, a new packet should be assumed to have begun.
163e93f7393Sniklas 
164e93f7393Sniklas    TYPE_LEN
165e93f7393Sniklas    This byte contains the upper five bits of the logical length
166e93f7393Sniklas    of the data section, plus a single bit indicating whether this
167e93f7393Sniklas    is a data packet or an acknowledgement.  The documentation
168e93f7393Sniklas    indicates that this bit is 1 for a data packet, but the actual
169e93f7393Sniklas    board uses 1 for an acknowledgement.  The value of the byte is
170e93f7393Sniklas    0x40 + (ack ? 0x20 : 0) + (len >> 6)
171e93f7393Sniklas    (we always have 0 <= len < 1024).  Acknowledgement packets do
172e93f7393Sniklas    not carry data, and must have a data length of 0.
173e93f7393Sniklas 
174e93f7393Sniklas    LEN1 This byte contains the lower six bits of the logical length of
175e93f7393Sniklas    the data section.  The value is
176e93f7393Sniklas    0x40 + (len & 0x3f)
177e93f7393Sniklas 
178e93f7393Sniklas    SEQ  This byte contains the six bit sequence number of the packet.
179e93f7393Sniklas    The value is
180e93f7393Sniklas    0x40 + seq
181e93f7393Sniklas    An acknowlegment packet contains the sequence number of the
182e93f7393Sniklas    packet being acknowledged plus 1 modulo 64.  Data packets are
183e93f7393Sniklas    transmitted in sequence.  There may only be one outstanding
184e93f7393Sniklas    unacknowledged data packet at a time.  The sequence numbers
185e93f7393Sniklas    are independent in each direction.  If an acknowledgement for
186e93f7393Sniklas    the previous packet is received (i.e., an acknowledgement with
187e93f7393Sniklas    the sequence number of the packet just sent) the packet just
188e93f7393Sniklas    sent should be retransmitted.  If no acknowledgement is
189e93f7393Sniklas    received within a timeout period, the packet should be
190e93f7393Sniklas    retransmitted.  This has an unfortunate failure condition on a
191e93f7393Sniklas    high-latency line, as a delayed acknowledgement may lead to an
192e93f7393Sniklas    endless series of duplicate packets.
193e93f7393Sniklas 
194e93f7393Sniklas    DATA The actual data bytes follow.  The following characters are
195e93f7393Sniklas    escaped inline with DLE (ASCII 020, or ^P):
196e93f7393Sniklas    SYN (026)    DLE S
197e93f7393Sniklas    DLE (020)    DLE D
198e93f7393Sniklas    ^C  (003)    DLE C
199e93f7393Sniklas    ^S  (023)    DLE s
200e93f7393Sniklas    ^Q  (021)    DLE q
201e93f7393Sniklas    The additional DLE characters are not counted in the logical
202e93f7393Sniklas    length stored in the TYPE_LEN and LEN1 bytes.
203e93f7393Sniklas 
204e93f7393Sniklas    CSUM1
205e93f7393Sniklas    CSUM2
206e93f7393Sniklas    CSUM3
207e93f7393Sniklas    These bytes contain an 18 bit checksum of the complete
208e93f7393Sniklas    contents of the packet excluding the SEQ byte and the
209e93f7393Sniklas    CSUM[123] bytes.  The checksum is simply the twos complement
210e93f7393Sniklas    addition of all the bytes treated as unsigned characters.  The
211e93f7393Sniklas    values of the checksum bytes are:
212e93f7393Sniklas    CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
213e93f7393Sniklas    CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
214e93f7393Sniklas    CSUM3: 0x40 + (cksum & 0x3f)
215e93f7393Sniklas 
216e93f7393Sniklas    It happens that the MIPS remote debugging protocol always
217e93f7393Sniklas    communicates with ASCII strings.  Because of this, this
218e93f7393Sniklas    implementation doesn't bother to handle the DLE quoting mechanism,
219e93f7393Sniklas    since it will never be required.  */
220b725ae77Skettenis /* *INDENT-ON* */
221b725ae77Skettenis 
222e93f7393Sniklas 
223e93f7393Sniklas /* The SYN character which starts each packet.  */
224e93f7393Sniklas #define SYN '\026'
225e93f7393Sniklas 
226e93f7393Sniklas /* The 0x40 used to offset each packet (this value ensures that all of
227e93f7393Sniklas    the header and trailer bytes, other than SYN, are printable ASCII
228e93f7393Sniklas    characters).  */
229e93f7393Sniklas #define HDR_OFFSET 0x40
230e93f7393Sniklas 
231e93f7393Sniklas /* The indices of the bytes in the packet header.  */
232e93f7393Sniklas #define HDR_INDX_SYN 0
233e93f7393Sniklas #define HDR_INDX_TYPE_LEN 1
234e93f7393Sniklas #define HDR_INDX_LEN1 2
235e93f7393Sniklas #define HDR_INDX_SEQ 3
236e93f7393Sniklas #define HDR_LENGTH 4
237e93f7393Sniklas 
238e93f7393Sniklas /* The data/ack bit in the TYPE_LEN header byte.  */
239e93f7393Sniklas #define TYPE_LEN_DA_BIT 0x20
240e93f7393Sniklas #define TYPE_LEN_DATA 0
241e93f7393Sniklas #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
242e93f7393Sniklas 
243e93f7393Sniklas /* How to compute the header bytes.  */
244e93f7393Sniklas #define HDR_SET_SYN(data, len, seq) (SYN)
245e93f7393Sniklas #define HDR_SET_TYPE_LEN(data, len, seq) \
246e93f7393Sniklas   (HDR_OFFSET \
247e93f7393Sniklas    + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
248e93f7393Sniklas    + (((len) >> 6) & 0x1f))
249e93f7393Sniklas #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
250e93f7393Sniklas #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
251e93f7393Sniklas 
252e93f7393Sniklas /* Check that a header byte is reasonable.  */
253e93f7393Sniklas #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
254e93f7393Sniklas 
255e93f7393Sniklas /* Get data from the header.  These macros evaluate their argument
256e93f7393Sniklas    multiple times.  */
257e93f7393Sniklas #define HDR_IS_DATA(hdr) \
258e93f7393Sniklas   (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
259e93f7393Sniklas #define HDR_GET_LEN(hdr) \
260e93f7393Sniklas   ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
261b725ae77Skettenis #define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
262e93f7393Sniklas 
263e93f7393Sniklas /* The maximum data length.  */
264e93f7393Sniklas #define DATA_MAXLEN 1023
265e93f7393Sniklas 
266e93f7393Sniklas /* The trailer offset.  */
267e93f7393Sniklas #define TRLR_OFFSET HDR_OFFSET
268e93f7393Sniklas 
269e93f7393Sniklas /* The indices of the bytes in the packet trailer.  */
270e93f7393Sniklas #define TRLR_INDX_CSUM1 0
271e93f7393Sniklas #define TRLR_INDX_CSUM2 1
272e93f7393Sniklas #define TRLR_INDX_CSUM3 2
273e93f7393Sniklas #define TRLR_LENGTH 3
274e93f7393Sniklas 
275e93f7393Sniklas /* How to compute the trailer bytes.  */
276e93f7393Sniklas #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
277e93f7393Sniklas #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >>  6) & 0x3f))
278e93f7393Sniklas #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum)      ) & 0x3f))
279e93f7393Sniklas 
280e93f7393Sniklas /* Check that a trailer byte is reasonable.  */
281e93f7393Sniklas #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
282e93f7393Sniklas 
283e93f7393Sniklas /* Get data from the trailer.  This evaluates its argument multiple
284e93f7393Sniklas    times.  */
285e93f7393Sniklas #define TRLR_GET_CKSUM(trlr) \
286e93f7393Sniklas   ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
287e93f7393Sniklas    + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) <<  6) \
288e93f7393Sniklas    + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
289e93f7393Sniklas 
290e93f7393Sniklas /* The sequence number modulos.  */
291e93f7393Sniklas #define SEQ_MODULOS (64)
292e93f7393Sniklas 
293b725ae77Skettenis /* PMON commands to load from the serial port or UDP socket.  */
294b725ae77Skettenis #define LOAD_CMD	"load -b -s tty0\r"
295b725ae77Skettenis #define LOAD_CMD_UDP	"load -b -s udp\r"
296b725ae77Skettenis 
297b725ae77Skettenis /* The target vectors for the four different remote MIPS targets.
298b725ae77Skettenis    These are initialized with code in _initialize_remote_mips instead
299b725ae77Skettenis    of static initializers, to make it easier to extend the target_ops
300b725ae77Skettenis    vector later.  */
301b725ae77Skettenis struct target_ops mips_ops, pmon_ops, ddb_ops, lsi_ops;
302b725ae77Skettenis 
303b725ae77Skettenis enum mips_monitor_type
304b725ae77Skettenis   {
305e93f7393Sniklas     /* IDT/SIM monitor being used: */
306e93f7393Sniklas     MON_IDT,
307e93f7393Sniklas     /* PMON monitor being used: */
308e93f7393Sniklas     MON_PMON,			/* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov  9 1995 17:19:50 */
309e93f7393Sniklas     MON_DDB,			/* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems,  Thu Jun 6 09:28:40 PDT 1996 */
310b725ae77Skettenis     MON_LSI,			/* 4.3.12 [EB,FP], LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
311e93f7393Sniklas     /* Last and unused value, for sizing vectors, etc. */
312e93f7393Sniklas     MON_LAST
313e93f7393Sniklas   };
314e93f7393Sniklas static enum mips_monitor_type mips_monitor = MON_LAST;
315e93f7393Sniklas 
316b725ae77Skettenis /* The monitor prompt text.  If the user sets the PMON prompt
317b725ae77Skettenis    to some new value, the GDB `set monitor-prompt' command must also
318b725ae77Skettenis    be used to inform GDB about the expected prompt.  Otherwise, GDB
319b725ae77Skettenis    will not be able to connect to PMON in mips_initialize().
320b725ae77Skettenis    If the `set monitor-prompt' command is not used, the expected
321b725ae77Skettenis    default prompt will be set according the target:
322b725ae77Skettenis    target               prompt
323b725ae77Skettenis    -----                -----
324b725ae77Skettenis    pmon         PMON>
325b725ae77Skettenis    ddb          NEC010>
326b725ae77Skettenis    lsi          PMON>
327b725ae77Skettenis  */
328b725ae77Skettenis static char *mips_monitor_prompt;
329e93f7393Sniklas 
330e93f7393Sniklas /* Set to 1 if the target is open.  */
331e93f7393Sniklas static int mips_is_open;
332e93f7393Sniklas 
333e93f7393Sniklas /* Currently active target description (if mips_is_open == 1) */
334e93f7393Sniklas static struct target_ops *current_ops;
335e93f7393Sniklas 
336e93f7393Sniklas /* Set to 1 while the connection is being initialized.  */
337e93f7393Sniklas static int mips_initializing;
338e93f7393Sniklas 
339b725ae77Skettenis /* Set to 1 while the connection is being brought down.  */
340b725ae77Skettenis static int mips_exiting;
341b725ae77Skettenis 
342e93f7393Sniklas /* The next sequence number to send.  */
343e93f7393Sniklas static unsigned int mips_send_seq;
344e93f7393Sniklas 
345e93f7393Sniklas /* The next sequence number we expect to receive.  */
346e93f7393Sniklas static unsigned int mips_receive_seq;
347e93f7393Sniklas 
348e93f7393Sniklas /* The time to wait before retransmitting a packet, in seconds.  */
349e93f7393Sniklas static int mips_retransmit_wait = 3;
350e93f7393Sniklas 
351e93f7393Sniklas /* The number of times to try retransmitting a packet before giving up.  */
352e93f7393Sniklas static int mips_send_retries = 10;
353e93f7393Sniklas 
354e93f7393Sniklas /* The number of garbage characters to accept when looking for an
355e93f7393Sniklas    SYN for the next packet.  */
356b725ae77Skettenis static int mips_syn_garbage = 10;
357e93f7393Sniklas 
358e93f7393Sniklas /* The time to wait for a packet, in seconds.  */
359e93f7393Sniklas static int mips_receive_wait = 5;
360e93f7393Sniklas 
361e93f7393Sniklas /* Set if we have sent a packet to the board but have not yet received
362e93f7393Sniklas    a reply.  */
363e93f7393Sniklas static int mips_need_reply = 0;
364e93f7393Sniklas 
365e93f7393Sniklas /* Handle used to access serial I/O stream.  */
366b725ae77Skettenis static struct serial *mips_desc;
367b725ae77Skettenis 
368b725ae77Skettenis /* UDP handle used to download files to target.  */
369b725ae77Skettenis static struct serial *udp_desc;
370b725ae77Skettenis static int udp_in_use;
371b725ae77Skettenis 
372b725ae77Skettenis /* TFTP filename used to download files to DDB board, in the form
373b725ae77Skettenis    host:filename.  */
374b725ae77Skettenis static char *tftp_name;		/* host:filename */
375b725ae77Skettenis static char *tftp_localname;	/* filename portion of above */
376b725ae77Skettenis static int tftp_in_use;
377b725ae77Skettenis static FILE *tftp_file;
378e93f7393Sniklas 
379e93f7393Sniklas /* Counts the number of times the user tried to interrupt the target (usually
380e93f7393Sniklas    via ^C.  */
381e93f7393Sniklas static int interrupt_count;
382e93f7393Sniklas 
383e93f7393Sniklas /* If non-zero, means that the target is running. */
384e93f7393Sniklas static int mips_wait_flag = 0;
385e93f7393Sniklas 
386e93f7393Sniklas /* If non-zero, monitor supports breakpoint commands. */
387b725ae77Skettenis static int monitor_supports_breakpoints = 0;
388e93f7393Sniklas 
389e93f7393Sniklas /* Data cache header.  */
390e93f7393Sniklas 
391b725ae77Skettenis #if 0				/* not used (yet?) */
392e93f7393Sniklas static DCACHE *mips_dcache;
393b725ae77Skettenis #endif
394e93f7393Sniklas 
395e93f7393Sniklas /* Non-zero means that we've just hit a read or write watchpoint */
396e93f7393Sniklas static int hit_watchpoint;
397e93f7393Sniklas 
398b725ae77Skettenis /* Table of breakpoints/watchpoints (used only on LSI PMON target).
399b725ae77Skettenis    The table is indexed by a breakpoint number, which is an integer
400b725ae77Skettenis    from 0 to 255 returned by the LSI PMON when a breakpoint is set.
401b725ae77Skettenis  */
402b725ae77Skettenis #define MAX_LSI_BREAKPOINTS 256
403b725ae77Skettenis struct lsi_breakpoint_info
404b725ae77Skettenis   {
405b725ae77Skettenis     enum break_type type;	/* type of breakpoint */
406b725ae77Skettenis     CORE_ADDR addr;		/* address of breakpoint */
407b725ae77Skettenis     int len;			/* length of region being watched */
408b725ae77Skettenis     unsigned long value;	/* value to watch */
409b725ae77Skettenis   }
410b725ae77Skettenis lsi_breakpoints[MAX_LSI_BREAKPOINTS];
411b725ae77Skettenis 
412b725ae77Skettenis /* Error/warning codes returned by LSI PMON for breakpoint commands.
413b725ae77Skettenis    Warning values may be ORed together; error values may not.  */
414b725ae77Skettenis #define W_WARN	0x100		/* This bit is set if the error code is a warning */
415b725ae77Skettenis #define W_MSK   0x101		/* warning: Range feature is supported via mask */
416b725ae77Skettenis #define W_VAL   0x102		/* warning: Value check is not supported in hardware */
417b725ae77Skettenis #define W_QAL   0x104		/* warning: Requested qualifiers are not supported in hardware */
418b725ae77Skettenis 
419b725ae77Skettenis #define E_ERR	0x200		/* This bit is set if the error code is an error */
420b725ae77Skettenis #define E_BPT   0x200		/* error: No such breakpoint number */
421b725ae77Skettenis #define E_RGE   0x201		/* error: Range is not supported */
422b725ae77Skettenis #define E_QAL   0x202		/* error: The requested qualifiers can not be used */
423b725ae77Skettenis #define E_OUT   0x203		/* error: Out of hardware resources */
424b725ae77Skettenis #define E_NON   0x204		/* error: Hardware breakpoint not supported */
425b725ae77Skettenis 
426b725ae77Skettenis struct lsi_error
427b725ae77Skettenis   {
428b725ae77Skettenis     int code;			/* error code */
429b725ae77Skettenis     char *string;		/* string associated with this code */
430b725ae77Skettenis   };
431b725ae77Skettenis 
432b725ae77Skettenis struct lsi_error lsi_warning_table[] =
433b725ae77Skettenis {
434b725ae77Skettenis   {W_MSK, "Range feature is supported via mask"},
435b725ae77Skettenis   {W_VAL, "Value check is not supported in hardware"},
436b725ae77Skettenis   {W_QAL, "Requested qualifiers are not supported in hardware"},
437b725ae77Skettenis   {0, NULL}
438b725ae77Skettenis };
439b725ae77Skettenis 
440b725ae77Skettenis struct lsi_error lsi_error_table[] =
441b725ae77Skettenis {
442b725ae77Skettenis   {E_BPT, "No such breakpoint number"},
443b725ae77Skettenis   {E_RGE, "Range is not supported"},
444b725ae77Skettenis   {E_QAL, "The requested qualifiers can not be used"},
445b725ae77Skettenis   {E_OUT, "Out of hardware resources"},
446b725ae77Skettenis   {E_NON, "Hardware breakpoint not supported"},
447b725ae77Skettenis   {0, NULL}
448b725ae77Skettenis };
449b725ae77Skettenis 
450b725ae77Skettenis /* Set to 1 with the 'set monitor-warnings' command to enable printing
451b725ae77Skettenis    of warnings returned by PMON when hardware breakpoints are used.  */
452b725ae77Skettenis static int monitor_warnings;
453b725ae77Skettenis 
454b725ae77Skettenis 
455b725ae77Skettenis static void
close_ports(void)456b725ae77Skettenis close_ports (void)
457b725ae77Skettenis {
458b725ae77Skettenis   mips_is_open = 0;
459b725ae77Skettenis   serial_close (mips_desc);
460b725ae77Skettenis 
461b725ae77Skettenis   if (udp_in_use)
462b725ae77Skettenis     {
463b725ae77Skettenis       serial_close (udp_desc);
464b725ae77Skettenis       udp_in_use = 0;
465b725ae77Skettenis     }
466b725ae77Skettenis   tftp_in_use = 0;
467b725ae77Skettenis }
468b725ae77Skettenis 
469e93f7393Sniklas /* Handle low-level error that we can't recover from.  Note that just
470e93f7393Sniklas    error()ing out from target_wait or some such low-level place will cause
471e93f7393Sniklas    all hell to break loose--the rest of GDB will tend to get left in an
472e93f7393Sniklas    inconsistent state.  */
473e93f7393Sniklas 
474e93f7393Sniklas static NORETURN void
mips_error(char * string,...)475e93f7393Sniklas mips_error (char *string,...)
476e93f7393Sniklas {
477e93f7393Sniklas   va_list args;
478e93f7393Sniklas 
479e93f7393Sniklas   va_start (args, string);
480e93f7393Sniklas 
481e93f7393Sniklas   target_terminal_ours ();
482e93f7393Sniklas   wrap_here ("");		/* Force out any buffered output */
483e93f7393Sniklas   gdb_flush (gdb_stdout);
484e93f7393Sniklas   if (error_pre_print)
485b725ae77Skettenis     fputs_filtered (error_pre_print, gdb_stderr);
486e93f7393Sniklas   vfprintf_filtered (gdb_stderr, string, args);
487e93f7393Sniklas   fprintf_filtered (gdb_stderr, "\n");
488e93f7393Sniklas   va_end (args);
489e93f7393Sniklas   gdb_flush (gdb_stderr);
490e93f7393Sniklas 
491e93f7393Sniklas   /* Clean up in such a way that mips_close won't try to talk to the
492e93f7393Sniklas      board (it almost surely won't work since we weren't able to talk to
493e93f7393Sniklas      it).  */
494b725ae77Skettenis   close_ports ();
495e93f7393Sniklas 
496e93f7393Sniklas   printf_unfiltered ("Ending remote MIPS debugging.\n");
497e93f7393Sniklas   target_mourn_inferior ();
498e93f7393Sniklas 
499b725ae77Skettenis   throw_exception (RETURN_ERROR);
500e93f7393Sniklas }
501e93f7393Sniklas 
502b725ae77Skettenis /* putc_readable - print a character, displaying non-printable chars in
503b725ae77Skettenis    ^x notation or in hex.  */
504e93f7393Sniklas 
505b725ae77Skettenis static void
fputc_readable(int ch,struct ui_file * file)506b725ae77Skettenis fputc_readable (int ch, struct ui_file *file)
507e93f7393Sniklas {
508b725ae77Skettenis   if (ch == '\n')
509b725ae77Skettenis     fputc_unfiltered ('\n', file);
510b725ae77Skettenis   else if (ch == '\r')
511b725ae77Skettenis     fprintf_unfiltered (file, "\\r");
512b725ae77Skettenis   else if (ch < 0x20)		/* ASCII control character */
513b725ae77Skettenis     fprintf_unfiltered (file, "^%c", ch + '@');
514b725ae77Skettenis   else if (ch >= 0x7f)		/* non-ASCII characters (rubout or greater) */
515b725ae77Skettenis     fprintf_unfiltered (file, "[%02x]", ch & 0xff);
516b725ae77Skettenis   else
517b725ae77Skettenis     fputc_unfiltered (ch, file);
518b725ae77Skettenis }
519e93f7393Sniklas 
520b725ae77Skettenis 
521b725ae77Skettenis /* puts_readable - print a string, displaying non-printable chars in
522b725ae77Skettenis    ^x notation or in hex.  */
523b725ae77Skettenis 
524b725ae77Skettenis static void
fputs_readable(const char * string,struct ui_file * file)525b725ae77Skettenis fputs_readable (const char *string, struct ui_file *file)
526b725ae77Skettenis {
527b725ae77Skettenis   int c;
528b725ae77Skettenis 
529b725ae77Skettenis   while ((c = *string++) != '\0')
530b725ae77Skettenis     fputc_readable (c, file);
531b725ae77Skettenis }
532b725ae77Skettenis 
533b725ae77Skettenis 
534b725ae77Skettenis /* Wait until STRING shows up in mips_desc.  Returns 1 if successful, else 0 if
535b725ae77Skettenis    timed out.  TIMEOUT specifies timeout value in seconds.
536b725ae77Skettenis  */
537b725ae77Skettenis 
538b725ae77Skettenis static int
mips_expect_timeout(const char * string,int timeout)539b725ae77Skettenis mips_expect_timeout (const char *string, int timeout)
540b725ae77Skettenis {
541b725ae77Skettenis   const char *p = string;
542b725ae77Skettenis 
543b725ae77Skettenis   if (remote_debug)
544b725ae77Skettenis     {
545b725ae77Skettenis       fprintf_unfiltered (gdb_stdlog, "Expected \"");
546b725ae77Skettenis       fputs_readable (string, gdb_stdlog);
547b725ae77Skettenis       fprintf_unfiltered (gdb_stdlog, "\", got \"");
548b725ae77Skettenis     }
549b725ae77Skettenis 
550b725ae77Skettenis   immediate_quit++;
551e93f7393Sniklas   while (1)
552e93f7393Sniklas     {
553e93f7393Sniklas       int c;
554e93f7393Sniklas 
555b725ae77Skettenis       /* Must use serial_readchar() here cuz mips_readchar would get
556b725ae77Skettenis 	 confused if we were waiting for the mips_monitor_prompt... */
557e93f7393Sniklas 
558b725ae77Skettenis       c = serial_readchar (mips_desc, timeout);
559e93f7393Sniklas 
560e93f7393Sniklas       if (c == SERIAL_TIMEOUT)
561b725ae77Skettenis 	{
562b725ae77Skettenis 	  if (remote_debug)
563b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog, "\": FAIL\n");
564e93f7393Sniklas 	  return 0;
565b725ae77Skettenis 	}
566b725ae77Skettenis 
567b725ae77Skettenis       if (remote_debug)
568b725ae77Skettenis 	fputc_readable (c, gdb_stdlog);
569e93f7393Sniklas 
570e93f7393Sniklas       if (c == *p++)
571e93f7393Sniklas 	{
572e93f7393Sniklas 	  if (*p == '\0')
573e93f7393Sniklas 	    {
574b725ae77Skettenis 	      immediate_quit--;
575b725ae77Skettenis 	      if (remote_debug)
576b725ae77Skettenis 		fprintf_unfiltered (gdb_stdlog, "\": OK\n");
577e93f7393Sniklas 	      return 1;
578e93f7393Sniklas 	    }
579e93f7393Sniklas 	}
580e93f7393Sniklas       else
581e93f7393Sniklas 	{
582e93f7393Sniklas 	  p = string;
583e93f7393Sniklas 	  if (c == *p)
584e93f7393Sniklas 	    p++;
585e93f7393Sniklas 	}
586e93f7393Sniklas     }
587e93f7393Sniklas }
588e93f7393Sniklas 
589b725ae77Skettenis /* Wait until STRING shows up in mips_desc.  Returns 1 if successful, else 0 if
590b725ae77Skettenis    timed out.  The timeout value is hard-coded to 2 seconds.  Use
591b725ae77Skettenis    mips_expect_timeout if a different timeout value is needed.
592b725ae77Skettenis  */
593b725ae77Skettenis 
594b725ae77Skettenis static int
mips_expect(const char * string)595b725ae77Skettenis mips_expect (const char *string)
596e93f7393Sniklas {
597b725ae77Skettenis   return mips_expect_timeout (string, remote_timeout);
598e93f7393Sniklas }
599e93f7393Sniklas 
600e93f7393Sniklas /* Read a character from the remote, aborting on error.  Returns
601b725ae77Skettenis    SERIAL_TIMEOUT on timeout (since that's what serial_readchar()
602b725ae77Skettenis    returns).  FIXME: If we see the string mips_monitor_prompt from the
603b725ae77Skettenis    board, then we are debugging on the main console port, and we have
604b725ae77Skettenis    somehow dropped out of remote debugging mode.  In this case, we
605b725ae77Skettenis    automatically go back in to remote debugging mode.  This is a hack,
606b725ae77Skettenis    put in because I can't find any way for a program running on the
607b725ae77Skettenis    remote board to terminate without also ending remote debugging
608e93f7393Sniklas    mode.  I assume users won't have any trouble with this; for one
609e93f7393Sniklas    thing, the IDT documentation generally assumes that the remote
610e93f7393Sniklas    debugging port is not the console port.  This is, however, very
611e93f7393Sniklas    convenient for DejaGnu when you only have one connected serial
612e93f7393Sniklas    port.  */
613e93f7393Sniklas 
614e93f7393Sniklas static int
mips_readchar(int timeout)615b725ae77Skettenis mips_readchar (int timeout)
616e93f7393Sniklas {
617e93f7393Sniklas   int ch;
618e93f7393Sniklas   static int state = 0;
619b725ae77Skettenis   int mips_monitor_prompt_len = strlen (mips_monitor_prompt);
620e93f7393Sniklas 
621e93f7393Sniklas   {
622e93f7393Sniklas     int i;
623e93f7393Sniklas 
624e93f7393Sniklas     i = timeout;
625e93f7393Sniklas     if (i == -1 && watchdog > 0)
626e93f7393Sniklas       i = watchdog;
627e93f7393Sniklas   }
628e93f7393Sniklas 
629e93f7393Sniklas   if (state == mips_monitor_prompt_len)
630e93f7393Sniklas     timeout = 1;
631b725ae77Skettenis   ch = serial_readchar (mips_desc, timeout);
632b725ae77Skettenis 
633e93f7393Sniklas   if (ch == SERIAL_TIMEOUT && timeout == -1)	/* Watchdog went off */
634e93f7393Sniklas     {
635e93f7393Sniklas       target_mourn_inferior ();
636e93f7393Sniklas       error ("Watchdog has expired.  Target detached.\n");
637e93f7393Sniklas     }
638b725ae77Skettenis 
639e93f7393Sniklas   if (ch == SERIAL_EOF)
640e93f7393Sniklas     mips_error ("End of file from remote");
641e93f7393Sniklas   if (ch == SERIAL_ERROR)
642e93f7393Sniklas     mips_error ("Error reading from remote: %s", safe_strerror (errno));
643e93f7393Sniklas   if (remote_debug > 1)
644e93f7393Sniklas     {
645e93f7393Sniklas       /* Don't use _filtered; we can't deal with a QUIT out of
646e93f7393Sniklas          target_wait, and I think this might be called from there.  */
647e93f7393Sniklas       if (ch != SERIAL_TIMEOUT)
648b725ae77Skettenis 	fprintf_unfiltered (gdb_stdlog, "Read '%c' %d 0x%x\n", ch, ch, ch);
649e93f7393Sniklas       else
650b725ae77Skettenis 	fprintf_unfiltered (gdb_stdlog, "Timed out in read\n");
651e93f7393Sniklas     }
652e93f7393Sniklas 
653e93f7393Sniklas   /* If we have seen mips_monitor_prompt and we either time out, or
654e93f7393Sniklas      we see a @ (which was echoed from a packet we sent), reset the
655e93f7393Sniklas      board as described above.  The first character in a packet after
656e93f7393Sniklas      the SYN (which is not echoed) is always an @ unless the packet is
657e93f7393Sniklas      more than 64 characters long, which ours never are.  */
658e93f7393Sniklas   if ((ch == SERIAL_TIMEOUT || ch == '@')
659e93f7393Sniklas       && state == mips_monitor_prompt_len
660b725ae77Skettenis       && !mips_initializing
661b725ae77Skettenis       && !mips_exiting)
662e93f7393Sniklas     {
663e93f7393Sniklas       if (remote_debug > 0)
664e93f7393Sniklas 	/* Don't use _filtered; we can't deal with a QUIT out of
665e93f7393Sniklas 	   target_wait, and I think this might be called from there.  */
666b725ae77Skettenis 	fprintf_unfiltered (gdb_stdlog, "Reinitializing MIPS debugging mode\n");
667e93f7393Sniklas 
668e93f7393Sniklas       mips_need_reply = 0;
669e93f7393Sniklas       mips_initialize ();
670e93f7393Sniklas 
671e93f7393Sniklas       state = 0;
672e93f7393Sniklas 
673e93f7393Sniklas       /* At this point, about the only thing we can do is abort the command
674e93f7393Sniklas          in progress and get back to command level as quickly as possible. */
675e93f7393Sniklas 
676e93f7393Sniklas       error ("Remote board reset, debug protocol re-initialized.");
677e93f7393Sniklas     }
678e93f7393Sniklas 
679e93f7393Sniklas   if (ch == mips_monitor_prompt[state])
680e93f7393Sniklas     ++state;
681e93f7393Sniklas   else
682e93f7393Sniklas     state = 0;
683e93f7393Sniklas 
684e93f7393Sniklas   return ch;
685e93f7393Sniklas }
686e93f7393Sniklas 
687e93f7393Sniklas /* Get a packet header, putting the data in the supplied buffer.
688e93f7393Sniklas    PGARBAGE is a pointer to the number of garbage characters received
689e93f7393Sniklas    so far.  CH is the last character received.  Returns 0 for success,
690e93f7393Sniklas    or -1 for timeout.  */
691e93f7393Sniklas 
692e93f7393Sniklas static int
mips_receive_header(unsigned char * hdr,int * pgarbage,int ch,int timeout)693b725ae77Skettenis mips_receive_header (unsigned char *hdr, int *pgarbage, int ch, int timeout)
694e93f7393Sniklas {
695e93f7393Sniklas   int i;
696e93f7393Sniklas 
697e93f7393Sniklas   while (1)
698e93f7393Sniklas     {
699e93f7393Sniklas       /* Wait for a SYN.  mips_syn_garbage is intended to prevent
700e93f7393Sniklas          sitting here indefinitely if the board sends us one garbage
701e93f7393Sniklas          character per second.  ch may already have a value from the
702e93f7393Sniklas          last time through the loop.  */
703e93f7393Sniklas       while (ch != SYN)
704e93f7393Sniklas 	{
705e93f7393Sniklas 	  ch = mips_readchar (timeout);
706e93f7393Sniklas 	  if (ch == SERIAL_TIMEOUT)
707e93f7393Sniklas 	    return -1;
708e93f7393Sniklas 	  if (ch != SYN)
709e93f7393Sniklas 	    {
710e93f7393Sniklas 	      /* Printing the character here lets the user of gdb see
711e93f7393Sniklas 	         what the program is outputting, if the debugging is
712b725ae77Skettenis 	         being done on the console port.  Don't use _filtered:
713b725ae77Skettenis 	         we can't deal with a QUIT out of target_wait and
714b725ae77Skettenis 	         buffered target output confuses the user. */
715e93f7393Sniklas  	      if (!mips_initializing || remote_debug > 0)
716e93f7393Sniklas   		{
717b725ae77Skettenis 		  if (isprint (ch) || isspace (ch))
718e93f7393Sniklas 		    {
719b725ae77Skettenis 		      fputc_unfiltered (ch, gdb_stdtarg);
720e93f7393Sniklas 		    }
721e93f7393Sniklas 		  else
722b725ae77Skettenis 		    {
723b725ae77Skettenis 		      fputc_readable (ch, gdb_stdtarg);
724b725ae77Skettenis 		    }
725b725ae77Skettenis 		  gdb_flush (gdb_stdtarg);
726e93f7393Sniklas   		}
727e93f7393Sniklas 
728b725ae77Skettenis 	      /* Only count unprintable characters. */
729b725ae77Skettenis 	      if (! (isprint (ch) || isspace (ch)))
730b725ae77Skettenis 		(*pgarbage) += 1;
731b725ae77Skettenis 
732e93f7393Sniklas 	      if (mips_syn_garbage > 0
733e93f7393Sniklas 		  && *pgarbage > mips_syn_garbage)
734e93f7393Sniklas 		mips_error ("Debug protocol failure:  more than %d characters before a sync.",
735e93f7393Sniklas 			    mips_syn_garbage);
736e93f7393Sniklas 	    }
737e93f7393Sniklas 	}
738e93f7393Sniklas 
739e93f7393Sniklas       /* Get the packet header following the SYN.  */
740e93f7393Sniklas       for (i = 1; i < HDR_LENGTH; i++)
741e93f7393Sniklas 	{
742e93f7393Sniklas 	  ch = mips_readchar (timeout);
743e93f7393Sniklas 	  if (ch == SERIAL_TIMEOUT)
744e93f7393Sniklas 	    return -1;
745e93f7393Sniklas 	  /* Make sure this is a header byte.  */
746e93f7393Sniklas 	  if (ch == SYN || !HDR_CHECK (ch))
747e93f7393Sniklas 	    break;
748e93f7393Sniklas 
749e93f7393Sniklas 	  hdr[i] = ch;
750e93f7393Sniklas 	}
751e93f7393Sniklas 
752e93f7393Sniklas       /* If we got the complete header, we can return.  Otherwise we
753e93f7393Sniklas          loop around and keep looking for SYN.  */
754e93f7393Sniklas       if (i >= HDR_LENGTH)
755e93f7393Sniklas 	return 0;
756e93f7393Sniklas     }
757e93f7393Sniklas }
758e93f7393Sniklas 
759e93f7393Sniklas /* Get a packet header, putting the data in the supplied buffer.
760e93f7393Sniklas    PGARBAGE is a pointer to the number of garbage characters received
761e93f7393Sniklas    so far.  The last character read is returned in *PCH.  Returns 0
762e93f7393Sniklas    for success, -1 for timeout, -2 for error.  */
763e93f7393Sniklas 
764e93f7393Sniklas static int
mips_receive_trailer(unsigned char * trlr,int * pgarbage,int * pch,int timeout)765b725ae77Skettenis mips_receive_trailer (unsigned char *trlr, int *pgarbage, int *pch, int timeout)
766e93f7393Sniklas {
767e93f7393Sniklas   int i;
768e93f7393Sniklas   int ch;
769e93f7393Sniklas 
770e93f7393Sniklas   for (i = 0; i < TRLR_LENGTH; i++)
771e93f7393Sniklas     {
772e93f7393Sniklas       ch = mips_readchar (timeout);
773e93f7393Sniklas       *pch = ch;
774e93f7393Sniklas       if (ch == SERIAL_TIMEOUT)
775e93f7393Sniklas 	return -1;
776e93f7393Sniklas       if (!TRLR_CHECK (ch))
777e93f7393Sniklas 	return -2;
778e93f7393Sniklas       trlr[i] = ch;
779e93f7393Sniklas     }
780e93f7393Sniklas   return 0;
781e93f7393Sniklas }
782e93f7393Sniklas 
783e93f7393Sniklas /* Get the checksum of a packet.  HDR points to the packet header.
784e93f7393Sniklas    DATA points to the packet data.  LEN is the length of DATA.  */
785e93f7393Sniklas 
786e93f7393Sniklas static int
mips_cksum(const unsigned char * hdr,const unsigned char * data,int len)787b725ae77Skettenis mips_cksum (const unsigned char *hdr, const unsigned char *data, int len)
788e93f7393Sniklas {
789b725ae77Skettenis   const unsigned char *p;
790b725ae77Skettenis   int c;
791b725ae77Skettenis   int cksum;
792e93f7393Sniklas 
793e93f7393Sniklas   cksum = 0;
794e93f7393Sniklas 
795e93f7393Sniklas   /* The initial SYN is not included in the checksum.  */
796e93f7393Sniklas   c = HDR_LENGTH - 1;
797e93f7393Sniklas   p = hdr + 1;
798e93f7393Sniklas   while (c-- != 0)
799e93f7393Sniklas     cksum += *p++;
800e93f7393Sniklas 
801e93f7393Sniklas   c = len;
802e93f7393Sniklas   p = data;
803e93f7393Sniklas   while (c-- != 0)
804e93f7393Sniklas     cksum += *p++;
805e93f7393Sniklas 
806e93f7393Sniklas   return cksum;
807e93f7393Sniklas }
808e93f7393Sniklas 
809e93f7393Sniklas /* Send a packet containing the given ASCII string.  */
810e93f7393Sniklas 
811e93f7393Sniklas static void
mips_send_packet(const char * s,int get_ack)812b725ae77Skettenis mips_send_packet (const char *s, int get_ack)
813e93f7393Sniklas {
814b725ae77Skettenis   /* unsigned */ int len;
815e93f7393Sniklas   unsigned char *packet;
816b725ae77Skettenis   int cksum;
817e93f7393Sniklas   int try;
818e93f7393Sniklas 
819e93f7393Sniklas   len = strlen (s);
820e93f7393Sniklas   if (len > DATA_MAXLEN)
821e93f7393Sniklas     mips_error ("MIPS protocol data packet too long: %s", s);
822e93f7393Sniklas 
823e93f7393Sniklas   packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
824e93f7393Sniklas 
825e93f7393Sniklas   packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
826e93f7393Sniklas   packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
827e93f7393Sniklas   packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
828e93f7393Sniklas   packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
829e93f7393Sniklas 
830e93f7393Sniklas   memcpy (packet + HDR_LENGTH, s, len);
831e93f7393Sniklas 
832e93f7393Sniklas   cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
833e93f7393Sniklas   packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
834e93f7393Sniklas   packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
835e93f7393Sniklas   packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
836e93f7393Sniklas 
837e93f7393Sniklas   /* Increment the sequence number.  This will set mips_send_seq to
838e93f7393Sniklas      the sequence number we expect in the acknowledgement.  */
839e93f7393Sniklas   mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
840e93f7393Sniklas 
841e93f7393Sniklas   /* We can only have one outstanding data packet, so we just wait for
842e93f7393Sniklas      the acknowledgement here.  Keep retransmitting the packet until
843e93f7393Sniklas      we get one, or until we've tried too many times.  */
844e93f7393Sniklas   for (try = 0; try < mips_send_retries; try++)
845e93f7393Sniklas     {
846e93f7393Sniklas       int garbage;
847e93f7393Sniklas       int ch;
848e93f7393Sniklas 
849e93f7393Sniklas       if (remote_debug > 0)
850e93f7393Sniklas 	{
851e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
852e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
853e93f7393Sniklas 	  packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
854b725ae77Skettenis 	  fprintf_unfiltered (gdb_stdlog, "Writing \"%s\"\n", packet + 1);
855e93f7393Sniklas 	}
856e93f7393Sniklas 
857b725ae77Skettenis       if (serial_write (mips_desc, packet,
858e93f7393Sniklas 			HDR_LENGTH + len + TRLR_LENGTH) != 0)
859e93f7393Sniklas 	mips_error ("write to target failed: %s", safe_strerror (errno));
860e93f7393Sniklas 
861e93f7393Sniklas       if (!get_ack)
862e93f7393Sniklas 	return;
863e93f7393Sniklas 
864e93f7393Sniklas       garbage = 0;
865e93f7393Sniklas       ch = 0;
866e93f7393Sniklas       while (1)
867e93f7393Sniklas 	{
868e93f7393Sniklas 	  unsigned char hdr[HDR_LENGTH + 1];
869e93f7393Sniklas 	  unsigned char trlr[TRLR_LENGTH + 1];
870e93f7393Sniklas 	  int err;
871b725ae77Skettenis 	  unsigned int seq;
872e93f7393Sniklas 
873e93f7393Sniklas 	  /* Get the packet header.  If we time out, resend the data
874e93f7393Sniklas 	     packet.  */
875e93f7393Sniklas 	  err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
876e93f7393Sniklas 	  if (err != 0)
877e93f7393Sniklas 	    break;
878e93f7393Sniklas 
879e93f7393Sniklas 	  ch = 0;
880e93f7393Sniklas 
881e93f7393Sniklas 	  /* If we get a data packet, assume it is a duplicate and
882e93f7393Sniklas 	     ignore it.  FIXME: If the acknowledgement is lost, this
883e93f7393Sniklas 	     data packet may be the packet the remote sends after the
884e93f7393Sniklas 	     acknowledgement.  */
885b725ae77Skettenis 	  if (HDR_IS_DATA (hdr))
886b725ae77Skettenis 	    {
887e93f7393Sniklas 	      int i;
888e93f7393Sniklas 
889e93f7393Sniklas 	      /* Ignore any errors raised whilst attempting to ignore
890e93f7393Sniklas 	         packet. */
891e93f7393Sniklas 
892e93f7393Sniklas 	      len = HDR_GET_LEN (hdr);
893e93f7393Sniklas 
894e93f7393Sniklas 	      for (i = 0; i < len; i++)
895e93f7393Sniklas 		{
896e93f7393Sniklas 		  int rch;
897e93f7393Sniklas 
898b725ae77Skettenis 		  rch = mips_readchar (remote_timeout);
899e93f7393Sniklas 		  if (rch == SYN)
900e93f7393Sniklas 		    {
901e93f7393Sniklas 		      ch = SYN;
902e93f7393Sniklas 		      break;
903e93f7393Sniklas 		    }
904e93f7393Sniklas 		  if (rch == SERIAL_TIMEOUT)
905e93f7393Sniklas 		    break;
906e93f7393Sniklas 		  /* ignore the character */
907e93f7393Sniklas 		}
908e93f7393Sniklas 
909e93f7393Sniklas 	      if (i == len)
910b725ae77Skettenis 		(void) mips_receive_trailer (trlr, &garbage, &ch,
911b725ae77Skettenis 					     remote_timeout);
912e93f7393Sniklas 
913e93f7393Sniklas 	      /* We don't bother checking the checksum, or providing an
914e93f7393Sniklas 	         ACK to the packet. */
915e93f7393Sniklas 	      continue;
916e93f7393Sniklas 	    }
917e93f7393Sniklas 
918e93f7393Sniklas 	  /* If the length is not 0, this is a garbled packet.  */
919e93f7393Sniklas 	  if (HDR_GET_LEN (hdr) != 0)
920e93f7393Sniklas 	    continue;
921e93f7393Sniklas 
922e93f7393Sniklas 	  /* Get the packet trailer.  */
923e93f7393Sniklas 	  err = mips_receive_trailer (trlr, &garbage, &ch,
924e93f7393Sniklas 				      mips_retransmit_wait);
925e93f7393Sniklas 
926e93f7393Sniklas 	  /* If we timed out, resend the data packet.  */
927e93f7393Sniklas 	  if (err == -1)
928e93f7393Sniklas 	    break;
929e93f7393Sniklas 
930e93f7393Sniklas 	  /* If we got a bad character, reread the header.  */
931e93f7393Sniklas 	  if (err != 0)
932e93f7393Sniklas 	    continue;
933e93f7393Sniklas 
934e93f7393Sniklas 	  /* If the checksum does not match the trailer checksum, this
935e93f7393Sniklas 	     is a bad packet; ignore it.  */
936e93f7393Sniklas 	  if (mips_cksum (hdr, (unsigned char *) NULL, 0)
937e93f7393Sniklas 	      != TRLR_GET_CKSUM (trlr))
938e93f7393Sniklas 	    continue;
939e93f7393Sniklas 
940e93f7393Sniklas 	  if (remote_debug > 0)
941e93f7393Sniklas 	    {
942e93f7393Sniklas 	      hdr[HDR_LENGTH] = '\0';
943e93f7393Sniklas 	      trlr[TRLR_LENGTH] = '\0';
944e93f7393Sniklas 	      /* Don't use _filtered; we can't deal with a QUIT out of
945e93f7393Sniklas 	         target_wait, and I think this might be called from there.  */
946b725ae77Skettenis 	      fprintf_unfiltered (gdb_stdlog, "Got ack %d \"%s%s\"\n",
947e93f7393Sniklas 				  HDR_GET_SEQ (hdr), hdr + 1, trlr);
948e93f7393Sniklas 	    }
949e93f7393Sniklas 
950e93f7393Sniklas 	  /* If this ack is for the current packet, we're done.  */
951e93f7393Sniklas 	  seq = HDR_GET_SEQ (hdr);
952e93f7393Sniklas 	  if (seq == mips_send_seq)
953e93f7393Sniklas 	    return;
954e93f7393Sniklas 
955e93f7393Sniklas 	  /* If this ack is for the last packet, resend the current
956e93f7393Sniklas 	     packet.  */
957e93f7393Sniklas 	  if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
958e93f7393Sniklas 	    break;
959e93f7393Sniklas 
960e93f7393Sniklas 	  /* Otherwise this is a bad ack; ignore it.  Increment the
961e93f7393Sniklas 	     garbage count to ensure that we do not stay in this loop
962e93f7393Sniklas 	     forever.  */
963e93f7393Sniklas 	  ++garbage;
964e93f7393Sniklas 	}
965e93f7393Sniklas     }
966e93f7393Sniklas 
967e93f7393Sniklas   mips_error ("Remote did not acknowledge packet");
968e93f7393Sniklas }
969e93f7393Sniklas 
970e93f7393Sniklas /* Receive and acknowledge a packet, returning the data in BUFF (which
971e93f7393Sniklas    should be DATA_MAXLEN + 1 bytes).  The protocol documentation
972e93f7393Sniklas    implies that only the sender retransmits packets, so this code just
973e93f7393Sniklas    waits silently for a packet.  It returns the length of the received
974e93f7393Sniklas    packet.  If THROW_ERROR is nonzero, call error() on errors.  If not,
975e93f7393Sniklas    don't print an error message and return -1.  */
976e93f7393Sniklas 
977e93f7393Sniklas static int
mips_receive_packet(char * buff,int throw_error,int timeout)978b725ae77Skettenis mips_receive_packet (char *buff, int throw_error, int timeout)
979e93f7393Sniklas {
980e93f7393Sniklas   int ch;
981e93f7393Sniklas   int garbage;
982e93f7393Sniklas   int len;
983e93f7393Sniklas   unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
984e93f7393Sniklas   int cksum;
985e93f7393Sniklas 
986e93f7393Sniklas   ch = 0;
987e93f7393Sniklas   garbage = 0;
988e93f7393Sniklas   while (1)
989e93f7393Sniklas     {
990e93f7393Sniklas       unsigned char hdr[HDR_LENGTH];
991e93f7393Sniklas       unsigned char trlr[TRLR_LENGTH];
992e93f7393Sniklas       int i;
993e93f7393Sniklas       int err;
994e93f7393Sniklas 
995e93f7393Sniklas       if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
996e93f7393Sniklas 	{
997e93f7393Sniklas 	  if (throw_error)
998e93f7393Sniklas 	    mips_error ("Timed out waiting for remote packet");
999e93f7393Sniklas 	  else
1000e93f7393Sniklas 	    return -1;
1001e93f7393Sniklas 	}
1002e93f7393Sniklas 
1003e93f7393Sniklas       ch = 0;
1004e93f7393Sniklas 
1005e93f7393Sniklas       /* An acknowledgement is probably a duplicate; ignore it.  */
1006e93f7393Sniklas       if (!HDR_IS_DATA (hdr))
1007e93f7393Sniklas 	{
1008e93f7393Sniklas 	  len = HDR_GET_LEN (hdr);
1009e93f7393Sniklas 	  /* Check if the length is valid for an ACK, we may aswell
1010e93f7393Sniklas 	     try and read the remainder of the packet: */
1011e93f7393Sniklas 	  if (len == 0)
1012e93f7393Sniklas 	    {
1013e93f7393Sniklas 	      /* Ignore the error condition, since we are going to
1014e93f7393Sniklas 	         ignore the packet anyway. */
1015e93f7393Sniklas 	      (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
1016e93f7393Sniklas 	    }
1017e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
1018e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
1019e93f7393Sniklas 	  if (remote_debug > 0)
1020b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog, "Ignoring unexpected ACK\n");
1021e93f7393Sniklas 	  continue;
1022e93f7393Sniklas 	}
1023e93f7393Sniklas 
1024e93f7393Sniklas       len = HDR_GET_LEN (hdr);
1025e93f7393Sniklas       for (i = 0; i < len; i++)
1026e93f7393Sniklas 	{
1027e93f7393Sniklas 	  int rch;
1028e93f7393Sniklas 
1029e93f7393Sniklas 	  rch = mips_readchar (timeout);
1030e93f7393Sniklas 	  if (rch == SYN)
1031e93f7393Sniklas 	    {
1032e93f7393Sniklas 	      ch = SYN;
1033e93f7393Sniklas 	      break;
1034e93f7393Sniklas 	    }
1035e93f7393Sniklas 	  if (rch == SERIAL_TIMEOUT)
1036e93f7393Sniklas 	    {
1037e93f7393Sniklas 	      if (throw_error)
1038e93f7393Sniklas 		mips_error ("Timed out waiting for remote packet");
1039e93f7393Sniklas 	      else
1040e93f7393Sniklas 		return -1;
1041e93f7393Sniklas 	    }
1042e93f7393Sniklas 	  buff[i] = rch;
1043e93f7393Sniklas 	}
1044e93f7393Sniklas 
1045e93f7393Sniklas       if (i < len)
1046e93f7393Sniklas 	{
1047e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
1048e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
1049e93f7393Sniklas 	  if (remote_debug > 0)
1050b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog,
1051b725ae77Skettenis 				"Got new SYN after %d chars (wanted %d)\n",
1052e93f7393Sniklas 				i, len);
1053e93f7393Sniklas 	  continue;
1054e93f7393Sniklas 	}
1055e93f7393Sniklas 
1056e93f7393Sniklas       err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
1057e93f7393Sniklas       if (err == -1)
1058e93f7393Sniklas 	{
1059e93f7393Sniklas 	  if (throw_error)
1060e93f7393Sniklas 	    mips_error ("Timed out waiting for packet");
1061e93f7393Sniklas 	  else
1062e93f7393Sniklas 	    return -1;
1063e93f7393Sniklas 	}
1064e93f7393Sniklas       if (err == -2)
1065e93f7393Sniklas 	{
1066e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
1067e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
1068e93f7393Sniklas 	  if (remote_debug > 0)
1069b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog, "Got SYN when wanted trailer\n");
1070e93f7393Sniklas 	  continue;
1071e93f7393Sniklas 	}
1072e93f7393Sniklas 
1073e93f7393Sniklas       /* If this is the wrong sequence number, ignore it.  */
1074e93f7393Sniklas       if (HDR_GET_SEQ (hdr) != mips_receive_seq)
1075e93f7393Sniklas 	{
1076e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
1077e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
1078e93f7393Sniklas 	  if (remote_debug > 0)
1079b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog,
1080b725ae77Skettenis 				"Ignoring sequence number %d (want %d)\n",
1081e93f7393Sniklas 				HDR_GET_SEQ (hdr), mips_receive_seq);
1082e93f7393Sniklas 	  continue;
1083e93f7393Sniklas 	}
1084e93f7393Sniklas 
1085e93f7393Sniklas       if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
1086e93f7393Sniklas 	break;
1087e93f7393Sniklas 
1088e93f7393Sniklas       if (remote_debug > 0)
1089e93f7393Sniklas 	/* Don't use _filtered; we can't deal with a QUIT out of
1090e93f7393Sniklas 	   target_wait, and I think this might be called from there.  */
1091e93f7393Sniklas 	printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
1092e93f7393Sniklas 			   mips_cksum (hdr, buff, len),
1093e93f7393Sniklas 			   TRLR_GET_CKSUM (trlr));
1094e93f7393Sniklas 
1095e93f7393Sniklas       /* The checksum failed.  Send an acknowledgement for the
1096e93f7393Sniklas          previous packet to tell the remote to resend the packet.  */
1097e93f7393Sniklas       ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1098e93f7393Sniklas       ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1099e93f7393Sniklas       ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1100e93f7393Sniklas       ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1101e93f7393Sniklas 
1102e93f7393Sniklas       cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1103e93f7393Sniklas 
1104e93f7393Sniklas       ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1105e93f7393Sniklas       ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1106e93f7393Sniklas       ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1107e93f7393Sniklas 
1108e93f7393Sniklas       if (remote_debug > 0)
1109e93f7393Sniklas 	{
1110e93f7393Sniklas 	  ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1111e93f7393Sniklas 	  /* Don't use _filtered; we can't deal with a QUIT out of
1112e93f7393Sniklas 	     target_wait, and I think this might be called from there.  */
1113e93f7393Sniklas 	  printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1114e93f7393Sniklas 			     ack + 1);
1115e93f7393Sniklas 	}
1116e93f7393Sniklas 
1117b725ae77Skettenis       if (serial_write (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1118e93f7393Sniklas 	{
1119e93f7393Sniklas 	  if (throw_error)
1120e93f7393Sniklas 	    mips_error ("write to target failed: %s", safe_strerror (errno));
1121e93f7393Sniklas 	  else
1122e93f7393Sniklas 	    return -1;
1123e93f7393Sniklas 	}
1124e93f7393Sniklas     }
1125e93f7393Sniklas 
1126e93f7393Sniklas   if (remote_debug > 0)
1127e93f7393Sniklas     {
1128e93f7393Sniklas       buff[len] = '\0';
1129e93f7393Sniklas       /* Don't use _filtered; we can't deal with a QUIT out of
1130e93f7393Sniklas          target_wait, and I think this might be called from there.  */
1131e93f7393Sniklas       printf_unfiltered ("Got packet \"%s\"\n", buff);
1132e93f7393Sniklas     }
1133e93f7393Sniklas 
1134e93f7393Sniklas   /* We got the packet.  Send an acknowledgement.  */
1135e93f7393Sniklas   mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1136e93f7393Sniklas 
1137e93f7393Sniklas   ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1138e93f7393Sniklas   ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1139e93f7393Sniklas   ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1140e93f7393Sniklas   ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1141e93f7393Sniklas 
1142e93f7393Sniklas   cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1143e93f7393Sniklas 
1144e93f7393Sniklas   ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1145e93f7393Sniklas   ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1146e93f7393Sniklas   ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1147e93f7393Sniklas 
1148e93f7393Sniklas   if (remote_debug > 0)
1149e93f7393Sniklas     {
1150e93f7393Sniklas       ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1151e93f7393Sniklas       /* Don't use _filtered; we can't deal with a QUIT out of
1152e93f7393Sniklas          target_wait, and I think this might be called from there.  */
1153e93f7393Sniklas       printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1154e93f7393Sniklas 			 ack + 1);
1155e93f7393Sniklas     }
1156e93f7393Sniklas 
1157b725ae77Skettenis   if (serial_write (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1158e93f7393Sniklas     {
1159e93f7393Sniklas       if (throw_error)
1160e93f7393Sniklas 	mips_error ("write to target failed: %s", safe_strerror (errno));
1161e93f7393Sniklas       else
1162e93f7393Sniklas 	return -1;
1163e93f7393Sniklas     }
1164e93f7393Sniklas 
1165e93f7393Sniklas   return len;
1166e93f7393Sniklas }
1167e93f7393Sniklas 
1168e93f7393Sniklas /* Optionally send a request to the remote system and optionally wait
1169e93f7393Sniklas    for the reply.  This implements the remote debugging protocol,
1170e93f7393Sniklas    which is built on top of the packet protocol defined above.  Each
1171e93f7393Sniklas    request has an ADDR argument and a DATA argument.  The following
1172e93f7393Sniklas    requests are defined:
1173e93f7393Sniklas 
1174e93f7393Sniklas    \0   don't send a request; just wait for a reply
1175e93f7393Sniklas    i    read word from instruction space at ADDR
1176e93f7393Sniklas    d    read word from data space at ADDR
1177e93f7393Sniklas    I    write DATA to instruction space at ADDR
1178e93f7393Sniklas    D    write DATA to data space at ADDR
1179e93f7393Sniklas    r    read register number ADDR
1180e93f7393Sniklas    R    set register number ADDR to value DATA
1181e93f7393Sniklas    c    continue execution (if ADDR != 1, set pc to ADDR)
1182e93f7393Sniklas    s    single step (if ADDR != 1, set pc to ADDR)
1183e93f7393Sniklas 
1184e93f7393Sniklas    The read requests return the value requested.  The write requests
1185e93f7393Sniklas    return the previous value in the changed location.  The execution
1186e93f7393Sniklas    requests return a UNIX wait value (the approximate signal which
1187e93f7393Sniklas    caused execution to stop is in the upper eight bits).
1188e93f7393Sniklas 
1189e93f7393Sniklas    If PERR is not NULL, this function waits for a reply.  If an error
1190e93f7393Sniklas    occurs, it sets *PERR to 1 and sets errno according to what the
1191e93f7393Sniklas    target board reports.  */
1192e93f7393Sniklas 
1193b725ae77Skettenis static ULONGEST
mips_request(int cmd,ULONGEST addr,ULONGEST data,int * perr,int timeout,char * buff)1194b725ae77Skettenis mips_request (int cmd,
1195b725ae77Skettenis 	      ULONGEST addr,
1196b725ae77Skettenis 	      ULONGEST data,
1197b725ae77Skettenis 	      int *perr,
1198b725ae77Skettenis 	      int timeout,
1199b725ae77Skettenis 	      char *buff)
1200e93f7393Sniklas {
1201e93f7393Sniklas   char myBuff[DATA_MAXLEN + 1];
1202e93f7393Sniklas   int len;
1203e93f7393Sniklas   int rpid;
1204e93f7393Sniklas   char rcmd;
1205e93f7393Sniklas   int rerrflg;
1206b725ae77Skettenis   unsigned long rresponse;
1207e93f7393Sniklas 
1208e93f7393Sniklas   if (buff == (char *) NULL)
1209e93f7393Sniklas     buff = myBuff;
1210e93f7393Sniklas 
1211e93f7393Sniklas   if (cmd != '\0')
1212e93f7393Sniklas     {
1213e93f7393Sniklas       if (mips_need_reply)
1214b725ae77Skettenis 	internal_error (__FILE__, __LINE__,
1215b725ae77Skettenis 			"mips_request: Trying to send command before reply");
1216b725ae77Skettenis       sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
1217e93f7393Sniklas       mips_send_packet (buff, 1);
1218e93f7393Sniklas       mips_need_reply = 1;
1219e93f7393Sniklas     }
1220e93f7393Sniklas 
1221e93f7393Sniklas   if (perr == (int *) NULL)
1222e93f7393Sniklas     return 0;
1223e93f7393Sniklas 
1224e93f7393Sniklas   if (!mips_need_reply)
1225b725ae77Skettenis     internal_error (__FILE__, __LINE__,
1226b725ae77Skettenis 		    "mips_request: Trying to get reply before command");
1227e93f7393Sniklas 
1228e93f7393Sniklas   mips_need_reply = 0;
1229e93f7393Sniklas 
1230e93f7393Sniklas   len = mips_receive_packet (buff, 1, timeout);
1231e93f7393Sniklas   buff[len] = '\0';
1232e93f7393Sniklas 
1233b725ae77Skettenis   if (sscanf (buff, "0x%x %c 0x%x 0x%lx",
1234e93f7393Sniklas 	      &rpid, &rcmd, &rerrflg, &rresponse) != 4
1235e93f7393Sniklas       || (cmd != '\0' && rcmd != cmd))
1236e93f7393Sniklas     mips_error ("Bad response from remote board");
1237e93f7393Sniklas 
1238e93f7393Sniklas   if (rerrflg != 0)
1239e93f7393Sniklas     {
1240e93f7393Sniklas       *perr = 1;
1241e93f7393Sniklas 
1242e93f7393Sniklas       /* FIXME: This will returns MIPS errno numbers, which may or may
1243e93f7393Sniklas          not be the same as errno values used on other systems.  If
1244e93f7393Sniklas          they stick to common errno values, they will be the same, but
1245e93f7393Sniklas          if they don't, they must be translated.  */
1246e93f7393Sniklas       errno = rresponse;
1247e93f7393Sniklas 
1248e93f7393Sniklas       return 0;
1249e93f7393Sniklas     }
1250e93f7393Sniklas 
1251e93f7393Sniklas   *perr = 0;
1252e93f7393Sniklas   return rresponse;
1253e93f7393Sniklas }
1254e93f7393Sniklas 
1255e93f7393Sniklas static void
mips_initialize_cleanups(void * arg)1256b725ae77Skettenis mips_initialize_cleanups (void *arg)
1257e93f7393Sniklas {
1258e93f7393Sniklas   mips_initializing = 0;
1259e93f7393Sniklas }
1260e93f7393Sniklas 
1261e93f7393Sniklas static void
mips_exit_cleanups(void * arg)1262b725ae77Skettenis mips_exit_cleanups (void *arg)
1263e93f7393Sniklas {
1264b725ae77Skettenis   mips_exiting = 0;
1265b725ae77Skettenis }
1266b725ae77Skettenis 
1267b725ae77Skettenis static void
mips_send_command(const char * cmd,int prompt)1268b725ae77Skettenis mips_send_command (const char *cmd, int prompt)
1269b725ae77Skettenis {
1270b725ae77Skettenis   serial_write (mips_desc, cmd, strlen (cmd));
1271e93f7393Sniklas   mips_expect (cmd);
1272b725ae77Skettenis   mips_expect ("\n");
1273e93f7393Sniklas   if (prompt)
1274e93f7393Sniklas     mips_expect (mips_monitor_prompt);
1275e93f7393Sniklas }
1276e93f7393Sniklas 
1277e93f7393Sniklas /* Enter remote (dbx) debug mode: */
1278e93f7393Sniklas static void
mips_enter_debug(void)1279b725ae77Skettenis mips_enter_debug (void)
1280e93f7393Sniklas {
1281e93f7393Sniklas   /* Reset the sequence numbers, ready for the new debug sequence: */
1282e93f7393Sniklas   mips_send_seq = 0;
1283e93f7393Sniklas   mips_receive_seq = 0;
1284e93f7393Sniklas 
1285b725ae77Skettenis   if (mips_monitor != MON_IDT)
1286b725ae77Skettenis     mips_send_command ("debug\r", 0);
1287e93f7393Sniklas   else				/* assume IDT monitor by default */
1288b725ae77Skettenis     mips_send_command ("db tty0\r", 0);
1289e93f7393Sniklas 
1290b725ae77Skettenis   sleep (1);
1291b725ae77Skettenis   serial_write (mips_desc, "\r", sizeof "\r" - 1);
1292e93f7393Sniklas 
1293e93f7393Sniklas   /* We don't need to absorb any spurious characters here, since the
1294e93f7393Sniklas      mips_receive_header will eat up a reasonable number of characters
1295e93f7393Sniklas      whilst looking for the SYN, however this avoids the "garbage"
1296e93f7393Sniklas      being displayed to the user. */
1297b725ae77Skettenis   if (mips_monitor != MON_IDT)
1298b725ae77Skettenis     mips_expect ("\r");
1299e93f7393Sniklas 
1300e93f7393Sniklas   {
1301e93f7393Sniklas     char buff[DATA_MAXLEN + 1];
1302e93f7393Sniklas     if (mips_receive_packet (buff, 1, 3) < 0)
1303e93f7393Sniklas       mips_error ("Failed to initialize (didn't receive packet).");
1304e93f7393Sniklas   }
1305e93f7393Sniklas }
1306e93f7393Sniklas 
1307e93f7393Sniklas /* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1308e93f7393Sniklas static int
mips_exit_debug(void)1309b725ae77Skettenis mips_exit_debug (void)
1310e93f7393Sniklas {
1311e93f7393Sniklas   int err;
1312b725ae77Skettenis   struct cleanup *old_cleanups = make_cleanup (mips_exit_cleanups, NULL);
1313e93f7393Sniklas 
1314b725ae77Skettenis   mips_exiting = 1;
1315b725ae77Skettenis 
1316b725ae77Skettenis   if (mips_monitor != MON_IDT)
1317e93f7393Sniklas     {
1318b725ae77Skettenis       /* The DDB (NEC) and MiniRISC (LSI) versions of PMON exit immediately,
1319b725ae77Skettenis          so we do not get a reply to this command: */
1320b725ae77Skettenis       mips_request ('x', 0, 0, NULL, mips_receive_wait, NULL);
1321e93f7393Sniklas       mips_need_reply = 0;
1322e93f7393Sniklas       if (!mips_expect (" break!"))
1323e93f7393Sniklas 	return -1;
1324e93f7393Sniklas     }
1325e93f7393Sniklas   else
1326b725ae77Skettenis     mips_request ('x', 0, 0, &err, mips_receive_wait, NULL);
1327e93f7393Sniklas 
1328e93f7393Sniklas   if (!mips_expect (mips_monitor_prompt))
1329e93f7393Sniklas     return -1;
1330e93f7393Sniklas 
1331b725ae77Skettenis   do_cleanups (old_cleanups);
1332b725ae77Skettenis 
1333e93f7393Sniklas   return 0;
1334e93f7393Sniklas }
1335e93f7393Sniklas 
1336e93f7393Sniklas /* Initialize a new connection to the MIPS board, and make sure we are
1337e93f7393Sniklas    really connected.  */
1338e93f7393Sniklas 
1339e93f7393Sniklas static void
mips_initialize(void)1340b725ae77Skettenis mips_initialize (void)
1341e93f7393Sniklas {
1342e93f7393Sniklas   int err;
1343e93f7393Sniklas   struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1344e93f7393Sniklas   int j;
1345e93f7393Sniklas 
1346e93f7393Sniklas   /* What is this code doing here?  I don't see any way it can happen, and
1347e93f7393Sniklas      it might mean mips_initializing didn't get cleared properly.
1348e93f7393Sniklas      So I'll make it a warning.  */
1349e93f7393Sniklas 
1350e93f7393Sniklas   if (mips_initializing)
1351e93f7393Sniklas     {
1352e93f7393Sniklas       warning ("internal error: mips_initialize called twice");
1353e93f7393Sniklas       return;
1354e93f7393Sniklas     }
1355e93f7393Sniklas 
1356e93f7393Sniklas   mips_wait_flag = 0;
1357e93f7393Sniklas   mips_initializing = 1;
1358e93f7393Sniklas 
1359e93f7393Sniklas   /* At this point, the packit protocol isn't responding.  We'll try getting
1360e93f7393Sniklas      into the monitor, and restarting the protocol.  */
1361e93f7393Sniklas 
1362e93f7393Sniklas   /* Force the system into the monitor.  After this we *should* be at
1363e93f7393Sniklas      the mips_monitor_prompt.  */
1364b725ae77Skettenis   if (mips_monitor != MON_IDT)
1365e93f7393Sniklas     j = 0;			/* start by checking if we are already at the prompt */
1366e93f7393Sniklas   else
1367e93f7393Sniklas     j = 1;			/* start by sending a break */
1368e93f7393Sniklas   for (; j <= 4; j++)
1369e93f7393Sniklas     {
1370e93f7393Sniklas       switch (j)
1371e93f7393Sniklas 	{
1372e93f7393Sniklas 	case 0:		/* First, try sending a CR */
1373b725ae77Skettenis 	  serial_flush_input (mips_desc);
1374b725ae77Skettenis 	  serial_write (mips_desc, "\r", 1);
1375e93f7393Sniklas 	  break;
1376e93f7393Sniklas 	case 1:		/* First, try sending a break */
1377b725ae77Skettenis 	  serial_send_break (mips_desc);
1378e93f7393Sniklas 	  break;
1379e93f7393Sniklas 	case 2:		/* Then, try a ^C */
1380b725ae77Skettenis 	  serial_write (mips_desc, "\003", 1);
1381e93f7393Sniklas 	  break;
1382e93f7393Sniklas 	case 3:		/* Then, try escaping from download */
1383e93f7393Sniklas 	  {
1384b725ae77Skettenis 	    if (mips_monitor != MON_IDT)
1385e93f7393Sniklas 	      {
1386e93f7393Sniklas 		char tbuff[7];
1387e93f7393Sniklas 
1388e93f7393Sniklas 		/* We shouldn't need to send multiple termination
1389e93f7393Sniklas 		   sequences, since the target performs line (or
1390e93f7393Sniklas 		   block) reads, and then processes those
1391e93f7393Sniklas 		   packets. In-case we were downloading a large packet
1392e93f7393Sniklas 		   we flush the output buffer before inserting a
1393e93f7393Sniklas 		   termination sequence. */
1394b725ae77Skettenis 		serial_flush_output (mips_desc);
1395b725ae77Skettenis 		sprintf (tbuff, "\r/E/E\r");
1396b725ae77Skettenis 		serial_write (mips_desc, tbuff, 6);
1397e93f7393Sniklas 	      }
1398e93f7393Sniklas 	    else
1399e93f7393Sniklas 	      {
1400e93f7393Sniklas 		char srec[10];
1401e93f7393Sniklas 		int i;
1402e93f7393Sniklas 
1403e93f7393Sniklas 		/* We are possibly in binary download mode, having
1404e93f7393Sniklas 		   aborted in the middle of an S-record.  ^C won't
1405e93f7393Sniklas 		   work because of binary mode.  The only reliable way
1406e93f7393Sniklas 		   out is to send enough termination packets (8 bytes)
1407e93f7393Sniklas 		   to fill up and then overflow the largest size
1408e93f7393Sniklas 		   S-record (255 bytes in this case).  This amounts to
1409e93f7393Sniklas 		   256/8 + 1 packets.
1410e93f7393Sniklas 		 */
1411e93f7393Sniklas 
1412e93f7393Sniklas 		mips_make_srec (srec, '7', 0, NULL, 0);
1413e93f7393Sniklas 
1414e93f7393Sniklas 		for (i = 1; i <= 33; i++)
1415e93f7393Sniklas 		  {
1416b725ae77Skettenis 		    serial_write (mips_desc, srec, 8);
1417e93f7393Sniklas 
1418b725ae77Skettenis 		    if (serial_readchar (mips_desc, 0) >= 0)
1419e93f7393Sniklas 		      break;	/* Break immediatly if we get something from
1420e93f7393Sniklas 				   the board. */
1421e93f7393Sniklas 		  }
1422e93f7393Sniklas 	      }
1423e93f7393Sniklas 	  }
1424e93f7393Sniklas 	  break;
1425e93f7393Sniklas 	case 4:
1426e93f7393Sniklas 	  mips_error ("Failed to initialize.");
1427e93f7393Sniklas 	}
1428e93f7393Sniklas 
1429e93f7393Sniklas       if (mips_expect (mips_monitor_prompt))
1430e93f7393Sniklas 	break;
1431e93f7393Sniklas     }
1432e93f7393Sniklas 
1433b725ae77Skettenis   if (mips_monitor != MON_IDT)
1434e93f7393Sniklas     {
1435b725ae77Skettenis       /* Sometimes PMON ignores the first few characters in the first
1436b725ae77Skettenis          command sent after a load.  Sending a blank command gets
1437b725ae77Skettenis          around that.  */
1438b725ae77Skettenis       mips_send_command ("\r", -1);
1439b725ae77Skettenis 
1440e93f7393Sniklas       /* Ensure the correct target state: */
1441b725ae77Skettenis       if (mips_monitor != MON_LSI)
1442b725ae77Skettenis 	mips_send_command ("set regsize 64\r", -1);
1443b725ae77Skettenis       mips_send_command ("set hostport tty0\r", -1);
1444b725ae77Skettenis       mips_send_command ("set brkcmd \"\"\r", -1);
1445e93f7393Sniklas       /* Delete all the current breakpoints: */
1446b725ae77Skettenis       mips_send_command ("db *\r", -1);
1447e93f7393Sniklas       /* NOTE: PMON does not have breakpoint support through the
1448e93f7393Sniklas          "debug" mode, only at the monitor command-line. */
1449e93f7393Sniklas     }
1450e93f7393Sniklas 
1451e93f7393Sniklas   mips_enter_debug ();
1452e93f7393Sniklas 
1453e93f7393Sniklas   /* Clear all breakpoints: */
1454b725ae77Skettenis   if ((mips_monitor == MON_IDT
1455b725ae77Skettenis        && clear_breakpoint (-1, 0, BREAK_UNUSED) == 0)
1456b725ae77Skettenis       || mips_monitor == MON_LSI)
1457e93f7393Sniklas     monitor_supports_breakpoints = 1;
1458b725ae77Skettenis   else
1459b725ae77Skettenis     monitor_supports_breakpoints = 0;
1460e93f7393Sniklas 
1461e93f7393Sniklas   do_cleanups (old_cleanups);
1462e93f7393Sniklas 
1463e93f7393Sniklas   /* If this doesn't call error, we have connected; we don't care if
1464e93f7393Sniklas      the request itself succeeds or fails.  */
1465e93f7393Sniklas 
1466b725ae77Skettenis   mips_request ('r', 0, 0, &err, mips_receive_wait, NULL);
1467e93f7393Sniklas }
1468e93f7393Sniklas 
1469e93f7393Sniklas /* Open a connection to the remote board.  */
1470e93f7393Sniklas static void
common_open(struct target_ops * ops,char * name,int from_tty,enum mips_monitor_type new_monitor,const char * new_monitor_prompt)1471b725ae77Skettenis common_open (struct target_ops *ops, char *name, int from_tty,
1472b725ae77Skettenis 	     enum mips_monitor_type new_monitor,
1473b725ae77Skettenis 	     const char *new_monitor_prompt)
1474e93f7393Sniklas {
1475e93f7393Sniklas   char *ptype;
1476b725ae77Skettenis   char *serial_port_name;
1477b725ae77Skettenis   char *remote_name = 0;
1478b725ae77Skettenis   char *local_name = 0;
1479b725ae77Skettenis   char **argv;
1480e93f7393Sniklas 
1481e93f7393Sniklas   if (name == 0)
1482e93f7393Sniklas     error (
1483e93f7393Sniklas 	    "To open a MIPS remote debugging connection, you need to specify what serial\n\
1484b725ae77Skettenis device is attached to the target board (e.g., /dev/ttya).\n"
1485b725ae77Skettenis 	    "If you want to use TFTP to download to the board, specify the name of a\n"
1486b725ae77Skettenis 	    "temporary file to be used by GDB for downloads as the second argument.\n"
1487b725ae77Skettenis 	    "This filename must be in the form host:filename, where host is the name\n"
1488b725ae77Skettenis 	    "of the host running the TFTP server, and the file must be readable by the\n"
1489b725ae77Skettenis 	    "world.  If the local name of the temporary file differs from the name as\n"
1490b725ae77Skettenis 	    "seen from the board via TFTP, specify that name as the third parameter.\n");
1491b725ae77Skettenis 
1492b725ae77Skettenis   /* Parse the serial port name, the optional TFTP name, and the
1493b725ae77Skettenis      optional local TFTP name.  */
1494b725ae77Skettenis   if ((argv = buildargv (name)) == NULL)
1495b725ae77Skettenis     nomem (0);
1496b725ae77Skettenis   make_cleanup_freeargv (argv);
1497b725ae77Skettenis 
1498b725ae77Skettenis   serial_port_name = xstrdup (argv[0]);
1499b725ae77Skettenis   if (argv[1])			/* remote TFTP name specified? */
1500b725ae77Skettenis     {
1501b725ae77Skettenis       remote_name = argv[1];
1502b725ae77Skettenis       if (argv[2])		/* local TFTP filename specified? */
1503b725ae77Skettenis 	local_name = argv[2];
1504b725ae77Skettenis     }
1505e93f7393Sniklas 
1506e93f7393Sniklas   target_preopen (from_tty);
1507e93f7393Sniklas 
1508e93f7393Sniklas   if (mips_is_open)
1509e93f7393Sniklas     unpush_target (current_ops);
1510e93f7393Sniklas 
1511b725ae77Skettenis   /* Open and initialize the serial port.  */
1512b725ae77Skettenis   mips_desc = serial_open (serial_port_name);
1513b725ae77Skettenis   if (mips_desc == NULL)
1514b725ae77Skettenis     perror_with_name (serial_port_name);
1515e93f7393Sniklas 
1516e93f7393Sniklas   if (baud_rate != -1)
1517e93f7393Sniklas     {
1518b725ae77Skettenis       if (serial_setbaudrate (mips_desc, baud_rate))
1519e93f7393Sniklas 	{
1520b725ae77Skettenis 	  serial_close (mips_desc);
1521b725ae77Skettenis 	  perror_with_name (serial_port_name);
1522e93f7393Sniklas 	}
1523e93f7393Sniklas     }
1524e93f7393Sniklas 
1525b725ae77Skettenis   serial_raw (mips_desc);
1526b725ae77Skettenis 
1527b725ae77Skettenis   /* Open and initialize the optional download port.  If it is in the form
1528b725ae77Skettenis      hostname#portnumber, it's a UDP socket.  If it is in the form
1529b725ae77Skettenis      hostname:filename, assume it's the TFTP filename that must be
1530b725ae77Skettenis      passed to the DDB board to tell it where to get the load file.  */
1531b725ae77Skettenis   if (remote_name)
1532b725ae77Skettenis     {
1533b725ae77Skettenis       if (strchr (remote_name, '#'))
1534b725ae77Skettenis 	{
1535b725ae77Skettenis 	  udp_desc = serial_open (remote_name);
1536b725ae77Skettenis 	  if (!udp_desc)
1537b725ae77Skettenis 	    perror_with_name ("Unable to open UDP port");
1538b725ae77Skettenis 	  udp_in_use = 1;
1539b725ae77Skettenis 	}
1540b725ae77Skettenis       else
1541b725ae77Skettenis 	{
1542b725ae77Skettenis 	  /* Save the remote and local names of the TFTP temp file.  If
1543b725ae77Skettenis 	     the user didn't specify a local name, assume it's the same
1544b725ae77Skettenis 	     as the part of the remote name after the "host:".  */
1545b725ae77Skettenis 	  if (tftp_name)
1546b725ae77Skettenis 	    xfree (tftp_name);
1547b725ae77Skettenis 	  if (tftp_localname)
1548b725ae77Skettenis 	    xfree (tftp_localname);
1549b725ae77Skettenis 	  if (local_name == NULL)
1550b725ae77Skettenis 	    if ((local_name = strchr (remote_name, ':')) != NULL)
1551b725ae77Skettenis 	      local_name++;	/* skip over the colon */
1552b725ae77Skettenis 	  if (local_name == NULL)
1553b725ae77Skettenis 	    local_name = remote_name;	/* local name same as remote name */
1554b725ae77Skettenis 	  tftp_name = xstrdup (remote_name);
1555b725ae77Skettenis 	  tftp_localname = xstrdup (local_name);
1556b725ae77Skettenis 	  tftp_in_use = 1;
1557b725ae77Skettenis 	}
1558b725ae77Skettenis     }
1559e93f7393Sniklas 
1560e93f7393Sniklas   current_ops = ops;
1561e93f7393Sniklas   mips_is_open = 1;
1562e93f7393Sniklas 
1563b725ae77Skettenis   /* Reset the expected monitor prompt if it's never been set before.  */
1564b725ae77Skettenis   if (mips_monitor_prompt == NULL)
1565b725ae77Skettenis     mips_monitor_prompt = xstrdup (new_monitor_prompt);
1566b725ae77Skettenis   mips_monitor = new_monitor;
1567b725ae77Skettenis 
1568e93f7393Sniklas   mips_initialize ();
1569e93f7393Sniklas 
1570e93f7393Sniklas   if (from_tty)
1571b725ae77Skettenis     printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
1572e93f7393Sniklas 
1573e93f7393Sniklas   /* Switch to using remote target now.  */
1574e93f7393Sniklas   push_target (ops);
1575e93f7393Sniklas 
1576e93f7393Sniklas   /* FIXME: Should we call start_remote here?  */
1577e93f7393Sniklas 
1578e93f7393Sniklas   /* Try to figure out the processor model if possible.  */
1579b725ae77Skettenis   deprecated_mips_set_processor_regs_hack ();
1580e93f7393Sniklas 
1581b725ae77Skettenis   /* This is really the job of start_remote however, that makes an
1582b725ae77Skettenis      assumption that the target is about to print out a status message
1583b725ae77Skettenis      of some sort.  That doesn't happen here (in fact, it may not be
1584b725ae77Skettenis      possible to get the monitor to send the appropriate packet).  */
1585e93f7393Sniklas 
1586e93f7393Sniklas   flush_cached_frames ();
1587e93f7393Sniklas   registers_changed ();
1588e93f7393Sniklas   stop_pc = read_pc ();
1589*63addd46Skettenis   print_stack_frame (get_selected_frame (), 0, SRC_AND_LOC);
1590b725ae77Skettenis   xfree (serial_port_name);
1591e93f7393Sniklas }
1592e93f7393Sniklas 
1593e93f7393Sniklas static void
mips_open(char * name,int from_tty)1594b725ae77Skettenis mips_open (char *name, int from_tty)
1595e93f7393Sniklas {
1596b725ae77Skettenis   const char *monitor_prompt = NULL;
1597b725ae77Skettenis   if (TARGET_ARCHITECTURE != NULL
1598b725ae77Skettenis       && TARGET_ARCHITECTURE->arch == bfd_arch_mips)
1599b725ae77Skettenis     {
1600b725ae77Skettenis     switch (TARGET_ARCHITECTURE->mach)
1601b725ae77Skettenis       {
1602b725ae77Skettenis       case bfd_mach_mips4100:
1603b725ae77Skettenis       case bfd_mach_mips4300:
1604b725ae77Skettenis       case bfd_mach_mips4600:
1605b725ae77Skettenis       case bfd_mach_mips4650:
1606b725ae77Skettenis       case bfd_mach_mips5000:
1607b725ae77Skettenis 	monitor_prompt = "<RISQ> ";
1608b725ae77Skettenis 	break;
1609b725ae77Skettenis       }
1610b725ae77Skettenis     }
1611b725ae77Skettenis   if (monitor_prompt == NULL)
1612b725ae77Skettenis     monitor_prompt = "<IDT>";
1613b725ae77Skettenis   common_open (&mips_ops, name, from_tty, MON_IDT, monitor_prompt);
1614e93f7393Sniklas }
1615e93f7393Sniklas 
1616e93f7393Sniklas static void
pmon_open(char * name,int from_tty)1617b725ae77Skettenis pmon_open (char *name, int from_tty)
1618e93f7393Sniklas {
1619b725ae77Skettenis   common_open (&pmon_ops, name, from_tty, MON_PMON, "PMON> ");
1620e93f7393Sniklas }
1621e93f7393Sniklas 
1622e93f7393Sniklas static void
ddb_open(char * name,int from_tty)1623b725ae77Skettenis ddb_open (char *name, int from_tty)
1624e93f7393Sniklas {
1625b725ae77Skettenis   common_open (&ddb_ops, name, from_tty, MON_DDB, "NEC010>");
1626b725ae77Skettenis }
1627b725ae77Skettenis 
1628b725ae77Skettenis static void
lsi_open(char * name,int from_tty)1629b725ae77Skettenis lsi_open (char *name, int from_tty)
1630b725ae77Skettenis {
1631b725ae77Skettenis   int i;
1632b725ae77Skettenis 
1633b725ae77Skettenis   /* Clear the LSI breakpoint table.  */
1634b725ae77Skettenis   for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1635b725ae77Skettenis     lsi_breakpoints[i].type = BREAK_UNUSED;
1636b725ae77Skettenis 
1637b725ae77Skettenis   common_open (&lsi_ops, name, from_tty, MON_LSI, "PMON> ");
1638e93f7393Sniklas }
1639e93f7393Sniklas 
1640e93f7393Sniklas /* Close a connection to the remote board.  */
1641e93f7393Sniklas 
1642e93f7393Sniklas static void
mips_close(int quitting)1643b725ae77Skettenis mips_close (int quitting)
1644e93f7393Sniklas {
1645e93f7393Sniklas   if (mips_is_open)
1646e93f7393Sniklas     {
1647e93f7393Sniklas       /* Get the board out of remote debugging mode.  */
1648e93f7393Sniklas       (void) mips_exit_debug ();
1649e93f7393Sniklas 
1650b725ae77Skettenis       close_ports ();
1651e93f7393Sniklas     }
1652e93f7393Sniklas }
1653e93f7393Sniklas 
1654e93f7393Sniklas /* Detach from the remote board.  */
1655e93f7393Sniklas 
1656e93f7393Sniklas static void
mips_detach(char * args,int from_tty)1657b725ae77Skettenis mips_detach (char *args, int from_tty)
1658e93f7393Sniklas {
1659e93f7393Sniklas   if (args)
1660e93f7393Sniklas     error ("Argument given to \"detach\" when remotely debugging.");
1661e93f7393Sniklas 
1662e93f7393Sniklas   pop_target ();
1663e93f7393Sniklas 
1664e93f7393Sniklas   mips_close (1);
1665e93f7393Sniklas 
1666e93f7393Sniklas   if (from_tty)
1667e93f7393Sniklas     printf_unfiltered ("Ending remote MIPS debugging.\n");
1668e93f7393Sniklas }
1669e93f7393Sniklas 
1670e93f7393Sniklas /* Tell the target board to resume.  This does not wait for a reply
1671b725ae77Skettenis    from the board, except in the case of single-stepping on LSI boards,
1672b725ae77Skettenis    where PMON does return a reply.  */
1673e93f7393Sniklas 
1674e93f7393Sniklas static void
mips_resume(ptid_t ptid,int step,enum target_signal siggnal)1675b725ae77Skettenis mips_resume (ptid_t ptid, int step, enum target_signal siggnal)
1676e93f7393Sniklas {
1677b725ae77Skettenis   int err;
1678e93f7393Sniklas 
1679b725ae77Skettenis   /* LSI PMON requires returns a reply packet "0x1 s 0x0 0x57f" after
1680b725ae77Skettenis      a single step, so we wait for that.  */
1681b725ae77Skettenis   mips_request (step ? 's' : 'c', 1, siggnal,
1682b725ae77Skettenis 		mips_monitor == MON_LSI && step ? &err : (int *) NULL,
1683e93f7393Sniklas 		mips_receive_wait, NULL);
1684e93f7393Sniklas }
1685e93f7393Sniklas 
1686e93f7393Sniklas /* Return the signal corresponding to SIG, where SIG is the number which
1687e93f7393Sniklas    the MIPS protocol uses for the signal.  */
1688b725ae77Skettenis static enum target_signal
mips_signal_from_protocol(int sig)1689b725ae77Skettenis mips_signal_from_protocol (int sig)
1690e93f7393Sniklas {
1691e93f7393Sniklas   /* We allow a few more signals than the IDT board actually returns, on
1692e93f7393Sniklas      the theory that there is at least *some* hope that perhaps the numbering
1693e93f7393Sniklas      for these signals is widely agreed upon.  */
1694e93f7393Sniklas   if (sig <= 0
1695e93f7393Sniklas       || sig > 31)
1696e93f7393Sniklas     return TARGET_SIGNAL_UNKNOWN;
1697e93f7393Sniklas 
1698e93f7393Sniklas   /* Don't want to use target_signal_from_host because we are converting
1699e93f7393Sniklas      from MIPS signal numbers, not host ones.  Our internal numbers
1700e93f7393Sniklas      match the MIPS numbers for the signals the board can return, which
1701e93f7393Sniklas      are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP.  */
1702e93f7393Sniklas   return (enum target_signal) sig;
1703e93f7393Sniklas }
1704e93f7393Sniklas 
1705e93f7393Sniklas /* Wait until the remote stops, and return a wait status.  */
1706e93f7393Sniklas 
1707b725ae77Skettenis static ptid_t
mips_wait(ptid_t ptid,struct target_waitstatus * status)1708b725ae77Skettenis mips_wait (ptid_t ptid, struct target_waitstatus *status)
1709e93f7393Sniklas {
1710e93f7393Sniklas   int rstatus;
1711e93f7393Sniklas   int err;
1712e93f7393Sniklas   char buff[DATA_MAXLEN];
1713e93f7393Sniklas   int rpc, rfp, rsp;
1714e93f7393Sniklas   char flags[20];
1715e93f7393Sniklas   int nfields;
1716b725ae77Skettenis   int i;
1717e93f7393Sniklas 
1718e93f7393Sniklas   interrupt_count = 0;
1719e93f7393Sniklas   hit_watchpoint = 0;
1720e93f7393Sniklas 
1721e93f7393Sniklas   /* If we have not sent a single step or continue command, then the
1722e93f7393Sniklas      board is waiting for us to do something.  Return a status
1723e93f7393Sniklas      indicating that it is stopped.  */
1724e93f7393Sniklas   if (!mips_need_reply)
1725e93f7393Sniklas     {
1726e93f7393Sniklas       status->kind = TARGET_WAITKIND_STOPPED;
1727e93f7393Sniklas       status->value.sig = TARGET_SIGNAL_TRAP;
1728b725ae77Skettenis       return inferior_ptid;
1729e93f7393Sniklas     }
1730e93f7393Sniklas 
1731e93f7393Sniklas   /* No timeout; we sit here as long as the program continues to execute.  */
1732e93f7393Sniklas   mips_wait_flag = 1;
1733b725ae77Skettenis   rstatus = mips_request ('\000', 0, 0, &err, -1, buff);
1734e93f7393Sniklas   mips_wait_flag = 0;
1735e93f7393Sniklas   if (err)
1736e93f7393Sniklas     mips_error ("Remote failure: %s", safe_strerror (errno));
1737e93f7393Sniklas 
1738b725ae77Skettenis   /* On returning from a continue, the PMON monitor seems to start
1739b725ae77Skettenis      echoing back the messages we send prior to sending back the
1740b725ae77Skettenis      ACK. The code can cope with this, but to try and avoid the
1741b725ae77Skettenis      unnecessary serial traffic, and "spurious" characters displayed
1742b725ae77Skettenis      to the user, we cheat and reset the debug protocol. The problems
1743b725ae77Skettenis      seems to be caused by a check on the number of arguments, and the
1744b725ae77Skettenis      command length, within the monitor causing it to echo the command
1745b725ae77Skettenis      as a bad packet. */
1746b725ae77Skettenis   if (mips_monitor == MON_PMON)
1747b725ae77Skettenis     {
1748b725ae77Skettenis       mips_exit_debug ();
1749b725ae77Skettenis       mips_enter_debug ();
1750b725ae77Skettenis     }
1751e93f7393Sniklas 
1752e93f7393Sniklas   /* See if we got back extended status.  If so, pick out the pc, fp, sp, etc... */
1753e93f7393Sniklas 
1754b725ae77Skettenis   nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1755b725ae77Skettenis 		    &rpc, &rfp, &rsp, flags);
1756b725ae77Skettenis   if (nfields >= 3)
1757e93f7393Sniklas     {
1758b725ae77Skettenis       char buf[MAX_REGISTER_SIZE];
1759e93f7393Sniklas 
1760*63addd46Skettenis       store_unsigned_integer (buf, register_size (current_gdbarch, PC_REGNUM), rpc);
1761*63addd46Skettenis       regcache_raw_supply (current_regcache, PC_REGNUM, buf);
1762e93f7393Sniklas 
1763*63addd46Skettenis       store_unsigned_integer (buf, register_size (current_gdbarch, PC_REGNUM), rfp);
1764*63addd46Skettenis       regcache_raw_supply (current_regcache, 30, buf);	/* This register they are avoiding and so it is unnamed */
1765e93f7393Sniklas 
1766*63addd46Skettenis       store_unsigned_integer (buf, register_size (current_gdbarch, SP_REGNUM), rsp);
1767*63addd46Skettenis       regcache_raw_supply (current_regcache, SP_REGNUM, buf);
1768e93f7393Sniklas 
1769*63addd46Skettenis       store_unsigned_integer (buf, register_size (current_gdbarch, DEPRECATED_FP_REGNUM), 0);
1770*63addd46Skettenis       regcache_raw_supply (current_regcache, DEPRECATED_FP_REGNUM, buf);
1771e93f7393Sniklas 
1772e93f7393Sniklas       if (nfields == 9)
1773e93f7393Sniklas 	{
1774e93f7393Sniklas 	  int i;
1775e93f7393Sniklas 
1776e93f7393Sniklas 	  for (i = 0; i <= 2; i++)
1777e93f7393Sniklas 	    if (flags[i] == 'r' || flags[i] == 'w')
1778e93f7393Sniklas 	      hit_watchpoint = 1;
1779e93f7393Sniklas 	    else if (flags[i] == '\000')
1780e93f7393Sniklas 	      break;
1781e93f7393Sniklas 	}
1782e93f7393Sniklas     }
1783e93f7393Sniklas 
1784b725ae77Skettenis   if (strcmp (target_shortname, "lsi") == 0)
1785e93f7393Sniklas     {
1786b725ae77Skettenis #if 0
1787b725ae77Skettenis       /* If this is an LSI PMON target, see if we just hit a hardrdware watchpoint.
1788b725ae77Skettenis          Right now, PMON doesn't give us enough information to determine which
1789b725ae77Skettenis          breakpoint we hit.  So we have to look up the PC in our own table
1790b725ae77Skettenis          of breakpoints, and if found, assume it's just a normal instruction
1791b725ae77Skettenis          fetch breakpoint, not a data watchpoint.  FIXME when PMON
1792b725ae77Skettenis          provides some way to tell us what type of breakpoint it is.  */
1793b725ae77Skettenis       int i;
1794b725ae77Skettenis       CORE_ADDR pc = read_pc ();
1795e93f7393Sniklas 
1796b725ae77Skettenis       hit_watchpoint = 1;
1797b725ae77Skettenis       for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1798e93f7393Sniklas 	{
1799b725ae77Skettenis 	  if (lsi_breakpoints[i].addr == pc
1800b725ae77Skettenis 	      && lsi_breakpoints[i].type == BREAK_FETCH)
1801b725ae77Skettenis 	    {
1802e93f7393Sniklas 	      hit_watchpoint = 0;
1803b725ae77Skettenis 	      break;
1804e93f7393Sniklas 	    }
1805b725ae77Skettenis 	}
1806b725ae77Skettenis #else
1807b725ae77Skettenis       /* If a data breakpoint was hit, PMON returns the following packet:
1808b725ae77Skettenis          0x1 c 0x0 0x57f 0x1
1809b725ae77Skettenis          The return packet from an ordinary breakpoint doesn't have the
1810b725ae77Skettenis          extra 0x01 field tacked onto the end.  */
1811b725ae77Skettenis       if (nfields == 1 && rpc == 1)
1812b725ae77Skettenis 	hit_watchpoint = 1;
1813b725ae77Skettenis #endif
1814b725ae77Skettenis     }
1815e93f7393Sniklas 
1816e93f7393Sniklas   /* NOTE: The following (sig) numbers are defined by PMON:
1817e93f7393Sniklas      SPP_SIGTRAP     5       breakpoint
1818e93f7393Sniklas      SPP_SIGINT      2
1819e93f7393Sniklas      SPP_SIGSEGV     11
1820e93f7393Sniklas      SPP_SIGBUS      10
1821e93f7393Sniklas      SPP_SIGILL      4
1822e93f7393Sniklas      SPP_SIGFPE      8
1823e93f7393Sniklas      SPP_SIGTERM     15 */
1824e93f7393Sniklas 
1825e93f7393Sniklas   /* Translate a MIPS waitstatus.  We use constants here rather than WTERMSIG
1826e93f7393Sniklas      and so on, because the constants we want here are determined by the
1827e93f7393Sniklas      MIPS protocol and have nothing to do with what host we are running on.  */
1828b725ae77Skettenis   if ((rstatus & 0xff) == 0)
1829e93f7393Sniklas     {
1830e93f7393Sniklas       status->kind = TARGET_WAITKIND_EXITED;
1831b725ae77Skettenis       status->value.integer = (((rstatus) >> 8) & 0xff);
1832e93f7393Sniklas     }
1833b725ae77Skettenis   else if ((rstatus & 0xff) == 0x7f)
1834e93f7393Sniklas     {
1835e93f7393Sniklas       status->kind = TARGET_WAITKIND_STOPPED;
1836b725ae77Skettenis       status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0xff);
1837b725ae77Skettenis 
1838b725ae77Skettenis       /* If the stop PC is in the _exit function, assume
1839b725ae77Skettenis          we hit the 'break 0x3ff' instruction in _exit, so this
1840b725ae77Skettenis          is not a normal breakpoint.  */
1841b725ae77Skettenis       if (strcmp (target_shortname, "lsi") == 0)
1842b725ae77Skettenis 	{
1843b725ae77Skettenis 	  char *func_name;
1844b725ae77Skettenis 	  CORE_ADDR func_start;
1845b725ae77Skettenis 	  CORE_ADDR pc = read_pc ();
1846b725ae77Skettenis 
1847b725ae77Skettenis 	  find_pc_partial_function (pc, &func_name, &func_start, NULL);
1848b725ae77Skettenis 	  if (func_name != NULL && strcmp (func_name, "_exit") == 0
1849b725ae77Skettenis 	      && func_start == pc)
1850b725ae77Skettenis 	    status->kind = TARGET_WAITKIND_EXITED;
1851b725ae77Skettenis 	}
1852e93f7393Sniklas     }
1853e93f7393Sniklas   else
1854e93f7393Sniklas     {
1855e93f7393Sniklas       status->kind = TARGET_WAITKIND_SIGNALLED;
1856b725ae77Skettenis       status->value.sig = mips_signal_from_protocol (rstatus & 0x7f);
1857e93f7393Sniklas     }
1858e93f7393Sniklas 
1859b725ae77Skettenis   return inferior_ptid;
1860e93f7393Sniklas }
1861e93f7393Sniklas 
1862e93f7393Sniklas /* We have to map between the register numbers used by gdb and the
1863e93f7393Sniklas    register numbers used by the debugging protocol.  This function
1864e93f7393Sniklas    assumes that we are using tm-mips.h.  */
1865e93f7393Sniklas 
1866e93f7393Sniklas #define REGNO_OFFSET 96
1867e93f7393Sniklas 
1868e93f7393Sniklas static int
mips_map_regno(int regno)1869b725ae77Skettenis mips_map_regno (int regno)
1870e93f7393Sniklas {
1871e93f7393Sniklas   if (regno < 32)
1872e93f7393Sniklas     return regno;
1873b725ae77Skettenis   if (regno >= mips_regnum (current_gdbarch)->fp0
1874b725ae77Skettenis       && regno < mips_regnum (current_gdbarch)->fp0 + 32)
1875b725ae77Skettenis     return regno - mips_regnum (current_gdbarch)->fp0 + 32;
1876b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->pc)
1877e93f7393Sniklas     return REGNO_OFFSET + 0;
1878b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->cause)
1879e93f7393Sniklas     return REGNO_OFFSET + 1;
1880b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->hi)
1881e93f7393Sniklas     return REGNO_OFFSET + 2;
1882b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->lo)
1883e93f7393Sniklas     return REGNO_OFFSET + 3;
1884b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
1885e93f7393Sniklas     return REGNO_OFFSET + 4;
1886b725ae77Skettenis   else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
1887e93f7393Sniklas     return REGNO_OFFSET + 5;
1888b725ae77Skettenis   else
1889e93f7393Sniklas     /* FIXME: Is there a way to get the status register?  */
1890e93f7393Sniklas     return 0;
1891e93f7393Sniklas }
1892e93f7393Sniklas 
1893e93f7393Sniklas /* Fetch the remote registers.  */
1894e93f7393Sniklas 
1895e93f7393Sniklas static void
mips_fetch_registers(int regno)1896b725ae77Skettenis mips_fetch_registers (int regno)
1897e93f7393Sniklas {
1898e93f7393Sniklas   unsigned LONGEST val;
1899e93f7393Sniklas   int err;
1900e93f7393Sniklas 
1901e93f7393Sniklas   if (regno == -1)
1902e93f7393Sniklas     {
1903e93f7393Sniklas       for (regno = 0; regno < NUM_REGS; regno++)
1904e93f7393Sniklas 	mips_fetch_registers (regno);
1905e93f7393Sniklas       return;
1906e93f7393Sniklas     }
1907e93f7393Sniklas 
1908b725ae77Skettenis   if (regno == DEPRECATED_FP_REGNUM || regno == ZERO_REGNUM)
1909b725ae77Skettenis     /* DEPRECATED_FP_REGNUM on the mips is a hack which is just
1910b725ae77Skettenis        supposed to read zero (see also mips-nat.c).  */
1911b725ae77Skettenis     val = 0;
1912b725ae77Skettenis   else
1913b725ae77Skettenis     {
1914b725ae77Skettenis       /* If PMON doesn't support this register, don't waste serial
1915b725ae77Skettenis          bandwidth trying to read it.  */
1916b725ae77Skettenis       int pmon_reg = mips_map_regno (regno);
1917b725ae77Skettenis       if (regno != 0 && pmon_reg == 0)
1918e93f7393Sniklas 	val = 0;
1919e93f7393Sniklas       else
1920e93f7393Sniklas 	{
1921e93f7393Sniklas 	  /* Unfortunately the PMON version in the Vr4300 board has been
1922e93f7393Sniklas 	     compiled without the 64bit register access commands. This
1923e93f7393Sniklas 	     means we cannot get hold of the full register width. */
1924e93f7393Sniklas 	  if (mips_monitor == MON_DDB)
1925b725ae77Skettenis 	    val = (unsigned) mips_request ('t', pmon_reg, 0,
1926b725ae77Skettenis 					   &err, mips_receive_wait, NULL);
1927e93f7393Sniklas 	  else
1928b725ae77Skettenis 	    val = mips_request ('r', pmon_reg, 0,
1929b725ae77Skettenis 				&err, mips_receive_wait, NULL);
1930e93f7393Sniklas 	  if (err)
1931e93f7393Sniklas 	    mips_error ("Can't read register %d: %s", regno,
1932e93f7393Sniklas 			safe_strerror (errno));
1933e93f7393Sniklas 	}
1934b725ae77Skettenis     }
1935e93f7393Sniklas 
1936e93f7393Sniklas   {
1937b725ae77Skettenis     char buf[MAX_REGISTER_SIZE];
1938e93f7393Sniklas 
1939e93f7393Sniklas     /* We got the number the register holds, but gdb expects to see a
1940e93f7393Sniklas        value in the target byte ordering.  */
1941*63addd46Skettenis     store_unsigned_integer (buf, register_size (current_gdbarch, regno), val);
1942*63addd46Skettenis     regcache_raw_supply (current_regcache, regno, buf);
1943e93f7393Sniklas   }
1944e93f7393Sniklas }
1945e93f7393Sniklas 
1946e93f7393Sniklas /* Prepare to store registers.  The MIPS protocol can store individual
1947e93f7393Sniklas    registers, so this function doesn't have to do anything.  */
1948e93f7393Sniklas 
1949e93f7393Sniklas static void
mips_prepare_to_store(void)1950b725ae77Skettenis mips_prepare_to_store (void)
1951e93f7393Sniklas {
1952e93f7393Sniklas }
1953e93f7393Sniklas 
1954e93f7393Sniklas /* Store remote register(s).  */
1955e93f7393Sniklas 
1956e93f7393Sniklas static void
mips_store_registers(int regno)1957b725ae77Skettenis mips_store_registers (int regno)
1958e93f7393Sniklas {
1959e93f7393Sniklas   int err;
1960e93f7393Sniklas 
1961e93f7393Sniklas   if (regno == -1)
1962e93f7393Sniklas     {
1963e93f7393Sniklas       for (regno = 0; regno < NUM_REGS; regno++)
1964e93f7393Sniklas 	mips_store_registers (regno);
1965e93f7393Sniklas       return;
1966e93f7393Sniklas     }
1967e93f7393Sniklas 
1968b725ae77Skettenis   mips_request ('R', mips_map_regno (regno),
1969e93f7393Sniklas 		read_register (regno),
1970e93f7393Sniklas 		&err, mips_receive_wait, NULL);
1971e93f7393Sniklas   if (err)
1972e93f7393Sniklas     mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1973e93f7393Sniklas }
1974e93f7393Sniklas 
1975e93f7393Sniklas /* Fetch a word from the target board.  */
1976e93f7393Sniklas 
1977e93f7393Sniklas static unsigned int
mips_fetch_word(CORE_ADDR addr)1978b725ae77Skettenis mips_fetch_word (CORE_ADDR addr)
1979e93f7393Sniklas {
1980e93f7393Sniklas   unsigned int val;
1981e93f7393Sniklas   int err;
1982e93f7393Sniklas 
1983b725ae77Skettenis   val = mips_request ('d', addr, 0, &err, mips_receive_wait, NULL);
1984e93f7393Sniklas   if (err)
1985e93f7393Sniklas     {
1986e93f7393Sniklas       /* Data space failed; try instruction space.  */
1987b725ae77Skettenis       val = mips_request ('i', addr, 0, &err,
1988e93f7393Sniklas 			  mips_receive_wait, NULL);
1989e93f7393Sniklas       if (err)
1990e93f7393Sniklas 	mips_error ("Can't read address 0x%s: %s",
1991b725ae77Skettenis 		    paddr_nz (addr), safe_strerror (errno));
1992e93f7393Sniklas     }
1993e93f7393Sniklas   return val;
1994e93f7393Sniklas }
1995e93f7393Sniklas 
1996e93f7393Sniklas /* Store a word to the target board.  Returns errno code or zero for
1997e93f7393Sniklas    success.  If OLD_CONTENTS is non-NULL, put the old contents of that
1998e93f7393Sniklas    memory location there.  */
1999e93f7393Sniklas 
2000e93f7393Sniklas /* FIXME! make sure only 32-bit quantities get stored! */
2001e93f7393Sniklas static int
mips_store_word(CORE_ADDR addr,unsigned int val,char * old_contents)2002b725ae77Skettenis mips_store_word (CORE_ADDR addr, unsigned int val, char *old_contents)
2003e93f7393Sniklas {
2004e93f7393Sniklas   int err;
2005e93f7393Sniklas   unsigned int oldcontents;
2006e93f7393Sniklas 
2007b725ae77Skettenis   oldcontents = mips_request ('D', addr, val, &err,
2008e93f7393Sniklas 			      mips_receive_wait, NULL);
2009e93f7393Sniklas   if (err)
2010e93f7393Sniklas     {
2011e93f7393Sniklas       /* Data space failed; try instruction space.  */
2012b725ae77Skettenis       oldcontents = mips_request ('I', addr, val, &err,
2013e93f7393Sniklas 				  mips_receive_wait, NULL);
2014e93f7393Sniklas       if (err)
2015e93f7393Sniklas 	return errno;
2016e93f7393Sniklas     }
2017e93f7393Sniklas   if (old_contents != NULL)
2018e93f7393Sniklas     store_unsigned_integer (old_contents, 4, oldcontents);
2019e93f7393Sniklas   return 0;
2020e93f7393Sniklas }
2021e93f7393Sniklas 
2022e93f7393Sniklas /* Read or write LEN bytes from inferior memory at MEMADDR,
2023e93f7393Sniklas    transferring to or from debugger address MYADDR.  Write to inferior
2024e93f7393Sniklas    if SHOULD_WRITE is nonzero.  Returns length of data written or
2025e93f7393Sniklas    read; 0 for error.  Note that protocol gives us the correct value
2026e93f7393Sniklas    for a longword, since it transfers values in ASCII.  We want the
2027e93f7393Sniklas    byte values, so we have to swap the longword values.  */
2028e93f7393Sniklas 
2029b725ae77Skettenis static int mask_address_p = 1;
2030e93f7393Sniklas 
2031b725ae77Skettenis static int
mips_xfer_memory(CORE_ADDR memaddr,char * myaddr,int len,int write,struct mem_attrib * attrib,struct target_ops * target)2032b725ae77Skettenis mips_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2033b725ae77Skettenis 		  struct mem_attrib *attrib, struct target_ops *target)
2034b725ae77Skettenis {
2035b725ae77Skettenis   int i;
2036b725ae77Skettenis   CORE_ADDR addr;
2037b725ae77Skettenis   int count;
2038b725ae77Skettenis   char *buffer;
2039e93f7393Sniklas   int status;
2040e93f7393Sniklas 
2041b725ae77Skettenis   /* PMON targets do not cope well with 64 bit addresses.  Mask the
2042b725ae77Skettenis      value down to 32 bits. */
2043b725ae77Skettenis   if (mask_address_p)
2044b725ae77Skettenis     memaddr &= (CORE_ADDR) 0xffffffff;
2045b725ae77Skettenis 
2046b725ae77Skettenis   /* Round starting address down to longword boundary.  */
2047b725ae77Skettenis   addr = memaddr & ~3;
2048b725ae77Skettenis   /* Round ending address up; get number of longwords that makes.  */
2049b725ae77Skettenis   count = (((memaddr + len) - addr) + 3) / 4;
2050b725ae77Skettenis   /* Allocate buffer of that many longwords.  */
2051b725ae77Skettenis   buffer = alloca (count * 4);
2052b725ae77Skettenis 
2053e93f7393Sniklas   if (write)
2054e93f7393Sniklas     {
2055e93f7393Sniklas       /* Fill start and end extra bytes of buffer with existing data.  */
2056e93f7393Sniklas       if (addr != memaddr || len < 4)
2057e93f7393Sniklas 	{
2058e93f7393Sniklas 	  /* Need part of initial word -- fetch it.  */
2059e93f7393Sniklas 	  store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
2060e93f7393Sniklas 	}
2061e93f7393Sniklas 
2062e93f7393Sniklas       if (count > 1)
2063e93f7393Sniklas 	{
2064e93f7393Sniklas 	  /* Need part of last word -- fetch it.  FIXME: we do this even
2065e93f7393Sniklas 	     if we don't need it.  */
2066e93f7393Sniklas 	  store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2067e93f7393Sniklas 				  mips_fetch_word (addr + (count - 1) * 4));
2068e93f7393Sniklas 	}
2069e93f7393Sniklas 
2070e93f7393Sniklas       /* Copy data to be written over corresponding part of buffer */
2071e93f7393Sniklas 
2072e93f7393Sniklas       memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2073e93f7393Sniklas 
2074e93f7393Sniklas       /* Write the entire buffer.  */
2075e93f7393Sniklas 
2076e93f7393Sniklas       for (i = 0; i < count; i++, addr += 4)
2077e93f7393Sniklas 	{
2078e93f7393Sniklas 	  status = mips_store_word (addr,
2079e93f7393Sniklas 			       extract_unsigned_integer (&buffer[i * 4], 4),
2080e93f7393Sniklas 				    NULL);
2081e93f7393Sniklas 	  /* Report each kilobyte (we download 32-bit words at a time) */
2082e93f7393Sniklas 	  if (i % 256 == 255)
2083e93f7393Sniklas 	    {
2084e93f7393Sniklas 	      printf_unfiltered ("*");
2085b725ae77Skettenis 	      gdb_flush (gdb_stdout);
2086e93f7393Sniklas 	    }
2087e93f7393Sniklas 	  if (status)
2088e93f7393Sniklas 	    {
2089e93f7393Sniklas 	      errno = status;
2090e93f7393Sniklas 	      return 0;
2091e93f7393Sniklas 	    }
2092e93f7393Sniklas 	  /* FIXME: Do we want a QUIT here?  */
2093e93f7393Sniklas 	}
2094e93f7393Sniklas       if (count >= 256)
2095e93f7393Sniklas 	printf_unfiltered ("\n");
2096e93f7393Sniklas     }
2097e93f7393Sniklas   else
2098e93f7393Sniklas     {
2099e93f7393Sniklas       /* Read all the longwords */
2100e93f7393Sniklas       for (i = 0; i < count; i++, addr += 4)
2101e93f7393Sniklas 	{
2102e93f7393Sniklas 	  store_unsigned_integer (&buffer[i * 4], 4, mips_fetch_word (addr));
2103e93f7393Sniklas 	  QUIT;
2104e93f7393Sniklas 	}
2105e93f7393Sniklas 
2106e93f7393Sniklas       /* Copy appropriate bytes out of the buffer.  */
2107e93f7393Sniklas       memcpy (myaddr, buffer + (memaddr & 3), len);
2108e93f7393Sniklas     }
2109e93f7393Sniklas   return len;
2110e93f7393Sniklas }
2111e93f7393Sniklas 
2112e93f7393Sniklas /* Print info on this target.  */
2113e93f7393Sniklas 
2114e93f7393Sniklas static void
mips_files_info(struct target_ops * ignore)2115b725ae77Skettenis mips_files_info (struct target_ops *ignore)
2116e93f7393Sniklas {
2117e93f7393Sniklas   printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
2118e93f7393Sniklas }
2119e93f7393Sniklas 
2120e93f7393Sniklas /* Kill the process running on the board.  This will actually only
2121e93f7393Sniklas    work if we are doing remote debugging over the console input.  I
2122e93f7393Sniklas    think that if IDT/sim had the remote debug interrupt enabled on the
2123e93f7393Sniklas    right port, we could interrupt the process with a break signal.  */
2124e93f7393Sniklas 
2125e93f7393Sniklas static void
mips_kill(void)2126b725ae77Skettenis mips_kill (void)
2127e93f7393Sniklas {
2128e93f7393Sniklas   if (!mips_wait_flag)
2129e93f7393Sniklas     return;
2130e93f7393Sniklas 
2131e93f7393Sniklas   interrupt_count++;
2132e93f7393Sniklas 
2133e93f7393Sniklas   if (interrupt_count >= 2)
2134e93f7393Sniklas     {
2135e93f7393Sniklas       interrupt_count = 0;
2136e93f7393Sniklas 
2137e93f7393Sniklas       target_terminal_ours ();
2138e93f7393Sniklas 
2139e93f7393Sniklas       if (query ("Interrupted while waiting for the program.\n\
2140e93f7393Sniklas Give up (and stop debugging it)? "))
2141e93f7393Sniklas 	{
2142e93f7393Sniklas 	  /* Clean up in such a way that mips_close won't try to talk to the
2143e93f7393Sniklas 	     board (it almost surely won't work since we weren't able to talk to
2144e93f7393Sniklas 	     it).  */
2145e93f7393Sniklas 	  mips_wait_flag = 0;
2146b725ae77Skettenis 	  close_ports ();
2147e93f7393Sniklas 
2148e93f7393Sniklas 	  printf_unfiltered ("Ending remote MIPS debugging.\n");
2149e93f7393Sniklas 	  target_mourn_inferior ();
2150e93f7393Sniklas 
2151b725ae77Skettenis 	  throw_exception (RETURN_QUIT);
2152e93f7393Sniklas 	}
2153e93f7393Sniklas 
2154e93f7393Sniklas       target_terminal_inferior ();
2155e93f7393Sniklas     }
2156e93f7393Sniklas 
2157e93f7393Sniklas   if (remote_debug > 0)
2158e93f7393Sniklas     printf_unfiltered ("Sending break\n");
2159e93f7393Sniklas 
2160b725ae77Skettenis   serial_send_break (mips_desc);
2161e93f7393Sniklas 
2162e93f7393Sniklas #if 0
2163e93f7393Sniklas   if (mips_is_open)
2164e93f7393Sniklas     {
2165e93f7393Sniklas       char cc;
2166e93f7393Sniklas 
2167e93f7393Sniklas       /* Send a ^C.  */
2168e93f7393Sniklas       cc = '\003';
2169b725ae77Skettenis       serial_write (mips_desc, &cc, 1);
2170e93f7393Sniklas       sleep (1);
2171e93f7393Sniklas       target_mourn_inferior ();
2172e93f7393Sniklas     }
2173e93f7393Sniklas #endif
2174e93f7393Sniklas }
2175e93f7393Sniklas 
2176e93f7393Sniklas /* Start running on the target board.  */
2177e93f7393Sniklas 
2178e93f7393Sniklas static void
mips_create_inferior(char * execfile,char * args,char ** env,int from_tty)2179*63addd46Skettenis mips_create_inferior (char *execfile, char *args, char **env, int from_tty)
2180e93f7393Sniklas {
2181e93f7393Sniklas   CORE_ADDR entry_pt;
2182e93f7393Sniklas 
2183e93f7393Sniklas   if (args && *args)
2184e93f7393Sniklas     {
2185e93f7393Sniklas       warning ("\
2186e93f7393Sniklas Can't pass arguments to remote MIPS board; arguments ignored.");
2187e93f7393Sniklas       /* And don't try to use them on the next "run" command.  */
2188e93f7393Sniklas       execute_command ("set args", 0);
2189e93f7393Sniklas     }
2190e93f7393Sniklas 
2191e93f7393Sniklas   if (execfile == 0 || exec_bfd == 0)
2192e93f7393Sniklas     error ("No executable file specified");
2193e93f7393Sniklas 
2194e93f7393Sniklas   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2195e93f7393Sniklas 
2196e93f7393Sniklas   init_wait_for_inferior ();
2197e93f7393Sniklas 
2198b725ae77Skettenis   /* FIXME: Should we set inferior_ptid here?  */
2199e93f7393Sniklas 
2200e93f7393Sniklas   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
2201e93f7393Sniklas }
2202e93f7393Sniklas 
2203e93f7393Sniklas /* Clean up after a process.  Actually nothing to do.  */
2204e93f7393Sniklas 
2205e93f7393Sniklas static void
mips_mourn_inferior(void)2206b725ae77Skettenis mips_mourn_inferior (void)
2207e93f7393Sniklas {
2208e93f7393Sniklas   if (current_ops != NULL)
2209e93f7393Sniklas     unpush_target (current_ops);
2210e93f7393Sniklas   generic_mourn_inferior ();
2211e93f7393Sniklas }
2212e93f7393Sniklas 
2213e93f7393Sniklas /* We can write a breakpoint and read the shadow contents in one
2214e93f7393Sniklas    operation.  */
2215e93f7393Sniklas 
2216b725ae77Skettenis /* Insert a breakpoint.  On targets that don't have built-in
2217b725ae77Skettenis    breakpoint support, we read the contents of the target location and
2218b725ae77Skettenis    stash it, then overwrite it with a breakpoint instruction.  ADDR is
2219b725ae77Skettenis    the target location in the target machine.  CONTENTS_CACHE is a
2220b725ae77Skettenis    pointer to memory allocated for saving the target contents.  It is
2221b725ae77Skettenis    guaranteed by the caller to be long enough to save the breakpoint
2222b725ae77Skettenis    length returned by BREAKPOINT_FROM_PC.  */
2223e93f7393Sniklas 
2224e93f7393Sniklas static int
mips_insert_breakpoint(CORE_ADDR addr,char * contents_cache)2225b725ae77Skettenis mips_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
2226e93f7393Sniklas {
2227e93f7393Sniklas   if (monitor_supports_breakpoints)
2228b725ae77Skettenis     return set_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2229b725ae77Skettenis   else
2230b725ae77Skettenis     return memory_insert_breakpoint (addr, contents_cache);
2231e93f7393Sniklas }
2232e93f7393Sniklas 
2233e93f7393Sniklas static int
mips_remove_breakpoint(CORE_ADDR addr,char * contents_cache)2234b725ae77Skettenis mips_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
2235e93f7393Sniklas {
2236e93f7393Sniklas   if (monitor_supports_breakpoints)
2237b725ae77Skettenis     return clear_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2238b725ae77Skettenis   else
2239b725ae77Skettenis     return memory_remove_breakpoint (addr, contents_cache);
2240e93f7393Sniklas }
2241e93f7393Sniklas 
2242b725ae77Skettenis /* Tell whether this target can support a hardware breakpoint.  CNT
2243b725ae77Skettenis    is the number of hardware breakpoints already installed.  This
2244b725ae77Skettenis    implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.  */
2245e93f7393Sniklas 
2246b725ae77Skettenis int
mips_can_use_watchpoint(int type,int cnt,int othertype)2247b725ae77Skettenis mips_can_use_watchpoint (int type, int cnt, int othertype)
2248e93f7393Sniklas {
2249b725ae77Skettenis   return cnt < MAX_LSI_BREAKPOINTS && strcmp (target_shortname, "lsi") == 0;
2250e93f7393Sniklas }
2251e93f7393Sniklas 
2252e93f7393Sniklas 
2253e93f7393Sniklas /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2254e93f7393Sniklas    This is used for memory ref breakpoints.  */
2255e93f7393Sniklas 
2256e93f7393Sniklas static unsigned long
calculate_mask(CORE_ADDR addr,int len)2257b725ae77Skettenis calculate_mask (CORE_ADDR addr, int len)
2258e93f7393Sniklas {
2259e93f7393Sniklas   unsigned long mask;
2260e93f7393Sniklas   int i;
2261e93f7393Sniklas 
2262e93f7393Sniklas   mask = addr ^ (addr + len - 1);
2263e93f7393Sniklas 
2264e93f7393Sniklas   for (i = 32; i >= 0; i--)
2265e93f7393Sniklas     if (mask == 0)
2266e93f7393Sniklas       break;
2267e93f7393Sniklas     else
2268e93f7393Sniklas       mask >>= 1;
2269e93f7393Sniklas 
2270e93f7393Sniklas   mask = (unsigned long) 0xffffffff >> i;
2271e93f7393Sniklas 
2272e93f7393Sniklas   return mask;
2273e93f7393Sniklas }
2274e93f7393Sniklas 
2275b725ae77Skettenis 
2276b725ae77Skettenis /* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
2277b725ae77Skettenis    for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
2278b725ae77Skettenis    watchpoint. */
2279e93f7393Sniklas 
2280e93f7393Sniklas int
mips_insert_watchpoint(CORE_ADDR addr,int len,int type)2281b725ae77Skettenis mips_insert_watchpoint (CORE_ADDR addr, int len, int type)
2282e93f7393Sniklas {
2283b725ae77Skettenis   if (set_breakpoint (addr, len, type))
2284e93f7393Sniklas     return -1;
2285e93f7393Sniklas 
2286e93f7393Sniklas   return 0;
2287e93f7393Sniklas }
2288e93f7393Sniklas 
2289e93f7393Sniklas int
mips_remove_watchpoint(CORE_ADDR addr,int len,int type)2290b725ae77Skettenis mips_remove_watchpoint (CORE_ADDR addr, int len, int type)
2291e93f7393Sniklas {
2292b725ae77Skettenis   if (clear_breakpoint (addr, len, type))
2293e93f7393Sniklas     return -1;
2294e93f7393Sniklas 
2295e93f7393Sniklas   return 0;
2296e93f7393Sniklas }
2297e93f7393Sniklas 
2298e93f7393Sniklas int
mips_stopped_by_watchpoint(void)2299b725ae77Skettenis mips_stopped_by_watchpoint (void)
2300e93f7393Sniklas {
2301e93f7393Sniklas   return hit_watchpoint;
2302e93f7393Sniklas }
2303e93f7393Sniklas 
2304e93f7393Sniklas 
2305b725ae77Skettenis /* Insert a breakpoint.  */
2306e93f7393Sniklas 
2307e93f7393Sniklas static int
set_breakpoint(CORE_ADDR addr,int len,enum break_type type)2308b725ae77Skettenis set_breakpoint (CORE_ADDR addr, int len, enum break_type type)
2309e93f7393Sniklas {
2310b725ae77Skettenis   return common_breakpoint (1, addr, len, type);
2311b725ae77Skettenis }
2312b725ae77Skettenis 
2313b725ae77Skettenis 
2314b725ae77Skettenis /* Clear a breakpoint.  */
2315b725ae77Skettenis 
2316b725ae77Skettenis static int
clear_breakpoint(CORE_ADDR addr,int len,enum break_type type)2317b725ae77Skettenis clear_breakpoint (CORE_ADDR addr, int len, enum break_type type)
2318b725ae77Skettenis {
2319b725ae77Skettenis   return common_breakpoint (0, addr, len, type);
2320b725ae77Skettenis }
2321b725ae77Skettenis 
2322b725ae77Skettenis 
2323b725ae77Skettenis /* Check the error code from the return packet for an LSI breakpoint
2324b725ae77Skettenis    command.  If there's no error, just return 0.  If it's a warning,
2325b725ae77Skettenis    print the warning text and return 0.  If it's an error, print
2326b725ae77Skettenis    the error text and return 1.  <ADDR> is the address of the breakpoint
2327b725ae77Skettenis    that was being set.  <RERRFLG> is the error code returned by PMON.
2328b725ae77Skettenis    This is a helper function for common_breakpoint.  */
2329b725ae77Skettenis 
2330b725ae77Skettenis static int
check_lsi_error(CORE_ADDR addr,int rerrflg)2331b725ae77Skettenis check_lsi_error (CORE_ADDR addr, int rerrflg)
2332b725ae77Skettenis {
2333b725ae77Skettenis   struct lsi_error *err;
2334b725ae77Skettenis   char *saddr = paddr_nz (addr);	/* printable address string */
2335b725ae77Skettenis 
2336b725ae77Skettenis   if (rerrflg == 0)		/* no error */
2337b725ae77Skettenis     return 0;
2338b725ae77Skettenis 
2339b725ae77Skettenis   /* Warnings can be ORed together, so check them all.  */
2340b725ae77Skettenis   if (rerrflg & W_WARN)
2341b725ae77Skettenis     {
2342b725ae77Skettenis       if (monitor_warnings)
2343b725ae77Skettenis 	{
2344b725ae77Skettenis 	  int found = 0;
2345b725ae77Skettenis 	  for (err = lsi_warning_table; err->code != 0; err++)
2346b725ae77Skettenis 	    {
2347b725ae77Skettenis 	      if ((err->code & rerrflg) == err->code)
2348b725ae77Skettenis 		{
2349b725ae77Skettenis 		  found = 1;
2350b725ae77Skettenis 		  fprintf_unfiltered (gdb_stderr,
2351b725ae77Skettenis 				  "common_breakpoint (0x%s): Warning: %s\n",
2352b725ae77Skettenis 				      saddr,
2353b725ae77Skettenis 				      err->string);
2354b725ae77Skettenis 		}
2355b725ae77Skettenis 	    }
2356b725ae77Skettenis 	  if (!found)
2357b725ae77Skettenis 	    fprintf_unfiltered (gdb_stderr,
2358b725ae77Skettenis 			"common_breakpoint (0x%s): Unknown warning: 0x%x\n",
2359b725ae77Skettenis 				saddr,
2360b725ae77Skettenis 				rerrflg);
2361b725ae77Skettenis 	}
2362b725ae77Skettenis       return 0;
2363b725ae77Skettenis     }
2364b725ae77Skettenis 
2365b725ae77Skettenis   /* Errors are unique, i.e. can't be ORed together.  */
2366b725ae77Skettenis   for (err = lsi_error_table; err->code != 0; err++)
2367b725ae77Skettenis     {
2368b725ae77Skettenis       if ((err->code & rerrflg) == err->code)
2369b725ae77Skettenis 	{
2370b725ae77Skettenis 	  fprintf_unfiltered (gdb_stderr,
2371b725ae77Skettenis 			      "common_breakpoint (0x%s): Error: %s\n",
2372b725ae77Skettenis 			      saddr,
2373b725ae77Skettenis 			      err->string);
2374b725ae77Skettenis 	  return 1;
2375b725ae77Skettenis 	}
2376b725ae77Skettenis     }
2377b725ae77Skettenis   fprintf_unfiltered (gdb_stderr,
2378b725ae77Skettenis 		      "common_breakpoint (0x%s): Unknown error: 0x%x\n",
2379b725ae77Skettenis 		      saddr,
2380b725ae77Skettenis 		      rerrflg);
2381b725ae77Skettenis   return 1;
2382b725ae77Skettenis }
2383b725ae77Skettenis 
2384b725ae77Skettenis 
2385b725ae77Skettenis /* This routine sends a breakpoint command to the remote target.
2386b725ae77Skettenis 
2387b725ae77Skettenis    <SET> is 1 if setting a breakpoint, or 0 if clearing a breakpoint.
2388b725ae77Skettenis    <ADDR> is the address of the breakpoint.
2389b725ae77Skettenis    <LEN> the length of the region to break on.
2390b725ae77Skettenis    <TYPE> is the type of breakpoint:
2391b725ae77Skettenis    0 = write                    (BREAK_WRITE)
2392b725ae77Skettenis    1 = read                     (BREAK_READ)
2393b725ae77Skettenis    2 = read/write               (BREAK_ACCESS)
2394b725ae77Skettenis    3 = instruction fetch        (BREAK_FETCH)
2395b725ae77Skettenis 
2396b725ae77Skettenis    Return 0 if successful; otherwise 1.  */
2397b725ae77Skettenis 
2398b725ae77Skettenis static int
common_breakpoint(int set,CORE_ADDR addr,int len,enum break_type type)2399b725ae77Skettenis common_breakpoint (int set, CORE_ADDR addr, int len, enum break_type type)
2400b725ae77Skettenis {
2401e93f7393Sniklas   char buf[DATA_MAXLEN + 1];
2402b725ae77Skettenis   char cmd, rcmd;
2403b725ae77Skettenis   int rpid, rerrflg, rresponse, rlen;
2404e93f7393Sniklas   int nfields;
2405e93f7393Sniklas 
2406b725ae77Skettenis   addr = ADDR_BITS_REMOVE (addr);
2407b725ae77Skettenis 
2408b725ae77Skettenis   if (mips_monitor == MON_LSI)
2409b725ae77Skettenis     {
2410b725ae77Skettenis       if (set == 0)		/* clear breakpoint */
2411b725ae77Skettenis 	{
2412b725ae77Skettenis 	  /* The LSI PMON "clear breakpoint" has this form:
2413b725ae77Skettenis 	     <pid> 'b' <bptn> 0x0
2414b725ae77Skettenis 	     reply:
2415b725ae77Skettenis 	     <pid> 'b' 0x0 <code>
2416b725ae77Skettenis 
2417b725ae77Skettenis 	     <bptn> is a breakpoint number returned by an earlier 'B' command.
2418b725ae77Skettenis 	     Possible return codes: OK, E_BPT.  */
2419b725ae77Skettenis 
2420b725ae77Skettenis 	  int i;
2421b725ae77Skettenis 
2422b725ae77Skettenis 	  /* Search for the breakpoint in the table.  */
2423b725ae77Skettenis 	  for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
2424b725ae77Skettenis 	    if (lsi_breakpoints[i].type == type
2425b725ae77Skettenis 		&& lsi_breakpoints[i].addr == addr
2426b725ae77Skettenis 		&& lsi_breakpoints[i].len == len)
2427b725ae77Skettenis 	      break;
2428b725ae77Skettenis 
2429b725ae77Skettenis 	  /* Clear the table entry and tell PMON to clear the breakpoint.  */
2430b725ae77Skettenis 	  if (i == MAX_LSI_BREAKPOINTS)
2431b725ae77Skettenis 	    {
2432b725ae77Skettenis 	      warning ("common_breakpoint: Attempt to clear bogus breakpoint at %s\n",
2433b725ae77Skettenis 		       paddr_nz (addr));
2434b725ae77Skettenis 	      return 1;
2435b725ae77Skettenis 	    }
2436b725ae77Skettenis 
2437b725ae77Skettenis 	  lsi_breakpoints[i].type = BREAK_UNUSED;
2438b725ae77Skettenis 	  sprintf (buf, "0x0 b 0x%x 0x0", i);
2439b725ae77Skettenis 	  mips_send_packet (buf, 1);
2440b725ae77Skettenis 
2441b725ae77Skettenis 	  rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2442b725ae77Skettenis 	  buf[rlen] = '\0';
2443b725ae77Skettenis 
2444b725ae77Skettenis 	  nfields = sscanf (buf, "0x%x b 0x0 0x%x", &rpid, &rerrflg);
2445b725ae77Skettenis 	  if (nfields != 2)
2446b725ae77Skettenis 	    mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2447b725ae77Skettenis 
2448b725ae77Skettenis 	  return (check_lsi_error (addr, rerrflg));
2449b725ae77Skettenis 	}
2450e93f7393Sniklas       else
2451b725ae77Skettenis 	/* set a breakpoint */
2452b725ae77Skettenis 	{
2453b725ae77Skettenis 	  /* The LSI PMON "set breakpoint" command has this form:
2454b725ae77Skettenis 	     <pid> 'B' <addr> 0x0
2455b725ae77Skettenis 	     reply:
2456b725ae77Skettenis 	     <pid> 'B' <bptn> <code>
2457b725ae77Skettenis 
2458b725ae77Skettenis 	     The "set data breakpoint" command has this form:
2459b725ae77Skettenis 
2460b725ae77Skettenis 	     <pid> 'A' <addr1> <type> [<addr2>  [<value>]]
2461b725ae77Skettenis 
2462b725ae77Skettenis 	     where: type= "0x1" = read
2463b725ae77Skettenis 	     "0x2" = write
2464b725ae77Skettenis 	     "0x3" = access (read or write)
2465b725ae77Skettenis 
2466b725ae77Skettenis 	     The reply returns two values:
2467b725ae77Skettenis 	     bptn - a breakpoint number, which is a small integer with
2468b725ae77Skettenis 	     possible values of zero through 255.
2469b725ae77Skettenis 	     code - an error return code, a value of zero indicates a
2470b725ae77Skettenis 	     succesful completion, other values indicate various
2471b725ae77Skettenis 	     errors and warnings.
2472b725ae77Skettenis 
2473b725ae77Skettenis 	     Possible return codes: OK, W_QAL, E_QAL, E_OUT, E_NON.
2474b725ae77Skettenis 
2475b725ae77Skettenis 	   */
2476b725ae77Skettenis 
2477b725ae77Skettenis 	  if (type == BREAK_FETCH)	/* instruction breakpoint */
2478b725ae77Skettenis 	    {
2479b725ae77Skettenis 	      cmd = 'B';
2480b725ae77Skettenis 	      sprintf (buf, "0x0 B 0x%s 0x0", paddr_nz (addr));
2481b725ae77Skettenis 	    }
2482b725ae77Skettenis 	  else
2483b725ae77Skettenis 	    /* watchpoint */
2484b725ae77Skettenis 	    {
2485b725ae77Skettenis 	      cmd = 'A';
2486b725ae77Skettenis 	      sprintf (buf, "0x0 A 0x%s 0x%x 0x%s", paddr_nz (addr),
2487b725ae77Skettenis 		     type == BREAK_READ ? 1 : (type == BREAK_WRITE ? 2 : 3),
2488b725ae77Skettenis 		       paddr_nz (addr + len - 1));
2489b725ae77Skettenis 	    }
2490b725ae77Skettenis 	  mips_send_packet (buf, 1);
2491b725ae77Skettenis 
2492b725ae77Skettenis 	  rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2493b725ae77Skettenis 	  buf[rlen] = '\0';
2494b725ae77Skettenis 
2495b725ae77Skettenis 	  nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2496b725ae77Skettenis 			    &rpid, &rcmd, &rresponse, &rerrflg);
2497b725ae77Skettenis 	  if (nfields != 4 || rcmd != cmd || rresponse > 255)
2498b725ae77Skettenis 	    mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2499b725ae77Skettenis 
2500b725ae77Skettenis 	  if (rerrflg != 0)
2501b725ae77Skettenis 	    if (check_lsi_error (addr, rerrflg))
2502b725ae77Skettenis 	      return 1;
2503b725ae77Skettenis 
2504b725ae77Skettenis 	  /* rresponse contains PMON's breakpoint number.  Record the
2505b725ae77Skettenis 	     information for this breakpoint so we can clear it later.  */
2506b725ae77Skettenis 	  lsi_breakpoints[rresponse].type = type;
2507b725ae77Skettenis 	  lsi_breakpoints[rresponse].addr = addr;
2508b725ae77Skettenis 	  lsi_breakpoints[rresponse].len = len;
2509b725ae77Skettenis 
2510b725ae77Skettenis 	  return 0;
2511b725ae77Skettenis 	}
2512b725ae77Skettenis     }
2513b725ae77Skettenis   else
2514b725ae77Skettenis     {
2515b725ae77Skettenis       /* On non-LSI targets, the breakpoint command has this form:
2516b725ae77Skettenis          0x0 <CMD> <ADDR> <MASK> <FLAGS>
2517b725ae77Skettenis          <MASK> is a don't care mask for addresses.
2518b725ae77Skettenis          <FLAGS> is any combination of `r', `w', or `f' for read/write/fetch.
2519b725ae77Skettenis        */
2520b725ae77Skettenis       unsigned long mask;
2521b725ae77Skettenis 
2522b725ae77Skettenis       mask = calculate_mask (addr, len);
2523b725ae77Skettenis       addr &= ~mask;
2524b725ae77Skettenis 
2525b725ae77Skettenis       if (set)			/* set a breakpoint */
2526b725ae77Skettenis 	{
2527b725ae77Skettenis 	  char *flags;
2528b725ae77Skettenis 	  switch (type)
2529b725ae77Skettenis 	    {
2530b725ae77Skettenis 	    case BREAK_WRITE:	/* write */
2531b725ae77Skettenis 	      flags = "w";
2532b725ae77Skettenis 	      break;
2533b725ae77Skettenis 	    case BREAK_READ:	/* read */
2534b725ae77Skettenis 	      flags = "r";
2535b725ae77Skettenis 	      break;
2536b725ae77Skettenis 	    case BREAK_ACCESS:	/* read/write */
2537b725ae77Skettenis 	      flags = "rw";
2538b725ae77Skettenis 	      break;
2539b725ae77Skettenis 	    case BREAK_FETCH:	/* fetch */
2540b725ae77Skettenis 	      flags = "f";
2541b725ae77Skettenis 	      break;
2542b725ae77Skettenis 	    default:
2543b725ae77Skettenis 	      internal_error (__FILE__, __LINE__, "failed internal consistency check");
2544b725ae77Skettenis 	    }
2545b725ae77Skettenis 
2546b725ae77Skettenis 	  cmd = 'B';
2547b725ae77Skettenis 	  sprintf (buf, "0x0 B 0x%s 0x%s %s", paddr_nz (addr),
2548b725ae77Skettenis 		   paddr_nz (mask), flags);
2549b725ae77Skettenis 	}
2550b725ae77Skettenis       else
2551b725ae77Skettenis 	{
2552b725ae77Skettenis 	  cmd = 'b';
2553b725ae77Skettenis 	  sprintf (buf, "0x0 b 0x%s", paddr_nz (addr));
2554b725ae77Skettenis 	}
2555e93f7393Sniklas 
2556e93f7393Sniklas       mips_send_packet (buf, 1);
2557e93f7393Sniklas 
2558b725ae77Skettenis       rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2559b725ae77Skettenis       buf[rlen] = '\0';
2560e93f7393Sniklas 
2561b725ae77Skettenis       nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2562b725ae77Skettenis 			&rpid, &rcmd, &rerrflg, &rresponse);
2563e93f7393Sniklas 
2564b725ae77Skettenis       if (nfields != 4 || rcmd != cmd)
2565b725ae77Skettenis 	mips_error ("common_breakpoint: Bad response from remote board: %s",
2566b725ae77Skettenis 		    buf);
2567e93f7393Sniklas 
2568e93f7393Sniklas       if (rerrflg != 0)
2569e93f7393Sniklas 	{
2570e93f7393Sniklas 	  /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2571e93f7393Sniklas 	     Cogent returns "0x0 b 0xffffffff 0x16\000": */
2572e93f7393Sniklas 	  if (mips_monitor == MON_DDB)
2573e93f7393Sniklas 	    rresponse = rerrflg;
2574e93f7393Sniklas 	  if (rresponse != 22)	/* invalid argument */
2575b725ae77Skettenis 	    fprintf_unfiltered (gdb_stderr,
2576b725ae77Skettenis 			     "common_breakpoint (0x%s):  Got error: 0x%x\n",
2577b725ae77Skettenis 				paddr_nz (addr), rresponse);
2578e93f7393Sniklas 	  return 1;
2579e93f7393Sniklas 	}
2580b725ae77Skettenis     }
2581e93f7393Sniklas   return 0;
2582e93f7393Sniklas }
2583e93f7393Sniklas 
2584e93f7393Sniklas static void
send_srec(char * srec,int len,CORE_ADDR addr)2585b725ae77Skettenis send_srec (char *srec, int len, CORE_ADDR addr)
2586e93f7393Sniklas {
2587e93f7393Sniklas   while (1)
2588e93f7393Sniklas     {
2589e93f7393Sniklas       int ch;
2590e93f7393Sniklas 
2591b725ae77Skettenis       serial_write (mips_desc, srec, len);
2592e93f7393Sniklas 
2593b725ae77Skettenis       ch = mips_readchar (remote_timeout);
2594e93f7393Sniklas 
2595e93f7393Sniklas       switch (ch)
2596e93f7393Sniklas 	{
2597e93f7393Sniklas 	case SERIAL_TIMEOUT:
2598e93f7393Sniklas 	  error ("Timeout during download.");
2599e93f7393Sniklas 	  break;
2600e93f7393Sniklas 	case 0x6:		/* ACK */
2601e93f7393Sniklas 	  return;
2602e93f7393Sniklas 	case 0x15:		/* NACK */
2603b725ae77Skettenis 	  fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %s!  Retrying.\n", paddr_u (addr));
2604e93f7393Sniklas 	  continue;
2605e93f7393Sniklas 	default:
2606e93f7393Sniklas 	  error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2607e93f7393Sniklas 	}
2608e93f7393Sniklas     }
2609e93f7393Sniklas }
2610e93f7393Sniklas 
2611e93f7393Sniklas /*  Download a binary file by converting it to S records. */
2612e93f7393Sniklas 
2613e93f7393Sniklas static void
mips_load_srec(char * args)2614b725ae77Skettenis mips_load_srec (char *args)
2615e93f7393Sniklas {
2616e93f7393Sniklas   bfd *abfd;
2617e93f7393Sniklas   asection *s;
2618e93f7393Sniklas   char *buffer, srec[1024];
2619b725ae77Skettenis   unsigned int i;
2620b725ae77Skettenis   unsigned int srec_frame = 200;
2621e93f7393Sniklas   int reclen;
2622e93f7393Sniklas   static int hashmark = 1;
2623e93f7393Sniklas 
2624e93f7393Sniklas   buffer = alloca (srec_frame * 2 + 256);
2625e93f7393Sniklas 
2626e93f7393Sniklas   abfd = bfd_openr (args, 0);
2627e93f7393Sniklas   if (!abfd)
2628e93f7393Sniklas     {
2629e93f7393Sniklas       printf_filtered ("Unable to open file %s\n", args);
2630e93f7393Sniklas       return;
2631e93f7393Sniklas     }
2632e93f7393Sniklas 
2633e93f7393Sniklas   if (bfd_check_format (abfd, bfd_object) == 0)
2634e93f7393Sniklas     {
2635e93f7393Sniklas       printf_filtered ("File is not an object file\n");
2636e93f7393Sniklas       return;
2637e93f7393Sniklas     }
2638e93f7393Sniklas 
2639e93f7393Sniklas /* This actually causes a download in the IDT binary format: */
2640e93f7393Sniklas   mips_send_command (LOAD_CMD, 0);
2641e93f7393Sniklas 
2642e93f7393Sniklas   for (s = abfd->sections; s; s = s->next)
2643e93f7393Sniklas     {
2644e93f7393Sniklas       if (s->flags & SEC_LOAD)
2645e93f7393Sniklas 	{
2646b725ae77Skettenis 	  unsigned int numbytes;
2647e93f7393Sniklas 
2648b725ae77Skettenis 	  /* FIXME!  vma too small????? */
2649b725ae77Skettenis 	  printf_filtered ("%s\t: 0x%4lx .. 0x%4lx  ", s->name,
2650b725ae77Skettenis 			   (long) s->vma,
2651*63addd46Skettenis 			   (long) (s->vma + bfd_get_section_size (s)));
2652e93f7393Sniklas 	  gdb_flush (gdb_stdout);
2653e93f7393Sniklas 
2654*63addd46Skettenis 	  for (i = 0; i < bfd_get_section_size (s); i += numbytes)
2655e93f7393Sniklas 	    {
2656*63addd46Skettenis 	      numbytes = min (srec_frame, bfd_get_section_size (s) - i);
2657e93f7393Sniklas 
2658e93f7393Sniklas 	      bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2659e93f7393Sniklas 
2660e93f7393Sniklas 	      reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2661e93f7393Sniklas 	      send_srec (srec, reclen, s->vma + i);
2662e93f7393Sniklas 
2663*63addd46Skettenis 	      if (deprecated_ui_load_progress_hook)
2664*63addd46Skettenis 		deprecated_ui_load_progress_hook (s->name, i);
2665b725ae77Skettenis 
2666e93f7393Sniklas 	      if (hashmark)
2667e93f7393Sniklas 		{
2668e93f7393Sniklas 		  putchar_unfiltered ('#');
2669e93f7393Sniklas 		  gdb_flush (gdb_stdout);
2670e93f7393Sniklas 		}
2671e93f7393Sniklas 
2672e93f7393Sniklas 	    }			/* Per-packet (or S-record) loop */
2673e93f7393Sniklas 
2674e93f7393Sniklas 	  putchar_unfiltered ('\n');
2675e93f7393Sniklas 	}			/* Loadable sections */
2676e93f7393Sniklas     }
2677e93f7393Sniklas   if (hashmark)
2678e93f7393Sniklas     putchar_unfiltered ('\n');
2679e93f7393Sniklas 
2680e93f7393Sniklas   /* Write a type 7 terminator record. no data for a type 7, and there
2681e93f7393Sniklas      is no data, so len is 0.  */
2682e93f7393Sniklas 
2683e93f7393Sniklas   reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2684e93f7393Sniklas 
2685e93f7393Sniklas   send_srec (srec, reclen, abfd->start_address);
2686e93f7393Sniklas 
2687b725ae77Skettenis   serial_flush_input (mips_desc);
2688e93f7393Sniklas }
2689e93f7393Sniklas 
2690e93f7393Sniklas /*
2691e93f7393Sniklas  * mips_make_srec -- make an srecord. This writes each line, one at a
2692e93f7393Sniklas  *      time, each with it's own header and trailer line.
2693e93f7393Sniklas  *      An srecord looks like this:
2694e93f7393Sniklas  *
2695e93f7393Sniklas  * byte count-+     address
2696e93f7393Sniklas  * start ---+ |        |       data        +- checksum
2697e93f7393Sniklas  *          | |        |                   |
2698e93f7393Sniklas  *        S01000006F6B692D746573742E73726563E4
2699e93f7393Sniklas  *        S315000448600000000000000000FC00005900000000E9
2700e93f7393Sniklas  *        S31A0004000023C1400037DE00F023604000377B009020825000348D
2701e93f7393Sniklas  *        S30B0004485A0000000000004E
2702e93f7393Sniklas  *        S70500040000F6
2703e93f7393Sniklas  *
2704e93f7393Sniklas  *      S<type><length><address><data><checksum>
2705e93f7393Sniklas  *
2706e93f7393Sniklas  *      Where
2707e93f7393Sniklas  *      - length
2708e93f7393Sniklas  *        is the number of bytes following upto the checksum. Note that
2709e93f7393Sniklas  *        this is not the number of chars following, since it takes two
2710e93f7393Sniklas  *        chars to represent a byte.
2711e93f7393Sniklas  *      - type
2712e93f7393Sniklas  *        is one of:
2713e93f7393Sniklas  *        0) header record
2714e93f7393Sniklas  *        1) two byte address data record
2715e93f7393Sniklas  *        2) three byte address data record
2716e93f7393Sniklas  *        3) four byte address data record
2717e93f7393Sniklas  *        7) four byte address termination record
2718e93f7393Sniklas  *        8) three byte address termination record
2719e93f7393Sniklas  *        9) two byte address termination record
2720e93f7393Sniklas  *
2721e93f7393Sniklas  *      - address
2722e93f7393Sniklas  *        is the start address of the data following, or in the case of
2723e93f7393Sniklas  *        a termination record, the start address of the image
2724e93f7393Sniklas  *      - data
2725e93f7393Sniklas  *        is the data.
2726e93f7393Sniklas  *      - checksum
2727e93f7393Sniklas  *        is the sum of all the raw byte data in the record, from the length
2728e93f7393Sniklas  *        upwards, modulo 256 and subtracted from 255.
2729e93f7393Sniklas  *
2730e93f7393Sniklas  * This routine returns the length of the S-record.
2731e93f7393Sniklas  *
2732e93f7393Sniklas  */
2733e93f7393Sniklas 
2734e93f7393Sniklas static int
mips_make_srec(char * buf,int type,CORE_ADDR memaddr,unsigned char * myaddr,int len)2735b725ae77Skettenis mips_make_srec (char *buf, int type, CORE_ADDR memaddr, unsigned char *myaddr,
2736b725ae77Skettenis 		int len)
2737e93f7393Sniklas {
2738e93f7393Sniklas   unsigned char checksum;
2739e93f7393Sniklas   int i;
2740e93f7393Sniklas 
2741e93f7393Sniklas   /* Create the header for the srec. addr_size is the number of bytes in the address,
2742e93f7393Sniklas      and 1 is the number of bytes in the count.  */
2743e93f7393Sniklas 
2744e93f7393Sniklas   /* FIXME!! bigger buf required for 64-bit! */
2745e93f7393Sniklas   buf[0] = 'S';
2746e93f7393Sniklas   buf[1] = type;
2747e93f7393Sniklas   buf[2] = len + 4 + 1;		/* len + 4 byte address + 1 byte checksum */
2748e93f7393Sniklas   /* This assumes S3 style downloads (4byte addresses). There should
2749e93f7393Sniklas      probably be a check, or the code changed to make it more
2750e93f7393Sniklas      explicit. */
2751e93f7393Sniklas   buf[3] = memaddr >> 24;
2752e93f7393Sniklas   buf[4] = memaddr >> 16;
2753e93f7393Sniklas   buf[5] = memaddr >> 8;
2754e93f7393Sniklas   buf[6] = memaddr;
2755e93f7393Sniklas   memcpy (&buf[7], myaddr, len);
2756e93f7393Sniklas 
2757e93f7393Sniklas   /* Note that the checksum is calculated on the raw data, not the
2758e93f7393Sniklas      hexified data.  It includes the length, address and the data
2759e93f7393Sniklas      portions of the packet.  */
2760e93f7393Sniklas   checksum = 0;
2761e93f7393Sniklas   buf += 2;			/* Point at length byte */
2762e93f7393Sniklas   for (i = 0; i < len + 4 + 1; i++)
2763e93f7393Sniklas     checksum += *buf++;
2764e93f7393Sniklas 
2765e93f7393Sniklas   *buf = ~checksum;
2766e93f7393Sniklas 
2767e93f7393Sniklas   return len + 8;
2768e93f7393Sniklas }
2769e93f7393Sniklas 
2770e93f7393Sniklas /* The following manifest controls whether we enable the simple flow
2771e93f7393Sniklas    control support provided by the monitor. If enabled the code will
2772e93f7393Sniklas    wait for an affirmative ACK between transmitting packets. */
2773e93f7393Sniklas #define DOETXACK (1)
2774e93f7393Sniklas 
2775e93f7393Sniklas /* The PMON fast-download uses an encoded packet format constructed of
2776e93f7393Sniklas    3byte data packets (encoded as 4 printable ASCII characters), and
2777e93f7393Sniklas    escape sequences (preceded by a '/'):
2778e93f7393Sniklas 
2779e93f7393Sniklas    'K'     clear checksum
2780e93f7393Sniklas    'C'     compare checksum (12bit value, not included in checksum calculation)
2781e93f7393Sniklas    'S'     define symbol name (for addr) terminated with "," and padded to 4char boundary
2782e93f7393Sniklas    'Z'     zero fill multiple of 3bytes
2783e93f7393Sniklas    'B'     byte (12bit encoded value, of 8bit data)
2784e93f7393Sniklas    'A'     address (36bit encoded value)
2785e93f7393Sniklas    'E'     define entry as original address, and exit load
2786e93f7393Sniklas 
2787e93f7393Sniklas    The packets are processed in 4 character chunks, so the escape
2788e93f7393Sniklas    sequences that do not have any data (or variable length data)
2789e93f7393Sniklas    should be padded to a 4 character boundary.  The decoder will give
2790e93f7393Sniklas    an error if the complete message block size is not a multiple of
2791e93f7393Sniklas    4bytes (size of record).
2792e93f7393Sniklas 
2793e93f7393Sniklas    The encoding of numbers is done in 6bit fields.  The 6bit value is
2794e93f7393Sniklas    used to index into this string to get the specific character
2795e93f7393Sniklas    encoding for the value: */
2796e93f7393Sniklas static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
2797e93f7393Sniklas 
2798e93f7393Sniklas /* Convert the number of bits required into an encoded number, 6bits
2799e93f7393Sniklas    at a time (range 0..63).  Keep a checksum if required (passed
2800e93f7393Sniklas    pointer non-NULL). The function returns the number of encoded
2801e93f7393Sniklas    characters written into the buffer. */
2802e93f7393Sniklas static int
pmon_makeb64(unsigned long v,char * p,int n,int * chksum)2803b725ae77Skettenis pmon_makeb64 (unsigned long v, char *p, int n, int *chksum)
2804e93f7393Sniklas {
2805e93f7393Sniklas   int count = (n / 6);
2806e93f7393Sniklas 
2807b725ae77Skettenis   if ((n % 12) != 0)
2808b725ae77Skettenis     {
2809b725ae77Skettenis       fprintf_unfiltered (gdb_stderr,
2810b725ae77Skettenis 			  "Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n", n, (n == 1) ? "" : "s");
2811e93f7393Sniklas       return (0);
2812e93f7393Sniklas     }
2813b725ae77Skettenis   if (n > 36)
2814b725ae77Skettenis     {
2815b725ae77Skettenis       fprintf_unfiltered (gdb_stderr,
2816b725ae77Skettenis 			  "Fast encoding cannot process more than 36bits at the moment: %dbits\n", n);
2817e93f7393Sniklas       return (0);
2818e93f7393Sniklas     }
2819e93f7393Sniklas 
2820e93f7393Sniklas   /* Deal with the checksum: */
2821b725ae77Skettenis   if (chksum != NULL)
2822b725ae77Skettenis     {
2823b725ae77Skettenis       switch (n)
2824b725ae77Skettenis 	{
2825b725ae77Skettenis 	case 36:
2826b725ae77Skettenis 	  *chksum += ((v >> 24) & 0xFFF);
2827b725ae77Skettenis 	case 24:
2828b725ae77Skettenis 	  *chksum += ((v >> 12) & 0xFFF);
2829b725ae77Skettenis 	case 12:
2830b725ae77Skettenis 	  *chksum += ((v >> 0) & 0xFFF);
2831e93f7393Sniklas 	}
2832e93f7393Sniklas     }
2833e93f7393Sniklas 
2834b725ae77Skettenis   do
2835b725ae77Skettenis     {
2836e93f7393Sniklas       n -= 6;
2837e93f7393Sniklas       *p++ = encoding[(v >> n) & 0x3F];
2838b725ae77Skettenis     }
2839b725ae77Skettenis   while (n > 0);
2840e93f7393Sniklas 
2841e93f7393Sniklas   return (count);
2842e93f7393Sniklas }
2843e93f7393Sniklas 
2844e93f7393Sniklas /* Shorthand function (that could be in-lined) to output the zero-fill
2845e93f7393Sniklas    escape sequence into the data stream. */
2846e93f7393Sniklas static int
pmon_zeroset(int recsize,char ** buff,int * amount,unsigned int * chksum)2847b725ae77Skettenis pmon_zeroset (int recsize, char **buff, int *amount, unsigned int *chksum)
2848e93f7393Sniklas {
2849e93f7393Sniklas   int count;
2850e93f7393Sniklas 
2851e93f7393Sniklas   sprintf (*buff, "/Z");
2852e93f7393Sniklas   count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
2853e93f7393Sniklas   *buff += (count + 2);
2854e93f7393Sniklas   *amount = 0;
2855e93f7393Sniklas   return (recsize + count + 2);
2856e93f7393Sniklas }
2857e93f7393Sniklas 
2858e93f7393Sniklas static int
pmon_checkset(int recsize,char ** buff,int * value)2859b725ae77Skettenis pmon_checkset (int recsize, char **buff, int *value)
2860e93f7393Sniklas {
2861e93f7393Sniklas   int count;
2862e93f7393Sniklas 
2863e93f7393Sniklas   /* Add the checksum (without updating the value): */
2864e93f7393Sniklas   sprintf (*buff, "/C");
2865e93f7393Sniklas   count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
2866e93f7393Sniklas   *buff += (count + 2);
2867b725ae77Skettenis   sprintf (*buff, "\n");
2868e93f7393Sniklas   *buff += 2;			/* include zero terminator */
2869e93f7393Sniklas   /* Forcing a checksum validation clears the sum: */
2870e93f7393Sniklas   *value = 0;
2871e93f7393Sniklas   return (recsize + count + 3);
2872e93f7393Sniklas }
2873e93f7393Sniklas 
2874e93f7393Sniklas /* Amount of padding we leave after at the end of the output buffer,
2875e93f7393Sniklas    for the checksum and line termination characters: */
2876e93f7393Sniklas #define CHECKSIZE (4 + 4 + 4 + 2)
2877e93f7393Sniklas /* zero-fill, checksum, transfer end and line termination space. */
2878e93f7393Sniklas 
2879e93f7393Sniklas /* The amount of binary data loaded from the object file in a single
2880e93f7393Sniklas    operation: */
2881e93f7393Sniklas #define BINCHUNK (1024)
2882e93f7393Sniklas 
2883e93f7393Sniklas /* Maximum line of data accepted by the monitor: */
2884e93f7393Sniklas #define MAXRECSIZE (550)
2885e93f7393Sniklas /* NOTE: This constant depends on the monitor being used. This value
2886e93f7393Sniklas    is for PMON 5.x on the Cogent Vr4300 board. */
2887e93f7393Sniklas 
2888e93f7393Sniklas static void
pmon_make_fastrec(char ** outbuf,unsigned char * inbuf,int * inptr,int inamount,int * recsize,unsigned int * csum,unsigned int * zerofill)2889b725ae77Skettenis pmon_make_fastrec (char **outbuf, unsigned char *inbuf, int *inptr,
2890b725ae77Skettenis 		   int inamount, int *recsize, unsigned int *csum,
2891b725ae77Skettenis 		   unsigned int *zerofill)
2892e93f7393Sniklas {
2893e93f7393Sniklas   int count = 0;
2894e93f7393Sniklas   char *p = *outbuf;
2895e93f7393Sniklas 
2896e93f7393Sniklas   /* This is a simple check to ensure that our data will fit within
2897e93f7393Sniklas      the maximum allowable record size. Each record output is 4bytes
2898e93f7393Sniklas      in length. We must allow space for a pending zero fill command,
2899e93f7393Sniklas      the record, and a checksum record. */
2900b725ae77Skettenis   while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0))
2901b725ae77Skettenis     {
2902e93f7393Sniklas       /* Process the binary data: */
2903b725ae77Skettenis       if ((inamount - *inptr) < 3)
2904b725ae77Skettenis 	{
2905e93f7393Sniklas 	  if (*zerofill != 0)
2906e93f7393Sniklas 	    *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2907e93f7393Sniklas 	  sprintf (p, "/B");
2908e93f7393Sniklas 	  count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
2909e93f7393Sniklas 	  p += (2 + count);
2910e93f7393Sniklas 	  *recsize += (2 + count);
2911e93f7393Sniklas 	  (*inptr)++;
2912b725ae77Skettenis 	}
2913b725ae77Skettenis       else
2914b725ae77Skettenis 	{
2915e93f7393Sniklas 	  unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
2916e93f7393Sniklas 	  /* Simple check for zero data. TODO: A better check would be
2917e93f7393Sniklas 	     to check the last, and then the middle byte for being zero
2918e93f7393Sniklas 	     (if the first byte is not). We could then check for
2919e93f7393Sniklas 	     following runs of zeros, and if above a certain size it is
2920e93f7393Sniklas 	     worth the 4 or 8 character hit of the byte insertions used
2921e93f7393Sniklas 	     to pad to the start of the zeroes. NOTE: This also depends
2922e93f7393Sniklas 	     on the alignment at the end of the zero run. */
2923b725ae77Skettenis 	  if (value == 0x00000000)
2924b725ae77Skettenis 	    {
2925e93f7393Sniklas 	      (*zerofill)++;
2926e93f7393Sniklas 	      if (*zerofill == 0xFFF)	/* 12bit counter */
2927e93f7393Sniklas 		*recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2928b725ae77Skettenis 	    }
2929b725ae77Skettenis 	  else
2930b725ae77Skettenis 	    {
2931e93f7393Sniklas 	      if (*zerofill != 0)
2932e93f7393Sniklas 		*recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2933e93f7393Sniklas 	      count = pmon_makeb64 (value, p, 24, csum);
2934e93f7393Sniklas 	      p += count;
2935e93f7393Sniklas 	      *recsize += count;
2936e93f7393Sniklas 	    }
2937e93f7393Sniklas 	  *inptr += 3;
2938e93f7393Sniklas 	}
2939e93f7393Sniklas     }
2940e93f7393Sniklas 
2941e93f7393Sniklas   *outbuf = p;
2942e93f7393Sniklas   return;
2943e93f7393Sniklas }
2944e93f7393Sniklas 
2945e93f7393Sniklas static int
pmon_check_ack(char * mesg)2946b725ae77Skettenis pmon_check_ack (char *mesg)
2947e93f7393Sniklas {
2948b725ae77Skettenis #if defined(DOETXACK)
2949b725ae77Skettenis   int c;
2950b725ae77Skettenis 
2951b725ae77Skettenis   if (!tftp_in_use)
2952b725ae77Skettenis     {
2953b725ae77Skettenis       c = serial_readchar (udp_in_use ? udp_desc : mips_desc,
2954b725ae77Skettenis 			   remote_timeout);
2955b725ae77Skettenis       if ((c == SERIAL_TIMEOUT) || (c != 0x06))
2956b725ae77Skettenis 	{
2957b725ae77Skettenis 	  fprintf_unfiltered (gdb_stderr,
2958b725ae77Skettenis 			      "Failed to receive valid ACK for %s\n", mesg);
2959e93f7393Sniklas 	  return (-1);		/* terminate the download */
2960e93f7393Sniklas 	}
2961e93f7393Sniklas     }
2962e93f7393Sniklas #endif /* DOETXACK */
2963b725ae77Skettenis   return (0);
2964b725ae77Skettenis }
2965b725ae77Skettenis 
2966b725ae77Skettenis /* pmon_download - Send a sequence of characters to the PMON download port,
2967b725ae77Skettenis    which is either a serial port or a UDP socket.  */
2968e93f7393Sniklas 
2969e93f7393Sniklas static void
pmon_start_download(void)2970b725ae77Skettenis pmon_start_download (void)
2971b725ae77Skettenis {
2972b725ae77Skettenis   if (tftp_in_use)
2973b725ae77Skettenis     {
2974b725ae77Skettenis       /* Create the temporary download file.  */
2975b725ae77Skettenis       if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
2976b725ae77Skettenis 	perror_with_name (tftp_localname);
2977b725ae77Skettenis     }
2978b725ae77Skettenis   else
2979b725ae77Skettenis     {
2980b725ae77Skettenis       mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
2981b725ae77Skettenis       mips_expect ("Downloading from ");
2982b725ae77Skettenis       mips_expect (udp_in_use ? "udp" : "tty0");
2983b725ae77Skettenis       mips_expect (", ^C to abort\r\n");
2984b725ae77Skettenis     }
2985b725ae77Skettenis }
2986b725ae77Skettenis 
2987b725ae77Skettenis static int
mips_expect_download(char * string)2988b725ae77Skettenis mips_expect_download (char *string)
2989b725ae77Skettenis {
2990b725ae77Skettenis   if (!mips_expect (string))
2991b725ae77Skettenis     {
2992b725ae77Skettenis       fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
2993b725ae77Skettenis       if (tftp_in_use)
2994b725ae77Skettenis 	remove (tftp_localname);	/* Remove temporary file */
2995b725ae77Skettenis       return 0;
2996b725ae77Skettenis     }
2997b725ae77Skettenis   else
2998b725ae77Skettenis     return 1;
2999b725ae77Skettenis }
3000b725ae77Skettenis 
3001b725ae77Skettenis static void
pmon_check_entry_address(char * entry_address,int final)3002b725ae77Skettenis pmon_check_entry_address (char *entry_address, int final)
3003b725ae77Skettenis {
3004b725ae77Skettenis   char hexnumber[9];		/* includes '\0' space */
3005b725ae77Skettenis   mips_expect_timeout (entry_address, tftp_in_use ? 15 : remote_timeout);
3006b725ae77Skettenis   sprintf (hexnumber, "%x", final);
3007b725ae77Skettenis   mips_expect (hexnumber);
3008b725ae77Skettenis   mips_expect ("\r\n");
3009b725ae77Skettenis }
3010b725ae77Skettenis 
3011b725ae77Skettenis static int
pmon_check_total(int bintotal)3012b725ae77Skettenis pmon_check_total (int bintotal)
3013b725ae77Skettenis {
3014b725ae77Skettenis   char hexnumber[9];		/* includes '\0' space */
3015b725ae77Skettenis   mips_expect ("\r\ntotal = 0x");
3016b725ae77Skettenis   sprintf (hexnumber, "%x", bintotal);
3017b725ae77Skettenis   mips_expect (hexnumber);
3018b725ae77Skettenis   return mips_expect_download (" bytes\r\n");
3019b725ae77Skettenis }
3020b725ae77Skettenis 
3021b725ae77Skettenis static void
pmon_end_download(int final,int bintotal)3022b725ae77Skettenis pmon_end_download (int final, int bintotal)
3023b725ae77Skettenis {
3024b725ae77Skettenis   char hexnumber[9];		/* includes '\0' space */
3025b725ae77Skettenis 
3026b725ae77Skettenis   if (tftp_in_use)
3027b725ae77Skettenis     {
3028b725ae77Skettenis       static char *load_cmd_prefix = "load -b -s ";
3029b725ae77Skettenis       char *cmd;
3030b725ae77Skettenis       struct stat stbuf;
3031b725ae77Skettenis 
3032b725ae77Skettenis       /* Close off the temporary file containing the load data.  */
3033b725ae77Skettenis       fclose (tftp_file);
3034b725ae77Skettenis       tftp_file = NULL;
3035b725ae77Skettenis 
3036b725ae77Skettenis       /* Make the temporary file readable by the world.  */
3037b725ae77Skettenis       if (stat (tftp_localname, &stbuf) == 0)
3038b725ae77Skettenis 	chmod (tftp_localname, stbuf.st_mode | S_IROTH);
3039b725ae77Skettenis 
3040b725ae77Skettenis       /* Must reinitialize the board to prevent PMON from crashing.  */
3041b725ae77Skettenis       mips_send_command ("initEther\r", -1);
3042b725ae77Skettenis 
3043b725ae77Skettenis       /* Send the load command.  */
3044b725ae77Skettenis       cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
3045b725ae77Skettenis       strcpy (cmd, load_cmd_prefix);
3046b725ae77Skettenis       strcat (cmd, tftp_name);
3047b725ae77Skettenis       strcat (cmd, "\r");
3048b725ae77Skettenis       mips_send_command (cmd, 0);
3049b725ae77Skettenis       xfree (cmd);
3050b725ae77Skettenis       if (!mips_expect_download ("Downloading from "))
3051b725ae77Skettenis 	return;
3052b725ae77Skettenis       if (!mips_expect_download (tftp_name))
3053b725ae77Skettenis 	return;
3054b725ae77Skettenis       if (!mips_expect_download (", ^C to abort\r\n"))
3055b725ae77Skettenis 	return;
3056b725ae77Skettenis     }
3057b725ae77Skettenis 
3058b725ae77Skettenis   /* Wait for the stuff that PMON prints after the load has completed.
3059b725ae77Skettenis      The timeout value for use in the tftp case (15 seconds) was picked
3060b725ae77Skettenis      arbitrarily but might be too small for really large downloads. FIXME. */
3061b725ae77Skettenis   switch (mips_monitor)
3062b725ae77Skettenis     {
3063b725ae77Skettenis     case MON_LSI:
3064b725ae77Skettenis       pmon_check_ack ("termination");
3065b725ae77Skettenis       pmon_check_entry_address ("Entry address is ", final);
3066b725ae77Skettenis       if (!pmon_check_total (bintotal))
3067b725ae77Skettenis 	return;
3068b725ae77Skettenis       break;
3069b725ae77Skettenis     default:
3070b725ae77Skettenis       pmon_check_entry_address ("Entry Address  = ", final);
3071b725ae77Skettenis       pmon_check_ack ("termination");
3072b725ae77Skettenis       if (!pmon_check_total (bintotal))
3073b725ae77Skettenis 	return;
3074b725ae77Skettenis       break;
3075b725ae77Skettenis     }
3076b725ae77Skettenis 
3077b725ae77Skettenis   if (tftp_in_use)
3078b725ae77Skettenis     remove (tftp_localname);	/* Remove temporary file */
3079b725ae77Skettenis }
3080b725ae77Skettenis 
3081b725ae77Skettenis static void
pmon_download(char * buffer,int length)3082b725ae77Skettenis pmon_download (char *buffer, int length)
3083b725ae77Skettenis {
3084b725ae77Skettenis   if (tftp_in_use)
3085b725ae77Skettenis     fwrite (buffer, 1, length, tftp_file);
3086b725ae77Skettenis   else
3087b725ae77Skettenis     serial_write (udp_in_use ? udp_desc : mips_desc, buffer, length);
3088b725ae77Skettenis }
3089b725ae77Skettenis 
3090b725ae77Skettenis static void
pmon_load_fast(char * file)3091b725ae77Skettenis pmon_load_fast (char *file)
3092e93f7393Sniklas {
3093e93f7393Sniklas   bfd *abfd;
3094e93f7393Sniklas   asection *s;
3095e93f7393Sniklas   unsigned char *binbuf;
3096e93f7393Sniklas   char *buffer;
3097e93f7393Sniklas   int reclen;
3098e93f7393Sniklas   unsigned int csum = 0;
3099b725ae77Skettenis   int hashmark = !tftp_in_use;
3100e93f7393Sniklas   int bintotal = 0;
3101b725ae77Skettenis   int final = 0;
3102e93f7393Sniklas   int finished = 0;
3103e93f7393Sniklas 
3104e93f7393Sniklas   buffer = (char *) xmalloc (MAXRECSIZE + 1);
3105e93f7393Sniklas   binbuf = (unsigned char *) xmalloc (BINCHUNK);
3106e93f7393Sniklas 
3107e93f7393Sniklas   abfd = bfd_openr (file, 0);
3108e93f7393Sniklas   if (!abfd)
3109e93f7393Sniklas     {
3110e93f7393Sniklas       printf_filtered ("Unable to open file %s\n", file);
3111e93f7393Sniklas       return;
3112e93f7393Sniklas     }
3113e93f7393Sniklas 
3114e93f7393Sniklas   if (bfd_check_format (abfd, bfd_object) == 0)
3115e93f7393Sniklas     {
3116e93f7393Sniklas       printf_filtered ("File is not an object file\n");
3117e93f7393Sniklas       return;
3118e93f7393Sniklas     }
3119e93f7393Sniklas 
3120e93f7393Sniklas   /* Setup the required download state: */
3121b725ae77Skettenis   mips_send_command ("set dlproto etxack\r", -1);
3122b725ae77Skettenis   mips_send_command ("set dlecho off\r", -1);
3123e93f7393Sniklas   /* NOTE: We get a "cannot set variable" message if the variable is
3124e93f7393Sniklas      already defined to have the argument we give. The code doesn't
3125e93f7393Sniklas      care, since it just scans to the next prompt anyway. */
3126e93f7393Sniklas   /* Start the download: */
3127b725ae77Skettenis   pmon_start_download ();
3128e93f7393Sniklas 
3129e93f7393Sniklas   /* Zero the checksum */
3130b725ae77Skettenis   sprintf (buffer, "/Kxx\n");
3131e93f7393Sniklas   reclen = strlen (buffer);
3132b725ae77Skettenis   pmon_download (buffer, reclen);
3133b725ae77Skettenis   finished = pmon_check_ack ("/Kxx");
3134e93f7393Sniklas 
3135e93f7393Sniklas   for (s = abfd->sections; s && !finished; s = s->next)
3136e93f7393Sniklas     if (s->flags & SEC_LOAD)	/* only deal with loadable sections */
3137e93f7393Sniklas       {
3138*63addd46Skettenis 	bintotal += bfd_get_section_size (s);
3139*63addd46Skettenis 	final = (s->vma + bfd_get_section_size (s));
3140e93f7393Sniklas 
3141e93f7393Sniklas 	printf_filtered ("%s\t: 0x%4x .. 0x%4x  ", s->name, (unsigned int) s->vma,
3142*63addd46Skettenis 			 (unsigned int) (s->vma + bfd_get_section_size (s)));
3143e93f7393Sniklas 	gdb_flush (gdb_stdout);
3144e93f7393Sniklas 
3145e93f7393Sniklas 	/* Output the starting address */
3146e93f7393Sniklas 	sprintf (buffer, "/A");
3147e93f7393Sniklas 	reclen = pmon_makeb64 (s->vma, &buffer[2], 36, &csum);
3148b725ae77Skettenis 	buffer[2 + reclen] = '\n';
3149e93f7393Sniklas 	buffer[3 + reclen] = '\0';
3150e93f7393Sniklas 	reclen += 3;		/* for the initial escape code and carriage return */
3151b725ae77Skettenis 	pmon_download (buffer, reclen);
3152b725ae77Skettenis 	finished = pmon_check_ack ("/A");
3153e93f7393Sniklas 
3154e93f7393Sniklas 	if (!finished)
3155e93f7393Sniklas 	  {
3156b725ae77Skettenis 	    unsigned int binamount;
3157e93f7393Sniklas 	    unsigned int zerofill = 0;
3158e93f7393Sniklas 	    char *bp = buffer;
3159b725ae77Skettenis 	    unsigned int i;
3160e93f7393Sniklas 
3161e93f7393Sniklas 	    reclen = 0;
3162e93f7393Sniklas 
3163*63addd46Skettenis 	    for (i = 0;
3164*63addd46Skettenis 		 i < bfd_get_section_size (s) && !finished;
3165*63addd46Skettenis 		 i += binamount)
3166b725ae77Skettenis 	      {
3167e93f7393Sniklas 		int binptr = 0;
3168e93f7393Sniklas 
3169*63addd46Skettenis 		binamount = min (BINCHUNK, bfd_get_section_size (s) - i);
3170e93f7393Sniklas 
3171e93f7393Sniklas 		bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3172e93f7393Sniklas 
3173e93f7393Sniklas 		/* This keeps a rolling checksum, until we decide to output
3174e93f7393Sniklas 		   the line: */
3175b725ae77Skettenis 		for (; ((binamount - binptr) > 0);)
3176b725ae77Skettenis 		  {
3177e93f7393Sniklas 		    pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3178b725ae77Skettenis 		    if (reclen >= (MAXRECSIZE - CHECKSIZE))
3179b725ae77Skettenis 		      {
3180e93f7393Sniklas 			reclen = pmon_checkset (reclen, &bp, &csum);
3181b725ae77Skettenis 			pmon_download (buffer, reclen);
3182b725ae77Skettenis 			finished = pmon_check_ack ("data record");
3183b725ae77Skettenis 			if (finished)
3184b725ae77Skettenis 			  {
3185e93f7393Sniklas 			    zerofill = 0;	/* do not transmit pending zerofills */
3186e93f7393Sniklas 			    break;
3187e93f7393Sniklas 			  }
3188e93f7393Sniklas 
3189*63addd46Skettenis 			if (deprecated_ui_load_progress_hook)
3190*63addd46Skettenis 			  deprecated_ui_load_progress_hook (s->name, i);
3191b725ae77Skettenis 
3192b725ae77Skettenis 			if (hashmark)
3193b725ae77Skettenis 			  {
3194e93f7393Sniklas 			    putchar_unfiltered ('#');
3195e93f7393Sniklas 			    gdb_flush (gdb_stdout);
3196e93f7393Sniklas 			  }
3197e93f7393Sniklas 
3198e93f7393Sniklas 			bp = buffer;
3199e93f7393Sniklas 			reclen = 0;	/* buffer processed */
3200e93f7393Sniklas 		      }
3201e93f7393Sniklas 		  }
3202e93f7393Sniklas 	      }
3203e93f7393Sniklas 
3204e93f7393Sniklas 	    /* Ensure no out-standing zerofill requests: */
3205e93f7393Sniklas 	    if (zerofill != 0)
3206e93f7393Sniklas 	      reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3207e93f7393Sniklas 
3208e93f7393Sniklas 	    /* and then flush the line: */
3209b725ae77Skettenis 	    if (reclen > 0)
3210b725ae77Skettenis 	      {
3211e93f7393Sniklas 		reclen = pmon_checkset (reclen, &bp, &csum);
3212e93f7393Sniklas 		/* Currently pmon_checkset outputs the line terminator by
3213e93f7393Sniklas 		   default, so we write out the buffer so far: */
3214b725ae77Skettenis 		pmon_download (buffer, reclen);
3215b725ae77Skettenis 		finished = pmon_check_ack ("record remnant");
3216e93f7393Sniklas 	      }
3217e93f7393Sniklas 	  }
3218e93f7393Sniklas 
3219e93f7393Sniklas 	putchar_unfiltered ('\n');
3220e93f7393Sniklas       }
3221e93f7393Sniklas 
3222e93f7393Sniklas   /* Terminate the transfer. We know that we have an empty output
3223e93f7393Sniklas      buffer at this point. */
3224b725ae77Skettenis   sprintf (buffer, "/E/E\n");	/* include dummy padding characters */
3225e93f7393Sniklas   reclen = strlen (buffer);
3226b725ae77Skettenis   pmon_download (buffer, reclen);
3227e93f7393Sniklas 
3228b725ae77Skettenis   if (finished)
3229b725ae77Skettenis     {				/* Ignore the termination message: */
3230b725ae77Skettenis       serial_flush_input (udp_in_use ? udp_desc : mips_desc);
3231b725ae77Skettenis     }
3232b725ae77Skettenis   else
3233b725ae77Skettenis     {				/* Deal with termination message: */
3234b725ae77Skettenis       pmon_end_download (final, bintotal);
3235e93f7393Sniklas     }
3236e93f7393Sniklas 
3237e93f7393Sniklas   return;
3238e93f7393Sniklas }
3239e93f7393Sniklas 
3240e93f7393Sniklas /* mips_load -- download a file. */
3241e93f7393Sniklas 
3242e93f7393Sniklas static void
mips_load(char * file,int from_tty)3243b725ae77Skettenis mips_load (char *file, int from_tty)
3244e93f7393Sniklas {
3245e93f7393Sniklas   /* Get the board out of remote debugging mode.  */
3246e93f7393Sniklas   if (mips_exit_debug ())
3247e93f7393Sniklas     error ("mips_load:  Couldn't get into monitor mode.");
3248e93f7393Sniklas 
3249b725ae77Skettenis   if (mips_monitor != MON_IDT)
3250e93f7393Sniklas     pmon_load_fast (file);
3251e93f7393Sniklas   else
3252e93f7393Sniklas     mips_load_srec (file);
3253e93f7393Sniklas 
3254e93f7393Sniklas   mips_initialize ();
3255e93f7393Sniklas 
3256e93f7393Sniklas   /* Finally, make the PC point at the start address */
3257b725ae77Skettenis   if (mips_monitor != MON_IDT)
3258e93f7393Sniklas     {
3259b725ae77Skettenis       /* Work around problem where PMON monitor updates the PC after a load
3260b725ae77Skettenis          to a different value than GDB thinks it has. The following ensures
3261b725ae77Skettenis          that the write_pc() WILL update the PC value: */
3262b725ae77Skettenis       deprecated_register_valid[PC_REGNUM] = 0;
3263e93f7393Sniklas     }
3264e93f7393Sniklas   if (exec_bfd)
3265e93f7393Sniklas     write_pc (bfd_get_start_address (exec_bfd));
3266e93f7393Sniklas 
3267b725ae77Skettenis   inferior_ptid = null_ptid;	/* No process now */
3268e93f7393Sniklas 
3269e93f7393Sniklas /* This is necessary because many things were based on the PC at the time that
3270e93f7393Sniklas    we attached to the monitor, which is no longer valid now that we have loaded
3271e93f7393Sniklas    new code (and just changed the PC).  Another way to do this might be to call
3272e93f7393Sniklas    normal_stop, except that the stack may not be valid, and things would get
3273e93f7393Sniklas    horribly confused... */
3274e93f7393Sniklas 
3275e93f7393Sniklas   clear_symtab_users ();
3276e93f7393Sniklas }
3277e93f7393Sniklas 
3278b725ae77Skettenis 
3279b725ae77Skettenis /* Pass the command argument as a packet to PMON verbatim.  */
3280b725ae77Skettenis 
3281b725ae77Skettenis static void
pmon_command(char * args,int from_tty)3282b725ae77Skettenis pmon_command (char *args, int from_tty)
3283e93f7393Sniklas {
3284b725ae77Skettenis   char buf[DATA_MAXLEN + 1];
3285b725ae77Skettenis   int rlen;
3286b725ae77Skettenis 
3287b725ae77Skettenis   sprintf (buf, "0x0 %s", args);
3288b725ae77Skettenis   mips_send_packet (buf, 1);
3289b725ae77Skettenis   printf_filtered ("Send packet: %s\n", buf);
3290b725ae77Skettenis 
3291b725ae77Skettenis   rlen = mips_receive_packet (buf, 1, mips_receive_wait);
3292b725ae77Skettenis   buf[rlen] = '\0';
3293b725ae77Skettenis   printf_filtered ("Received packet: %s\n", buf);
3294b725ae77Skettenis }
3295b725ae77Skettenis 
3296b725ae77Skettenis extern initialize_file_ftype _initialize_remote_mips; /* -Wmissing-prototypes */
3297b725ae77Skettenis 
3298b725ae77Skettenis void
_initialize_remote_mips(void)3299b725ae77Skettenis _initialize_remote_mips (void)
3300b725ae77Skettenis {
3301b725ae77Skettenis   /* Initialize the fields in mips_ops that are common to all four targets.  */
3302b725ae77Skettenis   mips_ops.to_longname = "Remote MIPS debugging over serial line";
3303b725ae77Skettenis   mips_ops.to_close = mips_close;
3304b725ae77Skettenis   mips_ops.to_detach = mips_detach;
3305b725ae77Skettenis   mips_ops.to_resume = mips_resume;
3306b725ae77Skettenis   mips_ops.to_fetch_registers = mips_fetch_registers;
3307b725ae77Skettenis   mips_ops.to_store_registers = mips_store_registers;
3308b725ae77Skettenis   mips_ops.to_prepare_to_store = mips_prepare_to_store;
3309*63addd46Skettenis   mips_ops.deprecated_xfer_memory = mips_xfer_memory;
3310b725ae77Skettenis   mips_ops.to_files_info = mips_files_info;
3311b725ae77Skettenis   mips_ops.to_insert_breakpoint = mips_insert_breakpoint;
3312b725ae77Skettenis   mips_ops.to_remove_breakpoint = mips_remove_breakpoint;
3313b725ae77Skettenis   mips_ops.to_insert_watchpoint = mips_insert_watchpoint;
3314b725ae77Skettenis   mips_ops.to_remove_watchpoint = mips_remove_watchpoint;
3315b725ae77Skettenis   mips_ops.to_stopped_by_watchpoint = mips_stopped_by_watchpoint;
3316b725ae77Skettenis   mips_ops.to_can_use_hw_breakpoint = mips_can_use_watchpoint;
3317b725ae77Skettenis   mips_ops.to_kill = mips_kill;
3318b725ae77Skettenis   mips_ops.to_load = mips_load;
3319b725ae77Skettenis   mips_ops.to_create_inferior = mips_create_inferior;
3320b725ae77Skettenis   mips_ops.to_mourn_inferior = mips_mourn_inferior;
3321b725ae77Skettenis   mips_ops.to_stratum = process_stratum;
3322b725ae77Skettenis   mips_ops.to_has_all_memory = 1;
3323b725ae77Skettenis   mips_ops.to_has_memory = 1;
3324b725ae77Skettenis   mips_ops.to_has_stack = 1;
3325b725ae77Skettenis   mips_ops.to_has_registers = 1;
3326b725ae77Skettenis   mips_ops.to_has_execution = 1;
3327b725ae77Skettenis   mips_ops.to_magic = OPS_MAGIC;
3328b725ae77Skettenis 
3329b725ae77Skettenis   /* Copy the common fields to all four target vectors.  */
3330b725ae77Skettenis   pmon_ops = ddb_ops = lsi_ops = mips_ops;
3331b725ae77Skettenis 
3332b725ae77Skettenis   /* Initialize target-specific fields in the target vectors.  */
3333b725ae77Skettenis   mips_ops.to_shortname = "mips";
3334b725ae77Skettenis   mips_ops.to_doc = "\
3335e93f7393Sniklas Debug a board using the MIPS remote debugging protocol over a serial line.\n\
3336e93f7393Sniklas The argument is the device it is connected to or, if it contains a colon,\n\
3337b725ae77Skettenis HOST:PORT to access a board over a network";
3338b725ae77Skettenis   mips_ops.to_open = mips_open;
3339b725ae77Skettenis   mips_ops.to_wait = mips_wait;
3340b725ae77Skettenis 
3341b725ae77Skettenis   pmon_ops.to_shortname = "pmon";
3342b725ae77Skettenis   pmon_ops.to_doc = "\
3343e93f7393Sniklas Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3344e93f7393Sniklas line. The argument is the device it is connected to or, if it contains a\n\
3345b725ae77Skettenis colon, HOST:PORT to access a board over a network";
3346b725ae77Skettenis   pmon_ops.to_open = pmon_open;
3347b725ae77Skettenis   pmon_ops.to_wait = mips_wait;
3348b725ae77Skettenis 
3349b725ae77Skettenis   ddb_ops.to_shortname = "ddb";
3350b725ae77Skettenis   ddb_ops.to_doc = "\
3351e93f7393Sniklas Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3352b725ae77Skettenis line. The first argument is the device it is connected to or, if it contains\n\
3353b725ae77Skettenis a colon, HOST:PORT to access a board over a network.  The optional second\n\
3354b725ae77Skettenis parameter is the temporary file in the form HOST:FILENAME to be used for\n\
3355b725ae77Skettenis TFTP downloads to the board.  The optional third parameter is the local name\n\
3356b725ae77Skettenis of the TFTP temporary file, if it differs from the filename seen by the board.";
3357b725ae77Skettenis   ddb_ops.to_open = ddb_open;
3358b725ae77Skettenis   ddb_ops.to_wait = mips_wait;
3359b725ae77Skettenis 
3360b725ae77Skettenis   lsi_ops.to_shortname = "lsi";
3361b725ae77Skettenis   lsi_ops.to_doc = pmon_ops.to_doc;
3362b725ae77Skettenis   lsi_ops.to_open = lsi_open;
3363b725ae77Skettenis   lsi_ops.to_wait = mips_wait;
3364b725ae77Skettenis 
3365b725ae77Skettenis   /* Add the targets.  */
3366e93f7393Sniklas   add_target (&mips_ops);
3367e93f7393Sniklas   add_target (&pmon_ops);
3368e93f7393Sniklas   add_target (&ddb_ops);
3369b725ae77Skettenis   add_target (&lsi_ops);
3370e93f7393Sniklas 
3371*63addd46Skettenis   deprecated_add_show_from_set
3372*63addd46Skettenis     (add_set_cmd ("timeout", no_class, var_zinteger,
3373e93f7393Sniklas 		  (char *) &mips_receive_wait,
3374e93f7393Sniklas 		  "Set timeout in seconds for remote MIPS serial I/O.",
3375e93f7393Sniklas 		  &setlist),
3376e93f7393Sniklas      &showlist);
3377e93f7393Sniklas 
3378*63addd46Skettenis   deprecated_add_show_from_set
3379*63addd46Skettenis     (add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3380*63addd46Skettenis 		  (char *) &mips_retransmit_wait, "\
3381*63addd46Skettenis Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3382e93f7393Sniklas This is the number of seconds to wait for an acknowledgement to a packet\n\
3383e93f7393Sniklas before resending the packet.", &setlist),
3384e93f7393Sniklas      &showlist);
3385e93f7393Sniklas 
3386*63addd46Skettenis   deprecated_add_show_from_set
3387*63addd46Skettenis     (add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3388*63addd46Skettenis 		  (char *) &mips_syn_garbage, "\
3389*63addd46Skettenis Set the maximum number of characters to ignore when scanning for a SYN.\n\
3390e93f7393Sniklas This is the maximum number of characters GDB will ignore when trying to\n\
3391*63addd46Skettenis synchronize with the remote system.  A value of -1 means that there is no\n\
3392*63addd46Skettenis limit. (Note that these characters are printed out even though they are\n\
3393*63addd46Skettenis ignored.)",
3394e93f7393Sniklas 		  &setlist),
3395e93f7393Sniklas      &showlist);
3396b725ae77Skettenis 
3397*63addd46Skettenis   deprecated_add_show_from_set
3398b725ae77Skettenis     (add_set_cmd ("monitor-prompt", class_obscure, var_string,
3399b725ae77Skettenis 		  (char *) &mips_monitor_prompt,
3400b725ae77Skettenis 		  "Set the prompt that GDB expects from the monitor.",
3401b725ae77Skettenis 		  &setlist),
3402b725ae77Skettenis      &showlist);
3403b725ae77Skettenis 
3404*63addd46Skettenis   deprecated_add_show_from_set
3405*63addd46Skettenis     (add_set_cmd ("monitor-warnings", class_obscure, var_zinteger,
3406b725ae77Skettenis 		  (char *) &monitor_warnings,
3407b725ae77Skettenis 		  "Set printing of monitor warnings.\n"
3408b725ae77Skettenis 		  "When enabled, monitor warnings about hardware breakpoints "
3409b725ae77Skettenis 		  "will be displayed.",
3410b725ae77Skettenis 		  &setlist),
3411b725ae77Skettenis      &showlist);
3412b725ae77Skettenis 
3413b725ae77Skettenis   add_com ("pmon <command>", class_obscure, pmon_command,
3414b725ae77Skettenis 	   "Send a packet to PMON (must be in debug mode).");
3415b725ae77Skettenis 
3416*63addd46Skettenis   deprecated_add_show_from_set
3417*63addd46Skettenis     (add_set_cmd ("mask-address", no_class,
3418*63addd46Skettenis 		  var_boolean, &mask_address_p, "\
3419*63addd46Skettenis Set zeroing of upper 32 bits of 64-bit addresses when talking to PMON targets.\n\
3420b725ae77Skettenis Use \"on\" to enable the masking and \"off\" to disable it.\n",
3421b725ae77Skettenis 		  &setlist),
3422b725ae77Skettenis      &showlist);
3423e93f7393Sniklas }
3424