2 * Copyright (c) 2019 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Implements the user interface to a serial terminal
45 #include <sys/ioctl.h>
47 #if defined(__FreeBSD__)
51 #include <sys/termios.h>
54 #include "dev/serial/terminal.hh"
66 #include "base/atomicio.hh"
67 #include "base/logging.hh"
68 #include "base/output.hh"
69 #include "base/socket.hh"
70 #include "base/trace.hh"
71 #include "debug/Terminal.hh"
72 #include "debug/TerminalVerbose.hh"
73 #include "dev/platform.hh"
74 #include "dev/serial/uart.hh"
80 * Poll event for the listen socket
82 Terminal::ListenEvent::ListenEvent(Terminal
*t
, int fd
, int e
)
83 : PollEvent(fd
, e
), term(t
)
88 Terminal::ListenEvent::process(int revent
)
94 * Poll event for the data socket
96 Terminal::DataEvent::DataEvent(Terminal
*t
, int fd
, int e
)
97 : PollEvent(fd
, e
), term(t
)
102 Terminal::DataEvent::process(int revent
)
104 // As a consequence of being called from the PollQueue, we might
105 // have been called from a different thread. Migrate to "our"
107 EventQueue::ScopedMigration
migrate(term
->eventQueue());
111 else if (revent
& POLLNVAL
)
118 Terminal::Terminal(const Params
*p
)
119 : SerialDevice(p
), listenEvent(NULL
), dataEvent(NULL
),
120 number(p
->number
), data_fd(-1), txbuf(16384), rxbuf(16384),
121 outfile(terminalDump(p
))
127 outfile
->stream()->setf(ios::unitbuf
);
133 Terminal::~Terminal()
146 Terminal::terminalDump(const TerminalParams
* p
)
148 switch (p
->outfile
) {
149 case TerminalDump::none
:
151 case TerminalDump::stdoutput
:
152 return simout
.findOrCreate("stdout");
153 case TerminalDump::stderror
:
154 return simout
.findOrCreate("stderr");
155 case TerminalDump::file
:
156 return simout
.findOrCreate(p
->name
);
158 panic("Invalid option\n");
162 ///////////////////////////////////////////////////////////////////////
163 // socket creation and terminal attach
167 Terminal::listen(int port
)
169 if (ListenSocket::allDisabled()) {
170 warn_once("Sockets disabled, not accepting terminal connections");
174 while (!listener
.listen(port
, true)) {
176 ": can't bind address terminal port %d inuse PID %d\n",
181 ccprintf(cerr
, "%s: Listening for connections on port %d\n",
184 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
185 pollQueue
.schedule(listenEvent
);
191 if (!listener
.islistening())
192 panic("%s: cannot accept a connection if not listening!", name());
194 int fd
= listener
.accept(true);
196 char message
[] = "terminal already attached!\n";
197 atomic_write(fd
, message
, sizeof(message
));
203 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
204 pollQueue
.schedule(dataEvent
);
207 ccprintf(stream
, "==== m5 terminal: Terminal %d ====", number
);
209 // we need an actual carriage return followed by a newline for the
213 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
215 DPRINTFN("attach terminal %d\n", number
);
217 for (size_t i
= 0; i
< txbuf
.size(); i
+= sizeof(buf
)) {
218 const size_t chunk_len(std::min(txbuf
.size() - i
, sizeof(buf
)));
219 txbuf
.peek(buf
, i
, chunk_len
);
220 write((const uint8_t *)buf
, chunk_len
);
232 pollQueue
.remove(dataEvent
);
236 DPRINTFN("detach terminal %d\n", number
);
245 len
= read(buf
, sizeof(buf
));
247 rxbuf
.write((char *)buf
, len
);
253 Terminal::read(uint8_t *buf
, size_t len
)
256 panic("Terminal not properly attached.\n");
260 ret
= ::read(data_fd
, buf
, len
);
261 } while (ret
== -1 && errno
== EINTR
);
265 DPRINTFN("Read failed.\n");
277 Terminal::write(const uint8_t *buf
, size_t len
)
280 panic("Terminal not properly attached.\n");
282 ssize_t ret
= atomic_write(data_fd
, buf
, len
);
289 #define MORE_PENDING (ULL(1) << 61)
290 #define RECEIVE_SUCCESS (ULL(0) << 62)
291 #define RECEIVE_NONE (ULL(2) << 62)
292 #define RECEIVE_ERROR (ULL(3) << 62)
299 assert(!rxbuf
.empty());
300 rxbuf
.read((char *)&c
, 1);
302 DPRINTF(TerminalVerbose
, "in: \'%c\' %#02x more: %d\n",
303 isprint(c
) ? c
: ' ', c
, !rxbuf
.empty());
309 Terminal::console_in()
313 if (dataAvailable()) {
314 value
= RECEIVE_SUCCESS
| readData();
316 value
|= MORE_PENDING
;
318 value
= RECEIVE_NONE
;
321 DPRINTF(TerminalVerbose
, "console_in: return: %#x\n", value
);
327 Terminal::writeData(uint8_t c
)
330 if (DTRACE(Terminal
)) {
331 static char last
= '\0';
333 if ((c
!= '\n' && c
!= '\r') || (last
!= '\n' && last
!= '\r')) {
334 if (c
== '\n' || c
== '\r') {
335 int size
= linebuf
.size();
336 char *buffer
= new char[size
+ 1];
337 linebuf
.read(buffer
, size
);
339 DPRINTF(Terminal
, "%s\n", buffer
);
342 linebuf
.write(&c
, 1);
356 outfile
->stream()->put((char)c
);
358 DPRINTF(TerminalVerbose
, "out: \'%c\' %#02x\n",
359 isprint(c
) ? c
: ' ', (int)c
);
364 TerminalParams::create()
366 return new Terminal(this);