Merge ktlim@zizzer:/bk/m5
[gem5.git] / src / 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 uint8_t
206 SimConsole::in()
207 {
208 bool empty;
209 uint8_t c;
210
211 empty = rxbuf.empty();
212 assert(!empty);
213 rxbuf.read((char *)&c, 1);
214 empty = rxbuf.empty();
215
216
217 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d\n",
218 isprint(c) ? c : ' ', c, !empty);
219
220 return c;
221 }
222
223 uint64_t
224 SimConsole::console_in()
225 {
226 uint64_t value;
227
228 if (dataAvailable()) {
229 value = RECEIVE_SUCCESS | in();
230 if (!rxbuf.empty())
231 value |= MORE_PENDING;
232 } else {
233 value = RECEIVE_NONE;
234 }
235
236 DPRINTF(ConsoleVerbose, "console_in: return: %#x\n", value);
237
238 return value;
239 }
240
241 void
242 SimConsole::out(char c)
243 {
244 #if TRACING_ON == 1
245 if (DTRACE(Console)) {
246 static char last = '\0';
247
248 if (c != '\n' && c != '\r' ||
249 last != '\n' && last != '\r') {
250 if (c == '\n' || c == '\r') {
251 int size = linebuf.size();
252 char *buffer = new char[size + 1];
253 linebuf.read(buffer, size);
254 buffer[size] = '\0';
255 DPRINTF(Console, "%s\n", buffer);
256 delete [] buffer;
257 } else {
258 linebuf.write(c);
259 }
260 }
261
262 last = c;
263 }
264 #endif
265
266 txbuf.write(c);
267
268 if (out_fd >= 0)
269 write(c);
270
271 if (outfile)
272 outfile->write(&c, 1);
273
274 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x\n",
275 isprint(c) ? c : ' ', (int)c);
276
277 }
278
279
280 void
281 SimConsole::serialize(ostream &os)
282 {
283 }
284
285 void
286 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
287 {
288 }
289
290
291 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
292
293 SimObjectParam<ConsoleListener *> listener;
294 SimObjectParam<IntrControl *> intr_control;
295 Param<string> output;
296 Param<bool> append_name;
297 Param<int> number;
298
299 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
300
301 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
302
303 INIT_PARAM(listener, "console listener"),
304 INIT_PARAM(intr_control, "interrupt controller"),
305 INIT_PARAM(output, "file to dump output to"),
306 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
307 INIT_PARAM_DFLT(number, "console number", 0)
308
309 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
310
311 CREATE_SIM_OBJECT(SimConsole)
312 {
313 string filename = output;
314 ostream *stream = NULL;
315
316 if (!filename.empty()) {
317 if (append_name)
318 filename += "." + getInstanceName();
319 stream = simout.find(filename);
320 }
321
322 SimConsole *console = new SimConsole(getInstanceName(), stream, number);
323 ((ConsoleListener *)listener)->add(console);
324
325 return console;
326 }
327
328 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
329
330 ////////////////////////////////////////////////////////////////////////
331 //
332 //
333
334 ConsoleListener::ConsoleListener(const string &name)
335 : SimObject(name), event(NULL)
336 {}
337
338 ConsoleListener::~ConsoleListener()
339 {
340 if (event)
341 delete event;
342 }
343
344 void
345 ConsoleListener::Event::process(int revent)
346 {
347 listener->accept();
348 }
349
350 ///////////////////////////////////////////////////////////////////////
351 // socket creation and console attach
352 //
353
354 void
355 ConsoleListener::listen(int port)
356 {
357 while (!listener.listen(port, true)) {
358 DPRINTF(Console,
359 ": can't bind address console port %d inuse PID %d\n",
360 port, getpid());
361 port++;
362 }
363
364 ccprintf(cerr, "Listening for console connection on port %d\n", port);
365
366 event = new Event(this, listener.getfd(), POLLIN);
367 pollQueue.schedule(event);
368 }
369
370 void
371 ConsoleListener::add(SimConsole *cons)
372 { ConsoleList.push_back(cons);}
373
374 void
375 ConsoleListener::accept()
376 {
377 if (!listener.islistening())
378 panic("%s: cannot accept a connection if not listening!", name());
379
380 int sfd = listener.accept(true);
381 if (sfd != -1) {
382 iter_t i = ConsoleList.begin();
383 iter_t end = ConsoleList.end();
384 if (i == end) {
385 close(sfd);
386 } else {
387 (*i)->attach(sfd, this);
388 i = ConsoleList.erase(i);
389 }
390 }
391 }
392
393 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
394
395 Param<int> port;
396
397 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
398
399 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
400
401 INIT_PARAM_DFLT(port, "listen port", 3456)
402
403 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
404
405 CREATE_SIM_OBJECT(ConsoleListener)
406 {
407 ConsoleListener *listener = new ConsoleListener(getInstanceName());
408 listener->listen(port);
409
410 return listener;
411 }
412
413 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)