d8d890e15d69af42b059482558606535fefe7e8d
2 * Copyright (c) 2004 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 * Implements the user interface to a serial console
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/platform.hh"
49 #include "dev/simconsole.hh"
50 #include "dev/uart.hh"
51 #include "mem/functional_mem/memory_control.hh"
52 #include "sim/builder.hh"
56 ////////////////////////////////////////////////////////////////////////
60 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
61 : PollEvent(fd
, e
), cons(c
)
66 SimConsole::Event::process(int revent
)
70 else if (revent
& POLLNVAL
)
74 SimConsole::SimConsole(const string
&name
, std::ostream
*os
, int num
)
75 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
76 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(os
)
82 outfile
->setf(ios::unitbuf
);
85 SimConsole::~SimConsole()
89 closeOutputStream(outfile
);
98 if (out_fd
!= in_fd
&& out_fd
!= -1)
103 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
109 event
= new Event(this, in
, POLLIN
);
110 pollQueue
.schedule(event
);
113 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
115 // we need an actual carriage return followed by a newline for the
119 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
122 DPRINTFN("attach console %d\n", number
);
134 pollQueue
.remove(event
);
141 DPRINTFN("detach console %d\n", number
);
150 len
= read(buf
, sizeof(buf
));
152 rxbuf
.write((char *)buf
, len
);
153 // Inform the UART there is data available
154 uart
->dataAvailable();
159 SimConsole::read(uint8_t *buf
, size_t len
)
162 panic("Console not properly attached.\n");
166 ret
= ::read(in_fd
, buf
, len
);
167 } while (ret
== -1 && errno
== EINTR
);
171 DPRINTFN("Read failed.\n");
183 SimConsole::write(const uint8_t *buf
, size_t len
)
186 panic("Console not properly attached.\n");
190 ret
= ::write(out_fd
, buf
, len
);
202 #define MORE_PENDING (ULL(1) << 61)
203 #define RECEIVE_SUCCESS (ULL(0) << 62)
204 #define RECEIVE_NONE (ULL(2) << 62)
205 #define RECEIVE_ERROR (ULL(3) << 62)
208 SimConsole::in(uint8_t &c
)
212 empty
= rxbuf
.empty();
215 rxbuf
.read((char *)&c
, 1);
216 empty
= rxbuf
.empty();
219 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
220 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
226 SimConsole::console_in()
232 value
= RECEIVE_SUCCESS
| c
;
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
)
318 if (!output
.isValid()) {
319 filename
= getInstanceName();
320 } else if (append_name
) {
321 filename
= (string
)output
+ "." + getInstanceName();
326 SimConsole
*console
= new SimConsole(getInstanceName(),
327 makeOutputStream(filename
), number
);
328 ((ConsoleListener
*)listener
)->add(console
);
333 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
335 ////////////////////////////////////////////////////////////////////////
339 ConsoleListener::ConsoleListener(const string
&name
)
340 : SimObject(name
), event(NULL
)
343 ConsoleListener::~ConsoleListener()
350 ConsoleListener::Event::process(int revent
)
355 ///////////////////////////////////////////////////////////////////////
356 // socket creation and console attach
360 ConsoleListener::listen(int port
)
362 while (!listener
.listen(port
, true)) {
364 ": can't bind address console port %d inuse PID %d\n",
369 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
371 event
= new Event(this, listener
.getfd(), POLLIN
);
372 pollQueue
.schedule(event
);
376 ConsoleListener::add(SimConsole
*cons
)
377 { ConsoleList
.push_back(cons
);}
380 ConsoleListener::accept()
382 if (!listener
.islistening())
383 panic("%s: cannot accept a connection if not listening!", name());
385 int sfd
= listener
.accept(true);
387 iter_t i
= ConsoleList
.begin();
388 iter_t end
= ConsoleList
.end();
392 (*i
)->attach(sfd
, this);
393 i
= ConsoleList
.erase(i
);
398 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
402 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
404 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
406 INIT_PARAM_DFLT(port
, "listen port", 3456)
408 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
410 CREATE_SIM_OBJECT(ConsoleListener
)
412 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
413 listener
->listen(port
);
418 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)