]> git.proxmox.com Git - mirror_qemu.git/blob - vl.c
Adding seccomp calls to vl.c (v8)
[mirror_qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
31 #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 /***********************************************************/
774 /* QEMU Block devices */
775
776 #define HD_OPTS "media=disk"
777 #define CDROM_OPTS "media=cdrom"
778 #define FD_OPTS ""
779 #define PFLASH_OPTS ""
780 #define MTD_OPTS ""
781 #define SD_OPTS ""
782
783 static int drive_init_func(QemuOpts *opts, void *opaque)
784 {
785 int *use_scsi = opaque;
786
787 return drive_init(opts, *use_scsi) == NULL;
788 }
789
790 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
791 {
792 if (NULL == qemu_opt_get(opts, "snapshot")) {
793 qemu_opt_set(opts, "snapshot", "on");
794 }
795 return 0;
796 }
797
798 static void default_drive(int enable, int snapshot, int use_scsi,
799 BlockInterfaceType type, int index,
800 const char *optstr)
801 {
802 QemuOpts *opts;
803
804 if (type == IF_DEFAULT) {
805 type = use_scsi ? IF_SCSI : IF_IDE;
806 }
807
808 if (!enable || drive_get_by_index(type, index)) {
809 return;
810 }
811
812 opts = drive_add(type, index, NULL, optstr);
813 if (snapshot) {
814 drive_enable_snapshot(opts, NULL);
815 }
816 if (!drive_init(opts, use_scsi)) {
817 exit(1);
818 }
819 }
820
821 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
822 {
823 boot_set_handler = func;
824 boot_set_opaque = opaque;
825 }
826
827 int qemu_boot_set(const char *boot_devices)
828 {
829 if (!boot_set_handler) {
830 return -EINVAL;
831 }
832 return boot_set_handler(boot_set_opaque, boot_devices);
833 }
834
835 static void validate_bootdevices(char *devices)
836 {
837 /* We just do some generic consistency checks */
838 const char *p;
839 int bitmap = 0;
840
841 for (p = devices; *p != '\0'; p++) {
842 /* Allowed boot devices are:
843 * a-b: floppy disk drives
844 * c-f: IDE disk drives
845 * g-m: machine implementation dependent drives
846 * n-p: network devices
847 * It's up to each machine implementation to check if the given boot
848 * devices match the actual hardware implementation and firmware
849 * features.
850 */
851 if (*p < 'a' || *p > 'p') {
852 fprintf(stderr, "Invalid boot device '%c'\n", *p);
853 exit(1);
854 }
855 if (bitmap & (1 << (*p - 'a'))) {
856 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
857 exit(1);
858 }
859 bitmap |= 1 << (*p - 'a');
860 }
861 }
862
863 static void restore_boot_devices(void *opaque)
864 {
865 char *standard_boot_devices = opaque;
866 static int first = 1;
867
868 /* Restore boot order and remove ourselves after the first boot */
869 if (first) {
870 first = 0;
871 return;
872 }
873
874 qemu_boot_set(standard_boot_devices);
875
876 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
877 g_free(standard_boot_devices);
878 }
879
880 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
881 const char *suffix)
882 {
883 FWBootEntry *node, *i;
884
885 if (bootindex < 0) {
886 return;
887 }
888
889 assert(dev != NULL || suffix != NULL);
890
891 node = g_malloc0(sizeof(FWBootEntry));
892 node->bootindex = bootindex;
893 node->suffix = suffix ? g_strdup(suffix) : NULL;
894 node->dev = dev;
895
896 QTAILQ_FOREACH(i, &fw_boot_order, link) {
897 if (i->bootindex == bootindex) {
898 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
899 exit(1);
900 } else if (i->bootindex < bootindex) {
901 continue;
902 }
903 QTAILQ_INSERT_BEFORE(i, node, link);
904 return;
905 }
906 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
907 }
908
909 /*
910 * This function returns null terminated string that consist of new line
911 * separated device paths.
912 *
913 * memory pointed by "size" is assigned total length of the array in bytes
914 *
915 */
916 char *get_boot_devices_list(uint32_t *size)
917 {
918 FWBootEntry *i;
919 uint32_t total = 0;
920 char *list = NULL;
921
922 QTAILQ_FOREACH(i, &fw_boot_order, link) {
923 char *devpath = NULL, *bootpath;
924 int len;
925
926 if (i->dev) {
927 devpath = qdev_get_fw_dev_path(i->dev);
928 assert(devpath);
929 }
930
931 if (i->suffix && devpath) {
932 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
933
934 bootpath = g_malloc(bootpathlen);
935 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
936 g_free(devpath);
937 } else if (devpath) {
938 bootpath = devpath;
939 } else {
940 assert(i->suffix);
941 bootpath = g_strdup(i->suffix);
942 }
943
944 if (total) {
945 list[total-1] = '\n';
946 }
947 len = strlen(bootpath) + 1;
948 list = g_realloc(list, total + len);
949 memcpy(&list[total], bootpath, len);
950 total += len;
951 g_free(bootpath);
952 }
953
954 *size = total;
955
956 return list;
957 }
958
959 static void numa_add(const char *optarg)
960 {
961 char option[128];
962 char *endptr;
963 unsigned long long value, endvalue;
964 int nodenr;
965
966 value = endvalue = 0ULL;
967
968 optarg = get_opt_name(option, 128, optarg, ',') + 1;
969 if (!strcmp(option, "node")) {
970 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
971 nodenr = nb_numa_nodes;
972 } else {
973 nodenr = strtoull(option, NULL, 10);
974 }
975
976 if (get_param_value(option, 128, "mem", optarg) == 0) {
977 node_mem[nodenr] = 0;
978 } else {
979 int64_t sval;
980 sval = strtosz(option, &endptr);
981 if (sval < 0 || *endptr) {
982 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
983 exit(1);
984 }
985 node_mem[nodenr] = sval;
986 }
987 if (get_param_value(option, 128, "cpus", optarg) != 0) {
988 value = strtoull(option, &endptr, 10);
989 if (*endptr == '-') {
990 endvalue = strtoull(endptr+1, &endptr, 10);
991 } else {
992 endvalue = value;
993 }
994
995 if (!(endvalue < MAX_CPUMASK_BITS)) {
996 endvalue = MAX_CPUMASK_BITS - 1;
997 fprintf(stderr,
998 "A max of %d CPUs are supported in a guest\n",
999 MAX_CPUMASK_BITS);
1000 }
1001
1002 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1003 }
1004 nb_numa_nodes++;
1005 }
1006 return;
1007 }
1008
1009 static void smp_parse(const char *optarg)
1010 {
1011 int smp, sockets = 0, threads = 0, cores = 0;
1012 char *endptr;
1013 char option[128];
1014
1015 smp = strtoul(optarg, &endptr, 10);
1016 if (endptr != optarg) {
1017 if (*endptr == ',') {
1018 endptr++;
1019 }
1020 }
1021 if (get_param_value(option, 128, "sockets", endptr) != 0)
1022 sockets = strtoull(option, NULL, 10);
1023 if (get_param_value(option, 128, "cores", endptr) != 0)
1024 cores = strtoull(option, NULL, 10);
1025 if (get_param_value(option, 128, "threads", endptr) != 0)
1026 threads = strtoull(option, NULL, 10);
1027 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1028 max_cpus = strtoull(option, NULL, 10);
1029
1030 /* compute missing values, prefer sockets over cores over threads */
1031 if (smp == 0 || sockets == 0) {
1032 sockets = sockets > 0 ? sockets : 1;
1033 cores = cores > 0 ? cores : 1;
1034 threads = threads > 0 ? threads : 1;
1035 if (smp == 0) {
1036 smp = cores * threads * sockets;
1037 }
1038 } else {
1039 if (cores == 0) {
1040 threads = threads > 0 ? threads : 1;
1041 cores = smp / (sockets * threads);
1042 } else {
1043 threads = smp / (cores * sockets);
1044 }
1045 }
1046 smp_cpus = smp;
1047 smp_cores = cores > 0 ? cores : 1;
1048 smp_threads = threads > 0 ? threads : 1;
1049 if (max_cpus == 0)
1050 max_cpus = smp_cpus;
1051 }
1052
1053 /***********************************************************/
1054 /* USB devices */
1055
1056 static int usb_device_add(const char *devname)
1057 {
1058 const char *p;
1059 USBDevice *dev = NULL;
1060
1061 if (!usb_enabled)
1062 return -1;
1063
1064 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1065 dev = usbdevice_create(devname);
1066 if (dev)
1067 goto done;
1068
1069 /* the other ones */
1070 #ifndef CONFIG_LINUX
1071 /* only the linux version is qdev-ified, usb-bsd still needs this */
1072 if (strstart(devname, "host:", &p)) {
1073 dev = usb_host_device_open(usb_bus_find(-1), p);
1074 } else
1075 #endif
1076 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1077 dev = usb_bt_init(usb_bus_find(-1),
1078 devname[2] ? hci_init(p)
1079 : bt_new_hci(qemu_find_bt_vlan(0)));
1080 } else {
1081 return -1;
1082 }
1083 if (!dev)
1084 return -1;
1085
1086 done:
1087 return 0;
1088 }
1089
1090 static int usb_device_del(const char *devname)
1091 {
1092 int bus_num, addr;
1093 const char *p;
1094
1095 if (strstart(devname, "host:", &p))
1096 return usb_host_device_close(p);
1097
1098 if (!usb_enabled)
1099 return -1;
1100
1101 p = strchr(devname, '.');
1102 if (!p)
1103 return -1;
1104 bus_num = strtoul(devname, NULL, 0);
1105 addr = strtoul(p + 1, NULL, 0);
1106
1107 return usb_device_delete_addr(bus_num, addr);
1108 }
1109
1110 static int usb_parse(const char *cmdline)
1111 {
1112 int r;
1113 r = usb_device_add(cmdline);
1114 if (r < 0) {
1115 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1116 }
1117 return r;
1118 }
1119
1120 void do_usb_add(Monitor *mon, const QDict *qdict)
1121 {
1122 const char *devname = qdict_get_str(qdict, "devname");
1123 if (usb_device_add(devname) < 0) {
1124 error_report("could not add USB device '%s'", devname);
1125 }
1126 }
1127
1128 void do_usb_del(Monitor *mon, const QDict *qdict)
1129 {
1130 const char *devname = qdict_get_str(qdict, "devname");
1131 if (usb_device_del(devname) < 0) {
1132 error_report("could not delete USB device '%s'", devname);
1133 }
1134 }
1135
1136 /***********************************************************/
1137 /* PCMCIA/Cardbus */
1138
1139 static struct pcmcia_socket_entry_s {
1140 PCMCIASocket *socket;
1141 struct pcmcia_socket_entry_s *next;
1142 } *pcmcia_sockets = 0;
1143
1144 void pcmcia_socket_register(PCMCIASocket *socket)
1145 {
1146 struct pcmcia_socket_entry_s *entry;
1147
1148 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1149 entry->socket = socket;
1150 entry->next = pcmcia_sockets;
1151 pcmcia_sockets = entry;
1152 }
1153
1154 void pcmcia_socket_unregister(PCMCIASocket *socket)
1155 {
1156 struct pcmcia_socket_entry_s *entry, **ptr;
1157
1158 ptr = &pcmcia_sockets;
1159 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1160 if (entry->socket == socket) {
1161 *ptr = entry->next;
1162 g_free(entry);
1163 }
1164 }
1165
1166 void pcmcia_info(Monitor *mon)
1167 {
1168 struct pcmcia_socket_entry_s *iter;
1169
1170 if (!pcmcia_sockets)
1171 monitor_printf(mon, "No PCMCIA sockets\n");
1172
1173 for (iter = pcmcia_sockets; iter; iter = iter->next)
1174 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1175 iter->socket->attached ? iter->socket->card_string :
1176 "Empty");
1177 }
1178
1179 /***********************************************************/
1180 /* machine registration */
1181
1182 static QEMUMachine *first_machine = NULL;
1183 QEMUMachine *current_machine = NULL;
1184
1185 int qemu_register_machine(QEMUMachine *m)
1186 {
1187 QEMUMachine **pm;
1188 pm = &first_machine;
1189 while (*pm != NULL)
1190 pm = &(*pm)->next;
1191 m->next = NULL;
1192 *pm = m;
1193 return 0;
1194 }
1195
1196 static QEMUMachine *find_machine(const char *name)
1197 {
1198 QEMUMachine *m;
1199
1200 for(m = first_machine; m != NULL; m = m->next) {
1201 if (!strcmp(m->name, name))
1202 return m;
1203 if (m->alias && !strcmp(m->alias, name))
1204 return m;
1205 }
1206 return NULL;
1207 }
1208
1209 QEMUMachine *find_default_machine(void)
1210 {
1211 QEMUMachine *m;
1212
1213 for(m = first_machine; m != NULL; m = m->next) {
1214 if (m->is_default) {
1215 return m;
1216 }
1217 }
1218 return NULL;
1219 }
1220
1221 MachineInfoList *qmp_query_machines(Error **errp)
1222 {
1223 MachineInfoList *mach_list = NULL;
1224 QEMUMachine *m;
1225
1226 for (m = first_machine; m; m = m->next) {
1227 MachineInfoList *entry;
1228 MachineInfo *info;
1229
1230 info = g_malloc0(sizeof(*info));
1231 if (m->is_default) {
1232 info->has_is_default = true;
1233 info->is_default = true;
1234 }
1235
1236 if (m->alias) {
1237 info->has_alias = true;
1238 info->alias = g_strdup(m->alias);
1239 }
1240
1241 info->name = g_strdup(m->name);
1242
1243 entry = g_malloc0(sizeof(*entry));
1244 entry->value = info;
1245 entry->next = mach_list;
1246 mach_list = entry;
1247 }
1248
1249 return mach_list;
1250 }
1251
1252 /***********************************************************/
1253 /* main execution loop */
1254
1255 static void gui_update(void *opaque)
1256 {
1257 uint64_t interval = GUI_REFRESH_INTERVAL;
1258 DisplayState *ds = opaque;
1259 DisplayChangeListener *dcl = ds->listeners;
1260
1261 dpy_refresh(ds);
1262
1263 while (dcl != NULL) {
1264 if (dcl->gui_timer_interval &&
1265 dcl->gui_timer_interval < interval)
1266 interval = dcl->gui_timer_interval;
1267 dcl = dcl->next;
1268 }
1269 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1270 }
1271
1272 struct vm_change_state_entry {
1273 VMChangeStateHandler *cb;
1274 void *opaque;
1275 QLIST_ENTRY (vm_change_state_entry) entries;
1276 };
1277
1278 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1279
1280 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1281 void *opaque)
1282 {
1283 VMChangeStateEntry *e;
1284
1285 e = g_malloc0(sizeof (*e));
1286
1287 e->cb = cb;
1288 e->opaque = opaque;
1289 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1290 return e;
1291 }
1292
1293 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1294 {
1295 QLIST_REMOVE (e, entries);
1296 g_free (e);
1297 }
1298
1299 void vm_state_notify(int running, RunState state)
1300 {
1301 VMChangeStateEntry *e;
1302
1303 trace_vm_state_notify(running, state);
1304
1305 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1306 e->cb(e->opaque, running, state);
1307 }
1308 }
1309
1310 void vm_start(void)
1311 {
1312 if (!runstate_is_running()) {
1313 cpu_enable_ticks();
1314 runstate_set(RUN_STATE_RUNNING);
1315 vm_state_notify(1, RUN_STATE_RUNNING);
1316 resume_all_vcpus();
1317 monitor_protocol_event(QEVENT_RESUME, NULL);
1318 }
1319 }
1320
1321 /* reset/shutdown handler */
1322
1323 typedef struct QEMUResetEntry {
1324 QTAILQ_ENTRY(QEMUResetEntry) entry;
1325 QEMUResetHandler *func;
1326 void *opaque;
1327 } QEMUResetEntry;
1328
1329 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1330 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1331 static int reset_requested;
1332 static int shutdown_requested, shutdown_signal = -1;
1333 static pid_t shutdown_pid;
1334 static int powerdown_requested;
1335 static int debug_requested;
1336 static int suspend_requested;
1337 static int wakeup_requested;
1338 static NotifierList suspend_notifiers =
1339 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1340 static NotifierList wakeup_notifiers =
1341 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1342 static uint32_t wakeup_reason_mask = ~0;
1343 static RunState vmstop_requested = RUN_STATE_MAX;
1344
1345 int qemu_shutdown_requested_get(void)
1346 {
1347 return shutdown_requested;
1348 }
1349
1350 int qemu_reset_requested_get(void)
1351 {
1352 return reset_requested;
1353 }
1354
1355 int qemu_shutdown_requested(void)
1356 {
1357 int r = shutdown_requested;
1358 shutdown_requested = 0;
1359 return r;
1360 }
1361
1362 void qemu_kill_report(void)
1363 {
1364 if (!qtest_enabled() && shutdown_signal != -1) {
1365 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1366 if (shutdown_pid == 0) {
1367 /* This happens for eg ^C at the terminal, so it's worth
1368 * avoiding printing an odd message in that case.
1369 */
1370 fputc('\n', stderr);
1371 } else {
1372 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1373 }
1374 shutdown_signal = -1;
1375 }
1376 }
1377
1378 int qemu_reset_requested(void)
1379 {
1380 int r = reset_requested;
1381 reset_requested = 0;
1382 return r;
1383 }
1384
1385 static int qemu_suspend_requested(void)
1386 {
1387 int r = suspend_requested;
1388 suspend_requested = 0;
1389 return r;
1390 }
1391
1392 static int qemu_wakeup_requested(void)
1393 {
1394 int r = wakeup_requested;
1395 wakeup_requested = 0;
1396 return r;
1397 }
1398
1399 int qemu_powerdown_requested(void)
1400 {
1401 int r = powerdown_requested;
1402 powerdown_requested = 0;
1403 return r;
1404 }
1405
1406 static int qemu_debug_requested(void)
1407 {
1408 int r = debug_requested;
1409 debug_requested = 0;
1410 return r;
1411 }
1412
1413 /* We use RUN_STATE_MAX but any invalid value will do */
1414 static bool qemu_vmstop_requested(RunState *r)
1415 {
1416 if (vmstop_requested < RUN_STATE_MAX) {
1417 *r = vmstop_requested;
1418 vmstop_requested = RUN_STATE_MAX;
1419 return true;
1420 }
1421
1422 return false;
1423 }
1424
1425 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1426 {
1427 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1428
1429 re->func = func;
1430 re->opaque = opaque;
1431 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1432 }
1433
1434 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1435 {
1436 QEMUResetEntry *re;
1437
1438 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1439 if (re->func == func && re->opaque == opaque) {
1440 QTAILQ_REMOVE(&reset_handlers, re, entry);
1441 g_free(re);
1442 return;
1443 }
1444 }
1445 }
1446
1447 void qemu_system_reset(bool report)
1448 {
1449 QEMUResetEntry *re, *nre;
1450
1451 /* reset all devices */
1452 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1453 re->func(re->opaque);
1454 }
1455 if (report) {
1456 monitor_protocol_event(QEVENT_RESET, NULL);
1457 }
1458 cpu_synchronize_all_post_reset();
1459 }
1460
1461 void qemu_system_reset_request(void)
1462 {
1463 if (no_reboot) {
1464 shutdown_requested = 1;
1465 } else {
1466 reset_requested = 1;
1467 }
1468 cpu_stop_current();
1469 qemu_notify_event();
1470 }
1471
1472 static void qemu_system_suspend(void)
1473 {
1474 pause_all_vcpus();
1475 notifier_list_notify(&suspend_notifiers, NULL);
1476 runstate_set(RUN_STATE_SUSPENDED);
1477 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1478 }
1479
1480 void qemu_system_suspend_request(void)
1481 {
1482 if (runstate_check(RUN_STATE_SUSPENDED)) {
1483 return;
1484 }
1485 suspend_requested = 1;
1486 cpu_stop_current();
1487 qemu_notify_event();
1488 }
1489
1490 void qemu_register_suspend_notifier(Notifier *notifier)
1491 {
1492 notifier_list_add(&suspend_notifiers, notifier);
1493 }
1494
1495 void qemu_system_wakeup_request(WakeupReason reason)
1496 {
1497 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1498 return;
1499 }
1500 if (!(wakeup_reason_mask & (1 << reason))) {
1501 return;
1502 }
1503 runstate_set(RUN_STATE_RUNNING);
1504 notifier_list_notify(&wakeup_notifiers, &reason);
1505 wakeup_requested = 1;
1506 qemu_notify_event();
1507 }
1508
1509 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1510 {
1511 if (enabled) {
1512 wakeup_reason_mask |= (1 << reason);
1513 } else {
1514 wakeup_reason_mask &= ~(1 << reason);
1515 }
1516 }
1517
1518 void qemu_register_wakeup_notifier(Notifier *notifier)
1519 {
1520 notifier_list_add(&wakeup_notifiers, notifier);
1521 }
1522
1523 void qemu_system_killed(int signal, pid_t pid)
1524 {
1525 shutdown_signal = signal;
1526 shutdown_pid = pid;
1527 no_shutdown = 0;
1528 qemu_system_shutdown_request();
1529 }
1530
1531 void qemu_system_shutdown_request(void)
1532 {
1533 shutdown_requested = 1;
1534 qemu_notify_event();
1535 }
1536
1537 void qemu_system_powerdown_request(void)
1538 {
1539 powerdown_requested = 1;
1540 qemu_notify_event();
1541 }
1542
1543 void qemu_system_debug_request(void)
1544 {
1545 debug_requested = 1;
1546 qemu_notify_event();
1547 }
1548
1549 void qemu_system_vmstop_request(RunState state)
1550 {
1551 vmstop_requested = state;
1552 qemu_notify_event();
1553 }
1554
1555 qemu_irq qemu_system_powerdown;
1556
1557 static bool main_loop_should_exit(void)
1558 {
1559 RunState r;
1560 if (qemu_debug_requested()) {
1561 vm_stop(RUN_STATE_DEBUG);
1562 }
1563 if (qemu_suspend_requested()) {
1564 qemu_system_suspend();
1565 }
1566 if (qemu_shutdown_requested()) {
1567 qemu_kill_report();
1568 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1569 if (no_shutdown) {
1570 vm_stop(RUN_STATE_SHUTDOWN);
1571 } else {
1572 return true;
1573 }
1574 }
1575 if (qemu_reset_requested()) {
1576 pause_all_vcpus();
1577 cpu_synchronize_all_states();
1578 qemu_system_reset(VMRESET_REPORT);
1579 resume_all_vcpus();
1580 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1581 runstate_check(RUN_STATE_SHUTDOWN)) {
1582 runstate_set(RUN_STATE_PAUSED);
1583 }
1584 }
1585 if (qemu_wakeup_requested()) {
1586 pause_all_vcpus();
1587 cpu_synchronize_all_states();
1588 qemu_system_reset(VMRESET_SILENT);
1589 resume_all_vcpus();
1590 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1591 }
1592 if (qemu_powerdown_requested()) {
1593 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1594 qemu_irq_raise(qemu_system_powerdown);
1595 }
1596 if (qemu_vmstop_requested(&r)) {
1597 vm_stop(r);
1598 }
1599 return false;
1600 }
1601
1602 static void main_loop(void)
1603 {
1604 bool nonblocking;
1605 int last_io = 0;
1606 #ifdef CONFIG_PROFILER
1607 int64_t ti;
1608 #endif
1609 do {
1610 nonblocking = !kvm_enabled() && last_io > 0;
1611 #ifdef CONFIG_PROFILER
1612 ti = profile_getclock();
1613 #endif
1614 last_io = main_loop_wait(nonblocking);
1615 #ifdef CONFIG_PROFILER
1616 dev_time += profile_getclock() - ti;
1617 #endif
1618 } while (!main_loop_should_exit());
1619 }
1620
1621 static void version(void)
1622 {
1623 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1624 }
1625
1626 static void help(int exitcode)
1627 {
1628 version();
1629 printf("usage: %s [options] [disk_image]\n\n"
1630 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1631 error_get_progname());
1632
1633 #define QEMU_OPTIONS_GENERATE_HELP
1634 #include "qemu-options-wrapper.h"
1635
1636 printf("\nDuring emulation, the following keys are useful:\n"
1637 "ctrl-alt-f toggle full screen\n"
1638 "ctrl-alt-n switch to virtual console 'n'\n"
1639 "ctrl-alt toggle mouse and keyboard grab\n"
1640 "\n"
1641 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1642
1643 exit(exitcode);
1644 }
1645
1646 #define HAS_ARG 0x0001
1647
1648 typedef struct QEMUOption {
1649 const char *name;
1650 int flags;
1651 int index;
1652 uint32_t arch_mask;
1653 } QEMUOption;
1654
1655 static const QEMUOption qemu_options[] = {
1656 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1657 #define QEMU_OPTIONS_GENERATE_OPTIONS
1658 #include "qemu-options-wrapper.h"
1659 { NULL },
1660 };
1661
1662 static bool vga_available(void)
1663 {
1664 return qdev_exists("VGA") || qdev_exists("isa-vga");
1665 }
1666
1667 static bool cirrus_vga_available(void)
1668 {
1669 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1670 }
1671
1672 static bool vmware_vga_available(void)
1673 {
1674 return qdev_exists("vmware-svga");
1675 }
1676
1677 static void select_vgahw (const char *p)
1678 {
1679 const char *opts;
1680
1681 vga_interface_type = VGA_NONE;
1682 if (strstart(p, "std", &opts)) {
1683 if (vga_available()) {
1684 vga_interface_type = VGA_STD;
1685 } else {
1686 fprintf(stderr, "Error: standard VGA not available\n");
1687 exit(0);
1688 }
1689 } else if (strstart(p, "cirrus", &opts)) {
1690 if (cirrus_vga_available()) {
1691 vga_interface_type = VGA_CIRRUS;
1692 } else {
1693 fprintf(stderr, "Error: Cirrus VGA not available\n");
1694 exit(0);
1695 }
1696 } else if (strstart(p, "vmware", &opts)) {
1697 if (vmware_vga_available()) {
1698 vga_interface_type = VGA_VMWARE;
1699 } else {
1700 fprintf(stderr, "Error: VMWare SVGA not available\n");
1701 exit(0);
1702 }
1703 } else if (strstart(p, "xenfb", &opts)) {
1704 vga_interface_type = VGA_XENFB;
1705 } else if (strstart(p, "qxl", &opts)) {
1706 vga_interface_type = VGA_QXL;
1707 } else if (!strstart(p, "none", &opts)) {
1708 invalid_vga:
1709 fprintf(stderr, "Unknown vga type: %s\n", p);
1710 exit(1);
1711 }
1712 while (*opts) {
1713 const char *nextopt;
1714
1715 if (strstart(opts, ",retrace=", &nextopt)) {
1716 opts = nextopt;
1717 if (strstart(opts, "dumb", &nextopt))
1718 vga_retrace_method = VGA_RETRACE_DUMB;
1719 else if (strstart(opts, "precise", &nextopt))
1720 vga_retrace_method = VGA_RETRACE_PRECISE;
1721 else goto invalid_vga;
1722 } else goto invalid_vga;
1723 opts = nextopt;
1724 }
1725 }
1726
1727 static DisplayType select_display(const char *p)
1728 {
1729 const char *opts;
1730 DisplayType display = DT_DEFAULT;
1731
1732 if (strstart(p, "sdl", &opts)) {
1733 #ifdef CONFIG_SDL
1734 display = DT_SDL;
1735 while (*opts) {
1736 const char *nextopt;
1737
1738 if (strstart(opts, ",frame=", &nextopt)) {
1739 opts = nextopt;
1740 if (strstart(opts, "on", &nextopt)) {
1741 no_frame = 0;
1742 } else if (strstart(opts, "off", &nextopt)) {
1743 no_frame = 1;
1744 } else {
1745 goto invalid_sdl_args;
1746 }
1747 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1748 opts = nextopt;
1749 if (strstart(opts, "on", &nextopt)) {
1750 alt_grab = 1;
1751 } else if (strstart(opts, "off", &nextopt)) {
1752 alt_grab = 0;
1753 } else {
1754 goto invalid_sdl_args;
1755 }
1756 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1757 opts = nextopt;
1758 if (strstart(opts, "on", &nextopt)) {
1759 ctrl_grab = 1;
1760 } else if (strstart(opts, "off", &nextopt)) {
1761 ctrl_grab = 0;
1762 } else {
1763 goto invalid_sdl_args;
1764 }
1765 } else if (strstart(opts, ",window_close=", &nextopt)) {
1766 opts = nextopt;
1767 if (strstart(opts, "on", &nextopt)) {
1768 no_quit = 0;
1769 } else if (strstart(opts, "off", &nextopt)) {
1770 no_quit = 1;
1771 } else {
1772 goto invalid_sdl_args;
1773 }
1774 } else {
1775 invalid_sdl_args:
1776 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1777 exit(1);
1778 }
1779 opts = nextopt;
1780 }
1781 #else
1782 fprintf(stderr, "SDL support is disabled\n");
1783 exit(1);
1784 #endif
1785 } else if (strstart(p, "vnc", &opts)) {
1786 #ifdef CONFIG_VNC
1787 display_remote++;
1788
1789 if (*opts) {
1790 const char *nextopt;
1791
1792 if (strstart(opts, "=", &nextopt)) {
1793 vnc_display = nextopt;
1794 }
1795 }
1796 if (!vnc_display) {
1797 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1798 exit(1);
1799 }
1800 #else
1801 fprintf(stderr, "VNC support is disabled\n");
1802 exit(1);
1803 #endif
1804 } else if (strstart(p, "curses", &opts)) {
1805 #ifdef CONFIG_CURSES
1806 display = DT_CURSES;
1807 #else
1808 fprintf(stderr, "Curses support is disabled\n");
1809 exit(1);
1810 #endif
1811 } else if (strstart(p, "none", &opts)) {
1812 display = DT_NONE;
1813 } else {
1814 fprintf(stderr, "Unknown display type: %s\n", p);
1815 exit(1);
1816 }
1817
1818 return display;
1819 }
1820
1821 static int balloon_parse(const char *arg)
1822 {
1823 QemuOpts *opts;
1824
1825 if (strcmp(arg, "none") == 0) {
1826 return 0;
1827 }
1828
1829 if (!strncmp(arg, "virtio", 6)) {
1830 if (arg[6] == ',') {
1831 /* have params -> parse them */
1832 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1833 if (!opts)
1834 return -1;
1835 } else {
1836 /* create empty opts */
1837 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1838 }
1839 qemu_opt_set(opts, "driver", "virtio-balloon");
1840 return 0;
1841 }
1842
1843 return -1;
1844 }
1845
1846 char *qemu_find_file(int type, const char *name)
1847 {
1848 int len;
1849 const char *subdir;
1850 char *buf;
1851
1852 /* Try the name as a straight path first */
1853 if (access(name, R_OK) == 0) {
1854 return g_strdup(name);
1855 }
1856 switch (type) {
1857 case QEMU_FILE_TYPE_BIOS:
1858 subdir = "";
1859 break;
1860 case QEMU_FILE_TYPE_KEYMAP:
1861 subdir = "keymaps/";
1862 break;
1863 default:
1864 abort();
1865 }
1866 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1867 buf = g_malloc0(len);
1868 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1869 if (access(buf, R_OK)) {
1870 g_free(buf);
1871 return NULL;
1872 }
1873 return buf;
1874 }
1875
1876 static int device_help_func(QemuOpts *opts, void *opaque)
1877 {
1878 return qdev_device_help(opts);
1879 }
1880
1881 static int device_init_func(QemuOpts *opts, void *opaque)
1882 {
1883 DeviceState *dev;
1884
1885 dev = qdev_device_add(opts);
1886 if (!dev)
1887 return -1;
1888 return 0;
1889 }
1890
1891 static int chardev_init_func(QemuOpts *opts, void *opaque)
1892 {
1893 CharDriverState *chr;
1894
1895 chr = qemu_chr_new_from_opts(opts, NULL);
1896 if (!chr)
1897 return -1;
1898 return 0;
1899 }
1900
1901 #ifdef CONFIG_VIRTFS
1902 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1903 {
1904 int ret;
1905 ret = qemu_fsdev_add(opts);
1906
1907 return ret;
1908 }
1909 #endif
1910
1911 static int mon_init_func(QemuOpts *opts, void *opaque)
1912 {
1913 CharDriverState *chr;
1914 const char *chardev;
1915 const char *mode;
1916 int flags;
1917
1918 mode = qemu_opt_get(opts, "mode");
1919 if (mode == NULL) {
1920 mode = "readline";
1921 }
1922 if (strcmp(mode, "readline") == 0) {
1923 flags = MONITOR_USE_READLINE;
1924 } else if (strcmp(mode, "control") == 0) {
1925 flags = MONITOR_USE_CONTROL;
1926 } else {
1927 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1928 exit(1);
1929 }
1930
1931 if (qemu_opt_get_bool(opts, "pretty", 0))
1932 flags |= MONITOR_USE_PRETTY;
1933
1934 if (qemu_opt_get_bool(opts, "default", 0))
1935 flags |= MONITOR_IS_DEFAULT;
1936
1937 chardev = qemu_opt_get(opts, "chardev");
1938 chr = qemu_chr_find(chardev);
1939 if (chr == NULL) {
1940 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1941 exit(1);
1942 }
1943
1944 monitor_init(chr, flags);
1945 return 0;
1946 }
1947
1948 static void monitor_parse(const char *optarg, const char *mode)
1949 {
1950 static int monitor_device_index = 0;
1951 QemuOpts *opts;
1952 const char *p;
1953 char label[32];
1954 int def = 0;
1955
1956 if (strstart(optarg, "chardev:", &p)) {
1957 snprintf(label, sizeof(label), "%s", p);
1958 } else {
1959 snprintf(label, sizeof(label), "compat_monitor%d",
1960 monitor_device_index);
1961 if (monitor_device_index == 0) {
1962 def = 1;
1963 }
1964 opts = qemu_chr_parse_compat(label, optarg);
1965 if (!opts) {
1966 fprintf(stderr, "parse error: %s\n", optarg);
1967 exit(1);
1968 }
1969 }
1970
1971 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
1972 if (!opts) {
1973 fprintf(stderr, "duplicate chardev: %s\n", label);
1974 exit(1);
1975 }
1976 qemu_opt_set(opts, "mode", mode);
1977 qemu_opt_set(opts, "chardev", label);
1978 if (def)
1979 qemu_opt_set(opts, "default", "on");
1980 monitor_device_index++;
1981 }
1982
1983 struct device_config {
1984 enum {
1985 DEV_USB, /* -usbdevice */
1986 DEV_BT, /* -bt */
1987 DEV_SERIAL, /* -serial */
1988 DEV_PARALLEL, /* -parallel */
1989 DEV_VIRTCON, /* -virtioconsole */
1990 DEV_DEBUGCON, /* -debugcon */
1991 DEV_GDB, /* -gdb, -s */
1992 } type;
1993 const char *cmdline;
1994 Location loc;
1995 QTAILQ_ENTRY(device_config) next;
1996 };
1997 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1998
1999 static void add_device_config(int type, const char *cmdline)
2000 {
2001 struct device_config *conf;
2002
2003 conf = g_malloc0(sizeof(*conf));
2004 conf->type = type;
2005 conf->cmdline = cmdline;
2006 loc_save(&conf->loc);
2007 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2008 }
2009
2010 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2011 {
2012 struct device_config *conf;
2013 int rc;
2014
2015 QTAILQ_FOREACH(conf, &device_configs, next) {
2016 if (conf->type != type)
2017 continue;
2018 loc_push_restore(&conf->loc);
2019 rc = func(conf->cmdline);
2020 loc_pop(&conf->loc);
2021 if (0 != rc)
2022 return rc;
2023 }
2024 return 0;
2025 }
2026
2027 static int serial_parse(const char *devname)
2028 {
2029 static int index = 0;
2030 char label[32];
2031
2032 if (strcmp(devname, "none") == 0)
2033 return 0;
2034 if (index == MAX_SERIAL_PORTS) {
2035 fprintf(stderr, "qemu: too many serial ports\n");
2036 exit(1);
2037 }
2038 snprintf(label, sizeof(label), "serial%d", index);
2039 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2040 if (!serial_hds[index]) {
2041 fprintf(stderr, "qemu: could not connect serial device"
2042 " to character backend '%s'\n", devname);
2043 return -1;
2044 }
2045 index++;
2046 return 0;
2047 }
2048
2049 static int parallel_parse(const char *devname)
2050 {
2051 static int index = 0;
2052 char label[32];
2053
2054 if (strcmp(devname, "none") == 0)
2055 return 0;
2056 if (index == MAX_PARALLEL_PORTS) {
2057 fprintf(stderr, "qemu: too many parallel ports\n");
2058 exit(1);
2059 }
2060 snprintf(label, sizeof(label), "parallel%d", index);
2061 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2062 if (!parallel_hds[index]) {
2063 fprintf(stderr, "qemu: could not connect parallel device"
2064 " to character backend '%s'\n", devname);
2065 return -1;
2066 }
2067 index++;
2068 return 0;
2069 }
2070
2071 static int virtcon_parse(const char *devname)
2072 {
2073 QemuOptsList *device = qemu_find_opts("device");
2074 static int index = 0;
2075 char label[32];
2076 QemuOpts *bus_opts, *dev_opts;
2077
2078 if (strcmp(devname, "none") == 0)
2079 return 0;
2080 if (index == MAX_VIRTIO_CONSOLES) {
2081 fprintf(stderr, "qemu: too many virtio consoles\n");
2082 exit(1);
2083 }
2084
2085 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2086 if (arch_type == QEMU_ARCH_S390X) {
2087 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2088 } else {
2089 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2090 }
2091
2092 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2093 qemu_opt_set(dev_opts, "driver", "virtconsole");
2094
2095 snprintf(label, sizeof(label), "virtcon%d", index);
2096 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2097 if (!virtcon_hds[index]) {
2098 fprintf(stderr, "qemu: could not connect virtio console"
2099 " to character backend '%s'\n", devname);
2100 return -1;
2101 }
2102 qemu_opt_set(dev_opts, "chardev", label);
2103
2104 index++;
2105 return 0;
2106 }
2107
2108 static int debugcon_parse(const char *devname)
2109 {
2110 QemuOpts *opts;
2111
2112 if (!qemu_chr_new("debugcon", devname, NULL)) {
2113 exit(1);
2114 }
2115 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2116 if (!opts) {
2117 fprintf(stderr, "qemu: already have a debugcon device\n");
2118 exit(1);
2119 }
2120 qemu_opt_set(opts, "driver", "isa-debugcon");
2121 qemu_opt_set(opts, "chardev", "debugcon");
2122 return 0;
2123 }
2124
2125 static QEMUMachine *machine_parse(const char *name)
2126 {
2127 QEMUMachine *m, *machine = NULL;
2128
2129 if (name) {
2130 machine = find_machine(name);
2131 }
2132 if (machine) {
2133 return machine;
2134 }
2135 printf("Supported machines are:\n");
2136 for (m = first_machine; m != NULL; m = m->next) {
2137 if (m->alias) {
2138 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2139 }
2140 printf("%-20s %s%s\n", m->name, m->desc,
2141 m->is_default ? " (default)" : "");
2142 }
2143 exit(!name || !is_help_option(name));
2144 }
2145
2146 static int tcg_init(void)
2147 {
2148 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2149 return 0;
2150 }
2151
2152 static struct {
2153 const char *opt_name;
2154 const char *name;
2155 int (*available)(void);
2156 int (*init)(void);
2157 int *allowed;
2158 } accel_list[] = {
2159 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2160 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2161 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2162 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2163 };
2164
2165 static int configure_accelerator(void)
2166 {
2167 const char *p = NULL;
2168 char buf[10];
2169 int i, ret;
2170 bool accel_initialised = 0;
2171 bool init_failed = 0;
2172
2173 QemuOptsList *list = qemu_find_opts("machine");
2174 if (!QTAILQ_EMPTY(&list->head)) {
2175 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2176 }
2177
2178 if (p == NULL) {
2179 /* Use the default "accelerator", tcg */
2180 p = "tcg";
2181 }
2182
2183 while (!accel_initialised && *p != '\0') {
2184 if (*p == ':') {
2185 p++;
2186 }
2187 p = get_opt_name(buf, sizeof (buf), p, ':');
2188 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2189 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2190 *(accel_list[i].allowed) = 1;
2191 ret = accel_list[i].init();
2192 if (ret < 0) {
2193 init_failed = 1;
2194 if (!accel_list[i].available()) {
2195 printf("%s not supported for this target\n",
2196 accel_list[i].name);
2197 } else {
2198 fprintf(stderr, "failed to initialize %s: %s\n",
2199 accel_list[i].name,
2200 strerror(-ret));
2201 }
2202 *(accel_list[i].allowed) = 0;
2203 } else {
2204 accel_initialised = 1;
2205 }
2206 break;
2207 }
2208 }
2209 if (i == ARRAY_SIZE(accel_list)) {
2210 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2211 }
2212 }
2213
2214 if (!accel_initialised) {
2215 fprintf(stderr, "No accelerator found!\n");
2216 exit(1);
2217 }
2218
2219 if (init_failed) {
2220 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2221 }
2222
2223 return !accel_initialised;
2224 }
2225
2226 void qemu_add_exit_notifier(Notifier *notify)
2227 {
2228 notifier_list_add(&exit_notifiers, notify);
2229 }
2230
2231 void qemu_remove_exit_notifier(Notifier *notify)
2232 {
2233 notifier_remove(notify);
2234 }
2235
2236 static void qemu_run_exit_notifiers(void)
2237 {
2238 notifier_list_notify(&exit_notifiers, NULL);
2239 }
2240
2241 void qemu_add_machine_init_done_notifier(Notifier *notify)
2242 {
2243 notifier_list_add(&machine_init_done_notifiers, notify);
2244 }
2245
2246 static void qemu_run_machine_init_done_notifiers(void)
2247 {
2248 notifier_list_notify(&machine_init_done_notifiers, NULL);
2249 }
2250
2251 static const QEMUOption *lookup_opt(int argc, char **argv,
2252 const char **poptarg, int *poptind)
2253 {
2254 const QEMUOption *popt;
2255 int optind = *poptind;
2256 char *r = argv[optind];
2257 const char *optarg;
2258
2259 loc_set_cmdline(argv, optind, 1);
2260 optind++;
2261 /* Treat --foo the same as -foo. */
2262 if (r[1] == '-')
2263 r++;
2264 popt = qemu_options;
2265 for(;;) {
2266 if (!popt->name) {
2267 error_report("invalid option");
2268 exit(1);
2269 }
2270 if (!strcmp(popt->name, r + 1))
2271 break;
2272 popt++;
2273 }
2274 if (popt->flags & HAS_ARG) {
2275 if (optind >= argc) {
2276 error_report("requires an argument");
2277 exit(1);
2278 }
2279 optarg = argv[optind++];
2280 loc_set_cmdline(argv, optind - 2, 2);
2281 } else {
2282 optarg = NULL;
2283 }
2284
2285 *poptarg = optarg;
2286 *poptind = optind;
2287
2288 return popt;
2289 }
2290
2291 static gpointer malloc_and_trace(gsize n_bytes)
2292 {
2293 void *ptr = malloc(n_bytes);
2294 trace_g_malloc(n_bytes, ptr);
2295 return ptr;
2296 }
2297
2298 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2299 {
2300 void *ptr = realloc(mem, n_bytes);
2301 trace_g_realloc(mem, n_bytes, ptr);
2302 return ptr;
2303 }
2304
2305 static void free_and_trace(gpointer mem)
2306 {
2307 trace_g_free(mem);
2308 free(mem);
2309 }
2310
2311 int qemu_init_main_loop(void)
2312 {
2313 return main_loop_init();
2314 }
2315
2316 int main(int argc, char **argv, char **envp)
2317 {
2318 int i;
2319 int snapshot, linux_boot;
2320 const char *icount_option = NULL;
2321 const char *initrd_filename;
2322 const char *kernel_filename, *kernel_cmdline;
2323 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2324 DisplayState *ds;
2325 DisplayChangeListener *dcl;
2326 int cyls, heads, secs, translation;
2327 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2328 QemuOptsList *olist;
2329 int optind;
2330 const char *optarg;
2331 const char *loadvm = NULL;
2332 QEMUMachine *machine;
2333 const char *cpu_model;
2334 const char *vga_model = "none";
2335 const char *pid_file = NULL;
2336 const char *incoming = NULL;
2337 #ifdef CONFIG_VNC
2338 int show_vnc_port = 0;
2339 #endif
2340 bool defconfig = true;
2341 bool userconfig = true;
2342 const char *log_mask = NULL;
2343 const char *log_file = NULL;
2344 GMemVTable mem_trace = {
2345 .malloc = malloc_and_trace,
2346 .realloc = realloc_and_trace,
2347 .free = free_and_trace,
2348 };
2349 const char *trace_events = NULL;
2350 const char *trace_file = NULL;
2351
2352 #ifdef CONFIG_SECCOMP
2353 if (seccomp_start() < 0) {
2354 fprintf(stderr,
2355 "seccomp: failed to install syscall filter in the kernel\n");
2356 exit(1);
2357 }
2358 #endif
2359
2360 atexit(qemu_run_exit_notifiers);
2361 error_set_progname(argv[0]);
2362
2363 g_mem_set_vtable(&mem_trace);
2364 if (!g_thread_supported()) {
2365 #if !GLIB_CHECK_VERSION(2, 31, 0)
2366 g_thread_init(NULL);
2367 #else
2368 fprintf(stderr, "glib threading failed to initialize.\n");
2369 exit(1);
2370 #endif
2371 }
2372
2373 module_call_init(MODULE_INIT_QOM);
2374
2375 runstate_init();
2376
2377 init_clocks();
2378 rtc_clock = host_clock;
2379
2380 qemu_cache_utils_init(envp);
2381
2382 QLIST_INIT (&vm_change_state_head);
2383 os_setup_early_signal_handling();
2384
2385 module_call_init(MODULE_INIT_MACHINE);
2386 machine = find_default_machine();
2387 cpu_model = NULL;
2388 ram_size = 0;
2389 snapshot = 0;
2390 cyls = heads = secs = 0;
2391 translation = BIOS_ATA_TRANSLATION_AUTO;
2392
2393 for (i = 0; i < MAX_NODES; i++) {
2394 node_mem[i] = 0;
2395 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2396 }
2397
2398 nb_numa_nodes = 0;
2399 nb_nics = 0;
2400
2401 autostart= 1;
2402
2403 /* first pass of option parsing */
2404 optind = 1;
2405 while (optind < argc) {
2406 if (argv[optind][0] != '-') {
2407 /* disk image */
2408 optind++;
2409 continue;
2410 } else {
2411 const QEMUOption *popt;
2412
2413 popt = lookup_opt(argc, argv, &optarg, &optind);
2414 switch (popt->index) {
2415 case QEMU_OPTION_nodefconfig:
2416 defconfig = false;
2417 break;
2418 case QEMU_OPTION_nouserconfig:
2419 userconfig = false;
2420 break;
2421 }
2422 }
2423 }
2424
2425 if (defconfig) {
2426 int ret;
2427 ret = qemu_read_default_config_files(userconfig);
2428 if (ret < 0) {
2429 exit(1);
2430 }
2431 }
2432
2433 /* second pass of option parsing */
2434 optind = 1;
2435 for(;;) {
2436 if (optind >= argc)
2437 break;
2438 if (argv[optind][0] != '-') {
2439 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2440 } else {
2441 const QEMUOption *popt;
2442
2443 popt = lookup_opt(argc, argv, &optarg, &optind);
2444 if (!(popt->arch_mask & arch_type)) {
2445 printf("Option %s not supported for this target\n", popt->name);
2446 exit(1);
2447 }
2448 switch(popt->index) {
2449 case QEMU_OPTION_M:
2450 machine = machine_parse(optarg);
2451 break;
2452 case QEMU_OPTION_cpu:
2453 /* hw initialization will check this */
2454 cpu_model = optarg;
2455 break;
2456 case QEMU_OPTION_hda:
2457 {
2458 char buf[256];
2459 if (cyls == 0)
2460 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2461 else
2462 snprintf(buf, sizeof(buf),
2463 "%s,cyls=%d,heads=%d,secs=%d%s",
2464 HD_OPTS , cyls, heads, secs,
2465 translation == BIOS_ATA_TRANSLATION_LBA ?
2466 ",trans=lba" :
2467 translation == BIOS_ATA_TRANSLATION_NONE ?
2468 ",trans=none" : "");
2469 drive_add(IF_DEFAULT, 0, optarg, buf);
2470 break;
2471 }
2472 case QEMU_OPTION_hdb:
2473 case QEMU_OPTION_hdc:
2474 case QEMU_OPTION_hdd:
2475 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2476 HD_OPTS);
2477 break;
2478 case QEMU_OPTION_drive:
2479 if (drive_def(optarg) == NULL) {
2480 exit(1);
2481 }
2482 break;
2483 case QEMU_OPTION_set:
2484 if (qemu_set_option(optarg) != 0)
2485 exit(1);
2486 break;
2487 case QEMU_OPTION_global:
2488 if (qemu_global_option(optarg) != 0)
2489 exit(1);
2490 break;
2491 case QEMU_OPTION_mtdblock:
2492 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2493 break;
2494 case QEMU_OPTION_sd:
2495 drive_add(IF_SD, 0, optarg, SD_OPTS);
2496 break;
2497 case QEMU_OPTION_pflash:
2498 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2499 break;
2500 case QEMU_OPTION_snapshot:
2501 snapshot = 1;
2502 break;
2503 case QEMU_OPTION_hdachs:
2504 {
2505 const char *p;
2506 p = optarg;
2507 cyls = strtol(p, (char **)&p, 0);
2508 if (cyls < 1 || cyls > 16383)
2509 goto chs_fail;
2510 if (*p != ',')
2511 goto chs_fail;
2512 p++;
2513 heads = strtol(p, (char **)&p, 0);
2514 if (heads < 1 || heads > 16)
2515 goto chs_fail;
2516 if (*p != ',')
2517 goto chs_fail;
2518 p++;
2519 secs = strtol(p, (char **)&p, 0);
2520 if (secs < 1 || secs > 63)
2521 goto chs_fail;
2522 if (*p == ',') {
2523 p++;
2524 if (!strcmp(p, "none"))
2525 translation = BIOS_ATA_TRANSLATION_NONE;
2526 else if (!strcmp(p, "lba"))
2527 translation = BIOS_ATA_TRANSLATION_LBA;
2528 else if (!strcmp(p, "auto"))
2529 translation = BIOS_ATA_TRANSLATION_AUTO;
2530 else
2531 goto chs_fail;
2532 } else if (*p != '\0') {
2533 chs_fail:
2534 fprintf(stderr, "qemu: invalid physical CHS format\n");
2535 exit(1);
2536 }
2537 if (hda_opts != NULL) {
2538 char num[16];
2539 snprintf(num, sizeof(num), "%d", cyls);
2540 qemu_opt_set(hda_opts, "cyls", num);
2541 snprintf(num, sizeof(num), "%d", heads);
2542 qemu_opt_set(hda_opts, "heads", num);
2543 snprintf(num, sizeof(num), "%d", secs);
2544 qemu_opt_set(hda_opts, "secs", num);
2545 if (translation == BIOS_ATA_TRANSLATION_LBA)
2546 qemu_opt_set(hda_opts, "trans", "lba");
2547 if (translation == BIOS_ATA_TRANSLATION_NONE)
2548 qemu_opt_set(hda_opts, "trans", "none");
2549 }
2550 }
2551 break;
2552 case QEMU_OPTION_numa:
2553 if (nb_numa_nodes >= MAX_NODES) {
2554 fprintf(stderr, "qemu: too many NUMA nodes\n");
2555 exit(1);
2556 }
2557 numa_add(optarg);
2558 break;
2559 case QEMU_OPTION_display:
2560 display_type = select_display(optarg);
2561 break;
2562 case QEMU_OPTION_nographic:
2563 display_type = DT_NOGRAPHIC;
2564 break;
2565 case QEMU_OPTION_curses:
2566 #ifdef CONFIG_CURSES
2567 display_type = DT_CURSES;
2568 #else
2569 fprintf(stderr, "Curses support is disabled\n");
2570 exit(1);
2571 #endif
2572 break;
2573 case QEMU_OPTION_portrait:
2574 graphic_rotate = 90;
2575 break;
2576 case QEMU_OPTION_rotate:
2577 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2578 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2579 graphic_rotate != 180 && graphic_rotate != 270) {
2580 fprintf(stderr,
2581 "qemu: only 90, 180, 270 deg rotation is available\n");
2582 exit(1);
2583 }
2584 break;
2585 case QEMU_OPTION_kernel:
2586 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2587 break;
2588 case QEMU_OPTION_initrd:
2589 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2590 break;
2591 case QEMU_OPTION_append:
2592 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2593 break;
2594 case QEMU_OPTION_dtb:
2595 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2596 break;
2597 case QEMU_OPTION_cdrom:
2598 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2599 break;
2600 case QEMU_OPTION_boot:
2601 {
2602 static const char * const params[] = {
2603 "order", "once", "menu",
2604 "splash", "splash-time", NULL
2605 };
2606 char buf[sizeof(boot_devices)];
2607 char *standard_boot_devices;
2608 int legacy = 0;
2609
2610 if (!strchr(optarg, '=')) {
2611 legacy = 1;
2612 pstrcpy(buf, sizeof(buf), optarg);
2613 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2614 fprintf(stderr,
2615 "qemu: unknown boot parameter '%s' in '%s'\n",
2616 buf, optarg);
2617 exit(1);
2618 }
2619
2620 if (legacy ||
2621 get_param_value(buf, sizeof(buf), "order", optarg)) {
2622 validate_bootdevices(buf);
2623 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2624 }
2625 if (!legacy) {
2626 if (get_param_value(buf, sizeof(buf),
2627 "once", optarg)) {
2628 validate_bootdevices(buf);
2629 standard_boot_devices = g_strdup(boot_devices);
2630 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2631 qemu_register_reset(restore_boot_devices,
2632 standard_boot_devices);
2633 }
2634 if (get_param_value(buf, sizeof(buf),
2635 "menu", optarg)) {
2636 if (!strcmp(buf, "on")) {
2637 boot_menu = 1;
2638 } else if (!strcmp(buf, "off")) {
2639 boot_menu = 0;
2640 } else {
2641 fprintf(stderr,
2642 "qemu: invalid option value '%s'\n",
2643 buf);
2644 exit(1);
2645 }
2646 }
2647 qemu_opts_parse(qemu_find_opts("boot-opts"),
2648 optarg, 0);
2649 }
2650 }
2651 break;
2652 case QEMU_OPTION_fda:
2653 case QEMU_OPTION_fdb:
2654 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2655 optarg, FD_OPTS);
2656 break;
2657 case QEMU_OPTION_no_fd_bootchk:
2658 fd_bootchk = 0;
2659 break;
2660 case QEMU_OPTION_netdev:
2661 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2662 exit(1);
2663 }
2664 break;
2665 case QEMU_OPTION_net:
2666 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2667 exit(1);
2668 }
2669 break;
2670 #ifdef CONFIG_LIBISCSI
2671 case QEMU_OPTION_iscsi:
2672 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2673 if (!opts) {
2674 exit(1);
2675 }
2676 break;
2677 #endif
2678 #ifdef CONFIG_SLIRP
2679 case QEMU_OPTION_tftp:
2680 legacy_tftp_prefix = optarg;
2681 break;
2682 case QEMU_OPTION_bootp:
2683 legacy_bootp_filename = optarg;
2684 break;
2685 case QEMU_OPTION_redir:
2686 if (net_slirp_redir(optarg) < 0)
2687 exit(1);
2688 break;
2689 #endif
2690 case QEMU_OPTION_bt:
2691 add_device_config(DEV_BT, optarg);
2692 break;
2693 case QEMU_OPTION_audio_help:
2694 if (!(audio_available())) {
2695 printf("Option %s not supported for this target\n", popt->name);
2696 exit(1);
2697 }
2698 AUD_help ();
2699 exit (0);
2700 break;
2701 case QEMU_OPTION_soundhw:
2702 if (!(audio_available())) {
2703 printf("Option %s not supported for this target\n", popt->name);
2704 exit(1);
2705 }
2706 select_soundhw (optarg);
2707 break;
2708 case QEMU_OPTION_h:
2709 help(0);
2710 break;
2711 case QEMU_OPTION_version:
2712 version();
2713 exit(0);
2714 break;
2715 case QEMU_OPTION_m: {
2716 int64_t value;
2717 char *end;
2718
2719 value = strtosz(optarg, &end);
2720 if (value < 0 || *end) {
2721 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2722 exit(1);
2723 }
2724
2725 if (value != (uint64_t)(ram_addr_t)value) {
2726 fprintf(stderr, "qemu: ram size too large\n");
2727 exit(1);
2728 }
2729 ram_size = value;
2730 break;
2731 }
2732 case QEMU_OPTION_mempath:
2733 mem_path = optarg;
2734 break;
2735 #ifdef MAP_POPULATE
2736 case QEMU_OPTION_mem_prealloc:
2737 mem_prealloc = 1;
2738 break;
2739 #endif
2740 case QEMU_OPTION_d:
2741 log_mask = optarg;
2742 break;
2743 case QEMU_OPTION_D:
2744 log_file = optarg;
2745 break;
2746 case QEMU_OPTION_s:
2747 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2748 break;
2749 case QEMU_OPTION_gdb:
2750 add_device_config(DEV_GDB, optarg);
2751 break;
2752 case QEMU_OPTION_L:
2753 data_dir = optarg;
2754 break;
2755 case QEMU_OPTION_bios:
2756 bios_name = optarg;
2757 break;
2758 case QEMU_OPTION_singlestep:
2759 singlestep = 1;
2760 break;
2761 case QEMU_OPTION_S:
2762 autostart = 0;
2763 break;
2764 case QEMU_OPTION_k:
2765 keyboard_layout = optarg;
2766 break;
2767 case QEMU_OPTION_localtime:
2768 rtc_utc = 0;
2769 break;
2770 case QEMU_OPTION_vga:
2771 vga_model = optarg;
2772 default_vga = 0;
2773 break;
2774 case QEMU_OPTION_g:
2775 {
2776 const char *p;
2777 int w, h, depth;
2778 p = optarg;
2779 w = strtol(p, (char **)&p, 10);
2780 if (w <= 0) {
2781 graphic_error:
2782 fprintf(stderr, "qemu: invalid resolution or depth\n");
2783 exit(1);
2784 }
2785 if (*p != 'x')
2786 goto graphic_error;
2787 p++;
2788 h = strtol(p, (char **)&p, 10);
2789 if (h <= 0)
2790 goto graphic_error;
2791 if (*p == 'x') {
2792 p++;
2793 depth = strtol(p, (char **)&p, 10);
2794 if (depth != 8 && depth != 15 && depth != 16 &&
2795 depth != 24 && depth != 32)
2796 goto graphic_error;
2797 } else if (*p == '\0') {
2798 depth = graphic_depth;
2799 } else {
2800 goto graphic_error;
2801 }
2802
2803 graphic_width = w;
2804 graphic_height = h;
2805 graphic_depth = depth;
2806 }
2807 break;
2808 case QEMU_OPTION_echr:
2809 {
2810 char *r;
2811 term_escape_char = strtol(optarg, &r, 0);
2812 if (r == optarg)
2813 printf("Bad argument to echr\n");
2814 break;
2815 }
2816 case QEMU_OPTION_monitor:
2817 monitor_parse(optarg, "readline");
2818 default_monitor = 0;
2819 break;
2820 case QEMU_OPTION_qmp:
2821 monitor_parse(optarg, "control");
2822 default_monitor = 0;
2823 break;
2824 case QEMU_OPTION_mon:
2825 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2826 if (!opts) {
2827 exit(1);
2828 }
2829 default_monitor = 0;
2830 break;
2831 case QEMU_OPTION_chardev:
2832 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2833 if (!opts) {
2834 exit(1);
2835 }
2836 break;
2837 case QEMU_OPTION_fsdev:
2838 olist = qemu_find_opts("fsdev");
2839 if (!olist) {
2840 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2841 exit(1);
2842 }
2843 opts = qemu_opts_parse(olist, optarg, 1);
2844 if (!opts) {
2845 fprintf(stderr, "parse error: %s\n", optarg);
2846 exit(1);
2847 }
2848 break;
2849 case QEMU_OPTION_virtfs: {
2850 QemuOpts *fsdev;
2851 QemuOpts *device;
2852 const char *writeout, *sock_fd, *socket;
2853
2854 olist = qemu_find_opts("virtfs");
2855 if (!olist) {
2856 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2857 exit(1);
2858 }
2859 opts = qemu_opts_parse(olist, optarg, 1);
2860 if (!opts) {
2861 fprintf(stderr, "parse error: %s\n", optarg);
2862 exit(1);
2863 }
2864
2865 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2866 qemu_opt_get(opts, "mount_tag") == NULL) {
2867 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2868 exit(1);
2869 }
2870 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2871 qemu_opt_get(opts, "mount_tag"),
2872 1, NULL);
2873 if (!fsdev) {
2874 fprintf(stderr, "duplicate fsdev id: %s\n",
2875 qemu_opt_get(opts, "mount_tag"));
2876 exit(1);
2877 }
2878
2879 writeout = qemu_opt_get(opts, "writeout");
2880 if (writeout) {
2881 #ifdef CONFIG_SYNC_FILE_RANGE
2882 qemu_opt_set(fsdev, "writeout", writeout);
2883 #else
2884 fprintf(stderr, "writeout=immediate not supported on "
2885 "this platform\n");
2886 exit(1);
2887 #endif
2888 }
2889 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2890 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2891 qemu_opt_set(fsdev, "security_model",
2892 qemu_opt_get(opts, "security_model"));
2893 socket = qemu_opt_get(opts, "socket");
2894 if (socket) {
2895 qemu_opt_set(fsdev, "socket", socket);
2896 }
2897 sock_fd = qemu_opt_get(opts, "sock_fd");
2898 if (sock_fd) {
2899 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2900 }
2901
2902 qemu_opt_set_bool(fsdev, "readonly",
2903 qemu_opt_get_bool(opts, "readonly", 0));
2904 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2905 NULL);
2906 qemu_opt_set(device, "driver", "virtio-9p-pci");
2907 qemu_opt_set(device, "fsdev",
2908 qemu_opt_get(opts, "mount_tag"));
2909 qemu_opt_set(device, "mount_tag",
2910 qemu_opt_get(opts, "mount_tag"));
2911 break;
2912 }
2913 case QEMU_OPTION_virtfs_synth: {
2914 QemuOpts *fsdev;
2915 QemuOpts *device;
2916
2917 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2918 1, NULL);
2919 if (!fsdev) {
2920 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2921 exit(1);
2922 }
2923 qemu_opt_set(fsdev, "fsdriver", "synth");
2924
2925 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2926 NULL);
2927 qemu_opt_set(device, "driver", "virtio-9p-pci");
2928 qemu_opt_set(device, "fsdev", "v_synth");
2929 qemu_opt_set(device, "mount_tag", "v_synth");
2930 break;
2931 }
2932 case QEMU_OPTION_serial:
2933 add_device_config(DEV_SERIAL, optarg);
2934 default_serial = 0;
2935 if (strncmp(optarg, "mon:", 4) == 0) {
2936 default_monitor = 0;
2937 }
2938 break;
2939 case QEMU_OPTION_watchdog:
2940 if (watchdog) {
2941 fprintf(stderr,
2942 "qemu: only one watchdog option may be given\n");
2943 return 1;
2944 }
2945 watchdog = optarg;
2946 break;
2947 case QEMU_OPTION_watchdog_action:
2948 if (select_watchdog_action(optarg) == -1) {
2949 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2950 exit(1);
2951 }
2952 break;
2953 case QEMU_OPTION_virtiocon:
2954 add_device_config(DEV_VIRTCON, optarg);
2955 default_virtcon = 0;
2956 if (strncmp(optarg, "mon:", 4) == 0) {
2957 default_monitor = 0;
2958 }
2959 break;
2960 case QEMU_OPTION_parallel:
2961 add_device_config(DEV_PARALLEL, optarg);
2962 default_parallel = 0;
2963 if (strncmp(optarg, "mon:", 4) == 0) {
2964 default_monitor = 0;
2965 }
2966 break;
2967 case QEMU_OPTION_debugcon:
2968 add_device_config(DEV_DEBUGCON, optarg);
2969 break;
2970 case QEMU_OPTION_loadvm:
2971 loadvm = optarg;
2972 break;
2973 case QEMU_OPTION_full_screen:
2974 full_screen = 1;
2975 break;
2976 #ifdef CONFIG_SDL
2977 case QEMU_OPTION_no_frame:
2978 no_frame = 1;
2979 break;
2980 case QEMU_OPTION_alt_grab:
2981 alt_grab = 1;
2982 break;
2983 case QEMU_OPTION_ctrl_grab:
2984 ctrl_grab = 1;
2985 break;
2986 case QEMU_OPTION_no_quit:
2987 no_quit = 1;
2988 break;
2989 case QEMU_OPTION_sdl:
2990 display_type = DT_SDL;
2991 break;
2992 #else
2993 case QEMU_OPTION_no_frame:
2994 case QEMU_OPTION_alt_grab:
2995 case QEMU_OPTION_ctrl_grab:
2996 case QEMU_OPTION_no_quit:
2997 case QEMU_OPTION_sdl:
2998 fprintf(stderr, "SDL support is disabled\n");
2999 exit(1);
3000 #endif
3001 case QEMU_OPTION_pidfile:
3002 pid_file = optarg;
3003 break;
3004 case QEMU_OPTION_win2k_hack:
3005 win2k_install_hack = 1;
3006 break;
3007 case QEMU_OPTION_rtc_td_hack: {
3008 static GlobalProperty slew_lost_ticks[] = {
3009 {
3010 .driver = "mc146818rtc",
3011 .property = "lost_tick_policy",
3012 .value = "slew",
3013 },
3014 { /* end of list */ }
3015 };
3016
3017 qdev_prop_register_global_list(slew_lost_ticks);
3018 break;
3019 }
3020 case QEMU_OPTION_acpitable:
3021 do_acpitable_option(optarg);
3022 break;
3023 case QEMU_OPTION_smbios:
3024 do_smbios_option(optarg);
3025 break;
3026 case QEMU_OPTION_enable_kvm:
3027 olist = qemu_find_opts("machine");
3028 qemu_opts_parse(olist, "accel=kvm", 0);
3029 break;
3030 case QEMU_OPTION_machine:
3031 olist = qemu_find_opts("machine");
3032 opts = qemu_opts_parse(olist, optarg, 1);
3033 if (!opts) {
3034 fprintf(stderr, "parse error: %s\n", optarg);
3035 exit(1);
3036 }
3037 optarg = qemu_opt_get(opts, "type");
3038 if (optarg) {
3039 machine = machine_parse(optarg);
3040 }
3041 break;
3042 case QEMU_OPTION_usb:
3043 usb_enabled = 1;
3044 break;
3045 case QEMU_OPTION_usbdevice:
3046 usb_enabled = 1;
3047 add_device_config(DEV_USB, optarg);
3048 break;
3049 case QEMU_OPTION_device:
3050 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3051 exit(1);
3052 }
3053 break;
3054 case QEMU_OPTION_smp:
3055 smp_parse(optarg);
3056 if (smp_cpus < 1) {
3057 fprintf(stderr, "Invalid number of CPUs\n");
3058 exit(1);
3059 }
3060 if (max_cpus < smp_cpus) {
3061 fprintf(stderr, "maxcpus must be equal to or greater than "
3062 "smp\n");
3063 exit(1);
3064 }
3065 if (max_cpus > 255) {
3066 fprintf(stderr, "Unsupported number of maxcpus\n");
3067 exit(1);
3068 }
3069 break;
3070 case QEMU_OPTION_vnc:
3071 #ifdef CONFIG_VNC
3072 display_remote++;
3073 vnc_display = optarg;
3074 #else
3075 fprintf(stderr, "VNC support is disabled\n");
3076 exit(1);
3077 #endif
3078 break;
3079 case QEMU_OPTION_no_acpi:
3080 acpi_enabled = 0;
3081 break;
3082 case QEMU_OPTION_no_hpet:
3083 no_hpet = 1;
3084 break;
3085 case QEMU_OPTION_balloon:
3086 if (balloon_parse(optarg) < 0) {
3087 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3088 exit(1);
3089 }
3090 break;
3091 case QEMU_OPTION_no_reboot:
3092 no_reboot = 1;
3093 break;
3094 case QEMU_OPTION_no_shutdown:
3095 no_shutdown = 1;
3096 break;
3097 case QEMU_OPTION_show_cursor:
3098 cursor_hide = 0;
3099 break;
3100 case QEMU_OPTION_uuid:
3101 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3102 fprintf(stderr, "Fail to parse UUID string."
3103 " Wrong format.\n");
3104 exit(1);
3105 }
3106 break;
3107 case QEMU_OPTION_option_rom:
3108 if (nb_option_roms >= MAX_OPTION_ROMS) {
3109 fprintf(stderr, "Too many option ROMs\n");
3110 exit(1);
3111 }
3112 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3113 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3114 option_rom[nb_option_roms].bootindex =
3115 qemu_opt_get_number(opts, "bootindex", -1);
3116 if (!option_rom[nb_option_roms].name) {
3117 fprintf(stderr, "Option ROM file is not specified\n");
3118 exit(1);
3119 }
3120 nb_option_roms++;
3121 break;
3122 case QEMU_OPTION_semihosting:
3123 semihosting_enabled = 1;
3124 break;
3125 case QEMU_OPTION_name:
3126 qemu_name = g_strdup(optarg);
3127 {
3128 char *p = strchr(qemu_name, ',');
3129 if (p != NULL) {
3130 *p++ = 0;
3131 if (strncmp(p, "process=", 8)) {
3132 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3133 exit(1);
3134 }
3135 p += 8;
3136 os_set_proc_name(p);
3137 }
3138 }
3139 break;
3140 case QEMU_OPTION_prom_env:
3141 if (nb_prom_envs >= MAX_PROM_ENVS) {
3142 fprintf(stderr, "Too many prom variables\n");
3143 exit(1);
3144 }
3145 prom_envs[nb_prom_envs] = optarg;
3146 nb_prom_envs++;
3147 break;
3148 case QEMU_OPTION_old_param:
3149 old_param = 1;
3150 break;
3151 case QEMU_OPTION_clock:
3152 configure_alarms(optarg);
3153 break;
3154 case QEMU_OPTION_startdate:
3155 configure_rtc_date_offset(optarg, 1);
3156 break;
3157 case QEMU_OPTION_rtc:
3158 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3159 if (!opts) {
3160 exit(1);
3161 }
3162 configure_rtc(opts);
3163 break;
3164 case QEMU_OPTION_tb_size:
3165 tcg_tb_size = strtol(optarg, NULL, 0);
3166 if (tcg_tb_size < 0) {
3167 tcg_tb_size = 0;
3168 }
3169 break;
3170 case QEMU_OPTION_icount:
3171 icount_option = optarg;
3172 break;
3173 case QEMU_OPTION_incoming:
3174 incoming = optarg;
3175 runstate_set(RUN_STATE_INMIGRATE);
3176 break;
3177 case QEMU_OPTION_nodefaults:
3178 default_serial = 0;
3179 default_parallel = 0;
3180 default_virtcon = 0;
3181 default_monitor = 0;
3182 default_net = 0;
3183 default_floppy = 0;
3184 default_cdrom = 0;
3185 default_sdcard = 0;
3186 default_vga = 0;
3187 break;
3188 case QEMU_OPTION_xen_domid:
3189 if (!(xen_available())) {
3190 printf("Option %s not supported for this target\n", popt->name);
3191 exit(1);
3192 }
3193 xen_domid = atoi(optarg);
3194 break;
3195 case QEMU_OPTION_xen_create:
3196 if (!(xen_available())) {
3197 printf("Option %s not supported for this target\n", popt->name);
3198 exit(1);
3199 }
3200 xen_mode = XEN_CREATE;
3201 break;
3202 case QEMU_OPTION_xen_attach:
3203 if (!(xen_available())) {
3204 printf("Option %s not supported for this target\n", popt->name);
3205 exit(1);
3206 }
3207 xen_mode = XEN_ATTACH;
3208 break;
3209 case QEMU_OPTION_trace:
3210 {
3211 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3212 if (!opts) {
3213 exit(1);
3214 }
3215 trace_events = qemu_opt_get(opts, "events");
3216 trace_file = qemu_opt_get(opts, "file");
3217 break;
3218 }
3219 case QEMU_OPTION_readconfig:
3220 {
3221 int ret = qemu_read_config_file(optarg);
3222 if (ret < 0) {
3223 fprintf(stderr, "read config %s: %s\n", optarg,
3224 strerror(-ret));
3225 exit(1);
3226 }
3227 break;
3228 }
3229 case QEMU_OPTION_spice:
3230 olist = qemu_find_opts("spice");
3231 if (!olist) {
3232 fprintf(stderr, "spice is not supported by this qemu build.\n");
3233 exit(1);
3234 }
3235 opts = qemu_opts_parse(olist, optarg, 0);
3236 if (!opts) {
3237 fprintf(stderr, "parse error: %s\n", optarg);
3238 exit(1);
3239 }
3240 break;
3241 case QEMU_OPTION_writeconfig:
3242 {
3243 FILE *fp;
3244 if (strcmp(optarg, "-") == 0) {
3245 fp = stdout;
3246 } else {
3247 fp = fopen(optarg, "w");
3248 if (fp == NULL) {
3249 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3250 exit(1);
3251 }
3252 }
3253 qemu_config_write(fp);
3254 fclose(fp);
3255 break;
3256 }
3257 case QEMU_OPTION_qtest:
3258 qtest_chrdev = optarg;
3259 break;
3260 case QEMU_OPTION_qtest_log:
3261 qtest_log = optarg;
3262 break;
3263 default:
3264 os_parse_cmd_args(popt->index, optarg);
3265 }
3266 }
3267 }
3268 loc_set_none();
3269
3270 if (machine == NULL) {
3271 fprintf(stderr, "No machine found.\n");
3272 exit(1);
3273 }
3274
3275 if (machine->hw_version) {
3276 qemu_set_version(machine->hw_version);
3277 }
3278
3279 /* Init CPU def lists, based on config
3280 * - Must be called after all the qemu_read_config_file() calls
3281 * - Must be called before list_cpus()
3282 * - Must be called before machine->init()
3283 */
3284 cpudef_init();
3285
3286 if (cpu_model && is_help_option(cpu_model)) {
3287 list_cpus(stdout, &fprintf, cpu_model);
3288 exit(0);
3289 }
3290
3291 /* Open the logfile at this point, if necessary. We can't open the logfile
3292 * when encountering either of the logging options (-d or -D) because the
3293 * other one may be encountered later on the command line, changing the
3294 * location or level of logging.
3295 */
3296 if (log_mask) {
3297 if (log_file) {
3298 set_cpu_log_filename(log_file);
3299 }
3300 set_cpu_log(log_mask);
3301 }
3302
3303 if (!trace_backend_init(trace_events, trace_file)) {
3304 exit(1);
3305 }
3306
3307 /* If no data_dir is specified then try to find it relative to the
3308 executable path. */
3309 if (!data_dir) {
3310 data_dir = os_find_datadir(argv[0]);
3311 }
3312 /* If all else fails use the install path specified when building. */
3313 if (!data_dir) {
3314 data_dir = CONFIG_QEMU_DATADIR;
3315 }
3316
3317 /*
3318 * Default to max_cpus = smp_cpus, in case the user doesn't
3319 * specify a max_cpus value.
3320 */
3321 if (!max_cpus)
3322 max_cpus = smp_cpus;
3323
3324 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3325 if (smp_cpus > machine->max_cpus) {
3326 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3327 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3328 machine->max_cpus);
3329 exit(1);
3330 }
3331
3332 /*
3333 * Get the default machine options from the machine if it is not already
3334 * specified either by the configuration file or by the command line.
3335 */
3336 if (machine->default_machine_opts) {
3337 qemu_opts_set_defaults(qemu_find_opts("machine"),
3338 machine->default_machine_opts, 0);
3339 }
3340
3341 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3342 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3343
3344 if (machine->no_serial) {
3345 default_serial = 0;
3346 }
3347 if (machine->no_parallel) {
3348 default_parallel = 0;
3349 }
3350 if (!machine->use_virtcon) {
3351 default_virtcon = 0;
3352 }
3353 if (machine->no_floppy) {
3354 default_floppy = 0;
3355 }
3356 if (machine->no_cdrom) {
3357 default_cdrom = 0;
3358 }
3359 if (machine->no_sdcard) {
3360 default_sdcard = 0;
3361 }
3362
3363 if (display_type == DT_NOGRAPHIC) {
3364 if (default_parallel)
3365 add_device_config(DEV_PARALLEL, "null");
3366 if (default_serial && default_monitor) {
3367 add_device_config(DEV_SERIAL, "mon:stdio");
3368 } else if (default_virtcon && default_monitor) {
3369 add_device_config(DEV_VIRTCON, "mon:stdio");
3370 } else {
3371 if (default_serial)
3372 add_device_config(DEV_SERIAL, "stdio");
3373 if (default_virtcon)
3374 add_device_config(DEV_VIRTCON, "stdio");
3375 if (default_monitor)
3376 monitor_parse("stdio", "readline");
3377 }
3378 } else {
3379 if (default_serial)
3380 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3381 if (default_parallel)
3382 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3383 if (default_monitor)
3384 monitor_parse("vc:80Cx24C", "readline");
3385 if (default_virtcon)
3386 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3387 }
3388
3389 socket_init();
3390
3391 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3392 exit(1);
3393 #ifdef CONFIG_VIRTFS
3394 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3395 exit(1);
3396 }
3397 #endif
3398
3399 os_daemonize();
3400
3401 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3402 os_pidfile_error();
3403 exit(1);
3404 }
3405
3406 /* init the memory */
3407 if (ram_size == 0) {
3408 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3409 }
3410
3411 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3412 != 0) {
3413 exit(0);
3414 }
3415
3416 configure_accelerator();
3417
3418 qemu_init_cpu_loop();
3419 if (qemu_init_main_loop()) {
3420 fprintf(stderr, "qemu_init_main_loop failed\n");
3421 exit(1);
3422 }
3423
3424 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3425 if (machine_opts) {
3426 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3427 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3428 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3429 } else {
3430 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3431 }
3432
3433 if (!kernel_cmdline) {
3434 kernel_cmdline = "";
3435 }
3436
3437 linux_boot = (kernel_filename != NULL);
3438
3439 if (!linux_boot && *kernel_cmdline != '\0') {
3440 fprintf(stderr, "-append only allowed with -kernel option\n");
3441 exit(1);
3442 }
3443
3444 if (!linux_boot && initrd_filename != NULL) {
3445 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3446 exit(1);
3447 }
3448
3449 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3450 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3451 exit(1);
3452 }
3453
3454 os_set_line_buffering();
3455
3456 if (init_timer_alarm() < 0) {
3457 fprintf(stderr, "could not initialize alarm timer\n");
3458 exit(1);
3459 }
3460
3461 #ifdef CONFIG_SPICE
3462 /* spice needs the timers to be initialized by this point */
3463 qemu_spice_init();
3464 #endif
3465
3466 if (icount_option && (kvm_enabled() || xen_enabled())) {
3467 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3468 exit(1);
3469 }
3470 configure_icount(icount_option);
3471
3472 if (net_init_clients() < 0) {
3473 exit(1);
3474 }
3475
3476 /* init the bluetooth world */
3477 if (foreach_device_config(DEV_BT, bt_parse))
3478 exit(1);
3479
3480 if (!xen_enabled()) {
3481 /* On 32-bit hosts, QEMU is limited by virtual address space */
3482 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3483 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3484 exit(1);
3485 }
3486 }
3487
3488 cpu_exec_init_all();
3489
3490 bdrv_init_with_whitelist();
3491
3492 blk_mig_init();
3493
3494 /* open the virtual block devices */
3495 if (snapshot)
3496 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3497 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3498 exit(1);
3499
3500 default_drive(default_cdrom, snapshot, machine->use_scsi,
3501 IF_DEFAULT, 2, CDROM_OPTS);
3502 default_drive(default_floppy, snapshot, machine->use_scsi,
3503 IF_FLOPPY, 0, FD_OPTS);
3504 default_drive(default_sdcard, snapshot, machine->use_scsi,
3505 IF_SD, 0, SD_OPTS);
3506
3507 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3508
3509 if (nb_numa_nodes > 0) {
3510 int i;
3511
3512 if (nb_numa_nodes > MAX_NODES) {
3513 nb_numa_nodes = MAX_NODES;
3514 }
3515
3516 /* If no memory size if given for any node, assume the default case
3517 * and distribute the available memory equally across all nodes
3518 */
3519 for (i = 0; i < nb_numa_nodes; i++) {
3520 if (node_mem[i] != 0)
3521 break;
3522 }
3523 if (i == nb_numa_nodes) {
3524 uint64_t usedmem = 0;
3525
3526 /* On Linux, the each node's border has to be 8MB aligned,
3527 * the final node gets the rest.
3528 */
3529 for (i = 0; i < nb_numa_nodes - 1; i++) {
3530 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3531 usedmem += node_mem[i];
3532 }
3533 node_mem[i] = ram_size - usedmem;
3534 }
3535
3536 for (i = 0; i < nb_numa_nodes; i++) {
3537 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3538 break;
3539 }
3540 }
3541 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3542 * must cope with this anyway, because there are BIOSes out there in
3543 * real machines which also use this scheme.
3544 */
3545 if (i == nb_numa_nodes) {
3546 for (i = 0; i < max_cpus; i++) {
3547 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3548 }
3549 }
3550 }
3551
3552 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3553 exit(1);
3554 }
3555
3556 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3557 exit(1);
3558 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3559 exit(1);
3560 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3561 exit(1);
3562 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3563 exit(1);
3564
3565 /* If no default VGA is requested, the default is "none". */
3566 if (default_vga && cirrus_vga_available()) {
3567 vga_model = "cirrus";
3568 }
3569 select_vgahw(vga_model);
3570
3571 if (watchdog) {
3572 i = select_watchdog(watchdog);
3573 if (i > 0)
3574 exit (i == 1 ? 1 : 0);
3575 }
3576
3577 if (machine->compat_props) {
3578 qdev_prop_register_global_list(machine->compat_props);
3579 }
3580 qemu_add_globals();
3581
3582 qdev_machine_init();
3583
3584 machine->init(ram_size, boot_devices,
3585 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3586
3587 cpu_synchronize_all_post_init();
3588
3589 set_numa_modes();
3590
3591 current_machine = machine;
3592
3593 /* init USB devices */
3594 if (usb_enabled) {
3595 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3596 exit(1);
3597 }
3598
3599 /* init generic devices */
3600 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3601 exit(1);
3602
3603 net_check_clients();
3604
3605 /* just use the first displaystate for the moment */
3606 ds = get_displaystate();
3607
3608 if (using_spice)
3609 display_remote++;
3610 if (display_type == DT_DEFAULT && !display_remote) {
3611 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3612 display_type = DT_SDL;
3613 #elif defined(CONFIG_VNC)
3614 vnc_display = "localhost:0,to=99";
3615 show_vnc_port = 1;
3616 #else
3617 display_type = DT_NONE;
3618 #endif
3619 }
3620
3621
3622 /* init local displays */
3623 switch (display_type) {
3624 case DT_NOGRAPHIC:
3625 break;
3626 #if defined(CONFIG_CURSES)
3627 case DT_CURSES:
3628 curses_display_init(ds, full_screen);
3629 break;
3630 #endif
3631 #if defined(CONFIG_SDL)
3632 case DT_SDL:
3633 sdl_display_init(ds, full_screen, no_frame);
3634 break;
3635 #elif defined(CONFIG_COCOA)
3636 case DT_SDL:
3637 cocoa_display_init(ds, full_screen);
3638 break;
3639 #endif
3640 default:
3641 break;
3642 }
3643
3644 /* must be after terminal init, SDL library changes signal handlers */
3645 os_setup_signal_handling();
3646
3647 #ifdef CONFIG_VNC
3648 /* init remote displays */
3649 if (vnc_display) {
3650 vnc_display_init(ds);
3651 if (vnc_display_open(ds, vnc_display) < 0) {
3652 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3653 vnc_display);
3654 exit(1);
3655 }
3656
3657 if (show_vnc_port) {
3658 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3659 }
3660 }
3661 #endif
3662 #ifdef CONFIG_SPICE
3663 if (using_spice && !qxl_enabled) {
3664 qemu_spice_display_init(ds);
3665 }
3666 #endif
3667
3668 /* display setup */
3669 dpy_resize(ds);
3670 dcl = ds->listeners;
3671 while (dcl != NULL) {
3672 if (dcl->dpy_refresh != NULL) {
3673 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3674 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3675 break;
3676 }
3677 dcl = dcl->next;
3678 }
3679 text_consoles_set_display(ds);
3680
3681 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3682 exit(1);
3683 }
3684
3685 qdev_machine_creation_done();
3686
3687 if (rom_load_all() != 0) {
3688 fprintf(stderr, "rom loading failed\n");
3689 exit(1);
3690 }
3691
3692 /* TODO: once all bus devices are qdevified, this should be done
3693 * when bus is created by qdev.c */
3694 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3695 qemu_run_machine_init_done_notifiers();
3696
3697 qemu_system_reset(VMRESET_SILENT);
3698 if (loadvm) {
3699 if (load_vmstate(loadvm) < 0) {
3700 autostart = 0;
3701 }
3702 }
3703
3704 if (incoming) {
3705 Error *errp = NULL;
3706 int ret = qemu_start_incoming_migration(incoming, &errp);
3707 if (ret < 0) {
3708 if (error_is_set(&errp)) {
3709 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3710 error_free(errp);
3711 }
3712 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3713 incoming, ret);
3714 exit(ret);
3715 }
3716 } else if (autostart) {
3717 vm_start();
3718 }
3719
3720 os_setup_post();
3721
3722 resume_all_vcpus();
3723 main_loop();
3724 bdrv_close_all();
3725 pause_all_vcpus();
3726 net_cleanup();
3727 res_free();
3728
3729 return 0;
3730 }