5 * User Console Definitions
9 #include <sys/termios.h>
10 #include <sys/types.h>
20 #include "base/misc.hh"
21 #include "base/socket.hh"
22 #include "base/trace.hh"
23 #include "dev/console.hh"
24 #include "mem/functional_mem/memory_control.hh"
25 #include "sim/builder.hh"
26 #include "targetarch/ev5.hh"
27 #include "dev/platform.hh"
31 ////////////////////////////////////////////////////////////////////////
35 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
36 : PollEvent(fd
, e
), cons(c
)
41 SimConsole::Event::process(int revent
)
45 else if (revent
& POLLNVAL
)
49 SimConsole::SimConsole(const string
&name
, const string
&file
, int num
)
50 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
51 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(NULL
),
55 _status(0), _enable(0), intr(NULL
), platform(NULL
)
58 outfile
= new ofstream(file
.c_str());
61 outfile
->setf(ios::unitbuf
);
64 SimConsole::~SimConsole()
78 if (out_fd
!= in_fd
&& out_fd
!= -1)
83 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
89 event
= new Event(this, in
, POLLIN
);
90 pollQueue
.schedule(event
);
93 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
95 // we need an actual carriage return followed by a newline for the
99 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
102 DPRINTFN("attach console %d\n", number
);
114 pollQueue
.remove(event
);
121 DPRINTFN("detach console %d\n", number
);
130 len
= read(buf
, sizeof(buf
));
132 rxbuf
.write((char *)buf
, len
);
133 raiseInt(ReceiveInterrupt
);
138 SimConsole::read(uint8_t *buf
, size_t len
)
141 panic("SimConsole(read): Console not properly attached.\n");
145 ret
= ::read(in_fd
, buf
, len
);
146 } while (ret
== -1 && errno
== EINTR
);
150 DPRINTFN("SimConsole(read): Read failed.\n");
162 SimConsole::write(const uint8_t *buf
, size_t len
)
165 panic("SimConsole(write): Console not properly attached.\n");
169 ret
= ::write(out_fd
, buf
, len
);
182 SimConsole::configTerm()
186 if (isatty(out_fd
)) {
187 if (tcgetattr(out_fd
, &ios
) < 0) {
188 panic( "tcgetattr\n");
190 ios
.c_iflag
&= ~(ISTRIP
|ICRNL
|IGNCR
|ICRNL
|IXOFF
|IXON
);
191 ios
.c_oflag
&= ~(OPOST
);
192 ios
.c_oflag
&= (ONLCR
);
193 ios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
196 if (tcsetattr(out_fd
, TCSANOW
, &ios
) < 0) {
197 panic( "tcsetattr\n");
202 #define MORE_PENDING (ULL(1) << 61)
203 #define RECEIVE_SUCCESS (ULL(0) << 62)
204 #define RECEIVE_NONE (ULL(2) << 62)
205 #define RECEIVE_ERROR (ULL(3) << 62)
208 SimConsole::in(uint8_t &c
)
212 empty
= rxbuf
.empty();
215 rxbuf
.read((char *)&c
, 1);
216 empty
= rxbuf
.empty();
220 clearInt(ReceiveInterrupt
);
222 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
223 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
229 SimConsole::console_in()
235 value
= RECEIVE_SUCCESS
| c
;
237 value
|= MORE_PENDING
;
239 value
= RECEIVE_NONE
;
242 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
248 SimConsole::out(char c
, bool raise_int
)
251 if (DTRACE(Console
)) {
252 static char last
= '\0';
254 if (c
!= '\n' && c
!= '\r' ||
255 last
!= '\n' && last
!= '\r') {
256 if (c
== '\n' || c
== '\r') {
257 int size
= linebuf
.size();
258 char *buffer
= new char[size
+ 1];
259 linebuf
.read(buffer
, size
);
261 DPRINTF(Console
, "%s\n", buffer
);
278 outfile
->write(&c
, 1);
281 raiseInt(TransmitInterrupt
);
283 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x",
284 isprint(c
) ? c
: ' ', (int)c
);
287 DPRINTF(ConsoleVerbose
, "status: %#x\n", _status
);
289 DPRINTF(ConsoleVerbose
, "\n");
293 MaskStatus(int status
, int mask
)
294 { return (status
& mask
) != 0; }
297 SimConsole::clearInt(int i
)
301 //if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
302 platform
->clearConsoleInt();
308 SimConsole::raiseInt(int i
)
312 //if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
313 platform
->postConsoleInt();
317 SimConsole::initInt(IntrControl
*i
)
320 panic("Console has already been initialized.");
326 SimConsole::setInt(int bits
)
330 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
331 panic("An interrupt was not set!");
336 //if (MaskStatus(_status, old) != MaskStatus(_status, _enable) && intr) {
338 if (MaskStatus(_status
, _enable
))
339 platform
->postConsoleInt();
341 platform
->clearConsoleInt();
346 SimConsole::setPlatform(Platform
*p
)
349 platform
->cons
= this;
353 SimConsole::serialize(ostream
&os
)
355 SERIALIZE_SCALAR(_status
);
356 SERIALIZE_SCALAR(_enable
);
360 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
362 UNSERIALIZE_SCALAR(_status
);
363 UNSERIALIZE_SCALAR(_enable
);
367 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
369 SimObjectParam
<ConsoleListener
*> listener
;
370 SimObjectParam
<IntrControl
*> intr_control
;
371 SimObjectParam
<Platform
*> platform
;
372 Param
<string
> output
;
373 Param
<bool> append_name
;
376 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
378 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
380 INIT_PARAM(listener
, "console listener"),
381 INIT_PARAM(intr_control
, "interrupt controller"),
382 INIT_PARAM(platform
, "platform"),
383 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
384 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
385 INIT_PARAM_DFLT(number
, "console number", 0)
387 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
389 CREATE_SIM_OBJECT(SimConsole
)
391 string filename
= output
;
392 if (filename
.empty()) {
393 if (!outputDirectory
.empty())
394 filename
= outputDirectory
+ getInstanceName();
397 filename
+= "." + getInstanceName();
398 if (!outputDirectory
.empty())
399 filename
= outputDirectory
+ filename
;
402 SimConsole
*console
= new SimConsole(getInstanceName(), filename
, number
);
403 ((ConsoleListener
*)listener
)->add(console
);
404 ((SimConsole
*)console
)->initInt(intr_control
);
405 ((SimConsole
*)console
)->setPlatform(platform
);
406 //((SimConsole *)console)->setInt(SimConsole::TransmitInterrupt |
407 // SimConsole::ReceiveInterrupt);
412 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
414 ////////////////////////////////////////////////////////////////////////
418 ConsoleListener::ConsoleListener(const string
&name
)
419 : SimObject(name
), event(NULL
)
422 ConsoleListener::~ConsoleListener()
429 ConsoleListener::Event::process(int revent
)
434 ///////////////////////////////////////////////////////////////////////
435 // socket creation and console attach
439 ConsoleListener::listen(int port
)
441 while (!listener
.listen(port
, true)) {
443 ": can't bind address console port %d inuse PID %d\n",
448 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
450 event
= new Event(this, listener
.getfd(), POLLIN
);
451 pollQueue
.schedule(event
);
455 ConsoleListener::add(SimConsole
*cons
)
456 { ConsoleList
.push_back(cons
);}
459 ConsoleListener::accept()
461 if (!listener
.islistening())
462 panic("%s: cannot accept a connection if not listening!", name());
464 int sfd
= listener
.accept(true);
466 iter_t i
= ConsoleList
.begin();
467 iter_t end
= ConsoleList
.end();
471 (*i
)->attach(sfd
, this);
472 i
= ConsoleList
.erase(i
);
477 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
481 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
483 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
485 INIT_PARAM_DFLT(port
, "listen port", 3456)
487 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
489 CREATE_SIM_OBJECT(ConsoleListener
)
491 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
492 listener
->listen(port
);
497 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)