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"
60 * Poll event for the listen socket
62 SimConsole::ListenEvent::ListenEvent(SimConsole
*c
, int fd
, int e
)
63 : PollEvent(fd
, e
), cons(c
)
68 SimConsole::ListenEvent::process(int revent
)
74 * Poll event for the data socket
76 SimConsole::DataEvent::DataEvent(SimConsole
*c
, int fd
, int e
)
77 : PollEvent(fd
, e
), cons(c
)
82 SimConsole::DataEvent::process(int revent
)
86 else if (revent
& POLLNVAL
)
93 SimConsole::SimConsole(const Params
*p
)
94 : SimObject(p
), listenEvent(NULL
), dataEvent(NULL
), number(p
->number
),
95 data_fd(-1), txbuf(16384), rxbuf(16384), outfile(NULL
)
100 if (!p
->output
.empty()) {
102 outfile
= simout
.find(p
->output
+ "." + p
->name
);
104 outfile
= simout
.find(p
->output
);
106 outfile
->setf(ios::unitbuf
);
113 SimConsole::~SimConsole()
125 ///////////////////////////////////////////////////////////////////////
126 // socket creation and console attach
130 SimConsole::listen(int port
)
132 while (!listener
.listen(port
, true)) {
134 ": can't bind address console port %d inuse PID %d\n",
140 p2
= name().rfind('.') - 1;
141 p1
= name().rfind('.', p2
);
142 ccprintf(cerr
, "Listening for %s connection on port %d\n",
143 name().substr(p1
+1,p2
-p1
), port
);
145 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
146 pollQueue
.schedule(listenEvent
);
152 if (!listener
.islistening())
153 panic("%s: cannot accept a connection if not listening!", name());
155 int fd
= listener
.accept(true);
157 char message
[] = "console already attached!\n";
158 ::write(fd
, message
, sizeof(message
));
164 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
165 pollQueue
.schedule(dataEvent
);
168 ccprintf(stream
, "==== m5 slave console: Console %d ====", number
);
170 // we need an actual carriage return followed by a newline for the
174 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
176 DPRINTFN("attach console %d\n", number
);
178 txbuf
.readall(data_fd
);
189 pollQueue
.remove(dataEvent
);
193 DPRINTFN("detach console %d\n", number
);
202 len
= read(buf
, sizeof(buf
));
204 rxbuf
.write((char *)buf
, len
);
205 // Inform the UART there is data available
206 uart
->dataAvailable();
211 SimConsole::read(uint8_t *buf
, size_t len
)
214 panic("Console not properly attached.\n");
218 ret
= ::read(data_fd
, buf
, len
);
219 } while (ret
== -1 && errno
== EINTR
);
223 DPRINTFN("Read failed.\n");
235 SimConsole::write(const uint8_t *buf
, size_t len
)
238 panic("Console not properly attached.\n");
242 ret
= ::write(data_fd
, buf
, len
);
254 #define MORE_PENDING (ULL(1) << 61)
255 #define RECEIVE_SUCCESS (ULL(0) << 62)
256 #define RECEIVE_NONE (ULL(2) << 62)
257 #define RECEIVE_ERROR (ULL(3) << 62)
265 empty
= rxbuf
.empty();
267 rxbuf
.read((char *)&c
, 1);
268 empty
= rxbuf
.empty();
271 DPRINTF(ConsoleVerbose
, "in: \'%c\' %#02x more: %d\n",
272 isprint(c
) ? c
: ' ', c
, !empty
);
278 SimConsole::console_in()
282 if (dataAvailable()) {
283 value
= RECEIVE_SUCCESS
| in();
285 value
|= MORE_PENDING
;
287 value
= RECEIVE_NONE
;
290 DPRINTF(ConsoleVerbose
, "console_in: return: %#x\n", value
);
296 SimConsole::out(char c
)
299 if (DTRACE(Console
)) {
300 static char last
= '\0';
302 if (c
!= '\n' && c
!= '\r' ||
303 last
!= '\n' && last
!= '\r') {
304 if (c
== '\n' || c
== '\r') {
305 int size
= linebuf
.size();
306 char *buffer
= new char[size
+ 1];
307 linebuf
.read(buffer
, size
);
309 DPRINTF(Console
, "%s\n", buffer
);
326 outfile
->write(&c
, 1);
328 DPRINTF(ConsoleVerbose
, "out: \'%c\' %#02x\n",
329 isprint(c
) ? c
: ' ', (int)c
);
334 SimConsoleParams::create()
336 return new SimConsole(this);