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