Very minor formatting glitches.
[gem5.git] / dev / simconsole.cc
1 /*
2 * Copyright (c) 2004 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/socket.hh"
47 #include "base/trace.hh"
48 #include "dev/simconsole.hh"
49 #include "mem/functional_mem/memory_control.hh"
50 #include "sim/builder.hh"
51 #include "targetarch/ev5.hh"
52 #include "dev/uart.hh"
53 #include "dev/platform.hh"
54
55 using namespace std;
56
57 ////////////////////////////////////////////////////////////////////////
58 //
59 //
60
61 SimConsole::Event::Event(SimConsole *c, int fd, int e)
62 : PollEvent(fd, e), cons(c)
63 {
64 }
65
66 void
67 SimConsole::Event::process(int revent)
68 {
69 if (revent & POLLIN)
70 cons->data();
71 else if (revent & POLLNVAL)
72 cons->detach();
73 }
74
75 SimConsole::SimConsole(const string &name, std::ostream *os, int num)
76 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
77 listener(NULL), txbuf(16384), rxbuf(16384), outfile(os)
78 #if TRACING_ON == 1
79 , linebuf(16384)
80 #endif
81 {
82 if (outfile)
83 outfile->setf(ios::unitbuf);
84 }
85
86 SimConsole::~SimConsole()
87 {
88 close();
89 if (outfile)
90 closeOutputStream(outfile);
91 }
92
93 void
94 SimConsole::close()
95 {
96 if (in_fd != -1)
97 ::close(in_fd);
98
99 if (out_fd != in_fd && out_fd != -1)
100 ::close(out_fd);
101 }
102
103 void
104 SimConsole::attach(int in, int out, ConsoleListener *l)
105 {
106 in_fd = in;
107 out_fd = out;
108 listener = l;
109
110 event = new Event(this, in, POLLIN);
111 pollQueue.schedule(event);
112
113 stringstream stream;
114 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
115
116 // we need an actual carriage return followed by a newline for the
117 // terminal
118 stream << "\r\n";
119
120 write((const uint8_t *)stream.str().c_str(), stream.str().size());
121
122
123 DPRINTFN("attach console %d\n", number);
124
125 txbuf.readall(out);
126 }
127
128 void
129 SimConsole::detach()
130 {
131 close();
132 in_fd = -1;
133 out_fd = -1;
134
135 pollQueue.remove(event);
136
137 if (listener) {
138 listener->add(this);
139 listener = NULL;
140 }
141
142 DPRINTFN("detach console %d\n", number);
143 }
144
145 void
146 SimConsole::data()
147 {
148 uint8_t buf[1024];
149 int len;
150
151 len = read(buf, sizeof(buf));
152 if (len) {
153 rxbuf.write((char *)buf, len);
154 // Inform the UART there is data available
155 uart->dataAvailable();
156 }
157 }
158
159 size_t
160 SimConsole::read(uint8_t *buf, size_t len)
161 {
162 if (in_fd < 0)
163 panic("Console not properly attached.\n");
164
165 size_t ret;
166 do {
167 ret = ::read(in_fd, buf, len);
168 } while (ret == -1 && errno == EINTR);
169
170
171 if (ret < 0)
172 DPRINTFN("Read failed.\n");
173
174 if (ret <= 0) {
175 detach();
176 return 0;
177 }
178
179 return ret;
180 }
181
182 // Console output.
183 size_t
184 SimConsole::write(const uint8_t *buf, size_t len)
185 {
186 if (out_fd < 0)
187 panic("Console not properly attached.\n");
188
189 size_t ret;
190 for (;;) {
191 ret = ::write(out_fd, buf, len);
192
193 if (ret >= 0)
194 break;
195
196 if (errno != EINTR)
197 detach();
198 }
199
200 return ret;
201 }
202
203 #define MORE_PENDING (ULL(1) << 61)
204 #define RECEIVE_SUCCESS (ULL(0) << 62)
205 #define RECEIVE_NONE (ULL(2) << 62)
206 #define RECEIVE_ERROR (ULL(3) << 62)
207
208 bool
209 SimConsole::in(uint8_t &c)
210 {
211 bool empty, ret;
212
213 empty = rxbuf.empty();
214 ret = !empty;
215 if (!empty) {
216 rxbuf.read((char *)&c, 1);
217 empty = rxbuf.empty();
218 }
219
220 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d, return: %d\n",
221 isprint(c) ? c : ' ', c, !empty, ret);
222
223 return ret;
224 }
225
226 uint64_t
227 SimConsole::console_in()
228 {
229 uint8_t c;
230 uint64_t value;
231
232 if (in(c)) {
233 value = RECEIVE_SUCCESS | c;
234 if (!rxbuf.empty())
235 value |= MORE_PENDING;
236 } else {
237 value = RECEIVE_NONE;
238 }
239
240 DPRINTF(ConsoleVerbose, "console_in: return: %#x\n", value);
241
242 return value;
243 }
244
245 void
246 SimConsole::out(char c)
247 {
248 #if TRACING_ON == 1
249 if (DTRACE(Console)) {
250 static char last = '\0';
251
252 if (c != '\n' && c != '\r' ||
253 last != '\n' && last != '\r') {
254 if (c == '\n' || c == '\r') {
255 int size = linebuf.size();
256 char *buffer = new char[size + 1];
257 linebuf.read(buffer, size);
258 buffer[size] = '\0';
259 DPRINTF(Console, "%s\n", buffer);
260 delete [] buffer;
261 } else {
262 linebuf.write(c);
263 }
264 }
265
266 last = c;
267 }
268 #endif
269
270 txbuf.write(c);
271
272 if (out_fd >= 0)
273 write(c);
274
275 if (outfile)
276 outfile->write(&c, 1);
277
278 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x\n",
279 isprint(c) ? c : ' ', (int)c);
280
281 }
282
283
284 void
285 SimConsole::serialize(ostream &os)
286 {
287 }
288
289 void
290 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
291 {
292 }
293
294
295 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
296
297 SimObjectParam<ConsoleListener *> listener;
298 SimObjectParam<IntrControl *> intr_control;
299 Param<string> output;
300 Param<bool> append_name;
301 Param<int> number;
302
303 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
304
305 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
306
307 INIT_PARAM(listener, "console listener"),
308 INIT_PARAM(intr_control, "interrupt controller"),
309 INIT_PARAM(output, "file to dump output to"),
310 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
311 INIT_PARAM_DFLT(number, "console number", 0)
312
313 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
314
315 CREATE_SIM_OBJECT(SimConsole)
316 {
317 string filename;
318
319 if (!output.isValid()) {
320 filename = getInstanceName();
321 } else if (append_name) {
322 filename = (string)output + "." + getInstanceName();
323 } else {
324 filename = output;
325 }
326
327 SimConsole *console = new SimConsole(getInstanceName(),
328 makeOutputStream(filename), number);
329 ((ConsoleListener *)listener)->add(console);
330
331 return console;
332 }
333
334 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
335
336 ////////////////////////////////////////////////////////////////////////
337 //
338 //
339
340 ConsoleListener::ConsoleListener(const string &name)
341 : SimObject(name), event(NULL)
342 {}
343
344 ConsoleListener::~ConsoleListener()
345 {
346 if (event)
347 delete event;
348 }
349
350 void
351 ConsoleListener::Event::process(int revent)
352 {
353 listener->accept();
354 }
355
356 ///////////////////////////////////////////////////////////////////////
357 // socket creation and console attach
358 //
359
360 void
361 ConsoleListener::listen(int port)
362 {
363 while (!listener.listen(port, true)) {
364 DPRINTF(Console,
365 ": can't bind address console port %d inuse PID %d\n",
366 port, getpid());
367 port++;
368 }
369
370 ccprintf(cerr, "Listening for console connection on port %d\n", port);
371
372 event = new Event(this, listener.getfd(), POLLIN);
373 pollQueue.schedule(event);
374 }
375
376 void
377 ConsoleListener::add(SimConsole *cons)
378 { ConsoleList.push_back(cons);}
379
380 void
381 ConsoleListener::accept()
382 {
383 if (!listener.islistening())
384 panic("%s: cannot accept a connection if not listening!", name());
385
386 int sfd = listener.accept(true);
387 if (sfd != -1) {
388 iter_t i = ConsoleList.begin();
389 iter_t end = ConsoleList.end();
390 if (i == end) {
391 close(sfd);
392 } else {
393 (*i)->attach(sfd, this);
394 i = ConsoleList.erase(i);
395 }
396 }
397 }
398
399 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
400
401 Param<int> port;
402
403 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
404
405 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
406
407 INIT_PARAM_DFLT(port, "listen port", 3456)
408
409 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
410
411 CREATE_SIM_OBJECT(ConsoleListener)
412 {
413 ConsoleListener *listener = new ConsoleListener(getInstanceName());
414 listener->listen(port);
415
416 return listener;
417 }
418
419 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)