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