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 "debug/Terminal.hh"
54 #include "debug/TerminalVerbose.hh"
55 #include "dev/platform.hh"
56 #include "dev/terminal.hh"
57 #include "dev/uart.hh"
63 * Poll event for the listen socket
65 Terminal::ListenEvent::ListenEvent(Terminal
*t
, int fd
, int e
)
66 : PollEvent(fd
, e
), term(t
)
71 Terminal::ListenEvent::process(int revent
)
77 * Poll event for the data socket
79 Terminal::DataEvent::DataEvent(Terminal
*t
, int fd
, int e
)
80 : PollEvent(fd
, e
), term(t
)
85 Terminal::DataEvent::process(int revent
)
89 else if (revent
& POLLNVAL
)
96 Terminal::Terminal(const Params
*p
)
97 : SimObject(p
), listenEvent(NULL
), dataEvent(NULL
), number(p
->number
),
98 data_fd(-1), txbuf(16384), rxbuf(16384), outfile(NULL
)
104 outfile
= simout
.find(p
->name
);
106 outfile
= simout
.create(p
->name
);
108 outfile
->setf(ios::unitbuf
);
115 Terminal::~Terminal()
127 ///////////////////////////////////////////////////////////////////////
128 // socket creation and terminal attach
132 Terminal::listen(int port
)
134 if (ListenSocket::allDisabled()) {
135 warn_once("Sockets disabled, not accepting terminal connections");
139 while (!listener
.listen(port
, true)) {
141 ": can't bind address terminal port %d inuse PID %d\n",
147 p2
= name().rfind('.') - 1;
148 p1
= name().rfind('.', p2
);
149 ccprintf(cerr
, "Listening for %s connection on port %d\n",
150 name().substr(p1
+1,p2
-p1
), port
);
152 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
153 pollQueue
.schedule(listenEvent
);
159 if (!listener
.islistening())
160 panic("%s: cannot accept a connection if not listening!", name());
162 int fd
= listener
.accept(true);
164 char message
[] = "terminal already attached!\n";
165 atomic_write(fd
, message
, sizeof(message
));
171 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
172 pollQueue
.schedule(dataEvent
);
175 ccprintf(stream
, "==== m5 slave terminal: Terminal %d ====", number
);
177 // we need an actual carriage return followed by a newline for the
181 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
183 DPRINTFN("attach terminal %d\n", number
);
185 txbuf
.readall(data_fd
);
196 pollQueue
.remove(dataEvent
);
200 DPRINTFN("detach terminal %d\n", number
);
209 len
= read(buf
, sizeof(buf
));
211 rxbuf
.write((char *)buf
, len
);
212 // Inform the UART there is data available
213 uart
->dataAvailable();
218 Terminal::read(uint8_t *buf
, size_t len
)
221 panic("Terminal not properly attached.\n");
225 ret
= ::read(data_fd
, buf
, len
);
226 } while (ret
== -1 && errno
== EINTR
);
230 DPRINTFN("Read failed.\n");
242 Terminal::write(const uint8_t *buf
, size_t len
)
245 panic("Terminal not properly attached.\n");
247 ssize_t ret
= atomic_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)
264 assert(!rxbuf
.empty());
265 rxbuf
.read((char *)&c
, 1);
267 DPRINTF(TerminalVerbose
, "in: \'%c\' %#02x more: %d\n",
268 isprint(c
) ? c
: ' ', c
, !rxbuf
.empty());
274 Terminal::console_in()
278 if (dataAvailable()) {
279 value
= RECEIVE_SUCCESS
| in();
281 value
|= MORE_PENDING
;
283 value
= RECEIVE_NONE
;
286 DPRINTF(TerminalVerbose
, "console_in: return: %#x\n", value
);
292 Terminal::out(char c
)
295 if (DTRACE(Terminal
)) {
296 static char last
= '\0';
298 if ((c
!= '\n' && c
!= '\r') || (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(Terminal
, "%s\n", buffer
);
321 outfile
->write(&c
, 1);
323 DPRINTF(TerminalVerbose
, "out: \'%c\' %#02x\n",
324 isprint(c
) ? c
: ' ', (int)c
);
329 TerminalParams::create()
331 return new Terminal(this);