2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
33 * Implements the user interface to a serial terminal
36 #include <sys/ioctl.h>
37 #include <sys/termios.h>
38 #include <sys/types.h>
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/terminal.hh"
54 #include "dev/uart.hh"
60 * Poll event for the listen socket
62 Terminal::ListenEvent::ListenEvent(Terminal
*t
, int fd
, int e
)
63 : PollEvent(fd
, e
), term(t
)
68 Terminal::ListenEvent::process(int revent
)
74 * Poll event for the data socket
76 Terminal::DataEvent::DataEvent(Terminal
*t
, int fd
, int e
)
77 : PollEvent(fd
, e
), term(t
)
82 Terminal::DataEvent::process(int revent
)
86 else if (revent
& POLLNVAL
)
93 Terminal::Terminal(const Params
*p
)
94 : SimObject(p
), listenEvent(NULL
), dataEvent(NULL
), number(p
->number
),
95 data_fd(-1), txbuf(16384), rxbuf(16384), outfile(NULL
)
101 outfile
= simout
.find(p
->name
);
102 outfile
->setf(ios::unitbuf
);
109 Terminal::~Terminal()
121 ///////////////////////////////////////////////////////////////////////
122 // socket creation and terminal attach
126 Terminal::listen(int port
)
128 while (!listener
.listen(port
, true)) {
130 ": can't bind address terminal port %d inuse PID %d\n",
136 p2
= name().rfind('.') - 1;
137 p1
= name().rfind('.', p2
);
138 ccprintf(cerr
, "Listening for %s connection on port %d\n",
139 name().substr(p1
+1,p2
-p1
), port
);
141 listenEvent
= new ListenEvent(this, listener
.getfd(), POLLIN
);
142 pollQueue
.schedule(listenEvent
);
148 if (!listener
.islistening())
149 panic("%s: cannot accept a connection if not listening!", name());
151 int fd
= listener
.accept(true);
153 char message
[] = "terminal already attached!\n";
154 ::write(fd
, message
, sizeof(message
));
160 dataEvent
= new DataEvent(this, data_fd
, POLLIN
);
161 pollQueue
.schedule(dataEvent
);
164 ccprintf(stream
, "==== m5 slave terminal: Terminal %d ====", number
);
166 // we need an actual carriage return followed by a newline for the
170 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
172 DPRINTFN("attach terminal %d\n", number
);
174 txbuf
.readall(data_fd
);
185 pollQueue
.remove(dataEvent
);
189 DPRINTFN("detach terminal %d\n", number
);
198 len
= read(buf
, sizeof(buf
));
200 rxbuf
.write((char *)buf
, len
);
201 // Inform the UART there is data available
202 uart
->dataAvailable();
207 Terminal::read(uint8_t *buf
, size_t len
)
210 panic("Terminal not properly attached.\n");
214 ret
= ::read(data_fd
, buf
, len
);
215 } while (ret
== -1 && errno
== EINTR
);
219 DPRINTFN("Read failed.\n");
231 Terminal::write(const uint8_t *buf
, size_t len
)
234 panic("Terminal not properly attached.\n");
238 ret
= ::write(data_fd
, buf
, len
);
250 #define MORE_PENDING (ULL(1) << 61)
251 #define RECEIVE_SUCCESS (ULL(0) << 62)
252 #define RECEIVE_NONE (ULL(2) << 62)
253 #define RECEIVE_ERROR (ULL(3) << 62)
261 empty
= rxbuf
.empty();
263 rxbuf
.read((char *)&c
, 1);
264 empty
= rxbuf
.empty();
267 DPRINTF(TerminalVerbose
, "in: \'%c\' %#02x more: %d\n",
268 isprint(c
) ? c
: ' ', c
, !empty
);
274 Terminal::console_in()
278 if (dataAvailable()) {
279 value
= RECEIVE_SUCCESS
| in();
281 value
|= MORE_PENDING
;
283 value
= RECEIVE_NONE
;
286 DPRINTF(TerminalVerbose
, "console_in: return: %#x\n", value
);
292 Terminal::out(char c
)
295 if (DTRACE(Terminal
)) {
296 static char last
= '\0';
298 if (c
!= '\n' && c
!= '\r' ||
299 last
!= '\n' && last
!= '\r') {
300 if (c
== '\n' || c
== '\r') {
301 int size
= linebuf
.size();
302 char *buffer
= new char[size
+ 1];
303 linebuf
.read(buffer
, size
);
305 DPRINTF(Terminal
, "%s\n", buffer
);
322 outfile
->write(&c
, 1);
324 DPRINTF(TerminalVerbose
, "out: \'%c\' %#02x\n",
325 isprint(c
) ? c
: ' ', (int)c
);
330 TerminalParams::create()
332 return new Terminal(this);