2 * Copyright (c) 2003 The Regents of The University of Michigan
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.
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.
30 * User Console Definitions
33 #include <sys/ioctl.h>
34 #include <sys/termios.h>
35 #include <sys/types.h>
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 #include "dev/platform.hh"
56 ////////////////////////////////////////////////////////////////////////
60 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
61 : PollEvent(fd
, e
), cons(c
)
66 SimConsole::Event::process(int revent
)
70 else if (revent
& POLLNVAL
)
74 SimConsole::SimConsole(const string
&name
, const string
&file
, int num
)
75 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
76 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(NULL
),
80 _status(0), _enable(0), intr(NULL
), platform(NULL
)
83 outfile
= new ofstream(file
.c_str());
86 outfile
->setf(ios::unitbuf
);
89 SimConsole::~SimConsole()
103 if (out_fd
!= in_fd
&& out_fd
!= -1)
108 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
114 event
= new Event(this, in
, POLLIN
);
115 pollQueue
.schedule(event
);
118 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
120 // we need an actual carriage return followed by a newline for the
124 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
127 DPRINTFN("attach console %d\n", number
);
139 pollQueue
.remove(event
);
146 DPRINTFN("detach console %d\n", number
);
155 len
= read(buf
, sizeof(buf
));
157 rxbuf
.write((char *)buf
, len
);
158 raiseInt(ReceiveInterrupt
);
163 SimConsole::read(uint8_t *buf
, size_t len
)
166 panic("SimConsole(read): Console not properly attached.\n");
170 ret
= ::read(in_fd
, buf
, len
);
171 } while (ret
== -1 && errno
== EINTR
);
175 DPRINTFN("SimConsole(read): Read failed.\n");
187 SimConsole::write(const uint8_t *buf
, size_t len
)
190 panic("SimConsole(write): Console not properly attached.\n");
194 ret
= ::write(out_fd
, buf
, len
);
207 SimConsole::configTerm()
211 if (isatty(out_fd
)) {
212 if (tcgetattr(out_fd
, &ios
) < 0) {
213 panic( "tcgetattr\n");
215 ios
.c_iflag
&= ~(ISTRIP
|ICRNL
|IGNCR
|ICRNL
|IXOFF
|IXON
);
216 ios
.c_oflag
&= ~(OPOST
);
217 ios
.c_oflag
&= (ONLCR
);
218 ios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
221 if (tcsetattr(out_fd
, TCSANOW
, &ios
) < 0) {
222 panic( "tcsetattr\n");
227 #define MORE_PENDING (ULL(1) << 61)
228 #define RECEIVE_SUCCESS (ULL(0) << 62)
229 #define RECEIVE_NONE (ULL(2) << 62)
230 #define RECEIVE_ERROR (ULL(3) << 62)
233 SimConsole::in(uint8_t &c
)
237 empty
= rxbuf
.empty();
240 rxbuf
.read((char *)&c
, 1);
241 empty
= rxbuf
.empty();
245 clearInt(ReceiveInterrupt
);
247 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
248 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
254 SimConsole::console_in()
260 value
= RECEIVE_SUCCESS
| c
;
262 value
|= MORE_PENDING
;
264 value
= RECEIVE_NONE
;
267 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
273 SimConsole::out(char c
, bool raise_int
)
276 if (DTRACE(Console
)) {
277 static char last
= '\0';
279 if (c
!= '\n' && c
!= '\r' ||
280 last
!= '\n' && last
!= '\r') {
281 if (c
== '\n' || c
== '\r') {
282 int size
= linebuf
.size();
283 char *buffer
= new char[size
+ 1];
284 linebuf
.read(buffer
, size
);
286 DPRINTF(Console
, "%s\n", buffer
);
303 outfile
->write(&c
, 1);
306 raiseInt(TransmitInterrupt
);
308 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x",
309 isprint(c
) ? c
: ' ', (int)c
);
312 DPRINTF(ConsoleVerbose
, "status: %#x\n", _status
);
314 DPRINTF(ConsoleVerbose
, "\n");
318 MaskStatus(int status
, int mask
)
319 { return (status
& mask
) != 0; }
322 SimConsole::clearInt(int i
)
326 //if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
327 platform
->clearConsoleInt();
333 SimConsole::raiseInt(int i
)
337 //if (MaskStatus(old, _enable) != MaskStatus(_status, _enable) && intr)
338 platform
->postConsoleInt();
342 SimConsole::initInt(IntrControl
*i
)
345 panic("Console has already been initialized.");
351 SimConsole::setInt(int bits
)
355 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
356 panic("An interrupt was not set!");
361 //if (MaskStatus(_status, old) != MaskStatus(_status, _enable) && intr) {
363 if (MaskStatus(_status
, _enable
))
364 platform
->postConsoleInt();
366 platform
->clearConsoleInt();
371 SimConsole::setPlatform(Platform
*p
)
374 platform
->cons
= this;
378 SimConsole::serialize(ostream
&os
)
383 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
388 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
390 SimObjectParam
<ConsoleListener
*> listener
;
391 SimObjectParam
<IntrControl
*> intr_control
;
392 SimObjectParam
<Platform
*> platform
;
393 Param
<string
> output
;
394 Param
<bool> append_name
;
397 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
399 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
401 INIT_PARAM(listener
, "console listener"),
402 INIT_PARAM(intr_control
, "interrupt controller"),
403 INIT_PARAM(platform
, "platform"),
404 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
405 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
406 INIT_PARAM_DFLT(number
, "console number", 0)
408 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
410 CREATE_SIM_OBJECT(SimConsole
)
412 string filename
= output
;
413 if (filename
.empty()) {
414 if (!outputDirectory
.empty())
415 filename
= outputDirectory
+ getInstanceName();
418 filename
+= "." + getInstanceName();
419 if (!outputDirectory
.empty())
420 filename
= outputDirectory
+ filename
;
423 SimConsole
*console
= new SimConsole(getInstanceName(), filename
, number
);
424 ((ConsoleListener
*)listener
)->add(console
);
425 ((SimConsole
*)console
)->initInt(intr_control
);
426 ((SimConsole
*)console
)->setPlatform(platform
);
427 //((SimConsole *)console)->setInt(SimConsole::TransmitInterrupt |
428 // SimConsole::ReceiveInterrupt);
433 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
435 ////////////////////////////////////////////////////////////////////////
439 ConsoleListener::ConsoleListener(const string
&name
)
440 : SimObject(name
), event(NULL
)
443 ConsoleListener::~ConsoleListener()
450 ConsoleListener::Event::process(int revent
)
455 ///////////////////////////////////////////////////////////////////////
456 // socket creation and console attach
460 ConsoleListener::listen(int port
)
462 while (!listener
.listen(port
, true)) {
464 ": can't bind address console port %d inuse PID %d\n",
469 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
471 event
= new Event(this, listener
.getfd(), POLLIN
);
472 pollQueue
.schedule(event
);
476 ConsoleListener::add(SimConsole
*cons
)
477 { ConsoleList
.push_back(cons
);}
480 ConsoleListener::accept()
482 if (!listener
.islistening())
483 panic("%s: cannot accept a connection if not listening!", name());
485 int sfd
= listener
.accept(true);
487 iter_t i
= ConsoleList
.begin();
488 iter_t end
= ConsoleList
.end();
492 (*i
)->attach(sfd
, this);
493 i
= ConsoleList
.erase(i
);
498 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
502 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
504 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
506 INIT_PARAM_DFLT(port
, "listen port", 3456)
508 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
510 CREATE_SIM_OBJECT(ConsoleListener
)
512 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
513 listener
->listen(port
);
518 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)