2 * Copyright (c) 2001-2005 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.
28 * Authors: Nathan Binkert
33 * Implements the user interface to a serial console
36 #include <sys/ioctl.h>
37 #include <sys/termios.h>
38 #include <sys/types.h>
48 #include "base/misc.hh"
49 #include "base/output.hh"
50 #include "base/socket.hh"
51 #include "base/trace.hh"
52 #include "dev/platform.hh"
53 #include "dev/simconsole.hh"
54 #include "dev/uart.hh"
55 #include "sim/builder.hh"
59 ////////////////////////////////////////////////////////////////////////
63 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
64 : PollEvent(fd
, e
), cons(c
)
69 SimConsole::Event::process(int revent
)
73 else if (revent
& POLLNVAL
)
77 SimConsole::SimConsole(const string
&name
, ostream
*os
, int num
)
78 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
79 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(os
)
85 outfile
->setf(ios::unitbuf
);
88 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
, "==== m5 slave console: Console %d ====", number
);
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 // Inform the UART there is data available
155 uart
->dataAvailable();
160 SimConsole::read(uint8_t *buf
, size_t len
)
163 panic("Console not properly attached.\n");
167 ret
= ::read(in_fd
, buf
, len
);
168 } while (ret
== -1 && errno
== EINTR
);
172 DPRINTFN("Read failed.\n");
184 SimConsole::write(const uint8_t *buf
, size_t len
)
187 panic("Console not properly attached.\n");
191 ret
= ::write(out_fd
, buf
, len
);
203 #define MORE_PENDING (ULL(1) << 61)
204 #define RECEIVE_SUCCESS (ULL(0) << 62)
205 #define RECEIVE_NONE (ULL(2) << 62)
206 #define RECEIVE_ERROR (ULL(3) << 62)
214 empty
= rxbuf
.empty();
216 rxbuf
.read((char *)&c
, 1);
217 empty
= rxbuf
.empty();
220 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d\n",
221 isprint(c
) ? c
: ' ', c
, !empty
);
227 SimConsole::console_in()
231 if (dataAvailable()) {
232 value
= RECEIVE_SUCCESS
| in();
234 value
|= MORE_PENDING
;
236 value
= RECEIVE_NONE
;
239 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
245 SimConsole::out(char c
)
248 if (DTRACE(Console
)) {
249 static char last
= '\0';
251 if (c
!= '\n' && c
!= '\r' ||
252 last
!= '\n' && last
!= '\r') {
253 if (c
== '\n' || c
== '\r') {
254 int size
= linebuf
.size();
255 char *buffer
= new char[size
+ 1];
256 linebuf
.read(buffer
, size
);
258 DPRINTF(Console
, "%s\n", buffer
);
275 outfile
->write(&c
, 1);
277 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
278 isprint(c
) ? c
: ' ', (int)c
);
284 SimConsole::serialize(ostream
&os
)
289 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
294 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
296 SimObjectParam
<ConsoleListener
*> listener
;
297 SimObjectParam
<IntrControl
*> intr_control
;
298 Param
<string
> output
;
299 Param
<bool> append_name
;
302 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
304 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
306 INIT_PARAM(listener
, "console listener"),
307 INIT_PARAM(intr_control
, "interrupt controller"),
308 INIT_PARAM(output
, "file to dump output to"),
309 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
310 INIT_PARAM_DFLT(number
, "console number", 0)
312 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
314 CREATE_SIM_OBJECT(SimConsole
)
316 string filename
= output
;
317 ostream
*stream
= NULL
;
319 if (!filename
.empty()) {
321 filename
+= "." + getInstanceName();
322 stream
= simout
.find(filename
);
325 SimConsole
*console
= new SimConsole(getInstanceName(), stream
, number
);
326 ((ConsoleListener
*)listener
)->add(console
);
331 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
333 ////////////////////////////////////////////////////////////////////////
337 ConsoleListener::ConsoleListener(const string
&name
)
338 : SimObject(name
), event(NULL
)
341 ConsoleListener::~ConsoleListener()
348 ConsoleListener::Event::process(int revent
)
353 ///////////////////////////////////////////////////////////////////////
354 // socket creation and console attach
358 ConsoleListener::listen(int port
)
360 while (!listener
.listen(port
, true)) {
362 ": can't bind address console port %d inuse PID %d\n",
367 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
369 event
= new Event(this, listener
.getfd(), POLLIN
);
370 pollQueue
.schedule(event
);
374 ConsoleListener::add(SimConsole
*cons
)
375 { ConsoleList
.push_back(cons
);}
378 ConsoleListener::accept()
380 if (!listener
.islistening())
381 panic("%s: cannot accept a connection if not listening!", name());
383 int sfd
= listener
.accept(true);
385 iter_t i
= ConsoleList
.begin();
386 iter_t end
= ConsoleList
.end();
390 (*i
)->attach(sfd
, this);
391 i
= ConsoleList
.erase(i
);
396 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
400 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
402 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
404 INIT_PARAM_DFLT(port
, "listen port", 3456)
406 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
408 CREATE_SIM_OBJECT(ConsoleListener
)
410 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
411 listener
->listen(port
);
416 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)