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
, const string
&file
, int num
)
76 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
77 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(NULL
)
83 outfile
= new ofstream(file
.c_str());
86 outfile
->setf(ios::unitbuf
);
90 SimConsole::~SimConsole()
104 if (out_fd
!= in_fd
&& out_fd
!= -1)
109 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
115 event
= new Event(this, in
, POLLIN
);
116 pollQueue
.schedule(event
);
119 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
121 // we need an actual carriage return followed by a newline for the
125 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
128 DPRINTFN("attach console %d\n", number
);
140 pollQueue
.remove(event
);
147 DPRINTFN("detach console %d\n", number
);
156 len
= read(buf
, sizeof(buf
));
158 rxbuf
.write((char *)buf
, len
);
159 // Inform the UART there is data available
160 uart
->dataAvailable();
165 SimConsole::read(uint8_t *buf
, size_t len
)
168 panic("Console not properly attached.\n");
172 ret
= ::read(in_fd
, buf
, len
);
173 } while (ret
== -1 && errno
== EINTR
);
177 DPRINTFN("Read failed.\n");
189 SimConsole::write(const uint8_t *buf
, size_t len
)
192 panic("Console not properly attached.\n");
196 ret
= ::write(out_fd
, buf
, len
);
208 #define MORE_PENDING (ULL(1) << 61)
209 #define RECEIVE_SUCCESS (ULL(0) << 62)
210 #define RECEIVE_NONE (ULL(2) << 62)
211 #define RECEIVE_ERROR (ULL(3) << 62)
214 SimConsole::in(uint8_t &c
)
218 empty
= rxbuf
.empty();
221 rxbuf
.read((char *)&c
, 1);
222 empty
= rxbuf
.empty();
225 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d, return: %d\n",
226 isprint(c
) ? c
: ' ', c
, !empty
, ret
);
232 SimConsole::console_in()
238 value
= RECEIVE_SUCCESS
| c
;
240 value
|= MORE_PENDING
;
242 value
= RECEIVE_NONE
;
245 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
251 SimConsole::out(char c
)
254 if (DTRACE(Console
)) {
255 static char last
= '\0';
257 if (c
!= '\n' && c
!= '\r' ||
258 last
!= '\n' && last
!= '\r') {
259 if (c
== '\n' || c
== '\r') {
260 int size
= linebuf
.size();
261 char *buffer
= new char[size
+ 1];
262 linebuf
.read(buffer
, size
);
264 DPRINTF(Console
, "%s\n", buffer
);
281 outfile
->write(&c
, 1);
283 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
284 isprint(c
) ? c
: ' ', (int)c
);
290 SimConsole::serialize(ostream
&os
)
295 SimConsole::unserialize(Checkpoint
*cp
, const std::string
§ion
)
300 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
302 SimObjectParam
<ConsoleListener
*> listener
;
303 SimObjectParam
<IntrControl
*> intr_control
;
304 Param
<string
> output
;
305 Param
<bool> append_name
;
308 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
310 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
312 INIT_PARAM(listener
, "console listener"),
313 INIT_PARAM(intr_control
, "interrupt controller"),
314 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
315 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
316 INIT_PARAM_DFLT(number
, "console number", 0)
318 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
320 CREATE_SIM_OBJECT(SimConsole
)
322 string filename
= output
;
323 if (filename
.empty()) {
324 if (!outputDirectory
.empty())
325 filename
= outputDirectory
+ getInstanceName();
328 filename
+= "." + getInstanceName();
329 if (!outputDirectory
.empty())
330 filename
= outputDirectory
+ filename
;
333 SimConsole
*console
= new SimConsole(getInstanceName(), filename
, number
);
334 ((ConsoleListener
*)listener
)->add(console
);
339 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
341 ////////////////////////////////////////////////////////////////////////
345 ConsoleListener::ConsoleListener(const string
&name
)
346 : SimObject(name
), event(NULL
)
349 ConsoleListener::~ConsoleListener()
356 ConsoleListener::Event::process(int revent
)
361 ///////////////////////////////////////////////////////////////////////
362 // socket creation and console attach
366 ConsoleListener::listen(int port
)
368 while (!listener
.listen(port
, true)) {
370 ": can't bind address console port %d inuse PID %d\n",
375 ccprintf(cerr
, "Listening for console connection on port %d\n", port
);
377 event
= new Event(this, listener
.getfd(), POLLIN
);
378 pollQueue
.schedule(event
);
382 ConsoleListener::add(SimConsole
*cons
)
383 { ConsoleList
.push_back(cons
);}
386 ConsoleListener::accept()
388 if (!listener
.islistening())
389 panic("%s: cannot accept a connection if not listening!", name());
391 int sfd
= listener
.accept(true);
393 iter_t i
= ConsoleList
.begin();
394 iter_t end
= ConsoleList
.end();
398 (*i
)->attach(sfd
, this);
399 i
= ConsoleList
.erase(i
);
404 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
408 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
410 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
412 INIT_PARAM_DFLT(port
, "listen port", 3456)
414 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
416 CREATE_SIM_OBJECT(ConsoleListener
)
418 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
419 listener
->listen(port
);
424 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)