]> git.proxmox.com Git - mirror_qemu.git/blob - vl.c
monitor: Replace monitor_init() with monitor_init_{hmp, qmp}()
[mirror_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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/units.h"
28 #include "qapi/error.h"
29 #include "qemu-version.h"
30 #include "qemu/cutils.h"
31 #include "qemu/help_option.h"
32 #include "qemu/uuid.h"
33 #include "sysemu/seccomp.h"
34 #include "sysemu/tcg.h"
35
36 #ifdef CONFIG_SDL
37 #if defined(__APPLE__) || defined(main)
38 #include <SDL.h>
39 int qemu_main(int argc, char **argv, char **envp);
40 int main(int argc, char **argv)
41 {
42 return qemu_main(argc, argv, NULL);
43 }
44 #undef main
45 #define main qemu_main
46 #endif
47 #endif /* CONFIG_SDL */
48
49 #ifdef CONFIG_COCOA
50 #undef main
51 #define main qemu_main
52 #endif /* CONFIG_COCOA */
53
54
55 #include "qemu/error-report.h"
56 #include "qemu/sockets.h"
57 #include "hw/hw.h"
58 #include "hw/boards.h"
59 #include "sysemu/accel.h"
60 #include "hw/usb.h"
61 #include "hw/isa/isa.h"
62 #include "hw/scsi/scsi.h"
63 #include "hw/display/vga.h"
64 #include "hw/bt.h"
65 #include "sysemu/watchdog.h"
66 #include "hw/firmware/smbios.h"
67 #include "hw/acpi/acpi.h"
68 #include "hw/xen/xen.h"
69 #include "hw/qdev.h"
70 #include "hw/loader.h"
71 #include "monitor/qdev.h"
72 #include "sysemu/bt.h"
73 #include "net/net.h"
74 #include "net/slirp.h"
75 #include "monitor/monitor.h"
76 #include "ui/console.h"
77 #include "ui/input.h"
78 #include "sysemu/sysemu.h"
79 #include "sysemu/numa.h"
80 #include "exec/gdbstub.h"
81 #include "qemu/timer.h"
82 #include "chardev/char.h"
83 #include "qemu/bitmap.h"
84 #include "qemu/log.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/misc.h"
88 #include "migration/snapshot.h"
89 #include "migration/global_state.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "hw/audio/soundhw.h"
93 #include "audio/audio.h"
94 #include "sysemu/cpus.h"
95 #include "migration/colo.h"
96 #include "migration/postcopy-ram.h"
97 #include "sysemu/kvm.h"
98 #include "sysemu/hax.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qemu/option.h"
101 #include "qemu/config-file.h"
102 #include "qemu-options.h"
103 #include "qemu/main-loop.h"
104 #ifdef CONFIG_VIRTFS
105 #include "fsdev/qemu-fsdev.h"
106 #endif
107 #include "sysemu/qtest.h"
108
109 #include "disas/disas.h"
110
111 #include "trace-root.h"
112 #include "trace/control.h"
113 #include "qemu/queue.h"
114 #include "sysemu/arch_init.h"
115
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qapi/clone-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "hw/semihosting/semihost.h"
122 #include "crypto/init.h"
123 #include "sysemu/replay.h"
124 #include "qapi/qapi-events-run-state.h"
125 #include "qapi/qapi-visit-block-core.h"
126 #include "qapi/qapi-visit-ui.h"
127 #include "qapi/qapi-commands-block-core.h"
128 #include "qapi/qapi-commands-misc.h"
129 #include "qapi/qapi-commands-run-state.h"
130 #include "qapi/qapi-commands-ui.h"
131 #include "qapi/qmp/qerror.h"
132 #include "sysemu/iothread.h"
133 #include "qemu/guest-random.h"
134
135 #define MAX_VIRTIO_CONSOLES 1
136
137 static const char *data_dir[16];
138 static int data_dir_idx;
139 const char *bios_name = NULL;
140 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
141 int display_opengl;
142 const char* keyboard_layout = NULL;
143 ram_addr_t ram_size;
144 const char *mem_path = NULL;
145 int mem_prealloc = 0; /* force preallocation of physical target memory */
146 bool enable_mlock = false;
147 bool enable_cpu_pm = false;
148 int nb_nics;
149 NICInfo nd_table[MAX_NICS];
150 int autostart;
151 static enum {
152 RTC_BASE_UTC,
153 RTC_BASE_LOCALTIME,
154 RTC_BASE_DATETIME,
155 } rtc_base_type = RTC_BASE_UTC;
156 static time_t rtc_ref_start_datetime;
157 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
158 static int rtc_host_datetime_offset = -1; /* valid & used only with
159 RTC_BASE_DATETIME */
160 QEMUClockType rtc_clock;
161 int vga_interface_type = VGA_NONE;
162 static DisplayOptions dpy;
163 static int num_serial_hds;
164 static Chardev **serial_hds;
165 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
166 int win2k_install_hack = 0;
167 int singlestep = 0;
168 int smp_cpus;
169 unsigned int max_cpus;
170 int smp_cores = 1;
171 int smp_threads = 1;
172 int acpi_enabled = 1;
173 int no_hpet = 0;
174 int fd_bootchk = 1;
175 static int no_reboot;
176 int no_shutdown = 0;
177 int cursor_hide = 1;
178 int graphic_rotate = 0;
179 const char *watchdog;
180 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
181 int nb_option_roms;
182 int old_param = 0;
183 const char *qemu_name;
184 int alt_grab = 0;
185 int ctrl_grab = 0;
186 unsigned int nb_prom_envs = 0;
187 const char *prom_envs[MAX_PROM_ENVS];
188 int boot_menu;
189 bool boot_strict;
190 uint8_t *boot_splash_filedata;
191 int only_migratable; /* turn it off unless user states otherwise */
192 bool wakeup_suspend_enabled;
193
194 int icount_align_option;
195
196 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
197 * little-endian "wire format" described in the SMBIOS 2.6 specification.
198 */
199 QemuUUID qemu_uuid;
200 bool qemu_uuid_set;
201
202 static NotifierList exit_notifiers =
203 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
204
205 static NotifierList machine_init_done_notifiers =
206 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
207
208 bool xen_allowed;
209 uint32_t xen_domid;
210 enum xen_mode xen_mode = XEN_EMULATE;
211 bool xen_domid_restrict;
212
213 static int has_defaults = 1;
214 static int default_serial = 1;
215 static int default_parallel = 1;
216 static int default_monitor = 1;
217 static int default_floppy = 1;
218 static int default_cdrom = 1;
219 static int default_sdcard = 1;
220 static int default_vga = 1;
221 static int default_net = 1;
222
223 static struct {
224 const char *driver;
225 int *flag;
226 } default_list[] = {
227 { .driver = "isa-serial", .flag = &default_serial },
228 { .driver = "isa-parallel", .flag = &default_parallel },
229 { .driver = "isa-fdc", .flag = &default_floppy },
230 { .driver = "floppy", .flag = &default_floppy },
231 { .driver = "ide-cd", .flag = &default_cdrom },
232 { .driver = "ide-hd", .flag = &default_cdrom },
233 { .driver = "ide-drive", .flag = &default_cdrom },
234 { .driver = "scsi-cd", .flag = &default_cdrom },
235 { .driver = "scsi-hd", .flag = &default_cdrom },
236 { .driver = "VGA", .flag = &default_vga },
237 { .driver = "isa-vga", .flag = &default_vga },
238 { .driver = "cirrus-vga", .flag = &default_vga },
239 { .driver = "isa-cirrus-vga", .flag = &default_vga },
240 { .driver = "vmware-svga", .flag = &default_vga },
241 { .driver = "qxl-vga", .flag = &default_vga },
242 { .driver = "virtio-vga", .flag = &default_vga },
243 { .driver = "ati-vga", .flag = &default_vga },
244 { .driver = "vhost-user-vga", .flag = &default_vga },
245 };
246
247 static QemuOptsList qemu_rtc_opts = {
248 .name = "rtc",
249 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
250 .merge_lists = true,
251 .desc = {
252 {
253 .name = "base",
254 .type = QEMU_OPT_STRING,
255 },{
256 .name = "clock",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "driftfix",
260 .type = QEMU_OPT_STRING,
261 },
262 { /* end of list */ }
263 },
264 };
265
266 static QemuOptsList qemu_option_rom_opts = {
267 .name = "option-rom",
268 .implied_opt_name = "romfile",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
270 .desc = {
271 {
272 .name = "bootindex",
273 .type = QEMU_OPT_NUMBER,
274 }, {
275 .name = "romfile",
276 .type = QEMU_OPT_STRING,
277 },
278 { /* end of list */ }
279 },
280 };
281
282 static QemuOptsList qemu_machine_opts = {
283 .name = "machine",
284 .implied_opt_name = "type",
285 .merge_lists = true,
286 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
287 .desc = {
288 /*
289 * no elements => accept any
290 * sanity checking will happen later
291 * when setting machine properties
292 */
293 { }
294 },
295 };
296
297 static QemuOptsList qemu_accel_opts = {
298 .name = "accel",
299 .implied_opt_name = "accel",
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
301 .merge_lists = true,
302 .desc = {
303 {
304 .name = "accel",
305 .type = QEMU_OPT_STRING,
306 .help = "Select the type of accelerator",
307 },
308 {
309 .name = "thread",
310 .type = QEMU_OPT_STRING,
311 .help = "Enable/disable multi-threaded TCG",
312 },
313 { /* end of list */ }
314 },
315 };
316
317 static QemuOptsList qemu_boot_opts = {
318 .name = "boot-opts",
319 .implied_opt_name = "order",
320 .merge_lists = true,
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
322 .desc = {
323 {
324 .name = "order",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "once",
328 .type = QEMU_OPT_STRING,
329 }, {
330 .name = "menu",
331 .type = QEMU_OPT_BOOL,
332 }, {
333 .name = "splash",
334 .type = QEMU_OPT_STRING,
335 }, {
336 .name = "splash-time",
337 .type = QEMU_OPT_NUMBER,
338 }, {
339 .name = "reboot-timeout",
340 .type = QEMU_OPT_NUMBER,
341 }, {
342 .name = "strict",
343 .type = QEMU_OPT_BOOL,
344 },
345 { /*End of list */ }
346 },
347 };
348
349 static QemuOptsList qemu_add_fd_opts = {
350 .name = "add-fd",
351 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
352 .desc = {
353 {
354 .name = "fd",
355 .type = QEMU_OPT_NUMBER,
356 .help = "file descriptor of which a duplicate is added to fd set",
357 },{
358 .name = "set",
359 .type = QEMU_OPT_NUMBER,
360 .help = "ID of the fd set to add fd to",
361 },{
362 .name = "opaque",
363 .type = QEMU_OPT_STRING,
364 .help = "free-form string used to describe fd",
365 },
366 { /* end of list */ }
367 },
368 };
369
370 static QemuOptsList qemu_object_opts = {
371 .name = "object",
372 .implied_opt_name = "qom-type",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
374 .desc = {
375 { }
376 },
377 };
378
379 static QemuOptsList qemu_tpmdev_opts = {
380 .name = "tpmdev",
381 .implied_opt_name = "type",
382 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
383 .desc = {
384 /* options are defined in the TPM backends */
385 { /* end of list */ }
386 },
387 };
388
389 static QemuOptsList qemu_realtime_opts = {
390 .name = "realtime",
391 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
392 .desc = {
393 {
394 .name = "mlock",
395 .type = QEMU_OPT_BOOL,
396 },
397 { /* end of list */ }
398 },
399 };
400
401 static QemuOptsList qemu_overcommit_opts = {
402 .name = "overcommit",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
404 .desc = {
405 {
406 .name = "mem-lock",
407 .type = QEMU_OPT_BOOL,
408 },
409 {
410 .name = "cpu-pm",
411 .type = QEMU_OPT_BOOL,
412 },
413 { /* end of list */ }
414 },
415 };
416
417 static QemuOptsList qemu_msg_opts = {
418 .name = "msg",
419 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
420 .desc = {
421 {
422 .name = "timestamp",
423 .type = QEMU_OPT_BOOL,
424 },
425 { /* end of list */ }
426 },
427 };
428
429 static QemuOptsList qemu_name_opts = {
430 .name = "name",
431 .implied_opt_name = "guest",
432 .merge_lists = true,
433 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
434 .desc = {
435 {
436 .name = "guest",
437 .type = QEMU_OPT_STRING,
438 .help = "Sets the name of the guest.\n"
439 "This name will be displayed in the SDL window caption.\n"
440 "The name will also be used for the VNC server",
441 }, {
442 .name = "process",
443 .type = QEMU_OPT_STRING,
444 .help = "Sets the name of the QEMU process, as shown in top etc",
445 }, {
446 .name = "debug-threads",
447 .type = QEMU_OPT_BOOL,
448 .help = "When enabled, name the individual threads; defaults off.\n"
449 "NOTE: The thread names are for debugging and not a\n"
450 "stable API.",
451 },
452 { /* End of list */ }
453 },
454 };
455
456 static QemuOptsList qemu_mem_opts = {
457 .name = "memory",
458 .implied_opt_name = "size",
459 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
460 .merge_lists = true,
461 .desc = {
462 {
463 .name = "size",
464 .type = QEMU_OPT_SIZE,
465 },
466 {
467 .name = "slots",
468 .type = QEMU_OPT_NUMBER,
469 },
470 {
471 .name = "maxmem",
472 .type = QEMU_OPT_SIZE,
473 },
474 { /* end of list */ }
475 },
476 };
477
478 static QemuOptsList qemu_icount_opts = {
479 .name = "icount",
480 .implied_opt_name = "shift",
481 .merge_lists = true,
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
483 .desc = {
484 {
485 .name = "shift",
486 .type = QEMU_OPT_STRING,
487 }, {
488 .name = "align",
489 .type = QEMU_OPT_BOOL,
490 }, {
491 .name = "sleep",
492 .type = QEMU_OPT_BOOL,
493 }, {
494 .name = "rr",
495 .type = QEMU_OPT_STRING,
496 }, {
497 .name = "rrfile",
498 .type = QEMU_OPT_STRING,
499 }, {
500 .name = "rrsnapshot",
501 .type = QEMU_OPT_STRING,
502 },
503 { /* end of list */ }
504 },
505 };
506
507 static QemuOptsList qemu_fw_cfg_opts = {
508 .name = "fw_cfg",
509 .implied_opt_name = "name",
510 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
511 .desc = {
512 {
513 .name = "name",
514 .type = QEMU_OPT_STRING,
515 .help = "Sets the fw_cfg name of the blob to be inserted",
516 }, {
517 .name = "file",
518 .type = QEMU_OPT_STRING,
519 .help = "Sets the name of the file from which "
520 "the fw_cfg blob will be loaded",
521 }, {
522 .name = "string",
523 .type = QEMU_OPT_STRING,
524 .help = "Sets content of the blob to be inserted from a string",
525 },
526 { /* end of list */ }
527 },
528 };
529
530 /**
531 * Get machine options
532 *
533 * Returns: machine options (never null).
534 */
535 QemuOpts *qemu_get_machine_opts(void)
536 {
537 return qemu_find_opts_singleton("machine");
538 }
539
540 const char *qemu_get_vm_name(void)
541 {
542 return qemu_name;
543 }
544
545 static void res_free(void)
546 {
547 g_free(boot_splash_filedata);
548 boot_splash_filedata = NULL;
549 }
550
551 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
552 {
553 const char *driver = qemu_opt_get(opts, "driver");
554 int i;
555
556 if (!driver)
557 return 0;
558 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
559 if (strcmp(default_list[i].driver, driver) != 0)
560 continue;
561 *(default_list[i].flag) = 0;
562 }
563 return 0;
564 }
565
566 /***********************************************************/
567 /* QEMU state */
568
569 static RunState current_run_state = RUN_STATE_PRECONFIG;
570
571 /* We use RUN_STATE__MAX but any invalid value will do */
572 static RunState vmstop_requested = RUN_STATE__MAX;
573 static QemuMutex vmstop_lock;
574
575 typedef struct {
576 RunState from;
577 RunState to;
578 } RunStateTransition;
579
580 static const RunStateTransition runstate_transitions_def[] = {
581 /* from -> to */
582 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
583 /* Early switch to inmigrate state to allow -incoming CLI option work
584 * as it used to. TODO: delay actual switching to inmigrate state to
585 * the point after machine is built and remove this hack.
586 */
587 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
588
589 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
590 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
591 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
592
593 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
594 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
595 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
596 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
597 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
598 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
599 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
600 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
601 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
603 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
604 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
605
606 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
607 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
609
610 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
611 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
613
614 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
615 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
617 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
618 { RUN_STATE_PAUSED, RUN_STATE_COLO},
619
620 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
621 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
623
624 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
625 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
627
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
629 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
630 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
631 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
633
634 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
635 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
636
637 { RUN_STATE_COLO, RUN_STATE_RUNNING },
638
639 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
640 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
641 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
642 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
643 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
644 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
645 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
646 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
647 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
648 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
649 { RUN_STATE_RUNNING, RUN_STATE_COLO},
650
651 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
652
653 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
654 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
656
657 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
658 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
659 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
660 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
661 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
662 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
663
664 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
665 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
666 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
668
669 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
670 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
672
673 { RUN_STATE__MAX, RUN_STATE__MAX },
674 };
675
676 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
677
678 bool runstate_check(RunState state)
679 {
680 return current_run_state == state;
681 }
682
683 bool runstate_store(char *str, size_t size)
684 {
685 const char *state = RunState_str(current_run_state);
686 size_t len = strlen(state) + 1;
687
688 if (len > size) {
689 return false;
690 }
691 memcpy(str, state, len);
692 return true;
693 }
694
695 static void runstate_init(void)
696 {
697 const RunStateTransition *p;
698
699 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
700 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
701 runstate_valid_transitions[p->from][p->to] = true;
702 }
703
704 qemu_mutex_init(&vmstop_lock);
705 }
706
707 /* This function will abort() on invalid state transitions */
708 void runstate_set(RunState new_state)
709 {
710 assert(new_state < RUN_STATE__MAX);
711
712 trace_runstate_set(current_run_state, RunState_str(current_run_state),
713 new_state, RunState_str(new_state));
714
715 if (current_run_state == new_state) {
716 return;
717 }
718
719 if (!runstate_valid_transitions[current_run_state][new_state]) {
720 error_report("invalid runstate transition: '%s' -> '%s'",
721 RunState_str(current_run_state),
722 RunState_str(new_state));
723 abort();
724 }
725
726 current_run_state = new_state;
727 }
728
729 int runstate_is_running(void)
730 {
731 return runstate_check(RUN_STATE_RUNNING);
732 }
733
734 bool runstate_needs_reset(void)
735 {
736 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
737 runstate_check(RUN_STATE_SHUTDOWN);
738 }
739
740 StatusInfo *qmp_query_status(Error **errp)
741 {
742 StatusInfo *info = g_malloc0(sizeof(*info));
743
744 info->running = runstate_is_running();
745 info->singlestep = singlestep;
746 info->status = current_run_state;
747
748 return info;
749 }
750
751 bool qemu_vmstop_requested(RunState *r)
752 {
753 qemu_mutex_lock(&vmstop_lock);
754 *r = vmstop_requested;
755 vmstop_requested = RUN_STATE__MAX;
756 qemu_mutex_unlock(&vmstop_lock);
757 return *r < RUN_STATE__MAX;
758 }
759
760 void qemu_system_vmstop_request_prepare(void)
761 {
762 qemu_mutex_lock(&vmstop_lock);
763 }
764
765 void qemu_system_vmstop_request(RunState state)
766 {
767 vmstop_requested = state;
768 qemu_mutex_unlock(&vmstop_lock);
769 qemu_notify_event();
770 }
771
772 /***********************************************************/
773 /* RTC reference time/date access */
774 static time_t qemu_ref_timedate(QEMUClockType clock)
775 {
776 time_t value = qemu_clock_get_ms(clock) / 1000;
777 switch (clock) {
778 case QEMU_CLOCK_REALTIME:
779 value -= rtc_realtime_clock_offset;
780 /* no break */
781 case QEMU_CLOCK_VIRTUAL:
782 value += rtc_ref_start_datetime;
783 break;
784 case QEMU_CLOCK_HOST:
785 if (rtc_base_type == RTC_BASE_DATETIME) {
786 value -= rtc_host_datetime_offset;
787 }
788 break;
789 default:
790 assert(0);
791 }
792 return value;
793 }
794
795 void qemu_get_timedate(struct tm *tm, int offset)
796 {
797 time_t ti = qemu_ref_timedate(rtc_clock);
798
799 ti += offset;
800
801 switch (rtc_base_type) {
802 case RTC_BASE_DATETIME:
803 case RTC_BASE_UTC:
804 gmtime_r(&ti, tm);
805 break;
806 case RTC_BASE_LOCALTIME:
807 localtime_r(&ti, tm);
808 break;
809 }
810 }
811
812 int qemu_timedate_diff(struct tm *tm)
813 {
814 time_t seconds;
815
816 switch (rtc_base_type) {
817 case RTC_BASE_DATETIME:
818 case RTC_BASE_UTC:
819 seconds = mktimegm(tm);
820 break;
821 case RTC_BASE_LOCALTIME:
822 {
823 struct tm tmp = *tm;
824 tmp.tm_isdst = -1; /* use timezone to figure it out */
825 seconds = mktime(&tmp);
826 break;
827 }
828 default:
829 abort();
830 }
831
832 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
833 }
834
835 static void configure_rtc_base_datetime(const char *startdate)
836 {
837 time_t rtc_start_datetime;
838 struct tm tm;
839
840 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
841 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
842 /* OK */
843 } else if (sscanf(startdate, "%d-%d-%d",
844 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
845 tm.tm_hour = 0;
846 tm.tm_min = 0;
847 tm.tm_sec = 0;
848 } else {
849 goto date_fail;
850 }
851 tm.tm_year -= 1900;
852 tm.tm_mon--;
853 rtc_start_datetime = mktimegm(&tm);
854 if (rtc_start_datetime == -1) {
855 date_fail:
856 error_report("invalid datetime format");
857 error_printf("valid formats: "
858 "'2006-06-17T16:01:21' or '2006-06-17'\n");
859 exit(1);
860 }
861 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
862 rtc_ref_start_datetime = rtc_start_datetime;
863 }
864
865 static void configure_rtc(QemuOpts *opts)
866 {
867 const char *value;
868
869 /* Set defaults */
870 rtc_clock = QEMU_CLOCK_HOST;
871 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
872 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
873
874 value = qemu_opt_get(opts, "base");
875 if (value) {
876 if (!strcmp(value, "utc")) {
877 rtc_base_type = RTC_BASE_UTC;
878 } else if (!strcmp(value, "localtime")) {
879 Error *blocker = NULL;
880 rtc_base_type = RTC_BASE_LOCALTIME;
881 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
882 "-rtc base=localtime");
883 replay_add_blocker(blocker);
884 } else {
885 rtc_base_type = RTC_BASE_DATETIME;
886 configure_rtc_base_datetime(value);
887 }
888 }
889 value = qemu_opt_get(opts, "clock");
890 if (value) {
891 if (!strcmp(value, "host")) {
892 rtc_clock = QEMU_CLOCK_HOST;
893 } else if (!strcmp(value, "rt")) {
894 rtc_clock = QEMU_CLOCK_REALTIME;
895 } else if (!strcmp(value, "vm")) {
896 rtc_clock = QEMU_CLOCK_VIRTUAL;
897 } else {
898 error_report("invalid option value '%s'", value);
899 exit(1);
900 }
901 }
902 value = qemu_opt_get(opts, "driftfix");
903 if (value) {
904 if (!strcmp(value, "slew")) {
905 static GlobalProperty slew_lost_ticks = {
906 .driver = "mc146818rtc",
907 .property = "lost_tick_policy",
908 .value = "slew",
909 };
910
911 qdev_prop_register_global(&slew_lost_ticks);
912 } else if (!strcmp(value, "none")) {
913 /* discard is default */
914 } else {
915 error_report("invalid option value '%s'", value);
916 exit(1);
917 }
918 }
919 }
920
921 /***********************************************************/
922 /* Bluetooth support */
923 static int nb_hcis;
924 static int cur_hci;
925 static struct HCIInfo *hci_table[MAX_NICS];
926
927 struct HCIInfo *qemu_next_hci(void)
928 {
929 if (cur_hci == nb_hcis)
930 return &null_hci;
931
932 return hci_table[cur_hci++];
933 }
934
935 static int bt_hci_parse(const char *str)
936 {
937 struct HCIInfo *hci;
938 bdaddr_t bdaddr;
939
940 if (nb_hcis >= MAX_NICS) {
941 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
942 return -1;
943 }
944
945 hci = hci_init(str);
946 if (!hci)
947 return -1;
948
949 bdaddr.b[0] = 0x52;
950 bdaddr.b[1] = 0x54;
951 bdaddr.b[2] = 0x00;
952 bdaddr.b[3] = 0x12;
953 bdaddr.b[4] = 0x34;
954 bdaddr.b[5] = 0x56 + nb_hcis;
955 hci->bdaddr_set(hci, bdaddr.b);
956
957 hci_table[nb_hcis++] = hci;
958
959 return 0;
960 }
961
962 static void bt_vhci_add(int vlan_id)
963 {
964 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
965
966 if (!vlan->slave)
967 warn_report("adding a VHCI to an empty scatternet %i",
968 vlan_id);
969
970 bt_vhci_init(bt_new_hci(vlan));
971 }
972
973 static struct bt_device_s *bt_device_add(const char *opt)
974 {
975 struct bt_scatternet_s *vlan;
976 int vlan_id = 0;
977 char *endp = strstr(opt, ",vlan=");
978 int len = (endp ? endp - opt : strlen(opt)) + 1;
979 char devname[10];
980
981 pstrcpy(devname, MIN(sizeof(devname), len), opt);
982
983 if (endp) {
984 vlan_id = strtol(endp + 6, &endp, 0);
985 if (*endp) {
986 error_report("unrecognised bluetooth vlan Id");
987 return 0;
988 }
989 }
990
991 vlan = qemu_find_bt_vlan(vlan_id);
992
993 if (!vlan->slave)
994 warn_report("adding a slave device to an empty scatternet %i",
995 vlan_id);
996
997 if (!strcmp(devname, "keyboard"))
998 return bt_keyboard_init(vlan);
999
1000 error_report("unsupported bluetooth device '%s'", devname);
1001 return 0;
1002 }
1003
1004 static int bt_parse(const char *opt)
1005 {
1006 const char *endp, *p;
1007 int vlan;
1008
1009 if (strstart(opt, "hci", &endp)) {
1010 if (!*endp || *endp == ',') {
1011 if (*endp)
1012 if (!strstart(endp, ",vlan=", 0))
1013 opt = endp + 1;
1014
1015 return bt_hci_parse(opt);
1016 }
1017 } else if (strstart(opt, "vhci", &endp)) {
1018 if (!*endp || *endp == ',') {
1019 if (*endp) {
1020 if (strstart(endp, ",vlan=", &p)) {
1021 vlan = strtol(p, (char **) &endp, 0);
1022 if (*endp) {
1023 error_report("bad scatternet '%s'", p);
1024 return 1;
1025 }
1026 } else {
1027 error_report("bad parameter '%s'", endp + 1);
1028 return 1;
1029 }
1030 } else
1031 vlan = 0;
1032
1033 bt_vhci_add(vlan);
1034 return 0;
1035 }
1036 } else if (strstart(opt, "device:", &endp))
1037 return !bt_device_add(endp);
1038
1039 error_report("bad bluetooth parameter '%s'", opt);
1040 return 1;
1041 }
1042
1043 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1044 {
1045 const char *proc_name;
1046
1047 if (qemu_opt_get(opts, "debug-threads")) {
1048 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1049 }
1050 qemu_name = qemu_opt_get(opts, "guest");
1051
1052 proc_name = qemu_opt_get(opts, "process");
1053 if (proc_name) {
1054 os_set_proc_name(proc_name);
1055 }
1056
1057 return 0;
1058 }
1059
1060 bool defaults_enabled(void)
1061 {
1062 return has_defaults;
1063 }
1064
1065 #ifndef _WIN32
1066 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1067 {
1068 int fd, dupfd, flags;
1069 int64_t fdset_id;
1070 const char *fd_opaque = NULL;
1071 AddfdInfo *fdinfo;
1072
1073 fd = qemu_opt_get_number(opts, "fd", -1);
1074 fdset_id = qemu_opt_get_number(opts, "set", -1);
1075 fd_opaque = qemu_opt_get(opts, "opaque");
1076
1077 if (fd < 0) {
1078 error_setg(errp, "fd option is required and must be non-negative");
1079 return -1;
1080 }
1081
1082 if (fd <= STDERR_FILENO) {
1083 error_setg(errp, "fd cannot be a standard I/O stream");
1084 return -1;
1085 }
1086
1087 /*
1088 * All fds inherited across exec() necessarily have FD_CLOEXEC
1089 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1090 */
1091 flags = fcntl(fd, F_GETFD);
1092 if (flags == -1 || (flags & FD_CLOEXEC)) {
1093 error_setg(errp, "fd is not valid or already in use");
1094 return -1;
1095 }
1096
1097 if (fdset_id < 0) {
1098 error_setg(errp, "set option is required and must be non-negative");
1099 return -1;
1100 }
1101
1102 #ifdef F_DUPFD_CLOEXEC
1103 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1104 #else
1105 dupfd = dup(fd);
1106 if (dupfd != -1) {
1107 qemu_set_cloexec(dupfd);
1108 }
1109 #endif
1110 if (dupfd == -1) {
1111 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1112 return -1;
1113 }
1114
1115 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1116 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1117 &error_abort);
1118 g_free(fdinfo);
1119
1120 return 0;
1121 }
1122
1123 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1124 {
1125 int fd;
1126
1127 fd = qemu_opt_get_number(opts, "fd", -1);
1128 close(fd);
1129
1130 return 0;
1131 }
1132 #endif
1133
1134 /***********************************************************/
1135 /* QEMU Block devices */
1136
1137 #define HD_OPTS "media=disk"
1138 #define CDROM_OPTS "media=cdrom"
1139 #define FD_OPTS ""
1140 #define PFLASH_OPTS ""
1141 #define MTD_OPTS ""
1142 #define SD_OPTS ""
1143
1144 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1145 {
1146 BlockInterfaceType *block_default_type = opaque;
1147
1148 return drive_new(opts, *block_default_type, errp) == NULL;
1149 }
1150
1151 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1152 {
1153 if (qemu_opt_get(opts, "snapshot") == NULL) {
1154 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1155 }
1156 return 0;
1157 }
1158
1159 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1160 int index, const char *optstr)
1161 {
1162 QemuOpts *opts;
1163 DriveInfo *dinfo;
1164
1165 if (!enable || drive_get_by_index(type, index)) {
1166 return;
1167 }
1168
1169 opts = drive_add(type, index, NULL, optstr);
1170 if (snapshot) {
1171 drive_enable_snapshot(NULL, opts, NULL);
1172 }
1173
1174 dinfo = drive_new(opts, type, &error_abort);
1175 dinfo->is_default = true;
1176
1177 }
1178
1179 typedef struct BlockdevOptionsQueueEntry {
1180 BlockdevOptions *bdo;
1181 Location loc;
1182 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1183 } BlockdevOptionsQueueEntry;
1184
1185 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1186
1187 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1188 MachineClass *machine_class, int snapshot)
1189 {
1190 /*
1191 * If the currently selected machine wishes to override the
1192 * units-per-bus property of its default HBA interface type, do so
1193 * now.
1194 */
1195 if (machine_class->units_per_default_bus) {
1196 override_max_devs(machine_class->block_default_type,
1197 machine_class->units_per_default_bus);
1198 }
1199
1200 /* open the virtual block devices */
1201 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1202 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1203
1204 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1205 loc_push_restore(&bdo->loc);
1206 qmp_blockdev_add(bdo->bdo, &error_fatal);
1207 loc_pop(&bdo->loc);
1208 qapi_free_BlockdevOptions(bdo->bdo);
1209 g_free(bdo);
1210 }
1211 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
1212 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1213 NULL, NULL);
1214 }
1215 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1216 &machine_class->block_default_type, &error_fatal)) {
1217 /* We printed help */
1218 exit(0);
1219 }
1220
1221 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1222 CDROM_OPTS);
1223 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1224 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1225
1226 }
1227
1228 static QemuOptsList qemu_smp_opts = {
1229 .name = "smp-opts",
1230 .implied_opt_name = "cpus",
1231 .merge_lists = true,
1232 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1233 .desc = {
1234 {
1235 .name = "cpus",
1236 .type = QEMU_OPT_NUMBER,
1237 }, {
1238 .name = "sockets",
1239 .type = QEMU_OPT_NUMBER,
1240 }, {
1241 .name = "cores",
1242 .type = QEMU_OPT_NUMBER,
1243 }, {
1244 .name = "threads",
1245 .type = QEMU_OPT_NUMBER,
1246 }, {
1247 .name = "maxcpus",
1248 .type = QEMU_OPT_NUMBER,
1249 },
1250 { /*End of list */ }
1251 },
1252 };
1253
1254 static void smp_parse(QemuOpts *opts)
1255 {
1256 if (opts) {
1257 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1258 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1259 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1260 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1261
1262 /* compute missing values, prefer sockets over cores over threads */
1263 if (cpus == 0 || sockets == 0) {
1264 cores = cores > 0 ? cores : 1;
1265 threads = threads > 0 ? threads : 1;
1266 if (cpus == 0) {
1267 sockets = sockets > 0 ? sockets : 1;
1268 cpus = cores * threads * sockets;
1269 } else {
1270 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1271 sockets = max_cpus / (cores * threads);
1272 }
1273 } else if (cores == 0) {
1274 threads = threads > 0 ? threads : 1;
1275 cores = cpus / (sockets * threads);
1276 cores = cores > 0 ? cores : 1;
1277 } else if (threads == 0) {
1278 threads = cpus / (cores * sockets);
1279 threads = threads > 0 ? threads : 1;
1280 } else if (sockets * cores * threads < cpus) {
1281 error_report("cpu topology: "
1282 "sockets (%u) * cores (%u) * threads (%u) < "
1283 "smp_cpus (%u)",
1284 sockets, cores, threads, cpus);
1285 exit(1);
1286 }
1287
1288 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1289
1290 if (max_cpus < cpus) {
1291 error_report("maxcpus must be equal to or greater than smp");
1292 exit(1);
1293 }
1294
1295 if (sockets * cores * threads > max_cpus) {
1296 error_report("cpu topology: "
1297 "sockets (%u) * cores (%u) * threads (%u) > "
1298 "maxcpus (%u)",
1299 sockets, cores, threads, max_cpus);
1300 exit(1);
1301 }
1302
1303 if (sockets * cores * threads != max_cpus) {
1304 warn_report("Invalid CPU topology deprecated: "
1305 "sockets (%u) * cores (%u) * threads (%u) "
1306 "!= maxcpus (%u)",
1307 sockets, cores, threads, max_cpus);
1308 }
1309
1310 smp_cpus = cpus;
1311 smp_cores = cores;
1312 smp_threads = threads;
1313 }
1314
1315 if (smp_cpus > 1) {
1316 Error *blocker = NULL;
1317 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1318 replay_add_blocker(blocker);
1319 }
1320 }
1321
1322 static void realtime_init(void)
1323 {
1324 if (enable_mlock) {
1325 if (os_mlock() < 0) {
1326 error_report("locking memory failed");
1327 exit(1);
1328 }
1329 }
1330 }
1331
1332
1333 static void configure_msg(QemuOpts *opts)
1334 {
1335 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1336 }
1337
1338
1339 /* Now we still need this for compatibility with XEN. */
1340 bool has_igd_gfx_passthru;
1341 static void igd_gfx_passthru(void)
1342 {
1343 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1344 }
1345
1346 /***********************************************************/
1347 /* USB devices */
1348
1349 static int usb_device_add(const char *devname)
1350 {
1351 USBDevice *dev = NULL;
1352
1353 if (!machine_usb(current_machine)) {
1354 return -1;
1355 }
1356
1357 dev = usbdevice_create(devname);
1358 if (!dev)
1359 return -1;
1360
1361 return 0;
1362 }
1363
1364 static int usb_parse(const char *cmdline)
1365 {
1366 int r;
1367 r = usb_device_add(cmdline);
1368 if (r < 0) {
1369 error_report("could not add USB device '%s'", cmdline);
1370 }
1371 return r;
1372 }
1373
1374 /***********************************************************/
1375 /* machine registration */
1376
1377 MachineState *current_machine;
1378
1379 static MachineClass *find_machine(const char *name, GSList *machines)
1380 {
1381 GSList *el;
1382
1383 for (el = machines; el; el = el->next) {
1384 MachineClass *mc = el->data;
1385
1386 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1387 return mc;
1388 }
1389 }
1390
1391 return NULL;
1392 }
1393
1394 static MachineClass *find_default_machine(GSList *machines)
1395 {
1396 GSList *el;
1397
1398 for (el = machines; el; el = el->next) {
1399 MachineClass *mc = el->data;
1400
1401 if (mc->is_default) {
1402 return mc;
1403 }
1404 }
1405
1406 return NULL;
1407 }
1408
1409 MachineInfoList *qmp_query_machines(Error **errp)
1410 {
1411 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1412 MachineInfoList *mach_list = NULL;
1413
1414 for (el = machines; el; el = el->next) {
1415 MachineClass *mc = el->data;
1416 MachineInfoList *entry;
1417 MachineInfo *info;
1418
1419 info = g_malloc0(sizeof(*info));
1420 if (mc->is_default) {
1421 info->has_is_default = true;
1422 info->is_default = true;
1423 }
1424
1425 if (mc->alias) {
1426 info->has_alias = true;
1427 info->alias = g_strdup(mc->alias);
1428 }
1429
1430 info->name = g_strdup(mc->name);
1431 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1432 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1433
1434 entry = g_malloc0(sizeof(*entry));
1435 entry->value = info;
1436 entry->next = mach_list;
1437 mach_list = entry;
1438 }
1439
1440 g_slist_free(machines);
1441 return mach_list;
1442 }
1443
1444 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1445 {
1446 ObjectProperty *prop;
1447 ObjectPropertyIterator iter;
1448
1449 if (!qemu_opt_has_help_opt(opts)) {
1450 return 0;
1451 }
1452
1453 object_property_iter_init(&iter, OBJECT(machine));
1454 while ((prop = object_property_iter_next(&iter))) {
1455 if (!prop->set) {
1456 continue;
1457 }
1458
1459 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1460 prop->name, prop->type);
1461 if (prop->description) {
1462 printf(" (%s)\n", prop->description);
1463 } else {
1464 printf("\n");
1465 }
1466 }
1467
1468 return 1;
1469 }
1470
1471 struct vm_change_state_entry {
1472 VMChangeStateHandler *cb;
1473 void *opaque;
1474 QLIST_ENTRY (vm_change_state_entry) entries;
1475 };
1476
1477 static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head;
1478
1479 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1480 void *opaque)
1481 {
1482 VMChangeStateEntry *e;
1483
1484 e = g_malloc0(sizeof (*e));
1485
1486 e->cb = cb;
1487 e->opaque = opaque;
1488 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1489 return e;
1490 }
1491
1492 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1493 {
1494 QLIST_REMOVE (e, entries);
1495 g_free (e);
1496 }
1497
1498 void vm_state_notify(int running, RunState state)
1499 {
1500 VMChangeStateEntry *e, *next;
1501
1502 trace_vm_state_notify(running, state, RunState_str(state));
1503
1504 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1505 e->cb(e->opaque, running, state);
1506 }
1507 }
1508
1509 static ShutdownCause reset_requested;
1510 static ShutdownCause shutdown_requested;
1511 static int shutdown_signal;
1512 static pid_t shutdown_pid;
1513 static int powerdown_requested;
1514 static int debug_requested;
1515 static int suspend_requested;
1516 static bool preconfig_exit_requested = true;
1517 static WakeupReason wakeup_reason;
1518 static NotifierList powerdown_notifiers =
1519 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1520 static NotifierList suspend_notifiers =
1521 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1522 static NotifierList wakeup_notifiers =
1523 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1524 static NotifierList shutdown_notifiers =
1525 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1526 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1527
1528 ShutdownCause qemu_shutdown_requested_get(void)
1529 {
1530 return shutdown_requested;
1531 }
1532
1533 ShutdownCause qemu_reset_requested_get(void)
1534 {
1535 return reset_requested;
1536 }
1537
1538 static int qemu_shutdown_requested(void)
1539 {
1540 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1541 }
1542
1543 static void qemu_kill_report(void)
1544 {
1545 if (!qtest_driver() && shutdown_signal) {
1546 if (shutdown_pid == 0) {
1547 /* This happens for eg ^C at the terminal, so it's worth
1548 * avoiding printing an odd message in that case.
1549 */
1550 error_report("terminating on signal %d", shutdown_signal);
1551 } else {
1552 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1553
1554 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1555 shutdown_signal, shutdown_pid,
1556 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1557 g_free(shutdown_cmd);
1558 }
1559 shutdown_signal = 0;
1560 }
1561 }
1562
1563 static ShutdownCause qemu_reset_requested(void)
1564 {
1565 ShutdownCause r = reset_requested;
1566
1567 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1568 reset_requested = SHUTDOWN_CAUSE_NONE;
1569 return r;
1570 }
1571 return SHUTDOWN_CAUSE_NONE;
1572 }
1573
1574 static int qemu_suspend_requested(void)
1575 {
1576 int r = suspend_requested;
1577 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1578 suspend_requested = 0;
1579 return r;
1580 }
1581 return false;
1582 }
1583
1584 static WakeupReason qemu_wakeup_requested(void)
1585 {
1586 return wakeup_reason;
1587 }
1588
1589 static int qemu_powerdown_requested(void)
1590 {
1591 int r = powerdown_requested;
1592 powerdown_requested = 0;
1593 return r;
1594 }
1595
1596 static int qemu_debug_requested(void)
1597 {
1598 int r = debug_requested;
1599 debug_requested = 0;
1600 return r;
1601 }
1602
1603 void qemu_exit_preconfig_request(void)
1604 {
1605 preconfig_exit_requested = true;
1606 }
1607
1608 /*
1609 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1610 */
1611 void qemu_system_reset(ShutdownCause reason)
1612 {
1613 MachineClass *mc;
1614
1615 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1616
1617 cpu_synchronize_all_states();
1618
1619 if (mc && mc->reset) {
1620 mc->reset();
1621 } else {
1622 qemu_devices_reset();
1623 }
1624 if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1625 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1626 }
1627 cpu_synchronize_all_post_reset();
1628 }
1629
1630 void qemu_system_guest_panicked(GuestPanicInformation *info)
1631 {
1632 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1633
1634 if (current_cpu) {
1635 current_cpu->crash_occurred = true;
1636 }
1637 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1638 !!info, info);
1639 vm_stop(RUN_STATE_GUEST_PANICKED);
1640 if (!no_shutdown) {
1641 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1642 !!info, info);
1643 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1644 }
1645
1646 if (info) {
1647 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1648 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1649 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1650 info->u.hyper_v.arg1,
1651 info->u.hyper_v.arg2,
1652 info->u.hyper_v.arg3,
1653 info->u.hyper_v.arg4,
1654 info->u.hyper_v.arg5);
1655 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1656 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1657 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1658 info->u.s390.core,
1659 S390CrashReason_str(info->u.s390.reason),
1660 info->u.s390.psw_mask,
1661 info->u.s390.psw_addr);
1662 }
1663 qapi_free_GuestPanicInformation(info);
1664 }
1665 }
1666
1667 void qemu_system_reset_request(ShutdownCause reason)
1668 {
1669 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1670 shutdown_requested = reason;
1671 } else {
1672 reset_requested = reason;
1673 }
1674 cpu_stop_current();
1675 qemu_notify_event();
1676 }
1677
1678 static void qemu_system_suspend(void)
1679 {
1680 pause_all_vcpus();
1681 notifier_list_notify(&suspend_notifiers, NULL);
1682 runstate_set(RUN_STATE_SUSPENDED);
1683 qapi_event_send_suspend();
1684 }
1685
1686 void qemu_system_suspend_request(void)
1687 {
1688 if (runstate_check(RUN_STATE_SUSPENDED)) {
1689 return;
1690 }
1691 suspend_requested = 1;
1692 cpu_stop_current();
1693 qemu_notify_event();
1694 }
1695
1696 void qemu_register_suspend_notifier(Notifier *notifier)
1697 {
1698 notifier_list_add(&suspend_notifiers, notifier);
1699 }
1700
1701 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1702 {
1703 trace_system_wakeup_request(reason);
1704
1705 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1706 error_setg(errp,
1707 "Unable to wake up: guest is not in suspended state");
1708 return;
1709 }
1710 if (!(wakeup_reason_mask & (1 << reason))) {
1711 return;
1712 }
1713 runstate_set(RUN_STATE_RUNNING);
1714 wakeup_reason = reason;
1715 qemu_notify_event();
1716 }
1717
1718 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1719 {
1720 if (enabled) {
1721 wakeup_reason_mask |= (1 << reason);
1722 } else {
1723 wakeup_reason_mask &= ~(1 << reason);
1724 }
1725 }
1726
1727 void qemu_register_wakeup_notifier(Notifier *notifier)
1728 {
1729 notifier_list_add(&wakeup_notifiers, notifier);
1730 }
1731
1732 void qemu_register_wakeup_support(void)
1733 {
1734 wakeup_suspend_enabled = true;
1735 }
1736
1737 bool qemu_wakeup_suspend_enabled(void)
1738 {
1739 return wakeup_suspend_enabled;
1740 }
1741
1742 CurrentMachineParams *qmp_query_current_machine(Error **errp)
1743 {
1744 CurrentMachineParams *params = g_malloc0(sizeof(*params));
1745 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
1746
1747 return params;
1748 }
1749
1750 void qemu_system_killed(int signal, pid_t pid)
1751 {
1752 shutdown_signal = signal;
1753 shutdown_pid = pid;
1754 no_shutdown = 0;
1755
1756 /* Cannot call qemu_system_shutdown_request directly because
1757 * we are in a signal handler.
1758 */
1759 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1760 qemu_notify_event();
1761 }
1762
1763 void qemu_system_shutdown_request(ShutdownCause reason)
1764 {
1765 trace_qemu_system_shutdown_request(reason);
1766 replay_shutdown_request(reason);
1767 shutdown_requested = reason;
1768 qemu_notify_event();
1769 }
1770
1771 static void qemu_system_powerdown(void)
1772 {
1773 qapi_event_send_powerdown();
1774 notifier_list_notify(&powerdown_notifiers, NULL);
1775 }
1776
1777 static void qemu_system_shutdown(ShutdownCause cause)
1778 {
1779 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1780 notifier_list_notify(&shutdown_notifiers, &cause);
1781 }
1782
1783 void qemu_system_powerdown_request(void)
1784 {
1785 trace_qemu_system_powerdown_request();
1786 powerdown_requested = 1;
1787 qemu_notify_event();
1788 }
1789
1790 void qemu_register_powerdown_notifier(Notifier *notifier)
1791 {
1792 notifier_list_add(&powerdown_notifiers, notifier);
1793 }
1794
1795 void qemu_register_shutdown_notifier(Notifier *notifier)
1796 {
1797 notifier_list_add(&shutdown_notifiers, notifier);
1798 }
1799
1800 void qemu_system_debug_request(void)
1801 {
1802 debug_requested = 1;
1803 qemu_notify_event();
1804 }
1805
1806 static bool main_loop_should_exit(void)
1807 {
1808 RunState r;
1809 ShutdownCause request;
1810
1811 if (preconfig_exit_requested) {
1812 if (runstate_check(RUN_STATE_PRECONFIG)) {
1813 runstate_set(RUN_STATE_PRELAUNCH);
1814 }
1815 preconfig_exit_requested = false;
1816 return true;
1817 }
1818 if (qemu_debug_requested()) {
1819 vm_stop(RUN_STATE_DEBUG);
1820 }
1821 if (qemu_suspend_requested()) {
1822 qemu_system_suspend();
1823 }
1824 request = qemu_shutdown_requested();
1825 if (request) {
1826 qemu_kill_report();
1827 qemu_system_shutdown(request);
1828 if (no_shutdown) {
1829 vm_stop(RUN_STATE_SHUTDOWN);
1830 } else {
1831 return true;
1832 }
1833 }
1834 request = qemu_reset_requested();
1835 if (request) {
1836 pause_all_vcpus();
1837 qemu_system_reset(request);
1838 resume_all_vcpus();
1839 if (!runstate_check(RUN_STATE_RUNNING) &&
1840 !runstate_check(RUN_STATE_INMIGRATE)) {
1841 runstate_set(RUN_STATE_PRELAUNCH);
1842 }
1843 }
1844 if (qemu_wakeup_requested()) {
1845 pause_all_vcpus();
1846 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1847 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1848 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1849 resume_all_vcpus();
1850 qapi_event_send_wakeup();
1851 }
1852 if (qemu_powerdown_requested()) {
1853 qemu_system_powerdown();
1854 }
1855 if (qemu_vmstop_requested(&r)) {
1856 vm_stop(r);
1857 }
1858 return false;
1859 }
1860
1861 static void main_loop(void)
1862 {
1863 #ifdef CONFIG_PROFILER
1864 int64_t ti;
1865 #endif
1866 while (!main_loop_should_exit()) {
1867 #ifdef CONFIG_PROFILER
1868 ti = profile_getclock();
1869 #endif
1870 main_loop_wait(false);
1871 #ifdef CONFIG_PROFILER
1872 dev_time += profile_getclock() - ti;
1873 #endif
1874 }
1875 }
1876
1877 static void version(void)
1878 {
1879 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1880 QEMU_COPYRIGHT "\n");
1881 }
1882
1883 static void help(int exitcode)
1884 {
1885 version();
1886 printf("usage: %s [options] [disk_image]\n\n"
1887 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1888 error_get_progname());
1889
1890 #define QEMU_OPTIONS_GENERATE_HELP
1891 #include "qemu-options-wrapper.h"
1892
1893 printf("\nDuring emulation, the following keys are useful:\n"
1894 "ctrl-alt-f toggle full screen\n"
1895 "ctrl-alt-n switch to virtual console 'n'\n"
1896 "ctrl-alt toggle mouse and keyboard grab\n"
1897 "\n"
1898 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1899 "\n"
1900 QEMU_HELP_BOTTOM "\n");
1901
1902 exit(exitcode);
1903 }
1904
1905 #define HAS_ARG 0x0001
1906
1907 typedef struct QEMUOption {
1908 const char *name;
1909 int flags;
1910 int index;
1911 uint32_t arch_mask;
1912 } QEMUOption;
1913
1914 static const QEMUOption qemu_options[] = {
1915 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1916 #define QEMU_OPTIONS_GENERATE_OPTIONS
1917 #include "qemu-options-wrapper.h"
1918 { NULL },
1919 };
1920
1921 typedef struct VGAInterfaceInfo {
1922 const char *opt_name; /* option name */
1923 const char *name; /* human-readable name */
1924 /* Class names indicating that support is available.
1925 * If no class is specified, the interface is always available */
1926 const char *class_names[2];
1927 } VGAInterfaceInfo;
1928
1929 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1930 [VGA_NONE] = {
1931 .opt_name = "none",
1932 .name = "no graphic card",
1933 },
1934 [VGA_STD] = {
1935 .opt_name = "std",
1936 .name = "standard VGA",
1937 .class_names = { "VGA", "isa-vga" },
1938 },
1939 [VGA_CIRRUS] = {
1940 .opt_name = "cirrus",
1941 .name = "Cirrus VGA",
1942 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1943 },
1944 [VGA_VMWARE] = {
1945 .opt_name = "vmware",
1946 .name = "VMWare SVGA",
1947 .class_names = { "vmware-svga" },
1948 },
1949 [VGA_VIRTIO] = {
1950 .opt_name = "virtio",
1951 .name = "Virtio VGA",
1952 .class_names = { "virtio-vga" },
1953 },
1954 [VGA_QXL] = {
1955 .opt_name = "qxl",
1956 .name = "QXL VGA",
1957 .class_names = { "qxl-vga" },
1958 },
1959 [VGA_TCX] = {
1960 .opt_name = "tcx",
1961 .name = "TCX framebuffer",
1962 .class_names = { "SUNW,tcx" },
1963 },
1964 [VGA_CG3] = {
1965 .opt_name = "cg3",
1966 .name = "CG3 framebuffer",
1967 .class_names = { "cgthree" },
1968 },
1969 [VGA_XENFB] = {
1970 .opt_name = "xenfb",
1971 .name = "Xen paravirtualized framebuffer",
1972 },
1973 };
1974
1975 static bool vga_interface_available(VGAInterfaceType t)
1976 {
1977 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1978
1979 assert(t < VGA_TYPE_MAX);
1980 return !ti->class_names[0] ||
1981 object_class_by_name(ti->class_names[0]) ||
1982 object_class_by_name(ti->class_names[1]);
1983 }
1984
1985 static const char *
1986 get_default_vga_model(const MachineClass *machine_class)
1987 {
1988 if (machine_class->default_display) {
1989 return machine_class->default_display;
1990 } else if (vga_interface_available(VGA_CIRRUS)) {
1991 return "cirrus";
1992 } else if (vga_interface_available(VGA_STD)) {
1993 return "std";
1994 }
1995
1996 return NULL;
1997 }
1998
1999 static void select_vgahw(const MachineClass *machine_class, const char *p)
2000 {
2001 const char *opts;
2002 int t;
2003
2004 if (g_str_equal(p, "help")) {
2005 const char *def = get_default_vga_model(machine_class);
2006
2007 for (t = 0; t < VGA_TYPE_MAX; t++) {
2008 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2009
2010 if (vga_interface_available(t) && ti->opt_name) {
2011 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
2012 g_str_equal(ti->opt_name, def) ? " (default)" : "");
2013 }
2014 }
2015 exit(0);
2016 }
2017
2018 assert(vga_interface_type == VGA_NONE);
2019 for (t = 0; t < VGA_TYPE_MAX; t++) {
2020 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2021 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2022 if (!vga_interface_available(t)) {
2023 error_report("%s not available", ti->name);
2024 exit(1);
2025 }
2026 vga_interface_type = t;
2027 break;
2028 }
2029 }
2030 if (t == VGA_TYPE_MAX) {
2031 invalid_vga:
2032 error_report("unknown vga type: %s", p);
2033 exit(1);
2034 }
2035 while (*opts) {
2036 const char *nextopt;
2037
2038 if (strstart(opts, ",retrace=", &nextopt)) {
2039 opts = nextopt;
2040 if (strstart(opts, "dumb", &nextopt))
2041 vga_retrace_method = VGA_RETRACE_DUMB;
2042 else if (strstart(opts, "precise", &nextopt))
2043 vga_retrace_method = VGA_RETRACE_PRECISE;
2044 else goto invalid_vga;
2045 } else goto invalid_vga;
2046 opts = nextopt;
2047 }
2048 }
2049
2050 static void parse_display_qapi(const char *optarg)
2051 {
2052 DisplayOptions *opts;
2053 Visitor *v;
2054
2055 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2056
2057 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2058 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2059
2060 qapi_free_DisplayOptions(opts);
2061 visit_free(v);
2062 }
2063
2064 DisplayOptions *qmp_query_display_options(Error **errp)
2065 {
2066 return QAPI_CLONE(DisplayOptions, &dpy);
2067 }
2068
2069 static void parse_display(const char *p)
2070 {
2071 const char *opts;
2072
2073 if (strstart(p, "sdl", &opts)) {
2074 /*
2075 * sdl DisplayType needs hand-crafted parser instead of
2076 * parse_display_qapi() due to some options not in
2077 * DisplayOptions, specifically:
2078 * - frame
2079 * Already deprecated.
2080 * - ctrl_grab + alt_grab
2081 * Not clear yet what happens to them long-term. Should
2082 * replaced by something better or deprecated and dropped.
2083 */
2084 dpy.type = DISPLAY_TYPE_SDL;
2085 while (*opts) {
2086 const char *nextopt;
2087
2088 if (strstart(opts, ",alt_grab=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "on", &nextopt)) {
2091 alt_grab = 1;
2092 } else if (strstart(opts, "off", &nextopt)) {
2093 alt_grab = 0;
2094 } else {
2095 goto invalid_sdl_args;
2096 }
2097 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2098 opts = nextopt;
2099 if (strstart(opts, "on", &nextopt)) {
2100 ctrl_grab = 1;
2101 } else if (strstart(opts, "off", &nextopt)) {
2102 ctrl_grab = 0;
2103 } else {
2104 goto invalid_sdl_args;
2105 }
2106 } else if (strstart(opts, ",window_close=", &nextopt)) {
2107 opts = nextopt;
2108 dpy.has_window_close = true;
2109 if (strstart(opts, "on", &nextopt)) {
2110 dpy.window_close = true;
2111 } else if (strstart(opts, "off", &nextopt)) {
2112 dpy.window_close = false;
2113 } else {
2114 goto invalid_sdl_args;
2115 }
2116 } else if (strstart(opts, ",gl=", &nextopt)) {
2117 opts = nextopt;
2118 dpy.has_gl = true;
2119 if (strstart(opts, "on", &nextopt)) {
2120 dpy.gl = DISPLAYGL_MODE_ON;
2121 } else if (strstart(opts, "core", &nextopt)) {
2122 dpy.gl = DISPLAYGL_MODE_CORE;
2123 } else if (strstart(opts, "es", &nextopt)) {
2124 dpy.gl = DISPLAYGL_MODE_ES;
2125 } else if (strstart(opts, "off", &nextopt)) {
2126 dpy.gl = DISPLAYGL_MODE_OFF;
2127 } else {
2128 goto invalid_sdl_args;
2129 }
2130 } else {
2131 invalid_sdl_args:
2132 error_report("invalid SDL option string");
2133 exit(1);
2134 }
2135 opts = nextopt;
2136 }
2137 } else if (strstart(p, "vnc", &opts)) {
2138 /*
2139 * vnc isn't a (local) DisplayType but a protocol for remote
2140 * display access.
2141 */
2142 if (*opts == '=') {
2143 vnc_parse(opts + 1, &error_fatal);
2144 } else {
2145 error_report("VNC requires a display argument vnc=<display>");
2146 exit(1);
2147 }
2148 } else {
2149 parse_display_qapi(p);
2150 }
2151 }
2152
2153 char *qemu_find_file(int type, const char *name)
2154 {
2155 int i;
2156 const char *subdir;
2157 char *buf;
2158
2159 /* Try the name as a straight path first */
2160 if (access(name, R_OK) == 0) {
2161 trace_load_file(name, name);
2162 return g_strdup(name);
2163 }
2164
2165 switch (type) {
2166 case QEMU_FILE_TYPE_BIOS:
2167 subdir = "";
2168 break;
2169 case QEMU_FILE_TYPE_KEYMAP:
2170 subdir = "keymaps/";
2171 break;
2172 default:
2173 abort();
2174 }
2175
2176 for (i = 0; i < data_dir_idx; i++) {
2177 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2178 if (access(buf, R_OK) == 0) {
2179 trace_load_file(name, buf);
2180 return buf;
2181 }
2182 g_free(buf);
2183 }
2184 return NULL;
2185 }
2186
2187 static void qemu_add_data_dir(const char *path)
2188 {
2189 int i;
2190
2191 if (path == NULL) {
2192 return;
2193 }
2194 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2195 return;
2196 }
2197 for (i = 0; i < data_dir_idx; i++) {
2198 if (strcmp(data_dir[i], path) == 0) {
2199 return; /* duplicate */
2200 }
2201 }
2202 data_dir[data_dir_idx++] = g_strdup(path);
2203 }
2204
2205 static inline bool nonempty_str(const char *str)
2206 {
2207 return str && *str;
2208 }
2209
2210 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2211 {
2212 gchar *buf;
2213 size_t size;
2214 const char *name, *file, *str;
2215 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2216
2217 if (fw_cfg == NULL) {
2218 error_setg(errp, "fw_cfg device not available");
2219 return -1;
2220 }
2221 name = qemu_opt_get(opts, "name");
2222 file = qemu_opt_get(opts, "file");
2223 str = qemu_opt_get(opts, "string");
2224
2225 /* we need name and either a file or the content string */
2226 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2227 error_setg(errp, "invalid argument(s)");
2228 return -1;
2229 }
2230 if (nonempty_str(file) && nonempty_str(str)) {
2231 error_setg(errp, "file and string are mutually exclusive");
2232 return -1;
2233 }
2234 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2235 error_setg(errp, "name too long (max. %d char)",
2236 FW_CFG_MAX_FILE_PATH - 1);
2237 return -1;
2238 }
2239 if (strncmp(name, "opt/", 4) != 0) {
2240 warn_report("externally provided fw_cfg item names "
2241 "should be prefixed with \"opt/\"");
2242 }
2243 if (nonempty_str(str)) {
2244 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2245 buf = g_memdup(str, size);
2246 } else {
2247 GError *err = NULL;
2248 if (!g_file_get_contents(file, &buf, &size, &err)) {
2249 error_setg(errp, "can't load %s: %s", file, err->message);
2250 g_error_free(err);
2251 return -1;
2252 }
2253 }
2254 /* For legacy, keep user files in a specific global order. */
2255 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2256 fw_cfg_add_file(fw_cfg, name, buf, size);
2257 fw_cfg_reset_order_override(fw_cfg);
2258 return 0;
2259 }
2260
2261 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2262 {
2263 return qdev_device_help(opts);
2264 }
2265
2266 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2267 {
2268 DeviceState *dev;
2269
2270 dev = qdev_device_add(opts, errp);
2271 if (!dev) {
2272 return -1;
2273 }
2274 object_unref(OBJECT(dev));
2275 return 0;
2276 }
2277
2278 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2279 {
2280 Error *local_err = NULL;
2281
2282 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2283 if (local_err) {
2284 error_propagate(errp, local_err);
2285 return -1;
2286 }
2287 exit(0);
2288 }
2289 return 0;
2290 }
2291
2292 #ifdef CONFIG_VIRTFS
2293 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2294 {
2295 return qemu_fsdev_add(opts, errp);
2296 }
2297 #endif
2298
2299 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2300 {
2301 Chardev *chr;
2302 bool qmp;
2303 bool pretty = false;
2304 const char *chardev;
2305 const char *mode;
2306
2307 mode = qemu_opt_get(opts, "mode");
2308 if (mode == NULL) {
2309 mode = "readline";
2310 }
2311 if (strcmp(mode, "readline") == 0) {
2312 qmp = false;
2313 } else if (strcmp(mode, "control") == 0) {
2314 qmp = true;
2315 } else {
2316 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2317 return -1;
2318 }
2319
2320 if (qemu_opt_get_bool(opts, "pretty", 0)) {
2321 pretty = true;
2322 }
2323
2324 chardev = qemu_opt_get(opts, "chardev");
2325 if (!chardev) {
2326 error_report("chardev is required");
2327 exit(1);
2328 }
2329 chr = qemu_chr_find(chardev);
2330 if (chr == NULL) {
2331 error_setg(errp, "chardev \"%s\" not found", chardev);
2332 return -1;
2333 }
2334
2335 if (qmp) {
2336 monitor_init_qmp(chr, pretty);
2337 } else {
2338 monitor_init_hmp(chr, true);
2339 }
2340 return 0;
2341 }
2342
2343 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2344 {
2345 static int monitor_device_index = 0;
2346 QemuOpts *opts;
2347 const char *p;
2348 char label[32];
2349
2350 if (strstart(optarg, "chardev:", &p)) {
2351 snprintf(label, sizeof(label), "%s", p);
2352 } else {
2353 snprintf(label, sizeof(label), "compat_monitor%d",
2354 monitor_device_index);
2355 opts = qemu_chr_parse_compat(label, optarg, true);
2356 if (!opts) {
2357 error_report("parse error: %s", optarg);
2358 exit(1);
2359 }
2360 }
2361
2362 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2363 qemu_opt_set(opts, "mode", mode, &error_abort);
2364 qemu_opt_set(opts, "chardev", label, &error_abort);
2365 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2366 monitor_device_index++;
2367 }
2368
2369 struct device_config {
2370 enum {
2371 DEV_USB, /* -usbdevice */
2372 DEV_BT, /* -bt */
2373 DEV_SERIAL, /* -serial */
2374 DEV_PARALLEL, /* -parallel */
2375 DEV_DEBUGCON, /* -debugcon */
2376 DEV_GDB, /* -gdb, -s */
2377 DEV_SCLP, /* s390 sclp */
2378 } type;
2379 const char *cmdline;
2380 Location loc;
2381 QTAILQ_ENTRY(device_config) next;
2382 };
2383
2384 static QTAILQ_HEAD(, device_config) device_configs =
2385 QTAILQ_HEAD_INITIALIZER(device_configs);
2386
2387 static void add_device_config(int type, const char *cmdline)
2388 {
2389 struct device_config *conf;
2390
2391 conf = g_malloc0(sizeof(*conf));
2392 conf->type = type;
2393 conf->cmdline = cmdline;
2394 loc_save(&conf->loc);
2395 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2396 }
2397
2398 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2399 {
2400 struct device_config *conf;
2401 int rc;
2402
2403 QTAILQ_FOREACH(conf, &device_configs, next) {
2404 if (conf->type != type)
2405 continue;
2406 loc_push_restore(&conf->loc);
2407 rc = func(conf->cmdline);
2408 loc_pop(&conf->loc);
2409 if (rc) {
2410 return rc;
2411 }
2412 }
2413 return 0;
2414 }
2415
2416 static int serial_parse(const char *devname)
2417 {
2418 int index = num_serial_hds;
2419 char label[32];
2420
2421 if (strcmp(devname, "none") == 0)
2422 return 0;
2423 snprintf(label, sizeof(label), "serial%d", index);
2424 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2425
2426 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2427 if (!serial_hds[index]) {
2428 error_report("could not connect serial device"
2429 " to character backend '%s'", devname);
2430 return -1;
2431 }
2432 num_serial_hds++;
2433 return 0;
2434 }
2435
2436 Chardev *serial_hd(int i)
2437 {
2438 assert(i >= 0);
2439 if (i < num_serial_hds) {
2440 return serial_hds[i];
2441 }
2442 return NULL;
2443 }
2444
2445 int serial_max_hds(void)
2446 {
2447 return num_serial_hds;
2448 }
2449
2450 static int parallel_parse(const char *devname)
2451 {
2452 static int index = 0;
2453 char label[32];
2454
2455 if (strcmp(devname, "none") == 0)
2456 return 0;
2457 if (index == MAX_PARALLEL_PORTS) {
2458 error_report("too many parallel ports");
2459 exit(1);
2460 }
2461 snprintf(label, sizeof(label), "parallel%d", index);
2462 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2463 if (!parallel_hds[index]) {
2464 error_report("could not connect parallel device"
2465 " to character backend '%s'", devname);
2466 return -1;
2467 }
2468 index++;
2469 return 0;
2470 }
2471
2472 static int debugcon_parse(const char *devname)
2473 {
2474 QemuOpts *opts;
2475
2476 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2477 error_report("invalid character backend '%s'", devname);
2478 exit(1);
2479 }
2480 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2481 if (!opts) {
2482 error_report("already have a debugcon device");
2483 exit(1);
2484 }
2485 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2486 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2487 return 0;
2488 }
2489
2490 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2491 {
2492 const MachineClass *mc1 = a, *mc2 = b;
2493 int res;
2494
2495 if (mc1->family == NULL) {
2496 if (mc2->family == NULL) {
2497 /* Compare standalone machine types against each other; they sort
2498 * in increasing order.
2499 */
2500 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2501 object_class_get_name(OBJECT_CLASS(mc2)));
2502 }
2503
2504 /* Standalone machine types sort after families. */
2505 return 1;
2506 }
2507
2508 if (mc2->family == NULL) {
2509 /* Families sort before standalone machine types. */
2510 return -1;
2511 }
2512
2513 /* Families sort between each other alphabetically increasingly. */
2514 res = strcmp(mc1->family, mc2->family);
2515 if (res != 0) {
2516 return res;
2517 }
2518
2519 /* Within the same family, machine types sort in decreasing order. */
2520 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2521 object_class_get_name(OBJECT_CLASS(mc1)));
2522 }
2523
2524 static MachineClass *machine_parse(const char *name, GSList *machines)
2525 {
2526 MachineClass *mc;
2527 GSList *el;
2528
2529 if (is_help_option(name)) {
2530 printf("Supported machines are:\n");
2531 machines = g_slist_sort(machines, machine_class_cmp);
2532 for (el = machines; el; el = el->next) {
2533 MachineClass *mc = el->data;
2534 if (mc->alias) {
2535 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2536 }
2537 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2538 mc->is_default ? " (default)" : "",
2539 mc->deprecation_reason ? " (deprecated)" : "");
2540 }
2541 exit(0);
2542 }
2543
2544 mc = find_machine(name, machines);
2545 if (!mc) {
2546 error_report("unsupported machine type");
2547 error_printf("Use -machine help to list supported machines\n");
2548 exit(1);
2549 }
2550 return mc;
2551 }
2552
2553 void qemu_add_exit_notifier(Notifier *notify)
2554 {
2555 notifier_list_add(&exit_notifiers, notify);
2556 }
2557
2558 void qemu_remove_exit_notifier(Notifier *notify)
2559 {
2560 notifier_remove(notify);
2561 }
2562
2563 static void qemu_run_exit_notifiers(void)
2564 {
2565 notifier_list_notify(&exit_notifiers, NULL);
2566 }
2567
2568 static const char *pid_file;
2569 static Notifier qemu_unlink_pidfile_notifier;
2570
2571 static void qemu_unlink_pidfile(Notifier *n, void *data)
2572 {
2573 if (pid_file) {
2574 unlink(pid_file);
2575 }
2576 }
2577
2578 bool machine_init_done;
2579
2580 void qemu_add_machine_init_done_notifier(Notifier *notify)
2581 {
2582 notifier_list_add(&machine_init_done_notifiers, notify);
2583 if (machine_init_done) {
2584 notify->notify(notify, NULL);
2585 }
2586 }
2587
2588 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2589 {
2590 notifier_remove(notify);
2591 }
2592
2593 static void qemu_run_machine_init_done_notifiers(void)
2594 {
2595 machine_init_done = true;
2596 notifier_list_notify(&machine_init_done_notifiers, NULL);
2597 }
2598
2599 static const QEMUOption *lookup_opt(int argc, char **argv,
2600 const char **poptarg, int *poptind)
2601 {
2602 const QEMUOption *popt;
2603 int optind = *poptind;
2604 char *r = argv[optind];
2605 const char *optarg;
2606
2607 loc_set_cmdline(argv, optind, 1);
2608 optind++;
2609 /* Treat --foo the same as -foo. */
2610 if (r[1] == '-')
2611 r++;
2612 popt = qemu_options;
2613 for(;;) {
2614 if (!popt->name) {
2615 error_report("invalid option");
2616 exit(1);
2617 }
2618 if (!strcmp(popt->name, r + 1))
2619 break;
2620 popt++;
2621 }
2622 if (popt->flags & HAS_ARG) {
2623 if (optind >= argc) {
2624 error_report("requires an argument");
2625 exit(1);
2626 }
2627 optarg = argv[optind++];
2628 loc_set_cmdline(argv, optind - 2, 2);
2629 } else {
2630 optarg = NULL;
2631 }
2632
2633 *poptarg = optarg;
2634 *poptind = optind;
2635
2636 return popt;
2637 }
2638
2639 static MachineClass *select_machine(void)
2640 {
2641 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2642 MachineClass *machine_class = find_default_machine(machines);
2643 const char *optarg;
2644 QemuOpts *opts;
2645 Location loc;
2646
2647 loc_push_none(&loc);
2648
2649 opts = qemu_get_machine_opts();
2650 qemu_opts_loc_restore(opts);
2651
2652 optarg = qemu_opt_get(opts, "type");
2653 if (optarg) {
2654 machine_class = machine_parse(optarg, machines);
2655 }
2656
2657 if (!machine_class) {
2658 error_report("No machine specified, and there is no default");
2659 error_printf("Use -machine help to list supported machines\n");
2660 exit(1);
2661 }
2662
2663 loc_pop(&loc);
2664 g_slist_free(machines);
2665 return machine_class;
2666 }
2667
2668 static int machine_set_property(void *opaque,
2669 const char *name, const char *value,
2670 Error **errp)
2671 {
2672 Object *obj = OBJECT(opaque);
2673 Error *local_err = NULL;
2674 char *p, *qom_name;
2675
2676 if (strcmp(name, "type") == 0) {
2677 return 0;
2678 }
2679
2680 qom_name = g_strdup(name);
2681 for (p = qom_name; *p; p++) {
2682 if (*p == '_') {
2683 *p = '-';
2684 }
2685 }
2686
2687 object_property_parse(obj, value, qom_name, &local_err);
2688 g_free(qom_name);
2689
2690 if (local_err) {
2691 error_propagate(errp, local_err);
2692 return -1;
2693 }
2694
2695 return 0;
2696 }
2697
2698
2699 /*
2700 * Initial object creation happens before all other
2701 * QEMU data types are created. The majority of objects
2702 * can be created at this point. The rng-egd object
2703 * cannot be created here, as it depends on the chardev
2704 * already existing.
2705 */
2706 static bool object_create_initial(const char *type, QemuOpts *opts)
2707 {
2708 ObjectClass *klass;
2709
2710 if (is_help_option(type)) {
2711 GSList *l, *list;
2712
2713 printf("List of user creatable objects:\n");
2714 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
2715 for (l = list; l != NULL; l = l->next) {
2716 ObjectClass *oc = OBJECT_CLASS(l->data);
2717 printf(" %s\n", object_class_get_name(oc));
2718 }
2719 g_slist_free(list);
2720 exit(0);
2721 }
2722
2723 klass = object_class_by_name(type);
2724 if (klass && qemu_opt_has_help_opt(opts)) {
2725 ObjectPropertyIterator iter;
2726 ObjectProperty *prop;
2727 GPtrArray *array = g_ptr_array_new();
2728 int i;
2729
2730 object_class_property_iter_init(&iter, klass);
2731 while ((prop = object_property_iter_next(&iter))) {
2732 GString *str;
2733
2734 if (!prop->set) {
2735 continue;
2736 }
2737
2738 str = g_string_new(NULL);
2739 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
2740 if (prop->description) {
2741 if (str->len < 24) {
2742 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
2743 }
2744 g_string_append_printf(str, " - %s", prop->description);
2745 }
2746 g_ptr_array_add(array, g_string_free(str, false));
2747 }
2748 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
2749 if (array->len > 0) {
2750 printf("%s options:\n", type);
2751 } else {
2752 printf("There are no options for %s.\n", type);
2753 }
2754 for (i = 0; i < array->len; i++) {
2755 printf("%s\n", (char *)array->pdata[i]);
2756 }
2757 g_ptr_array_set_free_func(array, g_free);
2758 g_ptr_array_free(array, true);
2759 exit(0);
2760 }
2761
2762 /*
2763 * Objects should not be made "delayed" without a reason. If you
2764 * add one, state the reason in a comment!
2765 */
2766
2767 /* Reason: rng-egd property "chardev" */
2768 if (g_str_equal(type, "rng-egd")) {
2769 return false;
2770 }
2771
2772 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2773 /* Reason: cryptodev-vhost-user property "chardev" */
2774 if (g_str_equal(type, "cryptodev-vhost-user")) {
2775 return false;
2776 }
2777 #endif
2778
2779 /*
2780 * Reason: filter-* property "netdev" etc.
2781 */
2782 if (g_str_equal(type, "filter-buffer") ||
2783 g_str_equal(type, "filter-dump") ||
2784 g_str_equal(type, "filter-mirror") ||
2785 g_str_equal(type, "filter-redirector") ||
2786 g_str_equal(type, "colo-compare") ||
2787 g_str_equal(type, "filter-rewriter") ||
2788 g_str_equal(type, "filter-replay")) {
2789 return false;
2790 }
2791
2792 /* Memory allocation by backends needs to be done
2793 * after configure_accelerator() (due to the tcg_enabled()
2794 * checks at memory_region_init_*()).
2795 *
2796 * Also, allocation of large amounts of memory may delay
2797 * chardev initialization for too long, and trigger timeouts
2798 * on software that waits for a monitor socket to be created
2799 * (e.g. libvirt).
2800 */
2801 if (g_str_has_prefix(type, "memory-backend-")) {
2802 return false;
2803 }
2804
2805 return true;
2806 }
2807
2808
2809 /*
2810 * The remainder of object creation happens after the
2811 * creation of chardev, fsdev, net clients and device data types.
2812 */
2813 static bool object_create_delayed(const char *type, QemuOpts *opts)
2814 {
2815 return !object_create_initial(type, opts);
2816 }
2817
2818
2819 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2820 MachineClass *mc)
2821 {
2822 uint64_t sz;
2823 const char *mem_str;
2824 const ram_addr_t default_ram_size = mc->default_ram_size;
2825 QemuOpts *opts = qemu_find_opts_singleton("memory");
2826 Location loc;
2827
2828 loc_push_none(&loc);
2829 qemu_opts_loc_restore(opts);
2830
2831 sz = 0;
2832 mem_str = qemu_opt_get(opts, "size");
2833 if (mem_str) {
2834 if (!*mem_str) {
2835 error_report("missing 'size' option value");
2836 exit(EXIT_FAILURE);
2837 }
2838
2839 sz = qemu_opt_get_size(opts, "size", ram_size);
2840
2841 /* Fix up legacy suffix-less format */
2842 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2843 uint64_t overflow_check = sz;
2844
2845 sz *= MiB;
2846 if (sz / MiB != overflow_check) {
2847 error_report("too large 'size' option value");
2848 exit(EXIT_FAILURE);
2849 }
2850 }
2851 }
2852
2853 /* backward compatibility behaviour for case "-m 0" */
2854 if (sz == 0) {
2855 sz = default_ram_size;
2856 }
2857
2858 sz = QEMU_ALIGN_UP(sz, 8192);
2859 ram_size = sz;
2860 if (ram_size != sz) {
2861 error_report("ram size too large");
2862 exit(EXIT_FAILURE);
2863 }
2864
2865 /* store value for the future use */
2866 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2867 *maxram_size = ram_size;
2868
2869 if (qemu_opt_get(opts, "maxmem")) {
2870 uint64_t slots;
2871
2872 sz = qemu_opt_get_size(opts, "maxmem", 0);
2873 slots = qemu_opt_get_number(opts, "slots", 0);
2874 if (sz < ram_size) {
2875 error_report("invalid value of -m option maxmem: "
2876 "maximum memory size (0x%" PRIx64 ") must be at least "
2877 "the initial memory size (0x" RAM_ADDR_FMT ")",
2878 sz, ram_size);
2879 exit(EXIT_FAILURE);
2880 } else if (slots && sz == ram_size) {
2881 error_report("invalid value of -m option maxmem: "
2882 "memory slots were specified but maximum memory size "
2883 "(0x%" PRIx64 ") is equal to the initial memory size "
2884 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2885 exit(EXIT_FAILURE);
2886 }
2887
2888 *maxram_size = sz;
2889 *ram_slots = slots;
2890 } else if (qemu_opt_get(opts, "slots")) {
2891 error_report("invalid -m option value: missing 'maxmem' option");
2892 exit(EXIT_FAILURE);
2893 }
2894
2895 loc_pop(&loc);
2896 }
2897
2898 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2899 {
2900 GlobalProperty *g;
2901
2902 g = g_malloc0(sizeof(*g));
2903 g->driver = qemu_opt_get(opts, "driver");
2904 g->property = qemu_opt_get(opts, "property");
2905 g->value = qemu_opt_get(opts, "value");
2906 qdev_prop_register_global(g);
2907 return 0;
2908 }
2909
2910 static int qemu_read_default_config_file(void)
2911 {
2912 int ret;
2913
2914 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2915 if (ret < 0 && ret != -ENOENT) {
2916 return ret;
2917 }
2918
2919 return 0;
2920 }
2921
2922 static void user_register_global_props(void)
2923 {
2924 qemu_opts_foreach(qemu_find_opts("global"),
2925 global_init_func, NULL, NULL);
2926 }
2927
2928 int main(int argc, char **argv, char **envp)
2929 {
2930 int i;
2931 int snapshot, linux_boot;
2932 const char *initrd_filename;
2933 const char *kernel_filename, *kernel_cmdline;
2934 const char *boot_order = NULL;
2935 const char *boot_once = NULL;
2936 DisplayState *ds;
2937 QemuOpts *opts, *machine_opts;
2938 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2939 QemuOptsList *olist;
2940 int optind;
2941 const char *optarg;
2942 const char *loadvm = NULL;
2943 MachineClass *machine_class;
2944 const char *cpu_option;
2945 const char *vga_model = NULL;
2946 const char *qtest_chrdev = NULL;
2947 const char *qtest_log = NULL;
2948 const char *incoming = NULL;
2949 bool userconfig = true;
2950 bool nographic = false;
2951 int display_remote = 0;
2952 const char *log_mask = NULL;
2953 const char *log_file = NULL;
2954 char *trace_file = NULL;
2955 ram_addr_t maxram_size;
2956 uint64_t ram_slots = 0;
2957 FILE *vmstate_dump_file = NULL;
2958 Error *main_loop_err = NULL;
2959 Error *err = NULL;
2960 bool list_data_dirs = false;
2961 char *dir, **dirs;
2962 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2963
2964 error_init(argv[0]);
2965 module_call_init(MODULE_INIT_TRACE);
2966
2967 qemu_init_cpu_list();
2968 qemu_init_cpu_loop();
2969
2970 qemu_mutex_lock_iothread();
2971
2972 atexit(qemu_run_exit_notifiers);
2973 qemu_init_exec_dir(argv[0]);
2974
2975 module_call_init(MODULE_INIT_QOM);
2976
2977 qemu_add_opts(&qemu_drive_opts);
2978 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2979 qemu_add_drive_opts(&qemu_common_drive_opts);
2980 qemu_add_drive_opts(&qemu_drive_opts);
2981 qemu_add_drive_opts(&bdrv_runtime_opts);
2982 qemu_add_opts(&qemu_chardev_opts);
2983 qemu_add_opts(&qemu_device_opts);
2984 qemu_add_opts(&qemu_netdev_opts);
2985 qemu_add_opts(&qemu_nic_opts);
2986 qemu_add_opts(&qemu_net_opts);
2987 qemu_add_opts(&qemu_rtc_opts);
2988 qemu_add_opts(&qemu_global_opts);
2989 qemu_add_opts(&qemu_mon_opts);
2990 qemu_add_opts(&qemu_trace_opts);
2991 qemu_add_opts(&qemu_option_rom_opts);
2992 qemu_add_opts(&qemu_machine_opts);
2993 qemu_add_opts(&qemu_accel_opts);
2994 qemu_add_opts(&qemu_mem_opts);
2995 qemu_add_opts(&qemu_smp_opts);
2996 qemu_add_opts(&qemu_boot_opts);
2997 qemu_add_opts(&qemu_add_fd_opts);
2998 qemu_add_opts(&qemu_object_opts);
2999 qemu_add_opts(&qemu_tpmdev_opts);
3000 qemu_add_opts(&qemu_realtime_opts);
3001 qemu_add_opts(&qemu_overcommit_opts);
3002 qemu_add_opts(&qemu_msg_opts);
3003 qemu_add_opts(&qemu_name_opts);
3004 qemu_add_opts(&qemu_numa_opts);
3005 qemu_add_opts(&qemu_icount_opts);
3006 qemu_add_opts(&qemu_semihosting_config_opts);
3007 qemu_add_opts(&qemu_fw_cfg_opts);
3008 module_call_init(MODULE_INIT_OPTS);
3009
3010 runstate_init();
3011 precopy_infrastructure_init();
3012 postcopy_infrastructure_init();
3013 monitor_init_globals();
3014
3015 if (qcrypto_init(&err) < 0) {
3016 error_reportf_err(err, "cannot initialize crypto: ");
3017 exit(1);
3018 }
3019
3020 QLIST_INIT (&vm_change_state_head);
3021 os_setup_early_signal_handling();
3022
3023 cpu_option = NULL;
3024 snapshot = 0;
3025
3026 nb_nics = 0;
3027
3028 bdrv_init_with_whitelist();
3029
3030 autostart = 1;
3031
3032 /* first pass of option parsing */
3033 optind = 1;
3034 while (optind < argc) {
3035 if (argv[optind][0] != '-') {
3036 /* disk image */
3037 optind++;
3038 } else {
3039 const QEMUOption *popt;
3040
3041 popt = lookup_opt(argc, argv, &optarg, &optind);
3042 switch (popt->index) {
3043 case QEMU_OPTION_nouserconfig:
3044 userconfig = false;
3045 break;
3046 }
3047 }
3048 }
3049
3050 if (userconfig) {
3051 if (qemu_read_default_config_file() < 0) {
3052 exit(1);
3053 }
3054 }
3055
3056 /* second pass of option parsing */
3057 optind = 1;
3058 for(;;) {
3059 if (optind >= argc)
3060 break;
3061 if (argv[optind][0] != '-') {
3062 loc_set_cmdline(argv, optind, 1);
3063 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3064 } else {
3065 const QEMUOption *popt;
3066
3067 popt = lookup_opt(argc, argv, &optarg, &optind);
3068 if (!(popt->arch_mask & arch_type)) {
3069 error_report("Option not supported for this target");
3070 exit(1);
3071 }
3072 switch(popt->index) {
3073 case QEMU_OPTION_cpu:
3074 /* hw initialization will check this */
3075 cpu_option = optarg;
3076 break;
3077 case QEMU_OPTION_hda:
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_blockdev:
3085 {
3086 Visitor *v;
3087 BlockdevOptionsQueueEntry *bdo;
3088
3089 v = qobject_input_visitor_new_str(optarg, "driver",
3090 &error_fatal);
3091
3092 bdo = g_new(BlockdevOptionsQueueEntry, 1);
3093 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3094 &error_fatal);
3095 visit_free(v);
3096 loc_save(&bdo->loc);
3097 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3098 break;
3099 }
3100 case QEMU_OPTION_drive:
3101 if (drive_def(optarg) == NULL) {
3102 exit(1);
3103 }
3104 break;
3105 case QEMU_OPTION_set:
3106 if (qemu_set_option(optarg) != 0)
3107 exit(1);
3108 break;
3109 case QEMU_OPTION_global:
3110 if (qemu_global_option(optarg) != 0)
3111 exit(1);
3112 break;
3113 case QEMU_OPTION_mtdblock:
3114 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3115 break;
3116 case QEMU_OPTION_sd:
3117 drive_add(IF_SD, -1, optarg, SD_OPTS);
3118 break;
3119 case QEMU_OPTION_pflash:
3120 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3121 break;
3122 case QEMU_OPTION_snapshot:
3123 snapshot = 1;
3124 break;
3125 case QEMU_OPTION_numa:
3126 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3127 optarg, true);
3128 if (!opts) {
3129 exit(1);
3130 }
3131 break;
3132 case QEMU_OPTION_display:
3133 parse_display(optarg);
3134 break;
3135 case QEMU_OPTION_nographic:
3136 olist = qemu_find_opts("machine");
3137 qemu_opts_parse_noisily(olist, "graphics=off", false);
3138 nographic = true;
3139 dpy.type = DISPLAY_TYPE_NONE;
3140 break;
3141 case QEMU_OPTION_curses:
3142 #ifdef CONFIG_CURSES
3143 dpy.type = DISPLAY_TYPE_CURSES;
3144 #else
3145 error_report("curses or iconv support is disabled");
3146 exit(1);
3147 #endif
3148 break;
3149 case QEMU_OPTION_portrait:
3150 graphic_rotate = 90;
3151 break;
3152 case QEMU_OPTION_rotate:
3153 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3154 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3155 graphic_rotate != 180 && graphic_rotate != 270) {
3156 error_report("only 90, 180, 270 deg rotation is available");
3157 exit(1);
3158 }
3159 break;
3160 case QEMU_OPTION_kernel:
3161 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3162 &error_abort);
3163 break;
3164 case QEMU_OPTION_initrd:
3165 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3166 &error_abort);
3167 break;
3168 case QEMU_OPTION_append:
3169 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3170 &error_abort);
3171 break;
3172 case QEMU_OPTION_dtb:
3173 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3174 &error_abort);
3175 break;
3176 case QEMU_OPTION_cdrom:
3177 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3178 break;
3179 case QEMU_OPTION_boot:
3180 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3181 optarg, true);
3182 if (!opts) {
3183 exit(1);
3184 }
3185 break;
3186 case QEMU_OPTION_fda:
3187 case QEMU_OPTION_fdb:
3188 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3189 optarg, FD_OPTS);
3190 break;
3191 case QEMU_OPTION_no_fd_bootchk:
3192 fd_bootchk = 0;
3193 break;
3194 case QEMU_OPTION_netdev:
3195 default_net = 0;
3196 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3197 exit(1);
3198 }
3199 break;
3200 case QEMU_OPTION_nic:
3201 default_net = 0;
3202 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3203 exit(1);
3204 }
3205 break;
3206 case QEMU_OPTION_net:
3207 default_net = 0;
3208 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3209 exit(1);
3210 }
3211 break;
3212 #ifdef CONFIG_LIBISCSI
3213 case QEMU_OPTION_iscsi:
3214 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3215 optarg, false);
3216 if (!opts) {
3217 exit(1);
3218 }
3219 break;
3220 #endif
3221 case QEMU_OPTION_bt:
3222 warn_report("The bluetooth subsystem is deprecated and will "
3223 "be removed soon. If the bluetooth subsystem is "
3224 "still useful for you, please send a mail to "
3225 "qemu-devel@nongnu.org with your usecase.");
3226 add_device_config(DEV_BT, optarg);
3227 break;
3228 case QEMU_OPTION_audio_help:
3229 audio_legacy_help();
3230 exit (0);
3231 break;
3232 case QEMU_OPTION_audiodev:
3233 audio_parse_option(optarg);
3234 break;
3235 case QEMU_OPTION_soundhw:
3236 select_soundhw (optarg);
3237 break;
3238 case QEMU_OPTION_h:
3239 help(0);
3240 break;
3241 case QEMU_OPTION_version:
3242 version();
3243 exit(0);
3244 break;
3245 case QEMU_OPTION_m:
3246 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3247 optarg, true);
3248 if (!opts) {
3249 exit(EXIT_FAILURE);
3250 }
3251 break;
3252 #ifdef CONFIG_TPM
3253 case QEMU_OPTION_tpmdev:
3254 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3255 exit(1);
3256 }
3257 break;
3258 #endif
3259 case QEMU_OPTION_mempath:
3260 mem_path = optarg;
3261 break;
3262 case QEMU_OPTION_mem_prealloc:
3263 mem_prealloc = 1;
3264 break;
3265 case QEMU_OPTION_d:
3266 log_mask = optarg;
3267 break;
3268 case QEMU_OPTION_D:
3269 log_file = optarg;
3270 break;
3271 case QEMU_OPTION_DFILTER:
3272 qemu_set_dfilter_ranges(optarg, &error_fatal);
3273 break;
3274 case QEMU_OPTION_seed:
3275 qemu_guest_random_seed_main(optarg, &error_fatal);
3276 break;
3277 case QEMU_OPTION_s:
3278 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3279 break;
3280 case QEMU_OPTION_gdb:
3281 add_device_config(DEV_GDB, optarg);
3282 break;
3283 case QEMU_OPTION_L:
3284 if (is_help_option(optarg)) {
3285 list_data_dirs = true;
3286 } else {
3287 qemu_add_data_dir(optarg);
3288 }
3289 break;
3290 case QEMU_OPTION_bios:
3291 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3292 &error_abort);
3293 break;
3294 case QEMU_OPTION_singlestep:
3295 singlestep = 1;
3296 break;
3297 case QEMU_OPTION_S:
3298 autostart = 0;
3299 break;
3300 case QEMU_OPTION_k:
3301 keyboard_layout = optarg;
3302 break;
3303 case QEMU_OPTION_vga:
3304 vga_model = optarg;
3305 default_vga = 0;
3306 break;
3307 case QEMU_OPTION_g:
3308 {
3309 const char *p;
3310 int w, h, depth;
3311 p = optarg;
3312 w = strtol(p, (char **)&p, 10);
3313 if (w <= 0) {
3314 graphic_error:
3315 error_report("invalid resolution or depth");
3316 exit(1);
3317 }
3318 if (*p != 'x')
3319 goto graphic_error;
3320 p++;
3321 h = strtol(p, (char **)&p, 10);
3322 if (h <= 0)
3323 goto graphic_error;
3324 if (*p == 'x') {
3325 p++;
3326 depth = strtol(p, (char **)&p, 10);
3327 if (depth != 8 && depth != 15 && depth != 16 &&
3328 depth != 24 && depth != 32)
3329 goto graphic_error;
3330 } else if (*p == '\0') {
3331 depth = graphic_depth;
3332 } else {
3333 goto graphic_error;
3334 }
3335
3336 graphic_width = w;
3337 graphic_height = h;
3338 graphic_depth = depth;
3339 }
3340 break;
3341 case QEMU_OPTION_echr:
3342 {
3343 char *r;
3344 term_escape_char = strtol(optarg, &r, 0);
3345 if (r == optarg)
3346 printf("Bad argument to echr\n");
3347 break;
3348 }
3349 case QEMU_OPTION_monitor:
3350 default_monitor = 0;
3351 if (strncmp(optarg, "none", 4)) {
3352 monitor_parse(optarg, "readline", false);
3353 }
3354 break;
3355 case QEMU_OPTION_qmp:
3356 monitor_parse(optarg, "control", false);
3357 default_monitor = 0;
3358 break;
3359 case QEMU_OPTION_qmp_pretty:
3360 monitor_parse(optarg, "control", true);
3361 default_monitor = 0;
3362 break;
3363 case QEMU_OPTION_mon:
3364 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3365 true);
3366 if (!opts) {
3367 exit(1);
3368 }
3369 default_monitor = 0;
3370 break;
3371 case QEMU_OPTION_chardev:
3372 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3373 optarg, true);
3374 if (!opts) {
3375 exit(1);
3376 }
3377 break;
3378 case QEMU_OPTION_fsdev:
3379 olist = qemu_find_opts("fsdev");
3380 if (!olist) {
3381 error_report("fsdev support is disabled");
3382 exit(1);
3383 }
3384 opts = qemu_opts_parse_noisily(olist, optarg, true);
3385 if (!opts) {
3386 exit(1);
3387 }
3388 break;
3389 case QEMU_OPTION_virtfs: {
3390 QemuOpts *fsdev;
3391 QemuOpts *device;
3392 const char *writeout, *sock_fd, *socket, *path, *security_model;
3393
3394 olist = qemu_find_opts("virtfs");
3395 if (!olist) {
3396 error_report("virtfs support is disabled");
3397 exit(1);
3398 }
3399 opts = qemu_opts_parse_noisily(olist, optarg, true);
3400 if (!opts) {
3401 exit(1);
3402 }
3403
3404 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3405 qemu_opt_get(opts, "mount_tag") == NULL) {
3406 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3407 exit(1);
3408 }
3409 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3410 qemu_opts_id(opts) ?:
3411 qemu_opt_get(opts, "mount_tag"),
3412 1, NULL);
3413 if (!fsdev) {
3414 error_report("duplicate or invalid fsdev id: %s",
3415 qemu_opt_get(opts, "mount_tag"));
3416 exit(1);
3417 }
3418
3419 writeout = qemu_opt_get(opts, "writeout");
3420 if (writeout) {
3421 #ifdef CONFIG_SYNC_FILE_RANGE
3422 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3423 #else
3424 error_report("writeout=immediate not supported "
3425 "on this platform");
3426 exit(1);
3427 #endif
3428 }
3429 qemu_opt_set(fsdev, "fsdriver",
3430 qemu_opt_get(opts, "fsdriver"), &error_abort);
3431 path = qemu_opt_get(opts, "path");
3432 if (path) {
3433 qemu_opt_set(fsdev, "path", path, &error_abort);
3434 }
3435 security_model = qemu_opt_get(opts, "security_model");
3436 if (security_model) {
3437 qemu_opt_set(fsdev, "security_model", security_model,
3438 &error_abort);
3439 }
3440 socket = qemu_opt_get(opts, "socket");
3441 if (socket) {
3442 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3443 }
3444 sock_fd = qemu_opt_get(opts, "sock_fd");
3445 if (sock_fd) {
3446 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3447 }
3448
3449 qemu_opt_set_bool(fsdev, "readonly",
3450 qemu_opt_get_bool(opts, "readonly", 0),
3451 &error_abort);
3452 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3453 &error_abort);
3454 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3455 qemu_opt_set(device, "fsdev",
3456 qemu_opts_id(fsdev), &error_abort);
3457 qemu_opt_set(device, "mount_tag",
3458 qemu_opt_get(opts, "mount_tag"), &error_abort);
3459 break;
3460 }
3461 case QEMU_OPTION_virtfs_synth: {
3462 QemuOpts *fsdev;
3463 QemuOpts *device;
3464
3465 warn_report("'-virtfs_synth' is deprecated, please use "
3466 "'-fsdev synth' and '-device virtio-9p-...' "
3467 "instead");
3468
3469 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3470 1, NULL);
3471 if (!fsdev) {
3472 error_report("duplicate option: %s", "virtfs_synth");
3473 exit(1);
3474 }
3475 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3476
3477 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3478 &error_abort);
3479 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3480 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3481 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3482 break;
3483 }
3484 case QEMU_OPTION_serial:
3485 add_device_config(DEV_SERIAL, optarg);
3486 default_serial = 0;
3487 if (strncmp(optarg, "mon:", 4) == 0) {
3488 default_monitor = 0;
3489 }
3490 break;
3491 case QEMU_OPTION_watchdog:
3492 if (watchdog) {
3493 error_report("only one watchdog option may be given");
3494 return 1;
3495 }
3496 watchdog = optarg;
3497 break;
3498 case QEMU_OPTION_watchdog_action:
3499 if (select_watchdog_action(optarg) == -1) {
3500 error_report("unknown -watchdog-action parameter");
3501 exit(1);
3502 }
3503 break;
3504 case QEMU_OPTION_parallel:
3505 add_device_config(DEV_PARALLEL, optarg);
3506 default_parallel = 0;
3507 if (strncmp(optarg, "mon:", 4) == 0) {
3508 default_monitor = 0;
3509 }
3510 break;
3511 case QEMU_OPTION_debugcon:
3512 add_device_config(DEV_DEBUGCON, optarg);
3513 break;
3514 case QEMU_OPTION_loadvm:
3515 loadvm = optarg;
3516 break;
3517 case QEMU_OPTION_full_screen:
3518 dpy.has_full_screen = true;
3519 dpy.full_screen = true;
3520 break;
3521 case QEMU_OPTION_alt_grab:
3522 alt_grab = 1;
3523 break;
3524 case QEMU_OPTION_ctrl_grab:
3525 ctrl_grab = 1;
3526 break;
3527 case QEMU_OPTION_no_quit:
3528 dpy.has_window_close = true;
3529 dpy.window_close = false;
3530 break;
3531 case QEMU_OPTION_sdl:
3532 #ifdef CONFIG_SDL
3533 dpy.type = DISPLAY_TYPE_SDL;
3534 break;
3535 #else
3536 error_report("SDL support is disabled");
3537 exit(1);
3538 #endif
3539 case QEMU_OPTION_pidfile:
3540 pid_file = optarg;
3541 break;
3542 case QEMU_OPTION_win2k_hack:
3543 win2k_install_hack = 1;
3544 break;
3545 case QEMU_OPTION_acpitable:
3546 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3547 optarg, true);
3548 if (!opts) {
3549 exit(1);
3550 }
3551 acpi_table_add(opts, &error_fatal);
3552 break;
3553 case QEMU_OPTION_smbios:
3554 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3555 optarg, false);
3556 if (!opts) {
3557 exit(1);
3558 }
3559 smbios_entry_add(opts, &error_fatal);
3560 break;
3561 case QEMU_OPTION_fwcfg:
3562 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3563 optarg, true);
3564 if (opts == NULL) {
3565 exit(1);
3566 }
3567 break;
3568 case QEMU_OPTION_preconfig:
3569 preconfig_exit_requested = false;
3570 break;
3571 case QEMU_OPTION_enable_kvm:
3572 olist = qemu_find_opts("machine");
3573 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3574 break;
3575 case QEMU_OPTION_M:
3576 case QEMU_OPTION_machine:
3577 olist = qemu_find_opts("machine");
3578 opts = qemu_opts_parse_noisily(olist, optarg, true);
3579 if (!opts) {
3580 exit(1);
3581 }
3582 break;
3583 case QEMU_OPTION_no_kvm:
3584 olist = qemu_find_opts("machine");
3585 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3586 break;
3587 case QEMU_OPTION_accel:
3588 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3589 optarg, true);
3590 optarg = qemu_opt_get(accel_opts, "accel");
3591 if (!optarg || is_help_option(optarg)) {
3592 printf("Accelerators supported in QEMU binary:\n");
3593 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3594 false);
3595 for (el = accel_list; el; el = el->next) {
3596 gchar *typename = g_strdup(object_class_get_name(
3597 OBJECT_CLASS(el->data)));
3598 /* omit qtest which is used for tests only */
3599 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3600 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3601 gchar **optname = g_strsplit(typename,
3602 ACCEL_CLASS_SUFFIX, 0);
3603 printf("%s\n", optname[0]);
3604 g_free(optname);
3605 }
3606 g_free(typename);
3607 }
3608 g_slist_free(accel_list);
3609 exit(0);
3610 }
3611 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3612 false, &error_abort);
3613 qemu_opt_set(opts, "accel", optarg, &error_abort);
3614 break;
3615 case QEMU_OPTION_usb:
3616 olist = qemu_find_opts("machine");
3617 qemu_opts_parse_noisily(olist, "usb=on", false);
3618 break;
3619 case QEMU_OPTION_usbdevice:
3620 error_report("'-usbdevice' is deprecated, please use "
3621 "'-device usb-...' instead");
3622 olist = qemu_find_opts("machine");
3623 qemu_opts_parse_noisily(olist, "usb=on", false);
3624 add_device_config(DEV_USB, optarg);
3625 break;
3626 case QEMU_OPTION_device:
3627 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3628 optarg, true)) {
3629 exit(1);
3630 }
3631 break;
3632 case QEMU_OPTION_smp:
3633 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3634 optarg, true)) {
3635 exit(1);
3636 }
3637 break;
3638 case QEMU_OPTION_vnc:
3639 vnc_parse(optarg, &error_fatal);
3640 break;
3641 case QEMU_OPTION_no_acpi:
3642 acpi_enabled = 0;
3643 break;
3644 case QEMU_OPTION_no_hpet:
3645 no_hpet = 1;
3646 break;
3647 case QEMU_OPTION_no_reboot:
3648 no_reboot = 1;
3649 break;
3650 case QEMU_OPTION_no_shutdown:
3651 no_shutdown = 1;
3652 break;
3653 case QEMU_OPTION_show_cursor:
3654 cursor_hide = 0;
3655 break;
3656 case QEMU_OPTION_uuid:
3657 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3658 error_report("failed to parse UUID string: wrong format");
3659 exit(1);
3660 }
3661 qemu_uuid_set = true;
3662 break;
3663 case QEMU_OPTION_option_rom:
3664 if (nb_option_roms >= MAX_OPTION_ROMS) {
3665 error_report("too many option ROMs");
3666 exit(1);
3667 }
3668 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3669 optarg, true);
3670 if (!opts) {
3671 exit(1);
3672 }
3673 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3674 option_rom[nb_option_roms].bootindex =
3675 qemu_opt_get_number(opts, "bootindex", -1);
3676 if (!option_rom[nb_option_roms].name) {
3677 error_report("Option ROM file is not specified");
3678 exit(1);
3679 }
3680 nb_option_roms++;
3681 break;
3682 case QEMU_OPTION_semihosting:
3683 qemu_semihosting_enable();
3684 break;
3685 case QEMU_OPTION_semihosting_config:
3686 if (qemu_semihosting_config_options(optarg) != 0) {
3687 exit(1);
3688 }
3689 break;
3690 case QEMU_OPTION_name:
3691 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3692 optarg, true);
3693 if (!opts) {
3694 exit(1);
3695 }
3696 break;
3697 case QEMU_OPTION_prom_env:
3698 if (nb_prom_envs >= MAX_PROM_ENVS) {
3699 error_report("too many prom variables");
3700 exit(1);
3701 }
3702 prom_envs[nb_prom_envs] = optarg;
3703 nb_prom_envs++;
3704 break;
3705 case QEMU_OPTION_old_param:
3706 old_param = 1;
3707 break;
3708 case QEMU_OPTION_rtc:
3709 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3710 false);
3711 if (!opts) {
3712 exit(1);
3713 }
3714 break;
3715 case QEMU_OPTION_tb_size:
3716 #ifndef CONFIG_TCG
3717 error_report("TCG is disabled");
3718 exit(1);
3719 #endif
3720 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3721 error_report("Invalid argument to -tb-size");
3722 exit(1);
3723 }
3724 break;
3725 case QEMU_OPTION_icount:
3726 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3727 optarg, true);
3728 if (!icount_opts) {
3729 exit(1);
3730 }
3731 break;
3732 case QEMU_OPTION_incoming:
3733 if (!incoming) {
3734 runstate_set(RUN_STATE_INMIGRATE);
3735 }
3736 incoming = optarg;
3737 break;
3738 case QEMU_OPTION_only_migratable:
3739 only_migratable = 1;
3740 break;
3741 case QEMU_OPTION_nodefaults:
3742 has_defaults = 0;
3743 break;
3744 case QEMU_OPTION_xen_domid:
3745 if (!(xen_available())) {
3746 error_report("Option not supported for this target");
3747 exit(1);
3748 }
3749 xen_domid = atoi(optarg);
3750 break;
3751 case QEMU_OPTION_xen_attach:
3752 if (!(xen_available())) {
3753 error_report("Option not supported for this target");
3754 exit(1);
3755 }
3756 xen_mode = XEN_ATTACH;
3757 break;
3758 case QEMU_OPTION_xen_domid_restrict:
3759 if (!(xen_available())) {
3760 error_report("Option not supported for this target");
3761 exit(1);
3762 }
3763 xen_domid_restrict = true;
3764 break;
3765 case QEMU_OPTION_trace:
3766 g_free(trace_file);
3767 trace_file = trace_opt_parse(optarg);
3768 break;
3769 case QEMU_OPTION_readconfig:
3770 {
3771 int ret = qemu_read_config_file(optarg);
3772 if (ret < 0) {
3773 error_report("read config %s: %s", optarg,
3774 strerror(-ret));
3775 exit(1);
3776 }
3777 break;
3778 }
3779 case QEMU_OPTION_spice:
3780 olist = qemu_find_opts("spice");
3781 if (!olist) {
3782 error_report("spice support is disabled");
3783 exit(1);
3784 }
3785 opts = qemu_opts_parse_noisily(olist, optarg, false);
3786 if (!opts) {
3787 exit(1);
3788 }
3789 display_remote++;
3790 break;
3791 case QEMU_OPTION_writeconfig:
3792 {
3793 FILE *fp;
3794 if (strcmp(optarg, "-") == 0) {
3795 fp = stdout;
3796 } else {
3797 fp = fopen(optarg, "w");
3798 if (fp == NULL) {
3799 error_report("open %s: %s", optarg,
3800 strerror(errno));
3801 exit(1);
3802 }
3803 }
3804 qemu_config_write(fp);
3805 if (fp != stdout) {
3806 fclose(fp);
3807 }
3808 break;
3809 }
3810 case QEMU_OPTION_qtest:
3811 qtest_chrdev = optarg;
3812 break;
3813 case QEMU_OPTION_qtest_log:
3814 qtest_log = optarg;
3815 break;
3816 case QEMU_OPTION_sandbox:
3817 olist = qemu_find_opts("sandbox");
3818 if (!olist) {
3819 #ifndef CONFIG_SECCOMP
3820 error_report("-sandbox support is not enabled "
3821 "in this QEMU binary");
3822 #endif
3823 exit(1);
3824 }
3825
3826 opts = qemu_opts_parse_noisily(olist, optarg, true);
3827 if (!opts) {
3828 exit(1);
3829 }
3830 break;
3831 case QEMU_OPTION_add_fd:
3832 #ifndef _WIN32
3833 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3834 optarg, false);
3835 if (!opts) {
3836 exit(1);
3837 }
3838 #else
3839 error_report("File descriptor passing is disabled on this "
3840 "platform");
3841 exit(1);
3842 #endif
3843 break;
3844 case QEMU_OPTION_object:
3845 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3846 optarg, true);
3847 if (!opts) {
3848 exit(1);
3849 }
3850 break;
3851 case QEMU_OPTION_realtime:
3852 warn_report("'-realtime mlock=...' is deprecated, please use "
3853 "'-overcommit mem-lock=...' instead");
3854 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3855 optarg, false);
3856 if (!opts) {
3857 exit(1);
3858 }
3859 /* Don't override the -overcommit option if set */
3860 enable_mlock = enable_mlock ||
3861 qemu_opt_get_bool(opts, "mlock", true);
3862 break;
3863 case QEMU_OPTION_overcommit:
3864 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3865 optarg, false);
3866 if (!opts) {
3867 exit(1);
3868 }
3869 /* Don't override the -realtime option if set */
3870 enable_mlock = enable_mlock ||
3871 qemu_opt_get_bool(opts, "mem-lock", false);
3872 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3873 break;
3874 case QEMU_OPTION_msg:
3875 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3876 false);
3877 if (!opts) {
3878 exit(1);
3879 }
3880 configure_msg(opts);
3881 break;
3882 case QEMU_OPTION_dump_vmstate:
3883 if (vmstate_dump_file) {
3884 error_report("only one '-dump-vmstate' "
3885 "option may be given");
3886 exit(1);
3887 }
3888 vmstate_dump_file = fopen(optarg, "w");
3889 if (vmstate_dump_file == NULL) {
3890 error_report("open %s: %s", optarg, strerror(errno));
3891 exit(1);
3892 }
3893 break;
3894 case QEMU_OPTION_enable_sync_profile:
3895 qsp_enable();
3896 break;
3897 case QEMU_OPTION_nouserconfig:
3898 /* Nothing to be parsed here. Especially, do not error out below. */
3899 break;
3900 default:
3901 if (os_parse_cmd_args(popt->index, optarg)) {
3902 error_report("Option not supported in this build");
3903 exit(1);
3904 }
3905 }
3906 }
3907 }
3908 /*
3909 * Clear error location left behind by the loop.
3910 * Best done right after the loop. Do not insert code here!
3911 */
3912 loc_set_none();
3913
3914 user_register_global_props();
3915
3916 replay_configure(icount_opts);
3917
3918 if (incoming && !preconfig_exit_requested) {
3919 error_report("'preconfig' and 'incoming' options are "
3920 "mutually exclusive");
3921 exit(EXIT_FAILURE);
3922 }
3923
3924 configure_rtc(qemu_find_opts_singleton("rtc"));
3925
3926 machine_class = select_machine();
3927 object_set_machine_compat_props(machine_class->compat_props);
3928
3929 set_memory_options(&ram_slots, &maxram_size, machine_class);
3930
3931 os_daemonize();
3932 rcu_disable_atfork();
3933
3934 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3935 error_reportf_err(err, "cannot create PID file: ");
3936 exit(1);
3937 }
3938
3939 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3940 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3941
3942 if (qemu_init_main_loop(&main_loop_err)) {
3943 error_report_err(main_loop_err);
3944 exit(1);
3945 }
3946
3947 #ifdef CONFIG_SECCOMP
3948 olist = qemu_find_opts_err("sandbox", NULL);
3949 if (olist) {
3950 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
3951 }
3952 #endif
3953
3954 qemu_opts_foreach(qemu_find_opts("name"),
3955 parse_name, NULL, &error_fatal);
3956
3957 #ifndef _WIN32
3958 qemu_opts_foreach(qemu_find_opts("add-fd"),
3959 parse_add_fd, NULL, &error_fatal);
3960
3961 qemu_opts_foreach(qemu_find_opts("add-fd"),
3962 cleanup_add_fd, NULL, &error_fatal);
3963 #endif
3964
3965 current_machine = MACHINE(object_new(object_class_get_name(
3966 OBJECT_CLASS(machine_class))));
3967 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3968 exit(0);
3969 }
3970 object_property_add_child(object_get_root(), "machine",
3971 OBJECT(current_machine), &error_abort);
3972 object_property_add_child(container_get(OBJECT(current_machine),
3973 "/unattached"),
3974 "sysbus", OBJECT(sysbus_get_default()),
3975 NULL);
3976
3977 if (machine_class->minimum_page_bits) {
3978 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3979 /* This would be a board error: specifying a minimum smaller than
3980 * a target's compile-time fixed setting.
3981 */
3982 g_assert_not_reached();
3983 }
3984 }
3985
3986 cpu_exec_init_all();
3987
3988 if (machine_class->hw_version) {
3989 qemu_set_hw_version(machine_class->hw_version);
3990 }
3991
3992 if (cpu_option && is_help_option(cpu_option)) {
3993 list_cpus(cpu_option);
3994 exit(0);
3995 }
3996
3997 if (!trace_init_backends()) {
3998 exit(1);
3999 }
4000 trace_init_file(trace_file);
4001
4002 /* Open the logfile at this point and set the log mask if necessary.
4003 */
4004 if (log_file) {
4005 qemu_set_log_filename(log_file, &error_fatal);
4006 }
4007
4008 if (log_mask) {
4009 int mask;
4010 mask = qemu_str_to_log_mask(log_mask);
4011 if (!mask) {
4012 qemu_print_log_usage(stdout);
4013 exit(1);
4014 }
4015 qemu_set_log(mask);
4016 } else {
4017 qemu_set_log(0);
4018 }
4019
4020 /* add configured firmware directories */
4021 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4022 for (i = 0; dirs[i] != NULL; i++) {
4023 qemu_add_data_dir(dirs[i]);
4024 }
4025 g_strfreev(dirs);
4026
4027 /* try to find datadir relative to the executable path */
4028 dir = os_find_datadir();
4029 qemu_add_data_dir(dir);
4030 g_free(dir);
4031
4032 /* add the datadir specified when building */
4033 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4034
4035 /* -L help lists the data directories and exits. */
4036 if (list_data_dirs) {
4037 for (i = 0; i < data_dir_idx; i++) {
4038 printf("%s\n", data_dir[i]);
4039 }
4040 exit(0);
4041 }
4042
4043 /* machine_class: default to UP */
4044 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4045 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4046 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4047
4048 /* default to machine_class->default_cpus */
4049 smp_cpus = machine_class->default_cpus;
4050 max_cpus = machine_class->default_cpus;
4051
4052 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4053
4054 /* sanity-check smp_cpus and max_cpus against machine_class */
4055 if (smp_cpus < machine_class->min_cpus) {
4056 error_report("Invalid SMP CPUs %d. The min CPUs "
4057 "supported by machine '%s' is %d", smp_cpus,
4058 machine_class->name, machine_class->min_cpus);
4059 exit(1);
4060 }
4061 if (max_cpus > machine_class->max_cpus) {
4062 error_report("Invalid SMP CPUs %d. The max CPUs "
4063 "supported by machine '%s' is %d", max_cpus,
4064 machine_class->name, machine_class->max_cpus);
4065 exit(1);
4066 }
4067
4068 /*
4069 * Get the default machine options from the machine if it is not already
4070 * specified either by the configuration file or by the command line.
4071 */
4072 if (machine_class->default_machine_opts) {
4073 qemu_opts_set_defaults(qemu_find_opts("machine"),
4074 machine_class->default_machine_opts, 0);
4075 }
4076
4077 qemu_opts_foreach(qemu_find_opts("device"),
4078 default_driver_check, NULL, NULL);
4079 qemu_opts_foreach(qemu_find_opts("global"),
4080 default_driver_check, NULL, NULL);
4081
4082 if (!vga_model && !default_vga) {
4083 vga_interface_type = VGA_DEVICE;
4084 }
4085 if (!has_defaults || machine_class->no_serial) {
4086 default_serial = 0;
4087 }
4088 if (!has_defaults || machine_class->no_parallel) {
4089 default_parallel = 0;
4090 }
4091 if (!has_defaults || machine_class->no_floppy) {
4092 default_floppy = 0;
4093 }
4094 if (!has_defaults || machine_class->no_cdrom) {
4095 default_cdrom = 0;
4096 }
4097 if (!has_defaults || machine_class->no_sdcard) {
4098 default_sdcard = 0;
4099 }
4100 if (!has_defaults) {
4101 default_monitor = 0;
4102 default_net = 0;
4103 default_vga = 0;
4104 }
4105
4106 if (is_daemonized()) {
4107 if (!preconfig_exit_requested) {
4108 error_report("'preconfig' and 'daemonize' options are "
4109 "mutually exclusive");
4110 exit(EXIT_FAILURE);
4111 }
4112
4113 /* According to documentation and historically, -nographic redirects
4114 * serial port, parallel port and monitor to stdio, which does not work
4115 * with -daemonize. We can redirect these to null instead, but since
4116 * -nographic is legacy, let's just error out.
4117 * We disallow -nographic only if all other ports are not redirected
4118 * explicitly, to not break existing legacy setups which uses
4119 * -nographic _and_ redirects all ports explicitly - this is valid
4120 * usage, -nographic is just a no-op in this case.
4121 */
4122 if (nographic
4123 && (default_parallel || default_serial || default_monitor)) {
4124 error_report("-nographic cannot be used with -daemonize");
4125 exit(1);
4126 }
4127 #ifdef CONFIG_CURSES
4128 if (dpy.type == DISPLAY_TYPE_CURSES) {
4129 error_report("curses display cannot be used with -daemonize");
4130 exit(1);
4131 }
4132 #endif
4133 }
4134
4135 if (nographic) {
4136 if (default_parallel)
4137 add_device_config(DEV_PARALLEL, "null");
4138 if (default_serial && default_monitor) {
4139 add_device_config(DEV_SERIAL, "mon:stdio");
4140 } else {
4141 if (default_serial)
4142 add_device_config(DEV_SERIAL, "stdio");
4143 if (default_monitor)
4144 monitor_parse("stdio", "readline", false);
4145 }
4146 } else {
4147 if (default_serial)
4148 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4149 if (default_parallel)
4150 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4151 if (default_monitor)
4152 monitor_parse("vc:80Cx24C", "readline", false);
4153 }
4154
4155 #if defined(CONFIG_VNC)
4156 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4157 display_remote++;
4158 }
4159 #endif
4160 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4161 if (!qemu_display_find_default(&dpy)) {
4162 dpy.type = DISPLAY_TYPE_NONE;
4163 #if defined(CONFIG_VNC)
4164 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4165 #endif
4166 }
4167 }
4168 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4169 dpy.type = DISPLAY_TYPE_NONE;
4170 }
4171
4172 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4173 error_report("-alt-grab and -ctrl-grab are only valid "
4174 "for SDL, ignoring option");
4175 }
4176 if (dpy.has_window_close &&
4177 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4178 error_report("-no-quit is only valid for GTK and SDL, "
4179 "ignoring option");
4180 }
4181
4182 qemu_display_early_init(&dpy);
4183 qemu_console_early_init();
4184
4185 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4186 #if defined(CONFIG_OPENGL)
4187 error_report("OpenGL is not supported by the display");
4188 #else
4189 error_report("OpenGL support is disabled");
4190 #endif
4191 exit(1);
4192 }
4193
4194 page_size_init();
4195 socket_init();
4196
4197 qemu_opts_foreach(qemu_find_opts("object"),
4198 user_creatable_add_opts_foreach,
4199 object_create_initial, &error_fatal);
4200
4201 qemu_opts_foreach(qemu_find_opts("chardev"),
4202 chardev_init_func, NULL, &error_fatal);
4203 /* now chardevs have been created we may have semihosting to connect */
4204 qemu_semihosting_connect_chardevs();
4205
4206 #ifdef CONFIG_VIRTFS
4207 qemu_opts_foreach(qemu_find_opts("fsdev"),
4208 fsdev_init_func, NULL, &error_fatal);
4209 #endif
4210
4211 if (qemu_opts_foreach(qemu_find_opts("device"),
4212 device_help_func, NULL, NULL)) {
4213 exit(0);
4214 }
4215
4216 /*
4217 * Note: we need to create block backends before
4218 * machine_set_property(), so machine properties can refer to
4219 * them.
4220 */
4221 configure_blockdev(&bdo_queue, machine_class, snapshot);
4222
4223 machine_opts = qemu_get_machine_opts();
4224 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4225 &error_fatal);
4226 current_machine->ram_size = ram_size;
4227 current_machine->maxram_size = maxram_size;
4228 current_machine->ram_slots = ram_slots;
4229
4230 /*
4231 * Note: uses machine properties such as kernel-irqchip, must run
4232 * after machine_set_property().
4233 */
4234 configure_accelerator(current_machine, argv[0]);
4235
4236 /*
4237 * Beware, QOM objects created before this point miss global and
4238 * compat properties.
4239 *
4240 * Global properties get set up by qdev_prop_register_global(),
4241 * called from user_register_global_props(), and certain option
4242 * desugaring. Also in CPU feature desugaring (buried in
4243 * parse_cpu_option()), which happens below this point, but may
4244 * only target the CPU type, which can only be created after
4245 * parse_cpu_option() returned the type.
4246 *
4247 * Machine compat properties: object_set_machine_compat_props().
4248 * Accelerator compat props: object_set_accelerator_compat_props(),
4249 * called from configure_accelerator().
4250 */
4251
4252 if (!qtest_enabled() && machine_class->deprecation_reason) {
4253 error_report("Machine type '%s' is deprecated: %s",
4254 machine_class->name, machine_class->deprecation_reason);
4255 }
4256
4257 /*
4258 * Note: creates a QOM object, must run only after global and
4259 * compat properties have been set up.
4260 */
4261 migration_object_init();
4262
4263 if (qtest_chrdev) {
4264 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4265 }
4266
4267 machine_opts = qemu_get_machine_opts();
4268 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4269 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4270 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4271 bios_name = qemu_opt_get(machine_opts, "firmware");
4272
4273 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4274 if (opts) {
4275 boot_order = qemu_opt_get(opts, "order");
4276 if (boot_order) {
4277 validate_bootdevices(boot_order, &error_fatal);
4278 }
4279
4280 boot_once = qemu_opt_get(opts, "once");
4281 if (boot_once) {
4282 validate_bootdevices(boot_once, &error_fatal);
4283 }
4284
4285 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4286 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4287 }
4288
4289 if (!boot_order) {
4290 boot_order = machine_class->default_boot_order;
4291 }
4292
4293 if (!kernel_cmdline) {
4294 kernel_cmdline = "";
4295 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4296 }
4297
4298 linux_boot = (kernel_filename != NULL);
4299
4300 if (!linux_boot && *kernel_cmdline != '\0') {
4301 error_report("-append only allowed with -kernel option");
4302 exit(1);
4303 }
4304
4305 if (!linux_boot && initrd_filename != NULL) {
4306 error_report("-initrd only allowed with -kernel option");
4307 exit(1);
4308 }
4309
4310 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4311 /* fall back to the -kernel/-append */
4312 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4313 }
4314
4315 os_set_line_buffering();
4316
4317 /* spice needs the timers to be initialized by this point */
4318 qemu_spice_init();
4319
4320 cpu_ticks_init();
4321 if (icount_opts) {
4322 if (!tcg_enabled()) {
4323 error_report("-icount is not allowed with hardware virtualization");
4324 exit(1);
4325 }
4326 configure_icount(icount_opts, &error_abort);
4327 qemu_opts_del(icount_opts);
4328 }
4329
4330 if (tcg_enabled()) {
4331 qemu_tcg_configure(accel_opts, &error_fatal);
4332 }
4333
4334 if (default_net) {
4335 QemuOptsList *net = qemu_find_opts("net");
4336 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4337 #ifdef CONFIG_SLIRP
4338 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4339 #endif
4340 }
4341
4342 if (net_init_clients(&err) < 0) {
4343 error_report_err(err);
4344 exit(1);
4345 }
4346
4347 qemu_opts_foreach(qemu_find_opts("object"),
4348 user_creatable_add_opts_foreach,
4349 object_create_delayed, &error_fatal);
4350
4351 tpm_init();
4352
4353 /* init the bluetooth world */
4354 if (foreach_device_config(DEV_BT, bt_parse))
4355 exit(1);
4356
4357 if (!xen_enabled()) {
4358 /* On 32-bit hosts, QEMU is limited by virtual address space */
4359 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4360 error_report("at most 2047 MB RAM can be simulated");
4361 exit(1);
4362 }
4363 }
4364
4365 blk_mig_init();
4366 ram_mig_init();
4367 dirty_bitmap_mig_init();
4368
4369 qemu_opts_foreach(qemu_find_opts("mon"),
4370 mon_init_func, NULL, &error_fatal);
4371
4372 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4373 exit(1);
4374 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4375 exit(1);
4376 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4377 exit(1);
4378
4379 /* If no default VGA is requested, the default is "none". */
4380 if (default_vga) {
4381 vga_model = get_default_vga_model(machine_class);
4382 }
4383 if (vga_model) {
4384 select_vgahw(machine_class, vga_model);
4385 }
4386
4387 if (watchdog) {
4388 i = select_watchdog(watchdog);
4389 if (i > 0)
4390 exit (i == 1 ? 1 : 0);
4391 }
4392
4393 /* This checkpoint is required by replay to separate prior clock
4394 reading from the other reads, because timer polling functions query
4395 clock values from the log. */
4396 replay_checkpoint(CHECKPOINT_INIT);
4397 qdev_machine_init();
4398
4399 current_machine->boot_order = boot_order;
4400
4401 /* parse features once if machine provides default cpu_type */
4402 current_machine->cpu_type = machine_class->default_cpu_type;
4403 if (cpu_option) {
4404 current_machine->cpu_type = parse_cpu_option(cpu_option);
4405 }
4406 parse_numa_opts(current_machine);
4407
4408 /* do monitor/qmp handling at preconfig state if requested */
4409 main_loop();
4410
4411 audio_init_audiodevs();
4412
4413 /* from here on runstate is RUN_STATE_PRELAUNCH */
4414 machine_run_board_init(current_machine);
4415
4416 realtime_init();
4417
4418 soundhw_init();
4419
4420 if (hax_enabled()) {
4421 hax_sync_vcpus();
4422 }
4423
4424 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4425 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4426
4427 /* init USB devices */
4428 if (machine_usb(current_machine)) {
4429 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4430 exit(1);
4431 }
4432
4433 /* Check if IGD GFX passthrough. */
4434 igd_gfx_passthru();
4435
4436 /* init generic devices */
4437 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4438 qemu_opts_foreach(qemu_find_opts("device"),
4439 device_init_func, NULL, &error_fatal);
4440
4441 cpu_synchronize_all_post_init();
4442
4443 rom_reset_order_override();
4444
4445 /* Did we create any drives that we failed to create a device for? */
4446 drive_check_orphaned();
4447
4448 /* Don't warn about the default network setup that you get if
4449 * no command line -net or -netdev options are specified. There
4450 * are two cases that we would otherwise complain about:
4451 * (1) board doesn't support a NIC but the implicit "-net nic"
4452 * requested one
4453 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4454 * sets up a nic that isn't connected to anything.
4455 */
4456 if (!default_net && (!qtest_enabled() || has_defaults)) {
4457 net_check_clients();
4458 }
4459
4460 if (boot_once) {
4461 qemu_boot_set(boot_once, &error_fatal);
4462 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4463 }
4464
4465 /* init local displays */
4466 ds = init_displaystate();
4467 qemu_display_init(ds, &dpy);
4468
4469 /* must be after terminal init, SDL library changes signal handlers */
4470 os_setup_signal_handling();
4471
4472 /* init remote displays */
4473 #ifdef CONFIG_VNC
4474 qemu_opts_foreach(qemu_find_opts("vnc"),
4475 vnc_init_func, NULL, &error_fatal);
4476 #endif
4477
4478 if (using_spice) {
4479 qemu_spice_display_init();
4480 }
4481
4482 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4483 exit(1);
4484 }
4485
4486 qdev_machine_creation_done();
4487
4488 /* TODO: once all bus devices are qdevified, this should be done
4489 * when bus is created by qdev.c */
4490 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4491 qemu_run_machine_init_done_notifiers();
4492
4493 if (rom_check_and_register_reset() != 0) {
4494 error_report("rom check and register reset failed");
4495 exit(1);
4496 }
4497
4498 replay_start();
4499
4500 /* This checkpoint is required by replay to separate prior clock
4501 reading from the other reads, because timer polling functions query
4502 clock values from the log. */
4503 replay_checkpoint(CHECKPOINT_RESET);
4504 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4505 register_global_state();
4506 if (loadvm) {
4507 Error *local_err = NULL;
4508 if (load_snapshot(loadvm, &local_err) < 0) {
4509 error_report_err(local_err);
4510 autostart = 0;
4511 exit(1);
4512 }
4513 }
4514 if (replay_mode != REPLAY_MODE_NONE) {
4515 replay_vmstate_init();
4516 }
4517
4518 qdev_prop_check_globals();
4519 if (vmstate_dump_file) {
4520 /* dump and exit */
4521 dump_vmstate_json_to_file(vmstate_dump_file);
4522 return 0;
4523 }
4524
4525 if (incoming) {
4526 Error *local_err = NULL;
4527 qemu_start_incoming_migration(incoming, &local_err);
4528 if (local_err) {
4529 error_reportf_err(local_err, "-incoming %s: ", incoming);
4530 exit(1);
4531 }
4532 } else if (autostart) {
4533 vm_start();
4534 }
4535
4536 accel_setup_post(current_machine);
4537 os_setup_post();
4538
4539 main_loop();
4540
4541 gdbserver_cleanup();
4542
4543 /*
4544 * cleaning up the migration object cancels any existing migration
4545 * try to do this early so that it also stops using devices.
4546 */
4547 migration_shutdown();
4548
4549 /* No more vcpu or device emulation activity beyond this point */
4550 vm_shutdown();
4551
4552 job_cancel_sync_all();
4553 bdrv_close_all();
4554
4555 res_free();
4556
4557 /* vhost-user must be cleaned up before chardevs. */
4558 tpm_cleanup();
4559 net_cleanup();
4560 audio_cleanup();
4561 monitor_cleanup();
4562 qemu_chr_cleanup();
4563 user_creatable_cleanup();
4564 /* TODO: unref root container, check all devices are ok */
4565
4566 return 0;
4567 }