Make each stat take up one full line. This allows us to use grep
[gem5.git] / dev / console.cc
1 /*
2 * Copyright (c) 2003 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 * User Console Definitions
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/console.hh"
49 #include "mem/functional_mem/memory_control.hh"
50 #include "sim/builder.hh"
51 #include "targetarch/ev5.hh"
52
53 using namespace std;
54
55 ////////////////////////////////////////////////////////////////////////
56 //
57 //
58
59 SimConsole::Event::Event(SimConsole *c, int fd, int e)
60 : PollEvent(fd, e), cons(c)
61 {
62 }
63
64 void
65 SimConsole::Event::process(int revent)
66 {
67 if (revent & POLLIN)
68 cons->data();
69 else if (revent & POLLNVAL)
70 cons->detach();
71 }
72
73 SimConsole::SimConsole(const string &name, const string &file, int num)
74 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
75 listener(NULL), txbuf(16384), rxbuf(16384), outfile(NULL),
76 #if TRACING_ON == 1
77 linebuf(16384),
78 #endif
79 _status(0), _enable(0), intr(NULL)
80 {
81 if (!file.empty())
82 outfile = new ofstream(file.c_str());
83
84 if (outfile)
85 outfile->setf(ios::unitbuf);
86 }
87
88 SimConsole::~SimConsole()
89 {
90 close();
91
92 if (outfile)
93 delete outfile;
94 }
95
96 void
97 SimConsole::close()
98 {
99 if (in_fd != -1)
100 ::close(in_fd);
101
102 if (out_fd != in_fd && out_fd != -1)
103 ::close(out_fd);
104 }
105
106 void
107 SimConsole::attach(int in, int out, ConsoleListener *l)
108 {
109 in_fd = in;
110 out_fd = out;
111 listener = l;
112
113 event = new Event(this, in, POLLIN);
114 pollQueue.schedule(event);
115
116 stringstream stream;
117 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
118
119 // we need an actual carriage return followed by a newline for the
120 // terminal
121 stream << "\r\n";
122
123 write((const uint8_t *)stream.str().c_str(), stream.str().size());
124
125
126 DPRINTFN("attach console %d\n", number);
127
128 txbuf.readall(out);
129 }
130
131 void
132 SimConsole::detach()
133 {
134 close();
135 in_fd = -1;
136 out_fd = -1;
137
138 pollQueue.remove(event);
139
140 if (listener) {
141 listener->add(this);
142 listener = NULL;
143 }
144
145 DPRINTFN("detach console %d\n", number);
146 }
147
148 void
149 SimConsole::data()
150 {
151 uint8_t buf[1024];
152 int len;
153
154 len = read(buf, sizeof(buf));
155 if (len) {
156 rxbuf.write((char *)buf, len);
157 raiseInt(ReceiveInterrupt);
158 }
159 }
160
161 size_t
162 SimConsole::read(uint8_t *buf, size_t len)
163 {
164 if (in_fd < 0)
165 panic("SimConsole(read): Console not properly attached.\n");
166
167 size_t ret;
168 do {
169 ret = ::read(in_fd, buf, len);
170 } while (ret == -1 && errno == EINTR);
171
172
173 if (ret < 0)
174 DPRINTFN("SimConsole(read): Read failed.\n");
175
176 if (ret <= 0) {
177 detach();
178 return 0;
179 }
180
181 return ret;
182 }
183
184 // Console output.
185 size_t
186 SimConsole::write(const uint8_t *buf, size_t len)
187 {
188 if (out_fd < 0)
189 panic("SimConsole(write): Console not properly attached.\n");
190
191 size_t ret;
192 for (;;) {
193 ret = ::write(out_fd, buf, len);
194
195 if (ret >= 0)
196 break;
197
198 if (errno != EINTR)
199 detach();
200 }
201
202 return ret;
203 }
204
205 void
206 SimConsole::configTerm()
207 {
208 struct termios ios;
209
210 if (isatty(out_fd)) {
211 if (tcgetattr(out_fd, &ios) < 0) {
212 panic( "tcgetattr\n");
213 }
214 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
215 ios.c_oflag &= ~(OPOST);
216 ios.c_oflag &= (ONLCR);
217 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
218 ios.c_cc[VMIN] = 1;
219 ios.c_cc[VTIME] = 0;
220 if (tcsetattr(out_fd, TCSANOW, &ios) < 0) {
221 panic( "tcsetattr\n");
222 }
223 }
224 }
225
226 #define MORE_PENDING (ULL(1) << 61)
227 #define RECEIVE_SUCCESS (ULL(0) << 62)
228 #define RECEIVE_NONE (ULL(2) << 62)
229 #define RECEIVE_ERROR (ULL(3) << 62)
230
231 uint64_t
232 SimConsole::in()
233 {
234 char c = 0;
235 uint64_t val = 0;
236 if (rxbuf.empty()) {
237 clearInt(ReceiveInterrupt);
238 val |= RECEIVE_NONE;
239 return 0x8;
240 } else {
241 uint64_t val;
242 rxbuf.read(&c, 1);
243 val |= RECEIVE_SUCCESS | c;
244 if (!rxbuf.empty())
245 val |= MORE_PENDING;
246 }
247
248 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x retval: %#x\n",
249 isprint(c) ? c : ' ', c, val);
250
251 return val;
252 }
253
254 void
255 SimConsole::out(char c, bool raise_int)
256 {
257 #if TRACING_ON == 1
258 if (DTRACE(Console)) {
259 static char last = '\0';
260
261 if (c != '\n' && c != '\r' ||
262 last != '\n' && last != '\r') {
263 if (c == '\n' || c == '\r') {
264 int size = linebuf.size();
265 char *buffer = new char[size + 1];
266 linebuf.read(buffer, size);
267 buffer[size] = '\0';
268 DPRINTF(Console, "%s\n", buffer);
269 delete [] buffer;
270 } else {
271 linebuf.write(c);
272 }
273 }
274
275 last = c;
276 }
277 #endif
278
279 txbuf.write(c);
280
281 if (out_fd >= 0)
282 write(c);
283
284 if (outfile)
285 outfile->write(&c, 1);
286
287 if (raise_int)
288 raiseInt(TransmitInterrupt);
289
290 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x",
291 isprint(c) ? c : ' ', (int)c);
292
293 if (raise_int)
294 DPRINTF(ConsoleVerbose, "status: %#x\n", _status);
295 else
296 DPRINTF(ConsoleVerbose, "\n");
297 }
298
299 inline bool
300 MaskStatus(int status, int mask)
301 { return (status & mask) != 0; }
302
303 int
304 SimConsole::clearInt(int i)
305 {
306 int old = _status;
307 _status &= ~i;
308 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
309 intr->clear(TheISA::INTLEVEL_IRQ0);
310
311 return old;
312 }
313
314 void
315 SimConsole::raiseInt(int i)
316 {
317 int old = _status;
318 _status |= i;
319 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
320 intr->post(TheISA::INTLEVEL_IRQ0);
321 }
322
323 void
324 SimConsole::initInt(IntrControl *i)
325 {
326 if (intr)
327 panic("Console has already been initialized.");
328
329 intr = i;
330 }
331
332 void
333 SimConsole::setInt(int bits)
334 {
335 int old;
336
337 if (bits & ~(TransmitInterrupt | ReceiveInterrupt))
338 panic("An interrupt was not set!");
339
340 old = _enable;
341 _enable |= bits;
342
343 if (MaskStatus(_status, old) != MaskStatus(_status, _enable) && intr) {
344 if (MaskStatus(_status, _enable))
345 intr->post(TheISA::INTLEVEL_IRQ0);
346 else
347 intr->clear(TheISA::INTLEVEL_IRQ0);
348 }
349 }
350
351
352 void
353 SimConsole::serialize(ostream &os)
354 {
355 }
356
357 void
358 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
359 {
360 }
361
362
363 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
364
365 SimObjectParam<ConsoleListener *> listener;
366 SimObjectParam<IntrControl *> intr_control;
367 Param<string> output;
368 Param<bool> append_name;
369 Param<int> number;
370
371 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
372
373 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
374
375 INIT_PARAM(listener, "console listener"),
376 INIT_PARAM(intr_control, "interrupt controller"),
377 INIT_PARAM_DFLT(output, "file to dump output to", ""),
378 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
379 INIT_PARAM_DFLT(number, "console number", 0)
380
381 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
382
383 CREATE_SIM_OBJECT(SimConsole)
384 {
385 string filename = output;
386 if (!filename.empty() && append_name)
387 filename += "." + getInstanceName();
388 SimConsole *console = new SimConsole(getInstanceName(), filename, number);
389 ((ConsoleListener *)listener)->add(console);
390 ((SimConsole *)console)->initInt(intr_control);
391 ((SimConsole *)console)->setInt(SimConsole::TransmitInterrupt |
392 SimConsole::ReceiveInterrupt);
393
394 return console;
395 }
396
397 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
398
399 ////////////////////////////////////////////////////////////////////////
400 //
401 //
402
403 ConsoleListener::ConsoleListener(const string &name)
404 : SimObject(name), event(NULL)
405 {}
406
407 ConsoleListener::~ConsoleListener()
408 {
409 if (event)
410 delete event;
411 }
412
413 void
414 ConsoleListener::Event::process(int revent)
415 {
416 listener->accept();
417 }
418
419 ///////////////////////////////////////////////////////////////////////
420 // socket creation and console attach
421 //
422
423 void
424 ConsoleListener::listen(int port)
425 {
426 while (!listener.listen(port, true)) {
427 DPRINTF(Console,
428 ": can't bind address console port %d inuse PID %d\n",
429 port, getpid());
430 port++;
431 }
432
433 ccprintf(cerr, "Listening for console connection on port %d\n", port);
434
435 event = new Event(this, listener.getfd(), POLLIN);
436 pollQueue.schedule(event);
437 }
438
439 void
440 ConsoleListener::add(SimConsole *cons)
441 { ConsoleList.push_back(cons);}
442
443 void
444 ConsoleListener::accept()
445 {
446 if (!listener.islistening())
447 panic("%s: cannot accept a connection if not listening!", name());
448
449 int sfd = listener.accept(true);
450 if (sfd != -1) {
451 iter_t i = ConsoleList.begin();
452 iter_t end = ConsoleList.end();
453 if (i == end) {
454 close(sfd);
455 } else {
456 (*i)->attach(sfd, this);
457 i = ConsoleList.erase(i);
458 }
459 }
460 }
461
462 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
463
464 Param<int> port;
465
466 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
467
468 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
469
470 INIT_PARAM_DFLT(port, "listen port", 3456)
471
472 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
473
474 CREATE_SIM_OBJECT(ConsoleListener)
475 {
476 ConsoleListener *listener = new ConsoleListener(getInstanceName());
477 listener->listen(port);
478
479 return listener;
480 }
481
482 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)