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