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