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