Commit | Line | Data |
---|---|---|
c906108c SS |
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 | |
3fd725ef | 6 | the Free Software Foundation; either version 3 of the License, or |
c906108c SS |
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 | |
51b318de | 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
16 | |
17 | /**********************************************************************/ | |
18 | /* Forks the ARMulator and hangs on a socket passing on RDP messages */ | |
19 | /* down a pipe to the ARMulator which translates them into RDI calls. */ | |
20 | /**********************************************************************/ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/socket.h> | |
26 | #include <netinet/in.h> | |
27 | #include <signal.h> | |
28 | #include <netdb.h> | |
29 | #include <unistd.h> | |
30 | ||
31 | #include "armdefs.h" | |
32 | #include "dbg_rdi.h" | |
33 | #include "dbg_conf.h" | |
34 | ||
35 | #define MAXHOSTNAMELENGTH 64 | |
36 | ||
37 | /* Read and write routines down sockets and pipes */ | |
38 | ||
dfcd3bfb JM |
39 | void MYread_chars (int sock, void *p, int n); |
40 | unsigned char MYread_char (int sock); | |
41 | ARMword MYread_word (int sock); | |
42 | void MYread_FPword (int sock, char *putinhere); | |
c906108c | 43 | |
dfcd3bfb JM |
44 | void MYwrite_word (int sock, ARMword i); |
45 | void MYwrite_string (int sock, char *s); | |
46 | void MYwrite_FPword (int sock, char *fromhere); | |
47 | void MYwrite_char (int sock, unsigned char c); | |
c906108c | 48 | |
dfcd3bfb | 49 | void passon (int source, int dest, int n); |
c906108c SS |
50 | |
51 | ||
52 | /* Mother and child processes */ | |
53 | void parent (void); | |
dfcd3bfb | 54 | void kid (void); |
c906108c SS |
55 | |
56 | /* The child process id. */ | |
57 | pid_t child; | |
58 | ||
59 | /* The socket to the debugger */ | |
60 | int debugsock; | |
61 | ||
62 | /* The pipes between the two processes */ | |
63 | int mumkid[2]; | |
64 | int kidmum[2]; | |
65 | ||
66 | /* A pipe for handling SWI return values that goes straight from the */ | |
67 | /* parent to the ARMulator host interface, bypassing the childs RDP */ | |
68 | /* to RDI interpreter */ | |
69 | int DebuggerARMul[2]; | |
70 | ||
71 | /* The maximum number of file descriptors */ | |
72 | int nfds; | |
73 | ||
74 | /* The socket handle */ | |
75 | int sockethandle; | |
76 | ||
77 | /* The machine name */ | |
78 | char localhost[MAXHOSTNAMELENGTH + 1]; | |
79 | ||
80 | /* The socket number */ | |
81 | unsigned int socketnumber; | |
82 | ||
83 | /**************************************************************/ | |
84 | /* Takes one argument: the socket number. */ | |
85 | /* Opens a socket to the debugger, and once opened spawns the */ | |
86 | /* ARMulator and sets up a couple of pipes. */ | |
87 | /**************************************************************/ | |
dfcd3bfb JM |
88 | int |
89 | main (int argc, char *argv[]) | |
90 | { | |
c906108c SS |
91 | int i; |
92 | struct sockaddr_in devil, isa; | |
93 | struct hostent *hp; | |
94 | ||
95 | ||
dfcd3bfb JM |
96 | if (argc == 1) |
97 | { | |
98 | fprintf (stderr, "No socket number\n"); | |
99 | return 1; | |
100 | } | |
c906108c | 101 | |
dfcd3bfb JM |
102 | sscanf (argv[1], "%d", &socketnumber); |
103 | if (!socketnumber || socketnumber > 0xffff) | |
104 | { | |
105 | fprintf (stderr, "Invalid socket number: %d\n", socketnumber); | |
106 | return 1; | |
107 | } | |
c906108c | 108 | |
dfcd3bfb JM |
109 | gethostname (localhost, MAXHOSTNAMELENGTH); |
110 | hp = gethostbyname (localhost); | |
111 | if (!hp) | |
112 | { | |
113 | fprintf (stderr, "Cannot get local host info\n"); | |
114 | return 1; | |
115 | } | |
c906108c SS |
116 | |
117 | /* Open a socket */ | |
dfcd3bfb | 118 | sockethandle = socket (hp->h_addrtype, SOCK_STREAM, 0); |
363a6e9f | 119 | if (sockethandle == -1) |
dfcd3bfb JM |
120 | { |
121 | perror ("socket"); | |
122 | return 1; | |
123 | } | |
c906108c SS |
124 | |
125 | devil.sin_family = hp->h_addrtype; | |
dfcd3bfb | 126 | devil.sin_port = htons (socketnumber); |
c906108c | 127 | devil.sin_addr.s_addr = 0; |
dfcd3bfb JM |
128 | for (i = 0; i < sizeof (devil.sin_zero); i++) |
129 | devil.sin_zero[i] = '\000'; | |
130 | memcpy (&devil.sin_addr, hp->h_addr_list[0], hp->h_length); | |
c906108c | 131 | |
dfcd3bfb JM |
132 | if (bind (sockethandle, &devil, sizeof (devil)) < 0) |
133 | { | |
134 | perror ("bind"); | |
135 | return 1; | |
136 | } | |
c906108c SS |
137 | |
138 | /* May only accept one debugger at once */ | |
139 | ||
dfcd3bfb JM |
140 | if (listen (sockethandle, 0)) |
141 | { | |
142 | perror ("listen"); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | fprintf (stderr, "Waiting for connection from debugger..."); | |
147 | ||
148 | debugsock = accept (sockethandle, &isa, &i); | |
363a6e9f | 149 | if (debugsock == -1) |
dfcd3bfb JM |
150 | { |
151 | perror ("accept"); | |
152 | return 1; | |
153 | } | |
154 | ||
155 | fprintf (stderr, " done.\nConnection Established.\n"); | |
156 | ||
157 | nfds = getdtablesize (); | |
158 | ||
159 | if (pipe (mumkid)) | |
160 | { | |
161 | perror ("pipe"); | |
162 | return 1; | |
163 | } | |
164 | if (pipe (kidmum)) | |
165 | { | |
166 | perror ("pipe"); | |
167 | return 1; | |
168 | } | |
169 | ||
170 | if (pipe (DebuggerARMul)) | |
171 | { | |
172 | perror ("pipe"); | |
173 | return 1; | |
174 | } | |
175 | ||
c906108c | 176 | #ifdef DEBUG |
dfcd3bfb | 177 | fprintf (stderr, "Created pipes ok\n"); |
c906108c SS |
178 | #endif |
179 | ||
dfcd3bfb | 180 | child = fork (); |
c906108c SS |
181 | |
182 | #ifdef DEBUG | |
dfcd3bfb | 183 | fprintf (stderr, "fork() ok\n"); |
c906108c SS |
184 | #endif |
185 | ||
dfcd3bfb JM |
186 | if (child == 0) |
187 | kid (); | |
188 | if (child != -1) | |
189 | parent (); | |
c906108c | 190 | |
dfcd3bfb | 191 | perror ("fork"); |
c906108c SS |
192 | return 1; |
193 | } |