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