gem5  v21.1.0.2
terminal.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 ARM Limited
3  * All rights reserved
4  *
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.
13  *
14  * Copyright (c) 2001-2005 The Regents of The University of Michigan
15  * All rights reserved.
16  *
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.
27  *
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.
39  */
40 
41 /* @file
42  * Implements the user interface to a serial terminal
43  */
44 
45 #include <sys/ioctl.h>
46 
47 #if defined(__FreeBSD__)
48 #include <termios.h>
49 
50 #else
51 #include <sys/termios.h>
52 
53 #endif
54 #include "dev/serial/terminal.hh"
55 
56 #include <poll.h>
57 #include <unistd.h>
58 
59 #include <cctype>
60 #include <cerrno>
61 #include <fstream>
62 #include <iostream>
63 #include <sstream>
64 #include <string>
65 
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"
75 
76 namespace gem5
77 {
78 
79 /*
80  * Poll event for the listen socket
81  */
83  : PollEvent(fd, e), term(t)
84 {
85 }
86 
87 void
89 {
90  term->accept();
91 }
92 
93 /*
94  * Poll event for the data socket
95  */
97  : PollEvent(fd, e), term(t)
98 {
99 }
100 
101 void
103 {
104  // As a consequence of being called from the PollQueue, we might
105  // have been called from a different thread. Migrate to "our"
106  // thread.
107  EventQueue::ScopedMigration migrate(term->eventQueue());
108 
109  if (revent & POLLIN)
110  term->data();
111  else if (revent & POLLNVAL)
112  term->detach();
113 }
114 
115 /*
116  * Terminal code
117  */
119  : SerialDevice(p), listenEvent(NULL), dataEvent(NULL),
120  number(p.number), data_fd(-1), txbuf(16384), rxbuf(16384),
122 #if TRACING_ON == 1
123  , linebuf(16384)
124 #endif
125 {
126  if (outfile)
127  outfile->stream()->setf(std::ios::unitbuf);
128 
129  if (p.port)
130  listen(p.port);
131 }
132 
134 {
135  if (data_fd != -1)
136  ::close(data_fd);
137 
138  if (listenEvent)
139  delete listenEvent;
140 
141  if (dataEvent)
142  delete dataEvent;
143 }
144 
145 OutputStream *
146 Terminal::terminalDump(const TerminalParams &p)
147 {
148  switch (p.outfile) {
149  case TerminalDump::none:
150  return nullptr;
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);
157  default:
158  panic("Invalid option\n");
159  }
160 }
161 
163 // socket creation and terminal attach
164 //
165 
166 void
168 {
170  warn_once("Sockets disabled, not accepting terminal connections");
171  return;
172  }
173 
174  while (!listener.listen(port, true)) {
176  ": can't bind address terminal port %d inuse PID %d\n",
177  port, getpid());
178  port++;
179  }
180 
181  ccprintf(std::cerr, "%s: Listening for connections on port %d\n",
182  name(), port);
183 
184  listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
186 }
187 
188 void
190 {
191  if (!listener.islistening())
192  panic("%s: cannot accept a connection if not listening!", name());
193 
194  int fd = listener.accept(true);
195  if (data_fd != -1) {
196  char message[] = "terminal already attached!\n";
197  atomic_write(fd, message, sizeof(message));
198  ::close(fd);
199  return;
200  }
201 
202  data_fd = fd;
203  dataEvent = new DataEvent(this, data_fd, POLLIN);
205 
206  std::stringstream stream;
207  ccprintf(stream, "==== m5 terminal: Terminal %d ====", number);
208 
209  // we need an actual carriage return followed by a newline for the
210  // terminal
211  stream << "\r\n";
212 
213  write((const uint8_t *)stream.str().c_str(), stream.str().size());
214 
215  DPRINTFN("attach terminal %d\n", number);
216  char buf[1024];
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);
221  }
222 }
223 
224 void
226 {
227  if (data_fd != -1) {
228  ::close(data_fd);
229  data_fd = -1;
230  }
231 
233  delete dataEvent;
234  dataEvent = NULL;
235 
236  DPRINTFN("detach terminal %d\n", number);
237 }
238 
239 void
241 {
242  uint8_t buf[1024];
243  int len;
244 
245  len = read(buf, sizeof(buf));
246  if (len) {
247  rxbuf.write((char *)buf, len);
248  notifyInterface();
249  }
250 }
251 
252 size_t
253 Terminal::read(uint8_t *buf, size_t len)
254 {
255  if (data_fd < 0)
256  panic("Terminal not properly attached.\n");
257 
258  ssize_t ret;
259  do {
260  ret = ::read(data_fd, buf, len);
261  } while (ret == -1 && errno == EINTR);
262 
263 
264  if (ret < 0)
265  DPRINTFN("Read failed.\n");
266 
267  if (ret <= 0) {
268  detach();
269  return 0;
270  }
271 
272  return ret;
273 }
274 
275 // Terminal output.
276 size_t
277 Terminal::write(const uint8_t *buf, size_t len)
278 {
279  if (data_fd < 0)
280  panic("Terminal not properly attached.\n");
281 
282  ssize_t ret = atomic_write(data_fd, buf, len);
283  if (ret < len)
284  detach();
285 
286  return ret;
287 }
288 
289 #define MORE_PENDING (1ULL << 61)
290 #define RECEIVE_SUCCESS (0ULL << 62)
291 #define RECEIVE_NONE (2ULL << 62)
292 #define RECEIVE_ERROR (3ULL << 62)
293 
294 uint8_t
296 {
297  uint8_t c;
298 
299  assert(!rxbuf.empty());
300  rxbuf.read((char *)&c, 1);
301 
302  DPRINTF(TerminalVerbose, "in: \'%c\' %#02x more: %d\n",
303  isprint(c) ? c : ' ', c, !rxbuf.empty());
304 
305  return c;
306 }
307 
308 uint64_t
310 {
311  uint64_t value;
312 
313  if (dataAvailable()) {
314  value = RECEIVE_SUCCESS | readData();
315  if (!rxbuf.empty())
316  value |= MORE_PENDING;
317  } else {
318  value = RECEIVE_NONE;
319  }
320 
321  DPRINTF(TerminalVerbose, "console_in: return: %#x\n", value);
322 
323  return value;
324 }
325 
326 void
328 {
329 #if TRACING_ON == 1
330  if (debug::Terminal) {
331  static char last = '\0';
332 
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);
338  buffer[size] = '\0';
339  DPRINTF(Terminal, "%s\n", buffer);
340  delete [] buffer;
341  } else {
342  linebuf.write(&c, 1);
343  }
344  }
345 
346  last = c;
347  }
348 #endif
349 
350  txbuf.write(&c, 1);
351 
352  if (data_fd >= 0)
353  write(c);
354 
355  if (outfile)
356  outfile->stream()->put((char)c);
357 
358  DPRINTF(TerminalVerbose, "out: \'%c\' %#02x\n",
359  isprint(c) ? c : ' ', (int)c);
360 
361 }
362 
363 } // namespace gem5
gem5::SerialDevice
Base class for serial devices such as terminals.
Definition: serial.hh:94
gem5::Terminal::outfile
OutputStream * outfile
Definition: terminal.hh:112
gem5::Terminal::terminalDump
OutputStream * terminalDump(const TerminalParams &p)
Definition: terminal.cc:146
gem5::Terminal::data
void data()
Definition: terminal.cc:240
socket.hh
gem5::ArmISA::len
Bitfield< 18, 16 > len
Definition: misc_types.hh:444
gem5::Terminal::DataEvent::process
void process(int revent)
Definition: terminal.cc:102
gem5::ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:150
atomicio.hh
gem5::Terminal::read
void read(uint8_t &c)
Definition: terminal.hh:123
warn_once
#define warn_once(...)
Definition: logging.hh:249
RECEIVE_SUCCESS
#define RECEIVE_SUCCESS
Definition: terminal.cc:290
gem5::pollQueue
PollQueue pollQueue
Definition: pollevent.cc:55
gem5::Terminal::Terminal
Terminal(const Params &p)
Definition: terminal.cc:118
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:64
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::CircleBuf::peek
void peek(OutputIterator out, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition: circlebuf.hh:93
gem5::Terminal::listen
void listen(int port)
Definition: terminal.cc:167
gem5::Terminal::write
void write(uint8_t c)
Definition: terminal.hh:125
gem5::PollEvent
Definition: pollevent.hh:43
terminal.hh
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::Terminal::txbuf
CircleBuf< char > txbuf
Definition: terminal.hh:110
gem5::OutputDirectory::findOrCreate
OutputStream * findOrCreate(const std::string &name, bool binary=false)
Definition: output.cc:262
output.hh
gem5::PollQueue::remove
void remove(PollEvent *event)
Definition: pollevent.cc:139
gem5::statistics::none
const FlagsType none
Nothing extra to print.
Definition: info.hh:54
RECEIVE_NONE
#define RECEIVE_NONE
Definition: terminal.cc:291
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::CircleBuf::empty
bool empty() const
Definition: circlebuf.hh:71
gem5::Terminal::listener
ListenSocket listener
Definition: terminal.hh:104
gem5::Terminal::readData
uint8_t readData() override
Read a character from the device.
Definition: terminal.cc:295
gem5::OutputStream::stream
std::ostream * stream() const
Get the output underlying output stream.
Definition: output.hh:62
gem5::Terminal::listenEvent
ListenEvent * listenEvent
Definition: terminal.hh:78
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
uart.hh
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::Terminal
Definition: terminal.hh:64
gem5::Terminal::DataEvent
friend class DataEvent
Definition: terminal.hh:90
gem5::ListenSocket::listen
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:100
gem5::Terminal::accept
void accept()
Definition: terminal.cc:189
gem5::ListenSocket::getfd
int getfd() const
Definition: socket.hh:75
gem5::EventQueue::ScopedMigration
Definition: eventq.hh:672
gem5::CircleBuf::read
void read(OutputIterator out, size_t len)
Copy buffer contents and advance the read pointer.
Definition: circlebuf.hh:140
gem5::Terminal::dataEvent
DataEvent * dataEvent
Definition: terminal.hh:91
gem5::PollQueue::schedule
void schedule(PollEvent *event)
Definition: pollevent.cc:159
gem5::Terminal::dataAvailable
bool dataAvailable() const override
Check if there is pending data from the serial device.
Definition: terminal.hh:132
gem5::Terminal::ListenEvent
friend class ListenEvent
Definition: terminal.hh:77
gem5::ArmISA::t
Bitfield< 5 > t
Definition: misc_types.hh:70
gem5::ArmISA::c
Bitfield< 29 > c
Definition: misc_types.hh:53
platform.hh
gem5::Terminal::writeData
void writeData(uint8_t c) override
Transmit a character from the host interface to the device.
Definition: terminal.cc:327
gem5::ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:71
gem5::atomic_write
ssize_t atomic_write(int fd, const void *s, size_t n)
Definition: atomicio.cc:67
gem5::Terminal::console_in
uint64_t console_in()
Definition: terminal.cc:309
gem5::SerialDevice::notifyInterface
void notifyInterface()
Notify the host interface of pending data.
Definition: serial.cc:67
gem5::Terminal::detach
void detach()
Definition: terminal.cc:225
gem5::Terminal::number
int number
Definition: terminal.hh:94
gem5::Terminal::rxbuf
CircleBuf< char > rxbuf
Definition: terminal.hh:111
gem5::Terminal::data_fd
int data_fd
Definition: terminal.hh:95
gem5::CircleBuf::size
size_t size() const
Definition: circlebuf.hh:72
MORE_PENDING
#define MORE_PENDING
Definition: terminal.cc:289
gem5::Terminal::ListenEvent::process
void process(int revent)
Definition: terminal.cc:88
logging.hh
gem5::Terminal::ListenEvent::ListenEvent
ListenEvent(Terminal *t, int fd, int e)
Definition: terminal.cc:82
trace.hh
DPRINTFN
#define DPRINTFN(...)
Definition: trace.hh:214
gem5::ListenSocket::accept
virtual int accept(bool nodelay=false)
Definition: socket.cc:149
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::ListenSocket::islistening
bool islistening() const
Definition: socket.hh:76
gem5::Terminal::~Terminal
~Terminal()
Definition: terminal.cc:133
gem5::OutputStream
Definition: output.hh:56
gem5::Terminal::Params
TerminalParams Params
Definition: terminal.hh:98
gem5::Terminal::DataEvent::DataEvent
DataEvent(Terminal *t, int fd, int e)
Definition: terminal.cc:96
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177
gem5::CircleBuf::write
void write(InputIterator in, size_t len)
Add elements to the end of the ring buffers and advance.
Definition: circlebuf.hh:158

Generated on Tue Sep 21 2021 12:25:18 for gem5 by doxygen 1.8.17