vl.c 126 KB
Newer Older
1
/*
2
 * QEMU System Emulator
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
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>
29 30
#include <sys/time.h>

blueswir1's avatar
blueswir1 committed
31 32
#include "config-host.h"

33
#ifdef CONFIG_SECCOMP
34
#include "sysemu/seccomp.h"
35 36
#endif

37 38 39 40
#if defined(CONFIG_VDE)
#include <libvdeplug.h>
#endif

bellard's avatar
bellard committed
41
#ifdef CONFIG_SDL
42
#if defined(__APPLE__) || defined(main)
Stefan Weil's avatar
Stefan Weil committed
43
#include <SDL.h>
malc's avatar
malc committed
44 45 46
int qemu_main(int argc, char **argv, char **envp);
int main(int argc, char **argv)
{
47
    return qemu_main(argc, argv, NULL);
malc's avatar
malc committed
48 49 50
}
#undef main
#define main qemu_main
bellard's avatar
bellard committed
51
#endif
bellard's avatar
bellard committed
52
#endif /* CONFIG_SDL */
53

54 55 56 57 58
#ifdef CONFIG_COCOA
#undef main
#define main qemu_main
#endif /* CONFIG_COCOA */

59 60
#include <glib.h>

61
#include "qemu/sockets.h"
62 63
#include "hw/hw.h"
#include "hw/boards.h"
64
#include "sysemu/accel.h"
65
#include "hw/usb.h"
66 67
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
68
#include "hw/bt.h"
69 70 71
#include "sysemu/watchdog.h"
#include "hw/i386/smbios.h"
#include "hw/xen/xen.h"
72
#include "hw/qdev.h"
73
#include "hw/loader.h"
74
#include "monitor/qdev.h"
75
#include "sysemu/bt.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
76
#include "net/net.h"
77
#include "net/slirp.h"
78
#include "monitor/monitor.h"
79
#include "ui/console.h"
80
#include "sysemu/sysemu.h"
81
#include "exec/gdbstub.h"
82
#include "qemu/timer.h"
83
#include "sysemu/char.h"
Michael Tokarev's avatar
Michael Tokarev committed
84
#include "qemu/bitmap.h"
85
#include "sysemu/blockdev.h"
86
#include "hw/block/block.h"
87
#include "migration/block.h"
88
#include "sysemu/tpm.h"
89
#include "sysemu/dma.h"
90
#include "audio/audio.h"
91
#include "migration/migration.h"
92
#include "sysemu/kvm.h"
93
#include "qapi/qmp/qjson.h"
94 95
#include "qemu/option.h"
#include "qemu/config-file.h"
96
#include "qemu-options.h"
97
#include "qmp-commands.h"
98
#include "qemu/main-loop.h"
99
#ifdef CONFIG_VIRTFS
100 101
#include "fsdev/qemu-fsdev.h"
#endif
102
#include "sysemu/qtest.h"
103

104
#include "disas/disas.h"
bellard's avatar
bellard committed
105

106

Jan Kiszka's avatar
Jan Kiszka committed
107
#include "slirp/libslirp.h"
108

109
#include "trace.h"
110
#include "trace/control.h"
111
#include "qemu/queue.h"
112 113
#include "sysemu/cpus.h"
#include "sysemu/arch_init.h"
114
#include "qemu/osdep.h"
115

Gerd Hoffmann's avatar
Gerd Hoffmann committed
116
#include "ui/qemu-spice.h"
117
#include "qapi/string-input-visitor.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
118
#include "qapi/opts-visitor.h"
119 120
#include "qom/object_interfaces.h"
#include "qapi-event.h"
Gerd Hoffmann's avatar
Gerd Hoffmann committed
121

122
#define DEFAULT_RAM_SIZE 128
123

124
#define MAX_VIRTIO_CONSOLES 1
125
#define MAX_SCLP_CONSOLES 1
126

127 128
static const char *data_dir[16];
static int data_dir_idx;
129
const char *bios_name = NULL;
130
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131
DisplayType display_type = DT_DEFAULT;
Blue Swirl's avatar
Blue Swirl committed
132
static int display_remote;
133
const char* keyboard_layout = NULL;
134
ram_addr_t ram_size;
135 136
const char *mem_path = NULL;
int mem_prealloc = 0; /* force preallocation of physical target memory */
137
bool enable_mlock = false;
138
int nb_nics;
bellard's avatar
bellard committed
139
NICInfo nd_table[MAX_NICS];
Paolo Bonzini's avatar
Paolo Bonzini committed
140
int autostart;
141 142
static int rtc_utc = 1;
static int rtc_date_offset = -1; /* -1 means no change */
143
QEMUClockType rtc_clock;
144
int vga_interface_type = VGA_NONE;
blueswir1's avatar
blueswir1 committed
145 146
static int full_screen = 0;
static int no_frame = 0;
147
int no_quit = 0;
148 149 150
#ifdef CONFIG_GTK
static bool grab_on_hover;
#endif
151
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154
CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155
int win2k_install_hack = 0;
156
int singlestep = 0;
bellard's avatar
bellard committed
157
int smp_cpus = 1;
158
int max_cpus = 0;
159 160
int smp_cores = 1;
int smp_threads = 1;
Jes Sorensen's avatar
Jes Sorensen committed
161
#ifdef CONFIG_VNC
162
const char *vnc_display;
Jes Sorensen's avatar
Jes Sorensen committed
163
#endif
bellard's avatar
bellard committed
164
int acpi_enabled = 1;
165
int no_hpet = 0;
166
int fd_bootchk = 1;
Blue Swirl's avatar
Blue Swirl committed
167
static int no_reboot;
aurel32's avatar
aurel32 committed
168
int no_shutdown = 0;
169
int cursor_hide = 1;
170
int graphic_rotate = 0;
171
const char *watchdog;
172
QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173
int nb_option_roms;
174
int semihosting_enabled = 0;
175
int old_param = 0;
176
const char *qemu_name;
177
int alt_grab = 0;
178
int ctrl_grab = 0;
179 180
unsigned int nb_prom_envs = 0;
const char *prom_envs[MAX_PROM_ENVS];
181
int boot_menu;
182
bool boot_strict;
183
uint8_t *boot_splash_filedata;
184
size_t boot_splash_filedata_size;
185
uint8_t qemu_extra_params_fw[2];
186

187
int icount_align_option;
188

189
int nb_numa_nodes;
190
int max_numa_nodeid;
191
NodeInfo numa_info[MAX_NODES];
192

193 194 195
/* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
 * little-endian "wire format" described in the SMBIOS 2.6 specification.
 */
196
uint8_t qemu_uuid[16];
197
bool qemu_uuid_set;
198

199 200 201
static QEMUBootSetHandler *boot_set_handler;
static void *boot_set_opaque;

Gerd Hoffmann's avatar
Gerd Hoffmann committed
202 203 204
static NotifierList exit_notifiers =
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);

205 206 207
static NotifierList machine_init_done_notifiers =
    NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);

208
bool xen_allowed;
209 210 211
uint32_t xen_domid;
enum xen_mode xen_mode = XEN_EMULATE;

212
static int has_defaults = 1;
213
static int default_serial = 1;
214
static int default_parallel = 1;
215
static int default_virtcon = 1;
216
static int default_sclp = 1;
217
static int default_monitor = 1;
218 219 220
static int default_floppy = 1;
static int default_cdrom = 1;
static int default_sdcard = 1;
221
static int default_vga = 1;
222 223 224 225 226

static struct {
    const char *driver;
    int *flag;
} default_list[] = {
227 228
    { .driver = "isa-serial",           .flag = &default_serial    },
    { .driver = "isa-parallel",         .flag = &default_parallel  },
229
    { .driver = "isa-fdc",              .flag = &default_floppy    },
230 231
    { .driver = "ide-cd",               .flag = &default_cdrom     },
    { .driver = "ide-hd",               .flag = &default_cdrom     },
232
    { .driver = "ide-drive",            .flag = &default_cdrom     },
233
    { .driver = "scsi-cd",              .flag = &default_cdrom     },
234 235 236
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
237 238 239 240 241 242
    { .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       },
243 244
};

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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
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",
352
            .type = QEMU_OPT_NUMBER,
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            .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",
370 371 372 373
        },{
            .name = "firmware",
            .type = QEMU_OPT_STRING,
            .help = "firmware image",
374 375 376 377
        },{
            .name = "kvm-type",
            .type = QEMU_OPT_STRING,
            .help = "Specifies the KVM virtualization mode (HV, PR)",
378 379 380 381
        },{
            .name = PC_MACHINE_MAX_RAM_BELOW_4G,
            .type = QEMU_OPT_SIZE,
            .help = "maximum ram below the 4G boundary (32bit boundary)",
382 383
        }, {
            .name = PC_MACHINE_VMPORT,
384
            .type = QEMU_OPT_STRING,
385
            .help = "Enable vmport (pc & q35)",
386 387 388 389
        },{
            .name = "iommu",
            .type = QEMU_OPT_BOOL,
            .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
390 391 392 393 394 395 396
        },
        { /* End of list */ }
    },
};

static QemuOptsList qemu_boot_opts = {
    .name = "boot-opts",
397 398
    .implied_opt_name = "order",
    .merge_lists = true,
399 400 401 402 403 404 405 406 407 408
    .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
    .desc = {
        {
            .name = "order",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "once",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "menu",
409
            .type = QEMU_OPT_BOOL,
410 411 412 413 414 415 416 417 418
        }, {
            .name = "splash",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "splash-time",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "reboot-timeout",
            .type = QEMU_OPT_STRING,
419 420
        }, {
            .name = "strict",
421
            .type = QEMU_OPT_BOOL,
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        },
        { /*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 = {
        { }
    },
};

457 458 459 460 461
static QemuOptsList qemu_tpmdev_opts = {
    .name = "tpmdev",
    .implied_opt_name = "type",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
    .desc = {
462
        /* options are defined in the TPM backends */
463 464 465 466
        { /* end of list */ }
    },
};

467 468 469 470 471 472 473 474 475 476 477 478
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 */ }
    },
};

479 480 481 482 483 484 485 486 487 488 489 490
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 */ }
    },
};

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
static QemuOptsList qemu_name_opts = {
    .name = "name",
    .implied_opt_name = "guest",
    .merge_lists = true,
    .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
    .desc = {
        {
            .name = "guest",
            .type = QEMU_OPT_STRING,
            .help = "Sets the name of the guest.\n"
                    "This name will be displayed in the SDL window caption.\n"
                    "The name will also be used for the VNC server",
        }, {
            .name = "process",
            .type = QEMU_OPT_STRING,
            .help = "Sets the name of the QEMU process, as shown in top etc",
507 508 509 510 511 512
        }, {
            .name = "debug-threads",
            .type = QEMU_OPT_BOOL,
            .help = "When enabled, name the individual threads; defaults off.\n"
                    "NOTE: The thread names are for debugging and not a\n"
                    "stable API.",
513 514 515 516 517
        },
        { /* End of list */ }
    },
};

518 519 520 521 522 523 524 525 526 527
static QemuOptsList qemu_mem_opts = {
    .name = "memory",
    .implied_opt_name = "size",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
    .merge_lists = true,
    .desc = {
        {
            .name = "size",
            .type = QEMU_OPT_SIZE,
        },
528 529 530 531 532 533 534 535
        {
            .name = "slots",
            .type = QEMU_OPT_NUMBER,
        },
        {
            .name = "maxmem",
            .type = QEMU_OPT_SIZE,
        },
536 537 538 539
        { /* end of list */ }
    },
};

540 541 542 543 544 545 546 547 548
static QemuOptsList qemu_icount_opts = {
    .name = "icount",
    .implied_opt_name = "shift",
    .merge_lists = true,
    .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
    .desc = {
        {
            .name = "shift",
            .type = QEMU_OPT_STRING,
549 550 551
        }, {
            .name = "align",
            .type = QEMU_OPT_BOOL,
552 553 554 555 556
        },
        { /* end of list */ }
    },
};

557 558 559 560 561 562 563
/**
 * Get machine options
 *
 * Returns: machine options (never null).
 */
QemuOpts *qemu_get_machine_opts(void)
{
564
    return qemu_find_opts_singleton("machine");
565 566
}

567 568 569 570 571
const char *qemu_get_vm_name(void)
{
    return qemu_name;
}

572 573 574
static void res_free(void)
{
    if (boot_splash_filedata != NULL) {
575
        g_free(boot_splash_filedata);
576 577 578 579
        boot_splash_filedata = NULL;
    }
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
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;
}

595 596 597
/***********************************************************/
/* QEMU state */

598
static RunState current_run_state = RUN_STATE_PRELAUNCH;
599

600 601 602 603
/* We use RUN_STATE_MAX but any invalid value will do */
static RunState vmstop_requested = RUN_STATE_MAX;
static QemuMutex vmstop_lock;

604 605 606 607 608 609 610
typedef struct {
    RunState from;
    RunState to;
} RunStateTransition;

static const RunStateTransition runstate_transitions_def[] = {
    /*     from      ->     to      */
611
    { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
612
    { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
613

614
    { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
615
    { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
616

617
    { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
618
    { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
619

620
    { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
621
    { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
622

623
    { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
624
    { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
625

626
    { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
627
    { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
628

629
    { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
630
    { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
631
    { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
632

633 634
    { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
    { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
635

636
    { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
637

638 639 640 641 642 643 644 645 646
    { 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 },
647
    { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
648

649
    { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
650

651
    { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
652
    { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
653

654 655 656 657 658
    { 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 },

659
    { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
660
    { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
661

662
    { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
663
    { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
664

665
    { RUN_STATE_MAX, RUN_STATE_MAX },
666 667
};

668 669
static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];

670 671 672 673 674
bool runstate_check(RunState state)
{
    return current_run_state == state;
}

Blue Swirl's avatar
Blue Swirl committed
675
static void runstate_init(void)
676 677 678 679
{
    const RunStateTransition *p;

    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
680
    for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
681 682
        runstate_valid_transitions[p->from][p->to] = true;
    }
683 684

    qemu_mutex_init(&vmstop_lock);
685 686 687
}

/* This function will abort() on invalid state transitions */
688 689
void runstate_set(RunState new_state)
{
690 691 692 693 694 695
    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]);
696 697
        abort();
    }
698
    trace_runstate_set(new_state);
699 700 701
    current_run_state = new_state;
}

702
int runstate_is_running(void)
703
{
704
    return runstate_check(RUN_STATE_RUNNING);
705 706
}

707 708 709
bool runstate_needs_reset(void)
{
    return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
710
        runstate_check(RUN_STATE_SHUTDOWN);
711 712
}

713
StatusInfo *qmp_query_status(Error **errp)
714
{
715 716 717 718 719 720 721
    StatusInfo *info = g_malloc0(sizeof(*info));

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

    return info;
722 723
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
static bool qemu_vmstop_requested(RunState *r)
{
    qemu_mutex_lock(&vmstop_lock);
    *r = vmstop_requested;
    vmstop_requested = RUN_STATE_MAX;
    qemu_mutex_unlock(&vmstop_lock);
    return *r < RUN_STATE_MAX;
}

void qemu_system_vmstop_request_prepare(void)
{
    qemu_mutex_lock(&vmstop_lock);
}

void qemu_system_vmstop_request(RunState state)
{
    vmstop_requested = state;
    qemu_mutex_unlock(&vmstop_lock);
    qemu_notify_event();
}

void vm_start(void)
{
    RunState requested;

    qemu_vmstop_requested(&requested);
    if (runstate_is_running() && requested == RUN_STATE_MAX) {
        return;
    }

    /* Ensure that a STOP/RESUME pair of events is emitted if a
     * vmstop request was pending.  The BLOCK_IO_ERROR event, for
     * example, according to documentation is always followed by
     * the STOP event.
     */
    if (runstate_is_running()) {
Wenchao Xia's avatar
Wenchao Xia committed
760
        qapi_event_send_stop(&error_abort);
761 762 763 764 765 766 767
    } else {
        cpu_enable_ticks();
        runstate_set(RUN_STATE_RUNNING);
        vm_state_notify(1, RUN_STATE_RUNNING);
        resume_all_vcpus();
    }

Wenchao Xia's avatar
Wenchao Xia committed
768
    qapi_event_send_resume(&error_abort);
769 770 771
}


772 773
/***********************************************************/
/* real time host monotonic timer */
774

775 776 777 778 779 780 781 782 783 784
/***********************************************************/
/* 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)
785
            gmtime_r(&ti, tm);
786
        else
787
            localtime_r(&ti, tm);
788 789
    } else {
        ti -= rtc_date_offset;
790
        gmtime_r(&ti, tm);
791 792 793 794 795 796 797 798 799 800
    }
}

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

    if (rtc_date_offset == -1)
        if (rtc_utc)
            seconds = mktimegm(tm);
801 802 803 804 805
        else {
            struct tm tmp = *tm;
            tmp.tm_isdst = -1; /* use timezone to figure it out */
            seconds = mktime(&tmp);
	}
806 807 808 809 810 811
    else
        seconds = mktimegm(tm) + rtc_date_offset;

    return seconds - time(NULL);
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
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
865 866 867
    value = qemu_opt_get(opts, "clock");
    if (value) {
        if (!strcmp(value, "host")) {
868
            rtc_clock = QEMU_CLOCK_HOST;
Paolo Bonzini's avatar
Paolo Bonzini committed
869
        } else if (!strcmp(value, "rt")) {
870
            rtc_clock = QEMU_CLOCK_REALTIME;
Jan Kiszka's avatar
Jan Kiszka committed
871
        } else if (!strcmp(value, "vm")) {
872
            rtc_clock = QEMU_CLOCK_VIRTUAL;
Jan Kiszka's avatar
Jan Kiszka committed
873 874 875 876 877
        } else {
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
            exit(1);
        }
    }
878 879
    value = qemu_opt_get(opts, "driftfix");
    if (value) {
Blue Swirl's avatar
Blue Swirl committed
880
        if (!strcmp(value, "slew")) {
881 882 883 884 885 886 887 888 889 890
            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
891
        } else if (!strcmp(value, "none")) {
892
            /* discard is default */
893 894 895 896 897 898 899
        } else {
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
            exit(1);
        }
    }
}

900 901 902 903 904
/***********************************************************/
/* Bluetooth support */
static int nb_hcis;
static int cur_hci;
static struct HCIInfo *hci_table[MAX_NICS];
905

906 907 908 909 910 911 912 913
struct HCIInfo *qemu_next_hci(void)
{
    if (cur_hci == nb_hcis)
        return &null_hci;

    return hci_table[cur_hci++];
}

914 915 916
static int bt_hci_parse(const char *str)
{
    struct HCIInfo *hci;
917
    bdaddr_t bdaddr;
918 919 920 921 922 923 924 925 926 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

    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;
}

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
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;
}

1042
static int parse_name(QemuOpts *opts, void *opaque)
1043 1044 1045
{
    const char *proc_name;

1046 1047 1048
    if (qemu_opt_get(opts, "debug-threads")) {
        qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
    }
1049 1050 1051 1052 1053 1054
    qemu_name = qemu_opt_get(opts, "guest");

    proc_name = qemu_opt_get(opts, "process");
    if (proc_name) {
        os_set_proc_name(proc_name);
    }
1055 1056

    return 0;
1057 1058
}

1059 1060
bool usb_enabled(bool default_usb)
{
1061 1062
    return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
                             has_defaults && default_usb);
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 1128 1129 1130 1131 1132 1133 1134 1135 1136
#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

1137 1138 1139
/***********************************************************/
/* QEMU Block devices */

1140 1141 1142 1143 1144 1145
#define HD_OPTS "media=disk"
#define CDROM_OPTS "media=cdrom"
#define FD_OPTS ""
#define PFLASH_OPTS ""
#define MTD_OPTS ""
#define SD_OPTS ""
1146

1147 1148
static int drive_init_func(QemuOpts *opts, void *opaque)
{
1149
    BlockInterfaceType *block_default_type = opaque;
1150

1151
    return drive_new(opts, *block_default_type) == NULL;
1152 1153 1154 1155
}

static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
{
1156
    if (qemu_opt_get(opts, "snapshot") == NULL) {
1157 1158 1159 1160 1161
        qemu_opt_set(opts, "snapshot", "on");
    }
    return 0;
}

1162 1163
static void default_drive(int enable, int snapshot, BlockInterfaceType type,
                          int index, const char *optstr)
1164 1165
{
    QemuOpts *opts;
1166
    DriveInfo *dinfo;
1167 1168 1169 1170 1171 1172 1173 1174 1175

    if (!enable || drive_get_by_index(type, index)) {
        return;
    }

    opts = drive_add(type, index, NULL, optstr);
    if (snapshot) {
        drive_enable_snapshot(opts, NULL);
    }
1176 1177 1178

    dinfo = drive_new(opts, type);
    if (!dinfo) {
1179 1180
        exit(1);
    }
1181 1182
    dinfo->is_default = true;

1183 1184
}

1185 1186 1187 1188 1189 1190
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
{
    boot_set_handler = func;
    boot_set_opaque = opaque;
}

1191
int qemu_boot_set(const char *boot_order)
1192 1193 1194 1195
{
    if (!boot_set_handler) {
        return -EINVAL;
    }
1196
    return boot_set_handler(boot_set_opaque, boot_order);
1197 1198
}

1199
static void validate_bootdevices(const char *devices)
Jan Kiszka's avatar
Jan Kiszka committed
1200 1201 1202 1203 1204 1205 1206 1207 1208
{
    /* We just do some generic consistency checks */
    const char *p;
    int bitmap = 0;

    for (p = devices; *p != '\0'; p++) {
        /* Allowed boot devices are:
         * a-b: floppy disk drives
         * c-f: IDE disk drives
1209
         * g-m: machine implementation dependent drives
Jan Kiszka's avatar
Jan Kiszka committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
         * n-p: network devices
         * It's up to each machine implementation to check if the given boot
         * devices match the actual hardware implementation and firmware
         * features.
         */
        if (*p < 'a' || *p > 'p') {
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
            exit(1);
        }
        if (bitmap & (1 << (*p - 'a'))) {
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
            exit(1);
        }
        bitmap |= 1 << (*p - 'a');
    }
}

1227
static void restore_boot_order(void *opaque)
Jan Kiszka's avatar
Jan Kiszka committed
1228
{
1229
    char *normal_boot_order = opaque;
Alex Williamson's avatar
Alex Williamson committed
1230 1231 1232 1233 1234 1235 1236
    static int first = 1;

    /* Restore boot order and remove ourselves after the first boot */
    if (first) {
        first = 0;
        return;
    }
Jan Kiszka's avatar
Jan Kiszka committed
1237

1238
    qemu_boot_set(normal_boot_order);
Jan Kiszka's avatar
Jan Kiszka committed
1239

1240 1241
    qemu_unregister_reset(restore_boot_order, normal_boot_order);
    g_free(normal_boot_order);
Jan Kiszka's avatar
Jan Kiszka committed
1242 1243
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
static QemuOptsList qemu_smp_opts = {
    .name = "smp-opts",
    .implied_opt_name = "cpus",
    .merge_lists = true,
    .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
    .desc = {
        {
            .name = "cpus",
            .type = QEMU_OPT_NUMBER,
        }, {
            .name = "sockets",
            .type = QEMU_OPT_NUMBER,
        }, {
            .name = "cores",
            .type = QEMU_OPT_NUMBER,
        }, {
            .name = "threads",
            .type = QEMU_OPT_NUMBER,
        }, {
            .name = "maxcpus",
            .type = QEMU_OPT_NUMBER,
        },
        { /*End of list */ }
    },
};

static void smp_parse(QemuOpts *opts)
1271
{
1272
    if (opts) {
1273

1274 1275 1276 1277 1278 1279 1280 1281 1282
        unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
        unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
        unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
        unsigned threads = qemu_opt_get_number(opts, "threads", 0);

        /* compute missing values, prefer sockets over cores over threads */
        if (cpus == 0 || sockets == 0) {
            sockets = sockets > 0 ? sockets : 1;
            cores = cores > 0 ? cores : 1;
1283
            threads = threads > 0 ? threads : 1;
1284 1285 1286
            if (cpus == 0) {
                cpus = cores * threads * sockets;
            }
1287
        } else {
1288 1289 1290 1291 1292 1293
            if (cores == 0) {
                threads = threads > 0 ? threads : 1;
                cores = cpus / (sockets * threads);
            } else {
                threads = cpus / (cores * sockets);
            }
1294
        }
1295 1296 1297 1298 1299 1300 1301

        max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);

        smp_cpus = cpus;
        smp_cores = cores > 0 ? cores : 1;
        smp_threads = threads > 0 ? threads : 1;

1302
    }
1303 1304

    if (max_cpus == 0) {
1305
        max_cpus = smp_cpus;
1306 1307
    }

1308
    if (max_cpus > MAX_CPUMASK_BITS) {
1309 1310 1311 1312 1313 1314 1315 1316
        fprintf(stderr, "Unsupported number of maxcpus\n");
        exit(1);
    }
    if (max_cpus < smp_cpus) {
        fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
        exit(1);
    }

1317 1318
}

1319
static void realtime_init(void)
1320 1321 1322 1323 1324 1325 1326 1327 1328
{
    if (enable_mlock) {
        if (os_mlock() < 0) {
            fprintf(stderr, "qemu: locking memory failed\n");
            exit(1);
        }
    }
}

1329 1330 1331 1332 1333 1334

static void configure_msg(QemuOpts *opts)
{
    enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
}

bellard's avatar
bellard committed
1335 1336 1337
/***********************************************************/
/* USB devices */

1338
static int usb_device_add(const char *devname)
bellard's avatar
bellard committed
1339
{
1340
    USBDevice *dev = NULL;
1341 1342 1343
#ifndef CONFIG_LINUX
    const char *p;
#endif
bellard's avatar
bellard committed
1344

1345
    if (!usb_enabled(false)) {
bellard's avatar
bellard committed
1346
        return -1;
1347
    }
bellard's avatar
bellard committed
1348

1349 1350 1351 1352 1353
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
    dev = usbdevice_create(devname);
    if (dev)
        goto done;

1354
    /* the other ones */
1355 1356
#ifndef CONFIG_LINUX
    /* only the linux version is qdev-ified, usb-bsd still needs this */
bellard's avatar
bellard committed
1357
    if (strstart(devname, "host:", &p)) {
1358
        dev = usb_host_device_open(usb_bus_find(-1), p);
bellard's avatar
bellard committed
1359
    }
1360
#endif
pbrook's avatar
pbrook committed
1361 1362 1363
    if (!dev)
        return -1;

1364
done:
bellard's avatar
bellard committed
1365 1366 1367
    return 0;
}

1368 1369 1370 1371 1372
static int usb_device_del(const char *devname)
{
    int bus_num, addr;
    const char *p;

1373 1374 1375
    if (strstart(devname, "host:", &p)) {
        return -1;
    }
1376

1377
    if (!usb_enabled(false)) {
1378
        return -1;
1379
    }
1380 1381 1382 1383 1384 1385 1386

    p = strchr(devname, '.');
    if (!p)
        return -1;
    bus_num = strtoul(devname, NULL, 0);
    addr = strtoul(p + 1, NULL, 0);

1387
    return usb_device_delete_addr(bus_num, addr);
1388 1389
}

1390 1391
static int usb_parse(const char *cmdline)
{
1392
    int r;
1393
    r = usb_device_add(cmdline);
1394 1395 1396 1397
    if (r < 0) {
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
    }
    return r;
1398 1399
}

1400
void do_usb_add(Monitor *mon, const QDict *qdict)
bellard's avatar
bellard committed
1401
{
1402
    const char *devname = qdict_get_str(qdict, "devname");
1403
    if (usb_device_add(devname) < 0) {
1404
        error_report("could not add USB device '%s'", devname);
1405
    }
bellard's avatar
bellard committed
1406 1407
}

1408
void do_usb_del(Monitor *mon, const QDict *qdict)
bellard's avatar
bellard committed
1409
{
1410 1411
    const char *devname = qdict_get_str(qdict, "devname");
    if (usb_device_del(devname) < 0) {
1412
        error_report("could not delete USB device '%s'", devname);
1413
    }
bellard's avatar
bellard committed
1414 1415
}

1416 1417 1418
/***********************************************************/
/* machine registration */

1419
MachineState *current_machine;
1420

1421 1422 1423
static void machine_class_init(ObjectClass *oc, void *data)
{
    MachineClass *mc = MACHINE_CLASS(oc);
1424
    QEMUMachine *qm = data;
1425

1426
    mc->family = qm->family;
1427 1428 1429 1430 1431 1432 1433 1434
    mc->name = qm->name;
    mc->alias = qm->alias;
    mc->desc = qm->desc;
    mc->init = qm->init;
    mc->reset = qm->reset;
    mc->hot_add_cpu = qm->hot_add_cpu;
    mc->kvm_type = qm->kvm_type;
    mc->block_default_type = qm->block_default_type;
1435
    mc->units_per_default_bus = qm->units_per_default_bus;
1436 1437 1438 1439 1440 1441 1442 1443
    mc->max_cpus = qm->max_cpus;
    mc->no_serial = qm->no_serial;
    mc->no_parallel = qm->no_parallel;
    mc->use_virtcon = qm->use_virtcon;
    mc->use_sclp = qm->use_sclp;
    mc->no_floppy = qm->no_floppy;
    mc->no_cdrom = qm->no_cdrom;
    mc->no_sdcard = qm->no_sdcard;
1444
    mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1445 1446 1447
    mc->is_default = qm->is_default;
    mc->default_machine_opts = qm->default_machine_opts;
    mc->default_boot_order = qm->default_boot_order;
1448
    mc->default_display = qm->default_display;
1449 1450
    mc->compat_props = qm->compat_props;
    mc->hw_version = qm->hw_version;
1451 1452
}

1453 1454
int qemu_register_machine(QEMUMachine *m)
{
1455
    char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1456
    TypeInfo ti = {
1457
        .name       = name,
1458 1459 1460 1461 1462 1463
        .parent     = TYPE_MACHINE,
        .class_init = machine_class_init,
        .class_data = (void *)m,
    };

    type_register(&ti);
1464
    g_free(name);
1465

1466 1467 1468
    return 0;
}

1469
static MachineClass *find_machine(const char *name)
1470
{
1471
    GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1472
    MachineClass *mc = NULL;
1473

1474
    for (el = machines; el; el = el->next) {
1475
        MachineClass *temp = el->data;
1476

1477
        if (!strcmp(temp->name, name)) {
1478
            mc = temp;
1479 1480
            break;
        }
1481 1482
        if (temp->alias &&
            !strcmp(temp->alias, name)) {
1483
            mc = temp;
1484 1485
            break;
        }
1486
    }
1487 1488

    g_slist_free(machines);
1489
    return mc;
1490 1491
}

1492
MachineClass *find_default_machine(void)
1493
{
1494
    GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1495
    MachineClass *mc = NULL;
1496

1497
    for (el = machines; el; el = el->next) {
1498
        MachineClass *temp = el->data;
1499

1500
        if (temp->is_default) {