f012b28e6da1bb53fb64b160021aa219c618bad0
[external/binutils.git] / sim / arm / main.c
1 /*  main.c -- top level of ARMulator:  ARM6 Instruction Emulator.
2     Copyright (C) 1994 Advanced RISC Machines Ltd.
3  
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8  
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13  
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 /**********************************************************************/
19 /* Forks the ARMulator and hangs on a socket passing on RDP messages  */
20 /* down a pipe to the ARMulator which translates them into RDI calls. */
21 /**********************************************************************/
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <signal.h>
29 #include <netdb.h>
30 #include <unistd.h>
31
32 #include "armdefs.h"
33 #include "dbg_rdi.h"
34 #include "dbg_conf.h"
35
36 #define MAXHOSTNAMELENGTH 64
37
38 /* Read and write routines down sockets and pipes */
39
40 void MYread_chars(int sock, void *p, int n);
41 unsigned char MYread_char(int sock);
42 ARMword MYread_word(int sock);
43 void MYread_FPword(int sock, char *putinhere);
44
45 void MYwrite_word(int sock, ARMword i);
46 void MYwrite_string(int sock, char *s);
47 void MYwrite_FPword(int sock, char *fromhere);
48 void MYwrite_char(int sock, unsigned char c);
49
50 void passon(int source, int dest, int n);
51
52
53 /* Mother and child processes */
54 void parent (void);
55 void kid(void);
56
57 /* The child process id. */
58 pid_t child;
59
60 /* The socket to the debugger */
61 int debugsock;
62
63 /* The pipes between the two processes */
64 int mumkid[2];
65 int kidmum[2];
66
67 /* A pipe for handling SWI return values that goes straight from the */
68 /* parent to the ARMulator host interface, bypassing the childs RDP */
69 /* to RDI interpreter */
70 int DebuggerARMul[2];
71
72 /* The maximum number of file descriptors */
73 int nfds;
74
75 /* The socket handle */
76 int sockethandle;
77
78 /* The machine name */
79 char localhost[MAXHOSTNAMELENGTH + 1];
80
81 /* The socket number */
82 unsigned int socketnumber;
83
84 /**************************************************************/
85 /* Takes one argument: the socket number.                     */
86 /* Opens a socket to the debugger, and once opened spawns the */
87 /* ARMulator and sets up a couple of pipes.                   */
88 /**************************************************************/
89 int main(int argc, char *argv[]) {
90   int i;
91   struct sockaddr_in devil, isa;
92   struct hostent *hp;
93
94
95   if (argc == 1) {
96     fprintf(stderr, "No socket number\n");
97     return 1;
98   }
99
100   sscanf(argv[1], "%d", &socketnumber);
101   if (!socketnumber || socketnumber > 0xffff) {
102     fprintf(stderr, "Invalid socket number: %d\n", socketnumber);
103     return 1;
104   }
105
106   gethostname(localhost, MAXHOSTNAMELENGTH);
107   hp = gethostbyname(localhost);
108   if (!hp) {
109     fprintf(stderr, "Cannot get local host info\n");
110     return 1;
111   }
112
113   /* Open a socket */
114   sockethandle = socket(hp->h_addrtype, SOCK_STREAM, 0);
115   if (sockethandle < 0) {
116     perror("socket");
117     return 1;
118   }
119
120   devil.sin_family = hp->h_addrtype;
121   devil.sin_port = htons(socketnumber);
122   devil.sin_addr.s_addr = 0;
123   for(i = 0; i < sizeof(devil.sin_zero); i++) devil.sin_zero[i] = '\000';
124   memcpy(&devil.sin_addr, hp->h_addr_list[0], hp->h_length);
125
126   if (bind(sockethandle, &devil, sizeof(devil)) < 0) {
127     perror("bind");
128     return 1;
129   }
130
131   /* May only accept one debugger at once */
132
133   if (listen(sockethandle, 0)) {
134     perror("listen");
135     return 1;
136   }
137
138   fprintf(stderr, "Waiting for connection from debugger...");
139
140   debugsock = accept(sockethandle, &isa, &i);
141   if (debugsock < 0) {
142     perror("accept");
143     return 1;
144   }
145   
146   fprintf(stderr, " done.\nConnection Established.\n");
147
148   nfds = getdtablesize();
149
150   if (pipe(mumkid)) {
151     perror("pipe");
152     return 1;
153   }
154   if (pipe(kidmum)) {
155     perror("pipe");
156     return 1;
157   }
158   
159   if (pipe(DebuggerARMul)) {
160     perror("pipe");
161     return 1;
162   }
163   
164 #ifdef DEBUG
165       fprintf(stderr, "Created pipes ok\n");
166 #endif
167
168   child = fork();
169
170 #ifdef DEBUG
171   fprintf(stderr, "fork() ok\n");
172 #endif
173
174   if (child ==  0) kid ();
175   if (child != -1) parent ();
176
177   perror("fork");
178   return 1;
179 }
180
181
182
183