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