]> Git Repo - qemu.git/blob - vl.c
vnc: tight: stop using qdict for palette stuff
[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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
34
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
50 #ifdef CONFIG_BSD
51 #include <sys/stat.h>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
53 #include <libutil.h>
54 #include <sys/sysctl.h>
55 #else
56 #include <util.h>
57 #endif
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
62
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
66 #ifdef __sun__
67 #include <sys/stat.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
76 #include <net/if.h>
77 #include <syslog.h>
78 #include <stropts.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80    discussion about Solaris header problems */
81 extern int madvise(caddr_t, size_t, int);
82 #endif
83 #endif
84 #endif
85
86 #if defined(__OpenBSD__)
87 #include <util.h>
88 #endif
89
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
92 #endif
93
94 #ifdef _WIN32
95 #include <windows.h>
96 #endif
97
98 #ifdef CONFIG_SDL
99 #if defined(__APPLE__) || defined(main)
100 #include <SDL.h>
101 int qemu_main(int argc, char **argv, char **envp);
102 int main(int argc, char **argv)
103 {
104     return qemu_main(argc, argv, NULL);
105 }
106 #undef main
107 #define main qemu_main
108 #endif
109 #endif /* CONFIG_SDL */
110
111 #ifdef CONFIG_COCOA
112 #undef main
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
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 "block.h"
140 #include "blockdev.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 "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
153
154 #include "disas.h"
155
156 #include "qemu_socket.h"
157
158 #include "slirp/libslirp.h"
159
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
163
164 //#define DEBUG_NET
165 //#define DEBUG_SLIRP
166
167 #define DEFAULT_RAM_SIZE 128
168
169 #define MAX_VIRTIO_CONSOLES 1
170
171 static const char *data_dir;
172 const char *bios_name = NULL;
173 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
174 DisplayType display_type = DT_DEFAULT;
175 const char* keyboard_layout = NULL;
176 ram_addr_t ram_size;
177 const char *mem_path = NULL;
178 #ifdef MAP_POPULATE
179 int mem_prealloc = 0; /* force preallocation of physical target memory */
180 #endif
181 int nb_nics;
182 NICInfo nd_table[MAX_NICS];
183 int vm_running;
184 int autostart;
185 static int rtc_utc = 1;
186 static int rtc_date_offset = -1; /* -1 means no change */
187 QEMUClock *rtc_clock;
188 int vga_interface_type = VGA_NONE;
189 static int full_screen = 0;
190 #ifdef CONFIG_SDL
191 static int no_frame = 0;
192 #endif
193 int no_quit = 0;
194 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
195 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
196 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
197 int win2k_install_hack = 0;
198 int rtc_td_hack = 0;
199 int usb_enabled = 0;
200 int singlestep = 0;
201 int smp_cpus = 1;
202 int max_cpus = 0;
203 int smp_cores = 1;
204 int smp_threads = 1;
205 const char *vnc_display;
206 int acpi_enabled = 1;
207 int no_hpet = 0;
208 int fd_bootchk = 1;
209 int no_reboot = 0;
210 int no_shutdown = 0;
211 int cursor_hide = 1;
212 int graphic_rotate = 0;
213 uint8_t irq0override = 1;
214 const char *watchdog;
215 const char *option_rom[MAX_OPTION_ROMS];
216 int nb_option_roms;
217 int semihosting_enabled = 0;
218 int old_param = 0;
219 const char *qemu_name;
220 int alt_grab = 0;
221 int ctrl_grab = 0;
222 unsigned int nb_prom_envs = 0;
223 const char *prom_envs[MAX_PROM_ENVS];
224 int boot_menu;
225
226 int nb_numa_nodes;
227 uint64_t node_mem[MAX_NODES];
228 uint64_t node_cpumask[MAX_NODES];
229
230 static QEMUTimer *nographic_timer;
231
232 uint8_t qemu_uuid[16];
233
234 static QEMUBootSetHandler *boot_set_handler;
235 static void *boot_set_opaque;
236
237 static NotifierList exit_notifiers =
238     NOTIFIER_LIST_INITIALIZER(exit_notifiers);
239
240 int kvm_allowed = 0;
241 uint32_t xen_domid;
242 enum xen_mode xen_mode = XEN_EMULATE;
243
244 static int default_serial = 1;
245 static int default_parallel = 1;
246 static int default_virtcon = 1;
247 static int default_monitor = 1;
248 static int default_vga = 1;
249 static int default_floppy = 1;
250 static int default_cdrom = 1;
251 static int default_sdcard = 1;
252
253 static struct {
254     const char *driver;
255     int *flag;
256 } default_list[] = {
257     { .driver = "isa-serial",           .flag = &default_serial    },
258     { .driver = "isa-parallel",         .flag = &default_parallel  },
259     { .driver = "isa-fdc",              .flag = &default_floppy    },
260     { .driver = "ide-drive",            .flag = &default_cdrom     },
261     { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
262     { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
263     { .driver = "virtio-serial",        .flag = &default_virtcon   },
264     { .driver = "VGA",                  .flag = &default_vga       },
265     { .driver = "cirrus-vga",           .flag = &default_vga       },
266     { .driver = "vmware-svga",          .flag = &default_vga       },
267 };
268
269 static int default_driver_check(QemuOpts *opts, void *opaque)
270 {
271     const char *driver = qemu_opt_get(opts, "driver");
272     int i;
273
274     if (!driver)
275         return 0;
276     for (i = 0; i < ARRAY_SIZE(default_list); i++) {
277         if (strcmp(default_list[i].driver, driver) != 0)
278             continue;
279         *(default_list[i].flag) = 0;
280     }
281     return 0;
282 }
283
284 /***********************************************************/
285 /* real time host monotonic timer */
286
287 /* compute with 96 bit intermediate result: (a*b)/c */
288 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
289 {
290     union {
291         uint64_t ll;
292         struct {
293 #ifdef HOST_WORDS_BIGENDIAN
294             uint32_t high, low;
295 #else
296             uint32_t low, high;
297 #endif
298         } l;
299     } u, res;
300     uint64_t rl, rh;
301
302     u.ll = a;
303     rl = (uint64_t)u.l.low * (uint64_t)b;
304     rh = (uint64_t)u.l.high * (uint64_t)b;
305     rh += (rl >> 32);
306     res.l.high = rh / c;
307     res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
308     return res.ll;
309 }
310
311 /***********************************************************/
312 /* host time/date access */
313 void qemu_get_timedate(struct tm *tm, int offset)
314 {
315     time_t ti;
316     struct tm *ret;
317
318     time(&ti);
319     ti += offset;
320     if (rtc_date_offset == -1) {
321         if (rtc_utc)
322             ret = gmtime(&ti);
323         else
324             ret = localtime(&ti);
325     } else {
326         ti -= rtc_date_offset;
327         ret = gmtime(&ti);
328     }
329
330     memcpy(tm, ret, sizeof(struct tm));
331 }
332
333 int qemu_timedate_diff(struct tm *tm)
334 {
335     time_t seconds;
336
337     if (rtc_date_offset == -1)
338         if (rtc_utc)
339             seconds = mktimegm(tm);
340         else
341             seconds = mktime(tm);
342     else
343         seconds = mktimegm(tm) + rtc_date_offset;
344
345     return seconds - time(NULL);
346 }
347
348 void rtc_change_mon_event(struct tm *tm)
349 {
350     QObject *data;
351
352     data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
353     monitor_protocol_event(QEVENT_RTC_CHANGE, data);
354     qobject_decref(data);
355 }
356
357 static void configure_rtc_date_offset(const char *startdate, int legacy)
358 {
359     time_t rtc_start_date;
360     struct tm tm;
361
362     if (!strcmp(startdate, "now") && legacy) {
363         rtc_date_offset = -1;
364     } else {
365         if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
366                    &tm.tm_year,
367                    &tm.tm_mon,
368                    &tm.tm_mday,
369                    &tm.tm_hour,
370                    &tm.tm_min,
371                    &tm.tm_sec) == 6) {
372             /* OK */
373         } else if (sscanf(startdate, "%d-%d-%d",
374                           &tm.tm_year,
375                           &tm.tm_mon,
376                           &tm.tm_mday) == 3) {
377             tm.tm_hour = 0;
378             tm.tm_min = 0;
379             tm.tm_sec = 0;
380         } else {
381             goto date_fail;
382         }
383         tm.tm_year -= 1900;
384         tm.tm_mon--;
385         rtc_start_date = mktimegm(&tm);
386         if (rtc_start_date == -1) {
387         date_fail:
388             fprintf(stderr, "Invalid date format. Valid formats are:\n"
389                             "'2006-06-17T16:01:21' or '2006-06-17'\n");
390             exit(1);
391         }
392         rtc_date_offset = time(NULL) - rtc_start_date;
393     }
394 }
395
396 static void configure_rtc(QemuOpts *opts)
397 {
398     const char *value;
399
400     value = qemu_opt_get(opts, "base");
401     if (value) {
402         if (!strcmp(value, "utc")) {
403             rtc_utc = 1;
404         } else if (!strcmp(value, "localtime")) {
405             rtc_utc = 0;
406         } else {
407             configure_rtc_date_offset(value, 0);
408         }
409     }
410     value = qemu_opt_get(opts, "clock");
411     if (value) {
412         if (!strcmp(value, "host")) {
413             rtc_clock = host_clock;
414         } else if (!strcmp(value, "vm")) {
415             rtc_clock = vm_clock;
416         } else {
417             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
418             exit(1);
419         }
420     }
421     value = qemu_opt_get(opts, "driftfix");
422     if (value) {
423         if (!strcmp(value, "slew")) {
424             rtc_td_hack = 1;
425         } else if (!strcmp(value, "none")) {
426             rtc_td_hack = 0;
427         } else {
428             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
429             exit(1);
430         }
431     }
432 }
433
434 /***********************************************************/
435 /* Bluetooth support */
436 static int nb_hcis;
437 static int cur_hci;
438 static struct HCIInfo *hci_table[MAX_NICS];
439
440 static struct bt_vlan_s {
441     struct bt_scatternet_s net;
442     int id;
443     struct bt_vlan_s *next;
444 } *first_bt_vlan;
445
446 /* find or alloc a new bluetooth "VLAN" */
447 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
448 {
449     struct bt_vlan_s **pvlan, *vlan;
450     for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
451         if (vlan->id == id)
452             return &vlan->net;
453     }
454     vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
455     vlan->id = id;
456     pvlan = &first_bt_vlan;
457     while (*pvlan != NULL)
458         pvlan = &(*pvlan)->next;
459     *pvlan = vlan;
460     return &vlan->net;
461 }
462
463 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
464 {
465 }
466
467 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
468 {
469     return -ENOTSUP;
470 }
471
472 static struct HCIInfo null_hci = {
473     .cmd_send = null_hci_send,
474     .sco_send = null_hci_send,
475     .acl_send = null_hci_send,
476     .bdaddr_set = null_hci_addr_set,
477 };
478
479 struct HCIInfo *qemu_next_hci(void)
480 {
481     if (cur_hci == nb_hcis)
482         return &null_hci;
483
484     return hci_table[cur_hci++];
485 }
486
487 static struct HCIInfo *hci_init(const char *str)
488 {
489     char *endp;
490     struct bt_scatternet_s *vlan = 0;
491
492     if (!strcmp(str, "null"))
493         /* null */
494         return &null_hci;
495     else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
496         /* host[:hciN] */
497         return bt_host_hci(str[4] ? str + 5 : "hci0");
498     else if (!strncmp(str, "hci", 3)) {
499         /* hci[,vlan=n] */
500         if (str[3]) {
501             if (!strncmp(str + 3, ",vlan=", 6)) {
502                 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
503                 if (*endp)
504                     vlan = 0;
505             }
506         } else
507             vlan = qemu_find_bt_vlan(0);
508         if (vlan)
509            return bt_new_hci(vlan);
510     }
511
512     fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
513
514     return 0;
515 }
516
517 static int bt_hci_parse(const char *str)
518 {
519     struct HCIInfo *hci;
520     bdaddr_t bdaddr;
521
522     if (nb_hcis >= MAX_NICS) {
523         fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
524         return -1;
525     }
526
527     hci = hci_init(str);
528     if (!hci)
529         return -1;
530
531     bdaddr.b[0] = 0x52;
532     bdaddr.b[1] = 0x54;
533     bdaddr.b[2] = 0x00;
534     bdaddr.b[3] = 0x12;
535     bdaddr.b[4] = 0x34;
536     bdaddr.b[5] = 0x56 + nb_hcis;
537     hci->bdaddr_set(hci, bdaddr.b);
538
539     hci_table[nb_hcis++] = hci;
540
541     return 0;
542 }
543
544 static void bt_vhci_add(int vlan_id)
545 {
546     struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
547
548     if (!vlan->slave)
549         fprintf(stderr, "qemu: warning: adding a VHCI to "
550                         "an empty scatternet %i\n", vlan_id);
551
552     bt_vhci_init(bt_new_hci(vlan));
553 }
554
555 static struct bt_device_s *bt_device_add(const char *opt)
556 {
557     struct bt_scatternet_s *vlan;
558     int vlan_id = 0;
559     char *endp = strstr(opt, ",vlan=");
560     int len = (endp ? endp - opt : strlen(opt)) + 1;
561     char devname[10];
562
563     pstrcpy(devname, MIN(sizeof(devname), len), opt);
564
565     if (endp) {
566         vlan_id = strtol(endp + 6, &endp, 0);
567         if (*endp) {
568             fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
569             return 0;
570         }
571     }
572
573     vlan = qemu_find_bt_vlan(vlan_id);
574
575     if (!vlan->slave)
576         fprintf(stderr, "qemu: warning: adding a slave device to "
577                         "an empty scatternet %i\n", vlan_id);
578
579     if (!strcmp(devname, "keyboard"))
580         return bt_keyboard_init(vlan);
581
582     fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
583     return 0;
584 }
585
586 static int bt_parse(const char *opt)
587 {
588     const char *endp, *p;
589     int vlan;
590
591     if (strstart(opt, "hci", &endp)) {
592         if (!*endp || *endp == ',') {
593             if (*endp)
594                 if (!strstart(endp, ",vlan=", 0))
595                     opt = endp + 1;
596
597             return bt_hci_parse(opt);
598        }
599     } else if (strstart(opt, "vhci", &endp)) {
600         if (!*endp || *endp == ',') {
601             if (*endp) {
602                 if (strstart(endp, ",vlan=", &p)) {
603                     vlan = strtol(p, (char **) &endp, 0);
604                     if (*endp) {
605                         fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
606                         return 1;
607                     }
608                 } else {
609                     fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
610                     return 1;
611                 }
612             } else
613                 vlan = 0;
614
615             bt_vhci_add(vlan);
616             return 0;
617         }
618     } else if (strstart(opt, "device:", &endp))
619         return !bt_device_add(endp);
620
621     fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
622     return 1;
623 }
624
625 /***********************************************************/
626 /* QEMU Block devices */
627
628 #define HD_ALIAS "index=%d,media=disk"
629 #define CDROM_ALIAS "index=2,media=cdrom"
630 #define FD_ALIAS "index=%d,if=floppy"
631 #define PFLASH_ALIAS "if=pflash"
632 #define MTD_ALIAS "if=mtd"
633 #define SD_ALIAS "index=0,if=sd"
634
635 static int drive_init_func(QemuOpts *opts, void *opaque)
636 {
637     int *use_scsi = opaque;
638     int fatal_error = 0;
639
640     if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
641         if (fatal_error)
642             return 1;
643     }
644     return 0;
645 }
646
647 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
648 {
649     if (NULL == qemu_opt_get(opts, "snapshot")) {
650         qemu_opt_set(opts, "snapshot", "on");
651     }
652     return 0;
653 }
654
655 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
656 {
657     boot_set_handler = func;
658     boot_set_opaque = opaque;
659 }
660
661 int qemu_boot_set(const char *boot_devices)
662 {
663     if (!boot_set_handler) {
664         return -EINVAL;
665     }
666     return boot_set_handler(boot_set_opaque, boot_devices);
667 }
668
669 static void validate_bootdevices(char *devices)
670 {
671     /* We just do some generic consistency checks */
672     const char *p;
673     int bitmap = 0;
674
675     for (p = devices; *p != '\0'; p++) {
676         /* Allowed boot devices are:
677          * a-b: floppy disk drives
678          * c-f: IDE disk drives
679          * g-m: machine implementation dependant drives
680          * n-p: network devices
681          * It's up to each machine implementation to check if the given boot
682          * devices match the actual hardware implementation and firmware
683          * features.
684          */
685         if (*p < 'a' || *p > 'p') {
686             fprintf(stderr, "Invalid boot device '%c'\n", *p);
687             exit(1);
688         }
689         if (bitmap & (1 << (*p - 'a'))) {
690             fprintf(stderr, "Boot device '%c' was given twice\n", *p);
691             exit(1);
692         }
693         bitmap |= 1 << (*p - 'a');
694     }
695 }
696
697 static void restore_boot_devices(void *opaque)
698 {
699     char *standard_boot_devices = opaque;
700     static int first = 1;
701
702     /* Restore boot order and remove ourselves after the first boot */
703     if (first) {
704         first = 0;
705         return;
706     }
707
708     qemu_boot_set(standard_boot_devices);
709
710     qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
711     qemu_free(standard_boot_devices);
712 }
713
714 static void numa_add(const char *optarg)
715 {
716     char option[128];
717     char *endptr;
718     unsigned long long value, endvalue;
719     int nodenr;
720
721     optarg = get_opt_name(option, 128, optarg, ',') + 1;
722     if (!strcmp(option, "node")) {
723         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
724             nodenr = nb_numa_nodes;
725         } else {
726             nodenr = strtoull(option, NULL, 10);
727         }
728
729         if (get_param_value(option, 128, "mem", optarg) == 0) {
730             node_mem[nodenr] = 0;
731         } else {
732             value = strtoull(option, &endptr, 0);
733             switch (*endptr) {
734             case 0: case 'M': case 'm':
735                 value <<= 20;
736                 break;
737             case 'G': case 'g':
738                 value <<= 30;
739                 break;
740             }
741             node_mem[nodenr] = value;
742         }
743         if (get_param_value(option, 128, "cpus", optarg) == 0) {
744             node_cpumask[nodenr] = 0;
745         } else {
746             value = strtoull(option, &endptr, 10);
747             if (value >= 64) {
748                 value = 63;
749                 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
750             } else {
751                 if (*endptr == '-') {
752                     endvalue = strtoull(endptr+1, &endptr, 10);
753                     if (endvalue >= 63) {
754                         endvalue = 62;
755                         fprintf(stderr,
756                             "only 63 CPUs in NUMA mode supported.\n");
757                     }
758                     value = (2ULL << endvalue) - (1ULL << value);
759                 } else {
760                     value = 1ULL << value;
761                 }
762             }
763             node_cpumask[nodenr] = value;
764         }
765         nb_numa_nodes++;
766     }
767     return;
768 }
769
770 static void smp_parse(const char *optarg)
771 {
772     int smp, sockets = 0, threads = 0, cores = 0;
773     char *endptr;
774     char option[128];
775
776     smp = strtoul(optarg, &endptr, 10);
777     if (endptr != optarg) {
778         if (*endptr == ',') {
779             endptr++;
780         }
781     }
782     if (get_param_value(option, 128, "sockets", endptr) != 0)
783         sockets = strtoull(option, NULL, 10);
784     if (get_param_value(option, 128, "cores", endptr) != 0)
785         cores = strtoull(option, NULL, 10);
786     if (get_param_value(option, 128, "threads", endptr) != 0)
787         threads = strtoull(option, NULL, 10);
788     if (get_param_value(option, 128, "maxcpus", endptr) != 0)
789         max_cpus = strtoull(option, NULL, 10);
790
791     /* compute missing values, prefer sockets over cores over threads */
792     if (smp == 0 || sockets == 0) {
793         sockets = sockets > 0 ? sockets : 1;
794         cores = cores > 0 ? cores : 1;
795         threads = threads > 0 ? threads : 1;
796         if (smp == 0) {
797             smp = cores * threads * sockets;
798         }
799     } else {
800         if (cores == 0) {
801             threads = threads > 0 ? threads : 1;
802             cores = smp / (sockets * threads);
803         } else {
804             if (sockets) {
805                 threads = smp / (cores * sockets);
806             }
807         }
808     }
809     smp_cpus = smp;
810     smp_cores = cores > 0 ? cores : 1;
811     smp_threads = threads > 0 ? threads : 1;
812     if (max_cpus == 0)
813         max_cpus = smp_cpus;
814 }
815
816 /***********************************************************/
817 /* USB devices */
818
819 static int usb_device_add(const char *devname)
820 {
821     const char *p;
822     USBDevice *dev = NULL;
823
824     if (!usb_enabled)
825         return -1;
826
827     /* drivers with .usbdevice_name entry in USBDeviceInfo */
828     dev = usbdevice_create(devname);
829     if (dev)
830         goto done;
831
832     /* the other ones */
833     if (strstart(devname, "host:", &p)) {
834         dev = usb_host_device_open(p);
835     } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
836         dev = usb_bt_init(devname[2] ? hci_init(p) :
837                         bt_new_hci(qemu_find_bt_vlan(0)));
838     } else {
839         return -1;
840     }
841     if (!dev)
842         return -1;
843
844 done:
845     return 0;
846 }
847
848 static int usb_device_del(const char *devname)
849 {
850     int bus_num, addr;
851     const char *p;
852
853     if (strstart(devname, "host:", &p))
854         return usb_host_device_close(p);
855
856     if (!usb_enabled)
857         return -1;
858
859     p = strchr(devname, '.');
860     if (!p)
861         return -1;
862     bus_num = strtoul(devname, NULL, 0);
863     addr = strtoul(p + 1, NULL, 0);
864
865     return usb_device_delete_addr(bus_num, addr);
866 }
867
868 static int usb_parse(const char *cmdline)
869 {
870     int r;
871     r = usb_device_add(cmdline);
872     if (r < 0) {
873         fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
874     }
875     return r;
876 }
877
878 void do_usb_add(Monitor *mon, const QDict *qdict)
879 {
880     const char *devname = qdict_get_str(qdict, "devname");
881     if (usb_device_add(devname) < 0) {
882         error_report("could not add USB device '%s'", devname);
883     }
884 }
885
886 void do_usb_del(Monitor *mon, const QDict *qdict)
887 {
888     const char *devname = qdict_get_str(qdict, "devname");
889     if (usb_device_del(devname) < 0) {
890         error_report("could not delete USB device '%s'", devname);
891     }
892 }
893
894 /***********************************************************/
895 /* PCMCIA/Cardbus */
896
897 static struct pcmcia_socket_entry_s {
898     PCMCIASocket *socket;
899     struct pcmcia_socket_entry_s *next;
900 } *pcmcia_sockets = 0;
901
902 void pcmcia_socket_register(PCMCIASocket *socket)
903 {
904     struct pcmcia_socket_entry_s *entry;
905
906     entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
907     entry->socket = socket;
908     entry->next = pcmcia_sockets;
909     pcmcia_sockets = entry;
910 }
911
912 void pcmcia_socket_unregister(PCMCIASocket *socket)
913 {
914     struct pcmcia_socket_entry_s *entry, **ptr;
915
916     ptr = &pcmcia_sockets;
917     for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
918         if (entry->socket == socket) {
919             *ptr = entry->next;
920             qemu_free(entry);
921         }
922 }
923
924 void pcmcia_info(Monitor *mon)
925 {
926     struct pcmcia_socket_entry_s *iter;
927
928     if (!pcmcia_sockets)
929         monitor_printf(mon, "No PCMCIA sockets\n");
930
931     for (iter = pcmcia_sockets; iter; iter = iter->next)
932         monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
933                        iter->socket->attached ? iter->socket->card_string :
934                        "Empty");
935 }
936
937 /***********************************************************/
938 /* I/O handling */
939
940 typedef struct IOHandlerRecord {
941     int fd;
942     IOCanReadHandler *fd_read_poll;
943     IOHandler *fd_read;
944     IOHandler *fd_write;
945     int deleted;
946     void *opaque;
947     /* temporary data */
948     struct pollfd *ufd;
949     QLIST_ENTRY(IOHandlerRecord) next;
950 } IOHandlerRecord;
951
952 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
953     QLIST_HEAD_INITIALIZER(io_handlers);
954
955
956 /* XXX: fd_read_poll should be suppressed, but an API change is
957    necessary in the character devices to suppress fd_can_read(). */
958 int qemu_set_fd_handler2(int fd,
959                          IOCanReadHandler *fd_read_poll,
960                          IOHandler *fd_read,
961                          IOHandler *fd_write,
962                          void *opaque)
963 {
964     IOHandlerRecord *ioh;
965
966     if (!fd_read && !fd_write) {
967         QLIST_FOREACH(ioh, &io_handlers, next) {
968             if (ioh->fd == fd) {
969                 ioh->deleted = 1;
970                 break;
971             }
972         }
973     } else {
974         QLIST_FOREACH(ioh, &io_handlers, next) {
975             if (ioh->fd == fd)
976                 goto found;
977         }
978         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
979         QLIST_INSERT_HEAD(&io_handlers, ioh, next);
980     found:
981         ioh->fd = fd;
982         ioh->fd_read_poll = fd_read_poll;
983         ioh->fd_read = fd_read;
984         ioh->fd_write = fd_write;
985         ioh->opaque = opaque;
986         ioh->deleted = 0;
987     }
988     return 0;
989 }
990
991 int qemu_set_fd_handler(int fd,
992                         IOHandler *fd_read,
993                         IOHandler *fd_write,
994                         void *opaque)
995 {
996     return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
997 }
998
999 /***********************************************************/
1000 /* machine registration */
1001
1002 static QEMUMachine *first_machine = NULL;
1003 QEMUMachine *current_machine = NULL;
1004
1005 int qemu_register_machine(QEMUMachine *m)
1006 {
1007     QEMUMachine **pm;
1008     pm = &first_machine;
1009     while (*pm != NULL)
1010         pm = &(*pm)->next;
1011     m->next = NULL;
1012     *pm = m;
1013     return 0;
1014 }
1015
1016 static QEMUMachine *find_machine(const char *name)
1017 {
1018     QEMUMachine *m;
1019
1020     for(m = first_machine; m != NULL; m = m->next) {
1021         if (!strcmp(m->name, name))
1022             return m;
1023         if (m->alias && !strcmp(m->alias, name))
1024             return m;
1025     }
1026     return NULL;
1027 }
1028
1029 static QEMUMachine *find_default_machine(void)
1030 {
1031     QEMUMachine *m;
1032
1033     for(m = first_machine; m != NULL; m = m->next) {
1034         if (m->is_default) {
1035             return m;
1036         }
1037     }
1038     return NULL;
1039 }
1040
1041 /***********************************************************/
1042 /* main execution loop */
1043
1044 static void gui_update(void *opaque)
1045 {
1046     uint64_t interval = GUI_REFRESH_INTERVAL;
1047     DisplayState *ds = opaque;
1048     DisplayChangeListener *dcl = ds->listeners;
1049
1050     qemu_flush_coalesced_mmio_buffer();
1051     dpy_refresh(ds);
1052
1053     while (dcl != NULL) {
1054         if (dcl->gui_timer_interval &&
1055             dcl->gui_timer_interval < interval)
1056             interval = dcl->gui_timer_interval;
1057         dcl = dcl->next;
1058     }
1059     qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1060 }
1061
1062 static void nographic_update(void *opaque)
1063 {
1064     uint64_t interval = GUI_REFRESH_INTERVAL;
1065
1066     qemu_flush_coalesced_mmio_buffer();
1067     qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1068 }
1069
1070 struct vm_change_state_entry {
1071     VMChangeStateHandler *cb;
1072     void *opaque;
1073     QLIST_ENTRY (vm_change_state_entry) entries;
1074 };
1075
1076 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1077
1078 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1079                                                      void *opaque)
1080 {
1081     VMChangeStateEntry *e;
1082
1083     e = qemu_mallocz(sizeof (*e));
1084
1085     e->cb = cb;
1086     e->opaque = opaque;
1087     QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1088     return e;
1089 }
1090
1091 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1092 {
1093     QLIST_REMOVE (e, entries);
1094     qemu_free (e);
1095 }
1096
1097 void vm_state_notify(int running, int reason)
1098 {
1099     VMChangeStateEntry *e;
1100
1101     for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1102         e->cb(e->opaque, running, reason);
1103     }
1104 }
1105
1106 void vm_start(void)
1107 {
1108     if (!vm_running) {
1109         cpu_enable_ticks();
1110         vm_running = 1;
1111         vm_state_notify(1, 0);
1112         resume_all_vcpus();
1113         monitor_protocol_event(QEVENT_RESUME, NULL);
1114     }
1115 }
1116
1117 /* reset/shutdown handler */
1118
1119 typedef struct QEMUResetEntry {
1120     QTAILQ_ENTRY(QEMUResetEntry) entry;
1121     QEMUResetHandler *func;
1122     void *opaque;
1123 } QEMUResetEntry;
1124
1125 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1126     QTAILQ_HEAD_INITIALIZER(reset_handlers);
1127 static int reset_requested;
1128 static int shutdown_requested;
1129 static int powerdown_requested;
1130 int debug_requested;
1131 int vmstop_requested;
1132
1133 int qemu_shutdown_requested(void)
1134 {
1135     int r = shutdown_requested;
1136     shutdown_requested = 0;
1137     return r;
1138 }
1139
1140 int qemu_reset_requested(void)
1141 {
1142     int r = reset_requested;
1143     reset_requested = 0;
1144     return r;
1145 }
1146
1147 int qemu_powerdown_requested(void)
1148 {
1149     int r = powerdown_requested;
1150     powerdown_requested = 0;
1151     return r;
1152 }
1153
1154 static int qemu_debug_requested(void)
1155 {
1156     int r = debug_requested;
1157     debug_requested = 0;
1158     return r;
1159 }
1160
1161 static int qemu_vmstop_requested(void)
1162 {
1163     int r = vmstop_requested;
1164     vmstop_requested = 0;
1165     return r;
1166 }
1167
1168 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1169 {
1170     QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1171
1172     re->func = func;
1173     re->opaque = opaque;
1174     QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1175 }
1176
1177 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1178 {
1179     QEMUResetEntry *re;
1180
1181     QTAILQ_FOREACH(re, &reset_handlers, entry) {
1182         if (re->func == func && re->opaque == opaque) {
1183             QTAILQ_REMOVE(&reset_handlers, re, entry);
1184             qemu_free(re);
1185             return;
1186         }
1187     }
1188 }
1189
1190 void qemu_system_reset(void)
1191 {
1192     QEMUResetEntry *re, *nre;
1193
1194     /* reset all devices */
1195     QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1196         re->func(re->opaque);
1197     }
1198     monitor_protocol_event(QEVENT_RESET, NULL);
1199     cpu_synchronize_all_post_reset();
1200 }
1201
1202 void qemu_system_reset_request(void)
1203 {
1204     if (no_reboot) {
1205         shutdown_requested = 1;
1206     } else {
1207         reset_requested = 1;
1208     }
1209     qemu_notify_event();
1210 }
1211
1212 void qemu_system_shutdown_request(void)
1213 {
1214     shutdown_requested = 1;
1215     qemu_notify_event();
1216 }
1217
1218 void qemu_system_powerdown_request(void)
1219 {
1220     powerdown_requested = 1;
1221     qemu_notify_event();
1222 }
1223
1224 void main_loop_wait(int nonblocking)
1225 {
1226     IOHandlerRecord *ioh;
1227     fd_set rfds, wfds, xfds;
1228     int ret, nfds;
1229     struct timeval tv;
1230     int timeout;
1231
1232     if (nonblocking)
1233         timeout = 0;
1234     else {
1235         timeout = qemu_calculate_timeout();
1236         qemu_bh_update_timeout(&timeout);
1237     }
1238
1239     os_host_main_loop_wait(&timeout);
1240
1241     /* poll any events */
1242     /* XXX: separate device handlers from system ones */
1243     nfds = -1;
1244     FD_ZERO(&rfds);
1245     FD_ZERO(&wfds);
1246     FD_ZERO(&xfds);
1247     QLIST_FOREACH(ioh, &io_handlers, next) {
1248         if (ioh->deleted)
1249             continue;
1250         if (ioh->fd_read &&
1251             (!ioh->fd_read_poll ||
1252              ioh->fd_read_poll(ioh->opaque) != 0)) {
1253             FD_SET(ioh->fd, &rfds);
1254             if (ioh->fd > nfds)
1255                 nfds = ioh->fd;
1256         }
1257         if (ioh->fd_write) {
1258             FD_SET(ioh->fd, &wfds);
1259             if (ioh->fd > nfds)
1260                 nfds = ioh->fd;
1261         }
1262     }
1263
1264     tv.tv_sec = timeout / 1000;
1265     tv.tv_usec = (timeout % 1000) * 1000;
1266
1267     slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1268
1269     qemu_mutex_unlock_iothread();
1270     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1271     qemu_mutex_lock_iothread();
1272     if (ret > 0) {
1273         IOHandlerRecord *pioh;
1274
1275         QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1276             if (ioh->deleted) {
1277                 QLIST_REMOVE(ioh, next);
1278                 qemu_free(ioh);
1279                 continue;
1280             }
1281             if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1282                 ioh->fd_read(ioh->opaque);
1283             }
1284             if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1285                 ioh->fd_write(ioh->opaque);
1286             }
1287         }
1288     }
1289
1290     slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1291
1292     qemu_run_all_timers();
1293
1294     /* Check bottom-halves last in case any of the earlier events triggered
1295        them.  */
1296     qemu_bh_poll();
1297
1298 }
1299
1300 static int vm_can_run(void)
1301 {
1302     if (powerdown_requested)
1303         return 0;
1304     if (reset_requested)
1305         return 0;
1306     if (shutdown_requested)
1307         return 0;
1308     if (debug_requested)
1309         return 0;
1310     return 1;
1311 }
1312
1313 qemu_irq qemu_system_powerdown;
1314
1315 static void main_loop(void)
1316 {
1317     int r;
1318
1319     qemu_main_loop_start();
1320
1321     for (;;) {
1322         do {
1323             bool nonblocking = false;
1324 #ifdef CONFIG_PROFILER
1325             int64_t ti;
1326 #endif
1327 #ifndef CONFIG_IOTHREAD
1328             nonblocking = cpu_exec_all();
1329 #endif
1330 #ifdef CONFIG_PROFILER
1331             ti = profile_getclock();
1332 #endif
1333             main_loop_wait(nonblocking);
1334 #ifdef CONFIG_PROFILER
1335             dev_time += profile_getclock() - ti;
1336 #endif
1337         } while (vm_can_run());
1338
1339         if ((r = qemu_debug_requested())) {
1340             vm_stop(r);
1341         }
1342         if (qemu_shutdown_requested()) {
1343             monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1344             if (no_shutdown) {
1345                 vm_stop(0);
1346                 no_shutdown = 0;
1347             } else
1348                 break;
1349         }
1350         if (qemu_reset_requested()) {
1351             pause_all_vcpus();
1352             qemu_system_reset();
1353             resume_all_vcpus();
1354         }
1355         if (qemu_powerdown_requested()) {
1356             monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1357             qemu_irq_raise(qemu_system_powerdown);
1358         }
1359         if ((r = qemu_vmstop_requested())) {
1360             vm_stop(r);
1361         }
1362     }
1363     bdrv_close_all();
1364     pause_all_vcpus();
1365 }
1366
1367 static void version(void)
1368 {
1369     printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1370 }
1371
1372 static void help(int exitcode)
1373 {
1374     const char *options_help =
1375 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1376         opt_help
1377 #define DEFHEADING(text) stringify(text) "\n"
1378 #include "qemu-options.def"
1379 #undef DEF
1380 #undef DEFHEADING
1381 #undef GEN_DOCS
1382         ;
1383     version();
1384     printf("usage: %s [options] [disk_image]\n"
1385            "\n"
1386            "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1387            "\n"
1388            "%s\n"
1389            "During emulation, the following keys are useful:\n"
1390            "ctrl-alt-f      toggle full screen\n"
1391            "ctrl-alt-n      switch to virtual console 'n'\n"
1392            "ctrl-alt        toggle mouse and keyboard grab\n"
1393            "\n"
1394            "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395            "qemu",
1396            options_help);
1397     exit(exitcode);
1398 }
1399
1400 #define HAS_ARG 0x0001
1401
1402 typedef struct QEMUOption {
1403     const char *name;
1404     int flags;
1405     int index;
1406     uint32_t arch_mask;
1407 } QEMUOption;
1408
1409 static const QEMUOption qemu_options[] = {
1410     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1412     { option, opt_arg, opt_enum, arch_mask },
1413 #define DEFHEADING(text)
1414 #include "qemu-options.def"
1415 #undef DEF
1416 #undef DEFHEADING
1417 #undef GEN_DOCS
1418     { NULL },
1419 };
1420 static void select_vgahw (const char *p)
1421 {
1422     const char *opts;
1423
1424     default_vga = 0;
1425     vga_interface_type = VGA_NONE;
1426     if (strstart(p, "std", &opts)) {
1427         vga_interface_type = VGA_STD;
1428     } else if (strstart(p, "cirrus", &opts)) {
1429         vga_interface_type = VGA_CIRRUS;
1430     } else if (strstart(p, "vmware", &opts)) {
1431         vga_interface_type = VGA_VMWARE;
1432     } else if (strstart(p, "xenfb", &opts)) {
1433         vga_interface_type = VGA_XENFB;
1434     } else if (!strstart(p, "none", &opts)) {
1435     invalid_vga:
1436         fprintf(stderr, "Unknown vga type: %s\n", p);
1437         exit(1);
1438     }
1439     while (*opts) {
1440         const char *nextopt;
1441
1442         if (strstart(opts, ",retrace=", &nextopt)) {
1443             opts = nextopt;
1444             if (strstart(opts, "dumb", &nextopt))
1445                 vga_retrace_method = VGA_RETRACE_DUMB;
1446             else if (strstart(opts, "precise", &nextopt))
1447                 vga_retrace_method = VGA_RETRACE_PRECISE;
1448             else goto invalid_vga;
1449         } else goto invalid_vga;
1450         opts = nextopt;
1451     }
1452 }
1453
1454 static int balloon_parse(const char *arg)
1455 {
1456     QemuOpts *opts;
1457
1458     if (strcmp(arg, "none") == 0) {
1459         return 0;
1460     }
1461
1462     if (!strncmp(arg, "virtio", 6)) {
1463         if (arg[6] == ',') {
1464             /* have params -> parse them */
1465             opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1466             if (!opts)
1467                 return  -1;
1468         } else {
1469             /* create empty opts */
1470             opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1471         }
1472         qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473         return 0;
1474     }
1475
1476     return -1;
1477 }
1478
1479 char *qemu_find_file(int type, const char *name)
1480 {
1481     int len;
1482     const char *subdir;
1483     char *buf;
1484
1485     /* If name contains path separators then try it as a straight path.  */
1486     if ((strchr(name, '/') || strchr(name, '\\'))
1487         && access(name, R_OK) == 0) {
1488         return qemu_strdup(name);
1489     }
1490     switch (type) {
1491     case QEMU_FILE_TYPE_BIOS:
1492         subdir = "";
1493         break;
1494     case QEMU_FILE_TYPE_KEYMAP:
1495         subdir = "keymaps/";
1496         break;
1497     default:
1498         abort();
1499     }
1500     len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501     buf = qemu_mallocz(len);
1502     snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1503     if (access(buf, R_OK)) {
1504         qemu_free(buf);
1505         return NULL;
1506     }
1507     return buf;
1508 }
1509
1510 static int device_help_func(QemuOpts *opts, void *opaque)
1511 {
1512     return qdev_device_help(opts);
1513 }
1514
1515 static int device_init_func(QemuOpts *opts, void *opaque)
1516 {
1517     DeviceState *dev;
1518
1519     dev = qdev_device_add(opts);
1520     if (!dev)
1521         return -1;
1522     return 0;
1523 }
1524
1525 static int chardev_init_func(QemuOpts *opts, void *opaque)
1526 {
1527     CharDriverState *chr;
1528
1529     chr = qemu_chr_open_opts(opts, NULL);
1530     if (!chr)
1531         return -1;
1532     return 0;
1533 }
1534
1535 #ifdef CONFIG_VIRTFS
1536 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1537 {
1538     int ret;
1539     ret = qemu_fsdev_add(opts);
1540
1541     return ret;
1542 }
1543 #endif
1544
1545 static int mon_init_func(QemuOpts *opts, void *opaque)
1546 {
1547     CharDriverState *chr;
1548     const char *chardev;
1549     const char *mode;
1550     int flags;
1551
1552     mode = qemu_opt_get(opts, "mode");
1553     if (mode == NULL) {
1554         mode = "readline";
1555     }
1556     if (strcmp(mode, "readline") == 0) {
1557         flags = MONITOR_USE_READLINE;
1558     } else if (strcmp(mode, "control") == 0) {
1559         flags = MONITOR_USE_CONTROL;
1560     } else {
1561         fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562         exit(1);
1563     }
1564
1565     if (qemu_opt_get_bool(opts, "default", 0))
1566         flags |= MONITOR_IS_DEFAULT;
1567
1568     chardev = qemu_opt_get(opts, "chardev");
1569     chr = qemu_chr_find(chardev);
1570     if (chr == NULL) {
1571         fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1572         exit(1);
1573     }
1574
1575     monitor_init(chr, flags);
1576     return 0;
1577 }
1578
1579 static void monitor_parse(const char *optarg, const char *mode)
1580 {
1581     static int monitor_device_index = 0;
1582     QemuOpts *opts;
1583     const char *p;
1584     char label[32];
1585     int def = 0;
1586
1587     if (strstart(optarg, "chardev:", &p)) {
1588         snprintf(label, sizeof(label), "%s", p);
1589     } else {
1590         snprintf(label, sizeof(label), "compat_monitor%d",
1591                  monitor_device_index);
1592         if (monitor_device_index == 0) {
1593             def = 1;
1594         }
1595         opts = qemu_chr_parse_compat(label, optarg);
1596         if (!opts) {
1597             fprintf(stderr, "parse error: %s\n", optarg);
1598             exit(1);
1599         }
1600     }
1601
1602     opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1603     if (!opts) {
1604         fprintf(stderr, "duplicate chardev: %s\n", label);
1605         exit(1);
1606     }
1607     qemu_opt_set(opts, "mode", mode);
1608     qemu_opt_set(opts, "chardev", label);
1609     if (def)
1610         qemu_opt_set(opts, "default", "on");
1611     monitor_device_index++;
1612 }
1613
1614 struct device_config {
1615     enum {
1616         DEV_USB,       /* -usbdevice     */
1617         DEV_BT,        /* -bt            */
1618         DEV_SERIAL,    /* -serial        */
1619         DEV_PARALLEL,  /* -parallel      */
1620         DEV_VIRTCON,   /* -virtioconsole */
1621         DEV_DEBUGCON,  /* -debugcon */
1622     } type;
1623     const char *cmdline;
1624     QTAILQ_ENTRY(device_config) next;
1625 };
1626 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1627
1628 static void add_device_config(int type, const char *cmdline)
1629 {
1630     struct device_config *conf;
1631
1632     conf = qemu_mallocz(sizeof(*conf));
1633     conf->type = type;
1634     conf->cmdline = cmdline;
1635     QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1636 }
1637
1638 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1639 {
1640     struct device_config *conf;
1641     int rc;
1642
1643     QTAILQ_FOREACH(conf, &device_configs, next) {
1644         if (conf->type != type)
1645             continue;
1646         rc = func(conf->cmdline);
1647         if (0 != rc)
1648             return rc;
1649     }
1650     return 0;
1651 }
1652
1653 static int serial_parse(const char *devname)
1654 {
1655     static int index = 0;
1656     char label[32];
1657
1658     if (strcmp(devname, "none") == 0)
1659         return 0;
1660     if (index == MAX_SERIAL_PORTS) {
1661         fprintf(stderr, "qemu: too many serial ports\n");
1662         exit(1);
1663     }
1664     snprintf(label, sizeof(label), "serial%d", index);
1665     serial_hds[index] = qemu_chr_open(label, devname, NULL);
1666     if (!serial_hds[index]) {
1667         fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1668                 devname, strerror(errno));
1669         return -1;
1670     }
1671     index++;
1672     return 0;
1673 }
1674
1675 static int parallel_parse(const char *devname)
1676 {
1677     static int index = 0;
1678     char label[32];
1679
1680     if (strcmp(devname, "none") == 0)
1681         return 0;
1682     if (index == MAX_PARALLEL_PORTS) {
1683         fprintf(stderr, "qemu: too many parallel ports\n");
1684         exit(1);
1685     }
1686     snprintf(label, sizeof(label), "parallel%d", index);
1687     parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1688     if (!parallel_hds[index]) {
1689         fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1690                 devname, strerror(errno));
1691         return -1;
1692     }
1693     index++;
1694     return 0;
1695 }
1696
1697 static int virtcon_parse(const char *devname)
1698 {
1699     static int index = 0;
1700     char label[32];
1701     QemuOpts *bus_opts, *dev_opts;
1702
1703     if (strcmp(devname, "none") == 0)
1704         return 0;
1705     if (index == MAX_VIRTIO_CONSOLES) {
1706         fprintf(stderr, "qemu: too many virtio consoles\n");
1707         exit(1);
1708     }
1709
1710     bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1711     qemu_opt_set(bus_opts, "driver", "virtio-serial");
1712
1713     dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1714     qemu_opt_set(dev_opts, "driver", "virtconsole");
1715
1716     snprintf(label, sizeof(label), "virtcon%d", index);
1717     virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1718     if (!virtcon_hds[index]) {
1719         fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1720                 devname, strerror(errno));
1721         return -1;
1722     }
1723     qemu_opt_set(dev_opts, "chardev", label);
1724
1725     index++;
1726     return 0;
1727 }
1728
1729 static int debugcon_parse(const char *devname)
1730 {   
1731     QemuOpts *opts;
1732
1733     if (!qemu_chr_open("debugcon", devname, NULL)) {
1734         exit(1);
1735     }
1736     opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1737     if (!opts) {
1738         fprintf(stderr, "qemu: already have a debugcon device\n");
1739         exit(1);
1740     }
1741     qemu_opt_set(opts, "driver", "isa-debugcon");
1742     qemu_opt_set(opts, "chardev", "debugcon");
1743     return 0;
1744 }
1745
1746 void qemu_add_exit_notifier(Notifier *notify)
1747 {
1748     notifier_list_add(&exit_notifiers, notify);
1749 }
1750
1751 void qemu_remove_exit_notifier(Notifier *notify)
1752 {
1753     notifier_list_remove(&exit_notifiers, notify);
1754 }
1755
1756 static void qemu_run_exit_notifiers(void)
1757 {
1758     notifier_list_notify(&exit_notifiers);
1759 }
1760
1761 static const QEMUOption *lookup_opt(int argc, char **argv,
1762                                     const char **poptarg, int *poptind)
1763 {
1764     const QEMUOption *popt;
1765     int optind = *poptind;
1766     char *r = argv[optind];
1767     const char *optarg;
1768
1769     loc_set_cmdline(argv, optind, 1);
1770     optind++;
1771     /* Treat --foo the same as -foo.  */
1772     if (r[1] == '-')
1773         r++;
1774     popt = qemu_options;
1775     for(;;) {
1776         if (!popt->name) {
1777             error_report("invalid option");
1778             exit(1);
1779         }
1780         if (!strcmp(popt->name, r + 1))
1781             break;
1782         popt++;
1783     }
1784     if (popt->flags & HAS_ARG) {
1785         if (optind >= argc) {
1786             error_report("requires an argument");
1787             exit(1);
1788         }
1789         optarg = argv[optind++];
1790         loc_set_cmdline(argv, optind - 2, 2);
1791     } else {
1792         optarg = NULL;
1793     }
1794
1795     *poptarg = optarg;
1796     *poptind = optind;
1797
1798     return popt;
1799 }
1800
1801 int main(int argc, char **argv, char **envp)
1802 {
1803     const char *gdbstub_dev = NULL;
1804     int i;
1805     int snapshot, linux_boot;
1806     const char *icount_option = NULL;
1807     const char *initrd_filename;
1808     const char *kernel_filename, *kernel_cmdline;
1809     char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1810     DisplayState *ds;
1811     DisplayChangeListener *dcl;
1812     int cyls, heads, secs, translation;
1813     QemuOpts *hda_opts = NULL, *opts;
1814     int optind;
1815     const char *optarg;
1816     const char *loadvm = NULL;
1817     QEMUMachine *machine;
1818     const char *cpu_model;
1819     int tb_size;
1820     const char *pid_file = NULL;
1821     const char *incoming = NULL;
1822     int show_vnc_port = 0;
1823     int defconfig = 1;
1824
1825     atexit(qemu_run_exit_notifiers);
1826     error_set_progname(argv[0]);
1827
1828     init_clocks();
1829
1830     qemu_cache_utils_init(envp);
1831
1832     QLIST_INIT (&vm_change_state_head);
1833     os_setup_early_signal_handling();
1834
1835     module_call_init(MODULE_INIT_MACHINE);
1836     machine = find_default_machine();
1837     cpu_model = NULL;
1838     initrd_filename = NULL;
1839     ram_size = 0;
1840     snapshot = 0;
1841     kernel_filename = NULL;
1842     kernel_cmdline = "";
1843     cyls = heads = secs = 0;
1844     translation = BIOS_ATA_TRANSLATION_AUTO;
1845
1846     for (i = 0; i < MAX_NODES; i++) {
1847         node_mem[i] = 0;
1848         node_cpumask[i] = 0;
1849     }
1850
1851     nb_numa_nodes = 0;
1852     nb_nics = 0;
1853
1854     tb_size = 0;
1855     autostart= 1;
1856
1857     /* first pass of option parsing */
1858     optind = 1;
1859     while (optind < argc) {
1860         if (argv[optind][0] != '-') {
1861             /* disk image */
1862             optind++;
1863             continue;
1864         } else {
1865             const QEMUOption *popt;
1866
1867             popt = lookup_opt(argc, argv, &optarg, &optind);
1868             switch (popt->index) {
1869             case QEMU_OPTION_nodefconfig:
1870                 defconfig=0;
1871                 break;
1872             }
1873         }
1874     }
1875
1876     if (defconfig) {
1877         int ret;
1878
1879         ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1880         if (ret < 0 && ret != -ENOENT) {
1881             exit(1);
1882         }
1883
1884         ret = qemu_read_config_file(arch_config_name);
1885         if (ret < 0 && ret != -ENOENT) {
1886             exit(1);
1887         }
1888     }
1889     cpudef_init();
1890
1891     /* second pass of option parsing */
1892     optind = 1;
1893     for(;;) {
1894         if (optind >= argc)
1895             break;
1896         if (argv[optind][0] != '-') {
1897             hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1898         } else {
1899             const QEMUOption *popt;
1900
1901             popt = lookup_opt(argc, argv, &optarg, &optind);
1902             if (!(popt->arch_mask & arch_type)) {
1903                 printf("Option %s not supported for this target\n", popt->name);
1904                 exit(1);
1905             }
1906             switch(popt->index) {
1907             case QEMU_OPTION_M:
1908                 machine = find_machine(optarg);
1909                 if (!machine) {
1910                     QEMUMachine *m;
1911                     printf("Supported machines are:\n");
1912                     for(m = first_machine; m != NULL; m = m->next) {
1913                         if (m->alias)
1914                             printf("%-10s %s (alias of %s)\n",
1915                                    m->alias, m->desc, m->name);
1916                         printf("%-10s %s%s\n",
1917                                m->name, m->desc,
1918                                m->is_default ? " (default)" : "");
1919                     }
1920                     exit(*optarg != '?');
1921                 }
1922                 break;
1923             case QEMU_OPTION_cpu:
1924                 /* hw initialization will check this */
1925                 if (*optarg == '?') {
1926                     list_cpus(stdout, &fprintf, optarg);
1927                     exit(0);
1928                 } else {
1929                     cpu_model = optarg;
1930                 }
1931                 break;
1932             case QEMU_OPTION_initrd:
1933                 initrd_filename = optarg;
1934                 break;
1935             case QEMU_OPTION_hda:
1936                 if (cyls == 0)
1937                     hda_opts = drive_add(optarg, HD_ALIAS, 0);
1938                 else
1939                     hda_opts = drive_add(optarg, HD_ALIAS
1940                              ",cyls=%d,heads=%d,secs=%d%s",
1941                              0, cyls, heads, secs,
1942                              translation == BIOS_ATA_TRANSLATION_LBA ?
1943                                  ",trans=lba" :
1944                              translation == BIOS_ATA_TRANSLATION_NONE ?
1945                                  ",trans=none" : "");
1946                  break;
1947             case QEMU_OPTION_hdb:
1948             case QEMU_OPTION_hdc:
1949             case QEMU_OPTION_hdd:
1950                 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1951                 break;
1952             case QEMU_OPTION_drive:
1953                 drive_add(NULL, "%s", optarg);
1954                 break;
1955             case QEMU_OPTION_set:
1956                 if (qemu_set_option(optarg) != 0)
1957                     exit(1);
1958                 break;
1959             case QEMU_OPTION_global:
1960                 if (qemu_global_option(optarg) != 0)
1961                     exit(1);
1962                 break;
1963             case QEMU_OPTION_mtdblock:
1964                 drive_add(optarg, MTD_ALIAS);
1965                 break;
1966             case QEMU_OPTION_sd:
1967                 drive_add(optarg, SD_ALIAS);
1968                 break;
1969             case QEMU_OPTION_pflash:
1970                 drive_add(optarg, PFLASH_ALIAS);
1971                 break;
1972             case QEMU_OPTION_snapshot:
1973                 snapshot = 1;
1974                 break;
1975             case QEMU_OPTION_hdachs:
1976                 {
1977                     const char *p;
1978                     p = optarg;
1979                     cyls = strtol(p, (char **)&p, 0);
1980                     if (cyls < 1 || cyls > 16383)
1981                         goto chs_fail;
1982                     if (*p != ',')
1983                         goto chs_fail;
1984                     p++;
1985                     heads = strtol(p, (char **)&p, 0);
1986                     if (heads < 1 || heads > 16)
1987                         goto chs_fail;
1988                     if (*p != ',')
1989                         goto chs_fail;
1990                     p++;
1991                     secs = strtol(p, (char **)&p, 0);
1992                     if (secs < 1 || secs > 63)
1993                         goto chs_fail;
1994                     if (*p == ',') {
1995                         p++;
1996                         if (!strcmp(p, "none"))
1997                             translation = BIOS_ATA_TRANSLATION_NONE;
1998                         else if (!strcmp(p, "lba"))
1999                             translation = BIOS_ATA_TRANSLATION_LBA;
2000                         else if (!strcmp(p, "auto"))
2001                             translation = BIOS_ATA_TRANSLATION_AUTO;
2002                         else
2003                             goto chs_fail;
2004                     } else if (*p != '\0') {
2005                     chs_fail:
2006                         fprintf(stderr, "qemu: invalid physical CHS format\n");
2007                         exit(1);
2008                     }
2009                     if (hda_opts != NULL) {
2010                         char num[16];
2011                         snprintf(num, sizeof(num), "%d", cyls);
2012                         qemu_opt_set(hda_opts, "cyls", num);
2013                         snprintf(num, sizeof(num), "%d", heads);
2014                         qemu_opt_set(hda_opts, "heads", num);
2015                         snprintf(num, sizeof(num), "%d", secs);
2016                         qemu_opt_set(hda_opts, "secs", num);
2017                         if (translation == BIOS_ATA_TRANSLATION_LBA)
2018                             qemu_opt_set(hda_opts, "trans", "lba");
2019                         if (translation == BIOS_ATA_TRANSLATION_NONE)
2020                             qemu_opt_set(hda_opts, "trans", "none");
2021                     }
2022                 }
2023                 break;
2024             case QEMU_OPTION_numa:
2025                 if (nb_numa_nodes >= MAX_NODES) {
2026                     fprintf(stderr, "qemu: too many NUMA nodes\n");
2027                     exit(1);
2028                 }
2029                 numa_add(optarg);
2030                 break;
2031             case QEMU_OPTION_nographic:
2032                 display_type = DT_NOGRAPHIC;
2033                 break;
2034 #ifdef CONFIG_CURSES
2035             case QEMU_OPTION_curses:
2036                 display_type = DT_CURSES;
2037                 break;
2038 #endif
2039             case QEMU_OPTION_portrait:
2040                 graphic_rotate = 1;
2041                 break;
2042             case QEMU_OPTION_kernel:
2043                 kernel_filename = optarg;
2044                 break;
2045             case QEMU_OPTION_append:
2046                 kernel_cmdline = optarg;
2047                 break;
2048             case QEMU_OPTION_cdrom:
2049                 drive_add(optarg, CDROM_ALIAS);
2050                 break;
2051             case QEMU_OPTION_boot:
2052                 {
2053                     static const char * const params[] = {
2054                         "order", "once", "menu", NULL
2055                     };
2056                     char buf[sizeof(boot_devices)];
2057                     char *standard_boot_devices;
2058                     int legacy = 0;
2059
2060                     if (!strchr(optarg, '=')) {
2061                         legacy = 1;
2062                         pstrcpy(buf, sizeof(buf), optarg);
2063                     } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2064                         fprintf(stderr,
2065                                 "qemu: unknown boot parameter '%s' in '%s'\n",
2066                                 buf, optarg);
2067                         exit(1);
2068                     }
2069
2070                     if (legacy ||
2071                         get_param_value(buf, sizeof(buf), "order", optarg)) {
2072                         validate_bootdevices(buf);
2073                         pstrcpy(boot_devices, sizeof(boot_devices), buf);
2074                     }
2075                     if (!legacy) {
2076                         if (get_param_value(buf, sizeof(buf),
2077                                             "once", optarg)) {
2078                             validate_bootdevices(buf);
2079                             standard_boot_devices = qemu_strdup(boot_devices);
2080                             pstrcpy(boot_devices, sizeof(boot_devices), buf);
2081                             qemu_register_reset(restore_boot_devices,
2082                                                 standard_boot_devices);
2083                         }
2084                         if (get_param_value(buf, sizeof(buf),
2085                                             "menu", optarg)) {
2086                             if (!strcmp(buf, "on")) {
2087                                 boot_menu = 1;
2088                             } else if (!strcmp(buf, "off")) {
2089                                 boot_menu = 0;
2090                             } else {
2091                                 fprintf(stderr,
2092                                         "qemu: invalid option value '%s'\n",
2093                                         buf);
2094                                 exit(1);
2095                             }
2096                         }
2097                     }
2098                 }
2099                 break;
2100             case QEMU_OPTION_fda:
2101             case QEMU_OPTION_fdb:
2102                 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2103                 break;
2104             case QEMU_OPTION_no_fd_bootchk:
2105                 fd_bootchk = 0;
2106                 break;
2107             case QEMU_OPTION_netdev:
2108                 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2109                     exit(1);
2110                 }
2111                 break;
2112             case QEMU_OPTION_net:
2113                 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2114                     exit(1);
2115                 }
2116                 break;
2117 #ifdef CONFIG_SLIRP
2118             case QEMU_OPTION_tftp:
2119                 legacy_tftp_prefix = optarg;
2120                 break;
2121             case QEMU_OPTION_bootp:
2122                 legacy_bootp_filename = optarg;
2123                 break;
2124             case QEMU_OPTION_redir:
2125                 if (net_slirp_redir(optarg) < 0)
2126                     exit(1);
2127                 break;
2128 #endif
2129             case QEMU_OPTION_bt:
2130                 add_device_config(DEV_BT, optarg);
2131                 break;
2132             case QEMU_OPTION_audio_help:
2133                 if (!(audio_available())) {
2134                     printf("Option %s not supported for this target\n", popt->name);
2135                     exit(1);
2136                 }
2137                 AUD_help ();
2138                 exit (0);
2139                 break;
2140             case QEMU_OPTION_soundhw:
2141                 if (!(audio_available())) {
2142                     printf("Option %s not supported for this target\n", popt->name);
2143                     exit(1);
2144                 }
2145                 select_soundhw (optarg);
2146                 break;
2147             case QEMU_OPTION_h:
2148                 help(0);
2149                 break;
2150             case QEMU_OPTION_version:
2151                 version();
2152                 exit(0);
2153                 break;
2154             case QEMU_OPTION_m: {
2155                 uint64_t value;
2156                 char *ptr;
2157
2158                 value = strtoul(optarg, &ptr, 10);
2159                 switch (*ptr) {
2160                 case 0: case 'M': case 'm':
2161                     value <<= 20;
2162                     break;
2163                 case 'G': case 'g':
2164                     value <<= 30;
2165                     break;
2166                 default:
2167                     fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2168                     exit(1);
2169                 }
2170
2171                 /* On 32-bit hosts, QEMU is limited by virtual address space */
2172                 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2173                     fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2174                     exit(1);
2175                 }
2176                 if (value != (uint64_t)(ram_addr_t)value) {
2177                     fprintf(stderr, "qemu: ram size too large\n");
2178                     exit(1);
2179                 }
2180                 ram_size = value;
2181                 break;
2182             }
2183             case QEMU_OPTION_mempath:
2184                 mem_path = optarg;
2185                 break;
2186 #ifdef MAP_POPULATE
2187             case QEMU_OPTION_mem_prealloc:
2188                 mem_prealloc = 1;
2189                 break;
2190 #endif
2191             case QEMU_OPTION_d:
2192                 set_cpu_log(optarg);
2193                 break;
2194             case QEMU_OPTION_s:
2195                 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2196                 break;
2197             case QEMU_OPTION_gdb:
2198                 gdbstub_dev = optarg;
2199                 break;
2200             case QEMU_OPTION_L:
2201                 data_dir = optarg;
2202                 break;
2203             case QEMU_OPTION_bios:
2204                 bios_name = optarg;
2205                 break;
2206             case QEMU_OPTION_singlestep:
2207                 singlestep = 1;
2208                 break;
2209             case QEMU_OPTION_S:
2210                 autostart = 0;
2211                 break;
2212             case QEMU_OPTION_k:
2213                 keyboard_layout = optarg;
2214                 break;
2215             case QEMU_OPTION_localtime:
2216                 rtc_utc = 0;
2217                 break;
2218             case QEMU_OPTION_vga:
2219                 select_vgahw (optarg);
2220                 break;
2221             case QEMU_OPTION_g:
2222                 {
2223                     const char *p;
2224                     int w, h, depth;
2225                     p = optarg;
2226                     w = strtol(p, (char **)&p, 10);
2227                     if (w <= 0) {
2228                     graphic_error:
2229                         fprintf(stderr, "qemu: invalid resolution or depth\n");
2230                         exit(1);
2231                     }
2232                     if (*p != 'x')
2233                         goto graphic_error;
2234                     p++;
2235                     h = strtol(p, (char **)&p, 10);
2236                     if (h <= 0)
2237                         goto graphic_error;
2238                     if (*p == 'x') {
2239                         p++;
2240                         depth = strtol(p, (char **)&p, 10);
2241                         if (depth != 8 && depth != 15 && depth != 16 &&
2242                             depth != 24 && depth != 32)
2243                             goto graphic_error;
2244                     } else if (*p == '\0') {
2245                         depth = graphic_depth;
2246                     } else {
2247                         goto graphic_error;
2248                     }
2249
2250                     graphic_width = w;
2251                     graphic_height = h;
2252                     graphic_depth = depth;
2253                 }
2254                 break;
2255             case QEMU_OPTION_echr:
2256                 {
2257                     char *r;
2258                     term_escape_char = strtol(optarg, &r, 0);
2259                     if (r == optarg)
2260                         printf("Bad argument to echr\n");
2261                     break;
2262                 }
2263             case QEMU_OPTION_monitor:
2264                 monitor_parse(optarg, "readline");
2265                 default_monitor = 0;
2266                 break;
2267             case QEMU_OPTION_qmp:
2268                 monitor_parse(optarg, "control");
2269                 default_monitor = 0;
2270                 break;
2271             case QEMU_OPTION_mon:
2272                 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2273                 if (!opts) {
2274                     exit(1);
2275                 }
2276                 default_monitor = 0;
2277                 break;
2278             case QEMU_OPTION_chardev:
2279                 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2280                 if (!opts) {
2281                     exit(1);
2282                 }
2283                 break;
2284 #ifdef CONFIG_VIRTFS
2285             case QEMU_OPTION_fsdev:
2286                 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2287                 if (!opts) {
2288                     fprintf(stderr, "parse error: %s\n", optarg);
2289                     exit(1);
2290                 }
2291                 break;
2292             case QEMU_OPTION_virtfs: {
2293                 char *arg_fsdev = NULL;
2294                 char *arg_9p = NULL;
2295                 int len = 0;
2296
2297                 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2298                 if (!opts) {
2299                     fprintf(stderr, "parse error: %s\n", optarg);
2300                     exit(1);
2301                 }
2302
2303                 if (qemu_opt_get(opts, "fstype") == NULL ||
2304                         qemu_opt_get(opts, "mount_tag") == NULL ||
2305                         qemu_opt_get(opts, "path") == NULL ||
2306                         qemu_opt_get(opts, "security_model") == NULL) {
2307                     fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2308                             "security_model=[mapped|passthrough],"
2309                             "mnt_tag=tag.\n");
2310                     exit(1);
2311                 }
2312
2313                 len = strlen(",id=,path=,security_model=");
2314                 len += strlen(qemu_opt_get(opts, "fstype"));
2315                 len += strlen(qemu_opt_get(opts, "mount_tag"));
2316                 len += strlen(qemu_opt_get(opts, "path"));
2317                 len += strlen(qemu_opt_get(opts, "security_model"));
2318                 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2319
2320                 if (!arg_fsdev) {
2321                     fprintf(stderr, "No memory to parse -fsdev for %s\n",
2322                             optarg);
2323                     exit(1);
2324                 }
2325
2326                 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
2327                                 qemu_opt_get(opts, "fstype"),
2328                                 qemu_opt_get(opts, "mount_tag"),
2329                                 qemu_opt_get(opts, "path"),
2330                                 qemu_opt_get(opts, "security_model"));
2331
2332                 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2333                 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2334                 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2335
2336                 if (!arg_9p) {
2337                     fprintf(stderr, "No memory to parse -device for %s\n",
2338                             optarg);
2339                     exit(1);
2340                 }
2341
2342                 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2343                                 qemu_opt_get(opts, "mount_tag"),
2344                                 qemu_opt_get(opts, "mount_tag"));
2345
2346                 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2347                     fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2348                     exit(1);
2349                 }
2350
2351                 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2352                     fprintf(stderr, "parse error [device]: %s\n", optarg);
2353                     exit(1);
2354                 }
2355
2356                 qemu_free(arg_fsdev);
2357                 qemu_free(arg_9p);
2358                 break;
2359             }
2360 #endif
2361             case QEMU_OPTION_serial:
2362                 add_device_config(DEV_SERIAL, optarg);
2363                 default_serial = 0;
2364                 if (strncmp(optarg, "mon:", 4) == 0) {
2365                     default_monitor = 0;
2366                 }
2367                 break;
2368             case QEMU_OPTION_watchdog:
2369                 if (watchdog) {
2370                     fprintf(stderr,
2371                             "qemu: only one watchdog option may be given\n");
2372                     return 1;
2373                 }
2374                 watchdog = optarg;
2375                 break;
2376             case QEMU_OPTION_watchdog_action:
2377                 if (select_watchdog_action(optarg) == -1) {
2378                     fprintf(stderr, "Unknown -watchdog-action parameter\n");
2379                     exit(1);
2380                 }
2381                 break;
2382             case QEMU_OPTION_virtiocon:
2383                 add_device_config(DEV_VIRTCON, optarg);
2384                 default_virtcon = 0;
2385                 if (strncmp(optarg, "mon:", 4) == 0) {
2386                     default_monitor = 0;
2387                 }
2388                 break;
2389             case QEMU_OPTION_parallel:
2390                 add_device_config(DEV_PARALLEL, optarg);
2391                 default_parallel = 0;
2392                 if (strncmp(optarg, "mon:", 4) == 0) {
2393                     default_monitor = 0;
2394                 }
2395                 break;
2396             case QEMU_OPTION_debugcon:
2397                 add_device_config(DEV_DEBUGCON, optarg);
2398                 break;
2399             case QEMU_OPTION_loadvm:
2400                 loadvm = optarg;
2401                 break;
2402             case QEMU_OPTION_full_screen:
2403                 full_screen = 1;
2404                 break;
2405 #ifdef CONFIG_SDL
2406             case QEMU_OPTION_no_frame:
2407                 no_frame = 1;
2408                 break;
2409             case QEMU_OPTION_alt_grab:
2410                 alt_grab = 1;
2411                 break;
2412             case QEMU_OPTION_ctrl_grab:
2413                 ctrl_grab = 1;
2414                 break;
2415             case QEMU_OPTION_no_quit:
2416                 no_quit = 1;
2417                 break;
2418             case QEMU_OPTION_sdl:
2419                 display_type = DT_SDL;
2420                 break;
2421 #endif
2422             case QEMU_OPTION_pidfile:
2423                 pid_file = optarg;
2424                 break;
2425             case QEMU_OPTION_win2k_hack:
2426                 win2k_install_hack = 1;
2427                 break;
2428             case QEMU_OPTION_rtc_td_hack:
2429                 rtc_td_hack = 1;
2430                 break;
2431             case QEMU_OPTION_acpitable:
2432                 do_acpitable_option(optarg);
2433                 break;
2434             case QEMU_OPTION_smbios:
2435                 do_smbios_option(optarg);
2436                 break;
2437             case QEMU_OPTION_enable_kvm:
2438                 kvm_allowed = 1;
2439                 break;
2440             case QEMU_OPTION_usb:
2441                 usb_enabled = 1;
2442                 break;
2443             case QEMU_OPTION_usbdevice:
2444                 usb_enabled = 1;
2445                 add_device_config(DEV_USB, optarg);
2446                 break;
2447             case QEMU_OPTION_device:
2448                 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2449                     exit(1);
2450                 }
2451                 break;
2452             case QEMU_OPTION_smp:
2453                 smp_parse(optarg);
2454                 if (smp_cpus < 1) {
2455                     fprintf(stderr, "Invalid number of CPUs\n");
2456                     exit(1);
2457                 }
2458                 if (max_cpus < smp_cpus) {
2459                     fprintf(stderr, "maxcpus must be equal to or greater than "
2460                             "smp\n");
2461                     exit(1);
2462                 }
2463                 if (max_cpus > 255) {
2464                     fprintf(stderr, "Unsupported number of maxcpus\n");
2465                     exit(1);
2466                 }
2467                 break;
2468             case QEMU_OPTION_vnc:
2469                 display_type = DT_VNC;
2470                 vnc_display = optarg;
2471                 break;
2472             case QEMU_OPTION_no_acpi:
2473                 acpi_enabled = 0;
2474                 break;
2475             case QEMU_OPTION_no_hpet:
2476                 no_hpet = 1;
2477                 break;
2478             case QEMU_OPTION_balloon:
2479                 if (balloon_parse(optarg) < 0) {
2480                     fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2481                     exit(1);
2482                 }
2483                 break;
2484             case QEMU_OPTION_no_reboot:
2485                 no_reboot = 1;
2486                 break;
2487             case QEMU_OPTION_no_shutdown:
2488                 no_shutdown = 1;
2489                 break;
2490             case QEMU_OPTION_show_cursor:
2491                 cursor_hide = 0;
2492                 break;
2493             case QEMU_OPTION_uuid:
2494                 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2495                     fprintf(stderr, "Fail to parse UUID string."
2496                             " Wrong format.\n");
2497                     exit(1);
2498                 }
2499                 break;
2500             case QEMU_OPTION_option_rom:
2501                 if (nb_option_roms >= MAX_OPTION_ROMS) {
2502                     fprintf(stderr, "Too many option ROMs\n");
2503                     exit(1);
2504                 }
2505                 option_rom[nb_option_roms] = optarg;
2506                 nb_option_roms++;
2507                 break;
2508             case QEMU_OPTION_semihosting:
2509                 semihosting_enabled = 1;
2510                 break;
2511             case QEMU_OPTION_name:
2512                 qemu_name = qemu_strdup(optarg);
2513                  {
2514                      char *p = strchr(qemu_name, ',');
2515                      if (p != NULL) {
2516                         *p++ = 0;
2517                         if (strncmp(p, "process=", 8)) {
2518                             fprintf(stderr, "Unknown subargument %s to -name", p);
2519                             exit(1);
2520                         }
2521                         p += 8;
2522                         os_set_proc_name(p);
2523                      }  
2524                  }      
2525                 break;
2526             case QEMU_OPTION_prom_env:
2527                 if (nb_prom_envs >= MAX_PROM_ENVS) {
2528                     fprintf(stderr, "Too many prom variables\n");
2529                     exit(1);
2530                 }
2531                 prom_envs[nb_prom_envs] = optarg;
2532                 nb_prom_envs++;
2533                 break;
2534             case QEMU_OPTION_old_param:
2535                 old_param = 1;
2536                 break;
2537             case QEMU_OPTION_clock:
2538                 configure_alarms(optarg);
2539                 break;
2540             case QEMU_OPTION_startdate:
2541                 configure_rtc_date_offset(optarg, 1);
2542                 break;
2543             case QEMU_OPTION_rtc:
2544                 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2545                 if (!opts) {
2546                     exit(1);
2547                 }
2548                 configure_rtc(opts);
2549                 break;
2550             case QEMU_OPTION_tb_size:
2551                 tb_size = strtol(optarg, NULL, 0);
2552                 if (tb_size < 0)
2553                     tb_size = 0;
2554                 break;
2555             case QEMU_OPTION_icount:
2556                 icount_option = optarg;
2557                 break;
2558             case QEMU_OPTION_incoming:
2559                 incoming = optarg;
2560                 break;
2561             case QEMU_OPTION_nodefaults:
2562                 default_serial = 0;
2563                 default_parallel = 0;
2564                 default_virtcon = 0;
2565                 default_monitor = 0;
2566                 default_vga = 0;
2567                 default_net = 0;
2568                 default_floppy = 0;
2569                 default_cdrom = 0;
2570                 default_sdcard = 0;
2571                 break;
2572             case QEMU_OPTION_xen_domid:
2573                 if (!(xen_available())) {
2574                     printf("Option %s not supported for this target\n", popt->name);
2575                     exit(1);
2576                 }
2577                 xen_domid = atoi(optarg);
2578                 break;
2579             case QEMU_OPTION_xen_create:
2580                 if (!(xen_available())) {
2581                     printf("Option %s not supported for this target\n", popt->name);
2582                     exit(1);
2583                 }
2584                 xen_mode = XEN_CREATE;
2585                 break;
2586             case QEMU_OPTION_xen_attach:
2587                 if (!(xen_available())) {
2588                     printf("Option %s not supported for this target\n", popt->name);
2589                     exit(1);
2590                 }
2591                 xen_mode = XEN_ATTACH;
2592                 break;
2593             case QEMU_OPTION_readconfig:
2594                 {
2595                     int ret = qemu_read_config_file(optarg);
2596                     if (ret < 0) {
2597                         fprintf(stderr, "read config %s: %s\n", optarg,
2598                             strerror(-ret));
2599                         exit(1);
2600                     }
2601                     break;
2602                 }
2603             case QEMU_OPTION_writeconfig:
2604                 {
2605                     FILE *fp;
2606                     if (strcmp(optarg, "-") == 0) {
2607                         fp = stdout;
2608                     } else {
2609                         fp = fopen(optarg, "w");
2610                         if (fp == NULL) {
2611                             fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2612                             exit(1);
2613                         }
2614                     }
2615                     qemu_config_write(fp);
2616                     fclose(fp);
2617                     break;
2618                 }
2619             default:
2620                 os_parse_cmd_args(popt->index, optarg);
2621             }
2622         }
2623     }
2624     loc_set_none();
2625
2626     /* If no data_dir is specified then try to find it relative to the
2627        executable path.  */
2628     if (!data_dir) {
2629         data_dir = os_find_datadir(argv[0]);
2630     }
2631     /* If all else fails use the install patch specified when building.  */
2632     if (!data_dir) {
2633         data_dir = CONFIG_QEMU_DATADIR;
2634     }
2635
2636     /*
2637      * Default to max_cpus = smp_cpus, in case the user doesn't
2638      * specify a max_cpus value.
2639      */
2640     if (!max_cpus)
2641         max_cpus = smp_cpus;
2642
2643     machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2644     if (smp_cpus > machine->max_cpus) {
2645         fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2646                 "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2647                 machine->max_cpus);
2648         exit(1);
2649     }
2650
2651     qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
2652     qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
2653
2654     if (machine->no_serial) {
2655         default_serial = 0;
2656     }
2657     if (machine->no_parallel) {
2658         default_parallel = 0;
2659     }
2660     if (!machine->use_virtcon) {
2661         default_virtcon = 0;
2662     }
2663     if (machine->no_vga) {
2664         default_vga = 0;
2665     }
2666     if (machine->no_floppy) {
2667         default_floppy = 0;
2668     }
2669     if (machine->no_cdrom) {
2670         default_cdrom = 0;
2671     }
2672     if (machine->no_sdcard) {
2673         default_sdcard = 0;
2674     }
2675
2676     if (display_type == DT_NOGRAPHIC) {
2677         if (default_parallel)
2678             add_device_config(DEV_PARALLEL, "null");
2679         if (default_serial && default_monitor) {
2680             add_device_config(DEV_SERIAL, "mon:stdio");
2681         } else if (default_virtcon && default_monitor) {
2682             add_device_config(DEV_VIRTCON, "mon:stdio");
2683         } else {
2684             if (default_serial)
2685                 add_device_config(DEV_SERIAL, "stdio");
2686             if (default_virtcon)
2687                 add_device_config(DEV_VIRTCON, "stdio");
2688             if (default_monitor)
2689                 monitor_parse("stdio", "readline");
2690         }
2691     } else {
2692         if (default_serial)
2693             add_device_config(DEV_SERIAL, "vc:80Cx24C");
2694         if (default_parallel)
2695             add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2696         if (default_monitor)
2697             monitor_parse("vc:80Cx24C", "readline");
2698         if (default_virtcon)
2699             add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2700     }
2701     if (default_vga)
2702         vga_interface_type = VGA_CIRRUS;
2703
2704     socket_init();
2705
2706     if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2707         exit(1);
2708 #ifdef CONFIG_VIRTFS
2709     if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2710         exit(1);
2711     }
2712 #endif
2713
2714     os_daemonize();
2715
2716     if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2717         os_pidfile_error();
2718         exit(1);
2719     }
2720
2721     if (kvm_allowed) {
2722         int ret = kvm_init(smp_cpus);
2723         if (ret < 0) {
2724             if (!kvm_available()) {
2725                 printf("KVM not supported for this target\n");
2726             } else {
2727                 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2728             }
2729             exit(1);
2730         }
2731     }
2732
2733     if (qemu_init_main_loop()) {
2734         fprintf(stderr, "qemu_init_main_loop failed\n");
2735         exit(1);
2736     }
2737     linux_boot = (kernel_filename != NULL);
2738
2739     if (!linux_boot && *kernel_cmdline != '\0') {
2740         fprintf(stderr, "-append only allowed with -kernel option\n");
2741         exit(1);
2742     }
2743
2744     if (!linux_boot && initrd_filename != NULL) {
2745         fprintf(stderr, "-initrd only allowed with -kernel option\n");
2746         exit(1);
2747     }
2748
2749     os_set_line_buffering();
2750
2751     if (init_timer_alarm() < 0) {
2752         fprintf(stderr, "could not initialize alarm timer\n");
2753         exit(1);
2754     }
2755     configure_icount(icount_option);
2756
2757     if (net_init_clients() < 0) {
2758         exit(1);
2759     }
2760
2761     /* init the bluetooth world */
2762     if (foreach_device_config(DEV_BT, bt_parse))
2763         exit(1);
2764
2765     /* init the memory */
2766     if (ram_size == 0)
2767         ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2768
2769     /* init the dynamic translator */
2770     cpu_exec_init_all(tb_size * 1024 * 1024);
2771
2772     bdrv_init_with_whitelist();
2773
2774     blk_mig_init();
2775
2776     if (default_cdrom) {
2777         /* we always create the cdrom drive, even if no disk is there */
2778         drive_add(NULL, CDROM_ALIAS);
2779     }
2780
2781     if (default_floppy) {
2782         /* we always create at least one floppy */
2783         drive_add(NULL, FD_ALIAS, 0);
2784     }
2785
2786     if (default_sdcard) {
2787         /* we always create one sd slot, even if no card is in it */
2788         drive_add(NULL, SD_ALIAS);
2789     }
2790
2791     /* open the virtual block devices */
2792     if (snapshot)
2793         qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
2794     if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
2795         exit(1);
2796
2797     register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2798                          ram_load, NULL);
2799
2800     if (nb_numa_nodes > 0) {
2801         int i;
2802
2803         if (nb_numa_nodes > smp_cpus) {
2804             nb_numa_nodes = smp_cpus;
2805         }
2806
2807         /* If no memory size if given for any node, assume the default case
2808          * and distribute the available memory equally across all nodes
2809          */
2810         for (i = 0; i < nb_numa_nodes; i++) {
2811             if (node_mem[i] != 0)
2812                 break;
2813         }
2814         if (i == nb_numa_nodes) {
2815             uint64_t usedmem = 0;
2816
2817             /* On Linux, the each node's border has to be 8MB aligned,
2818              * the final node gets the rest.
2819              */
2820             for (i = 0; i < nb_numa_nodes - 1; i++) {
2821                 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2822                 usedmem += node_mem[i];
2823             }
2824             node_mem[i] = ram_size - usedmem;
2825         }
2826
2827         for (i = 0; i < nb_numa_nodes; i++) {
2828             if (node_cpumask[i] != 0)
2829                 break;
2830         }
2831         /* assigning the VCPUs round-robin is easier to implement, guest OSes
2832          * must cope with this anyway, because there are BIOSes out there in
2833          * real machines which also use this scheme.
2834          */
2835         if (i == nb_numa_nodes) {
2836             for (i = 0; i < smp_cpus; i++) {
2837                 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2838             }
2839         }
2840     }
2841
2842     if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2843         exit(1);
2844     }
2845
2846     if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2847         exit(1);
2848     if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2849         exit(1);
2850     if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2851         exit(1);
2852     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2853         exit(1);
2854
2855     module_call_init(MODULE_INIT_DEVICE);
2856
2857     if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2858         exit(0);
2859
2860     if (watchdog) {
2861         i = select_watchdog(watchdog);
2862         if (i > 0)
2863             exit (i == 1 ? 1 : 0);
2864     }
2865
2866     if (machine->compat_props) {
2867         qdev_prop_register_global_list(machine->compat_props);
2868     }
2869     qemu_add_globals();
2870
2871     machine->init(ram_size, boot_devices,
2872                   kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2873
2874     cpu_synchronize_all_post_init();
2875
2876     /* must be after terminal init, SDL library changes signal handlers */
2877     os_setup_signal_handling();
2878
2879     set_numa_modes();
2880
2881     current_machine = machine;
2882
2883     /* init USB devices */
2884     if (usb_enabled) {
2885         if (foreach_device_config(DEV_USB, usb_parse) < 0)
2886             exit(1);
2887     }
2888
2889     /* init generic devices */
2890     if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
2891         exit(1);
2892
2893     net_check_clients();
2894
2895     /* just use the first displaystate for the moment */
2896     ds = get_displaystate();
2897
2898     if (display_type == DT_DEFAULT) {
2899 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2900         display_type = DT_SDL;
2901 #else
2902         display_type = DT_VNC;
2903         vnc_display = "localhost:0,to=99";
2904         show_vnc_port = 1;
2905 #endif
2906     }
2907         
2908
2909     switch (display_type) {
2910     case DT_NOGRAPHIC:
2911         break;
2912 #if defined(CONFIG_CURSES)
2913     case DT_CURSES:
2914         curses_display_init(ds, full_screen);
2915         break;
2916 #endif
2917 #if defined(CONFIG_SDL)
2918     case DT_SDL:
2919         sdl_display_init(ds, full_screen, no_frame);
2920         break;
2921 #elif defined(CONFIG_COCOA)
2922     case DT_SDL:
2923         cocoa_display_init(ds, full_screen);
2924         break;
2925 #endif
2926     case DT_VNC:
2927         vnc_display_init(ds);
2928         if (vnc_display_open(ds, vnc_display) < 0)
2929             exit(1);
2930
2931         if (show_vnc_port) {
2932             printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2933         }
2934         break;
2935     default:
2936         break;
2937     }
2938     dpy_resize(ds);
2939
2940     dcl = ds->listeners;
2941     while (dcl != NULL) {
2942         if (dcl->dpy_refresh != NULL) {
2943             ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2944             qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2945             break;
2946         }
2947         dcl = dcl->next;
2948     }
2949
2950     if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
2951         nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2952         qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2953     }
2954
2955     text_consoles_set_display(ds);
2956
2957     if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2958         fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2959                 gdbstub_dev);
2960         exit(1);
2961     }
2962
2963     qdev_machine_creation_done();
2964
2965     if (rom_load_all() != 0) {
2966         fprintf(stderr, "rom loading failed\n");
2967         exit(1);
2968     }
2969
2970     qemu_system_reset();
2971     if (loadvm) {
2972         if (load_vmstate(loadvm) < 0) {
2973             autostart = 0;
2974         }
2975     }
2976
2977     if (incoming) {
2978         int ret = qemu_start_incoming_migration(incoming);
2979         if (ret < 0) {
2980             fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2981                     incoming, ret);
2982             exit(ret);
2983         }
2984     } else if (autostart) {
2985         vm_start();
2986     }
2987
2988     os_setup_post();
2989
2990     main_loop();
2991     quit_timers();
2992     net_cleanup();
2993
2994     return 0;
2995 }
This page took 0.182726 seconds and 4 git commands to generate.