]> git.proxmox.com Git - mirror_qemu.git/blob - vl.c
move vm_start to cpus.c
[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(void)
1683 {
1684 if (current_cpu) {
1685 current_cpu->crash_occurred = true;
1686 }
1687 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1688 vm_stop(RUN_STATE_GUEST_PANICKED);
1689 if (!no_shutdown) {
1690 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1691 &error_abort);
1692 qemu_system_shutdown_request();
1693 }
1694 }
1695
1696 void qemu_system_reset_request(void)
1697 {
1698 if (no_reboot) {
1699 shutdown_requested = 1;
1700 } else {
1701 reset_requested = 1;
1702 }
1703 cpu_stop_current();
1704 qemu_notify_event();
1705 }
1706
1707 static void qemu_system_suspend(void)
1708 {
1709 pause_all_vcpus();
1710 notifier_list_notify(&suspend_notifiers, NULL);
1711 runstate_set(RUN_STATE_SUSPENDED);
1712 qapi_event_send_suspend(&error_abort);
1713 }
1714
1715 void qemu_system_suspend_request(void)
1716 {
1717 if (runstate_check(RUN_STATE_SUSPENDED)) {
1718 return;
1719 }
1720 suspend_requested = 1;
1721 cpu_stop_current();
1722 qemu_notify_event();
1723 }
1724
1725 void qemu_register_suspend_notifier(Notifier *notifier)
1726 {
1727 notifier_list_add(&suspend_notifiers, notifier);
1728 }
1729
1730 void qemu_system_wakeup_request(WakeupReason reason)
1731 {
1732 trace_system_wakeup_request(reason);
1733
1734 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1735 return;
1736 }
1737 if (!(wakeup_reason_mask & (1 << reason))) {
1738 return;
1739 }
1740 runstate_set(RUN_STATE_RUNNING);
1741 wakeup_reason = reason;
1742 qemu_notify_event();
1743 }
1744
1745 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1746 {
1747 if (enabled) {
1748 wakeup_reason_mask |= (1 << reason);
1749 } else {
1750 wakeup_reason_mask &= ~(1 << reason);
1751 }
1752 }
1753
1754 void qemu_register_wakeup_notifier(Notifier *notifier)
1755 {
1756 notifier_list_add(&wakeup_notifiers, notifier);
1757 }
1758
1759 void qemu_system_killed(int signal, pid_t pid)
1760 {
1761 shutdown_signal = signal;
1762 shutdown_pid = pid;
1763 no_shutdown = 0;
1764
1765 /* Cannot call qemu_system_shutdown_request directly because
1766 * we are in a signal handler.
1767 */
1768 shutdown_requested = 1;
1769 qemu_notify_event();
1770 }
1771
1772 void qemu_system_shutdown_request(void)
1773 {
1774 trace_qemu_system_shutdown_request();
1775 replay_shutdown_request();
1776 shutdown_requested = 1;
1777 qemu_notify_event();
1778 }
1779
1780 static void qemu_system_powerdown(void)
1781 {
1782 qapi_event_send_powerdown(&error_abort);
1783 notifier_list_notify(&powerdown_notifiers, NULL);
1784 }
1785
1786 void qemu_system_powerdown_request(void)
1787 {
1788 trace_qemu_system_powerdown_request();
1789 powerdown_requested = 1;
1790 qemu_notify_event();
1791 }
1792
1793 void qemu_register_powerdown_notifier(Notifier *notifier)
1794 {
1795 notifier_list_add(&powerdown_notifiers, notifier);
1796 }
1797
1798 void qemu_system_debug_request(void)
1799 {
1800 debug_requested = 1;
1801 qemu_notify_event();
1802 }
1803
1804 static bool main_loop_should_exit(void)
1805 {
1806 RunState r;
1807 if (qemu_debug_requested()) {
1808 vm_stop(RUN_STATE_DEBUG);
1809 }
1810 if (qemu_suspend_requested()) {
1811 qemu_system_suspend();
1812 }
1813 if (qemu_shutdown_requested()) {
1814 qemu_kill_report();
1815 qapi_event_send_shutdown(&error_abort);
1816 if (no_shutdown) {
1817 vm_stop(RUN_STATE_SHUTDOWN);
1818 } else {
1819 return true;
1820 }
1821 }
1822 if (qemu_reset_requested()) {
1823 pause_all_vcpus();
1824 qemu_system_reset(VMRESET_REPORT);
1825 resume_all_vcpus();
1826 if (!runstate_check(RUN_STATE_RUNNING) &&
1827 !runstate_check(RUN_STATE_INMIGRATE)) {
1828 runstate_set(RUN_STATE_PRELAUNCH);
1829 }
1830 }
1831 if (qemu_wakeup_requested()) {
1832 pause_all_vcpus();
1833 qemu_system_reset(VMRESET_SILENT);
1834 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1835 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1836 resume_all_vcpus();
1837 qapi_event_send_wakeup(&error_abort);
1838 }
1839 if (qemu_powerdown_requested()) {
1840 qemu_system_powerdown();
1841 }
1842 if (qemu_vmstop_requested(&r)) {
1843 vm_stop(r);
1844 }
1845 return false;
1846 }
1847
1848 static void main_loop(void)
1849 {
1850 bool nonblocking;
1851 int last_io = 0;
1852 #ifdef CONFIG_PROFILER
1853 int64_t ti;
1854 #endif
1855 do {
1856 nonblocking = tcg_enabled() && last_io > 0;
1857 #ifdef CONFIG_PROFILER
1858 ti = profile_getclock();
1859 #endif
1860 last_io = main_loop_wait(nonblocking);
1861 #ifdef CONFIG_PROFILER
1862 dev_time += profile_getclock() - ti;
1863 #endif
1864 } while (!main_loop_should_exit());
1865 }
1866
1867 static void version(void)
1868 {
1869 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1870 QEMU_COPYRIGHT "\n");
1871 }
1872
1873 static void help(int exitcode)
1874 {
1875 version();
1876 printf("usage: %s [options] [disk_image]\n\n"
1877 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1878 error_get_progname());
1879
1880 #define QEMU_OPTIONS_GENERATE_HELP
1881 #include "qemu-options-wrapper.h"
1882
1883 printf("\nDuring emulation, the following keys are useful:\n"
1884 "ctrl-alt-f toggle full screen\n"
1885 "ctrl-alt-n switch to virtual console 'n'\n"
1886 "ctrl-alt toggle mouse and keyboard grab\n"
1887 "\n"
1888 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1889
1890 exit(exitcode);
1891 }
1892
1893 #define HAS_ARG 0x0001
1894
1895 typedef struct QEMUOption {
1896 const char *name;
1897 int flags;
1898 int index;
1899 uint32_t arch_mask;
1900 } QEMUOption;
1901
1902 static const QEMUOption qemu_options[] = {
1903 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1904 #define QEMU_OPTIONS_GENERATE_OPTIONS
1905 #include "qemu-options-wrapper.h"
1906 { NULL },
1907 };
1908
1909 typedef struct VGAInterfaceInfo {
1910 const char *opt_name; /* option name */
1911 const char *name; /* human-readable name */
1912 /* Class names indicating that support is available.
1913 * If no class is specified, the interface is always available */
1914 const char *class_names[2];
1915 } VGAInterfaceInfo;
1916
1917 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1918 [VGA_NONE] = {
1919 .opt_name = "none",
1920 },
1921 [VGA_STD] = {
1922 .opt_name = "std",
1923 .name = "standard VGA",
1924 .class_names = { "VGA", "isa-vga" },
1925 },
1926 [VGA_CIRRUS] = {
1927 .opt_name = "cirrus",
1928 .name = "Cirrus VGA",
1929 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1930 },
1931 [VGA_VMWARE] = {
1932 .opt_name = "vmware",
1933 .name = "VMWare SVGA",
1934 .class_names = { "vmware-svga" },
1935 },
1936 [VGA_VIRTIO] = {
1937 .opt_name = "virtio",
1938 .name = "Virtio VGA",
1939 .class_names = { "virtio-vga" },
1940 },
1941 [VGA_QXL] = {
1942 .opt_name = "qxl",
1943 .name = "QXL VGA",
1944 .class_names = { "qxl-vga" },
1945 },
1946 [VGA_TCX] = {
1947 .opt_name = "tcx",
1948 .name = "TCX framebuffer",
1949 .class_names = { "SUNW,tcx" },
1950 },
1951 [VGA_CG3] = {
1952 .opt_name = "cg3",
1953 .name = "CG3 framebuffer",
1954 .class_names = { "cgthree" },
1955 },
1956 [VGA_XENFB] = {
1957 .opt_name = "xenfb",
1958 },
1959 };
1960
1961 static bool vga_interface_available(VGAInterfaceType t)
1962 {
1963 VGAInterfaceInfo *ti = &vga_interfaces[t];
1964
1965 assert(t < VGA_TYPE_MAX);
1966 return !ti->class_names[0] ||
1967 object_class_by_name(ti->class_names[0]) ||
1968 object_class_by_name(ti->class_names[1]);
1969 }
1970
1971 static void select_vgahw(const char *p)
1972 {
1973 const char *opts;
1974 int t;
1975
1976 assert(vga_interface_type == VGA_NONE);
1977 for (t = 0; t < VGA_TYPE_MAX; t++) {
1978 VGAInterfaceInfo *ti = &vga_interfaces[t];
1979 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1980 if (!vga_interface_available(t)) {
1981 error_report("%s not available", ti->name);
1982 exit(1);
1983 }
1984 vga_interface_type = t;
1985 break;
1986 }
1987 }
1988 if (t == VGA_TYPE_MAX) {
1989 invalid_vga:
1990 error_report("unknown vga type: %s", p);
1991 exit(1);
1992 }
1993 while (*opts) {
1994 const char *nextopt;
1995
1996 if (strstart(opts, ",retrace=", &nextopt)) {
1997 opts = nextopt;
1998 if (strstart(opts, "dumb", &nextopt))
1999 vga_retrace_method = VGA_RETRACE_DUMB;
2000 else if (strstart(opts, "precise", &nextopt))
2001 vga_retrace_method = VGA_RETRACE_PRECISE;
2002 else goto invalid_vga;
2003 } else goto invalid_vga;
2004 opts = nextopt;
2005 }
2006 }
2007
2008 typedef enum DisplayType {
2009 DT_DEFAULT,
2010 DT_CURSES,
2011 DT_SDL,
2012 DT_COCOA,
2013 DT_GTK,
2014 DT_NONE,
2015 } DisplayType;
2016
2017 static DisplayType select_display(const char *p)
2018 {
2019 const char *opts;
2020 DisplayType display = DT_DEFAULT;
2021
2022 if (strstart(p, "sdl", &opts)) {
2023 #ifdef CONFIG_SDL
2024 display = DT_SDL;
2025 while (*opts) {
2026 const char *nextopt;
2027
2028 if (strstart(opts, ",frame=", &nextopt)) {
2029 opts = nextopt;
2030 if (strstart(opts, "on", &nextopt)) {
2031 no_frame = 0;
2032 } else if (strstart(opts, "off", &nextopt)) {
2033 no_frame = 1;
2034 } else {
2035 goto invalid_sdl_args;
2036 }
2037 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2038 opts = nextopt;
2039 if (strstart(opts, "on", &nextopt)) {
2040 alt_grab = 1;
2041 } else if (strstart(opts, "off", &nextopt)) {
2042 alt_grab = 0;
2043 } else {
2044 goto invalid_sdl_args;
2045 }
2046 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2047 opts = nextopt;
2048 if (strstart(opts, "on", &nextopt)) {
2049 ctrl_grab = 1;
2050 } else if (strstart(opts, "off", &nextopt)) {
2051 ctrl_grab = 0;
2052 } else {
2053 goto invalid_sdl_args;
2054 }
2055 } else if (strstart(opts, ",window_close=", &nextopt)) {
2056 opts = nextopt;
2057 if (strstart(opts, "on", &nextopt)) {
2058 no_quit = 0;
2059 } else if (strstart(opts, "off", &nextopt)) {
2060 no_quit = 1;
2061 } else {
2062 goto invalid_sdl_args;
2063 }
2064 } else if (strstart(opts, ",gl=", &nextopt)) {
2065 opts = nextopt;
2066 if (strstart(opts, "on", &nextopt)) {
2067 request_opengl = 1;
2068 } else if (strstart(opts, "off", &nextopt)) {
2069 request_opengl = 0;
2070 } else {
2071 goto invalid_sdl_args;
2072 }
2073 } else {
2074 invalid_sdl_args:
2075 error_report("invalid SDL option string");
2076 exit(1);
2077 }
2078 opts = nextopt;
2079 }
2080 #else
2081 error_report("SDL support is disabled");
2082 exit(1);
2083 #endif
2084 } else if (strstart(p, "vnc", &opts)) {
2085 if (*opts == '=') {
2086 vnc_parse(opts + 1, &error_fatal);
2087 } else {
2088 error_report("VNC requires a display argument vnc=<display>");
2089 exit(1);
2090 }
2091 } else if (strstart(p, "curses", &opts)) {
2092 #ifdef CONFIG_CURSES
2093 display = DT_CURSES;
2094 #else
2095 error_report("curses support is disabled");
2096 exit(1);
2097 #endif
2098 } else if (strstart(p, "gtk", &opts)) {
2099 #ifdef CONFIG_GTK
2100 display = DT_GTK;
2101 while (*opts) {
2102 const char *nextopt;
2103
2104 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2105 opts = nextopt;
2106 if (strstart(opts, "on", &nextopt)) {
2107 grab_on_hover = true;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 grab_on_hover = false;
2110 } else {
2111 goto invalid_gtk_args;
2112 }
2113 } else if (strstart(opts, ",gl=", &nextopt)) {
2114 opts = nextopt;
2115 if (strstart(opts, "on", &nextopt)) {
2116 request_opengl = 1;
2117 } else if (strstart(opts, "off", &nextopt)) {
2118 request_opengl = 0;
2119 } else {
2120 goto invalid_gtk_args;
2121 }
2122 } else {
2123 invalid_gtk_args:
2124 error_report("invalid GTK option string");
2125 exit(1);
2126 }
2127 opts = nextopt;
2128 }
2129 #else
2130 error_report("GTK support is disabled");
2131 exit(1);
2132 #endif
2133 } else if (strstart(p, "none", &opts)) {
2134 display = DT_NONE;
2135 } else {
2136 error_report("unknown display type");
2137 exit(1);
2138 }
2139
2140 return display;
2141 }
2142
2143 static int balloon_parse(const char *arg)
2144 {
2145 QemuOpts *opts;
2146
2147 if (strcmp(arg, "none") == 0) {
2148 return 0;
2149 }
2150
2151 if (!strncmp(arg, "virtio", 6)) {
2152 if (arg[6] == ',') {
2153 /* have params -> parse them */
2154 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2155 false);
2156 if (!opts)
2157 return -1;
2158 } else {
2159 /* create empty opts */
2160 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2161 &error_abort);
2162 }
2163 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2164 return 0;
2165 }
2166
2167 return -1;
2168 }
2169
2170 char *qemu_find_file(int type, const char *name)
2171 {
2172 int i;
2173 const char *subdir;
2174 char *buf;
2175
2176 /* Try the name as a straight path first */
2177 if (access(name, R_OK) == 0) {
2178 trace_load_file(name, name);
2179 return g_strdup(name);
2180 }
2181
2182 switch (type) {
2183 case QEMU_FILE_TYPE_BIOS:
2184 subdir = "";
2185 break;
2186 case QEMU_FILE_TYPE_KEYMAP:
2187 subdir = "keymaps/";
2188 break;
2189 default:
2190 abort();
2191 }
2192
2193 for (i = 0; i < data_dir_idx; i++) {
2194 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2195 if (access(buf, R_OK) == 0) {
2196 trace_load_file(name, buf);
2197 return buf;
2198 }
2199 g_free(buf);
2200 }
2201 return NULL;
2202 }
2203
2204 static inline bool nonempty_str(const char *str)
2205 {
2206 return str && *str;
2207 }
2208
2209 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2210 {
2211 gchar *buf;
2212 size_t size;
2213 const char *name, *file, *str;
2214 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2215
2216 if (fw_cfg == NULL) {
2217 error_report("fw_cfg device not available");
2218 return -1;
2219 }
2220 name = qemu_opt_get(opts, "name");
2221 file = qemu_opt_get(opts, "file");
2222 str = qemu_opt_get(opts, "string");
2223
2224 /* we need name and either a file or the content string */
2225 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2226 error_report("invalid argument(s)");
2227 return -1;
2228 }
2229 if (nonempty_str(file) && nonempty_str(str)) {
2230 error_report("file and string are mutually exclusive");
2231 return -1;
2232 }
2233 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2234 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2235 return -1;
2236 }
2237 if (strncmp(name, "opt/", 4) != 0) {
2238 error_report("warning: externally provided fw_cfg item names "
2239 "should be prefixed with \"opt/\"");
2240 }
2241 if (nonempty_str(str)) {
2242 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2243 buf = g_memdup(str, size);
2244 } else {
2245 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2246 error_report("can't load %s", file);
2247 return -1;
2248 }
2249 }
2250 /* For legacy, keep user files in a specific global order. */
2251 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2252 fw_cfg_add_file(fw_cfg, name, buf, size);
2253 fw_cfg_reset_order_override(fw_cfg);
2254 return 0;
2255 }
2256
2257 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2258 {
2259 return qdev_device_help(opts);
2260 }
2261
2262 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2263 {
2264 Error *err = NULL;
2265 DeviceState *dev;
2266
2267 dev = qdev_device_add(opts, &err);
2268 if (!dev) {
2269 error_report_err(err);
2270 return -1;
2271 }
2272 object_unref(OBJECT(dev));
2273 return 0;
2274 }
2275
2276 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2277 {
2278 Error *local_err = NULL;
2279
2280 qemu_chr_new_from_opts(opts, &local_err);
2281 if (local_err) {
2282 error_report_err(local_err);
2283 return -1;
2284 }
2285 return 0;
2286 }
2287
2288 #ifdef CONFIG_VIRTFS
2289 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2290 {
2291 return qemu_fsdev_add(opts);
2292 }
2293 #endif
2294
2295 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2296 {
2297 Chardev *chr;
2298 const char *chardev;
2299 const char *mode;
2300 int flags;
2301
2302 mode = qemu_opt_get(opts, "mode");
2303 if (mode == NULL) {
2304 mode = "readline";
2305 }
2306 if (strcmp(mode, "readline") == 0) {
2307 flags = MONITOR_USE_READLINE;
2308 } else if (strcmp(mode, "control") == 0) {
2309 flags = MONITOR_USE_CONTROL;
2310 } else {
2311 error_report("unknown monitor mode \"%s\"", mode);
2312 exit(1);
2313 }
2314
2315 if (qemu_opt_get_bool(opts, "pretty", 0))
2316 flags |= MONITOR_USE_PRETTY;
2317
2318 if (qemu_opt_get_bool(opts, "default", 0)) {
2319 error_report("option 'default' does nothing and is deprecated");
2320 }
2321
2322 chardev = qemu_opt_get(opts, "chardev");
2323 chr = qemu_chr_find(chardev);
2324 if (chr == NULL) {
2325 error_report("chardev \"%s\" not found", chardev);
2326 exit(1);
2327 }
2328
2329 monitor_init(chr, flags);
2330 return 0;
2331 }
2332
2333 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2334 {
2335 static int monitor_device_index = 0;
2336 QemuOpts *opts;
2337 const char *p;
2338 char label[32];
2339
2340 if (strstart(optarg, "chardev:", &p)) {
2341 snprintf(label, sizeof(label), "%s", p);
2342 } else {
2343 snprintf(label, sizeof(label), "compat_monitor%d",
2344 monitor_device_index);
2345 opts = qemu_chr_parse_compat(label, optarg);
2346 if (!opts) {
2347 error_report("parse error: %s", optarg);
2348 exit(1);
2349 }
2350 }
2351
2352 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2353 qemu_opt_set(opts, "mode", mode, &error_abort);
2354 qemu_opt_set(opts, "chardev", label, &error_abort);
2355 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2356 monitor_device_index++;
2357 }
2358
2359 struct device_config {
2360 enum {
2361 DEV_USB, /* -usbdevice */
2362 DEV_BT, /* -bt */
2363 DEV_SERIAL, /* -serial */
2364 DEV_PARALLEL, /* -parallel */
2365 DEV_VIRTCON, /* -virtioconsole */
2366 DEV_DEBUGCON, /* -debugcon */
2367 DEV_GDB, /* -gdb, -s */
2368 DEV_SCLP, /* s390 sclp */
2369 } type;
2370 const char *cmdline;
2371 Location loc;
2372 QTAILQ_ENTRY(device_config) next;
2373 };
2374
2375 static QTAILQ_HEAD(, device_config) device_configs =
2376 QTAILQ_HEAD_INITIALIZER(device_configs);
2377
2378 static void add_device_config(int type, const char *cmdline)
2379 {
2380 struct device_config *conf;
2381
2382 conf = g_malloc0(sizeof(*conf));
2383 conf->type = type;
2384 conf->cmdline = cmdline;
2385 loc_save(&conf->loc);
2386 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2387 }
2388
2389 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2390 {
2391 struct device_config *conf;
2392 int rc;
2393
2394 QTAILQ_FOREACH(conf, &device_configs, next) {
2395 if (conf->type != type)
2396 continue;
2397 loc_push_restore(&conf->loc);
2398 rc = func(conf->cmdline);
2399 loc_pop(&conf->loc);
2400 if (rc) {
2401 return rc;
2402 }
2403 }
2404 return 0;
2405 }
2406
2407 static int serial_parse(const char *devname)
2408 {
2409 static int index = 0;
2410 char label[32];
2411
2412 if (strcmp(devname, "none") == 0)
2413 return 0;
2414 if (index == MAX_SERIAL_PORTS) {
2415 error_report("too many serial ports");
2416 exit(1);
2417 }
2418 snprintf(label, sizeof(label), "serial%d", index);
2419 serial_hds[index] = qemu_chr_new(label, devname);
2420 if (!serial_hds[index]) {
2421 error_report("could not connect serial device"
2422 " to character backend '%s'", devname);
2423 return -1;
2424 }
2425 index++;
2426 return 0;
2427 }
2428
2429 static int parallel_parse(const char *devname)
2430 {
2431 static int index = 0;
2432 char label[32];
2433
2434 if (strcmp(devname, "none") == 0)
2435 return 0;
2436 if (index == MAX_PARALLEL_PORTS) {
2437 error_report("too many parallel ports");
2438 exit(1);
2439 }
2440 snprintf(label, sizeof(label), "parallel%d", index);
2441 parallel_hds[index] = qemu_chr_new(label, devname);
2442 if (!parallel_hds[index]) {
2443 error_report("could not connect parallel device"
2444 " to character backend '%s'", devname);
2445 return -1;
2446 }
2447 index++;
2448 return 0;
2449 }
2450
2451 static int virtcon_parse(const char *devname)
2452 {
2453 QemuOptsList *device = qemu_find_opts("device");
2454 static int index = 0;
2455 char label[32];
2456 QemuOpts *bus_opts, *dev_opts;
2457
2458 if (strcmp(devname, "none") == 0)
2459 return 0;
2460 if (index == MAX_VIRTIO_CONSOLES) {
2461 error_report("too many virtio consoles");
2462 exit(1);
2463 }
2464
2465 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2466 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2467
2468 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2469 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2470
2471 snprintf(label, sizeof(label), "virtcon%d", index);
2472 virtcon_hds[index] = qemu_chr_new(label, devname);
2473 if (!virtcon_hds[index]) {
2474 error_report("could not connect virtio console"
2475 " to character backend '%s'", devname);
2476 return -1;
2477 }
2478 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2479
2480 index++;
2481 return 0;
2482 }
2483
2484 static int sclp_parse(const char *devname)
2485 {
2486 QemuOptsList *device = qemu_find_opts("device");
2487 static int index = 0;
2488 char label[32];
2489 QemuOpts *dev_opts;
2490
2491 if (strcmp(devname, "none") == 0) {
2492 return 0;
2493 }
2494 if (index == MAX_SCLP_CONSOLES) {
2495 error_report("too many sclp consoles");
2496 exit(1);
2497 }
2498
2499 assert(arch_type == QEMU_ARCH_S390X);
2500
2501 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2502 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2503
2504 snprintf(label, sizeof(label), "sclpcon%d", index);
2505 sclp_hds[index] = qemu_chr_new(label, devname);
2506 if (!sclp_hds[index]) {
2507 error_report("could not connect sclp console"
2508 " to character backend '%s'", devname);
2509 return -1;
2510 }
2511 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2512
2513 index++;
2514 return 0;
2515 }
2516
2517 static int debugcon_parse(const char *devname)
2518 {
2519 QemuOpts *opts;
2520
2521 if (!qemu_chr_new("debugcon", devname)) {
2522 exit(1);
2523 }
2524 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2525 if (!opts) {
2526 error_report("already have a debugcon device");
2527 exit(1);
2528 }
2529 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2530 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2531 return 0;
2532 }
2533
2534 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2535 {
2536 const MachineClass *mc1 = a, *mc2 = b;
2537 int res;
2538
2539 if (mc1->family == NULL) {
2540 if (mc2->family == NULL) {
2541 /* Compare standalone machine types against each other; they sort
2542 * in increasing order.
2543 */
2544 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2545 object_class_get_name(OBJECT_CLASS(mc2)));
2546 }
2547
2548 /* Standalone machine types sort after families. */
2549 return 1;
2550 }
2551
2552 if (mc2->family == NULL) {
2553 /* Families sort before standalone machine types. */
2554 return -1;
2555 }
2556
2557 /* Families sort between each other alphabetically increasingly. */
2558 res = strcmp(mc1->family, mc2->family);
2559 if (res != 0) {
2560 return res;
2561 }
2562
2563 /* Within the same family, machine types sort in decreasing order. */
2564 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2565 object_class_get_name(OBJECT_CLASS(mc1)));
2566 }
2567
2568 static MachineClass *machine_parse(const char *name)
2569 {
2570 MachineClass *mc = NULL;
2571 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2572
2573 if (name) {
2574 mc = find_machine(name);
2575 }
2576 if (mc) {
2577 g_slist_free(machines);
2578 return mc;
2579 }
2580 if (name && !is_help_option(name)) {
2581 error_report("unsupported machine type");
2582 error_printf("Use -machine help to list supported machines\n");
2583 } else {
2584 printf("Supported machines are:\n");
2585 machines = g_slist_sort(machines, machine_class_cmp);
2586 for (el = machines; el; el = el->next) {
2587 MachineClass *mc = el->data;
2588 if (mc->alias) {
2589 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2590 }
2591 printf("%-20s %s%s\n", mc->name, mc->desc,
2592 mc->is_default ? " (default)" : "");
2593 }
2594 }
2595
2596 g_slist_free(machines);
2597 exit(!name || !is_help_option(name));
2598 }
2599
2600 void qemu_add_exit_notifier(Notifier *notify)
2601 {
2602 notifier_list_add(&exit_notifiers, notify);
2603 }
2604
2605 void qemu_remove_exit_notifier(Notifier *notify)
2606 {
2607 notifier_remove(notify);
2608 }
2609
2610 static void qemu_run_exit_notifiers(void)
2611 {
2612 notifier_list_notify(&exit_notifiers, NULL);
2613 }
2614
2615 static bool machine_init_done;
2616
2617 void qemu_add_machine_init_done_notifier(Notifier *notify)
2618 {
2619 notifier_list_add(&machine_init_done_notifiers, notify);
2620 if (machine_init_done) {
2621 notify->notify(notify, NULL);
2622 }
2623 }
2624
2625 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2626 {
2627 notifier_remove(notify);
2628 }
2629
2630 static void qemu_run_machine_init_done_notifiers(void)
2631 {
2632 notifier_list_notify(&machine_init_done_notifiers, NULL);
2633 machine_init_done = true;
2634 }
2635
2636 static const QEMUOption *lookup_opt(int argc, char **argv,
2637 const char **poptarg, int *poptind)
2638 {
2639 const QEMUOption *popt;
2640 int optind = *poptind;
2641 char *r = argv[optind];
2642 const char *optarg;
2643
2644 loc_set_cmdline(argv, optind, 1);
2645 optind++;
2646 /* Treat --foo the same as -foo. */
2647 if (r[1] == '-')
2648 r++;
2649 popt = qemu_options;
2650 for(;;) {
2651 if (!popt->name) {
2652 error_report("invalid option");
2653 exit(1);
2654 }
2655 if (!strcmp(popt->name, r + 1))
2656 break;
2657 popt++;
2658 }
2659 if (popt->flags & HAS_ARG) {
2660 if (optind >= argc) {
2661 error_report("requires an argument");
2662 exit(1);
2663 }
2664 optarg = argv[optind++];
2665 loc_set_cmdline(argv, optind - 2, 2);
2666 } else {
2667 optarg = NULL;
2668 }
2669
2670 *poptarg = optarg;
2671 *poptind = optind;
2672
2673 return popt;
2674 }
2675
2676 static MachineClass *select_machine(void)
2677 {
2678 MachineClass *machine_class = find_default_machine();
2679 const char *optarg;
2680 QemuOpts *opts;
2681 Location loc;
2682
2683 loc_push_none(&loc);
2684
2685 opts = qemu_get_machine_opts();
2686 qemu_opts_loc_restore(opts);
2687
2688 optarg = qemu_opt_get(opts, "type");
2689 if (optarg) {
2690 machine_class = machine_parse(optarg);
2691 }
2692
2693 if (!machine_class) {
2694 error_report("No machine specified, and there is no default");
2695 error_printf("Use -machine help to list supported machines\n");
2696 exit(1);
2697 }
2698
2699 loc_pop(&loc);
2700 return machine_class;
2701 }
2702
2703 static int machine_set_property(void *opaque,
2704 const char *name, const char *value,
2705 Error **errp)
2706 {
2707 Object *obj = OBJECT(opaque);
2708 Error *local_err = NULL;
2709 char *p, *qom_name;
2710
2711 if (strcmp(name, "type") == 0) {
2712 return 0;
2713 }
2714
2715 qom_name = g_strdup(name);
2716 for (p = qom_name; *p; p++) {
2717 if (*p == '_') {
2718 *p = '-';
2719 }
2720 }
2721
2722 object_property_parse(obj, value, qom_name, &local_err);
2723 g_free(qom_name);
2724
2725 if (local_err) {
2726 error_report_err(local_err);
2727 return -1;
2728 }
2729
2730 return 0;
2731 }
2732
2733
2734 /*
2735 * Initial object creation happens before all other
2736 * QEMU data types are created. The majority of objects
2737 * can be created at this point. The rng-egd object
2738 * cannot be created here, as it depends on the chardev
2739 * already existing.
2740 */
2741 static bool object_create_initial(const char *type)
2742 {
2743 if (g_str_equal(type, "rng-egd")) {
2744 return false;
2745 }
2746
2747 /*
2748 * return false for concrete netfilters since
2749 * they depend on netdevs already existing
2750 */
2751 if (g_str_equal(type, "filter-buffer") ||
2752 g_str_equal(type, "filter-dump") ||
2753 g_str_equal(type, "filter-mirror") ||
2754 g_str_equal(type, "filter-redirector") ||
2755 g_str_equal(type, "colo-compare") ||
2756 g_str_equal(type, "filter-rewriter") ||
2757 g_str_equal(type, "filter-replay")) {
2758 return false;
2759 }
2760
2761 /* Memory allocation by backends needs to be done
2762 * after configure_accelerator() (due to the tcg_enabled()
2763 * checks at memory_region_init_*()).
2764 *
2765 * Also, allocation of large amounts of memory may delay
2766 * chardev initialization for too long, and trigger timeouts
2767 * on software that waits for a monitor socket to be created
2768 * (e.g. libvirt).
2769 */
2770 if (g_str_has_prefix(type, "memory-backend-")) {
2771 return false;
2772 }
2773
2774 return true;
2775 }
2776
2777
2778 /*
2779 * The remainder of object creation happens after the
2780 * creation of chardev, fsdev, net clients and device data types.
2781 */
2782 static bool object_create_delayed(const char *type)
2783 {
2784 return !object_create_initial(type);
2785 }
2786
2787
2788 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2789 MachineClass *mc)
2790 {
2791 uint64_t sz;
2792 const char *mem_str;
2793 const char *maxmem_str, *slots_str;
2794 const ram_addr_t default_ram_size = mc->default_ram_size;
2795 QemuOpts *opts = qemu_find_opts_singleton("memory");
2796 Location loc;
2797
2798 loc_push_none(&loc);
2799 qemu_opts_loc_restore(opts);
2800
2801 sz = 0;
2802 mem_str = qemu_opt_get(opts, "size");
2803 if (mem_str) {
2804 if (!*mem_str) {
2805 error_report("missing 'size' option value");
2806 exit(EXIT_FAILURE);
2807 }
2808
2809 sz = qemu_opt_get_size(opts, "size", ram_size);
2810
2811 /* Fix up legacy suffix-less format */
2812 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2813 uint64_t overflow_check = sz;
2814
2815 sz <<= 20;
2816 if ((sz >> 20) != overflow_check) {
2817 error_report("too large 'size' option value");
2818 exit(EXIT_FAILURE);
2819 }
2820 }
2821 }
2822
2823 /* backward compatibility behaviour for case "-m 0" */
2824 if (sz == 0) {
2825 sz = default_ram_size;
2826 }
2827
2828 sz = QEMU_ALIGN_UP(sz, 8192);
2829 ram_size = sz;
2830 if (ram_size != sz) {
2831 error_report("ram size too large");
2832 exit(EXIT_FAILURE);
2833 }
2834
2835 /* store value for the future use */
2836 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2837 *maxram_size = ram_size;
2838
2839 maxmem_str = qemu_opt_get(opts, "maxmem");
2840 slots_str = qemu_opt_get(opts, "slots");
2841 if (maxmem_str && slots_str) {
2842 uint64_t slots;
2843
2844 sz = qemu_opt_get_size(opts, "maxmem", 0);
2845 slots = qemu_opt_get_number(opts, "slots", 0);
2846 if (sz < ram_size) {
2847 error_report("invalid value of -m option maxmem: "
2848 "maximum memory size (0x%" PRIx64 ") must be at least "
2849 "the initial memory size (0x" RAM_ADDR_FMT ")",
2850 sz, ram_size);
2851 exit(EXIT_FAILURE);
2852 } else if (sz > ram_size) {
2853 if (!slots) {
2854 error_report("invalid value of -m option: maxmem was "
2855 "specified, but no hotplug slots were specified");
2856 exit(EXIT_FAILURE);
2857 }
2858 } else if (slots) {
2859 error_report("invalid value of -m option maxmem: "
2860 "memory slots were specified but maximum memory size "
2861 "(0x%" PRIx64 ") is equal to the initial memory size "
2862 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2863 exit(EXIT_FAILURE);
2864 }
2865
2866 *maxram_size = sz;
2867 *ram_slots = slots;
2868 } else if ((!maxmem_str && slots_str) ||
2869 (maxmem_str && !slots_str)) {
2870 error_report("invalid -m option value: missing "
2871 "'%s' option", slots_str ? "maxmem" : "slots");
2872 exit(EXIT_FAILURE);
2873 }
2874
2875 loc_pop(&loc);
2876 }
2877
2878 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2879 {
2880 GlobalProperty *g;
2881
2882 g = g_malloc0(sizeof(*g));
2883 g->driver = qemu_opt_get(opts, "driver");
2884 g->property = qemu_opt_get(opts, "property");
2885 g->value = qemu_opt_get(opts, "value");
2886 g->user_provided = true;
2887 g->errp = &error_fatal;
2888 qdev_prop_register_global(g);
2889 return 0;
2890 }
2891
2892 static int qemu_read_default_config_file(void)
2893 {
2894 int ret;
2895
2896 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2897 if (ret < 0 && ret != -ENOENT) {
2898 return ret;
2899 }
2900
2901 return 0;
2902 }
2903
2904 int main(int argc, char **argv, char **envp)
2905 {
2906 int i;
2907 int snapshot, linux_boot;
2908 const char *initrd_filename;
2909 const char *kernel_filename, *kernel_cmdline;
2910 const char *boot_order = NULL;
2911 const char *boot_once = NULL;
2912 DisplayState *ds;
2913 int cyls, heads, secs, translation;
2914 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2915 QemuOptsList *olist;
2916 int optind;
2917 const char *optarg;
2918 const char *loadvm = NULL;
2919 MachineClass *machine_class;
2920 const char *cpu_model;
2921 const char *vga_model = NULL;
2922 const char *qtest_chrdev = NULL;
2923 const char *qtest_log = NULL;
2924 const char *pid_file = NULL;
2925 const char *incoming = NULL;
2926 bool defconfig = true;
2927 bool userconfig = true;
2928 bool nographic = false;
2929 DisplayType display_type = DT_DEFAULT;
2930 int display_remote = 0;
2931 const char *log_mask = NULL;
2932 const char *log_file = NULL;
2933 char *trace_file = NULL;
2934 ram_addr_t maxram_size;
2935 uint64_t ram_slots = 0;
2936 FILE *vmstate_dump_file = NULL;
2937 Error *main_loop_err = NULL;
2938 Error *err = NULL;
2939 bool list_data_dirs = false;
2940
2941 module_call_init(MODULE_INIT_TRACE);
2942
2943 qemu_init_cpu_list();
2944 qemu_init_cpu_loop();
2945 qemu_mutex_lock_iothread();
2946
2947 atexit(qemu_run_exit_notifiers);
2948 error_set_progname(argv[0]);
2949 qemu_init_exec_dir(argv[0]);
2950
2951 module_call_init(MODULE_INIT_QOM);
2952 module_call_init(MODULE_INIT_QAPI);
2953
2954 qemu_add_opts(&qemu_drive_opts);
2955 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2956 qemu_add_drive_opts(&qemu_common_drive_opts);
2957 qemu_add_drive_opts(&qemu_drive_opts);
2958 qemu_add_drive_opts(&bdrv_runtime_opts);
2959 qemu_add_opts(&qemu_chardev_opts);
2960 qemu_add_opts(&qemu_device_opts);
2961 qemu_add_opts(&qemu_netdev_opts);
2962 qemu_add_opts(&qemu_net_opts);
2963 qemu_add_opts(&qemu_rtc_opts);
2964 qemu_add_opts(&qemu_global_opts);
2965 qemu_add_opts(&qemu_mon_opts);
2966 qemu_add_opts(&qemu_trace_opts);
2967 qemu_add_opts(&qemu_option_rom_opts);
2968 qemu_add_opts(&qemu_machine_opts);
2969 qemu_add_opts(&qemu_mem_opts);
2970 qemu_add_opts(&qemu_smp_opts);
2971 qemu_add_opts(&qemu_boot_opts);
2972 qemu_add_opts(&qemu_sandbox_opts);
2973 qemu_add_opts(&qemu_add_fd_opts);
2974 qemu_add_opts(&qemu_object_opts);
2975 qemu_add_opts(&qemu_tpmdev_opts);
2976 qemu_add_opts(&qemu_realtime_opts);
2977 qemu_add_opts(&qemu_msg_opts);
2978 qemu_add_opts(&qemu_name_opts);
2979 qemu_add_opts(&qemu_numa_opts);
2980 qemu_add_opts(&qemu_icount_opts);
2981 qemu_add_opts(&qemu_semihosting_config_opts);
2982 qemu_add_opts(&qemu_fw_cfg_opts);
2983 module_call_init(MODULE_INIT_OPTS);
2984
2985 runstate_init();
2986
2987 if (qcrypto_init(&err) < 0) {
2988 error_reportf_err(err, "cannot initialize crypto: ");
2989 exit(1);
2990 }
2991 rtc_clock = QEMU_CLOCK_HOST;
2992
2993 QLIST_INIT (&vm_change_state_head);
2994 os_setup_early_signal_handling();
2995
2996 cpu_model = NULL;
2997 snapshot = 0;
2998 cyls = heads = secs = 0;
2999 translation = BIOS_ATA_TRANSLATION_AUTO;
3000
3001 nb_nics = 0;
3002
3003 bdrv_init_with_whitelist();
3004
3005 autostart = 1;
3006
3007 /* first pass of option parsing */
3008 optind = 1;
3009 while (optind < argc) {
3010 if (argv[optind][0] != '-') {
3011 /* disk image */
3012 optind++;
3013 } else {
3014 const QEMUOption *popt;
3015
3016 popt = lookup_opt(argc, argv, &optarg, &optind);
3017 switch (popt->index) {
3018 case QEMU_OPTION_nodefconfig:
3019 defconfig = false;
3020 break;
3021 case QEMU_OPTION_nouserconfig:
3022 userconfig = false;
3023 break;
3024 }
3025 }
3026 }
3027
3028 if (defconfig && userconfig) {
3029 if (qemu_read_default_config_file() < 0) {
3030 exit(1);
3031 }
3032 }
3033
3034 /* second pass of option parsing */
3035 optind = 1;
3036 for(;;) {
3037 if (optind >= argc)
3038 break;
3039 if (argv[optind][0] != '-') {
3040 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3041 } else {
3042 const QEMUOption *popt;
3043
3044 popt = lookup_opt(argc, argv, &optarg, &optind);
3045 if (!(popt->arch_mask & arch_type)) {
3046 error_report("Option not supported for this target");
3047 exit(1);
3048 }
3049 switch(popt->index) {
3050 case QEMU_OPTION_no_kvm_irqchip: {
3051 olist = qemu_find_opts("machine");
3052 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3053 break;
3054 }
3055 case QEMU_OPTION_cpu:
3056 /* hw initialization will check this */
3057 cpu_model = optarg;
3058 break;
3059 case QEMU_OPTION_hda:
3060 {
3061 char buf[256];
3062 if (cyls == 0)
3063 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3064 else
3065 snprintf(buf, sizeof(buf),
3066 "%s,cyls=%d,heads=%d,secs=%d%s",
3067 HD_OPTS , cyls, heads, secs,
3068 translation == BIOS_ATA_TRANSLATION_LBA ?
3069 ",trans=lba" :
3070 translation == BIOS_ATA_TRANSLATION_NONE ?
3071 ",trans=none" : "");
3072 drive_add(IF_DEFAULT, 0, optarg, buf);
3073 break;
3074 }
3075 case QEMU_OPTION_hdb:
3076 case QEMU_OPTION_hdc:
3077 case QEMU_OPTION_hdd:
3078 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3079 HD_OPTS);
3080 break;
3081 case QEMU_OPTION_drive:
3082 if (drive_def(optarg) == NULL) {
3083 exit(1);
3084 }
3085 break;
3086 case QEMU_OPTION_set:
3087 if (qemu_set_option(optarg) != 0)
3088 exit(1);
3089 break;
3090 case QEMU_OPTION_global:
3091 if (qemu_global_option(optarg) != 0)
3092 exit(1);
3093 break;
3094 case QEMU_OPTION_mtdblock:
3095 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3096 break;
3097 case QEMU_OPTION_sd:
3098 drive_add(IF_SD, -1, optarg, SD_OPTS);
3099 break;
3100 case QEMU_OPTION_pflash:
3101 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3102 break;
3103 case QEMU_OPTION_snapshot:
3104 snapshot = 1;
3105 break;
3106 case QEMU_OPTION_hdachs:
3107 {
3108 const char *p;
3109 p = optarg;
3110 cyls = strtol(p, (char **)&p, 0);
3111 if (cyls < 1 || cyls > 16383)
3112 goto chs_fail;
3113 if (*p != ',')
3114 goto chs_fail;
3115 p++;
3116 heads = strtol(p, (char **)&p, 0);
3117 if (heads < 1 || heads > 16)
3118 goto chs_fail;
3119 if (*p != ',')
3120 goto chs_fail;
3121 p++;
3122 secs = strtol(p, (char **)&p, 0);
3123 if (secs < 1 || secs > 63)
3124 goto chs_fail;
3125 if (*p == ',') {
3126 p++;
3127 if (!strcmp(p, "large")) {
3128 translation = BIOS_ATA_TRANSLATION_LARGE;
3129 } else if (!strcmp(p, "rechs")) {
3130 translation = BIOS_ATA_TRANSLATION_RECHS;
3131 } else if (!strcmp(p, "none")) {
3132 translation = BIOS_ATA_TRANSLATION_NONE;
3133 } else if (!strcmp(p, "lba")) {
3134 translation = BIOS_ATA_TRANSLATION_LBA;
3135 } else if (!strcmp(p, "auto")) {
3136 translation = BIOS_ATA_TRANSLATION_AUTO;
3137 } else {
3138 goto chs_fail;
3139 }
3140 } else if (*p != '\0') {
3141 chs_fail:
3142 error_report("invalid physical CHS format");
3143 exit(1);
3144 }
3145 if (hda_opts != NULL) {
3146 qemu_opt_set_number(hda_opts, "cyls", cyls,
3147 &error_abort);
3148 qemu_opt_set_number(hda_opts, "heads", heads,
3149 &error_abort);
3150 qemu_opt_set_number(hda_opts, "secs", secs,
3151 &error_abort);
3152 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3153 qemu_opt_set(hda_opts, "trans", "large",
3154 &error_abort);
3155 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3156 qemu_opt_set(hda_opts, "trans", "rechs",
3157 &error_abort);
3158 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3159 qemu_opt_set(hda_opts, "trans", "lba",
3160 &error_abort);
3161 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3162 qemu_opt_set(hda_opts, "trans", "none",
3163 &error_abort);
3164 }
3165 }
3166 }
3167 break;
3168 case QEMU_OPTION_numa:
3169 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3170 optarg, true);
3171 if (!opts) {
3172 exit(1);
3173 }
3174 break;
3175 case QEMU_OPTION_display:
3176 display_type = select_display(optarg);
3177 break;
3178 case QEMU_OPTION_nographic:
3179 olist = qemu_find_opts("machine");
3180 qemu_opts_parse_noisily(olist, "graphics=off", false);
3181 nographic = true;
3182 display_type = DT_NONE;
3183 break;
3184 case QEMU_OPTION_curses:
3185 #ifdef CONFIG_CURSES
3186 display_type = DT_CURSES;
3187 #else
3188 error_report("curses support is disabled");
3189 exit(1);
3190 #endif
3191 break;
3192 case QEMU_OPTION_portrait:
3193 graphic_rotate = 90;
3194 break;
3195 case QEMU_OPTION_rotate:
3196 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3197 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3198 graphic_rotate != 180 && graphic_rotate != 270) {
3199 error_report("only 90, 180, 270 deg rotation is available");
3200 exit(1);
3201 }
3202 break;
3203 case QEMU_OPTION_kernel:
3204 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3205 &error_abort);
3206 break;
3207 case QEMU_OPTION_initrd:
3208 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3209 &error_abort);
3210 break;
3211 case QEMU_OPTION_append:
3212 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3213 &error_abort);
3214 break;
3215 case QEMU_OPTION_dtb:
3216 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3217 &error_abort);
3218 break;
3219 case QEMU_OPTION_cdrom:
3220 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3221 break;
3222 case QEMU_OPTION_boot:
3223 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3224 optarg, true);
3225 if (!opts) {
3226 exit(1);
3227 }
3228 break;
3229 case QEMU_OPTION_fda:
3230 case QEMU_OPTION_fdb:
3231 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3232 optarg, FD_OPTS);
3233 break;
3234 case QEMU_OPTION_no_fd_bootchk:
3235 fd_bootchk = 0;
3236 break;
3237 case QEMU_OPTION_netdev:
3238 default_net = 0;
3239 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3240 exit(1);
3241 }
3242 break;
3243 case QEMU_OPTION_net:
3244 default_net = 0;
3245 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3246 exit(1);
3247 }
3248 break;
3249 #ifdef CONFIG_LIBISCSI
3250 case QEMU_OPTION_iscsi:
3251 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3252 optarg, false);
3253 if (!opts) {
3254 exit(1);
3255 }
3256 break;
3257 #endif
3258 #ifdef CONFIG_SLIRP
3259 case QEMU_OPTION_tftp:
3260 error_report("The -tftp option is deprecated. "
3261 "Please use '-netdev user,tftp=...' instead.");
3262 legacy_tftp_prefix = optarg;
3263 break;
3264 case QEMU_OPTION_bootp:
3265 error_report("The -bootp option is deprecated. "
3266 "Please use '-netdev user,bootfile=...' instead.");
3267 legacy_bootp_filename = optarg;
3268 break;
3269 case QEMU_OPTION_redir:
3270 error_report("The -redir option is deprecated. "
3271 "Please use '-netdev user,hostfwd=...' instead.");
3272 if (net_slirp_redir(optarg) < 0)
3273 exit(1);
3274 break;
3275 #endif
3276 case QEMU_OPTION_bt:
3277 add_device_config(DEV_BT, optarg);
3278 break;
3279 case QEMU_OPTION_audio_help:
3280 AUD_help ();
3281 exit (0);
3282 break;
3283 case QEMU_OPTION_soundhw:
3284 select_soundhw (optarg);
3285 break;
3286 case QEMU_OPTION_h:
3287 help(0);
3288 break;
3289 case QEMU_OPTION_version:
3290 version();
3291 exit(0);
3292 break;
3293 case QEMU_OPTION_m:
3294 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3295 optarg, true);
3296 if (!opts) {
3297 exit(EXIT_FAILURE);
3298 }
3299 break;
3300 #ifdef CONFIG_TPM
3301 case QEMU_OPTION_tpmdev:
3302 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3303 exit(1);
3304 }
3305 break;
3306 #endif
3307 case QEMU_OPTION_mempath:
3308 mem_path = optarg;
3309 break;
3310 case QEMU_OPTION_mem_prealloc:
3311 mem_prealloc = 1;
3312 break;
3313 case QEMU_OPTION_d:
3314 log_mask = optarg;
3315 break;
3316 case QEMU_OPTION_D:
3317 log_file = optarg;
3318 break;
3319 case QEMU_OPTION_DFILTER:
3320 qemu_set_dfilter_ranges(optarg, &error_fatal);
3321 break;
3322 case QEMU_OPTION_s:
3323 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3324 break;
3325 case QEMU_OPTION_gdb:
3326 add_device_config(DEV_GDB, optarg);
3327 break;
3328 case QEMU_OPTION_L:
3329 if (is_help_option(optarg)) {
3330 list_data_dirs = true;
3331 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3332 data_dir[data_dir_idx++] = optarg;
3333 }
3334 break;
3335 case QEMU_OPTION_bios:
3336 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3337 &error_abort);
3338 break;
3339 case QEMU_OPTION_singlestep:
3340 singlestep = 1;
3341 break;
3342 case QEMU_OPTION_S:
3343 autostart = 0;
3344 break;
3345 case QEMU_OPTION_k:
3346 keyboard_layout = optarg;
3347 break;
3348 case QEMU_OPTION_localtime:
3349 rtc_utc = 0;
3350 break;
3351 case QEMU_OPTION_vga:
3352 vga_model = optarg;
3353 default_vga = 0;
3354 break;
3355 case QEMU_OPTION_g:
3356 {
3357 const char *p;
3358 int w, h, depth;
3359 p = optarg;
3360 w = strtol(p, (char **)&p, 10);
3361 if (w <= 0) {
3362 graphic_error:
3363 error_report("invalid resolution or depth");
3364 exit(1);
3365 }
3366 if (*p != 'x')
3367 goto graphic_error;
3368 p++;
3369 h = strtol(p, (char **)&p, 10);
3370 if (h <= 0)
3371 goto graphic_error;
3372 if (*p == 'x') {
3373 p++;
3374 depth = strtol(p, (char **)&p, 10);
3375 if (depth != 8 && depth != 15 && depth != 16 &&
3376 depth != 24 && depth != 32)
3377 goto graphic_error;
3378 } else if (*p == '\0') {
3379 depth = graphic_depth;
3380 } else {
3381 goto graphic_error;
3382 }
3383
3384 graphic_width = w;
3385 graphic_height = h;
3386 graphic_depth = depth;
3387 }
3388 break;
3389 case QEMU_OPTION_echr:
3390 {
3391 char *r;
3392 term_escape_char = strtol(optarg, &r, 0);
3393 if (r == optarg)
3394 printf("Bad argument to echr\n");
3395 break;
3396 }
3397 case QEMU_OPTION_monitor:
3398 default_monitor = 0;
3399 if (strncmp(optarg, "none", 4)) {
3400 monitor_parse(optarg, "readline", false);
3401 }
3402 break;
3403 case QEMU_OPTION_qmp:
3404 monitor_parse(optarg, "control", false);
3405 default_monitor = 0;
3406 break;
3407 case QEMU_OPTION_qmp_pretty:
3408 monitor_parse(optarg, "control", true);
3409 default_monitor = 0;
3410 break;
3411 case QEMU_OPTION_mon:
3412 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3413 true);
3414 if (!opts) {
3415 exit(1);
3416 }
3417 default_monitor = 0;
3418 break;
3419 case QEMU_OPTION_chardev:
3420 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3421 optarg, true);
3422 if (!opts) {
3423 exit(1);
3424 }
3425 break;
3426 case QEMU_OPTION_fsdev:
3427 olist = qemu_find_opts("fsdev");
3428 if (!olist) {
3429 error_report("fsdev support is disabled");
3430 exit(1);
3431 }
3432 opts = qemu_opts_parse_noisily(olist, optarg, true);
3433 if (!opts) {
3434 exit(1);
3435 }
3436 break;
3437 case QEMU_OPTION_virtfs: {
3438 QemuOpts *fsdev;
3439 QemuOpts *device;
3440 const char *writeout, *sock_fd, *socket;
3441
3442 olist = qemu_find_opts("virtfs");
3443 if (!olist) {
3444 error_report("virtfs support is disabled");
3445 exit(1);
3446 }
3447 opts = qemu_opts_parse_noisily(olist, optarg, true);
3448 if (!opts) {
3449 exit(1);
3450 }
3451
3452 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3453 qemu_opt_get(opts, "mount_tag") == NULL) {
3454 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3455 exit(1);
3456 }
3457 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3458 qemu_opt_get(opts, "mount_tag"),
3459 1, NULL);
3460 if (!fsdev) {
3461 error_report("duplicate fsdev id: %s",
3462 qemu_opt_get(opts, "mount_tag"));
3463 exit(1);
3464 }
3465
3466 writeout = qemu_opt_get(opts, "writeout");
3467 if (writeout) {
3468 #ifdef CONFIG_SYNC_FILE_RANGE
3469 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3470 #else
3471 error_report("writeout=immediate not supported "
3472 "on this platform");
3473 exit(1);
3474 #endif
3475 }
3476 qemu_opt_set(fsdev, "fsdriver",
3477 qemu_opt_get(opts, "fsdriver"), &error_abort);
3478 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3479 &error_abort);
3480 qemu_opt_set(fsdev, "security_model",
3481 qemu_opt_get(opts, "security_model"),
3482 &error_abort);
3483 socket = qemu_opt_get(opts, "socket");
3484 if (socket) {
3485 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3486 }
3487 sock_fd = qemu_opt_get(opts, "sock_fd");
3488 if (sock_fd) {
3489 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3490 }
3491
3492 qemu_opt_set_bool(fsdev, "readonly",
3493 qemu_opt_get_bool(opts, "readonly", 0),
3494 &error_abort);
3495 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3496 &error_abort);
3497 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3498 qemu_opt_set(device, "fsdev",
3499 qemu_opt_get(opts, "mount_tag"), &error_abort);
3500 qemu_opt_set(device, "mount_tag",
3501 qemu_opt_get(opts, "mount_tag"), &error_abort);
3502 break;
3503 }
3504 case QEMU_OPTION_virtfs_synth: {
3505 QemuOpts *fsdev;
3506 QemuOpts *device;
3507
3508 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3509 1, NULL);
3510 if (!fsdev) {
3511 error_report("duplicate option: %s", "virtfs_synth");
3512 exit(1);
3513 }
3514 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3515
3516 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3517 &error_abort);
3518 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3519 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3520 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3521 break;
3522 }
3523 case QEMU_OPTION_serial:
3524 add_device_config(DEV_SERIAL, optarg);
3525 default_serial = 0;
3526 if (strncmp(optarg, "mon:", 4) == 0) {
3527 default_monitor = 0;
3528 }
3529 break;
3530 case QEMU_OPTION_watchdog:
3531 if (watchdog) {
3532 error_report("only one watchdog option may be given");
3533 return 1;
3534 }
3535 watchdog = optarg;
3536 break;
3537 case QEMU_OPTION_watchdog_action:
3538 if (select_watchdog_action(optarg) == -1) {
3539 error_report("unknown -watchdog-action parameter");
3540 exit(1);
3541 }
3542 break;
3543 case QEMU_OPTION_virtiocon:
3544 add_device_config(DEV_VIRTCON, optarg);
3545 default_virtcon = 0;
3546 if (strncmp(optarg, "mon:", 4) == 0) {
3547 default_monitor = 0;
3548 }
3549 break;
3550 case QEMU_OPTION_parallel:
3551 add_device_config(DEV_PARALLEL, optarg);
3552 default_parallel = 0;
3553 if (strncmp(optarg, "mon:", 4) == 0) {
3554 default_monitor = 0;
3555 }
3556 break;
3557 case QEMU_OPTION_debugcon:
3558 add_device_config(DEV_DEBUGCON, optarg);
3559 break;
3560 case QEMU_OPTION_loadvm:
3561 loadvm = optarg;
3562 break;
3563 case QEMU_OPTION_full_screen:
3564 full_screen = 1;
3565 break;
3566 case QEMU_OPTION_no_frame:
3567 no_frame = 1;
3568 break;
3569 case QEMU_OPTION_alt_grab:
3570 alt_grab = 1;
3571 break;
3572 case QEMU_OPTION_ctrl_grab:
3573 ctrl_grab = 1;
3574 break;
3575 case QEMU_OPTION_no_quit:
3576 no_quit = 1;
3577 break;
3578 case QEMU_OPTION_sdl:
3579 #ifdef CONFIG_SDL
3580 display_type = DT_SDL;
3581 break;
3582 #else
3583 error_report("SDL support is disabled");
3584 exit(1);
3585 #endif
3586 case QEMU_OPTION_pidfile:
3587 pid_file = optarg;
3588 break;
3589 case QEMU_OPTION_win2k_hack:
3590 win2k_install_hack = 1;
3591 break;
3592 case QEMU_OPTION_rtc_td_hack: {
3593 static GlobalProperty slew_lost_ticks = {
3594 .driver = "mc146818rtc",
3595 .property = "lost_tick_policy",
3596 .value = "slew",
3597 };
3598
3599 qdev_prop_register_global(&slew_lost_ticks);
3600 break;
3601 }
3602 case QEMU_OPTION_acpitable:
3603 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3604 optarg, true);
3605 if (!opts) {
3606 exit(1);
3607 }
3608 acpi_table_add(opts, &error_fatal);
3609 break;
3610 case QEMU_OPTION_smbios:
3611 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3612 optarg, false);
3613 if (!opts) {
3614 exit(1);
3615 }
3616 smbios_entry_add(opts, &error_fatal);
3617 break;
3618 case QEMU_OPTION_fwcfg:
3619 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3620 optarg, true);
3621 if (opts == NULL) {
3622 exit(1);
3623 }
3624 break;
3625 case QEMU_OPTION_enable_kvm:
3626 olist = qemu_find_opts("machine");
3627 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3628 break;
3629 case QEMU_OPTION_enable_hax:
3630 olist = qemu_find_opts("machine");
3631 qemu_opts_parse_noisily(olist, "accel=hax", false);
3632 break;
3633 case QEMU_OPTION_M:
3634 case QEMU_OPTION_machine:
3635 olist = qemu_find_opts("machine");
3636 opts = qemu_opts_parse_noisily(olist, optarg, true);
3637 if (!opts) {
3638 exit(1);
3639 }
3640 break;
3641 case QEMU_OPTION_no_kvm:
3642 olist = qemu_find_opts("machine");
3643 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3644 break;
3645 case QEMU_OPTION_no_kvm_pit: {
3646 error_report("warning: ignoring deprecated option");
3647 break;
3648 }
3649 case QEMU_OPTION_no_kvm_pit_reinjection: {
3650 static GlobalProperty kvm_pit_lost_tick_policy = {
3651 .driver = "kvm-pit",
3652 .property = "lost_tick_policy",
3653 .value = "discard",
3654 };
3655
3656 error_report("warning: deprecated, replaced by "
3657 "-global kvm-pit.lost_tick_policy=discard");
3658 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3659 break;
3660 }
3661 case QEMU_OPTION_usb:
3662 olist = qemu_find_opts("machine");
3663 qemu_opts_parse_noisily(olist, "usb=on", false);
3664 break;
3665 case QEMU_OPTION_usbdevice:
3666 olist = qemu_find_opts("machine");
3667 qemu_opts_parse_noisily(olist, "usb=on", false);
3668 add_device_config(DEV_USB, optarg);
3669 break;
3670 case QEMU_OPTION_device:
3671 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3672 optarg, true)) {
3673 exit(1);
3674 }
3675 break;
3676 case QEMU_OPTION_smp:
3677 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3678 optarg, true)) {
3679 exit(1);
3680 }
3681 break;
3682 case QEMU_OPTION_vnc:
3683 vnc_parse(optarg, &error_fatal);
3684 break;
3685 case QEMU_OPTION_no_acpi:
3686 acpi_enabled = 0;
3687 break;
3688 case QEMU_OPTION_no_hpet:
3689 no_hpet = 1;
3690 break;
3691 case QEMU_OPTION_balloon:
3692 if (balloon_parse(optarg) < 0) {
3693 error_report("unknown -balloon argument %s", optarg);
3694 exit(1);
3695 }
3696 break;
3697 case QEMU_OPTION_no_reboot:
3698 no_reboot = 1;
3699 break;
3700 case QEMU_OPTION_no_shutdown:
3701 no_shutdown = 1;
3702 break;
3703 case QEMU_OPTION_show_cursor:
3704 cursor_hide = 0;
3705 break;
3706 case QEMU_OPTION_uuid:
3707 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3708 error_report("failed to parse UUID string: wrong format");
3709 exit(1);
3710 }
3711 qemu_uuid_set = true;
3712 break;
3713 case QEMU_OPTION_option_rom:
3714 if (nb_option_roms >= MAX_OPTION_ROMS) {
3715 error_report("too many option ROMs");
3716 exit(1);
3717 }
3718 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3719 optarg, true);
3720 if (!opts) {
3721 exit(1);
3722 }
3723 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3724 option_rom[nb_option_roms].bootindex =
3725 qemu_opt_get_number(opts, "bootindex", -1);
3726 if (!option_rom[nb_option_roms].name) {
3727 error_report("Option ROM file is not specified");
3728 exit(1);
3729 }
3730 nb_option_roms++;
3731 break;
3732 case QEMU_OPTION_semihosting:
3733 semihosting.enabled = true;
3734 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3735 break;
3736 case QEMU_OPTION_semihosting_config:
3737 semihosting.enabled = true;
3738 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3739 optarg, false);
3740 if (opts != NULL) {
3741 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3742 true);
3743 const char *target = qemu_opt_get(opts, "target");
3744 if (target != NULL) {
3745 if (strcmp("native", target) == 0) {
3746 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3747 } else if (strcmp("gdb", target) == 0) {
3748 semihosting.target = SEMIHOSTING_TARGET_GDB;
3749 } else if (strcmp("auto", target) == 0) {
3750 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3751 } else {
3752 error_report("unsupported semihosting-config %s",
3753 optarg);
3754 exit(1);
3755 }
3756 } else {
3757 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3758 }
3759 /* Set semihosting argument count and vector */
3760 qemu_opt_foreach(opts, add_semihosting_arg,
3761 &semihosting, NULL);
3762 } else {
3763 error_report("unsupported semihosting-config %s", optarg);
3764 exit(1);
3765 }
3766 break;
3767 case QEMU_OPTION_tdf:
3768 error_report("warning: ignoring deprecated option");
3769 break;
3770 case QEMU_OPTION_name:
3771 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3772 optarg, true);
3773 if (!opts) {
3774 exit(1);
3775 }
3776 break;
3777 case QEMU_OPTION_prom_env:
3778 if (nb_prom_envs >= MAX_PROM_ENVS) {
3779 error_report("too many prom variables");
3780 exit(1);
3781 }
3782 prom_envs[nb_prom_envs] = optarg;
3783 nb_prom_envs++;
3784 break;
3785 case QEMU_OPTION_old_param:
3786 old_param = 1;
3787 break;
3788 case QEMU_OPTION_clock:
3789 /* Clock options no longer exist. Keep this option for
3790 * backward compatibility.
3791 */
3792 break;
3793 case QEMU_OPTION_startdate:
3794 configure_rtc_date_offset(optarg, 1);
3795 break;
3796 case QEMU_OPTION_rtc:
3797 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3798 false);
3799 if (!opts) {
3800 exit(1);
3801 }
3802 configure_rtc(opts);
3803 break;
3804 case QEMU_OPTION_tb_size:
3805 tcg_tb_size = strtol(optarg, NULL, 0);
3806 if (tcg_tb_size < 0) {
3807 tcg_tb_size = 0;
3808 }
3809 break;
3810 case QEMU_OPTION_icount:
3811 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3812 optarg, true);
3813 if (!icount_opts) {
3814 exit(1);
3815 }
3816 break;
3817 case QEMU_OPTION_incoming:
3818 if (!incoming) {
3819 runstate_set(RUN_STATE_INMIGRATE);
3820 }
3821 incoming = optarg;
3822 break;
3823 case QEMU_OPTION_only_migratable:
3824 only_migratable = 1;
3825 break;
3826 case QEMU_OPTION_nodefaults:
3827 has_defaults = 0;
3828 break;
3829 case QEMU_OPTION_xen_domid:
3830 if (!(xen_available())) {
3831 error_report("Option not supported for this target");
3832 exit(1);
3833 }
3834 xen_domid = atoi(optarg);
3835 break;
3836 case QEMU_OPTION_xen_create:
3837 if (!(xen_available())) {
3838 error_report("Option not supported for this target");
3839 exit(1);
3840 }
3841 xen_mode = XEN_CREATE;
3842 break;
3843 case QEMU_OPTION_xen_attach:
3844 if (!(xen_available())) {
3845 error_report("Option not supported for this target");
3846 exit(1);
3847 }
3848 xen_mode = XEN_ATTACH;
3849 break;
3850 case QEMU_OPTION_trace:
3851 g_free(trace_file);
3852 trace_file = trace_opt_parse(optarg);
3853 break;
3854 case QEMU_OPTION_readconfig:
3855 {
3856 int ret = qemu_read_config_file(optarg);
3857 if (ret < 0) {
3858 error_report("read config %s: %s", optarg,
3859 strerror(-ret));
3860 exit(1);
3861 }
3862 break;
3863 }
3864 case QEMU_OPTION_spice:
3865 olist = qemu_find_opts("spice");
3866 if (!olist) {
3867 error_report("spice support is disabled");
3868 exit(1);
3869 }
3870 opts = qemu_opts_parse_noisily(olist, optarg, false);
3871 if (!opts) {
3872 exit(1);
3873 }
3874 display_remote++;
3875 break;
3876 case QEMU_OPTION_writeconfig:
3877 {
3878 FILE *fp;
3879 if (strcmp(optarg, "-") == 0) {
3880 fp = stdout;
3881 } else {
3882 fp = fopen(optarg, "w");
3883 if (fp == NULL) {
3884 error_report("open %s: %s", optarg,
3885 strerror(errno));
3886 exit(1);
3887 }
3888 }
3889 qemu_config_write(fp);
3890 if (fp != stdout) {
3891 fclose(fp);
3892 }
3893 break;
3894 }
3895 case QEMU_OPTION_qtest:
3896 qtest_chrdev = optarg;
3897 break;
3898 case QEMU_OPTION_qtest_log:
3899 qtest_log = optarg;
3900 break;
3901 case QEMU_OPTION_sandbox:
3902 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3903 optarg, true);
3904 if (!opts) {
3905 exit(1);
3906 }
3907 break;
3908 case QEMU_OPTION_add_fd:
3909 #ifndef _WIN32
3910 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3911 optarg, false);
3912 if (!opts) {
3913 exit(1);
3914 }
3915 #else
3916 error_report("File descriptor passing is disabled on this "
3917 "platform");
3918 exit(1);
3919 #endif
3920 break;
3921 case QEMU_OPTION_object:
3922 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3923 optarg, true);
3924 if (!opts) {
3925 exit(1);
3926 }
3927 break;
3928 case QEMU_OPTION_realtime:
3929 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3930 optarg, false);
3931 if (!opts) {
3932 exit(1);
3933 }
3934 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3935 break;
3936 case QEMU_OPTION_msg:
3937 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3938 false);
3939 if (!opts) {
3940 exit(1);
3941 }
3942 configure_msg(opts);
3943 break;
3944 case QEMU_OPTION_dump_vmstate:
3945 if (vmstate_dump_file) {
3946 error_report("only one '-dump-vmstate' "
3947 "option may be given");
3948 exit(1);
3949 }
3950 vmstate_dump_file = fopen(optarg, "w");
3951 if (vmstate_dump_file == NULL) {
3952 error_report("open %s: %s", optarg, strerror(errno));
3953 exit(1);
3954 }
3955 break;
3956 default:
3957 os_parse_cmd_args(popt->index, optarg);
3958 }
3959 }
3960 }
3961 /*
3962 * Clear error location left behind by the loop.
3963 * Best done right after the loop. Do not insert code here!
3964 */
3965 loc_set_none();
3966
3967 replay_configure(icount_opts);
3968
3969 machine_class = select_machine();
3970
3971 set_memory_options(&ram_slots, &maxram_size, machine_class);
3972
3973 os_daemonize();
3974
3975 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3976 error_report("could not acquire pid file: %s", strerror(errno));
3977 exit(1);
3978 }
3979
3980 if (qemu_init_main_loop(&main_loop_err)) {
3981 error_report_err(main_loop_err);
3982 exit(1);
3983 }
3984
3985 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
3986 parse_sandbox, NULL, NULL)) {
3987 exit(1);
3988 }
3989
3990 if (qemu_opts_foreach(qemu_find_opts("name"),
3991 parse_name, NULL, NULL)) {
3992 exit(1);
3993 }
3994
3995 #ifndef _WIN32
3996 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
3997 parse_add_fd, NULL, NULL)) {
3998 exit(1);
3999 }
4000
4001 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4002 cleanup_add_fd, NULL, NULL)) {
4003 exit(1);
4004 }
4005 #endif
4006
4007 current_machine = MACHINE(object_new(object_class_get_name(
4008 OBJECT_CLASS(machine_class))));
4009 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4010 exit(0);
4011 }
4012 object_property_add_child(object_get_root(), "machine",
4013 OBJECT(current_machine), &error_abort);
4014
4015 if (machine_class->minimum_page_bits) {
4016 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4017 /* This would be a board error: specifying a minimum smaller than
4018 * a target's compile-time fixed setting.
4019 */
4020 g_assert_not_reached();
4021 }
4022 }
4023
4024 cpu_exec_init_all();
4025
4026 if (machine_class->hw_version) {
4027 qemu_set_hw_version(machine_class->hw_version);
4028 }
4029
4030 if (cpu_model && is_help_option(cpu_model)) {
4031 list_cpus(stdout, &fprintf, cpu_model);
4032 exit(0);
4033 }
4034
4035 if (!trace_init_backends()) {
4036 exit(1);
4037 }
4038 trace_init_file(trace_file);
4039
4040 /* Open the logfile at this point and set the log mask if necessary.
4041 */
4042 if (log_file) {
4043 qemu_set_log_filename(log_file, &error_fatal);
4044 }
4045
4046 if (log_mask) {
4047 int mask;
4048 mask = qemu_str_to_log_mask(log_mask);
4049 if (!mask) {
4050 qemu_print_log_usage(stdout);
4051 exit(1);
4052 }
4053 qemu_set_log(mask);
4054 } else {
4055 qemu_set_log(0);
4056 }
4057
4058 /* If no data_dir is specified then try to find it relative to the
4059 executable path. */
4060 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4061 data_dir[data_dir_idx] = os_find_datadir();
4062 if (data_dir[data_dir_idx] != NULL) {
4063 data_dir_idx++;
4064 }
4065 }
4066 /* If all else fails use the install path specified when building. */
4067 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4068 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4069 }
4070
4071 /* -L help lists the data directories and exits. */
4072 if (list_data_dirs) {
4073 for (i = 0; i < data_dir_idx; i++) {
4074 printf("%s\n", data_dir[i]);
4075 }
4076 exit(0);
4077 }
4078
4079 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4080
4081 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4082 if (max_cpus > machine_class->max_cpus) {
4083 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4084 "supported by machine '%s' (%d)", max_cpus,
4085 machine_class->name, machine_class->max_cpus);
4086 exit(1);
4087 }
4088
4089 /*
4090 * Get the default machine options from the machine if it is not already
4091 * specified either by the configuration file or by the command line.
4092 */
4093 if (machine_class->default_machine_opts) {
4094 qemu_opts_set_defaults(qemu_find_opts("machine"),
4095 machine_class->default_machine_opts, 0);
4096 }
4097
4098 qemu_opts_foreach(qemu_find_opts("device"),
4099 default_driver_check, NULL, NULL);
4100 qemu_opts_foreach(qemu_find_opts("global"),
4101 default_driver_check, NULL, NULL);
4102
4103 if (!vga_model && !default_vga) {
4104 vga_interface_type = VGA_DEVICE;
4105 }
4106 if (!has_defaults || machine_class->no_serial) {
4107 default_serial = 0;
4108 }
4109 if (!has_defaults || machine_class->no_parallel) {
4110 default_parallel = 0;
4111 }
4112 if (!has_defaults || !machine_class->use_virtcon) {
4113 default_virtcon = 0;
4114 }
4115 if (!has_defaults || !machine_class->use_sclp) {
4116 default_sclp = 0;
4117 }
4118 if (!has_defaults || machine_class->no_floppy) {
4119 default_floppy = 0;
4120 }
4121 if (!has_defaults || machine_class->no_cdrom) {
4122 default_cdrom = 0;
4123 }
4124 if (!has_defaults || machine_class->no_sdcard) {
4125 default_sdcard = 0;
4126 }
4127 if (!has_defaults) {
4128 default_monitor = 0;
4129 default_net = 0;
4130 default_vga = 0;
4131 }
4132
4133 if (is_daemonized()) {
4134 /* According to documentation and historically, -nographic redirects
4135 * serial port, parallel port and monitor to stdio, which does not work
4136 * with -daemonize. We can redirect these to null instead, but since
4137 * -nographic is legacy, let's just error out.
4138 * We disallow -nographic only if all other ports are not redirected
4139 * explicitly, to not break existing legacy setups which uses
4140 * -nographic _and_ redirects all ports explicitly - this is valid
4141 * usage, -nographic is just a no-op in this case.
4142 */
4143 if (nographic
4144 && (default_parallel || default_serial
4145 || default_monitor || default_virtcon)) {
4146 error_report("-nographic cannot be used with -daemonize");
4147 exit(1);
4148 }
4149 #ifdef CONFIG_CURSES
4150 if (display_type == DT_CURSES) {
4151 error_report("curses display cannot be used with -daemonize");
4152 exit(1);
4153 }
4154 #endif
4155 }
4156
4157 if (nographic) {
4158 if (default_parallel)
4159 add_device_config(DEV_PARALLEL, "null");
4160 if (default_serial && default_monitor) {
4161 add_device_config(DEV_SERIAL, "mon:stdio");
4162 } else if (default_virtcon && default_monitor) {
4163 add_device_config(DEV_VIRTCON, "mon:stdio");
4164 } else if (default_sclp && default_monitor) {
4165 add_device_config(DEV_SCLP, "mon:stdio");
4166 } else {
4167 if (default_serial)
4168 add_device_config(DEV_SERIAL, "stdio");
4169 if (default_virtcon)
4170 add_device_config(DEV_VIRTCON, "stdio");
4171 if (default_sclp) {
4172 add_device_config(DEV_SCLP, "stdio");
4173 }
4174 if (default_monitor)
4175 monitor_parse("stdio", "readline", false);
4176 }
4177 } else {
4178 if (default_serial)
4179 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4180 if (default_parallel)
4181 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4182 if (default_monitor)
4183 monitor_parse("vc:80Cx24C", "readline", false);
4184 if (default_virtcon)
4185 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4186 if (default_sclp) {
4187 add_device_config(DEV_SCLP, "vc:80Cx24C");
4188 }
4189 }
4190
4191 #if defined(CONFIG_VNC)
4192 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4193 display_remote++;
4194 }
4195 #endif
4196 if (display_type == DT_DEFAULT && !display_remote) {
4197 #if defined(CONFIG_GTK)
4198 display_type = DT_GTK;
4199 #elif defined(CONFIG_SDL)
4200 display_type = DT_SDL;
4201 #elif defined(CONFIG_COCOA)
4202 display_type = DT_COCOA;
4203 #elif defined(CONFIG_VNC)
4204 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4205 #else
4206 display_type = DT_NONE;
4207 #endif
4208 }
4209
4210 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4211 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4212 "for SDL, ignoring option");
4213 }
4214 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4215 error_report("-no-quit is only valid for GTK and SDL, "
4216 "ignoring option");
4217 }
4218
4219 if (display_type == DT_GTK) {
4220 early_gtk_display_init(request_opengl);
4221 }
4222
4223 if (display_type == DT_SDL) {
4224 sdl_display_early_init(request_opengl);
4225 }
4226
4227 qemu_console_early_init();
4228
4229 if (request_opengl == 1 && display_opengl == 0) {
4230 #if defined(CONFIG_OPENGL)
4231 error_report("OpenGL is not supported by the display");
4232 #else
4233 error_report("OpenGL support is disabled");
4234 #endif
4235 exit(1);
4236 }
4237
4238 page_size_init();
4239 socket_init();
4240
4241 if (qemu_opts_foreach(qemu_find_opts("object"),
4242 user_creatable_add_opts_foreach,
4243 object_create_initial, NULL)) {
4244 exit(1);
4245 }
4246
4247 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4248 chardev_init_func, NULL, NULL)) {
4249 exit(1);
4250 }
4251
4252 #ifdef CONFIG_VIRTFS
4253 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4254 fsdev_init_func, NULL, NULL)) {
4255 exit(1);
4256 }
4257 #endif
4258
4259 if (qemu_opts_foreach(qemu_find_opts("device"),
4260 device_help_func, NULL, NULL)) {
4261 exit(0);
4262 }
4263
4264 machine_opts = qemu_get_machine_opts();
4265 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4266 NULL)) {
4267 object_unref(OBJECT(current_machine));
4268 exit(1);
4269 }
4270
4271 configure_accelerator(current_machine);
4272
4273 if (qtest_chrdev) {
4274 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4275 }
4276
4277 machine_opts = qemu_get_machine_opts();
4278 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4279 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4280 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4281 bios_name = qemu_opt_get(machine_opts, "firmware");
4282
4283 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4284 if (opts) {
4285 boot_order = qemu_opt_get(opts, "order");
4286 if (boot_order) {
4287 validate_bootdevices(boot_order, &error_fatal);
4288 }
4289
4290 boot_once = qemu_opt_get(opts, "once");
4291 if (boot_once) {
4292 validate_bootdevices(boot_once, &error_fatal);
4293 }
4294
4295 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4296 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4297 }
4298
4299 if (!boot_order) {
4300 boot_order = machine_class->default_boot_order;
4301 }
4302
4303 if (!kernel_cmdline) {
4304 kernel_cmdline = "";
4305 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4306 }
4307
4308 linux_boot = (kernel_filename != NULL);
4309
4310 if (!linux_boot && *kernel_cmdline != '\0') {
4311 error_report("-append only allowed with -kernel option");
4312 exit(1);
4313 }
4314
4315 if (!linux_boot && initrd_filename != NULL) {
4316 error_report("-initrd only allowed with -kernel option");
4317 exit(1);
4318 }
4319
4320 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4321 /* fall back to the -kernel/-append */
4322 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4323 }
4324
4325 os_set_line_buffering();
4326
4327 /* spice needs the timers to be initialized by this point */
4328 qemu_spice_init();
4329
4330 cpu_ticks_init();
4331 if (icount_opts) {
4332 if (!tcg_enabled()) {
4333 error_report("-icount is not allowed with hardware virtualization");
4334 exit(1);
4335 }
4336 configure_icount(icount_opts, &error_abort);
4337 qemu_opts_del(icount_opts);
4338 }
4339
4340 if (default_net) {
4341 QemuOptsList *net = qemu_find_opts("net");
4342 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4343 #ifdef CONFIG_SLIRP
4344 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4345 #endif
4346 }
4347
4348 colo_info_init();
4349
4350 if (net_init_clients() < 0) {
4351 exit(1);
4352 }
4353
4354 if (qemu_opts_foreach(qemu_find_opts("object"),
4355 user_creatable_add_opts_foreach,
4356 object_create_delayed, NULL)) {
4357 exit(1);
4358 }
4359
4360 #ifdef CONFIG_TPM
4361 if (tpm_init() < 0) {
4362 exit(1);
4363 }
4364 #endif
4365
4366 /* init the bluetooth world */
4367 if (foreach_device_config(DEV_BT, bt_parse))
4368 exit(1);
4369
4370 if (!xen_enabled()) {
4371 /* On 32-bit hosts, QEMU is limited by virtual address space */
4372 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4373 error_report("at most 2047 MB RAM can be simulated");
4374 exit(1);
4375 }
4376 }
4377
4378 blk_mig_init();
4379 ram_mig_init();
4380
4381 /* If the currently selected machine wishes to override the units-per-bus
4382 * property of its default HBA interface type, do so now. */
4383 if (machine_class->units_per_default_bus) {
4384 override_max_devs(machine_class->block_default_type,
4385 machine_class->units_per_default_bus);
4386 }
4387
4388 /* open the virtual block devices */
4389 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4390 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4391 NULL, NULL);
4392 }
4393 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4394 &machine_class->block_default_type, NULL)) {
4395 exit(1);
4396 }
4397
4398 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4399 CDROM_OPTS);
4400 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4401 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4402
4403 parse_numa_opts(machine_class);
4404
4405 if (qemu_opts_foreach(qemu_find_opts("mon"),
4406 mon_init_func, NULL, NULL)) {
4407 exit(1);
4408 }
4409
4410 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4411 exit(1);
4412 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4413 exit(1);
4414 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4415 exit(1);
4416 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4417 exit(1);
4418 }
4419 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4420 exit(1);
4421
4422 /* If no default VGA is requested, the default is "none". */
4423 if (default_vga) {
4424 if (machine_class->default_display) {
4425 vga_model = machine_class->default_display;
4426 } else if (vga_interface_available(VGA_CIRRUS)) {
4427 vga_model = "cirrus";
4428 } else if (vga_interface_available(VGA_STD)) {
4429 vga_model = "std";
4430 }
4431 }
4432 if (vga_model) {
4433 select_vgahw(vga_model);
4434 }
4435
4436 if (watchdog) {
4437 i = select_watchdog(watchdog);
4438 if (i > 0)
4439 exit (i == 1 ? 1 : 0);
4440 }
4441
4442 machine_register_compat_props(current_machine);
4443
4444 qemu_opts_foreach(qemu_find_opts("global"),
4445 global_init_func, NULL, NULL);
4446
4447 /* This checkpoint is required by replay to separate prior clock
4448 reading from the other reads, because timer polling functions query
4449 clock values from the log. */
4450 replay_checkpoint(CHECKPOINT_INIT);
4451 qdev_machine_init();
4452
4453 current_machine->ram_size = ram_size;
4454 current_machine->maxram_size = maxram_size;
4455 current_machine->ram_slots = ram_slots;
4456 current_machine->boot_order = boot_order;
4457 current_machine->cpu_model = cpu_model;
4458
4459 machine_class->init(current_machine);
4460
4461 realtime_init();
4462
4463 audio_init();
4464
4465 cpu_synchronize_all_post_init();
4466
4467 if (hax_enabled()) {
4468 hax_sync_vcpus();
4469 }
4470
4471 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4472 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4473 exit(1);
4474 }
4475
4476 /* init USB devices */
4477 if (machine_usb(current_machine)) {
4478 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4479 exit(1);
4480 }
4481
4482 /* Check if IGD GFX passthrough. */
4483 igd_gfx_passthru();
4484
4485 /* init generic devices */
4486 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4487 if (qemu_opts_foreach(qemu_find_opts("device"),
4488 device_init_func, NULL, NULL)) {
4489 exit(1);
4490 }
4491
4492 numa_post_machine_init();
4493
4494 rom_reset_order_override();
4495
4496 /* Did we create any drives that we failed to create a device for? */
4497 drive_check_orphaned();
4498
4499 /* Don't warn about the default network setup that you get if
4500 * no command line -net or -netdev options are specified. There
4501 * are two cases that we would otherwise complain about:
4502 * (1) board doesn't support a NIC but the implicit "-net nic"
4503 * requested one
4504 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4505 * sets up a nic that isn't connected to anything.
4506 */
4507 if (!default_net) {
4508 net_check_clients();
4509 }
4510
4511
4512 if (boot_once) {
4513 qemu_boot_set(boot_once, &error_fatal);
4514 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4515 }
4516
4517 ds = init_displaystate();
4518
4519 /* init local displays */
4520 switch (display_type) {
4521 case DT_CURSES:
4522 curses_display_init(ds, full_screen);
4523 break;
4524 case DT_SDL:
4525 sdl_display_init(ds, full_screen, no_frame);
4526 break;
4527 case DT_COCOA:
4528 cocoa_display_init(ds, full_screen);
4529 break;
4530 case DT_GTK:
4531 gtk_display_init(ds, full_screen, grab_on_hover);
4532 break;
4533 default:
4534 break;
4535 }
4536
4537 /* must be after terminal init, SDL library changes signal handlers */
4538 os_setup_signal_handling();
4539
4540 /* init remote displays */
4541 #ifdef CONFIG_VNC
4542 qemu_opts_foreach(qemu_find_opts("vnc"),
4543 vnc_init_func, NULL, NULL);
4544 #endif
4545
4546 if (using_spice) {
4547 qemu_spice_display_init();
4548 }
4549
4550 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4551 exit(1);
4552 }
4553
4554 qdev_machine_creation_done();
4555
4556 /* TODO: once all bus devices are qdevified, this should be done
4557 * when bus is created by qdev.c */
4558 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4559 qemu_run_machine_init_done_notifiers();
4560
4561 if (rom_check_and_register_reset() != 0) {
4562 error_report("rom check and register reset failed");
4563 exit(1);
4564 }
4565
4566 replay_start();
4567
4568 /* This checkpoint is required by replay to separate prior clock
4569 reading from the other reads, because timer polling functions query
4570 clock values from the log. */
4571 replay_checkpoint(CHECKPOINT_RESET);
4572 qemu_system_reset(VMRESET_SILENT);
4573 register_global_state();
4574 if (replay_mode != REPLAY_MODE_NONE) {
4575 replay_vmstate_init();
4576 } else if (loadvm) {
4577 if (load_vmstate(loadvm) < 0) {
4578 autostart = 0;
4579 }
4580 }
4581
4582 qdev_prop_check_globals();
4583 if (vmstate_dump_file) {
4584 /* dump and exit */
4585 dump_vmstate_json_to_file(vmstate_dump_file);
4586 return 0;
4587 }
4588
4589 if (incoming) {
4590 Error *local_err = NULL;
4591 qemu_start_incoming_migration(incoming, &local_err);
4592 if (local_err) {
4593 error_reportf_err(local_err, "-incoming %s: ", incoming);
4594 exit(1);
4595 }
4596 } else if (autostart) {
4597 vm_start();
4598 }
4599
4600 os_setup_post();
4601
4602 main_loop();
4603 replay_disable_events();
4604 iothread_stop_all();
4605
4606 bdrv_close_all();
4607 pause_all_vcpus();
4608 res_free();
4609
4610 /* vhost-user must be cleaned up before chardevs. */
4611 net_cleanup();
4612 audio_cleanup();
4613 monitor_cleanup();
4614 qemu_chr_cleanup();
4615
4616 return 0;
4617 }