Merge ktlim@zamp:/z/ktlim2/clean/m5-o3
[gem5.git] / src / dev / simconsole.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 console
34 */
35
36 #include <sys/ioctl.h>
37 #include <sys/termios.h>
38 #include <sys/types.h>
39 #include <errno.h>
40 #include <poll.h>
41 #include <unistd.h>
42
43 #include <iostream>
44 #include <fstream>
45 #include <sstream>
46 #include <string>
47
48 #include "base/misc.hh"
49 #include "base/output.hh"
50 #include "base/socket.hh"
51 #include "base/trace.hh"
52 #include "dev/platform.hh"
53 #include "dev/simconsole.hh"
54 #include "dev/uart.hh"
55 #include "sim/builder.hh"
56
57 using namespace std;
58
59 ////////////////////////////////////////////////////////////////////////
60 //
61 //
62
63 SimConsole::Event::Event(SimConsole *c, int fd, int e)
64 : PollEvent(fd, e), cons(c)
65 {
66 }
67
68 void
69 SimConsole::Event::process(int revent)
70 {
71 if (revent & POLLIN)
72 cons->data();
73 else if (revent & POLLNVAL)
74 cons->detach();
75 }
76
77 SimConsole::SimConsole(const string &name, ostream *os, int num)
78 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
79 listener(NULL), txbuf(16384), rxbuf(16384), outfile(os)
80 #if TRACING_ON == 1
81 , linebuf(16384)
82 #endif
83 {
84 if (outfile)
85 outfile->setf(ios::unitbuf);
86 }
87
88 SimConsole::~SimConsole()
89 {
90 close();
91 }
92
93 void
94 SimConsole::close()
95 {
96 if (in_fd != -1)
97 ::close(in_fd);
98
99 if (out_fd != in_fd && out_fd != -1)
100 ::close(out_fd);
101 }
102
103 void
104 SimConsole::attach(int in, int out, ConsoleListener *l)
105 {
106 in_fd = in;
107 out_fd = out;
108 listener = l;
109
110 event = new Event(this, in, POLLIN);
111 pollQueue.schedule(event);
112
113 stringstream stream;
114 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
115
116 // we need an actual carriage return followed by a newline for the
117 // terminal
118 stream << "\r\n";
119
120 write((const uint8_t *)stream.str().c_str(), stream.str().size());
121
122
123 DPRINTFN("attach console %d\n", number);
124
125 txbuf.readall(out);
126 }
127
128 void
129 SimConsole::detach()
130 {
131 close();
132 in_fd = -1;
133 out_fd = -1;
134
135 pollQueue.remove(event);
136
137 if (listener) {
138 listener->add(this);
139 listener = NULL;
140 }
141
142 DPRINTFN("detach console %d\n", number);
143 }
144
145 void
146 SimConsole::data()
147 {
148 uint8_t buf[1024];
149 int len;
150
151 len = read(buf, sizeof(buf));
152 if (len) {
153 rxbuf.write((char *)buf, len);
154 // Inform the UART there is data available
155 uart->dataAvailable();
156 }
157 }
158
159 size_t
160 SimConsole::read(uint8_t *buf, size_t len)
161 {
162 if (in_fd < 0)
163 panic("Console not properly attached.\n");
164
165 size_t ret;
166 do {
167 ret = ::read(in_fd, buf, len);
168 } while (ret == -1 && errno == EINTR);
169
170
171 if (ret < 0)
172 DPRINTFN("Read failed.\n");
173
174 if (ret <= 0) {
175 detach();
176 return 0;
177 }
178
179 return ret;
180 }
181
182 // Console output.
183 size_t
184 SimConsole::write(const uint8_t *buf, size_t len)
185 {
186 if (out_fd < 0)
187 panic("Console not properly attached.\n");
188
189 size_t ret;
190 for (;;) {
191 ret = ::write(out_fd, buf, len);
192
193 if (ret >= 0)
194 break;
195
196 if (errno != EINTR)
197 detach();
198 }
199
200 return ret;
201 }
202
203 #define MORE_PENDING (ULL(1) << 61)
204 #define RECEIVE_SUCCESS (ULL(0) << 62)
205 #define RECEIVE_NONE (ULL(2) << 62)
206 #define RECEIVE_ERROR (ULL(3) << 62)
207
208 uint8_t
209 SimConsole::in()
210 {
211 bool empty;
212 uint8_t c;
213
214 empty = rxbuf.empty();
215 assert(!empty);
216 rxbuf.read((char *)&c, 1);
217 empty = rxbuf.empty();
218
219
220 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d\n",
221 isprint(c) ? c : ' ', c, !empty);
222
223 return c;
224 }
225
226 uint64_t
227 SimConsole::console_in()
228 {
229 uint64_t value;
230
231 if (dataAvailable()) {
232 value = RECEIVE_SUCCESS | in();
233 if (!rxbuf.empty())
234 value |= MORE_PENDING;
235 } else {
236 value = RECEIVE_NONE;
237 }
238
239 DPRINTF(ConsoleVerbose, "console_in: return: %#x\n", value);
240
241 return value;
242 }
243
244 void
245 SimConsole::out(char c)
246 {
247 #if TRACING_ON == 1
248 if (DTRACE(Console)) {
249 static char last = '\0';
250
251 if (c != '\n' && c != '\r' ||
252 last != '\n' && last != '\r') {
253 if (c == '\n' || c == '\r') {
254 int size = linebuf.size();
255 char *buffer = new char[size + 1];
256 linebuf.read(buffer, size);
257 buffer[size] = '\0';
258 DPRINTF(Console, "%s\n", buffer);
259 delete [] buffer;
260 } else {
261 linebuf.write(c);
262 }
263 }
264
265 last = c;
266 }
267 #endif
268
269 txbuf.write(c);
270
271 if (out_fd >= 0)
272 write(c);
273
274 if (outfile)
275 outfile->write(&c, 1);
276
277 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x\n",
278 isprint(c) ? c : ' ', (int)c);
279
280 }
281
282
283 void
284 SimConsole::serialize(ostream &os)
285 {
286 }
287
288 void
289 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
290 {
291 }
292
293
294 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
295
296 SimObjectParam<ConsoleListener *> listener;
297 SimObjectParam<IntrControl *> intr_control;
298 Param<string> output;
299 Param<bool> append_name;
300 Param<int> number;
301
302 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
303
304 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
305
306 INIT_PARAM(listener, "console listener"),
307 INIT_PARAM(intr_control, "interrupt controller"),
308 INIT_PARAM(output, "file to dump output to"),
309 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
310 INIT_PARAM_DFLT(number, "console number", 0)
311
312 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
313
314 CREATE_SIM_OBJECT(SimConsole)
315 {
316 string filename = output;
317 ostream *stream = NULL;
318
319 if (!filename.empty()) {
320 if (append_name)
321 filename += "." + getInstanceName();
322 stream = simout.find(filename);
323 }
324
325 SimConsole *console = new SimConsole(getInstanceName(), stream, number);
326 ((ConsoleListener *)listener)->add(console);
327
328 return console;
329 }
330
331 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
332
333 ////////////////////////////////////////////////////////////////////////
334 //
335 //
336
337 ConsoleListener::ConsoleListener(const string &name)
338 : SimObject(name), event(NULL)
339 {}
340
341 ConsoleListener::~ConsoleListener()
342 {
343 if (event)
344 delete event;
345 }
346
347 void
348 ConsoleListener::Event::process(int revent)
349 {
350 listener->accept();
351 }
352
353 ///////////////////////////////////////////////////////////////////////
354 // socket creation and console attach
355 //
356
357 void
358 ConsoleListener::listen(int port)
359 {
360 while (!listener.listen(port, true)) {
361 DPRINTF(Console,
362 ": can't bind address console port %d inuse PID %d\n",
363 port, getpid());
364 port++;
365 }
366
367 ccprintf(cerr, "Listening for console connection on port %d\n", port);
368
369 event = new Event(this, listener.getfd(), POLLIN);
370 pollQueue.schedule(event);
371 }
372
373 void
374 ConsoleListener::add(SimConsole *cons)
375 { ConsoleList.push_back(cons);}
376
377 void
378 ConsoleListener::accept()
379 {
380 if (!listener.islistening())
381 panic("%s: cannot accept a connection if not listening!", name());
382
383 int sfd = listener.accept(true);
384 if (sfd != -1) {
385 iter_t i = ConsoleList.begin();
386 iter_t end = ConsoleList.end();
387 if (i == end) {
388 close(sfd);
389 } else {
390 (*i)->attach(sfd, this);
391 i = ConsoleList.erase(i);
392 }
393 }
394 }
395
396 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
397
398 Param<int> port;
399
400 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
401
402 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
403
404 INIT_PARAM_DFLT(port, "listen port", 3456)
405
406 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
407
408 CREATE_SIM_OBJECT(ConsoleListener)
409 {
410 ConsoleListener *listener = new ConsoleListener(getInstanceName());
411 listener->listen(port);
412
413 return listener;
414 }
415
416 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)