]> git.proxmox.com Git - mirror_qemu.git/blob - softmmu/vl.c
include: rename qemu-common.h qemu/help-texts.h
[mirror_qemu.git] / softmmu / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include "qemu/help-texts.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "exec/page-vary.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/compat-policy.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qapi/qmp/qjson.h"
37 #include "qemu-version.h"
38 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
40 #include "qemu/hw-version.h"
41 #include "qemu/uuid.h"
42 #include "sysemu/reset.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/runstate-action.h"
45 #include "sysemu/seccomp.h"
46 #include "sysemu/tcg.h"
47 #include "sysemu/xen.h"
48
49 #include "qemu/error-report.h"
50 #include "qemu/sockets.h"
51 #include "qemu/accel.h"
52 #include "hw/usb.h"
53 #include "hw/isa/isa.h"
54 #include "hw/scsi/scsi.h"
55 #include "hw/display/vga.h"
56 #include "sysemu/watchdog.h"
57 #include "hw/firmware/smbios.h"
58 #include "hw/acpi/acpi.h"
59 #include "hw/xen/xen.h"
60 #include "hw/loader.h"
61 #include "monitor/qdev.h"
62 #include "net/net.h"
63 #include "net/slirp.h"
64 #include "monitor/monitor.h"
65 #include "ui/console.h"
66 #include "ui/input.h"
67 #include "sysemu/sysemu.h"
68 #include "sysemu/numa.h"
69 #include "sysemu/hostmem.h"
70 #include "exec/gdbstub.h"
71 #include "qemu/timer.h"
72 #include "chardev/char.h"
73 #include "qemu/bitmap.h"
74 #include "qemu/log.h"
75 #include "sysemu/blockdev.h"
76 #include "hw/block/block.h"
77 #include "hw/i386/x86.h"
78 #include "hw/i386/pc.h"
79 #include "migration/misc.h"
80 #include "migration/snapshot.h"
81 #include "sysemu/tpm.h"
82 #include "sysemu/dma.h"
83 #include "hw/audio/soundhw.h"
84 #include "audio/audio.h"
85 #include "sysemu/cpus.h"
86 #include "sysemu/cpu-timers.h"
87 #include "migration/colo.h"
88 #include "migration/postcopy-ram.h"
89 #include "sysemu/kvm.h"
90 #include "sysemu/hax.h"
91 #include "qapi/qobject-input-visitor.h"
92 #include "qemu/option.h"
93 #include "qemu/config-file.h"
94 #include "qemu/qemu-options.h"
95 #include "qemu/main-loop.h"
96 #ifdef CONFIG_VIRTFS
97 #include "fsdev/qemu-fsdev.h"
98 #endif
99 #include "sysemu/qtest.h"
100
101 #include "disas/disas.h"
102
103 #include "trace.h"
104 #include "trace/control.h"
105 #include "qemu/plugin.h"
106 #include "qemu/queue.h"
107 #include "sysemu/arch_init.h"
108 #include "exec/confidential-guest-support.h"
109
110 #include "ui/qemu-spice.h"
111 #include "qapi/string-input-visitor.h"
112 #include "qapi/opts-visitor.h"
113 #include "qapi/clone-visitor.h"
114 #include "qom/object_interfaces.h"
115 #include "semihosting/semihost.h"
116 #include "crypto/init.h"
117 #include "sysemu/replay.h"
118 #include "qapi/qapi-events-run-state.h"
119 #include "qapi/qapi-visit-block-core.h"
120 #include "qapi/qapi-visit-compat.h"
121 #include "qapi/qapi-visit-ui.h"
122 #include "qapi/qapi-commands-block-core.h"
123 #include "qapi/qapi-commands-migration.h"
124 #include "qapi/qapi-commands-misc.h"
125 #include "qapi/qapi-visit-qom.h"
126 #include "qapi/qapi-commands-ui.h"
127 #include "qapi/qmp/qdict.h"
128 #include "qapi/qmp/qerror.h"
129 #include "sysemu/iothread.h"
130 #include "qemu/guest-random.h"
131
132 #include "config-host.h"
133
134 #define MAX_VIRTIO_CONSOLES 1
135
136 typedef struct BlockdevOptionsQueueEntry {
137 BlockdevOptions *bdo;
138 Location loc;
139 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
140 } BlockdevOptionsQueueEntry;
141
142 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
143
144 typedef struct ObjectOption {
145 ObjectOptions *opts;
146 QTAILQ_ENTRY(ObjectOption) next;
147 } ObjectOption;
148
149 typedef struct DeviceOption {
150 QDict *opts;
151 Location loc;
152 QTAILQ_ENTRY(DeviceOption) next;
153 } DeviceOption;
154
155 static const char *cpu_option;
156 static const char *mem_path;
157 static const char *incoming;
158 static const char *loadvm;
159 static const char *accelerators;
160 static QDict *machine_opts_dict;
161 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
162 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
163 static ram_addr_t maxram_size;
164 static uint64_t ram_slots;
165 static int display_remote;
166 static int snapshot;
167 static bool preconfig_requested;
168 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
169 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
170 static bool nographic = false;
171 static int mem_prealloc; /* force preallocation of physical target memory */
172 static ram_addr_t ram_size;
173 static const char *vga_model = NULL;
174 static DisplayOptions dpy;
175 static int num_serial_hds;
176 static Chardev **serial_hds;
177 static const char *log_mask;
178 static const char *log_file;
179 static bool list_data_dirs;
180 static const char *watchdog;
181 static const char *qtest_chrdev;
182 static const char *qtest_log;
183
184 static int has_defaults = 1;
185 static int default_serial = 1;
186 static int default_parallel = 1;
187 static int default_monitor = 1;
188 static int default_floppy = 1;
189 static int default_cdrom = 1;
190 static int default_sdcard = 1;
191 static int default_vga = 1;
192 static int default_net = 1;
193
194 static struct {
195 const char *driver;
196 int *flag;
197 } default_list[] = {
198 { .driver = "isa-serial", .flag = &default_serial },
199 { .driver = "isa-parallel", .flag = &default_parallel },
200 { .driver = "isa-fdc", .flag = &default_floppy },
201 { .driver = "floppy", .flag = &default_floppy },
202 { .driver = "ide-cd", .flag = &default_cdrom },
203 { .driver = "ide-hd", .flag = &default_cdrom },
204 { .driver = "scsi-cd", .flag = &default_cdrom },
205 { .driver = "scsi-hd", .flag = &default_cdrom },
206 { .driver = "VGA", .flag = &default_vga },
207 { .driver = "isa-vga", .flag = &default_vga },
208 { .driver = "cirrus-vga", .flag = &default_vga },
209 { .driver = "isa-cirrus-vga", .flag = &default_vga },
210 { .driver = "vmware-svga", .flag = &default_vga },
211 { .driver = "qxl-vga", .flag = &default_vga },
212 { .driver = "virtio-vga", .flag = &default_vga },
213 { .driver = "ati-vga", .flag = &default_vga },
214 { .driver = "vhost-user-vga", .flag = &default_vga },
215 { .driver = "virtio-vga-gl", .flag = &default_vga },
216 };
217
218 static QemuOptsList qemu_rtc_opts = {
219 .name = "rtc",
220 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
221 .merge_lists = true,
222 .desc = {
223 {
224 .name = "base",
225 .type = QEMU_OPT_STRING,
226 },{
227 .name = "clock",
228 .type = QEMU_OPT_STRING,
229 },{
230 .name = "driftfix",
231 .type = QEMU_OPT_STRING,
232 },
233 { /* end of list */ }
234 },
235 };
236
237 static QemuOptsList qemu_option_rom_opts = {
238 .name = "option-rom",
239 .implied_opt_name = "romfile",
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
241 .desc = {
242 {
243 .name = "bootindex",
244 .type = QEMU_OPT_NUMBER,
245 }, {
246 .name = "romfile",
247 .type = QEMU_OPT_STRING,
248 },
249 { /* end of list */ }
250 },
251 };
252
253 static QemuOptsList qemu_accel_opts = {
254 .name = "accel",
255 .implied_opt_name = "accel",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
257 .desc = {
258 /*
259 * no elements => accept any
260 * sanity checking will happen later
261 * when setting accelerator properties
262 */
263 { }
264 },
265 };
266
267 static QemuOptsList qemu_boot_opts = {
268 .name = "boot-opts",
269 .implied_opt_name = "order",
270 .merge_lists = true,
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
272 .desc = {
273 {
274 .name = "order",
275 .type = QEMU_OPT_STRING,
276 }, {
277 .name = "once",
278 .type = QEMU_OPT_STRING,
279 }, {
280 .name = "menu",
281 .type = QEMU_OPT_BOOL,
282 }, {
283 .name = "splash",
284 .type = QEMU_OPT_STRING,
285 }, {
286 .name = "splash-time",
287 .type = QEMU_OPT_NUMBER,
288 }, {
289 .name = "reboot-timeout",
290 .type = QEMU_OPT_NUMBER,
291 }, {
292 .name = "strict",
293 .type = QEMU_OPT_BOOL,
294 },
295 { /*End of list */ }
296 },
297 };
298
299 static QemuOptsList qemu_add_fd_opts = {
300 .name = "add-fd",
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
302 .desc = {
303 {
304 .name = "fd",
305 .type = QEMU_OPT_NUMBER,
306 .help = "file descriptor of which a duplicate is added to fd set",
307 },{
308 .name = "set",
309 .type = QEMU_OPT_NUMBER,
310 .help = "ID of the fd set to add fd to",
311 },{
312 .name = "opaque",
313 .type = QEMU_OPT_STRING,
314 .help = "free-form string used to describe fd",
315 },
316 { /* end of list */ }
317 },
318 };
319
320 static QemuOptsList qemu_object_opts = {
321 .name = "object",
322 .implied_opt_name = "qom-type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
324 .desc = {
325 { }
326 },
327 };
328
329 static QemuOptsList qemu_tpmdev_opts = {
330 .name = "tpmdev",
331 .implied_opt_name = "type",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
333 .desc = {
334 /* options are defined in the TPM backends */
335 { /* end of list */ }
336 },
337 };
338
339 static QemuOptsList qemu_overcommit_opts = {
340 .name = "overcommit",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
342 .desc = {
343 {
344 .name = "mem-lock",
345 .type = QEMU_OPT_BOOL,
346 },
347 {
348 .name = "cpu-pm",
349 .type = QEMU_OPT_BOOL,
350 },
351 { /* end of list */ }
352 },
353 };
354
355 static QemuOptsList qemu_msg_opts = {
356 .name = "msg",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
358 .desc = {
359 {
360 .name = "timestamp",
361 .type = QEMU_OPT_BOOL,
362 },
363 {
364 .name = "guest-name",
365 .type = QEMU_OPT_BOOL,
366 .help = "Prepends guest name for error messages but only if "
367 "-name guest is set otherwise option is ignored\n",
368 },
369 { /* end of list */ }
370 },
371 };
372
373 static QemuOptsList qemu_name_opts = {
374 .name = "name",
375 .implied_opt_name = "guest",
376 .merge_lists = true,
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
378 .desc = {
379 {
380 .name = "guest",
381 .type = QEMU_OPT_STRING,
382 .help = "Sets the name of the guest.\n"
383 "This name will be displayed in the SDL window caption.\n"
384 "The name will also be used for the VNC server",
385 }, {
386 .name = "process",
387 .type = QEMU_OPT_STRING,
388 .help = "Sets the name of the QEMU process, as shown in top etc",
389 }, {
390 .name = "debug-threads",
391 .type = QEMU_OPT_BOOL,
392 .help = "When enabled, name the individual threads; defaults off.\n"
393 "NOTE: The thread names are for debugging and not a\n"
394 "stable API.",
395 },
396 { /* End of list */ }
397 },
398 };
399
400 static QemuOptsList qemu_mem_opts = {
401 .name = "memory",
402 .implied_opt_name = "size",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
404 .merge_lists = true,
405 .desc = {
406 {
407 .name = "size",
408 .type = QEMU_OPT_SIZE,
409 },
410 {
411 .name = "slots",
412 .type = QEMU_OPT_NUMBER,
413 },
414 {
415 .name = "maxmem",
416 .type = QEMU_OPT_SIZE,
417 },
418 { /* end of list */ }
419 },
420 };
421
422 static QemuOptsList qemu_icount_opts = {
423 .name = "icount",
424 .implied_opt_name = "shift",
425 .merge_lists = true,
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
427 .desc = {
428 {
429 .name = "shift",
430 .type = QEMU_OPT_STRING,
431 }, {
432 .name = "align",
433 .type = QEMU_OPT_BOOL,
434 }, {
435 .name = "sleep",
436 .type = QEMU_OPT_BOOL,
437 }, {
438 .name = "rr",
439 .type = QEMU_OPT_STRING,
440 }, {
441 .name = "rrfile",
442 .type = QEMU_OPT_STRING,
443 }, {
444 .name = "rrsnapshot",
445 .type = QEMU_OPT_STRING,
446 },
447 { /* end of list */ }
448 },
449 };
450
451 static QemuOptsList qemu_fw_cfg_opts = {
452 .name = "fw_cfg",
453 .implied_opt_name = "name",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
455 .desc = {
456 {
457 .name = "name",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets the fw_cfg name of the blob to be inserted",
460 }, {
461 .name = "file",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the file from which "
464 "the fw_cfg blob will be loaded",
465 }, {
466 .name = "string",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets content of the blob to be inserted from a string",
469 }, {
470 .name = "gen_id",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets id of the object generating the fw_cfg blob "
473 "to be inserted",
474 },
475 { /* end of list */ }
476 },
477 };
478
479 static QemuOptsList qemu_action_opts = {
480 .name = "action",
481 .merge_lists = true,
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
483 .desc = {
484 {
485 .name = "shutdown",
486 .type = QEMU_OPT_STRING,
487 },{
488 .name = "reboot",
489 .type = QEMU_OPT_STRING,
490 },{
491 .name = "panic",
492 .type = QEMU_OPT_STRING,
493 },{
494 .name = "watchdog",
495 .type = QEMU_OPT_STRING,
496 },
497 { /* end of list */ }
498 },
499 };
500
501 const char *qemu_get_vm_name(void)
502 {
503 return qemu_name;
504 }
505
506 static void default_driver_disable(const char *driver)
507 {
508 int i;
509
510 if (!driver) {
511 return;
512 }
513
514 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
515 if (strcmp(default_list[i].driver, driver) != 0)
516 continue;
517 *(default_list[i].flag) = 0;
518 }
519 }
520
521 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
522 {
523 const char *driver = qemu_opt_get(opts, "driver");
524
525 default_driver_disable(driver);
526 return 0;
527 }
528
529 static void default_driver_check_json(void)
530 {
531 DeviceOption *opt;
532
533 QTAILQ_FOREACH(opt, &device_opts, next) {
534 const char *driver = qdict_get_try_str(opt->opts, "driver");
535 default_driver_disable(driver);
536 }
537 }
538
539 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
540 {
541 const char *proc_name;
542
543 if (qemu_opt_get(opts, "debug-threads")) {
544 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
545 }
546 qemu_name = qemu_opt_get(opts, "guest");
547
548 proc_name = qemu_opt_get(opts, "process");
549 if (proc_name) {
550 os_set_proc_name(proc_name);
551 }
552
553 return 0;
554 }
555
556 bool defaults_enabled(void)
557 {
558 return has_defaults;
559 }
560
561 #ifndef _WIN32
562 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
563 {
564 int fd, dupfd, flags;
565 int64_t fdset_id;
566 const char *fd_opaque = NULL;
567 AddfdInfo *fdinfo;
568
569 fd = qemu_opt_get_number(opts, "fd", -1);
570 fdset_id = qemu_opt_get_number(opts, "set", -1);
571 fd_opaque = qemu_opt_get(opts, "opaque");
572
573 if (fd < 0) {
574 error_setg(errp, "fd option is required and must be non-negative");
575 return -1;
576 }
577
578 if (fd <= STDERR_FILENO) {
579 error_setg(errp, "fd cannot be a standard I/O stream");
580 return -1;
581 }
582
583 /*
584 * All fds inherited across exec() necessarily have FD_CLOEXEC
585 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
586 */
587 flags = fcntl(fd, F_GETFD);
588 if (flags == -1 || (flags & FD_CLOEXEC)) {
589 error_setg(errp, "fd is not valid or already in use");
590 return -1;
591 }
592
593 if (fdset_id < 0) {
594 error_setg(errp, "set option is required and must be non-negative");
595 return -1;
596 }
597
598 #ifdef F_DUPFD_CLOEXEC
599 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
600 #else
601 dupfd = dup(fd);
602 if (dupfd != -1) {
603 qemu_set_cloexec(dupfd);
604 }
605 #endif
606 if (dupfd == -1) {
607 error_setg(errp, "error duplicating fd: %s", strerror(errno));
608 return -1;
609 }
610
611 /* add the duplicate fd, and optionally the opaque string, to the fd set */
612 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
613 &error_abort);
614 g_free(fdinfo);
615
616 return 0;
617 }
618
619 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
620 {
621 int fd;
622
623 fd = qemu_opt_get_number(opts, "fd", -1);
624 close(fd);
625
626 return 0;
627 }
628 #endif
629
630 /***********************************************************/
631 /* QEMU Block devices */
632
633 #define HD_OPTS "media=disk"
634 #define CDROM_OPTS "media=cdrom"
635 #define FD_OPTS ""
636 #define PFLASH_OPTS ""
637 #define MTD_OPTS ""
638 #define SD_OPTS ""
639
640 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
641 {
642 BlockInterfaceType *block_default_type = opaque;
643
644 return drive_new(opts, *block_default_type, errp) == NULL;
645 }
646
647 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
648 {
649 if (qemu_opt_get(opts, "snapshot") == NULL) {
650 qemu_opt_set(opts, "snapshot", "on", &error_abort);
651 }
652 return 0;
653 }
654
655 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
656 int index, const char *optstr)
657 {
658 QemuOpts *opts;
659 DriveInfo *dinfo;
660
661 if (!enable || drive_get_by_index(type, index)) {
662 return;
663 }
664
665 opts = drive_add(type, index, NULL, optstr);
666 if (snapshot) {
667 drive_enable_snapshot(NULL, opts, NULL);
668 }
669
670 dinfo = drive_new(opts, type, &error_abort);
671 dinfo->is_default = true;
672
673 }
674
675 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
676 MachineClass *machine_class, int snapshot)
677 {
678 /*
679 * If the currently selected machine wishes to override the
680 * units-per-bus property of its default HBA interface type, do so
681 * now.
682 */
683 if (machine_class->units_per_default_bus) {
684 override_max_devs(machine_class->block_default_type,
685 machine_class->units_per_default_bus);
686 }
687
688 /* open the virtual block devices */
689 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
690 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
691
692 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
693 loc_push_restore(&bdo->loc);
694 qmp_blockdev_add(bdo->bdo, &error_fatal);
695 loc_pop(&bdo->loc);
696 qapi_free_BlockdevOptions(bdo->bdo);
697 g_free(bdo);
698 }
699 if (snapshot) {
700 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
701 NULL, NULL);
702 }
703 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
704 &machine_class->block_default_type, &error_fatal)) {
705 /* We printed help */
706 exit(0);
707 }
708
709 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
710 CDROM_OPTS);
711 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
712 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
713
714 }
715
716 static QemuOptsList qemu_smp_opts = {
717 .name = "smp-opts",
718 .implied_opt_name = "cpus",
719 .merge_lists = true,
720 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
721 .desc = {
722 {
723 .name = "cpus",
724 .type = QEMU_OPT_NUMBER,
725 }, {
726 .name = "sockets",
727 .type = QEMU_OPT_NUMBER,
728 }, {
729 .name = "dies",
730 .type = QEMU_OPT_NUMBER,
731 }, {
732 .name = "clusters",
733 .type = QEMU_OPT_NUMBER,
734 }, {
735 .name = "cores",
736 .type = QEMU_OPT_NUMBER,
737 }, {
738 .name = "threads",
739 .type = QEMU_OPT_NUMBER,
740 }, {
741 .name = "maxcpus",
742 .type = QEMU_OPT_NUMBER,
743 },
744 { /*End of list */ }
745 },
746 };
747
748 static void realtime_init(void)
749 {
750 if (enable_mlock) {
751 if (os_mlock() < 0) {
752 error_report("locking memory failed");
753 exit(1);
754 }
755 }
756 }
757
758
759 static void configure_msg(QemuOpts *opts)
760 {
761 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
762 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
763 }
764
765
766 /***********************************************************/
767 /* USB devices */
768
769 static int usb_device_add(const char *devname)
770 {
771 USBDevice *dev = NULL;
772
773 if (!machine_usb(current_machine)) {
774 return -1;
775 }
776
777 dev = usbdevice_create(devname);
778 if (!dev)
779 return -1;
780
781 return 0;
782 }
783
784 static int usb_parse(const char *cmdline)
785 {
786 int r;
787 r = usb_device_add(cmdline);
788 if (r < 0) {
789 error_report("could not add USB device '%s'", cmdline);
790 }
791 return r;
792 }
793
794 /***********************************************************/
795 /* machine registration */
796
797 static MachineClass *find_machine(const char *name, GSList *machines)
798 {
799 GSList *el;
800
801 for (el = machines; el; el = el->next) {
802 MachineClass *mc = el->data;
803
804 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
805 return mc;
806 }
807 }
808
809 return NULL;
810 }
811
812 static MachineClass *find_default_machine(GSList *machines)
813 {
814 GSList *el;
815 MachineClass *default_machineclass = NULL;
816
817 for (el = machines; el; el = el->next) {
818 MachineClass *mc = el->data;
819
820 if (mc->is_default) {
821 assert(default_machineclass == NULL && "Multiple default machines");
822 default_machineclass = mc;
823 }
824 }
825
826 return default_machineclass;
827 }
828
829 static void version(void)
830 {
831 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
832 QEMU_COPYRIGHT "\n");
833 }
834
835 static void help(int exitcode)
836 {
837 version();
838 printf("usage: %s [options] [disk_image]\n\n"
839 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
840 g_get_prgname());
841
842 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
843 if ((arch_mask) & arch_type) \
844 fputs(opt_help, stdout);
845
846 #define ARCHHEADING(text, arch_mask) \
847 if ((arch_mask) & arch_type) \
848 puts(stringify(text));
849
850 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
851
852 #include "qemu-options.def"
853
854 printf("\nDuring emulation, the following keys are useful:\n"
855 "ctrl-alt-f toggle full screen\n"
856 "ctrl-alt-n switch to virtual console 'n'\n"
857 "ctrl-alt toggle mouse and keyboard grab\n"
858 "\n"
859 "When using -nographic, press 'ctrl-a h' to get some help.\n"
860 "\n"
861 QEMU_HELP_BOTTOM "\n");
862
863 exit(exitcode);
864 }
865
866 #define HAS_ARG 0x0001
867
868 typedef struct QEMUOption {
869 const char *name;
870 int flags;
871 int index;
872 uint32_t arch_mask;
873 } QEMUOption;
874
875 static const QEMUOption qemu_options[] = {
876 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
877
878 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
879 { option, opt_arg, opt_enum, arch_mask },
880 #define DEFHEADING(text)
881 #define ARCHHEADING(text, arch_mask)
882
883 #include "qemu-options.def"
884 { NULL },
885 };
886
887 typedef struct VGAInterfaceInfo {
888 const char *opt_name; /* option name */
889 const char *name; /* human-readable name */
890 /* Class names indicating that support is available.
891 * If no class is specified, the interface is always available */
892 const char *class_names[2];
893 } VGAInterfaceInfo;
894
895 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
896 [VGA_NONE] = {
897 .opt_name = "none",
898 .name = "no graphic card",
899 },
900 [VGA_STD] = {
901 .opt_name = "std",
902 .name = "standard VGA",
903 .class_names = { "VGA", "isa-vga" },
904 },
905 [VGA_CIRRUS] = {
906 .opt_name = "cirrus",
907 .name = "Cirrus VGA",
908 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
909 },
910 [VGA_VMWARE] = {
911 .opt_name = "vmware",
912 .name = "VMWare SVGA",
913 .class_names = { "vmware-svga" },
914 },
915 [VGA_VIRTIO] = {
916 .opt_name = "virtio",
917 .name = "Virtio VGA",
918 .class_names = { "virtio-vga" },
919 },
920 [VGA_QXL] = {
921 .opt_name = "qxl",
922 .name = "QXL VGA",
923 .class_names = { "qxl-vga" },
924 },
925 [VGA_TCX] = {
926 .opt_name = "tcx",
927 .name = "TCX framebuffer",
928 .class_names = { "sun-tcx" },
929 },
930 [VGA_CG3] = {
931 .opt_name = "cg3",
932 .name = "CG3 framebuffer",
933 .class_names = { "cgthree" },
934 },
935 [VGA_XENFB] = {
936 .opt_name = "xenfb",
937 .name = "Xen paravirtualized framebuffer",
938 },
939 };
940
941 static bool vga_interface_available(VGAInterfaceType t)
942 {
943 const VGAInterfaceInfo *ti = &vga_interfaces[t];
944
945 assert(t < VGA_TYPE_MAX);
946 return !ti->class_names[0] ||
947 module_object_class_by_name(ti->class_names[0]) ||
948 module_object_class_by_name(ti->class_names[1]);
949 }
950
951 static const char *
952 get_default_vga_model(const MachineClass *machine_class)
953 {
954 if (machine_class->default_display) {
955 return machine_class->default_display;
956 } else if (vga_interface_available(VGA_CIRRUS)) {
957 return "cirrus";
958 } else if (vga_interface_available(VGA_STD)) {
959 return "std";
960 }
961
962 return NULL;
963 }
964
965 static void select_vgahw(const MachineClass *machine_class, const char *p)
966 {
967 const char *opts;
968 int t;
969
970 if (g_str_equal(p, "help")) {
971 const char *def = get_default_vga_model(machine_class);
972
973 for (t = 0; t < VGA_TYPE_MAX; t++) {
974 const VGAInterfaceInfo *ti = &vga_interfaces[t];
975
976 if (vga_interface_available(t) && ti->opt_name) {
977 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
978 g_str_equal(ti->opt_name, def) ? " (default)" : "");
979 }
980 }
981 exit(0);
982 }
983
984 assert(vga_interface_type == VGA_NONE);
985 for (t = 0; t < VGA_TYPE_MAX; t++) {
986 const VGAInterfaceInfo *ti = &vga_interfaces[t];
987 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
988 if (!vga_interface_available(t)) {
989 error_report("%s not available", ti->name);
990 exit(1);
991 }
992 vga_interface_type = t;
993 break;
994 }
995 }
996 if (t == VGA_TYPE_MAX) {
997 invalid_vga:
998 error_report("unknown vga type: %s", p);
999 exit(1);
1000 }
1001 while (*opts) {
1002 const char *nextopt;
1003
1004 if (strstart(opts, ",retrace=", &nextopt)) {
1005 opts = nextopt;
1006 if (strstart(opts, "dumb", &nextopt))
1007 vga_retrace_method = VGA_RETRACE_DUMB;
1008 else if (strstart(opts, "precise", &nextopt))
1009 vga_retrace_method = VGA_RETRACE_PRECISE;
1010 else goto invalid_vga;
1011 } else goto invalid_vga;
1012 opts = nextopt;
1013 }
1014 }
1015
1016 static void parse_display_qapi(const char *optarg)
1017 {
1018 DisplayOptions *opts;
1019 Visitor *v;
1020
1021 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1022
1023 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1024 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1025
1026 qapi_free_DisplayOptions(opts);
1027 visit_free(v);
1028 }
1029
1030 DisplayOptions *qmp_query_display_options(Error **errp)
1031 {
1032 return QAPI_CLONE(DisplayOptions, &dpy);
1033 }
1034
1035 static void parse_display(const char *p)
1036 {
1037 const char *opts;
1038
1039 if (is_help_option(p)) {
1040 qemu_display_help();
1041 exit(0);
1042 }
1043
1044 if (strstart(p, "sdl", &opts)) {
1045 /*
1046 * sdl DisplayType needs hand-crafted parser instead of
1047 * parse_display_qapi() due to some options not in
1048 * DisplayOptions, specifically:
1049 * - ctrl_grab + alt_grab
1050 * They can't be moved into the QAPI since they use underscores,
1051 * thus they will get replaced by "grab-mod" in the long term
1052 */
1053 #if defined(CONFIG_SDL)
1054 dpy.type = DISPLAY_TYPE_SDL;
1055 while (*opts) {
1056 const char *nextopt;
1057
1058 if (strstart(opts, ",grab-mod=", &nextopt)) {
1059 opts = nextopt;
1060 if (strstart(opts, "lshift-lctrl-lalt", &nextopt)) {
1061 alt_grab = 1;
1062 } else if (strstart(opts, "rctrl", &nextopt)) {
1063 ctrl_grab = 1;
1064 } else {
1065 goto invalid_sdl_args;
1066 }
1067 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1068 opts = nextopt;
1069 if (strstart(opts, "on", &nextopt)) {
1070 alt_grab = 1;
1071 } else if (strstart(opts, "off", &nextopt)) {
1072 alt_grab = 0;
1073 } else {
1074 goto invalid_sdl_args;
1075 }
1076 warn_report("alt_grab is deprecated, use grab-mod instead.");
1077 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1078 opts = nextopt;
1079 if (strstart(opts, "on", &nextopt)) {
1080 ctrl_grab = 1;
1081 } else if (strstart(opts, "off", &nextopt)) {
1082 ctrl_grab = 0;
1083 } else {
1084 goto invalid_sdl_args;
1085 }
1086 warn_report("ctrl_grab is deprecated, use grab-mod instead.");
1087 } else if (strstart(opts, ",window_close=", &nextopt) ||
1088 strstart(opts, ",window-close=", &nextopt)) {
1089 if (strstart(opts, ",window_close=", NULL)) {
1090 warn_report("window_close with an underscore is deprecated,"
1091 " please use window-close instead.");
1092 }
1093 opts = nextopt;
1094 dpy.has_window_close = true;
1095 if (strstart(opts, "on", &nextopt)) {
1096 dpy.window_close = true;
1097 } else if (strstart(opts, "off", &nextopt)) {
1098 dpy.window_close = false;
1099 } else {
1100 goto invalid_sdl_args;
1101 }
1102 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1103 opts = nextopt;
1104 dpy.has_show_cursor = true;
1105 if (strstart(opts, "on", &nextopt)) {
1106 dpy.show_cursor = true;
1107 } else if (strstart(opts, "off", &nextopt)) {
1108 dpy.show_cursor = false;
1109 } else {
1110 goto invalid_sdl_args;
1111 }
1112 } else if (strstart(opts, ",gl=", &nextopt)) {
1113 opts = nextopt;
1114 dpy.has_gl = true;
1115 if (strstart(opts, "on", &nextopt)) {
1116 dpy.gl = DISPLAYGL_MODE_ON;
1117 } else if (strstart(opts, "core", &nextopt)) {
1118 dpy.gl = DISPLAYGL_MODE_CORE;
1119 } else if (strstart(opts, "es", &nextopt)) {
1120 dpy.gl = DISPLAYGL_MODE_ES;
1121 } else if (strstart(opts, "off", &nextopt)) {
1122 dpy.gl = DISPLAYGL_MODE_OFF;
1123 } else {
1124 goto invalid_sdl_args;
1125 }
1126 } else {
1127 invalid_sdl_args:
1128 error_report("invalid SDL option string");
1129 exit(1);
1130 }
1131 opts = nextopt;
1132 }
1133 #else
1134 error_report("SDL display supported is not available in this binary");
1135 exit(1);
1136 #endif
1137 } else if (strstart(p, "vnc", &opts)) {
1138 /*
1139 * vnc isn't a (local) DisplayType but a protocol for remote
1140 * display access.
1141 */
1142 if (*opts == '=') {
1143 vnc_parse(opts + 1);
1144 } else {
1145 error_report("VNC requires a display argument vnc=<display>");
1146 exit(1);
1147 }
1148 } else {
1149 parse_display_qapi(p);
1150 }
1151 }
1152
1153 static inline bool nonempty_str(const char *str)
1154 {
1155 return str && *str;
1156 }
1157
1158 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1159 {
1160 gchar *buf;
1161 size_t size;
1162 const char *name, *file, *str, *gen_id;
1163 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1164
1165 if (fw_cfg == NULL) {
1166 error_setg(errp, "fw_cfg device not available");
1167 return -1;
1168 }
1169 name = qemu_opt_get(opts, "name");
1170 file = qemu_opt_get(opts, "file");
1171 str = qemu_opt_get(opts, "string");
1172 gen_id = qemu_opt_get(opts, "gen_id");
1173
1174 /* we need the name, and exactly one of: file, content string, gen_id */
1175 if (!nonempty_str(name) ||
1176 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1177 error_setg(errp, "name, plus exactly one of file,"
1178 " string and gen_id, are needed");
1179 return -1;
1180 }
1181 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1182 error_setg(errp, "name too long (max. %d char)",
1183 FW_CFG_MAX_FILE_PATH - 1);
1184 return -1;
1185 }
1186 if (nonempty_str(gen_id)) {
1187 /*
1188 * In this particular case where the content is populated
1189 * internally, the "etc/" namespace protection is relaxed,
1190 * so do not emit a warning.
1191 */
1192 } else if (strncmp(name, "opt/", 4) != 0) {
1193 warn_report("externally provided fw_cfg item names "
1194 "should be prefixed with \"opt/\"");
1195 }
1196 if (nonempty_str(str)) {
1197 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1198 buf = g_memdup(str, size);
1199 } else if (nonempty_str(gen_id)) {
1200 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1201 return -1;
1202 }
1203 return 0;
1204 } else {
1205 GError *err = NULL;
1206 if (!g_file_get_contents(file, &buf, &size, &err)) {
1207 error_setg(errp, "can't load %s: %s", file, err->message);
1208 g_error_free(err);
1209 return -1;
1210 }
1211 }
1212 /* For legacy, keep user files in a specific global order. */
1213 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1214 fw_cfg_add_file(fw_cfg, name, buf, size);
1215 fw_cfg_reset_order_override(fw_cfg);
1216 return 0;
1217 }
1218
1219 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1220 {
1221 return qdev_device_help(opts);
1222 }
1223
1224 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1225 {
1226 DeviceState *dev;
1227
1228 dev = qdev_device_add(opts, errp);
1229 if (!dev && *errp) {
1230 error_report_err(*errp);
1231 return -1;
1232 } else if (dev) {
1233 object_unref(OBJECT(dev));
1234 }
1235 return 0;
1236 }
1237
1238 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1239 {
1240 Error *local_err = NULL;
1241
1242 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1243 if (local_err) {
1244 error_propagate(errp, local_err);
1245 return -1;
1246 }
1247 exit(0);
1248 }
1249 return 0;
1250 }
1251
1252 #ifdef CONFIG_VIRTFS
1253 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1254 {
1255 return qemu_fsdev_add(opts, errp);
1256 }
1257 #endif
1258
1259 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1260 {
1261 return monitor_init_opts(opts, errp);
1262 }
1263
1264 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1265 {
1266 static int monitor_device_index = 0;
1267 QemuOpts *opts;
1268 const char *p;
1269 char label[32];
1270
1271 if (strstart(optarg, "chardev:", &p)) {
1272 snprintf(label, sizeof(label), "%s", p);
1273 } else {
1274 snprintf(label, sizeof(label), "compat_monitor%d",
1275 monitor_device_index);
1276 opts = qemu_chr_parse_compat(label, optarg, true);
1277 if (!opts) {
1278 error_report("parse error: %s", optarg);
1279 exit(1);
1280 }
1281 }
1282
1283 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1284 qemu_opt_set(opts, "mode", mode, &error_abort);
1285 qemu_opt_set(opts, "chardev", label, &error_abort);
1286 if (!strcmp(mode, "control")) {
1287 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1288 } else {
1289 assert(pretty == false);
1290 }
1291 monitor_device_index++;
1292 }
1293
1294 struct device_config {
1295 enum {
1296 DEV_USB, /* -usbdevice */
1297 DEV_SERIAL, /* -serial */
1298 DEV_PARALLEL, /* -parallel */
1299 DEV_DEBUGCON, /* -debugcon */
1300 DEV_GDB, /* -gdb, -s */
1301 DEV_SCLP, /* s390 sclp */
1302 } type;
1303 const char *cmdline;
1304 Location loc;
1305 QTAILQ_ENTRY(device_config) next;
1306 };
1307
1308 static QTAILQ_HEAD(, device_config) device_configs =
1309 QTAILQ_HEAD_INITIALIZER(device_configs);
1310
1311 static void add_device_config(int type, const char *cmdline)
1312 {
1313 struct device_config *conf;
1314
1315 conf = g_malloc0(sizeof(*conf));
1316 conf->type = type;
1317 conf->cmdline = cmdline;
1318 loc_save(&conf->loc);
1319 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1320 }
1321
1322 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1323 {
1324 struct device_config *conf;
1325 int rc;
1326
1327 QTAILQ_FOREACH(conf, &device_configs, next) {
1328 if (conf->type != type)
1329 continue;
1330 loc_push_restore(&conf->loc);
1331 rc = func(conf->cmdline);
1332 loc_pop(&conf->loc);
1333 if (rc) {
1334 return rc;
1335 }
1336 }
1337 return 0;
1338 }
1339
1340 static void qemu_disable_default_devices(void)
1341 {
1342 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1343
1344 default_driver_check_json();
1345 qemu_opts_foreach(qemu_find_opts("device"),
1346 default_driver_check, NULL, NULL);
1347 qemu_opts_foreach(qemu_find_opts("global"),
1348 default_driver_check, NULL, NULL);
1349
1350 if (!vga_model && !default_vga) {
1351 vga_interface_type = VGA_DEVICE;
1352 }
1353 if (!has_defaults || machine_class->no_serial) {
1354 default_serial = 0;
1355 }
1356 if (!has_defaults || machine_class->no_parallel) {
1357 default_parallel = 0;
1358 }
1359 if (!has_defaults || machine_class->no_floppy) {
1360 default_floppy = 0;
1361 }
1362 if (!has_defaults || machine_class->no_cdrom) {
1363 default_cdrom = 0;
1364 }
1365 if (!has_defaults || machine_class->no_sdcard) {
1366 default_sdcard = 0;
1367 }
1368 if (!has_defaults) {
1369 default_monitor = 0;
1370 default_net = 0;
1371 default_vga = 0;
1372 }
1373 }
1374
1375 static void qemu_create_default_devices(void)
1376 {
1377 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1378
1379 if (is_daemonized()) {
1380 /* According to documentation and historically, -nographic redirects
1381 * serial port, parallel port and monitor to stdio, which does not work
1382 * with -daemonize. We can redirect these to null instead, but since
1383 * -nographic is legacy, let's just error out.
1384 * We disallow -nographic only if all other ports are not redirected
1385 * explicitly, to not break existing legacy setups which uses
1386 * -nographic _and_ redirects all ports explicitly - this is valid
1387 * usage, -nographic is just a no-op in this case.
1388 */
1389 if (nographic
1390 && (default_parallel || default_serial || default_monitor)) {
1391 error_report("-nographic cannot be used with -daemonize");
1392 exit(1);
1393 }
1394 }
1395
1396 if (nographic) {
1397 if (default_parallel)
1398 add_device_config(DEV_PARALLEL, "null");
1399 if (default_serial && default_monitor) {
1400 add_device_config(DEV_SERIAL, "mon:stdio");
1401 } else {
1402 if (default_serial)
1403 add_device_config(DEV_SERIAL, "stdio");
1404 if (default_monitor)
1405 monitor_parse("stdio", "readline", false);
1406 }
1407 } else {
1408 if (default_serial)
1409 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1410 if (default_parallel)
1411 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1412 if (default_monitor)
1413 monitor_parse("vc:80Cx24C", "readline", false);
1414 }
1415
1416 if (default_net) {
1417 QemuOptsList *net = qemu_find_opts("net");
1418 qemu_opts_parse(net, "nic", true, &error_abort);
1419 #ifdef CONFIG_SLIRP
1420 qemu_opts_parse(net, "user", true, &error_abort);
1421 #endif
1422 }
1423
1424 #if defined(CONFIG_VNC)
1425 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1426 display_remote++;
1427 }
1428 #endif
1429 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1430 if (!qemu_display_find_default(&dpy)) {
1431 dpy.type = DISPLAY_TYPE_NONE;
1432 #if defined(CONFIG_VNC)
1433 vnc_parse("localhost:0,to=99,id=default");
1434 #endif
1435 }
1436 }
1437 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1438 dpy.type = DISPLAY_TYPE_NONE;
1439 }
1440
1441 /* If no default VGA is requested, the default is "none". */
1442 if (default_vga) {
1443 vga_model = get_default_vga_model(machine_class);
1444 }
1445 if (vga_model) {
1446 select_vgahw(machine_class, vga_model);
1447 }
1448 }
1449
1450 static int serial_parse(const char *devname)
1451 {
1452 int index = num_serial_hds;
1453 char label[32];
1454
1455 if (strcmp(devname, "none") == 0)
1456 return 0;
1457 snprintf(label, sizeof(label), "serial%d", index);
1458 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1459
1460 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1461 if (!serial_hds[index]) {
1462 error_report("could not connect serial device"
1463 " to character backend '%s'", devname);
1464 return -1;
1465 }
1466 num_serial_hds++;
1467 return 0;
1468 }
1469
1470 Chardev *serial_hd(int i)
1471 {
1472 assert(i >= 0);
1473 if (i < num_serial_hds) {
1474 return serial_hds[i];
1475 }
1476 return NULL;
1477 }
1478
1479 static int parallel_parse(const char *devname)
1480 {
1481 static int index = 0;
1482 char label[32];
1483
1484 if (strcmp(devname, "none") == 0)
1485 return 0;
1486 if (index == MAX_PARALLEL_PORTS) {
1487 error_report("too many parallel ports");
1488 exit(1);
1489 }
1490 snprintf(label, sizeof(label), "parallel%d", index);
1491 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1492 if (!parallel_hds[index]) {
1493 error_report("could not connect parallel device"
1494 " to character backend '%s'", devname);
1495 return -1;
1496 }
1497 index++;
1498 return 0;
1499 }
1500
1501 static int debugcon_parse(const char *devname)
1502 {
1503 QemuOpts *opts;
1504
1505 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1506 error_report("invalid character backend '%s'", devname);
1507 exit(1);
1508 }
1509 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1510 if (!opts) {
1511 error_report("already have a debugcon device");
1512 exit(1);
1513 }
1514 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1515 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1516 return 0;
1517 }
1518
1519 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1520 {
1521 const MachineClass *mc1 = a, *mc2 = b;
1522 int res;
1523
1524 if (mc1->family == NULL) {
1525 if (mc2->family == NULL) {
1526 /* Compare standalone machine types against each other; they sort
1527 * in increasing order.
1528 */
1529 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1530 object_class_get_name(OBJECT_CLASS(mc2)));
1531 }
1532
1533 /* Standalone machine types sort after families. */
1534 return 1;
1535 }
1536
1537 if (mc2->family == NULL) {
1538 /* Families sort before standalone machine types. */
1539 return -1;
1540 }
1541
1542 /* Families sort between each other alphabetically increasingly. */
1543 res = strcmp(mc1->family, mc2->family);
1544 if (res != 0) {
1545 return res;
1546 }
1547
1548 /* Within the same family, machine types sort in decreasing order. */
1549 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1550 object_class_get_name(OBJECT_CLASS(mc1)));
1551 }
1552
1553 static void machine_help_func(const QDict *qdict)
1554 {
1555 GSList *machines, *el;
1556 const char *type = qdict_get_try_str(qdict, "type");
1557
1558 machines = object_class_get_list(TYPE_MACHINE, false);
1559 if (type) {
1560 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1561 if (machine_class) {
1562 type_print_class_properties(object_class_get_name(machine_class));
1563 return;
1564 }
1565 }
1566
1567 printf("Supported machines are:\n");
1568 machines = g_slist_sort(machines, machine_class_cmp);
1569 for (el = machines; el; el = el->next) {
1570 MachineClass *mc = el->data;
1571 if (mc->alias) {
1572 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1573 }
1574 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1575 mc->is_default ? " (default)" : "",
1576 mc->deprecation_reason ? " (deprecated)" : "");
1577 }
1578 }
1579
1580 static void
1581 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1582 {
1583 QDict *opts;
1584
1585 opts = qdict_new();
1586 /* Preserve the caller's reference to prop. */
1587 qobject_ref(prop);
1588 qdict_put(opts, propname, prop);
1589 keyval_merge(machine_opts_dict, opts, errp);
1590 qobject_unref(opts);
1591 }
1592
1593 static void
1594 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1595 const char *arg)
1596 {
1597 QDict *prop = NULL;
1598 bool help = false;
1599
1600 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1601 if (help) {
1602 qemu_opts_print_help(opts_list, true);
1603 exit(0);
1604 }
1605 machine_merge_property(propname, prop, &error_fatal);
1606 qobject_unref(prop);
1607 }
1608
1609 static const char *pid_file;
1610 static Notifier qemu_unlink_pidfile_notifier;
1611
1612 static void qemu_unlink_pidfile(Notifier *n, void *data)
1613 {
1614 if (pid_file) {
1615 unlink(pid_file);
1616 }
1617 }
1618
1619 static const QEMUOption *lookup_opt(int argc, char **argv,
1620 const char **poptarg, int *poptind)
1621 {
1622 const QEMUOption *popt;
1623 int optind = *poptind;
1624 char *r = argv[optind];
1625 const char *optarg;
1626
1627 loc_set_cmdline(argv, optind, 1);
1628 optind++;
1629 /* Treat --foo the same as -foo. */
1630 if (r[1] == '-')
1631 r++;
1632 popt = qemu_options;
1633 for(;;) {
1634 if (!popt->name) {
1635 error_report("invalid option");
1636 exit(1);
1637 }
1638 if (!strcmp(popt->name, r + 1))
1639 break;
1640 popt++;
1641 }
1642 if (popt->flags & HAS_ARG) {
1643 if (optind >= argc) {
1644 error_report("requires an argument");
1645 exit(1);
1646 }
1647 optarg = argv[optind++];
1648 loc_set_cmdline(argv, optind - 2, 2);
1649 } else {
1650 optarg = NULL;
1651 }
1652
1653 *poptarg = optarg;
1654 *poptind = optind;
1655
1656 return popt;
1657 }
1658
1659 static MachineClass *select_machine(QDict *qdict, Error **errp)
1660 {
1661 const char *optarg = qdict_get_try_str(qdict, "type");
1662 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1663 MachineClass *machine_class;
1664 Error *local_err = NULL;
1665
1666 if (optarg) {
1667 machine_class = find_machine(optarg, machines);
1668 qdict_del(qdict, "type");
1669 if (!machine_class) {
1670 error_setg(&local_err, "unsupported machine type");
1671 }
1672 } else {
1673 machine_class = find_default_machine(machines);
1674 if (!machine_class) {
1675 error_setg(&local_err, "No machine specified, and there is no default");
1676 }
1677 }
1678
1679 g_slist_free(machines);
1680 if (local_err) {
1681 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1682 error_propagate(errp, local_err);
1683 }
1684 return machine_class;
1685 }
1686
1687 static int object_parse_property_opt(Object *obj,
1688 const char *name, const char *value,
1689 const char *skip, Error **errp)
1690 {
1691 if (g_str_equal(name, skip)) {
1692 return 0;
1693 }
1694
1695 if (!object_property_parse(obj, name, value, errp)) {
1696 return -1;
1697 }
1698
1699 return 0;
1700 }
1701
1702 /* *Non*recursively replace underscores with dashes in QDict keys. */
1703 static void keyval_dashify(QDict *qdict, Error **errp)
1704 {
1705 const QDictEntry *ent, *next;
1706 char *p;
1707
1708 for (ent = qdict_first(qdict); ent; ent = next) {
1709 g_autofree char *new_key = NULL;
1710
1711 next = qdict_next(qdict, ent);
1712 if (!strchr(ent->key, '_')) {
1713 continue;
1714 }
1715 new_key = g_strdup(ent->key);
1716 for (p = new_key; *p; p++) {
1717 if (*p == '_') {
1718 *p = '-';
1719 }
1720 }
1721 if (qdict_haskey(qdict, new_key)) {
1722 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1723 return;
1724 }
1725 qobject_ref(ent->value);
1726 qdict_put_obj(qdict, new_key, ent->value);
1727 qdict_del(qdict, ent->key);
1728 }
1729 }
1730
1731 static void qemu_apply_legacy_machine_options(QDict *qdict)
1732 {
1733 const char *value;
1734
1735 keyval_dashify(qdict, &error_fatal);
1736
1737 /* Legacy options do not correspond to MachineState properties. */
1738 value = qdict_get_try_str(qdict, "accel");
1739 if (value) {
1740 accelerators = g_strdup(value);
1741 qdict_del(qdict, "accel");
1742 }
1743
1744 value = qdict_get_try_str(qdict, "igd-passthru");
1745 if (value) {
1746 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1747 false);
1748 qdict_del(qdict, "igd-passthru");
1749 }
1750
1751 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1752 if (value) {
1753 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1754 false);
1755 qdict_del(qdict, "kvm-shadow-mem");
1756 }
1757
1758 value = qdict_get_try_str(qdict, "kernel-irqchip");
1759 if (value) {
1760 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1761 false);
1762 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1763 false);
1764 qdict_del(qdict, "kernel-irqchip");
1765 }
1766 }
1767
1768 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1769 {
1770 ObjectOption *opt, *next;
1771
1772 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1773 const char *type = ObjectType_str(opt->opts->qom_type);
1774 if (type_opt_predicate(type)) {
1775 user_creatable_add_qapi(opt->opts, &error_fatal);
1776 qapi_free_ObjectOptions(opt->opts);
1777 QTAILQ_REMOVE(&object_opts, opt, next);
1778 g_free(opt);
1779 }
1780 }
1781 }
1782
1783 static void object_option_add_visitor(Visitor *v)
1784 {
1785 ObjectOption *opt = g_new0(ObjectOption, 1);
1786 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1787 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1788 }
1789
1790 static void object_option_parse(const char *optarg)
1791 {
1792 QemuOpts *opts;
1793 const char *type;
1794 Visitor *v;
1795
1796 if (optarg[0] == '{') {
1797 QObject *obj = qobject_from_json(optarg, &error_fatal);
1798
1799 v = qobject_input_visitor_new(obj);
1800 qobject_unref(obj);
1801 } else {
1802 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1803 optarg, true);
1804 if (!opts) {
1805 exit(1);
1806 }
1807
1808 type = qemu_opt_get(opts, "qom-type");
1809 if (!type) {
1810 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1811 }
1812 if (user_creatable_print_help(type, opts)) {
1813 exit(0);
1814 }
1815
1816 v = opts_visitor_new(opts);
1817 }
1818
1819 object_option_add_visitor(v);
1820 visit_free(v);
1821 }
1822
1823 /*
1824 * Initial object creation happens before all other
1825 * QEMU data types are created. The majority of objects
1826 * can be created at this point. The rng-egd object
1827 * cannot be created here, as it depends on the chardev
1828 * already existing.
1829 */
1830 static bool object_create_early(const char *type)
1831 {
1832 /*
1833 * Objects should not be made "delayed" without a reason. If you
1834 * add one, state the reason in a comment!
1835 */
1836
1837 /* Reason: property "chardev" */
1838 if (g_str_equal(type, "rng-egd") ||
1839 g_str_equal(type, "qtest")) {
1840 return false;
1841 }
1842
1843 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1844 /* Reason: cryptodev-vhost-user property "chardev" */
1845 if (g_str_equal(type, "cryptodev-vhost-user")) {
1846 return false;
1847 }
1848 #endif
1849
1850 /* Reason: vhost-user-blk-server property "node-name" */
1851 if (g_str_equal(type, "vhost-user-blk-server")) {
1852 return false;
1853 }
1854 /*
1855 * Reason: filter-* property "netdev" etc.
1856 */
1857 if (g_str_equal(type, "filter-buffer") ||
1858 g_str_equal(type, "filter-dump") ||
1859 g_str_equal(type, "filter-mirror") ||
1860 g_str_equal(type, "filter-redirector") ||
1861 g_str_equal(type, "colo-compare") ||
1862 g_str_equal(type, "filter-rewriter") ||
1863 g_str_equal(type, "filter-replay")) {
1864 return false;
1865 }
1866
1867 /*
1868 * Allocation of large amounts of memory may delay
1869 * chardev initialization for too long, and trigger timeouts
1870 * on software that waits for a monitor socket to be created
1871 * (e.g. libvirt).
1872 */
1873 if (g_str_has_prefix(type, "memory-backend-")) {
1874 return false;
1875 }
1876
1877 return true;
1878 }
1879
1880 static void qemu_apply_machine_options(QDict *qdict)
1881 {
1882 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1883 const char *boot_order = NULL;
1884 const char *boot_once = NULL;
1885 QemuOpts *opts;
1886
1887 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1888 current_machine->ram_size = ram_size;
1889 current_machine->maxram_size = maxram_size;
1890 current_machine->ram_slots = ram_slots;
1891
1892 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1893 if (opts) {
1894 boot_order = qemu_opt_get(opts, "order");
1895 if (boot_order) {
1896 validate_bootdevices(boot_order, &error_fatal);
1897 }
1898
1899 boot_once = qemu_opt_get(opts, "once");
1900 if (boot_once) {
1901 validate_bootdevices(boot_once, &error_fatal);
1902 }
1903
1904 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1905 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1906 }
1907
1908 if (!boot_order) {
1909 boot_order = machine_class->default_boot_order;
1910 }
1911
1912 current_machine->boot_order = boot_order;
1913 current_machine->boot_once = boot_once;
1914
1915 if (semihosting_enabled() && !semihosting_get_argc()) {
1916 /* fall back to the -kernel/-append */
1917 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1918 }
1919
1920 if (current_machine->smp.cpus > 1) {
1921 Error *blocker = NULL;
1922 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1923 replay_add_blocker(blocker);
1924 }
1925 }
1926
1927 static void qemu_create_early_backends(void)
1928 {
1929 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1930 #if defined(CONFIG_SDL)
1931 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1932 #else
1933 const bool use_sdl = false;
1934 #endif
1935 #if defined(CONFIG_GTK)
1936 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1937 #else
1938 const bool use_gtk = false;
1939 #endif
1940
1941 if ((alt_grab || ctrl_grab) && !use_sdl) {
1942 error_report("-alt-grab and -ctrl-grab are only valid "
1943 "for SDL, ignoring option");
1944 }
1945 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1946 error_report("window-close is only valid for GTK and SDL, "
1947 "ignoring option");
1948 }
1949
1950 qemu_display_early_init(&dpy);
1951 qemu_console_early_init();
1952
1953 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1954 #if defined(CONFIG_OPENGL)
1955 error_report("OpenGL is not supported by the display");
1956 #else
1957 error_report("OpenGL support is disabled");
1958 #endif
1959 exit(1);
1960 }
1961
1962 object_option_foreach_add(object_create_early);
1963
1964 /* spice needs the timers to be initialized by this point */
1965 /* spice must initialize before audio as it changes the default audiodev */
1966 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1967 qemu_spice.init();
1968
1969 qemu_opts_foreach(qemu_find_opts("chardev"),
1970 chardev_init_func, NULL, &error_fatal);
1971
1972 #ifdef CONFIG_VIRTFS
1973 qemu_opts_foreach(qemu_find_opts("fsdev"),
1974 fsdev_init_func, NULL, &error_fatal);
1975 #endif
1976
1977 /*
1978 * Note: we need to create audio and block backends before
1979 * setting machine properties, so they can be referred to.
1980 */
1981 configure_blockdev(&bdo_queue, machine_class, snapshot);
1982 audio_init_audiodevs();
1983 }
1984
1985
1986 /*
1987 * The remainder of object creation happens after the
1988 * creation of chardev, fsdev, net clients and device data types.
1989 */
1990 static bool object_create_late(const char *type)
1991 {
1992 return !object_create_early(type);
1993 }
1994
1995 static void qemu_create_late_backends(void)
1996 {
1997 if (qtest_chrdev) {
1998 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1999 }
2000
2001 net_init_clients(&error_fatal);
2002
2003 object_option_foreach_add(object_create_late);
2004
2005 if (tpm_init() < 0) {
2006 exit(1);
2007 }
2008
2009 qemu_opts_foreach(qemu_find_opts("mon"),
2010 mon_init_func, NULL, &error_fatal);
2011
2012 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2013 exit(1);
2014 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2015 exit(1);
2016 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2017 exit(1);
2018
2019 /* now chardevs have been created we may have semihosting to connect */
2020 qemu_semihosting_connect_chardevs();
2021 qemu_semihosting_console_init();
2022 }
2023
2024 static bool have_custom_ram_size(void)
2025 {
2026 QemuOpts *opts = qemu_find_opts_singleton("memory");
2027 return !!qemu_opt_get_size(opts, "size", 0);
2028 }
2029
2030 static void qemu_resolve_machine_memdev(void)
2031 {
2032 if (current_machine->ram_memdev_id) {
2033 Object *backend;
2034 ram_addr_t backend_size;
2035
2036 backend = object_resolve_path_type(current_machine->ram_memdev_id,
2037 TYPE_MEMORY_BACKEND, NULL);
2038 if (!backend) {
2039 error_report("Memory backend '%s' not found",
2040 current_machine->ram_memdev_id);
2041 exit(EXIT_FAILURE);
2042 }
2043 backend_size = object_property_get_uint(backend, "size", &error_abort);
2044 if (have_custom_ram_size() && backend_size != ram_size) {
2045 error_report("Size specified by -m option must match size of "
2046 "explicitly specified 'memory-backend' property");
2047 exit(EXIT_FAILURE);
2048 }
2049 if (mem_path) {
2050 error_report("'-mem-path' can't be used together with"
2051 "'-machine memory-backend'");
2052 exit(EXIT_FAILURE);
2053 }
2054 ram_size = backend_size;
2055 }
2056
2057 if (!xen_enabled()) {
2058 /* On 32-bit hosts, QEMU is limited by virtual address space */
2059 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2060 error_report("at most 2047 MB RAM can be simulated");
2061 exit(1);
2062 }
2063 }
2064 }
2065
2066 static void set_memory_options(MachineClass *mc)
2067 {
2068 uint64_t sz;
2069 const char *mem_str;
2070 const ram_addr_t default_ram_size = mc->default_ram_size;
2071 QemuOpts *opts = qemu_find_opts_singleton("memory");
2072 Location loc;
2073
2074 loc_push_none(&loc);
2075 qemu_opts_loc_restore(opts);
2076
2077 sz = 0;
2078 mem_str = qemu_opt_get(opts, "size");
2079 if (mem_str) {
2080 if (!*mem_str) {
2081 error_report("missing 'size' option value");
2082 exit(EXIT_FAILURE);
2083 }
2084
2085 sz = qemu_opt_get_size(opts, "size", ram_size);
2086
2087 /* Fix up legacy suffix-less format */
2088 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2089 uint64_t overflow_check = sz;
2090
2091 sz *= MiB;
2092 if (sz / MiB != overflow_check) {
2093 error_report("too large 'size' option value");
2094 exit(EXIT_FAILURE);
2095 }
2096 }
2097 }
2098
2099 /* backward compatibility behaviour for case "-m 0" */
2100 if (sz == 0) {
2101 sz = default_ram_size;
2102 }
2103
2104 sz = QEMU_ALIGN_UP(sz, 8192);
2105 if (mc->fixup_ram_size) {
2106 sz = mc->fixup_ram_size(sz);
2107 }
2108 ram_size = sz;
2109 if (ram_size != sz) {
2110 error_report("ram size too large");
2111 exit(EXIT_FAILURE);
2112 }
2113
2114 maxram_size = ram_size;
2115
2116 if (qemu_opt_get(opts, "maxmem")) {
2117 uint64_t slots;
2118
2119 sz = qemu_opt_get_size(opts, "maxmem", 0);
2120 slots = qemu_opt_get_number(opts, "slots", 0);
2121 if (sz < ram_size) {
2122 error_report("invalid value of -m option maxmem: "
2123 "maximum memory size (0x%" PRIx64 ") must be at least "
2124 "the initial memory size (0x" RAM_ADDR_FMT ")",
2125 sz, ram_size);
2126 exit(EXIT_FAILURE);
2127 } else if (slots && sz == ram_size) {
2128 error_report("invalid value of -m option maxmem: "
2129 "memory slots were specified but maximum memory size "
2130 "(0x%" PRIx64 ") is equal to the initial memory size "
2131 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2132 exit(EXIT_FAILURE);
2133 }
2134
2135 maxram_size = sz;
2136 ram_slots = slots;
2137 } else if (qemu_opt_get(opts, "slots")) {
2138 error_report("invalid -m option value: missing 'maxmem' option");
2139 exit(EXIT_FAILURE);
2140 }
2141
2142 loc_pop(&loc);
2143 }
2144
2145 static void qemu_create_machine(QDict *qdict)
2146 {
2147 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2148 object_set_machine_compat_props(machine_class->compat_props);
2149
2150 set_memory_options(machine_class);
2151
2152 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2153 object_property_add_child(object_get_root(), "machine",
2154 OBJECT(current_machine));
2155 object_property_add_child(container_get(OBJECT(current_machine),
2156 "/unattached"),
2157 "sysbus", OBJECT(sysbus_get_default()));
2158
2159 if (machine_class->minimum_page_bits) {
2160 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2161 /* This would be a board error: specifying a minimum smaller than
2162 * a target's compile-time fixed setting.
2163 */
2164 g_assert_not_reached();
2165 }
2166 }
2167
2168 cpu_exec_init_all();
2169 page_size_init();
2170
2171 if (machine_class->hw_version) {
2172 qemu_set_hw_version(machine_class->hw_version);
2173 }
2174
2175 /*
2176 * Get the default machine options from the machine if it is not already
2177 * specified either by the configuration file or by the command line.
2178 */
2179 if (machine_class->default_machine_opts) {
2180 QDict *default_opts =
2181 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2182 &error_abort);
2183 qemu_apply_legacy_machine_options(default_opts);
2184 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2185 false, &error_abort);
2186 qobject_unref(default_opts);
2187 }
2188 }
2189
2190 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2191 {
2192 GlobalProperty *g;
2193
2194 g = g_malloc0(sizeof(*g));
2195 g->driver = qemu_opt_get(opts, "driver");
2196 g->property = qemu_opt_get(opts, "property");
2197 g->value = qemu_opt_get(opts, "value");
2198 qdev_prop_register_global(g);
2199 return 0;
2200 }
2201
2202 /*
2203 * Return whether configuration group @group is stored in QemuOpts, or
2204 * recorded as one or more QDicts by qemu_record_config_group.
2205 */
2206 static bool is_qemuopts_group(const char *group)
2207 {
2208 if (g_str_equal(group, "object") ||
2209 g_str_equal(group, "machine") ||
2210 g_str_equal(group, "smp-opts")) {
2211 return false;
2212 }
2213 return true;
2214 }
2215
2216 static void qemu_record_config_group(const char *group, QDict *dict,
2217 bool from_json, Error **errp)
2218 {
2219 if (g_str_equal(group, "object")) {
2220 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2221 object_option_add_visitor(v);
2222 visit_free(v);
2223 } else if (g_str_equal(group, "machine")) {
2224 /*
2225 * Cannot merge string-valued and type-safe dictionaries, so JSON
2226 * is not accepted yet for -M.
2227 */
2228 assert(!from_json);
2229 keyval_merge(machine_opts_dict, dict, errp);
2230 } else if (g_str_equal(group, "smp-opts")) {
2231 machine_merge_property("smp", dict, &error_fatal);
2232 } else {
2233 abort();
2234 }
2235 }
2236
2237 /*
2238 * Parse non-QemuOpts config file groups, pass the rest to
2239 * qemu_config_do_parse.
2240 */
2241 static void qemu_parse_config_group(const char *group, QDict *qdict,
2242 void *opaque, Error **errp)
2243 {
2244 QObject *crumpled;
2245 if (is_qemuopts_group(group)) {
2246 qemu_config_do_parse(group, qdict, opaque, errp);
2247 return;
2248 }
2249
2250 crumpled = qdict_crumple(qdict, errp);
2251 if (!crumpled) {
2252 return;
2253 }
2254 switch (qobject_type(crumpled)) {
2255 case QTYPE_QDICT:
2256 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2257 break;
2258 case QTYPE_QLIST:
2259 error_setg(errp, "Lists cannot be at top level of a configuration section");
2260 break;
2261 default:
2262 g_assert_not_reached();
2263 }
2264 qobject_unref(crumpled);
2265 }
2266
2267 static void qemu_read_default_config_file(Error **errp)
2268 {
2269 ERRP_GUARD();
2270 int ret;
2271 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2272
2273 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2274 if (ret < 0) {
2275 if (ret == -ENOENT) {
2276 error_free(*errp);
2277 *errp = NULL;
2278 }
2279 }
2280 }
2281
2282 static void qemu_set_option(const char *str, Error **errp)
2283 {
2284 char group[64], id[64], arg[64];
2285 QemuOptsList *list;
2286 QemuOpts *opts;
2287 int rc, offset;
2288
2289 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2290 if (rc < 3 || str[offset] != '=') {
2291 error_setg(errp, "can't parse: \"%s\"", str);
2292 return;
2293 }
2294
2295 if (!is_qemuopts_group(group)) {
2296 error_setg(errp, "-set is not supported with %s", group);
2297 } else {
2298 list = qemu_find_opts_err(group, errp);
2299 if (list) {
2300 opts = qemu_opts_find(list, id);
2301 if (!opts) {
2302 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2303 return;
2304 }
2305 qemu_opt_set(opts, arg, str + offset + 1, errp);
2306 }
2307 }
2308 }
2309
2310 static void user_register_global_props(void)
2311 {
2312 qemu_opts_foreach(qemu_find_opts("global"),
2313 global_init_func, NULL, NULL);
2314 }
2315
2316 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2317 {
2318 icount_configure(opts, errp);
2319 return 0;
2320 }
2321
2322 static int accelerator_set_property(void *opaque,
2323 const char *name, const char *value,
2324 Error **errp)
2325 {
2326 return object_parse_property_opt(opaque, name, value, "accel", errp);
2327 }
2328
2329 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2330 {
2331 bool *p_init_failed = opaque;
2332 const char *acc = qemu_opt_get(opts, "accel");
2333 AccelClass *ac = accel_find(acc);
2334 AccelState *accel;
2335 int ret;
2336 bool qtest_with_kvm;
2337
2338 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2339
2340 if (!ac) {
2341 *p_init_failed = true;
2342 if (!qtest_with_kvm) {
2343 error_report("invalid accelerator %s", acc);
2344 }
2345 return 0;
2346 }
2347 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2348 object_apply_compat_props(OBJECT(accel));
2349 qemu_opt_foreach(opts, accelerator_set_property,
2350 accel,
2351 &error_fatal);
2352
2353 ret = accel_init_machine(accel, current_machine);
2354 if (ret < 0) {
2355 *p_init_failed = true;
2356 if (!qtest_with_kvm || ret != -ENOENT) {
2357 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2358 }
2359 return 0;
2360 }
2361
2362 return 1;
2363 }
2364
2365 static void configure_accelerators(const char *progname)
2366 {
2367 bool init_failed = false;
2368
2369 qemu_opts_foreach(qemu_find_opts("icount"),
2370 do_configure_icount, NULL, &error_fatal);
2371
2372 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2373 char **accel_list, **tmp;
2374
2375 if (accelerators == NULL) {
2376 /* Select the default accelerator */
2377 bool have_tcg = accel_find("tcg");
2378 bool have_kvm = accel_find("kvm");
2379
2380 if (have_tcg && have_kvm) {
2381 if (g_str_has_suffix(progname, "kvm")) {
2382 /* If the program name ends with "kvm", we prefer KVM */
2383 accelerators = "kvm:tcg";
2384 } else {
2385 accelerators = "tcg:kvm";
2386 }
2387 } else if (have_kvm) {
2388 accelerators = "kvm";
2389 } else if (have_tcg) {
2390 accelerators = "tcg";
2391 } else {
2392 error_report("No accelerator selected and"
2393 " no default accelerator available");
2394 exit(1);
2395 }
2396 }
2397 accel_list = g_strsplit(accelerators, ":", 0);
2398
2399 for (tmp = accel_list; *tmp; tmp++) {
2400 /*
2401 * Filter invalid accelerators here, to prevent obscenities
2402 * such as "-machine accel=tcg,,thread=single".
2403 */
2404 if (accel_find(*tmp)) {
2405 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2406 } else {
2407 init_failed = true;
2408 error_report("invalid accelerator %s", *tmp);
2409 }
2410 }
2411 g_strfreev(accel_list);
2412 } else {
2413 if (accelerators != NULL) {
2414 error_report("The -accel and \"-machine accel=\" options are incompatible");
2415 exit(1);
2416 }
2417 }
2418
2419 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2420 do_configure_accelerator, &init_failed, &error_fatal)) {
2421 if (!init_failed) {
2422 error_report("no accelerator found");
2423 }
2424 exit(1);
2425 }
2426
2427 if (init_failed && !qtest_chrdev) {
2428 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2429 error_report("falling back to %s", ac->name);
2430 }
2431
2432 if (icount_enabled() && !tcg_enabled()) {
2433 error_report("-icount is not allowed with hardware virtualization");
2434 exit(1);
2435 }
2436 }
2437
2438 static void create_default_memdev(MachineState *ms, const char *path)
2439 {
2440 Object *obj;
2441 MachineClass *mc = MACHINE_GET_CLASS(ms);
2442
2443 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2444 if (path) {
2445 object_property_set_str(obj, "mem-path", path, &error_fatal);
2446 }
2447 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2448 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2449 obj);
2450 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2451 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2452 false, &error_fatal);
2453 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2454 object_unref(obj);
2455 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2456 &error_fatal);
2457 }
2458
2459 static void qemu_validate_options(const QDict *machine_opts)
2460 {
2461 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2462 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2463 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2464
2465 if (kernel_filename == NULL) {
2466 if (kernel_cmdline != NULL) {
2467 error_report("-append only allowed with -kernel option");
2468 exit(1);
2469 }
2470
2471 if (initrd_filename != NULL) {
2472 error_report("-initrd only allowed with -kernel option");
2473 exit(1);
2474 }
2475 }
2476
2477 if (loadvm && preconfig_requested) {
2478 error_report("'preconfig' and 'loadvm' options are "
2479 "mutually exclusive");
2480 exit(EXIT_FAILURE);
2481 }
2482 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2483 error_report("'preconfig' supports '-incoming defer' only");
2484 exit(EXIT_FAILURE);
2485 }
2486
2487 #ifdef CONFIG_CURSES
2488 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2489 error_report("curses display cannot be used with -daemonize");
2490 exit(1);
2491 }
2492 #endif
2493 }
2494
2495 static void qemu_process_sugar_options(void)
2496 {
2497 if (mem_prealloc) {
2498 QObject *smp = qdict_get(machine_opts_dict, "smp");
2499 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2500 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2501 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2502 const char *val = qstring_get_str(qobject_to(QString, cpus));
2503 object_register_sugar_prop("memory-backend", "prealloc-threads",
2504 val, false);
2505 }
2506 }
2507 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2508 }
2509
2510 if (watchdog) {
2511 int i = select_watchdog(watchdog);
2512 if (i > 0)
2513 exit (i == 1 ? 1 : 0);
2514 }
2515 }
2516
2517 /* -action processing */
2518
2519 /*
2520 * Process all the -action parameters parsed from cmdline.
2521 */
2522 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2523 {
2524 Error *local_err = NULL;
2525 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2526 QObject *ret = NULL;
2527 qmp_marshal_set_action(qdict, &ret, &local_err);
2528 qobject_unref(ret);
2529 qobject_unref(qdict);
2530 if (local_err) {
2531 error_propagate(errp, local_err);
2532 return 1;
2533 }
2534 return 0;
2535 }
2536
2537 static void qemu_process_early_options(void)
2538 {
2539 #ifdef CONFIG_SECCOMP
2540 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2541 if (olist) {
2542 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2543 }
2544 #endif
2545
2546 qemu_opts_foreach(qemu_find_opts("name"),
2547 parse_name, NULL, &error_fatal);
2548
2549 if (qemu_opts_foreach(qemu_find_opts("action"),
2550 process_runstate_actions, NULL, &error_fatal)) {
2551 exit(1);
2552 }
2553
2554 #ifndef _WIN32
2555 qemu_opts_foreach(qemu_find_opts("add-fd"),
2556 parse_add_fd, NULL, &error_fatal);
2557
2558 qemu_opts_foreach(qemu_find_opts("add-fd"),
2559 cleanup_add_fd, NULL, &error_fatal);
2560 #endif
2561
2562 /* Open the logfile at this point and set the log mask if necessary. */
2563 {
2564 int mask = 0;
2565 if (log_mask) {
2566 mask = qemu_str_to_log_mask(log_mask);
2567 if (!mask) {
2568 qemu_print_log_usage(stdout);
2569 exit(1);
2570 }
2571 }
2572 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
2573 }
2574
2575 qemu_add_default_firmwarepath();
2576 }
2577
2578 static void qemu_process_help_options(void)
2579 {
2580 /*
2581 * Check for -cpu help and -device help before we call select_machine(),
2582 * which will return an error if the architecture has no default machine
2583 * type and the user did not specify one, so that the user doesn't need
2584 * to say '-cpu help -machine something'.
2585 */
2586 if (cpu_option && is_help_option(cpu_option)) {
2587 list_cpus(cpu_option);
2588 exit(0);
2589 }
2590
2591 if (qemu_opts_foreach(qemu_find_opts("device"),
2592 device_help_func, NULL, NULL)) {
2593 exit(0);
2594 }
2595
2596 /* -L help lists the data directories and exits. */
2597 if (list_data_dirs) {
2598 qemu_list_data_dirs();
2599 exit(0);
2600 }
2601 }
2602
2603 static void qemu_maybe_daemonize(const char *pid_file)
2604 {
2605 Error *err = NULL;
2606
2607 os_daemonize();
2608 rcu_disable_atfork();
2609
2610 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2611 error_reportf_err(err, "cannot create PID file: ");
2612 exit(1);
2613 }
2614
2615 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2616 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2617 }
2618
2619 static void qemu_init_displays(void)
2620 {
2621 DisplayState *ds;
2622
2623 /* init local displays */
2624 ds = init_displaystate();
2625 qemu_display_init(ds, &dpy);
2626
2627 /* must be after terminal init, SDL library changes signal handlers */
2628 os_setup_signal_handling();
2629
2630 /* init remote displays */
2631 #ifdef CONFIG_VNC
2632 qemu_opts_foreach(qemu_find_opts("vnc"),
2633 vnc_init_func, NULL, &error_fatal);
2634 #endif
2635
2636 if (using_spice) {
2637 qemu_spice.display_init();
2638 }
2639 }
2640
2641 static void qemu_init_board(void)
2642 {
2643 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2644
2645 if (machine_class->default_ram_id && current_machine->ram_size &&
2646 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2647 create_default_memdev(current_machine, mem_path);
2648 }
2649
2650 /* process plugin before CPUs are created, but once -smp has been parsed */
2651 qemu_plugin_load_list(&plugin_list, &error_fatal);
2652
2653 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2654 machine_run_board_init(current_machine);
2655
2656 drive_check_orphaned();
2657
2658 realtime_init();
2659
2660 if (hax_enabled()) {
2661 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2662 hax_sync_vcpus();
2663 }
2664 }
2665
2666 static void qemu_create_cli_devices(void)
2667 {
2668 DeviceOption *opt;
2669
2670 soundhw_init();
2671
2672 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2673 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2674
2675 /* init USB devices */
2676 if (machine_usb(current_machine)) {
2677 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2678 exit(1);
2679 }
2680
2681 /* init generic devices */
2682 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2683 qemu_opts_foreach(qemu_find_opts("device"),
2684 device_init_func, NULL, &error_fatal);
2685 QTAILQ_FOREACH(opt, &device_opts, next) {
2686 DeviceState *dev;
2687 loc_push_restore(&opt->loc);
2688 /*
2689 * TODO Eventually we should call qmp_device_add() here to make sure it
2690 * behaves the same, but QMP still has to accept incorrectly typed
2691 * options until libvirt is fixed and we want to be strict on the CLI
2692 * from the start, so call qdev_device_add_from_qdict() directly for
2693 * now.
2694 */
2695 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2696 object_unref(OBJECT(dev));
2697 loc_pop(&opt->loc);
2698 }
2699 rom_reset_order_override();
2700 }
2701
2702 static void qemu_machine_creation_done(void)
2703 {
2704 MachineState *machine = MACHINE(qdev_get_machine());
2705
2706 /* Did we create any drives that we failed to create a device for? */
2707 drive_check_orphaned();
2708
2709 /* Don't warn about the default network setup that you get if
2710 * no command line -net or -netdev options are specified. There
2711 * are two cases that we would otherwise complain about:
2712 * (1) board doesn't support a NIC but the implicit "-net nic"
2713 * requested one
2714 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2715 * sets up a nic that isn't connected to anything.
2716 */
2717 if (!default_net && (!qtest_enabled() || has_defaults)) {
2718 net_check_clients();
2719 }
2720
2721 qdev_prop_check_globals();
2722
2723 qdev_machine_creation_done();
2724
2725 if (machine->cgs) {
2726 /*
2727 * Verify that Confidential Guest Support has actually been initialized
2728 */
2729 assert(machine->cgs->ready);
2730 }
2731
2732 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2733 exit(1);
2734 }
2735 }
2736
2737 void qmp_x_exit_preconfig(Error **errp)
2738 {
2739 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2740 error_setg(errp, "The command is permitted only before machine initialization");
2741 return;
2742 }
2743
2744 qemu_init_board();
2745 qemu_create_cli_devices();
2746 qemu_machine_creation_done();
2747
2748 if (loadvm) {
2749 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2750 }
2751 if (replay_mode != REPLAY_MODE_NONE) {
2752 replay_vmstate_init();
2753 }
2754
2755 if (incoming) {
2756 Error *local_err = NULL;
2757 if (strcmp(incoming, "defer") != 0) {
2758 qmp_migrate_incoming(incoming, &local_err);
2759 if (local_err) {
2760 error_reportf_err(local_err, "-incoming %s: ", incoming);
2761 exit(1);
2762 }
2763 }
2764 } else if (autostart) {
2765 qmp_cont(NULL);
2766 }
2767 }
2768
2769 void qemu_init(int argc, char **argv, char **envp)
2770 {
2771 QemuOpts *opts;
2772 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2773 QemuOptsList *olist;
2774 int optind;
2775 const char *optarg;
2776 MachineClass *machine_class;
2777 bool userconfig = true;
2778 FILE *vmstate_dump_file = NULL;
2779
2780 qemu_add_opts(&qemu_drive_opts);
2781 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2782 qemu_add_drive_opts(&qemu_common_drive_opts);
2783 qemu_add_drive_opts(&qemu_drive_opts);
2784 qemu_add_drive_opts(&bdrv_runtime_opts);
2785 qemu_add_opts(&qemu_chardev_opts);
2786 qemu_add_opts(&qemu_device_opts);
2787 qemu_add_opts(&qemu_netdev_opts);
2788 qemu_add_opts(&qemu_nic_opts);
2789 qemu_add_opts(&qemu_net_opts);
2790 qemu_add_opts(&qemu_rtc_opts);
2791 qemu_add_opts(&qemu_global_opts);
2792 qemu_add_opts(&qemu_mon_opts);
2793 qemu_add_opts(&qemu_trace_opts);
2794 qemu_plugin_add_opts();
2795 qemu_add_opts(&qemu_option_rom_opts);
2796 qemu_add_opts(&qemu_accel_opts);
2797 qemu_add_opts(&qemu_mem_opts);
2798 qemu_add_opts(&qemu_smp_opts);
2799 qemu_add_opts(&qemu_boot_opts);
2800 qemu_add_opts(&qemu_add_fd_opts);
2801 qemu_add_opts(&qemu_object_opts);
2802 qemu_add_opts(&qemu_tpmdev_opts);
2803 qemu_add_opts(&qemu_overcommit_opts);
2804 qemu_add_opts(&qemu_msg_opts);
2805 qemu_add_opts(&qemu_name_opts);
2806 qemu_add_opts(&qemu_numa_opts);
2807 qemu_add_opts(&qemu_icount_opts);
2808 qemu_add_opts(&qemu_semihosting_config_opts);
2809 qemu_add_opts(&qemu_fw_cfg_opts);
2810 qemu_add_opts(&qemu_action_opts);
2811 module_call_init(MODULE_INIT_OPTS);
2812
2813 error_init(argv[0]);
2814 qemu_init_exec_dir(argv[0]);
2815
2816 qemu_init_arch_modules();
2817
2818 qemu_init_subsystems();
2819
2820 /* first pass of option parsing */
2821 optind = 1;
2822 while (optind < argc) {
2823 if (argv[optind][0] != '-') {
2824 /* disk image */
2825 optind++;
2826 } else {
2827 const QEMUOption *popt;
2828
2829 popt = lookup_opt(argc, argv, &optarg, &optind);
2830 switch (popt->index) {
2831 case QEMU_OPTION_nouserconfig:
2832 userconfig = false;
2833 break;
2834 }
2835 }
2836 }
2837
2838 machine_opts_dict = qdict_new();
2839 if (userconfig) {
2840 qemu_read_default_config_file(&error_fatal);
2841 }
2842
2843 /* second pass of option parsing */
2844 optind = 1;
2845 for(;;) {
2846 if (optind >= argc)
2847 break;
2848 if (argv[optind][0] != '-') {
2849 loc_set_cmdline(argv, optind, 1);
2850 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2851 } else {
2852 const QEMUOption *popt;
2853
2854 popt = lookup_opt(argc, argv, &optarg, &optind);
2855 if (!(popt->arch_mask & arch_type)) {
2856 error_report("Option not supported for this target");
2857 exit(1);
2858 }
2859 switch(popt->index) {
2860 case QEMU_OPTION_cpu:
2861 /* hw initialization will check this */
2862 cpu_option = optarg;
2863 break;
2864 case QEMU_OPTION_hda:
2865 case QEMU_OPTION_hdb:
2866 case QEMU_OPTION_hdc:
2867 case QEMU_OPTION_hdd:
2868 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2869 HD_OPTS);
2870 break;
2871 case QEMU_OPTION_blockdev:
2872 {
2873 Visitor *v;
2874 BlockdevOptionsQueueEntry *bdo;
2875
2876 v = qobject_input_visitor_new_str(optarg, "driver",
2877 &error_fatal);
2878
2879 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2880 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2881 &error_fatal);
2882 visit_free(v);
2883 loc_save(&bdo->loc);
2884 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2885 break;
2886 }
2887 case QEMU_OPTION_drive:
2888 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2889 optarg, false);
2890 if (opts == NULL) {
2891 exit(1);
2892 }
2893 break;
2894 case QEMU_OPTION_set:
2895 qemu_set_option(optarg, &error_fatal);
2896 break;
2897 case QEMU_OPTION_global:
2898 if (qemu_global_option(optarg) != 0)
2899 exit(1);
2900 break;
2901 case QEMU_OPTION_mtdblock:
2902 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2903 break;
2904 case QEMU_OPTION_sd:
2905 drive_add(IF_SD, -1, optarg, SD_OPTS);
2906 break;
2907 case QEMU_OPTION_pflash:
2908 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2909 break;
2910 case QEMU_OPTION_snapshot:
2911 {
2912 Error *blocker = NULL;
2913 snapshot = 1;
2914 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2915 "-snapshot");
2916 replay_add_blocker(blocker);
2917 }
2918 break;
2919 case QEMU_OPTION_numa:
2920 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2921 optarg, true);
2922 if (!opts) {
2923 exit(1);
2924 }
2925 break;
2926 case QEMU_OPTION_display:
2927 parse_display(optarg);
2928 break;
2929 case QEMU_OPTION_nographic:
2930 qdict_put_str(machine_opts_dict, "graphics", "off");
2931 nographic = true;
2932 dpy.type = DISPLAY_TYPE_NONE;
2933 break;
2934 case QEMU_OPTION_curses:
2935 warn_report("-curses is deprecated, "
2936 "use -display curses instead.");
2937 #ifdef CONFIG_CURSES
2938 dpy.type = DISPLAY_TYPE_CURSES;
2939 #else
2940 error_report("curses or iconv support is disabled");
2941 exit(1);
2942 #endif
2943 break;
2944 case QEMU_OPTION_portrait:
2945 graphic_rotate = 90;
2946 break;
2947 case QEMU_OPTION_rotate:
2948 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2949 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2950 graphic_rotate != 180 && graphic_rotate != 270) {
2951 error_report("only 90, 180, 270 deg rotation is available");
2952 exit(1);
2953 }
2954 break;
2955 case QEMU_OPTION_kernel:
2956 qdict_put_str(machine_opts_dict, "kernel", optarg);
2957 break;
2958 case QEMU_OPTION_initrd:
2959 qdict_put_str(machine_opts_dict, "initrd", optarg);
2960 break;
2961 case QEMU_OPTION_append:
2962 qdict_put_str(machine_opts_dict, "append", optarg);
2963 break;
2964 case QEMU_OPTION_dtb:
2965 qdict_put_str(machine_opts_dict, "dtb", optarg);
2966 break;
2967 case QEMU_OPTION_cdrom:
2968 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2969 break;
2970 case QEMU_OPTION_boot:
2971 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2972 optarg, true);
2973 if (!opts) {
2974 exit(1);
2975 }
2976 break;
2977 case QEMU_OPTION_fda:
2978 case QEMU_OPTION_fdb:
2979 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2980 optarg, FD_OPTS);
2981 break;
2982 case QEMU_OPTION_no_fd_bootchk:
2983 fd_bootchk = 0;
2984 break;
2985 case QEMU_OPTION_netdev:
2986 default_net = 0;
2987 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2988 exit(1);
2989 }
2990 break;
2991 case QEMU_OPTION_nic:
2992 default_net = 0;
2993 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2994 exit(1);
2995 }
2996 break;
2997 case QEMU_OPTION_net:
2998 default_net = 0;
2999 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3000 exit(1);
3001 }
3002 break;
3003 #ifdef CONFIG_LIBISCSI
3004 case QEMU_OPTION_iscsi:
3005 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3006 optarg, false);
3007 if (!opts) {
3008 exit(1);
3009 }
3010 break;
3011 #endif
3012 case QEMU_OPTION_audio_help:
3013 audio_legacy_help();
3014 exit (0);
3015 break;
3016 case QEMU_OPTION_audiodev:
3017 audio_parse_option(optarg);
3018 break;
3019 case QEMU_OPTION_soundhw:
3020 select_soundhw (optarg);
3021 break;
3022 case QEMU_OPTION_h:
3023 help(0);
3024 break;
3025 case QEMU_OPTION_version:
3026 version();
3027 exit(0);
3028 break;
3029 case QEMU_OPTION_m:
3030 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3031 optarg, true);
3032 if (!opts) {
3033 exit(EXIT_FAILURE);
3034 }
3035 break;
3036 #ifdef CONFIG_TPM
3037 case QEMU_OPTION_tpmdev:
3038 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3039 exit(1);
3040 }
3041 break;
3042 #endif
3043 case QEMU_OPTION_mempath:
3044 mem_path = optarg;
3045 break;
3046 case QEMU_OPTION_mem_prealloc:
3047 mem_prealloc = 1;
3048 break;
3049 case QEMU_OPTION_d:
3050 log_mask = optarg;
3051 break;
3052 case QEMU_OPTION_D:
3053 log_file = optarg;
3054 break;
3055 case QEMU_OPTION_DFILTER:
3056 qemu_set_dfilter_ranges(optarg, &error_fatal);
3057 break;
3058 case QEMU_OPTION_seed:
3059 qemu_guest_random_seed_main(optarg, &error_fatal);
3060 break;
3061 case QEMU_OPTION_s:
3062 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3063 break;
3064 case QEMU_OPTION_gdb:
3065 add_device_config(DEV_GDB, optarg);
3066 break;
3067 case QEMU_OPTION_L:
3068 if (is_help_option(optarg)) {
3069 list_data_dirs = true;
3070 } else {
3071 qemu_add_data_dir(g_strdup(optarg));
3072 }
3073 break;
3074 case QEMU_OPTION_bios:
3075 qdict_put_str(machine_opts_dict, "firmware", optarg);
3076 break;
3077 case QEMU_OPTION_singlestep:
3078 singlestep = 1;
3079 break;
3080 case QEMU_OPTION_S:
3081 autostart = 0;
3082 break;
3083 case QEMU_OPTION_k:
3084 keyboard_layout = optarg;
3085 break;
3086 case QEMU_OPTION_vga:
3087 vga_model = optarg;
3088 default_vga = 0;
3089 break;
3090 case QEMU_OPTION_g:
3091 {
3092 const char *p;
3093 int w, h, depth;
3094 p = optarg;
3095 w = strtol(p, (char **)&p, 10);
3096 if (w <= 0) {
3097 graphic_error:
3098 error_report("invalid resolution or depth");
3099 exit(1);
3100 }
3101 if (*p != 'x')
3102 goto graphic_error;
3103 p++;
3104 h = strtol(p, (char **)&p, 10);
3105 if (h <= 0)
3106 goto graphic_error;
3107 if (*p == 'x') {
3108 p++;
3109 depth = strtol(p, (char **)&p, 10);
3110 if (depth != 1 && depth != 2 && depth != 4 &&
3111 depth != 8 && depth != 15 && depth != 16 &&
3112 depth != 24 && depth != 32)
3113 goto graphic_error;
3114 } else if (*p == '\0') {
3115 depth = graphic_depth;
3116 } else {
3117 goto graphic_error;
3118 }
3119
3120 graphic_width = w;
3121 graphic_height = h;
3122 graphic_depth = depth;
3123 }
3124 break;
3125 case QEMU_OPTION_echr:
3126 {
3127 char *r;
3128 term_escape_char = strtol(optarg, &r, 0);
3129 if (r == optarg)
3130 printf("Bad argument to echr\n");
3131 break;
3132 }
3133 case QEMU_OPTION_monitor:
3134 default_monitor = 0;
3135 if (strncmp(optarg, "none", 4)) {
3136 monitor_parse(optarg, "readline", false);
3137 }
3138 break;
3139 case QEMU_OPTION_qmp:
3140 monitor_parse(optarg, "control", false);
3141 default_monitor = 0;
3142 break;
3143 case QEMU_OPTION_qmp_pretty:
3144 monitor_parse(optarg, "control", true);
3145 default_monitor = 0;
3146 break;
3147 case QEMU_OPTION_mon:
3148 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3149 true);
3150 if (!opts) {
3151 exit(1);
3152 }
3153 default_monitor = 0;
3154 break;
3155 case QEMU_OPTION_chardev:
3156 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3157 optarg, true);
3158 if (!opts) {
3159 exit(1);
3160 }
3161 break;
3162 case QEMU_OPTION_fsdev:
3163 olist = qemu_find_opts("fsdev");
3164 if (!olist) {
3165 error_report("fsdev support is disabled");
3166 exit(1);
3167 }
3168 opts = qemu_opts_parse_noisily(olist, optarg, true);
3169 if (!opts) {
3170 exit(1);
3171 }
3172 break;
3173 case QEMU_OPTION_virtfs: {
3174 QemuOpts *fsdev;
3175 QemuOpts *device;
3176 const char *writeout, *sock_fd, *socket, *path, *security_model,
3177 *multidevs;
3178
3179 olist = qemu_find_opts("virtfs");
3180 if (!olist) {
3181 error_report("virtfs support is disabled");
3182 exit(1);
3183 }
3184 opts = qemu_opts_parse_noisily(olist, optarg, true);
3185 if (!opts) {
3186 exit(1);
3187 }
3188
3189 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3190 qemu_opt_get(opts, "mount_tag") == NULL) {
3191 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3192 exit(1);
3193 }
3194 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3195 qemu_opts_id(opts) ?:
3196 qemu_opt_get(opts, "mount_tag"),
3197 1, NULL);
3198 if (!fsdev) {
3199 error_report("duplicate or invalid fsdev id: %s",
3200 qemu_opt_get(opts, "mount_tag"));
3201 exit(1);
3202 }
3203
3204 writeout = qemu_opt_get(opts, "writeout");
3205 if (writeout) {
3206 #ifdef CONFIG_SYNC_FILE_RANGE
3207 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3208 #else
3209 error_report("writeout=immediate not supported "
3210 "on this platform");
3211 exit(1);
3212 #endif
3213 }
3214 qemu_opt_set(fsdev, "fsdriver",
3215 qemu_opt_get(opts, "fsdriver"), &error_abort);
3216 path = qemu_opt_get(opts, "path");
3217 if (path) {
3218 qemu_opt_set(fsdev, "path", path, &error_abort);
3219 }
3220 security_model = qemu_opt_get(opts, "security_model");
3221 if (security_model) {
3222 qemu_opt_set(fsdev, "security_model", security_model,
3223 &error_abort);
3224 }
3225 socket = qemu_opt_get(opts, "socket");
3226 if (socket) {
3227 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3228 }
3229 sock_fd = qemu_opt_get(opts, "sock_fd");
3230 if (sock_fd) {
3231 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3232 }
3233
3234 qemu_opt_set_bool(fsdev, "readonly",
3235 qemu_opt_get_bool(opts, "readonly", 0),
3236 &error_abort);
3237 multidevs = qemu_opt_get(opts, "multidevs");
3238 if (multidevs) {
3239 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3240 }
3241 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3242 &error_abort);
3243 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3244 qemu_opt_set(device, "fsdev",
3245 qemu_opts_id(fsdev), &error_abort);
3246 qemu_opt_set(device, "mount_tag",
3247 qemu_opt_get(opts, "mount_tag"), &error_abort);
3248 break;
3249 }
3250 case QEMU_OPTION_serial:
3251 add_device_config(DEV_SERIAL, optarg);
3252 default_serial = 0;
3253 if (strncmp(optarg, "mon:", 4) == 0) {
3254 default_monitor = 0;
3255 }
3256 break;
3257 case QEMU_OPTION_watchdog:
3258 if (watchdog) {
3259 error_report("only one watchdog option may be given");
3260 exit(1);
3261 }
3262 warn_report("-watchdog is deprecated; use -device instead.");
3263 watchdog = optarg;
3264 break;
3265 case QEMU_OPTION_action:
3266 olist = qemu_find_opts("action");
3267 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3268 exit(1);
3269 }
3270 break;
3271 case QEMU_OPTION_watchdog_action: {
3272 QemuOpts *opts;
3273 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3274 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3275 break;
3276 }
3277 case QEMU_OPTION_parallel:
3278 add_device_config(DEV_PARALLEL, optarg);
3279 default_parallel = 0;
3280 if (strncmp(optarg, "mon:", 4) == 0) {
3281 default_monitor = 0;
3282 }
3283 break;
3284 case QEMU_OPTION_debugcon:
3285 add_device_config(DEV_DEBUGCON, optarg);
3286 break;
3287 case QEMU_OPTION_loadvm:
3288 loadvm = optarg;
3289 break;
3290 case QEMU_OPTION_full_screen:
3291 dpy.has_full_screen = true;
3292 dpy.full_screen = true;
3293 break;
3294 case QEMU_OPTION_alt_grab:
3295 alt_grab = 1;
3296 warn_report("-alt-grab is deprecated, please use "
3297 "-display sdl,grab-mod=lshift-lctrl-lalt instead.");
3298 break;
3299 case QEMU_OPTION_ctrl_grab:
3300 ctrl_grab = 1;
3301 warn_report("-ctrl-grab is deprecated, please use "
3302 "-display sdl,grab-mod=rctrl instead.");
3303 break;
3304 case QEMU_OPTION_sdl:
3305 warn_report("-sdl is deprecated, use -display sdl instead.");
3306 #ifdef CONFIG_SDL
3307 dpy.type = DISPLAY_TYPE_SDL;
3308 break;
3309 #else
3310 error_report("SDL support is disabled");
3311 exit(1);
3312 #endif
3313 case QEMU_OPTION_pidfile:
3314 pid_file = optarg;
3315 break;
3316 case QEMU_OPTION_win2k_hack:
3317 win2k_install_hack = 1;
3318 break;
3319 case QEMU_OPTION_acpitable:
3320 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3321 optarg, true);
3322 if (!opts) {
3323 exit(1);
3324 }
3325 acpi_table_add(opts, &error_fatal);
3326 break;
3327 case QEMU_OPTION_smbios:
3328 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3329 optarg, false);
3330 if (!opts) {
3331 exit(1);
3332 }
3333 smbios_entry_add(opts, &error_fatal);
3334 break;
3335 case QEMU_OPTION_fwcfg:
3336 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3337 optarg, true);
3338 if (opts == NULL) {
3339 exit(1);
3340 }
3341 break;
3342 case QEMU_OPTION_preconfig:
3343 preconfig_requested = true;
3344 break;
3345 case QEMU_OPTION_enable_kvm:
3346 qdict_put_str(machine_opts_dict, "accel", "kvm");
3347 break;
3348 case QEMU_OPTION_M:
3349 case QEMU_OPTION_machine:
3350 {
3351 bool help;
3352
3353 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3354 if (help) {
3355 machine_help_func(machine_opts_dict);
3356 exit(EXIT_SUCCESS);
3357 }
3358 break;
3359 }
3360 case QEMU_OPTION_accel:
3361 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3362 optarg, true);
3363 optarg = qemu_opt_get(accel_opts, "accel");
3364 if (!optarg || is_help_option(optarg)) {
3365 printf("Accelerators supported in QEMU binary:\n");
3366 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3367 false);
3368 for (el = accel_list; el; el = el->next) {
3369 gchar *typename = g_strdup(object_class_get_name(
3370 OBJECT_CLASS(el->data)));
3371 /* omit qtest which is used for tests only */
3372 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3373 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3374 gchar **optname = g_strsplit(typename,
3375 ACCEL_CLASS_SUFFIX, 0);
3376 printf("%s\n", optname[0]);
3377 g_strfreev(optname);
3378 }
3379 g_free(typename);
3380 }
3381 g_slist_free(accel_list);
3382 exit(0);
3383 }
3384 break;
3385 case QEMU_OPTION_usb:
3386 qdict_put_str(machine_opts_dict, "usb", "on");
3387 break;
3388 case QEMU_OPTION_usbdevice:
3389 qdict_put_str(machine_opts_dict, "usb", "on");
3390 add_device_config(DEV_USB, optarg);
3391 break;
3392 case QEMU_OPTION_device:
3393 if (optarg[0] == '{') {
3394 QObject *obj = qobject_from_json(optarg, &error_fatal);
3395 DeviceOption *opt = g_new0(DeviceOption, 1);
3396 opt->opts = qobject_to(QDict, obj);
3397 loc_save(&opt->loc);
3398 assert(opt->opts != NULL);
3399 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3400 } else {
3401 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3402 optarg, true)) {
3403 exit(1);
3404 }
3405 }
3406 break;
3407 case QEMU_OPTION_smp:
3408 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3409 "smp", optarg);
3410 break;
3411 case QEMU_OPTION_vnc:
3412 vnc_parse(optarg);
3413 break;
3414 case QEMU_OPTION_no_acpi:
3415 qdict_put_str(machine_opts_dict, "acpi", "off");
3416 break;
3417 case QEMU_OPTION_no_hpet:
3418 qdict_put_str(machine_opts_dict, "hpet", "off");
3419 break;
3420 case QEMU_OPTION_no_reboot:
3421 olist = qemu_find_opts("action");
3422 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3423 break;
3424 case QEMU_OPTION_no_shutdown:
3425 olist = qemu_find_opts("action");
3426 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3427 break;
3428 case QEMU_OPTION_uuid:
3429 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3430 error_report("failed to parse UUID string: wrong format");
3431 exit(1);
3432 }
3433 qemu_uuid_set = true;
3434 break;
3435 case QEMU_OPTION_option_rom:
3436 if (nb_option_roms >= MAX_OPTION_ROMS) {
3437 error_report("too many option ROMs");
3438 exit(1);
3439 }
3440 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3441 optarg, true);
3442 if (!opts) {
3443 exit(1);
3444 }
3445 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3446 option_rom[nb_option_roms].bootindex =
3447 qemu_opt_get_number(opts, "bootindex", -1);
3448 if (!option_rom[nb_option_roms].name) {
3449 error_report("Option ROM file is not specified");
3450 exit(1);
3451 }
3452 nb_option_roms++;
3453 break;
3454 case QEMU_OPTION_semihosting:
3455 qemu_semihosting_enable();
3456 break;
3457 case QEMU_OPTION_semihosting_config:
3458 if (qemu_semihosting_config_options(optarg) != 0) {
3459 exit(1);
3460 }
3461 break;
3462 case QEMU_OPTION_name:
3463 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3464 optarg, true);
3465 if (!opts) {
3466 exit(1);
3467 }
3468 /* Capture guest name if -msg guest-name is used later */
3469 error_guest_name = qemu_opt_get(opts, "guest");
3470 break;
3471 case QEMU_OPTION_prom_env:
3472 if (nb_prom_envs >= MAX_PROM_ENVS) {
3473 error_report("too many prom variables");
3474 exit(1);
3475 }
3476 prom_envs[nb_prom_envs] = optarg;
3477 nb_prom_envs++;
3478 break;
3479 case QEMU_OPTION_old_param:
3480 old_param = 1;
3481 break;
3482 case QEMU_OPTION_rtc:
3483 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3484 false);
3485 if (!opts) {
3486 exit(1);
3487 }
3488 break;
3489 case QEMU_OPTION_icount:
3490 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3491 optarg, true);
3492 if (!icount_opts) {
3493 exit(1);
3494 }
3495 break;
3496 case QEMU_OPTION_incoming:
3497 if (!incoming) {
3498 runstate_set(RUN_STATE_INMIGRATE);
3499 }
3500 incoming = optarg;
3501 break;
3502 case QEMU_OPTION_only_migratable:
3503 only_migratable = 1;
3504 break;
3505 case QEMU_OPTION_nodefaults:
3506 has_defaults = 0;
3507 break;
3508 case QEMU_OPTION_xen_domid:
3509 if (!(accel_find("xen"))) {
3510 error_report("Option not supported for this target");
3511 exit(1);
3512 }
3513 xen_domid = atoi(optarg);
3514 break;
3515 case QEMU_OPTION_xen_attach:
3516 if (!(accel_find("xen"))) {
3517 error_report("Option not supported for this target");
3518 exit(1);
3519 }
3520 xen_mode = XEN_ATTACH;
3521 break;
3522 case QEMU_OPTION_xen_domid_restrict:
3523 if (!(accel_find("xen"))) {
3524 error_report("Option not supported for this target");
3525 exit(1);
3526 }
3527 xen_domid_restrict = true;
3528 break;
3529 case QEMU_OPTION_trace:
3530 trace_opt_parse(optarg);
3531 break;
3532 case QEMU_OPTION_plugin:
3533 qemu_plugin_opt_parse(optarg, &plugin_list);
3534 break;
3535 case QEMU_OPTION_readconfig:
3536 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3537 break;
3538 #ifdef CONFIG_SPICE
3539 case QEMU_OPTION_spice:
3540 olist = qemu_find_opts_err("spice", NULL);
3541 if (!olist) {
3542 error_report("spice support is disabled");
3543 exit(1);
3544 }
3545 opts = qemu_opts_parse_noisily(olist, optarg, false);
3546 if (!opts) {
3547 exit(1);
3548 }
3549 display_remote++;
3550 break;
3551 #endif
3552 case QEMU_OPTION_writeconfig:
3553 {
3554 FILE *fp;
3555 warn_report("-writeconfig is deprecated and will go away without a replacement");
3556 if (strcmp(optarg, "-") == 0) {
3557 fp = stdout;
3558 } else {
3559 fp = fopen(optarg, "w");
3560 if (fp == NULL) {
3561 error_report("open %s: %s", optarg,
3562 strerror(errno));
3563 exit(1);
3564 }
3565 }
3566 qemu_config_write(fp);
3567 if (fp != stdout) {
3568 fclose(fp);
3569 }
3570 break;
3571 }
3572 case QEMU_OPTION_qtest:
3573 qtest_chrdev = optarg;
3574 break;
3575 case QEMU_OPTION_qtest_log:
3576 qtest_log = optarg;
3577 break;
3578 case QEMU_OPTION_sandbox:
3579 olist = qemu_find_opts("sandbox");
3580 if (!olist) {
3581 #ifndef CONFIG_SECCOMP
3582 error_report("-sandbox support is not enabled "
3583 "in this QEMU binary");
3584 #endif
3585 exit(1);
3586 }
3587
3588 opts = qemu_opts_parse_noisily(olist, optarg, true);
3589 if (!opts) {
3590 exit(1);
3591 }
3592 break;
3593 case QEMU_OPTION_add_fd:
3594 #ifndef _WIN32
3595 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3596 optarg, false);
3597 if (!opts) {
3598 exit(1);
3599 }
3600 #else
3601 error_report("File descriptor passing is disabled on this "
3602 "platform");
3603 exit(1);
3604 #endif
3605 break;
3606 case QEMU_OPTION_object:
3607 object_option_parse(optarg);
3608 break;
3609 case QEMU_OPTION_overcommit:
3610 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3611 optarg, false);
3612 if (!opts) {
3613 exit(1);
3614 }
3615 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3616 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3617 break;
3618 case QEMU_OPTION_compat:
3619 {
3620 CompatPolicy *opts;
3621 Visitor *v;
3622
3623 v = qobject_input_visitor_new_str(optarg, NULL,
3624 &error_fatal);
3625
3626 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3627 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3628
3629 qapi_free_CompatPolicy(opts);
3630 visit_free(v);
3631 break;
3632 }
3633 case QEMU_OPTION_msg:
3634 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3635 false);
3636 if (!opts) {
3637 exit(1);
3638 }
3639 configure_msg(opts);
3640 break;
3641 case QEMU_OPTION_dump_vmstate:
3642 if (vmstate_dump_file) {
3643 error_report("only one '-dump-vmstate' "
3644 "option may be given");
3645 exit(1);
3646 }
3647 vmstate_dump_file = fopen(optarg, "w");
3648 if (vmstate_dump_file == NULL) {
3649 error_report("open %s: %s", optarg, strerror(errno));
3650 exit(1);
3651 }
3652 break;
3653 case QEMU_OPTION_enable_sync_profile:
3654 qsp_enable();
3655 break;
3656 case QEMU_OPTION_nouserconfig:
3657 /* Nothing to be parsed here. Especially, do not error out below. */
3658 break;
3659 default:
3660 if (os_parse_cmd_args(popt->index, optarg)) {
3661 error_report("Option not supported in this build");
3662 exit(1);
3663 }
3664 }
3665 }
3666 }
3667 /*
3668 * Clear error location left behind by the loop.
3669 * Best done right after the loop. Do not insert code here!
3670 */
3671 loc_set_none();
3672
3673 qemu_validate_options(machine_opts_dict);
3674 qemu_process_sugar_options();
3675
3676 /*
3677 * These options affect everything else and should be processed
3678 * before daemonizing.
3679 */
3680 qemu_process_early_options();
3681
3682 qemu_process_help_options();
3683 qemu_maybe_daemonize(pid_file);
3684
3685 /*
3686 * The trace backend must be initialized after daemonizing.
3687 * trace_init_backends() will call st_init(), which will create the
3688 * trace thread in the parent, and also register st_flush_trace_buffer()
3689 * in atexit(). This function will force the parent to wait for the
3690 * writeout thread to finish, which will not occur, and the parent
3691 * process will be left in the host.
3692 */
3693 if (!trace_init_backends()) {
3694 exit(1);
3695 }
3696 trace_init_file();
3697
3698 qemu_init_main_loop(&error_fatal);
3699 cpu_timers_init();
3700
3701 user_register_global_props();
3702 replay_configure(icount_opts);
3703
3704 configure_rtc(qemu_find_opts_singleton("rtc"));
3705
3706 qemu_create_machine(machine_opts_dict);
3707
3708 suspend_mux_open();
3709
3710 qemu_disable_default_devices();
3711 qemu_create_default_devices();
3712 qemu_create_early_backends();
3713
3714 qemu_apply_legacy_machine_options(machine_opts_dict);
3715 qemu_apply_machine_options(machine_opts_dict);
3716 qobject_unref(machine_opts_dict);
3717 phase_advance(PHASE_MACHINE_CREATED);
3718
3719 /*
3720 * Note: uses machine properties such as kernel-irqchip, must run
3721 * after qemu_apply_machine_options.
3722 */
3723 configure_accelerators(argv[0]);
3724 phase_advance(PHASE_ACCEL_CREATED);
3725
3726 /*
3727 * Beware, QOM objects created before this point miss global and
3728 * compat properties.
3729 *
3730 * Global properties get set up by qdev_prop_register_global(),
3731 * called from user_register_global_props(), and certain option
3732 * desugaring. Also in CPU feature desugaring (buried in
3733 * parse_cpu_option()), which happens below this point, but may
3734 * only target the CPU type, which can only be created after
3735 * parse_cpu_option() returned the type.
3736 *
3737 * Machine compat properties: object_set_machine_compat_props().
3738 * Accelerator compat props: object_set_accelerator_compat_props(),
3739 * called from do_configure_accelerator().
3740 */
3741
3742 machine_class = MACHINE_GET_CLASS(current_machine);
3743 if (!qtest_enabled() && machine_class->deprecation_reason) {
3744 error_report("Machine type '%s' is deprecated: %s",
3745 machine_class->name, machine_class->deprecation_reason);
3746 }
3747
3748 /*
3749 * Note: creates a QOM object, must run only after global and
3750 * compat properties have been set up.
3751 */
3752 migration_object_init();
3753
3754 qemu_create_late_backends();
3755
3756 /* parse features once if machine provides default cpu_type */
3757 current_machine->cpu_type = machine_class->default_cpu_type;
3758 if (cpu_option) {
3759 current_machine->cpu_type = parse_cpu_option(cpu_option);
3760 }
3761 /* NB: for machine none cpu_type could STILL be NULL here! */
3762
3763 qemu_resolve_machine_memdev();
3764 parse_numa_opts(current_machine);
3765
3766 if (vmstate_dump_file) {
3767 /* dump and exit */
3768 module_load_qom_all();
3769 dump_vmstate_json_to_file(vmstate_dump_file);
3770 exit(0);
3771 }
3772
3773 if (!preconfig_requested) {
3774 qmp_x_exit_preconfig(&error_fatal);
3775 }
3776 qemu_init_displays();
3777 accel_setup_post(current_machine);
3778 os_setup_post();
3779 resume_mux_open();
3780 }