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