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