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