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