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>
38 #if defined(__FreeBSD__)
42 #include <sys/termios.h>
45 #include "dev/terminal.hh"
57 #include "base/atomicio.hh"
58 #include "base/misc.hh"
59 #include "base/output.hh"
60 #include "base/socket.hh"
61 #include "base/trace.hh"
62 #include "debug/Terminal.hh"
63 #include "debug/TerminalVerbose.hh"
64 #include "dev/platform.hh"
65 #include "dev/uart.hh"
71 * Poll event for the listen socket
73 Terminal::ListenEvent::ListenEvent(Terminal
*t
, int fd
, int e
)
74 : PollEvent(fd
, e
), term(t
)
79 Terminal::ListenEvent::process(int revent
)
85 * Poll event for the data socket
87 Terminal::DataEvent::DataEvent(Terminal
*t
, int fd
, int e
)
88 : PollEvent(fd
, e
), term(t
)
93 Terminal::DataEvent::process(int revent
)
95 // As a consequence of being called from the PollQueue, we might
96 // have been called from a different thread. Migrate to "our"
98 EventQueue::ScopedMigration
migrate(term
->eventQueue());
102 else if (revent
& POLLNVAL
)
109 Terminal::Terminal(const Params
*p
)
110 : SimObject(p
), termDataAvail(NULL
), listenEvent(NULL
), dataEvent(NULL
),
111 number(p
->number
), data_fd(-1), txbuf(16384), rxbuf(16384), outfile(NULL
)
117 outfile
= simout
.find(p
->name
);
119 outfile
= simout
.create(p
->name
);
121 outfile
->setf(ios::unitbuf
);
128 Terminal::~Terminal()
141 Terminal::regDataAvailCallback(Callback
*c
)
143 // This can happen if the user has connected multiple UARTs to the
144 // same terminal. In that case, each of them tries to register
147 fatal("Terminal already has already been associated with a UART.\n");
151 ///////////////////////////////////////////////////////////////////////
152 // socket creation and terminal attach
156 Terminal::listen(int port
)
158 if (ListenSocket::allDisabled()) {
159 warn_once("Sockets disabled, not accepting terminal connections");
163 while (!listener
.listen(port
, true)) {
165 ": can't bind address terminal port %d inuse PID %d\n",
171 p2
= name().rfind('.') - 1;
172 p1
= name().rfind('.', p2
);
173 ccprintf(cerr
, "Listening for %s connection on port %d\n",
174 name().substr(p1
+1,p2
-p1
), port
);
176 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
177 pollQueue
.schedule(listenEvent
);
183 if (!listener
.islistening())
184 panic("%s: cannot accept a connection if not listening!", name());
186 int fd
= listener
.accept(true);
188 char message
[] = "terminal already attached!\n";
189 atomic_write(fd
, message
, sizeof(message
));
195 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
196 pollQueue
.schedule(dataEvent
);
199 ccprintf(stream
, "==== m5 slave terminal: Terminal %d ====", number
);
201 // we need an actual carriage return followed by a newline for the
205 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
207 DPRINTFN("attach terminal %d\n", number
);
209 txbuf
.readall(data_fd
);
220 pollQueue
.remove(dataEvent
);
224 DPRINTFN("detach terminal %d\n", number
);
233 len
= read(buf
, sizeof(buf
));
235 rxbuf
.write((char *)buf
, len
);
236 // Inform the UART there is data available
237 assert(termDataAvail
);
238 termDataAvail
->process();
243 Terminal::read(uint8_t *buf
, size_t len
)
246 panic("Terminal not properly attached.\n");
250 ret
= ::read(data_fd
, buf
, len
);
251 } while (ret
== -1 && errno
== EINTR
);
255 DPRINTFN("Read failed.\n");
267 Terminal::write(const uint8_t *buf
, size_t len
)
270 panic("Terminal not properly attached.\n");
272 ssize_t ret
= atomic_write(data_fd
, buf
, len
);
279 #define MORE_PENDING (ULL(1) << 61)
280 #define RECEIVE_SUCCESS (ULL(0) << 62)
281 #define RECEIVE_NONE (ULL(2) << 62)
282 #define RECEIVE_ERROR (ULL(3) << 62)
289 assert(!rxbuf
.empty());
290 rxbuf
.read((char *)&c
, 1);
292 DPRINTF(TerminalVerbose
, "in: \'%c\' %#02x more: %d\n",
293 isprint(c
) ? c
: ' ', c
, !rxbuf
.empty());
299 Terminal::console_in()
303 if (dataAvailable()) {
304 value
= RECEIVE_SUCCESS
| in();
306 value
|= MORE_PENDING
;
308 value
= RECEIVE_NONE
;
311 DPRINTF(TerminalVerbose
, "console_in: return: %#x\n", value
);
317 Terminal::out(char c
)
320 if (DTRACE(Terminal
)) {
321 static char last
= '\0';
323 if ((c
!= '\n' && c
!= '\r') || (last
!= '\n' && last
!= '\r')) {
324 if (c
== '\n' || c
== '\r') {
325 int size
= linebuf
.size();
326 char *buffer
= new char[size
+ 1];
327 linebuf
.read(buffer
, size
);
329 DPRINTF(Terminal
, "%s\n", buffer
);
346 outfile
->write(&c
, 1);
348 DPRINTF(TerminalVerbose
, "out: \'%c\' %#02x\n",
349 isprint(c
) ? c
: ' ', (int)c
);
354 TerminalParams::create()
356 return new Terminal(this);