]> git.proxmox.com Git - mirror_qemu.git/blob - vl.c
NUMA: Add numa_info structure to contain numa nodes info
[mirror_qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <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 NodeInfo numa_info[MAX_NODES];
199
200 uint8_t qemu_uuid[16];
201 bool qemu_uuid_set;
202
203 static QEMUBootSetHandler *boot_set_handler;
204 static void *boot_set_opaque;
205
206 static NotifierList exit_notifiers =
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
208
209 static NotifierList machine_init_done_notifiers =
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
211
212 static bool tcg_allowed = true;
213 bool xen_allowed;
214 uint32_t xen_domid;
215 enum xen_mode xen_mode = XEN_EMULATE;
216 static int tcg_tb_size;
217
218 static int has_defaults = 1;
219 static int default_serial = 1;
220 static int default_parallel = 1;
221 static int default_virtcon = 1;
222 static int default_sclp = 1;
223 static int default_monitor = 1;
224 static int default_floppy = 1;
225 static int default_cdrom = 1;
226 static int default_sdcard = 1;
227 static int default_vga = 1;
228
229 static struct {
230 const char *driver;
231 int *flag;
232 } default_list[] = {
233 { .driver = "isa-serial", .flag = &default_serial },
234 { .driver = "isa-parallel", .flag = &default_parallel },
235 { .driver = "isa-fdc", .flag = &default_floppy },
236 { .driver = "ide-cd", .flag = &default_cdrom },
237 { .driver = "ide-hd", .flag = &default_cdrom },
238 { .driver = "ide-drive", .flag = &default_cdrom },
239 { .driver = "scsi-cd", .flag = &default_cdrom },
240 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
241 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
242 { .driver = "virtio-serial", .flag = &default_virtcon },
243 { .driver = "VGA", .flag = &default_vga },
244 { .driver = "isa-vga", .flag = &default_vga },
245 { .driver = "cirrus-vga", .flag = &default_vga },
246 { .driver = "isa-cirrus-vga", .flag = &default_vga },
247 { .driver = "vmware-svga", .flag = &default_vga },
248 { .driver = "qxl-vga", .flag = &default_vga },
249 };
250
251 static QemuOptsList qemu_rtc_opts = {
252 .name = "rtc",
253 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
254 .desc = {
255 {
256 .name = "base",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "clock",
260 .type = QEMU_OPT_STRING,
261 },{
262 .name = "driftfix",
263 .type = QEMU_OPT_STRING,
264 },
265 { /* end of list */ }
266 },
267 };
268
269 static QemuOptsList qemu_sandbox_opts = {
270 .name = "sandbox",
271 .implied_opt_name = "enable",
272 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
273 .desc = {
274 {
275 .name = "enable",
276 .type = QEMU_OPT_BOOL,
277 },
278 { /* end of list */ }
279 },
280 };
281
282 static QemuOptsList qemu_trace_opts = {
283 .name = "trace",
284 .implied_opt_name = "trace",
285 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
286 .desc = {
287 {
288 .name = "events",
289 .type = QEMU_OPT_STRING,
290 },{
291 .name = "file",
292 .type = QEMU_OPT_STRING,
293 },
294 { /* end of list */ }
295 },
296 };
297
298 static QemuOptsList qemu_option_rom_opts = {
299 .name = "option-rom",
300 .implied_opt_name = "romfile",
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
302 .desc = {
303 {
304 .name = "bootindex",
305 .type = QEMU_OPT_NUMBER,
306 }, {
307 .name = "romfile",
308 .type = QEMU_OPT_STRING,
309 },
310 { /* end of list */ }
311 },
312 };
313
314 static QemuOptsList qemu_machine_opts = {
315 .name = "machine",
316 .implied_opt_name = "type",
317 .merge_lists = true,
318 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
319 .desc = {
320 {
321 .name = "type",
322 .type = QEMU_OPT_STRING,
323 .help = "emulated machine"
324 }, {
325 .name = "accel",
326 .type = QEMU_OPT_STRING,
327 .help = "accelerator list",
328 }, {
329 .name = "kernel_irqchip",
330 .type = QEMU_OPT_BOOL,
331 .help = "use KVM in-kernel irqchip",
332 }, {
333 .name = "kvm_shadow_mem",
334 .type = QEMU_OPT_SIZE,
335 .help = "KVM shadow MMU size",
336 }, {
337 .name = "kernel",
338 .type = QEMU_OPT_STRING,
339 .help = "Linux kernel image file",
340 }, {
341 .name = "initrd",
342 .type = QEMU_OPT_STRING,
343 .help = "Linux initial ramdisk file",
344 }, {
345 .name = "append",
346 .type = QEMU_OPT_STRING,
347 .help = "Linux kernel command line",
348 }, {
349 .name = "dtb",
350 .type = QEMU_OPT_STRING,
351 .help = "Linux kernel device tree file",
352 }, {
353 .name = "dumpdtb",
354 .type = QEMU_OPT_STRING,
355 .help = "Dump current dtb to a file and quit",
356 }, {
357 .name = "phandle_start",
358 .type = QEMU_OPT_NUMBER,
359 .help = "The first phandle ID we may generate dynamically",
360 }, {
361 .name = "dt_compatible",
362 .type = QEMU_OPT_STRING,
363 .help = "Overrides the \"compatible\" property of the dt root node",
364 }, {
365 .name = "dump-guest-core",
366 .type = QEMU_OPT_BOOL,
367 .help = "Include guest memory in a core dump",
368 }, {
369 .name = "mem-merge",
370 .type = QEMU_OPT_BOOL,
371 .help = "enable/disable memory merge support",
372 },{
373 .name = "usb",
374 .type = QEMU_OPT_BOOL,
375 .help = "Set on/off to enable/disable usb",
376 },{
377 .name = "firmware",
378 .type = QEMU_OPT_STRING,
379 .help = "firmware image",
380 },{
381 .name = "kvm-type",
382 .type = QEMU_OPT_STRING,
383 .help = "Specifies the KVM virtualization mode (HV, PR)",
384 },
385 { /* End of list */ }
386 },
387 };
388
389 static QemuOptsList qemu_boot_opts = {
390 .name = "boot-opts",
391 .implied_opt_name = "order",
392 .merge_lists = true,
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
394 .desc = {
395 {
396 .name = "order",
397 .type = QEMU_OPT_STRING,
398 }, {
399 .name = "once",
400 .type = QEMU_OPT_STRING,
401 }, {
402 .name = "menu",
403 .type = QEMU_OPT_BOOL,
404 }, {
405 .name = "splash",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "splash-time",
409 .type = QEMU_OPT_STRING,
410 }, {
411 .name = "reboot-timeout",
412 .type = QEMU_OPT_STRING,
413 }, {
414 .name = "strict",
415 .type = QEMU_OPT_BOOL,
416 },
417 { /*End of list */ }
418 },
419 };
420
421 static QemuOptsList qemu_add_fd_opts = {
422 .name = "add-fd",
423 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
424 .desc = {
425 {
426 .name = "fd",
427 .type = QEMU_OPT_NUMBER,
428 .help = "file descriptor of which a duplicate is added to fd set",
429 },{
430 .name = "set",
431 .type = QEMU_OPT_NUMBER,
432 .help = "ID of the fd set to add fd to",
433 },{
434 .name = "opaque",
435 .type = QEMU_OPT_STRING,
436 .help = "free-form string used to describe fd",
437 },
438 { /* end of list */ }
439 },
440 };
441
442 static QemuOptsList qemu_object_opts = {
443 .name = "object",
444 .implied_opt_name = "qom-type",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
446 .desc = {
447 { }
448 },
449 };
450
451 static QemuOptsList qemu_tpmdev_opts = {
452 .name = "tpmdev",
453 .implied_opt_name = "type",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
455 .desc = {
456 /* options are defined in the TPM backends */
457 { /* end of list */ }
458 },
459 };
460
461 static QemuOptsList qemu_realtime_opts = {
462 .name = "realtime",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
464 .desc = {
465 {
466 .name = "mlock",
467 .type = QEMU_OPT_BOOL,
468 },
469 { /* end of list */ }
470 },
471 };
472
473 static QemuOptsList qemu_msg_opts = {
474 .name = "msg",
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
476 .desc = {
477 {
478 .name = "timestamp",
479 .type = QEMU_OPT_BOOL,
480 },
481 { /* end of list */ }
482 },
483 };
484
485 static QemuOptsList qemu_name_opts = {
486 .name = "name",
487 .implied_opt_name = "guest",
488 .merge_lists = true,
489 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
490 .desc = {
491 {
492 .name = "guest",
493 .type = QEMU_OPT_STRING,
494 .help = "Sets the name of the guest.\n"
495 "This name will be displayed in the SDL window caption.\n"
496 "The name will also be used for the VNC server",
497 }, {
498 .name = "process",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the name of the QEMU process, as shown in top etc",
501 }, {
502 .name = "debug-threads",
503 .type = QEMU_OPT_BOOL,
504 .help = "When enabled, name the individual threads; defaults off.\n"
505 "NOTE: The thread names are for debugging and not a\n"
506 "stable API.",
507 },
508 { /* End of list */ }
509 },
510 };
511
512 static QemuOptsList qemu_mem_opts = {
513 .name = "memory",
514 .implied_opt_name = "size",
515 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
516 .merge_lists = true,
517 .desc = {
518 {
519 .name = "size",
520 .type = QEMU_OPT_SIZE,
521 },
522 {
523 .name = "slots",
524 .type = QEMU_OPT_NUMBER,
525 },
526 {
527 .name = "maxmem",
528 .type = QEMU_OPT_SIZE,
529 },
530 { /* end of list */ }
531 },
532 };
533
534 /**
535 * Get machine options
536 *
537 * Returns: machine options (never null).
538 */
539 QemuOpts *qemu_get_machine_opts(void)
540 {
541 return qemu_find_opts_singleton("machine");
542 }
543
544 const char *qemu_get_vm_name(void)
545 {
546 return qemu_name;
547 }
548
549 static void res_free(void)
550 {
551 if (boot_splash_filedata != NULL) {
552 g_free(boot_splash_filedata);
553 boot_splash_filedata = NULL;
554 }
555 }
556
557 static int default_driver_check(QemuOpts *opts, void *opaque)
558 {
559 const char *driver = qemu_opt_get(opts, "driver");
560 int i;
561
562 if (!driver)
563 return 0;
564 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
565 if (strcmp(default_list[i].driver, driver) != 0)
566 continue;
567 *(default_list[i].flag) = 0;
568 }
569 return 0;
570 }
571
572 /***********************************************************/
573 /* QEMU state */
574
575 static RunState current_run_state = RUN_STATE_PRELAUNCH;
576
577 typedef struct {
578 RunState from;
579 RunState to;
580 } RunStateTransition;
581
582 static const RunStateTransition runstate_transitions_def[] = {
583 /* from -> to */
584 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
585 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
586
587 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
588 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
589
590 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
591 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
592
593 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
594 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
595
596 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
597 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
598
599 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
600 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
601
602 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
603 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
604 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
605
606 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
607 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
608
609 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
610
611 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
612 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
613 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
614 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
615 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
617 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
618 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
619 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
620 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
621
622 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
623
624 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
625 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
626
627 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
628 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
629 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
630 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
631
632 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
633 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
634
635 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
636 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
637
638 { RUN_STATE_MAX, RUN_STATE_MAX },
639 };
640
641 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
642
643 bool runstate_check(RunState state)
644 {
645 return current_run_state == state;
646 }
647
648 static void runstate_init(void)
649 {
650 const RunStateTransition *p;
651
652 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
653
654 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
655 runstate_valid_transitions[p->from][p->to] = true;
656 }
657 }
658
659 /* This function will abort() on invalid state transitions */
660 void runstate_set(RunState new_state)
661 {
662 assert(new_state < RUN_STATE_MAX);
663
664 if (!runstate_valid_transitions[current_run_state][new_state]) {
665 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
666 RunState_lookup[current_run_state],
667 RunState_lookup[new_state]);
668 abort();
669 }
670 trace_runstate_set(new_state);
671 current_run_state = new_state;
672 }
673
674 int runstate_is_running(void)
675 {
676 return runstate_check(RUN_STATE_RUNNING);
677 }
678
679 bool runstate_needs_reset(void)
680 {
681 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
682 runstate_check(RUN_STATE_SHUTDOWN);
683 }
684
685 StatusInfo *qmp_query_status(Error **errp)
686 {
687 StatusInfo *info = g_malloc0(sizeof(*info));
688
689 info->running = runstate_is_running();
690 info->singlestep = singlestep;
691 info->status = current_run_state;
692
693 return info;
694 }
695
696 /***********************************************************/
697 /* real time host monotonic timer */
698
699 /***********************************************************/
700 /* host time/date access */
701 void qemu_get_timedate(struct tm *tm, int offset)
702 {
703 time_t ti;
704
705 time(&ti);
706 ti += offset;
707 if (rtc_date_offset == -1) {
708 if (rtc_utc)
709 gmtime_r(&ti, tm);
710 else
711 localtime_r(&ti, tm);
712 } else {
713 ti -= rtc_date_offset;
714 gmtime_r(&ti, tm);
715 }
716 }
717
718 int qemu_timedate_diff(struct tm *tm)
719 {
720 time_t seconds;
721
722 if (rtc_date_offset == -1)
723 if (rtc_utc)
724 seconds = mktimegm(tm);
725 else {
726 struct tm tmp = *tm;
727 tmp.tm_isdst = -1; /* use timezone to figure it out */
728 seconds = mktime(&tmp);
729 }
730 else
731 seconds = mktimegm(tm) + rtc_date_offset;
732
733 return seconds - time(NULL);
734 }
735
736 void rtc_change_mon_event(struct tm *tm)
737 {
738 QObject *data;
739
740 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
741 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
742 qobject_decref(data);
743 }
744
745 static void configure_rtc_date_offset(const char *startdate, int legacy)
746 {
747 time_t rtc_start_date;
748 struct tm tm;
749
750 if (!strcmp(startdate, "now") && legacy) {
751 rtc_date_offset = -1;
752 } else {
753 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
754 &tm.tm_year,
755 &tm.tm_mon,
756 &tm.tm_mday,
757 &tm.tm_hour,
758 &tm.tm_min,
759 &tm.tm_sec) == 6) {
760 /* OK */
761 } else if (sscanf(startdate, "%d-%d-%d",
762 &tm.tm_year,
763 &tm.tm_mon,
764 &tm.tm_mday) == 3) {
765 tm.tm_hour = 0;
766 tm.tm_min = 0;
767 tm.tm_sec = 0;
768 } else {
769 goto date_fail;
770 }
771 tm.tm_year -= 1900;
772 tm.tm_mon--;
773 rtc_start_date = mktimegm(&tm);
774 if (rtc_start_date == -1) {
775 date_fail:
776 fprintf(stderr, "Invalid date format. Valid formats are:\n"
777 "'2006-06-17T16:01:21' or '2006-06-17'\n");
778 exit(1);
779 }
780 rtc_date_offset = time(NULL) - rtc_start_date;
781 }
782 }
783
784 static void configure_rtc(QemuOpts *opts)
785 {
786 const char *value;
787
788 value = qemu_opt_get(opts, "base");
789 if (value) {
790 if (!strcmp(value, "utc")) {
791 rtc_utc = 1;
792 } else if (!strcmp(value, "localtime")) {
793 rtc_utc = 0;
794 } else {
795 configure_rtc_date_offset(value, 0);
796 }
797 }
798 value = qemu_opt_get(opts, "clock");
799 if (value) {
800 if (!strcmp(value, "host")) {
801 rtc_clock = QEMU_CLOCK_HOST;
802 } else if (!strcmp(value, "rt")) {
803 rtc_clock = QEMU_CLOCK_REALTIME;
804 } else if (!strcmp(value, "vm")) {
805 rtc_clock = QEMU_CLOCK_VIRTUAL;
806 } else {
807 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
808 exit(1);
809 }
810 }
811 value = qemu_opt_get(opts, "driftfix");
812 if (value) {
813 if (!strcmp(value, "slew")) {
814 static GlobalProperty slew_lost_ticks[] = {
815 {
816 .driver = "mc146818rtc",
817 .property = "lost_tick_policy",
818 .value = "slew",
819 },
820 { /* end of list */ }
821 };
822
823 qdev_prop_register_global_list(slew_lost_ticks);
824 } else if (!strcmp(value, "none")) {
825 /* discard is default */
826 } else {
827 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
828 exit(1);
829 }
830 }
831 }
832
833 /***********************************************************/
834 /* Bluetooth support */
835 static int nb_hcis;
836 static int cur_hci;
837 static struct HCIInfo *hci_table[MAX_NICS];
838
839 struct HCIInfo *qemu_next_hci(void)
840 {
841 if (cur_hci == nb_hcis)
842 return &null_hci;
843
844 return hci_table[cur_hci++];
845 }
846
847 static int bt_hci_parse(const char *str)
848 {
849 struct HCIInfo *hci;
850 bdaddr_t bdaddr;
851
852 if (nb_hcis >= MAX_NICS) {
853 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
854 return -1;
855 }
856
857 hci = hci_init(str);
858 if (!hci)
859 return -1;
860
861 bdaddr.b[0] = 0x52;
862 bdaddr.b[1] = 0x54;
863 bdaddr.b[2] = 0x00;
864 bdaddr.b[3] = 0x12;
865 bdaddr.b[4] = 0x34;
866 bdaddr.b[5] = 0x56 + nb_hcis;
867 hci->bdaddr_set(hci, bdaddr.b);
868
869 hci_table[nb_hcis++] = hci;
870
871 return 0;
872 }
873
874 static void bt_vhci_add(int vlan_id)
875 {
876 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
877
878 if (!vlan->slave)
879 fprintf(stderr, "qemu: warning: adding a VHCI to "
880 "an empty scatternet %i\n", vlan_id);
881
882 bt_vhci_init(bt_new_hci(vlan));
883 }
884
885 static struct bt_device_s *bt_device_add(const char *opt)
886 {
887 struct bt_scatternet_s *vlan;
888 int vlan_id = 0;
889 char *endp = strstr(opt, ",vlan=");
890 int len = (endp ? endp - opt : strlen(opt)) + 1;
891 char devname[10];
892
893 pstrcpy(devname, MIN(sizeof(devname), len), opt);
894
895 if (endp) {
896 vlan_id = strtol(endp + 6, &endp, 0);
897 if (*endp) {
898 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
899 return 0;
900 }
901 }
902
903 vlan = qemu_find_bt_vlan(vlan_id);
904
905 if (!vlan->slave)
906 fprintf(stderr, "qemu: warning: adding a slave device to "
907 "an empty scatternet %i\n", vlan_id);
908
909 if (!strcmp(devname, "keyboard"))
910 return bt_keyboard_init(vlan);
911
912 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
913 return 0;
914 }
915
916 static int bt_parse(const char *opt)
917 {
918 const char *endp, *p;
919 int vlan;
920
921 if (strstart(opt, "hci", &endp)) {
922 if (!*endp || *endp == ',') {
923 if (*endp)
924 if (!strstart(endp, ",vlan=", 0))
925 opt = endp + 1;
926
927 return bt_hci_parse(opt);
928 }
929 } else if (strstart(opt, "vhci", &endp)) {
930 if (!*endp || *endp == ',') {
931 if (*endp) {
932 if (strstart(endp, ",vlan=", &p)) {
933 vlan = strtol(p, (char **) &endp, 0);
934 if (*endp) {
935 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
936 return 1;
937 }
938 } else {
939 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
940 return 1;
941 }
942 } else
943 vlan = 0;
944
945 bt_vhci_add(vlan);
946 return 0;
947 }
948 } else if (strstart(opt, "device:", &endp))
949 return !bt_device_add(endp);
950
951 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
952 return 1;
953 }
954
955 static int parse_sandbox(QemuOpts *opts, void *opaque)
956 {
957 /* FIXME: change this to true for 1.3 */
958 if (qemu_opt_get_bool(opts, "enable", false)) {
959 #ifdef CONFIG_SECCOMP
960 if (seccomp_start() < 0) {
961 qerror_report(ERROR_CLASS_GENERIC_ERROR,
962 "failed to install seccomp syscall filter in the kernel");
963 return -1;
964 }
965 #else
966 qerror_report(ERROR_CLASS_GENERIC_ERROR,
967 "sandboxing request but seccomp is not compiled into this build");
968 return -1;
969 #endif
970 }
971
972 return 0;
973 }
974
975 static int parse_name(QemuOpts *opts, void *opaque)
976 {
977 const char *proc_name;
978
979 if (qemu_opt_get(opts, "debug-threads")) {
980 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
981 }
982 qemu_name = qemu_opt_get(opts, "guest");
983
984 proc_name = qemu_opt_get(opts, "process");
985 if (proc_name) {
986 os_set_proc_name(proc_name);
987 }
988
989 return 0;
990 }
991
992 bool usb_enabled(bool default_usb)
993 {
994 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
995 has_defaults && default_usb);
996 }
997
998 #ifndef _WIN32
999 static int parse_add_fd(QemuOpts *opts, void *opaque)
1000 {
1001 int fd, dupfd, flags;
1002 int64_t fdset_id;
1003 const char *fd_opaque = NULL;
1004
1005 fd = qemu_opt_get_number(opts, "fd", -1);
1006 fdset_id = qemu_opt_get_number(opts, "set", -1);
1007 fd_opaque = qemu_opt_get(opts, "opaque");
1008
1009 if (fd < 0) {
1010 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1011 "fd option is required and must be non-negative");
1012 return -1;
1013 }
1014
1015 if (fd <= STDERR_FILENO) {
1016 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1017 "fd cannot be a standard I/O stream");
1018 return -1;
1019 }
1020
1021 /*
1022 * All fds inherited across exec() necessarily have FD_CLOEXEC
1023 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1024 */
1025 flags = fcntl(fd, F_GETFD);
1026 if (flags == -1 || (flags & FD_CLOEXEC)) {
1027 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1028 "fd is not valid or already in use");
1029 return -1;
1030 }
1031
1032 if (fdset_id < 0) {
1033 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1034 "set option is required and must be non-negative");
1035 return -1;
1036 }
1037
1038 #ifdef F_DUPFD_CLOEXEC
1039 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1040 #else
1041 dupfd = dup(fd);
1042 if (dupfd != -1) {
1043 qemu_set_cloexec(dupfd);
1044 }
1045 #endif
1046 if (dupfd == -1) {
1047 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1048 "Error duplicating fd: %s", strerror(errno));
1049 return -1;
1050 }
1051
1052 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1053 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1054 fd_opaque, NULL);
1055
1056 return 0;
1057 }
1058
1059 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1060 {
1061 int fd;
1062
1063 fd = qemu_opt_get_number(opts, "fd", -1);
1064 close(fd);
1065
1066 return 0;
1067 }
1068 #endif
1069
1070 /***********************************************************/
1071 /* QEMU Block devices */
1072
1073 #define HD_OPTS "media=disk"
1074 #define CDROM_OPTS "media=cdrom"
1075 #define FD_OPTS ""
1076 #define PFLASH_OPTS ""
1077 #define MTD_OPTS ""
1078 #define SD_OPTS ""
1079
1080 static int drive_init_func(QemuOpts *opts, void *opaque)
1081 {
1082 BlockInterfaceType *block_default_type = opaque;
1083
1084 return drive_new(opts, *block_default_type) == NULL;
1085 }
1086
1087 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1088 {
1089 if (NULL == qemu_opt_get(opts, "snapshot")) {
1090 qemu_opt_set(opts, "snapshot", "on");
1091 }
1092 return 0;
1093 }
1094
1095 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1096 int index, const char *optstr)
1097 {
1098 QemuOpts *opts;
1099
1100 if (!enable || drive_get_by_index(type, index)) {
1101 return;
1102 }
1103
1104 opts = drive_add(type, index, NULL, optstr);
1105 if (snapshot) {
1106 drive_enable_snapshot(opts, NULL);
1107 }
1108 if (!drive_new(opts, type)) {
1109 exit(1);
1110 }
1111 }
1112
1113 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1114 {
1115 boot_set_handler = func;
1116 boot_set_opaque = opaque;
1117 }
1118
1119 int qemu_boot_set(const char *boot_order)
1120 {
1121 if (!boot_set_handler) {
1122 return -EINVAL;
1123 }
1124 return boot_set_handler(boot_set_opaque, boot_order);
1125 }
1126
1127 static void validate_bootdevices(const char *devices)
1128 {
1129 /* We just do some generic consistency checks */
1130 const char *p;
1131 int bitmap = 0;
1132
1133 for (p = devices; *p != '\0'; p++) {
1134 /* Allowed boot devices are:
1135 * a-b: floppy disk drives
1136 * c-f: IDE disk drives
1137 * g-m: machine implementation dependent drives
1138 * n-p: network devices
1139 * It's up to each machine implementation to check if the given boot
1140 * devices match the actual hardware implementation and firmware
1141 * features.
1142 */
1143 if (*p < 'a' || *p > 'p') {
1144 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1145 exit(1);
1146 }
1147 if (bitmap & (1 << (*p - 'a'))) {
1148 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1149 exit(1);
1150 }
1151 bitmap |= 1 << (*p - 'a');
1152 }
1153 }
1154
1155 static void restore_boot_order(void *opaque)
1156 {
1157 char *normal_boot_order = opaque;
1158 static int first = 1;
1159
1160 /* Restore boot order and remove ourselves after the first boot */
1161 if (first) {
1162 first = 0;
1163 return;
1164 }
1165
1166 qemu_boot_set(normal_boot_order);
1167
1168 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1169 g_free(normal_boot_order);
1170 }
1171
1172 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1173 const char *suffix)
1174 {
1175 FWBootEntry *node, *i;
1176
1177 if (bootindex < 0) {
1178 return;
1179 }
1180
1181 assert(dev != NULL || suffix != NULL);
1182
1183 node = g_malloc0(sizeof(FWBootEntry));
1184 node->bootindex = bootindex;
1185 node->suffix = g_strdup(suffix);
1186 node->dev = dev;
1187
1188 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1189 if (i->bootindex == bootindex) {
1190 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1191 exit(1);
1192 } else if (i->bootindex < bootindex) {
1193 continue;
1194 }
1195 QTAILQ_INSERT_BEFORE(i, node, link);
1196 return;
1197 }
1198 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1199 }
1200
1201 DeviceState *get_boot_device(uint32_t position)
1202 {
1203 uint32_t counter = 0;
1204 FWBootEntry *i = NULL;
1205 DeviceState *res = NULL;
1206
1207 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1208 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1209 if (counter == position) {
1210 res = i->dev;
1211 break;
1212 }
1213 counter++;
1214 }
1215 }
1216 return res;
1217 }
1218
1219 /*
1220 * This function returns null terminated string that consist of new line
1221 * separated device paths.
1222 *
1223 * memory pointed by "size" is assigned total length of the array in bytes
1224 *
1225 */
1226 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1227 {
1228 FWBootEntry *i;
1229 size_t total = 0;
1230 char *list = NULL;
1231
1232 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1233 char *devpath = NULL, *bootpath;
1234 size_t len;
1235
1236 if (i->dev) {
1237 devpath = qdev_get_fw_dev_path(i->dev);
1238 assert(devpath);
1239 }
1240
1241 if (i->suffix && !ignore_suffixes && devpath) {
1242 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1243
1244 bootpath = g_malloc(bootpathlen);
1245 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1246 g_free(devpath);
1247 } else if (devpath) {
1248 bootpath = devpath;
1249 } else if (!ignore_suffixes) {
1250 assert(i->suffix);
1251 bootpath = g_strdup(i->suffix);
1252 } else {
1253 bootpath = g_strdup("");
1254 }
1255
1256 if (total) {
1257 list[total-1] = '\n';
1258 }
1259 len = strlen(bootpath) + 1;
1260 list = g_realloc(list, total + len);
1261 memcpy(&list[total], bootpath, len);
1262 total += len;
1263 g_free(bootpath);
1264 }
1265
1266 *size = total;
1267
1268 if (boot_strict && *size > 0) {
1269 list[total-1] = '\n';
1270 list = g_realloc(list, total + 5);
1271 memcpy(&list[total], "HALT", 5);
1272 *size = total + 5;
1273 }
1274 return list;
1275 }
1276
1277 static QemuOptsList qemu_smp_opts = {
1278 .name = "smp-opts",
1279 .implied_opt_name = "cpus",
1280 .merge_lists = true,
1281 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1282 .desc = {
1283 {
1284 .name = "cpus",
1285 .type = QEMU_OPT_NUMBER,
1286 }, {
1287 .name = "sockets",
1288 .type = QEMU_OPT_NUMBER,
1289 }, {
1290 .name = "cores",
1291 .type = QEMU_OPT_NUMBER,
1292 }, {
1293 .name = "threads",
1294 .type = QEMU_OPT_NUMBER,
1295 }, {
1296 .name = "maxcpus",
1297 .type = QEMU_OPT_NUMBER,
1298 },
1299 { /*End of list */ }
1300 },
1301 };
1302
1303 static void smp_parse(QemuOpts *opts)
1304 {
1305 if (opts) {
1306
1307 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1308 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1309 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1310 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1311
1312 /* compute missing values, prefer sockets over cores over threads */
1313 if (cpus == 0 || sockets == 0) {
1314 sockets = sockets > 0 ? sockets : 1;
1315 cores = cores > 0 ? cores : 1;
1316 threads = threads > 0 ? threads : 1;
1317 if (cpus == 0) {
1318 cpus = cores * threads * sockets;
1319 }
1320 } else {
1321 if (cores == 0) {
1322 threads = threads > 0 ? threads : 1;
1323 cores = cpus / (sockets * threads);
1324 } else {
1325 threads = cpus / (cores * sockets);
1326 }
1327 }
1328
1329 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1330
1331 smp_cpus = cpus;
1332 smp_cores = cores > 0 ? cores : 1;
1333 smp_threads = threads > 0 ? threads : 1;
1334
1335 }
1336
1337 if (max_cpus == 0) {
1338 max_cpus = smp_cpus;
1339 }
1340
1341 if (max_cpus > MAX_CPUMASK_BITS) {
1342 fprintf(stderr, "Unsupported number of maxcpus\n");
1343 exit(1);
1344 }
1345 if (max_cpus < smp_cpus) {
1346 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1347 exit(1);
1348 }
1349
1350 }
1351
1352 static void configure_realtime(QemuOpts *opts)
1353 {
1354 bool enable_mlock;
1355
1356 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1357
1358 if (enable_mlock) {
1359 if (os_mlock() < 0) {
1360 fprintf(stderr, "qemu: locking memory failed\n");
1361 exit(1);
1362 }
1363 }
1364 }
1365
1366
1367 static void configure_msg(QemuOpts *opts)
1368 {
1369 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1370 }
1371
1372 /***********************************************************/
1373 /* USB devices */
1374
1375 static int usb_device_add(const char *devname)
1376 {
1377 USBDevice *dev = NULL;
1378 #ifndef CONFIG_LINUX
1379 const char *p;
1380 #endif
1381
1382 if (!usb_enabled(false)) {
1383 return -1;
1384 }
1385
1386 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1387 dev = usbdevice_create(devname);
1388 if (dev)
1389 goto done;
1390
1391 /* the other ones */
1392 #ifndef CONFIG_LINUX
1393 /* only the linux version is qdev-ified, usb-bsd still needs this */
1394 if (strstart(devname, "host:", &p)) {
1395 dev = usb_host_device_open(usb_bus_find(-1), p);
1396 }
1397 #endif
1398 if (!dev)
1399 return -1;
1400
1401 done:
1402 return 0;
1403 }
1404
1405 static int usb_device_del(const char *devname)
1406 {
1407 int bus_num, addr;
1408 const char *p;
1409
1410 if (strstart(devname, "host:", &p)) {
1411 return -1;
1412 }
1413
1414 if (!usb_enabled(false)) {
1415 return -1;
1416 }
1417
1418 p = strchr(devname, '.');
1419 if (!p)
1420 return -1;
1421 bus_num = strtoul(devname, NULL, 0);
1422 addr = strtoul(p + 1, NULL, 0);
1423
1424 return usb_device_delete_addr(bus_num, addr);
1425 }
1426
1427 static int usb_parse(const char *cmdline)
1428 {
1429 int r;
1430 r = usb_device_add(cmdline);
1431 if (r < 0) {
1432 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1433 }
1434 return r;
1435 }
1436
1437 void do_usb_add(Monitor *mon, const QDict *qdict)
1438 {
1439 const char *devname = qdict_get_str(qdict, "devname");
1440 if (usb_device_add(devname) < 0) {
1441 error_report("could not add USB device '%s'", devname);
1442 }
1443 }
1444
1445 void do_usb_del(Monitor *mon, const QDict *qdict)
1446 {
1447 const char *devname = qdict_get_str(qdict, "devname");
1448 if (usb_device_del(devname) < 0) {
1449 error_report("could not delete USB device '%s'", devname);
1450 }
1451 }
1452
1453 /***********************************************************/
1454 /* PCMCIA/Cardbus */
1455
1456 static struct pcmcia_socket_entry_s {
1457 PCMCIASocket *socket;
1458 struct pcmcia_socket_entry_s *next;
1459 } *pcmcia_sockets = 0;
1460
1461 void pcmcia_socket_register(PCMCIASocket *socket)
1462 {
1463 struct pcmcia_socket_entry_s *entry;
1464
1465 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1466 entry->socket = socket;
1467 entry->next = pcmcia_sockets;
1468 pcmcia_sockets = entry;
1469 }
1470
1471 void pcmcia_socket_unregister(PCMCIASocket *socket)
1472 {
1473 struct pcmcia_socket_entry_s *entry, **ptr;
1474
1475 ptr = &pcmcia_sockets;
1476 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1477 if (entry->socket == socket) {
1478 *ptr = entry->next;
1479 g_free(entry);
1480 }
1481 }
1482
1483 void pcmcia_info(Monitor *mon, const QDict *qdict)
1484 {
1485 struct pcmcia_socket_entry_s *iter;
1486
1487 if (!pcmcia_sockets)
1488 monitor_printf(mon, "No PCMCIA sockets\n");
1489
1490 for (iter = pcmcia_sockets; iter; iter = iter->next)
1491 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1492 iter->socket->attached ? iter->socket->card_string :
1493 "Empty");
1494 }
1495
1496 /***********************************************************/
1497 /* machine registration */
1498
1499 MachineState *current_machine;
1500
1501 static void machine_class_init(ObjectClass *oc, void *data)
1502 {
1503 MachineClass *mc = MACHINE_CLASS(oc);
1504 QEMUMachine *qm = data;
1505
1506 mc->name = qm->name;
1507 mc->alias = qm->alias;
1508 mc->desc = qm->desc;
1509 mc->init = qm->init;
1510 mc->reset = qm->reset;
1511 mc->hot_add_cpu = qm->hot_add_cpu;
1512 mc->kvm_type = qm->kvm_type;
1513 mc->block_default_type = qm->block_default_type;
1514 mc->max_cpus = qm->max_cpus;
1515 mc->no_serial = qm->no_serial;
1516 mc->no_parallel = qm->no_parallel;
1517 mc->use_virtcon = qm->use_virtcon;
1518 mc->use_sclp = qm->use_sclp;
1519 mc->no_floppy = qm->no_floppy;
1520 mc->no_cdrom = qm->no_cdrom;
1521 mc->no_sdcard = qm->no_sdcard;
1522 mc->is_default = qm->is_default;
1523 mc->default_machine_opts = qm->default_machine_opts;
1524 mc->default_boot_order = qm->default_boot_order;
1525 mc->compat_props = qm->compat_props;
1526 mc->hw_version = qm->hw_version;
1527 }
1528
1529 int qemu_register_machine(QEMUMachine *m)
1530 {
1531 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1532 TypeInfo ti = {
1533 .name = name,
1534 .parent = TYPE_MACHINE,
1535 .class_init = machine_class_init,
1536 .class_data = (void *)m,
1537 };
1538
1539 type_register(&ti);
1540 g_free(name);
1541
1542 return 0;
1543 }
1544
1545 static MachineClass *find_machine(const char *name)
1546 {
1547 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1548 MachineClass *mc = NULL;
1549
1550 for (el = machines; el; el = el->next) {
1551 MachineClass *temp = el->data;
1552
1553 if (!strcmp(temp->name, name)) {
1554 mc = temp;
1555 break;
1556 }
1557 if (temp->alias &&
1558 !strcmp(temp->alias, name)) {
1559 mc = temp;
1560 break;
1561 }
1562 }
1563
1564 g_slist_free(machines);
1565 return mc;
1566 }
1567
1568 MachineClass *find_default_machine(void)
1569 {
1570 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1571 MachineClass *mc = NULL;
1572
1573 for (el = machines; el; el = el->next) {
1574 MachineClass *temp = el->data;
1575
1576 if (temp->is_default) {
1577 mc = temp;
1578 break;
1579 }
1580 }
1581
1582 g_slist_free(machines);
1583 return mc;
1584 }
1585
1586 MachineInfoList *qmp_query_machines(Error **errp)
1587 {
1588 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1589 MachineInfoList *mach_list = NULL;
1590
1591 for (el = machines; el; el = el->next) {
1592 MachineClass *mc = el->data;
1593 MachineInfoList *entry;
1594 MachineInfo *info;
1595
1596 info = g_malloc0(sizeof(*info));
1597 if (mc->is_default) {
1598 info->has_is_default = true;
1599 info->is_default = true;
1600 }
1601
1602 if (mc->alias) {
1603 info->has_alias = true;
1604 info->alias = g_strdup(mc->alias);
1605 }
1606
1607 info->name = g_strdup(mc->name);
1608 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1609
1610 entry = g_malloc0(sizeof(*entry));
1611 entry->value = info;
1612 entry->next = mach_list;
1613 mach_list = entry;
1614 }
1615
1616 g_slist_free(machines);
1617 return mach_list;
1618 }
1619
1620 /***********************************************************/
1621 /* main execution loop */
1622
1623 struct vm_change_state_entry {
1624 VMChangeStateHandler *cb;
1625 void *opaque;
1626 QLIST_ENTRY (vm_change_state_entry) entries;
1627 };
1628
1629 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1630
1631 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1632 void *opaque)
1633 {
1634 VMChangeStateEntry *e;
1635
1636 e = g_malloc0(sizeof (*e));
1637
1638 e->cb = cb;
1639 e->opaque = opaque;
1640 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1641 return e;
1642 }
1643
1644 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1645 {
1646 QLIST_REMOVE (e, entries);
1647 g_free (e);
1648 }
1649
1650 void vm_state_notify(int running, RunState state)
1651 {
1652 VMChangeStateEntry *e;
1653
1654 trace_vm_state_notify(running, state);
1655
1656 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1657 e->cb(e->opaque, running, state);
1658 }
1659 }
1660
1661 void vm_start(void)
1662 {
1663 if (!runstate_is_running()) {
1664 cpu_enable_ticks();
1665 runstate_set(RUN_STATE_RUNNING);
1666 vm_state_notify(1, RUN_STATE_RUNNING);
1667 resume_all_vcpus();
1668 monitor_protocol_event(QEVENT_RESUME, NULL);
1669 }
1670 }
1671
1672 /* reset/shutdown handler */
1673
1674 typedef struct QEMUResetEntry {
1675 QTAILQ_ENTRY(QEMUResetEntry) entry;
1676 QEMUResetHandler *func;
1677 void *opaque;
1678 } QEMUResetEntry;
1679
1680 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1681 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1682 static int reset_requested;
1683 static int shutdown_requested, shutdown_signal = -1;
1684 static pid_t shutdown_pid;
1685 static int powerdown_requested;
1686 static int debug_requested;
1687 static int suspend_requested;
1688 static WakeupReason wakeup_reason;
1689 static NotifierList powerdown_notifiers =
1690 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1691 static NotifierList suspend_notifiers =
1692 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1693 static NotifierList wakeup_notifiers =
1694 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1695 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1696 static RunState vmstop_requested = RUN_STATE_MAX;
1697
1698 int qemu_shutdown_requested_get(void)
1699 {
1700 return shutdown_requested;
1701 }
1702
1703 int qemu_reset_requested_get(void)
1704 {
1705 return reset_requested;
1706 }
1707
1708 static int qemu_shutdown_requested(void)
1709 {
1710 int r = shutdown_requested;
1711 shutdown_requested = 0;
1712 return r;
1713 }
1714
1715 static void qemu_kill_report(void)
1716 {
1717 if (!qtest_driver() && shutdown_signal != -1) {
1718 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1719 if (shutdown_pid == 0) {
1720 /* This happens for eg ^C at the terminal, so it's worth
1721 * avoiding printing an odd message in that case.
1722 */
1723 fputc('\n', stderr);
1724 } else {
1725 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1726 }
1727 shutdown_signal = -1;
1728 }
1729 }
1730
1731 static int qemu_reset_requested(void)
1732 {
1733 int r = reset_requested;
1734 reset_requested = 0;
1735 return r;
1736 }
1737
1738 static int qemu_suspend_requested(void)
1739 {
1740 int r = suspend_requested;
1741 suspend_requested = 0;
1742 return r;
1743 }
1744
1745 static WakeupReason qemu_wakeup_requested(void)
1746 {
1747 return wakeup_reason;
1748 }
1749
1750 static int qemu_powerdown_requested(void)
1751 {
1752 int r = powerdown_requested;
1753 powerdown_requested = 0;
1754 return r;
1755 }
1756
1757 static int qemu_debug_requested(void)
1758 {
1759 int r = debug_requested;
1760 debug_requested = 0;
1761 return r;
1762 }
1763
1764 /* We use RUN_STATE_MAX but any invalid value will do */
1765 static bool qemu_vmstop_requested(RunState *r)
1766 {
1767 if (vmstop_requested < RUN_STATE_MAX) {
1768 *r = vmstop_requested;
1769 vmstop_requested = RUN_STATE_MAX;
1770 return true;
1771 }
1772
1773 return false;
1774 }
1775
1776 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1777 {
1778 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1779
1780 re->func = func;
1781 re->opaque = opaque;
1782 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1783 }
1784
1785 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1786 {
1787 QEMUResetEntry *re;
1788
1789 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1790 if (re->func == func && re->opaque == opaque) {
1791 QTAILQ_REMOVE(&reset_handlers, re, entry);
1792 g_free(re);
1793 return;
1794 }
1795 }
1796 }
1797
1798 void qemu_devices_reset(void)
1799 {
1800 QEMUResetEntry *re, *nre;
1801
1802 /* reset all devices */
1803 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1804 re->func(re->opaque);
1805 }
1806 }
1807
1808 void qemu_system_reset(bool report)
1809 {
1810 MachineClass *mc;
1811
1812 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1813
1814 if (mc && mc->reset) {
1815 mc->reset();
1816 } else {
1817 qemu_devices_reset();
1818 }
1819 if (report) {
1820 monitor_protocol_event(QEVENT_RESET, NULL);
1821 }
1822 cpu_synchronize_all_post_reset();
1823 }
1824
1825 void qemu_system_reset_request(void)
1826 {
1827 if (no_reboot) {
1828 shutdown_requested = 1;
1829 } else {
1830 reset_requested = 1;
1831 }
1832 cpu_stop_current();
1833 qemu_notify_event();
1834 }
1835
1836 static void qemu_system_suspend(void)
1837 {
1838 pause_all_vcpus();
1839 notifier_list_notify(&suspend_notifiers, NULL);
1840 runstate_set(RUN_STATE_SUSPENDED);
1841 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1842 }
1843
1844 void qemu_system_suspend_request(void)
1845 {
1846 if (runstate_check(RUN_STATE_SUSPENDED)) {
1847 return;
1848 }
1849 suspend_requested = 1;
1850 cpu_stop_current();
1851 qemu_notify_event();
1852 }
1853
1854 void qemu_register_suspend_notifier(Notifier *notifier)
1855 {
1856 notifier_list_add(&suspend_notifiers, notifier);
1857 }
1858
1859 void qemu_system_wakeup_request(WakeupReason reason)
1860 {
1861 trace_system_wakeup_request(reason);
1862
1863 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1864 return;
1865 }
1866 if (!(wakeup_reason_mask & (1 << reason))) {
1867 return;
1868 }
1869 runstate_set(RUN_STATE_RUNNING);
1870 wakeup_reason = reason;
1871 qemu_notify_event();
1872 }
1873
1874 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1875 {
1876 if (enabled) {
1877 wakeup_reason_mask |= (1 << reason);
1878 } else {
1879 wakeup_reason_mask &= ~(1 << reason);
1880 }
1881 }
1882
1883 void qemu_register_wakeup_notifier(Notifier *notifier)
1884 {
1885 notifier_list_add(&wakeup_notifiers, notifier);
1886 }
1887
1888 void qemu_system_killed(int signal, pid_t pid)
1889 {
1890 shutdown_signal = signal;
1891 shutdown_pid = pid;
1892 no_shutdown = 0;
1893 qemu_system_shutdown_request();
1894 }
1895
1896 void qemu_system_shutdown_request(void)
1897 {
1898 shutdown_requested = 1;
1899 qemu_notify_event();
1900 }
1901
1902 static void qemu_system_powerdown(void)
1903 {
1904 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1905 notifier_list_notify(&powerdown_notifiers, NULL);
1906 }
1907
1908 void qemu_system_powerdown_request(void)
1909 {
1910 powerdown_requested = 1;
1911 qemu_notify_event();
1912 }
1913
1914 void qemu_register_powerdown_notifier(Notifier *notifier)
1915 {
1916 notifier_list_add(&powerdown_notifiers, notifier);
1917 }
1918
1919 void qemu_system_debug_request(void)
1920 {
1921 debug_requested = 1;
1922 qemu_notify_event();
1923 }
1924
1925 void qemu_system_vmstop_request(RunState state)
1926 {
1927 vmstop_requested = state;
1928 qemu_notify_event();
1929 }
1930
1931 static bool main_loop_should_exit(void)
1932 {
1933 RunState r;
1934 if (qemu_debug_requested()) {
1935 vm_stop(RUN_STATE_DEBUG);
1936 }
1937 if (qemu_suspend_requested()) {
1938 qemu_system_suspend();
1939 }
1940 if (qemu_shutdown_requested()) {
1941 qemu_kill_report();
1942 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1943 if (no_shutdown) {
1944 vm_stop(RUN_STATE_SHUTDOWN);
1945 } else {
1946 return true;
1947 }
1948 }
1949 if (qemu_reset_requested()) {
1950 pause_all_vcpus();
1951 cpu_synchronize_all_states();
1952 qemu_system_reset(VMRESET_REPORT);
1953 resume_all_vcpus();
1954 if (runstate_needs_reset()) {
1955 runstate_set(RUN_STATE_PAUSED);
1956 }
1957 }
1958 if (qemu_wakeup_requested()) {
1959 pause_all_vcpus();
1960 cpu_synchronize_all_states();
1961 qemu_system_reset(VMRESET_SILENT);
1962 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1963 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1964 resume_all_vcpus();
1965 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1966 }
1967 if (qemu_powerdown_requested()) {
1968 qemu_system_powerdown();
1969 }
1970 if (qemu_vmstop_requested(&r)) {
1971 vm_stop(r);
1972 }
1973 return false;
1974 }
1975
1976 static void main_loop(void)
1977 {
1978 bool nonblocking;
1979 int last_io = 0;
1980 #ifdef CONFIG_PROFILER
1981 int64_t ti;
1982 #endif
1983 do {
1984 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1985 #ifdef CONFIG_PROFILER
1986 ti = profile_getclock();
1987 #endif
1988 last_io = main_loop_wait(nonblocking);
1989 #ifdef CONFIG_PROFILER
1990 dev_time += profile_getclock() - ti;
1991 #endif
1992 } while (!main_loop_should_exit());
1993 }
1994
1995 static void version(void)
1996 {
1997 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1998 }
1999
2000 static void help(int exitcode)
2001 {
2002 version();
2003 printf("usage: %s [options] [disk_image]\n\n"
2004 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2005 error_get_progname());
2006
2007 #define QEMU_OPTIONS_GENERATE_HELP
2008 #include "qemu-options-wrapper.h"
2009
2010 printf("\nDuring emulation, the following keys are useful:\n"
2011 "ctrl-alt-f toggle full screen\n"
2012 "ctrl-alt-n switch to virtual console 'n'\n"
2013 "ctrl-alt toggle mouse and keyboard grab\n"
2014 "\n"
2015 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2016
2017 exit(exitcode);
2018 }
2019
2020 #define HAS_ARG 0x0001
2021
2022 typedef struct QEMUOption {
2023 const char *name;
2024 int flags;
2025 int index;
2026 uint32_t arch_mask;
2027 } QEMUOption;
2028
2029 static const QEMUOption qemu_options[] = {
2030 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2031 #define QEMU_OPTIONS_GENERATE_OPTIONS
2032 #include "qemu-options-wrapper.h"
2033 { NULL },
2034 };
2035
2036 static bool vga_available(void)
2037 {
2038 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2039 }
2040
2041 static bool cirrus_vga_available(void)
2042 {
2043 return object_class_by_name("cirrus-vga")
2044 || object_class_by_name("isa-cirrus-vga");
2045 }
2046
2047 static bool vmware_vga_available(void)
2048 {
2049 return object_class_by_name("vmware-svga");
2050 }
2051
2052 static bool qxl_vga_available(void)
2053 {
2054 return object_class_by_name("qxl-vga");
2055 }
2056
2057 static bool tcx_vga_available(void)
2058 {
2059 return object_class_by_name("SUNW,tcx");
2060 }
2061
2062 static bool cg3_vga_available(void)
2063 {
2064 return object_class_by_name("cgthree");
2065 }
2066
2067 static void select_vgahw (const char *p)
2068 {
2069 const char *opts;
2070
2071 assert(vga_interface_type == VGA_NONE);
2072 if (strstart(p, "std", &opts)) {
2073 if (vga_available()) {
2074 vga_interface_type = VGA_STD;
2075 } else {
2076 fprintf(stderr, "Error: standard VGA not available\n");
2077 exit(0);
2078 }
2079 } else if (strstart(p, "cirrus", &opts)) {
2080 if (cirrus_vga_available()) {
2081 vga_interface_type = VGA_CIRRUS;
2082 } else {
2083 fprintf(stderr, "Error: Cirrus VGA not available\n");
2084 exit(0);
2085 }
2086 } else if (strstart(p, "vmware", &opts)) {
2087 if (vmware_vga_available()) {
2088 vga_interface_type = VGA_VMWARE;
2089 } else {
2090 fprintf(stderr, "Error: VMWare SVGA not available\n");
2091 exit(0);
2092 }
2093 } else if (strstart(p, "xenfb", &opts)) {
2094 vga_interface_type = VGA_XENFB;
2095 } else if (strstart(p, "qxl", &opts)) {
2096 if (qxl_vga_available()) {
2097 vga_interface_type = VGA_QXL;
2098 } else {
2099 fprintf(stderr, "Error: QXL VGA not available\n");
2100 exit(0);
2101 }
2102 } else if (strstart(p, "tcx", &opts)) {
2103 if (tcx_vga_available()) {
2104 vga_interface_type = VGA_TCX;
2105 } else {
2106 fprintf(stderr, "Error: TCX framebuffer not available\n");
2107 exit(0);
2108 }
2109 } else if (strstart(p, "cg3", &opts)) {
2110 if (cg3_vga_available()) {
2111 vga_interface_type = VGA_CG3;
2112 } else {
2113 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2114 exit(0);
2115 }
2116 } else if (!strstart(p, "none", &opts)) {
2117 invalid_vga:
2118 fprintf(stderr, "Unknown vga type: %s\n", p);
2119 exit(1);
2120 }
2121 while (*opts) {
2122 const char *nextopt;
2123
2124 if (strstart(opts, ",retrace=", &nextopt)) {
2125 opts = nextopt;
2126 if (strstart(opts, "dumb", &nextopt))
2127 vga_retrace_method = VGA_RETRACE_DUMB;
2128 else if (strstart(opts, "precise", &nextopt))
2129 vga_retrace_method = VGA_RETRACE_PRECISE;
2130 else goto invalid_vga;
2131 } else goto invalid_vga;
2132 opts = nextopt;
2133 }
2134 }
2135
2136 static DisplayType select_display(const char *p)
2137 {
2138 const char *opts;
2139 DisplayType display = DT_DEFAULT;
2140
2141 if (strstart(p, "sdl", &opts)) {
2142 #ifdef CONFIG_SDL
2143 display = DT_SDL;
2144 while (*opts) {
2145 const char *nextopt;
2146
2147 if (strstart(opts, ",frame=", &nextopt)) {
2148 opts = nextopt;
2149 if (strstart(opts, "on", &nextopt)) {
2150 no_frame = 0;
2151 } else if (strstart(opts, "off", &nextopt)) {
2152 no_frame = 1;
2153 } else {
2154 goto invalid_sdl_args;
2155 }
2156 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2157 opts = nextopt;
2158 if (strstart(opts, "on", &nextopt)) {
2159 alt_grab = 1;
2160 } else if (strstart(opts, "off", &nextopt)) {
2161 alt_grab = 0;
2162 } else {
2163 goto invalid_sdl_args;
2164 }
2165 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2166 opts = nextopt;
2167 if (strstart(opts, "on", &nextopt)) {
2168 ctrl_grab = 1;
2169 } else if (strstart(opts, "off", &nextopt)) {
2170 ctrl_grab = 0;
2171 } else {
2172 goto invalid_sdl_args;
2173 }
2174 } else if (strstart(opts, ",window_close=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 no_quit = 0;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 no_quit = 1;
2180 } else {
2181 goto invalid_sdl_args;
2182 }
2183 } else {
2184 invalid_sdl_args:
2185 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2186 exit(1);
2187 }
2188 opts = nextopt;
2189 }
2190 #else
2191 fprintf(stderr, "SDL support is disabled\n");
2192 exit(1);
2193 #endif
2194 } else if (strstart(p, "vnc", &opts)) {
2195 #ifdef CONFIG_VNC
2196 display_remote++;
2197
2198 if (*opts) {
2199 const char *nextopt;
2200
2201 if (strstart(opts, "=", &nextopt)) {
2202 vnc_display = nextopt;
2203 }
2204 }
2205 if (!vnc_display) {
2206 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2207 exit(1);
2208 }
2209 #else
2210 fprintf(stderr, "VNC support is disabled\n");
2211 exit(1);
2212 #endif
2213 } else if (strstart(p, "curses", &opts)) {
2214 #ifdef CONFIG_CURSES
2215 display = DT_CURSES;
2216 #else
2217 fprintf(stderr, "Curses support is disabled\n");
2218 exit(1);
2219 #endif
2220 } else if (strstart(p, "gtk", &opts)) {
2221 #ifdef CONFIG_GTK
2222 display = DT_GTK;
2223 while (*opts) {
2224 const char *nextopt;
2225
2226 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2227 opts = nextopt;
2228 if (strstart(opts, "on", &nextopt)) {
2229 grab_on_hover = true;
2230 } else if (strstart(opts, "off", &nextopt)) {
2231 grab_on_hover = false;
2232 } else {
2233 goto invalid_gtk_args;
2234 }
2235 } else {
2236 invalid_gtk_args:
2237 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2238 exit(1);
2239 }
2240 opts = nextopt;
2241 }
2242 #else
2243 fprintf(stderr, "GTK support is disabled\n");
2244 exit(1);
2245 #endif
2246 } else if (strstart(p, "none", &opts)) {
2247 display = DT_NONE;
2248 } else {
2249 fprintf(stderr, "Unknown display type: %s\n", p);
2250 exit(1);
2251 }
2252
2253 return display;
2254 }
2255
2256 static int balloon_parse(const char *arg)
2257 {
2258 QemuOpts *opts;
2259
2260 if (strcmp(arg, "none") == 0) {
2261 return 0;
2262 }
2263
2264 if (!strncmp(arg, "virtio", 6)) {
2265 if (arg[6] == ',') {
2266 /* have params -> parse them */
2267 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2268 if (!opts)
2269 return -1;
2270 } else {
2271 /* create empty opts */
2272 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2273 &error_abort);
2274 }
2275 qemu_opt_set(opts, "driver", "virtio-balloon");
2276 return 0;
2277 }
2278
2279 return -1;
2280 }
2281
2282 char *qemu_find_file(int type, const char *name)
2283 {
2284 int i;
2285 const char *subdir;
2286 char *buf;
2287
2288 /* Try the name as a straight path first */
2289 if (access(name, R_OK) == 0) {
2290 trace_load_file(name, name);
2291 return g_strdup(name);
2292 }
2293
2294 switch (type) {
2295 case QEMU_FILE_TYPE_BIOS:
2296 subdir = "";
2297 break;
2298 case QEMU_FILE_TYPE_KEYMAP:
2299 subdir = "keymaps/";
2300 break;
2301 default:
2302 abort();
2303 }
2304
2305 for (i = 0; i < data_dir_idx; i++) {
2306 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2307 if (access(buf, R_OK) == 0) {
2308 trace_load_file(name, buf);
2309 return buf;
2310 }
2311 g_free(buf);
2312 }
2313 return NULL;
2314 }
2315
2316 static int device_help_func(QemuOpts *opts, void *opaque)
2317 {
2318 return qdev_device_help(opts);
2319 }
2320
2321 static int device_init_func(QemuOpts *opts, void *opaque)
2322 {
2323 DeviceState *dev;
2324
2325 dev = qdev_device_add(opts);
2326 if (!dev)
2327 return -1;
2328 object_unref(OBJECT(dev));
2329 return 0;
2330 }
2331
2332 static int chardev_init_func(QemuOpts *opts, void *opaque)
2333 {
2334 Error *local_err = NULL;
2335
2336 qemu_chr_new_from_opts(opts, NULL, &local_err);
2337 if (local_err) {
2338 error_report("%s", error_get_pretty(local_err));
2339 error_free(local_err);
2340 return -1;
2341 }
2342 return 0;
2343 }
2344
2345 #ifdef CONFIG_VIRTFS
2346 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2347 {
2348 int ret;
2349 ret = qemu_fsdev_add(opts);
2350
2351 return ret;
2352 }
2353 #endif
2354
2355 static int mon_init_func(QemuOpts *opts, void *opaque)
2356 {
2357 CharDriverState *chr;
2358 const char *chardev;
2359 const char *mode;
2360 int flags;
2361
2362 mode = qemu_opt_get(opts, "mode");
2363 if (mode == NULL) {
2364 mode = "readline";
2365 }
2366 if (strcmp(mode, "readline") == 0) {
2367 flags = MONITOR_USE_READLINE;
2368 } else if (strcmp(mode, "control") == 0) {
2369 flags = MONITOR_USE_CONTROL;
2370 } else {
2371 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2372 exit(1);
2373 }
2374
2375 if (qemu_opt_get_bool(opts, "pretty", 0))
2376 flags |= MONITOR_USE_PRETTY;
2377
2378 if (qemu_opt_get_bool(opts, "default", 0))
2379 flags |= MONITOR_IS_DEFAULT;
2380
2381 chardev = qemu_opt_get(opts, "chardev");
2382 chr = qemu_chr_find(chardev);
2383 if (chr == NULL) {
2384 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2385 exit(1);
2386 }
2387
2388 qemu_chr_fe_claim_no_fail(chr);
2389 monitor_init(chr, flags);
2390 return 0;
2391 }
2392
2393 static void monitor_parse(const char *optarg, const char *mode)
2394 {
2395 static int monitor_device_index = 0;
2396 QemuOpts *opts;
2397 const char *p;
2398 char label[32];
2399 int def = 0;
2400
2401 if (strstart(optarg, "chardev:", &p)) {
2402 snprintf(label, sizeof(label), "%s", p);
2403 } else {
2404 snprintf(label, sizeof(label), "compat_monitor%d",
2405 monitor_device_index);
2406 if (monitor_device_index == 0) {
2407 def = 1;
2408 }
2409 opts = qemu_chr_parse_compat(label, optarg);
2410 if (!opts) {
2411 fprintf(stderr, "parse error: %s\n", optarg);
2412 exit(1);
2413 }
2414 }
2415
2416 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2417 if (!opts) {
2418 fprintf(stderr, "duplicate chardev: %s\n", label);
2419 exit(1);
2420 }
2421 qemu_opt_set(opts, "mode", mode);
2422 qemu_opt_set(opts, "chardev", label);
2423 if (def)
2424 qemu_opt_set(opts, "default", "on");
2425 monitor_device_index++;
2426 }
2427
2428 struct device_config {
2429 enum {
2430 DEV_USB, /* -usbdevice */
2431 DEV_BT, /* -bt */
2432 DEV_SERIAL, /* -serial */
2433 DEV_PARALLEL, /* -parallel */
2434 DEV_VIRTCON, /* -virtioconsole */
2435 DEV_DEBUGCON, /* -debugcon */
2436 DEV_GDB, /* -gdb, -s */
2437 DEV_SCLP, /* s390 sclp */
2438 } type;
2439 const char *cmdline;
2440 Location loc;
2441 QTAILQ_ENTRY(device_config) next;
2442 };
2443
2444 static QTAILQ_HEAD(, device_config) device_configs =
2445 QTAILQ_HEAD_INITIALIZER(device_configs);
2446
2447 static void add_device_config(int type, const char *cmdline)
2448 {
2449 struct device_config *conf;
2450
2451 conf = g_malloc0(sizeof(*conf));
2452 conf->type = type;
2453 conf->cmdline = cmdline;
2454 loc_save(&conf->loc);
2455 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2456 }
2457
2458 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2459 {
2460 struct device_config *conf;
2461 int rc;
2462
2463 QTAILQ_FOREACH(conf, &device_configs, next) {
2464 if (conf->type != type)
2465 continue;
2466 loc_push_restore(&conf->loc);
2467 rc = func(conf->cmdline);
2468 loc_pop(&conf->loc);
2469 if (0 != rc)
2470 return rc;
2471 }
2472 return 0;
2473 }
2474
2475 static int serial_parse(const char *devname)
2476 {
2477 static int index = 0;
2478 char label[32];
2479
2480 if (strcmp(devname, "none") == 0)
2481 return 0;
2482 if (index == MAX_SERIAL_PORTS) {
2483 fprintf(stderr, "qemu: too many serial ports\n");
2484 exit(1);
2485 }
2486 snprintf(label, sizeof(label), "serial%d", index);
2487 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2488 if (!serial_hds[index]) {
2489 fprintf(stderr, "qemu: could not connect serial device"
2490 " to character backend '%s'\n", devname);
2491 return -1;
2492 }
2493 index++;
2494 return 0;
2495 }
2496
2497 static int parallel_parse(const char *devname)
2498 {
2499 static int index = 0;
2500 char label[32];
2501
2502 if (strcmp(devname, "none") == 0)
2503 return 0;
2504 if (index == MAX_PARALLEL_PORTS) {
2505 fprintf(stderr, "qemu: too many parallel ports\n");
2506 exit(1);
2507 }
2508 snprintf(label, sizeof(label), "parallel%d", index);
2509 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2510 if (!parallel_hds[index]) {
2511 fprintf(stderr, "qemu: could not connect parallel device"
2512 " to character backend '%s'\n", devname);
2513 return -1;
2514 }
2515 index++;
2516 return 0;
2517 }
2518
2519 static int virtcon_parse(const char *devname)
2520 {
2521 QemuOptsList *device = qemu_find_opts("device");
2522 static int index = 0;
2523 char label[32];
2524 QemuOpts *bus_opts, *dev_opts;
2525
2526 if (strcmp(devname, "none") == 0)
2527 return 0;
2528 if (index == MAX_VIRTIO_CONSOLES) {
2529 fprintf(stderr, "qemu: too many virtio consoles\n");
2530 exit(1);
2531 }
2532
2533 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2534 if (arch_type == QEMU_ARCH_S390X) {
2535 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2536 } else {
2537 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2538 }
2539
2540 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2541 qemu_opt_set(dev_opts, "driver", "virtconsole");
2542
2543 snprintf(label, sizeof(label), "virtcon%d", index);
2544 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2545 if (!virtcon_hds[index]) {
2546 fprintf(stderr, "qemu: could not connect virtio console"
2547 " to character backend '%s'\n", devname);
2548 return -1;
2549 }
2550 qemu_opt_set(dev_opts, "chardev", label);
2551
2552 index++;
2553 return 0;
2554 }
2555
2556 static int sclp_parse(const char *devname)
2557 {
2558 QemuOptsList *device = qemu_find_opts("device");
2559 static int index = 0;
2560 char label[32];
2561 QemuOpts *dev_opts;
2562
2563 if (strcmp(devname, "none") == 0) {
2564 return 0;
2565 }
2566 if (index == MAX_SCLP_CONSOLES) {
2567 fprintf(stderr, "qemu: too many sclp consoles\n");
2568 exit(1);
2569 }
2570
2571 assert(arch_type == QEMU_ARCH_S390X);
2572
2573 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2574 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2575
2576 snprintf(label, sizeof(label), "sclpcon%d", index);
2577 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2578 if (!sclp_hds[index]) {
2579 fprintf(stderr, "qemu: could not connect sclp console"
2580 " to character backend '%s'\n", devname);
2581 return -1;
2582 }
2583 qemu_opt_set(dev_opts, "chardev", label);
2584
2585 index++;
2586 return 0;
2587 }
2588
2589 static int debugcon_parse(const char *devname)
2590 {
2591 QemuOpts *opts;
2592
2593 if (!qemu_chr_new("debugcon", devname, NULL)) {
2594 exit(1);
2595 }
2596 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2597 if (!opts) {
2598 fprintf(stderr, "qemu: already have a debugcon device\n");
2599 exit(1);
2600 }
2601 qemu_opt_set(opts, "driver", "isa-debugcon");
2602 qemu_opt_set(opts, "chardev", "debugcon");
2603 return 0;
2604 }
2605
2606 static MachineClass *machine_parse(const char *name)
2607 {
2608 MachineClass *mc = NULL;
2609 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2610
2611 if (name) {
2612 mc = find_machine(name);
2613 }
2614 if (mc) {
2615 return mc;
2616 }
2617 if (name && !is_help_option(name)) {
2618 error_report("Unsupported machine type");
2619 error_printf("Use -machine help to list supported machines!\n");
2620 } else {
2621 printf("Supported machines are:\n");
2622 for (el = machines; el; el = el->next) {
2623 MachineClass *mc = el->data;
2624 if (mc->alias) {
2625 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2626 }
2627 printf("%-20s %s%s\n", mc->name, mc->desc,
2628 mc->is_default ? " (default)" : "");
2629 }
2630 }
2631
2632 g_slist_free(machines);
2633 exit(!name || !is_help_option(name));
2634 }
2635
2636 static int tcg_init(MachineClass *mc)
2637 {
2638 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2639 return 0;
2640 }
2641
2642 static struct {
2643 const char *opt_name;
2644 const char *name;
2645 int (*available)(void);
2646 int (*init)(MachineClass *mc);
2647 bool *allowed;
2648 } accel_list[] = {
2649 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2650 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2651 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2652 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2653 };
2654
2655 static int configure_accelerator(MachineClass *mc)
2656 {
2657 const char *p;
2658 char buf[10];
2659 int i, ret;
2660 bool accel_initialised = false;
2661 bool init_failed = false;
2662
2663 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2664 if (p == NULL) {
2665 /* Use the default "accelerator", tcg */
2666 p = "tcg";
2667 }
2668
2669 while (!accel_initialised && *p != '\0') {
2670 if (*p == ':') {
2671 p++;
2672 }
2673 p = get_opt_name(buf, sizeof (buf), p, ':');
2674 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2675 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2676 if (!accel_list[i].available()) {
2677 printf("%s not supported for this target\n",
2678 accel_list[i].name);
2679 break;
2680 }
2681 *(accel_list[i].allowed) = true;
2682 ret = accel_list[i].init(mc);
2683 if (ret < 0) {
2684 init_failed = true;
2685 fprintf(stderr, "failed to initialize %s: %s\n",
2686 accel_list[i].name,
2687 strerror(-ret));
2688 *(accel_list[i].allowed) = false;
2689 } else {
2690 accel_initialised = true;
2691 }
2692 break;
2693 }
2694 }
2695 if (i == ARRAY_SIZE(accel_list)) {
2696 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2697 }
2698 }
2699
2700 if (!accel_initialised) {
2701 if (!init_failed) {
2702 fprintf(stderr, "No accelerator found!\n");
2703 }
2704 exit(1);
2705 }
2706
2707 if (init_failed) {
2708 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2709 }
2710
2711 return !accel_initialised;
2712 }
2713
2714 void qemu_add_exit_notifier(Notifier *notify)
2715 {
2716 notifier_list_add(&exit_notifiers, notify);
2717 }
2718
2719 void qemu_remove_exit_notifier(Notifier *notify)
2720 {
2721 notifier_remove(notify);
2722 }
2723
2724 static void qemu_run_exit_notifiers(void)
2725 {
2726 notifier_list_notify(&exit_notifiers, NULL);
2727 }
2728
2729 void qemu_add_machine_init_done_notifier(Notifier *notify)
2730 {
2731 notifier_list_add(&machine_init_done_notifiers, notify);
2732 }
2733
2734 static void qemu_run_machine_init_done_notifiers(void)
2735 {
2736 notifier_list_notify(&machine_init_done_notifiers, NULL);
2737 }
2738
2739 static const QEMUOption *lookup_opt(int argc, char **argv,
2740 const char **poptarg, int *poptind)
2741 {
2742 const QEMUOption *popt;
2743 int optind = *poptind;
2744 char *r = argv[optind];
2745 const char *optarg;
2746
2747 loc_set_cmdline(argv, optind, 1);
2748 optind++;
2749 /* Treat --foo the same as -foo. */
2750 if (r[1] == '-')
2751 r++;
2752 popt = qemu_options;
2753 for(;;) {
2754 if (!popt->name) {
2755 error_report("invalid option");
2756 exit(1);
2757 }
2758 if (!strcmp(popt->name, r + 1))
2759 break;
2760 popt++;
2761 }
2762 if (popt->flags & HAS_ARG) {
2763 if (optind >= argc) {
2764 error_report("requires an argument");
2765 exit(1);
2766 }
2767 optarg = argv[optind++];
2768 loc_set_cmdline(argv, optind - 2, 2);
2769 } else {
2770 optarg = NULL;
2771 }
2772
2773 *poptarg = optarg;
2774 *poptind = optind;
2775
2776 return popt;
2777 }
2778
2779 static gpointer malloc_and_trace(gsize n_bytes)
2780 {
2781 void *ptr = malloc(n_bytes);
2782 trace_g_malloc(n_bytes, ptr);
2783 return ptr;
2784 }
2785
2786 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2787 {
2788 void *ptr = realloc(mem, n_bytes);
2789 trace_g_realloc(mem, n_bytes, ptr);
2790 return ptr;
2791 }
2792
2793 static void free_and_trace(gpointer mem)
2794 {
2795 trace_g_free(mem);
2796 free(mem);
2797 }
2798
2799 static int object_set_property(const char *name, const char *value, void *opaque)
2800 {
2801 Object *obj = OBJECT(opaque);
2802 StringInputVisitor *siv;
2803 Error *local_err = NULL;
2804
2805 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2806 strcmp(name, "type") == 0) {
2807 return 0;
2808 }
2809
2810 siv = string_input_visitor_new(value);
2811 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2812 string_input_visitor_cleanup(siv);
2813
2814 if (local_err) {
2815 qerror_report_err(local_err);
2816 error_free(local_err);
2817 return -1;
2818 }
2819
2820 return 0;
2821 }
2822
2823 static int object_create(QemuOpts *opts, void *opaque)
2824 {
2825 const char *type = qemu_opt_get(opts, "qom-type");
2826 const char *id = qemu_opts_id(opts);
2827 Error *local_err = NULL;
2828 Object *obj;
2829
2830 g_assert(type != NULL);
2831
2832 if (id == NULL) {
2833 qerror_report(QERR_MISSING_PARAMETER, "id");
2834 return -1;
2835 }
2836
2837 obj = object_new(type);
2838 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2839 object_unref(obj);
2840 return -1;
2841 }
2842
2843 if (!object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
2844 error_setg(&local_err, "object '%s' isn't supported by -object",
2845 id);
2846 goto out;
2847 }
2848
2849 object_property_add_child(container_get(object_get_root(), "/objects"),
2850 id, obj, &local_err);
2851
2852 user_creatable_complete(obj, &local_err);
2853 if (local_err) {
2854 object_property_del(container_get(object_get_root(), "/objects"),
2855 id, &error_abort);
2856 goto out;
2857 }
2858 out:
2859 object_unref(obj);
2860 if (local_err) {
2861 qerror_report_err(local_err);
2862 error_free(local_err);
2863 return -1;
2864 }
2865 return 0;
2866 }
2867
2868 int main(int argc, char **argv, char **envp)
2869 {
2870 int i;
2871 int snapshot, linux_boot;
2872 const char *icount_option = NULL;
2873 const char *initrd_filename;
2874 const char *kernel_filename, *kernel_cmdline;
2875 const char *boot_order;
2876 DisplayState *ds;
2877 int cyls, heads, secs, translation;
2878 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2879 QemuOptsList *olist;
2880 int optind;
2881 const char *optarg;
2882 const char *loadvm = NULL;
2883 MachineClass *machine_class;
2884 const char *cpu_model;
2885 const char *vga_model = NULL;
2886 const char *qtest_chrdev = NULL;
2887 const char *qtest_log = NULL;
2888 const char *pid_file = NULL;
2889 const char *incoming = NULL;
2890 #ifdef CONFIG_VNC
2891 int show_vnc_port = 0;
2892 #endif
2893 bool defconfig = true;
2894 bool userconfig = true;
2895 const char *log_mask = NULL;
2896 const char *log_file = NULL;
2897 GMemVTable mem_trace = {
2898 .malloc = malloc_and_trace,
2899 .realloc = realloc_and_trace,
2900 .free = free_and_trace,
2901 };
2902 const char *trace_events = NULL;
2903 const char *trace_file = NULL;
2904 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2905 1024 * 1024;
2906 ram_addr_t maxram_size = default_ram_size;
2907 uint64_t ram_slots = 0;
2908
2909 atexit(qemu_run_exit_notifiers);
2910 error_set_progname(argv[0]);
2911 qemu_init_exec_dir(argv[0]);
2912
2913 g_mem_set_vtable(&mem_trace);
2914
2915 module_call_init(MODULE_INIT_QOM);
2916
2917 qemu_add_opts(&qemu_drive_opts);
2918 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2919 qemu_add_drive_opts(&qemu_common_drive_opts);
2920 qemu_add_drive_opts(&qemu_drive_opts);
2921 qemu_add_opts(&qemu_chardev_opts);
2922 qemu_add_opts(&qemu_device_opts);
2923 qemu_add_opts(&qemu_netdev_opts);
2924 qemu_add_opts(&qemu_net_opts);
2925 qemu_add_opts(&qemu_rtc_opts);
2926 qemu_add_opts(&qemu_global_opts);
2927 qemu_add_opts(&qemu_mon_opts);
2928 qemu_add_opts(&qemu_trace_opts);
2929 qemu_add_opts(&qemu_option_rom_opts);
2930 qemu_add_opts(&qemu_machine_opts);
2931 qemu_add_opts(&qemu_mem_opts);
2932 qemu_add_opts(&qemu_smp_opts);
2933 qemu_add_opts(&qemu_boot_opts);
2934 qemu_add_opts(&qemu_sandbox_opts);
2935 qemu_add_opts(&qemu_add_fd_opts);
2936 qemu_add_opts(&qemu_object_opts);
2937 qemu_add_opts(&qemu_tpmdev_opts);
2938 qemu_add_opts(&qemu_realtime_opts);
2939 qemu_add_opts(&qemu_msg_opts);
2940 qemu_add_opts(&qemu_name_opts);
2941
2942 runstate_init();
2943
2944 rtc_clock = QEMU_CLOCK_HOST;
2945
2946 qemu_init_auxval(envp);
2947 qemu_cache_utils_init();
2948
2949 QLIST_INIT (&vm_change_state_head);
2950 os_setup_early_signal_handling();
2951
2952 module_call_init(MODULE_INIT_MACHINE);
2953 machine_class = find_default_machine();
2954 cpu_model = NULL;
2955 ram_size = default_ram_size;
2956 snapshot = 0;
2957 cyls = heads = secs = 0;
2958 translation = BIOS_ATA_TRANSLATION_AUTO;
2959
2960 for (i = 0; i < MAX_NODES; i++) {
2961 numa_info[i].node_mem = 0;
2962 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2963 }
2964
2965 nb_numa_nodes = 0;
2966 nb_nics = 0;
2967
2968 bdrv_init_with_whitelist();
2969
2970 autostart = 1;
2971
2972 /* first pass of option parsing */
2973 optind = 1;
2974 while (optind < argc) {
2975 if (argv[optind][0] != '-') {
2976 /* disk image */
2977 optind++;
2978 } else {
2979 const QEMUOption *popt;
2980
2981 popt = lookup_opt(argc, argv, &optarg, &optind);
2982 switch (popt->index) {
2983 case QEMU_OPTION_nodefconfig:
2984 defconfig = false;
2985 break;
2986 case QEMU_OPTION_nouserconfig:
2987 userconfig = false;
2988 break;
2989 }
2990 }
2991 }
2992
2993 if (defconfig) {
2994 int ret;
2995 ret = qemu_read_default_config_files(userconfig);
2996 if (ret < 0) {
2997 exit(1);
2998 }
2999 }
3000
3001 /* second pass of option parsing */
3002 optind = 1;
3003 for(;;) {
3004 if (optind >= argc)
3005 break;
3006 if (argv[optind][0] != '-') {
3007 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3008 } else {
3009 const QEMUOption *popt;
3010
3011 popt = lookup_opt(argc, argv, &optarg, &optind);
3012 if (!(popt->arch_mask & arch_type)) {
3013 printf("Option %s not supported for this target\n", popt->name);
3014 exit(1);
3015 }
3016 switch(popt->index) {
3017 case QEMU_OPTION_M:
3018 machine_class = machine_parse(optarg);
3019 break;
3020 case QEMU_OPTION_no_kvm_irqchip: {
3021 olist = qemu_find_opts("machine");
3022 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3023 break;
3024 }
3025 case QEMU_OPTION_cpu:
3026 /* hw initialization will check this */
3027 cpu_model = optarg;
3028 break;
3029 case QEMU_OPTION_hda:
3030 {
3031 char buf[256];
3032 if (cyls == 0)
3033 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3034 else
3035 snprintf(buf, sizeof(buf),
3036 "%s,cyls=%d,heads=%d,secs=%d%s",
3037 HD_OPTS , cyls, heads, secs,
3038 translation == BIOS_ATA_TRANSLATION_LBA ?
3039 ",trans=lba" :
3040 translation == BIOS_ATA_TRANSLATION_NONE ?
3041 ",trans=none" : "");
3042 drive_add(IF_DEFAULT, 0, optarg, buf);
3043 break;
3044 }
3045 case QEMU_OPTION_hdb:
3046 case QEMU_OPTION_hdc:
3047 case QEMU_OPTION_hdd:
3048 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3049 HD_OPTS);
3050 break;
3051 case QEMU_OPTION_drive:
3052 if (drive_def(optarg) == NULL) {
3053 exit(1);
3054 }
3055 break;
3056 case QEMU_OPTION_set:
3057 if (qemu_set_option(optarg) != 0)
3058 exit(1);
3059 break;
3060 case QEMU_OPTION_global:
3061 if (qemu_global_option(optarg) != 0)
3062 exit(1);
3063 break;
3064 case QEMU_OPTION_mtdblock:
3065 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3066 break;
3067 case QEMU_OPTION_sd:
3068 drive_add(IF_SD, -1, optarg, SD_OPTS);
3069 break;
3070 case QEMU_OPTION_pflash:
3071 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3072 break;
3073 case QEMU_OPTION_snapshot:
3074 snapshot = 1;
3075 break;
3076 case QEMU_OPTION_hdachs:
3077 {
3078 const char *p;
3079 p = optarg;
3080 cyls = strtol(p, (char **)&p, 0);
3081 if (cyls < 1 || cyls > 16383)
3082 goto chs_fail;
3083 if (*p != ',')
3084 goto chs_fail;
3085 p++;
3086 heads = strtol(p, (char **)&p, 0);
3087 if (heads < 1 || heads > 16)
3088 goto chs_fail;
3089 if (*p != ',')
3090 goto chs_fail;
3091 p++;
3092 secs = strtol(p, (char **)&p, 0);
3093 if (secs < 1 || secs > 63)
3094 goto chs_fail;
3095 if (*p == ',') {
3096 p++;
3097 if (!strcmp(p, "large")) {
3098 translation = BIOS_ATA_TRANSLATION_LARGE;
3099 } else if (!strcmp(p, "rechs")) {
3100 translation = BIOS_ATA_TRANSLATION_RECHS;
3101 } else if (!strcmp(p, "none")) {
3102 translation = BIOS_ATA_TRANSLATION_NONE;
3103 } else if (!strcmp(p, "lba")) {
3104 translation = BIOS_ATA_TRANSLATION_LBA;
3105 } else if (!strcmp(p, "auto")) {
3106 translation = BIOS_ATA_TRANSLATION_AUTO;
3107 } else {
3108 goto chs_fail;
3109 }
3110 } else if (*p != '\0') {
3111 chs_fail:
3112 fprintf(stderr, "qemu: invalid physical CHS format\n");
3113 exit(1);
3114 }
3115 if (hda_opts != NULL) {
3116 char num[16];
3117 snprintf(num, sizeof(num), "%d", cyls);
3118 qemu_opt_set(hda_opts, "cyls", num);
3119 snprintf(num, sizeof(num), "%d", heads);
3120 qemu_opt_set(hda_opts, "heads", num);
3121 snprintf(num, sizeof(num), "%d", secs);
3122 qemu_opt_set(hda_opts, "secs", num);
3123 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3124 qemu_opt_set(hda_opts, "trans", "large");
3125 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3126 qemu_opt_set(hda_opts, "trans", "rechs");
3127 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3128 qemu_opt_set(hda_opts, "trans", "lba");
3129 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3130 qemu_opt_set(hda_opts, "trans", "none");
3131 }
3132 }
3133 }
3134 break;
3135 case QEMU_OPTION_numa:
3136 numa_add(optarg);
3137 break;
3138 case QEMU_OPTION_display:
3139 display_type = select_display(optarg);
3140 break;
3141 case QEMU_OPTION_nographic:
3142 display_type = DT_NOGRAPHIC;
3143 break;
3144 case QEMU_OPTION_curses:
3145 #ifdef CONFIG_CURSES
3146 display_type = DT_CURSES;
3147 #else
3148 fprintf(stderr, "Curses support is disabled\n");
3149 exit(1);
3150 #endif
3151 break;
3152 case QEMU_OPTION_portrait:
3153 graphic_rotate = 90;
3154 break;
3155 case QEMU_OPTION_rotate:
3156 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3157 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3158 graphic_rotate != 180 && graphic_rotate != 270) {
3159 fprintf(stderr,
3160 "qemu: only 90, 180, 270 deg rotation is available\n");
3161 exit(1);
3162 }
3163 break;
3164 case QEMU_OPTION_kernel:
3165 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3166 break;
3167 case QEMU_OPTION_initrd:
3168 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3169 break;
3170 case QEMU_OPTION_append:
3171 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3172 break;
3173 case QEMU_OPTION_dtb:
3174 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3175 break;
3176 case QEMU_OPTION_cdrom:
3177 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3178 break;
3179 case QEMU_OPTION_boot:
3180 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3181 if (!opts) {
3182 exit(1);
3183 }
3184 break;
3185 case QEMU_OPTION_fda:
3186 case QEMU_OPTION_fdb:
3187 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3188 optarg, FD_OPTS);
3189 break;
3190 case QEMU_OPTION_no_fd_bootchk:
3191 fd_bootchk = 0;
3192 break;
3193 case QEMU_OPTION_netdev:
3194 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3195 exit(1);
3196 }
3197 break;
3198 case QEMU_OPTION_net:
3199 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3200 exit(1);
3201 }
3202 break;
3203 #ifdef CONFIG_LIBISCSI
3204 case QEMU_OPTION_iscsi:
3205 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3206 if (!opts) {
3207 exit(1);
3208 }
3209 break;
3210 #endif
3211 #ifdef CONFIG_SLIRP
3212 case QEMU_OPTION_tftp:
3213 legacy_tftp_prefix = optarg;
3214 break;
3215 case QEMU_OPTION_bootp:
3216 legacy_bootp_filename = optarg;
3217 break;
3218 case QEMU_OPTION_redir:
3219 if (net_slirp_redir(optarg) < 0)
3220 exit(1);
3221 break;
3222 #endif
3223 case QEMU_OPTION_bt:
3224 add_device_config(DEV_BT, optarg);
3225 break;
3226 case QEMU_OPTION_audio_help:
3227 AUD_help ();
3228 exit (0);
3229 break;
3230 case QEMU_OPTION_soundhw:
3231 select_soundhw (optarg);
3232 break;
3233 case QEMU_OPTION_h:
3234 help(0);
3235 break;
3236 case QEMU_OPTION_version:
3237 version();
3238 exit(0);
3239 break;
3240 case QEMU_OPTION_m: {
3241 uint64_t sz;
3242 const char *mem_str;
3243 const char *maxmem_str, *slots_str;
3244
3245 opts = qemu_opts_parse(qemu_find_opts("memory"),
3246 optarg, 1);
3247 if (!opts) {
3248 exit(EXIT_FAILURE);
3249 }
3250
3251 mem_str = qemu_opt_get(opts, "size");
3252 if (!mem_str) {
3253 error_report("invalid -m option, missing 'size' option");
3254 exit(EXIT_FAILURE);
3255 }
3256 if (!*mem_str) {
3257 error_report("missing 'size' option value");
3258 exit(EXIT_FAILURE);
3259 }
3260
3261 sz = qemu_opt_get_size(opts, "size", ram_size);
3262
3263 /* Fix up legacy suffix-less format */
3264 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3265 uint64_t overflow_check = sz;
3266
3267 sz <<= 20;
3268 if ((sz >> 20) != overflow_check) {
3269 error_report("too large 'size' option value");
3270 exit(EXIT_FAILURE);
3271 }
3272 }
3273
3274 /* backward compatibility behaviour for case "-m 0" */
3275 if (sz == 0) {
3276 sz = default_ram_size;
3277 }
3278
3279 sz = QEMU_ALIGN_UP(sz, 8192);
3280 ram_size = sz;
3281 if (ram_size != sz) {
3282 error_report("ram size too large");
3283 exit(EXIT_FAILURE);
3284 }
3285
3286 maxmem_str = qemu_opt_get(opts, "maxmem");
3287 slots_str = qemu_opt_get(opts, "slots");
3288 if (maxmem_str && slots_str) {
3289 uint64_t slots;
3290
3291 sz = qemu_opt_get_size(opts, "maxmem", 0);
3292 if (sz < ram_size) {
3293 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3294 "(%" PRIu64 ") <= initial memory (%"
3295 PRIu64 ")\n", sz, ram_size);
3296 exit(EXIT_FAILURE);
3297 }
3298
3299 slots = qemu_opt_get_number(opts, "slots", 0);
3300 if ((sz > ram_size) && !slots) {
3301 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3302 "(%" PRIu64 ") more than initial memory (%"
3303 PRIu64 ") but no hotplug slots where "
3304 "specified\n", sz, ram_size);
3305 exit(EXIT_FAILURE);
3306 }
3307
3308 if ((sz <= ram_size) && slots) {
3309 fprintf(stderr, "qemu: invalid -m option value: %"
3310 PRIu64 " hotplug slots where specified but "
3311 "maxmem (%" PRIu64 ") <= initial memory (%"
3312 PRIu64 ")\n", slots, sz, ram_size);
3313 exit(EXIT_FAILURE);
3314 }
3315 maxram_size = sz;
3316 ram_slots = slots;
3317 } else if ((!maxmem_str && slots_str) ||
3318 (maxmem_str && !slots_str)) {
3319 fprintf(stderr, "qemu: invalid -m option value: missing "
3320 "'%s' option\n", slots_str ? "maxmem" : "slots");
3321 exit(EXIT_FAILURE);
3322 }
3323 break;
3324 }
3325 #ifdef CONFIG_TPM
3326 case QEMU_OPTION_tpmdev:
3327 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3328 exit(1);
3329 }
3330 break;
3331 #endif
3332 case QEMU_OPTION_mempath:
3333 mem_path = optarg;
3334 break;
3335 case QEMU_OPTION_mem_prealloc:
3336 mem_prealloc = 1;
3337 break;
3338 case QEMU_OPTION_d:
3339 log_mask = optarg;
3340 break;
3341 case QEMU_OPTION_D:
3342 log_file = optarg;
3343 break;
3344 case QEMU_OPTION_s:
3345 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3346 break;
3347 case QEMU_OPTION_gdb:
3348 add_device_config(DEV_GDB, optarg);
3349 break;
3350 case QEMU_OPTION_L:
3351 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3352 data_dir[data_dir_idx++] = optarg;
3353 }
3354 break;
3355 case QEMU_OPTION_bios:
3356 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3357 break;
3358 case QEMU_OPTION_singlestep:
3359 singlestep = 1;
3360 break;
3361 case QEMU_OPTION_S:
3362 autostart = 0;
3363 break;
3364 case QEMU_OPTION_k:
3365 keyboard_layout = optarg;
3366 break;
3367 case QEMU_OPTION_localtime:
3368 rtc_utc = 0;
3369 break;
3370 case QEMU_OPTION_vga:
3371 vga_model = optarg;
3372 default_vga = 0;
3373 break;
3374 case QEMU_OPTION_g:
3375 {
3376 const char *p;
3377 int w, h, depth;
3378 p = optarg;
3379 w = strtol(p, (char **)&p, 10);
3380 if (w <= 0) {
3381 graphic_error:
3382 fprintf(stderr, "qemu: invalid resolution or depth\n");
3383 exit(1);
3384 }
3385 if (*p != 'x')
3386 goto graphic_error;
3387 p++;
3388 h = strtol(p, (char **)&p, 10);
3389 if (h <= 0)
3390 goto graphic_error;
3391 if (*p == 'x') {
3392 p++;
3393 depth = strtol(p, (char **)&p, 10);
3394 if (depth != 8 && depth != 15 && depth != 16 &&
3395 depth != 24 && depth != 32)
3396 goto graphic_error;
3397 } else if (*p == '\0') {
3398 depth = graphic_depth;
3399 } else {
3400 goto graphic_error;
3401 }
3402
3403 graphic_width = w;
3404 graphic_height = h;
3405 graphic_depth = depth;
3406 }
3407 break;
3408 case QEMU_OPTION_echr:
3409 {
3410 char *r;
3411 term_escape_char = strtol(optarg, &r, 0);
3412 if (r == optarg)
3413 printf("Bad argument to echr\n");
3414 break;
3415 }
3416 case QEMU_OPTION_monitor:
3417 default_monitor = 0;
3418 if (strncmp(optarg, "none", 4)) {
3419 monitor_parse(optarg, "readline");
3420 }
3421 break;
3422 case QEMU_OPTION_qmp:
3423 monitor_parse(optarg, "control");
3424 default_monitor = 0;
3425 break;
3426 case QEMU_OPTION_mon:
3427 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3428 if (!opts) {
3429 exit(1);
3430 }
3431 default_monitor = 0;
3432 break;
3433 case QEMU_OPTION_chardev:
3434 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3435 if (!opts) {
3436 exit(1);
3437 }
3438 break;
3439 case QEMU_OPTION_fsdev:
3440 olist = qemu_find_opts("fsdev");
3441 if (!olist) {
3442 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3443 exit(1);
3444 }
3445 opts = qemu_opts_parse(olist, optarg, 1);
3446 if (!opts) {
3447 exit(1);
3448 }
3449 break;
3450 case QEMU_OPTION_virtfs: {
3451 QemuOpts *fsdev;
3452 QemuOpts *device;
3453 const char *writeout, *sock_fd, *socket;
3454
3455 olist = qemu_find_opts("virtfs");
3456 if (!olist) {
3457 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3458 exit(1);
3459 }
3460 opts = qemu_opts_parse(olist, optarg, 1);
3461 if (!opts) {
3462 exit(1);
3463 }
3464
3465 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3466 qemu_opt_get(opts, "mount_tag") == NULL) {
3467 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3468 exit(1);
3469 }
3470 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3471 qemu_opt_get(opts, "mount_tag"),
3472 1, NULL);
3473 if (!fsdev) {
3474 fprintf(stderr, "duplicate fsdev id: %s\n",
3475 qemu_opt_get(opts, "mount_tag"));
3476 exit(1);
3477 }
3478
3479 writeout = qemu_opt_get(opts, "writeout");
3480 if (writeout) {
3481 #ifdef CONFIG_SYNC_FILE_RANGE
3482 qemu_opt_set(fsdev, "writeout", writeout);
3483 #else
3484 fprintf(stderr, "writeout=immediate not supported on "
3485 "this platform\n");
3486 exit(1);
3487 #endif
3488 }
3489 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3490 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3491 qemu_opt_set(fsdev, "security_model",
3492 qemu_opt_get(opts, "security_model"));
3493 socket = qemu_opt_get(opts, "socket");
3494 if (socket) {
3495 qemu_opt_set(fsdev, "socket", socket);
3496 }
3497 sock_fd = qemu_opt_get(opts, "sock_fd");
3498 if (sock_fd) {
3499 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3500 }
3501
3502 qemu_opt_set_bool(fsdev, "readonly",
3503 qemu_opt_get_bool(opts, "readonly", 0));
3504 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3505 &error_abort);
3506 qemu_opt_set(device, "driver", "virtio-9p-pci");
3507 qemu_opt_set(device, "fsdev",
3508 qemu_opt_get(opts, "mount_tag"));
3509 qemu_opt_set(device, "mount_tag",
3510 qemu_opt_get(opts, "mount_tag"));
3511 break;
3512 }
3513 case QEMU_OPTION_virtfs_synth: {
3514 QemuOpts *fsdev;
3515 QemuOpts *device;
3516
3517 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3518 1, NULL);
3519 if (!fsdev) {
3520 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3521 exit(1);
3522 }
3523 qemu_opt_set(fsdev, "fsdriver", "synth");
3524
3525 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3526 &error_abort);
3527 qemu_opt_set(device, "driver", "virtio-9p-pci");
3528 qemu_opt_set(device, "fsdev", "v_synth");
3529 qemu_opt_set(device, "mount_tag", "v_synth");
3530 break;
3531 }
3532 case QEMU_OPTION_serial:
3533 add_device_config(DEV_SERIAL, optarg);
3534 default_serial = 0;
3535 if (strncmp(optarg, "mon:", 4) == 0) {
3536 default_monitor = 0;
3537 }
3538 break;
3539 case QEMU_OPTION_watchdog:
3540 if (watchdog) {
3541 fprintf(stderr,
3542 "qemu: only one watchdog option may be given\n");
3543 return 1;
3544 }
3545 watchdog = optarg;
3546 break;
3547 case QEMU_OPTION_watchdog_action:
3548 if (select_watchdog_action(optarg) == -1) {
3549 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3550 exit(1);
3551 }
3552 break;
3553 case QEMU_OPTION_virtiocon:
3554 add_device_config(DEV_VIRTCON, optarg);
3555 default_virtcon = 0;
3556 if (strncmp(optarg, "mon:", 4) == 0) {
3557 default_monitor = 0;
3558 }
3559 break;
3560 case QEMU_OPTION_parallel:
3561 add_device_config(DEV_PARALLEL, optarg);
3562 default_parallel = 0;
3563 if (strncmp(optarg, "mon:", 4) == 0) {
3564 default_monitor = 0;
3565 }
3566 break;
3567 case QEMU_OPTION_debugcon:
3568 add_device_config(DEV_DEBUGCON, optarg);
3569 break;
3570 case QEMU_OPTION_loadvm:
3571 loadvm = optarg;
3572 break;
3573 case QEMU_OPTION_full_screen:
3574 full_screen = 1;
3575 break;
3576 case QEMU_OPTION_no_frame:
3577 no_frame = 1;
3578 break;
3579 case QEMU_OPTION_alt_grab:
3580 alt_grab = 1;
3581 break;
3582 case QEMU_OPTION_ctrl_grab:
3583 ctrl_grab = 1;
3584 break;
3585 case QEMU_OPTION_no_quit:
3586 no_quit = 1;
3587 break;
3588 case QEMU_OPTION_sdl:
3589 #ifdef CONFIG_SDL
3590 display_type = DT_SDL;
3591 break;
3592 #else
3593 fprintf(stderr, "SDL support is disabled\n");
3594 exit(1);
3595 #endif
3596 case QEMU_OPTION_pidfile:
3597 pid_file = optarg;
3598 break;
3599 case QEMU_OPTION_win2k_hack:
3600 win2k_install_hack = 1;
3601 break;
3602 case QEMU_OPTION_rtc_td_hack: {
3603 static GlobalProperty slew_lost_ticks[] = {
3604 {
3605 .driver = "mc146818rtc",
3606 .property = "lost_tick_policy",
3607 .value = "slew",
3608 },
3609 { /* end of list */ }
3610 };
3611
3612 qdev_prop_register_global_list(slew_lost_ticks);
3613 break;
3614 }
3615 case QEMU_OPTION_acpitable:
3616 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3617 if (!opts) {
3618 exit(1);
3619 }
3620 do_acpitable_option(opts);
3621 break;
3622 case QEMU_OPTION_smbios:
3623 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3624 if (!opts) {
3625 exit(1);
3626 }
3627 do_smbios_option(opts);
3628 break;
3629 case QEMU_OPTION_enable_kvm:
3630 olist = qemu_find_opts("machine");
3631 qemu_opts_parse(olist, "accel=kvm", 0);
3632 break;
3633 case QEMU_OPTION_machine:
3634 olist = qemu_find_opts("machine");
3635 opts = qemu_opts_parse(olist, optarg, 1);
3636 if (!opts) {
3637 exit(1);
3638 }
3639 optarg = qemu_opt_get(opts, "type");
3640 if (optarg) {
3641 machine_class = machine_parse(optarg);
3642 }
3643 break;
3644 case QEMU_OPTION_no_kvm:
3645 olist = qemu_find_opts("machine");
3646 qemu_opts_parse(olist, "accel=tcg", 0);
3647 break;
3648 case QEMU_OPTION_no_kvm_pit: {
3649 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3650 "separately.\n");
3651 break;
3652 }
3653 case QEMU_OPTION_no_kvm_pit_reinjection: {
3654 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3655 {
3656 .driver = "kvm-pit",
3657 .property = "lost_tick_policy",
3658 .value = "discard",
3659 },
3660 { /* end of list */ }
3661 };
3662
3663 fprintf(stderr, "Warning: option deprecated, use "
3664 "lost_tick_policy property of kvm-pit instead.\n");
3665 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3666 break;
3667 }
3668 case QEMU_OPTION_usb:
3669 olist = qemu_find_opts("machine");
3670 qemu_opts_parse(olist, "usb=on", 0);
3671 break;
3672 case QEMU_OPTION_usbdevice:
3673 olist = qemu_find_opts("machine");
3674 qemu_opts_parse(olist, "usb=on", 0);
3675 add_device_config(DEV_USB, optarg);
3676 break;
3677 case QEMU_OPTION_device:
3678 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3679 exit(1);
3680 }
3681 break;
3682 case QEMU_OPTION_smp:
3683 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3684 exit(1);
3685 }
3686 break;
3687 case QEMU_OPTION_vnc:
3688 #ifdef CONFIG_VNC
3689 display_remote++;
3690 vnc_display = optarg;
3691 #else
3692 fprintf(stderr, "VNC support is disabled\n");
3693 exit(1);
3694 #endif
3695 break;
3696 case QEMU_OPTION_no_acpi:
3697 acpi_enabled = 0;
3698 break;
3699 case QEMU_OPTION_no_hpet:
3700 no_hpet = 1;
3701 break;
3702 case QEMU_OPTION_balloon:
3703 if (balloon_parse(optarg) < 0) {
3704 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3705 exit(1);
3706 }
3707 break;
3708 case QEMU_OPTION_no_reboot:
3709 no_reboot = 1;
3710 break;
3711 case QEMU_OPTION_no_shutdown:
3712 no_shutdown = 1;
3713 break;
3714 case QEMU_OPTION_show_cursor:
3715 cursor_hide = 0;
3716 break;
3717 case QEMU_OPTION_uuid:
3718 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3719 fprintf(stderr, "Fail to parse UUID string."
3720 " Wrong format.\n");
3721 exit(1);
3722 }
3723 qemu_uuid_set = true;
3724 break;
3725 case QEMU_OPTION_option_rom:
3726 if (nb_option_roms >= MAX_OPTION_ROMS) {
3727 fprintf(stderr, "Too many option ROMs\n");
3728 exit(1);
3729 }
3730 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3731 if (!opts) {
3732 exit(1);
3733 }
3734 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3735 option_rom[nb_option_roms].bootindex =
3736 qemu_opt_get_number(opts, "bootindex", -1);
3737 if (!option_rom[nb_option_roms].name) {
3738 fprintf(stderr, "Option ROM file is not specified\n");
3739 exit(1);
3740 }
3741 nb_option_roms++;
3742 break;
3743 case QEMU_OPTION_semihosting:
3744 semihosting_enabled = 1;
3745 break;
3746 case QEMU_OPTION_tdf:
3747 fprintf(stderr, "Warning: user space PIT time drift fix "
3748 "is no longer supported.\n");
3749 break;
3750 case QEMU_OPTION_name:
3751 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3752 if (!opts) {
3753 exit(1);
3754 }
3755 break;
3756 case QEMU_OPTION_prom_env:
3757 if (nb_prom_envs >= MAX_PROM_ENVS) {
3758 fprintf(stderr, "Too many prom variables\n");
3759 exit(1);
3760 }
3761 prom_envs[nb_prom_envs] = optarg;
3762 nb_prom_envs++;
3763 break;
3764 case QEMU_OPTION_old_param:
3765 old_param = 1;
3766 break;
3767 case QEMU_OPTION_clock:
3768 /* Clock options no longer exist. Keep this option for
3769 * backward compatibility.
3770 */
3771 break;
3772 case QEMU_OPTION_startdate:
3773 configure_rtc_date_offset(optarg, 1);
3774 break;
3775 case QEMU_OPTION_rtc:
3776 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3777 if (!opts) {
3778 exit(1);
3779 }
3780 configure_rtc(opts);
3781 break;
3782 case QEMU_OPTION_tb_size:
3783 tcg_tb_size = strtol(optarg, NULL, 0);
3784 if (tcg_tb_size < 0) {
3785 tcg_tb_size = 0;
3786 }
3787 break;
3788 case QEMU_OPTION_icount:
3789 icount_option = optarg;
3790 break;
3791 case QEMU_OPTION_incoming:
3792 incoming = optarg;
3793 runstate_set(RUN_STATE_INMIGRATE);
3794 break;
3795 case QEMU_OPTION_nodefaults:
3796 has_defaults = 0;
3797 break;
3798 case QEMU_OPTION_xen_domid:
3799 if (!(xen_available())) {
3800 printf("Option %s not supported for this target\n", popt->name);
3801 exit(1);
3802 }
3803 xen_domid = atoi(optarg);
3804 break;
3805 case QEMU_OPTION_xen_create:
3806 if (!(xen_available())) {
3807 printf("Option %s not supported for this target\n", popt->name);
3808 exit(1);
3809 }
3810 xen_mode = XEN_CREATE;
3811 break;
3812 case QEMU_OPTION_xen_attach:
3813 if (!(xen_available())) {
3814 printf("Option %s not supported for this target\n", popt->name);
3815 exit(1);
3816 }
3817 xen_mode = XEN_ATTACH;
3818 break;
3819 case QEMU_OPTION_trace:
3820 {
3821 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3822 if (!opts) {
3823 exit(1);
3824 }
3825 trace_events = qemu_opt_get(opts, "events");
3826 trace_file = qemu_opt_get(opts, "file");
3827 break;
3828 }
3829 case QEMU_OPTION_readconfig:
3830 {
3831 int ret = qemu_read_config_file(optarg);
3832 if (ret < 0) {
3833 fprintf(stderr, "read config %s: %s\n", optarg,
3834 strerror(-ret));
3835 exit(1);
3836 }
3837 break;
3838 }
3839 case QEMU_OPTION_spice:
3840 olist = qemu_find_opts("spice");
3841 if (!olist) {
3842 fprintf(stderr, "spice is not supported by this qemu build.\n");
3843 exit(1);
3844 }
3845 opts = qemu_opts_parse(olist, optarg, 0);
3846 if (!opts) {
3847 exit(1);
3848 }
3849 display_remote++;
3850 break;
3851 case QEMU_OPTION_writeconfig:
3852 {
3853 FILE *fp;
3854 if (strcmp(optarg, "-") == 0) {
3855 fp = stdout;
3856 } else {
3857 fp = fopen(optarg, "w");
3858 if (fp == NULL) {
3859 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3860 exit(1);
3861 }
3862 }
3863 qemu_config_write(fp);
3864 if (fp != stdout) {
3865 fclose(fp);
3866 }
3867 break;
3868 }
3869 case QEMU_OPTION_qtest:
3870 qtest_chrdev = optarg;
3871 break;
3872 case QEMU_OPTION_qtest_log:
3873 qtest_log = optarg;
3874 break;
3875 case QEMU_OPTION_sandbox:
3876 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3877 if (!opts) {
3878 exit(1);
3879 }
3880 break;
3881 case QEMU_OPTION_add_fd:
3882 #ifndef _WIN32
3883 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3884 if (!opts) {
3885 exit(1);
3886 }
3887 #else
3888 error_report("File descriptor passing is disabled on this "
3889 "platform");
3890 exit(1);
3891 #endif
3892 break;
3893 case QEMU_OPTION_object:
3894 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3895 if (!opts) {
3896 exit(1);
3897 }
3898 break;
3899 case QEMU_OPTION_realtime:
3900 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3901 if (!opts) {
3902 exit(1);
3903 }
3904 configure_realtime(opts);
3905 break;
3906 case QEMU_OPTION_msg:
3907 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3908 if (!opts) {
3909 exit(1);
3910 }
3911 configure_msg(opts);
3912 break;
3913 default:
3914 os_parse_cmd_args(popt->index, optarg);
3915 }
3916 }
3917 }
3918 loc_set_none();
3919
3920 os_daemonize();
3921
3922 if (qemu_init_main_loop()) {
3923 fprintf(stderr, "qemu_init_main_loop failed\n");
3924 exit(1);
3925 }
3926
3927 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3928 exit(1);
3929 }
3930
3931 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3932 exit(1);
3933 }
3934
3935 #ifndef _WIN32
3936 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3937 exit(1);
3938 }
3939
3940 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3941 exit(1);
3942 }
3943 #endif
3944
3945 if (machine_class == NULL) {
3946 fprintf(stderr, "No machine specified, and there is no default.\n"
3947 "Use -machine help to list supported machines!\n");
3948 exit(1);
3949 }
3950
3951 current_machine = MACHINE(object_new(object_class_get_name(
3952 OBJECT_CLASS(machine_class))));
3953 object_property_add_child(object_get_root(), "machine",
3954 OBJECT(current_machine), &error_abort);
3955
3956 if (machine_class->hw_version) {
3957 qemu_set_version(machine_class->hw_version);
3958 }
3959
3960 if (qemu_opts_foreach(qemu_find_opts("object"),
3961 object_create, NULL, 0) != 0) {
3962 exit(1);
3963 }
3964
3965 /* Init CPU def lists, based on config
3966 * - Must be called after all the qemu_read_config_file() calls
3967 * - Must be called before list_cpus()
3968 * - Must be called before machine->init()
3969 */
3970 cpudef_init();
3971
3972 if (cpu_model && is_help_option(cpu_model)) {
3973 list_cpus(stdout, &fprintf, cpu_model);
3974 exit(0);
3975 }
3976
3977 /* Open the logfile at this point, if necessary. We can't open the logfile
3978 * when encountering either of the logging options (-d or -D) because the
3979 * other one may be encountered later on the command line, changing the
3980 * location or level of logging.
3981 */
3982 if (log_mask) {
3983 int mask;
3984 if (log_file) {
3985 qemu_set_log_filename(log_file);
3986 }
3987
3988 mask = qemu_str_to_log_mask(log_mask);
3989 if (!mask) {
3990 qemu_print_log_usage(stdout);
3991 exit(1);
3992 }
3993 qemu_set_log(mask);
3994 }
3995
3996 if (!is_daemonized()) {
3997 if (!trace_init_backends(trace_events, trace_file)) {
3998 exit(1);
3999 }
4000 }
4001
4002 /* If no data_dir is specified then try to find it relative to the
4003 executable path. */
4004 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4005 data_dir[data_dir_idx] = os_find_datadir();
4006 if (data_dir[data_dir_idx] != NULL) {
4007 data_dir_idx++;
4008 }
4009 }
4010 /* If all else fails use the install path specified when building. */
4011 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4012 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4013 }
4014
4015 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4016
4017 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4018 if (smp_cpus > machine_class->max_cpus) {
4019 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4020 "supported by machine `%s' (%d)\n", smp_cpus,
4021 machine_class->name, machine_class->max_cpus);
4022 exit(1);
4023 }
4024
4025 /*
4026 * Get the default machine options from the machine if it is not already
4027 * specified either by the configuration file or by the command line.
4028 */
4029 if (machine_class->default_machine_opts) {
4030 qemu_opts_set_defaults(qemu_find_opts("machine"),
4031 machine_class->default_machine_opts, 0);
4032 }
4033
4034 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4035 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4036
4037 if (!vga_model && !default_vga) {
4038 vga_interface_type = VGA_DEVICE;
4039 }
4040 if (!has_defaults || machine_class->no_serial) {
4041 default_serial = 0;
4042 }
4043 if (!has_defaults || machine_class->no_parallel) {
4044 default_parallel = 0;
4045 }
4046 if (!has_defaults || !machine_class->use_virtcon) {
4047 default_virtcon = 0;
4048 }
4049 if (!has_defaults || !machine_class->use_sclp) {
4050 default_sclp = 0;
4051 }
4052 if (!has_defaults || machine_class->no_floppy) {
4053 default_floppy = 0;
4054 }
4055 if (!has_defaults || machine_class->no_cdrom) {
4056 default_cdrom = 0;
4057 }
4058 if (!has_defaults || machine_class->no_sdcard) {
4059 default_sdcard = 0;
4060 }
4061 if (!has_defaults) {
4062 default_monitor = 0;
4063 default_net = 0;
4064 default_vga = 0;
4065 }
4066
4067 if (is_daemonized()) {
4068 /* According to documentation and historically, -nographic redirects
4069 * serial port, parallel port and monitor to stdio, which does not work
4070 * with -daemonize. We can redirect these to null instead, but since
4071 * -nographic is legacy, let's just error out.
4072 * We disallow -nographic only if all other ports are not redirected
4073 * explicitly, to not break existing legacy setups which uses
4074 * -nographic _and_ redirects all ports explicitly - this is valid
4075 * usage, -nographic is just a no-op in this case.
4076 */
4077 if (display_type == DT_NOGRAPHIC
4078 && (default_parallel || default_serial
4079 || default_monitor || default_virtcon)) {
4080 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4081 exit(1);
4082 }
4083 #ifdef CONFIG_CURSES
4084 if (display_type == DT_CURSES) {
4085 fprintf(stderr, "curses display can not be used with -daemonize\n");
4086 exit(1);
4087 }
4088 #endif
4089 }
4090
4091 if (display_type == DT_NOGRAPHIC) {
4092 if (default_parallel)
4093 add_device_config(DEV_PARALLEL, "null");
4094 if (default_serial && default_monitor) {
4095 add_device_config(DEV_SERIAL, "mon:stdio");
4096 } else if (default_virtcon && default_monitor) {
4097 add_device_config(DEV_VIRTCON, "mon:stdio");
4098 } else if (default_sclp && default_monitor) {
4099 add_device_config(DEV_SCLP, "mon:stdio");
4100 } else {
4101 if (default_serial)
4102 add_device_config(DEV_SERIAL, "stdio");
4103 if (default_virtcon)
4104 add_device_config(DEV_VIRTCON, "stdio");
4105 if (default_sclp) {
4106 add_device_config(DEV_SCLP, "stdio");
4107 }
4108 if (default_monitor)
4109 monitor_parse("stdio", "readline");
4110 }
4111 } else {
4112 if (default_serial)
4113 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4114 if (default_parallel)
4115 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4116 if (default_monitor)
4117 monitor_parse("vc:80Cx24C", "readline");
4118 if (default_virtcon)
4119 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4120 if (default_sclp) {
4121 add_device_config(DEV_SCLP, "vc:80Cx24C");
4122 }
4123 }
4124
4125 if (display_type == DT_DEFAULT && !display_remote) {
4126 #if defined(CONFIG_GTK)
4127 display_type = DT_GTK;
4128 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4129 display_type = DT_SDL;
4130 #elif defined(CONFIG_VNC)
4131 vnc_display = "localhost:0,to=99";
4132 show_vnc_port = 1;
4133 #else
4134 display_type = DT_NONE;
4135 #endif
4136 }
4137
4138 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4139 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4140 "for SDL, ignoring option\n");
4141 }
4142 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4143 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4144 "ignoring option\n");
4145 }
4146
4147 #if defined(CONFIG_GTK)
4148 if (display_type == DT_GTK) {
4149 early_gtk_display_init();
4150 }
4151 #endif
4152
4153 socket_init();
4154
4155 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4156 exit(1);
4157 #ifdef CONFIG_VIRTFS
4158 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4159 exit(1);
4160 }
4161 #endif
4162
4163 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4164 os_pidfile_error();
4165 exit(1);
4166 }
4167
4168 /* store value for the future use */
4169 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4170
4171 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4172 != 0) {
4173 exit(0);
4174 }
4175
4176 machine_opts = qemu_get_machine_opts();
4177 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4178 1) < 0) {
4179 object_unref(OBJECT(current_machine));
4180 exit(1);
4181 }
4182
4183 configure_accelerator(machine_class);
4184
4185 if (qtest_chrdev) {
4186 Error *local_err = NULL;
4187 qtest_init(qtest_chrdev, qtest_log, &local_err);
4188 if (local_err) {
4189 error_report("%s", error_get_pretty(local_err));
4190 error_free(local_err);
4191 exit(1);
4192 }
4193 }
4194
4195 machine_opts = qemu_get_machine_opts();
4196 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4197 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4198 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4199 bios_name = qemu_opt_get(machine_opts, "firmware");
4200
4201 boot_order = machine_class->default_boot_order;
4202 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4203 if (opts) {
4204 char *normal_boot_order;
4205 const char *order, *once;
4206
4207 order = qemu_opt_get(opts, "order");
4208 if (order) {
4209 validate_bootdevices(order);
4210 boot_order = order;
4211 }
4212
4213 once = qemu_opt_get(opts, "once");
4214 if (once) {
4215 validate_bootdevices(once);
4216 normal_boot_order = g_strdup(boot_order);
4217 boot_order = once;
4218 qemu_register_reset(restore_boot_order, normal_boot_order);
4219 }
4220
4221 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4222 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4223 }
4224
4225 if (!kernel_cmdline) {
4226 kernel_cmdline = "";
4227 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4228 }
4229
4230 linux_boot = (kernel_filename != NULL);
4231
4232 if (!linux_boot && *kernel_cmdline != '\0') {
4233 fprintf(stderr, "-append only allowed with -kernel option\n");
4234 exit(1);
4235 }
4236
4237 if (!linux_boot && initrd_filename != NULL) {
4238 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4239 exit(1);
4240 }
4241
4242 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4243 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4244 exit(1);
4245 }
4246
4247 os_set_line_buffering();
4248
4249 qemu_init_cpu_loop();
4250 qemu_mutex_lock_iothread();
4251
4252 #ifdef CONFIG_SPICE
4253 /* spice needs the timers to be initialized by this point */
4254 qemu_spice_init();
4255 #endif
4256
4257 if (icount_option && (kvm_enabled() || xen_enabled())) {
4258 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4259 exit(1);
4260 }
4261 configure_icount(icount_option);
4262
4263 /* clean up network at qemu process termination */
4264 atexit(&net_cleanup);
4265
4266 if (net_init_clients() < 0) {
4267 exit(1);
4268 }
4269
4270 #ifdef CONFIG_TPM
4271 if (tpm_init() < 0) {
4272 exit(1);
4273 }
4274 #endif
4275
4276 /* init the bluetooth world */
4277 if (foreach_device_config(DEV_BT, bt_parse))
4278 exit(1);
4279
4280 if (!xen_enabled()) {
4281 /* On 32-bit hosts, QEMU is limited by virtual address space */
4282 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4283 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4284 exit(1);
4285 }
4286 }
4287
4288 cpu_exec_init_all();
4289
4290 blk_mig_init();
4291 ram_mig_init();
4292
4293 /* open the virtual block devices */
4294 if (snapshot)
4295 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4296 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4297 &machine_class->block_default_type, 1) != 0) {
4298 exit(1);
4299 }
4300
4301 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4302 CDROM_OPTS);
4303 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4304 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4305
4306 set_numa_nodes();
4307
4308 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4309 exit(1);
4310 }
4311
4312 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4313 exit(1);
4314 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4315 exit(1);
4316 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4317 exit(1);
4318 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4319 exit(1);
4320 }
4321 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4322 exit(1);
4323
4324 /* If no default VGA is requested, the default is "none". */
4325 if (default_vga) {
4326 if (cirrus_vga_available()) {
4327 vga_model = "cirrus";
4328 } else if (vga_available()) {
4329 vga_model = "std";
4330 }
4331 }
4332 if (vga_model) {
4333 select_vgahw(vga_model);
4334 }
4335
4336 if (watchdog) {
4337 i = select_watchdog(watchdog);
4338 if (i > 0)
4339 exit (i == 1 ? 1 : 0);
4340 }
4341
4342 if (machine_class->compat_props) {
4343 qdev_prop_register_global_list(machine_class->compat_props);
4344 }
4345 qemu_add_globals();
4346
4347 qdev_machine_init();
4348
4349 current_machine->ram_size = ram_size;
4350 current_machine->maxram_size = maxram_size;
4351 current_machine->ram_slots = ram_slots;
4352 current_machine->boot_order = boot_order;
4353 current_machine->cpu_model = cpu_model;
4354
4355 machine_class->init(current_machine);
4356
4357 audio_init();
4358
4359 cpu_synchronize_all_post_init();
4360
4361 set_numa_modes();
4362
4363 /* init USB devices */
4364 if (usb_enabled(false)) {
4365 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4366 exit(1);
4367 }
4368
4369 /* init generic devices */
4370 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4371 exit(1);
4372
4373 net_check_clients();
4374
4375 ds = init_displaystate();
4376
4377 /* init local displays */
4378 switch (display_type) {
4379 case DT_NOGRAPHIC:
4380 (void)ds; /* avoid warning if no display is configured */
4381 break;
4382 #if defined(CONFIG_CURSES)
4383 case DT_CURSES:
4384 curses_display_init(ds, full_screen);
4385 break;
4386 #endif
4387 #if defined(CONFIG_SDL)
4388 case DT_SDL:
4389 sdl_display_init(ds, full_screen, no_frame);
4390 break;
4391 #elif defined(CONFIG_COCOA)
4392 case DT_SDL:
4393 cocoa_display_init(ds, full_screen);
4394 break;
4395 #endif
4396 #if defined(CONFIG_GTK)
4397 case DT_GTK:
4398 gtk_display_init(ds, full_screen, grab_on_hover);
4399 break;
4400 #endif
4401 default:
4402 break;
4403 }
4404
4405 /* must be after terminal init, SDL library changes signal handlers */
4406 os_setup_signal_handling();
4407
4408 #ifdef CONFIG_VNC
4409 /* init remote displays */
4410 if (vnc_display) {
4411 Error *local_err = NULL;
4412 vnc_display_init(ds);
4413 vnc_display_open(ds, vnc_display, &local_err);
4414 if (local_err != NULL) {
4415 error_report("Failed to start VNC server on `%s': %s",
4416 vnc_display, error_get_pretty(local_err));
4417 error_free(local_err);
4418 exit(1);
4419 }
4420
4421 if (show_vnc_port) {
4422 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4423 }
4424 }
4425 #endif
4426 #ifdef CONFIG_SPICE
4427 if (using_spice) {
4428 qemu_spice_display_init();
4429 }
4430 #endif
4431
4432 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4433 exit(1);
4434 }
4435
4436 qdev_machine_creation_done();
4437
4438 if (rom_load_all() != 0) {
4439 fprintf(stderr, "rom loading failed\n");
4440 exit(1);
4441 }
4442
4443 /* TODO: once all bus devices are qdevified, this should be done
4444 * when bus is created by qdev.c */
4445 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4446 qemu_run_machine_init_done_notifiers();
4447
4448 /* Done notifiers can load ROMs */
4449 rom_load_done();
4450
4451 qemu_system_reset(VMRESET_SILENT);
4452 if (loadvm) {
4453 if (load_vmstate(loadvm) < 0) {
4454 autostart = 0;
4455 }
4456 }
4457
4458 qdev_prop_check_global();
4459
4460 if (incoming) {
4461 Error *local_err = NULL;
4462 qemu_start_incoming_migration(incoming, &local_err);
4463 if (local_err) {
4464 error_report("-incoming %s: %s", incoming,
4465 error_get_pretty(local_err));
4466 error_free(local_err);
4467 exit(1);
4468 }
4469 } else if (autostart) {
4470 vm_start();
4471 }
4472
4473 os_setup_post();
4474
4475 if (is_daemonized()) {
4476 if (!trace_init_backends(trace_events, trace_file)) {
4477 exit(1);
4478 }
4479 }
4480
4481 main_loop();
4482 bdrv_close_all();
4483 pause_all_vcpus();
4484 res_free();
4485 #ifdef CONFIG_TPM
4486 tpm_cleanup();
4487 #endif
4488
4489 return 0;
4490 }