1130812Smarcel/* Remote debugging interface for MIPS remote debugging protocol.
2130812Smarcel
3130812Smarcel   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4130812Smarcel   2002 Free Software Foundation, Inc.
5130812Smarcel
6130812Smarcel   Contributed by Cygnus Support.  Written by Ian Lance Taylor
7130812Smarcel   <ian@cygnus.com>.
8130812Smarcel
9130812Smarcel   This file is part of GDB.
10130812Smarcel
11130812Smarcel   This program is free software; you can redistribute it and/or modify
12130812Smarcel   it under the terms of the GNU General Public License as published by
13130812Smarcel   the Free Software Foundation; either version 2 of the License, or
14130812Smarcel   (at your option) any later version.
15130812Smarcel
16130812Smarcel   This program is distributed in the hope that it will be useful,
17130812Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
18130812Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19130812Smarcel   GNU General Public License for more details.
20130812Smarcel
21130812Smarcel   You should have received a copy of the GNU General Public License
22130812Smarcel   along with this program; if not, write to the Free Software
23130812Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
24130812Smarcel   Boston, MA 02111-1307, USA.  */
25130812Smarcel
26130812Smarcel#include "defs.h"
27130812Smarcel#include "inferior.h"
28130812Smarcel#include "bfd.h"
29130812Smarcel#include "symfile.h"
30130812Smarcel#include "gdbcmd.h"
31130812Smarcel#include "gdbcore.h"
32130812Smarcel#include "serial.h"
33130812Smarcel#include "target.h"
34130812Smarcel#include "remote-utils.h"
35130812Smarcel#include "gdb_string.h"
36130812Smarcel#include "gdb_stat.h"
37130812Smarcel#include "regcache.h"
38130812Smarcel#include <ctype.h>
39130812Smarcel#include "mips-tdep.h"
40130812Smarcel
41130812Smarcel
42130812Smarcel/* Breakpoint types.  Values 0, 1, and 2 must agree with the watch
43130812Smarcel   types passed by breakpoint.c to target_insert_watchpoint.
44130812Smarcel   Value 3 is our own invention, and is used for ordinary instruction
45130812Smarcel   breakpoints.  Value 4 is used to mark an unused watchpoint in tables.  */
46130812Smarcelenum break_type
47130812Smarcel  {
48130812Smarcel    BREAK_WRITE,		/* 0 */
49130812Smarcel    BREAK_READ,			/* 1 */
50130812Smarcel    BREAK_ACCESS,		/* 2 */
51130812Smarcel    BREAK_FETCH,		/* 3 */
52130812Smarcel    BREAK_UNUSED		/* 4 */
53130812Smarcel  };
54130812Smarcel
55130812Smarcel/* Prototypes for local functions.  */
56130812Smarcel
57130812Smarcelstatic int mips_readchar (int timeout);
58130812Smarcel
59130812Smarcelstatic int mips_receive_header (unsigned char *hdr, int *pgarbage,
60130812Smarcel				int ch, int timeout);
61130812Smarcel
62130812Smarcelstatic int mips_receive_trailer (unsigned char *trlr, int *pgarbage,
63130812Smarcel				 int *pch, int timeout);
64130812Smarcel
65130812Smarcelstatic int mips_cksum (const unsigned char *hdr,
66130812Smarcel		       const unsigned char *data, int len);
67130812Smarcel
68130812Smarcelstatic void mips_send_packet (const char *s, int get_ack);
69130812Smarcel
70130812Smarcelstatic void mips_send_command (const char *cmd, int prompt);
71130812Smarcel
72130812Smarcelstatic int mips_receive_packet (char *buff, int throw_error, int timeout);
73130812Smarcel
74130812Smarcelstatic ULONGEST mips_request (int cmd, ULONGEST addr, ULONGEST data,
75130812Smarcel			      int *perr, int timeout, char *buff);
76130812Smarcel
77130812Smarcelstatic void mips_initialize (void);
78130812Smarcel
79130812Smarcelstatic void mips_open (char *name, int from_tty);
80130812Smarcel
81130812Smarcelstatic void pmon_open (char *name, int from_tty);
82130812Smarcel
83130812Smarcelstatic void ddb_open (char *name, int from_tty);
84130812Smarcel
85130812Smarcelstatic void lsi_open (char *name, int from_tty);
86130812Smarcel
87130812Smarcelstatic void mips_close (int quitting);
88130812Smarcel
89130812Smarcelstatic void mips_detach (char *args, int from_tty);
90130812Smarcel
91130812Smarcelstatic void mips_resume (ptid_t ptid, int step,
92130812Smarcel                         enum target_signal siggnal);
93130812Smarcel
94130812Smarcelstatic ptid_t mips_wait (ptid_t ptid,
95130812Smarcel                               struct target_waitstatus *status);
96130812Smarcel
97130812Smarcelstatic int mips_map_regno (int regno);
98130812Smarcel
99130812Smarcelstatic void mips_fetch_registers (int regno);
100130812Smarcel
101130812Smarcelstatic void mips_prepare_to_store (void);
102130812Smarcel
103130812Smarcelstatic void mips_store_registers (int regno);
104130812Smarcel
105130812Smarcelstatic unsigned int mips_fetch_word (CORE_ADDR addr);
106130812Smarcel
107130812Smarcelstatic int mips_store_word (CORE_ADDR addr, unsigned int value,
108130812Smarcel			    char *old_contents);
109130812Smarcel
110130812Smarcelstatic int mips_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
111130812Smarcel			     int write,
112130812Smarcel			     struct mem_attrib *attrib,
113130812Smarcel			     struct target_ops *target);
114130812Smarcel
115130812Smarcelstatic void mips_files_info (struct target_ops *ignore);
116130812Smarcel
117130812Smarcelstatic void mips_create_inferior (char *execfile, char *args, char **env);
118130812Smarcel
119130812Smarcelstatic void mips_mourn_inferior (void);
120130812Smarcel
121130812Smarcelstatic int pmon_makeb64 (unsigned long v, char *p, int n, int *chksum);
122130812Smarcel
123130812Smarcelstatic int pmon_zeroset (int recsize, char **buff, int *amount,
124130812Smarcel			 unsigned int *chksum);
125130812Smarcel
126130812Smarcelstatic int pmon_checkset (int recsize, char **buff, int *value);
127130812Smarcel
128130812Smarcelstatic void pmon_make_fastrec (char **outbuf, unsigned char *inbuf,
129130812Smarcel			       int *inptr, int inamount, int *recsize,
130130812Smarcel			       unsigned int *csum, unsigned int *zerofill);
131130812Smarcel
132130812Smarcelstatic int pmon_check_ack (char *mesg);
133130812Smarcel
134130812Smarcelstatic void pmon_start_download (void);
135130812Smarcel
136130812Smarcelstatic void pmon_end_download (int final, int bintotal);
137130812Smarcel
138130812Smarcelstatic void pmon_download (char *buffer, int length);
139130812Smarcel
140130812Smarcelstatic void pmon_load_fast (char *file);
141130812Smarcel
142130812Smarcelstatic void mips_load (char *file, int from_tty);
143130812Smarcel
144130812Smarcelstatic int mips_make_srec (char *buffer, int type, CORE_ADDR memaddr,
145130812Smarcel			   unsigned char *myaddr, int len);
146130812Smarcel
147130812Smarcelstatic int set_breakpoint (CORE_ADDR addr, int len, enum break_type type);
148130812Smarcel
149130812Smarcelstatic int clear_breakpoint (CORE_ADDR addr, int len, enum break_type type);
150130812Smarcel
151130812Smarcelstatic int common_breakpoint (int set, CORE_ADDR addr, int len,
152130812Smarcel			      enum break_type type);
153130812Smarcel
154130812Smarcel/* Forward declarations.  */
155130812Smarcelextern struct target_ops mips_ops;
156130812Smarcelextern struct target_ops pmon_ops;
157130812Smarcelextern struct target_ops ddb_ops;
158130812Smarcel/* *INDENT-OFF* */
159130812Smarcel/* The MIPS remote debugging interface is built on top of a simple
160130812Smarcel   packet protocol.  Each packet is organized as follows:
161130812Smarcel
162130812Smarcel   SYN  The first character is always a SYN (ASCII 026, or ^V).  SYN
163130812Smarcel   may not appear anywhere else in the packet.  Any time a SYN is
164130812Smarcel   seen, a new packet should be assumed to have begun.
165130812Smarcel
166130812Smarcel   TYPE_LEN
167130812Smarcel   This byte contains the upper five bits of the logical length
168130812Smarcel   of the data section, plus a single bit indicating whether this
169130812Smarcel   is a data packet or an acknowledgement.  The documentation
170130812Smarcel   indicates that this bit is 1 for a data packet, but the actual
171130812Smarcel   board uses 1 for an acknowledgement.  The value of the byte is
172130812Smarcel   0x40 + (ack ? 0x20 : 0) + (len >> 6)
173130812Smarcel   (we always have 0 <= len < 1024).  Acknowledgement packets do
174130812Smarcel   not carry data, and must have a data length of 0.
175130812Smarcel
176130812Smarcel   LEN1 This byte contains the lower six bits of the logical length of
177130812Smarcel   the data section.  The value is
178130812Smarcel   0x40 + (len & 0x3f)
179130812Smarcel
180130812Smarcel   SEQ  This byte contains the six bit sequence number of the packet.
181130812Smarcel   The value is
182130812Smarcel   0x40 + seq
183130812Smarcel   An acknowlegment packet contains the sequence number of the
184130812Smarcel   packet being acknowledged plus 1 modulo 64.  Data packets are
185130812Smarcel   transmitted in sequence.  There may only be one outstanding
186130812Smarcel   unacknowledged data packet at a time.  The sequence numbers
187130812Smarcel   are independent in each direction.  If an acknowledgement for
188130812Smarcel   the previous packet is received (i.e., an acknowledgement with
189130812Smarcel   the sequence number of the packet just sent) the packet just
190130812Smarcel   sent should be retransmitted.  If no acknowledgement is
191130812Smarcel   received within a timeout period, the packet should be
192130812Smarcel   retransmitted.  This has an unfortunate failure condition on a
193130812Smarcel   high-latency line, as a delayed acknowledgement may lead to an
194130812Smarcel   endless series of duplicate packets.
195130812Smarcel
196130812Smarcel   DATA The actual data bytes follow.  The following characters are
197130812Smarcel   escaped inline with DLE (ASCII 020, or ^P):
198130812Smarcel   SYN (026)    DLE S
199130812Smarcel   DLE (020)    DLE D
200130812Smarcel   ^C  (003)    DLE C
201130812Smarcel   ^S  (023)    DLE s
202130812Smarcel   ^Q  (021)    DLE q
203130812Smarcel   The additional DLE characters are not counted in the logical
204130812Smarcel   length stored in the TYPE_LEN and LEN1 bytes.
205130812Smarcel
206130812Smarcel   CSUM1
207130812Smarcel   CSUM2
208130812Smarcel   CSUM3
209130812Smarcel   These bytes contain an 18 bit checksum of the complete
210130812Smarcel   contents of the packet excluding the SEQ byte and the
211130812Smarcel   CSUM[123] bytes.  The checksum is simply the twos complement
212130812Smarcel   addition of all the bytes treated as unsigned characters.  The
213130812Smarcel   values of the checksum bytes are:
214130812Smarcel   CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
215130812Smarcel   CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
216130812Smarcel   CSUM3: 0x40 + (cksum & 0x3f)
217130812Smarcel
218130812Smarcel   It happens that the MIPS remote debugging protocol always
219130812Smarcel   communicates with ASCII strings.  Because of this, this
220130812Smarcel   implementation doesn't bother to handle the DLE quoting mechanism,
221130812Smarcel   since it will never be required.  */
222130812Smarcel/* *INDENT-ON* */
223130812Smarcel
224130812Smarcel
225130812Smarcel/* The SYN character which starts each packet.  */
226130812Smarcel#define SYN '\026'
227130812Smarcel
228130812Smarcel/* The 0x40 used to offset each packet (this value ensures that all of
229130812Smarcel   the header and trailer bytes, other than SYN, are printable ASCII
230130812Smarcel   characters).  */
231130812Smarcel#define HDR_OFFSET 0x40
232130812Smarcel
233130812Smarcel/* The indices of the bytes in the packet header.  */
234130812Smarcel#define HDR_INDX_SYN 0
235130812Smarcel#define HDR_INDX_TYPE_LEN 1
236130812Smarcel#define HDR_INDX_LEN1 2
237130812Smarcel#define HDR_INDX_SEQ 3
238130812Smarcel#define HDR_LENGTH 4
239130812Smarcel
240130812Smarcel/* The data/ack bit in the TYPE_LEN header byte.  */
241130812Smarcel#define TYPE_LEN_DA_BIT 0x20
242130812Smarcel#define TYPE_LEN_DATA 0
243130812Smarcel#define TYPE_LEN_ACK TYPE_LEN_DA_BIT
244130812Smarcel
245130812Smarcel/* How to compute the header bytes.  */
246130812Smarcel#define HDR_SET_SYN(data, len, seq) (SYN)
247130812Smarcel#define HDR_SET_TYPE_LEN(data, len, seq) \
248130812Smarcel  (HDR_OFFSET \
249130812Smarcel   + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
250130812Smarcel   + (((len) >> 6) & 0x1f))
251130812Smarcel#define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
252130812Smarcel#define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
253130812Smarcel
254130812Smarcel/* Check that a header byte is reasonable.  */
255130812Smarcel#define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
256130812Smarcel
257130812Smarcel/* Get data from the header.  These macros evaluate their argument
258130812Smarcel   multiple times.  */
259130812Smarcel#define HDR_IS_DATA(hdr) \
260130812Smarcel  (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
261130812Smarcel#define HDR_GET_LEN(hdr) \
262130812Smarcel  ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
263130812Smarcel#define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
264130812Smarcel
265130812Smarcel/* The maximum data length.  */
266130812Smarcel#define DATA_MAXLEN 1023
267130812Smarcel
268130812Smarcel/* The trailer offset.  */
269130812Smarcel#define TRLR_OFFSET HDR_OFFSET
270130812Smarcel
271130812Smarcel/* The indices of the bytes in the packet trailer.  */
272130812Smarcel#define TRLR_INDX_CSUM1 0
273130812Smarcel#define TRLR_INDX_CSUM2 1
274130812Smarcel#define TRLR_INDX_CSUM3 2
275130812Smarcel#define TRLR_LENGTH 3
276130812Smarcel
277130812Smarcel/* How to compute the trailer bytes.  */
278130812Smarcel#define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
279130812Smarcel#define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >>  6) & 0x3f))
280130812Smarcel#define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum)      ) & 0x3f))
281130812Smarcel
282130812Smarcel/* Check that a trailer byte is reasonable.  */
283130812Smarcel#define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
284130812Smarcel
285130812Smarcel/* Get data from the trailer.  This evaluates its argument multiple
286130812Smarcel   times.  */
287130812Smarcel#define TRLR_GET_CKSUM(trlr) \
288130812Smarcel  ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
289130812Smarcel   + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) <<  6) \
290130812Smarcel   + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
291130812Smarcel
292130812Smarcel/* The sequence number modulos.  */
293130812Smarcel#define SEQ_MODULOS (64)
294130812Smarcel
295130812Smarcel/* PMON commands to load from the serial port or UDP socket.  */
296130812Smarcel#define LOAD_CMD	"load -b -s tty0\r"
297130812Smarcel#define LOAD_CMD_UDP	"load -b -s udp\r"
298130812Smarcel
299130812Smarcel/* The target vectors for the four different remote MIPS targets.
300130812Smarcel   These are initialized with code in _initialize_remote_mips instead
301130812Smarcel   of static initializers, to make it easier to extend the target_ops
302130812Smarcel   vector later.  */
303130812Smarcelstruct target_ops mips_ops, pmon_ops, ddb_ops, lsi_ops;
304130812Smarcel
305130812Smarcelenum mips_monitor_type
306130812Smarcel  {
307130812Smarcel    /* IDT/SIM monitor being used: */
308130812Smarcel    MON_IDT,
309130812Smarcel    /* PMON monitor being used: */
310130812Smarcel    MON_PMON,			/* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov  9 1995 17:19:50 */
311130812Smarcel    MON_DDB,			/* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems,  Thu Jun 6 09:28:40 PDT 1996 */
312130812Smarcel    MON_LSI,			/* 4.3.12 [EB,FP], LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
313130812Smarcel    /* Last and unused value, for sizing vectors, etc. */
314130812Smarcel    MON_LAST
315130812Smarcel  };
316130812Smarcelstatic enum mips_monitor_type mips_monitor = MON_LAST;
317130812Smarcel
318130812Smarcel/* The monitor prompt text.  If the user sets the PMON prompt
319130812Smarcel   to some new value, the GDB `set monitor-prompt' command must also
320130812Smarcel   be used to inform GDB about the expected prompt.  Otherwise, GDB
321130812Smarcel   will not be able to connect to PMON in mips_initialize().
322130812Smarcel   If the `set monitor-prompt' command is not used, the expected
323130812Smarcel   default prompt will be set according the target:
324130812Smarcel   target               prompt
325130812Smarcel   -----                -----
326130812Smarcel   pmon         PMON>
327130812Smarcel   ddb          NEC010>
328130812Smarcel   lsi          PMON>
329130812Smarcel */
330130812Smarcelstatic char *mips_monitor_prompt;
331130812Smarcel
332130812Smarcel/* Set to 1 if the target is open.  */
333130812Smarcelstatic int mips_is_open;
334130812Smarcel
335130812Smarcel/* Currently active target description (if mips_is_open == 1) */
336130812Smarcelstatic struct target_ops *current_ops;
337130812Smarcel
338130812Smarcel/* Set to 1 while the connection is being initialized.  */
339130812Smarcelstatic int mips_initializing;
340130812Smarcel
341130812Smarcel/* Set to 1 while the connection is being brought down.  */
342130812Smarcelstatic int mips_exiting;
343130812Smarcel
344130812Smarcel/* The next sequence number to send.  */
345130812Smarcelstatic unsigned int mips_send_seq;
346130812Smarcel
347130812Smarcel/* The next sequence number we expect to receive.  */
348130812Smarcelstatic unsigned int mips_receive_seq;
349130812Smarcel
350130812Smarcel/* The time to wait before retransmitting a packet, in seconds.  */
351130812Smarcelstatic int mips_retransmit_wait = 3;
352130812Smarcel
353130812Smarcel/* The number of times to try retransmitting a packet before giving up.  */
354130812Smarcelstatic int mips_send_retries = 10;
355130812Smarcel
356130812Smarcel/* The number of garbage characters to accept when looking for an
357130812Smarcel   SYN for the next packet.  */
358130812Smarcelstatic int mips_syn_garbage = 10;
359130812Smarcel
360130812Smarcel/* The time to wait for a packet, in seconds.  */
361130812Smarcelstatic int mips_receive_wait = 5;
362130812Smarcel
363130812Smarcel/* Set if we have sent a packet to the board but have not yet received
364130812Smarcel   a reply.  */
365130812Smarcelstatic int mips_need_reply = 0;
366130812Smarcel
367130812Smarcel/* Handle used to access serial I/O stream.  */
368130812Smarcelstatic struct serial *mips_desc;
369130812Smarcel
370130812Smarcel/* UDP handle used to download files to target.  */
371130812Smarcelstatic struct serial *udp_desc;
372130812Smarcelstatic int udp_in_use;
373130812Smarcel
374130812Smarcel/* TFTP filename used to download files to DDB board, in the form
375130812Smarcel   host:filename.  */
376130812Smarcelstatic char *tftp_name;		/* host:filename */
377130812Smarcelstatic char *tftp_localname;	/* filename portion of above */
378130812Smarcelstatic int tftp_in_use;
379130812Smarcelstatic FILE *tftp_file;
380130812Smarcel
381130812Smarcel/* Counts the number of times the user tried to interrupt the target (usually
382130812Smarcel   via ^C.  */
383130812Smarcelstatic int interrupt_count;
384130812Smarcel
385130812Smarcel/* If non-zero, means that the target is running. */
386130812Smarcelstatic int mips_wait_flag = 0;
387130812Smarcel
388130812Smarcel/* If non-zero, monitor supports breakpoint commands. */
389130812Smarcelstatic int monitor_supports_breakpoints = 0;
390130812Smarcel
391130812Smarcel/* Data cache header.  */
392130812Smarcel
393130812Smarcel#if 0				/* not used (yet?) */
394130812Smarcelstatic DCACHE *mips_dcache;
395130812Smarcel#endif
396130812Smarcel
397130812Smarcel/* Non-zero means that we've just hit a read or write watchpoint */
398130812Smarcelstatic int hit_watchpoint;
399130812Smarcel
400130812Smarcel/* Table of breakpoints/watchpoints (used only on LSI PMON target).
401130812Smarcel   The table is indexed by a breakpoint number, which is an integer
402130812Smarcel   from 0 to 255 returned by the LSI PMON when a breakpoint is set.
403130812Smarcel */
404130812Smarcel#define MAX_LSI_BREAKPOINTS 256
405130812Smarcelstruct lsi_breakpoint_info
406130812Smarcel  {
407130812Smarcel    enum break_type type;	/* type of breakpoint */
408130812Smarcel    CORE_ADDR addr;		/* address of breakpoint */
409130812Smarcel    int len;			/* length of region being watched */
410130812Smarcel    unsigned long value;	/* value to watch */
411130812Smarcel  }
412130812Smarcellsi_breakpoints[MAX_LSI_BREAKPOINTS];
413130812Smarcel
414130812Smarcel/* Error/warning codes returned by LSI PMON for breakpoint commands.
415130812Smarcel   Warning values may be ORed together; error values may not.  */
416130812Smarcel#define W_WARN	0x100		/* This bit is set if the error code is a warning */
417130812Smarcel#define W_MSK   0x101		/* warning: Range feature is supported via mask */
418130812Smarcel#define W_VAL   0x102		/* warning: Value check is not supported in hardware */
419130812Smarcel#define W_QAL   0x104		/* warning: Requested qualifiers are not supported in hardware */
420130812Smarcel
421130812Smarcel#define E_ERR	0x200		/* This bit is set if the error code is an error */
422130812Smarcel#define E_BPT   0x200		/* error: No such breakpoint number */
423130812Smarcel#define E_RGE   0x201		/* error: Range is not supported */
424130812Smarcel#define E_QAL   0x202		/* error: The requested qualifiers can not be used */
425130812Smarcel#define E_OUT   0x203		/* error: Out of hardware resources */
426130812Smarcel#define E_NON   0x204		/* error: Hardware breakpoint not supported */
427130812Smarcel
428130812Smarcelstruct lsi_error
429130812Smarcel  {
430130812Smarcel    int code;			/* error code */
431130812Smarcel    char *string;		/* string associated with this code */
432130812Smarcel  };
433130812Smarcel
434130812Smarcelstruct lsi_error lsi_warning_table[] =
435130812Smarcel{
436130812Smarcel  {W_MSK, "Range feature is supported via mask"},
437130812Smarcel  {W_VAL, "Value check is not supported in hardware"},
438130812Smarcel  {W_QAL, "Requested qualifiers are not supported in hardware"},
439130812Smarcel  {0, NULL}
440130812Smarcel};
441130812Smarcel
442130812Smarcelstruct lsi_error lsi_error_table[] =
443130812Smarcel{
444130812Smarcel  {E_BPT, "No such breakpoint number"},
445130812Smarcel  {E_RGE, "Range is not supported"},
446130812Smarcel  {E_QAL, "The requested qualifiers can not be used"},
447130812Smarcel  {E_OUT, "Out of hardware resources"},
448130812Smarcel  {E_NON, "Hardware breakpoint not supported"},
449130812Smarcel  {0, NULL}
450130812Smarcel};
451130812Smarcel
452130812Smarcel/* Set to 1 with the 'set monitor-warnings' command to enable printing
453130812Smarcel   of warnings returned by PMON when hardware breakpoints are used.  */
454130812Smarcelstatic int monitor_warnings;
455130812Smarcel
456130812Smarcel
457130812Smarcelstatic void
458130812Smarcelclose_ports (void)
459130812Smarcel{
460130812Smarcel  mips_is_open = 0;
461130812Smarcel  serial_close (mips_desc);
462130812Smarcel
463130812Smarcel  if (udp_in_use)
464130812Smarcel    {
465130812Smarcel      serial_close (udp_desc);
466130812Smarcel      udp_in_use = 0;
467130812Smarcel    }
468130812Smarcel  tftp_in_use = 0;
469130812Smarcel}
470130812Smarcel
471130812Smarcel/* Handle low-level error that we can't recover from.  Note that just
472130812Smarcel   error()ing out from target_wait or some such low-level place will cause
473130812Smarcel   all hell to break loose--the rest of GDB will tend to get left in an
474130812Smarcel   inconsistent state.  */
475130812Smarcel
476130812Smarcelstatic NORETURN void
477130812Smarcelmips_error (char *string,...)
478130812Smarcel{
479130812Smarcel  va_list args;
480130812Smarcel
481130812Smarcel  va_start (args, string);
482130812Smarcel
483130812Smarcel  target_terminal_ours ();
484130812Smarcel  wrap_here ("");		/* Force out any buffered output */
485130812Smarcel  gdb_flush (gdb_stdout);
486130812Smarcel  if (error_pre_print)
487130812Smarcel    fputs_filtered (error_pre_print, gdb_stderr);
488130812Smarcel  vfprintf_filtered (gdb_stderr, string, args);
489130812Smarcel  fprintf_filtered (gdb_stderr, "\n");
490130812Smarcel  va_end (args);
491130812Smarcel  gdb_flush (gdb_stderr);
492130812Smarcel
493130812Smarcel  /* Clean up in such a way that mips_close won't try to talk to the
494130812Smarcel     board (it almost surely won't work since we weren't able to talk to
495130812Smarcel     it).  */
496130812Smarcel  close_ports ();
497130812Smarcel
498130812Smarcel  printf_unfiltered ("Ending remote MIPS debugging.\n");
499130812Smarcel  target_mourn_inferior ();
500130812Smarcel
501130812Smarcel  throw_exception (RETURN_ERROR);
502130812Smarcel}
503130812Smarcel
504130812Smarcel/* putc_readable - print a character, displaying non-printable chars in
505130812Smarcel   ^x notation or in hex.  */
506130812Smarcel
507130812Smarcelstatic void
508130812Smarcelfputc_readable (int ch, struct ui_file *file)
509130812Smarcel{
510130812Smarcel  if (ch == '\n')
511130812Smarcel    fputc_unfiltered ('\n', file);
512130812Smarcel  else if (ch == '\r')
513130812Smarcel    fprintf_unfiltered (file, "\\r");
514130812Smarcel  else if (ch < 0x20)		/* ASCII control character */
515130812Smarcel    fprintf_unfiltered (file, "^%c", ch + '@');
516130812Smarcel  else if (ch >= 0x7f)		/* non-ASCII characters (rubout or greater) */
517130812Smarcel    fprintf_unfiltered (file, "[%02x]", ch & 0xff);
518130812Smarcel  else
519130812Smarcel    fputc_unfiltered (ch, file);
520130812Smarcel}
521130812Smarcel
522130812Smarcel
523130812Smarcel/* puts_readable - print a string, displaying non-printable chars in
524130812Smarcel   ^x notation or in hex.  */
525130812Smarcel
526130812Smarcelstatic void
527130812Smarcelfputs_readable (const char *string, struct ui_file *file)
528130812Smarcel{
529130812Smarcel  int c;
530130812Smarcel
531130812Smarcel  while ((c = *string++) != '\0')
532130812Smarcel    fputc_readable (c, file);
533130812Smarcel}
534130812Smarcel
535130812Smarcel
536130812Smarcel/* Wait until STRING shows up in mips_desc.  Returns 1 if successful, else 0 if
537130812Smarcel   timed out.  TIMEOUT specifies timeout value in seconds.
538130812Smarcel */
539130812Smarcel
540130812Smarcelstatic int
541130812Smarcelmips_expect_timeout (const char *string, int timeout)
542130812Smarcel{
543130812Smarcel  const char *p = string;
544130812Smarcel
545130812Smarcel  if (remote_debug)
546130812Smarcel    {
547130812Smarcel      fprintf_unfiltered (gdb_stdlog, "Expected \"");
548130812Smarcel      fputs_readable (string, gdb_stdlog);
549130812Smarcel      fprintf_unfiltered (gdb_stdlog, "\", got \"");
550130812Smarcel    }
551130812Smarcel
552130812Smarcel  immediate_quit++;
553130812Smarcel  while (1)
554130812Smarcel    {
555130812Smarcel      int c;
556130812Smarcel
557130812Smarcel      /* Must use serial_readchar() here cuz mips_readchar would get
558130812Smarcel	 confused if we were waiting for the mips_monitor_prompt... */
559130812Smarcel
560130812Smarcel      c = serial_readchar (mips_desc, timeout);
561130812Smarcel
562130812Smarcel      if (c == SERIAL_TIMEOUT)
563130812Smarcel	{
564130812Smarcel	  if (remote_debug)
565130812Smarcel	    fprintf_unfiltered (gdb_stdlog, "\": FAIL\n");
566130812Smarcel	  return 0;
567130812Smarcel	}
568130812Smarcel
569130812Smarcel      if (remote_debug)
570130812Smarcel	fputc_readable (c, gdb_stdlog);
571130812Smarcel
572130812Smarcel      if (c == *p++)
573130812Smarcel	{
574130812Smarcel	  if (*p == '\0')
575130812Smarcel	    {
576130812Smarcel	      immediate_quit--;
577130812Smarcel	      if (remote_debug)
578130812Smarcel		fprintf_unfiltered (gdb_stdlog, "\": OK\n");
579130812Smarcel	      return 1;
580130812Smarcel	    }
581130812Smarcel	}
582130812Smarcel      else
583130812Smarcel	{
584130812Smarcel	  p = string;
585130812Smarcel	  if (c == *p)
586130812Smarcel	    p++;
587130812Smarcel	}
588130812Smarcel    }
589130812Smarcel}
590130812Smarcel
591130812Smarcel/* Wait until STRING shows up in mips_desc.  Returns 1 if successful, else 0 if
592130812Smarcel   timed out.  The timeout value is hard-coded to 2 seconds.  Use
593130812Smarcel   mips_expect_timeout if a different timeout value is needed.
594130812Smarcel */
595130812Smarcel
596130812Smarcelstatic int
597130812Smarcelmips_expect (const char *string)
598130812Smarcel{
599130812Smarcel  return mips_expect_timeout (string, remote_timeout);
600130812Smarcel}
601130812Smarcel
602130812Smarcel/* Read a character from the remote, aborting on error.  Returns
603130812Smarcel   SERIAL_TIMEOUT on timeout (since that's what serial_readchar()
604130812Smarcel   returns).  FIXME: If we see the string mips_monitor_prompt from the
605130812Smarcel   board, then we are debugging on the main console port, and we have
606130812Smarcel   somehow dropped out of remote debugging mode.  In this case, we
607130812Smarcel   automatically go back in to remote debugging mode.  This is a hack,
608130812Smarcel   put in because I can't find any way for a program running on the
609130812Smarcel   remote board to terminate without also ending remote debugging
610130812Smarcel   mode.  I assume users won't have any trouble with this; for one
611130812Smarcel   thing, the IDT documentation generally assumes that the remote
612130812Smarcel   debugging port is not the console port.  This is, however, very
613130812Smarcel   convenient for DejaGnu when you only have one connected serial
614130812Smarcel   port.  */
615130812Smarcel
616130812Smarcelstatic int
617130812Smarcelmips_readchar (int timeout)
618130812Smarcel{
619130812Smarcel  int ch;
620130812Smarcel  static int state = 0;
621130812Smarcel  int mips_monitor_prompt_len = strlen (mips_monitor_prompt);
622130812Smarcel
623130812Smarcel  {
624130812Smarcel    int i;
625130812Smarcel
626130812Smarcel    i = timeout;
627130812Smarcel    if (i == -1 && watchdog > 0)
628130812Smarcel      i = watchdog;
629130812Smarcel  }
630130812Smarcel
631130812Smarcel  if (state == mips_monitor_prompt_len)
632130812Smarcel    timeout = 1;
633130812Smarcel  ch = serial_readchar (mips_desc, timeout);
634130812Smarcel
635130812Smarcel  if (ch == SERIAL_TIMEOUT && timeout == -1)	/* Watchdog went off */
636130812Smarcel    {
637130812Smarcel      target_mourn_inferior ();
638130812Smarcel      error ("Watchdog has expired.  Target detached.\n");
639130812Smarcel    }
640130812Smarcel
641130812Smarcel  if (ch == SERIAL_EOF)
642130812Smarcel    mips_error ("End of file from remote");
643130812Smarcel  if (ch == SERIAL_ERROR)
644130812Smarcel    mips_error ("Error reading from remote: %s", safe_strerror (errno));
645130812Smarcel  if (remote_debug > 1)
646130812Smarcel    {
647130812Smarcel      /* Don't use _filtered; we can't deal with a QUIT out of
648130812Smarcel         target_wait, and I think this might be called from there.  */
649130812Smarcel      if (ch != SERIAL_TIMEOUT)
650130812Smarcel	fprintf_unfiltered (gdb_stdlog, "Read '%c' %d 0x%x\n", ch, ch, ch);
651130812Smarcel      else
652130812Smarcel	fprintf_unfiltered (gdb_stdlog, "Timed out in read\n");
653130812Smarcel    }
654130812Smarcel
655130812Smarcel  /* If we have seen mips_monitor_prompt and we either time out, or
656130812Smarcel     we see a @ (which was echoed from a packet we sent), reset the
657130812Smarcel     board as described above.  The first character in a packet after
658130812Smarcel     the SYN (which is not echoed) is always an @ unless the packet is
659130812Smarcel     more than 64 characters long, which ours never are.  */
660130812Smarcel  if ((ch == SERIAL_TIMEOUT || ch == '@')
661130812Smarcel      && state == mips_monitor_prompt_len
662130812Smarcel      && !mips_initializing
663130812Smarcel      && !mips_exiting)
664130812Smarcel    {
665130812Smarcel      if (remote_debug > 0)
666130812Smarcel	/* Don't use _filtered; we can't deal with a QUIT out of
667130812Smarcel	   target_wait, and I think this might be called from there.  */
668130812Smarcel	fprintf_unfiltered (gdb_stdlog, "Reinitializing MIPS debugging mode\n");
669130812Smarcel
670130812Smarcel      mips_need_reply = 0;
671130812Smarcel      mips_initialize ();
672130812Smarcel
673130812Smarcel      state = 0;
674130812Smarcel
675130812Smarcel      /* At this point, about the only thing we can do is abort the command
676130812Smarcel         in progress and get back to command level as quickly as possible. */
677130812Smarcel
678130812Smarcel      error ("Remote board reset, debug protocol re-initialized.");
679130812Smarcel    }
680130812Smarcel
681130812Smarcel  if (ch == mips_monitor_prompt[state])
682130812Smarcel    ++state;
683130812Smarcel  else
684130812Smarcel    state = 0;
685130812Smarcel
686130812Smarcel  return ch;
687130812Smarcel}
688130812Smarcel
689130812Smarcel/* Get a packet header, putting the data in the supplied buffer.
690130812Smarcel   PGARBAGE is a pointer to the number of garbage characters received
691130812Smarcel   so far.  CH is the last character received.  Returns 0 for success,
692130812Smarcel   or -1 for timeout.  */
693130812Smarcel
694130812Smarcelstatic int
695130812Smarcelmips_receive_header (unsigned char *hdr, int *pgarbage, int ch, int timeout)
696130812Smarcel{
697130812Smarcel  int i;
698130812Smarcel
699130812Smarcel  while (1)
700130812Smarcel    {
701130812Smarcel      /* Wait for a SYN.  mips_syn_garbage is intended to prevent
702130812Smarcel         sitting here indefinitely if the board sends us one garbage
703130812Smarcel         character per second.  ch may already have a value from the
704130812Smarcel         last time through the loop.  */
705130812Smarcel      while (ch != SYN)
706130812Smarcel	{
707130812Smarcel	  ch = mips_readchar (timeout);
708130812Smarcel	  if (ch == SERIAL_TIMEOUT)
709130812Smarcel	    return -1;
710130812Smarcel	  if (ch != SYN)
711130812Smarcel	    {
712130812Smarcel	      /* Printing the character here lets the user of gdb see
713130812Smarcel	         what the program is outputting, if the debugging is
714130812Smarcel	         being done on the console port.  Don't use _filtered:
715130812Smarcel	         we can't deal with a QUIT out of target_wait and
716130812Smarcel	         buffered target output confuses the user. */
717130812Smarcel 	      if (!mips_initializing || remote_debug > 0)
718130812Smarcel  		{
719130812Smarcel		  if (isprint (ch) || isspace (ch))
720130812Smarcel		    {
721130812Smarcel		      fputc_unfiltered (ch, gdb_stdtarg);
722130812Smarcel		    }
723130812Smarcel		  else
724130812Smarcel		    {
725130812Smarcel		      fputc_readable (ch, gdb_stdtarg);
726130812Smarcel		    }
727130812Smarcel		  gdb_flush (gdb_stdtarg);
728130812Smarcel  		}
729130812Smarcel
730130812Smarcel	      /* Only count unprintable characters. */
731130812Smarcel	      if (! (isprint (ch) || isspace (ch)))
732130812Smarcel		(*pgarbage) += 1;
733130812Smarcel
734130812Smarcel	      if (mips_syn_garbage > 0
735130812Smarcel		  && *pgarbage > mips_syn_garbage)
736130812Smarcel		mips_error ("Debug protocol failure:  more than %d characters before a sync.",
737130812Smarcel			    mips_syn_garbage);
738130812Smarcel	    }
739130812Smarcel	}
740130812Smarcel
741130812Smarcel      /* Get the packet header following the SYN.  */
742130812Smarcel      for (i = 1; i < HDR_LENGTH; i++)
743130812Smarcel	{
744130812Smarcel	  ch = mips_readchar (timeout);
745130812Smarcel	  if (ch == SERIAL_TIMEOUT)
746130812Smarcel	    return -1;
747130812Smarcel	  /* Make sure this is a header byte.  */
748130812Smarcel	  if (ch == SYN || !HDR_CHECK (ch))
749130812Smarcel	    break;
750130812Smarcel
751130812Smarcel	  hdr[i] = ch;
752130812Smarcel	}
753130812Smarcel
754130812Smarcel      /* If we got the complete header, we can return.  Otherwise we
755130812Smarcel         loop around and keep looking for SYN.  */
756130812Smarcel      if (i >= HDR_LENGTH)
757130812Smarcel	return 0;
758130812Smarcel    }
759130812Smarcel}
760130812Smarcel
761130812Smarcel/* Get a packet header, putting the data in the supplied buffer.
762130812Smarcel   PGARBAGE is a pointer to the number of garbage characters received
763130812Smarcel   so far.  The last character read is returned in *PCH.  Returns 0
764130812Smarcel   for success, -1 for timeout, -2 for error.  */
765130812Smarcel
766130812Smarcelstatic int
767130812Smarcelmips_receive_trailer (unsigned char *trlr, int *pgarbage, int *pch, int timeout)
768130812Smarcel{
769130812Smarcel  int i;
770130812Smarcel  int ch;
771130812Smarcel
772130812Smarcel  for (i = 0; i < TRLR_LENGTH; i++)
773130812Smarcel    {
774130812Smarcel      ch = mips_readchar (timeout);
775130812Smarcel      *pch = ch;
776130812Smarcel      if (ch == SERIAL_TIMEOUT)
777130812Smarcel	return -1;
778130812Smarcel      if (!TRLR_CHECK (ch))
779130812Smarcel	return -2;
780130812Smarcel      trlr[i] = ch;
781130812Smarcel    }
782130812Smarcel  return 0;
783130812Smarcel}
784130812Smarcel
785130812Smarcel/* Get the checksum of a packet.  HDR points to the packet header.
786130812Smarcel   DATA points to the packet data.  LEN is the length of DATA.  */
787130812Smarcel
788130812Smarcelstatic int
789130812Smarcelmips_cksum (const unsigned char *hdr, const unsigned char *data, int len)
790130812Smarcel{
791130812Smarcel  const unsigned char *p;
792130812Smarcel  int c;
793130812Smarcel  int cksum;
794130812Smarcel
795130812Smarcel  cksum = 0;
796130812Smarcel
797130812Smarcel  /* The initial SYN is not included in the checksum.  */
798130812Smarcel  c = HDR_LENGTH - 1;
799130812Smarcel  p = hdr + 1;
800130812Smarcel  while (c-- != 0)
801130812Smarcel    cksum += *p++;
802130812Smarcel
803130812Smarcel  c = len;
804130812Smarcel  p = data;
805130812Smarcel  while (c-- != 0)
806130812Smarcel    cksum += *p++;
807130812Smarcel
808130812Smarcel  return cksum;
809130812Smarcel}
810130812Smarcel
811130812Smarcel/* Send a packet containing the given ASCII string.  */
812130812Smarcel
813130812Smarcelstatic void
814130812Smarcelmips_send_packet (const char *s, int get_ack)
815130812Smarcel{
816130812Smarcel  /* unsigned */ int len;
817130812Smarcel  unsigned char *packet;
818130812Smarcel  int cksum;
819130812Smarcel  int try;
820130812Smarcel
821130812Smarcel  len = strlen (s);
822130812Smarcel  if (len > DATA_MAXLEN)
823130812Smarcel    mips_error ("MIPS protocol data packet too long: %s", s);
824130812Smarcel
825130812Smarcel  packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
826130812Smarcel
827130812Smarcel  packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
828130812Smarcel  packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
829130812Smarcel  packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
830130812Smarcel  packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
831130812Smarcel
832130812Smarcel  memcpy (packet + HDR_LENGTH, s, len);
833130812Smarcel
834130812Smarcel  cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
835130812Smarcel  packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
836130812Smarcel  packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
837130812Smarcel  packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
838130812Smarcel
839130812Smarcel  /* Increment the sequence number.  This will set mips_send_seq to
840130812Smarcel     the sequence number we expect in the acknowledgement.  */
841130812Smarcel  mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
842130812Smarcel
843130812Smarcel  /* We can only have one outstanding data packet, so we just wait for
844130812Smarcel     the acknowledgement here.  Keep retransmitting the packet until
845130812Smarcel     we get one, or until we've tried too many times.  */
846130812Smarcel  for (try = 0; try < mips_send_retries; try++)
847130812Smarcel    {
848130812Smarcel      int garbage;
849130812Smarcel      int ch;
850130812Smarcel
851130812Smarcel      if (remote_debug > 0)
852130812Smarcel	{
853130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
854130812Smarcel	     target_wait, and I think this might be called from there.  */
855130812Smarcel	  packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
856130812Smarcel	  fprintf_unfiltered (gdb_stdlog, "Writing \"%s\"\n", packet + 1);
857130812Smarcel	}
858130812Smarcel
859130812Smarcel      if (serial_write (mips_desc, packet,
860130812Smarcel			HDR_LENGTH + len + TRLR_LENGTH) != 0)
861130812Smarcel	mips_error ("write to target failed: %s", safe_strerror (errno));
862130812Smarcel
863130812Smarcel      if (!get_ack)
864130812Smarcel	return;
865130812Smarcel
866130812Smarcel      garbage = 0;
867130812Smarcel      ch = 0;
868130812Smarcel      while (1)
869130812Smarcel	{
870130812Smarcel	  unsigned char hdr[HDR_LENGTH + 1];
871130812Smarcel	  unsigned char trlr[TRLR_LENGTH + 1];
872130812Smarcel	  int err;
873130812Smarcel	  unsigned int seq;
874130812Smarcel
875130812Smarcel	  /* Get the packet header.  If we time out, resend the data
876130812Smarcel	     packet.  */
877130812Smarcel	  err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
878130812Smarcel	  if (err != 0)
879130812Smarcel	    break;
880130812Smarcel
881130812Smarcel	  ch = 0;
882130812Smarcel
883130812Smarcel	  /* If we get a data packet, assume it is a duplicate and
884130812Smarcel	     ignore it.  FIXME: If the acknowledgement is lost, this
885130812Smarcel	     data packet may be the packet the remote sends after the
886130812Smarcel	     acknowledgement.  */
887130812Smarcel	  if (HDR_IS_DATA (hdr))
888130812Smarcel	    {
889130812Smarcel	      int i;
890130812Smarcel
891130812Smarcel	      /* Ignore any errors raised whilst attempting to ignore
892130812Smarcel	         packet. */
893130812Smarcel
894130812Smarcel	      len = HDR_GET_LEN (hdr);
895130812Smarcel
896130812Smarcel	      for (i = 0; i < len; i++)
897130812Smarcel		{
898130812Smarcel		  int rch;
899130812Smarcel
900130812Smarcel		  rch = mips_readchar (remote_timeout);
901130812Smarcel		  if (rch == SYN)
902130812Smarcel		    {
903130812Smarcel		      ch = SYN;
904130812Smarcel		      break;
905130812Smarcel		    }
906130812Smarcel		  if (rch == SERIAL_TIMEOUT)
907130812Smarcel		    break;
908130812Smarcel		  /* ignore the character */
909130812Smarcel		}
910130812Smarcel
911130812Smarcel	      if (i == len)
912130812Smarcel		(void) mips_receive_trailer (trlr, &garbage, &ch,
913130812Smarcel					     remote_timeout);
914130812Smarcel
915130812Smarcel	      /* We don't bother checking the checksum, or providing an
916130812Smarcel	         ACK to the packet. */
917130812Smarcel	      continue;
918130812Smarcel	    }
919130812Smarcel
920130812Smarcel	  /* If the length is not 0, this is a garbled packet.  */
921130812Smarcel	  if (HDR_GET_LEN (hdr) != 0)
922130812Smarcel	    continue;
923130812Smarcel
924130812Smarcel	  /* Get the packet trailer.  */
925130812Smarcel	  err = mips_receive_trailer (trlr, &garbage, &ch,
926130812Smarcel				      mips_retransmit_wait);
927130812Smarcel
928130812Smarcel	  /* If we timed out, resend the data packet.  */
929130812Smarcel	  if (err == -1)
930130812Smarcel	    break;
931130812Smarcel
932130812Smarcel	  /* If we got a bad character, reread the header.  */
933130812Smarcel	  if (err != 0)
934130812Smarcel	    continue;
935130812Smarcel
936130812Smarcel	  /* If the checksum does not match the trailer checksum, this
937130812Smarcel	     is a bad packet; ignore it.  */
938130812Smarcel	  if (mips_cksum (hdr, (unsigned char *) NULL, 0)
939130812Smarcel	      != TRLR_GET_CKSUM (trlr))
940130812Smarcel	    continue;
941130812Smarcel
942130812Smarcel	  if (remote_debug > 0)
943130812Smarcel	    {
944130812Smarcel	      hdr[HDR_LENGTH] = '\0';
945130812Smarcel	      trlr[TRLR_LENGTH] = '\0';
946130812Smarcel	      /* Don't use _filtered; we can't deal with a QUIT out of
947130812Smarcel	         target_wait, and I think this might be called from there.  */
948130812Smarcel	      fprintf_unfiltered (gdb_stdlog, "Got ack %d \"%s%s\"\n",
949130812Smarcel				  HDR_GET_SEQ (hdr), hdr + 1, trlr);
950130812Smarcel	    }
951130812Smarcel
952130812Smarcel	  /* If this ack is for the current packet, we're done.  */
953130812Smarcel	  seq = HDR_GET_SEQ (hdr);
954130812Smarcel	  if (seq == mips_send_seq)
955130812Smarcel	    return;
956130812Smarcel
957130812Smarcel	  /* If this ack is for the last packet, resend the current
958130812Smarcel	     packet.  */
959130812Smarcel	  if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
960130812Smarcel	    break;
961130812Smarcel
962130812Smarcel	  /* Otherwise this is a bad ack; ignore it.  Increment the
963130812Smarcel	     garbage count to ensure that we do not stay in this loop
964130812Smarcel	     forever.  */
965130812Smarcel	  ++garbage;
966130812Smarcel	}
967130812Smarcel    }
968130812Smarcel
969130812Smarcel  mips_error ("Remote did not acknowledge packet");
970130812Smarcel}
971130812Smarcel
972130812Smarcel/* Receive and acknowledge a packet, returning the data in BUFF (which
973130812Smarcel   should be DATA_MAXLEN + 1 bytes).  The protocol documentation
974130812Smarcel   implies that only the sender retransmits packets, so this code just
975130812Smarcel   waits silently for a packet.  It returns the length of the received
976130812Smarcel   packet.  If THROW_ERROR is nonzero, call error() on errors.  If not,
977130812Smarcel   don't print an error message and return -1.  */
978130812Smarcel
979130812Smarcelstatic int
980130812Smarcelmips_receive_packet (char *buff, int throw_error, int timeout)
981130812Smarcel{
982130812Smarcel  int ch;
983130812Smarcel  int garbage;
984130812Smarcel  int len;
985130812Smarcel  unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
986130812Smarcel  int cksum;
987130812Smarcel
988130812Smarcel  ch = 0;
989130812Smarcel  garbage = 0;
990130812Smarcel  while (1)
991130812Smarcel    {
992130812Smarcel      unsigned char hdr[HDR_LENGTH];
993130812Smarcel      unsigned char trlr[TRLR_LENGTH];
994130812Smarcel      int i;
995130812Smarcel      int err;
996130812Smarcel
997130812Smarcel      if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
998130812Smarcel	{
999130812Smarcel	  if (throw_error)
1000130812Smarcel	    mips_error ("Timed out waiting for remote packet");
1001130812Smarcel	  else
1002130812Smarcel	    return -1;
1003130812Smarcel	}
1004130812Smarcel
1005130812Smarcel      ch = 0;
1006130812Smarcel
1007130812Smarcel      /* An acknowledgement is probably a duplicate; ignore it.  */
1008130812Smarcel      if (!HDR_IS_DATA (hdr))
1009130812Smarcel	{
1010130812Smarcel	  len = HDR_GET_LEN (hdr);
1011130812Smarcel	  /* Check if the length is valid for an ACK, we may aswell
1012130812Smarcel	     try and read the remainder of the packet: */
1013130812Smarcel	  if (len == 0)
1014130812Smarcel	    {
1015130812Smarcel	      /* Ignore the error condition, since we are going to
1016130812Smarcel	         ignore the packet anyway. */
1017130812Smarcel	      (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
1018130812Smarcel	    }
1019130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
1020130812Smarcel	     target_wait, and I think this might be called from there.  */
1021130812Smarcel	  if (remote_debug > 0)
1022130812Smarcel	    fprintf_unfiltered (gdb_stdlog, "Ignoring unexpected ACK\n");
1023130812Smarcel	  continue;
1024130812Smarcel	}
1025130812Smarcel
1026130812Smarcel      len = HDR_GET_LEN (hdr);
1027130812Smarcel      for (i = 0; i < len; i++)
1028130812Smarcel	{
1029130812Smarcel	  int rch;
1030130812Smarcel
1031130812Smarcel	  rch = mips_readchar (timeout);
1032130812Smarcel	  if (rch == SYN)
1033130812Smarcel	    {
1034130812Smarcel	      ch = SYN;
1035130812Smarcel	      break;
1036130812Smarcel	    }
1037130812Smarcel	  if (rch == SERIAL_TIMEOUT)
1038130812Smarcel	    {
1039130812Smarcel	      if (throw_error)
1040130812Smarcel		mips_error ("Timed out waiting for remote packet");
1041130812Smarcel	      else
1042130812Smarcel		return -1;
1043130812Smarcel	    }
1044130812Smarcel	  buff[i] = rch;
1045130812Smarcel	}
1046130812Smarcel
1047130812Smarcel      if (i < len)
1048130812Smarcel	{
1049130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
1050130812Smarcel	     target_wait, and I think this might be called from there.  */
1051130812Smarcel	  if (remote_debug > 0)
1052130812Smarcel	    fprintf_unfiltered (gdb_stdlog,
1053130812Smarcel				"Got new SYN after %d chars (wanted %d)\n",
1054130812Smarcel				i, len);
1055130812Smarcel	  continue;
1056130812Smarcel	}
1057130812Smarcel
1058130812Smarcel      err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
1059130812Smarcel      if (err == -1)
1060130812Smarcel	{
1061130812Smarcel	  if (throw_error)
1062130812Smarcel	    mips_error ("Timed out waiting for packet");
1063130812Smarcel	  else
1064130812Smarcel	    return -1;
1065130812Smarcel	}
1066130812Smarcel      if (err == -2)
1067130812Smarcel	{
1068130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
1069130812Smarcel	     target_wait, and I think this might be called from there.  */
1070130812Smarcel	  if (remote_debug > 0)
1071130812Smarcel	    fprintf_unfiltered (gdb_stdlog, "Got SYN when wanted trailer\n");
1072130812Smarcel	  continue;
1073130812Smarcel	}
1074130812Smarcel
1075130812Smarcel      /* If this is the wrong sequence number, ignore it.  */
1076130812Smarcel      if (HDR_GET_SEQ (hdr) != mips_receive_seq)
1077130812Smarcel	{
1078130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
1079130812Smarcel	     target_wait, and I think this might be called from there.  */
1080130812Smarcel	  if (remote_debug > 0)
1081130812Smarcel	    fprintf_unfiltered (gdb_stdlog,
1082130812Smarcel				"Ignoring sequence number %d (want %d)\n",
1083130812Smarcel				HDR_GET_SEQ (hdr), mips_receive_seq);
1084130812Smarcel	  continue;
1085130812Smarcel	}
1086130812Smarcel
1087130812Smarcel      if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
1088130812Smarcel	break;
1089130812Smarcel
1090130812Smarcel      if (remote_debug > 0)
1091130812Smarcel	/* Don't use _filtered; we can't deal with a QUIT out of
1092130812Smarcel	   target_wait, and I think this might be called from there.  */
1093130812Smarcel	printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
1094130812Smarcel			   mips_cksum (hdr, buff, len),
1095130812Smarcel			   TRLR_GET_CKSUM (trlr));
1096130812Smarcel
1097130812Smarcel      /* The checksum failed.  Send an acknowledgement for the
1098130812Smarcel         previous packet to tell the remote to resend the packet.  */
1099130812Smarcel      ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1100130812Smarcel      ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1101130812Smarcel      ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1102130812Smarcel      ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1103130812Smarcel
1104130812Smarcel      cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1105130812Smarcel
1106130812Smarcel      ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1107130812Smarcel      ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1108130812Smarcel      ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1109130812Smarcel
1110130812Smarcel      if (remote_debug > 0)
1111130812Smarcel	{
1112130812Smarcel	  ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1113130812Smarcel	  /* Don't use _filtered; we can't deal with a QUIT out of
1114130812Smarcel	     target_wait, and I think this might be called from there.  */
1115130812Smarcel	  printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1116130812Smarcel			     ack + 1);
1117130812Smarcel	}
1118130812Smarcel
1119130812Smarcel      if (serial_write (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1120130812Smarcel	{
1121130812Smarcel	  if (throw_error)
1122130812Smarcel	    mips_error ("write to target failed: %s", safe_strerror (errno));
1123130812Smarcel	  else
1124130812Smarcel	    return -1;
1125130812Smarcel	}
1126130812Smarcel    }
1127130812Smarcel
1128130812Smarcel  if (remote_debug > 0)
1129130812Smarcel    {
1130130812Smarcel      buff[len] = '\0';
1131130812Smarcel      /* Don't use _filtered; we can't deal with a QUIT out of
1132130812Smarcel         target_wait, and I think this might be called from there.  */
1133130812Smarcel      printf_unfiltered ("Got packet \"%s\"\n", buff);
1134130812Smarcel    }
1135130812Smarcel
1136130812Smarcel  /* We got the packet.  Send an acknowledgement.  */
1137130812Smarcel  mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1138130812Smarcel
1139130812Smarcel  ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1140130812Smarcel  ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1141130812Smarcel  ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1142130812Smarcel  ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1143130812Smarcel
1144130812Smarcel  cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1145130812Smarcel
1146130812Smarcel  ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1147130812Smarcel  ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1148130812Smarcel  ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1149130812Smarcel
1150130812Smarcel  if (remote_debug > 0)
1151130812Smarcel    {
1152130812Smarcel      ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1153130812Smarcel      /* Don't use _filtered; we can't deal with a QUIT out of
1154130812Smarcel         target_wait, and I think this might be called from there.  */
1155130812Smarcel      printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1156130812Smarcel			 ack + 1);
1157130812Smarcel    }
1158130812Smarcel
1159130812Smarcel  if (serial_write (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1160130812Smarcel    {
1161130812Smarcel      if (throw_error)
1162130812Smarcel	mips_error ("write to target failed: %s", safe_strerror (errno));
1163130812Smarcel      else
1164130812Smarcel	return -1;
1165130812Smarcel    }
1166130812Smarcel
1167130812Smarcel  return len;
1168130812Smarcel}
1169130812Smarcel
1170130812Smarcel/* Optionally send a request to the remote system and optionally wait
1171130812Smarcel   for the reply.  This implements the remote debugging protocol,
1172130812Smarcel   which is built on top of the packet protocol defined above.  Each
1173130812Smarcel   request has an ADDR argument and a DATA argument.  The following
1174130812Smarcel   requests are defined:
1175130812Smarcel
1176130812Smarcel   \0   don't send a request; just wait for a reply
1177130812Smarcel   i    read word from instruction space at ADDR
1178130812Smarcel   d    read word from data space at ADDR
1179130812Smarcel   I    write DATA to instruction space at ADDR
1180130812Smarcel   D    write DATA to data space at ADDR
1181130812Smarcel   r    read register number ADDR
1182130812Smarcel   R    set register number ADDR to value DATA
1183130812Smarcel   c    continue execution (if ADDR != 1, set pc to ADDR)
1184130812Smarcel   s    single step (if ADDR != 1, set pc to ADDR)
1185130812Smarcel
1186130812Smarcel   The read requests return the value requested.  The write requests
1187130812Smarcel   return the previous value in the changed location.  The execution
1188130812Smarcel   requests return a UNIX wait value (the approximate signal which
1189130812Smarcel   caused execution to stop is in the upper eight bits).
1190130812Smarcel
1191130812Smarcel   If PERR is not NULL, this function waits for a reply.  If an error
1192130812Smarcel   occurs, it sets *PERR to 1 and sets errno according to what the
1193130812Smarcel   target board reports.  */
1194130812Smarcel
1195130812Smarcelstatic ULONGEST
1196130812Smarcelmips_request (int cmd,
1197130812Smarcel	      ULONGEST addr,
1198130812Smarcel	      ULONGEST data,
1199130812Smarcel	      int *perr,
1200130812Smarcel	      int timeout,
1201130812Smarcel	      char *buff)
1202130812Smarcel{
1203130812Smarcel  char myBuff[DATA_MAXLEN + 1];
1204130812Smarcel  int len;
1205130812Smarcel  int rpid;
1206130812Smarcel  char rcmd;
1207130812Smarcel  int rerrflg;
1208130812Smarcel  unsigned long rresponse;
1209130812Smarcel
1210130812Smarcel  if (buff == (char *) NULL)
1211130812Smarcel    buff = myBuff;
1212130812Smarcel
1213130812Smarcel  if (cmd != '\0')
1214130812Smarcel    {
1215130812Smarcel      if (mips_need_reply)
1216130812Smarcel	internal_error (__FILE__, __LINE__,
1217130812Smarcel			"mips_request: Trying to send command before reply");
1218130812Smarcel      sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
1219130812Smarcel      mips_send_packet (buff, 1);
1220130812Smarcel      mips_need_reply = 1;
1221130812Smarcel    }
1222130812Smarcel
1223130812Smarcel  if (perr == (int *) NULL)
1224130812Smarcel    return 0;
1225130812Smarcel
1226130812Smarcel  if (!mips_need_reply)
1227130812Smarcel    internal_error (__FILE__, __LINE__,
1228130812Smarcel		    "mips_request: Trying to get reply before command");
1229130812Smarcel
1230130812Smarcel  mips_need_reply = 0;
1231130812Smarcel
1232130812Smarcel  len = mips_receive_packet (buff, 1, timeout);
1233130812Smarcel  buff[len] = '\0';
1234130812Smarcel
1235130812Smarcel  if (sscanf (buff, "0x%x %c 0x%x 0x%lx",
1236130812Smarcel	      &rpid, &rcmd, &rerrflg, &rresponse) != 4
1237130812Smarcel      || (cmd != '\0' && rcmd != cmd))
1238130812Smarcel    mips_error ("Bad response from remote board");
1239130812Smarcel
1240130812Smarcel  if (rerrflg != 0)
1241130812Smarcel    {
1242130812Smarcel      *perr = 1;
1243130812Smarcel
1244130812Smarcel      /* FIXME: This will returns MIPS errno numbers, which may or may
1245130812Smarcel         not be the same as errno values used on other systems.  If
1246130812Smarcel         they stick to common errno values, they will be the same, but
1247130812Smarcel         if they don't, they must be translated.  */
1248130812Smarcel      errno = rresponse;
1249130812Smarcel
1250130812Smarcel      return 0;
1251130812Smarcel    }
1252130812Smarcel
1253130812Smarcel  *perr = 0;
1254130812Smarcel  return rresponse;
1255130812Smarcel}
1256130812Smarcel
1257130812Smarcelstatic void
1258130812Smarcelmips_initialize_cleanups (void *arg)
1259130812Smarcel{
1260130812Smarcel  mips_initializing = 0;
1261130812Smarcel}
1262130812Smarcel
1263130812Smarcelstatic void
1264130812Smarcelmips_exit_cleanups (void *arg)
1265130812Smarcel{
1266130812Smarcel  mips_exiting = 0;
1267130812Smarcel}
1268130812Smarcel
1269130812Smarcelstatic void
1270130812Smarcelmips_send_command (const char *cmd, int prompt)
1271130812Smarcel{
1272130812Smarcel  serial_write (mips_desc, cmd, strlen (cmd));
1273130812Smarcel  mips_expect (cmd);
1274130812Smarcel  mips_expect ("\n");
1275130812Smarcel  if (prompt)
1276130812Smarcel    mips_expect (mips_monitor_prompt);
1277130812Smarcel}
1278130812Smarcel
1279130812Smarcel/* Enter remote (dbx) debug mode: */
1280130812Smarcelstatic void
1281130812Smarcelmips_enter_debug (void)
1282130812Smarcel{
1283130812Smarcel  /* Reset the sequence numbers, ready for the new debug sequence: */
1284130812Smarcel  mips_send_seq = 0;
1285130812Smarcel  mips_receive_seq = 0;
1286130812Smarcel
1287130812Smarcel  if (mips_monitor != MON_IDT)
1288130812Smarcel    mips_send_command ("debug\r", 0);
1289130812Smarcel  else				/* assume IDT monitor by default */
1290130812Smarcel    mips_send_command ("db tty0\r", 0);
1291130812Smarcel
1292130812Smarcel  sleep (1);
1293130812Smarcel  serial_write (mips_desc, "\r", sizeof "\r" - 1);
1294130812Smarcel
1295130812Smarcel  /* We don't need to absorb any spurious characters here, since the
1296130812Smarcel     mips_receive_header will eat up a reasonable number of characters
1297130812Smarcel     whilst looking for the SYN, however this avoids the "garbage"
1298130812Smarcel     being displayed to the user. */
1299130812Smarcel  if (mips_monitor != MON_IDT)
1300130812Smarcel    mips_expect ("\r");
1301130812Smarcel
1302130812Smarcel  {
1303130812Smarcel    char buff[DATA_MAXLEN + 1];
1304130812Smarcel    if (mips_receive_packet (buff, 1, 3) < 0)
1305130812Smarcel      mips_error ("Failed to initialize (didn't receive packet).");
1306130812Smarcel  }
1307130812Smarcel}
1308130812Smarcel
1309130812Smarcel/* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1310130812Smarcelstatic int
1311130812Smarcelmips_exit_debug (void)
1312130812Smarcel{
1313130812Smarcel  int err;
1314130812Smarcel  struct cleanup *old_cleanups = make_cleanup (mips_exit_cleanups, NULL);
1315130812Smarcel
1316130812Smarcel  mips_exiting = 1;
1317130812Smarcel
1318130812Smarcel  if (mips_monitor != MON_IDT)
1319130812Smarcel    {
1320130812Smarcel      /* The DDB (NEC) and MiniRISC (LSI) versions of PMON exit immediately,
1321130812Smarcel         so we do not get a reply to this command: */
1322130812Smarcel      mips_request ('x', 0, 0, NULL, mips_receive_wait, NULL);
1323130812Smarcel      mips_need_reply = 0;
1324130812Smarcel      if (!mips_expect (" break!"))
1325130812Smarcel	return -1;
1326130812Smarcel    }
1327130812Smarcel  else
1328130812Smarcel    mips_request ('x', 0, 0, &err, mips_receive_wait, NULL);
1329130812Smarcel
1330130812Smarcel  if (!mips_expect (mips_monitor_prompt))
1331130812Smarcel    return -1;
1332130812Smarcel
1333130812Smarcel  do_cleanups (old_cleanups);
1334130812Smarcel
1335130812Smarcel  return 0;
1336130812Smarcel}
1337130812Smarcel
1338130812Smarcel/* Initialize a new connection to the MIPS board, and make sure we are
1339130812Smarcel   really connected.  */
1340130812Smarcel
1341130812Smarcelstatic void
1342130812Smarcelmips_initialize (void)
1343130812Smarcel{
1344130812Smarcel  int err;
1345130812Smarcel  struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1346130812Smarcel  int j;
1347130812Smarcel
1348130812Smarcel  /* What is this code doing here?  I don't see any way it can happen, and
1349130812Smarcel     it might mean mips_initializing didn't get cleared properly.
1350130812Smarcel     So I'll make it a warning.  */
1351130812Smarcel
1352130812Smarcel  if (mips_initializing)
1353130812Smarcel    {
1354130812Smarcel      warning ("internal error: mips_initialize called twice");
1355130812Smarcel      return;
1356130812Smarcel    }
1357130812Smarcel
1358130812Smarcel  mips_wait_flag = 0;
1359130812Smarcel  mips_initializing = 1;
1360130812Smarcel
1361130812Smarcel  /* At this point, the packit protocol isn't responding.  We'll try getting
1362130812Smarcel     into the monitor, and restarting the protocol.  */
1363130812Smarcel
1364130812Smarcel  /* Force the system into the monitor.  After this we *should* be at
1365130812Smarcel     the mips_monitor_prompt.  */
1366130812Smarcel  if (mips_monitor != MON_IDT)
1367130812Smarcel    j = 0;			/* start by checking if we are already at the prompt */
1368130812Smarcel  else
1369130812Smarcel    j = 1;			/* start by sending a break */
1370130812Smarcel  for (; j <= 4; j++)
1371130812Smarcel    {
1372130812Smarcel      switch (j)
1373130812Smarcel	{
1374130812Smarcel	case 0:		/* First, try sending a CR */
1375130812Smarcel	  serial_flush_input (mips_desc);
1376130812Smarcel	  serial_write (mips_desc, "\r", 1);
1377130812Smarcel	  break;
1378130812Smarcel	case 1:		/* First, try sending a break */
1379130812Smarcel	  serial_send_break (mips_desc);
1380130812Smarcel	  break;
1381130812Smarcel	case 2:		/* Then, try a ^C */
1382130812Smarcel	  serial_write (mips_desc, "\003", 1);
1383130812Smarcel	  break;
1384130812Smarcel	case 3:		/* Then, try escaping from download */
1385130812Smarcel	  {
1386130812Smarcel	    if (mips_monitor != MON_IDT)
1387130812Smarcel	      {
1388130812Smarcel		char tbuff[7];
1389130812Smarcel
1390130812Smarcel		/* We shouldn't need to send multiple termination
1391130812Smarcel		   sequences, since the target performs line (or
1392130812Smarcel		   block) reads, and then processes those
1393130812Smarcel		   packets. In-case we were downloading a large packet
1394130812Smarcel		   we flush the output buffer before inserting a
1395130812Smarcel		   termination sequence. */
1396130812Smarcel		serial_flush_output (mips_desc);
1397130812Smarcel		sprintf (tbuff, "\r/E/E\r");
1398130812Smarcel		serial_write (mips_desc, tbuff, 6);
1399130812Smarcel	      }
1400130812Smarcel	    else
1401130812Smarcel	      {
1402130812Smarcel		char srec[10];
1403130812Smarcel		int i;
1404130812Smarcel
1405130812Smarcel		/* We are possibly in binary download mode, having
1406130812Smarcel		   aborted in the middle of an S-record.  ^C won't
1407130812Smarcel		   work because of binary mode.  The only reliable way
1408130812Smarcel		   out is to send enough termination packets (8 bytes)
1409130812Smarcel		   to fill up and then overflow the largest size
1410130812Smarcel		   S-record (255 bytes in this case).  This amounts to
1411130812Smarcel		   256/8 + 1 packets.
1412130812Smarcel		 */
1413130812Smarcel
1414130812Smarcel		mips_make_srec (srec, '7', 0, NULL, 0);
1415130812Smarcel
1416130812Smarcel		for (i = 1; i <= 33; i++)
1417130812Smarcel		  {
1418130812Smarcel		    serial_write (mips_desc, srec, 8);
1419130812Smarcel
1420130812Smarcel		    if (serial_readchar (mips_desc, 0) >= 0)
1421130812Smarcel		      break;	/* Break immediatly if we get something from
1422130812Smarcel				   the board. */
1423130812Smarcel		  }
1424130812Smarcel	      }
1425130812Smarcel	  }
1426130812Smarcel	  break;
1427130812Smarcel	case 4:
1428130812Smarcel	  mips_error ("Failed to initialize.");
1429130812Smarcel	}
1430130812Smarcel
1431130812Smarcel      if (mips_expect (mips_monitor_prompt))
1432130812Smarcel	break;
1433130812Smarcel    }
1434130812Smarcel
1435130812Smarcel  if (mips_monitor != MON_IDT)
1436130812Smarcel    {
1437130812Smarcel      /* Sometimes PMON ignores the first few characters in the first
1438130812Smarcel         command sent after a load.  Sending a blank command gets
1439130812Smarcel         around that.  */
1440130812Smarcel      mips_send_command ("\r", -1);
1441130812Smarcel
1442130812Smarcel      /* Ensure the correct target state: */
1443130812Smarcel      if (mips_monitor != MON_LSI)
1444130812Smarcel	mips_send_command ("set regsize 64\r", -1);
1445130812Smarcel      mips_send_command ("set hostport tty0\r", -1);
1446130812Smarcel      mips_send_command ("set brkcmd \"\"\r", -1);
1447130812Smarcel      /* Delete all the current breakpoints: */
1448130812Smarcel      mips_send_command ("db *\r", -1);
1449130812Smarcel      /* NOTE: PMON does not have breakpoint support through the
1450130812Smarcel         "debug" mode, only at the monitor command-line. */
1451130812Smarcel    }
1452130812Smarcel
1453130812Smarcel  mips_enter_debug ();
1454130812Smarcel
1455130812Smarcel  /* Clear all breakpoints: */
1456130812Smarcel  if ((mips_monitor == MON_IDT
1457130812Smarcel       && clear_breakpoint (-1, 0, BREAK_UNUSED) == 0)
1458130812Smarcel      || mips_monitor == MON_LSI)
1459130812Smarcel    monitor_supports_breakpoints = 1;
1460130812Smarcel  else
1461130812Smarcel    monitor_supports_breakpoints = 0;
1462130812Smarcel
1463130812Smarcel  do_cleanups (old_cleanups);
1464130812Smarcel
1465130812Smarcel  /* If this doesn't call error, we have connected; we don't care if
1466130812Smarcel     the request itself succeeds or fails.  */
1467130812Smarcel
1468130812Smarcel  mips_request ('r', 0, 0, &err, mips_receive_wait, NULL);
1469130812Smarcel}
1470130812Smarcel
1471130812Smarcel/* Open a connection to the remote board.  */
1472130812Smarcelstatic void
1473130812Smarcelcommon_open (struct target_ops *ops, char *name, int from_tty,
1474130812Smarcel	     enum mips_monitor_type new_monitor,
1475130812Smarcel	     const char *new_monitor_prompt)
1476130812Smarcel{
1477130812Smarcel  char *ptype;
1478130812Smarcel  char *serial_port_name;
1479130812Smarcel  char *remote_name = 0;
1480130812Smarcel  char *local_name = 0;
1481130812Smarcel  char **argv;
1482130812Smarcel
1483130812Smarcel  if (name == 0)
1484130812Smarcel    error (
1485130812Smarcel	    "To open a MIPS remote debugging connection, you need to specify what serial\n\
1486130812Smarceldevice is attached to the target board (e.g., /dev/ttya).\n"
1487130812Smarcel	    "If you want to use TFTP to download to the board, specify the name of a\n"
1488130812Smarcel	    "temporary file to be used by GDB for downloads as the second argument.\n"
1489130812Smarcel	    "This filename must be in the form host:filename, where host is the name\n"
1490130812Smarcel	    "of the host running the TFTP server, and the file must be readable by the\n"
1491130812Smarcel	    "world.  If the local name of the temporary file differs from the name as\n"
1492130812Smarcel	    "seen from the board via TFTP, specify that name as the third parameter.\n");
1493130812Smarcel
1494130812Smarcel  /* Parse the serial port name, the optional TFTP name, and the
1495130812Smarcel     optional local TFTP name.  */
1496130812Smarcel  if ((argv = buildargv (name)) == NULL)
1497130812Smarcel    nomem (0);
1498130812Smarcel  make_cleanup_freeargv (argv);
1499130812Smarcel
1500130812Smarcel  serial_port_name = xstrdup (argv[0]);
1501130812Smarcel  if (argv[1])			/* remote TFTP name specified? */
1502130812Smarcel    {
1503130812Smarcel      remote_name = argv[1];
1504130812Smarcel      if (argv[2])		/* local TFTP filename specified? */
1505130812Smarcel	local_name = argv[2];
1506130812Smarcel    }
1507130812Smarcel
1508130812Smarcel  target_preopen (from_tty);
1509130812Smarcel
1510130812Smarcel  if (mips_is_open)
1511130812Smarcel    unpush_target (current_ops);
1512130812Smarcel
1513130812Smarcel  /* Open and initialize the serial port.  */
1514130812Smarcel  mips_desc = serial_open (serial_port_name);
1515130812Smarcel  if (mips_desc == NULL)
1516130812Smarcel    perror_with_name (serial_port_name);
1517130812Smarcel
1518130812Smarcel  if (baud_rate != -1)
1519130812Smarcel    {
1520130812Smarcel      if (serial_setbaudrate (mips_desc, baud_rate))
1521130812Smarcel	{
1522130812Smarcel	  serial_close (mips_desc);
1523130812Smarcel	  perror_with_name (serial_port_name);
1524130812Smarcel	}
1525130812Smarcel    }
1526130812Smarcel
1527130812Smarcel  serial_raw (mips_desc);
1528130812Smarcel
1529130812Smarcel  /* Open and initialize the optional download port.  If it is in the form
1530130812Smarcel     hostname#portnumber, it's a UDP socket.  If it is in the form
1531130812Smarcel     hostname:filename, assume it's the TFTP filename that must be
1532130812Smarcel     passed to the DDB board to tell it where to get the load file.  */
1533130812Smarcel  if (remote_name)
1534130812Smarcel    {
1535130812Smarcel      if (strchr (remote_name, '#'))
1536130812Smarcel	{
1537130812Smarcel	  udp_desc = serial_open (remote_name);
1538130812Smarcel	  if (!udp_desc)
1539130812Smarcel	    perror_with_name ("Unable to open UDP port");
1540130812Smarcel	  udp_in_use = 1;
1541130812Smarcel	}
1542130812Smarcel      else
1543130812Smarcel	{
1544130812Smarcel	  /* Save the remote and local names of the TFTP temp file.  If
1545130812Smarcel	     the user didn't specify a local name, assume it's the same
1546130812Smarcel	     as the part of the remote name after the "host:".  */
1547130812Smarcel	  if (tftp_name)
1548130812Smarcel	    xfree (tftp_name);
1549130812Smarcel	  if (tftp_localname)
1550130812Smarcel	    xfree (tftp_localname);
1551130812Smarcel	  if (local_name == NULL)
1552130812Smarcel	    if ((local_name = strchr (remote_name, ':')) != NULL)
1553130812Smarcel	      local_name++;	/* skip over the colon */
1554130812Smarcel	  if (local_name == NULL)
1555130812Smarcel	    local_name = remote_name;	/* local name same as remote name */
1556130812Smarcel	  tftp_name = xstrdup (remote_name);
1557130812Smarcel	  tftp_localname = xstrdup (local_name);
1558130812Smarcel	  tftp_in_use = 1;
1559130812Smarcel	}
1560130812Smarcel    }
1561130812Smarcel
1562130812Smarcel  current_ops = ops;
1563130812Smarcel  mips_is_open = 1;
1564130812Smarcel
1565130812Smarcel  /* Reset the expected monitor prompt if it's never been set before.  */
1566130812Smarcel  if (mips_monitor_prompt == NULL)
1567130812Smarcel    mips_monitor_prompt = xstrdup (new_monitor_prompt);
1568130812Smarcel  mips_monitor = new_monitor;
1569130812Smarcel
1570130812Smarcel  mips_initialize ();
1571130812Smarcel
1572130812Smarcel  if (from_tty)
1573130812Smarcel    printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
1574130812Smarcel
1575130812Smarcel  /* Switch to using remote target now.  */
1576130812Smarcel  push_target (ops);
1577130812Smarcel
1578130812Smarcel  /* FIXME: Should we call start_remote here?  */
1579130812Smarcel
1580130812Smarcel  /* Try to figure out the processor model if possible.  */
1581130812Smarcel  deprecated_mips_set_processor_regs_hack ();
1582130812Smarcel
1583130812Smarcel  /* This is really the job of start_remote however, that makes an
1584130812Smarcel     assumption that the target is about to print out a status message
1585130812Smarcel     of some sort.  That doesn't happen here (in fact, it may not be
1586130812Smarcel     possible to get the monitor to send the appropriate packet).  */
1587130812Smarcel
1588130812Smarcel  flush_cached_frames ();
1589130812Smarcel  registers_changed ();
1590130812Smarcel  stop_pc = read_pc ();
1591130812Smarcel  print_stack_frame (get_selected_frame (), -1, 1);
1592130812Smarcel  xfree (serial_port_name);
1593130812Smarcel}
1594130812Smarcel
1595130812Smarcelstatic void
1596130812Smarcelmips_open (char *name, int from_tty)
1597130812Smarcel{
1598130812Smarcel  const char *monitor_prompt = NULL;
1599130812Smarcel  if (TARGET_ARCHITECTURE != NULL
1600130812Smarcel      && TARGET_ARCHITECTURE->arch == bfd_arch_mips)
1601130812Smarcel    {
1602130812Smarcel    switch (TARGET_ARCHITECTURE->mach)
1603130812Smarcel      {
1604130812Smarcel      case bfd_mach_mips4100:
1605130812Smarcel      case bfd_mach_mips4300:
1606130812Smarcel      case bfd_mach_mips4600:
1607130812Smarcel      case bfd_mach_mips4650:
1608130812Smarcel      case bfd_mach_mips5000:
1609130812Smarcel	monitor_prompt = "<RISQ> ";
1610130812Smarcel	break;
1611130812Smarcel      }
1612130812Smarcel    }
1613130812Smarcel  if (monitor_prompt == NULL)
1614130812Smarcel    monitor_prompt = "<IDT>";
1615130812Smarcel  common_open (&mips_ops, name, from_tty, MON_IDT, monitor_prompt);
1616130812Smarcel}
1617130812Smarcel
1618130812Smarcelstatic void
1619130812Smarcelpmon_open (char *name, int from_tty)
1620130812Smarcel{
1621130812Smarcel  common_open (&pmon_ops, name, from_tty, MON_PMON, "PMON> ");
1622130812Smarcel}
1623130812Smarcel
1624130812Smarcelstatic void
1625130812Smarcelddb_open (char *name, int from_tty)
1626130812Smarcel{
1627130812Smarcel  common_open (&ddb_ops, name, from_tty, MON_DDB, "NEC010>");
1628130812Smarcel}
1629130812Smarcel
1630130812Smarcelstatic void
1631130812Smarcellsi_open (char *name, int from_tty)
1632130812Smarcel{
1633130812Smarcel  int i;
1634130812Smarcel
1635130812Smarcel  /* Clear the LSI breakpoint table.  */
1636130812Smarcel  for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1637130812Smarcel    lsi_breakpoints[i].type = BREAK_UNUSED;
1638130812Smarcel
1639130812Smarcel  common_open (&lsi_ops, name, from_tty, MON_LSI, "PMON> ");
1640130812Smarcel}
1641130812Smarcel
1642130812Smarcel/* Close a connection to the remote board.  */
1643130812Smarcel
1644130812Smarcelstatic void
1645130812Smarcelmips_close (int quitting)
1646130812Smarcel{
1647130812Smarcel  if (mips_is_open)
1648130812Smarcel    {
1649130812Smarcel      /* Get the board out of remote debugging mode.  */
1650130812Smarcel      (void) mips_exit_debug ();
1651130812Smarcel
1652130812Smarcel      close_ports ();
1653130812Smarcel    }
1654130812Smarcel}
1655130812Smarcel
1656130812Smarcel/* Detach from the remote board.  */
1657130812Smarcel
1658130812Smarcelstatic void
1659130812Smarcelmips_detach (char *args, int from_tty)
1660130812Smarcel{
1661130812Smarcel  if (args)
1662130812Smarcel    error ("Argument given to \"detach\" when remotely debugging.");
1663130812Smarcel
1664130812Smarcel  pop_target ();
1665130812Smarcel
1666130812Smarcel  mips_close (1);
1667130812Smarcel
1668130812Smarcel  if (from_tty)
1669130812Smarcel    printf_unfiltered ("Ending remote MIPS debugging.\n");
1670130812Smarcel}
1671130812Smarcel
1672130812Smarcel/* Tell the target board to resume.  This does not wait for a reply
1673130812Smarcel   from the board, except in the case of single-stepping on LSI boards,
1674130812Smarcel   where PMON does return a reply.  */
1675130812Smarcel
1676130812Smarcelstatic void
1677130812Smarcelmips_resume (ptid_t ptid, int step, enum target_signal siggnal)
1678130812Smarcel{
1679130812Smarcel  int err;
1680130812Smarcel
1681130812Smarcel  /* LSI PMON requires returns a reply packet "0x1 s 0x0 0x57f" after
1682130812Smarcel     a single step, so we wait for that.  */
1683130812Smarcel  mips_request (step ? 's' : 'c', 1, siggnal,
1684130812Smarcel		mips_monitor == MON_LSI && step ? &err : (int *) NULL,
1685130812Smarcel		mips_receive_wait, NULL);
1686130812Smarcel}
1687130812Smarcel
1688130812Smarcel/* Return the signal corresponding to SIG, where SIG is the number which
1689130812Smarcel   the MIPS protocol uses for the signal.  */
1690130812Smarcelstatic enum target_signal
1691130812Smarcelmips_signal_from_protocol (int sig)
1692130812Smarcel{
1693130812Smarcel  /* We allow a few more signals than the IDT board actually returns, on
1694130812Smarcel     the theory that there is at least *some* hope that perhaps the numbering
1695130812Smarcel     for these signals is widely agreed upon.  */
1696130812Smarcel  if (sig <= 0
1697130812Smarcel      || sig > 31)
1698130812Smarcel    return TARGET_SIGNAL_UNKNOWN;
1699130812Smarcel
1700130812Smarcel  /* Don't want to use target_signal_from_host because we are converting
1701130812Smarcel     from MIPS signal numbers, not host ones.  Our internal numbers
1702130812Smarcel     match the MIPS numbers for the signals the board can return, which
1703130812Smarcel     are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP.  */
1704130812Smarcel  return (enum target_signal) sig;
1705130812Smarcel}
1706130812Smarcel
1707130812Smarcel/* Wait until the remote stops, and return a wait status.  */
1708130812Smarcel
1709130812Smarcelstatic ptid_t
1710130812Smarcelmips_wait (ptid_t ptid, struct target_waitstatus *status)
1711130812Smarcel{
1712130812Smarcel  int rstatus;
1713130812Smarcel  int err;
1714130812Smarcel  char buff[DATA_MAXLEN];
1715130812Smarcel  int rpc, rfp, rsp;
1716130812Smarcel  char flags[20];
1717130812Smarcel  int nfields;
1718130812Smarcel  int i;
1719130812Smarcel
1720130812Smarcel  interrupt_count = 0;
1721130812Smarcel  hit_watchpoint = 0;
1722130812Smarcel
1723130812Smarcel  /* If we have not sent a single step or continue command, then the
1724130812Smarcel     board is waiting for us to do something.  Return a status
1725130812Smarcel     indicating that it is stopped.  */
1726130812Smarcel  if (!mips_need_reply)
1727130812Smarcel    {
1728130812Smarcel      status->kind = TARGET_WAITKIND_STOPPED;
1729130812Smarcel      status->value.sig = TARGET_SIGNAL_TRAP;
1730130812Smarcel      return inferior_ptid;
1731130812Smarcel    }
1732130812Smarcel
1733130812Smarcel  /* No timeout; we sit here as long as the program continues to execute.  */
1734130812Smarcel  mips_wait_flag = 1;
1735130812Smarcel  rstatus = mips_request ('\000', 0, 0, &err, -1, buff);
1736130812Smarcel  mips_wait_flag = 0;
1737130812Smarcel  if (err)
1738130812Smarcel    mips_error ("Remote failure: %s", safe_strerror (errno));
1739130812Smarcel
1740130812Smarcel  /* On returning from a continue, the PMON monitor seems to start
1741130812Smarcel     echoing back the messages we send prior to sending back the
1742130812Smarcel     ACK. The code can cope with this, but to try and avoid the
1743130812Smarcel     unnecessary serial traffic, and "spurious" characters displayed
1744130812Smarcel     to the user, we cheat and reset the debug protocol. The problems
1745130812Smarcel     seems to be caused by a check on the number of arguments, and the
1746130812Smarcel     command length, within the monitor causing it to echo the command
1747130812Smarcel     as a bad packet. */
1748130812Smarcel  if (mips_monitor == MON_PMON)
1749130812Smarcel    {
1750130812Smarcel      mips_exit_debug ();
1751130812Smarcel      mips_enter_debug ();
1752130812Smarcel    }
1753130812Smarcel
1754130812Smarcel  /* See if we got back extended status.  If so, pick out the pc, fp, sp, etc... */
1755130812Smarcel
1756130812Smarcel  nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1757130812Smarcel		    &rpc, &rfp, &rsp, flags);
1758130812Smarcel  if (nfields >= 3)
1759130812Smarcel    {
1760130812Smarcel      char buf[MAX_REGISTER_SIZE];
1761130812Smarcel
1762130812Smarcel      store_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1763130812Smarcel      supply_register (PC_REGNUM, buf);
1764130812Smarcel
1765130812Smarcel      store_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1766130812Smarcel      supply_register (30, buf);	/* This register they are avoiding and so it is unnamed */
1767130812Smarcel
1768130812Smarcel      store_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1769130812Smarcel      supply_register (SP_REGNUM, buf);
1770130812Smarcel
1771130812Smarcel      store_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (DEPRECATED_FP_REGNUM), 0);
1772130812Smarcel      supply_register (DEPRECATED_FP_REGNUM, buf);
1773130812Smarcel
1774130812Smarcel      if (nfields == 9)
1775130812Smarcel	{
1776130812Smarcel	  int i;
1777130812Smarcel
1778130812Smarcel	  for (i = 0; i <= 2; i++)
1779130812Smarcel	    if (flags[i] == 'r' || flags[i] == 'w')
1780130812Smarcel	      hit_watchpoint = 1;
1781130812Smarcel	    else if (flags[i] == '\000')
1782130812Smarcel	      break;
1783130812Smarcel	}
1784130812Smarcel    }
1785130812Smarcel
1786130812Smarcel  if (strcmp (target_shortname, "lsi") == 0)
1787130812Smarcel    {
1788130812Smarcel#if 0
1789130812Smarcel      /* If this is an LSI PMON target, see if we just hit a hardrdware watchpoint.
1790130812Smarcel         Right now, PMON doesn't give us enough information to determine which
1791130812Smarcel         breakpoint we hit.  So we have to look up the PC in our own table
1792130812Smarcel         of breakpoints, and if found, assume it's just a normal instruction
1793130812Smarcel         fetch breakpoint, not a data watchpoint.  FIXME when PMON
1794130812Smarcel         provides some way to tell us what type of breakpoint it is.  */
1795130812Smarcel      int i;
1796130812Smarcel      CORE_ADDR pc = read_pc ();
1797130812Smarcel
1798130812Smarcel      hit_watchpoint = 1;
1799130812Smarcel      for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1800130812Smarcel	{
1801130812Smarcel	  if (lsi_breakpoints[i].addr == pc
1802130812Smarcel	      && lsi_breakpoints[i].type == BREAK_FETCH)
1803130812Smarcel	    {
1804130812Smarcel	      hit_watchpoint = 0;
1805130812Smarcel	      break;
1806130812Smarcel	    }
1807130812Smarcel	}
1808130812Smarcel#else
1809130812Smarcel      /* If a data breakpoint was hit, PMON returns the following packet:
1810130812Smarcel         0x1 c 0x0 0x57f 0x1
1811130812Smarcel         The return packet from an ordinary breakpoint doesn't have the
1812130812Smarcel         extra 0x01 field tacked onto the end.  */
1813130812Smarcel      if (nfields == 1 && rpc == 1)
1814130812Smarcel	hit_watchpoint = 1;
1815130812Smarcel#endif
1816130812Smarcel    }
1817130812Smarcel
1818130812Smarcel  /* NOTE: The following (sig) numbers are defined by PMON:
1819130812Smarcel     SPP_SIGTRAP     5       breakpoint
1820130812Smarcel     SPP_SIGINT      2
1821130812Smarcel     SPP_SIGSEGV     11
1822130812Smarcel     SPP_SIGBUS      10
1823130812Smarcel     SPP_SIGILL      4
1824130812Smarcel     SPP_SIGFPE      8
1825130812Smarcel     SPP_SIGTERM     15 */
1826130812Smarcel
1827130812Smarcel  /* Translate a MIPS waitstatus.  We use constants here rather than WTERMSIG
1828130812Smarcel     and so on, because the constants we want here are determined by the
1829130812Smarcel     MIPS protocol and have nothing to do with what host we are running on.  */
1830130812Smarcel  if ((rstatus & 0xff) == 0)
1831130812Smarcel    {
1832130812Smarcel      status->kind = TARGET_WAITKIND_EXITED;
1833130812Smarcel      status->value.integer = (((rstatus) >> 8) & 0xff);
1834130812Smarcel    }
1835130812Smarcel  else if ((rstatus & 0xff) == 0x7f)
1836130812Smarcel    {
1837130812Smarcel      status->kind = TARGET_WAITKIND_STOPPED;
1838130812Smarcel      status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0xff);
1839130812Smarcel
1840130812Smarcel      /* If the stop PC is in the _exit function, assume
1841130812Smarcel         we hit the 'break 0x3ff' instruction in _exit, so this
1842130812Smarcel         is not a normal breakpoint.  */
1843130812Smarcel      if (strcmp (target_shortname, "lsi") == 0)
1844130812Smarcel	{
1845130812Smarcel	  char *func_name;
1846130812Smarcel	  CORE_ADDR func_start;
1847130812Smarcel	  CORE_ADDR pc = read_pc ();
1848130812Smarcel
1849130812Smarcel	  find_pc_partial_function (pc, &func_name, &func_start, NULL);
1850130812Smarcel	  if (func_name != NULL && strcmp (func_name, "_exit") == 0
1851130812Smarcel	      && func_start == pc)
1852130812Smarcel	    status->kind = TARGET_WAITKIND_EXITED;
1853130812Smarcel	}
1854130812Smarcel    }
1855130812Smarcel  else
1856130812Smarcel    {
1857130812Smarcel      status->kind = TARGET_WAITKIND_SIGNALLED;
1858130812Smarcel      status->value.sig = mips_signal_from_protocol (rstatus & 0x7f);
1859130812Smarcel    }
1860130812Smarcel
1861130812Smarcel  return inferior_ptid;
1862130812Smarcel}
1863130812Smarcel
1864130812Smarcel/* We have to map between the register numbers used by gdb and the
1865130812Smarcel   register numbers used by the debugging protocol.  This function
1866130812Smarcel   assumes that we are using tm-mips.h.  */
1867130812Smarcel
1868130812Smarcel#define REGNO_OFFSET 96
1869130812Smarcel
1870130812Smarcelstatic int
1871130812Smarcelmips_map_regno (int regno)
1872130812Smarcel{
1873130812Smarcel  if (regno < 32)
1874130812Smarcel    return regno;
1875130812Smarcel  if (regno >= mips_regnum (current_gdbarch)->fp0
1876130812Smarcel      && regno < mips_regnum (current_gdbarch)->fp0 + 32)
1877130812Smarcel    return regno - mips_regnum (current_gdbarch)->fp0 + 32;
1878130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->pc)
1879130812Smarcel    return REGNO_OFFSET + 0;
1880130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->cause)
1881130812Smarcel    return REGNO_OFFSET + 1;
1882130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->hi)
1883130812Smarcel    return REGNO_OFFSET + 2;
1884130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->lo)
1885130812Smarcel    return REGNO_OFFSET + 3;
1886130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
1887130812Smarcel    return REGNO_OFFSET + 4;
1888130812Smarcel  else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
1889130812Smarcel    return REGNO_OFFSET + 5;
1890130812Smarcel  else
1891130812Smarcel    /* FIXME: Is there a way to get the status register?  */
1892130812Smarcel    return 0;
1893130812Smarcel}
1894130812Smarcel
1895130812Smarcel/* Fetch the remote registers.  */
1896130812Smarcel
1897130812Smarcelstatic void
1898130812Smarcelmips_fetch_registers (int regno)
1899130812Smarcel{
1900130812Smarcel  unsigned LONGEST val;
1901130812Smarcel  int err;
1902130812Smarcel
1903130812Smarcel  if (regno == -1)
1904130812Smarcel    {
1905130812Smarcel      for (regno = 0; regno < NUM_REGS; regno++)
1906130812Smarcel	mips_fetch_registers (regno);
1907130812Smarcel      return;
1908130812Smarcel    }
1909130812Smarcel
1910130812Smarcel  if (regno == DEPRECATED_FP_REGNUM || regno == ZERO_REGNUM)
1911130812Smarcel    /* DEPRECATED_FP_REGNUM on the mips is a hack which is just
1912130812Smarcel       supposed to read zero (see also mips-nat.c).  */
1913130812Smarcel    val = 0;
1914130812Smarcel  else
1915130812Smarcel    {
1916130812Smarcel      /* If PMON doesn't support this register, don't waste serial
1917130812Smarcel         bandwidth trying to read it.  */
1918130812Smarcel      int pmon_reg = mips_map_regno (regno);
1919130812Smarcel      if (regno != 0 && pmon_reg == 0)
1920130812Smarcel	val = 0;
1921130812Smarcel      else
1922130812Smarcel	{
1923130812Smarcel	  /* Unfortunately the PMON version in the Vr4300 board has been
1924130812Smarcel	     compiled without the 64bit register access commands. This
1925130812Smarcel	     means we cannot get hold of the full register width. */
1926130812Smarcel	  if (mips_monitor == MON_DDB)
1927130812Smarcel	    val = (unsigned) mips_request ('t', pmon_reg, 0,
1928130812Smarcel					   &err, mips_receive_wait, NULL);
1929130812Smarcel	  else
1930130812Smarcel	    val = mips_request ('r', pmon_reg, 0,
1931130812Smarcel				&err, mips_receive_wait, NULL);
1932130812Smarcel	  if (err)
1933130812Smarcel	    mips_error ("Can't read register %d: %s", regno,
1934130812Smarcel			safe_strerror (errno));
1935130812Smarcel	}
1936130812Smarcel    }
1937130812Smarcel
1938130812Smarcel  {
1939130812Smarcel    char buf[MAX_REGISTER_SIZE];
1940130812Smarcel
1941130812Smarcel    /* We got the number the register holds, but gdb expects to see a
1942130812Smarcel       value in the target byte ordering.  */
1943130812Smarcel    store_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (regno), val);
1944130812Smarcel    supply_register (regno, buf);
1945130812Smarcel  }
1946130812Smarcel}
1947130812Smarcel
1948130812Smarcel/* Prepare to store registers.  The MIPS protocol can store individual
1949130812Smarcel   registers, so this function doesn't have to do anything.  */
1950130812Smarcel
1951130812Smarcelstatic void
1952130812Smarcelmips_prepare_to_store (void)
1953130812Smarcel{
1954130812Smarcel}
1955130812Smarcel
1956130812Smarcel/* Store remote register(s).  */
1957130812Smarcel
1958130812Smarcelstatic void
1959130812Smarcelmips_store_registers (int regno)
1960130812Smarcel{
1961130812Smarcel  int err;
1962130812Smarcel
1963130812Smarcel  if (regno == -1)
1964130812Smarcel    {
1965130812Smarcel      for (regno = 0; regno < NUM_REGS; regno++)
1966130812Smarcel	mips_store_registers (regno);
1967130812Smarcel      return;
1968130812Smarcel    }
1969130812Smarcel
1970130812Smarcel  mips_request ('R', mips_map_regno (regno),
1971130812Smarcel		read_register (regno),
1972130812Smarcel		&err, mips_receive_wait, NULL);
1973130812Smarcel  if (err)
1974130812Smarcel    mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1975130812Smarcel}
1976130812Smarcel
1977130812Smarcel/* Fetch a word from the target board.  */
1978130812Smarcel
1979130812Smarcelstatic unsigned int
1980130812Smarcelmips_fetch_word (CORE_ADDR addr)
1981130812Smarcel{
1982130812Smarcel  unsigned int val;
1983130812Smarcel  int err;
1984130812Smarcel
1985130812Smarcel  val = mips_request ('d', addr, 0, &err, mips_receive_wait, NULL);
1986130812Smarcel  if (err)
1987130812Smarcel    {
1988130812Smarcel      /* Data space failed; try instruction space.  */
1989130812Smarcel      val = mips_request ('i', addr, 0, &err,
1990130812Smarcel			  mips_receive_wait, NULL);
1991130812Smarcel      if (err)
1992130812Smarcel	mips_error ("Can't read address 0x%s: %s",
1993130812Smarcel		    paddr_nz (addr), safe_strerror (errno));
1994130812Smarcel    }
1995130812Smarcel  return val;
1996130812Smarcel}
1997130812Smarcel
1998130812Smarcel/* Store a word to the target board.  Returns errno code or zero for
1999130812Smarcel   success.  If OLD_CONTENTS is non-NULL, put the old contents of that
2000130812Smarcel   memory location there.  */
2001130812Smarcel
2002130812Smarcel/* FIXME! make sure only 32-bit quantities get stored! */
2003130812Smarcelstatic int
2004130812Smarcelmips_store_word (CORE_ADDR addr, unsigned int val, char *old_contents)
2005130812Smarcel{
2006130812Smarcel  int err;
2007130812Smarcel  unsigned int oldcontents;
2008130812Smarcel
2009130812Smarcel  oldcontents = mips_request ('D', addr, val, &err,
2010130812Smarcel			      mips_receive_wait, NULL);
2011130812Smarcel  if (err)
2012130812Smarcel    {
2013130812Smarcel      /* Data space failed; try instruction space.  */
2014130812Smarcel      oldcontents = mips_request ('I', addr, val, &err,
2015130812Smarcel				  mips_receive_wait, NULL);
2016130812Smarcel      if (err)
2017130812Smarcel	return errno;
2018130812Smarcel    }
2019130812Smarcel  if (old_contents != NULL)
2020130812Smarcel    store_unsigned_integer (old_contents, 4, oldcontents);
2021130812Smarcel  return 0;
2022130812Smarcel}
2023130812Smarcel
2024130812Smarcel/* Read or write LEN bytes from inferior memory at MEMADDR,
2025130812Smarcel   transferring to or from debugger address MYADDR.  Write to inferior
2026130812Smarcel   if SHOULD_WRITE is nonzero.  Returns length of data written or
2027130812Smarcel   read; 0 for error.  Note that protocol gives us the correct value
2028130812Smarcel   for a longword, since it transfers values in ASCII.  We want the
2029130812Smarcel   byte values, so we have to swap the longword values.  */
2030130812Smarcel
2031130812Smarcelstatic int mask_address_p = 1;
2032130812Smarcel
2033130812Smarcelstatic int
2034130812Smarcelmips_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2035130812Smarcel		  struct mem_attrib *attrib, struct target_ops *target)
2036130812Smarcel{
2037130812Smarcel  int i;
2038130812Smarcel  CORE_ADDR addr;
2039130812Smarcel  int count;
2040130812Smarcel  char *buffer;
2041130812Smarcel  int status;
2042130812Smarcel
2043130812Smarcel  /* PMON targets do not cope well with 64 bit addresses.  Mask the
2044130812Smarcel     value down to 32 bits. */
2045130812Smarcel  if (mask_address_p)
2046130812Smarcel    memaddr &= (CORE_ADDR) 0xffffffff;
2047130812Smarcel
2048130812Smarcel  /* Round starting address down to longword boundary.  */
2049130812Smarcel  addr = memaddr & ~3;
2050130812Smarcel  /* Round ending address up; get number of longwords that makes.  */
2051130812Smarcel  count = (((memaddr + len) - addr) + 3) / 4;
2052130812Smarcel  /* Allocate buffer of that many longwords.  */
2053130812Smarcel  buffer = alloca (count * 4);
2054130812Smarcel
2055130812Smarcel  if (write)
2056130812Smarcel    {
2057130812Smarcel      /* Fill start and end extra bytes of buffer with existing data.  */
2058130812Smarcel      if (addr != memaddr || len < 4)
2059130812Smarcel	{
2060130812Smarcel	  /* Need part of initial word -- fetch it.  */
2061130812Smarcel	  store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
2062130812Smarcel	}
2063130812Smarcel
2064130812Smarcel      if (count > 1)
2065130812Smarcel	{
2066130812Smarcel	  /* Need part of last word -- fetch it.  FIXME: we do this even
2067130812Smarcel	     if we don't need it.  */
2068130812Smarcel	  store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2069130812Smarcel				  mips_fetch_word (addr + (count - 1) * 4));
2070130812Smarcel	}
2071130812Smarcel
2072130812Smarcel      /* Copy data to be written over corresponding part of buffer */
2073130812Smarcel
2074130812Smarcel      memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2075130812Smarcel
2076130812Smarcel      /* Write the entire buffer.  */
2077130812Smarcel
2078130812Smarcel      for (i = 0; i < count; i++, addr += 4)
2079130812Smarcel	{
2080130812Smarcel	  status = mips_store_word (addr,
2081130812Smarcel			       extract_unsigned_integer (&buffer[i * 4], 4),
2082130812Smarcel				    NULL);
2083130812Smarcel	  /* Report each kilobyte (we download 32-bit words at a time) */
2084130812Smarcel	  if (i % 256 == 255)
2085130812Smarcel	    {
2086130812Smarcel	      printf_unfiltered ("*");
2087130812Smarcel	      gdb_flush (gdb_stdout);
2088130812Smarcel	    }
2089130812Smarcel	  if (status)
2090130812Smarcel	    {
2091130812Smarcel	      errno = status;
2092130812Smarcel	      return 0;
2093130812Smarcel	    }
2094130812Smarcel	  /* FIXME: Do we want a QUIT here?  */
2095130812Smarcel	}
2096130812Smarcel      if (count >= 256)
2097130812Smarcel	printf_unfiltered ("\n");
2098130812Smarcel    }
2099130812Smarcel  else
2100130812Smarcel    {
2101130812Smarcel      /* Read all the longwords */
2102130812Smarcel      for (i = 0; i < count; i++, addr += 4)
2103130812Smarcel	{
2104130812Smarcel	  store_unsigned_integer (&buffer[i * 4], 4, mips_fetch_word (addr));
2105130812Smarcel	  QUIT;
2106130812Smarcel	}
2107130812Smarcel
2108130812Smarcel      /* Copy appropriate bytes out of the buffer.  */
2109130812Smarcel      memcpy (myaddr, buffer + (memaddr & 3), len);
2110130812Smarcel    }
2111130812Smarcel  return len;
2112130812Smarcel}
2113130812Smarcel
2114130812Smarcel/* Print info on this target.  */
2115130812Smarcel
2116130812Smarcelstatic void
2117130812Smarcelmips_files_info (struct target_ops *ignore)
2118130812Smarcel{
2119130812Smarcel  printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
2120130812Smarcel}
2121130812Smarcel
2122130812Smarcel/* Kill the process running on the board.  This will actually only
2123130812Smarcel   work if we are doing remote debugging over the console input.  I
2124130812Smarcel   think that if IDT/sim had the remote debug interrupt enabled on the
2125130812Smarcel   right port, we could interrupt the process with a break signal.  */
2126130812Smarcel
2127130812Smarcelstatic void
2128130812Smarcelmips_kill (void)
2129130812Smarcel{
2130130812Smarcel  if (!mips_wait_flag)
2131130812Smarcel    return;
2132130812Smarcel
2133130812Smarcel  interrupt_count++;
2134130812Smarcel
2135130812Smarcel  if (interrupt_count >= 2)
2136130812Smarcel    {
2137130812Smarcel      interrupt_count = 0;
2138130812Smarcel
2139130812Smarcel      target_terminal_ours ();
2140130812Smarcel
2141130812Smarcel      if (query ("Interrupted while waiting for the program.\n\
2142130812SmarcelGive up (and stop debugging it)? "))
2143130812Smarcel	{
2144130812Smarcel	  /* Clean up in such a way that mips_close won't try to talk to the
2145130812Smarcel	     board (it almost surely won't work since we weren't able to talk to
2146130812Smarcel	     it).  */
2147130812Smarcel	  mips_wait_flag = 0;
2148130812Smarcel	  close_ports ();
2149130812Smarcel
2150130812Smarcel	  printf_unfiltered ("Ending remote MIPS debugging.\n");
2151130812Smarcel	  target_mourn_inferior ();
2152130812Smarcel
2153130812Smarcel	  throw_exception (RETURN_QUIT);
2154130812Smarcel	}
2155130812Smarcel
2156130812Smarcel      target_terminal_inferior ();
2157130812Smarcel    }
2158130812Smarcel
2159130812Smarcel  if (remote_debug > 0)
2160130812Smarcel    printf_unfiltered ("Sending break\n");
2161130812Smarcel
2162130812Smarcel  serial_send_break (mips_desc);
2163130812Smarcel
2164130812Smarcel#if 0
2165130812Smarcel  if (mips_is_open)
2166130812Smarcel    {
2167130812Smarcel      char cc;
2168130812Smarcel
2169130812Smarcel      /* Send a ^C.  */
2170130812Smarcel      cc = '\003';
2171130812Smarcel      serial_write (mips_desc, &cc, 1);
2172130812Smarcel      sleep (1);
2173130812Smarcel      target_mourn_inferior ();
2174130812Smarcel    }
2175130812Smarcel#endif
2176130812Smarcel}
2177130812Smarcel
2178130812Smarcel/* Start running on the target board.  */
2179130812Smarcel
2180130812Smarcelstatic void
2181130812Smarcelmips_create_inferior (char *execfile, char *args, char **env)
2182130812Smarcel{
2183130812Smarcel  CORE_ADDR entry_pt;
2184130812Smarcel
2185130812Smarcel  if (args && *args)
2186130812Smarcel    {
2187130812Smarcel      warning ("\
2188130812SmarcelCan't pass arguments to remote MIPS board; arguments ignored.");
2189130812Smarcel      /* And don't try to use them on the next "run" command.  */
2190130812Smarcel      execute_command ("set args", 0);
2191130812Smarcel    }
2192130812Smarcel
2193130812Smarcel  if (execfile == 0 || exec_bfd == 0)
2194130812Smarcel    error ("No executable file specified");
2195130812Smarcel
2196130812Smarcel  entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2197130812Smarcel
2198130812Smarcel  init_wait_for_inferior ();
2199130812Smarcel
2200130812Smarcel  /* FIXME: Should we set inferior_ptid here?  */
2201130812Smarcel
2202130812Smarcel  proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
2203130812Smarcel}
2204130812Smarcel
2205130812Smarcel/* Clean up after a process.  Actually nothing to do.  */
2206130812Smarcel
2207130812Smarcelstatic void
2208130812Smarcelmips_mourn_inferior (void)
2209130812Smarcel{
2210130812Smarcel  if (current_ops != NULL)
2211130812Smarcel    unpush_target (current_ops);
2212130812Smarcel  generic_mourn_inferior ();
2213130812Smarcel}
2214130812Smarcel
2215130812Smarcel/* We can write a breakpoint and read the shadow contents in one
2216130812Smarcel   operation.  */
2217130812Smarcel
2218130812Smarcel/* Insert a breakpoint.  On targets that don't have built-in
2219130812Smarcel   breakpoint support, we read the contents of the target location and
2220130812Smarcel   stash it, then overwrite it with a breakpoint instruction.  ADDR is
2221130812Smarcel   the target location in the target machine.  CONTENTS_CACHE is a
2222130812Smarcel   pointer to memory allocated for saving the target contents.  It is
2223130812Smarcel   guaranteed by the caller to be long enough to save the breakpoint
2224130812Smarcel   length returned by BREAKPOINT_FROM_PC.  */
2225130812Smarcel
2226130812Smarcelstatic int
2227130812Smarcelmips_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
2228130812Smarcel{
2229130812Smarcel  if (monitor_supports_breakpoints)
2230130812Smarcel    return set_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2231130812Smarcel  else
2232130812Smarcel    return memory_insert_breakpoint (addr, contents_cache);
2233130812Smarcel}
2234130812Smarcel
2235130812Smarcelstatic int
2236130812Smarcelmips_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
2237130812Smarcel{
2238130812Smarcel  if (monitor_supports_breakpoints)
2239130812Smarcel    return clear_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2240130812Smarcel  else
2241130812Smarcel    return memory_remove_breakpoint (addr, contents_cache);
2242130812Smarcel}
2243130812Smarcel
2244130812Smarcel/* Tell whether this target can support a hardware breakpoint.  CNT
2245130812Smarcel   is the number of hardware breakpoints already installed.  This
2246130812Smarcel   implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.  */
2247130812Smarcel
2248130812Smarcelint
2249130812Smarcelmips_can_use_watchpoint (int type, int cnt, int othertype)
2250130812Smarcel{
2251130812Smarcel  return cnt < MAX_LSI_BREAKPOINTS && strcmp (target_shortname, "lsi") == 0;
2252130812Smarcel}
2253130812Smarcel
2254130812Smarcel
2255130812Smarcel/* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2256130812Smarcel   This is used for memory ref breakpoints.  */
2257130812Smarcel
2258130812Smarcelstatic unsigned long
2259130812Smarcelcalculate_mask (CORE_ADDR addr, int len)
2260130812Smarcel{
2261130812Smarcel  unsigned long mask;
2262130812Smarcel  int i;
2263130812Smarcel
2264130812Smarcel  mask = addr ^ (addr + len - 1);
2265130812Smarcel
2266130812Smarcel  for (i = 32; i >= 0; i--)
2267130812Smarcel    if (mask == 0)
2268130812Smarcel      break;
2269130812Smarcel    else
2270130812Smarcel      mask >>= 1;
2271130812Smarcel
2272130812Smarcel  mask = (unsigned long) 0xffffffff >> i;
2273130812Smarcel
2274130812Smarcel  return mask;
2275130812Smarcel}
2276130812Smarcel
2277130812Smarcel
2278130812Smarcel/* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
2279130812Smarcel   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
2280130812Smarcel   watchpoint. */
2281130812Smarcel
2282130812Smarcelint
2283130812Smarcelmips_insert_watchpoint (CORE_ADDR addr, int len, int type)
2284130812Smarcel{
2285130812Smarcel  if (set_breakpoint (addr, len, type))
2286130812Smarcel    return -1;
2287130812Smarcel
2288130812Smarcel  return 0;
2289130812Smarcel}
2290130812Smarcel
2291130812Smarcelint
2292130812Smarcelmips_remove_watchpoint (CORE_ADDR addr, int len, int type)
2293130812Smarcel{
2294130812Smarcel  if (clear_breakpoint (addr, len, type))
2295130812Smarcel    return -1;
2296130812Smarcel
2297130812Smarcel  return 0;
2298130812Smarcel}
2299130812Smarcel
2300130812Smarcelint
2301130812Smarcelmips_stopped_by_watchpoint (void)
2302130812Smarcel{
2303130812Smarcel  return hit_watchpoint;
2304130812Smarcel}
2305130812Smarcel
2306130812Smarcel
2307130812Smarcel/* Insert a breakpoint.  */
2308130812Smarcel
2309130812Smarcelstatic int
2310130812Smarcelset_breakpoint (CORE_ADDR addr, int len, enum break_type type)
2311130812Smarcel{
2312130812Smarcel  return common_breakpoint (1, addr, len, type);
2313130812Smarcel}
2314130812Smarcel
2315130812Smarcel
2316130812Smarcel/* Clear a breakpoint.  */
2317130812Smarcel
2318130812Smarcelstatic int
2319130812Smarcelclear_breakpoint (CORE_ADDR addr, int len, enum break_type type)
2320130812Smarcel{
2321130812Smarcel  return common_breakpoint (0, addr, len, type);
2322130812Smarcel}
2323130812Smarcel
2324130812Smarcel
2325130812Smarcel/* Check the error code from the return packet for an LSI breakpoint
2326130812Smarcel   command.  If there's no error, just return 0.  If it's a warning,
2327130812Smarcel   print the warning text and return 0.  If it's an error, print
2328130812Smarcel   the error text and return 1.  <ADDR> is the address of the breakpoint
2329130812Smarcel   that was being set.  <RERRFLG> is the error code returned by PMON.
2330130812Smarcel   This is a helper function for common_breakpoint.  */
2331130812Smarcel
2332130812Smarcelstatic int
2333130812Smarcelcheck_lsi_error (CORE_ADDR addr, int rerrflg)
2334130812Smarcel{
2335130812Smarcel  struct lsi_error *err;
2336130812Smarcel  char *saddr = paddr_nz (addr);	/* printable address string */
2337130812Smarcel
2338130812Smarcel  if (rerrflg == 0)		/* no error */
2339130812Smarcel    return 0;
2340130812Smarcel
2341130812Smarcel  /* Warnings can be ORed together, so check them all.  */
2342130812Smarcel  if (rerrflg & W_WARN)
2343130812Smarcel    {
2344130812Smarcel      if (monitor_warnings)
2345130812Smarcel	{
2346130812Smarcel	  int found = 0;
2347130812Smarcel	  for (err = lsi_warning_table; err->code != 0; err++)
2348130812Smarcel	    {
2349130812Smarcel	      if ((err->code & rerrflg) == err->code)
2350130812Smarcel		{
2351130812Smarcel		  found = 1;
2352130812Smarcel		  fprintf_unfiltered (gdb_stderr,
2353130812Smarcel				  "common_breakpoint (0x%s): Warning: %s\n",
2354130812Smarcel				      saddr,
2355130812Smarcel				      err->string);
2356130812Smarcel		}
2357130812Smarcel	    }
2358130812Smarcel	  if (!found)
2359130812Smarcel	    fprintf_unfiltered (gdb_stderr,
2360130812Smarcel			"common_breakpoint (0x%s): Unknown warning: 0x%x\n",
2361130812Smarcel				saddr,
2362130812Smarcel				rerrflg);
2363130812Smarcel	}
2364130812Smarcel      return 0;
2365130812Smarcel    }
2366130812Smarcel
2367130812Smarcel  /* Errors are unique, i.e. can't be ORed together.  */
2368130812Smarcel  for (err = lsi_error_table; err->code != 0; err++)
2369130812Smarcel    {
2370130812Smarcel      if ((err->code & rerrflg) == err->code)
2371130812Smarcel	{
2372130812Smarcel	  fprintf_unfiltered (gdb_stderr,
2373130812Smarcel			      "common_breakpoint (0x%s): Error: %s\n",
2374130812Smarcel			      saddr,
2375130812Smarcel			      err->string);
2376130812Smarcel	  return 1;
2377130812Smarcel	}
2378130812Smarcel    }
2379130812Smarcel  fprintf_unfiltered (gdb_stderr,
2380130812Smarcel		      "common_breakpoint (0x%s): Unknown error: 0x%x\n",
2381130812Smarcel		      saddr,
2382130812Smarcel		      rerrflg);
2383130812Smarcel  return 1;
2384130812Smarcel}
2385130812Smarcel
2386130812Smarcel
2387130812Smarcel/* This routine sends a breakpoint command to the remote target.
2388130812Smarcel
2389130812Smarcel   <SET> is 1 if setting a breakpoint, or 0 if clearing a breakpoint.
2390130812Smarcel   <ADDR> is the address of the breakpoint.
2391130812Smarcel   <LEN> the length of the region to break on.
2392130812Smarcel   <TYPE> is the type of breakpoint:
2393130812Smarcel   0 = write                    (BREAK_WRITE)
2394130812Smarcel   1 = read                     (BREAK_READ)
2395130812Smarcel   2 = read/write               (BREAK_ACCESS)
2396130812Smarcel   3 = instruction fetch        (BREAK_FETCH)
2397130812Smarcel
2398130812Smarcel   Return 0 if successful; otherwise 1.  */
2399130812Smarcel
2400130812Smarcelstatic int
2401130812Smarcelcommon_breakpoint (int set, CORE_ADDR addr, int len, enum break_type type)
2402130812Smarcel{
2403130812Smarcel  char buf[DATA_MAXLEN + 1];
2404130812Smarcel  char cmd, rcmd;
2405130812Smarcel  int rpid, rerrflg, rresponse, rlen;
2406130812Smarcel  int nfields;
2407130812Smarcel
2408130812Smarcel  addr = ADDR_BITS_REMOVE (addr);
2409130812Smarcel
2410130812Smarcel  if (mips_monitor == MON_LSI)
2411130812Smarcel    {
2412130812Smarcel      if (set == 0)		/* clear breakpoint */
2413130812Smarcel	{
2414130812Smarcel	  /* The LSI PMON "clear breakpoint" has this form:
2415130812Smarcel	     <pid> 'b' <bptn> 0x0
2416130812Smarcel	     reply:
2417130812Smarcel	     <pid> 'b' 0x0 <code>
2418130812Smarcel
2419130812Smarcel	     <bptn> is a breakpoint number returned by an earlier 'B' command.
2420130812Smarcel	     Possible return codes: OK, E_BPT.  */
2421130812Smarcel
2422130812Smarcel	  int i;
2423130812Smarcel
2424130812Smarcel	  /* Search for the breakpoint in the table.  */
2425130812Smarcel	  for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
2426130812Smarcel	    if (lsi_breakpoints[i].type == type
2427130812Smarcel		&& lsi_breakpoints[i].addr == addr
2428130812Smarcel		&& lsi_breakpoints[i].len == len)
2429130812Smarcel	      break;
2430130812Smarcel
2431130812Smarcel	  /* Clear the table entry and tell PMON to clear the breakpoint.  */
2432130812Smarcel	  if (i == MAX_LSI_BREAKPOINTS)
2433130812Smarcel	    {
2434130812Smarcel	      warning ("common_breakpoint: Attempt to clear bogus breakpoint at %s\n",
2435130812Smarcel		       paddr_nz (addr));
2436130812Smarcel	      return 1;
2437130812Smarcel	    }
2438130812Smarcel
2439130812Smarcel	  lsi_breakpoints[i].type = BREAK_UNUSED;
2440130812Smarcel	  sprintf (buf, "0x0 b 0x%x 0x0", i);
2441130812Smarcel	  mips_send_packet (buf, 1);
2442130812Smarcel
2443130812Smarcel	  rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2444130812Smarcel	  buf[rlen] = '\0';
2445130812Smarcel
2446130812Smarcel	  nfields = sscanf (buf, "0x%x b 0x0 0x%x", &rpid, &rerrflg);
2447130812Smarcel	  if (nfields != 2)
2448130812Smarcel	    mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2449130812Smarcel
2450130812Smarcel	  return (check_lsi_error (addr, rerrflg));
2451130812Smarcel	}
2452130812Smarcel      else
2453130812Smarcel	/* set a breakpoint */
2454130812Smarcel	{
2455130812Smarcel	  /* The LSI PMON "set breakpoint" command has this form:
2456130812Smarcel	     <pid> 'B' <addr> 0x0
2457130812Smarcel	     reply:
2458130812Smarcel	     <pid> 'B' <bptn> <code>
2459130812Smarcel
2460130812Smarcel	     The "set data breakpoint" command has this form:
2461130812Smarcel
2462130812Smarcel	     <pid> 'A' <addr1> <type> [<addr2>  [<value>]]
2463130812Smarcel
2464130812Smarcel	     where: type= "0x1" = read
2465130812Smarcel	     "0x2" = write
2466130812Smarcel	     "0x3" = access (read or write)
2467130812Smarcel
2468130812Smarcel	     The reply returns two values:
2469130812Smarcel	     bptn - a breakpoint number, which is a small integer with
2470130812Smarcel	     possible values of zero through 255.
2471130812Smarcel	     code - an error return code, a value of zero indicates a
2472130812Smarcel	     succesful completion, other values indicate various
2473130812Smarcel	     errors and warnings.
2474130812Smarcel
2475130812Smarcel	     Possible return codes: OK, W_QAL, E_QAL, E_OUT, E_NON.
2476130812Smarcel
2477130812Smarcel	   */
2478130812Smarcel
2479130812Smarcel	  if (type == BREAK_FETCH)	/* instruction breakpoint */
2480130812Smarcel	    {
2481130812Smarcel	      cmd = 'B';
2482130812Smarcel	      sprintf (buf, "0x0 B 0x%s 0x0", paddr_nz (addr));
2483130812Smarcel	    }
2484130812Smarcel	  else
2485130812Smarcel	    /* watchpoint */
2486130812Smarcel	    {
2487130812Smarcel	      cmd = 'A';
2488130812Smarcel	      sprintf (buf, "0x0 A 0x%s 0x%x 0x%s", paddr_nz (addr),
2489130812Smarcel		     type == BREAK_READ ? 1 : (type == BREAK_WRITE ? 2 : 3),
2490130812Smarcel		       paddr_nz (addr + len - 1));
2491130812Smarcel	    }
2492130812Smarcel	  mips_send_packet (buf, 1);
2493130812Smarcel
2494130812Smarcel	  rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2495130812Smarcel	  buf[rlen] = '\0';
2496130812Smarcel
2497130812Smarcel	  nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2498130812Smarcel			    &rpid, &rcmd, &rresponse, &rerrflg);
2499130812Smarcel	  if (nfields != 4 || rcmd != cmd || rresponse > 255)
2500130812Smarcel	    mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2501130812Smarcel
2502130812Smarcel	  if (rerrflg != 0)
2503130812Smarcel	    if (check_lsi_error (addr, rerrflg))
2504130812Smarcel	      return 1;
2505130812Smarcel
2506130812Smarcel	  /* rresponse contains PMON's breakpoint number.  Record the
2507130812Smarcel	     information for this breakpoint so we can clear it later.  */
2508130812Smarcel	  lsi_breakpoints[rresponse].type = type;
2509130812Smarcel	  lsi_breakpoints[rresponse].addr = addr;
2510130812Smarcel	  lsi_breakpoints[rresponse].len = len;
2511130812Smarcel
2512130812Smarcel	  return 0;
2513130812Smarcel	}
2514130812Smarcel    }
2515130812Smarcel  else
2516130812Smarcel    {
2517130812Smarcel      /* On non-LSI targets, the breakpoint command has this form:
2518130812Smarcel         0x0 <CMD> <ADDR> <MASK> <FLAGS>
2519130812Smarcel         <MASK> is a don't care mask for addresses.
2520130812Smarcel         <FLAGS> is any combination of `r', `w', or `f' for read/write/fetch.
2521130812Smarcel       */
2522130812Smarcel      unsigned long mask;
2523130812Smarcel
2524130812Smarcel      mask = calculate_mask (addr, len);
2525130812Smarcel      addr &= ~mask;
2526130812Smarcel
2527130812Smarcel      if (set)			/* set a breakpoint */
2528130812Smarcel	{
2529130812Smarcel	  char *flags;
2530130812Smarcel	  switch (type)
2531130812Smarcel	    {
2532130812Smarcel	    case BREAK_WRITE:	/* write */
2533130812Smarcel	      flags = "w";
2534130812Smarcel	      break;
2535130812Smarcel	    case BREAK_READ:	/* read */
2536130812Smarcel	      flags = "r";
2537130812Smarcel	      break;
2538130812Smarcel	    case BREAK_ACCESS:	/* read/write */
2539130812Smarcel	      flags = "rw";
2540130812Smarcel	      break;
2541130812Smarcel	    case BREAK_FETCH:	/* fetch */
2542130812Smarcel	      flags = "f";
2543130812Smarcel	      break;
2544130812Smarcel	    default:
2545130812Smarcel	      internal_error (__FILE__, __LINE__, "failed internal consistency check");
2546130812Smarcel	    }
2547130812Smarcel
2548130812Smarcel	  cmd = 'B';
2549130812Smarcel	  sprintf (buf, "0x0 B 0x%s 0x%s %s", paddr_nz (addr),
2550130812Smarcel		   paddr_nz (mask), flags);
2551130812Smarcel	}
2552130812Smarcel      else
2553130812Smarcel	{
2554130812Smarcel	  cmd = 'b';
2555130812Smarcel	  sprintf (buf, "0x0 b 0x%s", paddr_nz (addr));
2556130812Smarcel	}
2557130812Smarcel
2558130812Smarcel      mips_send_packet (buf, 1);
2559130812Smarcel
2560130812Smarcel      rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2561130812Smarcel      buf[rlen] = '\0';
2562130812Smarcel
2563130812Smarcel      nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2564130812Smarcel			&rpid, &rcmd, &rerrflg, &rresponse);
2565130812Smarcel
2566130812Smarcel      if (nfields != 4 || rcmd != cmd)
2567130812Smarcel	mips_error ("common_breakpoint: Bad response from remote board: %s",
2568130812Smarcel		    buf);
2569130812Smarcel
2570130812Smarcel      if (rerrflg != 0)
2571130812Smarcel	{
2572130812Smarcel	  /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2573130812Smarcel	     Cogent returns "0x0 b 0xffffffff 0x16\000": */
2574130812Smarcel	  if (mips_monitor == MON_DDB)
2575130812Smarcel	    rresponse = rerrflg;
2576130812Smarcel	  if (rresponse != 22)	/* invalid argument */
2577130812Smarcel	    fprintf_unfiltered (gdb_stderr,
2578130812Smarcel			     "common_breakpoint (0x%s):  Got error: 0x%x\n",
2579130812Smarcel				paddr_nz (addr), rresponse);
2580130812Smarcel	  return 1;
2581130812Smarcel	}
2582130812Smarcel    }
2583130812Smarcel  return 0;
2584130812Smarcel}
2585130812Smarcel
2586130812Smarcelstatic void
2587130812Smarcelsend_srec (char *srec, int len, CORE_ADDR addr)
2588130812Smarcel{
2589130812Smarcel  while (1)
2590130812Smarcel    {
2591130812Smarcel      int ch;
2592130812Smarcel
2593130812Smarcel      serial_write (mips_desc, srec, len);
2594130812Smarcel
2595130812Smarcel      ch = mips_readchar (remote_timeout);
2596130812Smarcel
2597130812Smarcel      switch (ch)
2598130812Smarcel	{
2599130812Smarcel	case SERIAL_TIMEOUT:
2600130812Smarcel	  error ("Timeout during download.");
2601130812Smarcel	  break;
2602130812Smarcel	case 0x6:		/* ACK */
2603130812Smarcel	  return;
2604130812Smarcel	case 0x15:		/* NACK */
2605130812Smarcel	  fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %s!  Retrying.\n", paddr_u (addr));
2606130812Smarcel	  continue;
2607130812Smarcel	default:
2608130812Smarcel	  error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2609130812Smarcel	}
2610130812Smarcel    }
2611130812Smarcel}
2612130812Smarcel
2613130812Smarcel/*  Download a binary file by converting it to S records. */
2614130812Smarcel
2615130812Smarcelstatic void
2616130812Smarcelmips_load_srec (char *args)
2617130812Smarcel{
2618130812Smarcel  bfd *abfd;
2619130812Smarcel  asection *s;
2620130812Smarcel  char *buffer, srec[1024];
2621130812Smarcel  unsigned int i;
2622130812Smarcel  unsigned int srec_frame = 200;
2623130812Smarcel  int reclen;
2624130812Smarcel  static int hashmark = 1;
2625130812Smarcel
2626130812Smarcel  buffer = alloca (srec_frame * 2 + 256);
2627130812Smarcel
2628130812Smarcel  abfd = bfd_openr (args, 0);
2629130812Smarcel  if (!abfd)
2630130812Smarcel    {
2631130812Smarcel      printf_filtered ("Unable to open file %s\n", args);
2632130812Smarcel      return;
2633130812Smarcel    }
2634130812Smarcel
2635130812Smarcel  if (bfd_check_format (abfd, bfd_object) == 0)
2636130812Smarcel    {
2637130812Smarcel      printf_filtered ("File is not an object file\n");
2638130812Smarcel      return;
2639130812Smarcel    }
2640130812Smarcel
2641130812Smarcel/* This actually causes a download in the IDT binary format: */
2642130812Smarcel  mips_send_command (LOAD_CMD, 0);
2643130812Smarcel
2644130812Smarcel  for (s = abfd->sections; s; s = s->next)
2645130812Smarcel    {
2646130812Smarcel      if (s->flags & SEC_LOAD)
2647130812Smarcel	{
2648130812Smarcel	  unsigned int numbytes;
2649130812Smarcel
2650130812Smarcel	  /* FIXME!  vma too small????? */
2651130812Smarcel	  printf_filtered ("%s\t: 0x%4lx .. 0x%4lx  ", s->name,
2652130812Smarcel			   (long) s->vma,
2653130812Smarcel			   (long) (s->vma + s->_raw_size));
2654130812Smarcel	  gdb_flush (gdb_stdout);
2655130812Smarcel
2656130812Smarcel	  for (i = 0; i < s->_raw_size; i += numbytes)
2657130812Smarcel	    {
2658130812Smarcel	      numbytes = min (srec_frame, s->_raw_size - i);
2659130812Smarcel
2660130812Smarcel	      bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2661130812Smarcel
2662130812Smarcel	      reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2663130812Smarcel	      send_srec (srec, reclen, s->vma + i);
2664130812Smarcel
2665130812Smarcel	      if (ui_load_progress_hook)
2666130812Smarcel		ui_load_progress_hook (s->name, i);
2667130812Smarcel
2668130812Smarcel	      if (hashmark)
2669130812Smarcel		{
2670130812Smarcel		  putchar_unfiltered ('#');
2671130812Smarcel		  gdb_flush (gdb_stdout);
2672130812Smarcel		}
2673130812Smarcel
2674130812Smarcel	    }			/* Per-packet (or S-record) loop */
2675130812Smarcel
2676130812Smarcel	  putchar_unfiltered ('\n');
2677130812Smarcel	}			/* Loadable sections */
2678130812Smarcel    }
2679130812Smarcel  if (hashmark)
2680130812Smarcel    putchar_unfiltered ('\n');
2681130812Smarcel
2682130812Smarcel  /* Write a type 7 terminator record. no data for a type 7, and there
2683130812Smarcel     is no data, so len is 0.  */
2684130812Smarcel
2685130812Smarcel  reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2686130812Smarcel
2687130812Smarcel  send_srec (srec, reclen, abfd->start_address);
2688130812Smarcel
2689130812Smarcel  serial_flush_input (mips_desc);
2690130812Smarcel}
2691130812Smarcel
2692130812Smarcel/*
2693130812Smarcel * mips_make_srec -- make an srecord. This writes each line, one at a
2694130812Smarcel *      time, each with it's own header and trailer line.
2695130812Smarcel *      An srecord looks like this:
2696130812Smarcel *
2697130812Smarcel * byte count-+     address
2698130812Smarcel * start ---+ |        |       data        +- checksum
2699130812Smarcel *          | |        |                   |
2700130812Smarcel *        S01000006F6B692D746573742E73726563E4
2701130812Smarcel *        S315000448600000000000000000FC00005900000000E9
2702130812Smarcel *        S31A0004000023C1400037DE00F023604000377B009020825000348D
2703130812Smarcel *        S30B0004485A0000000000004E
2704130812Smarcel *        S70500040000F6
2705130812Smarcel *
2706130812Smarcel *      S<type><length><address><data><checksum>
2707130812Smarcel *
2708130812Smarcel *      Where
2709130812Smarcel *      - length
2710130812Smarcel *        is the number of bytes following upto the checksum. Note that
2711130812Smarcel *        this is not the number of chars following, since it takes two
2712130812Smarcel *        chars to represent a byte.
2713130812Smarcel *      - type
2714130812Smarcel *        is one of:
2715130812Smarcel *        0) header record
2716130812Smarcel *        1) two byte address data record
2717130812Smarcel *        2) three byte address data record
2718130812Smarcel *        3) four byte address data record
2719130812Smarcel *        7) four byte address termination record
2720130812Smarcel *        8) three byte address termination record
2721130812Smarcel *        9) two byte address termination record
2722130812Smarcel *
2723130812Smarcel *      - address
2724130812Smarcel *        is the start address of the data following, or in the case of
2725130812Smarcel *        a termination record, the start address of the image
2726130812Smarcel *      - data
2727130812Smarcel *        is the data.
2728130812Smarcel *      - checksum
2729130812Smarcel *        is the sum of all the raw byte data in the record, from the length
2730130812Smarcel *        upwards, modulo 256 and subtracted from 255.
2731130812Smarcel *
2732130812Smarcel * This routine returns the length of the S-record.
2733130812Smarcel *
2734130812Smarcel */
2735130812Smarcel
2736130812Smarcelstatic int
2737130812Smarcelmips_make_srec (char *buf, int type, CORE_ADDR memaddr, unsigned char *myaddr,
2738130812Smarcel		int len)
2739130812Smarcel{
2740130812Smarcel  unsigned char checksum;
2741130812Smarcel  int i;
2742130812Smarcel
2743130812Smarcel  /* Create the header for the srec. addr_size is the number of bytes in the address,
2744130812Smarcel     and 1 is the number of bytes in the count.  */
2745130812Smarcel
2746130812Smarcel  /* FIXME!! bigger buf required for 64-bit! */
2747130812Smarcel  buf[0] = 'S';
2748130812Smarcel  buf[1] = type;
2749130812Smarcel  buf[2] = len + 4 + 1;		/* len + 4 byte address + 1 byte checksum */
2750130812Smarcel  /* This assumes S3 style downloads (4byte addresses). There should
2751130812Smarcel     probably be a check, or the code changed to make it more
2752130812Smarcel     explicit. */
2753130812Smarcel  buf[3] = memaddr >> 24;
2754130812Smarcel  buf[4] = memaddr >> 16;
2755130812Smarcel  buf[5] = memaddr >> 8;
2756130812Smarcel  buf[6] = memaddr;
2757130812Smarcel  memcpy (&buf[7], myaddr, len);
2758130812Smarcel
2759130812Smarcel  /* Note that the checksum is calculated on the raw data, not the
2760130812Smarcel     hexified data.  It includes the length, address and the data
2761130812Smarcel     portions of the packet.  */
2762130812Smarcel  checksum = 0;
2763130812Smarcel  buf += 2;			/* Point at length byte */
2764130812Smarcel  for (i = 0; i < len + 4 + 1; i++)
2765130812Smarcel    checksum += *buf++;
2766130812Smarcel
2767130812Smarcel  *buf = ~checksum;
2768130812Smarcel
2769130812Smarcel  return len + 8;
2770130812Smarcel}
2771130812Smarcel
2772130812Smarcel/* The following manifest controls whether we enable the simple flow
2773130812Smarcel   control support provided by the monitor. If enabled the code will
2774130812Smarcel   wait for an affirmative ACK between transmitting packets. */
2775130812Smarcel#define DOETXACK (1)
2776130812Smarcel
2777130812Smarcel/* The PMON fast-download uses an encoded packet format constructed of
2778130812Smarcel   3byte data packets (encoded as 4 printable ASCII characters), and
2779130812Smarcel   escape sequences (preceded by a '/'):
2780130812Smarcel
2781130812Smarcel   'K'     clear checksum
2782130812Smarcel   'C'     compare checksum (12bit value, not included in checksum calculation)
2783130812Smarcel   'S'     define symbol name (for addr) terminated with "," and padded to 4char boundary
2784130812Smarcel   'Z'     zero fill multiple of 3bytes
2785130812Smarcel   'B'     byte (12bit encoded value, of 8bit data)
2786130812Smarcel   'A'     address (36bit encoded value)
2787130812Smarcel   'E'     define entry as original address, and exit load
2788130812Smarcel
2789130812Smarcel   The packets are processed in 4 character chunks, so the escape
2790130812Smarcel   sequences that do not have any data (or variable length data)
2791130812Smarcel   should be padded to a 4 character boundary.  The decoder will give
2792130812Smarcel   an error if the complete message block size is not a multiple of
2793130812Smarcel   4bytes (size of record).
2794130812Smarcel
2795130812Smarcel   The encoding of numbers is done in 6bit fields.  The 6bit value is
2796130812Smarcel   used to index into this string to get the specific character
2797130812Smarcel   encoding for the value: */
2798130812Smarcelstatic char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
2799130812Smarcel
2800130812Smarcel/* Convert the number of bits required into an encoded number, 6bits
2801130812Smarcel   at a time (range 0..63).  Keep a checksum if required (passed
2802130812Smarcel   pointer non-NULL). The function returns the number of encoded
2803130812Smarcel   characters written into the buffer. */
2804130812Smarcelstatic int
2805130812Smarcelpmon_makeb64 (unsigned long v, char *p, int n, int *chksum)
2806130812Smarcel{
2807130812Smarcel  int count = (n / 6);
2808130812Smarcel
2809130812Smarcel  if ((n % 12) != 0)
2810130812Smarcel    {
2811130812Smarcel      fprintf_unfiltered (gdb_stderr,
2812130812Smarcel			  "Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n", n, (n == 1) ? "" : "s");
2813130812Smarcel      return (0);
2814130812Smarcel    }
2815130812Smarcel  if (n > 36)
2816130812Smarcel    {
2817130812Smarcel      fprintf_unfiltered (gdb_stderr,
2818130812Smarcel			  "Fast encoding cannot process more than 36bits at the moment: %dbits\n", n);
2819130812Smarcel      return (0);
2820130812Smarcel    }
2821130812Smarcel
2822130812Smarcel  /* Deal with the checksum: */
2823130812Smarcel  if (chksum != NULL)
2824130812Smarcel    {
2825130812Smarcel      switch (n)
2826130812Smarcel	{
2827130812Smarcel	case 36:
2828130812Smarcel	  *chksum += ((v >> 24) & 0xFFF);
2829130812Smarcel	case 24:
2830130812Smarcel	  *chksum += ((v >> 12) & 0xFFF);
2831130812Smarcel	case 12:
2832130812Smarcel	  *chksum += ((v >> 0) & 0xFFF);
2833130812Smarcel	}
2834130812Smarcel    }
2835130812Smarcel
2836130812Smarcel  do
2837130812Smarcel    {
2838130812Smarcel      n -= 6;
2839130812Smarcel      *p++ = encoding[(v >> n) & 0x3F];
2840130812Smarcel    }
2841130812Smarcel  while (n > 0);
2842130812Smarcel
2843130812Smarcel  return (count);
2844130812Smarcel}
2845130812Smarcel
2846130812Smarcel/* Shorthand function (that could be in-lined) to output the zero-fill
2847130812Smarcel   escape sequence into the data stream. */
2848130812Smarcelstatic int
2849130812Smarcelpmon_zeroset (int recsize, char **buff, int *amount, unsigned int *chksum)
2850130812Smarcel{
2851130812Smarcel  int count;
2852130812Smarcel
2853130812Smarcel  sprintf (*buff, "/Z");
2854130812Smarcel  count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
2855130812Smarcel  *buff += (count + 2);
2856130812Smarcel  *amount = 0;
2857130812Smarcel  return (recsize + count + 2);
2858130812Smarcel}
2859130812Smarcel
2860130812Smarcelstatic int
2861130812Smarcelpmon_checkset (int recsize, char **buff, int *value)
2862130812Smarcel{
2863130812Smarcel  int count;
2864130812Smarcel
2865130812Smarcel  /* Add the checksum (without updating the value): */
2866130812Smarcel  sprintf (*buff, "/C");
2867130812Smarcel  count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
2868130812Smarcel  *buff += (count + 2);
2869130812Smarcel  sprintf (*buff, "\n");
2870130812Smarcel  *buff += 2;			/* include zero terminator */
2871130812Smarcel  /* Forcing a checksum validation clears the sum: */
2872130812Smarcel  *value = 0;
2873130812Smarcel  return (recsize + count + 3);
2874130812Smarcel}
2875130812Smarcel
2876130812Smarcel/* Amount of padding we leave after at the end of the output buffer,
2877130812Smarcel   for the checksum and line termination characters: */
2878130812Smarcel#define CHECKSIZE (4 + 4 + 4 + 2)
2879130812Smarcel/* zero-fill, checksum, transfer end and line termination space. */
2880130812Smarcel
2881130812Smarcel/* The amount of binary data loaded from the object file in a single
2882130812Smarcel   operation: */
2883130812Smarcel#define BINCHUNK (1024)
2884130812Smarcel
2885130812Smarcel/* Maximum line of data accepted by the monitor: */
2886130812Smarcel#define MAXRECSIZE (550)
2887130812Smarcel/* NOTE: This constant depends on the monitor being used. This value
2888130812Smarcel   is for PMON 5.x on the Cogent Vr4300 board. */
2889130812Smarcel
2890130812Smarcelstatic void
2891130812Smarcelpmon_make_fastrec (char **outbuf, unsigned char *inbuf, int *inptr,
2892130812Smarcel		   int inamount, int *recsize, unsigned int *csum,
2893130812Smarcel		   unsigned int *zerofill)
2894130812Smarcel{
2895130812Smarcel  int count = 0;
2896130812Smarcel  char *p = *outbuf;
2897130812Smarcel
2898130812Smarcel  /* This is a simple check to ensure that our data will fit within
2899130812Smarcel     the maximum allowable record size. Each record output is 4bytes
2900130812Smarcel     in length. We must allow space for a pending zero fill command,
2901130812Smarcel     the record, and a checksum record. */
2902130812Smarcel  while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0))
2903130812Smarcel    {
2904130812Smarcel      /* Process the binary data: */
2905130812Smarcel      if ((inamount - *inptr) < 3)
2906130812Smarcel	{
2907130812Smarcel	  if (*zerofill != 0)
2908130812Smarcel	    *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2909130812Smarcel	  sprintf (p, "/B");
2910130812Smarcel	  count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
2911130812Smarcel	  p += (2 + count);
2912130812Smarcel	  *recsize += (2 + count);
2913130812Smarcel	  (*inptr)++;
2914130812Smarcel	}
2915130812Smarcel      else
2916130812Smarcel	{
2917130812Smarcel	  unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
2918130812Smarcel	  /* Simple check for zero data. TODO: A better check would be
2919130812Smarcel	     to check the last, and then the middle byte for being zero
2920130812Smarcel	     (if the first byte is not). We could then check for
2921130812Smarcel	     following runs of zeros, and if above a certain size it is
2922130812Smarcel	     worth the 4 or 8 character hit of the byte insertions used
2923130812Smarcel	     to pad to the start of the zeroes. NOTE: This also depends
2924130812Smarcel	     on the alignment at the end of the zero run. */
2925130812Smarcel	  if (value == 0x00000000)
2926130812Smarcel	    {
2927130812Smarcel	      (*zerofill)++;
2928130812Smarcel	      if (*zerofill == 0xFFF)	/* 12bit counter */
2929130812Smarcel		*recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2930130812Smarcel	    }
2931130812Smarcel	  else
2932130812Smarcel	    {
2933130812Smarcel	      if (*zerofill != 0)
2934130812Smarcel		*recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2935130812Smarcel	      count = pmon_makeb64 (value, p, 24, csum);
2936130812Smarcel	      p += count;
2937130812Smarcel	      *recsize += count;
2938130812Smarcel	    }
2939130812Smarcel	  *inptr += 3;
2940130812Smarcel	}
2941130812Smarcel    }
2942130812Smarcel
2943130812Smarcel  *outbuf = p;
2944130812Smarcel  return;
2945130812Smarcel}
2946130812Smarcel
2947130812Smarcelstatic int
2948130812Smarcelpmon_check_ack (char *mesg)
2949130812Smarcel{
2950130812Smarcel#if defined(DOETXACK)
2951130812Smarcel  int c;
2952130812Smarcel
2953130812Smarcel  if (!tftp_in_use)
2954130812Smarcel    {
2955130812Smarcel      c = serial_readchar (udp_in_use ? udp_desc : mips_desc,
2956130812Smarcel			   remote_timeout);
2957130812Smarcel      if ((c == SERIAL_TIMEOUT) || (c != 0x06))
2958130812Smarcel	{
2959130812Smarcel	  fprintf_unfiltered (gdb_stderr,
2960130812Smarcel			      "Failed to receive valid ACK for %s\n", mesg);
2961130812Smarcel	  return (-1);		/* terminate the download */
2962130812Smarcel	}
2963130812Smarcel    }
2964130812Smarcel#endif /* DOETXACK */
2965130812Smarcel  return (0);
2966130812Smarcel}
2967130812Smarcel
2968130812Smarcel/* pmon_download - Send a sequence of characters to the PMON download port,
2969130812Smarcel   which is either a serial port or a UDP socket.  */
2970130812Smarcel
2971130812Smarcelstatic void
2972130812Smarcelpmon_start_download (void)
2973130812Smarcel{
2974130812Smarcel  if (tftp_in_use)
2975130812Smarcel    {
2976130812Smarcel      /* Create the temporary download file.  */
2977130812Smarcel      if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
2978130812Smarcel	perror_with_name (tftp_localname);
2979130812Smarcel    }
2980130812Smarcel  else
2981130812Smarcel    {
2982130812Smarcel      mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
2983130812Smarcel      mips_expect ("Downloading from ");
2984130812Smarcel      mips_expect (udp_in_use ? "udp" : "tty0");
2985130812Smarcel      mips_expect (", ^C to abort\r\n");
2986130812Smarcel    }
2987130812Smarcel}
2988130812Smarcel
2989130812Smarcelstatic int
2990130812Smarcelmips_expect_download (char *string)
2991130812Smarcel{
2992130812Smarcel  if (!mips_expect (string))
2993130812Smarcel    {
2994130812Smarcel      fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
2995130812Smarcel      if (tftp_in_use)
2996130812Smarcel	remove (tftp_localname);	/* Remove temporary file */
2997130812Smarcel      return 0;
2998130812Smarcel    }
2999130812Smarcel  else
3000130812Smarcel    return 1;
3001130812Smarcel}
3002130812Smarcel
3003130812Smarcelstatic void
3004130812Smarcelpmon_check_entry_address (char *entry_address, int final)
3005130812Smarcel{
3006130812Smarcel  char hexnumber[9];		/* includes '\0' space */
3007130812Smarcel  mips_expect_timeout (entry_address, tftp_in_use ? 15 : remote_timeout);
3008130812Smarcel  sprintf (hexnumber, "%x", final);
3009130812Smarcel  mips_expect (hexnumber);
3010130812Smarcel  mips_expect ("\r\n");
3011130812Smarcel}
3012130812Smarcel
3013130812Smarcelstatic int
3014130812Smarcelpmon_check_total (int bintotal)
3015130812Smarcel{
3016130812Smarcel  char hexnumber[9];		/* includes '\0' space */
3017130812Smarcel  mips_expect ("\r\ntotal = 0x");
3018130812Smarcel  sprintf (hexnumber, "%x", bintotal);
3019130812Smarcel  mips_expect (hexnumber);
3020130812Smarcel  return mips_expect_download (" bytes\r\n");
3021130812Smarcel}
3022130812Smarcel
3023130812Smarcelstatic void
3024130812Smarcelpmon_end_download (int final, int bintotal)
3025130812Smarcel{
3026130812Smarcel  char hexnumber[9];		/* includes '\0' space */
3027130812Smarcel
3028130812Smarcel  if (tftp_in_use)
3029130812Smarcel    {
3030130812Smarcel      static char *load_cmd_prefix = "load -b -s ";
3031130812Smarcel      char *cmd;
3032130812Smarcel      struct stat stbuf;
3033130812Smarcel
3034130812Smarcel      /* Close off the temporary file containing the load data.  */
3035130812Smarcel      fclose (tftp_file);
3036130812Smarcel      tftp_file = NULL;
3037130812Smarcel
3038130812Smarcel      /* Make the temporary file readable by the world.  */
3039130812Smarcel      if (stat (tftp_localname, &stbuf) == 0)
3040130812Smarcel	chmod (tftp_localname, stbuf.st_mode | S_IROTH);
3041130812Smarcel
3042130812Smarcel      /* Must reinitialize the board to prevent PMON from crashing.  */
3043130812Smarcel      mips_send_command ("initEther\r", -1);
3044130812Smarcel
3045130812Smarcel      /* Send the load command.  */
3046130812Smarcel      cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
3047130812Smarcel      strcpy (cmd, load_cmd_prefix);
3048130812Smarcel      strcat (cmd, tftp_name);
3049130812Smarcel      strcat (cmd, "\r");
3050130812Smarcel      mips_send_command (cmd, 0);
3051130812Smarcel      xfree (cmd);
3052130812Smarcel      if (!mips_expect_download ("Downloading from "))
3053130812Smarcel	return;
3054130812Smarcel      if (!mips_expect_download (tftp_name))
3055130812Smarcel	return;
3056130812Smarcel      if (!mips_expect_download (", ^C to abort\r\n"))
3057130812Smarcel	return;
3058130812Smarcel    }
3059130812Smarcel
3060130812Smarcel  /* Wait for the stuff that PMON prints after the load has completed.
3061130812Smarcel     The timeout value for use in the tftp case (15 seconds) was picked
3062130812Smarcel     arbitrarily but might be too small for really large downloads. FIXME. */
3063130812Smarcel  switch (mips_monitor)
3064130812Smarcel    {
3065130812Smarcel    case MON_LSI:
3066130812Smarcel      pmon_check_ack ("termination");
3067130812Smarcel      pmon_check_entry_address ("Entry address is ", final);
3068130812Smarcel      if (!pmon_check_total (bintotal))
3069130812Smarcel	return;
3070130812Smarcel      break;
3071130812Smarcel    default:
3072130812Smarcel      pmon_check_entry_address ("Entry Address  = ", final);
3073130812Smarcel      pmon_check_ack ("termination");
3074130812Smarcel      if (!pmon_check_total (bintotal))
3075130812Smarcel	return;
3076130812Smarcel      break;
3077130812Smarcel    }
3078130812Smarcel
3079130812Smarcel  if (tftp_in_use)
3080130812Smarcel    remove (tftp_localname);	/* Remove temporary file */
3081130812Smarcel}
3082130812Smarcel
3083130812Smarcelstatic void
3084130812Smarcelpmon_download (char *buffer, int length)
3085130812Smarcel{
3086130812Smarcel  if (tftp_in_use)
3087130812Smarcel    fwrite (buffer, 1, length, tftp_file);
3088130812Smarcel  else
3089130812Smarcel    serial_write (udp_in_use ? udp_desc : mips_desc, buffer, length);
3090130812Smarcel}
3091130812Smarcel
3092130812Smarcelstatic void
3093130812Smarcelpmon_load_fast (char *file)
3094130812Smarcel{
3095130812Smarcel  bfd *abfd;
3096130812Smarcel  asection *s;
3097130812Smarcel  unsigned char *binbuf;
3098130812Smarcel  char *buffer;
3099130812Smarcel  int reclen;
3100130812Smarcel  unsigned int csum = 0;
3101130812Smarcel  int hashmark = !tftp_in_use;
3102130812Smarcel  int bintotal = 0;
3103130812Smarcel  int final = 0;
3104130812Smarcel  int finished = 0;
3105130812Smarcel
3106130812Smarcel  buffer = (char *) xmalloc (MAXRECSIZE + 1);
3107130812Smarcel  binbuf = (unsigned char *) xmalloc (BINCHUNK);
3108130812Smarcel
3109130812Smarcel  abfd = bfd_openr (file, 0);
3110130812Smarcel  if (!abfd)
3111130812Smarcel    {
3112130812Smarcel      printf_filtered ("Unable to open file %s\n", file);
3113130812Smarcel      return;
3114130812Smarcel    }
3115130812Smarcel
3116130812Smarcel  if (bfd_check_format (abfd, bfd_object) == 0)
3117130812Smarcel    {
3118130812Smarcel      printf_filtered ("File is not an object file\n");
3119130812Smarcel      return;
3120130812Smarcel    }
3121130812Smarcel
3122130812Smarcel  /* Setup the required download state: */
3123130812Smarcel  mips_send_command ("set dlproto etxack\r", -1);
3124130812Smarcel  mips_send_command ("set dlecho off\r", -1);
3125130812Smarcel  /* NOTE: We get a "cannot set variable" message if the variable is
3126130812Smarcel     already defined to have the argument we give. The code doesn't
3127130812Smarcel     care, since it just scans to the next prompt anyway. */
3128130812Smarcel  /* Start the download: */
3129130812Smarcel  pmon_start_download ();
3130130812Smarcel
3131130812Smarcel  /* Zero the checksum */
3132130812Smarcel  sprintf (buffer, "/Kxx\n");
3133130812Smarcel  reclen = strlen (buffer);
3134130812Smarcel  pmon_download (buffer, reclen);
3135130812Smarcel  finished = pmon_check_ack ("/Kxx");
3136130812Smarcel
3137130812Smarcel  for (s = abfd->sections; s && !finished; s = s->next)
3138130812Smarcel    if (s->flags & SEC_LOAD)	/* only deal with loadable sections */
3139130812Smarcel      {
3140130812Smarcel	bintotal += s->_raw_size;
3141130812Smarcel	final = (s->vma + s->_raw_size);
3142130812Smarcel
3143130812Smarcel	printf_filtered ("%s\t: 0x%4x .. 0x%4x  ", s->name, (unsigned int) s->vma,
3144130812Smarcel			 (unsigned int) (s->vma + s->_raw_size));
3145130812Smarcel	gdb_flush (gdb_stdout);
3146130812Smarcel
3147130812Smarcel	/* Output the starting address */
3148130812Smarcel	sprintf (buffer, "/A");
3149130812Smarcel	reclen = pmon_makeb64 (s->vma, &buffer[2], 36, &csum);
3150130812Smarcel	buffer[2 + reclen] = '\n';
3151130812Smarcel	buffer[3 + reclen] = '\0';
3152130812Smarcel	reclen += 3;		/* for the initial escape code and carriage return */
3153130812Smarcel	pmon_download (buffer, reclen);
3154130812Smarcel	finished = pmon_check_ack ("/A");
3155130812Smarcel
3156130812Smarcel	if (!finished)
3157130812Smarcel	  {
3158130812Smarcel	    unsigned int binamount;
3159130812Smarcel	    unsigned int zerofill = 0;
3160130812Smarcel	    char *bp = buffer;
3161130812Smarcel	    unsigned int i;
3162130812Smarcel
3163130812Smarcel	    reclen = 0;
3164130812Smarcel
3165130812Smarcel	    for (i = 0; ((i < s->_raw_size) && !finished); i += binamount)
3166130812Smarcel	      {
3167130812Smarcel		int binptr = 0;
3168130812Smarcel
3169130812Smarcel		binamount = min (BINCHUNK, s->_raw_size - i);
3170130812Smarcel
3171130812Smarcel		bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3172130812Smarcel
3173130812Smarcel		/* This keeps a rolling checksum, until we decide to output
3174130812Smarcel		   the line: */
3175130812Smarcel		for (; ((binamount - binptr) > 0);)
3176130812Smarcel		  {
3177130812Smarcel		    pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3178130812Smarcel		    if (reclen >= (MAXRECSIZE - CHECKSIZE))
3179130812Smarcel		      {
3180130812Smarcel			reclen = pmon_checkset (reclen, &bp, &csum);
3181130812Smarcel			pmon_download (buffer, reclen);
3182130812Smarcel			finished = pmon_check_ack ("data record");
3183130812Smarcel			if (finished)
3184130812Smarcel			  {
3185130812Smarcel			    zerofill = 0;	/* do not transmit pending zerofills */
3186130812Smarcel			    break;
3187130812Smarcel			  }
3188130812Smarcel
3189130812Smarcel			if (ui_load_progress_hook)
3190130812Smarcel			  ui_load_progress_hook (s->name, i);
3191130812Smarcel
3192130812Smarcel			if (hashmark)
3193130812Smarcel			  {
3194130812Smarcel			    putchar_unfiltered ('#');
3195130812Smarcel			    gdb_flush (gdb_stdout);
3196130812Smarcel			  }
3197130812Smarcel
3198130812Smarcel			bp = buffer;
3199130812Smarcel			reclen = 0;	/* buffer processed */
3200130812Smarcel		      }
3201130812Smarcel		  }
3202130812Smarcel	      }
3203130812Smarcel
3204130812Smarcel	    /* Ensure no out-standing zerofill requests: */
3205130812Smarcel	    if (zerofill != 0)
3206130812Smarcel	      reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3207130812Smarcel
3208130812Smarcel	    /* and then flush the line: */
3209130812Smarcel	    if (reclen > 0)
3210130812Smarcel	      {
3211130812Smarcel		reclen = pmon_checkset (reclen, &bp, &csum);
3212130812Smarcel		/* Currently pmon_checkset outputs the line terminator by
3213130812Smarcel		   default, so we write out the buffer so far: */
3214130812Smarcel		pmon_download (buffer, reclen);
3215130812Smarcel		finished = pmon_check_ack ("record remnant");
3216130812Smarcel	      }
3217130812Smarcel	  }
3218130812Smarcel
3219130812Smarcel	putchar_unfiltered ('\n');
3220130812Smarcel      }
3221130812Smarcel
3222130812Smarcel  /* Terminate the transfer. We know that we have an empty output
3223130812Smarcel     buffer at this point. */
3224130812Smarcel  sprintf (buffer, "/E/E\n");	/* include dummy padding characters */
3225130812Smarcel  reclen = strlen (buffer);
3226130812Smarcel  pmon_download (buffer, reclen);
3227130812Smarcel
3228130812Smarcel  if (finished)
3229130812Smarcel    {				/* Ignore the termination message: */
3230130812Smarcel      serial_flush_input (udp_in_use ? udp_desc : mips_desc);
3231130812Smarcel    }
3232130812Smarcel  else
3233130812Smarcel    {				/* Deal with termination message: */
3234130812Smarcel      pmon_end_download (final, bintotal);
3235130812Smarcel    }
3236130812Smarcel
3237130812Smarcel  return;
3238130812Smarcel}
3239130812Smarcel
3240130812Smarcel/* mips_load -- download a file. */
3241130812Smarcel
3242130812Smarcelstatic void
3243130812Smarcelmips_load (char *file, int from_tty)
3244130812Smarcel{
3245130812Smarcel  /* Get the board out of remote debugging mode.  */
3246130812Smarcel  if (mips_exit_debug ())
3247130812Smarcel    error ("mips_load:  Couldn't get into monitor mode.");
3248130812Smarcel
3249130812Smarcel  if (mips_monitor != MON_IDT)
3250130812Smarcel    pmon_load_fast (file);
3251130812Smarcel  else
3252130812Smarcel    mips_load_srec (file);
3253130812Smarcel
3254130812Smarcel  mips_initialize ();
3255130812Smarcel
3256130812Smarcel  /* Finally, make the PC point at the start address */
3257130812Smarcel  if (mips_monitor != MON_IDT)
3258130812Smarcel    {
3259130812Smarcel      /* Work around problem where PMON monitor updates the PC after a load
3260130812Smarcel         to a different value than GDB thinks it has. The following ensures
3261130812Smarcel         that the write_pc() WILL update the PC value: */
3262130812Smarcel      deprecated_register_valid[PC_REGNUM] = 0;
3263130812Smarcel    }
3264130812Smarcel  if (exec_bfd)
3265130812Smarcel    write_pc (bfd_get_start_address (exec_bfd));
3266130812Smarcel
3267130812Smarcel  inferior_ptid = null_ptid;	/* No process now */
3268130812Smarcel
3269130812Smarcel/* This is necessary because many things were based on the PC at the time that
3270130812Smarcel   we attached to the monitor, which is no longer valid now that we have loaded
3271130812Smarcel   new code (and just changed the PC).  Another way to do this might be to call
3272130812Smarcel   normal_stop, except that the stack may not be valid, and things would get
3273130812Smarcel   horribly confused... */
3274130812Smarcel
3275130812Smarcel  clear_symtab_users ();
3276130812Smarcel}
3277130812Smarcel
3278130812Smarcel
3279130812Smarcel/* Pass the command argument as a packet to PMON verbatim.  */
3280130812Smarcel
3281130812Smarcelstatic void
3282130812Smarcelpmon_command (char *args, int from_tty)
3283130812Smarcel{
3284130812Smarcel  char buf[DATA_MAXLEN + 1];
3285130812Smarcel  int rlen;
3286130812Smarcel
3287130812Smarcel  sprintf (buf, "0x0 %s", args);
3288130812Smarcel  mips_send_packet (buf, 1);
3289130812Smarcel  printf_filtered ("Send packet: %s\n", buf);
3290130812Smarcel
3291130812Smarcel  rlen = mips_receive_packet (buf, 1, mips_receive_wait);
3292130812Smarcel  buf[rlen] = '\0';
3293130812Smarcel  printf_filtered ("Received packet: %s\n", buf);
3294130812Smarcel}
3295130812Smarcel
3296130812Smarcelextern initialize_file_ftype _initialize_remote_mips; /* -Wmissing-prototypes */
3297130812Smarcel
3298130812Smarcelvoid
3299130812Smarcel_initialize_remote_mips (void)
3300130812Smarcel{
3301130812Smarcel  /* Initialize the fields in mips_ops that are common to all four targets.  */
3302130812Smarcel  mips_ops.to_longname = "Remote MIPS debugging over serial line";
3303130812Smarcel  mips_ops.to_close = mips_close;
3304130812Smarcel  mips_ops.to_detach = mips_detach;
3305130812Smarcel  mips_ops.to_resume = mips_resume;
3306130812Smarcel  mips_ops.to_fetch_registers = mips_fetch_registers;
3307130812Smarcel  mips_ops.to_store_registers = mips_store_registers;
3308130812Smarcel  mips_ops.to_prepare_to_store = mips_prepare_to_store;
3309130812Smarcel  mips_ops.to_xfer_memory = mips_xfer_memory;
3310130812Smarcel  mips_ops.to_files_info = mips_files_info;
3311130812Smarcel  mips_ops.to_insert_breakpoint = mips_insert_breakpoint;
3312130812Smarcel  mips_ops.to_remove_breakpoint = mips_remove_breakpoint;
3313130812Smarcel  mips_ops.to_insert_watchpoint = mips_insert_watchpoint;
3314130812Smarcel  mips_ops.to_remove_watchpoint = mips_remove_watchpoint;
3315130812Smarcel  mips_ops.to_stopped_by_watchpoint = mips_stopped_by_watchpoint;
3316130812Smarcel  mips_ops.to_can_use_hw_breakpoint = mips_can_use_watchpoint;
3317130812Smarcel  mips_ops.to_kill = mips_kill;
3318130812Smarcel  mips_ops.to_load = mips_load;
3319130812Smarcel  mips_ops.to_create_inferior = mips_create_inferior;
3320130812Smarcel  mips_ops.to_mourn_inferior = mips_mourn_inferior;
3321130812Smarcel  mips_ops.to_stratum = process_stratum;
3322130812Smarcel  mips_ops.to_has_all_memory = 1;
3323130812Smarcel  mips_ops.to_has_memory = 1;
3324130812Smarcel  mips_ops.to_has_stack = 1;
3325130812Smarcel  mips_ops.to_has_registers = 1;
3326130812Smarcel  mips_ops.to_has_execution = 1;
3327130812Smarcel  mips_ops.to_magic = OPS_MAGIC;
3328130812Smarcel
3329130812Smarcel  /* Copy the common fields to all four target vectors.  */
3330130812Smarcel  pmon_ops = ddb_ops = lsi_ops = mips_ops;
3331130812Smarcel
3332130812Smarcel  /* Initialize target-specific fields in the target vectors.  */
3333130812Smarcel  mips_ops.to_shortname = "mips";
3334130812Smarcel  mips_ops.to_doc = "\
3335130812SmarcelDebug a board using the MIPS remote debugging protocol over a serial line.\n\
3336130812SmarcelThe argument is the device it is connected to or, if it contains a colon,\n\
3337130812SmarcelHOST:PORT to access a board over a network";
3338130812Smarcel  mips_ops.to_open = mips_open;
3339130812Smarcel  mips_ops.to_wait = mips_wait;
3340130812Smarcel
3341130812Smarcel  pmon_ops.to_shortname = "pmon";
3342130812Smarcel  pmon_ops.to_doc = "\
3343130812SmarcelDebug a board using the PMON MIPS remote debugging protocol over a serial\n\
3344130812Smarcelline. The argument is the device it is connected to or, if it contains a\n\
3345130812Smarcelcolon, HOST:PORT to access a board over a network";
3346130812Smarcel  pmon_ops.to_open = pmon_open;
3347130812Smarcel  pmon_ops.to_wait = mips_wait;
3348130812Smarcel
3349130812Smarcel  ddb_ops.to_shortname = "ddb";
3350130812Smarcel  ddb_ops.to_doc = "\
3351130812SmarcelDebug a board using the PMON MIPS remote debugging protocol over a serial\n\
3352130812Smarcelline. The first argument is the device it is connected to or, if it contains\n\
3353130812Smarcela colon, HOST:PORT to access a board over a network.  The optional second\n\
3354130812Smarcelparameter is the temporary file in the form HOST:FILENAME to be used for\n\
3355130812SmarcelTFTP downloads to the board.  The optional third parameter is the local name\n\
3356130812Smarcelof the TFTP temporary file, if it differs from the filename seen by the board.";
3357130812Smarcel  ddb_ops.to_open = ddb_open;
3358130812Smarcel  ddb_ops.to_wait = mips_wait;
3359130812Smarcel
3360130812Smarcel  lsi_ops.to_shortname = "lsi";
3361130812Smarcel  lsi_ops.to_doc = pmon_ops.to_doc;
3362130812Smarcel  lsi_ops.to_open = lsi_open;
3363130812Smarcel  lsi_ops.to_wait = mips_wait;
3364130812Smarcel
3365130812Smarcel  /* Add the targets.  */
3366130812Smarcel  add_target (&mips_ops);
3367130812Smarcel  add_target (&pmon_ops);
3368130812Smarcel  add_target (&ddb_ops);
3369130812Smarcel  add_target (&lsi_ops);
3370130812Smarcel
3371130812Smarcel  add_show_from_set (
3372130812Smarcel		      add_set_cmd ("timeout", no_class, var_zinteger,
3373130812Smarcel				   (char *) &mips_receive_wait,
3374130812Smarcel		       "Set timeout in seconds for remote MIPS serial I/O.",
3375130812Smarcel				   &setlist),
3376130812Smarcel		      &showlist);
3377130812Smarcel
3378130812Smarcel  add_show_from_set (
3379130812Smarcel		  add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3380130812Smarcel			       (char *) &mips_retransmit_wait,
3381130812Smarcel			       "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3382130812SmarcelThis is the number of seconds to wait for an acknowledgement to a packet\n\
3383130812Smarcelbefore resending the packet.", &setlist),
3384130812Smarcel		      &showlist);
3385130812Smarcel
3386130812Smarcel  add_show_from_set (
3387130812Smarcel		   add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3388130812Smarcel				(char *) &mips_syn_garbage,
3389130812Smarcel				"Set the maximum number of characters to ignore when scanning for a SYN.\n\
3390130812SmarcelThis is the maximum number of characters GDB will ignore when trying to\n\
3391130812Smarcelsynchronize with the remote system.  A value of -1 means that there is no limit\n\
3392130812Smarcel(Note that these characters are printed out even though they are ignored.)",
3393130812Smarcel				&setlist),
3394130812Smarcel		      &showlist);
3395130812Smarcel
3396130812Smarcel  add_show_from_set
3397130812Smarcel    (add_set_cmd ("monitor-prompt", class_obscure, var_string,
3398130812Smarcel		  (char *) &mips_monitor_prompt,
3399130812Smarcel		  "Set the prompt that GDB expects from the monitor.",
3400130812Smarcel		  &setlist),
3401130812Smarcel     &showlist);
3402130812Smarcel
3403130812Smarcel  add_show_from_set (
3404130812Smarcel	       add_set_cmd ("monitor-warnings", class_obscure, var_zinteger,
3405130812Smarcel			    (char *) &monitor_warnings,
3406130812Smarcel			    "Set printing of monitor warnings.\n"
3407130812Smarcel		"When enabled, monitor warnings about hardware breakpoints "
3408130812Smarcel			    "will be displayed.",
3409130812Smarcel			    &setlist),
3410130812Smarcel		      &showlist);
3411130812Smarcel
3412130812Smarcel  add_com ("pmon <command>", class_obscure, pmon_command,
3413130812Smarcel	   "Send a packet to PMON (must be in debug mode).");
3414130812Smarcel
3415130812Smarcel  add_show_from_set (add_set_cmd ("mask-address", no_class,
3416130812Smarcel				  var_boolean, &mask_address_p,
3417130812Smarcel				  "Set zeroing of upper 32 bits of 64-bit addresses when talking to PMON targets.\n\
3418130812SmarcelUse \"on\" to enable the masking and \"off\" to disable it.\n",
3419130812Smarcel				  &setlist),
3420130812Smarcel		     &showlist);
3421130812Smarcel}
3422