Add v{add,sub}cuw instructions.
[qemu/qemu-JZ.git] / qemu-char.c
blob27cdd325a289b48380da0c4244c585b3708b6193
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "net.h"
26 #include "console.h"
27 #include "sysemu.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "block.h"
31 #include "hw/usb.h"
32 #include "hw/baum.h"
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <time.h>
38 #include <errno.h>
39 #include <sys/time.h>
40 #include <zlib.h>
42 #ifndef _WIN32
43 #include <sys/times.h>
44 #include <sys/wait.h>
45 #include <termios.h>
46 #include <sys/mman.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <net/if.h>
52 #ifdef __NetBSD__
53 #include <net/if_tap.h>
54 #endif
55 #ifdef __linux__
56 #include <linux/if_tun.h>
57 #endif
58 #include <arpa/inet.h>
59 #include <dirent.h>
60 #include <netdb.h>
61 #include <sys/select.h>
62 #ifdef _BSD
63 #include <sys/stat.h>
64 #ifdef __FreeBSD__
65 #include <libutil.h>
66 #include <dev/ppbus/ppi.h>
67 #include <dev/ppbus/ppbconf.h>
68 #else
69 #include <util.h>
70 #endif
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
73 #else
74 #ifdef __linux__
75 #include <pty.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
79 #endif
80 #ifdef __sun__
81 #include <sys/stat.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
91 #include <net/if.h>
92 #include <syslog.h>
93 #include <stropts.h>
94 #endif
95 #endif
96 #endif
98 #include "qemu_socket.h"
100 /***********************************************************/
101 /* character device */
103 static void qemu_chr_event(CharDriverState *s, int event)
105 if (!s->chr_event)
106 return;
107 s->chr_event(s->handler_opaque, event);
110 static void qemu_chr_reset_bh(void *opaque)
112 CharDriverState *s = opaque;
113 qemu_chr_event(s, CHR_EVENT_RESET);
114 qemu_bh_delete(s->bh);
115 s->bh = NULL;
118 void qemu_chr_reset(CharDriverState *s)
120 if (s->bh == NULL) {
121 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
122 qemu_bh_schedule(s->bh);
126 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
128 return s->chr_write(s, buf, len);
131 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
133 if (!s->chr_ioctl)
134 return -ENOTSUP;
135 return s->chr_ioctl(s, cmd, arg);
138 int qemu_chr_can_read(CharDriverState *s)
140 if (!s->chr_can_read)
141 return 0;
142 return s->chr_can_read(s->handler_opaque);
145 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
147 s->chr_read(s->handler_opaque, buf, len);
150 void qemu_chr_accept_input(CharDriverState *s)
152 if (s->chr_accept_input)
153 s->chr_accept_input(s);
156 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
158 char buf[4096];
159 va_list ap;
160 va_start(ap, fmt);
161 vsnprintf(buf, sizeof(buf), fmt, ap);
162 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
163 va_end(ap);
166 void qemu_chr_send_event(CharDriverState *s, int event)
168 if (s->chr_send_event)
169 s->chr_send_event(s, event);
172 void qemu_chr_add_handlers(CharDriverState *s,
173 IOCanRWHandler *fd_can_read,
174 IOReadHandler *fd_read,
175 IOEventHandler *fd_event,
176 void *opaque)
178 s->chr_can_read = fd_can_read;
179 s->chr_read = fd_read;
180 s->chr_event = fd_event;
181 s->handler_opaque = opaque;
182 if (s->chr_update_read_handler)
183 s->chr_update_read_handler(s);
186 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
188 return len;
191 static CharDriverState *qemu_chr_open_null(void)
193 CharDriverState *chr;
195 chr = qemu_mallocz(sizeof(CharDriverState));
196 if (!chr)
197 return NULL;
198 chr->chr_write = null_chr_write;
199 return chr;
202 /* MUX driver for serial I/O splitting */
203 static int term_timestamps;
204 static int64_t term_timestamps_start;
205 #define MAX_MUX 4
206 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
207 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
208 typedef struct {
209 IOCanRWHandler *chr_can_read[MAX_MUX];
210 IOReadHandler *chr_read[MAX_MUX];
211 IOEventHandler *chr_event[MAX_MUX];
212 void *ext_opaque[MAX_MUX];
213 CharDriverState *drv;
214 unsigned char buffer[MUX_BUFFER_SIZE];
215 int prod;
216 int cons;
217 int mux_cnt;
218 int term_got_escape;
219 int max_size;
220 } MuxDriver;
223 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
225 MuxDriver *d = chr->opaque;
226 int ret;
227 if (!term_timestamps) {
228 ret = d->drv->chr_write(d->drv, buf, len);
229 } else {
230 int i;
232 ret = 0;
233 for(i = 0; i < len; i++) {
234 ret += d->drv->chr_write(d->drv, buf+i, 1);
235 if (buf[i] == '\n') {
236 char buf1[64];
237 int64_t ti;
238 int secs;
240 ti = qemu_get_clock(rt_clock);
241 if (term_timestamps_start == -1)
242 term_timestamps_start = ti;
243 ti -= term_timestamps_start;
244 secs = ti / 1000000000;
245 snprintf(buf1, sizeof(buf1),
246 "[%02d:%02d:%02d.%03d] ",
247 secs / 3600,
248 (secs / 60) % 60,
249 secs % 60,
250 (int)((ti / 1000000) % 1000));
251 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
255 return ret;
258 static const char * const mux_help[] = {
259 "% h print this help\n\r",
260 "% x exit emulator\n\r",
261 "% s save disk data back to file (if -snapshot)\n\r",
262 "% t toggle console timestamps\n\r"
263 "% b send break (magic sysrq)\n\r",
264 "% c switch between console and monitor\n\r",
265 "% % sends %\n\r",
266 NULL
269 int term_escape_char = 0x01; /* ctrl-a is used for escape */
270 static void mux_print_help(CharDriverState *chr)
272 int i, j;
273 char ebuf[15] = "Escape-Char";
274 char cbuf[50] = "\n\r";
276 if (term_escape_char > 0 && term_escape_char < 26) {
277 snprintf(cbuf, sizeof(cbuf), "\n\r");
278 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
279 } else {
280 snprintf(cbuf, sizeof(cbuf),
281 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
282 term_escape_char);
284 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
285 for (i = 0; mux_help[i] != NULL; i++) {
286 for (j=0; mux_help[i][j] != '\0'; j++) {
287 if (mux_help[i][j] == '%')
288 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
289 else
290 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
295 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
297 if (d->term_got_escape) {
298 d->term_got_escape = 0;
299 if (ch == term_escape_char)
300 goto send_char;
301 switch(ch) {
302 case '?':
303 case 'h':
304 mux_print_help(chr);
305 break;
306 case 'x':
308 const char *term = "QEMU: Terminated\n\r";
309 chr->chr_write(chr,(uint8_t *)term,strlen(term));
310 exit(0);
311 break;
313 case 's':
315 int i;
316 for (i = 0; i < nb_drives; i++) {
317 bdrv_commit(drives_table[i].bdrv);
320 break;
321 case 'b':
322 qemu_chr_event(chr, CHR_EVENT_BREAK);
323 break;
324 case 'c':
325 /* Switch to the next registered device */
326 chr->focus++;
327 if (chr->focus >= d->mux_cnt)
328 chr->focus = 0;
329 break;
330 case 't':
331 term_timestamps = !term_timestamps;
332 term_timestamps_start = -1;
333 break;
335 } else if (ch == term_escape_char) {
336 d->term_got_escape = 1;
337 } else {
338 send_char:
339 return 1;
341 return 0;
344 static void mux_chr_accept_input(CharDriverState *chr)
346 int m = chr->focus;
347 MuxDriver *d = chr->opaque;
349 while (d->prod != d->cons &&
350 d->chr_can_read[m] &&
351 d->chr_can_read[m](d->ext_opaque[m])) {
352 d->chr_read[m](d->ext_opaque[m],
353 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
357 static int mux_chr_can_read(void *opaque)
359 CharDriverState *chr = opaque;
360 MuxDriver *d = chr->opaque;
362 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
363 return 1;
364 if (d->chr_can_read[chr->focus])
365 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
366 return 0;
369 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
371 CharDriverState *chr = opaque;
372 MuxDriver *d = chr->opaque;
373 int m = chr->focus;
374 int i;
376 mux_chr_accept_input (opaque);
378 for(i = 0; i < size; i++)
379 if (mux_proc_byte(chr, d, buf[i])) {
380 if (d->prod == d->cons &&
381 d->chr_can_read[m] &&
382 d->chr_can_read[m](d->ext_opaque[m]))
383 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
384 else
385 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
389 static void mux_chr_event(void *opaque, int event)
391 CharDriverState *chr = opaque;
392 MuxDriver *d = chr->opaque;
393 int i;
395 /* Send the event to all registered listeners */
396 for (i = 0; i < d->mux_cnt; i++)
397 if (d->chr_event[i])
398 d->chr_event[i](d->ext_opaque[i], event);
401 static void mux_chr_update_read_handler(CharDriverState *chr)
403 MuxDriver *d = chr->opaque;
405 if (d->mux_cnt >= MAX_MUX) {
406 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
407 return;
409 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
410 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
411 d->chr_read[d->mux_cnt] = chr->chr_read;
412 d->chr_event[d->mux_cnt] = chr->chr_event;
413 /* Fix up the real driver with mux routines */
414 if (d->mux_cnt == 0) {
415 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
416 mux_chr_event, chr);
418 chr->focus = d->mux_cnt;
419 d->mux_cnt++;
422 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
424 CharDriverState *chr;
425 MuxDriver *d;
427 chr = qemu_mallocz(sizeof(CharDriverState));
428 if (!chr)
429 return NULL;
430 d = qemu_mallocz(sizeof(MuxDriver));
431 if (!d) {
432 free(chr);
433 return NULL;
436 chr->opaque = d;
437 d->drv = drv;
438 chr->focus = -1;
439 chr->chr_write = mux_chr_write;
440 chr->chr_update_read_handler = mux_chr_update_read_handler;
441 chr->chr_accept_input = mux_chr_accept_input;
442 return chr;
446 #ifdef _WIN32
447 int send_all(int fd, const void *buf, int len1)
449 int ret, len;
451 len = len1;
452 while (len > 0) {
453 ret = send(fd, buf, len, 0);
454 if (ret < 0) {
455 errno = WSAGetLastError();
456 if (errno != WSAEWOULDBLOCK) {
457 return -1;
459 } else if (ret == 0) {
460 break;
461 } else {
462 buf += ret;
463 len -= ret;
466 return len1 - len;
469 #else
471 static int unix_write(int fd, const uint8_t *buf, int len1)
473 int ret, len;
475 len = len1;
476 while (len > 0) {
477 ret = write(fd, buf, len);
478 if (ret < 0) {
479 if (errno != EINTR && errno != EAGAIN)
480 return -1;
481 } else if (ret == 0) {
482 break;
483 } else {
484 buf += ret;
485 len -= ret;
488 return len1 - len;
491 int send_all(int fd, const void *buf, int len1)
493 return unix_write(fd, buf, len1);
495 #endif /* !_WIN32 */
497 #ifndef _WIN32
499 typedef struct {
500 int fd_in, fd_out;
501 int max_size;
502 } FDCharDriver;
504 #define STDIO_MAX_CLIENTS 1
505 static int stdio_nb_clients = 0;
507 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
509 FDCharDriver *s = chr->opaque;
510 return send_all(s->fd_out, buf, len);
513 static int fd_chr_read_poll(void *opaque)
515 CharDriverState *chr = opaque;
516 FDCharDriver *s = chr->opaque;
518 s->max_size = qemu_chr_can_read(chr);
519 return s->max_size;
522 static void fd_chr_read(void *opaque)
524 CharDriverState *chr = opaque;
525 FDCharDriver *s = chr->opaque;
526 int size, len;
527 uint8_t buf[1024];
529 len = sizeof(buf);
530 if (len > s->max_size)
531 len = s->max_size;
532 if (len == 0)
533 return;
534 size = read(s->fd_in, buf, len);
535 if (size == 0) {
536 /* FD has been closed. Remove it from the active list. */
537 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
538 return;
540 if (size > 0) {
541 qemu_chr_read(chr, buf, size);
545 static void fd_chr_update_read_handler(CharDriverState *chr)
547 FDCharDriver *s = chr->opaque;
549 if (s->fd_in >= 0) {
550 if (nographic && s->fd_in == 0) {
551 } else {
552 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
553 fd_chr_read, NULL, chr);
558 static void fd_chr_close(struct CharDriverState *chr)
560 FDCharDriver *s = chr->opaque;
562 if (s->fd_in >= 0) {
563 if (nographic && s->fd_in == 0) {
564 } else {
565 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
569 qemu_free(s);
572 /* open a character device to a unix fd */
573 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
575 CharDriverState *chr;
576 FDCharDriver *s;
578 chr = qemu_mallocz(sizeof(CharDriverState));
579 if (!chr)
580 return NULL;
581 s = qemu_mallocz(sizeof(FDCharDriver));
582 if (!s) {
583 free(chr);
584 return NULL;
586 s->fd_in = fd_in;
587 s->fd_out = fd_out;
588 chr->opaque = s;
589 chr->chr_write = fd_chr_write;
590 chr->chr_update_read_handler = fd_chr_update_read_handler;
591 chr->chr_close = fd_chr_close;
593 qemu_chr_reset(chr);
595 return chr;
598 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
600 int fd_out;
602 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
603 if (fd_out < 0)
604 return NULL;
605 return qemu_chr_open_fd(-1, fd_out);
608 static CharDriverState *qemu_chr_open_pipe(const char *filename)
610 int fd_in, fd_out;
611 char filename_in[256], filename_out[256];
613 snprintf(filename_in, 256, "%s.in", filename);
614 snprintf(filename_out, 256, "%s.out", filename);
615 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
616 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
617 if (fd_in < 0 || fd_out < 0) {
618 if (fd_in >= 0)
619 close(fd_in);
620 if (fd_out >= 0)
621 close(fd_out);
622 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
623 if (fd_in < 0)
624 return NULL;
626 return qemu_chr_open_fd(fd_in, fd_out);
630 /* for STDIO, we handle the case where several clients use it
631 (nographic mode) */
633 #define TERM_FIFO_MAX_SIZE 1
635 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
636 static int term_fifo_size;
638 static int stdio_read_poll(void *opaque)
640 CharDriverState *chr = opaque;
642 /* try to flush the queue if needed */
643 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
644 qemu_chr_read(chr, term_fifo, 1);
645 term_fifo_size = 0;
647 /* see if we can absorb more chars */
648 if (term_fifo_size == 0)
649 return 1;
650 else
651 return 0;
654 static void stdio_read(void *opaque)
656 int size;
657 uint8_t buf[1];
658 CharDriverState *chr = opaque;
660 size = read(0, buf, 1);
661 if (size == 0) {
662 /* stdin has been closed. Remove it from the active list. */
663 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
664 return;
666 if (size > 0) {
667 if (qemu_chr_can_read(chr) > 0) {
668 qemu_chr_read(chr, buf, 1);
669 } else if (term_fifo_size == 0) {
670 term_fifo[term_fifo_size++] = buf[0];
675 /* init terminal so that we can grab keys */
676 static struct termios oldtty;
677 static int old_fd0_flags;
678 static int term_atexit_done;
680 static void term_exit(void)
682 tcsetattr (0, TCSANOW, &oldtty);
683 fcntl(0, F_SETFL, old_fd0_flags);
686 static void term_init(void)
688 struct termios tty;
690 tcgetattr (0, &tty);
691 oldtty = tty;
692 old_fd0_flags = fcntl(0, F_GETFL);
694 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
695 |INLCR|IGNCR|ICRNL|IXON);
696 tty.c_oflag |= OPOST;
697 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
698 /* if graphical mode, we allow Ctrl-C handling */
699 if (nographic)
700 tty.c_lflag &= ~ISIG;
701 tty.c_cflag &= ~(CSIZE|PARENB);
702 tty.c_cflag |= CS8;
703 tty.c_cc[VMIN] = 1;
704 tty.c_cc[VTIME] = 0;
706 tcsetattr (0, TCSANOW, &tty);
708 if (!term_atexit_done++)
709 atexit(term_exit);
711 fcntl(0, F_SETFL, O_NONBLOCK);
714 static void qemu_chr_close_stdio(struct CharDriverState *chr)
716 term_exit();
717 stdio_nb_clients--;
718 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
719 fd_chr_close(chr);
722 static CharDriverState *qemu_chr_open_stdio(void)
724 CharDriverState *chr;
726 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
727 return NULL;
728 chr = qemu_chr_open_fd(0, 1);
729 chr->chr_close = qemu_chr_close_stdio;
730 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
731 stdio_nb_clients++;
732 term_init();
734 return chr;
737 #ifdef __sun__
738 /* Once Solaris has openpty(), this is going to be removed. */
739 int openpty(int *amaster, int *aslave, char *name,
740 struct termios *termp, struct winsize *winp)
742 const char *slave;
743 int mfd = -1, sfd = -1;
745 *amaster = *aslave = -1;
747 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
748 if (mfd < 0)
749 goto err;
751 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
752 goto err;
754 if ((slave = ptsname(mfd)) == NULL)
755 goto err;
757 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
758 goto err;
760 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
761 (termp != NULL && tcgetattr(sfd, termp) < 0))
762 goto err;
764 if (amaster)
765 *amaster = mfd;
766 if (aslave)
767 *aslave = sfd;
768 if (winp)
769 ioctl(sfd, TIOCSWINSZ, winp);
771 return 0;
773 err:
774 if (sfd != -1)
775 close(sfd);
776 close(mfd);
777 return -1;
780 void cfmakeraw (struct termios *termios_p)
782 termios_p->c_iflag &=
783 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
784 termios_p->c_oflag &= ~OPOST;
785 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
786 termios_p->c_cflag &= ~(CSIZE|PARENB);
787 termios_p->c_cflag |= CS8;
789 termios_p->c_cc[VMIN] = 0;
790 termios_p->c_cc[VTIME] = 0;
792 #endif
794 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
795 || defined(__NetBSD__) || defined(__OpenBSD__)
797 typedef struct {
798 int fd;
799 int connected;
800 int polling;
801 int read_bytes;
802 QEMUTimer *timer;
803 } PtyCharDriver;
805 static void pty_chr_update_read_handler(CharDriverState *chr);
806 static void pty_chr_state(CharDriverState *chr, int connected);
808 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
810 PtyCharDriver *s = chr->opaque;
812 if (!s->connected) {
813 /* guest sends data, check for (re-)connect */
814 pty_chr_update_read_handler(chr);
815 return 0;
817 return send_all(s->fd, buf, len);
820 static int pty_chr_read_poll(void *opaque)
822 CharDriverState *chr = opaque;
823 PtyCharDriver *s = chr->opaque;
825 s->read_bytes = qemu_chr_can_read(chr);
826 return s->read_bytes;
829 static void pty_chr_read(void *opaque)
831 CharDriverState *chr = opaque;
832 PtyCharDriver *s = chr->opaque;
833 int size, len;
834 uint8_t buf[1024];
836 len = sizeof(buf);
837 if (len > s->read_bytes)
838 len = s->read_bytes;
839 if (len == 0)
840 return;
841 size = read(s->fd, buf, len);
842 if ((size == -1 && errno == EIO) ||
843 (size == 0)) {
844 pty_chr_state(chr, 0);
845 return;
847 if (size > 0) {
848 pty_chr_state(chr, 1);
849 qemu_chr_read(chr, buf, size);
853 static void pty_chr_update_read_handler(CharDriverState *chr)
855 PtyCharDriver *s = chr->opaque;
857 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
858 pty_chr_read, NULL, chr);
859 s->polling = 1;
861 * Short timeout here: just need wait long enougth that qemu makes
862 * it through the poll loop once. When reconnected we want a
863 * short timeout so we notice it almost instantly. Otherwise
864 * read() gives us -EIO instantly, making pty_chr_state() reset the
865 * timeout to the normal (much longer) poll interval before the
866 * timer triggers.
868 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
871 static void pty_chr_state(CharDriverState *chr, int connected)
873 PtyCharDriver *s = chr->opaque;
875 if (!connected) {
876 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
877 s->connected = 0;
878 s->polling = 0;
879 /* (re-)connect poll interval for idle guests: once per second.
880 * We check more frequently in case the guests sends data to
881 * the virtual device linked to our pty. */
882 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
883 } else {
884 if (!s->connected)
885 qemu_chr_reset(chr);
886 s->connected = 1;
890 static void pty_chr_timer(void *opaque)
892 struct CharDriverState *chr = opaque;
893 PtyCharDriver *s = chr->opaque;
895 if (s->connected)
896 return;
897 if (s->polling) {
898 /* If we arrive here without polling being cleared due
899 * read returning -EIO, then we are (re-)connected */
900 pty_chr_state(chr, 1);
901 return;
904 /* Next poll ... */
905 pty_chr_update_read_handler(chr);
908 static void pty_chr_close(struct CharDriverState *chr)
910 PtyCharDriver *s = chr->opaque;
912 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
913 close(s->fd);
914 qemu_free(s);
917 static CharDriverState *qemu_chr_open_pty(void)
919 CharDriverState *chr;
920 PtyCharDriver *s;
921 struct termios tty;
922 int slave_fd, len;
923 #if defined(__OpenBSD__)
924 char pty_name[PATH_MAX];
925 #define q_ptsname(x) pty_name
926 #else
927 char *pty_name = NULL;
928 #define q_ptsname(x) ptsname(x)
929 #endif
931 chr = qemu_mallocz(sizeof(CharDriverState));
932 if (!chr)
933 return NULL;
934 s = qemu_mallocz(sizeof(PtyCharDriver));
935 if (!s) {
936 qemu_free(chr);
937 return NULL;
940 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
941 return NULL;
944 /* Set raw attributes on the pty. */
945 tcgetattr(slave_fd, &tty);
946 cfmakeraw(&tty);
947 tcsetattr(slave_fd, TCSAFLUSH, &tty);
948 close(slave_fd);
950 len = strlen(q_ptsname(s->fd)) + 5;
951 chr->filename = qemu_malloc(len);
952 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
953 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
955 chr->opaque = s;
956 chr->chr_write = pty_chr_write;
957 chr->chr_update_read_handler = pty_chr_update_read_handler;
958 chr->chr_close = pty_chr_close;
960 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
962 return chr;
965 static void tty_serial_init(int fd, int speed,
966 int parity, int data_bits, int stop_bits)
968 struct termios tty;
969 speed_t spd;
971 #if 0
972 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
973 speed, parity, data_bits, stop_bits);
974 #endif
975 tcgetattr (fd, &tty);
977 #define MARGIN 1.1
978 if (speed <= 50 * MARGIN)
979 spd = B50;
980 else if (speed <= 75 * MARGIN)
981 spd = B75;
982 else if (speed <= 300 * MARGIN)
983 spd = B300;
984 else if (speed <= 600 * MARGIN)
985 spd = B600;
986 else if (speed <= 1200 * MARGIN)
987 spd = B1200;
988 else if (speed <= 2400 * MARGIN)
989 spd = B2400;
990 else if (speed <= 4800 * MARGIN)
991 spd = B4800;
992 else if (speed <= 9600 * MARGIN)
993 spd = B9600;
994 else if (speed <= 19200 * MARGIN)
995 spd = B19200;
996 else if (speed <= 38400 * MARGIN)
997 spd = B38400;
998 else if (speed <= 57600 * MARGIN)
999 spd = B57600;
1000 else if (speed <= 115200 * MARGIN)
1001 spd = B115200;
1002 else
1003 spd = B115200;
1005 cfsetispeed(&tty, spd);
1006 cfsetospeed(&tty, spd);
1008 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1009 |INLCR|IGNCR|ICRNL|IXON);
1010 tty.c_oflag |= OPOST;
1011 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1012 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1013 switch(data_bits) {
1014 default:
1015 case 8:
1016 tty.c_cflag |= CS8;
1017 break;
1018 case 7:
1019 tty.c_cflag |= CS7;
1020 break;
1021 case 6:
1022 tty.c_cflag |= CS6;
1023 break;
1024 case 5:
1025 tty.c_cflag |= CS5;
1026 break;
1028 switch(parity) {
1029 default:
1030 case 'N':
1031 break;
1032 case 'E':
1033 tty.c_cflag |= PARENB;
1034 break;
1035 case 'O':
1036 tty.c_cflag |= PARENB | PARODD;
1037 break;
1039 if (stop_bits == 2)
1040 tty.c_cflag |= CSTOPB;
1042 tcsetattr (fd, TCSANOW, &tty);
1045 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1047 FDCharDriver *s = chr->opaque;
1049 switch(cmd) {
1050 case CHR_IOCTL_SERIAL_SET_PARAMS:
1052 QEMUSerialSetParams *ssp = arg;
1053 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1054 ssp->data_bits, ssp->stop_bits);
1056 break;
1057 case CHR_IOCTL_SERIAL_SET_BREAK:
1059 int enable = *(int *)arg;
1060 if (enable)
1061 tcsendbreak(s->fd_in, 1);
1063 break;
1064 case CHR_IOCTL_SERIAL_GET_TIOCM:
1066 int sarg = 0;
1067 int *targ = (int *)arg;
1068 ioctl(s->fd_in, TIOCMGET, &sarg);
1069 *targ = 0;
1070 if (sarg | TIOCM_CTS)
1071 *targ |= CHR_TIOCM_CTS;
1072 if (sarg | TIOCM_CAR)
1073 *targ |= CHR_TIOCM_CAR;
1074 if (sarg | TIOCM_DSR)
1075 *targ |= CHR_TIOCM_DSR;
1076 if (sarg | TIOCM_RI)
1077 *targ |= CHR_TIOCM_RI;
1078 if (sarg | TIOCM_DTR)
1079 *targ |= CHR_TIOCM_DTR;
1080 if (sarg | TIOCM_RTS)
1081 *targ |= CHR_TIOCM_RTS;
1083 break;
1084 case CHR_IOCTL_SERIAL_SET_TIOCM:
1086 int sarg = *(int *)arg;
1087 int targ = 0;
1088 if (sarg | CHR_TIOCM_DTR)
1089 targ |= TIOCM_DTR;
1090 if (sarg | CHR_TIOCM_RTS)
1091 targ |= TIOCM_RTS;
1092 ioctl(s->fd_in, TIOCMSET, &targ);
1094 break;
1095 default:
1096 return -ENOTSUP;
1098 return 0;
1101 static CharDriverState *qemu_chr_open_tty(const char *filename)
1103 CharDriverState *chr;
1104 int fd;
1106 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1107 tty_serial_init(fd, 115200, 'N', 8, 1);
1108 chr = qemu_chr_open_fd(fd, fd);
1109 if (!chr) {
1110 close(fd);
1111 return NULL;
1113 chr->chr_ioctl = tty_serial_ioctl;
1114 qemu_chr_reset(chr);
1115 return chr;
1117 #else /* ! __linux__ && ! __sun__ */
1118 static CharDriverState *qemu_chr_open_pty(void)
1120 return NULL;
1122 #endif /* __linux__ || __sun__ */
1124 #if defined(__linux__)
1125 typedef struct {
1126 int fd;
1127 int mode;
1128 } ParallelCharDriver;
1130 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1132 if (s->mode != mode) {
1133 int m = mode;
1134 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1135 return 0;
1136 s->mode = mode;
1138 return 1;
1141 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1143 ParallelCharDriver *drv = chr->opaque;
1144 int fd = drv->fd;
1145 uint8_t b;
1147 switch(cmd) {
1148 case CHR_IOCTL_PP_READ_DATA:
1149 if (ioctl(fd, PPRDATA, &b) < 0)
1150 return -ENOTSUP;
1151 *(uint8_t *)arg = b;
1152 break;
1153 case CHR_IOCTL_PP_WRITE_DATA:
1154 b = *(uint8_t *)arg;
1155 if (ioctl(fd, PPWDATA, &b) < 0)
1156 return -ENOTSUP;
1157 break;
1158 case CHR_IOCTL_PP_READ_CONTROL:
1159 if (ioctl(fd, PPRCONTROL, &b) < 0)
1160 return -ENOTSUP;
1161 /* Linux gives only the lowest bits, and no way to know data
1162 direction! For better compatibility set the fixed upper
1163 bits. */
1164 *(uint8_t *)arg = b | 0xc0;
1165 break;
1166 case CHR_IOCTL_PP_WRITE_CONTROL:
1167 b = *(uint8_t *)arg;
1168 if (ioctl(fd, PPWCONTROL, &b) < 0)
1169 return -ENOTSUP;
1170 break;
1171 case CHR_IOCTL_PP_READ_STATUS:
1172 if (ioctl(fd, PPRSTATUS, &b) < 0)
1173 return -ENOTSUP;
1174 *(uint8_t *)arg = b;
1175 break;
1176 case CHR_IOCTL_PP_DATA_DIR:
1177 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1178 return -ENOTSUP;
1179 break;
1180 case CHR_IOCTL_PP_EPP_READ_ADDR:
1181 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1182 struct ParallelIOArg *parg = arg;
1183 int n = read(fd, parg->buffer, parg->count);
1184 if (n != parg->count) {
1185 return -EIO;
1188 break;
1189 case CHR_IOCTL_PP_EPP_READ:
1190 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1191 struct ParallelIOArg *parg = arg;
1192 int n = read(fd, parg->buffer, parg->count);
1193 if (n != parg->count) {
1194 return -EIO;
1197 break;
1198 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1199 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1200 struct ParallelIOArg *parg = arg;
1201 int n = write(fd, parg->buffer, parg->count);
1202 if (n != parg->count) {
1203 return -EIO;
1206 break;
1207 case CHR_IOCTL_PP_EPP_WRITE:
1208 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1209 struct ParallelIOArg *parg = arg;
1210 int n = write(fd, parg->buffer, parg->count);
1211 if (n != parg->count) {
1212 return -EIO;
1215 break;
1216 default:
1217 return -ENOTSUP;
1219 return 0;
1222 static void pp_close(CharDriverState *chr)
1224 ParallelCharDriver *drv = chr->opaque;
1225 int fd = drv->fd;
1227 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1228 ioctl(fd, PPRELEASE);
1229 close(fd);
1230 qemu_free(drv);
1233 static CharDriverState *qemu_chr_open_pp(const char *filename)
1235 CharDriverState *chr;
1236 ParallelCharDriver *drv;
1237 int fd;
1239 TFR(fd = open(filename, O_RDWR));
1240 if (fd < 0)
1241 return NULL;
1243 if (ioctl(fd, PPCLAIM) < 0) {
1244 close(fd);
1245 return NULL;
1248 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1249 if (!drv) {
1250 close(fd);
1251 return NULL;
1253 drv->fd = fd;
1254 drv->mode = IEEE1284_MODE_COMPAT;
1256 chr = qemu_mallocz(sizeof(CharDriverState));
1257 if (!chr) {
1258 qemu_free(drv);
1259 close(fd);
1260 return NULL;
1262 chr->chr_write = null_chr_write;
1263 chr->chr_ioctl = pp_ioctl;
1264 chr->chr_close = pp_close;
1265 chr->opaque = drv;
1267 qemu_chr_reset(chr);
1269 return chr;
1271 #endif /* __linux__ */
1273 #if defined(__FreeBSD__)
1274 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1276 int fd = (int)chr->opaque;
1277 uint8_t b;
1279 switch(cmd) {
1280 case CHR_IOCTL_PP_READ_DATA:
1281 if (ioctl(fd, PPIGDATA, &b) < 0)
1282 return -ENOTSUP;
1283 *(uint8_t *)arg = b;
1284 break;
1285 case CHR_IOCTL_PP_WRITE_DATA:
1286 b = *(uint8_t *)arg;
1287 if (ioctl(fd, PPISDATA, &b) < 0)
1288 return -ENOTSUP;
1289 break;
1290 case CHR_IOCTL_PP_READ_CONTROL:
1291 if (ioctl(fd, PPIGCTRL, &b) < 0)
1292 return -ENOTSUP;
1293 *(uint8_t *)arg = b;
1294 break;
1295 case CHR_IOCTL_PP_WRITE_CONTROL:
1296 b = *(uint8_t *)arg;
1297 if (ioctl(fd, PPISCTRL, &b) < 0)
1298 return -ENOTSUP;
1299 break;
1300 case CHR_IOCTL_PP_READ_STATUS:
1301 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1302 return -ENOTSUP;
1303 *(uint8_t *)arg = b;
1304 break;
1305 default:
1306 return -ENOTSUP;
1308 return 0;
1311 static CharDriverState *qemu_chr_open_pp(const char *filename)
1313 CharDriverState *chr;
1314 int fd;
1316 fd = open(filename, O_RDWR);
1317 if (fd < 0)
1318 return NULL;
1320 chr = qemu_mallocz(sizeof(CharDriverState));
1321 if (!chr) {
1322 close(fd);
1323 return NULL;
1325 chr->opaque = (void *)fd;
1326 chr->chr_write = null_chr_write;
1327 chr->chr_ioctl = pp_ioctl;
1328 return chr;
1330 #endif
1332 #else /* _WIN32 */
1334 typedef struct {
1335 int max_size;
1336 HANDLE hcom, hrecv, hsend;
1337 OVERLAPPED orecv, osend;
1338 BOOL fpipe;
1339 DWORD len;
1340 } WinCharState;
1342 #define NSENDBUF 2048
1343 #define NRECVBUF 2048
1344 #define MAXCONNECT 1
1345 #define NTIMEOUT 5000
1347 static int win_chr_poll(void *opaque);
1348 static int win_chr_pipe_poll(void *opaque);
1350 static void win_chr_close(CharDriverState *chr)
1352 WinCharState *s = chr->opaque;
1354 if (s->hsend) {
1355 CloseHandle(s->hsend);
1356 s->hsend = NULL;
1358 if (s->hrecv) {
1359 CloseHandle(s->hrecv);
1360 s->hrecv = NULL;
1362 if (s->hcom) {
1363 CloseHandle(s->hcom);
1364 s->hcom = NULL;
1366 if (s->fpipe)
1367 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1368 else
1369 qemu_del_polling_cb(win_chr_poll, chr);
1372 static int win_chr_init(CharDriverState *chr, const char *filename)
1374 WinCharState *s = chr->opaque;
1375 COMMCONFIG comcfg;
1376 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1377 COMSTAT comstat;
1378 DWORD size;
1379 DWORD err;
1381 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1382 if (!s->hsend) {
1383 fprintf(stderr, "Failed CreateEvent\n");
1384 goto fail;
1386 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1387 if (!s->hrecv) {
1388 fprintf(stderr, "Failed CreateEvent\n");
1389 goto fail;
1392 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1393 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1394 if (s->hcom == INVALID_HANDLE_VALUE) {
1395 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1396 s->hcom = NULL;
1397 goto fail;
1400 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1401 fprintf(stderr, "Failed SetupComm\n");
1402 goto fail;
1405 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1406 size = sizeof(COMMCONFIG);
1407 GetDefaultCommConfig(filename, &comcfg, &size);
1408 comcfg.dcb.DCBlength = sizeof(DCB);
1409 CommConfigDialog(filename, NULL, &comcfg);
1411 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1412 fprintf(stderr, "Failed SetCommState\n");
1413 goto fail;
1416 if (!SetCommMask(s->hcom, EV_ERR)) {
1417 fprintf(stderr, "Failed SetCommMask\n");
1418 goto fail;
1421 cto.ReadIntervalTimeout = MAXDWORD;
1422 if (!SetCommTimeouts(s->hcom, &cto)) {
1423 fprintf(stderr, "Failed SetCommTimeouts\n");
1424 goto fail;
1427 if (!ClearCommError(s->hcom, &err, &comstat)) {
1428 fprintf(stderr, "Failed ClearCommError\n");
1429 goto fail;
1431 qemu_add_polling_cb(win_chr_poll, chr);
1432 return 0;
1434 fail:
1435 win_chr_close(chr);
1436 return -1;
1439 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1441 WinCharState *s = chr->opaque;
1442 DWORD len, ret, size, err;
1444 len = len1;
1445 ZeroMemory(&s->osend, sizeof(s->osend));
1446 s->osend.hEvent = s->hsend;
1447 while (len > 0) {
1448 if (s->hsend)
1449 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1450 else
1451 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1452 if (!ret) {
1453 err = GetLastError();
1454 if (err == ERROR_IO_PENDING) {
1455 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1456 if (ret) {
1457 buf += size;
1458 len -= size;
1459 } else {
1460 break;
1462 } else {
1463 break;
1465 } else {
1466 buf += size;
1467 len -= size;
1470 return len1 - len;
1473 static int win_chr_read_poll(CharDriverState *chr)
1475 WinCharState *s = chr->opaque;
1477 s->max_size = qemu_chr_can_read(chr);
1478 return s->max_size;
1481 static void win_chr_readfile(CharDriverState *chr)
1483 WinCharState *s = chr->opaque;
1484 int ret, err;
1485 uint8_t buf[1024];
1486 DWORD size;
1488 ZeroMemory(&s->orecv, sizeof(s->orecv));
1489 s->orecv.hEvent = s->hrecv;
1490 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1491 if (!ret) {
1492 err = GetLastError();
1493 if (err == ERROR_IO_PENDING) {
1494 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1498 if (size > 0) {
1499 qemu_chr_read(chr, buf, size);
1503 static void win_chr_read(CharDriverState *chr)
1505 WinCharState *s = chr->opaque;
1507 if (s->len > s->max_size)
1508 s->len = s->max_size;
1509 if (s->len == 0)
1510 return;
1512 win_chr_readfile(chr);
1515 static int win_chr_poll(void *opaque)
1517 CharDriverState *chr = opaque;
1518 WinCharState *s = chr->opaque;
1519 COMSTAT status;
1520 DWORD comerr;
1522 ClearCommError(s->hcom, &comerr, &status);
1523 if (status.cbInQue > 0) {
1524 s->len = status.cbInQue;
1525 win_chr_read_poll(chr);
1526 win_chr_read(chr);
1527 return 1;
1529 return 0;
1532 static CharDriverState *qemu_chr_open_win(const char *filename)
1534 CharDriverState *chr;
1535 WinCharState *s;
1537 chr = qemu_mallocz(sizeof(CharDriverState));
1538 if (!chr)
1539 return NULL;
1540 s = qemu_mallocz(sizeof(WinCharState));
1541 if (!s) {
1542 free(chr);
1543 return NULL;
1545 chr->opaque = s;
1546 chr->chr_write = win_chr_write;
1547 chr->chr_close = win_chr_close;
1549 if (win_chr_init(chr, filename) < 0) {
1550 free(s);
1551 free(chr);
1552 return NULL;
1554 qemu_chr_reset(chr);
1555 return chr;
1558 static int win_chr_pipe_poll(void *opaque)
1560 CharDriverState *chr = opaque;
1561 WinCharState *s = chr->opaque;
1562 DWORD size;
1564 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1565 if (size > 0) {
1566 s->len = size;
1567 win_chr_read_poll(chr);
1568 win_chr_read(chr);
1569 return 1;
1571 return 0;
1574 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1576 WinCharState *s = chr->opaque;
1577 OVERLAPPED ov;
1578 int ret;
1579 DWORD size;
1580 char openname[256];
1582 s->fpipe = TRUE;
1584 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1585 if (!s->hsend) {
1586 fprintf(stderr, "Failed CreateEvent\n");
1587 goto fail;
1589 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1590 if (!s->hrecv) {
1591 fprintf(stderr, "Failed CreateEvent\n");
1592 goto fail;
1595 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1596 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1597 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1598 PIPE_WAIT,
1599 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1600 if (s->hcom == INVALID_HANDLE_VALUE) {
1601 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1602 s->hcom = NULL;
1603 goto fail;
1606 ZeroMemory(&ov, sizeof(ov));
1607 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1608 ret = ConnectNamedPipe(s->hcom, &ov);
1609 if (ret) {
1610 fprintf(stderr, "Failed ConnectNamedPipe\n");
1611 goto fail;
1614 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1615 if (!ret) {
1616 fprintf(stderr, "Failed GetOverlappedResult\n");
1617 if (ov.hEvent) {
1618 CloseHandle(ov.hEvent);
1619 ov.hEvent = NULL;
1621 goto fail;
1624 if (ov.hEvent) {
1625 CloseHandle(ov.hEvent);
1626 ov.hEvent = NULL;
1628 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1629 return 0;
1631 fail:
1632 win_chr_close(chr);
1633 return -1;
1637 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1639 CharDriverState *chr;
1640 WinCharState *s;
1642 chr = qemu_mallocz(sizeof(CharDriverState));
1643 if (!chr)
1644 return NULL;
1645 s = qemu_mallocz(sizeof(WinCharState));
1646 if (!s) {
1647 free(chr);
1648 return NULL;
1650 chr->opaque = s;
1651 chr->chr_write = win_chr_write;
1652 chr->chr_close = win_chr_close;
1654 if (win_chr_pipe_init(chr, filename) < 0) {
1655 free(s);
1656 free(chr);
1657 return NULL;
1659 qemu_chr_reset(chr);
1660 return chr;
1663 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1665 CharDriverState *chr;
1666 WinCharState *s;
1668 chr = qemu_mallocz(sizeof(CharDriverState));
1669 if (!chr)
1670 return NULL;
1671 s = qemu_mallocz(sizeof(WinCharState));
1672 if (!s) {
1673 free(chr);
1674 return NULL;
1676 s->hcom = fd_out;
1677 chr->opaque = s;
1678 chr->chr_write = win_chr_write;
1679 qemu_chr_reset(chr);
1680 return chr;
1683 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1685 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1688 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1690 HANDLE fd_out;
1692 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1693 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1694 if (fd_out == INVALID_HANDLE_VALUE)
1695 return NULL;
1697 return qemu_chr_open_win_file(fd_out);
1699 #endif /* !_WIN32 */
1701 /***********************************************************/
1702 /* UDP Net console */
1704 typedef struct {
1705 int fd;
1706 struct sockaddr_in daddr;
1707 uint8_t buf[1024];
1708 int bufcnt;
1709 int bufptr;
1710 int max_size;
1711 } NetCharDriver;
1713 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1715 NetCharDriver *s = chr->opaque;
1717 return sendto(s->fd, buf, len, 0,
1718 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1721 static int udp_chr_read_poll(void *opaque)
1723 CharDriverState *chr = opaque;
1724 NetCharDriver *s = chr->opaque;
1726 s->max_size = qemu_chr_can_read(chr);
1728 /* If there were any stray characters in the queue process them
1729 * first
1731 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1732 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1733 s->bufptr++;
1734 s->max_size = qemu_chr_can_read(chr);
1736 return s->max_size;
1739 static void udp_chr_read(void *opaque)
1741 CharDriverState *chr = opaque;
1742 NetCharDriver *s = chr->opaque;
1744 if (s->max_size == 0)
1745 return;
1746 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1747 s->bufptr = s->bufcnt;
1748 if (s->bufcnt <= 0)
1749 return;
1751 s->bufptr = 0;
1752 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1753 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1754 s->bufptr++;
1755 s->max_size = qemu_chr_can_read(chr);
1759 static void udp_chr_update_read_handler(CharDriverState *chr)
1761 NetCharDriver *s = chr->opaque;
1763 if (s->fd >= 0) {
1764 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1765 udp_chr_read, NULL, chr);
1769 static CharDriverState *qemu_chr_open_udp(const char *def)
1771 CharDriverState *chr = NULL;
1772 NetCharDriver *s = NULL;
1773 int fd = -1;
1774 struct sockaddr_in saddr;
1776 chr = qemu_mallocz(sizeof(CharDriverState));
1777 if (!chr)
1778 goto return_err;
1779 s = qemu_mallocz(sizeof(NetCharDriver));
1780 if (!s)
1781 goto return_err;
1783 fd = socket(PF_INET, SOCK_DGRAM, 0);
1784 if (fd < 0) {
1785 perror("socket(PF_INET, SOCK_DGRAM)");
1786 goto return_err;
1789 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1790 printf("Could not parse: %s\n", def);
1791 goto return_err;
1794 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1796 perror("bind");
1797 goto return_err;
1800 s->fd = fd;
1801 s->bufcnt = 0;
1802 s->bufptr = 0;
1803 chr->opaque = s;
1804 chr->chr_write = udp_chr_write;
1805 chr->chr_update_read_handler = udp_chr_update_read_handler;
1806 return chr;
1808 return_err:
1809 if (chr)
1810 free(chr);
1811 if (s)
1812 free(s);
1813 if (fd >= 0)
1814 closesocket(fd);
1815 return NULL;
1818 /***********************************************************/
1819 /* TCP Net console */
1821 typedef struct {
1822 int fd, listen_fd;
1823 int connected;
1824 int max_size;
1825 int do_telnetopt;
1826 int do_nodelay;
1827 int is_unix;
1828 } TCPCharDriver;
1830 static void tcp_chr_accept(void *opaque);
1832 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1834 TCPCharDriver *s = chr->opaque;
1835 if (s->connected) {
1836 return send_all(s->fd, buf, len);
1837 } else {
1838 /* XXX: indicate an error ? */
1839 return len;
1843 static int tcp_chr_read_poll(void *opaque)
1845 CharDriverState *chr = opaque;
1846 TCPCharDriver *s = chr->opaque;
1847 if (!s->connected)
1848 return 0;
1849 s->max_size = qemu_chr_can_read(chr);
1850 return s->max_size;
1853 #define IAC 255
1854 #define IAC_BREAK 243
1855 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1856 TCPCharDriver *s,
1857 uint8_t *buf, int *size)
1859 /* Handle any telnet client's basic IAC options to satisfy char by
1860 * char mode with no echo. All IAC options will be removed from
1861 * the buf and the do_telnetopt variable will be used to track the
1862 * state of the width of the IAC information.
1864 * IAC commands come in sets of 3 bytes with the exception of the
1865 * "IAC BREAK" command and the double IAC.
1868 int i;
1869 int j = 0;
1871 for (i = 0; i < *size; i++) {
1872 if (s->do_telnetopt > 1) {
1873 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1874 /* Double IAC means send an IAC */
1875 if (j != i)
1876 buf[j] = buf[i];
1877 j++;
1878 s->do_telnetopt = 1;
1879 } else {
1880 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1881 /* Handle IAC break commands by sending a serial break */
1882 qemu_chr_event(chr, CHR_EVENT_BREAK);
1883 s->do_telnetopt++;
1885 s->do_telnetopt++;
1887 if (s->do_telnetopt >= 4) {
1888 s->do_telnetopt = 1;
1890 } else {
1891 if ((unsigned char)buf[i] == IAC) {
1892 s->do_telnetopt = 2;
1893 } else {
1894 if (j != i)
1895 buf[j] = buf[i];
1896 j++;
1900 *size = j;
1903 static void tcp_chr_read(void *opaque)
1905 CharDriverState *chr = opaque;
1906 TCPCharDriver *s = chr->opaque;
1907 uint8_t buf[1024];
1908 int len, size;
1910 if (!s->connected || s->max_size <= 0)
1911 return;
1912 len = sizeof(buf);
1913 if (len > s->max_size)
1914 len = s->max_size;
1915 size = recv(s->fd, buf, len, 0);
1916 if (size == 0) {
1917 /* connection closed */
1918 s->connected = 0;
1919 if (s->listen_fd >= 0) {
1920 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1922 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1923 closesocket(s->fd);
1924 s->fd = -1;
1925 } else if (size > 0) {
1926 if (s->do_telnetopt)
1927 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1928 if (size > 0)
1929 qemu_chr_read(chr, buf, size);
1933 static void tcp_chr_connect(void *opaque)
1935 CharDriverState *chr = opaque;
1936 TCPCharDriver *s = chr->opaque;
1938 s->connected = 1;
1939 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1940 tcp_chr_read, NULL, chr);
1941 qemu_chr_reset(chr);
1944 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1945 static void tcp_chr_telnet_init(int fd)
1947 char buf[3];
1948 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1949 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1950 send(fd, (char *)buf, 3, 0);
1951 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1952 send(fd, (char *)buf, 3, 0);
1953 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1954 send(fd, (char *)buf, 3, 0);
1955 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1956 send(fd, (char *)buf, 3, 0);
1959 static void socket_set_nodelay(int fd)
1961 int val = 1;
1962 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1965 static void tcp_chr_accept(void *opaque)
1967 CharDriverState *chr = opaque;
1968 TCPCharDriver *s = chr->opaque;
1969 struct sockaddr_in saddr;
1970 #ifndef _WIN32
1971 struct sockaddr_un uaddr;
1972 #endif
1973 struct sockaddr *addr;
1974 socklen_t len;
1975 int fd;
1977 for(;;) {
1978 #ifndef _WIN32
1979 if (s->is_unix) {
1980 len = sizeof(uaddr);
1981 addr = (struct sockaddr *)&uaddr;
1982 } else
1983 #endif
1985 len = sizeof(saddr);
1986 addr = (struct sockaddr *)&saddr;
1988 fd = accept(s->listen_fd, addr, &len);
1989 if (fd < 0 && errno != EINTR) {
1990 return;
1991 } else if (fd >= 0) {
1992 if (s->do_telnetopt)
1993 tcp_chr_telnet_init(fd);
1994 break;
1997 socket_set_nonblock(fd);
1998 if (s->do_nodelay)
1999 socket_set_nodelay(fd);
2000 s->fd = fd;
2001 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2002 tcp_chr_connect(chr);
2005 static void tcp_chr_close(CharDriverState *chr)
2007 TCPCharDriver *s = chr->opaque;
2008 if (s->fd >= 0)
2009 closesocket(s->fd);
2010 if (s->listen_fd >= 0)
2011 closesocket(s->listen_fd);
2012 qemu_free(s);
2015 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
2016 int is_telnet,
2017 int is_unix)
2019 CharDriverState *chr = NULL;
2020 TCPCharDriver *s = NULL;
2021 int fd = -1, offset = 0;
2022 int is_listen = 0;
2023 int is_waitconnect = 1;
2024 int do_nodelay = 0;
2025 const char *ptr;
2027 ptr = host_str;
2028 while((ptr = strchr(ptr,','))) {
2029 ptr++;
2030 if (!strncmp(ptr,"server",6)) {
2031 is_listen = 1;
2032 } else if (!strncmp(ptr,"nowait",6)) {
2033 is_waitconnect = 0;
2034 } else if (!strncmp(ptr,"nodelay",6)) {
2035 do_nodelay = 1;
2036 } else if (!strncmp(ptr,"to=",3)) {
2037 /* nothing, inet_listen() parses this one */;
2038 } else {
2039 printf("Unknown option: %s\n", ptr);
2040 goto fail;
2043 if (!is_listen)
2044 is_waitconnect = 0;
2046 chr = qemu_mallocz(sizeof(CharDriverState));
2047 if (!chr)
2048 goto fail;
2049 s = qemu_mallocz(sizeof(TCPCharDriver));
2050 if (!s)
2051 goto fail;
2053 if (is_listen) {
2054 chr->filename = qemu_malloc(256);
2055 if (is_unix) {
2056 strcpy(chr->filename, "unix:");
2057 } else if (is_telnet) {
2058 strcpy(chr->filename, "telnet:");
2059 } else {
2060 strcpy(chr->filename, "tcp:");
2062 offset = strlen(chr->filename);
2064 if (is_unix) {
2065 if (is_listen) {
2066 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2067 } else {
2068 fd = unix_connect(host_str);
2070 } else {
2071 if (is_listen) {
2072 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2073 SOCK_STREAM, 0);
2074 } else {
2075 fd = inet_connect(host_str, SOCK_STREAM);
2078 if (fd < 0)
2079 goto fail;
2081 if (!is_waitconnect)
2082 socket_set_nonblock(fd);
2084 s->connected = 0;
2085 s->fd = -1;
2086 s->listen_fd = -1;
2087 s->is_unix = is_unix;
2088 s->do_nodelay = do_nodelay && !is_unix;
2090 chr->opaque = s;
2091 chr->chr_write = tcp_chr_write;
2092 chr->chr_close = tcp_chr_close;
2094 if (is_listen) {
2095 s->listen_fd = fd;
2096 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2097 if (is_telnet)
2098 s->do_telnetopt = 1;
2099 } else {
2100 s->connected = 1;
2101 s->fd = fd;
2102 socket_set_nodelay(fd);
2103 tcp_chr_connect(chr);
2106 if (is_listen && is_waitconnect) {
2107 printf("QEMU waiting for connection on: %s\n",
2108 chr->filename ? chr->filename : host_str);
2109 tcp_chr_accept(chr);
2110 socket_set_nonblock(s->listen_fd);
2113 return chr;
2114 fail:
2115 if (fd >= 0)
2116 closesocket(fd);
2117 qemu_free(s);
2118 qemu_free(chr);
2119 return NULL;
2122 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
2123 = TAILQ_HEAD_INITIALIZER(chardevs);
2125 CharDriverState *qemu_chr_open(const char *label, const char *filename)
2127 const char *p;
2128 CharDriverState *chr;
2130 if (!strcmp(filename, "vc")) {
2131 chr = text_console_init(&display_state, 0);
2132 } else
2133 if (strstart(filename, "vc:", &p)) {
2134 chr = text_console_init(&display_state, p);
2135 } else
2136 if (!strcmp(filename, "null")) {
2137 chr = qemu_chr_open_null();
2138 } else
2139 if (strstart(filename, "tcp:", &p)) {
2140 chr = qemu_chr_open_tcp(p, 0, 0);
2141 } else
2142 if (strstart(filename, "telnet:", &p)) {
2143 chr = qemu_chr_open_tcp(p, 1, 0);
2144 } else
2145 if (strstart(filename, "udp:", &p)) {
2146 chr = qemu_chr_open_udp(p);
2147 } else
2148 if (strstart(filename, "mon:", &p)) {
2149 chr = qemu_chr_open(label, p);
2150 if (chr) {
2151 chr = qemu_chr_open_mux(chr);
2152 monitor_init(chr, !nographic);
2153 } else {
2154 printf("Unable to open driver: %s\n", p);
2156 } else
2157 #ifndef _WIN32
2158 if (strstart(filename, "unix:", &p)) {
2159 chr = qemu_chr_open_tcp(p, 0, 1);
2160 } else if (strstart(filename, "file:", &p)) {
2161 chr = qemu_chr_open_file_out(p);
2162 } else if (strstart(filename, "pipe:", &p)) {
2163 chr = qemu_chr_open_pipe(p);
2164 } else if (!strcmp(filename, "pty")) {
2165 chr = qemu_chr_open_pty();
2166 } else if (!strcmp(filename, "stdio")) {
2167 chr = qemu_chr_open_stdio();
2168 } else
2169 #if defined(__linux__)
2170 if (strstart(filename, "/dev/parport", NULL)) {
2171 chr = qemu_chr_open_pp(filename);
2172 } else
2173 #elif defined(__FreeBSD__)
2174 if (strstart(filename, "/dev/ppi", NULL)) {
2175 chr = qemu_chr_open_pp(filename);
2176 } else
2177 #endif
2178 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2179 || defined(__NetBSD__) || defined(__OpenBSD__)
2180 if (strstart(filename, "/dev/", NULL)) {
2181 chr = qemu_chr_open_tty(filename);
2182 } else
2183 #endif
2184 #else /* !_WIN32 */
2185 if (strstart(filename, "COM", NULL)) {
2186 chr = qemu_chr_open_win(filename);
2187 } else
2188 if (strstart(filename, "pipe:", &p)) {
2189 chr = qemu_chr_open_win_pipe(p);
2190 } else
2191 if (strstart(filename, "con:", NULL)) {
2192 chr = qemu_chr_open_win_con(filename);
2193 } else
2194 if (strstart(filename, "file:", &p)) {
2195 chr = qemu_chr_open_win_file_out(p);
2196 } else
2197 #endif
2198 #ifdef CONFIG_BRLAPI
2199 if (!strcmp(filename, "braille")) {
2200 chr = chr_baum_init();
2201 } else
2202 #endif
2204 chr = NULL;
2207 if (chr) {
2208 if (!chr->filename)
2209 chr->filename = qemu_strdup(filename);
2210 chr->label = qemu_strdup(label);
2211 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2213 return chr;
2216 void qemu_chr_close(CharDriverState *chr)
2218 TAILQ_REMOVE(&chardevs, chr, next);
2219 if (chr->chr_close)
2220 chr->chr_close(chr);
2221 qemu_free(chr->filename);
2222 qemu_free(chr->label);
2223 qemu_free(chr);
2226 void qemu_chr_info(void)
2228 CharDriverState *chr;
2230 TAILQ_FOREACH(chr, &chardevs, next) {
2231 term_printf("%s: filename=%s\n", chr->label, chr->filename);