sim: Move the draining interface into a separate base class
[gem5.git] / src / dev / terminal.cc
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 * Ali Saidi
30 */
31
32 /* @file
33 * Implements the user interface to a serial terminal
34 */
35
36 #include <sys/ioctl.h>
37 #include <sys/termios.h>
38 #include <poll.h>
39 #include <unistd.h>
40
41 #include <cctype>
42 #include <cerrno>
43 #include <fstream>
44 #include <iostream>
45 #include <sstream>
46 #include <string>
47
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"
58
59 using namespace std;
60
61
62 /*
63 * Poll event for the listen socket
64 */
65 Terminal::ListenEvent::ListenEvent(Terminal *t, int fd, int e)
66 : PollEvent(fd, e), term(t)
67 {
68 }
69
70 void
71 Terminal::ListenEvent::process(int revent)
72 {
73 term->accept();
74 }
75
76 /*
77 * Poll event for the data socket
78 */
79 Terminal::DataEvent::DataEvent(Terminal *t, int fd, int e)
80 : PollEvent(fd, e), term(t)
81 {
82 }
83
84 void
85 Terminal::DataEvent::process(int revent)
86 {
87 if (revent & POLLIN)
88 term->data();
89 else if (revent & POLLNVAL)
90 term->detach();
91 }
92
93 /*
94 * Terminal code
95 */
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)
99 #if TRACING_ON == 1
100 , linebuf(16384)
101 #endif
102 {
103 if (p->output) {
104 outfile = simout.find(p->name);
105 if (!outfile)
106 outfile = simout.create(p->name);
107
108 outfile->setf(ios::unitbuf);
109 }
110
111 if (p->port)
112 listen(p->port);
113 }
114
115 Terminal::~Terminal()
116 {
117 if (data_fd != -1)
118 ::close(data_fd);
119
120 if (listenEvent)
121 delete listenEvent;
122
123 if (dataEvent)
124 delete dataEvent;
125 }
126
127 ///////////////////////////////////////////////////////////////////////
128 // socket creation and terminal attach
129 //
130
131 void
132 Terminal::listen(int port)
133 {
134 if (ListenSocket::allDisabled()) {
135 warn_once("Sockets disabled, not accepting terminal connections");
136 return;
137 }
138
139 while (!listener.listen(port, true)) {
140 DPRINTF(Terminal,
141 ": can't bind address terminal port %d inuse PID %d\n",
142 port, getpid());
143 port++;
144 }
145
146 int p1, p2;
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);
151
152 listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
153 pollQueue.schedule(listenEvent);
154 }
155
156 void
157 Terminal::accept()
158 {
159 if (!listener.islistening())
160 panic("%s: cannot accept a connection if not listening!", name());
161
162 int fd = listener.accept(true);
163 if (data_fd != -1) {
164 char message[] = "terminal already attached!\n";
165 atomic_write(fd, message, sizeof(message));
166 ::close(fd);
167 return;
168 }
169
170 data_fd = fd;
171 dataEvent = new DataEvent(this, data_fd, POLLIN);
172 pollQueue.schedule(dataEvent);
173
174 stringstream stream;
175 ccprintf(stream, "==== m5 slave terminal: Terminal %d ====", number);
176
177 // we need an actual carriage return followed by a newline for the
178 // terminal
179 stream << "\r\n";
180
181 write((const uint8_t *)stream.str().c_str(), stream.str().size());
182
183 DPRINTFN("attach terminal %d\n", number);
184
185 txbuf.readall(data_fd);
186 }
187
188 void
189 Terminal::detach()
190 {
191 if (data_fd != -1) {
192 ::close(data_fd);
193 data_fd = -1;
194 }
195
196 pollQueue.remove(dataEvent);
197 delete dataEvent;
198 dataEvent = NULL;
199
200 DPRINTFN("detach terminal %d\n", number);
201 }
202
203 void
204 Terminal::data()
205 {
206 uint8_t buf[1024];
207 int len;
208
209 len = read(buf, sizeof(buf));
210 if (len) {
211 rxbuf.write((char *)buf, len);
212 // Inform the UART there is data available
213 uart->dataAvailable();
214 }
215 }
216
217 size_t
218 Terminal::read(uint8_t *buf, size_t len)
219 {
220 if (data_fd < 0)
221 panic("Terminal not properly attached.\n");
222
223 size_t ret;
224 do {
225 ret = ::read(data_fd, buf, len);
226 } while (ret == -1 && errno == EINTR);
227
228
229 if (ret < 0)
230 DPRINTFN("Read failed.\n");
231
232 if (ret <= 0) {
233 detach();
234 return 0;
235 }
236
237 return ret;
238 }
239
240 // Terminal output.
241 size_t
242 Terminal::write(const uint8_t *buf, size_t len)
243 {
244 if (data_fd < 0)
245 panic("Terminal not properly attached.\n");
246
247 ssize_t ret = atomic_write(data_fd, buf, len);
248 if (ret < len)
249 detach();
250
251 return ret;
252 }
253
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)
258
259 uint8_t
260 Terminal::in()
261 {
262 uint8_t c;
263
264 assert(!rxbuf.empty());
265 rxbuf.read((char *)&c, 1);
266
267 DPRINTF(TerminalVerbose, "in: \'%c\' %#02x more: %d\n",
268 isprint(c) ? c : ' ', c, !rxbuf.empty());
269
270 return c;
271 }
272
273 uint64_t
274 Terminal::console_in()
275 {
276 uint64_t value;
277
278 if (dataAvailable()) {
279 value = RECEIVE_SUCCESS | in();
280 if (!rxbuf.empty())
281 value |= MORE_PENDING;
282 } else {
283 value = RECEIVE_NONE;
284 }
285
286 DPRINTF(TerminalVerbose, "console_in: return: %#x\n", value);
287
288 return value;
289 }
290
291 void
292 Terminal::out(char c)
293 {
294 #if TRACING_ON == 1
295 if (DTRACE(Terminal)) {
296 static char last = '\0';
297
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);
303 buffer[size] = '\0';
304 DPRINTF(Terminal, "%s\n", buffer);
305 delete [] buffer;
306 } else {
307 linebuf.write(c);
308 }
309 }
310
311 last = c;
312 }
313 #endif
314
315 txbuf.write(c);
316
317 if (data_fd >= 0)
318 write(c);
319
320 if (outfile)
321 outfile->write(&c, 1);
322
323 DPRINTF(TerminalVerbose, "out: \'%c\' %#02x\n",
324 isprint(c) ? c : ' ', (int)c);
325
326 }
327
328 Terminal *
329 TerminalParams::create()
330 {
331 return new Terminal(this);
332 }