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