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