always initalize the size of a packet (forgotten on checkpoints
[gem5.git] / dev / simconsole.cc
1 /*
2 * Copyright (c) 2004 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
29 /* @file
30 * Implements the user interface to a serial console
31 */
32
33 #include <sys/ioctl.h>
34 #include <sys/termios.h>
35 #include <sys/types.h>
36 #include <errno.h>
37 #include <poll.h>
38 #include <unistd.h>
39
40 #include <iostream>
41 #include <fstream>
42 #include <sstream>
43 #include <string>
44
45 #include "base/misc.hh"
46 #include "base/socket.hh"
47 #include "base/trace.hh"
48 #include "dev/platform.hh"
49 #include "dev/simconsole.hh"
50 #include "dev/uart.hh"
51 #include "mem/functional_mem/memory_control.hh"
52 #include "sim/builder.hh"
53
54 using namespace std;
55
56 ////////////////////////////////////////////////////////////////////////
57 //
58 //
59
60 SimConsole::Event::Event(SimConsole *c, int fd, int e)
61 : PollEvent(fd, e), cons(c)
62 {
63 }
64
65 void
66 SimConsole::Event::process(int revent)
67 {
68 if (revent & POLLIN)
69 cons->data();
70 else if (revent & POLLNVAL)
71 cons->detach();
72 }
73
74 SimConsole::SimConsole(const string &name, std::ostream *os, int num)
75 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
76 listener(NULL), txbuf(16384), rxbuf(16384), outfile(os)
77 #if TRACING_ON == 1
78 , linebuf(16384)
79 #endif
80 {
81 if (outfile)
82 outfile->setf(ios::unitbuf);
83 }
84
85 SimConsole::~SimConsole()
86 {
87 close();
88 if (outfile)
89 closeOutputStream(outfile);
90 }
91
92 void
93 SimConsole::close()
94 {
95 if (in_fd != -1)
96 ::close(in_fd);
97
98 if (out_fd != in_fd && out_fd != -1)
99 ::close(out_fd);
100 }
101
102 void
103 SimConsole::attach(int in, int out, ConsoleListener *l)
104 {
105 in_fd = in;
106 out_fd = out;
107 listener = l;
108
109 event = new Event(this, in, POLLIN);
110 pollQueue.schedule(event);
111
112 stringstream stream;
113 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
114
115 // we need an actual carriage return followed by a newline for the
116 // terminal
117 stream << "\r\n";
118
119 write((const uint8_t *)stream.str().c_str(), stream.str().size());
120
121
122 DPRINTFN("attach console %d\n", number);
123
124 txbuf.readall(out);
125 }
126
127 void
128 SimConsole::detach()
129 {
130 close();
131 in_fd = -1;
132 out_fd = -1;
133
134 pollQueue.remove(event);
135
136 if (listener) {
137 listener->add(this);
138 listener = NULL;
139 }
140
141 DPRINTFN("detach console %d\n", number);
142 }
143
144 void
145 SimConsole::data()
146 {
147 uint8_t buf[1024];
148 int len;
149
150 len = read(buf, sizeof(buf));
151 if (len) {
152 rxbuf.write((char *)buf, len);
153 // Inform the UART there is data available
154 uart->dataAvailable();
155 }
156 }
157
158 size_t
159 SimConsole::read(uint8_t *buf, size_t len)
160 {
161 if (in_fd < 0)
162 panic("Console not properly attached.\n");
163
164 size_t ret;
165 do {
166 ret = ::read(in_fd, buf, len);
167 } while (ret == -1 && errno == EINTR);
168
169
170 if (ret < 0)
171 DPRINTFN("Read failed.\n");
172
173 if (ret <= 0) {
174 detach();
175 return 0;
176 }
177
178 return ret;
179 }
180
181 // Console output.
182 size_t
183 SimConsole::write(const uint8_t *buf, size_t len)
184 {
185 if (out_fd < 0)
186 panic("Console not properly attached.\n");
187
188 size_t ret;
189 for (;;) {
190 ret = ::write(out_fd, buf, len);
191
192 if (ret >= 0)
193 break;
194
195 if (errno != EINTR)
196 detach();
197 }
198
199 return ret;
200 }
201
202 #define MORE_PENDING (ULL(1) << 61)
203 #define RECEIVE_SUCCESS (ULL(0) << 62)
204 #define RECEIVE_NONE (ULL(2) << 62)
205 #define RECEIVE_ERROR (ULL(3) << 62)
206
207 bool
208 SimConsole::in(uint8_t &c)
209 {
210 bool empty, ret;
211
212 empty = rxbuf.empty();
213 ret = !empty;
214 if (!empty) {
215 rxbuf.read((char *)&c, 1);
216 empty = rxbuf.empty();
217 }
218
219 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d, return: %d\n",
220 isprint(c) ? c : ' ', c, !empty, ret);
221
222 return ret;
223 }
224
225 uint64_t
226 SimConsole::console_in()
227 {
228 uint8_t c;
229 uint64_t value;
230
231 if (in(c)) {
232 value = RECEIVE_SUCCESS | c;
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;
317
318 if (!output.isValid()) {
319 filename = getInstanceName();
320 } else if (append_name) {
321 filename = (string)output + "." + getInstanceName();
322 } else {
323 filename = output;
324 }
325
326 SimConsole *console = new SimConsole(getInstanceName(),
327 makeOutputStream(filename), number);
328 ((ConsoleListener *)listener)->add(console);
329
330 return console;
331 }
332
333 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
334
335 ////////////////////////////////////////////////////////////////////////
336 //
337 //
338
339 ConsoleListener::ConsoleListener(const string &name)
340 : SimObject(name), event(NULL)
341 {}
342
343 ConsoleListener::~ConsoleListener()
344 {
345 if (event)
346 delete event;
347 }
348
349 void
350 ConsoleListener::Event::process(int revent)
351 {
352 listener->accept();
353 }
354
355 ///////////////////////////////////////////////////////////////////////
356 // socket creation and console attach
357 //
358
359 void
360 ConsoleListener::listen(int port)
361 {
362 while (!listener.listen(port, true)) {
363 DPRINTF(Console,
364 ": can't bind address console port %d inuse PID %d\n",
365 port, getpid());
366 port++;
367 }
368
369 ccprintf(cerr, "Listening for console connection on port %d\n", port);
370
371 event = new Event(this, listener.getfd(), POLLIN);
372 pollQueue.schedule(event);
373 }
374
375 void
376 ConsoleListener::add(SimConsole *cons)
377 { ConsoleList.push_back(cons);}
378
379 void
380 ConsoleListener::accept()
381 {
382 if (!listener.islistening())
383 panic("%s: cannot accept a connection if not listening!", name());
384
385 int sfd = listener.accept(true);
386 if (sfd != -1) {
387 iter_t i = ConsoleList.begin();
388 iter_t end = ConsoleList.end();
389 if (i == end) {
390 close(sfd);
391 } else {
392 (*i)->attach(sfd, this);
393 i = ConsoleList.erase(i);
394 }
395 }
396 }
397
398 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
399
400 Param<int> port;
401
402 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
403
404 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
405
406 INIT_PARAM_DFLT(port, "listen port", 3456)
407
408 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
409
410 CREATE_SIM_OBJECT(ConsoleListener)
411 {
412 ConsoleListener *listener = new ConsoleListener(getInstanceName());
413 listener->listen(port);
414
415 return listener;
416 }
417
418 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)