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/output.hh"
47 #include "base/socket.hh"
48 #include "base/trace.hh"
49 #include "dev/platform.hh"
50 #include "dev/simconsole.hh"
51 #include "dev/uart.hh"
52 #include "mem/functional_mem/memory_control.hh"
53 #include "sim/builder.hh"
57 ////////////////////////////////////////////////////////////////////////
61 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
62 : PollEvent(fd
, e
), cons(c
)
67 SimConsole::Event::process(int revent
)
71 else if (revent
& POLLNVAL
)
75 SimConsole::SimConsole(const string
&name
, ostream
*os
, int num
)
76 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
77 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(os
)
83 outfile
->setf(ios::unitbuf
);
86 SimConsole::~SimConsole()
97 if (out_fd
!= in_fd
&& out_fd
!= -1)
102 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
108 event
= new Event(this, in
, POLLIN
);
109 pollQueue
.schedule(event
);
112 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
114 // we need an actual carriage return followed by a newline for the
118 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
121 DPRINTFN("attach console %d\n", number
);
133 pollQueue
.remove(event
);
140 DPRINTFN("detach console %d\n", number
);
149 len
= read(buf
, sizeof(buf
));
151 rxbuf
.write((char *)buf
, len
);
152 // Inform the UART there is data available
153 uart
->dataAvailable();
158 SimConsole::read(uint8_t *buf
, size_t len
)
161 panic("Console not properly attached.\n");
165 ret
= ::read(in_fd
, buf
, len
);
166 } while (ret
== -1 && errno
== EINTR
);
170 DPRINTFN("Read failed.\n");
182 SimConsole::write(const uint8_t *buf
, size_t len
)
185 panic("Console not properly attached.\n");
189 ret
= ::write(out_fd
, buf
, len
);
201 #define MORE_PENDING (ULL(1) << 61)
202 #define RECEIVE_SUCCESS (ULL(0) << 62)
203 #define RECEIVE_NONE (ULL(2) << 62)
204 #define RECEIVE_ERROR (ULL(3) << 62)
207 SimConsole::in(uint8_t &c
)
211 empty
= rxbuf
.empty();
214 rxbuf
.read((char *)&c
, 1);
215 empty
= rxbuf
.empty();
218 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
219 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
225 SimConsole::console_in()
231 value
= RECEIVE_SUCCESS
| c
;
233 value
|= MORE_PENDING
;
235 value
= RECEIVE_NONE
;
238 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
244 SimConsole::out(char c
)
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);
276 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
277 isprint(c
) ? c
: ' ', (int)c
);
283 SimConsole::serialize(ostream
&os
)
288 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
293 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
295 SimObjectParam
<ConsoleListener
*> listener
;
296 SimObjectParam
<IntrControl
*> intr_control
;
297 Param
<string
> output
;
298 Param
<bool> append_name
;
301 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
303 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
305 INIT_PARAM(listener
, "console listener"),
306 INIT_PARAM(intr_control
, "interrupt controller"),
307 INIT_PARAM(output
, "file to dump output to"),
308 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
309 INIT_PARAM_DFLT(number
, "console number", 0)
311 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
313 CREATE_SIM_OBJECT(SimConsole
)
315 string filename
= output
;
316 ostream
*stream
= NULL
;
318 if (!filename
.empty()) {
320 filename
+= "." + getInstanceName();
321 stream
= simout
.find(filename
);
324 SimConsole
*console
= new SimConsole(getInstanceName(), stream
, number
);
325 ((ConsoleListener
*)listener
)->add(console
);
330 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
332 ////////////////////////////////////////////////////////////////////////
336 ConsoleListener::ConsoleListener(const string
&name
)
337 : SimObject(name
), event(NULL
)
340 ConsoleListener::~ConsoleListener()
347 ConsoleListener::Event::process(int revent
)
352 ///////////////////////////////////////////////////////////////////////
353 // socket creation and console attach
357 ConsoleListener::listen(int port
)
359 while (!listener
.listen(port
, true)) {
361 ": can't bind address console port %d inuse PID %d\n",
366 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
368 event
= new Event(this, listener
.getfd(), POLLIN
);
369 pollQueue
.schedule(event
);
373 ConsoleListener::add(SimConsole
*cons
)
374 { ConsoleList
.push_back(cons
);}
377 ConsoleListener::accept()
379 if (!listener
.islistening())
380 panic("%s: cannot accept a connection if not listening!", name());
382 int sfd
= listener
.accept(true);
384 iter_t i
= ConsoleList
.begin();
385 iter_t end
= ConsoleList
.end();
389 (*i
)->attach(sfd
, this);
390 i
= ConsoleList
.erase(i
);
395 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
399 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
401 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
403 INIT_PARAM_DFLT(port
, "listen port", 3456)
405 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
407 CREATE_SIM_OBJECT(ConsoleListener
)
409 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
410 listener
->listen(port
);
415 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)