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");
230 clearInt(ReceiveInterrupt
);
237 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x status: %#x\n",
238 isprint(c
) ? c
: ' ', c
, _status
);
244 SimConsole::out(char c
, bool raise_int
)
247 if (DTRACE(Console
)) {
248 static char last
= '\0';
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
);
257 DPRINTF(Console
, "%s\n", buffer
);
274 outfile
->write(&c
, 1);
277 raiseInt(TransmitInterrupt
);
279 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x",
280 isprint(c
) ? c
: ' ', (int)c
);
283 DPRINTF(ConsoleVerbose
, "status: %#x\n", _status
);
285 DPRINTF(ConsoleVerbose
, "\n");
289 MaskStatus(int status
, int mask
)
290 { return (status
& mask
) != 0; }
293 SimConsole::clearInt(int i
)
297 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
298 intr
->clear(TheISA::INTLEVEL_IRQ0
);
304 SimConsole::raiseInt(int i
)
308 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
309 intr
->post(TheISA::INTLEVEL_IRQ0
);
313 SimConsole::initInt(IntrControl
*i
)
316 panic("Console has already been initialized.");
322 SimConsole::setInt(int bits
)
326 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
327 panic("An interrupt was not set!");
332 if (MaskStatus(_status
, old
) != MaskStatus(_status
, _enable
) && intr
) {
333 if (MaskStatus(_status
, _enable
))
334 intr
->post(TheISA::INTLEVEL_IRQ0
);
336 intr
->clear(TheISA::INTLEVEL_IRQ0
);
342 SimConsole::serialize(ostream
&os
)
347 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
352 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
354 SimObjectParam
<ConsoleListener
*> listener
;
355 SimObjectParam
<IntrControl
*> intr_control
;
356 Param
<string
> output
;
357 Param
<bool> append_name
;
360 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
362 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
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)
370 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
372 CREATE_SIM_OBJECT(SimConsole
)
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
);
386 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
388 ////////////////////////////////////////////////////////////////////////
392 ConsoleListener::ConsoleListener(const string
&name
)
393 : SimObject(name
), event(NULL
)
396 ConsoleListener::~ConsoleListener()
403 ConsoleListener::Event::process(int revent
)
408 ///////////////////////////////////////////////////////////////////////
409 // socket creation and console attach
413 ConsoleListener::listen(int port
)
415 while (!listener
.listen(port
, true)) {
417 ": can't bind address console port %d inuse PID %d\n",
422 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
424 event
= new Event(this, listener
.getfd(), POLLIN
);
425 pollQueue
.schedule(event
);
429 ConsoleListener::add(SimConsole
*cons
)
430 { ConsoleList
.push_back(cons
);}
433 ConsoleListener::accept()
435 if (!listener
.islistening())
436 panic("%s: cannot accept a connection if not listening!", name());
438 int sfd
= listener
.accept(true);
440 iter_t i
= ConsoleList
.begin();
441 iter_t end
= ConsoleList
.end();
445 (*i
)->attach(sfd
, this);
446 i
= ConsoleList
.erase(i
);
451 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
455 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
457 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
459 INIT_PARAM_DFLT(port
, "listen port", 3456)
461 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
463 CREATE_SIM_OBJECT(ConsoleListener
)
465 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
466 listener
->listen(port
);
471 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)