vl.c 122 KB
Newer Older
1
/*
bellard's avatar
bellard committed
2
 * QEMU System Emulator
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
23 24 25 26 27 28
 */
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
bellard's avatar
bellard committed
29
#include <sys/time.h>
30
#include <zlib.h>
31
#include "qemu/bitmap.h"
bellard's avatar
bellard committed
32

Juan Quintela's avatar
Juan Quintela committed
33
/* Needed early for CONFIG_BSD etc. */
blueswir1's avatar
blueswir1 committed
34 35
#include "config-host.h"

bellard's avatar
bellard committed
36
#ifndef _WIN32
Paul Brook's avatar
Paul Brook committed
37
#include <libgen.h>
bellard's avatar
bellard committed
38
#include <sys/times.h>
bellard's avatar
bellard committed
39
#include <sys/wait.h>
bellard's avatar
bellard committed
40 41
#include <termios.h>
#include <sys/mman.h>
bellard's avatar
bellard committed
42
#include <sys/ioctl.h>
blueswir1's avatar
blueswir1 committed
43
#include <sys/resource.h>
bellard's avatar
bellard committed
44
#include <sys/socket.h>
bellard's avatar
bellard committed
45
#include <netinet/in.h>
blueswir1's avatar
blueswir1 committed
46 47
#include <net/if.h>
#include <arpa/inet.h>
bellard's avatar
bellard committed
48
#include <dirent.h>
bellard's avatar
bellard committed
49
#include <netdb.h>
50
#include <sys/select.h>
51

Juan Quintela's avatar
Juan Quintela committed
52
#ifdef CONFIG_BSD
bellard's avatar
bellard committed
53
#include <sys/stat.h>
Aurelien Jarno's avatar
Aurelien Jarno committed
54
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55
#include <sys/sysctl.h>
blueswir1's avatar
blueswir1 committed
56 57
#else
#include <util.h>
58
#endif
59
#else
60
#ifdef __linux__
bellard's avatar
bellard committed
61
#include <malloc.h>
ths's avatar
ths committed
62

bellard's avatar
bellard committed
63
#include <linux/ppdev.h>
64
#include <linux/parport.h>
65
#endif
66 67

#ifdef CONFIG_SECCOMP
68
#include "sysemu/seccomp.h"
69 70
#endif

71
#ifdef __sun__
72 73 74 75 76 77 78 79 80 81 82 83
#include <sys/stat.h>
#include <sys/ethernet.h>
#include <sys/sockio.h>
#include <netinet/arp.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h> // must come after ip.h
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <syslog.h>
#include <stropts.h>
bellard's avatar
bellard committed
84
#endif
bellard's avatar
bellard committed
85
#endif
bellard's avatar
bellard committed
86
#endif
bellard's avatar
bellard committed
87

88 89 90 91
#if defined(CONFIG_VDE)
#include <libvdeplug.h>
#endif

bellard's avatar
bellard committed
92
#ifdef _WIN32
93
#include <windows.h>
bellard's avatar
bellard committed
94 95
#endif

bellard's avatar
bellard committed
96
#ifdef CONFIG_SDL
Stefan Weil's avatar
Stefan Weil committed
97
#if defined(__APPLE__) || defined(main)
Stefan Weil's avatar
Stefan Weil committed
98
#include <SDL.h>
malc's avatar
malc committed
99 100 101
int qemu_main(int argc, char **argv, char **envp);
int main(int argc, char **argv)
{
Stefan Weil's avatar
Stefan Weil committed
102
    return qemu_main(argc, argv, NULL);
malc's avatar
malc committed
103 104 105
}
#undef main
#define main qemu_main
bellard's avatar
bellard committed
106
#endif
bellard's avatar
bellard committed
107
#endif /* CONFIG_SDL */
108

109 110 111 112 113
#ifdef CONFIG_COCOA
#undef main
#define main qemu_main
#endif /* CONFIG_COCOA */

114 115
#include <glib.h>

116 117 118 119
#include "hw/hw.h"
#include "hw/boards.h"
#include "hw/usb.h"
#include "hw/pcmcia.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
120 121
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
122
#include "hw/bt.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
123 124 125
#include "sysemu/watchdog.h"
#include "hw/i386/smbios.h"
#include "hw/xen/xen.h"
126
#include "hw/qdev.h"
127
#include "hw/loader.h"
128
#include "monitor/qdev.h"
129
#include "sysemu/bt.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
130
#include "net/net.h"
131
#include "net/slirp.h"
132
#include "monitor/monitor.h"
133
#include "ui/console.h"
134
#include "sysemu/sysemu.h"
135
#include "exec/gdbstub.h"
136
#include "qemu/timer.h"
137
#include "sysemu/char.h"
138
#include "qemu/cache-utils.h"
139
#include "sysemu/blockdev.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
140
#include "hw/block/block.h"
141
#include "migration/block.h"
142
#include "sysemu/tpm.h"
143
#include "sysemu/dma.h"
144
#include "audio/audio.h"
145
#include "migration/migration.h"
146
#include "sysemu/kvm.h"
147
#include "qapi/qmp/qjson.h"
148 149
#include "qemu/option.h"
#include "qemu/config-file.h"
150
#include "qemu-options.h"
Luiz Capitulino's avatar
Luiz Capitulino committed
151
#include "qmp-commands.h"
152
#include "qemu/main-loop.h"
153
#ifdef CONFIG_VIRTFS
154 155
#include "fsdev/qemu-fsdev.h"
#endif
156
#include "sysemu/qtest.h"
157

158
#include "disas/disas.h"
bellard's avatar
bellard committed
159

160
#include "qemu/sockets.h"
161

Jan Kiszka's avatar
Jan Kiszka committed
162
#include "slirp/libslirp.h"
163

164
#include "trace.h"
165
#include "trace/control.h"
166
#include "qemu/queue.h"
167 168
#include "sysemu/cpus.h"
#include "sysemu/arch_init.h"
169
#include "qemu/osdep.h"
Blue Swirl's avatar
Blue Swirl committed
170

Gerd Hoffmann's avatar
Gerd Hoffmann committed
171
#include "ui/qemu-spice.h"
172
#include "qapi/string-input-visitor.h"
Gerd Hoffmann's avatar
Gerd Hoffmann committed
173

174 175
//#define DEBUG_NET
//#define DEBUG_SLIRP
176

177
#define DEFAULT_RAM_SIZE 128
178

179
#define MAX_VIRTIO_CONSOLES 1
180
#define MAX_SCLP_CONSOLES 1
181

182 183
static const char *data_dir[16];
static int data_dir_idx;
184
const char *bios_name = NULL;
malc's avatar
malc committed
185
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
186
DisplayType display_type = DT_DEFAULT;
Blue Swirl's avatar
Blue Swirl committed
187
static int display_remote;
188
const char* keyboard_layout = NULL;
189
ram_addr_t ram_size;
190 191 192 193
const char *mem_path = NULL;
#ifdef MAP_POPULATE
int mem_prealloc = 0; /* force preallocation of physical target memory */
#endif
194
int nb_nics;
bellard's avatar
bellard committed
195
NICInfo nd_table[MAX_NICS];
Paolo Bonzini's avatar
Paolo Bonzini committed
196
int autostart;
197 198
static int rtc_utc = 1;
static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka's avatar
Jan Kiszka committed
199
QEMUClock *rtc_clock;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
200
int vga_interface_type = VGA_NONE;
blueswir1's avatar
blueswir1 committed
201 202
static int full_screen = 0;
static int no_frame = 0;
203
int no_quit = 0;
204
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
205
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
206
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
207
CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
208
int win2k_install_hack = 0;
209
int singlestep = 0;
bellard's avatar
bellard committed
210
int smp_cpus = 1;
211
int max_cpus = 0;
212 213
int smp_cores = 1;
int smp_threads = 1;
Jes Sorensen's avatar
Jes Sorensen committed
214
#ifdef CONFIG_VNC
215
const char *vnc_display;
Jes Sorensen's avatar
Jes Sorensen committed
216
#endif
bellard's avatar
bellard committed
217
int acpi_enabled = 1;
218
int no_hpet = 0;
219
int fd_bootchk = 1;
Blue Swirl's avatar
Blue Swirl committed
220
static int no_reboot;
aurel32's avatar
aurel32 committed
221
int no_shutdown = 0;
222
int cursor_hide = 1;
223
int graphic_rotate = 0;
Markus Armbruster's avatar
Markus Armbruster committed
224
const char *watchdog;
Gleb Natapov's avatar
Gleb Natapov committed
225
QEMUOptionRom option_rom[MAX_OPTION_ROMS];
226
int nb_option_roms;
227
int semihosting_enabled = 0;
228
int old_param = 0;
ths's avatar
ths committed
229
const char *qemu_name;
ths's avatar
ths committed
230
int alt_grab = 0;
231
int ctrl_grab = 0;
blueswir1's avatar
blueswir1 committed
232 233
unsigned int nb_prom_envs = 0;
const char *prom_envs[MAX_PROM_ENVS];
234
int boot_menu;
235
bool boot_strict;
wayne's avatar
wayne committed
236
uint8_t *boot_splash_filedata;
237
size_t boot_splash_filedata_size;
wayne's avatar
wayne committed
238
uint8_t qemu_extra_params_fw[2];
239

240 241 242 243 244 245 246 247 248
typedef struct FWBootEntry FWBootEntry;

struct FWBootEntry {
    QTAILQ_ENTRY(FWBootEntry) link;
    int32_t bootindex;
    DeviceState *dev;
    char *suffix;
};

Blue Swirl's avatar
Blue Swirl committed
249 250
static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
    QTAILQ_HEAD_INITIALIZER(fw_boot_order);
251

252 253
int nb_numa_nodes;
uint64_t node_mem[MAX_NODES];
254
unsigned long *node_cpumask[MAX_NODES];
255

256 257
uint8_t qemu_uuid[16];

Jan Kiszka's avatar
Jan Kiszka committed
258 259 260
static QEMUBootSetHandler *boot_set_handler;
static void *boot_set_opaque;

Gerd Hoffmann's avatar
Gerd Hoffmann committed
261 262 263
static NotifierList exit_notifiers =
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);

264 265 266
static NotifierList machine_init_done_notifiers =
    NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);

267 268
static bool tcg_allowed = true;
bool xen_allowed;
269 270
uint32_t xen_domid;
enum xen_mode xen_mode = XEN_EMULATE;
271
static int tcg_tb_size;
272

273
static int default_serial = 1;
274
static int default_parallel = 1;
275
static int default_virtcon = 1;
276
static int default_sclp = 1;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
277
static int default_monitor = 1;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
278 279 280
static int default_floppy = 1;
static int default_cdrom = 1;
static int default_sdcard = 1;
281
static int default_vga = 1;
282 283 284 285 286

static struct {
    const char *driver;
    int *flag;
} default_list[] = {
287 288
    { .driver = "isa-serial",           .flag = &default_serial    },
    { .driver = "isa-parallel",         .flag = &default_parallel  },
Gerd Hoffmann's avatar
Gerd Hoffmann committed
289
    { .driver = "isa-fdc",              .flag = &default_floppy    },
290 291
    { .driver = "ide-cd",               .flag = &default_cdrom     },
    { .driver = "ide-hd",               .flag = &default_cdrom     },
Gerd Hoffmann's avatar
Gerd Hoffmann committed
292
    { .driver = "ide-drive",            .flag = &default_cdrom     },
293
    { .driver = "scsi-cd",              .flag = &default_cdrom     },
294 295 296
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
297 298 299 300 301 302
    { .driver = "VGA",                  .flag = &default_vga       },
    { .driver = "isa-vga",              .flag = &default_vga       },
    { .driver = "cirrus-vga",           .flag = &default_vga       },
    { .driver = "isa-cirrus-vga",       .flag = &default_vga       },
    { .driver = "vmware-svga",          .flag = &default_vga       },
    { .driver = "qxl-vga",              .flag = &default_vga       },
303 304
};

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
static QemuOptsList qemu_rtc_opts = {
    .name = "rtc",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
    .desc = {
        {
            .name = "base",
            .type = QEMU_OPT_STRING,
        },{
            .name = "clock",
            .type = QEMU_OPT_STRING,
        },{
            .name = "driftfix",
            .type = QEMU_OPT_STRING,
        },
        { /* end of list */ }
    },
};

static QemuOptsList qemu_sandbox_opts = {
    .name = "sandbox",
    .implied_opt_name = "enable",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
    .desc = {
        {
            .name = "enable",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

static QemuOptsList qemu_trace_opts = {
    .name = "trace",
    .implied_opt_name = "trace",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
    .desc = {
        {
            .name = "events",
            .type = QEMU_OPT_STRING,
        },{
            .name = "file",
            .type = QEMU_OPT_STRING,
        },
        { /* end of list */ }
    },
};

static QemuOptsList qemu_option_rom_opts = {
    .name = "option-rom",
    .implied_opt_name = "romfile",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
    .desc = {
        {
            .name = "bootindex",
            .type = QEMU_OPT_NUMBER,
        }, {
            .name = "romfile",
            .type = QEMU_OPT_STRING,
        },
        { /* end of list */ }
    },
};

static QemuOptsList qemu_machine_opts = {
    .name = "machine",
    .implied_opt_name = "type",
    .merge_lists = true,
    .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
    .desc = {
        {
            .name = "type",
            .type = QEMU_OPT_STRING,
            .help = "emulated machine"
        }, {
            .name = "accel",
            .type = QEMU_OPT_STRING,
            .help = "accelerator list",
        }, {
            .name = "kernel_irqchip",
            .type = QEMU_OPT_BOOL,
            .help = "use KVM in-kernel irqchip",
        }, {
            .name = "kvm_shadow_mem",
            .type = QEMU_OPT_SIZE,
            .help = "KVM shadow MMU size",
        }, {
            .name = "kernel",
            .type = QEMU_OPT_STRING,
            .help = "Linux kernel image file",
        }, {
            .name = "initrd",
            .type = QEMU_OPT_STRING,
            .help = "Linux initial ramdisk file",
        }, {
            .name = "append",
            .type = QEMU_OPT_STRING,
            .help = "Linux kernel command line",
        }, {
            .name = "dtb",
            .type = QEMU_OPT_STRING,
            .help = "Linux kernel device tree file",
        }, {
            .name = "dumpdtb",
            .type = QEMU_OPT_STRING,
            .help = "Dump current dtb to a file and quit",
        }, {
            .name = "phandle_start",
412
            .type = QEMU_OPT_NUMBER,
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
            .help = "The first phandle ID we may generate dynamically",
        }, {
            .name = "dt_compatible",
            .type = QEMU_OPT_STRING,
            .help = "Overrides the \"compatible\" property of the dt root node",
        }, {
            .name = "dump-guest-core",
            .type = QEMU_OPT_BOOL,
            .help = "Include guest memory in  a core dump",
        }, {
            .name = "mem-merge",
            .type = QEMU_OPT_BOOL,
            .help = "enable/disable memory merge support",
        },{
            .name = "usb",
            .type = QEMU_OPT_BOOL,
            .help = "Set on/off to enable/disable usb",
        },
        { /* End of list */ }
    },
};

static QemuOptsList qemu_boot_opts = {
    .name = "boot-opts",
437 438
    .implied_opt_name = "order",
    .merge_lists = true,
439 440 441 442 443 444 445 446 447 448
    .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
    .desc = {
        {
            .name = "order",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "once",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "menu",
449
            .type = QEMU_OPT_BOOL,
450 451 452 453 454 455 456 457 458
        }, {
            .name = "splash",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "splash-time",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "reboot-timeout",
            .type = QEMU_OPT_STRING,
459 460 461
        }, {
            .name = "strict",
            .type = QEMU_OPT_STRING,
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        },
        { /*End of list */ }
    },
};

static QemuOptsList qemu_add_fd_opts = {
    .name = "add-fd",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
    .desc = {
        {
            .name = "fd",
            .type = QEMU_OPT_NUMBER,
            .help = "file descriptor of which a duplicate is added to fd set",
        },{
            .name = "set",
            .type = QEMU_OPT_NUMBER,
            .help = "ID of the fd set to add fd to",
        },{
            .name = "opaque",
            .type = QEMU_OPT_STRING,
            .help = "free-form string used to describe fd",
        },
        { /* end of list */ }
    },
};

static QemuOptsList qemu_object_opts = {
    .name = "object",
    .implied_opt_name = "qom-type",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
    .desc = {
        { }
    },
};

497 498 499 500 501
static QemuOptsList qemu_tpmdev_opts = {
    .name = "tpmdev",
    .implied_opt_name = "type",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
    .desc = {
502
        /* options are defined in the TPM backends */
503 504 505 506
        { /* end of list */ }
    },
};

507 508 509 510 511 512 513 514 515 516 517 518
static QemuOptsList qemu_realtime_opts = {
    .name = "realtime",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
    .desc = {
        {
            .name = "mlock",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

Seiji Aguchi's avatar
Seiji Aguchi committed
519 520 521 522 523 524 525 526 527 528 529 530
static QemuOptsList qemu_msg_opts = {
    .name = "msg",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
    .desc = {
        {
            .name = "timestamp",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
/**
 * Get machine options
 *
 * Returns: machine options (never null).
 */
QemuOpts *qemu_get_machine_opts(void)
{
    QemuOptsList *list;
    QemuOpts *opts;

    list = qemu_find_opts("machine");
    assert(list);
    opts = qemu_opts_find(list, NULL);
    if (!opts) {
        opts = qemu_opts_create_nofail(list);
    }
    return opts;
}

550 551 552 553 554
const char *qemu_get_vm_name(void)
{
    return qemu_name;
}

wayne's avatar
wayne committed
555 556 557
static void res_free(void)
{
    if (boot_splash_filedata != NULL) {
558
        g_free(boot_splash_filedata);
wayne's avatar
wayne committed
559 560 561 562
        boot_splash_filedata = NULL;
    }
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static int default_driver_check(QemuOpts *opts, void *opaque)
{
    const char *driver = qemu_opt_get(opts, "driver");
    int i;

    if (!driver)
        return 0;
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
        if (strcmp(default_list[i].driver, driver) != 0)
            continue;
        *(default_list[i].flag) = 0;
    }
    return 0;
}

578 579 580
/***********************************************************/
/* QEMU state */

581
static RunState current_run_state = RUN_STATE_PRELAUNCH;
582

583 584 585 586 587 588 589
typedef struct {
    RunState from;
    RunState to;
} RunStateTransition;

static const RunStateTransition runstate_transitions_def[] = {
    /*     from      ->     to      */
590
    { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
591

592
    { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
593
    { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
594

595
    { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
596
    { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
597

598
    { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
599
    { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
600

601
    { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
602
    { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
603

604
    { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
605
    { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
606

607
    { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
608
    { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
609
    { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
610

611 612
    { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
    { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
613

614
    { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
615

616 617 618 619 620 621 622 623 624
    { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
    { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
    { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
    { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
    { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
    { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
    { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
    { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
    { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
625
    { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
626

627
    { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
628

629
    { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
630
    { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
631

632 633 634 635 636
    { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
    { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
    { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
    { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },

637
    { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
638
    { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
639

640
    { RUN_STATE_GUEST_PANICKED, RUN_STATE_PAUSED },
641
    { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
642
    { RUN_STATE_GUEST_PANICKED, RUN_STATE_DEBUG },
643

644
    { RUN_STATE_MAX, RUN_STATE_MAX },
645 646
};

647 648
static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];

649 650 651 652 653
bool runstate_check(RunState state)
{
    return current_run_state == state;
}

Blue Swirl's avatar
Blue Swirl committed
654
static void runstate_init(void)
655 656 657 658 659
{
    const RunStateTransition *p;

    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));

660
    for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
661 662 663 664 665
        runstate_valid_transitions[p->from][p->to] = true;
    }
}

/* This function will abort() on invalid state transitions */
666 667
void runstate_set(RunState new_state)
{
668 669 670 671 672 673
    assert(new_state < RUN_STATE_MAX);

    if (!runstate_valid_transitions[current_run_state][new_state]) {
        fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
                RunState_lookup[current_run_state],
                RunState_lookup[new_state]);
674 675
        abort();
    }
Kazuya Saito's avatar
Kazuya Saito committed
676
    trace_runstate_set(new_state);
677 678 679
    current_run_state = new_state;
}

Luiz Capitulino's avatar
Luiz Capitulino committed
680
int runstate_is_running(void)
681
{
Luiz Capitulino's avatar
Luiz Capitulino committed
682
    return runstate_check(RUN_STATE_RUNNING);
683 684
}

685 686 687 688 689 690 691
bool runstate_needs_reset(void)
{
    return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
        runstate_check(RUN_STATE_SHUTDOWN) ||
        runstate_check(RUN_STATE_GUEST_PANICKED);
}

Luiz Capitulino's avatar
Luiz Capitulino committed
692
StatusInfo *qmp_query_status(Error **errp)
693
{
Luiz Capitulino's avatar
Luiz Capitulino committed
694 695 696 697 698 699 700
    StatusInfo *info = g_malloc0(sizeof(*info));

    info->running = runstate_is_running();
    info->singlestep = singlestep;
    info->status = current_run_state;

    return info;
701 702
}

703 704
/***********************************************************/
/* real time host monotonic timer */
705

706 707 708 709 710 711 712 713 714 715
/***********************************************************/
/* host time/date access */
void qemu_get_timedate(struct tm *tm, int offset)
{
    time_t ti;

    time(&ti);
    ti += offset;
    if (rtc_date_offset == -1) {
        if (rtc_utc)
716
            gmtime_r(&ti, tm);
717
        else
718
            localtime_r(&ti, tm);
719 720
    } else {
        ti -= rtc_date_offset;
721
        gmtime_r(&ti, tm);
722 723 724 725 726 727 728 729 730 731
    }
}

int qemu_timedate_diff(struct tm *tm)
{
    time_t seconds;

    if (rtc_date_offset == -1)
        if (rtc_utc)
            seconds = mktimegm(tm);
732 733 734 735 736
        else {
            struct tm tmp = *tm;
            tmp.tm_isdst = -1; /* use timezone to figure it out */
            seconds = mktime(&tmp);
	}
737 738 739 740 741 742
    else
        seconds = mktimegm(tm) + rtc_date_offset;

    return seconds - time(NULL);
}

743 744 745 746 747 748 749 750 751
void rtc_change_mon_event(struct tm *tm)
{
    QObject *data;

    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
    qobject_decref(data);
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
static void configure_rtc_date_offset(const char *startdate, int legacy)
{
    time_t rtc_start_date;
    struct tm tm;

    if (!strcmp(startdate, "now") && legacy) {
        rtc_date_offset = -1;
    } else {
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
                   &tm.tm_year,
                   &tm.tm_mon,
                   &tm.tm_mday,
                   &tm.tm_hour,
                   &tm.tm_min,
                   &tm.tm_sec) == 6) {
            /* OK */
        } else if (sscanf(startdate, "%d-%d-%d",
                          &tm.tm_year,
                          &tm.tm_mon,
                          &tm.tm_mday) == 3) {
            tm.tm_hour = 0;
            tm.tm_min = 0;
            tm.tm_sec = 0;
        } else {
            goto date_fail;
        }
        tm.tm_year -= 1900;
        tm.tm_mon--;
        rtc_start_date = mktimegm(&tm);
        if (rtc_start_date == -1) {
        date_fail:
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
            exit(1);
        }
        rtc_date_offset = time(NULL) - rtc_start_date;
    }
}

static void configure_rtc(QemuOpts *opts)
{
    const char *value;

    value = qemu_opt_get(opts, "base");
    if (value) {
        if (!strcmp(value, "utc")) {
            rtc_utc = 1;
        } else if (!strcmp(value, "localtime")) {
            rtc_utc = 0;
        } else {
            configure_rtc_date_offset(value, 0);
        }
    }
Jan Kiszka's avatar
Jan Kiszka committed
805 806 807 808
    value = qemu_opt_get(opts, "clock");
    if (value) {
        if (!strcmp(value, "host")) {
            rtc_clock = host_clock;
Paolo Bonzini's avatar
Paolo Bonzini committed
809 810
        } else if (!strcmp(value, "rt")) {
            rtc_clock = rt_clock;
Jan Kiszka's avatar
Jan Kiszka committed
811 812 813 814 815 816 817
        } else if (!strcmp(value, "vm")) {
            rtc_clock = vm_clock;
        } else {
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
            exit(1);
        }
    }
818 819
    value = qemu_opt_get(opts, "driftfix");
    if (value) {
Blue Swirl's avatar
Blue Swirl committed
820
        if (!strcmp(value, "slew")) {
821 822 823 824 825 826 827 828 829 830
            static GlobalProperty slew_lost_ticks[] = {
                {
                    .driver   = "mc146818rtc",
                    .property = "lost_tick_policy",
                    .value    = "slew",
                },
                { /* end of list */ }
            };

            qdev_prop_register_global_list(slew_lost_ticks);
Blue Swirl's avatar
Blue Swirl committed
831
        } else if (!strcmp(value, "none")) {
832
            /* discard is default */
833 834 835 836 837 838 839
        } else {
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
            exit(1);
        }
    }
}

840 841 842 843 844
/***********************************************************/
/* Bluetooth support */
static int nb_hcis;
static int cur_hci;
static struct HCIInfo *hci_table[MAX_NICS];
845

846 847 848 849 850 851 852
static struct bt_vlan_s {
    struct bt_scatternet_s net;
    int id;
    struct bt_vlan_s *next;
} *first_bt_vlan;

/* find or alloc a new bluetooth "VLAN" */
blueswir1's avatar
blueswir1 committed
853
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
854 855 856 857 858 859
{
    struct bt_vlan_s **pvlan, *vlan;
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
        if (vlan->id == id)
            return &vlan->net;
    }
860
    vlan = g_malloc0(sizeof(struct bt_vlan_s));
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    vlan->id = id;
    pvlan = &first_bt_vlan;
    while (*pvlan != NULL)
        pvlan = &(*pvlan)->next;
    *pvlan = vlan;
    return &vlan->net;
}

static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
{
}

static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
{
    return -ENOTSUP;
}

static struct HCIInfo null_hci = {
    .cmd_send = null_hci_send,
    .sco_send = null_hci_send,
    .acl_send = null_hci_send,
    .bdaddr_set = null_hci_addr_set,
};

struct HCIInfo *qemu_next_hci(void)
{
    if (cur_hci == nb_hcis)
        return &null_hci;

    return hci_table[cur_hci++];
}

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
static struct HCIInfo *hci_init(const char *str)
{
    char *endp;
    struct bt_scatternet_s *vlan = 0;

    if (!strcmp(str, "null"))
        /* null */
        return &null_hci;
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
        /* host[:hciN] */
        return bt_host_hci(str[4] ? str + 5 : "hci0");
    else if (!strncmp(str, "hci", 3)) {
        /* hci[,vlan=n] */
        if (str[3]) {
            if (!strncmp(str + 3, ",vlan=", 6)) {
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
                if (*endp)
                    vlan = 0;
            }
        } else
            vlan = qemu_find_bt_vlan(0);
        if (vlan)
           return bt_new_hci(vlan);
    }

    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);

    return 0;
}

static int bt_hci_parse(const char *str)
{
    struct HCIInfo *hci;
926
    bdaddr_t bdaddr;
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

    if (nb_hcis >= MAX_NICS) {
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
        return -1;
    }

    hci = hci_init(str);
    if (!hci)
        return -1;

    bdaddr.b[0] = 0x52;
    bdaddr.b[1] = 0x54;
    bdaddr.b[2] = 0x00;
    bdaddr.b[3] = 0x12;
    bdaddr.b[4] = 0x34;
    bdaddr.b[5] = 0x56 + nb_hcis;
    hci->bdaddr_set(hci, bdaddr.b);

    hci_table[nb_hcis++] = hci;

    return 0;
}

static void bt_vhci_add(int vlan_id)
{
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);

    if (!vlan->slave)
        fprintf(stderr, "qemu: warning: adding a VHCI to "
                        "an empty scatternet %i\n", vlan_id);

    bt_vhci_init(bt_new_hci(vlan));
}

static struct bt_device_s *bt_device_add(const char *opt)
{
    struct bt_scatternet_s *vlan;
    int vlan_id = 0;
    char *endp = strstr(opt, ",vlan=");
    int len = (endp ? endp - opt : strlen(opt)) + 1;
    char devname[10];

    pstrcpy(devname, MIN(sizeof(devname), len), opt);

    if (endp) {
        vlan_id = strtol(endp + 6, &endp, 0);
        if (*endp) {
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
            return 0;
        }
    }

    vlan = qemu_find_bt_vlan(vlan_id);

    if (!vlan->slave)
        fprintf(stderr, "qemu: warning: adding a slave device to "
                        "an empty scatternet %i\n", vlan_id);

    if (!strcmp(devname, "keyboard"))
        return bt_keyboard_init(vlan);

    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
    return 0;
}

static int bt_parse(const char *opt)
{
    const char *endp, *p;
    int vlan;

    if (strstart(opt, "hci", &endp)) {
        if (!*endp || *endp == ',') {
            if (*endp)
                if (!strstart(endp, ",vlan=", 0))
                    opt = endp + 1;

            return bt_hci_parse(opt);
       }
    } else if (strstart(opt, "vhci", &endp)) {
        if (!*endp || *endp == ',') {
            if (*endp) {
                if (strstart(endp, ",vlan=", &p)) {
                    vlan = strtol(p, (char **) &endp, 0);
                    if (*endp) {
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
                        return 1;
                    }
                } else {
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
                    return 1;
                }
            } else
                vlan = 0;

            bt_vhci_add(vlan);
            return 0;
        }
    } else if (strstart(opt, "device:", &endp))
        return !bt_device_add(endp);

    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
    return 1;
}

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
static int parse_sandbox(QemuOpts *opts, void *opaque)
{
    /* FIXME: change this to true for 1.3 */
    if (qemu_opt_get_bool(opts, "enable", false)) {
#ifdef CONFIG_SECCOMP
        if (seccomp_start() < 0) {
            qerror_report(ERROR_CLASS_GENERIC_ERROR,
                          "failed to install seccomp syscall filter in the kernel");
            return -1;
        }
#else
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "sandboxing request but seccomp is not compiled into this build");
        return -1;
#endif
    }

    return 0;
}

1051 1052
bool usb_enabled(bool default_usb)
{
1053
    return qemu_opt_get_bool(qemu_get_machine_opts(), "usb", default_usb);
1054 1055
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
#ifndef _WIN32
static int parse_add_fd(QemuOpts *opts, void *opaque)
{
    int fd, dupfd, flags;
    int64_t fdset_id;
    const char *fd_opaque = NULL;

    fd = qemu_opt_get_number(opts, "fd", -1);
    fdset_id = qemu_opt_get_number(opts, "set", -1);
    fd_opaque = qemu_opt_get(opts, "opaque");

    if (fd < 0) {
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "fd option is required and must be non-negative");
        return -1;
    }

    if (fd <= STDERR_FILENO) {
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "fd cannot be a standard I/O stream");
        return -1;
    }

    /*
     * All fds inherited across exec() necessarily have FD_CLOEXEC
     * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
     */
    flags = fcntl(fd, F_GETFD);
    if (flags == -1 || (flags & FD_CLOEXEC)) {
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "fd is not valid or already in use");
        return -1;
    }

    if (fdset_id < 0) {
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "set option is required and must be non-negative");
        return -1;
    }

#ifdef F_DUPFD_CLOEXEC
    dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
#else
    dupfd = dup(fd);
    if (dupfd != -1) {
        qemu_set_cloexec(dupfd);
    }
#endif
    if (dupfd == -1) {
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
                      "Error duplicating fd: %s", strerror(errno));
        return -1;
    }

    /* add the duplicate fd, and optionally the opaque string, to the fd set */
    monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
                         fd_opaque, NULL);

    return 0;
}

static int cleanup_add_fd(QemuOpts *opts, void *opaque)
{
    int fd;

    fd = qemu_opt_get_number(opts, "fd", -1);
    close(fd);

    return 0;
}
#endif

1128 1129 1130
/***********************************************************/
/* QEMU Block devices */