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