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