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