]> git.proxmox.com Git - qemu.git/blob - vl.c
vl: fix -hdachs/-hda argument order parsing issues
[qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #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>
99 int qemu_main(int argc, char **argv, char **envp);
100 int 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
179 static const char *data_dir;
180 const char *bios_name = NULL;
181 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
182 DisplayType display_type = DT_DEFAULT;
183 int display_remote = 0;
184 const char* keyboard_layout = NULL;
185 ram_addr_t ram_size;
186 const char *mem_path = NULL;
187 #ifdef MAP_POPULATE
188 int mem_prealloc = 0; /* force preallocation of physical target memory */
189 #endif
190 int nb_nics;
191 NICInfo nd_table[MAX_NICS];
192 int autostart;
193 static int rtc_utc = 1;
194 static int rtc_date_offset = -1; /* -1 means no change */
195 QEMUClock *rtc_clock;
196 int vga_interface_type = VGA_NONE;
197 static int full_screen = 0;
198 #ifdef CONFIG_SDL
199 static int no_frame = 0;
200 #endif
201 int no_quit = 0;
202 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
203 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
204 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
205 int win2k_install_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 #ifdef CONFIG_VNC
213 const char *vnc_display;
214 #endif
215 int acpi_enabled = 1;
216 int no_hpet = 0;
217 int fd_bootchk = 1;
218 int no_reboot = 0;
219 int no_shutdown = 0;
220 int cursor_hide = 1;
221 int graphic_rotate = 0;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
236
237 typedef struct FWBootEntry FWBootEntry;
238
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
244 };
245
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 unsigned long *node_cpumask[MAX_NODES];
251
252 uint8_t qemu_uuid[16];
253
254 static QEMUBootSetHandler *boot_set_handler;
255 static void *boot_set_opaque;
256
257 static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259
260 static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262
263 static int tcg_allowed = 1;
264 int kvm_allowed = 0;
265 int xen_allowed = 0;
266 uint32_t xen_domid;
267 enum xen_mode xen_mode = XEN_EMULATE;
268 static int tcg_tb_size;
269
270 static int default_serial = 1;
271 static int default_parallel = 1;
272 static int default_virtcon = 1;
273 static int default_monitor = 1;
274 static int default_floppy = 1;
275 static int default_cdrom = 1;
276 static int default_sdcard = 1;
277 static int default_vga = 1;
278
279 static 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
301 const char *qemu_get_vm_name(void)
302 {
303 return qemu_name;
304 }
305
306 static 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
314 static 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
332 static RunState current_run_state = RUN_STATE_PRELAUNCH;
333
334 typedef struct {
335 RunState from;
336 RunState to;
337 } RunStateTransition;
338
339 static 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
393 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
394
395 bool runstate_check(RunState state)
396 {
397 return current_run_state == state;
398 }
399
400 void 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 */
412 void 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
426 int runstate_is_running(void)
427 {
428 return runstate_check(RUN_STATE_RUNNING);
429 }
430
431 StatusInfo *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 */
447 void 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
467 int 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
485 void 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
494 static 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
533 static 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 */
584 static int nb_hcis;
585 static int cur_hci;
586 static struct HCIInfo *hci_table[MAX_NICS];
587
588 static 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" */
595 static 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
611 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
612 {
613 }
614
615 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
616 {
617 return -ENOTSUP;
618 }
619
620 static 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
627 struct 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
635 static 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
665 static 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
692 static 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
703 static 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
734 static 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
773 static 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
803 static 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
810 static 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
818 static 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
841 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
842 {
843 boot_set_handler = func;
844 boot_set_opaque = opaque;
845 }
846
847 int 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
855 static 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
883 static 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
900 void 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 */
936 char *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
979 static 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
1029 static 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
1076 static 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
1106 done:
1107 return 0;
1108 }
1109
1110 static 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
1130 static 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
1140 void 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
1148 void 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
1159 static struct pcmcia_socket_entry_s {
1160 PCMCIASocket *socket;
1161 struct pcmcia_socket_entry_s *next;
1162 } *pcmcia_sockets = 0;
1163
1164 void 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
1174 void 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
1186 void 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
1202 static QEMUMachine *first_machine = NULL;
1203 QEMUMachine *current_machine = NULL;
1204
1205 int 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
1216 static 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
1229 QEMUMachine *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
1241 MachineInfoList *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
1275 static 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
1292 struct vm_change_state_entry {
1293 VMChangeStateHandler *cb;
1294 void *opaque;
1295 QLIST_ENTRY (vm_change_state_entry) entries;
1296 };
1297
1298 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1299
1300 VMChangeStateEntry *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
1313 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1314 {
1315 QLIST_REMOVE (e, entries);
1316 g_free (e);
1317 }
1318
1319 void 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
1330 void 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
1343 typedef struct QEMUResetEntry {
1344 QTAILQ_ENTRY(QEMUResetEntry) entry;
1345 QEMUResetHandler *func;
1346 void *opaque;
1347 } QEMUResetEntry;
1348
1349 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1350 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1351 static int reset_requested;
1352 static int shutdown_requested, shutdown_signal = -1;
1353 static pid_t shutdown_pid;
1354 static int powerdown_requested;
1355 static int debug_requested;
1356 static int suspend_requested;
1357 static int wakeup_requested;
1358 static NotifierList suspend_notifiers =
1359 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1360 static NotifierList wakeup_notifiers =
1361 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1362 static uint32_t wakeup_reason_mask = ~0;
1363 static RunState vmstop_requested = RUN_STATE_MAX;
1364
1365 int qemu_shutdown_requested_get(void)
1366 {
1367 return shutdown_requested;
1368 }
1369
1370 int qemu_reset_requested_get(void)
1371 {
1372 return reset_requested;
1373 }
1374
1375 int qemu_shutdown_requested(void)
1376 {
1377 int r = shutdown_requested;
1378 shutdown_requested = 0;
1379 return r;
1380 }
1381
1382 void qemu_kill_report(void)
1383 {
1384 if (!qtest_enabled() && shutdown_signal != -1) {
1385 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1386 if (shutdown_pid == 0) {
1387 /* This happens for eg ^C at the terminal, so it's worth
1388 * avoiding printing an odd message in that case.
1389 */
1390 fputc('\n', stderr);
1391 } else {
1392 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1393 }
1394 shutdown_signal = -1;
1395 }
1396 }
1397
1398 int qemu_reset_requested(void)
1399 {
1400 int r = reset_requested;
1401 reset_requested = 0;
1402 return r;
1403 }
1404
1405 static int qemu_suspend_requested(void)
1406 {
1407 int r = suspend_requested;
1408 suspend_requested = 0;
1409 return r;
1410 }
1411
1412 static int qemu_wakeup_requested(void)
1413 {
1414 int r = wakeup_requested;
1415 wakeup_requested = 0;
1416 return r;
1417 }
1418
1419 int qemu_powerdown_requested(void)
1420 {
1421 int r = powerdown_requested;
1422 powerdown_requested = 0;
1423 return r;
1424 }
1425
1426 static int qemu_debug_requested(void)
1427 {
1428 int r = debug_requested;
1429 debug_requested = 0;
1430 return r;
1431 }
1432
1433 /* We use RUN_STATE_MAX but any invalid value will do */
1434 static bool qemu_vmstop_requested(RunState *r)
1435 {
1436 if (vmstop_requested < RUN_STATE_MAX) {
1437 *r = vmstop_requested;
1438 vmstop_requested = RUN_STATE_MAX;
1439 return true;
1440 }
1441
1442 return false;
1443 }
1444
1445 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1446 {
1447 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1448
1449 re->func = func;
1450 re->opaque = opaque;
1451 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1452 }
1453
1454 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1455 {
1456 QEMUResetEntry *re;
1457
1458 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1459 if (re->func == func && re->opaque == opaque) {
1460 QTAILQ_REMOVE(&reset_handlers, re, entry);
1461 g_free(re);
1462 return;
1463 }
1464 }
1465 }
1466
1467 void qemu_devices_reset(void)
1468 {
1469 QEMUResetEntry *re, *nre;
1470
1471 /* reset all devices */
1472 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1473 re->func(re->opaque);
1474 }
1475 }
1476
1477 void qemu_system_reset(bool report)
1478 {
1479 if (current_machine && current_machine->reset) {
1480 current_machine->reset();
1481 } else {
1482 qemu_devices_reset();
1483 }
1484 if (report) {
1485 monitor_protocol_event(QEVENT_RESET, NULL);
1486 }
1487 cpu_synchronize_all_post_reset();
1488 }
1489
1490 void qemu_system_reset_request(void)
1491 {
1492 if (no_reboot) {
1493 shutdown_requested = 1;
1494 } else {
1495 reset_requested = 1;
1496 }
1497 cpu_stop_current();
1498 qemu_notify_event();
1499 }
1500
1501 static void qemu_system_suspend(void)
1502 {
1503 pause_all_vcpus();
1504 notifier_list_notify(&suspend_notifiers, NULL);
1505 runstate_set(RUN_STATE_SUSPENDED);
1506 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1507 }
1508
1509 void qemu_system_suspend_request(void)
1510 {
1511 if (runstate_check(RUN_STATE_SUSPENDED)) {
1512 return;
1513 }
1514 suspend_requested = 1;
1515 cpu_stop_current();
1516 qemu_notify_event();
1517 }
1518
1519 void qemu_register_suspend_notifier(Notifier *notifier)
1520 {
1521 notifier_list_add(&suspend_notifiers, notifier);
1522 }
1523
1524 void qemu_system_wakeup_request(WakeupReason reason)
1525 {
1526 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1527 return;
1528 }
1529 if (!(wakeup_reason_mask & (1 << reason))) {
1530 return;
1531 }
1532 runstate_set(RUN_STATE_RUNNING);
1533 notifier_list_notify(&wakeup_notifiers, &reason);
1534 wakeup_requested = 1;
1535 qemu_notify_event();
1536 }
1537
1538 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1539 {
1540 if (enabled) {
1541 wakeup_reason_mask |= (1 << reason);
1542 } else {
1543 wakeup_reason_mask &= ~(1 << reason);
1544 }
1545 }
1546
1547 void qemu_register_wakeup_notifier(Notifier *notifier)
1548 {
1549 notifier_list_add(&wakeup_notifiers, notifier);
1550 }
1551
1552 void qemu_system_killed(int signal, pid_t pid)
1553 {
1554 shutdown_signal = signal;
1555 shutdown_pid = pid;
1556 no_shutdown = 0;
1557 qemu_system_shutdown_request();
1558 }
1559
1560 void qemu_system_shutdown_request(void)
1561 {
1562 shutdown_requested = 1;
1563 qemu_notify_event();
1564 }
1565
1566 void qemu_system_powerdown_request(void)
1567 {
1568 powerdown_requested = 1;
1569 qemu_notify_event();
1570 }
1571
1572 void qemu_system_debug_request(void)
1573 {
1574 debug_requested = 1;
1575 qemu_notify_event();
1576 }
1577
1578 void qemu_system_vmstop_request(RunState state)
1579 {
1580 vmstop_requested = state;
1581 qemu_notify_event();
1582 }
1583
1584 qemu_irq qemu_system_powerdown;
1585
1586 static bool main_loop_should_exit(void)
1587 {
1588 RunState r;
1589 if (qemu_debug_requested()) {
1590 vm_stop(RUN_STATE_DEBUG);
1591 }
1592 if (qemu_suspend_requested()) {
1593 qemu_system_suspend();
1594 }
1595 if (qemu_shutdown_requested()) {
1596 qemu_kill_report();
1597 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1598 if (no_shutdown) {
1599 vm_stop(RUN_STATE_SHUTDOWN);
1600 } else {
1601 return true;
1602 }
1603 }
1604 if (qemu_reset_requested()) {
1605 pause_all_vcpus();
1606 cpu_synchronize_all_states();
1607 qemu_system_reset(VMRESET_REPORT);
1608 resume_all_vcpus();
1609 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1610 runstate_check(RUN_STATE_SHUTDOWN)) {
1611 runstate_set(RUN_STATE_PAUSED);
1612 }
1613 }
1614 if (qemu_wakeup_requested()) {
1615 pause_all_vcpus();
1616 cpu_synchronize_all_states();
1617 qemu_system_reset(VMRESET_SILENT);
1618 resume_all_vcpus();
1619 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1620 }
1621 if (qemu_powerdown_requested()) {
1622 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1623 qemu_irq_raise(qemu_system_powerdown);
1624 }
1625 if (qemu_vmstop_requested(&r)) {
1626 vm_stop(r);
1627 }
1628 return false;
1629 }
1630
1631 static void main_loop(void)
1632 {
1633 bool nonblocking;
1634 int last_io = 0;
1635 #ifdef CONFIG_PROFILER
1636 int64_t ti;
1637 #endif
1638 do {
1639 nonblocking = !kvm_enabled() && last_io > 0;
1640 #ifdef CONFIG_PROFILER
1641 ti = profile_getclock();
1642 #endif
1643 last_io = main_loop_wait(nonblocking);
1644 #ifdef CONFIG_PROFILER
1645 dev_time += profile_getclock() - ti;
1646 #endif
1647 } while (!main_loop_should_exit());
1648 }
1649
1650 static void version(void)
1651 {
1652 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1653 }
1654
1655 static void help(int exitcode)
1656 {
1657 version();
1658 printf("usage: %s [options] [disk_image]\n\n"
1659 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1660 error_get_progname());
1661
1662 #define QEMU_OPTIONS_GENERATE_HELP
1663 #include "qemu-options-wrapper.h"
1664
1665 printf("\nDuring emulation, the following keys are useful:\n"
1666 "ctrl-alt-f toggle full screen\n"
1667 "ctrl-alt-n switch to virtual console 'n'\n"
1668 "ctrl-alt toggle mouse and keyboard grab\n"
1669 "\n"
1670 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1671
1672 exit(exitcode);
1673 }
1674
1675 #define HAS_ARG 0x0001
1676
1677 typedef struct QEMUOption {
1678 const char *name;
1679 int flags;
1680 int index;
1681 uint32_t arch_mask;
1682 } QEMUOption;
1683
1684 static const QEMUOption qemu_options[] = {
1685 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1686 #define QEMU_OPTIONS_GENERATE_OPTIONS
1687 #include "qemu-options-wrapper.h"
1688 { NULL },
1689 };
1690
1691 static bool vga_available(void)
1692 {
1693 return qdev_exists("VGA") || qdev_exists("isa-vga");
1694 }
1695
1696 static bool cirrus_vga_available(void)
1697 {
1698 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1699 }
1700
1701 static bool vmware_vga_available(void)
1702 {
1703 return qdev_exists("vmware-svga");
1704 }
1705
1706 static void select_vgahw (const char *p)
1707 {
1708 const char *opts;
1709
1710 vga_interface_type = VGA_NONE;
1711 if (strstart(p, "std", &opts)) {
1712 if (vga_available()) {
1713 vga_interface_type = VGA_STD;
1714 } else {
1715 fprintf(stderr, "Error: standard VGA not available\n");
1716 exit(0);
1717 }
1718 } else if (strstart(p, "cirrus", &opts)) {
1719 if (cirrus_vga_available()) {
1720 vga_interface_type = VGA_CIRRUS;
1721 } else {
1722 fprintf(stderr, "Error: Cirrus VGA not available\n");
1723 exit(0);
1724 }
1725 } else if (strstart(p, "vmware", &opts)) {
1726 if (vmware_vga_available()) {
1727 vga_interface_type = VGA_VMWARE;
1728 } else {
1729 fprintf(stderr, "Error: VMWare SVGA not available\n");
1730 exit(0);
1731 }
1732 } else if (strstart(p, "xenfb", &opts)) {
1733 vga_interface_type = VGA_XENFB;
1734 } else if (strstart(p, "qxl", &opts)) {
1735 vga_interface_type = VGA_QXL;
1736 } else if (!strstart(p, "none", &opts)) {
1737 invalid_vga:
1738 fprintf(stderr, "Unknown vga type: %s\n", p);
1739 exit(1);
1740 }
1741 while (*opts) {
1742 const char *nextopt;
1743
1744 if (strstart(opts, ",retrace=", &nextopt)) {
1745 opts = nextopt;
1746 if (strstart(opts, "dumb", &nextopt))
1747 vga_retrace_method = VGA_RETRACE_DUMB;
1748 else if (strstart(opts, "precise", &nextopt))
1749 vga_retrace_method = VGA_RETRACE_PRECISE;
1750 else goto invalid_vga;
1751 } else goto invalid_vga;
1752 opts = nextopt;
1753 }
1754 }
1755
1756 static DisplayType select_display(const char *p)
1757 {
1758 const char *opts;
1759 DisplayType display = DT_DEFAULT;
1760
1761 if (strstart(p, "sdl", &opts)) {
1762 #ifdef CONFIG_SDL
1763 display = DT_SDL;
1764 while (*opts) {
1765 const char *nextopt;
1766
1767 if (strstart(opts, ",frame=", &nextopt)) {
1768 opts = nextopt;
1769 if (strstart(opts, "on", &nextopt)) {
1770 no_frame = 0;
1771 } else if (strstart(opts, "off", &nextopt)) {
1772 no_frame = 1;
1773 } else {
1774 goto invalid_sdl_args;
1775 }
1776 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1777 opts = nextopt;
1778 if (strstart(opts, "on", &nextopt)) {
1779 alt_grab = 1;
1780 } else if (strstart(opts, "off", &nextopt)) {
1781 alt_grab = 0;
1782 } else {
1783 goto invalid_sdl_args;
1784 }
1785 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1786 opts = nextopt;
1787 if (strstart(opts, "on", &nextopt)) {
1788 ctrl_grab = 1;
1789 } else if (strstart(opts, "off", &nextopt)) {
1790 ctrl_grab = 0;
1791 } else {
1792 goto invalid_sdl_args;
1793 }
1794 } else if (strstart(opts, ",window_close=", &nextopt)) {
1795 opts = nextopt;
1796 if (strstart(opts, "on", &nextopt)) {
1797 no_quit = 0;
1798 } else if (strstart(opts, "off", &nextopt)) {
1799 no_quit = 1;
1800 } else {
1801 goto invalid_sdl_args;
1802 }
1803 } else {
1804 invalid_sdl_args:
1805 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1806 exit(1);
1807 }
1808 opts = nextopt;
1809 }
1810 #else
1811 fprintf(stderr, "SDL support is disabled\n");
1812 exit(1);
1813 #endif
1814 } else if (strstart(p, "vnc", &opts)) {
1815 #ifdef CONFIG_VNC
1816 display_remote++;
1817
1818 if (*opts) {
1819 const char *nextopt;
1820
1821 if (strstart(opts, "=", &nextopt)) {
1822 vnc_display = nextopt;
1823 }
1824 }
1825 if (!vnc_display) {
1826 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1827 exit(1);
1828 }
1829 #else
1830 fprintf(stderr, "VNC support is disabled\n");
1831 exit(1);
1832 #endif
1833 } else if (strstart(p, "curses", &opts)) {
1834 #ifdef CONFIG_CURSES
1835 display = DT_CURSES;
1836 #else
1837 fprintf(stderr, "Curses support is disabled\n");
1838 exit(1);
1839 #endif
1840 } else if (strstart(p, "none", &opts)) {
1841 display = DT_NONE;
1842 } else {
1843 fprintf(stderr, "Unknown display type: %s\n", p);
1844 exit(1);
1845 }
1846
1847 return display;
1848 }
1849
1850 static int balloon_parse(const char *arg)
1851 {
1852 QemuOpts *opts;
1853
1854 if (strcmp(arg, "none") == 0) {
1855 return 0;
1856 }
1857
1858 if (!strncmp(arg, "virtio", 6)) {
1859 if (arg[6] == ',') {
1860 /* have params -> parse them */
1861 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1862 if (!opts)
1863 return -1;
1864 } else {
1865 /* create empty opts */
1866 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1867 }
1868 qemu_opt_set(opts, "driver", "virtio-balloon");
1869 return 0;
1870 }
1871
1872 return -1;
1873 }
1874
1875 char *qemu_find_file(int type, const char *name)
1876 {
1877 int len;
1878 const char *subdir;
1879 char *buf;
1880
1881 /* Try the name as a straight path first */
1882 if (access(name, R_OK) == 0) {
1883 return g_strdup(name);
1884 }
1885 switch (type) {
1886 case QEMU_FILE_TYPE_BIOS:
1887 subdir = "";
1888 break;
1889 case QEMU_FILE_TYPE_KEYMAP:
1890 subdir = "keymaps/";
1891 break;
1892 default:
1893 abort();
1894 }
1895 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1896 buf = g_malloc0(len);
1897 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1898 if (access(buf, R_OK)) {
1899 g_free(buf);
1900 return NULL;
1901 }
1902 return buf;
1903 }
1904
1905 static int device_help_func(QemuOpts *opts, void *opaque)
1906 {
1907 return qdev_device_help(opts);
1908 }
1909
1910 static int device_init_func(QemuOpts *opts, void *opaque)
1911 {
1912 DeviceState *dev;
1913
1914 dev = qdev_device_add(opts);
1915 if (!dev)
1916 return -1;
1917 return 0;
1918 }
1919
1920 static int chardev_init_func(QemuOpts *opts, void *opaque)
1921 {
1922 CharDriverState *chr;
1923
1924 chr = qemu_chr_new_from_opts(opts, NULL);
1925 if (!chr)
1926 return -1;
1927 return 0;
1928 }
1929
1930 #ifdef CONFIG_VIRTFS
1931 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1932 {
1933 int ret;
1934 ret = qemu_fsdev_add(opts);
1935
1936 return ret;
1937 }
1938 #endif
1939
1940 static int mon_init_func(QemuOpts *opts, void *opaque)
1941 {
1942 CharDriverState *chr;
1943 const char *chardev;
1944 const char *mode;
1945 int flags;
1946
1947 mode = qemu_opt_get(opts, "mode");
1948 if (mode == NULL) {
1949 mode = "readline";
1950 }
1951 if (strcmp(mode, "readline") == 0) {
1952 flags = MONITOR_USE_READLINE;
1953 } else if (strcmp(mode, "control") == 0) {
1954 flags = MONITOR_USE_CONTROL;
1955 } else {
1956 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1957 exit(1);
1958 }
1959
1960 if (qemu_opt_get_bool(opts, "pretty", 0))
1961 flags |= MONITOR_USE_PRETTY;
1962
1963 if (qemu_opt_get_bool(opts, "default", 0))
1964 flags |= MONITOR_IS_DEFAULT;
1965
1966 chardev = qemu_opt_get(opts, "chardev");
1967 chr = qemu_chr_find(chardev);
1968 if (chr == NULL) {
1969 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1970 exit(1);
1971 }
1972
1973 monitor_init(chr, flags);
1974 return 0;
1975 }
1976
1977 static void monitor_parse(const char *optarg, const char *mode)
1978 {
1979 static int monitor_device_index = 0;
1980 QemuOpts *opts;
1981 const char *p;
1982 char label[32];
1983 int def = 0;
1984
1985 if (strstart(optarg, "chardev:", &p)) {
1986 snprintf(label, sizeof(label), "%s", p);
1987 } else {
1988 snprintf(label, sizeof(label), "compat_monitor%d",
1989 monitor_device_index);
1990 if (monitor_device_index == 0) {
1991 def = 1;
1992 }
1993 opts = qemu_chr_parse_compat(label, optarg);
1994 if (!opts) {
1995 fprintf(stderr, "parse error: %s\n", optarg);
1996 exit(1);
1997 }
1998 }
1999
2000 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2001 if (!opts) {
2002 fprintf(stderr, "duplicate chardev: %s\n", label);
2003 exit(1);
2004 }
2005 qemu_opt_set(opts, "mode", mode);
2006 qemu_opt_set(opts, "chardev", label);
2007 if (def)
2008 qemu_opt_set(opts, "default", "on");
2009 monitor_device_index++;
2010 }
2011
2012 struct device_config {
2013 enum {
2014 DEV_USB, /* -usbdevice */
2015 DEV_BT, /* -bt */
2016 DEV_SERIAL, /* -serial */
2017 DEV_PARALLEL, /* -parallel */
2018 DEV_VIRTCON, /* -virtioconsole */
2019 DEV_DEBUGCON, /* -debugcon */
2020 DEV_GDB, /* -gdb, -s */
2021 } type;
2022 const char *cmdline;
2023 Location loc;
2024 QTAILQ_ENTRY(device_config) next;
2025 };
2026 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2027
2028 static void add_device_config(int type, const char *cmdline)
2029 {
2030 struct device_config *conf;
2031
2032 conf = g_malloc0(sizeof(*conf));
2033 conf->type = type;
2034 conf->cmdline = cmdline;
2035 loc_save(&conf->loc);
2036 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2037 }
2038
2039 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2040 {
2041 struct device_config *conf;
2042 int rc;
2043
2044 QTAILQ_FOREACH(conf, &device_configs, next) {
2045 if (conf->type != type)
2046 continue;
2047 loc_push_restore(&conf->loc);
2048 rc = func(conf->cmdline);
2049 loc_pop(&conf->loc);
2050 if (0 != rc)
2051 return rc;
2052 }
2053 return 0;
2054 }
2055
2056 static int serial_parse(const char *devname)
2057 {
2058 static int index = 0;
2059 char label[32];
2060
2061 if (strcmp(devname, "none") == 0)
2062 return 0;
2063 if (index == MAX_SERIAL_PORTS) {
2064 fprintf(stderr, "qemu: too many serial ports\n");
2065 exit(1);
2066 }
2067 snprintf(label, sizeof(label), "serial%d", index);
2068 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2069 if (!serial_hds[index]) {
2070 fprintf(stderr, "qemu: could not connect serial device"
2071 " to character backend '%s'\n", devname);
2072 return -1;
2073 }
2074 index++;
2075 return 0;
2076 }
2077
2078 static int parallel_parse(const char *devname)
2079 {
2080 static int index = 0;
2081 char label[32];
2082
2083 if (strcmp(devname, "none") == 0)
2084 return 0;
2085 if (index == MAX_PARALLEL_PORTS) {
2086 fprintf(stderr, "qemu: too many parallel ports\n");
2087 exit(1);
2088 }
2089 snprintf(label, sizeof(label), "parallel%d", index);
2090 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2091 if (!parallel_hds[index]) {
2092 fprintf(stderr, "qemu: could not connect parallel device"
2093 " to character backend '%s'\n", devname);
2094 return -1;
2095 }
2096 index++;
2097 return 0;
2098 }
2099
2100 static int virtcon_parse(const char *devname)
2101 {
2102 QemuOptsList *device = qemu_find_opts("device");
2103 static int index = 0;
2104 char label[32];
2105 QemuOpts *bus_opts, *dev_opts;
2106
2107 if (strcmp(devname, "none") == 0)
2108 return 0;
2109 if (index == MAX_VIRTIO_CONSOLES) {
2110 fprintf(stderr, "qemu: too many virtio consoles\n");
2111 exit(1);
2112 }
2113
2114 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2115 if (arch_type == QEMU_ARCH_S390X) {
2116 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2117 } else {
2118 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2119 }
2120
2121 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2122 qemu_opt_set(dev_opts, "driver", "virtconsole");
2123
2124 snprintf(label, sizeof(label), "virtcon%d", index);
2125 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2126 if (!virtcon_hds[index]) {
2127 fprintf(stderr, "qemu: could not connect virtio console"
2128 " to character backend '%s'\n", devname);
2129 return -1;
2130 }
2131 qemu_opt_set(dev_opts, "chardev", label);
2132
2133 index++;
2134 return 0;
2135 }
2136
2137 static int debugcon_parse(const char *devname)
2138 {
2139 QemuOpts *opts;
2140
2141 if (!qemu_chr_new("debugcon", devname, NULL)) {
2142 exit(1);
2143 }
2144 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2145 if (!opts) {
2146 fprintf(stderr, "qemu: already have a debugcon device\n");
2147 exit(1);
2148 }
2149 qemu_opt_set(opts, "driver", "isa-debugcon");
2150 qemu_opt_set(opts, "chardev", "debugcon");
2151 return 0;
2152 }
2153
2154 static QEMUMachine *machine_parse(const char *name)
2155 {
2156 QEMUMachine *m, *machine = NULL;
2157
2158 if (name) {
2159 machine = find_machine(name);
2160 }
2161 if (machine) {
2162 return machine;
2163 }
2164 printf("Supported machines are:\n");
2165 for (m = first_machine; m != NULL; m = m->next) {
2166 if (m->alias) {
2167 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2168 }
2169 printf("%-20s %s%s\n", m->name, m->desc,
2170 m->is_default ? " (default)" : "");
2171 }
2172 exit(!name || !is_help_option(name));
2173 }
2174
2175 static int tcg_init(void)
2176 {
2177 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2178 return 0;
2179 }
2180
2181 static struct {
2182 const char *opt_name;
2183 const char *name;
2184 int (*available)(void);
2185 int (*init)(void);
2186 int *allowed;
2187 } accel_list[] = {
2188 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2189 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2190 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2191 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2192 };
2193
2194 static int configure_accelerator(void)
2195 {
2196 const char *p = NULL;
2197 char buf[10];
2198 int i, ret;
2199 bool accel_initialised = 0;
2200 bool init_failed = 0;
2201
2202 QemuOptsList *list = qemu_find_opts("machine");
2203 if (!QTAILQ_EMPTY(&list->head)) {
2204 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2205 }
2206
2207 if (p == NULL) {
2208 /* Use the default "accelerator", tcg */
2209 p = "tcg";
2210 }
2211
2212 while (!accel_initialised && *p != '\0') {
2213 if (*p == ':') {
2214 p++;
2215 }
2216 p = get_opt_name(buf, sizeof (buf), p, ':');
2217 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2218 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2219 *(accel_list[i].allowed) = 1;
2220 ret = accel_list[i].init();
2221 if (ret < 0) {
2222 init_failed = 1;
2223 if (!accel_list[i].available()) {
2224 printf("%s not supported for this target\n",
2225 accel_list[i].name);
2226 } else {
2227 fprintf(stderr, "failed to initialize %s: %s\n",
2228 accel_list[i].name,
2229 strerror(-ret));
2230 }
2231 *(accel_list[i].allowed) = 0;
2232 } else {
2233 accel_initialised = 1;
2234 }
2235 break;
2236 }
2237 }
2238 if (i == ARRAY_SIZE(accel_list)) {
2239 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2240 }
2241 }
2242
2243 if (!accel_initialised) {
2244 fprintf(stderr, "No accelerator found!\n");
2245 exit(1);
2246 }
2247
2248 if (init_failed) {
2249 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2250 }
2251
2252 return !accel_initialised;
2253 }
2254
2255 void qemu_add_exit_notifier(Notifier *notify)
2256 {
2257 notifier_list_add(&exit_notifiers, notify);
2258 }
2259
2260 void qemu_remove_exit_notifier(Notifier *notify)
2261 {
2262 notifier_remove(notify);
2263 }
2264
2265 static void qemu_run_exit_notifiers(void)
2266 {
2267 notifier_list_notify(&exit_notifiers, NULL);
2268 }
2269
2270 void qemu_add_machine_init_done_notifier(Notifier *notify)
2271 {
2272 notifier_list_add(&machine_init_done_notifiers, notify);
2273 }
2274
2275 static void qemu_run_machine_init_done_notifiers(void)
2276 {
2277 notifier_list_notify(&machine_init_done_notifiers, NULL);
2278 }
2279
2280 static const QEMUOption *lookup_opt(int argc, char **argv,
2281 const char **poptarg, int *poptind)
2282 {
2283 const QEMUOption *popt;
2284 int optind = *poptind;
2285 char *r = argv[optind];
2286 const char *optarg;
2287
2288 loc_set_cmdline(argv, optind, 1);
2289 optind++;
2290 /* Treat --foo the same as -foo. */
2291 if (r[1] == '-')
2292 r++;
2293 popt = qemu_options;
2294 for(;;) {
2295 if (!popt->name) {
2296 error_report("invalid option");
2297 exit(1);
2298 }
2299 if (!strcmp(popt->name, r + 1))
2300 break;
2301 popt++;
2302 }
2303 if (popt->flags & HAS_ARG) {
2304 if (optind >= argc) {
2305 error_report("requires an argument");
2306 exit(1);
2307 }
2308 optarg = argv[optind++];
2309 loc_set_cmdline(argv, optind - 2, 2);
2310 } else {
2311 optarg = NULL;
2312 }
2313
2314 *poptarg = optarg;
2315 *poptind = optind;
2316
2317 return popt;
2318 }
2319
2320 static gpointer malloc_and_trace(gsize n_bytes)
2321 {
2322 void *ptr = malloc(n_bytes);
2323 trace_g_malloc(n_bytes, ptr);
2324 return ptr;
2325 }
2326
2327 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2328 {
2329 void *ptr = realloc(mem, n_bytes);
2330 trace_g_realloc(mem, n_bytes, ptr);
2331 return ptr;
2332 }
2333
2334 static void free_and_trace(gpointer mem)
2335 {
2336 trace_g_free(mem);
2337 free(mem);
2338 }
2339
2340 int qemu_init_main_loop(void)
2341 {
2342 return main_loop_init();
2343 }
2344
2345 int main(int argc, char **argv, char **envp)
2346 {
2347 int i;
2348 int snapshot, linux_boot;
2349 const char *icount_option = NULL;
2350 const char *initrd_filename;
2351 const char *kernel_filename, *kernel_cmdline;
2352 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2353 DisplayState *ds;
2354 DisplayChangeListener *dcl;
2355 char hdachs_params[512]; /* save -hdachs to apply to later -hda */
2356 QemuOpts *hda_opts = NULL; /* save -hda to be modified by later -hdachs */
2357 QemuOpts *opts, *machine_opts;
2358 QemuOptsList *olist;
2359 int optind;
2360 const char *optarg;
2361 const char *loadvm = NULL;
2362 QEMUMachine *machine;
2363 const char *cpu_model;
2364 const char *vga_model = "none";
2365 const char *pid_file = NULL;
2366 const char *incoming = NULL;
2367 #ifdef CONFIG_VNC
2368 int show_vnc_port = 0;
2369 #endif
2370 bool defconfig = true;
2371 bool userconfig = true;
2372 const char *log_mask = NULL;
2373 const char *log_file = NULL;
2374 GMemVTable mem_trace = {
2375 .malloc = malloc_and_trace,
2376 .realloc = realloc_and_trace,
2377 .free = free_and_trace,
2378 };
2379 const char *trace_events = NULL;
2380 const char *trace_file = NULL;
2381
2382 atexit(qemu_run_exit_notifiers);
2383 error_set_progname(argv[0]);
2384
2385 g_mem_set_vtable(&mem_trace);
2386 if (!g_thread_supported()) {
2387 #if !GLIB_CHECK_VERSION(2, 31, 0)
2388 g_thread_init(NULL);
2389 #else
2390 fprintf(stderr, "glib threading failed to initialize.\n");
2391 exit(1);
2392 #endif
2393 }
2394
2395 module_call_init(MODULE_INIT_QOM);
2396
2397 runstate_init();
2398
2399 init_clocks();
2400 rtc_clock = host_clock;
2401
2402 qemu_cache_utils_init(envp);
2403
2404 QLIST_INIT (&vm_change_state_head);
2405 os_setup_early_signal_handling();
2406
2407 module_call_init(MODULE_INIT_MACHINE);
2408 machine = find_default_machine();
2409 cpu_model = NULL;
2410 ram_size = 0;
2411 snapshot = 0;
2412 snprintf(hdachs_params, sizeof(hdachs_params), "%s", HD_OPTS);
2413
2414 for (i = 0; i < MAX_NODES; i++) {
2415 node_mem[i] = 0;
2416 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2417 }
2418
2419 nb_numa_nodes = 0;
2420 nb_nics = 0;
2421
2422 autostart= 1;
2423
2424 /* first pass of option parsing */
2425 optind = 1;
2426 while (optind < argc) {
2427 if (argv[optind][0] != '-') {
2428 /* disk image */
2429 optind++;
2430 continue;
2431 } else {
2432 const QEMUOption *popt;
2433
2434 popt = lookup_opt(argc, argv, &optarg, &optind);
2435 switch (popt->index) {
2436 case QEMU_OPTION_nodefconfig:
2437 defconfig = false;
2438 break;
2439 case QEMU_OPTION_nouserconfig:
2440 userconfig = false;
2441 break;
2442 }
2443 }
2444 }
2445
2446 if (defconfig) {
2447 int ret;
2448 ret = qemu_read_default_config_files(userconfig);
2449 if (ret < 0) {
2450 exit(1);
2451 }
2452 }
2453
2454 /* second pass of option parsing */
2455 optind = 1;
2456 for(;;) {
2457 if (optind >= argc)
2458 break;
2459 if (argv[optind][0] != '-') {
2460 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], hdachs_params);
2461 } else {
2462 const QEMUOption *popt;
2463
2464 popt = lookup_opt(argc, argv, &optarg, &optind);
2465 if (!(popt->arch_mask & arch_type)) {
2466 printf("Option %s not supported for this target\n", popt->name);
2467 exit(1);
2468 }
2469 switch(popt->index) {
2470 case QEMU_OPTION_M:
2471 machine = machine_parse(optarg);
2472 break;
2473 case QEMU_OPTION_cpu:
2474 /* hw initialization will check this */
2475 cpu_model = optarg;
2476 break;
2477 case QEMU_OPTION_hda:
2478 hda_opts = drive_add(IF_DEFAULT, 0, optarg, hdachs_params);
2479 break;
2480 case QEMU_OPTION_hdb:
2481 case QEMU_OPTION_hdc:
2482 case QEMU_OPTION_hdd:
2483 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2484 HD_OPTS);
2485 break;
2486 case QEMU_OPTION_drive:
2487 if (drive_def(optarg) == NULL) {
2488 exit(1);
2489 }
2490 break;
2491 case QEMU_OPTION_set:
2492 if (qemu_set_option(optarg) != 0)
2493 exit(1);
2494 break;
2495 case QEMU_OPTION_global:
2496 if (qemu_global_option(optarg) != 0)
2497 exit(1);
2498 break;
2499 case QEMU_OPTION_mtdblock:
2500 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2501 break;
2502 case QEMU_OPTION_sd:
2503 drive_add(IF_SD, 0, optarg, SD_OPTS);
2504 break;
2505 case QEMU_OPTION_pflash:
2506 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2507 break;
2508 case QEMU_OPTION_snapshot:
2509 snapshot = 1;
2510 break;
2511 case QEMU_OPTION_hdachs:
2512 {
2513 int cyls, heads, secs, translation;
2514 const char *p;
2515 cyls = heads = secs = 0;
2516 translation = BIOS_ATA_TRANSLATION_AUTO;
2517 p = optarg;
2518 cyls = strtol(p, (char **)&p, 0);
2519 if (cyls < 1 || cyls > 16383)
2520 goto chs_fail;
2521 if (*p != ',')
2522 goto chs_fail;
2523 p++;
2524 heads = strtol(p, (char **)&p, 0);
2525 if (heads < 1 || heads > 16)
2526 goto chs_fail;
2527 if (*p != ',')
2528 goto chs_fail;
2529 p++;
2530 secs = strtol(p, (char **)&p, 0);
2531 if (secs < 1 || secs > 63)
2532 goto chs_fail;
2533 if (*p == ',') {
2534 p++;
2535 if (!strcmp(p, "none"))
2536 translation = BIOS_ATA_TRANSLATION_NONE;
2537 else if (!strcmp(p, "lba"))
2538 translation = BIOS_ATA_TRANSLATION_LBA;
2539 else if (!strcmp(p, "auto"))
2540 translation = BIOS_ATA_TRANSLATION_AUTO;
2541 else
2542 goto chs_fail;
2543 } else if (*p != '\0') {
2544 chs_fail:
2545 fprintf(stderr, "qemu: invalid physical CHS format\n");
2546 exit(1);
2547 }
2548 snprintf(hdachs_params, sizeof(hdachs_params),
2549 "%s,cyls=%d,heads=%d,secs=%d%s",
2550 HD_OPTS , cyls, heads, secs,
2551 translation == BIOS_ATA_TRANSLATION_LBA ?
2552 ",trans=lba" :
2553 translation == BIOS_ATA_TRANSLATION_NONE ?
2554 ",trans=none" : "");
2555 if (hda_opts != NULL) {
2556 char num[16];
2557 snprintf(num, sizeof(num), "%d", cyls);
2558 qemu_opt_set(hda_opts, "cyls", num);
2559 snprintf(num, sizeof(num), "%d", heads);
2560 qemu_opt_set(hda_opts, "heads", num);
2561 snprintf(num, sizeof(num), "%d", secs);
2562 qemu_opt_set(hda_opts, "secs", num);
2563 if (translation == BIOS_ATA_TRANSLATION_LBA)
2564 qemu_opt_set(hda_opts, "trans", "lba");
2565 if (translation == BIOS_ATA_TRANSLATION_NONE)
2566 qemu_opt_set(hda_opts, "trans", "none");
2567 }
2568 }
2569 break;
2570 case QEMU_OPTION_numa:
2571 if (nb_numa_nodes >= MAX_NODES) {
2572 fprintf(stderr, "qemu: too many NUMA nodes\n");
2573 exit(1);
2574 }
2575 numa_add(optarg);
2576 break;
2577 case QEMU_OPTION_display:
2578 display_type = select_display(optarg);
2579 break;
2580 case QEMU_OPTION_nographic:
2581 display_type = DT_NOGRAPHIC;
2582 break;
2583 case QEMU_OPTION_curses:
2584 #ifdef CONFIG_CURSES
2585 display_type = DT_CURSES;
2586 #else
2587 fprintf(stderr, "Curses support is disabled\n");
2588 exit(1);
2589 #endif
2590 break;
2591 case QEMU_OPTION_portrait:
2592 graphic_rotate = 90;
2593 break;
2594 case QEMU_OPTION_rotate:
2595 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2596 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2597 graphic_rotate != 180 && graphic_rotate != 270) {
2598 fprintf(stderr,
2599 "qemu: only 90, 180, 270 deg rotation is available\n");
2600 exit(1);
2601 }
2602 break;
2603 case QEMU_OPTION_kernel:
2604 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2605 break;
2606 case QEMU_OPTION_initrd:
2607 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2608 break;
2609 case QEMU_OPTION_append:
2610 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2611 break;
2612 case QEMU_OPTION_dtb:
2613 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2614 break;
2615 case QEMU_OPTION_cdrom:
2616 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2617 break;
2618 case QEMU_OPTION_boot:
2619 {
2620 static const char * const params[] = {
2621 "order", "once", "menu",
2622 "splash", "splash-time", NULL
2623 };
2624 char buf[sizeof(boot_devices)];
2625 char *standard_boot_devices;
2626 int legacy = 0;
2627
2628 if (!strchr(optarg, '=')) {
2629 legacy = 1;
2630 pstrcpy(buf, sizeof(buf), optarg);
2631 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2632 fprintf(stderr,
2633 "qemu: unknown boot parameter '%s' in '%s'\n",
2634 buf, optarg);
2635 exit(1);
2636 }
2637
2638 if (legacy ||
2639 get_param_value(buf, sizeof(buf), "order", optarg)) {
2640 validate_bootdevices(buf);
2641 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2642 }
2643 if (!legacy) {
2644 if (get_param_value(buf, sizeof(buf),
2645 "once", optarg)) {
2646 validate_bootdevices(buf);
2647 standard_boot_devices = g_strdup(boot_devices);
2648 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2649 qemu_register_reset(restore_boot_devices,
2650 standard_boot_devices);
2651 }
2652 if (get_param_value(buf, sizeof(buf),
2653 "menu", optarg)) {
2654 if (!strcmp(buf, "on")) {
2655 boot_menu = 1;
2656 } else if (!strcmp(buf, "off")) {
2657 boot_menu = 0;
2658 } else {
2659 fprintf(stderr,
2660 "qemu: invalid option value '%s'\n",
2661 buf);
2662 exit(1);
2663 }
2664 }
2665 qemu_opts_parse(qemu_find_opts("boot-opts"),
2666 optarg, 0);
2667 }
2668 }
2669 break;
2670 case QEMU_OPTION_fda:
2671 case QEMU_OPTION_fdb:
2672 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2673 optarg, FD_OPTS);
2674 break;
2675 case QEMU_OPTION_no_fd_bootchk:
2676 fd_bootchk = 0;
2677 break;
2678 case QEMU_OPTION_netdev:
2679 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2680 exit(1);
2681 }
2682 break;
2683 case QEMU_OPTION_net:
2684 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2685 exit(1);
2686 }
2687 break;
2688 #ifdef CONFIG_LIBISCSI
2689 case QEMU_OPTION_iscsi:
2690 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2691 if (!opts) {
2692 exit(1);
2693 }
2694 break;
2695 #endif
2696 #ifdef CONFIG_SLIRP
2697 case QEMU_OPTION_tftp:
2698 legacy_tftp_prefix = optarg;
2699 break;
2700 case QEMU_OPTION_bootp:
2701 legacy_bootp_filename = optarg;
2702 break;
2703 case QEMU_OPTION_redir:
2704 if (net_slirp_redir(optarg) < 0)
2705 exit(1);
2706 break;
2707 #endif
2708 case QEMU_OPTION_bt:
2709 add_device_config(DEV_BT, optarg);
2710 break;
2711 case QEMU_OPTION_audio_help:
2712 if (!(audio_available())) {
2713 printf("Option %s not supported for this target\n", popt->name);
2714 exit(1);
2715 }
2716 AUD_help ();
2717 exit (0);
2718 break;
2719 case QEMU_OPTION_soundhw:
2720 if (!(audio_available())) {
2721 printf("Option %s not supported for this target\n", popt->name);
2722 exit(1);
2723 }
2724 select_soundhw (optarg);
2725 break;
2726 case QEMU_OPTION_h:
2727 help(0);
2728 break;
2729 case QEMU_OPTION_version:
2730 version();
2731 exit(0);
2732 break;
2733 case QEMU_OPTION_m: {
2734 int64_t value;
2735 uint64_t sz;
2736 char *end;
2737
2738 value = strtosz(optarg, &end);
2739 if (value < 0 || *end) {
2740 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2741 exit(1);
2742 }
2743 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2744 ram_size = sz;
2745 if (ram_size != sz) {
2746 fprintf(stderr, "qemu: ram size too large\n");
2747 exit(1);
2748 }
2749 break;
2750 }
2751 case QEMU_OPTION_mempath:
2752 mem_path = optarg;
2753 break;
2754 #ifdef MAP_POPULATE
2755 case QEMU_OPTION_mem_prealloc:
2756 mem_prealloc = 1;
2757 break;
2758 #endif
2759 case QEMU_OPTION_d:
2760 log_mask = optarg;
2761 break;
2762 case QEMU_OPTION_D:
2763 log_file = optarg;
2764 break;
2765 case QEMU_OPTION_s:
2766 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2767 break;
2768 case QEMU_OPTION_gdb:
2769 add_device_config(DEV_GDB, optarg);
2770 break;
2771 case QEMU_OPTION_L:
2772 data_dir = optarg;
2773 break;
2774 case QEMU_OPTION_bios:
2775 bios_name = optarg;
2776 break;
2777 case QEMU_OPTION_singlestep:
2778 singlestep = 1;
2779 break;
2780 case QEMU_OPTION_S:
2781 autostart = 0;
2782 break;
2783 case QEMU_OPTION_k:
2784 keyboard_layout = optarg;
2785 break;
2786 case QEMU_OPTION_localtime:
2787 rtc_utc = 0;
2788 break;
2789 case QEMU_OPTION_vga:
2790 vga_model = optarg;
2791 default_vga = 0;
2792 break;
2793 case QEMU_OPTION_g:
2794 {
2795 const char *p;
2796 int w, h, depth;
2797 p = optarg;
2798 w = strtol(p, (char **)&p, 10);
2799 if (w <= 0) {
2800 graphic_error:
2801 fprintf(stderr, "qemu: invalid resolution or depth\n");
2802 exit(1);
2803 }
2804 if (*p != 'x')
2805 goto graphic_error;
2806 p++;
2807 h = strtol(p, (char **)&p, 10);
2808 if (h <= 0)
2809 goto graphic_error;
2810 if (*p == 'x') {
2811 p++;
2812 depth = strtol(p, (char **)&p, 10);
2813 if (depth != 8 && depth != 15 && depth != 16 &&
2814 depth != 24 && depth != 32)
2815 goto graphic_error;
2816 } else if (*p == '\0') {
2817 depth = graphic_depth;
2818 } else {
2819 goto graphic_error;
2820 }
2821
2822 graphic_width = w;
2823 graphic_height = h;
2824 graphic_depth = depth;
2825 }
2826 break;
2827 case QEMU_OPTION_echr:
2828 {
2829 char *r;
2830 term_escape_char = strtol(optarg, &r, 0);
2831 if (r == optarg)
2832 printf("Bad argument to echr\n");
2833 break;
2834 }
2835 case QEMU_OPTION_monitor:
2836 monitor_parse(optarg, "readline");
2837 default_monitor = 0;
2838 break;
2839 case QEMU_OPTION_qmp:
2840 monitor_parse(optarg, "control");
2841 default_monitor = 0;
2842 break;
2843 case QEMU_OPTION_mon:
2844 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2845 if (!opts) {
2846 exit(1);
2847 }
2848 default_monitor = 0;
2849 break;
2850 case QEMU_OPTION_chardev:
2851 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2852 if (!opts) {
2853 exit(1);
2854 }
2855 break;
2856 case QEMU_OPTION_fsdev:
2857 olist = qemu_find_opts("fsdev");
2858 if (!olist) {
2859 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2860 exit(1);
2861 }
2862 opts = qemu_opts_parse(olist, optarg, 1);
2863 if (!opts) {
2864 fprintf(stderr, "parse error: %s\n", optarg);
2865 exit(1);
2866 }
2867 break;
2868 case QEMU_OPTION_virtfs: {
2869 QemuOpts *fsdev;
2870 QemuOpts *device;
2871 const char *writeout, *sock_fd, *socket;
2872
2873 olist = qemu_find_opts("virtfs");
2874 if (!olist) {
2875 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2876 exit(1);
2877 }
2878 opts = qemu_opts_parse(olist, optarg, 1);
2879 if (!opts) {
2880 fprintf(stderr, "parse error: %s\n", optarg);
2881 exit(1);
2882 }
2883
2884 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2885 qemu_opt_get(opts, "mount_tag") == NULL) {
2886 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2887 exit(1);
2888 }
2889 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2890 qemu_opt_get(opts, "mount_tag"),
2891 1, NULL);
2892 if (!fsdev) {
2893 fprintf(stderr, "duplicate fsdev id: %s\n",
2894 qemu_opt_get(opts, "mount_tag"));
2895 exit(1);
2896 }
2897
2898 writeout = qemu_opt_get(opts, "writeout");
2899 if (writeout) {
2900 #ifdef CONFIG_SYNC_FILE_RANGE
2901 qemu_opt_set(fsdev, "writeout", writeout);
2902 #else
2903 fprintf(stderr, "writeout=immediate not supported on "
2904 "this platform\n");
2905 exit(1);
2906 #endif
2907 }
2908 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2909 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2910 qemu_opt_set(fsdev, "security_model",
2911 qemu_opt_get(opts, "security_model"));
2912 socket = qemu_opt_get(opts, "socket");
2913 if (socket) {
2914 qemu_opt_set(fsdev, "socket", socket);
2915 }
2916 sock_fd = qemu_opt_get(opts, "sock_fd");
2917 if (sock_fd) {
2918 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2919 }
2920
2921 qemu_opt_set_bool(fsdev, "readonly",
2922 qemu_opt_get_bool(opts, "readonly", 0));
2923 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2924 NULL);
2925 qemu_opt_set(device, "driver", "virtio-9p-pci");
2926 qemu_opt_set(device, "fsdev",
2927 qemu_opt_get(opts, "mount_tag"));
2928 qemu_opt_set(device, "mount_tag",
2929 qemu_opt_get(opts, "mount_tag"));
2930 break;
2931 }
2932 case QEMU_OPTION_virtfs_synth: {
2933 QemuOpts *fsdev;
2934 QemuOpts *device;
2935
2936 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2937 1, NULL);
2938 if (!fsdev) {
2939 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2940 exit(1);
2941 }
2942 qemu_opt_set(fsdev, "fsdriver", "synth");
2943
2944 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2945 NULL);
2946 qemu_opt_set(device, "driver", "virtio-9p-pci");
2947 qemu_opt_set(device, "fsdev", "v_synth");
2948 qemu_opt_set(device, "mount_tag", "v_synth");
2949 break;
2950 }
2951 case QEMU_OPTION_serial:
2952 add_device_config(DEV_SERIAL, optarg);
2953 default_serial = 0;
2954 if (strncmp(optarg, "mon:", 4) == 0) {
2955 default_monitor = 0;
2956 }
2957 break;
2958 case QEMU_OPTION_watchdog:
2959 if (watchdog) {
2960 fprintf(stderr,
2961 "qemu: only one watchdog option may be given\n");
2962 return 1;
2963 }
2964 watchdog = optarg;
2965 break;
2966 case QEMU_OPTION_watchdog_action:
2967 if (select_watchdog_action(optarg) == -1) {
2968 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2969 exit(1);
2970 }
2971 break;
2972 case QEMU_OPTION_virtiocon:
2973 add_device_config(DEV_VIRTCON, optarg);
2974 default_virtcon = 0;
2975 if (strncmp(optarg, "mon:", 4) == 0) {
2976 default_monitor = 0;
2977 }
2978 break;
2979 case QEMU_OPTION_parallel:
2980 add_device_config(DEV_PARALLEL, optarg);
2981 default_parallel = 0;
2982 if (strncmp(optarg, "mon:", 4) == 0) {
2983 default_monitor = 0;
2984 }
2985 break;
2986 case QEMU_OPTION_debugcon:
2987 add_device_config(DEV_DEBUGCON, optarg);
2988 break;
2989 case QEMU_OPTION_loadvm:
2990 loadvm = optarg;
2991 break;
2992 case QEMU_OPTION_full_screen:
2993 full_screen = 1;
2994 break;
2995 #ifdef CONFIG_SDL
2996 case QEMU_OPTION_no_frame:
2997 no_frame = 1;
2998 break;
2999 case QEMU_OPTION_alt_grab:
3000 alt_grab = 1;
3001 break;
3002 case QEMU_OPTION_ctrl_grab:
3003 ctrl_grab = 1;
3004 break;
3005 case QEMU_OPTION_no_quit:
3006 no_quit = 1;
3007 break;
3008 case QEMU_OPTION_sdl:
3009 display_type = DT_SDL;
3010 break;
3011 #else
3012 case QEMU_OPTION_no_frame:
3013 case QEMU_OPTION_alt_grab:
3014 case QEMU_OPTION_ctrl_grab:
3015 case QEMU_OPTION_no_quit:
3016 case QEMU_OPTION_sdl:
3017 fprintf(stderr, "SDL support is disabled\n");
3018 exit(1);
3019 #endif
3020 case QEMU_OPTION_pidfile:
3021 pid_file = optarg;
3022 break;
3023 case QEMU_OPTION_win2k_hack:
3024 win2k_install_hack = 1;
3025 break;
3026 case QEMU_OPTION_rtc_td_hack: {
3027 static GlobalProperty slew_lost_ticks[] = {
3028 {
3029 .driver = "mc146818rtc",
3030 .property = "lost_tick_policy",
3031 .value = "slew",
3032 },
3033 { /* end of list */ }
3034 };
3035
3036 qdev_prop_register_global_list(slew_lost_ticks);
3037 break;
3038 }
3039 case QEMU_OPTION_acpitable:
3040 do_acpitable_option(optarg);
3041 break;
3042 case QEMU_OPTION_smbios:
3043 do_smbios_option(optarg);
3044 break;
3045 case QEMU_OPTION_enable_kvm:
3046 olist = qemu_find_opts("machine");
3047 qemu_opts_parse(olist, "accel=kvm", 0);
3048 break;
3049 case QEMU_OPTION_machine:
3050 olist = qemu_find_opts("machine");
3051 opts = qemu_opts_parse(olist, optarg, 1);
3052 if (!opts) {
3053 fprintf(stderr, "parse error: %s\n", optarg);
3054 exit(1);
3055 }
3056 optarg = qemu_opt_get(opts, "type");
3057 if (optarg) {
3058 machine = machine_parse(optarg);
3059 }
3060 break;
3061 case QEMU_OPTION_usb:
3062 usb_enabled = 1;
3063 break;
3064 case QEMU_OPTION_usbdevice:
3065 usb_enabled = 1;
3066 add_device_config(DEV_USB, optarg);
3067 break;
3068 case QEMU_OPTION_device:
3069 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3070 exit(1);
3071 }
3072 break;
3073 case QEMU_OPTION_smp:
3074 smp_parse(optarg);
3075 if (smp_cpus < 1) {
3076 fprintf(stderr, "Invalid number of CPUs\n");
3077 exit(1);
3078 }
3079 if (max_cpus < smp_cpus) {
3080 fprintf(stderr, "maxcpus must be equal to or greater than "
3081 "smp\n");
3082 exit(1);
3083 }
3084 if (max_cpus > 255) {
3085 fprintf(stderr, "Unsupported number of maxcpus\n");
3086 exit(1);
3087 }
3088 break;
3089 case QEMU_OPTION_vnc:
3090 #ifdef CONFIG_VNC
3091 display_remote++;
3092 vnc_display = optarg;
3093 #else
3094 fprintf(stderr, "VNC support is disabled\n");
3095 exit(1);
3096 #endif
3097 break;
3098 case QEMU_OPTION_no_acpi:
3099 acpi_enabled = 0;
3100 break;
3101 case QEMU_OPTION_no_hpet:
3102 no_hpet = 1;
3103 break;
3104 case QEMU_OPTION_balloon:
3105 if (balloon_parse(optarg) < 0) {
3106 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3107 exit(1);
3108 }
3109 break;
3110 case QEMU_OPTION_no_reboot:
3111 no_reboot = 1;
3112 break;
3113 case QEMU_OPTION_no_shutdown:
3114 no_shutdown = 1;
3115 break;
3116 case QEMU_OPTION_show_cursor:
3117 cursor_hide = 0;
3118 break;
3119 case QEMU_OPTION_uuid:
3120 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3121 fprintf(stderr, "Fail to parse UUID string."
3122 " Wrong format.\n");
3123 exit(1);
3124 }
3125 break;
3126 case QEMU_OPTION_option_rom:
3127 if (nb_option_roms >= MAX_OPTION_ROMS) {
3128 fprintf(stderr, "Too many option ROMs\n");
3129 exit(1);
3130 }
3131 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3132 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3133 option_rom[nb_option_roms].bootindex =
3134 qemu_opt_get_number(opts, "bootindex", -1);
3135 if (!option_rom[nb_option_roms].name) {
3136 fprintf(stderr, "Option ROM file is not specified\n");
3137 exit(1);
3138 }
3139 nb_option_roms++;
3140 break;
3141 case QEMU_OPTION_semihosting:
3142 semihosting_enabled = 1;
3143 break;
3144 case QEMU_OPTION_name:
3145 qemu_name = g_strdup(optarg);
3146 {
3147 char *p = strchr(qemu_name, ',');
3148 if (p != NULL) {
3149 *p++ = 0;
3150 if (strncmp(p, "process=", 8)) {
3151 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3152 exit(1);
3153 }
3154 p += 8;
3155 os_set_proc_name(p);
3156 }
3157 }
3158 break;
3159 case QEMU_OPTION_prom_env:
3160 if (nb_prom_envs >= MAX_PROM_ENVS) {
3161 fprintf(stderr, "Too many prom variables\n");
3162 exit(1);
3163 }
3164 prom_envs[nb_prom_envs] = optarg;
3165 nb_prom_envs++;
3166 break;
3167 case QEMU_OPTION_old_param:
3168 old_param = 1;
3169 break;
3170 case QEMU_OPTION_clock:
3171 configure_alarms(optarg);
3172 break;
3173 case QEMU_OPTION_startdate:
3174 configure_rtc_date_offset(optarg, 1);
3175 break;
3176 case QEMU_OPTION_rtc:
3177 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3178 if (!opts) {
3179 exit(1);
3180 }
3181 configure_rtc(opts);
3182 break;
3183 case QEMU_OPTION_tb_size:
3184 tcg_tb_size = strtol(optarg, NULL, 0);
3185 if (tcg_tb_size < 0) {
3186 tcg_tb_size = 0;
3187 }
3188 break;
3189 case QEMU_OPTION_icount:
3190 icount_option = optarg;
3191 break;
3192 case QEMU_OPTION_incoming:
3193 incoming = optarg;
3194 runstate_set(RUN_STATE_INMIGRATE);
3195 break;
3196 case QEMU_OPTION_nodefaults:
3197 default_serial = 0;
3198 default_parallel = 0;
3199 default_virtcon = 0;
3200 default_monitor = 0;
3201 default_net = 0;
3202 default_floppy = 0;
3203 default_cdrom = 0;
3204 default_sdcard = 0;
3205 default_vga = 0;
3206 break;
3207 case QEMU_OPTION_xen_domid:
3208 if (!(xen_available())) {
3209 printf("Option %s not supported for this target\n", popt->name);
3210 exit(1);
3211 }
3212 xen_domid = atoi(optarg);
3213 break;
3214 case QEMU_OPTION_xen_create:
3215 if (!(xen_available())) {
3216 printf("Option %s not supported for this target\n", popt->name);
3217 exit(1);
3218 }
3219 xen_mode = XEN_CREATE;
3220 break;
3221 case QEMU_OPTION_xen_attach:
3222 if (!(xen_available())) {
3223 printf("Option %s not supported for this target\n", popt->name);
3224 exit(1);
3225 }
3226 xen_mode = XEN_ATTACH;
3227 break;
3228 case QEMU_OPTION_trace:
3229 {
3230 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3231 if (!opts) {
3232 exit(1);
3233 }
3234 trace_events = qemu_opt_get(opts, "events");
3235 trace_file = qemu_opt_get(opts, "file");
3236 break;
3237 }
3238 case QEMU_OPTION_readconfig:
3239 {
3240 int ret = qemu_read_config_file(optarg);
3241 if (ret < 0) {
3242 fprintf(stderr, "read config %s: %s\n", optarg,
3243 strerror(-ret));
3244 exit(1);
3245 }
3246 break;
3247 }
3248 case QEMU_OPTION_spice:
3249 olist = qemu_find_opts("spice");
3250 if (!olist) {
3251 fprintf(stderr, "spice is not supported by this qemu build.\n");
3252 exit(1);
3253 }
3254 opts = qemu_opts_parse(olist, optarg, 0);
3255 if (!opts) {
3256 fprintf(stderr, "parse error: %s\n", optarg);
3257 exit(1);
3258 }
3259 break;
3260 case QEMU_OPTION_writeconfig:
3261 {
3262 FILE *fp;
3263 if (strcmp(optarg, "-") == 0) {
3264 fp = stdout;
3265 } else {
3266 fp = fopen(optarg, "w");
3267 if (fp == NULL) {
3268 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3269 exit(1);
3270 }
3271 }
3272 qemu_config_write(fp);
3273 fclose(fp);
3274 break;
3275 }
3276 case QEMU_OPTION_qtest:
3277 qtest_chrdev = optarg;
3278 break;
3279 case QEMU_OPTION_qtest_log:
3280 qtest_log = optarg;
3281 break;
3282 case QEMU_OPTION_sandbox:
3283 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3284 if (!opts) {
3285 exit(0);
3286 }
3287 break;
3288 default:
3289 os_parse_cmd_args(popt->index, optarg);
3290 }
3291 }
3292 }
3293 loc_set_none();
3294
3295 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3296 exit(1);
3297 }
3298
3299 if (machine == NULL) {
3300 fprintf(stderr, "No machine found.\n");
3301 exit(1);
3302 }
3303
3304 if (machine->hw_version) {
3305 qemu_set_version(machine->hw_version);
3306 }
3307
3308 /* Init CPU def lists, based on config
3309 * - Must be called after all the qemu_read_config_file() calls
3310 * - Must be called before list_cpus()
3311 * - Must be called before machine->init()
3312 */
3313 cpudef_init();
3314
3315 if (cpu_model && is_help_option(cpu_model)) {
3316 list_cpus(stdout, &fprintf, cpu_model);
3317 exit(0);
3318 }
3319
3320 /* Open the logfile at this point, if necessary. We can't open the logfile
3321 * when encountering either of the logging options (-d or -D) because the
3322 * other one may be encountered later on the command line, changing the
3323 * location or level of logging.
3324 */
3325 if (log_mask) {
3326 if (log_file) {
3327 set_cpu_log_filename(log_file);
3328 }
3329 set_cpu_log(log_mask);
3330 }
3331
3332 if (!trace_backend_init(trace_events, trace_file)) {
3333 exit(1);
3334 }
3335
3336 /* If no data_dir is specified then try to find it relative to the
3337 executable path. */
3338 if (!data_dir) {
3339 data_dir = os_find_datadir(argv[0]);
3340 }
3341 /* If all else fails use the install path specified when building. */
3342 if (!data_dir) {
3343 data_dir = CONFIG_QEMU_DATADIR;
3344 }
3345
3346 /*
3347 * Default to max_cpus = smp_cpus, in case the user doesn't
3348 * specify a max_cpus value.
3349 */
3350 if (!max_cpus)
3351 max_cpus = smp_cpus;
3352
3353 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3354 if (smp_cpus > machine->max_cpus) {
3355 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3356 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3357 machine->max_cpus);
3358 exit(1);
3359 }
3360
3361 /*
3362 * Get the default machine options from the machine if it is not already
3363 * specified either by the configuration file or by the command line.
3364 */
3365 if (machine->default_machine_opts) {
3366 qemu_opts_set_defaults(qemu_find_opts("machine"),
3367 machine->default_machine_opts, 0);
3368 }
3369
3370 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3371 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3372
3373 if (machine->no_serial) {
3374 default_serial = 0;
3375 }
3376 if (machine->no_parallel) {
3377 default_parallel = 0;
3378 }
3379 if (!machine->use_virtcon) {
3380 default_virtcon = 0;
3381 }
3382 if (machine->no_floppy) {
3383 default_floppy = 0;
3384 }
3385 if (machine->no_cdrom) {
3386 default_cdrom = 0;
3387 }
3388 if (machine->no_sdcard) {
3389 default_sdcard = 0;
3390 }
3391
3392 if (display_type == DT_NOGRAPHIC) {
3393 if (default_parallel)
3394 add_device_config(DEV_PARALLEL, "null");
3395 if (default_serial && default_monitor) {
3396 add_device_config(DEV_SERIAL, "mon:stdio");
3397 } else if (default_virtcon && default_monitor) {
3398 add_device_config(DEV_VIRTCON, "mon:stdio");
3399 } else {
3400 if (default_serial)
3401 add_device_config(DEV_SERIAL, "stdio");
3402 if (default_virtcon)
3403 add_device_config(DEV_VIRTCON, "stdio");
3404 if (default_monitor)
3405 monitor_parse("stdio", "readline");
3406 }
3407 } else {
3408 if (default_serial)
3409 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3410 if (default_parallel)
3411 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3412 if (default_monitor)
3413 monitor_parse("vc:80Cx24C", "readline");
3414 if (default_virtcon)
3415 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3416 }
3417
3418 socket_init();
3419
3420 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3421 exit(1);
3422 #ifdef CONFIG_VIRTFS
3423 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3424 exit(1);
3425 }
3426 #endif
3427
3428 os_daemonize();
3429
3430 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3431 os_pidfile_error();
3432 exit(1);
3433 }
3434
3435 /* init the memory */
3436 if (ram_size == 0) {
3437 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3438 }
3439
3440 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3441 != 0) {
3442 exit(0);
3443 }
3444
3445 configure_accelerator();
3446
3447 qemu_init_cpu_loop();
3448 if (qemu_init_main_loop()) {
3449 fprintf(stderr, "qemu_init_main_loop failed\n");
3450 exit(1);
3451 }
3452
3453 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3454 if (machine_opts) {
3455 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3456 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3457 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3458 } else {
3459 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3460 }
3461
3462 if (!kernel_cmdline) {
3463 kernel_cmdline = "";
3464 }
3465
3466 linux_boot = (kernel_filename != NULL);
3467
3468 if (!linux_boot && *kernel_cmdline != '\0') {
3469 fprintf(stderr, "-append only allowed with -kernel option\n");
3470 exit(1);
3471 }
3472
3473 if (!linux_boot && initrd_filename != NULL) {
3474 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3475 exit(1);
3476 }
3477
3478 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3479 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3480 exit(1);
3481 }
3482
3483 os_set_line_buffering();
3484
3485 if (init_timer_alarm() < 0) {
3486 fprintf(stderr, "could not initialize alarm timer\n");
3487 exit(1);
3488 }
3489
3490 #ifdef CONFIG_SPICE
3491 /* spice needs the timers to be initialized by this point */
3492 qemu_spice_init();
3493 #endif
3494
3495 if (icount_option && (kvm_enabled() || xen_enabled())) {
3496 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3497 exit(1);
3498 }
3499 configure_icount(icount_option);
3500
3501 if (net_init_clients() < 0) {
3502 exit(1);
3503 }
3504
3505 /* init the bluetooth world */
3506 if (foreach_device_config(DEV_BT, bt_parse))
3507 exit(1);
3508
3509 if (!xen_enabled()) {
3510 /* On 32-bit hosts, QEMU is limited by virtual address space */
3511 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3512 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3513 exit(1);
3514 }
3515 }
3516
3517 cpu_exec_init_all();
3518
3519 bdrv_init_with_whitelist();
3520
3521 blk_mig_init();
3522
3523 /* open the virtual block devices */
3524 if (snapshot)
3525 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3526 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3527 exit(1);
3528
3529 default_drive(default_cdrom, snapshot, machine->use_scsi,
3530 IF_DEFAULT, 2, CDROM_OPTS);
3531 default_drive(default_floppy, snapshot, machine->use_scsi,
3532 IF_FLOPPY, 0, FD_OPTS);
3533 default_drive(default_sdcard, snapshot, machine->use_scsi,
3534 IF_SD, 0, SD_OPTS);
3535
3536 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3537
3538 if (nb_numa_nodes > 0) {
3539 int i;
3540
3541 if (nb_numa_nodes > MAX_NODES) {
3542 nb_numa_nodes = MAX_NODES;
3543 }
3544
3545 /* If no memory size if given for any node, assume the default case
3546 * and distribute the available memory equally across all nodes
3547 */
3548 for (i = 0; i < nb_numa_nodes; i++) {
3549 if (node_mem[i] != 0)
3550 break;
3551 }
3552 if (i == nb_numa_nodes) {
3553 uint64_t usedmem = 0;
3554
3555 /* On Linux, the each node's border has to be 8MB aligned,
3556 * the final node gets the rest.
3557 */
3558 for (i = 0; i < nb_numa_nodes - 1; i++) {
3559 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3560 usedmem += node_mem[i];
3561 }
3562 node_mem[i] = ram_size - usedmem;
3563 }
3564
3565 for (i = 0; i < nb_numa_nodes; i++) {
3566 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3567 break;
3568 }
3569 }
3570 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3571 * must cope with this anyway, because there are BIOSes out there in
3572 * real machines which also use this scheme.
3573 */
3574 if (i == nb_numa_nodes) {
3575 for (i = 0; i < max_cpus; i++) {
3576 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3577 }
3578 }
3579 }
3580
3581 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3582 exit(1);
3583 }
3584
3585 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3586 exit(1);
3587 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3588 exit(1);
3589 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3590 exit(1);
3591 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3592 exit(1);
3593
3594 /* If no default VGA is requested, the default is "none". */
3595 if (default_vga && cirrus_vga_available()) {
3596 vga_model = "cirrus";
3597 }
3598 select_vgahw(vga_model);
3599
3600 if (watchdog) {
3601 i = select_watchdog(watchdog);
3602 if (i > 0)
3603 exit (i == 1 ? 1 : 0);
3604 }
3605
3606 if (machine->compat_props) {
3607 qdev_prop_register_global_list(machine->compat_props);
3608 }
3609 qemu_add_globals();
3610
3611 qdev_machine_init();
3612
3613 machine->init(ram_size, boot_devices,
3614 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3615
3616 cpu_synchronize_all_post_init();
3617
3618 set_numa_modes();
3619
3620 current_machine = machine;
3621
3622 /* init USB devices */
3623 if (usb_enabled) {
3624 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3625 exit(1);
3626 }
3627
3628 /* init generic devices */
3629 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3630 exit(1);
3631
3632 net_check_clients();
3633
3634 /* just use the first displaystate for the moment */
3635 ds = get_displaystate();
3636
3637 if (using_spice)
3638 display_remote++;
3639 if (display_type == DT_DEFAULT && !display_remote) {
3640 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3641 display_type = DT_SDL;
3642 #elif defined(CONFIG_VNC)
3643 vnc_display = "localhost:0,to=99";
3644 show_vnc_port = 1;
3645 #else
3646 display_type = DT_NONE;
3647 #endif
3648 }
3649
3650
3651 /* init local displays */
3652 switch (display_type) {
3653 case DT_NOGRAPHIC:
3654 break;
3655 #if defined(CONFIG_CURSES)
3656 case DT_CURSES:
3657 curses_display_init(ds, full_screen);
3658 break;
3659 #endif
3660 #if defined(CONFIG_SDL)
3661 case DT_SDL:
3662 sdl_display_init(ds, full_screen, no_frame);
3663 break;
3664 #elif defined(CONFIG_COCOA)
3665 case DT_SDL:
3666 cocoa_display_init(ds, full_screen);
3667 break;
3668 #endif
3669 default:
3670 break;
3671 }
3672
3673 /* must be after terminal init, SDL library changes signal handlers */
3674 os_setup_signal_handling();
3675
3676 #ifdef CONFIG_VNC
3677 /* init remote displays */
3678 if (vnc_display) {
3679 vnc_display_init(ds);
3680 if (vnc_display_open(ds, vnc_display) < 0) {
3681 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3682 vnc_display);
3683 exit(1);
3684 }
3685
3686 if (show_vnc_port) {
3687 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3688 }
3689 }
3690 #endif
3691 #ifdef CONFIG_SPICE
3692 if (using_spice && !qxl_enabled) {
3693 qemu_spice_display_init(ds);
3694 }
3695 #endif
3696
3697 /* display setup */
3698 dpy_resize(ds);
3699 dcl = ds->listeners;
3700 while (dcl != NULL) {
3701 if (dcl->dpy_refresh != NULL) {
3702 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3703 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3704 break;
3705 }
3706 dcl = dcl->next;
3707 }
3708 text_consoles_set_display(ds);
3709
3710 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3711 exit(1);
3712 }
3713
3714 qdev_machine_creation_done();
3715
3716 if (rom_load_all() != 0) {
3717 fprintf(stderr, "rom loading failed\n");
3718 exit(1);
3719 }
3720
3721 /* TODO: once all bus devices are qdevified, this should be done
3722 * when bus is created by qdev.c */
3723 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3724 qemu_run_machine_init_done_notifiers();
3725
3726 qemu_system_reset(VMRESET_SILENT);
3727 if (loadvm) {
3728 if (load_vmstate(loadvm) < 0) {
3729 autostart = 0;
3730 }
3731 }
3732
3733 if (incoming) {
3734 Error *errp = NULL;
3735 int ret = qemu_start_incoming_migration(incoming, &errp);
3736 if (ret < 0) {
3737 if (error_is_set(&errp)) {
3738 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3739 error_free(errp);
3740 }
3741 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3742 incoming, ret);
3743 exit(ret);
3744 }
3745 } else if (autostart) {
3746 vm_start();
3747 }
3748
3749 os_setup_post();
3750
3751 resume_all_vcpus();
3752 main_loop();
3753 bdrv_close_all();
3754 pause_all_vcpus();
3755 net_cleanup();
3756 res_free();
3757
3758 return 0;
3759 }