1 /* Target dependent code for the Fujitsu SPARClite for GDB, the GNU debugger.
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "breakpoint.h"
28 #include <sys/types.h>
30 #if (!defined(__GO32__) && !defined(_WIN32)) || defined(__CYGWIN32__)
33 #include <sys/socket.h>
34 #include <netinet/in.h>
38 static struct target_ops sparclite_ops
;
40 static char *remote_target_name
= NULL
;
41 static struct serial
*remote_desc
= NULL
;
42 static int serial_flag
;
44 static int udp_fd
= -1;
47 static struct serial
*open_tty (char *name
);
48 static int send_resp (struct serial
*desc
, char c
);
49 static void close_tty (void * ignore
);
51 static int recv_udp_buf (int fd
, unsigned char *buf
, int len
, int timeout
);
52 static int send_udp_buf (int fd
, unsigned char *buf
, int len
);
54 static void sparclite_open (char *name
, int from_tty
);
55 static void sparclite_close (int quitting
);
56 static void download (char *target_name
, char *args
, int from_tty
,
57 void (*write_routine
) (bfd
* from_bfd
,
60 bfd_vma to_addr
, int len
),
61 void (*start_routine
) (bfd_vma entry
));
62 static void sparclite_serial_start (bfd_vma entry
);
63 static void sparclite_serial_write (bfd
* from_bfd
, asection
* from_sec
,
65 bfd_vma to_addr
, int len
);
67 static unsigned short calc_checksum (unsigned char *buffer
, int count
);
68 static void sparclite_udp_start (bfd_vma entry
);
69 static void sparclite_udp_write (bfd
* from_bfd
, asection
* from_sec
,
70 file_ptr from_addr
, bfd_vma to_addr
,
73 static void sparclite_download (char *filename
, int from_tty
);
75 #define DDA2_SUP_ASI 0xb000000
76 #define DDA1_SUP_ASI 0xb0000
78 #define DDA2_ASI_MASK 0xff000000
79 #define DDA1_ASI_MASK 0xff0000
80 #define DIA2_SUP_MODE 0x8000
81 #define DIA1_SUP_MODE 0x4000
82 #define DDA2_ENABLE 0x100
83 #define DDA1_ENABLE 0x80
84 #define DIA2_ENABLE 0x40
85 #define DIA1_ENABLE 0x20
86 #define DSINGLE_STEP 0x10 /* not used */
87 #define DDV_TYPE_MASK 0xc
88 #define DDV_TYPE_LOAD 0x0
89 #define DDV_TYPE_STORE 0x4
90 #define DDV_TYPE_ACCESS 0x8
91 #define DDV_TYPE_ALWAYS 0xc
96 sparclite_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
100 dcr
= read_register (DCR_REGNUM
);
102 if (!(dcr
& DDA1_ENABLE
))
104 write_register (DDA1_REGNUM
, addr
);
105 dcr
&= ~(DDA1_ASI_MASK
| DDV_TYPE_MASK
);
106 dcr
|= (DDA1_SUP_ASI
| DDA1_ENABLE
);
109 write_register (DDV1_REGNUM
, 0);
110 write_register (DDV2_REGNUM
, 0xffffffff);
111 dcr
|= (DDV_TYPE_LOAD
& (~DDV_COND
& ~DDV_MASK
));
115 write_register (DDV1_REGNUM
, 0);
116 write_register (DDV2_REGNUM
, 0xffffffff);
117 dcr
|= (DDV_TYPE_STORE
& (~DDV_COND
& ~DDV_MASK
));
121 write_register (DDV1_REGNUM
, 0);
122 write_register (DDV2_REGNUM
, 0xffffffff);
123 dcr
|= (DDV_TYPE_ACCESS
);
125 write_register (DCR_REGNUM
, dcr
);
127 else if (!(dcr
& DDA2_ENABLE
))
129 write_register (DDA2_REGNUM
, addr
);
130 dcr
&= ~(DDA2_ASI_MASK
& DDV_TYPE_MASK
);
131 dcr
|= (DDA2_SUP_ASI
| DDA2_ENABLE
);
134 write_register (DDV1_REGNUM
, 0);
135 write_register (DDV2_REGNUM
, 0xffffffff);
136 dcr
|= (DDV_TYPE_LOAD
& ~DDV_COND
& ~DDV_MASK
);
140 write_register (DDV1_REGNUM
, 0);
141 write_register (DDV2_REGNUM
, 0xffffffff);
142 dcr
|= (DDV_TYPE_STORE
& ~DDV_COND
& ~DDV_MASK
);
146 write_register (DDV1_REGNUM
, 0);
147 write_register (DDV2_REGNUM
, 0xffffffff);
148 dcr
|= (DDV_TYPE_ACCESS
);
150 write_register (DCR_REGNUM
, dcr
);
159 sparclite_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
161 CORE_ADDR dcr
, dda1
, dda2
;
163 dcr
= read_register (DCR_REGNUM
);
164 dda1
= read_register (DDA1_REGNUM
);
165 dda2
= read_register (DDA2_REGNUM
);
167 if ((dcr
& DDA1_ENABLE
) && addr
== dda1
)
168 write_register (DCR_REGNUM
, (dcr
& ~DDA1_ENABLE
));
169 else if ((dcr
& DDA2_ENABLE
) && addr
== dda2
)
170 write_register (DCR_REGNUM
, (dcr
& ~DDA2_ENABLE
));
178 sparclite_insert_hw_breakpoint (CORE_ADDR addr
, int len
)
182 dcr
= read_register (DCR_REGNUM
);
184 if (!(dcr
& DIA1_ENABLE
))
186 write_register (DIA1_REGNUM
, addr
);
187 write_register (DCR_REGNUM
, (dcr
| DIA1_ENABLE
| DIA1_SUP_MODE
));
189 else if (!(dcr
& DIA2_ENABLE
))
191 write_register (DIA2_REGNUM
, addr
);
192 write_register (DCR_REGNUM
, (dcr
| DIA2_ENABLE
| DIA2_SUP_MODE
));
201 sparclite_remove_hw_breakpoint (CORE_ADDR addr
, int shadow
)
203 CORE_ADDR dcr
, dia1
, dia2
;
205 dcr
= read_register (DCR_REGNUM
);
206 dia1
= read_register (DIA1_REGNUM
);
207 dia2
= read_register (DIA2_REGNUM
);
209 if ((dcr
& DIA1_ENABLE
) && addr
== dia1
)
210 write_register (DCR_REGNUM
, (dcr
& ~DIA1_ENABLE
));
211 else if ((dcr
& DIA2_ENABLE
) && addr
== dia2
)
212 write_register (DCR_REGNUM
, (dcr
& ~DIA2_ENABLE
));
220 sparclite_check_watch_resources (int type
, int cnt
, int ot
)
222 /* Watchpoints not supported on simulator. */
223 if (strcmp (target_shortname
, "sim") == 0)
226 if (type
== bp_hardware_breakpoint
)
228 if (TARGET_HW_BREAK_LIMIT
== 0)
230 else if (cnt
<= TARGET_HW_BREAK_LIMIT
)
235 if (TARGET_HW_WATCH_LIMIT
== 0)
239 else if (cnt
<= TARGET_HW_WATCH_LIMIT
)
246 sparclite_stopped_data_address (void)
248 CORE_ADDR dsr
, dda1
, dda2
;
250 dsr
= read_register (DSR_REGNUM
);
251 dda1
= read_register (DDA1_REGNUM
);
252 dda2
= read_register (DDA2_REGNUM
);
262 static struct serial
*
263 open_tty (char *name
)
267 desc
= serial_open (name
);
269 perror_with_name (name
);
273 if (serial_setbaudrate (desc
, baud_rate
))
276 perror_with_name (name
);
282 serial_flush_input (desc
);
287 /* Read a single character from the remote end, masking it down to 7 bits. */
290 readchar (struct serial
*desc
, int timeout
)
295 ch
= serial_readchar (desc
, timeout
);
300 error ("SPARClite remote connection closed");
302 perror_with_name ("SPARClite communication error");
304 error ("SPARClite remote timeout");
306 if (remote_debug
> 0)
308 sprintf (s
, "[%02x]", ch
& 0xff);
309 puts_debug ("read -->", s
, "<--");
316 debug_serial_write (struct serial
*desc
, char *buf
, int len
)
320 serial_write (desc
, buf
, len
);
321 if (remote_debug
> 0)
325 sprintf (s
, "[%02x]", *buf
& 0xff);
326 puts_debug ("Sent -->", s
, "<--");
334 send_resp (struct serial
*desc
, char c
)
336 debug_serial_write (desc
, &c
, 1);
337 return readchar (desc
, remote_timeout
);
341 close_tty (void *ignore
)
346 serial_close (remote_desc
);
353 recv_udp_buf (int fd
, unsigned char *buf
, int len
, int timeout
)
359 FD_SET (fd
, &readfds
);
363 struct timeval timebuf
;
365 timebuf
.tv_sec
= timeout
;
367 cc
= select (fd
+ 1, &readfds
, 0, 0, &timebuf
);
370 cc
= select (fd
+ 1, &readfds
, 0, 0, 0);
376 perror_with_name ("recv_udp_buf: Bad return value from select:");
378 cc
= recv (fd
, buf
, len
, 0);
381 perror_with_name ("Got an error from recv: ");
385 send_udp_buf (int fd
, unsigned char *buf
, int len
)
389 cc
= send (fd
, buf
, len
, 0);
395 perror_with_name ("Got an error from send: ");
397 error ("Short count in send: tried %d, sent %d\n", len
, cc
);
399 #endif /* HAVE_SOCKETS */
402 sparclite_open (char *name
, int from_tty
)
404 struct cleanup
*old_chain
;
409 error ("You need to specify what device or hostname is associated with the SparcLite board.");
411 target_preopen (from_tty
);
413 unpush_target (&sparclite_ops
);
415 if (remote_target_name
)
416 xfree (remote_target_name
);
418 remote_target_name
= xstrdup (name
);
420 /* We need a 'serial' or 'udp' keyword to disambiguate host:port, which can
421 mean either a serial port on a terminal server, or the IP address of a
422 SPARClite demo board. If there's no colon, then it pretty much has to be
423 a local device (except for DOS... grrmble) */
425 p
= strchr (name
, ' ');
430 while ((*p
!= '\000') && isspace (*p
))
433 if (strncmp (name
, "serial", strlen (name
)) == 0)
435 else if (strncmp (name
, "udp", strlen (name
)) == 0)
438 error ("Must specify either `serial' or `udp'.");
444 if (!strchr (name
, ':'))
445 serial_flag
= 1; /* No colon is unambiguous (local device) */
447 error ("Usage: target sparclite serial /dev/ttyb\n\
448 or: target sparclite udp host");
453 remote_desc
= open_tty (p
);
455 old_chain
= make_cleanup (close_tty
, 0 /*ignore*/);
457 c
= send_resp (remote_desc
, 0x00);
460 error ("Unknown response (0x%x) from SparcLite. Try resetting the board.",
463 c
= send_resp (remote_desc
, 0x55);
466 error ("Sparclite appears to be ill.");
472 struct sockaddr_in sockaddr
;
473 unsigned char buffer
[100];
476 /* Setup the socket. Must be raw UDP. */
478 he
= gethostbyname (p
);
481 error ("No such host %s.", p
);
483 udp_fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
485 old_chain
= make_cleanup (close
, udp_fd
);
487 sockaddr
.sin_family
= PF_INET
;
488 sockaddr
.sin_port
= htons (7000);
489 memcpy (&sockaddr
.sin_addr
.s_addr
, he
->h_addr
, sizeof (struct in_addr
));
491 if (connect (udp_fd
, &sockaddr
, sizeof (sockaddr
)))
492 perror_with_name ("Connect failed");
497 send_udp_buf (udp_fd
, buffer
, 2); /* Request version */
498 cc
= recv_udp_buf (udp_fd
, buffer
, sizeof (buffer
), 5); /* Get response */
500 error ("SPARClite isn't responding.");
503 error ("SPARClite appears to be ill.");
505 error ("UDP downloading is not supported for DOS hosts.");
506 #endif /* HAVE_SOCKETS */
509 printf_unfiltered ("[SPARClite appears to be alive]\n");
511 push_target (&sparclite_ops
);
513 discard_cleanups (old_chain
);
519 sparclite_close (int quitting
)
524 else if (udp_fd
!= -1)
529 #define LOAD_ADDRESS 0x40000000
532 download (char *target_name
, char *args
, int from_tty
,
533 void (*write_routine
) (bfd
*from_bfd
, asection
*from_sec
,
534 file_ptr from_addr
, bfd_vma to_addr
, int len
),
535 void (*start_routine
) (bfd_vma entry
))
537 struct cleanup
*old_chain
;
542 #define WRITESIZE 1024
551 while (*args
!= '\000')
555 while (isspace (*args
))
560 while ((*args
!= '\000') && !isspace (*args
))
568 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
570 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
573 error ("unknown option `%s'", arg
);
577 filename
= get_exec_file (1);
579 pbfd
= bfd_openr (filename
, gnutarget
);
582 perror_with_name (filename
);
585 old_chain
= make_cleanup_bfd_close (pbfd
);
587 if (!bfd_check_format (pbfd
, bfd_object
))
588 error ("\"%s\" is not an object file: %s", filename
,
589 bfd_errmsg (bfd_get_error ()));
591 for (section
= pbfd
->sections
; section
; section
= section
->next
)
593 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
595 bfd_vma section_address
;
596 bfd_size_type section_size
;
598 const char *section_name
;
600 section_name
= bfd_get_section_name (pbfd
, section
);
602 section_address
= bfd_get_section_vma (pbfd
, section
);
604 /* Adjust sections from a.out files, since they don't
605 carry their addresses with. */
606 if (bfd_get_flavour (pbfd
) == bfd_target_aout_flavour
)
608 if (strcmp (section_name
, ".text") == 0)
609 section_address
= bfd_get_start_address (pbfd
);
610 else if (strcmp (section_name
, ".data") == 0)
612 /* Read the first 8 bytes of the data section.
613 There should be the string 'DaTa' followed by
614 a word containing the actual section address. */
617 char signature
[4]; /* 'DaTa' */
618 unsigned char sdata
[4]; /* &sdata */
621 bfd_get_section_contents (pbfd
, section
, &marker
, 0,
623 if (strncmp (marker
.signature
, "DaTa", 4) == 0)
625 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
626 section_address
= bfd_getb32 (marker
.sdata
);
628 section_address
= bfd_getl32 (marker
.sdata
);
633 section_size
= bfd_get_section_size_before_reloc (section
);
636 printf_filtered ("[Loading section %s at 0x%x (%d bytes)]\n",
637 bfd_get_section_name (pbfd
, section
),
642 while (section_size
> 0)
645 static char inds
[] = "|/-\\";
650 count
= min (section_size
, WRITESIZE
);
652 write_routine (pbfd
, section
, fptr
, section_address
, count
);
656 printf_unfiltered ("\r%c", inds
[k
++ % 4]);
657 gdb_flush (gdb_stdout
);
660 section_address
+= count
;
662 section_size
-= count
;
669 entry
= bfd_get_start_address (pbfd
);
672 printf_unfiltered ("[Starting %s at 0x%x]\n", filename
, entry
);
674 start_routine (entry
);
677 do_cleanups (old_chain
);
681 sparclite_serial_start (bfd_vma entry
)
687 store_unsigned_integer (buffer
+ 1, 4, entry
);
689 debug_serial_write (remote_desc
, buffer
, 1 + 4);
690 i
= readchar (remote_desc
, remote_timeout
);
692 error ("Can't start SparcLite. Error code %d\n", i
);
696 sparclite_serial_write (bfd
*from_bfd
, asection
*from_sec
, file_ptr from_addr
,
697 bfd_vma to_addr
, int len
)
699 char buffer
[4 + 4 + WRITESIZE
]; /* addr + len + data */
700 unsigned char checksum
;
703 store_unsigned_integer (buffer
, 4, to_addr
); /* Address */
704 store_unsigned_integer (buffer
+ 4, 4, len
); /* Length */
706 bfd_get_section_contents (from_bfd
, from_sec
, buffer
+ 8, from_addr
, len
);
709 for (i
= 0; i
< len
; i
++)
710 checksum
+= buffer
[8 + i
];
712 i
= send_resp (remote_desc
, 0x01);
715 error ("Bad response from load command (0x%x)", i
);
717 debug_serial_write (remote_desc
, buffer
, 4 + 4 + len
);
718 i
= readchar (remote_desc
, remote_timeout
);
721 error ("Bad checksum from load command (0x%x)", i
);
726 static unsigned short
727 calc_checksum (unsigned char *buffer
, int count
)
729 unsigned short checksum
;
732 for (; count
> 0; count
-= 2, buffer
+= 2)
733 checksum
+= (*buffer
<< 8) | *(buffer
+ 1);
736 checksum
+= *buffer
<< 8;
742 sparclite_udp_start (bfd_vma entry
)
744 unsigned char buffer
[6];
749 buffer
[2] = entry
>> 24;
750 buffer
[3] = entry
>> 16;
751 buffer
[4] = entry
>> 8;
754 send_udp_buf (udp_fd
, buffer
, 6); /* Send start addr */
755 i
= recv_udp_buf (udp_fd
, buffer
, sizeof (buffer
), -1); /* Get response */
757 if (i
< 1 || buffer
[0] != 0x55)
758 error ("Failed to take start address.");
762 sparclite_udp_write (bfd
*from_bfd
, asection
*from_sec
, file_ptr from_addr
,
763 bfd_vma to_addr
, int len
)
765 unsigned char buffer
[2000];
766 unsigned short checksum
;
767 static int pkt_num
= 0;
768 static unsigned long old_addr
= -1;
773 if (to_addr
!= old_addr
)
775 buffer
[0] = 0x1; /* Load command */
776 buffer
[1] = 0x1; /* Loading address */
777 buffer
[2] = to_addr
>> 24;
778 buffer
[3] = to_addr
>> 16;
779 buffer
[4] = to_addr
>> 8;
783 for (i
= 0; i
< 6; i
++)
784 checksum
+= buffer
[i
];
787 send_udp_buf (udp_fd
, buffer
, 6);
788 i
= recv_udp_buf (udp_fd
, buffer
, sizeof buffer
, -1);
791 error ("Got back short checksum for load addr.");
793 if (checksum
!= buffer
[0])
794 error ("Got back bad checksum for load addr.");
796 pkt_num
= 0; /* Load addr resets packet seq # */
800 bfd_get_section_contents (from_bfd
, from_sec
, buffer
+ 6, from_addr
,
803 checksum
= calc_checksum (buffer
+ 6, len
);
805 buffer
[0] = 0x1; /* Load command */
806 buffer
[1] = 0x2; /* Loading data */
807 buffer
[2] = pkt_num
>> 8;
809 buffer
[4] = checksum
>> 8;
810 buffer
[5] = checksum
;
812 send_udp_buf (udp_fd
, buffer
, len
+ 6);
813 i
= recv_udp_buf (udp_fd
, buffer
, sizeof buffer
, 3);
817 fprintf_unfiltered (gdb_stderr
, "send_data: timeout sending %d bytes to address 0x%x retrying\n", len
, to_addr
);
821 if (buffer
[0] != 0xff)
822 error ("Got back bad response for load data.");
831 #endif /* HAVE_SOCKETS */
834 sparclite_download (char *filename
, int from_tty
)
838 download (remote_target_name
, filename
, from_tty
, sparclite_udp_write
,
839 sparclite_udp_start
);
841 internal_error (__FILE__
, __LINE__
, "failed internal consistency check"); /* sparclite_open should prevent this! */
844 download (remote_target_name
, filename
, from_tty
, sparclite_serial_write
,
845 sparclite_serial_start
);
848 /* Set up the sparclite target vector. */
851 init_sparclite_ops (void)
853 sparclite_ops
.to_shortname
= "sparclite";
854 sparclite_ops
.to_longname
= "SPARClite download target";
855 sparclite_ops
.to_doc
= "Download to a remote SPARClite target board via serial of UDP.\n\
856 Specify the device it is connected to (e.g. /dev/ttya).";
857 sparclite_ops
.to_open
= sparclite_open
;
858 sparclite_ops
.to_close
= sparclite_close
;
859 sparclite_ops
.to_load
= sparclite_download
;
860 sparclite_ops
.to_stratum
= download_stratum
;
861 sparclite_ops
.to_magic
= OPS_MAGIC
;
865 _initialize_sparcl_tdep (void)
867 init_sparclite_ops ();
868 add_target (&sparclite_ops
);