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