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