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