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)
237 clearInt(ReceiveInterrupt
);
243 val
|= RECEIVE_SUCCESS
| c
;
248 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x retval: %#x\n",
249 isprint(c
) ? c
: ' ', c
, val
);
255 SimConsole::out(char c
, bool raise_int
)
258 if (DTRACE(Console
)) {
259 static char last
= '\0';
261 if (c
!= '\n' && c
!= '\r' ||
262 last
!= '\n' && last
!= '\r') {
263 if (c
== '\n' || c
== '\r') {
264 int size
= linebuf
.size();
265 char *buffer
= new char[size
+ 1];
266 linebuf
.read(buffer
, size
);
268 DPRINTF(Console
, "%s\n", buffer
);
285 outfile
->write(&c
, 1);
288 raiseInt(TransmitInterrupt
);
290 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x",
291 isprint(c
) ? c
: ' ', (int)c
);
294 DPRINTF(ConsoleVerbose
, "status: %#x\n", _status
);
296 DPRINTF(ConsoleVerbose
, "\n");
300 MaskStatus(int status
, int mask
)
301 { return (status
& mask
) != 0; }
304 SimConsole::clearInt(int i
)
308 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
309 intr
->clear(TheISA::INTLEVEL_IRQ0
);
315 SimConsole::raiseInt(int i
)
319 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
320 intr
->post(TheISA::INTLEVEL_IRQ0
);
324 SimConsole::initInt(IntrControl
*i
)
327 panic("Console has already been initialized.");
333 SimConsole::setInt(int bits
)
337 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
338 panic("An interrupt was not set!");
343 if (MaskStatus(_status
, old
) != MaskStatus(_status
, _enable
) && intr
) {
344 if (MaskStatus(_status
, _enable
))
345 intr
->post(TheISA::INTLEVEL_IRQ0
);
347 intr
->clear(TheISA::INTLEVEL_IRQ0
);
353 SimConsole::serialize(ostream
&os
)
358 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
363 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
365 SimObjectParam
<ConsoleListener
*> listener
;
366 SimObjectParam
<IntrControl
*> intr_control
;
367 Param
<string
> output
;
368 Param
<bool> append_name
;
371 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
373 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
375 INIT_PARAM(listener
, "console listener"),
376 INIT_PARAM(intr_control
, "interrupt controller"),
377 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
378 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
379 INIT_PARAM_DFLT(number
, "console number", 0)
381 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
383 CREATE_SIM_OBJECT(SimConsole
)
385 string filename
= output
;
386 if (!filename
.empty() && append_name
)
387 filename
+= "." + getInstanceName();
388 SimConsole
*console
= new SimConsole(getInstanceName(), filename
, number
);
389 ((ConsoleListener
*)listener
)->add(console
);
390 ((SimConsole
*)console
)->initInt(intr_control
);
391 ((SimConsole
*)console
)->setInt(SimConsole::TransmitInterrupt
|
392 SimConsole::ReceiveInterrupt
);
397 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
399 ////////////////////////////////////////////////////////////////////////
403 ConsoleListener::ConsoleListener(const string
&name
)
404 : SimObject(name
), event(NULL
)
407 ConsoleListener::~ConsoleListener()
414 ConsoleListener::Event::process(int revent
)
419 ///////////////////////////////////////////////////////////////////////
420 // socket creation and console attach
424 ConsoleListener::listen(int port
)
426 while (!listener
.listen(port
, true)) {
428 ": can't bind address console port %d inuse PID %d\n",
433 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
435 event
= new Event(this, listener
.getfd(), POLLIN
);
436 pollQueue
.schedule(event
);
440 ConsoleListener::add(SimConsole
*cons
)
441 { ConsoleList
.push_back(cons
);}
444 ConsoleListener::accept()
446 if (!listener
.islistening())
447 panic("%s: cannot accept a connection if not listening!", name());
449 int sfd
= listener
.accept(true);
451 iter_t i
= ConsoleList
.begin();
452 iter_t end
= ConsoleList
.end();
456 (*i
)->attach(sfd
, this);
457 i
= ConsoleList
.erase(i
);
462 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
466 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
468 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
470 INIT_PARAM_DFLT(port
, "listen port", 3456)
472 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
474 CREATE_SIM_OBJECT(ConsoleListener
)
476 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
477 listener
->listen(port
);
482 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)