qapi event: convert SUSPEND
[qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30
31 #include "config-host.h"
32
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
36
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
40
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
46 {
47 return qemu_main(argc, argv, NULL);
48 }
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
53
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
58
59 #include <glib.h>
60
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "hw/usb.h"
65 #include "hw/pcmcia.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #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"
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"
84 #include "qemu/bitmap.h"
85 #include "qemu/cache-utils.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
100 #ifdef CONFIG_VIRTFS
101 #include "fsdev/qemu-fsdev.h"
102 #endif
103 #include "sysemu/qtest.h"
104
105 #include "disas/disas.h"
106
107
108 #include "slirp/libslirp.h"
109
110 #include "trace.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
116
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
122
123 #define DEFAULT_RAM_SIZE 128
124
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
127
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 DisplayType display_type = DT_DEFAULT;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 int nb_nics;
139 NICInfo nd_table[MAX_NICS];
140 int autostart;
141 static int rtc_utc = 1;
142 static int rtc_date_offset = -1; /* -1 means no change */
143 QEMUClockType rtc_clock;
144 int vga_interface_type = VGA_NONE;
145 static int full_screen = 0;
146 static int no_frame = 0;
147 int no_quit = 0;
148 #ifdef CONFIG_GTK
149 static bool grab_on_hover;
150 #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;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 #ifdef CONFIG_VNC
162 const char *vnc_display;
163 #endif
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
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 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 static 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 typedef struct FWBootEntry FWBootEntry;
188
189 struct FWBootEntry {
190 QTAILQ_ENTRY(FWBootEntry) link;
191 int32_t bootindex;
192 DeviceState *dev;
193 char *suffix;
194 };
195
196 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
197 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
198
199 int nb_numa_nodes;
200 NodeInfo numa_info[MAX_NODES];
201
202 uint8_t qemu_uuid[16];
203 bool qemu_uuid_set;
204
205 static QEMUBootSetHandler *boot_set_handler;
206 static void *boot_set_opaque;
207
208 static NotifierList exit_notifiers =
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
210
211 static NotifierList machine_init_done_notifiers =
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
213
214 static bool tcg_allowed = true;
215 bool xen_allowed;
216 uint32_t xen_domid;
217 enum xen_mode xen_mode = XEN_EMULATE;
218 static int tcg_tb_size;
219
220 static int has_defaults = 1;
221 static int default_serial = 1;
222 static int default_parallel = 1;
223 static int default_virtcon = 1;
224 static int default_sclp = 1;
225 static int default_monitor = 1;
226 static int default_floppy = 1;
227 static int default_cdrom = 1;
228 static int default_sdcard = 1;
229 static int default_vga = 1;
230
231 static struct {
232 const char *driver;
233 int *flag;
234 } default_list[] = {
235 { .driver = "isa-serial", .flag = &default_serial },
236 { .driver = "isa-parallel", .flag = &default_parallel },
237 { .driver = "isa-fdc", .flag = &default_floppy },
238 { .driver = "ide-cd", .flag = &default_cdrom },
239 { .driver = "ide-hd", .flag = &default_cdrom },
240 { .driver = "ide-drive", .flag = &default_cdrom },
241 { .driver = "scsi-cd", .flag = &default_cdrom },
242 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
243 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
244 { .driver = "virtio-serial", .flag = &default_virtcon },
245 { .driver = "VGA", .flag = &default_vga },
246 { .driver = "isa-vga", .flag = &default_vga },
247 { .driver = "cirrus-vga", .flag = &default_vga },
248 { .driver = "isa-cirrus-vga", .flag = &default_vga },
249 { .driver = "vmware-svga", .flag = &default_vga },
250 { .driver = "qxl-vga", .flag = &default_vga },
251 };
252
253 static QemuOptsList qemu_rtc_opts = {
254 .name = "rtc",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
256 .desc = {
257 {
258 .name = "base",
259 .type = QEMU_OPT_STRING,
260 },{
261 .name = "clock",
262 .type = QEMU_OPT_STRING,
263 },{
264 .name = "driftfix",
265 .type = QEMU_OPT_STRING,
266 },
267 { /* end of list */ }
268 },
269 };
270
271 static QemuOptsList qemu_sandbox_opts = {
272 .name = "sandbox",
273 .implied_opt_name = "enable",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
275 .desc = {
276 {
277 .name = "enable",
278 .type = QEMU_OPT_BOOL,
279 },
280 { /* end of list */ }
281 },
282 };
283
284 static QemuOptsList qemu_trace_opts = {
285 .name = "trace",
286 .implied_opt_name = "trace",
287 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
288 .desc = {
289 {
290 .name = "events",
291 .type = QEMU_OPT_STRING,
292 },{
293 .name = "file",
294 .type = QEMU_OPT_STRING,
295 },
296 { /* end of list */ }
297 },
298 };
299
300 static QemuOptsList qemu_option_rom_opts = {
301 .name = "option-rom",
302 .implied_opt_name = "romfile",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
304 .desc = {
305 {
306 .name = "bootindex",
307 .type = QEMU_OPT_NUMBER,
308 }, {
309 .name = "romfile",
310 .type = QEMU_OPT_STRING,
311 },
312 { /* end of list */ }
313 },
314 };
315
316 static QemuOptsList qemu_machine_opts = {
317 .name = "machine",
318 .implied_opt_name = "type",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
321 .desc = {
322 {
323 .name = "type",
324 .type = QEMU_OPT_STRING,
325 .help = "emulated machine"
326 }, {
327 .name = "accel",
328 .type = QEMU_OPT_STRING,
329 .help = "accelerator list",
330 }, {
331 .name = "kernel_irqchip",
332 .type = QEMU_OPT_BOOL,
333 .help = "use KVM in-kernel irqchip",
334 }, {
335 .name = "kvm_shadow_mem",
336 .type = QEMU_OPT_SIZE,
337 .help = "KVM shadow MMU size",
338 }, {
339 .name = "kernel",
340 .type = QEMU_OPT_STRING,
341 .help = "Linux kernel image file",
342 }, {
343 .name = "initrd",
344 .type = QEMU_OPT_STRING,
345 .help = "Linux initial ramdisk file",
346 }, {
347 .name = "append",
348 .type = QEMU_OPT_STRING,
349 .help = "Linux kernel command line",
350 }, {
351 .name = "dtb",
352 .type = QEMU_OPT_STRING,
353 .help = "Linux kernel device tree file",
354 }, {
355 .name = "dumpdtb",
356 .type = QEMU_OPT_STRING,
357 .help = "Dump current dtb to a file and quit",
358 }, {
359 .name = "phandle_start",
360 .type = QEMU_OPT_NUMBER,
361 .help = "The first phandle ID we may generate dynamically",
362 }, {
363 .name = "dt_compatible",
364 .type = QEMU_OPT_STRING,
365 .help = "Overrides the \"compatible\" property of the dt root node",
366 }, {
367 .name = "dump-guest-core",
368 .type = QEMU_OPT_BOOL,
369 .help = "Include guest memory in a core dump",
370 }, {
371 .name = "mem-merge",
372 .type = QEMU_OPT_BOOL,
373 .help = "enable/disable memory merge support",
374 },{
375 .name = "usb",
376 .type = QEMU_OPT_BOOL,
377 .help = "Set on/off to enable/disable usb",
378 },{
379 .name = "firmware",
380 .type = QEMU_OPT_STRING,
381 .help = "firmware image",
382 },{
383 .name = "kvm-type",
384 .type = QEMU_OPT_STRING,
385 .help = "Specifies the KVM virtualization mode (HV, PR)",
386 },
387 { /* End of list */ }
388 },
389 };
390
391 static QemuOptsList qemu_boot_opts = {
392 .name = "boot-opts",
393 .implied_opt_name = "order",
394 .merge_lists = true,
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
396 .desc = {
397 {
398 .name = "order",
399 .type = QEMU_OPT_STRING,
400 }, {
401 .name = "once",
402 .type = QEMU_OPT_STRING,
403 }, {
404 .name = "menu",
405 .type = QEMU_OPT_BOOL,
406 }, {
407 .name = "splash",
408 .type = QEMU_OPT_STRING,
409 }, {
410 .name = "splash-time",
411 .type = QEMU_OPT_STRING,
412 }, {
413 .name = "reboot-timeout",
414 .type = QEMU_OPT_STRING,
415 }, {
416 .name = "strict",
417 .type = QEMU_OPT_BOOL,
418 },
419 { /*End of list */ }
420 },
421 };
422
423 static QemuOptsList qemu_add_fd_opts = {
424 .name = "add-fd",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
426 .desc = {
427 {
428 .name = "fd",
429 .type = QEMU_OPT_NUMBER,
430 .help = "file descriptor of which a duplicate is added to fd set",
431 },{
432 .name = "set",
433 .type = QEMU_OPT_NUMBER,
434 .help = "ID of the fd set to add fd to",
435 },{
436 .name = "opaque",
437 .type = QEMU_OPT_STRING,
438 .help = "free-form string used to describe fd",
439 },
440 { /* end of list */ }
441 },
442 };
443
444 static QemuOptsList qemu_object_opts = {
445 .name = "object",
446 .implied_opt_name = "qom-type",
447 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
448 .desc = {
449 { }
450 },
451 };
452
453 static QemuOptsList qemu_tpmdev_opts = {
454 .name = "tpmdev",
455 .implied_opt_name = "type",
456 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
457 .desc = {
458 /* options are defined in the TPM backends */
459 { /* end of list */ }
460 },
461 };
462
463 static QemuOptsList qemu_realtime_opts = {
464 .name = "realtime",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
466 .desc = {
467 {
468 .name = "mlock",
469 .type = QEMU_OPT_BOOL,
470 },
471 { /* end of list */ }
472 },
473 };
474
475 static QemuOptsList qemu_msg_opts = {
476 .name = "msg",
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
478 .desc = {
479 {
480 .name = "timestamp",
481 .type = QEMU_OPT_BOOL,
482 },
483 { /* end of list */ }
484 },
485 };
486
487 static QemuOptsList qemu_name_opts = {
488 .name = "name",
489 .implied_opt_name = "guest",
490 .merge_lists = true,
491 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
492 .desc = {
493 {
494 .name = "guest",
495 .type = QEMU_OPT_STRING,
496 .help = "Sets the name of the guest.\n"
497 "This name will be displayed in the SDL window caption.\n"
498 "The name will also be used for the VNC server",
499 }, {
500 .name = "process",
501 .type = QEMU_OPT_STRING,
502 .help = "Sets the name of the QEMU process, as shown in top etc",
503 }, {
504 .name = "debug-threads",
505 .type = QEMU_OPT_BOOL,
506 .help = "When enabled, name the individual threads; defaults off.\n"
507 "NOTE: The thread names are for debugging and not a\n"
508 "stable API.",
509 },
510 { /* End of list */ }
511 },
512 };
513
514 static QemuOptsList qemu_mem_opts = {
515 .name = "memory",
516 .implied_opt_name = "size",
517 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
518 .merge_lists = true,
519 .desc = {
520 {
521 .name = "size",
522 .type = QEMU_OPT_SIZE,
523 },
524 {
525 .name = "slots",
526 .type = QEMU_OPT_NUMBER,
527 },
528 {
529 .name = "maxmem",
530 .type = QEMU_OPT_SIZE,
531 },
532 { /* end of list */ }
533 },
534 };
535
536 /**
537 * Get machine options
538 *
539 * Returns: machine options (never null).
540 */
541 QemuOpts *qemu_get_machine_opts(void)
542 {
543 return qemu_find_opts_singleton("machine");
544 }
545
546 const char *qemu_get_vm_name(void)
547 {
548 return qemu_name;
549 }
550
551 static void res_free(void)
552 {
553 if (boot_splash_filedata != NULL) {
554 g_free(boot_splash_filedata);
555 boot_splash_filedata = NULL;
556 }
557 }
558
559 static int default_driver_check(QemuOpts *opts, void *opaque)
560 {
561 const char *driver = qemu_opt_get(opts, "driver");
562 int i;
563
564 if (!driver)
565 return 0;
566 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
567 if (strcmp(default_list[i].driver, driver) != 0)
568 continue;
569 *(default_list[i].flag) = 0;
570 }
571 return 0;
572 }
573
574 /***********************************************************/
575 /* QEMU state */
576
577 static RunState current_run_state = RUN_STATE_PRELAUNCH;
578
579 /* We use RUN_STATE_MAX but any invalid value will do */
580 static RunState vmstop_requested = RUN_STATE_MAX;
581 static QemuMutex vmstop_lock;
582
583 typedef struct {
584 RunState from;
585 RunState to;
586 } RunStateTransition;
587
588 static const RunStateTransition runstate_transitions_def[] = {
589 /* from -> to */
590 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
591 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
592
593 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
594 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
595
596 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
597 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
598
599 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
600 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
601
602 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
603 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
604
605 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
606 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
607
608 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
609 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
611
612 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
613 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
614
615 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
616
617 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
618 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
619 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
620 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
621 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
623 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
624 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
625 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
626 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
627
628 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
629
630 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
631 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
632
633 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
634 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
635 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
636 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
637
638 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
639 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
640
641 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
642 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
643
644 { RUN_STATE_MAX, RUN_STATE_MAX },
645 };
646
647 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
648
649 bool runstate_check(RunState state)
650 {
651 return current_run_state == state;
652 }
653
654 static void runstate_init(void)
655 {
656 const RunStateTransition *p;
657
658 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
659 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
660 runstate_valid_transitions[p->from][p->to] = true;
661 }
662
663 qemu_mutex_init(&vmstop_lock);
664 }
665
666 /* This function will abort() on invalid state transitions */
667 void runstate_set(RunState new_state)
668 {
669 assert(new_state < RUN_STATE_MAX);
670
671 if (!runstate_valid_transitions[current_run_state][new_state]) {
672 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
673 RunState_lookup[current_run_state],
674 RunState_lookup[new_state]);
675 abort();
676 }
677 trace_runstate_set(new_state);
678 current_run_state = new_state;
679 }
680
681 int runstate_is_running(void)
682 {
683 return runstate_check(RUN_STATE_RUNNING);
684 }
685
686 bool runstate_needs_reset(void)
687 {
688 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
689 runstate_check(RUN_STATE_SHUTDOWN);
690 }
691
692 StatusInfo *qmp_query_status(Error **errp)
693 {
694 StatusInfo *info = g_malloc0(sizeof(*info));
695
696 info->running = runstate_is_running();
697 info->singlestep = singlestep;
698 info->status = current_run_state;
699
700 return info;
701 }
702
703 static bool qemu_vmstop_requested(RunState *r)
704 {
705 qemu_mutex_lock(&vmstop_lock);
706 *r = vmstop_requested;
707 vmstop_requested = RUN_STATE_MAX;
708 qemu_mutex_unlock(&vmstop_lock);
709 return *r < RUN_STATE_MAX;
710 }
711
712 void qemu_system_vmstop_request_prepare(void)
713 {
714 qemu_mutex_lock(&vmstop_lock);
715 }
716
717 void qemu_system_vmstop_request(RunState state)
718 {
719 vmstop_requested = state;
720 qemu_mutex_unlock(&vmstop_lock);
721 qemu_notify_event();
722 }
723
724 void vm_start(void)
725 {
726 RunState requested;
727
728 qemu_vmstop_requested(&requested);
729 if (runstate_is_running() && requested == RUN_STATE_MAX) {
730 return;
731 }
732
733 /* Ensure that a STOP/RESUME pair of events is emitted if a
734 * vmstop request was pending. The BLOCK_IO_ERROR event, for
735 * example, according to documentation is always followed by
736 * the STOP event.
737 */
738 if (runstate_is_running()) {
739 qapi_event_send_stop(&error_abort);
740 } else {
741 cpu_enable_ticks();
742 runstate_set(RUN_STATE_RUNNING);
743 vm_state_notify(1, RUN_STATE_RUNNING);
744 resume_all_vcpus();
745 }
746
747 qapi_event_send_resume(&error_abort);
748 }
749
750
751 /***********************************************************/
752 /* real time host monotonic timer */
753
754 /***********************************************************/
755 /* host time/date access */
756 void qemu_get_timedate(struct tm *tm, int offset)
757 {
758 time_t ti;
759
760 time(&ti);
761 ti += offset;
762 if (rtc_date_offset == -1) {
763 if (rtc_utc)
764 gmtime_r(&ti, tm);
765 else
766 localtime_r(&ti, tm);
767 } else {
768 ti -= rtc_date_offset;
769 gmtime_r(&ti, tm);
770 }
771 }
772
773 int qemu_timedate_diff(struct tm *tm)
774 {
775 time_t seconds;
776
777 if (rtc_date_offset == -1)
778 if (rtc_utc)
779 seconds = mktimegm(tm);
780 else {
781 struct tm tmp = *tm;
782 tmp.tm_isdst = -1; /* use timezone to figure it out */
783 seconds = mktime(&tmp);
784 }
785 else
786 seconds = mktimegm(tm) + rtc_date_offset;
787
788 return seconds - time(NULL);
789 }
790
791 void rtc_change_mon_event(struct tm *tm)
792 {
793 QObject *data;
794
795 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
796 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
797 qobject_decref(data);
798 }
799
800 static void configure_rtc_date_offset(const char *startdate, int legacy)
801 {
802 time_t rtc_start_date;
803 struct tm tm;
804
805 if (!strcmp(startdate, "now") && legacy) {
806 rtc_date_offset = -1;
807 } else {
808 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
809 &tm.tm_year,
810 &tm.tm_mon,
811 &tm.tm_mday,
812 &tm.tm_hour,
813 &tm.tm_min,
814 &tm.tm_sec) == 6) {
815 /* OK */
816 } else if (sscanf(startdate, "%d-%d-%d",
817 &tm.tm_year,
818 &tm.tm_mon,
819 &tm.tm_mday) == 3) {
820 tm.tm_hour = 0;
821 tm.tm_min = 0;
822 tm.tm_sec = 0;
823 } else {
824 goto date_fail;
825 }
826 tm.tm_year -= 1900;
827 tm.tm_mon--;
828 rtc_start_date = mktimegm(&tm);
829 if (rtc_start_date == -1) {
830 date_fail:
831 fprintf(stderr, "Invalid date format. Valid formats are:\n"
832 "'2006-06-17T16:01:21' or '2006-06-17'\n");
833 exit(1);
834 }
835 rtc_date_offset = time(NULL) - rtc_start_date;
836 }
837 }
838
839 static void configure_rtc(QemuOpts *opts)
840 {
841 const char *value;
842
843 value = qemu_opt_get(opts, "base");
844 if (value) {
845 if (!strcmp(value, "utc")) {
846 rtc_utc = 1;
847 } else if (!strcmp(value, "localtime")) {
848 rtc_utc = 0;
849 } else {
850 configure_rtc_date_offset(value, 0);
851 }
852 }
853 value = qemu_opt_get(opts, "clock");
854 if (value) {
855 if (!strcmp(value, "host")) {
856 rtc_clock = QEMU_CLOCK_HOST;
857 } else if (!strcmp(value, "rt")) {
858 rtc_clock = QEMU_CLOCK_REALTIME;
859 } else if (!strcmp(value, "vm")) {
860 rtc_clock = QEMU_CLOCK_VIRTUAL;
861 } else {
862 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
863 exit(1);
864 }
865 }
866 value = qemu_opt_get(opts, "driftfix");
867 if (value) {
868 if (!strcmp(value, "slew")) {
869 static GlobalProperty slew_lost_ticks[] = {
870 {
871 .driver = "mc146818rtc",
872 .property = "lost_tick_policy",
873 .value = "slew",
874 },
875 { /* end of list */ }
876 };
877
878 qdev_prop_register_global_list(slew_lost_ticks);
879 } else if (!strcmp(value, "none")) {
880 /* discard is default */
881 } else {
882 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
883 exit(1);
884 }
885 }
886 }
887
888 /***********************************************************/
889 /* Bluetooth support */
890 static int nb_hcis;
891 static int cur_hci;
892 static struct HCIInfo *hci_table[MAX_NICS];
893
894 struct HCIInfo *qemu_next_hci(void)
895 {
896 if (cur_hci == nb_hcis)
897 return &null_hci;
898
899 return hci_table[cur_hci++];
900 }
901
902 static int bt_hci_parse(const char *str)
903 {
904 struct HCIInfo *hci;
905 bdaddr_t bdaddr;
906
907 if (nb_hcis >= MAX_NICS) {
908 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
909 return -1;
910 }
911
912 hci = hci_init(str);
913 if (!hci)
914 return -1;
915
916 bdaddr.b[0] = 0x52;
917 bdaddr.b[1] = 0x54;
918 bdaddr.b[2] = 0x00;
919 bdaddr.b[3] = 0x12;
920 bdaddr.b[4] = 0x34;
921 bdaddr.b[5] = 0x56 + nb_hcis;
922 hci->bdaddr_set(hci, bdaddr.b);
923
924 hci_table[nb_hcis++] = hci;
925
926 return 0;
927 }
928
929 static void bt_vhci_add(int vlan_id)
930 {
931 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
932
933 if (!vlan->slave)
934 fprintf(stderr, "qemu: warning: adding a VHCI to "
935 "an empty scatternet %i\n", vlan_id);
936
937 bt_vhci_init(bt_new_hci(vlan));
938 }
939
940 static struct bt_device_s *bt_device_add(const char *opt)
941 {
942 struct bt_scatternet_s *vlan;
943 int vlan_id = 0;
944 char *endp = strstr(opt, ",vlan=");
945 int len = (endp ? endp - opt : strlen(opt)) + 1;
946 char devname[10];
947
948 pstrcpy(devname, MIN(sizeof(devname), len), opt);
949
950 if (endp) {
951 vlan_id = strtol(endp + 6, &endp, 0);
952 if (*endp) {
953 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
954 return 0;
955 }
956 }
957
958 vlan = qemu_find_bt_vlan(vlan_id);
959
960 if (!vlan->slave)
961 fprintf(stderr, "qemu: warning: adding a slave device to "
962 "an empty scatternet %i\n", vlan_id);
963
964 if (!strcmp(devname, "keyboard"))
965 return bt_keyboard_init(vlan);
966
967 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
968 return 0;
969 }
970
971 static int bt_parse(const char *opt)
972 {
973 const char *endp, *p;
974 int vlan;
975
976 if (strstart(opt, "hci", &endp)) {
977 if (!*endp || *endp == ',') {
978 if (*endp)
979 if (!strstart(endp, ",vlan=", 0))
980 opt = endp + 1;
981
982 return bt_hci_parse(opt);
983 }
984 } else if (strstart(opt, "vhci", &endp)) {
985 if (!*endp || *endp == ',') {
986 if (*endp) {
987 if (strstart(endp, ",vlan=", &p)) {
988 vlan = strtol(p, (char **) &endp, 0);
989 if (*endp) {
990 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
991 return 1;
992 }
993 } else {
994 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
995 return 1;
996 }
997 } else
998 vlan = 0;
999
1000 bt_vhci_add(vlan);
1001 return 0;
1002 }
1003 } else if (strstart(opt, "device:", &endp))
1004 return !bt_device_add(endp);
1005
1006 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1007 return 1;
1008 }
1009
1010 static int parse_sandbox(QemuOpts *opts, void *opaque)
1011 {
1012 /* FIXME: change this to true for 1.3 */
1013 if (qemu_opt_get_bool(opts, "enable", false)) {
1014 #ifdef CONFIG_SECCOMP
1015 if (seccomp_start() < 0) {
1016 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1017 "failed to install seccomp syscall filter in the kernel");
1018 return -1;
1019 }
1020 #else
1021 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1022 "sandboxing request but seccomp is not compiled into this build");
1023 return -1;
1024 #endif
1025 }
1026
1027 return 0;
1028 }
1029
1030 static int parse_name(QemuOpts *opts, void *opaque)
1031 {
1032 const char *proc_name;
1033
1034 if (qemu_opt_get(opts, "debug-threads")) {
1035 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1036 }
1037 qemu_name = qemu_opt_get(opts, "guest");
1038
1039 proc_name = qemu_opt_get(opts, "process");
1040 if (proc_name) {
1041 os_set_proc_name(proc_name);
1042 }
1043
1044 return 0;
1045 }
1046
1047 bool usb_enabled(bool default_usb)
1048 {
1049 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1050 has_defaults && default_usb);
1051 }
1052
1053 #ifndef _WIN32
1054 static int parse_add_fd(QemuOpts *opts, void *opaque)
1055 {
1056 int fd, dupfd, flags;
1057 int64_t fdset_id;
1058 const char *fd_opaque = NULL;
1059
1060 fd = qemu_opt_get_number(opts, "fd", -1);
1061 fdset_id = qemu_opt_get_number(opts, "set", -1);
1062 fd_opaque = qemu_opt_get(opts, "opaque");
1063
1064 if (fd < 0) {
1065 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1066 "fd option is required and must be non-negative");
1067 return -1;
1068 }
1069
1070 if (fd <= STDERR_FILENO) {
1071 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1072 "fd cannot be a standard I/O stream");
1073 return -1;
1074 }
1075
1076 /*
1077 * All fds inherited across exec() necessarily have FD_CLOEXEC
1078 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1079 */
1080 flags = fcntl(fd, F_GETFD);
1081 if (flags == -1 || (flags & FD_CLOEXEC)) {
1082 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1083 "fd is not valid or already in use");
1084 return -1;
1085 }
1086
1087 if (fdset_id < 0) {
1088 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1089 "set option is required and must be non-negative");
1090 return -1;
1091 }
1092
1093 #ifdef F_DUPFD_CLOEXEC
1094 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1095 #else
1096 dupfd = dup(fd);
1097 if (dupfd != -1) {
1098 qemu_set_cloexec(dupfd);
1099 }
1100 #endif
1101 if (dupfd == -1) {
1102 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1103 "Error duplicating fd: %s", strerror(errno));
1104 return -1;
1105 }
1106
1107 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1108 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1109 fd_opaque, NULL);
1110
1111 return 0;
1112 }
1113
1114 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1115 {
1116 int fd;
1117
1118 fd = qemu_opt_get_number(opts, "fd", -1);
1119 close(fd);
1120
1121 return 0;
1122 }
1123 #endif
1124
1125 /***********************************************************/
1126 /* QEMU Block devices */
1127
1128 #define HD_OPTS "media=disk"
1129 #define CDROM_OPTS "media=cdrom"
1130 #define FD_OPTS ""
1131 #define PFLASH_OPTS ""
1132 #define MTD_OPTS ""
1133 #define SD_OPTS ""
1134
1135 static int drive_init_func(QemuOpts *opts, void *opaque)
1136 {
1137 BlockInterfaceType *block_default_type = opaque;
1138
1139 return drive_new(opts, *block_default_type) == NULL;
1140 }
1141
1142 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1143 {
1144 if (NULL == qemu_opt_get(opts, "snapshot")) {
1145 qemu_opt_set(opts, "snapshot", "on");
1146 }
1147 return 0;
1148 }
1149
1150 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1151 int index, const char *optstr)
1152 {
1153 QemuOpts *opts;
1154
1155 if (!enable || drive_get_by_index(type, index)) {
1156 return;
1157 }
1158
1159 opts = drive_add(type, index, NULL, optstr);
1160 if (snapshot) {
1161 drive_enable_snapshot(opts, NULL);
1162 }
1163 if (!drive_new(opts, type)) {
1164 exit(1);
1165 }
1166 }
1167
1168 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1169 {
1170 boot_set_handler = func;
1171 boot_set_opaque = opaque;
1172 }
1173
1174 int qemu_boot_set(const char *boot_order)
1175 {
1176 if (!boot_set_handler) {
1177 return -EINVAL;
1178 }
1179 return boot_set_handler(boot_set_opaque, boot_order);
1180 }
1181
1182 static void validate_bootdevices(const char *devices)
1183 {
1184 /* We just do some generic consistency checks */
1185 const char *p;
1186 int bitmap = 0;
1187
1188 for (p = devices; *p != '\0'; p++) {
1189 /* Allowed boot devices are:
1190 * a-b: floppy disk drives
1191 * c-f: IDE disk drives
1192 * g-m: machine implementation dependent drives
1193 * n-p: network devices
1194 * It's up to each machine implementation to check if the given boot
1195 * devices match the actual hardware implementation and firmware
1196 * features.
1197 */
1198 if (*p < 'a' || *p > 'p') {
1199 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1200 exit(1);
1201 }
1202 if (bitmap & (1 << (*p - 'a'))) {
1203 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1204 exit(1);
1205 }
1206 bitmap |= 1 << (*p - 'a');
1207 }
1208 }
1209
1210 static void restore_boot_order(void *opaque)
1211 {
1212 char *normal_boot_order = opaque;
1213 static int first = 1;
1214
1215 /* Restore boot order and remove ourselves after the first boot */
1216 if (first) {
1217 first = 0;
1218 return;
1219 }
1220
1221 qemu_boot_set(normal_boot_order);
1222
1223 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1224 g_free(normal_boot_order);
1225 }
1226
1227 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1228 const char *suffix)
1229 {
1230 FWBootEntry *node, *i;
1231
1232 if (bootindex < 0) {
1233 return;
1234 }
1235
1236 assert(dev != NULL || suffix != NULL);
1237
1238 node = g_malloc0(sizeof(FWBootEntry));
1239 node->bootindex = bootindex;
1240 node->suffix = g_strdup(suffix);
1241 node->dev = dev;
1242
1243 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1244 if (i->bootindex == bootindex) {
1245 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1246 exit(1);
1247 } else if (i->bootindex < bootindex) {
1248 continue;
1249 }
1250 QTAILQ_INSERT_BEFORE(i, node, link);
1251 return;
1252 }
1253 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1254 }
1255
1256 DeviceState *get_boot_device(uint32_t position)
1257 {
1258 uint32_t counter = 0;
1259 FWBootEntry *i = NULL;
1260 DeviceState *res = NULL;
1261
1262 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1263 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1264 if (counter == position) {
1265 res = i->dev;
1266 break;
1267 }
1268 counter++;
1269 }
1270 }
1271 return res;
1272 }
1273
1274 /*
1275 * This function returns null terminated string that consist of new line
1276 * separated device paths.
1277 *
1278 * memory pointed by "size" is assigned total length of the array in bytes
1279 *
1280 */
1281 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1282 {
1283 FWBootEntry *i;
1284 size_t total = 0;
1285 char *list = NULL;
1286
1287 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1288 char *devpath = NULL, *bootpath;
1289 size_t len;
1290
1291 if (i->dev) {
1292 devpath = qdev_get_fw_dev_path(i->dev);
1293 assert(devpath);
1294 }
1295
1296 if (i->suffix && !ignore_suffixes && devpath) {
1297 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1298
1299 bootpath = g_malloc(bootpathlen);
1300 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1301 g_free(devpath);
1302 } else if (devpath) {
1303 bootpath = devpath;
1304 } else if (!ignore_suffixes) {
1305 assert(i->suffix);
1306 bootpath = g_strdup(i->suffix);
1307 } else {
1308 bootpath = g_strdup("");
1309 }
1310
1311 if (total) {
1312 list[total-1] = '\n';
1313 }
1314 len = strlen(bootpath) + 1;
1315 list = g_realloc(list, total + len);
1316 memcpy(&list[total], bootpath, len);
1317 total += len;
1318 g_free(bootpath);
1319 }
1320
1321 *size = total;
1322
1323 if (boot_strict && *size > 0) {
1324 list[total-1] = '\n';
1325 list = g_realloc(list, total + 5);
1326 memcpy(&list[total], "HALT", 5);
1327 *size = total + 5;
1328 }
1329 return list;
1330 }
1331
1332 static QemuOptsList qemu_smp_opts = {
1333 .name = "smp-opts",
1334 .implied_opt_name = "cpus",
1335 .merge_lists = true,
1336 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1337 .desc = {
1338 {
1339 .name = "cpus",
1340 .type = QEMU_OPT_NUMBER,
1341 }, {
1342 .name = "sockets",
1343 .type = QEMU_OPT_NUMBER,
1344 }, {
1345 .name = "cores",
1346 .type = QEMU_OPT_NUMBER,
1347 }, {
1348 .name = "threads",
1349 .type = QEMU_OPT_NUMBER,
1350 }, {
1351 .name = "maxcpus",
1352 .type = QEMU_OPT_NUMBER,
1353 },
1354 { /*End of list */ }
1355 },
1356 };
1357
1358 static void smp_parse(QemuOpts *opts)
1359 {
1360 if (opts) {
1361
1362 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1363 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1364 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1365 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1366
1367 /* compute missing values, prefer sockets over cores over threads */
1368 if (cpus == 0 || sockets == 0) {
1369 sockets = sockets > 0 ? sockets : 1;
1370 cores = cores > 0 ? cores : 1;
1371 threads = threads > 0 ? threads : 1;
1372 if (cpus == 0) {
1373 cpus = cores * threads * sockets;
1374 }
1375 } else {
1376 if (cores == 0) {
1377 threads = threads > 0 ? threads : 1;
1378 cores = cpus / (sockets * threads);
1379 } else {
1380 threads = cpus / (cores * sockets);
1381 }
1382 }
1383
1384 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1385
1386 smp_cpus = cpus;
1387 smp_cores = cores > 0 ? cores : 1;
1388 smp_threads = threads > 0 ? threads : 1;
1389
1390 }
1391
1392 if (max_cpus == 0) {
1393 max_cpus = smp_cpus;
1394 }
1395
1396 if (max_cpus > MAX_CPUMASK_BITS) {
1397 fprintf(stderr, "Unsupported number of maxcpus\n");
1398 exit(1);
1399 }
1400 if (max_cpus < smp_cpus) {
1401 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1402 exit(1);
1403 }
1404
1405 }
1406
1407 static void configure_realtime(QemuOpts *opts)
1408 {
1409 bool enable_mlock;
1410
1411 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1412
1413 if (enable_mlock) {
1414 if (os_mlock() < 0) {
1415 fprintf(stderr, "qemu: locking memory failed\n");
1416 exit(1);
1417 }
1418 }
1419 }
1420
1421
1422 static void configure_msg(QemuOpts *opts)
1423 {
1424 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1425 }
1426
1427 /***********************************************************/
1428 /* USB devices */
1429
1430 static int usb_device_add(const char *devname)
1431 {
1432 USBDevice *dev = NULL;
1433 #ifndef CONFIG_LINUX
1434 const char *p;
1435 #endif
1436
1437 if (!usb_enabled(false)) {
1438 return -1;
1439 }
1440
1441 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1442 dev = usbdevice_create(devname);
1443 if (dev)
1444 goto done;
1445
1446 /* the other ones */
1447 #ifndef CONFIG_LINUX
1448 /* only the linux version is qdev-ified, usb-bsd still needs this */
1449 if (strstart(devname, "host:", &p)) {
1450 dev = usb_host_device_open(usb_bus_find(-1), p);
1451 }
1452 #endif
1453 if (!dev)
1454 return -1;
1455
1456 done:
1457 return 0;
1458 }
1459
1460 static int usb_device_del(const char *devname)
1461 {
1462 int bus_num, addr;
1463 const char *p;
1464
1465 if (strstart(devname, "host:", &p)) {
1466 return -1;
1467 }
1468
1469 if (!usb_enabled(false)) {
1470 return -1;
1471 }
1472
1473 p = strchr(devname, '.');
1474 if (!p)
1475 return -1;
1476 bus_num = strtoul(devname, NULL, 0);
1477 addr = strtoul(p + 1, NULL, 0);
1478
1479 return usb_device_delete_addr(bus_num, addr);
1480 }
1481
1482 static int usb_parse(const char *cmdline)
1483 {
1484 int r;
1485 r = usb_device_add(cmdline);
1486 if (r < 0) {
1487 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1488 }
1489 return r;
1490 }
1491
1492 void do_usb_add(Monitor *mon, const QDict *qdict)
1493 {
1494 const char *devname = qdict_get_str(qdict, "devname");
1495 if (usb_device_add(devname) < 0) {
1496 error_report("could not add USB device '%s'", devname);
1497 }
1498 }
1499
1500 void do_usb_del(Monitor *mon, const QDict *qdict)
1501 {
1502 const char *devname = qdict_get_str(qdict, "devname");
1503 if (usb_device_del(devname) < 0) {
1504 error_report("could not delete USB device '%s'", devname);
1505 }
1506 }
1507
1508 /***********************************************************/
1509 /* PCMCIA/Cardbus */
1510
1511 static struct pcmcia_socket_entry_s {
1512 PCMCIASocket *socket;
1513 struct pcmcia_socket_entry_s *next;
1514 } *pcmcia_sockets = 0;
1515
1516 void pcmcia_socket_register(PCMCIASocket *socket)
1517 {
1518 struct pcmcia_socket_entry_s *entry;
1519
1520 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1521 entry->socket = socket;
1522 entry->next = pcmcia_sockets;
1523 pcmcia_sockets = entry;
1524 }
1525
1526 void pcmcia_socket_unregister(PCMCIASocket *socket)
1527 {
1528 struct pcmcia_socket_entry_s *entry, **ptr;
1529
1530 ptr = &pcmcia_sockets;
1531 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1532 if (entry->socket == socket) {
1533 *ptr = entry->next;
1534 g_free(entry);
1535 }
1536 }
1537
1538 void pcmcia_info(Monitor *mon, const QDict *qdict)
1539 {
1540 struct pcmcia_socket_entry_s *iter;
1541
1542 if (!pcmcia_sockets)
1543 monitor_printf(mon, "No PCMCIA sockets\n");
1544
1545 for (iter = pcmcia_sockets; iter; iter = iter->next)
1546 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1547 iter->socket->attached ? iter->socket->card_string :
1548 "Empty");
1549 }
1550
1551 /***********************************************************/
1552 /* machine registration */
1553
1554 MachineState *current_machine;
1555
1556 static void machine_class_init(ObjectClass *oc, void *data)
1557 {
1558 MachineClass *mc = MACHINE_CLASS(oc);
1559 QEMUMachine *qm = data;
1560
1561 mc->name = qm->name;
1562 mc->alias = qm->alias;
1563 mc->desc = qm->desc;
1564 mc->init = qm->init;
1565 mc->reset = qm->reset;
1566 mc->hot_add_cpu = qm->hot_add_cpu;
1567 mc->kvm_type = qm->kvm_type;
1568 mc->block_default_type = qm->block_default_type;
1569 mc->max_cpus = qm->max_cpus;
1570 mc->no_serial = qm->no_serial;
1571 mc->no_parallel = qm->no_parallel;
1572 mc->use_virtcon = qm->use_virtcon;
1573 mc->use_sclp = qm->use_sclp;
1574 mc->no_floppy = qm->no_floppy;
1575 mc->no_cdrom = qm->no_cdrom;
1576 mc->no_sdcard = qm->no_sdcard;
1577 mc->is_default = qm->is_default;
1578 mc->default_machine_opts = qm->default_machine_opts;
1579 mc->default_boot_order = qm->default_boot_order;
1580 mc->compat_props = qm->compat_props;
1581 mc->hw_version = qm->hw_version;
1582 }
1583
1584 int qemu_register_machine(QEMUMachine *m)
1585 {
1586 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1587 TypeInfo ti = {
1588 .name = name,
1589 .parent = TYPE_MACHINE,
1590 .class_init = machine_class_init,
1591 .class_data = (void *)m,
1592 };
1593
1594 type_register(&ti);
1595 g_free(name);
1596
1597 return 0;
1598 }
1599
1600 static MachineClass *find_machine(const char *name)
1601 {
1602 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1603 MachineClass *mc = NULL;
1604
1605 for (el = machines; el; el = el->next) {
1606 MachineClass *temp = el->data;
1607
1608 if (!strcmp(temp->name, name)) {
1609 mc = temp;
1610 break;
1611 }
1612 if (temp->alias &&
1613 !strcmp(temp->alias, name)) {
1614 mc = temp;
1615 break;
1616 }
1617 }
1618
1619 g_slist_free(machines);
1620 return mc;
1621 }
1622
1623 MachineClass *find_default_machine(void)
1624 {
1625 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1626 MachineClass *mc = NULL;
1627
1628 for (el = machines; el; el = el->next) {
1629 MachineClass *temp = el->data;
1630
1631 if (temp->is_default) {
1632 mc = temp;
1633 break;
1634 }
1635 }
1636
1637 g_slist_free(machines);
1638 return mc;
1639 }
1640
1641 MachineInfoList *qmp_query_machines(Error **errp)
1642 {
1643 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1644 MachineInfoList *mach_list = NULL;
1645
1646 for (el = machines; el; el = el->next) {
1647 MachineClass *mc = el->data;
1648 MachineInfoList *entry;
1649 MachineInfo *info;
1650
1651 info = g_malloc0(sizeof(*info));
1652 if (mc->is_default) {
1653 info->has_is_default = true;
1654 info->is_default = true;
1655 }
1656
1657 if (mc->alias) {
1658 info->has_alias = true;
1659 info->alias = g_strdup(mc->alias);
1660 }
1661
1662 info->name = g_strdup(mc->name);
1663 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1664
1665 entry = g_malloc0(sizeof(*entry));
1666 entry->value = info;
1667 entry->next = mach_list;
1668 mach_list = entry;
1669 }
1670
1671 g_slist_free(machines);
1672 return mach_list;
1673 }
1674
1675 /***********************************************************/
1676 /* main execution loop */
1677
1678 struct vm_change_state_entry {
1679 VMChangeStateHandler *cb;
1680 void *opaque;
1681 QLIST_ENTRY (vm_change_state_entry) entries;
1682 };
1683
1684 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1685
1686 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1687 void *opaque)
1688 {
1689 VMChangeStateEntry *e;
1690
1691 e = g_malloc0(sizeof (*e));
1692
1693 e->cb = cb;
1694 e->opaque = opaque;
1695 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1696 return e;
1697 }
1698
1699 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1700 {
1701 QLIST_REMOVE (e, entries);
1702 g_free (e);
1703 }
1704
1705 void vm_state_notify(int running, RunState state)
1706 {
1707 VMChangeStateEntry *e;
1708
1709 trace_vm_state_notify(running, state);
1710
1711 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1712 e->cb(e->opaque, running, state);
1713 }
1714 }
1715
1716 /* reset/shutdown handler */
1717
1718 typedef struct QEMUResetEntry {
1719 QTAILQ_ENTRY(QEMUResetEntry) entry;
1720 QEMUResetHandler *func;
1721 void *opaque;
1722 } QEMUResetEntry;
1723
1724 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1725 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1726 static int reset_requested;
1727 static int shutdown_requested, shutdown_signal = -1;
1728 static pid_t shutdown_pid;
1729 static int powerdown_requested;
1730 static int debug_requested;
1731 static int suspend_requested;
1732 static WakeupReason wakeup_reason;
1733 static NotifierList powerdown_notifiers =
1734 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1735 static NotifierList suspend_notifiers =
1736 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1737 static NotifierList wakeup_notifiers =
1738 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1739 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1740
1741 int qemu_shutdown_requested_get(void)
1742 {
1743 return shutdown_requested;
1744 }
1745
1746 int qemu_reset_requested_get(void)
1747 {
1748 return reset_requested;
1749 }
1750
1751 static int qemu_shutdown_requested(void)
1752 {
1753 int r = shutdown_requested;
1754 shutdown_requested = 0;
1755 return r;
1756 }
1757
1758 static void qemu_kill_report(void)
1759 {
1760 if (!qtest_driver() && shutdown_signal != -1) {
1761 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1762 if (shutdown_pid == 0) {
1763 /* This happens for eg ^C at the terminal, so it's worth
1764 * avoiding printing an odd message in that case.
1765 */
1766 fputc('\n', stderr);
1767 } else {
1768 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1769 }
1770 shutdown_signal = -1;
1771 }
1772 }
1773
1774 static int qemu_reset_requested(void)
1775 {
1776 int r = reset_requested;
1777 reset_requested = 0;
1778 return r;
1779 }
1780
1781 static int qemu_suspend_requested(void)
1782 {
1783 int r = suspend_requested;
1784 suspend_requested = 0;
1785 return r;
1786 }
1787
1788 static WakeupReason qemu_wakeup_requested(void)
1789 {
1790 return wakeup_reason;
1791 }
1792
1793 static int qemu_powerdown_requested(void)
1794 {
1795 int r = powerdown_requested;
1796 powerdown_requested = 0;
1797 return r;
1798 }
1799
1800 static int qemu_debug_requested(void)
1801 {
1802 int r = debug_requested;
1803 debug_requested = 0;
1804 return r;
1805 }
1806
1807 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1808 {
1809 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1810
1811 re->func = func;
1812 re->opaque = opaque;
1813 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1814 }
1815
1816 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1817 {
1818 QEMUResetEntry *re;
1819
1820 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1821 if (re->func == func && re->opaque == opaque) {
1822 QTAILQ_REMOVE(&reset_handlers, re, entry);
1823 g_free(re);
1824 return;
1825 }
1826 }
1827 }
1828
1829 void qemu_devices_reset(void)
1830 {
1831 QEMUResetEntry *re, *nre;
1832
1833 /* reset all devices */
1834 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1835 re->func(re->opaque);
1836 }
1837 }
1838
1839 void qemu_system_reset(bool report)
1840 {
1841 MachineClass *mc;
1842
1843 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1844
1845 if (mc && mc->reset) {
1846 mc->reset();
1847 } else {
1848 qemu_devices_reset();
1849 }
1850 if (report) {
1851 qapi_event_send_reset(&error_abort);
1852 }
1853 cpu_synchronize_all_post_reset();
1854 }
1855
1856 void qemu_system_reset_request(void)
1857 {
1858 if (no_reboot) {
1859 shutdown_requested = 1;
1860 } else {
1861 reset_requested = 1;
1862 }
1863 cpu_stop_current();
1864 qemu_notify_event();
1865 }
1866
1867 static void qemu_system_suspend(void)
1868 {
1869 pause_all_vcpus();
1870 notifier_list_notify(&suspend_notifiers, NULL);
1871 runstate_set(RUN_STATE_SUSPENDED);
1872 qapi_event_send_suspend(&error_abort);
1873 }
1874
1875 void qemu_system_suspend_request(void)
1876 {
1877 if (runstate_check(RUN_STATE_SUSPENDED)) {
1878 return;
1879 }
1880 suspend_requested = 1;
1881 cpu_stop_current();
1882 qemu_notify_event();
1883 }
1884
1885 void qemu_register_suspend_notifier(Notifier *notifier)
1886 {
1887 notifier_list_add(&suspend_notifiers, notifier);
1888 }
1889
1890 void qemu_system_wakeup_request(WakeupReason reason)
1891 {
1892 trace_system_wakeup_request(reason);
1893
1894 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1895 return;
1896 }
1897 if (!(wakeup_reason_mask & (1 << reason))) {
1898 return;
1899 }
1900 runstate_set(RUN_STATE_RUNNING);
1901 wakeup_reason = reason;
1902 qemu_notify_event();
1903 }
1904
1905 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1906 {
1907 if (enabled) {
1908 wakeup_reason_mask |= (1 << reason);
1909 } else {
1910 wakeup_reason_mask &= ~(1 << reason);
1911 }
1912 }
1913
1914 void qemu_register_wakeup_notifier(Notifier *notifier)
1915 {
1916 notifier_list_add(&wakeup_notifiers, notifier);
1917 }
1918
1919 void qemu_system_killed(int signal, pid_t pid)
1920 {
1921 shutdown_signal = signal;
1922 shutdown_pid = pid;
1923 no_shutdown = 0;
1924 qemu_system_shutdown_request();
1925 }
1926
1927 void qemu_system_shutdown_request(void)
1928 {
1929 shutdown_requested = 1;
1930 qemu_notify_event();
1931 }
1932
1933 static void qemu_system_powerdown(void)
1934 {
1935 qapi_event_send_powerdown(&error_abort);
1936 notifier_list_notify(&powerdown_notifiers, NULL);
1937 }
1938
1939 void qemu_system_powerdown_request(void)
1940 {
1941 powerdown_requested = 1;
1942 qemu_notify_event();
1943 }
1944
1945 void qemu_register_powerdown_notifier(Notifier *notifier)
1946 {
1947 notifier_list_add(&powerdown_notifiers, notifier);
1948 }
1949
1950 void qemu_system_debug_request(void)
1951 {
1952 debug_requested = 1;
1953 qemu_notify_event();
1954 }
1955
1956 static bool main_loop_should_exit(void)
1957 {
1958 RunState r;
1959 if (qemu_debug_requested()) {
1960 vm_stop(RUN_STATE_DEBUG);
1961 }
1962 if (qemu_suspend_requested()) {
1963 qemu_system_suspend();
1964 }
1965 if (qemu_shutdown_requested()) {
1966 qemu_kill_report();
1967 qapi_event_send_shutdown(&error_abort);
1968 if (no_shutdown) {
1969 vm_stop(RUN_STATE_SHUTDOWN);
1970 } else {
1971 return true;
1972 }
1973 }
1974 if (qemu_reset_requested()) {
1975 pause_all_vcpus();
1976 cpu_synchronize_all_states();
1977 qemu_system_reset(VMRESET_REPORT);
1978 resume_all_vcpus();
1979 if (runstate_needs_reset()) {
1980 runstate_set(RUN_STATE_PAUSED);
1981 }
1982 }
1983 if (qemu_wakeup_requested()) {
1984 pause_all_vcpus();
1985 cpu_synchronize_all_states();
1986 qemu_system_reset(VMRESET_SILENT);
1987 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1988 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1989 resume_all_vcpus();
1990 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1991 }
1992 if (qemu_powerdown_requested()) {
1993 qemu_system_powerdown();
1994 }
1995 if (qemu_vmstop_requested(&r)) {
1996 vm_stop(r);
1997 }
1998 return false;
1999 }
2000
2001 static void main_loop(void)
2002 {
2003 bool nonblocking;
2004 int last_io = 0;
2005 #ifdef CONFIG_PROFILER
2006 int64_t ti;
2007 #endif
2008 do {
2009 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2010 #ifdef CONFIG_PROFILER
2011 ti = profile_getclock();
2012 #endif
2013 last_io = main_loop_wait(nonblocking);
2014 #ifdef CONFIG_PROFILER
2015 dev_time += profile_getclock() - ti;
2016 #endif
2017 } while (!main_loop_should_exit());
2018 }
2019
2020 static void version(void)
2021 {
2022 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2023 }
2024
2025 static void help(int exitcode)
2026 {
2027 version();
2028 printf("usage: %s [options] [disk_image]\n\n"
2029 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2030 error_get_progname());
2031
2032 #define QEMU_OPTIONS_GENERATE_HELP
2033 #include "qemu-options-wrapper.h"
2034
2035 printf("\nDuring emulation, the following keys are useful:\n"
2036 "ctrl-alt-f toggle full screen\n"
2037 "ctrl-alt-n switch to virtual console 'n'\n"
2038 "ctrl-alt toggle mouse and keyboard grab\n"
2039 "\n"
2040 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2041
2042 exit(exitcode);
2043 }
2044
2045 #define HAS_ARG 0x0001
2046
2047 typedef struct QEMUOption {
2048 const char *name;
2049 int flags;
2050 int index;
2051 uint32_t arch_mask;
2052 } QEMUOption;
2053
2054 static const QEMUOption qemu_options[] = {
2055 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2056 #define QEMU_OPTIONS_GENERATE_OPTIONS
2057 #include "qemu-options-wrapper.h"
2058 { NULL },
2059 };
2060
2061 static bool vga_available(void)
2062 {
2063 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2064 }
2065
2066 static bool cirrus_vga_available(void)
2067 {
2068 return object_class_by_name("cirrus-vga")
2069 || object_class_by_name("isa-cirrus-vga");
2070 }
2071
2072 static bool vmware_vga_available(void)
2073 {
2074 return object_class_by_name("vmware-svga");
2075 }
2076
2077 static bool qxl_vga_available(void)
2078 {
2079 return object_class_by_name("qxl-vga");
2080 }
2081
2082 static bool tcx_vga_available(void)
2083 {
2084 return object_class_by_name("SUNW,tcx");
2085 }
2086
2087 static bool cg3_vga_available(void)
2088 {
2089 return object_class_by_name("cgthree");
2090 }
2091
2092 static void select_vgahw (const char *p)
2093 {
2094 const char *opts;
2095
2096 assert(vga_interface_type == VGA_NONE);
2097 if (strstart(p, "std", &opts)) {
2098 if (vga_available()) {
2099 vga_interface_type = VGA_STD;
2100 } else {
2101 fprintf(stderr, "Error: standard VGA not available\n");
2102 exit(0);
2103 }
2104 } else if (strstart(p, "cirrus", &opts)) {
2105 if (cirrus_vga_available()) {
2106 vga_interface_type = VGA_CIRRUS;
2107 } else {
2108 fprintf(stderr, "Error: Cirrus VGA not available\n");
2109 exit(0);
2110 }
2111 } else if (strstart(p, "vmware", &opts)) {
2112 if (vmware_vga_available()) {
2113 vga_interface_type = VGA_VMWARE;
2114 } else {
2115 fprintf(stderr, "Error: VMWare SVGA not available\n");
2116 exit(0);
2117 }
2118 } else if (strstart(p, "xenfb", &opts)) {
2119 vga_interface_type = VGA_XENFB;
2120 } else if (strstart(p, "qxl", &opts)) {
2121 if (qxl_vga_available()) {
2122 vga_interface_type = VGA_QXL;
2123 } else {
2124 fprintf(stderr, "Error: QXL VGA not available\n");
2125 exit(0);
2126 }
2127 } else if (strstart(p, "tcx", &opts)) {
2128 if (tcx_vga_available()) {
2129 vga_interface_type = VGA_TCX;
2130 } else {
2131 fprintf(stderr, "Error: TCX framebuffer not available\n");
2132 exit(0);
2133 }
2134 } else if (strstart(p, "cg3", &opts)) {
2135 if (cg3_vga_available()) {
2136 vga_interface_type = VGA_CG3;
2137 } else {
2138 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2139 exit(0);
2140 }
2141 } else if (!strstart(p, "none", &opts)) {
2142 invalid_vga:
2143 fprintf(stderr, "Unknown vga type: %s\n", p);
2144 exit(1);
2145 }
2146 while (*opts) {
2147 const char *nextopt;
2148
2149 if (strstart(opts, ",retrace=", &nextopt)) {
2150 opts = nextopt;
2151 if (strstart(opts, "dumb", &nextopt))
2152 vga_retrace_method = VGA_RETRACE_DUMB;
2153 else if (strstart(opts, "precise", &nextopt))
2154 vga_retrace_method = VGA_RETRACE_PRECISE;
2155 else goto invalid_vga;
2156 } else goto invalid_vga;
2157 opts = nextopt;
2158 }
2159 }
2160
2161 static DisplayType select_display(const char *p)
2162 {
2163 const char *opts;
2164 DisplayType display = DT_DEFAULT;
2165
2166 if (strstart(p, "sdl", &opts)) {
2167 #ifdef CONFIG_SDL
2168 display = DT_SDL;
2169 while (*opts) {
2170 const char *nextopt;
2171
2172 if (strstart(opts, ",frame=", &nextopt)) {
2173 opts = nextopt;
2174 if (strstart(opts, "on", &nextopt)) {
2175 no_frame = 0;
2176 } else if (strstart(opts, "off", &nextopt)) {
2177 no_frame = 1;
2178 } else {
2179 goto invalid_sdl_args;
2180 }
2181 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 alt_grab = 1;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 alt_grab = 0;
2187 } else {
2188 goto invalid_sdl_args;
2189 }
2190 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2191 opts = nextopt;
2192 if (strstart(opts, "on", &nextopt)) {
2193 ctrl_grab = 1;
2194 } else if (strstart(opts, "off", &nextopt)) {
2195 ctrl_grab = 0;
2196 } else {
2197 goto invalid_sdl_args;
2198 }
2199 } else if (strstart(opts, ",window_close=", &nextopt)) {
2200 opts = nextopt;
2201 if (strstart(opts, "on", &nextopt)) {
2202 no_quit = 0;
2203 } else if (strstart(opts, "off", &nextopt)) {
2204 no_quit = 1;
2205 } else {
2206 goto invalid_sdl_args;
2207 }
2208 } else {
2209 invalid_sdl_args:
2210 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2211 exit(1);
2212 }
2213 opts = nextopt;
2214 }
2215 #else
2216 fprintf(stderr, "SDL support is disabled\n");
2217 exit(1);
2218 #endif
2219 } else if (strstart(p, "vnc", &opts)) {
2220 #ifdef CONFIG_VNC
2221 display_remote++;
2222
2223 if (*opts) {
2224 const char *nextopt;
2225
2226 if (strstart(opts, "=", &nextopt)) {
2227 vnc_display = nextopt;
2228 }
2229 }
2230 if (!vnc_display) {
2231 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2232 exit(1);
2233 }
2234 #else
2235 fprintf(stderr, "VNC support is disabled\n");
2236 exit(1);
2237 #endif
2238 } else if (strstart(p, "curses", &opts)) {
2239 #ifdef CONFIG_CURSES
2240 display = DT_CURSES;
2241 #else
2242 fprintf(stderr, "Curses support is disabled\n");
2243 exit(1);
2244 #endif
2245 } else if (strstart(p, "gtk", &opts)) {
2246 #ifdef CONFIG_GTK
2247 display = DT_GTK;
2248 while (*opts) {
2249 const char *nextopt;
2250
2251 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2252 opts = nextopt;
2253 if (strstart(opts, "on", &nextopt)) {
2254 grab_on_hover = true;
2255 } else if (strstart(opts, "off", &nextopt)) {
2256 grab_on_hover = false;
2257 } else {
2258 goto invalid_gtk_args;
2259 }
2260 } else {
2261 invalid_gtk_args:
2262 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2263 exit(1);
2264 }
2265 opts = nextopt;
2266 }
2267 #else
2268 fprintf(stderr, "GTK support is disabled\n");
2269 exit(1);
2270 #endif
2271 } else if (strstart(p, "none", &opts)) {
2272 display = DT_NONE;
2273 } else {
2274 fprintf(stderr, "Unknown display type: %s\n", p);
2275 exit(1);
2276 }
2277
2278 return display;
2279 }
2280
2281 static int balloon_parse(const char *arg)
2282 {
2283 QemuOpts *opts;
2284
2285 if (strcmp(arg, "none") == 0) {
2286 return 0;
2287 }
2288
2289 if (!strncmp(arg, "virtio", 6)) {
2290 if (arg[6] == ',') {
2291 /* have params -> parse them */
2292 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2293 if (!opts)
2294 return -1;
2295 } else {
2296 /* create empty opts */
2297 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2298 &error_abort);
2299 }
2300 qemu_opt_set(opts, "driver", "virtio-balloon");
2301 return 0;
2302 }
2303
2304 return -1;
2305 }
2306
2307 char *qemu_find_file(int type, const char *name)
2308 {
2309 int i;
2310 const char *subdir;
2311 char *buf;
2312
2313 /* Try the name as a straight path first */
2314 if (access(name, R_OK) == 0) {
2315 trace_load_file(name, name);
2316 return g_strdup(name);
2317 }
2318
2319 switch (type) {
2320 case QEMU_FILE_TYPE_BIOS:
2321 subdir = "";
2322 break;
2323 case QEMU_FILE_TYPE_KEYMAP:
2324 subdir = "keymaps/";
2325 break;
2326 default:
2327 abort();
2328 }
2329
2330 for (i = 0; i < data_dir_idx; i++) {
2331 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2332 if (access(buf, R_OK) == 0) {
2333 trace_load_file(name, buf);
2334 return buf;
2335 }
2336 g_free(buf);
2337 }
2338 return NULL;
2339 }
2340
2341 static int device_help_func(QemuOpts *opts, void *opaque)
2342 {
2343 return qdev_device_help(opts);
2344 }
2345
2346 static int device_init_func(QemuOpts *opts, void *opaque)
2347 {
2348 DeviceState *dev;
2349
2350 dev = qdev_device_add(opts);
2351 if (!dev)
2352 return -1;
2353 object_unref(OBJECT(dev));
2354 return 0;
2355 }
2356
2357 static int chardev_init_func(QemuOpts *opts, void *opaque)
2358 {
2359 Error *local_err = NULL;
2360
2361 qemu_chr_new_from_opts(opts, NULL, &local_err);
2362 if (local_err) {
2363 error_report("%s", error_get_pretty(local_err));
2364 error_free(local_err);
2365 return -1;
2366 }
2367 return 0;
2368 }
2369
2370 #ifdef CONFIG_VIRTFS
2371 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2372 {
2373 int ret;
2374 ret = qemu_fsdev_add(opts);
2375
2376 return ret;
2377 }
2378 #endif
2379
2380 static int mon_init_func(QemuOpts *opts, void *opaque)
2381 {
2382 CharDriverState *chr;
2383 const char *chardev;
2384 const char *mode;
2385 int flags;
2386
2387 mode = qemu_opt_get(opts, "mode");
2388 if (mode == NULL) {
2389 mode = "readline";
2390 }
2391 if (strcmp(mode, "readline") == 0) {
2392 flags = MONITOR_USE_READLINE;
2393 } else if (strcmp(mode, "control") == 0) {
2394 flags = MONITOR_USE_CONTROL;
2395 } else {
2396 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2397 exit(1);
2398 }
2399
2400 if (qemu_opt_get_bool(opts, "pretty", 0))
2401 flags |= MONITOR_USE_PRETTY;
2402
2403 if (qemu_opt_get_bool(opts, "default", 0))
2404 flags |= MONITOR_IS_DEFAULT;
2405
2406 chardev = qemu_opt_get(opts, "chardev");
2407 chr = qemu_chr_find(chardev);
2408 if (chr == NULL) {
2409 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2410 exit(1);
2411 }
2412
2413 qemu_chr_fe_claim_no_fail(chr);
2414 monitor_init(chr, flags);
2415 return 0;
2416 }
2417
2418 static void monitor_parse(const char *optarg, const char *mode)
2419 {
2420 static int monitor_device_index = 0;
2421 QemuOpts *opts;
2422 const char *p;
2423 char label[32];
2424 int def = 0;
2425
2426 if (strstart(optarg, "chardev:", &p)) {
2427 snprintf(label, sizeof(label), "%s", p);
2428 } else {
2429 snprintf(label, sizeof(label), "compat_monitor%d",
2430 monitor_device_index);
2431 if (monitor_device_index == 0) {
2432 def = 1;
2433 }
2434 opts = qemu_chr_parse_compat(label, optarg);
2435 if (!opts) {
2436 fprintf(stderr, "parse error: %s\n", optarg);
2437 exit(1);
2438 }
2439 }
2440
2441 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2442 if (!opts) {
2443 fprintf(stderr, "duplicate chardev: %s\n", label);
2444 exit(1);
2445 }
2446 qemu_opt_set(opts, "mode", mode);
2447 qemu_opt_set(opts, "chardev", label);
2448 if (def)
2449 qemu_opt_set(opts, "default", "on");
2450 monitor_device_index++;
2451 }
2452
2453 struct device_config {
2454 enum {
2455 DEV_USB, /* -usbdevice */
2456 DEV_BT, /* -bt */
2457 DEV_SERIAL, /* -serial */
2458 DEV_PARALLEL, /* -parallel */
2459 DEV_VIRTCON, /* -virtioconsole */
2460 DEV_DEBUGCON, /* -debugcon */
2461 DEV_GDB, /* -gdb, -s */
2462 DEV_SCLP, /* s390 sclp */
2463 } type;
2464 const char *cmdline;
2465 Location loc;
2466 QTAILQ_ENTRY(device_config) next;
2467 };
2468
2469 static QTAILQ_HEAD(, device_config) device_configs =
2470 QTAILQ_HEAD_INITIALIZER(device_configs);
2471
2472 static void add_device_config(int type, const char *cmdline)
2473 {
2474 struct device_config *conf;
2475
2476 conf = g_malloc0(sizeof(*conf));
2477 conf->type = type;
2478 conf->cmdline = cmdline;
2479 loc_save(&conf->loc);
2480 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2481 }
2482
2483 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2484 {
2485 struct device_config *conf;
2486 int rc;
2487
2488 QTAILQ_FOREACH(conf, &device_configs, next) {
2489 if (conf->type != type)
2490 continue;
2491 loc_push_restore(&conf->loc);
2492 rc = func(conf->cmdline);
2493 loc_pop(&conf->loc);
2494 if (0 != rc)
2495 return rc;
2496 }
2497 return 0;
2498 }
2499
2500 static int serial_parse(const char *devname)
2501 {
2502 static int index = 0;
2503 char label[32];
2504
2505 if (strcmp(devname, "none") == 0)
2506 return 0;
2507 if (index == MAX_SERIAL_PORTS) {
2508 fprintf(stderr, "qemu: too many serial ports\n");
2509 exit(1);
2510 }
2511 snprintf(label, sizeof(label), "serial%d", index);
2512 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2513 if (!serial_hds[index]) {
2514 fprintf(stderr, "qemu: could not connect serial device"
2515 " to character backend '%s'\n", devname);
2516 return -1;
2517 }
2518 index++;
2519 return 0;
2520 }
2521
2522 static int parallel_parse(const char *devname)
2523 {
2524 static int index = 0;
2525 char label[32];
2526
2527 if (strcmp(devname, "none") == 0)
2528 return 0;
2529 if (index == MAX_PARALLEL_PORTS) {
2530 fprintf(stderr, "qemu: too many parallel ports\n");
2531 exit(1);
2532 }
2533 snprintf(label, sizeof(label), "parallel%d", index);
2534 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2535 if (!parallel_hds[index]) {
2536 fprintf(stderr, "qemu: could not connect parallel device"
2537 " to character backend '%s'\n", devname);
2538 return -1;
2539 }
2540 index++;
2541 return 0;
2542 }
2543
2544 static int virtcon_parse(const char *devname)
2545 {
2546 QemuOptsList *device = qemu_find_opts("device");
2547 static int index = 0;
2548 char label[32];
2549 QemuOpts *bus_opts, *dev_opts;
2550
2551 if (strcmp(devname, "none") == 0)
2552 return 0;
2553 if (index == MAX_VIRTIO_CONSOLES) {
2554 fprintf(stderr, "qemu: too many virtio consoles\n");
2555 exit(1);
2556 }
2557
2558 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2559 if (arch_type == QEMU_ARCH_S390X) {
2560 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2561 } else {
2562 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2563 }
2564
2565 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2566 qemu_opt_set(dev_opts, "driver", "virtconsole");
2567
2568 snprintf(label, sizeof(label), "virtcon%d", index);
2569 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2570 if (!virtcon_hds[index]) {
2571 fprintf(stderr, "qemu: could not connect virtio console"
2572 " to character backend '%s'\n", devname);
2573 return -1;
2574 }
2575 qemu_opt_set(dev_opts, "chardev", label);
2576
2577 index++;
2578 return 0;
2579 }
2580
2581 static int sclp_parse(const char *devname)
2582 {
2583 QemuOptsList *device = qemu_find_opts("device");
2584 static int index = 0;
2585 char label[32];
2586 QemuOpts *dev_opts;
2587
2588 if (strcmp(devname, "none") == 0) {
2589 return 0;
2590 }
2591 if (index == MAX_SCLP_CONSOLES) {
2592 fprintf(stderr, "qemu: too many sclp consoles\n");
2593 exit(1);
2594 }
2595
2596 assert(arch_type == QEMU_ARCH_S390X);
2597
2598 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2599 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2600
2601 snprintf(label, sizeof(label), "sclpcon%d", index);
2602 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2603 if (!sclp_hds[index]) {
2604 fprintf(stderr, "qemu: could not connect sclp console"
2605 " to character backend '%s'\n", devname);
2606 return -1;
2607 }
2608 qemu_opt_set(dev_opts, "chardev", label);
2609
2610 index++;
2611 return 0;
2612 }
2613
2614 static int debugcon_parse(const char *devname)
2615 {
2616 QemuOpts *opts;
2617
2618 if (!qemu_chr_new("debugcon", devname, NULL)) {
2619 exit(1);
2620 }
2621 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2622 if (!opts) {
2623 fprintf(stderr, "qemu: already have a debugcon device\n");
2624 exit(1);
2625 }
2626 qemu_opt_set(opts, "driver", "isa-debugcon");
2627 qemu_opt_set(opts, "chardev", "debugcon");
2628 return 0;
2629 }
2630
2631 static MachineClass *machine_parse(const char *name)
2632 {
2633 MachineClass *mc = NULL;
2634 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2635
2636 if (name) {
2637 mc = find_machine(name);
2638 }
2639 if (mc) {
2640 return mc;
2641 }
2642 if (name && !is_help_option(name)) {
2643 error_report("Unsupported machine type");
2644 error_printf("Use -machine help to list supported machines!\n");
2645 } else {
2646 printf("Supported machines are:\n");
2647 for (el = machines; el; el = el->next) {
2648 MachineClass *mc = el->data;
2649 if (mc->alias) {
2650 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2651 }
2652 printf("%-20s %s%s\n", mc->name, mc->desc,
2653 mc->is_default ? " (default)" : "");
2654 }
2655 }
2656
2657 g_slist_free(machines);
2658 exit(!name || !is_help_option(name));
2659 }
2660
2661 static int tcg_init(MachineClass *mc)
2662 {
2663 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2664 return 0;
2665 }
2666
2667 static struct {
2668 const char *opt_name;
2669 const char *name;
2670 int (*available)(void);
2671 int (*init)(MachineClass *mc);
2672 bool *allowed;
2673 } accel_list[] = {
2674 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2675 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2676 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2677 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2678 };
2679
2680 static int configure_accelerator(MachineClass *mc)
2681 {
2682 const char *p;
2683 char buf[10];
2684 int i, ret;
2685 bool accel_initialised = false;
2686 bool init_failed = false;
2687
2688 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2689 if (p == NULL) {
2690 /* Use the default "accelerator", tcg */
2691 p = "tcg";
2692 }
2693
2694 while (!accel_initialised && *p != '\0') {
2695 if (*p == ':') {
2696 p++;
2697 }
2698 p = get_opt_name(buf, sizeof (buf), p, ':');
2699 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2700 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2701 if (!accel_list[i].available()) {
2702 printf("%s not supported for this target\n",
2703 accel_list[i].name);
2704 break;
2705 }
2706 *(accel_list[i].allowed) = true;
2707 ret = accel_list[i].init(mc);
2708 if (ret < 0) {
2709 init_failed = true;
2710 fprintf(stderr, "failed to initialize %s: %s\n",
2711 accel_list[i].name,
2712 strerror(-ret));
2713 *(accel_list[i].allowed) = false;
2714 } else {
2715 accel_initialised = true;
2716 }
2717 break;
2718 }
2719 }
2720 if (i == ARRAY_SIZE(accel_list)) {
2721 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2722 }
2723 }
2724
2725 if (!accel_initialised) {
2726 if (!init_failed) {
2727 fprintf(stderr, "No accelerator found!\n");
2728 }
2729 exit(1);
2730 }
2731
2732 if (init_failed) {
2733 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2734 }
2735
2736 return !accel_initialised;
2737 }
2738
2739 void qemu_add_exit_notifier(Notifier *notify)
2740 {
2741 notifier_list_add(&exit_notifiers, notify);
2742 }
2743
2744 void qemu_remove_exit_notifier(Notifier *notify)
2745 {
2746 notifier_remove(notify);
2747 }
2748
2749 static void qemu_run_exit_notifiers(void)
2750 {
2751 notifier_list_notify(&exit_notifiers, NULL);
2752 }
2753
2754 void qemu_add_machine_init_done_notifier(Notifier *notify)
2755 {
2756 notifier_list_add(&machine_init_done_notifiers, notify);
2757 }
2758
2759 static void qemu_run_machine_init_done_notifiers(void)
2760 {
2761 notifier_list_notify(&machine_init_done_notifiers, NULL);
2762 }
2763
2764 static const QEMUOption *lookup_opt(int argc, char **argv,
2765 const char **poptarg, int *poptind)
2766 {
2767 const QEMUOption *popt;
2768 int optind = *poptind;
2769 char *r = argv[optind];
2770 const char *optarg;
2771
2772 loc_set_cmdline(argv, optind, 1);
2773 optind++;
2774 /* Treat --foo the same as -foo. */
2775 if (r[1] == '-')
2776 r++;
2777 popt = qemu_options;
2778 for(;;) {
2779 if (!popt->name) {
2780 error_report("invalid option");
2781 exit(1);
2782 }
2783 if (!strcmp(popt->name, r + 1))
2784 break;
2785 popt++;
2786 }
2787 if (popt->flags & HAS_ARG) {
2788 if (optind >= argc) {
2789 error_report("requires an argument");
2790 exit(1);
2791 }
2792 optarg = argv[optind++];
2793 loc_set_cmdline(argv, optind - 2, 2);
2794 } else {
2795 optarg = NULL;
2796 }
2797
2798 *poptarg = optarg;
2799 *poptind = optind;
2800
2801 return popt;
2802 }
2803
2804 static gpointer malloc_and_trace(gsize n_bytes)
2805 {
2806 void *ptr = malloc(n_bytes);
2807 trace_g_malloc(n_bytes, ptr);
2808 return ptr;
2809 }
2810
2811 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2812 {
2813 void *ptr = realloc(mem, n_bytes);
2814 trace_g_realloc(mem, n_bytes, ptr);
2815 return ptr;
2816 }
2817
2818 static void free_and_trace(gpointer mem)
2819 {
2820 trace_g_free(mem);
2821 free(mem);
2822 }
2823
2824 static int object_set_property(const char *name, const char *value, void *opaque)
2825 {
2826 Object *obj = OBJECT(opaque);
2827 StringInputVisitor *siv;
2828 Error *local_err = NULL;
2829
2830 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2831 strcmp(name, "type") == 0) {
2832 return 0;
2833 }
2834
2835 siv = string_input_visitor_new(value);
2836 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2837 string_input_visitor_cleanup(siv);
2838
2839 if (local_err) {
2840 qerror_report_err(local_err);
2841 error_free(local_err);
2842 return -1;
2843 }
2844
2845 return 0;
2846 }
2847
2848 static int object_create(QemuOpts *opts, void *opaque)
2849 {
2850 Error *err = NULL;
2851 char *type = NULL;
2852 char *id = NULL;
2853 void *dummy = NULL;
2854 OptsVisitor *ov;
2855 QDict *pdict;
2856
2857 ov = opts_visitor_new(opts);
2858 pdict = qemu_opts_to_qdict(opts, NULL);
2859
2860 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2861 if (err) {
2862 goto out;
2863 }
2864
2865 qdict_del(pdict, "qom-type");
2866 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2867 if (err) {
2868 goto out;
2869 }
2870
2871 qdict_del(pdict, "id");
2872 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2873 if (err) {
2874 goto out;
2875 }
2876
2877 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2878 if (err) {
2879 goto out;
2880 }
2881 visit_end_struct(opts_get_visitor(ov), &err);
2882 if (err) {
2883 qmp_object_del(id, NULL);
2884 }
2885
2886 out:
2887 opts_visitor_cleanup(ov);
2888
2889 QDECREF(pdict);
2890 g_free(id);
2891 g_free(type);
2892 g_free(dummy);
2893 if (err) {
2894 qerror_report_err(err);
2895 return -1;
2896 }
2897 return 0;
2898 }
2899
2900 int main(int argc, char **argv, char **envp)
2901 {
2902 int i;
2903 int snapshot, linux_boot;
2904 const char *icount_option = NULL;
2905 const char *initrd_filename;
2906 const char *kernel_filename, *kernel_cmdline;
2907 const char *boot_order;
2908 DisplayState *ds;
2909 int cyls, heads, secs, translation;
2910 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2911 QemuOptsList *olist;
2912 int optind;
2913 const char *optarg;
2914 const char *loadvm = NULL;
2915 MachineClass *machine_class;
2916 const char *cpu_model;
2917 const char *vga_model = NULL;
2918 const char *qtest_chrdev = NULL;
2919 const char *qtest_log = NULL;
2920 const char *pid_file = NULL;
2921 const char *incoming = NULL;
2922 #ifdef CONFIG_VNC
2923 int show_vnc_port = 0;
2924 #endif
2925 bool defconfig = true;
2926 bool userconfig = true;
2927 const char *log_mask = NULL;
2928 const char *log_file = NULL;
2929 GMemVTable mem_trace = {
2930 .malloc = malloc_and_trace,
2931 .realloc = realloc_and_trace,
2932 .free = free_and_trace,
2933 };
2934 const char *trace_events = NULL;
2935 const char *trace_file = NULL;
2936 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2937 1024 * 1024;
2938 ram_addr_t maxram_size = default_ram_size;
2939 uint64_t ram_slots = 0;
2940
2941 atexit(qemu_run_exit_notifiers);
2942 error_set_progname(argv[0]);
2943 qemu_init_exec_dir(argv[0]);
2944
2945 g_mem_set_vtable(&mem_trace);
2946
2947 module_call_init(MODULE_INIT_QOM);
2948
2949 qemu_add_opts(&qemu_drive_opts);
2950 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2951 qemu_add_drive_opts(&qemu_common_drive_opts);
2952 qemu_add_drive_opts(&qemu_drive_opts);
2953 qemu_add_opts(&qemu_chardev_opts);
2954 qemu_add_opts(&qemu_device_opts);
2955 qemu_add_opts(&qemu_netdev_opts);
2956 qemu_add_opts(&qemu_net_opts);
2957 qemu_add_opts(&qemu_rtc_opts);
2958 qemu_add_opts(&qemu_global_opts);
2959 qemu_add_opts(&qemu_mon_opts);
2960 qemu_add_opts(&qemu_trace_opts);
2961 qemu_add_opts(&qemu_option_rom_opts);
2962 qemu_add_opts(&qemu_machine_opts);
2963 qemu_add_opts(&qemu_mem_opts);
2964 qemu_add_opts(&qemu_smp_opts);
2965 qemu_add_opts(&qemu_boot_opts);
2966 qemu_add_opts(&qemu_sandbox_opts);
2967 qemu_add_opts(&qemu_add_fd_opts);
2968 qemu_add_opts(&qemu_object_opts);
2969 qemu_add_opts(&qemu_tpmdev_opts);
2970 qemu_add_opts(&qemu_realtime_opts);
2971 qemu_add_opts(&qemu_msg_opts);
2972 qemu_add_opts(&qemu_name_opts);
2973 qemu_add_opts(&qemu_numa_opts);
2974
2975 runstate_init();
2976
2977 rtc_clock = QEMU_CLOCK_HOST;
2978
2979 qemu_init_auxval(envp);
2980 qemu_cache_utils_init();
2981
2982 QLIST_INIT (&vm_change_state_head);
2983 os_setup_early_signal_handling();
2984
2985 module_call_init(MODULE_INIT_MACHINE);
2986 machine_class = find_default_machine();
2987 cpu_model = NULL;
2988 ram_size = default_ram_size;
2989 snapshot = 0;
2990 cyls = heads = secs = 0;
2991 translation = BIOS_ATA_TRANSLATION_AUTO;
2992
2993 for (i = 0; i < MAX_NODES; i++) {
2994 numa_info[i].node_mem = 0;
2995 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2996 }
2997
2998 nb_numa_nodes = 0;
2999 nb_nics = 0;
3000
3001 bdrv_init_with_whitelist();
3002
3003 autostart = 1;
3004
3005 /* first pass of option parsing */
3006 optind = 1;
3007 while (optind < argc) {
3008 if (argv[optind][0] != '-') {
3009 /* disk image */
3010 optind++;
3011 } else {
3012 const QEMUOption *popt;
3013
3014 popt = lookup_opt(argc, argv, &optarg, &optind);
3015 switch (popt->index) {
3016 case QEMU_OPTION_nodefconfig:
3017 defconfig = false;
3018 break;
3019 case QEMU_OPTION_nouserconfig:
3020 userconfig = false;
3021 break;
3022 }
3023 }
3024 }
3025
3026 if (defconfig) {
3027 int ret;
3028 ret = qemu_read_default_config_files(userconfig);
3029 if (ret < 0) {
3030 exit(1);
3031 }
3032 }
3033
3034 /* second pass of option parsing */
3035 optind = 1;
3036 for(;;) {
3037 if (optind >= argc)
3038 break;
3039 if (argv[optind][0] != '-') {
3040 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3041 } else {
3042 const QEMUOption *popt;
3043
3044 popt = lookup_opt(argc, argv, &optarg, &optind);
3045 if (!(popt->arch_mask & arch_type)) {
3046 printf("Option %s not supported for this target\n", popt->name);
3047 exit(1);
3048 }
3049 switch(popt->index) {
3050 case QEMU_OPTION_M:
3051 machine_class = machine_parse(optarg);
3052 break;
3053 case QEMU_OPTION_no_kvm_irqchip: {
3054 olist = qemu_find_opts("machine");
3055 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3056 break;
3057 }
3058 case QEMU_OPTION_cpu:
3059 /* hw initialization will check this */
3060 cpu_model = optarg;
3061 break;
3062 case QEMU_OPTION_hda:
3063 {
3064 char buf[256];
3065 if (cyls == 0)
3066 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3067 else
3068 snprintf(buf, sizeof(buf),
3069 "%s,cyls=%d,heads=%d,secs=%d%s",
3070 HD_OPTS , cyls, heads, secs,
3071 translation == BIOS_ATA_TRANSLATION_LBA ?
3072 ",trans=lba" :
3073 translation == BIOS_ATA_TRANSLATION_NONE ?
3074 ",trans=none" : "");
3075 drive_add(IF_DEFAULT, 0, optarg, buf);
3076 break;
3077 }
3078 case QEMU_OPTION_hdb:
3079 case QEMU_OPTION_hdc:
3080 case QEMU_OPTION_hdd:
3081 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3082 HD_OPTS);
3083 break;
3084 case QEMU_OPTION_drive:
3085 if (drive_def(optarg) == NULL) {
3086 exit(1);
3087 }
3088 break;
3089 case QEMU_OPTION_set:
3090 if (qemu_set_option(optarg) != 0)
3091 exit(1);
3092 break;
3093 case QEMU_OPTION_global:
3094 if (qemu_global_option(optarg) != 0)
3095 exit(1);
3096 break;
3097 case QEMU_OPTION_mtdblock:
3098 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3099 break;
3100 case QEMU_OPTION_sd:
3101 drive_add(IF_SD, -1, optarg, SD_OPTS);
3102 break;
3103 case QEMU_OPTION_pflash:
3104 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3105 break;
3106 case QEMU_OPTION_snapshot:
3107 snapshot = 1;
3108 break;
3109 case QEMU_OPTION_hdachs:
3110 {
3111 const char *p;
3112 p = optarg;
3113 cyls = strtol(p, (char **)&p, 0);
3114 if (cyls < 1 || cyls > 16383)
3115 goto chs_fail;
3116 if (*p != ',')
3117 goto chs_fail;
3118 p++;
3119 heads = strtol(p, (char **)&p, 0);
3120 if (heads < 1 || heads > 16)
3121 goto chs_fail;
3122 if (*p != ',')
3123 goto chs_fail;
3124 p++;
3125 secs = strtol(p, (char **)&p, 0);
3126 if (secs < 1 || secs > 63)
3127 goto chs_fail;
3128 if (*p == ',') {
3129 p++;
3130 if (!strcmp(p, "large")) {
3131 translation = BIOS_ATA_TRANSLATION_LARGE;
3132 } else if (!strcmp(p, "rechs")) {
3133 translation = BIOS_ATA_TRANSLATION_RECHS;
3134 } else if (!strcmp(p, "none")) {
3135 translation = BIOS_ATA_TRANSLATION_NONE;
3136 } else if (!strcmp(p, "lba")) {
3137 translation = BIOS_ATA_TRANSLATION_LBA;
3138 } else if (!strcmp(p, "auto")) {
3139 translation = BIOS_ATA_TRANSLATION_AUTO;
3140 } else {
3141 goto chs_fail;
3142 }
3143 } else if (*p != '\0') {
3144 chs_fail:
3145 fprintf(stderr, "qemu: invalid physical CHS format\n");
3146 exit(1);
3147 }
3148 if (hda_opts != NULL) {
3149 char num[16];
3150 snprintf(num, sizeof(num), "%d", cyls);
3151 qemu_opt_set(hda_opts, "cyls", num);
3152 snprintf(num, sizeof(num), "%d", heads);
3153 qemu_opt_set(hda_opts, "heads", num);
3154 snprintf(num, sizeof(num), "%d", secs);
3155 qemu_opt_set(hda_opts, "secs", num);
3156 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3157 qemu_opt_set(hda_opts, "trans", "large");
3158 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3159 qemu_opt_set(hda_opts, "trans", "rechs");
3160 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3161 qemu_opt_set(hda_opts, "trans", "lba");
3162 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3163 qemu_opt_set(hda_opts, "trans", "none");
3164 }
3165 }
3166 }
3167 break;
3168 case QEMU_OPTION_numa:
3169 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3170 if (!opts) {
3171 exit(1);
3172 }
3173 break;
3174 case QEMU_OPTION_display:
3175 display_type = select_display(optarg);
3176 break;
3177 case QEMU_OPTION_nographic:
3178 display_type = DT_NOGRAPHIC;
3179 break;
3180 case QEMU_OPTION_curses:
3181 #ifdef CONFIG_CURSES
3182 display_type = DT_CURSES;
3183 #else
3184 fprintf(stderr, "Curses support is disabled\n");
3185 exit(1);
3186 #endif
3187 break;
3188 case QEMU_OPTION_portrait:
3189 graphic_rotate = 90;
3190 break;
3191 case QEMU_OPTION_rotate:
3192 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3193 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3194 graphic_rotate != 180 && graphic_rotate != 270) {
3195 fprintf(stderr,
3196 "qemu: only 90, 180, 270 deg rotation is available\n");
3197 exit(1);
3198 }
3199 break;
3200 case QEMU_OPTION_kernel:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3202 break;
3203 case QEMU_OPTION_initrd:
3204 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3205 break;
3206 case QEMU_OPTION_append:
3207 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3208 break;
3209 case QEMU_OPTION_dtb:
3210 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3211 break;
3212 case QEMU_OPTION_cdrom:
3213 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3214 break;
3215 case QEMU_OPTION_boot:
3216 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3217 if (!opts) {
3218 exit(1);
3219 }
3220 break;
3221 case QEMU_OPTION_fda:
3222 case QEMU_OPTION_fdb:
3223 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3224 optarg, FD_OPTS);
3225 break;
3226 case QEMU_OPTION_no_fd_bootchk:
3227 fd_bootchk = 0;
3228 break;
3229 case QEMU_OPTION_netdev:
3230 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3231 exit(1);
3232 }
3233 break;
3234 case QEMU_OPTION_net:
3235 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3236 exit(1);
3237 }
3238 break;
3239 #ifdef CONFIG_LIBISCSI
3240 case QEMU_OPTION_iscsi:
3241 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3242 if (!opts) {
3243 exit(1);
3244 }
3245 break;
3246 #endif
3247 #ifdef CONFIG_SLIRP
3248 case QEMU_OPTION_tftp:
3249 legacy_tftp_prefix = optarg;
3250 break;
3251 case QEMU_OPTION_bootp:
3252 legacy_bootp_filename = optarg;
3253 break;
3254 case QEMU_OPTION_redir:
3255 if (net_slirp_redir(optarg) < 0)
3256 exit(1);
3257 break;
3258 #endif
3259 case QEMU_OPTION_bt:
3260 add_device_config(DEV_BT, optarg);
3261 break;
3262 case QEMU_OPTION_audio_help:
3263 AUD_help ();
3264 exit (0);
3265 break;
3266 case QEMU_OPTION_soundhw:
3267 select_soundhw (optarg);
3268 break;
3269 case QEMU_OPTION_h:
3270 help(0);
3271 break;
3272 case QEMU_OPTION_version:
3273 version();
3274 exit(0);
3275 break;
3276 case QEMU_OPTION_m: {
3277 uint64_t sz;
3278 const char *mem_str;
3279 const char *maxmem_str, *slots_str;
3280
3281 opts = qemu_opts_parse(qemu_find_opts("memory"),
3282 optarg, 1);
3283 if (!opts) {
3284 exit(EXIT_FAILURE);
3285 }
3286
3287 mem_str = qemu_opt_get(opts, "size");
3288 if (!mem_str) {
3289 error_report("invalid -m option, missing 'size' option");
3290 exit(EXIT_FAILURE);
3291 }
3292 if (!*mem_str) {
3293 error_report("missing 'size' option value");
3294 exit(EXIT_FAILURE);
3295 }
3296
3297 sz = qemu_opt_get_size(opts, "size", ram_size);
3298
3299 /* Fix up legacy suffix-less format */
3300 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3301 uint64_t overflow_check = sz;
3302
3303 sz <<= 20;
3304 if ((sz >> 20) != overflow_check) {
3305 error_report("too large 'size' option value");
3306 exit(EXIT_FAILURE);
3307 }
3308 }
3309
3310 /* backward compatibility behaviour for case "-m 0" */
3311 if (sz == 0) {
3312 sz = default_ram_size;
3313 }
3314
3315 sz = QEMU_ALIGN_UP(sz, 8192);
3316 ram_size = sz;
3317 if (ram_size != sz) {
3318 error_report("ram size too large");
3319 exit(EXIT_FAILURE);
3320 }
3321
3322 maxmem_str = qemu_opt_get(opts, "maxmem");
3323 slots_str = qemu_opt_get(opts, "slots");
3324 if (maxmem_str && slots_str) {
3325 uint64_t slots;
3326
3327 sz = qemu_opt_get_size(opts, "maxmem", 0);
3328 if (sz < ram_size) {
3329 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3330 "(%" PRIu64 ") <= initial memory ("
3331 RAM_ADDR_FMT ")\n", sz, ram_size);
3332 exit(EXIT_FAILURE);
3333 }
3334
3335 slots = qemu_opt_get_number(opts, "slots", 0);
3336 if ((sz > ram_size) && !slots) {
3337 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3338 "(%" PRIu64 ") more than initial memory ("
3339 RAM_ADDR_FMT ") but no hotplug slots where "
3340 "specified\n", sz, ram_size);
3341 exit(EXIT_FAILURE);
3342 }
3343
3344 if ((sz <= ram_size) && slots) {
3345 fprintf(stderr, "qemu: invalid -m option value: %"
3346 PRIu64 " hotplug slots where specified but "
3347 "maxmem (%" PRIu64 ") <= initial memory ("
3348 RAM_ADDR_FMT ")\n", slots, sz, ram_size);
3349 exit(EXIT_FAILURE);
3350 }
3351 maxram_size = sz;
3352 ram_slots = slots;
3353 } else if ((!maxmem_str && slots_str) ||
3354 (maxmem_str && !slots_str)) {
3355 fprintf(stderr, "qemu: invalid -m option value: missing "
3356 "'%s' option\n", slots_str ? "maxmem" : "slots");
3357 exit(EXIT_FAILURE);
3358 }
3359 break;
3360 }
3361 #ifdef CONFIG_TPM
3362 case QEMU_OPTION_tpmdev:
3363 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3364 exit(1);
3365 }
3366 break;
3367 #endif
3368 case QEMU_OPTION_mempath:
3369 mem_path = optarg;
3370 break;
3371 case QEMU_OPTION_mem_prealloc:
3372 mem_prealloc = 1;
3373 break;
3374 case QEMU_OPTION_d:
3375 log_mask = optarg;
3376 break;
3377 case QEMU_OPTION_D:
3378 log_file = optarg;
3379 break;
3380 case QEMU_OPTION_s:
3381 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3382 break;
3383 case QEMU_OPTION_gdb:
3384 add_device_config(DEV_GDB, optarg);
3385 break;
3386 case QEMU_OPTION_L:
3387 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3388 data_dir[data_dir_idx++] = optarg;
3389 }
3390 break;
3391 case QEMU_OPTION_bios:
3392 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3393 break;
3394 case QEMU_OPTION_singlestep:
3395 singlestep = 1;
3396 break;
3397 case QEMU_OPTION_S:
3398 autostart = 0;
3399 break;
3400 case QEMU_OPTION_k:
3401 keyboard_layout = optarg;
3402 break;
3403 case QEMU_OPTION_localtime:
3404 rtc_utc = 0;
3405 break;
3406 case QEMU_OPTION_vga:
3407 vga_model = optarg;
3408 default_vga = 0;
3409 break;
3410 case QEMU_OPTION_g:
3411 {
3412 const char *p;
3413 int w, h, depth;
3414 p = optarg;
3415 w = strtol(p, (char **)&p, 10);
3416 if (w <= 0) {
3417 graphic_error:
3418 fprintf(stderr, "qemu: invalid resolution or depth\n");
3419 exit(1);
3420 }
3421 if (*p != 'x')
3422 goto graphic_error;
3423 p++;
3424 h = strtol(p, (char **)&p, 10);
3425 if (h <= 0)
3426 goto graphic_error;
3427 if (*p == 'x') {
3428 p++;
3429 depth = strtol(p, (char **)&p, 10);
3430 if (depth != 8 && depth != 15 && depth != 16 &&
3431 depth != 24 && depth != 32)
3432 goto graphic_error;
3433 } else if (*p == '\0') {
3434 depth = graphic_depth;
3435 } else {
3436 goto graphic_error;
3437 }
3438
3439 graphic_width = w;
3440 graphic_height = h;
3441 graphic_depth = depth;
3442 }
3443 break;
3444 case QEMU_OPTION_echr:
3445 {
3446 char *r;
3447 term_escape_char = strtol(optarg, &r, 0);
3448 if (r == optarg)
3449 printf("Bad argument to echr\n");
3450 break;
3451 }
3452 case QEMU_OPTION_monitor:
3453 default_monitor = 0;
3454 if (strncmp(optarg, "none", 4)) {
3455 monitor_parse(optarg, "readline");
3456 }
3457 break;
3458 case QEMU_OPTION_qmp:
3459 monitor_parse(optarg, "control");
3460 default_monitor = 0;
3461 break;
3462 case QEMU_OPTION_mon:
3463 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3464 if (!opts) {
3465 exit(1);
3466 }
3467 default_monitor = 0;
3468 break;
3469 case QEMU_OPTION_chardev:
3470 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3471 if (!opts) {
3472 exit(1);
3473 }
3474 break;
3475 case QEMU_OPTION_fsdev:
3476 olist = qemu_find_opts("fsdev");
3477 if (!olist) {
3478 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3479 exit(1);
3480 }
3481 opts = qemu_opts_parse(olist, optarg, 1);
3482 if (!opts) {
3483 exit(1);
3484 }
3485 break;
3486 case QEMU_OPTION_virtfs: {
3487 QemuOpts *fsdev;
3488 QemuOpts *device;
3489 const char *writeout, *sock_fd, *socket;
3490
3491 olist = qemu_find_opts(