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