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