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