First steps toward getting full system to work with
[gem5.git] / dev / simconsole.cc
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* @file
30 * Implements the user interface to a serial console
31 */
32
33 #include <sys/ioctl.h>
34 #include <sys/termios.h>
35 #include <sys/types.h>
36 #include <errno.h>
37 #include <poll.h>
38 #include <unistd.h>
39
40 #include <iostream>
41 #include <fstream>
42 #include <sstream>
43 #include <string>
44
45 #include "base/misc.hh"
46 #include "base/output.hh"
47 #include "base/socket.hh"
48 #include "base/trace.hh"
49 #include "dev/platform.hh"
50 #include "dev/simconsole.hh"
51 #include "dev/uart.hh"
52 #include "sim/builder.hh"
53
54 using namespace std;
55
56 ////////////////////////////////////////////////////////////////////////
57 //
58 //
59
60 SimConsole::Event::Event(SimConsole *c, int fd, int e)
61 : PollEvent(fd, e), cons(c)
62 {
63 }
64
65 void
66 SimConsole::Event::process(int revent)
67 {
68 if (revent & POLLIN)
69 cons->data();
70 else if (revent & POLLNVAL)
71 cons->detach();
72 }
73
74 SimConsole::SimConsole(const string &name, ostream *os, int num)
75 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
76 listener(NULL), txbuf(16384), rxbuf(16384), outfile(os)
77 #if TRACING_ON == 1
78 , linebuf(16384)
79 #endif
80 {
81 if (outfile)
82 outfile->setf(ios::unitbuf);
83 }
84
85 SimConsole::~SimConsole()
86 {
87 close();
88 }
89
90 void
91 SimConsole::close()
92 {
93 if (in_fd != -1)
94 ::close(in_fd);
95
96 if (out_fd != in_fd && out_fd != -1)
97 ::close(out_fd);
98 }
99
100 void
101 SimConsole::attach(int in, int out, ConsoleListener *l)
102 {
103 in_fd = in;
104 out_fd = out;
105 listener = l;
106
107 event = new Event(this, in, POLLIN);
108 pollQueue.schedule(event);
109
110 stringstream stream;
111 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
112
113 // we need an actual carriage return followed by a newline for the
114 // terminal
115 stream << "\r\n";
116
117 write((const uint8_t *)stream.str().c_str(), stream.str().size());
118
119
120 DPRINTFN("attach console %d\n", number);
121
122 txbuf.readall(out);
123 }
124
125 void
126 SimConsole::detach()
127 {
128 close();
129 in_fd = -1;
130 out_fd = -1;
131
132 pollQueue.remove(event);
133
134 if (listener) {
135 listener->add(this);
136 listener = NULL;
137 }
138
139 DPRINTFN("detach console %d\n", number);
140 }
141
142 void
143 SimConsole::data()
144 {
145 uint8_t buf[1024];
146 int len;
147
148 len = read(buf, sizeof(buf));
149 if (len) {
150 rxbuf.write((char *)buf, len);
151 // Inform the UART there is data available
152 uart->dataAvailable();
153 }
154 }
155
156 size_t
157 SimConsole::read(uint8_t *buf, size_t len)
158 {
159 if (in_fd < 0)
160 panic("Console not properly attached.\n");
161
162 size_t ret;
163 do {
164 ret = ::read(in_fd, buf, len);
165 } while (ret == -1 && errno == EINTR);
166
167
168 if (ret < 0)
169 DPRINTFN("Read failed.\n");
170
171 if (ret <= 0) {
172 detach();
173 return 0;
174 }
175
176 return ret;
177 }
178
179 // Console output.
180 size_t
181 SimConsole::write(const uint8_t *buf, size_t len)
182 {
183 if (out_fd < 0)
184 panic("Console not properly attached.\n");
185
186 size_t ret;
187 for (;;) {
188 ret = ::write(out_fd, buf, len);
189
190 if (ret >= 0)
191 break;
192
193 if (errno != EINTR)
194 detach();
195 }
196
197 return ret;
198 }
199
200 #define MORE_PENDING (ULL(1) << 61)
201 #define RECEIVE_SUCCESS (ULL(0) << 62)
202 #define RECEIVE_NONE (ULL(2) << 62)
203 #define RECEIVE_ERROR (ULL(3) << 62)
204
205 bool
206 SimConsole::in(uint8_t &c)
207 {
208 bool empty, ret;
209
210 empty = rxbuf.empty();
211 ret = !empty;
212 if (!empty) {
213 rxbuf.read((char *)&c, 1);
214 empty = rxbuf.empty();
215 }
216
217 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d, return: %d\n",
218 isprint(c) ? c : ' ', c, !empty, ret);
219
220 return ret;
221 }
222
223 uint64_t
224 SimConsole::console_in()
225 {
226 uint8_t c;
227 uint64_t value;
228
229 if (in(c)) {
230 value = RECEIVE_SUCCESS | c;
231 if (!rxbuf.empty())
232 value |= MORE_PENDING;
233 } else {
234 value = RECEIVE_NONE;
235 }
236
237 DPRINTF(ConsoleVerbose, "console_in: return: %#x\n", value);
238
239 return value;
240 }
241
242 void
243 SimConsole::out(char c)
244 {
245 #if TRACING_ON == 1
246 if (DTRACE(Console)) {
247 static char last = '\0';
248
249 if (c != '\n' && c != '\r' ||
250 last != '\n' && last != '\r') {
251 if (c == '\n' || c == '\r') {
252 int size = linebuf.size();
253 char *buffer = new char[size + 1];
254 linebuf.read(buffer, size);
255 buffer[size] = '\0';
256 DPRINTF(Console, "%s\n", buffer);
257 delete [] buffer;
258 } else {
259 linebuf.write(c);
260 }
261 }
262
263 last = c;
264 }
265 #endif
266
267 txbuf.write(c);
268
269 if (out_fd >= 0)
270 write(c);
271
272 if (outfile)
273 outfile->write(&c, 1);
274
275 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x\n",
276 isprint(c) ? c : ' ', (int)c);
277
278 }
279
280
281 void
282 SimConsole::serialize(ostream &os)
283 {
284 }
285
286 void
287 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
288 {
289 }
290
291
292 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
293
294 SimObjectParam<ConsoleListener *> listener;
295 SimObjectParam<IntrControl *> intr_control;
296 Param<string> output;
297 Param<bool> append_name;
298 Param<int> number;
299
300 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
301
302 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
303
304 INIT_PARAM(listener, "console listener"),
305 INIT_PARAM(intr_control, "interrupt controller"),
306 INIT_PARAM(output, "file to dump output to"),
307 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
308 INIT_PARAM_DFLT(number, "console number", 0)
309
310 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
311
312 CREATE_SIM_OBJECT(SimConsole)
313 {
314 string filename = output;
315 ostream *stream = NULL;
316
317 if (!filename.empty()) {
318 if (append_name)
319 filename += "." + getInstanceName();
320 stream = simout.find(filename);
321 }
322
323 SimConsole *console = new SimConsole(getInstanceName(), stream, number);
324 ((ConsoleListener *)listener)->add(console);
325
326 return console;
327 }
328
329 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
330
331 ////////////////////////////////////////////////////////////////////////
332 //
333 //
334
335 ConsoleListener::ConsoleListener(const string &name)
336 : SimObject(name), event(NULL)
337 {}
338
339 ConsoleListener::~ConsoleListener()
340 {
341 if (event)
342 delete event;
343 }
344
345 void
346 ConsoleListener::Event::process(int revent)
347 {
348 listener->accept();
349 }
350
351 ///////////////////////////////////////////////////////////////////////
352 // socket creation and console attach
353 //
354
355 void
356 ConsoleListener::listen(int port)
357 {
358 while (!listener.listen(port, true)) {
359 DPRINTF(Console,
360 ": can't bind address console port %d inuse PID %d\n",
361 port, getpid());
362 port++;
363 }
364
365 ccprintf(cerr, "Listening for console connection on port %d\n", port);
366
367 event = new Event(this, listener.getfd(), POLLIN);
368 pollQueue.schedule(event);
369 }
370
371 void
372 ConsoleListener::add(SimConsole *cons)
373 { ConsoleList.push_back(cons);}
374
375 void
376 ConsoleListener::accept()
377 {
378 if (!listener.islistening())
379 panic("%s: cannot accept a connection if not listening!", name());
380
381 int sfd = listener.accept(true);
382 if (sfd != -1) {
383 iter_t i = ConsoleList.begin();
384 iter_t end = ConsoleList.end();
385 if (i == end) {
386 close(sfd);
387 } else {
388 (*i)->attach(sfd, this);
389 i = ConsoleList.erase(i);
390 }
391 }
392 }
393
394 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
395
396 Param<int> port;
397
398 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
399
400 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
401
402 INIT_PARAM_DFLT(port, "listen port", 3456)
403
404 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
405
406 CREATE_SIM_OBJECT(ConsoleListener)
407 {
408 ConsoleListener *listener = new ConsoleListener(getInstanceName());
409 listener->listen(port);
410
411 return listener;
412 }
413
414 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)