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 terminal
36 #include <sys/ioctl.h>
37 #include <sys/termios.h>
48 #include "base/atomicio.hh"
49 #include "base/misc.hh"
50 #include "base/output.hh"
51 #include "base/socket.hh"
52 #include "base/trace.hh"
53 #include "dev/platform.hh"
54 #include "dev/terminal.hh"
55 #include "dev/uart.hh"
61 * Poll event for the listen socket
63 Terminal::ListenEvent::ListenEvent(Terminal
*t
, int fd
, int e
)
64 : PollEvent(fd
, e
), term(t
)
69 Terminal::ListenEvent::process(int revent
)
75 * Poll event for the data socket
77 Terminal::DataEvent::DataEvent(Terminal
*t
, int fd
, int e
)
78 : PollEvent(fd
, e
), term(t
)
83 Terminal::DataEvent::process(int revent
)
87 else if (revent
& POLLNVAL
)
94 Terminal::Terminal(const Params
*p
)
95 : SimObject(p
), listenEvent(NULL
), dataEvent(NULL
), number(p
->number
),
96 data_fd(-1), txbuf(16384), rxbuf(16384), outfile(NULL
)
102 outfile
= simout
.find(p
->name
);
103 outfile
->setf(ios::unitbuf
);
110 Terminal::~Terminal()
122 ///////////////////////////////////////////////////////////////////////
123 // socket creation and terminal attach
127 Terminal::listen(int port
)
129 if (ListenSocket::allDisabled()) {
130 warn_once("Sockets disabled, not accepting terminal connections");
134 while (!listener
.listen(port
, true)) {
136 ": can't bind address terminal port %d inuse PID %d\n",
142 p2
= name().rfind('.') - 1;
143 p1
= name().rfind('.', p2
);
144 ccprintf(cerr
, "Listening for %s connection on port %d\n",
145 name().substr(p1
+1,p2
-p1
), port
);
147 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
148 pollQueue
.schedule(listenEvent
);
154 if (!listener
.islistening())
155 panic("%s: cannot accept a connection if not listening!", name());
157 int fd
= listener
.accept(true);
159 char message
[] = "terminal already attached!\n";
160 atomic_write(fd
, message
, sizeof(message
));
166 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
167 pollQueue
.schedule(dataEvent
);
170 ccprintf(stream
, "==== m5 slave terminal: Terminal %d ====", number
);
172 // we need an actual carriage return followed by a newline for the
176 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
178 DPRINTFN("attach terminal %d\n", number
);
180 txbuf
.readall(data_fd
);
191 pollQueue
.remove(dataEvent
);
195 DPRINTFN("detach terminal %d\n", number
);
204 len
= read(buf
, sizeof(buf
));
206 rxbuf
.write((char *)buf
, len
);
207 // Inform the UART there is data available
208 uart
->dataAvailable();
213 Terminal::read(uint8_t *buf
, size_t len
)
216 panic("Terminal not properly attached.\n");
220 ret
= ::read(data_fd
, buf
, len
);
221 } while (ret
== -1 && errno
== EINTR
);
225 DPRINTFN("Read failed.\n");
237 Terminal::write(const uint8_t *buf
, size_t len
)
240 panic("Terminal not properly attached.\n");
242 ssize_t ret
= atomic_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(TerminalVerbose
, "in: \'%c\' %#02x more: %d\n",
267 isprint(c
) ? c
: ' ', c
, !empty
);
273 Terminal::console_in()
277 if (dataAvailable()) {
278 value
= RECEIVE_SUCCESS
| in();
280 value
|= MORE_PENDING
;
282 value
= RECEIVE_NONE
;
285 DPRINTF(TerminalVerbose
, "console_in: return: %#x\n", value
);
291 Terminal::out(char c
)
294 if (DTRACE(Terminal
)) {
295 static char last
= '\0';
297 if ((c
!= '\n' && c
!= '\r') || (last
!= '\n' && last
!= '\r')) {
298 if (c
== '\n' || c
== '\r') {
299 int size
= linebuf
.size();
300 char *buffer
= new char[size
+ 1];
301 linebuf
.read(buffer
, size
);
303 DPRINTF(Terminal
, "%s\n", buffer
);
320 outfile
->write(&c
, 1);
322 DPRINTF(TerminalVerbose
, "out: \'%c\' %#02x\n",
323 isprint(c
) ? c
: ' ', (int)c
);
328 TerminalParams::create()
330 return new Terminal(this);