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