Don't schedule tickEvent if it's already been scheduled.
[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 _status(0), _enable(0), intr(NULL)
77 {
78 if (!file.empty())
79 outfile = new ofstream(file.c_str());
80
81 if (outfile)
82 outfile->setf(ios::unitbuf);
83 }
84
85 SimConsole::~SimConsole()
86 {
87 close();
88
89 if (outfile)
90 delete 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, "==== Simplescalar slave console: Console %d ====",
115 number);
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 raiseInt(ReceiveInterrupt);
155 }
156 }
157
158 size_t
159 SimConsole::read(uint8_t *buf, size_t len)
160 {
161 if (in_fd < 0)
162 panic("SimConsole(read): Console not properly attached.\n");
163
164 size_t ret;
165 do {
166 ret = ::read(in_fd, buf, len);
167 } while (ret == -1 && errno == EINTR);
168
169
170 if (ret < 0)
171 DPRINTFN("SimConsole(read): Read failed.\n");
172
173 if (ret <= 0) {
174 detach();
175 return 0;
176 }
177
178 return ret;
179 }
180
181 // Console output.
182 size_t
183 SimConsole::write(const uint8_t *buf, size_t len)
184 {
185 if (out_fd < 0)
186 panic("SimConsole(write): Console not properly attached.\n");
187
188 size_t ret;
189 for (;;) {
190 ret = ::write(out_fd, buf, len);
191
192 if (ret >= 0)
193 break;
194
195 if (errno != EINTR)
196 detach();
197 }
198
199 return ret;
200 }
201
202 void
203 SimConsole::configTerm()
204 {
205 struct termios ios;
206
207 if (isatty(out_fd)) {
208 if (tcgetattr(out_fd, &ios) < 0) {
209 panic( "tcgetattr\n");
210 }
211 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
212 ios.c_oflag &= ~(OPOST);
213 ios.c_oflag &= (ONLCR);
214 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
215 ios.c_cc[VMIN] = 1;
216 ios.c_cc[VTIME] = 0;
217 if (tcsetattr(out_fd, TCSANOW, &ios) < 0) {
218 panic( "tcsetattr\n");
219 }
220 }
221 }
222
223 int
224 SimConsole::in()
225 {
226 if (rxbuf.empty()) {
227 clearInt(ReceiveInterrupt);
228 return -1;
229 }
230
231 char c;
232 rxbuf.read(&c, 1);
233
234 DPRINTF(Console, "in: \'%c\' %#02x status: %#x\n",
235 isprint(c) ? c : ' ', c, _status);
236
237 return c;
238 }
239
240 void
241 SimConsole::out(char c, bool raise_int)
242 {
243 txbuf.write(c);
244
245 if (out_fd >= 0)
246 write(c);
247
248 if (outfile)
249 outfile->write(&c, 1);
250
251 if (raise_int)
252 raiseInt(TransmitInterrupt);
253
254 DPRINTF(Console, "out: \'%c\' %#02x",
255 isprint(c) ? c : ' ', (int)c);
256
257 if (raise_int)
258 DPRINTF(Console, "status: %#x\n", _status);
259 else
260 DPRINTF(Console, "\n");
261 }
262
263 inline bool
264 MaskStatus(int status, int mask)
265 { return (status & mask) != 0; }
266
267 int
268 SimConsole::clearInt(int i)
269 {
270 int old = _status;
271 _status &= ~i;
272 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
273 intr->clear(TheISA::INTLEVEL_IRQ0);
274
275 return old;
276 }
277
278 void
279 SimConsole::raiseInt(int i)
280 {
281 int old = _status;
282 _status |= i;
283 if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
284 intr->post(TheISA::INTLEVEL_IRQ0);
285 }
286
287 void
288 SimConsole::initInt(IntrControl *i)
289 {
290 if (intr)
291 panic("Console has already been initialized.");
292
293 intr = i;
294 }
295
296 void
297 SimConsole::setInt(int bits)
298 {
299 int old;
300
301 if (bits & ~(TransmitInterrupt | ReceiveInterrupt))
302 panic("An interrupt was not set!");
303
304 old = _enable;
305 _enable |= bits;
306
307 if (MaskStatus(_status, old) != MaskStatus(_status, _enable) && intr) {
308 if (MaskStatus(_status, _enable))
309 intr->post(TheISA::INTLEVEL_IRQ0);
310 else
311 intr->clear(TheISA::INTLEVEL_IRQ0);
312 }
313 }
314
315
316 void
317 SimConsole::serialize()
318 {
319 panic("Unimplemented");
320 }
321
322 void
323 SimConsole::unserialize(IniFile &db, const std::string &category,
324 ConfigNode *node)
325 {
326 panic("Unimplemented");
327 }
328
329
330 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
331
332 SimObjectParam<ConsoleListener *> listener;
333 SimObjectParam<IntrControl *> intr_control;
334 Param<string> output;
335 Param<int> number;
336
337 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
338
339 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
340
341 INIT_PARAM(listener, "console listener"),
342 INIT_PARAM(intr_control, "interrupt controller"),
343 INIT_PARAM_DFLT(output, "file to dump output to", ""),
344 INIT_PARAM_DFLT(number, "console number", 0)
345
346 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
347
348 CREATE_SIM_OBJECT(SimConsole)
349 {
350 SimConsole *console = new SimConsole(getInstanceName(), output, number);
351 ((ConsoleListener *)listener)->add(console);
352 ((SimConsole *)console)->initInt(intr_control);
353 ((SimConsole *)console)->setInt(SimConsole::TransmitInterrupt |
354 SimConsole::ReceiveInterrupt);
355
356 return console;
357 }
358
359 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
360
361 ////////////////////////////////////////////////////////////////////////
362 //
363 //
364
365 ConsoleListener::ConsoleListener(const string &name)
366 : SimObject(name), event(NULL)
367 {}
368
369 ConsoleListener::~ConsoleListener()
370 {
371 if (event)
372 delete event;
373 }
374
375 void
376 ConsoleListener::Event::process(int revent)
377 {
378 listener->accept();
379 }
380
381 ///////////////////////////////////////////////////////////////////////
382 // socket creation and console attach
383 //
384
385 void
386 ConsoleListener::listen(int port)
387 {
388 while (!listener.listen(port, true)) {
389 DPRINTF(Console, ": can't bind address console port %d inuse PID %d\n",
390 port, getpid());
391 port++;
392 }
393
394 cerr << "Listening for console connection on port " << port << endl;
395 event = new Event(this, listener.getfd(), POLLIN);
396 pollQueue.schedule(event);
397 }
398
399 void
400 ConsoleListener::add(SimConsole *cons)
401 { ConsoleList.push_back(cons);}
402
403 void
404 ConsoleListener::accept()
405 {
406 if (!listener.islistening())
407 panic("%s: cannot accept a connection if we're not listening!",
408 name());
409
410 int sfd = listener.accept(true);
411 if (sfd != -1) {
412 iter_t i = ConsoleList.begin();
413 iter_t end = ConsoleList.end();
414 if (i == end) {
415 close(sfd);
416 } else {
417 (*i)->attach(sfd, this);
418 i = ConsoleList.erase(i);
419 }
420 }
421 }
422
423 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
424
425 Param<int> port;
426
427 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
428
429 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
430
431 INIT_PARAM_DFLT(port, "listen port", 3456)
432
433 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
434
435 CREATE_SIM_OBJECT(ConsoleListener)
436 {
437 ConsoleListener *listener = new ConsoleListener(getInstanceName());
438 listener->listen(port);
439
440 return listener;
441 }
442
443 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)