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