change the path that i track from two separate paths to one.
[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 int
227 SimConsole::in()
228 {
229 if (rxbuf.empty()) {
230 clearInt(ReceiveInterrupt);
231 return -1;
232 }
233
234 char c;
235 rxbuf.read(&c, 1);
236
237 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x status: %#x\n",
238 isprint(c) ? c : ' ', c, _status);
239
240 return c;
241 }
242
243 void
244 SimConsole::out(char c, bool raise_int)
245 {
246 #if TRACING_ON == 1
247 if (DTRACE(Console)) {
248 static char last = '\0';
249
250 if (c != '\n' && c != '\r' ||
251 last != '\n' && last != '\r') {
252 if (c == '\n' || c == '\r') {
253 int size = linebuf.size();
254 char *buffer = new char[size + 1];
255 linebuf.read(buffer, size);
256 buffer[size] = '\0';
257 DPRINTF(Console, "%s\n", buffer);
258 delete [] buffer;
259 } else {
260 linebuf.write(c);
261 }
262 }
263
264 last = c;
265 }
266 #endif
267
268 txbuf.write(c);
269
270 if (out_fd >= 0)
271 write(c);
272
273 if (outfile)
274 outfile->write(&c, 1);
275
276 if (raise_int)
277 raiseInt(TransmitInterrupt);
278
279 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x",
280 isprint(c) ? c : ' ', (int)c);
281
282 if (raise_int)
283 DPRINTF(ConsoleVerbose, "status: %#x\n", _status);
284 else
285 DPRINTF(ConsoleVerbose, "\n");
286 }
287
288 inline bool
289 MaskStatus(int status, int mask)
290 { return (status & mask) != 0; }
291
292 int
293 SimConsole::clearInt(int i)
294 {
295 int old = _status;
296 _status &= ~i;
297 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
298 intr->clear(TheISA::INTLEVEL_IRQ0);
299
300 return old;
301 }
302
303 void
304 SimConsole::raiseInt(int i)
305 {
306 int old = _status;
307 _status |= i;
308 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
309 intr->post(TheISA::INTLEVEL_IRQ0);
310 }
311
312 void
313 SimConsole::initInt(IntrControl *i)
314 {
315 if (intr)
316 panic("Console has already been initialized.");
317
318 intr = i;
319 }
320
321 void
322 SimConsole::setInt(int bits)
323 {
324 int old;
325
326 if (bits & ~(TransmitInterrupt | ReceiveInterrupt))
327 panic("An interrupt was not set!");
328
329 old = _enable;
330 _enable |= bits;
331
332 if (MaskStatus(_status, old) != MaskStatus(_status, _enable) && intr) {
333 if (MaskStatus(_status, _enable))
334 intr->post(TheISA::INTLEVEL_IRQ0);
335 else
336 intr->clear(TheISA::INTLEVEL_IRQ0);
337 }
338 }
339
340
341 void
342 SimConsole::serialize(ostream &os)
343 {
344 }
345
346 void
347 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
348 {
349 }
350
351
352 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
353
354 SimObjectParam<ConsoleListener *> listener;
355 SimObjectParam<IntrControl *> intr_control;
356 Param<string> output;
357 Param<bool> append_name;
358 Param<int> number;
359
360 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
361
362 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
363
364 INIT_PARAM(listener, "console listener"),
365 INIT_PARAM(intr_control, "interrupt controller"),
366 INIT_PARAM_DFLT(output, "file to dump output to", ""),
367 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
368 INIT_PARAM_DFLT(number, "console number", 0)
369
370 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
371
372 CREATE_SIM_OBJECT(SimConsole)
373 {
374 string filename = output;
375 if (!filename.empty() && append_name)
376 filename += "." + getInstanceName();
377 SimConsole *console = new SimConsole(getInstanceName(), filename, number);
378 ((ConsoleListener *)listener)->add(console);
379 ((SimConsole *)console)->initInt(intr_control);
380 ((SimConsole *)console)->setInt(SimConsole::TransmitInterrupt |
381 SimConsole::ReceiveInterrupt);
382
383 return console;
384 }
385
386 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
387
388 ////////////////////////////////////////////////////////////////////////
389 //
390 //
391
392 ConsoleListener::ConsoleListener(const string &name)
393 : SimObject(name), event(NULL)
394 {}
395
396 ConsoleListener::~ConsoleListener()
397 {
398 if (event)
399 delete event;
400 }
401
402 void
403 ConsoleListener::Event::process(int revent)
404 {
405 listener->accept();
406 }
407
408 ///////////////////////////////////////////////////////////////////////
409 // socket creation and console attach
410 //
411
412 void
413 ConsoleListener::listen(int port)
414 {
415 while (!listener.listen(port, true)) {
416 DPRINTF(Console,
417 ": can't bind address console port %d inuse PID %d\n",
418 port, getpid());
419 port++;
420 }
421
422 ccprintf(cerr, "Listening for console connection on port %d\n", port);
423
424 event = new Event(this, listener.getfd(), POLLIN);
425 pollQueue.schedule(event);
426 }
427
428 void
429 ConsoleListener::add(SimConsole *cons)
430 { ConsoleList.push_back(cons);}
431
432 void
433 ConsoleListener::accept()
434 {
435 if (!listener.islistening())
436 panic("%s: cannot accept a connection if not listening!", name());
437
438 int sfd = listener.accept(true);
439 if (sfd != -1) {
440 iter_t i = ConsoleList.begin();
441 iter_t end = ConsoleList.end();
442 if (i == end) {
443 close(sfd);
444 } else {
445 (*i)->attach(sfd, this);
446 i = ConsoleList.erase(i);
447 }
448 }
449 }
450
451 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
452
453 Param<int> port;
454
455 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
456
457 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
458
459 INIT_PARAM_DFLT(port, "listen port", 3456)
460
461 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
462
463 CREATE_SIM_OBJECT(ConsoleListener)
464 {
465 ConsoleListener *listener = new ConsoleListener(getInstanceName());
466 listener->listen(port);
467
468 return listener;
469 }
470
471 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)