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