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"
61 * Poll event for the listen socket
63 SimConsole::ListenEvent::ListenEvent(SimConsole
*c
, int fd
, int e
)
64 : PollEvent(fd
, e
), cons(c
)
69 SimConsole::ListenEvent::process(int revent
)
75 * Poll event for the data socket
77 SimConsole::DataEvent::DataEvent(SimConsole
*c
, int fd
, int e
)
78 : PollEvent(fd
, e
), cons(c
)
83 SimConsole::DataEvent::process(int revent
)
87 else if (revent
& POLLNVAL
)
94 SimConsole::SimConsole(const string
&name
, ostream
*os
, int num
, int port
)
95 : SimObject(name
), listenEvent(NULL
), dataEvent(NULL
), number(num
),
96 data_fd(-1), txbuf(16384), rxbuf(16384), outfile(os
)
102 outfile
->setf(ios::unitbuf
);
108 SimConsole::~SimConsole()
120 ///////////////////////////////////////////////////////////////////////
121 // socket creation and console attach
125 SimConsole::listen(int port
)
127 while (!listener
.listen(port
, true)) {
129 ": can't bind address console port %d inuse PID %d\n",
135 p2
= name().rfind('.') - 1;
136 p1
= name().rfind('.', p2
);
137 ccprintf(cerr
, "Listening for %s connection on port %d\n",
138 name().substr(p1
+1,p2
-p1
), port
);
140 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
141 pollQueue
.schedule(listenEvent
);
147 if (!listener
.islistening())
148 panic("%s: cannot accept a connection if not listening!", name());
150 int fd
= listener
.accept(true);
152 char message
[] = "console already attached!\n";
153 ::write(fd
, message
, sizeof(message
));
159 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
160 pollQueue
.schedule(dataEvent
);
163 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
165 // we need an actual carriage return followed by a newline for the
169 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
171 DPRINTFN("attach console %d\n", number
);
173 txbuf
.readall(data_fd
);
184 pollQueue
.remove(dataEvent
);
188 DPRINTFN("detach console %d\n", number
);
197 len
= read(buf
, sizeof(buf
));
199 rxbuf
.write((char *)buf
, len
);
200 // Inform the UART there is data available
201 uart
->dataAvailable();
206 SimConsole::read(uint8_t *buf
, size_t len
)
209 panic("Console not properly attached.\n");
213 ret
= ::read(data_fd
, buf
, len
);
214 } while (ret
== -1 && errno
== EINTR
);
218 DPRINTFN("Read failed.\n");
230 SimConsole::write(const uint8_t *buf
, size_t len
)
233 panic("Console not properly attached.\n");
237 ret
= ::write(data_fd
, buf
, len
);
249 #define MORE_PENDING (ULL(1) << 61)
250 #define RECEIVE_SUCCESS (ULL(0) << 62)
251 #define RECEIVE_NONE (ULL(2) << 62)
252 #define RECEIVE_ERROR (ULL(3) << 62)
260 empty
= rxbuf
.empty();
262 rxbuf
.read((char *)&c
, 1);
263 empty
= rxbuf
.empty();
266 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d\n",
267 isprint(c
) ? c
: ' ', c
, !empty
);
273 SimConsole::console_in()
277 if (dataAvailable()) {
278 value
= RECEIVE_SUCCESS
| in();
280 value
|= MORE_PENDING
;
282 value
= RECEIVE_NONE
;
285 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
291 SimConsole::out(char c
)
294 if (DTRACE(Console
)) {
295 static char last
= '\0';
297 if (c
!= '\n' && c
!= '\r' ||
298 last
!= '\n' && last
!= '\r') {
299 if (c
== '\n' || c
== '\r') {
300 int size
= linebuf
.size();
301 char *buffer
= new char[size
+ 1];
302 linebuf
.read(buffer
, size
);
304 DPRINTF(Console
, "%s\n", buffer
);
321 outfile
->write(&c
, 1);
323 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
324 isprint(c
) ? c
: ' ', (int)c
);
328 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
330 SimObjectParam
<IntrControl
*> intr_control
;
331 Param
<string
> output
;
333 Param
<bool> append_name
;
336 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
338 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
340 INIT_PARAM(intr_control
, "interrupt controller"),
341 INIT_PARAM(output
, "file to dump output to"),
342 INIT_PARAM(port
, ""),
343 INIT_PARAM_DFLT(append_name
, "append name() to filename", true),
344 INIT_PARAM_DFLT(number
, "console number", 0)
346 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
348 CREATE_SIM_OBJECT(SimConsole
)
350 string filename
= output
;
351 ostream
*stream
= NULL
;
353 if (!filename
.empty()) {
355 filename
+= "." + getInstanceName();
356 stream
= simout
.find(filename
);
359 return new SimConsole(getInstanceName(), stream
, number
, port
);
362 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)