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
),
76 _status(0), _enable(0), intr(NULL
)
79 outfile
= new ofstream(file
.c_str());
82 outfile
->setf(ios::unitbuf
);
85 SimConsole::~SimConsole()
99 if (out_fd
!= in_fd
&& out_fd
!= -1)
104 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
110 event
= new Event(this, in
, POLLIN
);
111 pollQueue
.schedule(event
);
114 ccprintf(stream
, "==== Simplescalar slave console: Console %d ====",
116 // we need an actual carriage return followed by a newline for the
120 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
123 DPRINTFN("attach console %d\n", number
);
135 pollQueue
.remove(event
);
142 DPRINTFN("detach console %d\n", number
);
151 len
= read(buf
, sizeof(buf
));
153 rxbuf
.write((char *)buf
, len
);
154 raiseInt(ReceiveInterrupt
);
159 SimConsole::read(uint8_t *buf
, size_t len
)
162 panic("SimConsole(read): Console not properly attached.\n");
166 ret
= ::read(in_fd
, buf
, len
);
167 } while (ret
== -1 && errno
== EINTR
);
171 DPRINTFN("SimConsole(read): Read failed.\n");
183 SimConsole::write(const uint8_t *buf
, size_t len
)
186 panic("SimConsole(write): Console not properly attached.\n");
190 ret
= ::write(out_fd
, buf
, len
);
203 SimConsole::configTerm()
207 if (isatty(out_fd
)) {
208 if (tcgetattr(out_fd
, &ios
) < 0) {
209 panic( "tcgetattr\n");
211 ios
.c_iflag
&= ~(ISTRIP
|ICRNL
|IGNCR
|ICRNL
|IXOFF
|IXON
);
212 ios
.c_oflag
&= ~(OPOST
);
213 ios
.c_oflag
&= (ONLCR
);
214 ios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
217 if (tcsetattr(out_fd
, TCSANOW
, &ios
) < 0) {
218 panic( "tcsetattr\n");
227 clearInt(ReceiveInterrupt
);
234 DPRINTF(Console
, "in: \'%c\' %#02x status: %#x\n",
235 isprint(c
) ? c
: ' ', c
, _status
);
241 SimConsole::out(char c
, bool raise_int
)
249 outfile
->write(&c
, 1);
252 raiseInt(TransmitInterrupt
);
254 DPRINTF(Console
, "out: \'%c\' %#02x",
255 isprint(c
) ? c
: ' ', (int)c
);
258 DPRINTF(Console
, "status: %#x\n", _status
);
260 DPRINTF(Console
, "\n");
264 MaskStatus(int status
, int mask
)
265 { return (status
& mask
) != 0; }
268 SimConsole::clearInt(int i
)
272 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
273 intr
->clear(TheISA::INTLEVEL_IRQ0
);
279 SimConsole::raiseInt(int i
)
283 if (MaskStatus(old
, _enable
) != MaskStatus(_status
, _enable
) && intr
)
284 intr
->post(TheISA::INTLEVEL_IRQ0
);
288 SimConsole::initInt(IntrControl
*i
)
291 panic("Console has already been initialized.");
297 SimConsole::setInt(int bits
)
301 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
302 panic("An interrupt was not set!");
307 if (MaskStatus(_status
, old
) != MaskStatus(_status
, _enable
) && intr
) {
308 if (MaskStatus(_status
, _enable
))
309 intr
->post(TheISA::INTLEVEL_IRQ0
);
311 intr
->clear(TheISA::INTLEVEL_IRQ0
);
317 SimConsole::serialize()
319 panic("Unimplemented");
323 SimConsole::unserialize(IniFile
&db
, const std::string
&category
,
326 panic("Unimplemented");
330 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
332 SimObjectParam
<ConsoleListener
*> listener
;
333 SimObjectParam
<IntrControl
*> intr_control
;
334 Param
<string
> output
;
337 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
339 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
341 INIT_PARAM(listener
, "console listener"),
342 INIT_PARAM(intr_control
, "interrupt controller"),
343 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
344 INIT_PARAM_DFLT(number
, "console number", 0)
346 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
348 CREATE_SIM_OBJECT(SimConsole
)
350 SimConsole
*console
= new SimConsole(getInstanceName(), output
, number
);
351 ((ConsoleListener
*)listener
)->add(console
);
352 ((SimConsole
*)console
)->initInt(intr_control
);
353 ((SimConsole
*)console
)->setInt(SimConsole::TransmitInterrupt
|
354 SimConsole::ReceiveInterrupt
);
359 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
361 ////////////////////////////////////////////////////////////////////////
365 ConsoleListener::ConsoleListener(const string
&name
)
366 : SimObject(name
), event(NULL
)
369 ConsoleListener::~ConsoleListener()
376 ConsoleListener::Event::process(int revent
)
381 ///////////////////////////////////////////////////////////////////////
382 // socket creation and console attach
386 ConsoleListener::listen(int port
)
388 while (!listener
.listen(port
, true)) {
389 DPRINTF(Console
, ": can't bind address console port %d inuse PID %d\n",
394 cerr
<< "Listening for console connection on port " << port
<< endl
;
395 event
= new Event(this, listener
.getfd(), POLLIN
);
396 pollQueue
.schedule(event
);
400 ConsoleListener::add(SimConsole
*cons
)
401 { ConsoleList
.push_back(cons
);}
404 ConsoleListener::accept()
406 if (!listener
.islistening())
407 panic("%s: cannot accept a connection if we're not listening!",
410 int sfd
= listener
.accept(true);
412 iter_t i
= ConsoleList
.begin();
413 iter_t end
= ConsoleList
.end();
417 (*i
)->attach(sfd
, this);
418 i
= ConsoleList
.erase(i
);
423 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
427 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
429 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
431 INIT_PARAM_DFLT(port
, "listen port", 3456)
433 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
435 CREATE_SIM_OBJECT(ConsoleListener
)
437 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
438 listener
->listen(port
);
443 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)