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