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