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