1923
1848
atexit(socket_cleanup);
1927
static int send_all(int fd, const uint8_t *buf, int len1)
1933
ret = send(fd, buf, len, 0);
1936
errno = WSAGetLastError();
1937
if (errno != WSAEWOULDBLOCK) {
1940
} else if (ret == 0) {
1950
void socket_set_nonblock(int fd)
1952
unsigned long opt = 1;
1953
ioctlsocket(fd, FIONBIO, &opt);
1958
static int unix_write(int fd, const uint8_t *buf, int len1)
1964
ret = write(fd, buf, len);
1966
if (errno != EINTR && errno != EAGAIN)
1968
} else if (ret == 0) {
1978
static inline int send_all(int fd, const uint8_t *buf, int len1)
1980
return unix_write(fd, buf, len1);
1983
void socket_set_nonblock(int fd)
1985
fcntl(fd, F_SETFL, O_NONBLOCK);
1987
#endif /* !_WIN32 */
1996
#define STDIO_MAX_CLIENTS 1
1997
static int stdio_nb_clients = 0;
1999
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2001
FDCharDriver *s = chr->opaque;
2002
return unix_write(s->fd_out, buf, len);
2005
static int fd_chr_read_poll(void *opaque)
2007
CharDriverState *chr = opaque;
2008
FDCharDriver *s = chr->opaque;
2010
s->max_size = qemu_chr_can_read(chr);
2014
static void fd_chr_read(void *opaque)
2016
CharDriverState *chr = opaque;
2017
FDCharDriver *s = chr->opaque;
2022
if (len > s->max_size)
2026
size = read(s->fd_in, buf, len);
2028
/* FD has been closed. Remove it from the active list. */
2029
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2033
qemu_chr_read(chr, buf, size);
2037
static void fd_chr_update_read_handler(CharDriverState *chr)
2039
FDCharDriver *s = chr->opaque;
2041
if (s->fd_in >= 0) {
2042
if (nographic && s->fd_in == 0) {
2044
qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
2045
fd_chr_read, NULL, chr);
2050
/* open a character device to a unix fd */
2051
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2053
CharDriverState *chr;
2056
chr = qemu_mallocz(sizeof(CharDriverState));
2059
s = qemu_mallocz(sizeof(FDCharDriver));
2067
chr->chr_write = fd_chr_write;
2068
chr->chr_update_read_handler = fd_chr_update_read_handler;
2070
qemu_chr_reset(chr);
2075
static CharDriverState *qemu_chr_open_file_out(const char *file_out)
2079
TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
2082
return qemu_chr_open_fd(-1, fd_out);
2085
static CharDriverState *qemu_chr_open_pipe(const char *filename)
2088
char filename_in[256], filename_out[256];
2090
snprintf(filename_in, 256, "%s.in", filename);
2091
snprintf(filename_out, 256, "%s.out", filename);
2092
TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2093
TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
2094
if (fd_in < 0 || fd_out < 0) {
2099
TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
2103
return qemu_chr_open_fd(fd_in, fd_out);
2107
/* for STDIO, we handle the case where several clients use it
2110
#define TERM_FIFO_MAX_SIZE 1
2112
static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
2113
static int term_fifo_size;
2115
static int stdio_read_poll(void *opaque)
2117
CharDriverState *chr = opaque;
2119
/* try to flush the queue if needed */
2120
if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2121
qemu_chr_read(chr, term_fifo, 1);
2124
/* see if we can absorb more chars */
2125
if (term_fifo_size == 0)
2131
static void stdio_read(void *opaque)
2135
CharDriverState *chr = opaque;
2137
size = read(0, buf, 1);
2139
/* stdin has been closed. Remove it from the active list. */
2140
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2144
if (qemu_chr_can_read(chr) > 0) {
2145
qemu_chr_read(chr, buf, 1);
2146
} else if (term_fifo_size == 0) {
2147
term_fifo[term_fifo_size++] = buf[0];
2152
/* init terminal so that we can grab keys */
2153
static struct termios oldtty;
2154
static int old_fd0_flags;
2156
static void term_exit(void)
2158
tcsetattr (0, TCSANOW, &oldtty);
2159
fcntl(0, F_SETFL, old_fd0_flags);
2162
static void term_init(void)
2166
tcgetattr (0, &tty);
2168
old_fd0_flags = fcntl(0, F_GETFL);
2170
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2171
|INLCR|IGNCR|ICRNL|IXON);
2172
tty.c_oflag |= OPOST;
2173
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2174
/* if graphical mode, we allow Ctrl-C handling */
2176
tty.c_lflag &= ~ISIG;
2177
tty.c_cflag &= ~(CSIZE|PARENB);
2180
tty.c_cc[VTIME] = 0;
2182
tcsetattr (0, TCSANOW, &tty);
2186
fcntl(0, F_SETFL, O_NONBLOCK);
2189
static CharDriverState *qemu_chr_open_stdio(void)
2191
CharDriverState *chr;
2193
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2195
chr = qemu_chr_open_fd(0, 1);
2196
qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2203
#if defined(__linux__) || defined(__sun__)
2204
static CharDriverState *qemu_chr_open_pty(void)
2207
char slave_name[1024];
2208
int master_fd, slave_fd;
2210
#if defined(__linux__)
2211
/* Not satisfying */
2212
if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
2217
/* Disabling local echo and line-buffered output */
2218
tcgetattr (master_fd, &tty);
2219
tty.c_lflag &= ~(ECHO|ICANON|ISIG);
2221
tty.c_cc[VTIME] = 0;
2222
tcsetattr (master_fd, TCSAFLUSH, &tty);
2224
fprintf(stderr, "char device redirected to %s\n", slave_name);
2225
return qemu_chr_open_fd(master_fd, master_fd);
2228
static void tty_serial_init(int fd, int speed,
2229
int parity, int data_bits, int stop_bits)
2235
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2236
speed, parity, data_bits, stop_bits);
2238
tcgetattr (fd, &tty);
2280
cfsetispeed(&tty, spd);
2281
cfsetospeed(&tty, spd);
2283
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2284
|INLCR|IGNCR|ICRNL|IXON);
2285
tty.c_oflag |= OPOST;
2286
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
2287
tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
2308
tty.c_cflag |= PARENB;
2311
tty.c_cflag |= PARENB | PARODD;
2315
tty.c_cflag |= CSTOPB;
2317
tcsetattr (fd, TCSANOW, &tty);
2320
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
2322
FDCharDriver *s = chr->opaque;
2325
case CHR_IOCTL_SERIAL_SET_PARAMS:
2327
QEMUSerialSetParams *ssp = arg;
2328
tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
2329
ssp->data_bits, ssp->stop_bits);
2332
case CHR_IOCTL_SERIAL_SET_BREAK:
2334
int enable = *(int *)arg;
2336
tcsendbreak(s->fd_in, 1);
2345
static CharDriverState *qemu_chr_open_tty(const char *filename)
2347
CharDriverState *chr;
2350
TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
2351
fcntl(fd, F_SETFL, O_NONBLOCK);
2352
tty_serial_init(fd, 115200, 'N', 8, 1);
2353
chr = qemu_chr_open_fd(fd, fd);
2358
chr->chr_ioctl = tty_serial_ioctl;
2359
qemu_chr_reset(chr);
2362
#else /* ! __linux__ && ! __sun__ */
2363
static CharDriverState *qemu_chr_open_pty(void)
2367
#endif /* __linux__ || __sun__ */
2369
#if defined(__linux__)
2373
} ParallelCharDriver;
2375
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2377
if (s->mode != mode) {
2379
if (ioctl(s->fd, PPSETMODE, &m) < 0)
2386
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2388
ParallelCharDriver *drv = chr->opaque;
2393
case CHR_IOCTL_PP_READ_DATA:
2394
if (ioctl(fd, PPRDATA, &b) < 0)
2396
*(uint8_t *)arg = b;
2398
case CHR_IOCTL_PP_WRITE_DATA:
2399
b = *(uint8_t *)arg;
2400
if (ioctl(fd, PPWDATA, &b) < 0)
2403
case CHR_IOCTL_PP_READ_CONTROL:
2404
if (ioctl(fd, PPRCONTROL, &b) < 0)
2406
/* Linux gives only the lowest bits, and no way to know data
2407
direction! For better compatibility set the fixed upper
2409
*(uint8_t *)arg = b | 0xc0;
2411
case CHR_IOCTL_PP_WRITE_CONTROL:
2412
b = *(uint8_t *)arg;
2413
if (ioctl(fd, PPWCONTROL, &b) < 0)
2416
case CHR_IOCTL_PP_READ_STATUS:
2417
if (ioctl(fd, PPRSTATUS, &b) < 0)
2419
*(uint8_t *)arg = b;
2421
case CHR_IOCTL_PP_EPP_READ_ADDR:
2422
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2423
struct ParallelIOArg *parg = arg;
2424
int n = read(fd, parg->buffer, parg->count);
2425
if (n != parg->count) {
2430
case CHR_IOCTL_PP_EPP_READ:
2431
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2432
struct ParallelIOArg *parg = arg;
2433
int n = read(fd, parg->buffer, parg->count);
2434
if (n != parg->count) {
2439
case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2440
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2441
struct ParallelIOArg *parg = arg;
2442
int n = write(fd, parg->buffer, parg->count);
2443
if (n != parg->count) {
2448
case CHR_IOCTL_PP_EPP_WRITE:
2449
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2450
struct ParallelIOArg *parg = arg;
2451
int n = write(fd, parg->buffer, parg->count);
2452
if (n != parg->count) {
2463
static void pp_close(CharDriverState *chr)
2465
ParallelCharDriver *drv = chr->opaque;
2468
pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2469
ioctl(fd, PPRELEASE);
2474
static CharDriverState *qemu_chr_open_pp(const char *filename)
2476
CharDriverState *chr;
2477
ParallelCharDriver *drv;
2480
TFR(fd = open(filename, O_RDWR));
2484
if (ioctl(fd, PPCLAIM) < 0) {
2489
drv = qemu_mallocz(sizeof(ParallelCharDriver));
2495
drv->mode = IEEE1284_MODE_COMPAT;
2497
chr = qemu_mallocz(sizeof(CharDriverState));
2503
chr->chr_write = null_chr_write;
2504
chr->chr_ioctl = pp_ioctl;
2505
chr->chr_close = pp_close;
2508
qemu_chr_reset(chr);
2512
#endif /* __linux__ */
2518
HANDLE hcom, hrecv, hsend;
2519
OVERLAPPED orecv, osend;
2524
#define NSENDBUF 2048
2525
#define NRECVBUF 2048
2526
#define MAXCONNECT 1
2527
#define NTIMEOUT 5000
2529
static int win_chr_poll(void *opaque);
2530
static int win_chr_pipe_poll(void *opaque);
2532
static void win_chr_close(CharDriverState *chr)
2534
WinCharState *s = chr->opaque;
2537
CloseHandle(s->hsend);
2541
CloseHandle(s->hrecv);
2545
CloseHandle(s->hcom);
2549
qemu_del_polling_cb(win_chr_pipe_poll, chr);
2551
qemu_del_polling_cb(win_chr_poll, chr);
2554
static int win_chr_init(CharDriverState *chr, const char *filename)
2556
WinCharState *s = chr->opaque;
2558
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2563
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2565
fprintf(stderr, "Failed CreateEvent\n");
2568
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2570
fprintf(stderr, "Failed CreateEvent\n");
2574
s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2575
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2576
if (s->hcom == INVALID_HANDLE_VALUE) {
2577
fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2582
if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2583
fprintf(stderr, "Failed SetupComm\n");
2587
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2588
size = sizeof(COMMCONFIG);
2589
GetDefaultCommConfig(filename, &comcfg, &size);
2590
comcfg.dcb.DCBlength = sizeof(DCB);
2591
CommConfigDialog(filename, NULL, &comcfg);
2593
if (!SetCommState(s->hcom, &comcfg.dcb)) {
2594
fprintf(stderr, "Failed SetCommState\n");
2598
if (!SetCommMask(s->hcom, EV_ERR)) {
2599
fprintf(stderr, "Failed SetCommMask\n");
2603
cto.ReadIntervalTimeout = MAXDWORD;
2604
if (!SetCommTimeouts(s->hcom, &cto)) {
2605
fprintf(stderr, "Failed SetCommTimeouts\n");
2609
if (!ClearCommError(s->hcom, &err, &comstat)) {
2610
fprintf(stderr, "Failed ClearCommError\n");
2613
qemu_add_polling_cb(win_chr_poll, chr);
2621
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2623
WinCharState *s = chr->opaque;
2624
DWORD len, ret, size, err;
2627
ZeroMemory(&s->osend, sizeof(s->osend));
2628
s->osend.hEvent = s->hsend;
2631
ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2633
ret = WriteFile(s->hcom, buf, len, &size, NULL);
2635
err = GetLastError();
2636
if (err == ERROR_IO_PENDING) {
2637
ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2655
static int win_chr_read_poll(CharDriverState *chr)
2657
WinCharState *s = chr->opaque;
2659
s->max_size = qemu_chr_can_read(chr);
2663
static void win_chr_readfile(CharDriverState *chr)
2665
WinCharState *s = chr->opaque;
2670
ZeroMemory(&s->orecv, sizeof(s->orecv));
2671
s->orecv.hEvent = s->hrecv;
2672
ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2674
err = GetLastError();
2675
if (err == ERROR_IO_PENDING) {
2676
ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2681
qemu_chr_read(chr, buf, size);
2685
static void win_chr_read(CharDriverState *chr)
2687
WinCharState *s = chr->opaque;
2689
if (s->len > s->max_size)
2690
s->len = s->max_size;
2694
win_chr_readfile(chr);
2697
static int win_chr_poll(void *opaque)
2699
CharDriverState *chr = opaque;
2700
WinCharState *s = chr->opaque;
2704
ClearCommError(s->hcom, &comerr, &status);
2705
if (status.cbInQue > 0) {
2706
s->len = status.cbInQue;
2707
win_chr_read_poll(chr);
2714
static CharDriverState *qemu_chr_open_win(const char *filename)
2716
CharDriverState *chr;
2719
chr = qemu_mallocz(sizeof(CharDriverState));
2722
s = qemu_mallocz(sizeof(WinCharState));
2728
chr->chr_write = win_chr_write;
2729
chr->chr_close = win_chr_close;
2731
if (win_chr_init(chr, filename) < 0) {
2736
qemu_chr_reset(chr);
2740
static int win_chr_pipe_poll(void *opaque)
2742
CharDriverState *chr = opaque;
2743
WinCharState *s = chr->opaque;
2746
PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2749
win_chr_read_poll(chr);
2756
static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2758
WinCharState *s = chr->opaque;
2766
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2768
fprintf(stderr, "Failed CreateEvent\n");
2771
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2773
fprintf(stderr, "Failed CreateEvent\n");
2777
snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2778
s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2779
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2781
MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2782
if (s->hcom == INVALID_HANDLE_VALUE) {
2783
fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2788
ZeroMemory(&ov, sizeof(ov));
2789
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2790
ret = ConnectNamedPipe(s->hcom, &ov);
2792
fprintf(stderr, "Failed ConnectNamedPipe\n");
2796
ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2798
fprintf(stderr, "Failed GetOverlappedResult\n");
2800
CloseHandle(ov.hEvent);
2807
CloseHandle(ov.hEvent);
2810
qemu_add_polling_cb(win_chr_pipe_poll, chr);
2819
static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
2821
CharDriverState *chr;
2824
chr = qemu_mallocz(sizeof(CharDriverState));
2827
s = qemu_mallocz(sizeof(WinCharState));
2833
chr->chr_write = win_chr_write;
2834
chr->chr_close = win_chr_close;
2836
if (win_chr_pipe_init(chr, filename) < 0) {
2841
qemu_chr_reset(chr);
2845
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
2847
CharDriverState *chr;
2850
chr = qemu_mallocz(sizeof(CharDriverState));
2853
s = qemu_mallocz(sizeof(WinCharState));
2860
chr->chr_write = win_chr_write;
2861
qemu_chr_reset(chr);
2865
static CharDriverState *qemu_chr_open_win_con(const char *filename)
2867
return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2870
static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
2874
fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2875
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2876
if (fd_out == INVALID_HANDLE_VALUE)
2879
return qemu_chr_open_win_file(fd_out);
2881
#endif /* !_WIN32 */
2883
/***********************************************************/
2884
/* UDP Net console */
2888
struct sockaddr_in daddr;
2895
static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2897
NetCharDriver *s = chr->opaque;
2899
return sendto(s->fd, buf, len, 0,
2900
(struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
2903
static int udp_chr_read_poll(void *opaque)
2905
CharDriverState *chr = opaque;
2906
NetCharDriver *s = chr->opaque;
2908
s->max_size = qemu_chr_can_read(chr);
2910
/* If there were any stray characters in the queue process them
2913
while (s->max_size > 0 && s->bufptr < s->bufcnt) {
2914
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
2916
s->max_size = qemu_chr_can_read(chr);
2921
static void udp_chr_read(void *opaque)
2923
CharDriverState *chr = opaque;
2924
NetCharDriver *s = chr->opaque;
2926
if (s->max_size == 0)
2928
s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
2929
s->bufptr = s->bufcnt;
2934
while (s->max_size > 0 && s->bufptr < s->bufcnt) {
2935
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
2937
s->max_size = qemu_chr_can_read(chr);
2941
static void udp_chr_update_read_handler(CharDriverState *chr)
2943
NetCharDriver *s = chr->opaque;
2946
qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2947
udp_chr_read, NULL, chr);
2951
int parse_host_port(struct sockaddr_in *saddr, const char *str);
2953
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
2955
int parse_host_src_port(struct sockaddr_in *haddr,
2956
struct sockaddr_in *saddr,
2959
static CharDriverState *qemu_chr_open_udp(const char *def)
2961
CharDriverState *chr = NULL;
2962
NetCharDriver *s = NULL;
2964
struct sockaddr_in saddr;
2966
chr = qemu_mallocz(sizeof(CharDriverState));
2969
s = qemu_mallocz(sizeof(NetCharDriver));
2973
fd = socket(PF_INET, SOCK_DGRAM, 0);
2975
perror("socket(PF_INET, SOCK_DGRAM)");
2979
if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
2980
printf("Could not parse: %s\n", def);
2984
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
2994
chr->chr_write = udp_chr_write;
2995
chr->chr_update_read_handler = udp_chr_update_read_handler;
3008
/***********************************************************/
3009
/* TCP Net console */
3020
static void tcp_chr_accept(void *opaque);
3022
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3024
TCPCharDriver *s = chr->opaque;
3026
return send_all(s->fd, buf, len);
3028
/* XXX: indicate an error ? */
3033
static int tcp_chr_read_poll(void *opaque)
3035
CharDriverState *chr = opaque;
3036
TCPCharDriver *s = chr->opaque;
3039
s->max_size = qemu_chr_can_read(chr);
3044
#define IAC_BREAK 243
3045
static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3047
uint8_t *buf, int *size)
3049
/* Handle any telnet client's basic IAC options to satisfy char by
3050
* char mode with no echo. All IAC options will be removed from
3051
* the buf and the do_telnetopt variable will be used to track the
3052
* state of the width of the IAC information.
3054
* IAC commands come in sets of 3 bytes with the exception of the
3055
* "IAC BREAK" command and the double IAC.
3061
for (i = 0; i < *size; i++) {
3062
if (s->do_telnetopt > 1) {
3063
if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3064
/* Double IAC means send an IAC */
3068
s->do_telnetopt = 1;
3070
if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3071
/* Handle IAC break commands by sending a serial break */
3072
qemu_chr_event(chr, CHR_EVENT_BREAK);
3077
if (s->do_telnetopt >= 4) {
3078
s->do_telnetopt = 1;
3081
if ((unsigned char)buf[i] == IAC) {
3082
s->do_telnetopt = 2;
3093
static void tcp_chr_read(void *opaque)
3095
CharDriverState *chr = opaque;
3096
TCPCharDriver *s = chr->opaque;
3100
if (!s->connected || s->max_size <= 0)
3103
if (len > s->max_size)
3105
size = recv(s->fd, buf, len, 0);
3107
/* connection closed */
3109
if (s->listen_fd >= 0) {
3110
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3112
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3115
} else if (size > 0) {
3116
if (s->do_telnetopt)
3117
tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3119
qemu_chr_read(chr, buf, size);
3123
static void tcp_chr_connect(void *opaque)
3125
CharDriverState *chr = opaque;
3126
TCPCharDriver *s = chr->opaque;
3129
qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3130
tcp_chr_read, NULL, chr);
3131
qemu_chr_reset(chr);
3134
#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3135
static void tcp_chr_telnet_init(int fd)
3138
/* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3139
IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3140
send(fd, (char *)buf, 3, 0);
3141
IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3142
send(fd, (char *)buf, 3, 0);
3143
IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3144
send(fd, (char *)buf, 3, 0);
3145
IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3146
send(fd, (char *)buf, 3, 0);
3149
static void socket_set_nodelay(int fd)
3152
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3155
static void tcp_chr_accept(void *opaque)
3157
CharDriverState *chr = opaque;
3158
TCPCharDriver *s = chr->opaque;
3159
struct sockaddr_in saddr;
3161
struct sockaddr_un uaddr;
3163
struct sockaddr *addr;
3170
len = sizeof(uaddr);
3171
addr = (struct sockaddr *)&uaddr;
3175
len = sizeof(saddr);
3176
addr = (struct sockaddr *)&saddr;
3178
fd = accept(s->listen_fd, addr, &len);
3179
if (fd < 0 && errno != EINTR) {
3181
} else if (fd >= 0) {
3182
if (s->do_telnetopt)
3183
tcp_chr_telnet_init(fd);
3187
socket_set_nonblock(fd);
3189
socket_set_nodelay(fd);
3191
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3192
tcp_chr_connect(chr);
3195
static void tcp_chr_close(CharDriverState *chr)
3197
TCPCharDriver *s = chr->opaque;
3200
if (s->listen_fd >= 0)
3201
closesocket(s->listen_fd);
3205
static CharDriverState *qemu_chr_open_tcp(const char *host_str,
3209
CharDriverState *chr = NULL;
3210
TCPCharDriver *s = NULL;
3211
int fd = -1, ret, err, val;
3213
int is_waitconnect = 1;
3216
struct sockaddr_in saddr;
3218
struct sockaddr_un uaddr;
3220
struct sockaddr *addr;
3225
addr = (struct sockaddr *)&uaddr;
3226
addrlen = sizeof(uaddr);
3227
if (parse_unix_path(&uaddr, host_str) < 0)
3232
addr = (struct sockaddr *)&saddr;
3233
addrlen = sizeof(saddr);
3234
if (parse_host_port(&saddr, host_str) < 0)
3239
while((ptr = strchr(ptr,','))) {
3241
if (!strncmp(ptr,"server",6)) {
3243
} else if (!strncmp(ptr,"nowait",6)) {
3245
} else if (!strncmp(ptr,"nodelay",6)) {
3248
printf("Unknown option: %s\n", ptr);
3255
chr = qemu_mallocz(sizeof(CharDriverState));
3258
s = qemu_mallocz(sizeof(TCPCharDriver));
3264
fd = socket(PF_UNIX, SOCK_STREAM, 0);
3267
fd = socket(PF_INET, SOCK_STREAM, 0);
3272
if (!is_waitconnect)
3273
socket_set_nonblock(fd);
3278
s->is_unix = is_unix;
3279
s->do_nodelay = do_nodelay && !is_unix;
3282
chr->chr_write = tcp_chr_write;
3283
chr->chr_close = tcp_chr_close;
3286
/* allow fast reuse */
3290
strncpy(path, uaddr.sun_path, 108);
3297
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3300
ret = bind(fd, addr, addrlen);
3304
ret = listen(fd, 0);
3309
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3311
s->do_telnetopt = 1;
3314
ret = connect(fd, addr, addrlen);
3316
err = socket_error();
3317
if (err == EINTR || err == EWOULDBLOCK) {
3318
} else if (err == EINPROGRESS) {
3321
} else if (err == WSAEALREADY) {
3333
socket_set_nodelay(fd);
3335
tcp_chr_connect(chr);
3337
qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3340
if (is_listen && is_waitconnect) {
3341
printf("QEMU waiting for connection on: %s\n", host_str);
3342
tcp_chr_accept(chr);
3343
socket_set_nonblock(s->listen_fd);
3355
CharDriverState *qemu_chr_open(const char *filename)
3359
if (!strcmp(filename, "vc")) {
3360
return text_console_init(&display_state, 0);
3361
} else if (strstart(filename, "vc:", &p)) {
3362
return text_console_init(&display_state, p);
3363
} else if (!strcmp(filename, "null")) {
3364
return qemu_chr_open_null();
3366
if (strstart(filename, "tcp:", &p)) {
3367
return qemu_chr_open_tcp(p, 0, 0);
3369
if (strstart(filename, "telnet:", &p)) {
3370
return qemu_chr_open_tcp(p, 1, 0);
3372
if (strstart(filename, "udp:", &p)) {
3373
return qemu_chr_open_udp(p);
3375
if (strstart(filename, "mon:", &p)) {
3376
CharDriverState *drv = qemu_chr_open(p);
3378
drv = qemu_chr_open_mux(drv);
3379
monitor_init(drv, !nographic);
3382
printf("Unable to open driver: %s\n", p);
3386
if (strstart(filename, "unix:", &p)) {
3387
return qemu_chr_open_tcp(p, 0, 1);
3388
} else if (strstart(filename, "file:", &p)) {
3389
return qemu_chr_open_file_out(p);
3390
} else if (strstart(filename, "pipe:", &p)) {
3391
return qemu_chr_open_pipe(p);
3392
} else if (!strcmp(filename, "pty")) {
3393
return qemu_chr_open_pty();
3394
} else if (!strcmp(filename, "stdio")) {
3395
return qemu_chr_open_stdio();
3397
#if defined(__linux__)
3398
if (strstart(filename, "/dev/parport", NULL)) {
3399
return qemu_chr_open_pp(filename);
3402
#if defined(__linux__) || defined(__sun__)
3403
if (strstart(filename, "/dev/", NULL)) {
3404
return qemu_chr_open_tty(filename);
3408
if (strstart(filename, "COM", NULL)) {
3409
return qemu_chr_open_win(filename);
3411
if (strstart(filename, "pipe:", &p)) {
3412
return qemu_chr_open_win_pipe(p);
3414
if (strstart(filename, "con:", NULL)) {
3415
return qemu_chr_open_win_con(filename);
3417
if (strstart(filename, "file:", &p)) {
3418
return qemu_chr_open_win_file_out(p);
3426
void qemu_chr_close(CharDriverState *chr)
3429
chr->chr_close(chr);
3432
/***********************************************************/
3433
/* network device redirectors */
3435
__attribute__ (( unused ))
3436
static void hex_dump(FILE *f, const uint8_t *buf, int size)
3440
for(i=0;i<size;i+=16) {
3444
fprintf(f, "%08x ", i);
3447
fprintf(f, " %02x", buf[i+j]);
3452
for(j=0;j<len;j++) {
3454
if (c < ' ' || c > '~')
3456
fprintf(f, "%c", c);
3462
static int parse_macaddr(uint8_t *macaddr, const char *p)
3469
offset = strtol(p, &last_char, 0);
3470
if (0 == errno && '\0' == *last_char &&
3471
offset >= 0 && offset <= 0xFFFFFF) {
3472
macaddr[3] = (offset & 0xFF0000) >> 16;
3473
macaddr[4] = (offset & 0xFF00) >> 8;
3474
macaddr[5] = offset & 0xFF;
3477
for(i = 0; i < 6; i++) {
3478
macaddr[i] = strtol(p, (char **)&p, 16);
3483
if (*p != ':' && *p != '-')
3494
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3499
p1 = strchr(p, sep);
3505
if (len > buf_size - 1)
3507
memcpy(buf, p, len);
3514
int parse_host_src_port(struct sockaddr_in *haddr,
3515
struct sockaddr_in *saddr,
3516
const char *input_str)
3518
char *str = strdup(input_str);
3519
char *host_str = str;
3524
* Chop off any extra arguments at the end of the string which
3525
* would start with a comma, then fill in the src port information
3526
* if it was provided else use the "any address" and "any port".
3528
if ((ptr = strchr(str,',')))
3531
if ((src_str = strchr(input_str,'@'))) {
3536
if (parse_host_port(haddr, host_str) < 0)
3539
if (!src_str || *src_str == '\0')
3542
if (parse_host_port(saddr, src_str) < 0)
3553
int parse_host_port(struct sockaddr_in *saddr, const char *str)
3561
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3563
saddr->sin_family = AF_INET;
3564
if (buf[0] == '\0') {
3565
saddr->sin_addr.s_addr = 0;
3567
if (isdigit(buf[0])) {
3568
if (!inet_aton(buf, &saddr->sin_addr))
3571
if ((he = gethostbyname(buf)) == NULL)
3573
saddr->sin_addr = *(struct in_addr *)he->h_addr;
3576
port = strtol(p, (char **)&r, 0);
3579
saddr->sin_port = htons(port);
3584
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
3589
len = MIN(108, strlen(str));
3590
p = strchr(str, ',');
3592
len = MIN(len, p - str);
3594
memset(uaddr, 0, sizeof(*uaddr));
3596
uaddr->sun_family = AF_UNIX;
3597
memcpy(uaddr->sun_path, str, len);
3603
/* find or alloc a new VLAN */
3604
VLANState *qemu_find_vlan(int id)
3606
VLANState **pvlan, *vlan;
3607
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3611
vlan = qemu_mallocz(sizeof(VLANState));
3616
pvlan = &first_vlan;
3617
while (*pvlan != NULL)
3618
pvlan = &(*pvlan)->next;
3623
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
3624
IOReadHandler *fd_read,
3625
IOCanRWHandler *fd_can_read,
3628
VLANClientState *vc, **pvc;
3629
vc = qemu_mallocz(sizeof(VLANClientState));
3632
vc->fd_read = fd_read;
3633
vc->fd_can_read = fd_can_read;
3634
vc->opaque = opaque;
3638
pvc = &vlan->first_client;
3639
while (*pvc != NULL)
3640
pvc = &(*pvc)->next;
3645
int qemu_can_send_packet(VLANClientState *vc1)
3647
VLANState *vlan = vc1->vlan;
3648
VLANClientState *vc;
3650
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3652
if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3659
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3661
VLANState *vlan = vc1->vlan;
3662
VLANClientState *vc;
3665
printf("vlan %d send:\n", vlan->id);
3666
hex_dump(stdout, buf, size);
3668
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3670
vc->fd_read(vc->opaque, buf, size);
3675
#if defined(CONFIG_SLIRP)
3677
/* slirp network adapter */
3679
static int slirp_inited;
3680
static VLANClientState *slirp_vc;
3682
int slirp_can_output(void)
3684
return !slirp_vc || qemu_can_send_packet(slirp_vc);
3687
void slirp_output(const uint8_t *pkt, int pkt_len)
3690
printf("slirp output:\n");
3691
hex_dump(stdout, pkt, pkt_len);
3695
qemu_send_packet(slirp_vc, pkt, pkt_len);
3698
static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3701
printf("slirp input:\n");
3702
hex_dump(stdout, buf, size);
3704
slirp_input(buf, size);
3707
static int net_slirp_init(VLANState *vlan)
3709
if (!slirp_inited) {
3713
slirp_vc = qemu_new_vlan_client(vlan,
3714
slirp_receive, NULL, NULL);
3715
snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3719
static void net_slirp_redir(const char *redir_str)
3724
struct in_addr guest_addr;
3725
int host_port, guest_port;
3727
if (!slirp_inited) {
3733
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3735
if (!strcmp(buf, "tcp")) {
3737
} else if (!strcmp(buf, "udp")) {
3743
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3745
host_port = strtol(buf, &r, 0);
3749
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3751
if (buf[0] == '\0') {
3752
pstrcpy(buf, sizeof(buf), "10.0.2.15");
3754
if (!inet_aton(buf, &guest_addr))
3757
guest_port = strtol(p, &r, 0);
3761
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3762
fprintf(stderr, "qemu: could not set up redirection\n");
3767
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3775
static void smb_exit(void)
3779
char filename[1024];
3781
/* erase all the files in the directory */
3782
d = opendir(smb_dir);
3787
if (strcmp(de->d_name, ".") != 0 &&
3788
strcmp(de->d_name, "..") != 0) {
3789
snprintf(filename, sizeof(filename), "%s/%s",
3790
smb_dir, de->d_name);
3798
/* automatic user mode samba server configuration */
3799
static void net_slirp_smb(const char *exported_dir)
3801
char smb_conf[1024];
3802
char smb_cmdline[1024];
3805
if (!slirp_inited) {
3810
/* XXX: better tmp dir construction */
3811
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3812
if (mkdir(smb_dir, 0700) < 0) {
3813
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3816
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
3818
f = fopen(smb_conf, "w");
3820
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3827
"socket address=127.0.0.1\n"
3828
"pid directory=%s\n"
3829
"lock directory=%s\n"
3830
"log file=%s/log.smbd\n"
3831
"smb passwd file=%s/smbpasswd\n"
3832
"security = share\n"
3847
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3848
SMBD_COMMAND, smb_conf);
3850
slirp_add_exec(0, smb_cmdline, 4, 139);
3853
#endif /* !defined(_WIN32) */
3854
void do_info_slirp(void)
3859
#endif /* CONFIG_SLIRP */
3861
#if !defined(_WIN32)
3863
typedef struct TAPState {
3864
VLANClientState *vc;
3866
char down_script[1024];
3869
static void tap_receive(void *opaque, const uint8_t *buf, int size)
3871
TAPState *s = opaque;
3874
ret = write(s->fd, buf, size);
3875
if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
3882
static void tap_send(void *opaque)
3884
TAPState *s = opaque;
3891
sbuf.maxlen = sizeof(buf);
3893
size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
3895
size = read(s->fd, buf, sizeof(buf));
3898
qemu_send_packet(s->vc, buf, size);
3904
static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
3908
s = qemu_mallocz(sizeof(TAPState));
3912
s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
3913
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
3914
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
3918
#if defined (_BSD) || defined (__FreeBSD_kernel__)
3919
static int tap_open(char *ifname, int ifname_size)
3925
TFR(fd = open("/dev/tap", O_RDWR));
3927
fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
3932
dev = devname(s.st_rdev, S_IFCHR);
3933
pstrcpy(ifname, ifname_size, dev);
3935
fcntl(fd, F_SETFL, O_NONBLOCK);
3938
#elif defined(__sun__)
3939
#define TUNNEWPPA (('T'<<16) | 0x0001)
3941
* Allocate TAP device, returns opened fd.
3942
* Stores dev name in the first arg(must be large enough).
3944
int tap_alloc(char *dev)
3946
int tap_fd, if_fd, ppa = -1;
3947
static int ip_fd = 0;
3950
static int arp_fd = 0;
3951
int ip_muxid, arp_muxid;
3952
struct strioctl strioc_if, strioc_ppa;
3953
int link_type = I_PLINK;;
3955
char actual_name[32] = "";
3957
memset(&ifr, 0x0, sizeof(ifr));
3961
while( *ptr && !isdigit((int)*ptr) ) ptr++;
3965
/* Check if IP device was opened */
3969
TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
3971
syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
3975
TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
3977
syslog(LOG_ERR, "Can't open /dev/tap");
3981
/* Assign a new PPA and get its unit number. */
3982
strioc_ppa.ic_cmd = TUNNEWPPA;
3983
strioc_ppa.ic_timout = 0;
3984
strioc_ppa.ic_len = sizeof(ppa);
3985
strioc_ppa.ic_dp = (char *)&ppa;
3986
if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
3987
syslog (LOG_ERR, "Can't assign new interface");
3989
TFR(if_fd = open("/dev/tap", O_RDWR, 0));
3991
syslog(LOG_ERR, "Can't open /dev/tap (2)");
3994
if(ioctl(if_fd, I_PUSH, "ip") < 0){
3995
syslog(LOG_ERR, "Can't push IP module");
3999
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
4000
syslog(LOG_ERR, "Can't get flags\n");
4002
snprintf (actual_name, 32, "tap%d", ppa);
4003
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4006
/* Assign ppa according to the unit number returned by tun device */
4008
if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
4009
syslog (LOG_ERR, "Can't set PPA %d", ppa);
4010
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
4011
syslog (LOG_ERR, "Can't get flags\n");
4012
/* Push arp module to if_fd */
4013
if (ioctl (if_fd, I_PUSH, "arp") < 0)
4014
syslog (LOG_ERR, "Can't push ARP module (2)");
4016
/* Push arp module to ip_fd */
4017
if (ioctl (ip_fd, I_POP, NULL) < 0)
4018
syslog (LOG_ERR, "I_POP failed\n");
4019
if (ioctl (ip_fd, I_PUSH, "arp") < 0)
4020
syslog (LOG_ERR, "Can't push ARP module (3)\n");
4022
TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
4024
syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
4026
/* Set ifname to arp */
4027
strioc_if.ic_cmd = SIOCSLIFNAME;
4028
strioc_if.ic_timout = 0;
4029
strioc_if.ic_len = sizeof(ifr);
4030
strioc_if.ic_dp = (char *)𝔦
4031
if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
4032
syslog (LOG_ERR, "Can't set ifname to arp\n");
4035
if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
4036
syslog(LOG_ERR, "Can't link TAP device to IP");
4040
if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
4041
syslog (LOG_ERR, "Can't link TAP device to ARP");
4045
memset(&ifr, 0x0, sizeof(ifr));
4046
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4047
ifr.lifr_ip_muxid = ip_muxid;
4048
ifr.lifr_arp_muxid = arp_muxid;
4050
if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
4052
ioctl (ip_fd, I_PUNLINK , arp_muxid);
4053
ioctl (ip_fd, I_PUNLINK, ip_muxid);
4054
syslog (LOG_ERR, "Can't set multiplexor id");
4057
sprintf(dev, "tap%d", ppa);
4061
static int tap_open(char *ifname, int ifname_size)
4065
if( (fd = tap_alloc(dev)) < 0 ){
4066
fprintf(stderr, "Cannot allocate TAP device\n");
4069
pstrcpy(ifname, ifname_size, dev);
4070
fcntl(fd, F_SETFL, O_NONBLOCK);
4074
static int tap_open(char *ifname, int ifname_size)
4079
TFR(fd = open("/dev/net/tun", O_RDWR));
4081
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4084
memset(&ifr, 0, sizeof(ifr));
4085
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
4086
if (ifname[0] != '\0')
4087
pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4089
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
4090
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4092
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4096
pstrcpy(ifname, ifname_size, ifr.ifr_name);
4097
fcntl(fd, F_SETFL, O_NONBLOCK);
4102
static int launch_script(const char *setup_script, const char *ifname, int fd)
4108
/* try to launch network script */
4112
int open_max = sysconf (_SC_OPEN_MAX), i;
4113
for (i = 0; i < open_max; i++)
4114
if (i != STDIN_FILENO &&
4115
i != STDOUT_FILENO &&
4116
i != STDERR_FILENO &&
4121
*parg++ = (char *)setup_script;
4122
*parg++ = (char *)ifname;
4124
execv(setup_script, args);
4127
while (waitpid(pid, &status, 0) != pid);
4128
if (!WIFEXITED(status) ||
4129
WEXITSTATUS(status) != 0) {
4130
fprintf(stderr, "%s: could not launch network script\n",
4138
static int net_tap_init(VLANState *vlan, const char *ifname1,
4139
const char *setup_script, const char *down_script)
4145
if (ifname1 != NULL)
4146
pstrcpy(ifname, sizeof(ifname), ifname1);
4149
TFR(fd = tap_open(ifname, sizeof(ifname)));
4153
if (!setup_script || !strcmp(setup_script, "no"))
4155
if (setup_script[0] != '\0') {
4156
if (launch_script(setup_script, ifname, fd))
4159
s = net_tap_fd_init(vlan, fd);
4162
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4163
"tap: ifname=%s setup_script=%s", ifname, setup_script);
4164
if (down_script && strcmp(down_script, "no"))
4165
snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
4169
#endif /* !_WIN32 */
4171
/* network connection */
4172
typedef struct NetSocketState {
4173
VLANClientState *vc;
4175
int state; /* 0 = getting length, 1 = getting data */
4179
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4182
typedef struct NetSocketListenState {
4185
} NetSocketListenState;
4187
/* XXX: we consider we can send the whole packet without blocking */
4188
static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
4190
NetSocketState *s = opaque;
4194
send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4195
send_all(s->fd, buf, size);
4198
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4200
NetSocketState *s = opaque;
4201
sendto(s->fd, buf, size, 0,
4202
(struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4205
static void net_socket_send(void *opaque)
4207
NetSocketState *s = opaque;
4212
size = recv(s->fd, buf1, sizeof(buf1), 0);
4214
err = socket_error();
4215
if (err != EWOULDBLOCK)
4217
} else if (size == 0) {
4218
/* end of connection */
4220
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4226
/* reassemble a packet from the network */
4232
memcpy(s->buf + s->index, buf, l);
4236
if (s->index == 4) {
4238
s->packet_len = ntohl(*(uint32_t *)s->buf);
4244
l = s->packet_len - s->index;
4247
memcpy(s->buf + s->index, buf, l);
4251
if (s->index >= s->packet_len) {
4252
qemu_send_packet(s->vc, s->buf, s->packet_len);
4261
static void net_socket_send_dgram(void *opaque)
4263
NetSocketState *s = opaque;
4266
size = recv(s->fd, s->buf, sizeof(s->buf), 0);
4270
/* end of connection */
4271
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4274
qemu_send_packet(s->vc, s->buf, size);
4277
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4282
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4283
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4284
inet_ntoa(mcastaddr->sin_addr),
4285
(int)ntohl(mcastaddr->sin_addr.s_addr));
4289
fd = socket(PF_INET, SOCK_DGRAM, 0);
4291
perror("socket(PF_INET, SOCK_DGRAM)");
4296
ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
4297
(const char *)&val, sizeof(val));
4299
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4303
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4309
/* Add host to multicast group */
4310
imr.imr_multiaddr = mcastaddr->sin_addr;
4311
imr.imr_interface.s_addr = htonl(INADDR_ANY);
4313
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
4314
(const char *)&imr, sizeof(struct ip_mreq));
4316
perror("setsockopt(IP_ADD_MEMBERSHIP)");
4320
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
4322
ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
4323
(const char *)&val, sizeof(val));
4325
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4329
socket_set_nonblock(fd);
4337
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
4340
struct sockaddr_in saddr;
4342
socklen_t saddr_len;
4345
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4346
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
4347
* by ONLY ONE process: we must "clone" this dgram socket --jjo
4351
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4353
if (saddr.sin_addr.s_addr==0) {
4354
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4358
/* clone dgram socket */
4359
newfd = net_socket_mcast_create(&saddr);
4361
/* error already reported by net_socket_mcast_create() */
4365
/* clone newfd to fd, close newfd */
4370
fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4371
fd, strerror(errno));
4376
s = qemu_mallocz(sizeof(NetSocketState));
4381
s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
4382
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4384
/* mcast: save bound address as dst */
4385
if (is_connected) s->dgram_dst=saddr;
4387
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4388
"socket: fd=%d (%s mcast=%s:%d)",
4389
fd, is_connected? "cloned" : "",
4390
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4394
static void net_socket_connect(void *opaque)
4396
NetSocketState *s = opaque;
4397
qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4400
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
4404
s = qemu_mallocz(sizeof(NetSocketState));
4408
s->vc = qemu_new_vlan_client(vlan,
4409
net_socket_receive, NULL, s);
4410
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4411
"socket: fd=%d", fd);
4413
net_socket_connect(s);
4415
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4420
static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
4423
int so_type=-1, optlen=sizeof(so_type);
4425
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
4426
(socklen_t *)&optlen)< 0) {
4427
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
4432
return net_socket_fd_init_dgram(vlan, fd, is_connected);
4434
return net_socket_fd_init_stream(vlan, fd, is_connected);
4436
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
4437
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4438
return net_socket_fd_init_stream(vlan, fd, is_connected);
4443
static void net_socket_accept(void *opaque)
4445
NetSocketListenState *s = opaque;
4447
struct sockaddr_in saddr;
4452
len = sizeof(saddr);
4453
fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4454
if (fd < 0 && errno != EINTR) {
4456
} else if (fd >= 0) {
4460
s1 = net_socket_fd_init(s->vlan, fd, 1);
4464
snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
4465
"socket: connection from %s:%d",
4466
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4470
static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4472
NetSocketListenState *s;
4474
struct sockaddr_in saddr;
4476
if (parse_host_port(&saddr, host_str) < 0)
4479
s = qemu_mallocz(sizeof(NetSocketListenState));
4483
fd = socket(PF_INET, SOCK_STREAM, 0);
4488
socket_set_nonblock(fd);
4490
/* allow fast reuse */
4492
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
4494
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4499
ret = listen(fd, 0);
4506
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4510
static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4513
int fd, connected, ret, err;
4514
struct sockaddr_in saddr;
4516
if (parse_host_port(&saddr, host_str) < 0)
4519
fd = socket(PF_INET, SOCK_STREAM, 0);
4524
socket_set_nonblock(fd);
4528
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4530
err = socket_error();
4531
if (err == EINTR || err == EWOULDBLOCK) {
4532
} else if (err == EINPROGRESS) {
4535
} else if (err == WSAEALREADY) {
4548
s = net_socket_fd_init(vlan, fd, connected);
4551
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4552
"socket: connect to %s:%d",
4553
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4557
static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4561
struct sockaddr_in saddr;
4563
if (parse_host_port(&saddr, host_str) < 0)
4567
fd = net_socket_mcast_create(&saddr);
4571
s = net_socket_fd_init(vlan, fd, 0);
4575
s->dgram_dst = saddr;
4577
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4578
"socket: mcast=%s:%d",
4579
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4584
static const char *get_word(char *buf, int buf_size, const char *p)
1853
const char *get_opt_name(char *buf, int buf_size, const char *p)
1858
while (*p != '\0' && *p != '=') {
1859
if (q && (q - buf) < buf_size - 1)
1869
const char *get_opt_value(char *buf, int buf_size, const char *p)
4591
1874
while (*p != '\0') {
1876
if (*(p + 1) != ',')
4596
} else if (*p == '\"') {
4597
substring = !substring;
4600
} else if (!substring && (*p == ',' || *p == '='))
4602
1880
if (q && (q - buf) < buf_size - 1)
5537
3018
/***********************************************************/
5538
/* savevm/loadvm support */
5540
#define IO_BUF_SIZE 32768
5544
BlockDriverState *bs;
5547
int64_t base_offset;
5548
int64_t buf_offset; /* start of buffer when writing, end of buffer
5551
int buf_size; /* 0 when writing */
5552
uint8_t buf[IO_BUF_SIZE];
5555
QEMUFile *qemu_fopen(const char *filename, const char *mode)
5559
f = qemu_mallocz(sizeof(QEMUFile));
5562
if (!strcmp(mode, "wb")) {
5564
} else if (!strcmp(mode, "rb")) {
5569
f->outfile = fopen(filename, mode);
5581
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
5585
f = qemu_mallocz(sizeof(QEMUFile));
5590
f->is_writable = is_writable;
5591
f->base_offset = offset;
5595
void qemu_fflush(QEMUFile *f)
5597
if (!f->is_writable)
5599
if (f->buf_index > 0) {
5601
fseek(f->outfile, f->buf_offset, SEEK_SET);
5602
fwrite(f->buf, 1, f->buf_index, f->outfile);
5604
bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
5605
f->buf, f->buf_index);
5607
f->buf_offset += f->buf_index;
5612
static void qemu_fill_buffer(QEMUFile *f)
5619
fseek(f->outfile, f->buf_offset, SEEK_SET);
5620
len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
5624
len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
5625
f->buf, IO_BUF_SIZE);
5631
f->buf_offset += len;
5634
void qemu_fclose(QEMUFile *f)
5644
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
5648
l = IO_BUF_SIZE - f->buf_index;
5651
memcpy(f->buf + f->buf_index, buf, l);
5655
if (f->buf_index >= IO_BUF_SIZE)
5660
void qemu_put_byte(QEMUFile *f, int v)
5662
f->buf[f->buf_index++] = v;
5663
if (f->buf_index >= IO_BUF_SIZE)
5667
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
5673
l = f->buf_size - f->buf_index;
5675
qemu_fill_buffer(f);
5676
l = f->buf_size - f->buf_index;
5682
memcpy(buf, f->buf + f->buf_index, l);
5687
return size1 - size;
5690
int qemu_get_byte(QEMUFile *f)
5692
if (f->buf_index >= f->buf_size) {
5693
qemu_fill_buffer(f);
5694
if (f->buf_index >= f->buf_size)
5697
return f->buf[f->buf_index++];
5700
int64_t qemu_ftell(QEMUFile *f)
5702
return f->buf_offset - f->buf_size + f->buf_index;
5705
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
5707
if (whence == SEEK_SET) {
5709
} else if (whence == SEEK_CUR) {
5710
pos += qemu_ftell(f);
5712
/* SEEK_END not supported */
5715
if (f->is_writable) {
5717
f->buf_offset = pos;
5719
f->buf_offset = pos;
5726
void qemu_put_be16(QEMUFile *f, unsigned int v)
5728
qemu_put_byte(f, v >> 8);
5729
qemu_put_byte(f, v);
5732
void qemu_put_be32(QEMUFile *f, unsigned int v)
5734
qemu_put_byte(f, v >> 24);
5735
qemu_put_byte(f, v >> 16);
5736
qemu_put_byte(f, v >> 8);
5737
qemu_put_byte(f, v);
5740
void qemu_put_be64(QEMUFile *f, uint64_t v)
5742
qemu_put_be32(f, v >> 32);
5743
qemu_put_be32(f, v);
5746
unsigned int qemu_get_be16(QEMUFile *f)
5749
v = qemu_get_byte(f) << 8;
5750
v |= qemu_get_byte(f);
5754
unsigned int qemu_get_be32(QEMUFile *f)
5757
v = qemu_get_byte(f) << 24;
5758
v |= qemu_get_byte(f) << 16;
5759
v |= qemu_get_byte(f) << 8;
5760
v |= qemu_get_byte(f);
5764
uint64_t qemu_get_be64(QEMUFile *f)
5767
v = (uint64_t)qemu_get_be32(f) << 32;
5768
v |= qemu_get_be32(f);
5772
typedef struct SaveStateEntry {
5776
SaveStateHandler *save_state;
5777
LoadStateHandler *load_state;
5779
struct SaveStateEntry *next;
5782
static SaveStateEntry *first_se;
5784
int register_savevm(const char *idstr,
5787
SaveStateHandler *save_state,
5788
LoadStateHandler *load_state,
5791
SaveStateEntry *se, **pse;
5793
se = qemu_malloc(sizeof(SaveStateEntry));
5796
pstrcpy(se->idstr, sizeof(se->idstr), idstr);
5797
se->instance_id = instance_id;
5798
se->version_id = version_id;
5799
se->save_state = save_state;
5800
se->load_state = load_state;
5801
se->opaque = opaque;
5804
/* add at the end of list */
5806
while (*pse != NULL)
5807
pse = &(*pse)->next;
5812
#define QEMU_VM_FILE_MAGIC 0x5145564d
5813
#define QEMU_VM_FILE_VERSION 0x00000002
5815
static int qemu_savevm_state(QEMUFile *f)
5819
int64_t cur_pos, len_pos, total_len_pos;
5821
qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
5822
qemu_put_be32(f, QEMU_VM_FILE_VERSION);
5823
total_len_pos = qemu_ftell(f);
5824
qemu_put_be64(f, 0); /* total size */
5826
for(se = first_se; se != NULL; se = se->next) {
5828
len = strlen(se->idstr);
5829
qemu_put_byte(f, len);
5830
qemu_put_buffer(f, (uint8_t *)se->idstr, len);
5832
qemu_put_be32(f, se->instance_id);
5833
qemu_put_be32(f, se->version_id);
5835
/* record size: filled later */
5836
len_pos = qemu_ftell(f);
5837
qemu_put_be32(f, 0);
5838
se->save_state(f, se->opaque);
5840
/* fill record size */
5841
cur_pos = qemu_ftell(f);
5842
len = cur_pos - len_pos - 4;
5843
qemu_fseek(f, len_pos, SEEK_SET);
5844
qemu_put_be32(f, len);
5845
qemu_fseek(f, cur_pos, SEEK_SET);
5847
cur_pos = qemu_ftell(f);
5848
qemu_fseek(f, total_len_pos, SEEK_SET);
5849
qemu_put_be64(f, cur_pos - total_len_pos - 8);
5850
qemu_fseek(f, cur_pos, SEEK_SET);
5856
static SaveStateEntry *find_se(const char *idstr, int instance_id)
5860
for(se = first_se; se != NULL; se = se->next) {
5861
if (!strcmp(se->idstr, idstr) &&
5862
instance_id == se->instance_id)
5868
static int qemu_loadvm_state(QEMUFile *f)
5871
int len, ret, instance_id, record_len, version_id;
5872
int64_t total_len, end_pos, cur_pos;
5876
v = qemu_get_be32(f);
5877
if (v != QEMU_VM_FILE_MAGIC)
5879
v = qemu_get_be32(f);
5880
if (v != QEMU_VM_FILE_VERSION) {
5885
total_len = qemu_get_be64(f);
5886
end_pos = total_len + qemu_ftell(f);
5888
if (qemu_ftell(f) >= end_pos)
5890
len = qemu_get_byte(f);
5891
qemu_get_buffer(f, (uint8_t *)idstr, len);
5893
instance_id = qemu_get_be32(f);
5894
version_id = qemu_get_be32(f);
5895
record_len = qemu_get_be32(f);
5897
printf("idstr=%s instance=0x%x version=%d len=%d\n",
5898
idstr, instance_id, version_id, record_len);
5900
cur_pos = qemu_ftell(f);
5901
se = find_se(idstr, instance_id);
5903
fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5904
instance_id, idstr);
5906
ret = se->load_state(f, se->opaque, version_id);
5908
fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5909
instance_id, idstr);
5912
/* always seek to exact end of record */
5913
qemu_fseek(f, cur_pos + record_len, SEEK_SET);
5920
/* device can contain snapshots */
5921
static int bdrv_can_snapshot(BlockDriverState *bs)
5924
!bdrv_is_removable(bs) &&
5925
!bdrv_is_read_only(bs));
5928
/* device must be snapshots in order to have a reliable snapshot */
5929
static int bdrv_has_snapshot(BlockDriverState *bs)
5932
!bdrv_is_removable(bs) &&
5933
!bdrv_is_read_only(bs));
5936
static BlockDriverState *get_bs_snapshots(void)
5938
BlockDriverState *bs;
5942
return bs_snapshots;
5943
for(i = 0; i <= nb_drives; i++) {
5944
bs = drives_table[i].bdrv;
5945
if (bdrv_can_snapshot(bs))
5954
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
5957
QEMUSnapshotInfo *sn_tab, *sn;
5961
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5964
for(i = 0; i < nb_sns; i++) {
5966
if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
5976
void do_savevm(const char *name)
5978
BlockDriverState *bs, *bs1;
5979
QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
5980
int must_delete, ret, i;
5981
BlockDriverInfo bdi1, *bdi = &bdi1;
5983
int saved_vm_running;
5990
bs = get_bs_snapshots();
5992
term_printf("No block device can accept snapshots\n");
5996
/* ??? Should this occur after vm_stop? */
5999
saved_vm_running = vm_running;
6004
ret = bdrv_snapshot_find(bs, old_sn, name);
6009
memset(sn, 0, sizeof(*sn));
6011
pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
6012
pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
6015
pstrcpy(sn->name, sizeof(sn->name), name);
6018
/* fill auxiliary fields */
6021
sn->date_sec = tb.time;
6022
sn->date_nsec = tb.millitm * 1000000;
6024
gettimeofday(&tv, NULL);
6025
sn->date_sec = tv.tv_sec;
6026
sn->date_nsec = tv.tv_usec * 1000;
6028
sn->vm_clock_nsec = qemu_get_clock(vm_clock);
6030
if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6031
term_printf("Device %s does not support VM state snapshots\n",
6032
bdrv_get_device_name(bs));
6036
/* save the VM state */
6037
f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
6039
term_printf("Could not open VM state file\n");
6042
ret = qemu_savevm_state(f);
6043
sn->vm_state_size = qemu_ftell(f);
6046
term_printf("Error %d while writing VM\n", ret);
6050
/* create the snapshots */
6052
for(i = 0; i < nb_drives; i++) {
6053
bs1 = drives_table[i].bdrv;
6054
if (bdrv_has_snapshot(bs1)) {
6056
ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
6058
term_printf("Error while deleting snapshot on '%s'\n",
6059
bdrv_get_device_name(bs1));
6062
ret = bdrv_snapshot_create(bs1, sn);
6064
term_printf("Error while creating snapshot on '%s'\n",
6065
bdrv_get_device_name(bs1));
6071
if (saved_vm_running)
6075
void do_loadvm(const char *name)
6077
BlockDriverState *bs, *bs1;
6078
BlockDriverInfo bdi1, *bdi = &bdi1;
6081
int saved_vm_running;
6083
bs = get_bs_snapshots();
6085
term_printf("No block device supports snapshots\n");
6089
/* Flush all IO requests so they don't interfere with the new state. */
6092
saved_vm_running = vm_running;
6095
for(i = 0; i <= nb_drives; i++) {
6096
bs1 = drives_table[i].bdrv;
6097
if (bdrv_has_snapshot(bs1)) {
6098
ret = bdrv_snapshot_goto(bs1, name);
6101
term_printf("Warning: ");
6104
term_printf("Snapshots not supported on device '%s'\n",
6105
bdrv_get_device_name(bs1));
6108
term_printf("Could not find snapshot '%s' on device '%s'\n",
6109
name, bdrv_get_device_name(bs1));
6112
term_printf("Error %d while activating snapshot on '%s'\n",
6113
ret, bdrv_get_device_name(bs1));
6116
/* fatal on snapshot block device */
6123
if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6124
term_printf("Device %s does not support VM state snapshots\n",
6125
bdrv_get_device_name(bs));
6129
/* restore the VM state */
6130
f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
6132
term_printf("Could not open VM state file\n");
6135
ret = qemu_loadvm_state(f);
6138
term_printf("Error %d while loading VM state\n", ret);
6141
if (saved_vm_running)
6145
void do_delvm(const char *name)
6147
BlockDriverState *bs, *bs1;
6150
bs = get_bs_snapshots();
6152
term_printf("No block device supports snapshots\n");
6156
for(i = 0; i <= nb_drives; i++) {
6157
bs1 = drives_table[i].bdrv;
6158
if (bdrv_has_snapshot(bs1)) {
6159
ret = bdrv_snapshot_delete(bs1, name);
6161
if (ret == -ENOTSUP)
6162
term_printf("Snapshots not supported on device '%s'\n",
6163
bdrv_get_device_name(bs1));
6165
term_printf("Error %d while deleting snapshot on '%s'\n",
6166
ret, bdrv_get_device_name(bs1));
6172
void do_info_snapshots(void)
6174
BlockDriverState *bs, *bs1;
6175
QEMUSnapshotInfo *sn_tab, *sn;
6179
bs = get_bs_snapshots();
6181
term_printf("No available block device supports snapshots\n");
6184
term_printf("Snapshot devices:");
6185
for(i = 0; i <= nb_drives; i++) {
6186
bs1 = drives_table[i].bdrv;
6187
if (bdrv_has_snapshot(bs1)) {
6189
term_printf(" %s", bdrv_get_device_name(bs1));
6194
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6196
term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
6199
term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
6200
term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
6201
for(i = 0; i < nb_sns; i++) {
6203
term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
6208
/***********************************************************/
6209
/* cpu save/restore */
6211
#if defined(TARGET_I386)
6213
static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
6215
qemu_put_be32(f, dt->selector);
6216
qemu_put_betl(f, dt->base);
6217
qemu_put_be32(f, dt->limit);
6218
qemu_put_be32(f, dt->flags);
6221
static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
6223
dt->selector = qemu_get_be32(f);
6224
dt->base = qemu_get_betl(f);
6225
dt->limit = qemu_get_be32(f);
6226
dt->flags = qemu_get_be32(f);
6229
void cpu_save(QEMUFile *f, void *opaque)
6231
CPUState *env = opaque;
6232
uint16_t fptag, fpus, fpuc, fpregs_format;
6236
for(i = 0; i < CPU_NB_REGS; i++)
6237
qemu_put_betls(f, &env->regs[i]);
6238
qemu_put_betls(f, &env->eip);
6239
qemu_put_betls(f, &env->eflags);
6240
hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
6241
qemu_put_be32s(f, &hflags);
6245
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
6247
for(i = 0; i < 8; i++) {
6248
fptag |= ((!env->fptags[i]) << i);
6251
qemu_put_be16s(f, &fpuc);
6252
qemu_put_be16s(f, &fpus);
6253
qemu_put_be16s(f, &fptag);
6255
#ifdef USE_X86LDOUBLE
6260
qemu_put_be16s(f, &fpregs_format);
6262
for(i = 0; i < 8; i++) {
6263
#ifdef USE_X86LDOUBLE
6267
/* we save the real CPU data (in case of MMX usage only 'mant'
6268
contains the MMX register */
6269
cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
6270
qemu_put_be64(f, mant);
6271
qemu_put_be16(f, exp);
6274
/* if we use doubles for float emulation, we save the doubles to
6275
avoid losing information in case of MMX usage. It can give
6276
problems if the image is restored on a CPU where long
6277
doubles are used instead. */
6278
qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
6282
for(i = 0; i < 6; i++)
6283
cpu_put_seg(f, &env->segs[i]);
6284
cpu_put_seg(f, &env->ldt);
6285
cpu_put_seg(f, &env->tr);
6286
cpu_put_seg(f, &env->gdt);
6287
cpu_put_seg(f, &env->idt);
6289
qemu_put_be32s(f, &env->sysenter_cs);
6290
qemu_put_be32s(f, &env->sysenter_esp);
6291
qemu_put_be32s(f, &env->sysenter_eip);
6293
qemu_put_betls(f, &env->cr[0]);
6294
qemu_put_betls(f, &env->cr[2]);
6295
qemu_put_betls(f, &env->cr[3]);
6296
qemu_put_betls(f, &env->cr[4]);
6298
for(i = 0; i < 8; i++)
6299
qemu_put_betls(f, &env->dr[i]);
6302
qemu_put_be32s(f, &env->a20_mask);
6305
qemu_put_be32s(f, &env->mxcsr);
6306
for(i = 0; i < CPU_NB_REGS; i++) {
6307
qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
6308
qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
6311
#ifdef TARGET_X86_64
6312
qemu_put_be64s(f, &env->efer);
6313
qemu_put_be64s(f, &env->star);
6314
qemu_put_be64s(f, &env->lstar);
6315
qemu_put_be64s(f, &env->cstar);
6316
qemu_put_be64s(f, &env->fmask);
6317
qemu_put_be64s(f, &env->kernelgsbase);
6319
qemu_put_be32s(f, &env->smbase);
6322
#ifdef USE_X86LDOUBLE
6323
/* XXX: add that in a FPU generic layer */
6324
union x86_longdouble {
6329
#define MANTD1(fp) (fp & ((1LL << 52) - 1))
6330
#define EXPBIAS1 1023
6331
#define EXPD1(fp) ((fp >> 52) & 0x7FF)
6332
#define SIGND1(fp) ((fp >> 32) & 0x80000000)
6334
static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
6338
p->mant = (MANTD1(temp) << 11) | (1LL << 63);
6339
/* exponent + sign */
6340
e = EXPD1(temp) - EXPBIAS1 + 16383;
6341
e |= SIGND1(temp) >> 16;
6346
int cpu_load(QEMUFile *f, void *opaque, int version_id)
6348
CPUState *env = opaque;
6351
uint16_t fpus, fpuc, fptag, fpregs_format;
6353
if (version_id != 3 && version_id != 4)
6355
for(i = 0; i < CPU_NB_REGS; i++)
6356
qemu_get_betls(f, &env->regs[i]);
6357
qemu_get_betls(f, &env->eip);
6358
qemu_get_betls(f, &env->eflags);
6359
qemu_get_be32s(f, &hflags);
6361
qemu_get_be16s(f, &fpuc);
6362
qemu_get_be16s(f, &fpus);
6363
qemu_get_be16s(f, &fptag);
6364
qemu_get_be16s(f, &fpregs_format);
6366
/* NOTE: we cannot always restore the FPU state if the image come
6367
from a host with a different 'USE_X86LDOUBLE' define. We guess
6368
if we are in an MMX state to restore correctly in that case. */
6369
guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
6370
for(i = 0; i < 8; i++) {
6374
switch(fpregs_format) {
6376
mant = qemu_get_be64(f);
6377
exp = qemu_get_be16(f);
6378
#ifdef USE_X86LDOUBLE
6379
env->fpregs[i].d = cpu_set_fp80(mant, exp);
6381
/* difficult case */
6383
env->fpregs[i].mmx.MMX_Q(0) = mant;
6385
env->fpregs[i].d = cpu_set_fp80(mant, exp);
6389
mant = qemu_get_be64(f);
6390
#ifdef USE_X86LDOUBLE
6392
union x86_longdouble *p;
6393
/* difficult case */
6394
p = (void *)&env->fpregs[i];
6399
fp64_to_fp80(p, mant);
6403
env->fpregs[i].mmx.MMX_Q(0) = mant;
6412
/* XXX: restore FPU round state */
6413
env->fpstt = (fpus >> 11) & 7;
6414
env->fpus = fpus & ~0x3800;
6416
for(i = 0; i < 8; i++) {
6417
env->fptags[i] = (fptag >> i) & 1;
6420
for(i = 0; i < 6; i++)
6421
cpu_get_seg(f, &env->segs[i]);
6422
cpu_get_seg(f, &env->ldt);
6423
cpu_get_seg(f, &env->tr);
6424
cpu_get_seg(f, &env->gdt);
6425
cpu_get_seg(f, &env->idt);
6427
qemu_get_be32s(f, &env->sysenter_cs);
6428
qemu_get_be32s(f, &env->sysenter_esp);
6429
qemu_get_be32s(f, &env->sysenter_eip);
6431
qemu_get_betls(f, &env->cr[0]);
6432
qemu_get_betls(f, &env->cr[2]);
6433
qemu_get_betls(f, &env->cr[3]);
6434
qemu_get_betls(f, &env->cr[4]);
6436
for(i = 0; i < 8; i++)
6437
qemu_get_betls(f, &env->dr[i]);
6440
qemu_get_be32s(f, &env->a20_mask);
6442
qemu_get_be32s(f, &env->mxcsr);
6443
for(i = 0; i < CPU_NB_REGS; i++) {
6444
qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
6445
qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
6448
#ifdef TARGET_X86_64
6449
qemu_get_be64s(f, &env->efer);
6450
qemu_get_be64s(f, &env->star);
6451
qemu_get_be64s(f, &env->lstar);
6452
qemu_get_be64s(f, &env->cstar);
6453
qemu_get_be64s(f, &env->fmask);
6454
qemu_get_be64s(f, &env->kernelgsbase);
6456
if (version_id >= 4)
6457
qemu_get_be32s(f, &env->smbase);
6459
/* XXX: compute hflags from scratch, except for CPL and IIF */
6460
env->hflags = hflags;
6465
#elif defined(TARGET_PPC)
6466
void cpu_save(QEMUFile *f, void *opaque)
6470
int cpu_load(QEMUFile *f, void *opaque, int version_id)
6475
#elif defined(TARGET_MIPS)
6476
void cpu_save(QEMUFile *f, void *opaque)
6480
int cpu_load(QEMUFile *f, void *opaque, int version_id)
6485
#elif defined(TARGET_SPARC)
6486
void cpu_save(QEMUFile *f, void *opaque)
6488
CPUState *env = opaque;
6492
for(i = 0; i < 8; i++)
6493
qemu_put_betls(f, &env->gregs[i]);
6494
for(i = 0; i < NWINDOWS * 16; i++)
6495
qemu_put_betls(f, &env->regbase[i]);
6498
for(i = 0; i < TARGET_FPREGS; i++) {
6504
qemu_put_be32(f, u.i);
6507
qemu_put_betls(f, &env->pc);
6508
qemu_put_betls(f, &env->npc);
6509
qemu_put_betls(f, &env->y);
6511
qemu_put_be32(f, tmp);
6512
qemu_put_betls(f, &env->fsr);
6513
qemu_put_betls(f, &env->tbr);
6514
#ifndef TARGET_SPARC64
6515
qemu_put_be32s(f, &env->wim);
6517
for(i = 0; i < 16; i++)
6518
qemu_put_be32s(f, &env->mmuregs[i]);
6522
int cpu_load(QEMUFile *f, void *opaque, int version_id)
6524
CPUState *env = opaque;
6528
for(i = 0; i < 8; i++)
6529
qemu_get_betls(f, &env->gregs[i]);
6530
for(i = 0; i < NWINDOWS * 16; i++)
6531
qemu_get_betls(f, &env->regbase[i]);
6534
for(i = 0; i < TARGET_FPREGS; i++) {
6539
u.i = qemu_get_be32(f);
6543
qemu_get_betls(f, &env->pc);
6544
qemu_get_betls(f, &env->npc);
6545
qemu_get_betls(f, &env->y);
6546
tmp = qemu_get_be32(f);
6547
env->cwp = 0; /* needed to ensure that the wrapping registers are
6548
correctly updated */
6550
qemu_get_betls(f, &env->fsr);
6551
qemu_get_betls(f, &env->tbr);
6552
#ifndef TARGET_SPARC64
6553
qemu_get_be32s(f, &env->wim);
6555
for(i = 0; i < 16; i++)
6556
qemu_get_be32s(f, &env->mmuregs[i]);
6562
#elif defined(TARGET_ARM)
6564
void cpu_save(QEMUFile *f, void *opaque)
6567
CPUARMState *env = (CPUARMState *)opaque;
6569
for (i = 0; i < 16; i++) {
6570
qemu_put_be32(f, env->regs[i]);
6572
qemu_put_be32(f, cpsr_read(env));
6573
qemu_put_be32(f, env->spsr);
6574
for (i = 0; i < 6; i++) {
6575
qemu_put_be32(f, env->banked_spsr[i]);
6576
qemu_put_be32(f, env->banked_r13[i]);
6577
qemu_put_be32(f, env->banked_r14[i]);
6579
for (i = 0; i < 5; i++) {
6580
qemu_put_be32(f, env->usr_regs[i]);
6581
qemu_put_be32(f, env->fiq_regs[i]);
6583
qemu_put_be32(f, env->cp15.c0_cpuid);
6584
qemu_put_be32(f, env->cp15.c0_cachetype);
6585
qemu_put_be32(f, env->cp15.c1_sys);
6586
qemu_put_be32(f, env->cp15.c1_coproc);
6587
qemu_put_be32(f, env->cp15.c1_xscaleauxcr);
6588
qemu_put_be32(f, env->cp15.c2_base0);
6589
qemu_put_be32(f, env->cp15.c2_base1);
6590
qemu_put_be32(f, env->cp15.c2_mask);
6591
qemu_put_be32(f, env->cp15.c2_data);
6592
qemu_put_be32(f, env->cp15.c2_insn);
6593
qemu_put_be32(f, env->cp15.c3);
6594
qemu_put_be32(f, env->cp15.c5_insn);
6595
qemu_put_be32(f, env->cp15.c5_data);
6596
for (i = 0; i < 8; i++) {
6597
qemu_put_be32(f, env->cp15.c6_region[i]);
6599
qemu_put_be32(f, env->cp15.c6_insn);
6600
qemu_put_be32(f, env->cp15.c6_data);
6601
qemu_put_be32(f, env->cp15.c9_insn);
6602
qemu_put_be32(f, env->cp15.c9_data);
6603
qemu_put_be32(f, env->cp15.c13_fcse);
6604
qemu_put_be32(f, env->cp15.c13_context);
6605
qemu_put_be32(f, env->cp15.c13_tls1);
6606
qemu_put_be32(f, env->cp15.c13_tls2);
6607
qemu_put_be32(f, env->cp15.c13_tls3);
6608
qemu_put_be32(f, env->cp15.c15_cpar);
6610
qemu_put_be32(f, env->features);
6612
if (arm_feature(env, ARM_FEATURE_VFP)) {
6613
for (i = 0; i < 16; i++) {
6615
u.d = env->vfp.regs[i];
6616
qemu_put_be32(f, u.l.upper);
6617
qemu_put_be32(f, u.l.lower);
6619
for (i = 0; i < 16; i++) {
6620
qemu_put_be32(f, env->vfp.xregs[i]);
6623
/* TODO: Should use proper FPSCR access functions. */
6624
qemu_put_be32(f, env->vfp.vec_len);
6625
qemu_put_be32(f, env->vfp.vec_stride);
6627
if (arm_feature(env, ARM_FEATURE_VFP3)) {
6628
for (i = 16; i < 32; i++) {
6630
u.d = env->vfp.regs[i];
6631
qemu_put_be32(f, u.l.upper);
6632
qemu_put_be32(f, u.l.lower);
6637
if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6638
for (i = 0; i < 16; i++) {
6639
qemu_put_be64(f, env->iwmmxt.regs[i]);
6641
for (i = 0; i < 16; i++) {
6642
qemu_put_be32(f, env->iwmmxt.cregs[i]);
6646
if (arm_feature(env, ARM_FEATURE_M)) {
6647
qemu_put_be32(f, env->v7m.other_sp);
6648
qemu_put_be32(f, env->v7m.vecbase);
6649
qemu_put_be32(f, env->v7m.basepri);
6650
qemu_put_be32(f, env->v7m.control);
6651
qemu_put_be32(f, env->v7m.current_sp);
6652
qemu_put_be32(f, env->v7m.exception);
6656
int cpu_load(QEMUFile *f, void *opaque, int version_id)
6658
CPUARMState *env = (CPUARMState *)opaque;
6661
if (version_id != ARM_CPU_SAVE_VERSION)
6664
for (i = 0; i < 16; i++) {
6665
env->regs[i] = qemu_get_be32(f);
6667
cpsr_write(env, qemu_get_be32(f), 0xffffffff);
6668
env->spsr = qemu_get_be32(f);
6669
for (i = 0; i < 6; i++) {
6670
env->banked_spsr[i] = qemu_get_be32(f);
6671
env->banked_r13[i] = qemu_get_be32(f);
6672
env->banked_r14[i] = qemu_get_be32(f);
6674
for (i = 0; i < 5; i++) {
6675
env->usr_regs[i] = qemu_get_be32(f);
6676
env->fiq_regs[i] = qemu_get_be32(f);
6678
env->cp15.c0_cpuid = qemu_get_be32(f);
6679
env->cp15.c0_cachetype = qemu_get_be32(f);
6680
env->cp15.c1_sys = qemu_get_be32(f);
6681
env->cp15.c1_coproc = qemu_get_be32(f);
6682
env->cp15.c1_xscaleauxcr = qemu_get_be32(f);
6683
env->cp15.c2_base0 = qemu_get_be32(f);
6684
env->cp15.c2_base1 = qemu_get_be32(f);
6685
env->cp15.c2_mask = qemu_get_be32(f);
6686
env->cp15.c2_data = qemu_get_be32(f);
6687
env->cp15.c2_insn = qemu_get_be32(f);
6688
env->cp15.c3 = qemu_get_be32(f);
6689
env->cp15.c5_insn = qemu_get_be32(f);
6690
env->cp15.c5_data = qemu_get_be32(f);
6691
for (i = 0; i < 8; i++) {
6692
env->cp15.c6_region[i] = qemu_get_be32(f);
6694
env->cp15.c6_insn = qemu_get_be32(f);
6695
env->cp15.c6_data = qemu_get_be32(f);
6696
env->cp15.c9_insn = qemu_get_be32(f);
6697
env->cp15.c9_data = qemu_get_be32(f);
6698
env->cp15.c13_fcse = qemu_get_be32(f);
6699
env->cp15.c13_context = qemu_get_be32(f);
6700
env->cp15.c13_tls1 = qemu_get_be32(f);
6701
env->cp15.c13_tls2 = qemu_get_be32(f);
6702
env->cp15.c13_tls3 = qemu_get_be32(f);
6703
env->cp15.c15_cpar = qemu_get_be32(f);
6705
env->features = qemu_get_be32(f);
6707
if (arm_feature(env, ARM_FEATURE_VFP)) {
6708
for (i = 0; i < 16; i++) {
6710
u.l.upper = qemu_get_be32(f);
6711
u.l.lower = qemu_get_be32(f);
6712
env->vfp.regs[i] = u.d;
6714
for (i = 0; i < 16; i++) {
6715
env->vfp.xregs[i] = qemu_get_be32(f);
6718
/* TODO: Should use proper FPSCR access functions. */
6719
env->vfp.vec_len = qemu_get_be32(f);
6720
env->vfp.vec_stride = qemu_get_be32(f);
6722
if (arm_feature(env, ARM_FEATURE_VFP3)) {
6723
for (i = 0; i < 16; i++) {
6725
u.l.upper = qemu_get_be32(f);
6726
u.l.lower = qemu_get_be32(f);
6727
env->vfp.regs[i] = u.d;
6732
if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6733
for (i = 0; i < 16; i++) {
6734
env->iwmmxt.regs[i] = qemu_get_be64(f);
6736
for (i = 0; i < 16; i++) {
6737
env->iwmmxt.cregs[i] = qemu_get_be32(f);
6741
if (arm_feature(env, ARM_FEATURE_M)) {
6742
env->v7m.other_sp = qemu_get_be32(f);
6743
env->v7m.vecbase = qemu_get_be32(f);
6744
env->v7m.basepri = qemu_get_be32(f);
6745
env->v7m.control = qemu_get_be32(f);
6746
env->v7m.current_sp = qemu_get_be32(f);
6747
env->v7m.exception = qemu_get_be32(f);
6755
//#warning No CPU save/restore functions
6759
/***********************************************************/
6760
3019
/* ram save/restore */
6762
3021
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)