slirp: VMStatify sbuf
[qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
29
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
33
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
37
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
43 {
44 return qemu_main(argc, argv, NULL);
45 }
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
50
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
55
56
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/scsi/scsi.h"
66 #include "hw/bt.h"
67 #include "sysemu/watchdog.h"
68 #include "hw/smbios/smbios.h"
69 #include "hw/acpi/acpi.h"
70 #include "hw/xen/xen.h"
71 #include "hw/qdev.h"
72 #include "hw/loader.h"
73 #include "monitor/qdev.h"
74 #include "sysemu/bt.h"
75 #include "net/net.h"
76 #include "net/slirp.h"
77 #include "monitor/monitor.h"
78 #include "ui/console.h"
79 #include "ui/input.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "qemu/log.h"
87 #include "sysemu/blockdev.h"
88 #include "hw/block/block.h"
89 #include "migration/block.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "audio/audio.h"
93 #include "migration/migration.h"
94 #include "sysemu/cpus.h"
95 #include "migration/colo.h"
96 #include "sysemu/kvm.h"
97 #include "sysemu/hax.h"
98 #include "qapi/qobject-input-visitor.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qapi-visit.h"
101 #include "qapi/qmp/qjson.h"
102 #include "qemu/option.h"
103 #include "qemu/config-file.h"
104 #include "qemu-options.h"
105 #include "qmp-commands.h"
106 #include "qemu/main-loop.h"
107 #ifdef CONFIG_VIRTFS
108 #include "fsdev/qemu-fsdev.h"
109 #endif
110 #include "sysemu/qtest.h"
111
112 #include "disas/disas.h"
113
114
115 #include "slirp/libslirp.h"
116
117 #include "trace-root.h"
118 #include "trace/control.h"
119 #include "qemu/queue.h"
120 #include "sysemu/arch_init.h"
121
122 #include "ui/qemu-spice.h"
123 #include "qapi/string-input-visitor.h"
124 #include "qapi/opts-visitor.h"
125 #include "qom/object_interfaces.h"
126 #include "qapi-event.h"
127 #include "exec/semihost.h"
128 #include "crypto/init.h"
129 #include "sysemu/replay.h"
130 #include "qapi/qmp/qerror.h"
131 #include "sysemu/iothread.h"
132
133 #define MAX_VIRTIO_CONSOLES 1
134 #define MAX_SCLP_CONSOLES 1
135
136 static const char *data_dir[16];
137 static int data_dir_idx;
138 const char *bios_name = NULL;
139 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
140 int request_opengl = -1;
141 int display_opengl;
142 const char* keyboard_layout = NULL;
143 ram_addr_t ram_size;
144 const char *mem_path = NULL;
145 int mem_prealloc = 0; /* force preallocation of physical target memory */
146 bool enable_mlock = false;
147 int nb_nics;
148 NICInfo nd_table[MAX_NICS];
149 int autostart;
150 static int rtc_utc = 1;
151 static int rtc_date_offset = -1; /* -1 means no change */
152 QEMUClockType rtc_clock;
153 int vga_interface_type = VGA_NONE;
154 static int full_screen = 0;
155 static int no_frame = 0;
156 int no_quit = 0;
157 static bool grab_on_hover;
158 Chardev *serial_hds[MAX_SERIAL_PORTS];
159 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
160 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
161 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
162 int win2k_install_hack = 0;
163 int singlestep = 0;
164 int smp_cpus = 1;
165 int max_cpus = 1;
166 int smp_cores = 1;
167 int smp_threads = 1;
168 int acpi_enabled = 1;
169 int no_hpet = 0;
170 int fd_bootchk = 1;
171 static int no_reboot;
172 int no_shutdown = 0;
173 int cursor_hide = 1;
174 int graphic_rotate = 0;
175 const char *watchdog;
176 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
177 int nb_option_roms;
178 int old_param = 0;
179 const char *qemu_name;
180 int alt_grab = 0;
181 int ctrl_grab = 0;
182 unsigned int nb_prom_envs = 0;
183 const char *prom_envs[MAX_PROM_ENVS];
184 int boot_menu;
185 bool boot_strict;
186 uint8_t *boot_splash_filedata;
187 size_t boot_splash_filedata_size;
188 uint8_t qemu_extra_params_fw[2];
189 int only_migratable; /* turn it off unless user states otherwise */
190
191 int icount_align_option;
192
193 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
195 */
196 QemuUUID qemu_uuid;
197 bool qemu_uuid_set;
198
199 static NotifierList exit_notifiers =
200 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
201
202 static NotifierList machine_init_done_notifiers =
203 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
204
205 bool xen_allowed;
206 uint32_t xen_domid;
207 enum xen_mode xen_mode = XEN_EMULATE;
208 bool xen_domid_restrict;
209
210 static int has_defaults = 1;
211 static int default_serial = 1;
212 static int default_parallel = 1;
213 static int default_virtcon = 1;
214 static int default_sclp = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
220 static int default_net = 1;
221
222 static struct {
223 const char *driver;
224 int *flag;
225 } default_list[] = {
226 { .driver = "isa-serial", .flag = &default_serial },
227 { .driver = "isa-parallel", .flag = &default_parallel },
228 { .driver = "isa-fdc", .flag = &default_floppy },
229 { .driver = "floppy", .flag = &default_floppy },
230 { .driver = "ide-cd", .flag = &default_cdrom },
231 { .driver = "ide-hd", .flag = &default_cdrom },
232 { .driver = "ide-drive", .flag = &default_cdrom },
233 { .driver = "scsi-cd", .flag = &default_cdrom },
234 { .driver = "scsi-hd", .flag = &default_cdrom },
235 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
236 { .driver = "virtio-serial", .flag = &default_virtcon },
237 { .driver = "VGA", .flag = &default_vga },
238 { .driver = "isa-vga", .flag = &default_vga },
239 { .driver = "cirrus-vga", .flag = &default_vga },
240 { .driver = "isa-cirrus-vga", .flag = &default_vga },
241 { .driver = "vmware-svga", .flag = &default_vga },
242 { .driver = "qxl-vga", .flag = &default_vga },
243 { .driver = "virtio-vga", .flag = &default_vga },
244 };
245
246 static QemuOptsList qemu_rtc_opts = {
247 .name = "rtc",
248 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
249 .desc = {
250 {
251 .name = "base",
252 .type = QEMU_OPT_STRING,
253 },{
254 .name = "clock",
255 .type = QEMU_OPT_STRING,
256 },{
257 .name = "driftfix",
258 .type = QEMU_OPT_STRING,
259 },
260 { /* end of list */ }
261 },
262 };
263
264 static QemuOptsList qemu_sandbox_opts = {
265 .name = "sandbox",
266 .implied_opt_name = "enable",
267 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
268 .desc = {
269 {
270 .name = "enable",
271 .type = QEMU_OPT_BOOL,
272 },
273 { /* end of list */ }
274 },
275 };
276
277 static QemuOptsList qemu_option_rom_opts = {
278 .name = "option-rom",
279 .implied_opt_name = "romfile",
280 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
281 .desc = {
282 {
283 .name = "bootindex",
284 .type = QEMU_OPT_NUMBER,
285 }, {
286 .name = "romfile",
287 .type = QEMU_OPT_STRING,
288 },
289 { /* end of list */ }
290 },
291 };
292
293 static QemuOptsList qemu_machine_opts = {
294 .name = "machine",
295 .implied_opt_name = "type",
296 .merge_lists = true,
297 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
298 .desc = {
299 /*
300 * no elements => accept any
301 * sanity checking will happen later
302 * when setting machine properties
303 */
304 { }
305 },
306 };
307
308 static QemuOptsList qemu_accel_opts = {
309 .name = "accel",
310 .implied_opt_name = "accel",
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
312 .merge_lists = true,
313 .desc = {
314 {
315 .name = "accel",
316 .type = QEMU_OPT_STRING,
317 .help = "Select the type of accelerator",
318 },
319 {
320 .name = "thread",
321 .type = QEMU_OPT_STRING,
322 .help = "Enable/disable multi-threaded TCG",
323 },
324 { /* end of list */ }
325 },
326 };
327
328 static QemuOptsList qemu_boot_opts = {
329 .name = "boot-opts",
330 .implied_opt_name = "order",
331 .merge_lists = true,
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
333 .desc = {
334 {
335 .name = "order",
336 .type = QEMU_OPT_STRING,
337 }, {
338 .name = "once",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "menu",
342 .type = QEMU_OPT_BOOL,
343 }, {
344 .name = "splash",
345 .type = QEMU_OPT_STRING,
346 }, {
347 .name = "splash-time",
348 .type = QEMU_OPT_STRING,
349 }, {
350 .name = "reboot-timeout",
351 .type = QEMU_OPT_STRING,
352 }, {
353 .name = "strict",
354 .type = QEMU_OPT_BOOL,
355 },
356 { /*End of list */ }
357 },
358 };
359
360 static QemuOptsList qemu_add_fd_opts = {
361 .name = "add-fd",
362 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
363 .desc = {
364 {
365 .name = "fd",
366 .type = QEMU_OPT_NUMBER,
367 .help = "file descriptor of which a duplicate is added to fd set",
368 },{
369 .name = "set",
370 .type = QEMU_OPT_NUMBER,
371 .help = "ID of the fd set to add fd to",
372 },{
373 .name = "opaque",
374 .type = QEMU_OPT_STRING,
375 .help = "free-form string used to describe fd",
376 },
377 { /* end of list */ }
378 },
379 };
380
381 static QemuOptsList qemu_object_opts = {
382 .name = "object",
383 .implied_opt_name = "qom-type",
384 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
385 .desc = {
386 { }
387 },
388 };
389
390 static QemuOptsList qemu_tpmdev_opts = {
391 .name = "tpmdev",
392 .implied_opt_name = "type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
394 .desc = {
395 /* options are defined in the TPM backends */
396 { /* end of list */ }
397 },
398 };
399
400 static QemuOptsList qemu_realtime_opts = {
401 .name = "realtime",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
403 .desc = {
404 {
405 .name = "mlock",
406 .type = QEMU_OPT_BOOL,
407 },
408 { /* end of list */ }
409 },
410 };
411
412 static QemuOptsList qemu_msg_opts = {
413 .name = "msg",
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
415 .desc = {
416 {
417 .name = "timestamp",
418 .type = QEMU_OPT_BOOL,
419 },
420 { /* end of list */ }
421 },
422 };
423
424 static QemuOptsList qemu_name_opts = {
425 .name = "name",
426 .implied_opt_name = "guest",
427 .merge_lists = true,
428 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
429 .desc = {
430 {
431 .name = "guest",
432 .type = QEMU_OPT_STRING,
433 .help = "Sets the name of the guest.\n"
434 "This name will be displayed in the SDL window caption.\n"
435 "The name will also be used for the VNC server",
436 }, {
437 .name = "process",
438 .type = QEMU_OPT_STRING,
439 .help = "Sets the name of the QEMU process, as shown in top etc",
440 }, {
441 .name = "debug-threads",
442 .type = QEMU_OPT_BOOL,
443 .help = "When enabled, name the individual threads; defaults off.\n"
444 "NOTE: The thread names are for debugging and not a\n"
445 "stable API.",
446 },
447 { /* End of list */ }
448 },
449 };
450
451 static QemuOptsList qemu_mem_opts = {
452 .name = "memory",
453 .implied_opt_name = "size",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
455 .merge_lists = true,
456 .desc = {
457 {
458 .name = "size",
459 .type = QEMU_OPT_SIZE,
460 },
461 {
462 .name = "slots",
463 .type = QEMU_OPT_NUMBER,
464 },
465 {
466 .name = "maxmem",
467 .type = QEMU_OPT_SIZE,
468 },
469 { /* end of list */ }
470 },
471 };
472
473 static QemuOptsList qemu_icount_opts = {
474 .name = "icount",
475 .implied_opt_name = "shift",
476 .merge_lists = true,
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
478 .desc = {
479 {
480 .name = "shift",
481 .type = QEMU_OPT_STRING,
482 }, {
483 .name = "align",
484 .type = QEMU_OPT_BOOL,
485 }, {
486 .name = "sleep",
487 .type = QEMU_OPT_BOOL,
488 }, {
489 .name = "rr",
490 .type = QEMU_OPT_STRING,
491 }, {
492 .name = "rrfile",
493 .type = QEMU_OPT_STRING,
494 }, {
495 .name = "rrsnapshot",
496 .type = QEMU_OPT_STRING,
497 },
498 { /* end of list */ }
499 },
500 };
501
502 static QemuOptsList qemu_semihosting_config_opts = {
503 .name = "semihosting-config",
504 .implied_opt_name = "enable",
505 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
506 .desc = {
507 {
508 .name = "enable",
509 .type = QEMU_OPT_BOOL,
510 }, {
511 .name = "target",
512 .type = QEMU_OPT_STRING,
513 }, {
514 .name = "arg",
515 .type = QEMU_OPT_STRING,
516 },
517 { /* end of list */ }
518 },
519 };
520
521 static QemuOptsList qemu_fw_cfg_opts = {
522 .name = "fw_cfg",
523 .implied_opt_name = "name",
524 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
525 .desc = {
526 {
527 .name = "name",
528 .type = QEMU_OPT_STRING,
529 .help = "Sets the fw_cfg name of the blob to be inserted",
530 }, {
531 .name = "file",
532 .type = QEMU_OPT_STRING,
533 .help = "Sets the name of the file from which\n"
534 "the fw_cfg blob will be loaded",
535 }, {
536 .name = "string",
537 .type = QEMU_OPT_STRING,
538 .help = "Sets content of the blob to be inserted from a string",
539 },
540 { /* end of list */ }
541 },
542 };
543
544 /**
545 * Get machine options
546 *
547 * Returns: machine options (never null).
548 */
549 QemuOpts *qemu_get_machine_opts(void)
550 {
551 return qemu_find_opts_singleton("machine");
552 }
553
554 const char *qemu_get_vm_name(void)
555 {
556 return qemu_name;
557 }
558
559 static void res_free(void)
560 {
561 g_free(boot_splash_filedata);
562 boot_splash_filedata = NULL;
563 }
564
565 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
566 {
567 const char *driver = qemu_opt_get(opts, "driver");
568 int i;
569
570 if (!driver)
571 return 0;
572 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
573 if (strcmp(default_list[i].driver, driver) != 0)
574 continue;
575 *(default_list[i].flag) = 0;
576 }
577 return 0;
578 }
579
580 /***********************************************************/
581 /* QEMU state */
582
583 static RunState current_run_state = RUN_STATE_PRELAUNCH;
584
585 /* We use RUN_STATE__MAX but any invalid value will do */
586 static RunState vmstop_requested = RUN_STATE__MAX;
587 static QemuMutex vmstop_lock;
588
589 typedef struct {
590 RunState from;
591 RunState to;
592 } RunStateTransition;
593
594 static const RunStateTransition runstate_transitions_def[] = {
595 /* from -> to */
596 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
597 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
599
600 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
601 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
602 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
603 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
604 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
605 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
606 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
607 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
608 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
610 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
611 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
612
613 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
614 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
616
617 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
618 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
620
621 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
622 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_PAUSED, RUN_STATE_COLO},
625
626 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
629
630 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
633
634 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
635 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
638
639 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
640 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
641
642 { RUN_STATE_COLO, RUN_STATE_RUNNING },
643
644 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
645 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
646 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
648 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
650 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
652 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
653 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
654 { RUN_STATE_RUNNING, RUN_STATE_COLO},
655
656 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
657
658 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
659 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
661
662 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
663 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
664 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
665 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
666 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
668
669 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
670 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
672 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
673
674 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
677
678 { RUN_STATE__MAX, RUN_STATE__MAX },
679 };
680
681 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
682
683 bool runstate_check(RunState state)
684 {
685 return current_run_state == state;
686 }
687
688 bool runstate_store(char *str, size_t size)
689 {
690 const char *state = RunState_lookup[current_run_state];
691 size_t len = strlen(state) + 1;
692
693 if (len > size) {
694 return false;
695 }
696 memcpy(str, state, len);
697 return true;
698 }
699
700 static void runstate_init(void)
701 {
702 const RunStateTransition *p;
703
704 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
705 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
706 runstate_valid_transitions[p->from][p->to] = true;
707 }
708
709 qemu_mutex_init(&vmstop_lock);
710 }
711
712 /* This function will abort() on invalid state transitions */
713 void runstate_set(RunState new_state)
714 {
715 assert(new_state < RUN_STATE__MAX);
716
717 if (current_run_state == new_state) {
718 return;
719 }
720
721 if (!runstate_valid_transitions[current_run_state][new_state]) {
722 error_report("invalid runstate transition: '%s' -> '%s'",
723 RunState_lookup[current_run_state],
724 RunState_lookup[new_state]);
725 abort();
726 }
727 trace_runstate_set(new_state);
728 current_run_state = new_state;
729 }
730
731 int runstate_is_running(void)
732 {
733 return runstate_check(RUN_STATE_RUNNING);
734 }
735
736 bool runstate_needs_reset(void)
737 {
738 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
739 runstate_check(RUN_STATE_SHUTDOWN);
740 }
741
742 StatusInfo *qmp_query_status(Error **errp)
743 {
744 StatusInfo *info = g_malloc0(sizeof(*info));
745
746 info->running = runstate_is_running();
747 info->singlestep = singlestep;
748 info->status = current_run_state;
749
750 return info;
751 }
752
753 bool qemu_vmstop_requested(RunState *r)
754 {
755 qemu_mutex_lock(&vmstop_lock);
756 *r = vmstop_requested;
757 vmstop_requested = RUN_STATE__MAX;
758 qemu_mutex_unlock(&vmstop_lock);
759 return *r < RUN_STATE__MAX;
760 }
761
762 void qemu_system_vmstop_request_prepare(void)
763 {
764 qemu_mutex_lock(&vmstop_lock);
765 }
766
767 void qemu_system_vmstop_request(RunState state)
768 {
769 vmstop_requested = state;
770 qemu_mutex_unlock(&vmstop_lock);
771 qemu_notify_event();
772 }
773
774 /***********************************************************/
775 /* real time host monotonic timer */
776
777 static time_t qemu_time(void)
778 {
779 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
780 }
781
782 /***********************************************************/
783 /* host time/date access */
784 void qemu_get_timedate(struct tm *tm, int offset)
785 {
786 time_t ti = qemu_time();
787
788 ti += offset;
789 if (rtc_date_offset == -1) {
790 if (rtc_utc)
791 gmtime_r(&ti, tm);
792 else
793 localtime_r(&ti, tm);
794 } else {
795 ti -= rtc_date_offset;
796 gmtime_r(&ti, tm);
797 }
798 }
799
800 int qemu_timedate_diff(struct tm *tm)
801 {
802 time_t seconds;
803
804 if (rtc_date_offset == -1)
805 if (rtc_utc)
806 seconds = mktimegm(tm);
807 else {
808 struct tm tmp = *tm;
809 tmp.tm_isdst = -1; /* use timezone to figure it out */
810 seconds = mktime(&tmp);
811 }
812 else
813 seconds = mktimegm(tm) + rtc_date_offset;
814
815 return seconds - qemu_time();
816 }
817
818 static void configure_rtc_date_offset(const char *startdate, int legacy)
819 {
820 time_t rtc_start_date;
821 struct tm tm;
822
823 if (!strcmp(startdate, "now") && legacy) {
824 rtc_date_offset = -1;
825 } else {
826 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
827 &tm.tm_year,
828 &tm.tm_mon,
829 &tm.tm_mday,
830 &tm.tm_hour,
831 &tm.tm_min,
832 &tm.tm_sec) == 6) {
833 /* OK */
834 } else if (sscanf(startdate, "%d-%d-%d",
835 &tm.tm_year,
836 &tm.tm_mon,
837 &tm.tm_mday) == 3) {
838 tm.tm_hour = 0;
839 tm.tm_min = 0;
840 tm.tm_sec = 0;
841 } else {
842 goto date_fail;
843 }
844 tm.tm_year -= 1900;
845 tm.tm_mon--;
846 rtc_start_date = mktimegm(&tm);
847 if (rtc_start_date == -1) {
848 date_fail:
849 error_report("invalid date format");
850 error_printf("valid formats: "
851 "'2006-06-17T16:01:21' or '2006-06-17'\n");
852 exit(1);
853 }
854 rtc_date_offset = qemu_time() - rtc_start_date;
855 }
856 }
857
858 static void configure_rtc(QemuOpts *opts)
859 {
860 const char *value;
861
862 value = qemu_opt_get(opts, "base");
863 if (value) {
864 if (!strcmp(value, "utc")) {
865 rtc_utc = 1;
866 } else if (!strcmp(value, "localtime")) {
867 Error *blocker = NULL;
868 rtc_utc = 0;
869 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
870 "-rtc base=localtime");
871 replay_add_blocker(blocker);
872 } else {
873 configure_rtc_date_offset(value, 0);
874 }
875 }
876 value = qemu_opt_get(opts, "clock");
877 if (value) {
878 if (!strcmp(value, "host")) {
879 rtc_clock = QEMU_CLOCK_HOST;
880 } else if (!strcmp(value, "rt")) {
881 rtc_clock = QEMU_CLOCK_REALTIME;
882 } else if (!strcmp(value, "vm")) {
883 rtc_clock = QEMU_CLOCK_VIRTUAL;
884 } else {
885 error_report("invalid option value '%s'", value);
886 exit(1);
887 }
888 }
889 value = qemu_opt_get(opts, "driftfix");
890 if (value) {
891 if (!strcmp(value, "slew")) {
892 static GlobalProperty slew_lost_ticks = {
893 .driver = "mc146818rtc",
894 .property = "lost_tick_policy",
895 .value = "slew",
896 };
897
898 qdev_prop_register_global(&slew_lost_ticks);
899 } else if (!strcmp(value, "none")) {
900 /* discard is default */
901 } else {
902 error_report("invalid option value '%s'", value);
903 exit(1);
904 }
905 }
906 }
907
908 /***********************************************************/
909 /* Bluetooth support */
910 static int nb_hcis;
911 static int cur_hci;
912 static struct HCIInfo *hci_table[MAX_NICS];
913
914 struct HCIInfo *qemu_next_hci(void)
915 {
916 if (cur_hci == nb_hcis)
917 return &null_hci;
918
919 return hci_table[cur_hci++];
920 }
921
922 static int bt_hci_parse(const char *str)
923 {
924 struct HCIInfo *hci;
925 bdaddr_t bdaddr;
926
927 if (nb_hcis >= MAX_NICS) {
928 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
929 return -1;
930 }
931
932 hci = hci_init(str);
933 if (!hci)
934 return -1;
935
936 bdaddr.b[0] = 0x52;
937 bdaddr.b[1] = 0x54;
938 bdaddr.b[2] = 0x00;
939 bdaddr.b[3] = 0x12;
940 bdaddr.b[4] = 0x34;
941 bdaddr.b[5] = 0x56 + nb_hcis;
942 hci->bdaddr_set(hci, bdaddr.b);
943
944 hci_table[nb_hcis++] = hci;
945
946 return 0;
947 }
948
949 static void bt_vhci_add(int vlan_id)
950 {
951 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
952
953 if (!vlan->slave)
954 error_report("warning: adding a VHCI to an empty scatternet %i",
955 vlan_id);
956
957 bt_vhci_init(bt_new_hci(vlan));
958 }
959
960 static struct bt_device_s *bt_device_add(const char *opt)
961 {
962 struct bt_scatternet_s *vlan;
963 int vlan_id = 0;
964 char *endp = strstr(opt, ",vlan=");
965 int len = (endp ? endp - opt : strlen(opt)) + 1;
966 char devname[10];
967
968 pstrcpy(devname, MIN(sizeof(devname), len), opt);
969
970 if (endp) {
971 vlan_id = strtol(endp + 6, &endp, 0);
972 if (*endp) {
973 error_report("unrecognised bluetooth vlan Id");
974 return 0;
975 }
976 }
977
978 vlan = qemu_find_bt_vlan(vlan_id);
979
980 if (!vlan->slave)
981 error_report("warning: adding a slave device to an empty scatternet %i",
982 vlan_id);
983
984 if (!strcmp(devname, "keyboard"))
985 return bt_keyboard_init(vlan);
986
987 error_report("unsupported bluetooth device '%s'", devname);
988 return 0;
989 }
990
991 static int bt_parse(const char *opt)
992 {
993 const char *endp, *p;
994 int vlan;
995
996 if (strstart(opt, "hci", &endp)) {
997 if (!*endp || *endp == ',') {
998 if (*endp)
999 if (!strstart(endp, ",vlan=", 0))
1000 opt = endp + 1;
1001
1002 return bt_hci_parse(opt);
1003 }
1004 } else if (strstart(opt, "vhci", &endp)) {
1005 if (!*endp || *endp == ',') {
1006 if (*endp) {
1007 if (strstart(endp, ",vlan=", &p)) {
1008 vlan = strtol(p, (char **) &endp, 0);
1009 if (*endp) {
1010 error_report("bad scatternet '%s'", p);
1011 return 1;
1012 }
1013 } else {
1014 error_report("bad parameter '%s'", endp + 1);
1015 return 1;
1016 }
1017 } else
1018 vlan = 0;
1019
1020 bt_vhci_add(vlan);
1021 return 0;
1022 }
1023 } else if (strstart(opt, "device:", &endp))
1024 return !bt_device_add(endp);
1025
1026 error_report("bad bluetooth parameter '%s'", opt);
1027 return 1;
1028 }
1029
1030 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1031 {
1032 /* FIXME: change this to true for 1.3 */
1033 if (qemu_opt_get_bool(opts, "enable", false)) {
1034 #ifdef CONFIG_SECCOMP
1035 if (seccomp_start() < 0) {
1036 error_report("failed to install seccomp syscall filter "
1037 "in the kernel");
1038 return -1;
1039 }
1040 #else
1041 error_report("seccomp support is disabled");
1042 return -1;
1043 #endif
1044 }
1045
1046 return 0;
1047 }
1048
1049 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1050 {
1051 const char *proc_name;
1052
1053 if (qemu_opt_get(opts, "debug-threads")) {
1054 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1055 }
1056 qemu_name = qemu_opt_get(opts, "guest");
1057
1058 proc_name = qemu_opt_get(opts, "process");
1059 if (proc_name) {
1060 os_set_proc_name(proc_name);
1061 }
1062
1063 return 0;
1064 }
1065
1066 bool defaults_enabled(void)
1067 {
1068 return has_defaults;
1069 }
1070
1071 #ifndef _WIN32
1072 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1073 {
1074 int fd, dupfd, flags;
1075 int64_t fdset_id;
1076 const char *fd_opaque = NULL;
1077 AddfdInfo *fdinfo;
1078
1079 fd = qemu_opt_get_number(opts, "fd", -1);
1080 fdset_id = qemu_opt_get_number(opts, "set", -1);
1081 fd_opaque = qemu_opt_get(opts, "opaque");
1082
1083 if (fd < 0) {
1084 error_report("fd option is required and must be non-negative");
1085 return -1;
1086 }
1087
1088 if (fd <= STDERR_FILENO) {
1089 error_report("fd cannot be a standard I/O stream");
1090 return -1;
1091 }
1092
1093 /*
1094 * All fds inherited across exec() necessarily have FD_CLOEXEC
1095 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1096 */
1097 flags = fcntl(fd, F_GETFD);
1098 if (flags == -1 || (flags & FD_CLOEXEC)) {
1099 error_report("fd is not valid or already in use");
1100 return -1;
1101 }
1102
1103 if (fdset_id < 0) {
1104 error_report("set option is required and must be non-negative");
1105 return -1;
1106 }
1107
1108 #ifdef F_DUPFD_CLOEXEC
1109 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1110 #else
1111 dupfd = dup(fd);
1112 if (dupfd != -1) {
1113 qemu_set_cloexec(dupfd);
1114 }
1115 #endif
1116 if (dupfd == -1) {
1117 error_report("error duplicating fd: %s", strerror(errno));
1118 return -1;
1119 }
1120
1121 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1122 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1123 &error_abort);
1124 g_free(fdinfo);
1125
1126 return 0;
1127 }
1128
1129 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1130 {
1131 int fd;
1132
1133 fd = qemu_opt_get_number(opts, "fd", -1);
1134 close(fd);
1135
1136 return 0;
1137 }
1138 #endif
1139
1140 /***********************************************************/
1141 /* QEMU Block devices */
1142
1143 #define HD_OPTS "media=disk"
1144 #define CDROM_OPTS "media=cdrom"
1145 #define FD_OPTS ""
1146 #define PFLASH_OPTS ""
1147 #define MTD_OPTS ""
1148 #define SD_OPTS ""
1149
1150 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1151 {
1152 BlockInterfaceType *block_default_type = opaque;
1153
1154 return drive_new(opts, *block_default_type) == NULL;
1155 }
1156
1157 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1158 {
1159 if (qemu_opt_get(opts, "snapshot") == NULL) {
1160 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1161 }
1162 return 0;
1163 }
1164
1165 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1166 int index, const char *optstr)
1167 {
1168 QemuOpts *opts;
1169 DriveInfo *dinfo;
1170
1171 if (!enable || drive_get_by_index(type, index)) {
1172 return;
1173 }
1174
1175 opts = drive_add(type, index, NULL, optstr);
1176 if (snapshot) {
1177 drive_enable_snapshot(NULL, opts, NULL);
1178 }
1179
1180 dinfo = drive_new(opts, type);
1181 if (!dinfo) {
1182 exit(1);
1183 }
1184 dinfo->is_default = true;
1185
1186 }
1187
1188 static QemuOptsList qemu_smp_opts = {
1189 .name = "smp-opts",
1190 .implied_opt_name = "cpus",
1191 .merge_lists = true,
1192 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1193 .desc = {
1194 {
1195 .name = "cpus",
1196 .type = QEMU_OPT_NUMBER,
1197 }, {
1198 .name = "sockets",
1199 .type = QEMU_OPT_NUMBER,
1200 }, {
1201 .name = "cores",
1202 .type = QEMU_OPT_NUMBER,
1203 }, {
1204 .name = "threads",
1205 .type = QEMU_OPT_NUMBER,
1206 }, {
1207 .name = "maxcpus",
1208 .type = QEMU_OPT_NUMBER,
1209 },
1210 { /*End of list */ }
1211 },
1212 };
1213
1214 static void smp_parse(QemuOpts *opts)
1215 {
1216 if (opts) {
1217 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1218 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1219 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1220 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1221
1222 /* compute missing values, prefer sockets over cores over threads */
1223 if (cpus == 0 || sockets == 0) {
1224 sockets = sockets > 0 ? sockets : 1;
1225 cores = cores > 0 ? cores : 1;
1226 threads = threads > 0 ? threads : 1;
1227 if (cpus == 0) {
1228 cpus = cores * threads * sockets;
1229 }
1230 } else if (cores == 0) {
1231 threads = threads > 0 ? threads : 1;
1232 cores = cpus / (sockets * threads);
1233 cores = cores > 0 ? cores : 1;
1234 } else if (threads == 0) {
1235 threads = cpus / (cores * sockets);
1236 threads = threads > 0 ? threads : 1;
1237 } else if (sockets * cores * threads < cpus) {
1238 error_report("cpu topology: "
1239 "sockets (%u) * cores (%u) * threads (%u) < "
1240 "smp_cpus (%u)",
1241 sockets, cores, threads, cpus);
1242 exit(1);
1243 }
1244
1245 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1246
1247 if (max_cpus < cpus) {
1248 error_report("maxcpus must be equal to or greater than smp");
1249 exit(1);
1250 }
1251
1252 if (sockets * cores * threads > max_cpus) {
1253 error_report("cpu topology: "
1254 "sockets (%u) * cores (%u) * threads (%u) > "
1255 "maxcpus (%u)",
1256 sockets, cores, threads, max_cpus);
1257 exit(1);
1258 }
1259
1260 smp_cpus = cpus;
1261 smp_cores = cores;
1262 smp_threads = threads;
1263 }
1264
1265 if (smp_cpus > 1) {
1266 Error *blocker = NULL;
1267 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1268 replay_add_blocker(blocker);
1269 }
1270 }
1271
1272 static void realtime_init(void)
1273 {
1274 if (enable_mlock) {
1275 if (os_mlock() < 0) {
1276 error_report("locking memory failed");
1277 exit(1);
1278 }
1279 }
1280 }
1281
1282
1283 static void configure_msg(QemuOpts *opts)
1284 {
1285 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1286 }
1287
1288 /***********************************************************/
1289 /* Semihosting */
1290
1291 typedef struct SemihostingConfig {
1292 bool enabled;
1293 SemihostingTarget target;
1294 const char **argv;
1295 int argc;
1296 const char *cmdline; /* concatenated argv */
1297 } SemihostingConfig;
1298
1299 static SemihostingConfig semihosting;
1300
1301 bool semihosting_enabled(void)
1302 {
1303 return semihosting.enabled;
1304 }
1305
1306 SemihostingTarget semihosting_get_target(void)
1307 {
1308 return semihosting.target;
1309 }
1310
1311 const char *semihosting_get_arg(int i)
1312 {
1313 if (i >= semihosting.argc) {
1314 return NULL;
1315 }
1316 return semihosting.argv[i];
1317 }
1318
1319 int semihosting_get_argc(void)
1320 {
1321 return semihosting.argc;
1322 }
1323
1324 const char *semihosting_get_cmdline(void)
1325 {
1326 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1327 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1328 }
1329 return semihosting.cmdline;
1330 }
1331
1332 static int add_semihosting_arg(void *opaque,
1333 const char *name, const char *val,
1334 Error **errp)
1335 {
1336 SemihostingConfig *s = opaque;
1337 if (strcmp(name, "arg") == 0) {
1338 s->argc++;
1339 /* one extra element as g_strjoinv() expects NULL-terminated array */
1340 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1341 s->argv[s->argc - 1] = val;
1342 s->argv[s->argc] = NULL;
1343 }
1344 return 0;
1345 }
1346
1347 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1348 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1349 {
1350 char *cmd_token;
1351
1352 /* argv[0] */
1353 add_semihosting_arg(&semihosting, "arg", file, NULL);
1354
1355 /* split -append and initialize argv[1..n] */
1356 cmd_token = strtok(g_strdup(cmd), " ");
1357 while (cmd_token) {
1358 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1359 cmd_token = strtok(NULL, " ");
1360 }
1361 }
1362
1363 /* Now we still need this for compatibility with XEN. */
1364 bool has_igd_gfx_passthru;
1365 static void igd_gfx_passthru(void)
1366 {
1367 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1368 }
1369
1370 /***********************************************************/
1371 /* USB devices */
1372
1373 static int usb_device_add(const char *devname)
1374 {
1375 USBDevice *dev = NULL;
1376 #ifndef CONFIG_LINUX
1377 const char *p;
1378 #endif
1379
1380 if (!machine_usb(current_machine)) {
1381 return -1;
1382 }
1383
1384 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1385 dev = usbdevice_create(devname);
1386 if (dev)
1387 goto done;
1388
1389 /* the other ones */
1390 #ifndef CONFIG_LINUX
1391 /* only the linux version is qdev-ified, usb-bsd still needs this */
1392 if (strstart(devname, "host:", &p)) {
1393 dev = usb_host_device_open(usb_bus_find(-1), p);
1394 }
1395 #endif
1396 if (!dev)
1397 return -1;
1398
1399 done:
1400 return 0;
1401 }
1402
1403 static int usb_device_del(const char *devname)
1404 {
1405 int bus_num, addr;
1406 const char *p;
1407
1408 if (strstart(devname, "host:", &p)) {
1409 return -1;
1410 }
1411
1412 if (!machine_usb(current_machine)) {
1413 return -1;
1414 }
1415
1416 p = strchr(devname, '.');
1417 if (!p)
1418 return -1;
1419 bus_num = strtoul(devname, NULL, 0);
1420 addr = strtoul(p + 1, NULL, 0);
1421
1422 return usb_device_delete_addr(bus_num, addr);
1423 }
1424
1425 static int usb_parse(const char *cmdline)
1426 {
1427 int r;
1428 r = usb_device_add(cmdline);
1429 if (r < 0) {
1430 error_report("could not add USB device '%s'", cmdline);
1431 }
1432 return r;
1433 }
1434
1435 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1436 {
1437 const char *devname = qdict_get_str(qdict, "devname");
1438 if (usb_device_add(devname) < 0) {
1439 error_report("could not add USB device '%s'", devname);
1440 }
1441 }
1442
1443 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1444 {
1445 const char *devname = qdict_get_str(qdict, "devname");
1446 if (usb_device_del(devname) < 0) {
1447 error_report("could not delete USB device '%s'", devname);
1448 }
1449 }
1450
1451 /***********************************************************/
1452 /* machine registration */
1453
1454 MachineState *current_machine;
1455
1456 static MachineClass *find_machine(const char *name)
1457 {
1458 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1459 MachineClass *mc = NULL;
1460
1461 for (el = machines; el; el = el->next) {
1462 MachineClass *temp = el->data;
1463
1464 if (!strcmp(temp->name, name)) {
1465 mc = temp;
1466 break;
1467 }
1468 if (temp->alias &&
1469 !strcmp(temp->alias, name)) {
1470 mc = temp;
1471 break;
1472 }
1473 }
1474
1475 g_slist_free(machines);
1476 return mc;
1477 }
1478
1479 MachineClass *find_default_machine(void)
1480 {
1481 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1482 MachineClass *mc = NULL;
1483
1484 for (el = machines; el; el = el->next) {
1485 MachineClass *temp = el->data;
1486
1487 if (temp->is_default) {
1488 mc = temp;
1489 break;
1490 }
1491 }
1492
1493 g_slist_free(machines);
1494 return mc;
1495 }
1496
1497 MachineInfoList *qmp_query_machines(Error **errp)
1498 {
1499 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1500 MachineInfoList *mach_list = NULL;
1501
1502 for (el = machines; el; el = el->next) {
1503 MachineClass *mc = el->data;
1504 MachineInfoList *entry;
1505 MachineInfo *info;
1506
1507 info = g_malloc0(sizeof(*info));
1508 if (mc->is_default) {
1509 info->has_is_default = true;
1510 info->is_default = true;
1511 }
1512
1513 if (mc->alias) {
1514 info->has_alias = true;
1515 info->alias = g_strdup(mc->alias);
1516 }
1517
1518 info->name = g_strdup(mc->name);
1519 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1520 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1521
1522 entry = g_malloc0(sizeof(*entry));
1523 entry->value = info;
1524 entry->next = mach_list;
1525 mach_list = entry;
1526 }
1527
1528 g_slist_free(machines);
1529 return mach_list;
1530 }
1531
1532 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1533 {
1534 ObjectProperty *prop;
1535 ObjectPropertyIterator iter;
1536
1537 if (!qemu_opt_has_help_opt(opts)) {
1538 return 0;
1539 }
1540
1541 object_property_iter_init(&iter, OBJECT(machine));
1542 while ((prop = object_property_iter_next(&iter))) {
1543 if (!prop->set) {
1544 continue;
1545 }
1546
1547 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1548 prop->name, prop->type);
1549 if (prop->description) {
1550 error_printf(" (%s)\n", prop->description);
1551 } else {
1552 error_printf("\n");
1553 }
1554 }
1555
1556 return 1;
1557 }
1558
1559 /***********************************************************/
1560 /* main execution loop */
1561
1562 struct vm_change_state_entry {
1563 VMChangeStateHandler *cb;
1564 void *opaque;
1565 QLIST_ENTRY (vm_change_state_entry) entries;
1566 };
1567
1568 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1569
1570 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1571 void *opaque)
1572 {
1573 VMChangeStateEntry *e;
1574
1575 e = g_malloc0(sizeof (*e));
1576
1577 e->cb = cb;
1578 e->opaque = opaque;
1579 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1580 return e;
1581 }
1582
1583 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1584 {
1585 QLIST_REMOVE (e, entries);
1586 g_free (e);
1587 }
1588
1589 void vm_state_notify(int running, RunState state)
1590 {
1591 VMChangeStateEntry *e, *next;
1592
1593 trace_vm_state_notify(running, state);
1594
1595 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1596 e->cb(e->opaque, running, state);
1597 }
1598 }
1599
1600 static int reset_requested;
1601 static int shutdown_requested, shutdown_signal = -1;
1602 static pid_t shutdown_pid;
1603 static int powerdown_requested;
1604 static int debug_requested;
1605 static int suspend_requested;
1606 static WakeupReason wakeup_reason;
1607 static NotifierList powerdown_notifiers =
1608 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1609 static NotifierList suspend_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1611 static NotifierList wakeup_notifiers =
1612 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1613 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1614
1615 int qemu_shutdown_requested_get(void)
1616 {
1617 return shutdown_requested;
1618 }
1619
1620 int qemu_reset_requested_get(void)
1621 {
1622 return reset_requested;
1623 }
1624
1625 static int qemu_shutdown_requested(void)
1626 {
1627 return atomic_xchg(&shutdown_requested, 0);
1628 }
1629
1630 static void qemu_kill_report(void)
1631 {
1632 if (!qtest_driver() && shutdown_signal != -1) {
1633 if (shutdown_pid == 0) {
1634 /* This happens for eg ^C at the terminal, so it's worth
1635 * avoiding printing an odd message in that case.
1636 */
1637 error_report("terminating on signal %d", shutdown_signal);
1638 } else {
1639 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1640
1641 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1642 shutdown_signal, shutdown_pid,
1643 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1644 g_free(shutdown_cmd);
1645 }
1646 shutdown_signal = -1;
1647 }
1648 }
1649
1650 static int qemu_reset_requested(void)
1651 {
1652 int r = reset_requested;
1653 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1654 reset_requested = 0;
1655 return r;
1656 }
1657 return false;
1658 }
1659
1660 static int qemu_suspend_requested(void)
1661 {
1662 int r = suspend_requested;
1663 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1664 suspend_requested = 0;
1665 return r;
1666 }
1667 return false;
1668 }
1669
1670 static WakeupReason qemu_wakeup_requested(void)
1671 {
1672 return wakeup_reason;
1673 }
1674
1675 static int qemu_powerdown_requested(void)
1676 {
1677 int r = powerdown_requested;
1678 powerdown_requested = 0;
1679 return r;
1680 }
1681
1682 static int qemu_debug_requested(void)
1683 {
1684 int r = debug_requested;
1685 debug_requested = 0;
1686 return r;
1687 }
1688
1689 void qemu_system_reset(bool report)
1690 {
1691 MachineClass *mc;
1692
1693 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1694
1695 cpu_synchronize_all_states();
1696
1697 if (mc && mc->reset) {
1698 mc->reset();
1699 } else {
1700 qemu_devices_reset();
1701 }
1702 if (report) {
1703 qapi_event_send_reset(&error_abort);
1704 }
1705 cpu_synchronize_all_post_reset();
1706 }
1707
1708 void qemu_system_guest_panicked(GuestPanicInformation *info)
1709 {
1710 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1711
1712 if (current_cpu) {
1713 current_cpu->crash_occurred = true;
1714 }
1715 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1716 !!info, info, &error_abort);
1717 vm_stop(RUN_STATE_GUEST_PANICKED);
1718 if (!no_shutdown) {
1719 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1720 !!info, info, &error_abort);
1721 qemu_system_shutdown_request();
1722 }
1723
1724 if (info) {
1725 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1726 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1727 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1728 info->u.hyper_v.arg1,
1729 info->u.hyper_v.arg2,
1730 info->u.hyper_v.arg3,
1731 info->u.hyper_v.arg4,
1732 info->u.hyper_v.arg5);
1733 }
1734 qapi_free_GuestPanicInformation(info);
1735 }
1736 }
1737
1738 void qemu_system_reset_request(void)
1739 {
1740 if (no_reboot) {
1741 shutdown_requested = 1;
1742 } else {
1743 reset_requested = 1;
1744 }
1745 cpu_stop_current();
1746 qemu_notify_event();
1747 }
1748
1749 static void qemu_system_suspend(void)
1750 {
1751 pause_all_vcpus();
1752 notifier_list_notify(&suspend_notifiers, NULL);
1753 runstate_set(RUN_STATE_SUSPENDED);
1754 qapi_event_send_suspend(&error_abort);
1755 }
1756
1757 void qemu_system_suspend_request(void)
1758 {
1759 if (runstate_check(RUN_STATE_SUSPENDED)) {
1760 return;
1761 }
1762 suspend_requested = 1;
1763 cpu_stop_current();
1764 qemu_notify_event();
1765 }
1766
1767 void qemu_register_suspend_notifier(Notifier *notifier)
1768 {
1769 notifier_list_add(&suspend_notifiers, notifier);
1770 }
1771
1772 void qemu_system_wakeup_request(WakeupReason reason)
1773 {
1774 trace_system_wakeup_request(reason);
1775
1776 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1777 return;
1778 }
1779 if (!(wakeup_reason_mask & (1 << reason))) {
1780 return;
1781 }
1782 runstate_set(RUN_STATE_RUNNING);
1783 wakeup_reason = reason;
1784 qemu_notify_event();
1785 }
1786
1787 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1788 {
1789 if (enabled) {
1790 wakeup_reason_mask |= (1 << reason);
1791 } else {
1792 wakeup_reason_mask &= ~(1 << reason);
1793 }
1794 }
1795
1796 void qemu_register_wakeup_notifier(Notifier *notifier)
1797 {
1798 notifier_list_add(&wakeup_notifiers, notifier);
1799 }
1800
1801 void qemu_system_killed(int signal, pid_t pid)
1802 {
1803 shutdown_signal = signal;
1804 shutdown_pid = pid;
1805 no_shutdown = 0;
1806
1807 /* Cannot call qemu_system_shutdown_request directly because
1808 * we are in a signal handler.
1809 */
1810 shutdown_requested = 1;
1811 qemu_notify_event();
1812 }
1813
1814 void qemu_system_shutdown_request(void)
1815 {
1816 trace_qemu_system_shutdown_request();
1817 replay_shutdown_request();
1818 shutdown_requested = 1;
1819 qemu_notify_event();
1820 }
1821
1822 static void qemu_system_powerdown(void)
1823 {
1824 qapi_event_send_powerdown(&error_abort);
1825 notifier_list_notify(&powerdown_notifiers, NULL);
1826 }
1827
1828 void qemu_system_powerdown_request(void)
1829 {
1830 trace_qemu_system_powerdown_request();
1831 powerdown_requested = 1;
1832 qemu_notify_event();
1833 }
1834
1835 void qemu_register_powerdown_notifier(Notifier *notifier)
1836 {
1837 notifier_list_add(&powerdown_notifiers, notifier);
1838 }
1839
1840 void qemu_system_debug_request(void)
1841 {
1842 debug_requested = 1;
1843 qemu_notify_event();
1844 }
1845
1846 static bool main_loop_should_exit(void)
1847 {
1848 RunState r;
1849 if (qemu_debug_requested()) {
1850 vm_stop(RUN_STATE_DEBUG);
1851 }
1852 if (qemu_suspend_requested()) {
1853 qemu_system_suspend();
1854 }
1855 if (qemu_shutdown_requested()) {
1856 qemu_kill_report();
1857 qapi_event_send_shutdown(&error_abort);
1858 if (no_shutdown) {
1859 vm_stop(RUN_STATE_SHUTDOWN);
1860 } else {
1861 return true;
1862 }
1863 }
1864 if (qemu_reset_requested()) {
1865 pause_all_vcpus();
1866 qemu_system_reset(VMRESET_REPORT);
1867 resume_all_vcpus();
1868 if (!runstate_check(RUN_STATE_RUNNING) &&
1869 !runstate_check(RUN_STATE_INMIGRATE)) {
1870 runstate_set(RUN_STATE_PRELAUNCH);
1871 }
1872 }
1873 if (qemu_wakeup_requested()) {
1874 pause_all_vcpus();
1875 qemu_system_reset(VMRESET_SILENT);
1876 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1877 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1878 resume_all_vcpus();
1879 qapi_event_send_wakeup(&error_abort);
1880 }
1881 if (qemu_powerdown_requested()) {
1882 qemu_system_powerdown();
1883 }
1884 if (qemu_vmstop_requested(&r)) {
1885 vm_stop(r);
1886 }
1887 return false;
1888 }
1889
1890 static void main_loop(void)
1891 {
1892 #ifdef CONFIG_PROFILER
1893 int64_t ti;
1894 #endif
1895 do {
1896 #ifdef CONFIG_PROFILER
1897 ti = profile_getclock();
1898 #endif
1899 main_loop_wait(false);
1900 #ifdef CONFIG_PROFILER
1901 dev_time += profile_getclock() - ti;
1902 #endif
1903 } while (!main_loop_should_exit());
1904 }
1905
1906 static void version(void)
1907 {
1908 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1909 QEMU_COPYRIGHT "\n");
1910 }
1911
1912 static void help(int exitcode)
1913 {
1914 version();
1915 printf("usage: %s [options] [disk_image]\n\n"
1916 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1917 error_get_progname());
1918
1919 #define QEMU_OPTIONS_GENERATE_HELP
1920 #include "qemu-options-wrapper.h"
1921
1922 printf("\nDuring emulation, the following keys are useful:\n"
1923 "ctrl-alt-f toggle full screen\n"
1924 "ctrl-alt-n switch to virtual console 'n'\n"
1925 "ctrl-alt toggle mouse and keyboard grab\n"
1926 "\n"
1927 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1928
1929 exit(exitcode);
1930 }
1931
1932 #define HAS_ARG 0x0001
1933
1934 typedef struct QEMUOption {
1935 const char *name;
1936 int flags;
1937 int index;
1938 uint32_t arch_mask;
1939 } QEMUOption;
1940
1941 static const QEMUOption qemu_options[] = {
1942 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1943 #define QEMU_OPTIONS_GENERATE_OPTIONS
1944 #include "qemu-options-wrapper.h"
1945 { NULL },
1946 };
1947
1948 typedef struct VGAInterfaceInfo {
1949 const char *opt_name; /* option name */
1950 const char *name; /* human-readable name */
1951 /* Class names indicating that support is available.
1952 * If no class is specified, the interface is always available */
1953 const char *class_names[2];
1954 } VGAInterfaceInfo;
1955
1956 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1957 [VGA_NONE] = {
1958 .opt_name = "none",
1959 },
1960 [VGA_STD] = {
1961 .opt_name = "std",
1962 .name = "standard VGA",
1963 .class_names = { "VGA", "isa-vga" },
1964 },
1965 [VGA_CIRRUS] = {
1966 .opt_name = "cirrus",
1967 .name = "Cirrus VGA",
1968 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1969 },
1970 [VGA_VMWARE] = {
1971 .opt_name = "vmware",
1972 .name = "VMWare SVGA",
1973 .class_names = { "vmware-svga" },
1974 },
1975 [VGA_VIRTIO] = {
1976 .opt_name = "virtio",
1977 .name = "Virtio VGA",
1978 .class_names = { "virtio-vga" },
1979 },
1980 [VGA_QXL] = {
1981 .opt_name = "qxl",
1982 .name = "QXL VGA",
1983 .class_names = { "qxl-vga" },
1984 },
1985 [VGA_TCX] = {
1986 .opt_name = "tcx",
1987 .name = "TCX framebuffer",
1988 .class_names = { "SUNW,tcx" },
1989 },
1990 [VGA_CG3] = {
1991 .opt_name = "cg3",
1992 .name = "CG3 framebuffer",
1993 .class_names = { "cgthree" },
1994 },
1995 [VGA_XENFB] = {
1996 .opt_name = "xenfb",
1997 },
1998 };
1999
2000 static bool vga_interface_available(VGAInterfaceType t)
2001 {
2002 VGAInterfaceInfo *ti = &vga_interfaces[t];
2003
2004 assert(t < VGA_TYPE_MAX);
2005 return !ti->class_names[0] ||
2006 object_class_by_name(ti->class_names[0]) ||
2007 object_class_by_name(ti->class_names[1]);
2008 }
2009
2010 static void select_vgahw(const char *p)
2011 {
2012 const char *opts;
2013 int t;
2014
2015 assert(vga_interface_type == VGA_NONE);
2016 for (t = 0; t < VGA_TYPE_MAX; t++) {
2017 VGAInterfaceInfo *ti = &vga_interfaces[t];
2018 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2019 if (!vga_interface_available(t)) {
2020 error_report("%s not available", ti->name);
2021 exit(1);
2022 }
2023 vga_interface_type = t;
2024 break;
2025 }
2026 }
2027 if (t == VGA_TYPE_MAX) {
2028 invalid_vga:
2029 error_report("unknown vga type: %s", p);
2030 exit(1);
2031 }
2032 while (*opts) {
2033 const char *nextopt;
2034
2035 if (strstart(opts, ",retrace=", &nextopt)) {
2036 opts = nextopt;
2037 if (strstart(opts, "dumb", &nextopt))
2038 vga_retrace_method = VGA_RETRACE_DUMB;
2039 else if (strstart(opts, "precise", &nextopt))
2040 vga_retrace_method = VGA_RETRACE_PRECISE;
2041 else goto invalid_vga;
2042 } else goto invalid_vga;
2043 opts = nextopt;
2044 }
2045 }
2046
2047 typedef enum DisplayType {
2048 DT_DEFAULT,
2049 DT_CURSES,
2050 DT_SDL,
2051 DT_COCOA,
2052 DT_GTK,
2053 DT_NONE,
2054 } DisplayType;
2055
2056 static DisplayType select_display(const char *p)
2057 {
2058 const char *opts;
2059 DisplayType display = DT_DEFAULT;
2060
2061 if (strstart(p, "sdl", &opts)) {
2062 #ifdef CONFIG_SDL
2063 display = DT_SDL;
2064 while (*opts) {
2065 const char *nextopt;
2066
2067 if (strstart(opts, ",frame=", &nextopt)) {
2068 opts = nextopt;
2069 if (strstart(opts, "on", &nextopt)) {
2070 no_frame = 0;
2071 } else if (strstart(opts, "off", &nextopt)) {
2072 no_frame = 1;
2073 } else {
2074 goto invalid_sdl_args;
2075 }
2076 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2077 opts = nextopt;
2078 if (strstart(opts, "on", &nextopt)) {
2079 alt_grab = 1;
2080 } else if (strstart(opts, "off", &nextopt)) {
2081 alt_grab = 0;
2082 } else {
2083 goto invalid_sdl_args;
2084 }
2085 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2086 opts = nextopt;
2087 if (strstart(opts, "on", &nextopt)) {
2088 ctrl_grab = 1;
2089 } else if (strstart(opts, "off", &nextopt)) {
2090 ctrl_grab = 0;
2091 } else {
2092 goto invalid_sdl_args;
2093 }
2094 } else if (strstart(opts, ",window_close=", &nextopt)) {
2095 opts = nextopt;
2096 if (strstart(opts, "on", &nextopt)) {
2097 no_quit = 0;
2098 } else if (strstart(opts, "off", &nextopt)) {
2099 no_quit = 1;
2100 } else {
2101 goto invalid_sdl_args;
2102 }
2103 } else if (strstart(opts, ",gl=", &nextopt)) {
2104 opts = nextopt;
2105 if (strstart(opts, "on", &nextopt)) {
2106 request_opengl = 1;
2107 } else if (strstart(opts, "off", &nextopt)) {
2108 request_opengl = 0;
2109 } else {
2110 goto invalid_sdl_args;
2111 }
2112 } else {
2113 invalid_sdl_args:
2114 error_report("invalid SDL option string");
2115 exit(1);
2116 }
2117 opts = nextopt;
2118 }
2119 #else
2120 error_report("SDL support is disabled");
2121 exit(1);
2122 #endif
2123 } else if (strstart(p, "vnc", &opts)) {
2124 if (*opts == '=') {
2125 vnc_parse(opts + 1, &error_fatal);
2126 } else {
2127 error_report("VNC requires a display argument vnc=<display>");
2128 exit(1);
2129 }
2130 } else if (strstart(p, "curses", &opts)) {
2131 #ifdef CONFIG_CURSES
2132 display = DT_CURSES;
2133 #else
2134 error_report("curses support is disabled");
2135 exit(1);
2136 #endif
2137 } else if (strstart(p, "gtk", &opts)) {
2138 #ifdef CONFIG_GTK
2139 display = DT_GTK;
2140 while (*opts) {
2141 const char *nextopt;
2142
2143 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2144 opts = nextopt;
2145 if (strstart(opts, "on", &nextopt)) {
2146 grab_on_hover = true;
2147 } else if (strstart(opts, "off", &nextopt)) {
2148 grab_on_hover = false;
2149 } else {
2150 goto invalid_gtk_args;
2151 }
2152 } else if (strstart(opts, ",gl=", &nextopt)) {
2153 opts = nextopt;
2154 if (strstart(opts, "on", &nextopt)) {
2155 request_opengl = 1;
2156 } else if (strstart(opts, "off", &nextopt)) {
2157 request_opengl = 0;
2158 } else {
2159 goto invalid_gtk_args;
2160 }
2161 } else {
2162 invalid_gtk_args:
2163 error_report("invalid GTK option string");
2164 exit(1);
2165 }
2166 opts = nextopt;
2167 }
2168 #else
2169 error_report("GTK support is disabled");
2170 exit(1);
2171 #endif
2172 } else if (strstart(p, "none", &opts)) {
2173 display = DT_NONE;
2174 } else {
2175 error_report("unknown display type");
2176 exit(1);
2177 }
2178
2179 return display;
2180 }
2181
2182 static int balloon_parse(const char *arg)
2183 {
2184 QemuOpts *opts;
2185
2186 if (strcmp(arg, "none") == 0) {
2187 return 0;
2188 }
2189
2190 if (!strncmp(arg, "virtio", 6)) {
2191 if (arg[6] == ',') {
2192 /* have params -> parse them */
2193 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2194 false);
2195 if (!opts)
2196 return -1;
2197 } else {
2198 /* create empty opts */
2199 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2200 &error_abort);
2201 }
2202 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2203 return 0;
2204 }
2205
2206 return -1;
2207 }
2208
2209 char *qemu_find_file(int type, const char *name)
2210 {
2211 int i;
2212 const char *subdir;
2213 char *buf;
2214
2215 /* Try the name as a straight path first */
2216 if (access(name, R_OK) == 0) {
2217 trace_load_file(name, name);
2218 return g_strdup(name);
2219 }
2220
2221 switch (type) {
2222 case QEMU_FILE_TYPE_BIOS:
2223 subdir = "";
2224 break;
2225 case QEMU_FILE_TYPE_KEYMAP:
2226 subdir = "keymaps/";
2227 break;
2228 default:
2229 abort();
2230 }
2231
2232 for (i = 0; i < data_dir_idx; i++) {
2233 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2234 if (access(buf, R_OK) == 0) {
2235 trace_load_file(name, buf);
2236 return buf;
2237 }
2238 g_free(buf);
2239 }
2240 return NULL;
2241 }
2242
2243 static inline bool nonempty_str(const char *str)
2244 {
2245 return str && *str;
2246 }
2247
2248 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2249 {
2250 gchar *buf;
2251 size_t size;
2252 const char *name, *file, *str;
2253 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2254
2255 if (fw_cfg == NULL) {
2256 error_report("fw_cfg device not available");
2257 return -1;
2258 }
2259 name = qemu_opt_get(opts, "name");
2260 file = qemu_opt_get(opts, "file");
2261 str = qemu_opt_get(opts, "string");
2262
2263 /* we need name and either a file or the content string */
2264 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2265 error_report("invalid argument(s)");
2266 return -1;
2267 }
2268 if (nonempty_str(file) && nonempty_str(str)) {
2269 error_report("file and string are mutually exclusive");
2270 return -1;
2271 }
2272 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2273 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2274 return -1;
2275 }
2276 if (strncmp(name, "opt/", 4) != 0) {
2277 error_report("warning: externally provided fw_cfg item names "
2278 "should be prefixed with \"opt/\"");
2279 }
2280 if (nonempty_str(str)) {
2281 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2282 buf = g_memdup(str, size);
2283 } else {
2284 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2285 error_report("can't load %s", file);
2286 return -1;
2287 }
2288 }
2289 /* For legacy, keep user files in a specific global order. */
2290 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2291 fw_cfg_add_file(fw_cfg, name, buf, size);
2292 fw_cfg_reset_order_override(fw_cfg);
2293 return 0;
2294 }
2295
2296 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2297 {
2298 return qdev_device_help(opts);
2299 }
2300
2301 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2302 {
2303 Error *err = NULL;
2304 DeviceState *dev;
2305
2306 dev = qdev_device_add(opts, &err);
2307 if (!dev) {
2308 error_report_err(err);
2309 return -1;
2310 }
2311 object_unref(OBJECT(dev));
2312 return 0;
2313 }
2314
2315 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2316 {
2317 Error *local_err = NULL;
2318
2319 qemu_chr_new_from_opts(opts, &local_err);
2320 if (local_err) {
2321 error_report_err(local_err);
2322 return -1;
2323 }
2324 return 0;
2325 }
2326
2327 #ifdef CONFIG_VIRTFS
2328 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2329 {
2330 return qemu_fsdev_add(opts);
2331 }
2332 #endif
2333
2334 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2335 {
2336 Chardev *chr;
2337 const char *chardev;
2338 const char *mode;
2339 int flags;
2340
2341 mode = qemu_opt_get(opts, "mode");
2342 if (mode == NULL) {
2343 mode = "readline";
2344 }
2345 if (strcmp(mode, "readline") == 0) {
2346 flags = MONITOR_USE_READLINE;
2347 } else if (strcmp(mode, "control") == 0) {
2348 flags = MONITOR_USE_CONTROL;
2349 } else {
2350 error_report("unknown monitor mode \"%s\"", mode);
2351 exit(1);
2352 }
2353
2354 if (qemu_opt_get_bool(opts, "pretty", 0))
2355 flags |= MONITOR_USE_PRETTY;
2356
2357 if (qemu_opt_get_bool(opts, "default", 0)) {
2358 error_report("option 'default' does nothing and is deprecated");
2359 }
2360
2361 chardev = qemu_opt_get(opts, "chardev");
2362 chr = qemu_chr_find(chardev);
2363 if (chr == NULL) {
2364 error_report("chardev \"%s\" not found", chardev);
2365 exit(1);
2366 }
2367
2368 monitor_init(chr, flags);
2369 return 0;
2370 }
2371
2372 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2373 {
2374 static int monitor_device_index = 0;
2375 QemuOpts *opts;
2376 const char *p;
2377 char label[32];
2378
2379 if (strstart(optarg, "chardev:", &p)) {
2380 snprintf(label, sizeof(label), "%s", p);
2381 } else {
2382 snprintf(label, sizeof(label), "compat_monitor%d",
2383 monitor_device_index);
2384 opts = qemu_chr_parse_compat(label, optarg);
2385 if (!opts) {
2386 error_report("parse error: %s", optarg);
2387 exit(1);
2388 }
2389 }
2390
2391 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2392 qemu_opt_set(opts, "mode", mode, &error_abort);
2393 qemu_opt_set(opts, "chardev", label, &error_abort);
2394 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2395 monitor_device_index++;
2396 }
2397
2398 struct device_config {
2399 enum {
2400 DEV_USB, /* -usbdevice */
2401 DEV_BT, /* -bt */
2402 DEV_SERIAL, /* -serial */
2403 DEV_PARALLEL, /* -parallel */
2404 DEV_VIRTCON, /* -virtioconsole */
2405 DEV_DEBUGCON, /* -debugcon */
2406 DEV_GDB, /* -gdb, -s */
2407 DEV_SCLP, /* s390 sclp */
2408 } type;
2409 const char *cmdline;
2410 Location loc;
2411 QTAILQ_ENTRY(device_config) next;
2412 };
2413
2414 static QTAILQ_HEAD(, device_config) device_configs =
2415 QTAILQ_HEAD_INITIALIZER(device_configs);
2416
2417 static void add_device_config(int type, const char *cmdline)
2418 {
2419 struct device_config *conf;
2420
2421 conf = g_malloc0(sizeof(*conf));
2422 conf->type = type;
2423 conf->cmdline = cmdline;
2424 loc_save(&conf->loc);
2425 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2426 }
2427
2428 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2429 {
2430 struct device_config *conf;
2431 int rc;
2432
2433 QTAILQ_FOREACH(conf, &device_configs, next) {
2434 if (conf->type != type)
2435 continue;
2436 loc_push_restore(&conf->loc);
2437 rc = func(conf->cmdline);
2438 loc_pop(&conf->loc);
2439 if (rc) {
2440 return rc;
2441 }
2442 }
2443 return 0;
2444 }
2445
2446 static int serial_parse(const char *devname)
2447 {
2448 static int index = 0;
2449 char label[32];
2450
2451 if (strcmp(devname, "none") == 0)
2452 return 0;
2453 if (index == MAX_SERIAL_PORTS) {
2454 error_report("too many serial ports");
2455 exit(1);
2456 }
2457 snprintf(label, sizeof(label), "serial%d", index);
2458 serial_hds[index] = qemu_chr_new(label, devname);
2459 if (!serial_hds[index]) {
2460 error_report("could not connect serial device"
2461 " to character backend '%s'", devname);
2462 return -1;
2463 }
2464 index++;
2465 return 0;
2466 }
2467
2468 static int parallel_parse(const char *devname)
2469 {
2470 static int index = 0;
2471 char label[32];
2472
2473 if (strcmp(devname, "none") == 0)
2474 return 0;
2475 if (index == MAX_PARALLEL_PORTS) {
2476 error_report("too many parallel ports");
2477 exit(1);
2478 }
2479 snprintf(label, sizeof(label), "parallel%d", index);
2480 parallel_hds[index] = qemu_chr_new(label, devname);
2481 if (!parallel_hds[index]) {
2482 error_report("could not connect parallel device"
2483 " to character backend '%s'", devname);
2484 return -1;
2485 }
2486 index++;
2487 return 0;
2488 }
2489
2490 static int virtcon_parse(const char *devname)
2491 {
2492 QemuOptsList *device = qemu_find_opts("device");
2493 static int index = 0;
2494 char label[32];
2495 QemuOpts *bus_opts, *dev_opts;
2496
2497 if (strcmp(devname, "none") == 0)
2498 return 0;
2499 if (index == MAX_VIRTIO_CONSOLES) {
2500 error_report("too many virtio consoles");
2501 exit(1);
2502 }
2503
2504 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2505 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2506
2507 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2508 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2509
2510 snprintf(label, sizeof(label), "virtcon%d", index);
2511 virtcon_hds[index] = qemu_chr_new(label, devname);
2512 if (!virtcon_hds[index]) {
2513 error_report("could not connect virtio console"
2514 " to character backend '%s'", devname);
2515 return -1;
2516 }
2517 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2518
2519 index++;
2520 return 0;
2521 }
2522
2523 static int sclp_parse(const char *devname)
2524 {
2525 QemuOptsList *device = qemu_find_opts("device");
2526 static int index = 0;
2527 char label[32];
2528 QemuOpts *dev_opts;
2529
2530 if (strcmp(devname, "none") == 0) {
2531 return 0;
2532 }
2533 if (index == MAX_SCLP_CONSOLES) {
2534 error_report("too many sclp consoles");
2535 exit(1);
2536 }
2537
2538 assert(arch_type == QEMU_ARCH_S390X);
2539
2540 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2541 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2542
2543 snprintf(label, sizeof(label), "sclpcon%d", index);
2544 sclp_hds[index] = qemu_chr_new(label, devname);
2545 if (!sclp_hds[index]) {
2546 error_report("could not connect sclp console"
2547 " to character backend '%s'", devname);
2548 return -1;
2549 }
2550 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2551
2552 index++;
2553 return 0;
2554 }
2555
2556 static int debugcon_parse(const char *devname)
2557 {
2558 QemuOpts *opts;
2559
2560 if (!qemu_chr_new("debugcon", devname)) {
2561 exit(1);
2562 }
2563 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2564 if (!opts) {
2565 error_report("already have a debugcon device");
2566 exit(1);
2567 }
2568 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2569 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2570 return 0;
2571 }
2572
2573 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2574 {
2575 const MachineClass *mc1 = a, *mc2 = b;
2576 int res;
2577
2578 if (mc1->family == NULL) {
2579 if (mc2->family == NULL) {
2580 /* Compare standalone machine types against each other; they sort
2581 * in increasing order.
2582 */
2583 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2584 object_class_get_name(OBJECT_CLASS(mc2)));
2585 }
2586
2587 /* Standalone machine types sort after families. */
2588 return 1;
2589 }
2590
2591 if (mc2->family == NULL) {
2592 /* Families sort before standalone machine types. */
2593 return -1;
2594 }
2595
2596 /* Families sort between each other alphabetically increasingly. */
2597 res = strcmp(mc1->family, mc2->family);
2598 if (res != 0) {
2599 return res;
2600 }
2601
2602 /* Within the same family, machine types sort in decreasing order. */
2603 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2604 object_class_get_name(OBJECT_CLASS(mc1)));
2605 }
2606
2607 static MachineClass *machine_parse(const char *name)
2608 {
2609 MachineClass *mc = NULL;
2610 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2611
2612 if (name) {
2613 mc = find_machine(name);
2614 }
2615 if (mc) {
2616 g_slist_free(machines);
2617 return mc;
2618 }
2619 if (name && !is_help_option(name)) {
2620 error_report("unsupported machine type");
2621 error_printf("Use -machine help to list supported machines\n");
2622 } else {
2623 printf("Supported machines are:\n");
2624 machines = g_slist_sort(machines, machine_class_cmp);
2625 for (el = machines; el; el = el->next) {
2626 MachineClass *mc = el->data;
2627 if (mc->alias) {
2628 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2629 }
2630 printf("%-20s %s%s\n", mc->name, mc->desc,
2631 mc->is_default ? " (default)" : "");
2632 }
2633 }
2634
2635 g_slist_free(machines);
2636 exit(!name || !is_help_option(name));
2637 }
2638
2639 void qemu_add_exit_notifier(Notifier *notify)
2640 {
2641 notifier_list_add(&exit_notifiers, notify);
2642 }
2643
2644 void qemu_remove_exit_notifier(Notifier *notify)
2645 {
2646 notifier_remove(notify);
2647 }
2648
2649 static void qemu_run_exit_notifiers(void)
2650 {
2651 notifier_list_notify(&exit_notifiers, NULL);
2652 }
2653
2654 static bool machine_init_done;
2655
2656 void qemu_add_machine_init_done_notifier(Notifier *notify)
2657 {
2658 notifier_list_add(&machine_init_done_notifiers, notify);
2659 if (machine_init_done) {
2660 notify->notify(notify, NULL);
2661 }
2662 }
2663
2664 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2665 {
2666 notifier_remove(notify);
2667 }
2668
2669 static void qemu_run_machine_init_done_notifiers(void)
2670 {
2671 notifier_list_notify(&machine_init_done_notifiers, NULL);
2672 machine_init_done = true;
2673 }
2674
2675 static const QEMUOption *lookup_opt(int argc, char **argv,
2676 const char **poptarg, int *poptind)
2677 {
2678 const QEMUOption *popt;
2679 int optind = *poptind;
2680 char *r = argv[optind];
2681 const char *optarg;
2682
2683 loc_set_cmdline(argv, optind, 1);
2684 optind++;
2685 /* Treat --foo the same as -foo. */
2686 if (r[1] == '-')
2687 r++;
2688 popt = qemu_options;
2689 for(;;) {
2690 if (!popt->name) {
2691 error_report("invalid option");
2692 exit(1);
2693 }
2694 if (!strcmp(popt->name, r + 1))
2695 break;
2696 popt++;
2697 }
2698 if (popt->flags & HAS_ARG) {
2699 if (optind >= argc) {
2700 error_report("requires an argument");
2701 exit(1);
2702 }
2703 optarg = argv[optind++];
2704 loc_set_cmdline(argv, optind - 2, 2);
2705 } else {
2706 optarg = NULL;
2707 }
2708
2709 *poptarg = optarg;
2710 *poptind = optind;
2711
2712 return popt;
2713 }
2714
2715 static MachineClass *select_machine(void)
2716 {
2717 MachineClass *machine_class = find_default_machine();
2718 const char *optarg;
2719 QemuOpts *opts;
2720 Location loc;
2721
2722 loc_push_none(&loc);
2723
2724 opts = qemu_get_machine_opts();
2725 qemu_opts_loc_restore(opts);
2726
2727 optarg = qemu_opt_get(opts, "type");
2728 if (optarg) {
2729 machine_class = machine_parse(optarg);
2730 }
2731
2732 if (!machine_class) {
2733 error_report("No machine specified, and there is no default");
2734 error_printf("Use -machine help to list supported machines\n");
2735 exit(1);
2736 }
2737
2738 loc_pop(&loc);
2739 return machine_class;
2740 }
2741
2742 static int machine_set_property(void *opaque,
2743 const char *name, const char *value,
2744 Error **errp)
2745 {
2746 Object *obj = OBJECT(opaque);
2747 Error *local_err = NULL;
2748 char *p, *qom_name;
2749
2750 if (strcmp(name, "type") == 0) {
2751 return 0;
2752 }
2753
2754 qom_name = g_strdup(name);
2755 for (p = qom_name; *p; p++) {
2756 if (*p == '_') {
2757 *p = '-';
2758 }
2759 }
2760
2761 object_property_parse(obj, value, qom_name, &local_err);
2762 g_free(qom_name);
2763
2764 if (local_err) {
2765 error_report_err(local_err);
2766 return -1;
2767 }
2768
2769 return 0;
2770 }
2771
2772
2773 /*
2774 * Initial object creation happens before all other
2775 * QEMU data types are created. The majority of objects
2776 * can be created at this point. The rng-egd object
2777 * cannot be created here, as it depends on the chardev
2778 * already existing.
2779 */
2780 static bool object_create_initial(const char *type)
2781 {
2782 if (g_str_equal(type, "rng-egd")) {
2783 return false;
2784 }
2785
2786 /*
2787 * return false for concrete netfilters since
2788 * they depend on netdevs already existing
2789 */
2790 if (g_str_equal(type, "filter-buffer") ||
2791 g_str_equal(type, "filter-dump") ||
2792 g_str_equal(type, "filter-mirror") ||
2793 g_str_equal(type, "filter-redirector") ||
2794 g_str_equal(type, "colo-compare") ||
2795 g_str_equal(type, "filter-rewriter") ||
2796 g_str_equal(type, "filter-replay")) {
2797 return false;
2798 }
2799
2800 /* Memory allocation by backends needs to be done
2801 * after configure_accelerator() (due to the tcg_enabled()
2802 * checks at memory_region_init_*()).
2803 *
2804 * Also, allocation of large amounts of memory may delay
2805 * chardev initialization for too long, and trigger timeouts
2806 * on software that waits for a monitor socket to be created
2807 * (e.g. libvirt).
2808 */
2809 if (g_str_has_prefix(type, "memory-backend-")) {
2810 return false;
2811 }
2812
2813 return true;
2814 }
2815
2816
2817 /*
2818 * The remainder of object creation happens after the
2819 * creation of chardev, fsdev, net clients and device data types.
2820 */
2821 static bool object_create_delayed(const char *type)
2822 {
2823 return !object_create_initial(type);
2824 }
2825
2826
2827 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2828 MachineClass *mc)
2829 {
2830 uint64_t sz;
2831 const char *mem_str;
2832 const char *maxmem_str, *slots_str;
2833 const ram_addr_t default_ram_size = mc->default_ram_size;
2834 QemuOpts *opts = qemu_find_opts_singleton("memory");
2835 Location loc;
2836
2837 loc_push_none(&loc);
2838 qemu_opts_loc_restore(opts);
2839
2840 sz = 0;
2841 mem_str = qemu_opt_get(opts, "size");
2842 if (mem_str) {
2843 if (!*mem_str) {
2844 error_report("missing 'size' option value");
2845 exit(EXIT_FAILURE);
2846 }
2847
2848 sz = qemu_opt_get_size(opts, "size", ram_size);
2849
2850 /* Fix up legacy suffix-less format */
2851 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2852 uint64_t overflow_check = sz;
2853
2854 sz <<= 20;
2855 if ((sz >> 20) != overflow_check) {
2856 error_report("too large 'size' option value");
2857 exit(EXIT_FAILURE);
2858 }
2859 }
2860 }
2861
2862 /* backward compatibility behaviour for case "-m 0" */
2863 if (sz == 0) {
2864 sz = default_ram_size;
2865 }
2866
2867 sz = QEMU_ALIGN_UP(sz, 8192);
2868 ram_size = sz;
2869 if (ram_size != sz) {
2870 error_report("ram size too large");
2871 exit(EXIT_FAILURE);
2872 }
2873
2874 /* store value for the future use */
2875 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2876 *maxram_size = ram_size;
2877
2878 maxmem_str = qemu_opt_get(opts, "maxmem");
2879 slots_str = qemu_opt_get(opts, "slots");
2880 if (maxmem_str && slots_str) {
2881 uint64_t slots;
2882
2883 sz = qemu_opt_get_size(opts, "maxmem", 0);
2884 slots = qemu_opt_get_number(opts, "slots", 0);
2885 if (sz < ram_size) {
2886 error_report("invalid value of -m option maxmem: "
2887 "maximum memory size (0x%" PRIx64 ") must be at least "
2888 "the initial memory size (0x" RAM_ADDR_FMT ")",
2889 sz, ram_size);
2890 exit(EXIT_FAILURE);
2891 } else if (sz > ram_size) {
2892 if (!slots) {
2893 error_report("invalid value of -m option: maxmem was "
2894 "specified, but no hotplug slots were specified");
2895 exit(EXIT_FAILURE);
2896 }
2897 } else if (slots) {
2898 error_report("invalid value of -m option maxmem: "
2899 "memory slots were specified but maximum memory size "
2900 "(0x%" PRIx64 ") is equal to the initial memory size "
2901 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2902 exit(EXIT_FAILURE);
2903 }
2904
2905 *maxram_size = sz;
2906 *ram_slots = slots;
2907 } else if ((!maxmem_str && slots_str) ||
2908 (maxmem_str && !slots_str)) {
2909 error_report("invalid -m option value: missing "
2910 "'%s' option", slots_str ? "maxmem" : "slots");
2911 exit(EXIT_FAILURE);
2912 }
2913
2914 loc_pop(&loc);
2915 }
2916
2917 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2918 {
2919 GlobalProperty *g;
2920
2921 g = g_malloc0(sizeof(*g));
2922 g->driver = qemu_opt_get(opts, "driver");
2923 g->property = qemu_opt_get(opts, "property");
2924 g->value = qemu_opt_get(opts, "value");
2925 g->user_provided = true;
2926 g->errp = &error_fatal;
2927 qdev_prop_register_global(g);
2928 return 0;
2929 }
2930
2931 static int qemu_read_default_config_file(void)
2932 {
2933 int ret;
2934
2935 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2936 if (ret < 0 && ret != -ENOENT) {
2937 return ret;
2938 }
2939
2940 return 0;
2941 }
2942
2943 int main(int argc, char **argv, char **envp)
2944 {
2945 int i;
2946 int snapshot, linux_boot;
2947 const char *initrd_filename;
2948 const char *kernel_filename, *kernel_cmdline;
2949 const char *boot_order = NULL;
2950 const char *boot_once = NULL;
2951 DisplayState *ds;
2952 int cyls, heads, secs, translation;
2953 QemuOpts *opts, *machine_opts;
2954 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
2955 QemuOptsList *olist;
2956 int optind;
2957 const char *optarg;
2958 const char *loadvm = NULL;
2959 MachineClass *machine_class;
2960 const char *cpu_model;
2961 const char *vga_model = NULL;
2962 const char *qtest_chrdev = NULL;
2963 const char *qtest_log = NULL;
2964 const char *pid_file = NULL;
2965 const char *incoming = NULL;
2966 bool defconfig = true;
2967 bool userconfig = true;
2968 bool nographic = false;
2969 DisplayType display_type = DT_DEFAULT;
2970 int display_remote = 0;
2971 const char *log_mask = NULL;
2972 const char *log_file = NULL;
2973 char *trace_file = NULL;
2974 ram_addr_t maxram_size;
2975 uint64_t ram_slots = 0;
2976 FILE *vmstate_dump_file = NULL;
2977 Error *main_loop_err = NULL;
2978 Error *err = NULL;
2979 bool list_data_dirs = false;
2980 typedef struct BlockdevOptions_queue {
2981 BlockdevOptions *bdo;
2982 Location loc;
2983 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
2984 } BlockdevOptions_queue;
2985 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
2986 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2987
2988 module_call_init(MODULE_INIT_TRACE);
2989
2990 qemu_init_cpu_list();
2991 qemu_init_cpu_loop();
2992 qemu_mutex_lock_iothread();
2993
2994 atexit(qemu_run_exit_notifiers);
2995 error_set_progname(argv[0]);
2996 qemu_init_exec_dir(argv[0]);
2997
2998 module_call_init(MODULE_INIT_QOM);
2999 monitor_init_qmp_commands();
3000
3001 qemu_add_opts(&qemu_drive_opts);
3002 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3003 qemu_add_drive_opts(&qemu_common_drive_opts);
3004 qemu_add_drive_opts(&qemu_drive_opts);
3005 qemu_add_drive_opts(&bdrv_runtime_opts);
3006 qemu_add_opts(&qemu_chardev_opts);
3007 qemu_add_opts(&qemu_device_opts);
3008 qemu_add_opts(&qemu_netdev_opts);
3009 qemu_add_opts(&qemu_net_opts);
3010 qemu_add_opts(&qemu_rtc_opts);
3011 qemu_add_opts(&qemu_global_opts);
3012 qemu_add_opts(&qemu_mon_opts);
3013 qemu_add_opts(&qemu_trace_opts);
3014 qemu_add_opts(&qemu_option_rom_opts);
3015 qemu_add_opts(&qemu_machine_opts);
3016 qemu_add_opts(&qemu_accel_opts);
3017 qemu_add_opts(&qemu_mem_opts);
3018 qemu_add_opts(&qemu_smp_opts);
3019 qemu_add_opts(&qemu_boot_opts);
3020 qemu_add_opts(&qemu_sandbox_opts);
3021 qemu_add_opts(&qemu_add_fd_opts);
3022 qemu_add_opts(&qemu_object_opts);
3023 qemu_add_opts(&qemu_tpmdev_opts);
3024 qemu_add_opts(&qemu_realtime_opts);
3025 qemu_add_opts(&qemu_msg_opts);
3026 qemu_add_opts(&qemu_name_opts);
3027 qemu_add_opts(&qemu_numa_opts);
3028 qemu_add_opts(&qemu_icount_opts);
3029 qemu_add_opts(&qemu_semihosting_config_opts);
3030 qemu_add_opts(&qemu_fw_cfg_opts);
3031 module_call_init(MODULE_INIT_OPTS);
3032
3033 runstate_init();
3034
3035 if (qcrypto_init(&err) < 0) {
3036 error_reportf_err(err, "cannot initialize crypto: ");
3037 exit(1);
3038 }
3039 rtc_clock = QEMU_CLOCK_HOST;
3040
3041 QLIST_INIT (&vm_change_state_head);
3042 os_setup_early_signal_handling();
3043
3044 cpu_model = NULL;
3045 snapshot = 0;
3046 cyls = heads = secs = 0;
3047 translation = BIOS_ATA_TRANSLATION_AUTO;
3048
3049 nb_nics = 0;
3050
3051 bdrv_init_with_whitelist();
3052
3053 autostart = 1;
3054
3055 /* first pass of option parsing */
3056 optind = 1;
3057 while (optind < argc) {
3058 if (argv[optind][0] != '-') {
3059 /* disk image */
3060 optind++;
3061 } else {
3062 const QEMUOption *popt;
3063
3064 popt = lookup_opt(argc, argv, &optarg, &optind);
3065 switch (popt->index) {
3066 case QEMU_OPTION_nodefconfig:
3067 defconfig = false;
3068 break;
3069 case QEMU_OPTION_nouserconfig:
3070 userconfig = false;
3071 break;
3072 }
3073 }
3074 }
3075
3076 if (defconfig && userconfig) {
3077 if (qemu_read_default_config_file() < 0) {
3078 exit(1);
3079 }
3080 }
3081
3082 /* second pass of option parsing */
3083 optind = 1;
3084 for(;;) {
3085 if (optind >= argc)
3086 break;
3087 if (argv[optind][0] != '-') {
3088 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3089 } else {
3090 const QEMUOption *popt;
3091
3092 popt = lookup_opt(argc, argv, &optarg, &optind);
3093 if (!(popt->arch_mask & arch_type)) {
3094 error_report("Option not supported for this target");
3095 exit(1);
3096 }
3097 switch(popt->index) {
3098 case QEMU_OPTION_no_kvm_irqchip: {
3099 olist = qemu_find_opts("machine");
3100 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3101 break;
3102 }
3103 case QEMU_OPTION_cpu:
3104 /* hw initialization will check this */
3105 cpu_model = optarg;
3106 break;
3107 case QEMU_OPTION_hda:
3108 {
3109 char buf[256];
3110 if (cyls == 0)
3111 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3112 else
3113 snprintf(buf, sizeof(buf),
3114 "%s,cyls=%d,heads=%d,secs=%d%s",
3115 HD_OPTS , cyls, heads, secs,
3116 translation == BIOS_ATA_TRANSLATION_LBA ?
3117 ",trans=lba" :
3118 translation == BIOS_ATA_TRANSLATION_NONE ?
3119 ",trans=none" : "");
3120 drive_add(IF_DEFAULT, 0, optarg, buf);
3121 break;
3122 }
3123 case QEMU_OPTION_hdb:
3124 case QEMU_OPTION_hdc:
3125 case QEMU_OPTION_hdd:
3126 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3127 HD_OPTS);
3128 break;
3129 case QEMU_OPTION_blockdev:
3130 {
3131 Visitor *v;
3132 BlockdevOptions_queue *bdo;
3133
3134 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3135 if (!v) {
3136 error_report_err(err);
3137 exit(1);
3138 }
3139
3140 bdo = g_new(BlockdevOptions_queue, 1);
3141 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3142 &error_fatal);
3143 visit_free(v);
3144 loc_save(&bdo->loc);
3145 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3146 break;
3147 }
3148 case QEMU_OPTION_drive:
3149 if (drive_def(optarg) == NULL) {
3150 exit(1);
3151 }
3152 break;
3153 case QEMU_OPTION_set:
3154 if (qemu_set_option(optarg) != 0)
3155 exit(1);
3156 break;
3157 case QEMU_OPTION_global:
3158 if (qemu_global_option(optarg) != 0)
3159 exit(1);
3160 break;
3161 case QEMU_OPTION_mtdblock:
3162 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3163 break;
3164 case QEMU_OPTION_sd:
3165 drive_add(IF_SD, -1, optarg, SD_OPTS);
3166 break;
3167 case QEMU_OPTION_pflash:
3168 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3169 break;
3170 case QEMU_OPTION_snapshot:
3171 snapshot = 1;
3172 break;
3173 case QEMU_OPTION_hdachs:
3174 {
3175 const char *p;
3176 p = optarg;
3177 cyls = strtol(p, (char **)&p, 0);
3178 if (cyls < 1 || cyls > 16383)
3179 goto chs_fail;
3180 if (*p != ',')
3181 goto chs_fail;
3182 p++;
3183 heads = strtol(p, (char **)&p, 0);
3184 if (heads < 1 || heads > 16)
3185 goto chs_fail;
3186 if (*p != ',')
3187 goto chs_fail;
3188 p++;
3189 secs = strtol(p, (char **)&p, 0);
3190 if (secs < 1 || secs > 63)
3191 goto chs_fail;
3192 if (*p == ',') {
3193 p++;
3194 if (!strcmp(p, "large")) {
3195 translation = BIOS_ATA_TRANSLATION_LARGE;
3196 } else if (!strcmp(p, "rechs")) {
3197 translation = BIOS_ATA_TRANSLATION_RECHS;
3198 } else if (!strcmp(p, "none")) {
3199 translation = BIOS_ATA_TRANSLATION_NONE;
3200 } else if (!strcmp(p, "lba")) {
3201 translation = BIOS_ATA_TRANSLATION_LBA;
3202 } else if (!strcmp(p, "auto")) {
3203 translation = BIOS_ATA_TRANSLATION_AUTO;
3204 } else {
3205 goto chs_fail;
3206 }
3207 } else if (*p != '\0') {
3208 chs_fail:
3209 error_report("invalid physical CHS format");
3210 exit(1);
3211 }
3212 if (hda_opts != NULL) {
3213 qemu_opt_set_number(hda_opts, "cyls", cyls,
3214 &error_abort);
3215 qemu_opt_set_number(hda_opts, "heads", heads,
3216 &error_abort);
3217 qemu_opt_set_number(hda_opts, "secs", secs,
3218 &error_abort);
3219 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3220 qemu_opt_set(hda_opts, "trans", "large",
3221 &error_abort);
3222 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3223 qemu_opt_set(hda_opts, "trans", "rechs",
3224 &error_abort);
3225 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3226 qemu_opt_set(hda_opts, "trans", "lba",
3227 &error_abort);
3228 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3229 qemu_opt_set(hda_opts, "trans", "none",
3230 &error_abort);
3231 }
3232 }
3233 }
3234 break;
3235 case QEMU_OPTION_numa:
3236 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3237 optarg, true);
3238 if (!opts) {
3239 exit(1);
3240 }
3241 break;
3242 case QEMU_OPTION_display:
3243 display_type = select_display(optarg);
3244 break;
3245 case QEMU_OPTION_nographic:
3246 olist = qemu_find_opts("machine");
3247 qemu_opts_parse_noisily(olist, "graphics=off", false);
3248 nographic = true;
3249 display_type = DT_NONE;
3250 break;
3251 case QEMU_OPTION_curses:
3252 #ifdef CONFIG_CURSES
3253 display_type = DT_CURSES;
3254 #else
3255 error_report("curses support is disabled");
3256 exit(1);
3257 #endif
3258 break;
3259 case QEMU_OPTION_portrait:
3260 graphic_rotate = 90;
3261 break;
3262 case QEMU_OPTION_rotate:
3263 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3264 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3265 graphic_rotate != 180 && graphic_rotate != 270) {
3266 error_report("only 90, 180, 270 deg rotation is available");
3267 exit(1);
3268 }
3269 break;
3270 case QEMU_OPTION_kernel:
3271 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3272 &error_abort);
3273 break;
3274 case QEMU_OPTION_initrd:
3275 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3276 &error_abort);
3277 break;
3278 case QEMU_OPTION_append:
3279 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3280 &error_abort);
3281 break;
3282 case QEMU_OPTION_dtb:
3283 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3284 &error_abort);
3285 break;
3286 case QEMU_OPTION_cdrom:
3287 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3288 break;
3289 case QEMU_OPTION_boot:
3290 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3291 optarg, true);
3292 if (!opts) {
3293 exit(1);
3294 }
3295 break;
3296 case QEMU_OPTION_fda:
3297 case QEMU_OPTION_fdb:
3298 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3299 optarg, FD_OPTS);
3300 break;
3301 case QEMU_OPTION_no_fd_bootchk:
3302 fd_bootchk = 0;
3303 break;
3304 case QEMU_OPTION_netdev:
3305 default_net = 0;
3306 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3307 exit(1);
3308 }
3309 break;
3310 case QEMU_OPTION_net:
3311 default_net = 0;
3312 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3313 exit(1);
3314 }
3315 break;
3316 #ifdef CONFIG_LIBISCSI
3317 case QEMU_OPTION_iscsi:
3318 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3319 optarg, false);
3320 if (!opts) {
3321 exit(1);
3322 }
3323 break;
3324 #endif
3325 #ifdef CONFIG_SLIRP
3326 case QEMU_OPTION_tftp:
3327 error_report("The -tftp option is deprecated. "
3328 "Please use '-netdev user,tftp=...' instead.");
3329 legacy_tftp_prefix = optarg;
3330 break;
3331 case QEMU_OPTION_bootp:
3332 error_report("The -bootp option is deprecated. "
3333 "Please use '-netdev user,bootfile=...' instead.");
3334 legacy_bootp_filename = optarg;
3335 break;
3336 case QEMU_OPTION_redir:
3337 error_report("The -redir option is deprecated. "
3338 "Please use '-netdev user,hostfwd=...' instead.");
3339 if (net_slirp_redir(optarg) < 0)
3340 exit(1);
3341 break;
3342 #endif
3343 case QEMU_OPTION_bt:
3344 add_device_config(DEV_BT, optarg);
3345 break;
3346 case QEMU_OPTION_audio_help:
3347 AUD_help ();
3348 exit (0);
3349 break;
3350 case QEMU_OPTION_soundhw:
3351 select_soundhw (optarg);
3352 break;
3353 case QEMU_OPTION_h:
3354 help(0);
3355 break;
3356 case QEMU_OPTION_version:
3357 version();
3358 exit(0);
3359 break;
3360 case QEMU_OPTION_m:
3361 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3362 optarg, true);
3363 if (!opts) {
3364 exit(EXIT_FAILURE);
3365 }
3366 break;
3367 #ifdef CONFIG_TPM
3368 case QEMU_OPTION_tpmdev:
3369 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3370 exit(1);
3371 }
3372 break;
3373 #endif
3374 case QEMU_OPTION_mempath:
3375 mem_path = optarg;
3376 break;
3377 case QEMU_OPTION_mem_prealloc:
3378 mem_prealloc = 1;
3379 break;
3380 case QEMU_OPTION_d:
3381 log_mask = optarg;
3382 break;
3383 case QEMU_OPTION_D:
3384 log_file = optarg;
3385 break;
3386 case QEMU_OPTION_DFILTER:
3387 qemu_set_dfilter_ranges(optarg, &error_fatal);
3388 break;
3389 case QEMU_OPTION_s:
3390 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3391 break;
3392 case QEMU_OPTION_gdb:
3393 add_device_config(DEV_GDB, optarg);
3394 break;
3395 case QEMU_OPTION_L:
3396 if (is_help_option(optarg)) {
3397 list_data_dirs = true;
3398 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3399 data_dir[data_dir_idx++] = optarg;
3400 }
3401 break;
3402 case QEMU_OPTION_bios:
3403 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3404 &error_abort);
3405 break;
3406 case QEMU_OPTION_singlestep:
3407 singlestep = 1;
3408 break;
3409 case QEMU_OPTION_S:
3410 autostart = 0;
3411 break;
3412 case QEMU_OPTION_k:
3413 keyboard_layout = optarg;
3414 break;
3415 case QEMU_OPTION_localtime:
3416 rtc_utc = 0;
3417 break;
3418 case QEMU_OPTION_vga:
3419 vga_model = optarg;
3420 default_vga = 0;
3421 break;
3422 case QEMU_OPTION_g:
3423 {
3424 const char *p;
3425 int w, h, depth;
3426 p = optarg;
3427 w = strtol(p, (char **)&p, 10);
3428 if (w <= 0) {
3429 graphic_error:
3430 error_report("invalid resolution or depth");
3431 exit(1);
3432 }
3433 if (*p != 'x')
3434 goto graphic_error;
3435 p++;
3436 h = strtol(p, (char **)&p, 10);
3437 if (h <= 0)
3438 goto graphic_error;
3439 if (*p == 'x') {
3440 p++;
3441 depth = strtol(p, (char **)&p, 10);
3442 if (depth != 8 && depth != 15 && depth != 16 &&
3443 depth != 24 && depth != 32)
3444 goto graphic_error;
3445 } else if (*p == '\0') {
3446 depth = graphic_depth;
3447 } else {
3448 goto graphic_error;
3449 }
3450
3451 graphic_width = w;
3452 graphic_height = h;
3453 graphic_depth = depth;
3454 }
3455 break;
3456 case QEMU_OPTION_echr:
3457 {
3458 char *r;
3459 term_escape_char = strtol(optarg, &r, 0);
3460 if (r == optarg)
3461 printf("Bad argument to echr\n");
3462 break;
3463 }
3464 case QEMU_OPTION_monitor:
3465 default_monitor = 0;
3466 if (strncmp(optarg, "none", 4)) {
3467 monitor_parse(optarg, "readline", false);
3468 }
3469 break;
3470 case QEMU_OPTION_qmp:
3471 monitor_parse(optarg, "control", false);
3472 default_monitor = 0;
3473 break;
3474 case QEMU_OPTION_qmp_pretty:
3475 monitor_parse(optarg, "control", true);
3476 default_monitor = 0;
3477 break;
3478 case QEMU_OPTION_mon:
3479 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3480 true);
3481 if (!opts) {
3482 exit(1);
3483 }
3484 default_monitor = 0;
3485 break;
3486 case QEMU_OPTION_chardev:
3487 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3488 optarg, true);
3489 if (!opts) {
3490 exit(1);
3491 }
3492 break;
3493 case QEMU_OPTION_fsdev:
3494 olist = qemu_find_opts("fsdev");
3495 if (!olist) {
3496 error_report("fsdev support is disabled");
3497 exit(1);
3498 }
3499 opts = qemu_opts_parse_noisily(olist, optarg, true);
3500 if (!opts) {
3501 exit(1);
3502 }
3503 break;
3504 case QEMU_OPTION_virtfs: {
3505 QemuOpts *fsdev;
3506 QemuOpts