4 * User Console Definitions
8 #include <sys/termios.h>
19 #include "base/misc.hh"
20 #include "targetarch/ev5.hh"
22 #include "dev/console.hh"
23 #include "base/socket.hh"
24 #include "base/trace.hh"
25 #include "mem/functional_mem/memory_control.hh"
29 // check whether an int is pending
31 IntPending(int status
, int mask
)
32 { return (status
& mask
) != 0; }
35 IntTransition(int ostaus
, int omask
, int nstatus
, int nmask
)
36 { return IntPending(ostaus
, omask
) != IntPending(nstatus
, nmask
); }
38 ////////////////////////////////////////////////////////////////////////
42 SimConsole::Event::Event(SimConsole
*c
, int fd
, int e
)
43 : PollEvent(fd
, e
), cons(c
)
48 SimConsole::Event::process(int revent
)
52 else if (revent
& POLLNVAL
)
56 SimConsole::SimConsole(const string
&name
, const string
&file
, int num
)
57 : SimObject(name
), event(NULL
), number(num
), in_fd(-1), out_fd(-1),
58 listener(NULL
), txbuf(16384), rxbuf(16384), outfile(NULL
),
59 intr_status(0), intr_enable(0), intr(NULL
)
62 outfile
= new ofstream(file
.c_str());
65 outfile
->setf(ios::unitbuf
);
68 SimConsole::~SimConsole()
82 if (out_fd
!= in_fd
&& out_fd
!= -1)
87 SimConsole::attach(int in
, int out
, ConsoleListener
*l
)
93 event
= new Event(this, in
, POLLIN
);
94 pollQueue
.schedule(event
);
97 ccprintf(stream
, "==== Simplescalar slave console: Console %d ====",
99 // we need an actual carriage return followed by a newline for the
103 write((const uint8_t *)stream
.str().c_str(), stream
.str().size());
106 DPRINTFN("attach console %d\n", number
);
118 pollQueue
.remove(event
);
125 DPRINTFN("detach console %d\n", number
);
134 len
= read(buf
, sizeof(buf
));
136 rxbuf
.write((char *)buf
, len
);
137 raiseInt(ReceiveInterrupt
);
142 SimConsole::read(uint8_t *buf
, size_t len
)
145 panic("SimConsole(read): Console not properly attached.\n");
149 ret
= ::read(in_fd
, buf
, len
);
150 } while (ret
== -1 && errno
== EINTR
);
154 DPRINTFN("SimConsole(read): Read failed.\n");
166 SimConsole::write(const uint8_t *buf
, size_t len
)
169 panic("SimConsole(write): Console not properly attached.\n");
173 ret
= ::write(out_fd
, buf
, len
);
185 ///////////////////////////////////////////////////////////////////////
186 // ConfigureTerm turns off all character processing by the host OS so
187 // the launched OS can control it.
189 // We ignore anything except stdin; the sconsole program runs this
190 // same code on the ttys for the slave consoles before connecting.
193 SimConsole::configTerm()
197 if (isatty(out_fd
)) {
198 if (tcgetattr(out_fd
, &ios
) < 0) {
199 panic( "tcgetattr\n");
201 ios
.c_iflag
&= ~(ISTRIP
|ICRNL
|IGNCR
|ICRNL
|IXOFF
|IXON
);
202 ios
.c_oflag
&= ~(OPOST
);
203 ios
.c_oflag
&= (ONLCR
);
204 ios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
207 if (tcsetattr(out_fd
, TCSANOW
, &ios
) < 0) {
208 panic( "tcsetattr\n");
214 ///////////////////////////////////////////////////////////////////////
218 ///////////////////////////////////////////////////////////////////////
221 // Returns -1 if there is no character pending, otherwise returns the
222 // char. Calling this function clears the input int (if no further
223 // chars are pending).
229 clearInt(ReceiveInterrupt
);
236 DPRINTF(Console
, "in: \'%c\' %#02x status: %#x\n",
237 isprint(c
) ? c
: ' ', c
, intr_status
);
242 ///////////////////////////////////////////////////////////////////////
245 // NOTE: this very rudimentary device generates a TX int as soon as
246 // a character is output, since it has unlimited TX buffer capacity.
249 // Uses sim_console_out to perform functionality similar to 'write'
251 SimConsole::out(char c
)
259 outfile
->write(&c
, 1);
261 raiseInt(TransmitInterrupt
);
263 DPRINTF(Console
, "out: \'%c\' %#02x status: %#x\n",
264 isprint(c
) ? c
: ' ', (int)c
, intr_status
);
267 // Simple console output used by Alpha firmware (not by the OS) -
268 // outputs the character to console n, and doesn't raise any
271 SimConsole::simple(char c
)
279 outfile
->write(&c
, 1);
281 DPRINTF(Console
, "simple char: \'%c\' %#02x\n",
282 isprint(c
) ? c
: ' ', (int)c
);
285 // Read the current interrupt status of this console.
287 SimConsole::intStatus()
290 DPRINTF(Console
, "interrupt %d status: %#x\n",
291 number
, intr_status
);
298 SimConsole::clearInt(int i
)
300 int old_status
= intr_status
;
302 if (IntTransition(old_status
, intr_enable
, intr_status
, intr_enable
) &&
304 intr
->clear(TheISA::INTLEVEL_IRQ0
);
309 SimConsole::raiseInt(int i
)
311 int old
= intr_status
;
313 if (IntTransition(old
, intr_enable
, intr_status
, intr_enable
) && intr
)
314 intr
->post(TheISA::INTLEVEL_IRQ0
);
318 SimConsole::initInt(IntrControl
*i
)
321 panic("Console has already been initialized.");
323 // note: intr_status and intr_enable will normally be 0, since
324 // cs is statically allocated. When restoring from a checkpoint,
325 // these fields will be set, so don't touch them here.
326 intr
= i
; // interrupt handler
329 // Set the interrupt enable bits.
331 SimConsole::setInt(int bits
)
335 if (bits
& ~(TransmitInterrupt
| ReceiveInterrupt
))
336 panic("An interrupt was not set!");
338 old_enable
= intr_enable
;
341 if (IntTransition(intr_status
, old_enable
, intr_status
, intr_enable
) &&
343 if (IntPending(intr_status
, intr_enable
))
344 intr
->post(TheISA::INTLEVEL_IRQ0
);
346 intr
->clear(TheISA::INTLEVEL_IRQ0
);
352 SimConsole::serialize()
354 panic("Unimplemented");
358 SimConsole::unserialize(IniFile
&db
, const std::string
&category
,
361 panic("Unimplemented");
365 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
367 SimObjectParam
<ConsoleListener
*> listener
;
368 SimObjectParam
<IntrControl
*> intr_control
;
369 Param
<string
> output
;
372 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole
)
374 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole
)
376 INIT_PARAM(listener
, "console listener"),
377 INIT_PARAM(intr_control
, "interrupt controller"),
378 INIT_PARAM_DFLT(output
, "file to dump output to", ""),
379 INIT_PARAM_DFLT(number
, "console number", 0)
381 END_INIT_SIM_OBJECT_PARAMS(SimConsole
)
383 CREATE_SIM_OBJECT(SimConsole
)
385 SimConsole
*console
= new SimConsole(getInstanceName(), output
, number
);
386 ((ConsoleListener
*)listener
)->add(console
);
387 ((SimConsole
*)console
)->initInt(intr_control
);
388 ((SimConsole
*)console
)->setInt(SimConsole::TransmitInterrupt
|
389 SimConsole::ReceiveInterrupt
);
394 REGISTER_SIM_OBJECT("SimConsole", SimConsole
)
396 ////////////////////////////////////////////////////////////////////////
400 ConsoleListener::ConsoleListener(const string
&name
)
401 : SimObject(name
), event(NULL
)
404 ConsoleListener::~ConsoleListener()
411 ConsoleListener::Event::process(int revent
)
416 ///////////////////////////////////////////////////////////////////////
417 // socket creation and console attach
421 ConsoleListener::listen(int port
)
423 while (!listener
.listen(port
, true)) {
424 DPRINTF(Console
, ": can't bind address console port %d inuse PID %d\n",
429 cerr
<< "Listening for console connection on port " << port
<< endl
;
430 event
= new Event(this, listener
.getfd(), POLLIN
);
431 pollQueue
.schedule(event
);
435 ConsoleListener::add(SimConsole
*cons
)
436 { ConsoleList
.push_back(cons
);}
439 ConsoleListener::accept()
441 if (!listener
.islistening())
442 panic("%s: cannot accept a connection if we're not listening!",
445 int sfd
= listener
.accept(true);
447 iter_t i
= ConsoleList
.begin();
448 iter_t end
= ConsoleList
.end();
452 (*i
)->attach(sfd
, this);
453 i
= ConsoleList
.erase(i
);
458 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
462 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener
)
464 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
466 INIT_PARAM_DFLT(port
, "listen port", 3456)
468 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener
)
470 CREATE_SIM_OBJECT(ConsoleListener
)
472 ConsoleListener
*listener
= new ConsoleListener(getInstanceName());
473 listener
->listen(port
);
478 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener
)