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/simconsole.hh"
49 #include "mem/functional_mem/memory_control.hh"
50 #include "sim/builder.hh"
51 #include "targetarch/ev5.hh"
52 #include "dev/uart.hh"
53 #include "dev/platform.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
, std::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()
90 closeOutputStream(outfile
);
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)
209 SimConsole::in(uint8_t &c
)
213 empty
= rxbuf
.empty();
216 rxbuf
.read((char *)&c
, 1);
217 empty
= rxbuf
.empty();
220 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
221 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
227 SimConsole::console_in()
233 value
= RECEIVE_SUCCESS
| c
;
235 value
|= MORE_PENDING
;
237 value
= RECEIVE_NONE
;
240 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
246 SimConsole::out(char c
)
249 if (DTRACE(Console
)) {
250 static char last
= '\0';
252 if (c
!= '\n' && c
!= '\r' ||
253 last
!= '\n' && last
!= '\r') {
254 if (c
== '\n' || c
== '\r') {
255 int size
= linebuf
.size();
256 char *buffer
= new char[size
+ 1];
257 linebuf
.read(buffer
, size
);
259 DPRINTF(Console
, "%s\n", buffer
);
276 outfile
->write(&c
, 1);
278 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
279 isprint(c
) ? c
: ' ', (int)c
);
285 SimConsole::serialize(ostream
&os
)
290 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
295 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
297 SimObjectParam
<ConsoleListener
*> listener
;
298 SimObjectParam
<IntrControl
*> intr_control
;
299 Param
<string
> output
;
300 Param
<bool> append_name
;
303 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
305 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
307 INIT_PARAM(listener
, "console listener"),
308 INIT_PARAM(intr_control
, "interrupt controller"),
309 INIT_PARAM(output
, "file to dump output to"),
310 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
311 INIT_PARAM_DFLT(number
, "console number", 0)
313 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
315 CREATE_SIM_OBJECT(SimConsole
)
319 if (!output
.isValid()) {
320 filename
= getInstanceName();
321 } else if (append_name
) {
322 filename
= (string
)output
+ "." + getInstanceName();
327 SimConsole
*console
= new SimConsole(getInstanceName(),
328 makeOutputStream(filename
), number
);
329 ((ConsoleListener
*)listener
)->add(console
);
334 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
336 ////////////////////////////////////////////////////////////////////////
340 ConsoleListener::ConsoleListener(const string
&name
)
341 : SimObject(name
), event(NULL
)
344 ConsoleListener::~ConsoleListener()
351 ConsoleListener::Event::process(int revent
)
356 ///////////////////////////////////////////////////////////////////////
357 // socket creation and console attach
361 ConsoleListener::listen(int port
)
363 while (!listener
.listen(port
, true)) {
365 ": can't bind address console port %d inuse PID %d\n",
370 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
372 event
= new Event(this, listener
.getfd(), POLLIN
);
373 pollQueue
.schedule(event
);
377 ConsoleListener::add(SimConsole
*cons
)
378 { ConsoleList
.push_back(cons
);}
381 ConsoleListener::accept()
383 if (!listener
.islistening())
384 panic("%s: cannot accept a connection if not listening!", name());
386 int sfd
= listener
.accept(true);
388 iter_t i
= ConsoleList
.begin();
389 iter_t end
= ConsoleList
.end();
393 (*i
)->attach(sfd
, this);
394 i
= ConsoleList
.erase(i
);
399 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
403 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
405 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
407 INIT_PARAM_DFLT(port
, "listen port", 3456)
409 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
411 CREATE_SIM_OBJECT(ConsoleListener
)
413 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
414 listener
->listen(port
);
419 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)