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