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