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"
55 ////////////////////////////////////////////////////////////////////////
59 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
60 : PollEvent(fd
, e
), cons(c
)
65 SimConsole::Event::process(int revent
)
69 else if (revent
& POLLNVAL
)
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
),
79 _status(0), _enable(0), intr(NULL
)
82 outfile
= new ofstream(file
.c_str());
85 outfile
->setf(ios::unitbuf
);
88 SimConsole::~SimConsole()
102 if (out_fd
!= in_fd
&& out_fd
!= -1)
107 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
113 event
= new Event(this, in
, POLLIN
);
114 pollQueue
.schedule(event
);
117 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
119 // we need an actual carriage return followed by a newline for the
123 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
126 DPRINTFN("attach console %d\n", number
);
138 pollQueue
.remove(event
);
145 DPRINTFN("detach console %d\n", number
);
154 len
= read(buf
, sizeof(buf
));
156 rxbuf
.write((char *)buf
, len
);
157 raiseInt(ReceiveInterrupt
);
162 SimConsole::read(uint8_t *buf
, size_t len
)
165 panic("SimConsole(read): Console not properly attached.\n");
169 ret
= ::read(in_fd
, buf
, len
);
170 } while (ret
== -1 && errno
== EINTR
);
174 DPRINTFN("SimConsole(read): Read failed.\n");
186 SimConsole::write(const uint8_t *buf
, size_t len
)
189 panic("SimConsole(write): Console not properly attached.\n");
193 ret
= ::write(out_fd
, buf
, len
);
206 SimConsole::configTerm()
210 if (isatty(out_fd
)) {
211 if (tcgetattr(out_fd
, &ios
) < 0) {
212 panic( "tcgetattr\n");
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
);
220 if (tcsetattr(out_fd
, TCSANOW
, &ios
) < 0) {
221 panic( "tcsetattr\n");
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)
232 SimConsole::in(uint8_t &c
)
236 empty
= rxbuf
.empty();
239 rxbuf
.read((char *)&c
, 1);
240 empty
= rxbuf
.empty();
244 clearInt(ReceiveInterrupt
);
246 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
247 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
253 SimConsole::console_in()
259 value
= RECEIVE_SUCCESS
| c
;
261 value
|= MORE_PENDING
;
263 value
= RECEIVE_NONE
;
266 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
272 SimConsole::out(char c
, bool raise_int
)
275 if (DTRACE(Console
)) {
276 static char last
= '\0';
278 if (c
!= '\n' && c
!= '\r' ||
279 last
!= '\n' && last
!= '\r') {
280 if (c
== '\n' || c
== '\r') {
281 int size
= linebuf
.size();
282 char *buffer
= new char[size
+ 1];
283 linebuf
.read(buffer
, size
);
285 DPRINTF(Console
, "%s\n", buffer
);
302 outfile
->write(&c
, 1);
305 raiseInt(TransmitInterrupt
);
307 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x",
308 isprint(c
) ? c
: ' ', (int)c
);
311 DPRINTF(ConsoleVerbose
, "status: %#x\n", _status
);
313 DPRINTF(ConsoleVerbose
, "\n");
317 MaskStatus(int status
, int mask
)
318 { return (status
& mask
) != 0; }
321 SimConsole::clearInt(int i
)
325 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
326 intr
->clear(TheISA::INTLEVEL_IRQ0
);
332 SimConsole::raiseInt(int i
)
336 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
337 intr
->post(TheISA::INTLEVEL_IRQ0
);
341 SimConsole::initInt(IntrControl
*i
)
344 panic("Console has already been initialized.");
350 SimConsole::setInt(int bits
)
354 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
355 panic("An interrupt was not set!");
360 if (MaskStatus(_status
, old
) != MaskStatus(_status
, _enable
) && intr
) {
361 if (MaskStatus(_status
, _enable
))
362 intr
->post(TheISA::INTLEVEL_IRQ0
);
364 intr
->clear(TheISA::INTLEVEL_IRQ0
);
370 SimConsole::serialize(ostream
&os
)
375 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
380 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
382 SimObjectParam
<ConsoleListener
*> listener
;
383 SimObjectParam
<IntrControl
*> intr_control
;
384 Param
<string
> output
;
385 Param
<bool> append_name
;
388 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
390 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
392 INIT_PARAM(listener
, "console listener"),
393 INIT_PARAM(intr_control
, "interrupt controller"),
394 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
395 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
396 INIT_PARAM_DFLT(number
, "console number", 0)
398 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
400 CREATE_SIM_OBJECT(SimConsole
)
402 string filename
= output
;
403 if (filename
.empty()) {
404 if (!outputDirectory
.empty())
405 filename
= outputDirectory
+ getInstanceName();
408 filename
+= "." + getInstanceName();
409 if (!outputDirectory
.empty())
410 filename
= outputDirectory
+ filename
;
413 SimConsole
*console
= new SimConsole(getInstanceName(), filename
, number
);
414 ((ConsoleListener
*)listener
)->add(console
);
415 ((SimConsole
*)console
)->initInt(intr_control
);
416 ((SimConsole
*)console
)->setInt(SimConsole::TransmitInterrupt
|
417 SimConsole::ReceiveInterrupt
);
422 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
424 ////////////////////////////////////////////////////////////////////////
428 ConsoleListener::ConsoleListener(const string
&name
)
429 : SimObject(name
), event(NULL
)
432 ConsoleListener::~ConsoleListener()
439 ConsoleListener::Event::process(int revent
)
444 ///////////////////////////////////////////////////////////////////////
445 // socket creation and console attach
449 ConsoleListener::listen(int port
)
451 while (!listener
.listen(port
, true)) {
453 ": can't bind address console port %d inuse PID %d\n",
458 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
460 event
= new Event(this, listener
.getfd(), POLLIN
);
461 pollQueue
.schedule(event
);
465 ConsoleListener::add(SimConsole
*cons
)
466 { ConsoleList
.push_back(cons
);}
469 ConsoleListener::accept()
471 if (!listener
.islistening())
472 panic("%s: cannot accept a connection if not listening!", name());
474 int sfd
= listener
.accept(true);
476 iter_t i
= ConsoleList
.begin();
477 iter_t end
= ConsoleList
.end();
481 (*i
)->attach(sfd
, this);
482 i
= ConsoleList
.erase(i
);
487 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
491 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
493 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
495 INIT_PARAM_DFLT(port
, "listen port", 3456)
497 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
499 CREATE_SIM_OBJECT(ConsoleListener
)
501 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
502 listener
->listen(port
);
507 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)