]> git.proxmox.com Git - qemu.git/blame - vl.c
virtio: Move virtio-pci to hw library
[qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
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.
0824d6fc 23 */
0824d6fc 24#include <unistd.h>
0824d6fc
FB
25#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
0824d6fc 28#include <errno.h>
67b915a5 29#include <sys/time.h>
c88676f8 30#include <zlib.h>
67b915a5 31
71e72a19 32/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
33#include "config-host.h"
34
67b915a5 35#ifndef _WIN32
5cea8590 36#include <libgen.h>
67b915a5 37#include <sys/times.h>
f1510b2c 38#include <sys/wait.h>
67b915a5 39#include <termios.h>
67b915a5 40#include <sys/mman.h>
f1510b2c 41#include <sys/ioctl.h>
24646c7e 42#include <sys/resource.h>
f1510b2c 43#include <sys/socket.h>
c94c8d64 44#include <netinet/in.h>
24646c7e 45#include <net/if.h>
24646c7e 46#include <arpa/inet.h>
9d728e8c 47#include <dirent.h>
7c9d8e07 48#include <netdb.h>
cb4b976b 49#include <sys/select.h>
ab6540d5 50
71e72a19 51#ifdef CONFIG_BSD
7d3505c5 52#include <sys/stat.h>
a167ba50 53#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
7d3505c5 54#include <libutil.h>
92c0e657 55#include <sys/sysctl.h>
24646c7e
BS
56#else
57#include <util.h>
128ab2ff 58#endif
bbe813a2 59#else
223f0d72 60#ifdef __linux__
7d3505c5
FB
61#include <pty.h>
62#include <malloc.h>
bd494f4c 63
e57a8c0e 64#include <linux/ppdev.h>
5867c88a 65#include <linux/parport.h>
223f0d72
BS
66#endif
67#ifdef __sun__
d5d10bc3
TS
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
d5d10bc3 71#include <netinet/arp.h>
d5d10bc3
TS
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h> // must come after ip.h
75#include <netinet/udp.h>
76#include <netinet/tcp.h>
77#include <net/if.h>
78#include <syslog.h>
79#include <stropts.h>
67b915a5 80#endif
7d3505c5 81#endif
ec530c81 82#endif
67b915a5 83
9892fbfb
BS
84#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
8a16d273
TS
88#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
67b915a5 92#ifdef _WIN32
49dc768d 93#include <windows.h>
67b915a5
FB
94#endif
95
73332e5c 96#ifdef CONFIG_SDL
59a36a2f 97#if defined(__APPLE__) || defined(main)
6693665a 98#include <SDL.h>
880fec5d 99int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
59a36a2f 102 return qemu_main(argc, argv, NULL);
880fec5d 103}
104#undef main
105#define main qemu_main
96bcd4f8 106#endif
73332e5c 107#endif /* CONFIG_SDL */
0824d6fc 108
5b0753e0
FB
109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
511d2b14
BS
114#include "hw/hw.h"
115#include "hw/boards.h"
116#include "hw/usb.h"
117#include "hw/pcmcia.h"
118#include "hw/pc.h"
511d2b14
BS
119#include "hw/isa.h"
120#include "hw/baum.h"
121#include "hw/bt.h"
9dd986cc 122#include "hw/watchdog.h"
b6f6e3d3 123#include "hw/smbios.h"
e37630ca 124#include "hw/xen.h"
bd3c948d 125#include "hw/qdev.h"
45a50b16 126#include "hw/loader.h"
5ef4efa4 127#include "bt-host.h"
511d2b14 128#include "net.h"
68ac40d2 129#include "net/slirp.h"
511d2b14
BS
130#include "monitor.h"
131#include "console.h"
132#include "sysemu.h"
133#include "gdbstub.h"
134#include "qemu-timer.h"
135#include "qemu-char.h"
136#include "cache-utils.h"
137#include "block.h"
666daa68 138#include "blockdev.h"
c163b5ca 139#include "block-migration.h"
a718acec 140#include "dma.h"
511d2b14
BS
141#include "audio/audio.h"
142#include "migration.h"
143#include "kvm.h"
d3f24367 144#include "qemu-option.h"
7282a033 145#include "qemu-config.h"
e78c48ec 146#include "qemu-objects.h"
59a5264b 147#include "qemu-options.h"
758e8e38 148#ifdef CONFIG_VIRTFS
74db920c
GS
149#include "fsdev/qemu-fsdev.h"
150#endif
511d2b14 151
0824d6fc 152#include "disas.h"
fc01f7e7 153
511d2b14
BS
154#include "qemu_socket.h"
155
d918f23e 156#include "slirp/libslirp.h"
511d2b14 157
94b0b5ff 158#include "trace.h"
0b5538c3 159#include "simpletrace.h"
72cf2d4f 160#include "qemu-queue.h"
296af7c9 161#include "cpus.h"
ad96090a 162#include "arch_init.h"
72cf2d4f 163
29b0040b
GH
164#include "ui/qemu-spice.h"
165
9dc63a1e
BS
166//#define DEBUG_NET
167//#define DEBUG_SLIRP
330d0414 168
1bfe856e 169#define DEFAULT_RAM_SIZE 128
313aa567 170
98b19252
AS
171#define MAX_VIRTIO_CONSOLES 1
172
5cea8590 173static const char *data_dir;
1192dad8 174const char *bios_name = NULL;
cb5a7aa8 175enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 176DisplayType display_type = DT_DEFAULT;
6b62dc2d 177int display_remote = 0;
3d11d0eb 178const char* keyboard_layout = NULL;
c227f099 179ram_addr_t ram_size;
c902760f
MT
180const char *mem_path = NULL;
181#ifdef MAP_POPULATE
182int mem_prealloc = 0; /* force preallocation of physical target memory */
183#endif
c4b1fcc0 184int nb_nics;
7c9d8e07 185NICInfo nd_table[MAX_NICS];
8a7ddc38 186int vm_running;
d399f677 187int autostart;
8e84865e 188int incoming_expected; /* Started with -incoming and waiting for incoming */
f6503059
AZ
189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
6875204c 191QEMUClock *rtc_clock;
64465297 192int vga_interface_type = VGA_NONE;
dbed7e40 193static int full_screen = 0;
634a21f6 194#ifdef CONFIG_SDL
dbed7e40 195static int no_frame = 0;
634a21f6 196#endif
667accab 197int no_quit = 0;
8d11df9e 198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 201int win2k_install_hack = 0;
73822ec8 202int rtc_td_hack = 0;
bb36d470 203int usb_enabled = 0;
1b530a6d 204int singlestep = 0;
6a00d601 205int smp_cpus = 1;
6be68d7e 206int max_cpus = 0;
dc6b1c09
AP
207int smp_cores = 1;
208int smp_threads = 1;
821601ea 209#ifdef CONFIG_VNC
73fc9742 210const char *vnc_display;
821601ea 211#endif
6515b203 212int acpi_enabled = 1;
16b29ae1 213int no_hpet = 0;
52ca8d6a 214int fd_bootchk = 1;
d1beab82 215int no_reboot = 0;
b2f76161 216int no_shutdown = 0;
9467cd46 217int cursor_hide = 1;
a171fe39 218int graphic_rotate = 0;
6b35e7bf 219uint8_t irq0override = 1;
09aaa160 220const char *watchdog;
2e55e842 221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
9ae02555 222int nb_option_roms;
8e71621f 223int semihosting_enabled = 0;
2b8f2d41 224int old_param = 0;
c35734b2 225const char *qemu_name;
3780e197 226int alt_grab = 0;
0ca9f8a4 227int ctrl_grab = 0;
66508601
BS
228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
95387491 230int boot_menu;
0824d6fc 231
1ca4d09a
GN
232typedef struct FWBootEntry FWBootEntry;
233
234struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
239};
240
241QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242
268a362c
AL
243int nb_numa_nodes;
244uint64_t node_mem[MAX_NODES];
245uint64_t node_cpumask[MAX_NODES];
246
9043b62d 247static QEMUTimer *nographic_timer;
ee5605e5 248
8fcb1b90
BS
249uint8_t qemu_uuid[16];
250
76e30d0f
JK
251static QEMUBootSetHandler *boot_set_handler;
252static void *boot_set_opaque;
253
fd42deeb
GH
254static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256
4cab946a
GN
257static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
259
303d4e86 260static int tcg_allowed = 1;
d745bef8 261int kvm_allowed = 0;
3285cf4f 262int xen_allowed = 0;
d745bef8
BS
263uint32_t xen_domid;
264enum xen_mode xen_mode = XEN_EMULATE;
265
998bbd74 266static int default_serial = 1;
6a5e8b0e 267static int default_parallel = 1;
986c5f78 268static int default_virtcon = 1;
abdeed06 269static int default_monitor = 1;
64465297 270static int default_vga = 1;
ac33f8fa
GH
271static int default_floppy = 1;
272static int default_cdrom = 1;
273static int default_sdcard = 1;
998bbd74
GH
274
275static struct {
276 const char *driver;
277 int *flag;
278} default_list[] = {
6a5e8b0e
GH
279 { .driver = "isa-serial", .flag = &default_serial },
280 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf 281 { .driver = "isa-fdc", .flag = &default_floppy },
af6bf132
MA
282 { .driver = "ide-cd", .flag = &default_cdrom },
283 { .driver = "ide-hd", .flag = &default_cdrom },
d8bcbabf 284 { .driver = "ide-drive", .flag = &default_cdrom },
af6bf132 285 { .driver = "scsi-cd", .flag = &default_cdrom },
392ecf54
AS
286 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
287 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
288 { .driver = "virtio-serial", .flag = &default_virtcon },
64465297 289 { .driver = "VGA", .flag = &default_vga },
69fd02ee
GH
290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "vmware-svga", .flag = &default_vga },
0826c710 292 { .driver = "isa-vga", .flag = &default_vga },
998bbd74
GH
293};
294
295static int default_driver_check(QemuOpts *opts, void *opaque)
296{
297 const char *driver = qemu_opt_get(opts, "driver");
298 int i;
299
300 if (!driver)
301 return 0;
302 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
303 if (strcmp(default_list[i].driver, driver) != 0)
304 continue;
305 *(default_list[i].flag) = 0;
306 }
307 return 0;
308}
309
8f0056b7
PB
310/***********************************************************/
311/* real time host monotonic timer */
09b26c5e 312
f6503059
AZ
313/***********************************************************/
314/* host time/date access */
315void qemu_get_timedate(struct tm *tm, int offset)
316{
317 time_t ti;
318 struct tm *ret;
319
320 time(&ti);
321 ti += offset;
322 if (rtc_date_offset == -1) {
323 if (rtc_utc)
324 ret = gmtime(&ti);
325 else
326 ret = localtime(&ti);
327 } else {
328 ti -= rtc_date_offset;
329 ret = gmtime(&ti);
330 }
331
332 memcpy(tm, ret, sizeof(struct tm));
333}
334
335int qemu_timedate_diff(struct tm *tm)
336{
337 time_t seconds;
338
339 if (rtc_date_offset == -1)
340 if (rtc_utc)
341 seconds = mktimegm(tm);
342 else
343 seconds = mktime(tm);
344 else
345 seconds = mktimegm(tm) + rtc_date_offset;
346
347 return seconds - time(NULL);
348}
349
80cd3478
LC
350void rtc_change_mon_event(struct tm *tm)
351{
352 QObject *data;
353
354 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
355 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
356 qobject_decref(data);
357}
358
1ed2fc1f
JK
359static void configure_rtc_date_offset(const char *startdate, int legacy)
360{
361 time_t rtc_start_date;
362 struct tm tm;
363
364 if (!strcmp(startdate, "now") && legacy) {
365 rtc_date_offset = -1;
366 } else {
367 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday,
371 &tm.tm_hour,
372 &tm.tm_min,
373 &tm.tm_sec) == 6) {
374 /* OK */
375 } else if (sscanf(startdate, "%d-%d-%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday) == 3) {
379 tm.tm_hour = 0;
380 tm.tm_min = 0;
381 tm.tm_sec = 0;
382 } else {
383 goto date_fail;
384 }
385 tm.tm_year -= 1900;
386 tm.tm_mon--;
387 rtc_start_date = mktimegm(&tm);
388 if (rtc_start_date == -1) {
389 date_fail:
390 fprintf(stderr, "Invalid date format. Valid formats are:\n"
391 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 exit(1);
393 }
394 rtc_date_offset = time(NULL) - rtc_start_date;
395 }
396}
397
398static void configure_rtc(QemuOpts *opts)
399{
400 const char *value;
401
402 value = qemu_opt_get(opts, "base");
403 if (value) {
404 if (!strcmp(value, "utc")) {
405 rtc_utc = 1;
406 } else if (!strcmp(value, "localtime")) {
407 rtc_utc = 0;
408 } else {
409 configure_rtc_date_offset(value, 0);
410 }
411 }
6875204c
JK
412 value = qemu_opt_get(opts, "clock");
413 if (value) {
414 if (!strcmp(value, "host")) {
415 rtc_clock = host_clock;
416 } else if (!strcmp(value, "vm")) {
417 rtc_clock = vm_clock;
418 } else {
419 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
420 exit(1);
421 }
422 }
1ed2fc1f
JK
423 value = qemu_opt_get(opts, "driftfix");
424 if (value) {
7e4c0336 425 if (!strcmp(value, "slew")) {
1ed2fc1f 426 rtc_td_hack = 1;
7e4c0336 427 } else if (!strcmp(value, "none")) {
1ed2fc1f
JK
428 rtc_td_hack = 0;
429 } else {
430 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
431 exit(1);
432 }
433 }
1ed2fc1f
JK
434}
435
1ae26a18
AZ
436/***********************************************************/
437/* Bluetooth support */
438static int nb_hcis;
439static int cur_hci;
440static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 441
1ae26a18
AZ
442static struct bt_vlan_s {
443 struct bt_scatternet_s net;
444 int id;
445 struct bt_vlan_s *next;
446} *first_bt_vlan;
447
448/* find or alloc a new bluetooth "VLAN" */
674bb261 449static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
450{
451 struct bt_vlan_s **pvlan, *vlan;
452 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
453 if (vlan->id == id)
454 return &vlan->net;
455 }
456 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
457 vlan->id = id;
458 pvlan = &first_bt_vlan;
459 while (*pvlan != NULL)
460 pvlan = &(*pvlan)->next;
461 *pvlan = vlan;
462 return &vlan->net;
463}
464
465static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
466{
467}
468
469static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
470{
471 return -ENOTSUP;
472}
473
474static struct HCIInfo null_hci = {
475 .cmd_send = null_hci_send,
476 .sco_send = null_hci_send,
477 .acl_send = null_hci_send,
478 .bdaddr_set = null_hci_addr_set,
479};
480
481struct HCIInfo *qemu_next_hci(void)
482{
483 if (cur_hci == nb_hcis)
484 return &null_hci;
485
486 return hci_table[cur_hci++];
487}
488
dc72ac14
AZ
489static struct HCIInfo *hci_init(const char *str)
490{
491 char *endp;
492 struct bt_scatternet_s *vlan = 0;
493
494 if (!strcmp(str, "null"))
495 /* null */
496 return &null_hci;
497 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
498 /* host[:hciN] */
499 return bt_host_hci(str[4] ? str + 5 : "hci0");
500 else if (!strncmp(str, "hci", 3)) {
501 /* hci[,vlan=n] */
502 if (str[3]) {
503 if (!strncmp(str + 3, ",vlan=", 6)) {
504 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
505 if (*endp)
506 vlan = 0;
507 }
508 } else
509 vlan = qemu_find_bt_vlan(0);
510 if (vlan)
511 return bt_new_hci(vlan);
512 }
513
514 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
515
516 return 0;
517}
518
519static int bt_hci_parse(const char *str)
520{
521 struct HCIInfo *hci;
c227f099 522 bdaddr_t bdaddr;
dc72ac14
AZ
523
524 if (nb_hcis >= MAX_NICS) {
525 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
526 return -1;
527 }
528
529 hci = hci_init(str);
530 if (!hci)
531 return -1;
532
533 bdaddr.b[0] = 0x52;
534 bdaddr.b[1] = 0x54;
535 bdaddr.b[2] = 0x00;
536 bdaddr.b[3] = 0x12;
537 bdaddr.b[4] = 0x34;
538 bdaddr.b[5] = 0x56 + nb_hcis;
539 hci->bdaddr_set(hci, bdaddr.b);
540
541 hci_table[nb_hcis++] = hci;
542
543 return 0;
544}
545
546static void bt_vhci_add(int vlan_id)
547{
548 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
549
550 if (!vlan->slave)
551 fprintf(stderr, "qemu: warning: adding a VHCI to "
552 "an empty scatternet %i\n", vlan_id);
553
554 bt_vhci_init(bt_new_hci(vlan));
555}
556
557static struct bt_device_s *bt_device_add(const char *opt)
558{
559 struct bt_scatternet_s *vlan;
560 int vlan_id = 0;
561 char *endp = strstr(opt, ",vlan=");
562 int len = (endp ? endp - opt : strlen(opt)) + 1;
563 char devname[10];
564
565 pstrcpy(devname, MIN(sizeof(devname), len), opt);
566
567 if (endp) {
568 vlan_id = strtol(endp + 6, &endp, 0);
569 if (*endp) {
570 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
571 return 0;
572 }
573 }
574
575 vlan = qemu_find_bt_vlan(vlan_id);
576
577 if (!vlan->slave)
578 fprintf(stderr, "qemu: warning: adding a slave device to "
579 "an empty scatternet %i\n", vlan_id);
580
581 if (!strcmp(devname, "keyboard"))
582 return bt_keyboard_init(vlan);
583
584 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
585 return 0;
586}
587
588static int bt_parse(const char *opt)
589{
590 const char *endp, *p;
591 int vlan;
592
593 if (strstart(opt, "hci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp)
596 if (!strstart(endp, ",vlan=", 0))
597 opt = endp + 1;
598
599 return bt_hci_parse(opt);
600 }
601 } else if (strstart(opt, "vhci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp) {
604 if (strstart(endp, ",vlan=", &p)) {
605 vlan = strtol(p, (char **) &endp, 0);
606 if (*endp) {
607 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
608 return 1;
609 }
610 } else {
611 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
612 return 1;
613 }
614 } else
615 vlan = 0;
616
617 bt_vhci_add(vlan);
618 return 0;
619 }
620 } else if (strstart(opt, "device:", &endp))
621 return !bt_device_add(endp);
622
623 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
624 return 1;
625}
626
1ae26a18
AZ
627/***********************************************************/
628/* QEMU Block devices */
629
2292ddae
MA
630#define HD_OPTS "media=disk"
631#define CDROM_OPTS "media=cdrom"
632#define FD_OPTS ""
633#define PFLASH_OPTS ""
634#define MTD_OPTS ""
635#define SD_OPTS ""
e4bcb14c 636
9dfd7c7a
GH
637static int drive_init_func(QemuOpts *opts, void *opaque)
638{
a803cb8e 639 int *use_scsi = opaque;
9dfd7c7a 640
319ae529 641 return drive_init(opts, *use_scsi) == NULL;
9dfd7c7a
GH
642}
643
644static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
645{
646 if (NULL == qemu_opt_get(opts, "snapshot")) {
647 qemu_opt_set(opts, "snapshot", "on");
648 }
649 return 0;
650}
651
4e5d9b57
MA
652static void default_drive(int enable, int snapshot, int use_scsi,
653 BlockInterfaceType type, int index,
654 const char *optstr)
655{
656 QemuOpts *opts;
657
658 if (type == IF_DEFAULT) {
659 type = use_scsi ? IF_SCSI : IF_IDE;
660 }
661
662 if (!enable || drive_get_by_index(type, index)) {
663 return;
664 }
665
666 opts = drive_add(type, index, NULL, optstr);
667 if (snapshot) {
668 drive_enable_snapshot(opts, NULL);
669 }
319ae529 670 if (!drive_init(opts, use_scsi)) {
4e5d9b57
MA
671 exit(1);
672 }
673}
674
76e30d0f
JK
675void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
676{
677 boot_set_handler = func;
678 boot_set_opaque = opaque;
679}
680
681int qemu_boot_set(const char *boot_devices)
682{
683 if (!boot_set_handler) {
684 return -EINVAL;
685 }
686 return boot_set_handler(boot_set_opaque, boot_devices);
687}
688
4e9e9d6e 689static void validate_bootdevices(char *devices)
ef3adf68
JK
690{
691 /* We just do some generic consistency checks */
692 const char *p;
693 int bitmap = 0;
694
695 for (p = devices; *p != '\0'; p++) {
696 /* Allowed boot devices are:
697 * a-b: floppy disk drives
698 * c-f: IDE disk drives
699 * g-m: machine implementation dependant drives
700 * n-p: network devices
701 * It's up to each machine implementation to check if the given boot
702 * devices match the actual hardware implementation and firmware
703 * features.
704 */
705 if (*p < 'a' || *p > 'p') {
706 fprintf(stderr, "Invalid boot device '%c'\n", *p);
707 exit(1);
708 }
709 if (bitmap & (1 << (*p - 'a'))) {
710 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
711 exit(1);
712 }
713 bitmap |= 1 << (*p - 'a');
714 }
ef3adf68
JK
715}
716
e0f084bf
JK
717static void restore_boot_devices(void *opaque)
718{
719 char *standard_boot_devices = opaque;
37905d6a
AW
720 static int first = 1;
721
722 /* Restore boot order and remove ourselves after the first boot */
723 if (first) {
724 first = 0;
725 return;
726 }
e0f084bf
JK
727
728 qemu_boot_set(standard_boot_devices);
729
730 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
731 qemu_free(standard_boot_devices);
732}
733
1ca4d09a
GN
734void add_boot_device_path(int32_t bootindex, DeviceState *dev,
735 const char *suffix)
736{
737 FWBootEntry *node, *i;
738
739 if (bootindex < 0) {
740 return;
741 }
742
743 assert(dev != NULL || suffix != NULL);
744
745 node = qemu_mallocz(sizeof(FWBootEntry));
746 node->bootindex = bootindex;
4fef930a 747 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
1ca4d09a
GN
748 node->dev = dev;
749
750 QTAILQ_FOREACH(i, &fw_boot_order, link) {
751 if (i->bootindex == bootindex) {
752 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
753 exit(1);
754 } else if (i->bootindex < bootindex) {
755 continue;
756 }
757 QTAILQ_INSERT_BEFORE(i, node, link);
758 return;
759 }
760 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
761}
762
962630f2
GN
763/*
764 * This function returns null terminated string that consist of new line
71785aba 765 * separated device paths.
962630f2
GN
766 *
767 * memory pointed by "size" is assigned total length of the array in bytes
768 *
769 */
770char *get_boot_devices_list(uint32_t *size)
771{
772 FWBootEntry *i;
773 uint32_t total = 0;
774 char *list = NULL;
775
776 QTAILQ_FOREACH(i, &fw_boot_order, link) {
777 char *devpath = NULL, *bootpath;
778 int len;
779
780 if (i->dev) {
781 devpath = qdev_get_fw_dev_path(i->dev);
782 assert(devpath);
783 }
784
785 if (i->suffix && devpath) {
4fd37a98
BS
786 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
787
788 bootpath = qemu_malloc(bootpathlen);
789 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
962630f2
GN
790 qemu_free(devpath);
791 } else if (devpath) {
792 bootpath = devpath;
793 } else {
4fef930a 794 bootpath = qemu_strdup(i->suffix);
962630f2
GN
795 assert(bootpath);
796 }
797
798 if (total) {
799 list[total-1] = '\n';
800 }
801 len = strlen(bootpath) + 1;
802 list = qemu_realloc(list, total + len);
803 memcpy(&list[total], bootpath, len);
804 total += len;
805 qemu_free(bootpath);
806 }
807
808 *size = total;
809
810 return list;
811}
812
268a362c
AL
813static void numa_add(const char *optarg)
814{
815 char option[128];
816 char *endptr;
817 unsigned long long value, endvalue;
818 int nodenr;
819
820 optarg = get_opt_name(option, 128, optarg, ',') + 1;
821 if (!strcmp(option, "node")) {
822 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
823 nodenr = nb_numa_nodes;
824 } else {
825 nodenr = strtoull(option, NULL, 10);
826 }
827
828 if (get_param_value(option, 128, "mem", optarg) == 0) {
829 node_mem[nodenr] = 0;
830 } else {
70b4f4bb 831 int64_t sval;
9f9b17a4
JS
832 sval = strtosz(option, NULL);
833 if (sval < 0) {
834 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
835 exit(1);
268a362c 836 }
9f9b17a4 837 node_mem[nodenr] = sval;
268a362c
AL
838 }
839 if (get_param_value(option, 128, "cpus", optarg) == 0) {
840 node_cpumask[nodenr] = 0;
841 } else {
842 value = strtoull(option, &endptr, 10);
843 if (value >= 64) {
844 value = 63;
845 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
846 } else {
847 if (*endptr == '-') {
848 endvalue = strtoull(endptr+1, &endptr, 10);
849 if (endvalue >= 63) {
850 endvalue = 62;
851 fprintf(stderr,
852 "only 63 CPUs in NUMA mode supported.\n");
853 }
0dfbd514 854 value = (2ULL << endvalue) - (1ULL << value);
268a362c 855 } else {
0dfbd514 856 value = 1ULL << value;
268a362c
AL
857 }
858 }
859 node_cpumask[nodenr] = value;
860 }
861 nb_numa_nodes++;
862 }
863 return;
864}
865
dc6b1c09
AP
866static void smp_parse(const char *optarg)
867{
868 int smp, sockets = 0, threads = 0, cores = 0;
869 char *endptr;
870 char option[128];
871
872 smp = strtoul(optarg, &endptr, 10);
873 if (endptr != optarg) {
874 if (*endptr == ',') {
875 endptr++;
876 }
877 }
878 if (get_param_value(option, 128, "sockets", endptr) != 0)
879 sockets = strtoull(option, NULL, 10);
880 if (get_param_value(option, 128, "cores", endptr) != 0)
881 cores = strtoull(option, NULL, 10);
882 if (get_param_value(option, 128, "threads", endptr) != 0)
883 threads = strtoull(option, NULL, 10);
884 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
885 max_cpus = strtoull(option, NULL, 10);
886
887 /* compute missing values, prefer sockets over cores over threads */
888 if (smp == 0 || sockets == 0) {
889 sockets = sockets > 0 ? sockets : 1;
890 cores = cores > 0 ? cores : 1;
891 threads = threads > 0 ? threads : 1;
892 if (smp == 0) {
893 smp = cores * threads * sockets;
dc6b1c09
AP
894 }
895 } else {
896 if (cores == 0) {
897 threads = threads > 0 ? threads : 1;
898 cores = smp / (sockets * threads);
899 } else {
dca98169 900 threads = smp / (cores * sockets);
dc6b1c09
AP
901 }
902 }
903 smp_cpus = smp;
904 smp_cores = cores > 0 ? cores : 1;
905 smp_threads = threads > 0 ? threads : 1;
906 if (max_cpus == 0)
907 max_cpus = smp_cpus;
908}
909
a594cfbf
FB
910/***********************************************************/
911/* USB devices */
912
fb08000c 913static int usb_device_add(const char *devname)
a594cfbf
FB
914{
915 const char *p;
a5d2f727 916 USBDevice *dev = NULL;
a594cfbf 917
a5d2f727 918 if (!usb_enabled)
a594cfbf
FB
919 return -1;
920
0958b4cc
GH
921 /* drivers with .usbdevice_name entry in USBDeviceInfo */
922 dev = usbdevice_create(devname);
923 if (dev)
924 goto done;
925
a5d2f727 926 /* the other ones */
a594cfbf
FB
927 if (strstart(devname, "host:", &p)) {
928 dev = usb_host_device_open(p);
dc72ac14
AZ
929 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
930 dev = usb_bt_init(devname[2] ? hci_init(p) :
931 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
932 } else {
933 return -1;
934 }
0d92ed30
PB
935 if (!dev)
936 return -1;
937
a5d2f727 938done:
a594cfbf
FB
939 return 0;
940}
941
1f3870ab
AL
942static int usb_device_del(const char *devname)
943{
944 int bus_num, addr;
945 const char *p;
946
5d0c5750
AL
947 if (strstart(devname, "host:", &p))
948 return usb_host_device_close(p);
949
a5d2f727 950 if (!usb_enabled)
1f3870ab
AL
951 return -1;
952
953 p = strchr(devname, '.');
954 if (!p)
955 return -1;
956 bus_num = strtoul(devname, NULL, 0);
957 addr = strtoul(p + 1, NULL, 0);
958
a5d2f727 959 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
960}
961
bd3c948d
GH
962static int usb_parse(const char *cmdline)
963{
59d1c1c2 964 int r;
fb08000c 965 r = usb_device_add(cmdline);
59d1c1c2
ST
966 if (r < 0) {
967 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
968 }
969 return r;
bd3c948d
GH
970}
971
d54908a5 972void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 973{
59d1c1c2 974 const char *devname = qdict_get_str(qdict, "devname");
fb08000c 975 if (usb_device_add(devname) < 0) {
1ecda02b 976 error_report("could not add USB device '%s'", devname);
59d1c1c2 977 }
a594cfbf
FB
978}
979
d54908a5 980void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 981{
59d1c1c2
ST
982 const char *devname = qdict_get_str(qdict, "devname");
983 if (usb_device_del(devname) < 0) {
1ecda02b 984 error_report("could not delete USB device '%s'", devname);
59d1c1c2 985 }
a594cfbf
FB
986}
987
201a51fc
AZ
988/***********************************************************/
989/* PCMCIA/Cardbus */
990
991static struct pcmcia_socket_entry_s {
bc24a225 992 PCMCIASocket *socket;
201a51fc
AZ
993 struct pcmcia_socket_entry_s *next;
994} *pcmcia_sockets = 0;
995
bc24a225 996void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
997{
998 struct pcmcia_socket_entry_s *entry;
999
1000 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1001 entry->socket = socket;
1002 entry->next = pcmcia_sockets;
1003 pcmcia_sockets = entry;
1004}
1005
bc24a225 1006void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
1007{
1008 struct pcmcia_socket_entry_s *entry, **ptr;
1009
1010 ptr = &pcmcia_sockets;
1011 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1012 if (entry->socket == socket) {
1013 *ptr = entry->next;
1014 qemu_free(entry);
1015 }
1016}
1017
376253ec 1018void pcmcia_info(Monitor *mon)
201a51fc
AZ
1019{
1020 struct pcmcia_socket_entry_s *iter;
376253ec 1021
201a51fc 1022 if (!pcmcia_sockets)
376253ec 1023 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
1024
1025 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
1026 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1027 iter->socket->attached ? iter->socket->card_string :
1028 "Empty");
201a51fc
AZ
1029}
1030
cc1daa40
FB
1031/***********************************************************/
1032/* machine registration */
1033
bdaf78e0 1034static QEMUMachine *first_machine = NULL;
6f338c34 1035QEMUMachine *current_machine = NULL;
cc1daa40
FB
1036
1037int qemu_register_machine(QEMUMachine *m)
1038{
1039 QEMUMachine **pm;
1040 pm = &first_machine;
1041 while (*pm != NULL)
1042 pm = &(*pm)->next;
1043 m->next = NULL;
1044 *pm = m;
1045 return 0;
1046}
1047
9596ebb7 1048static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1049{
1050 QEMUMachine *m;
1051
1052 for(m = first_machine; m != NULL; m = m->next) {
1053 if (!strcmp(m->name, name))
1054 return m;
3f6599e6
MM
1055 if (m->alias && !strcmp(m->alias, name))
1056 return m;
cc1daa40
FB
1057 }
1058 return NULL;
1059}
1060
0c257437
AL
1061static QEMUMachine *find_default_machine(void)
1062{
1063 QEMUMachine *m;
1064
1065 for(m = first_machine; m != NULL; m = m->next) {
1066 if (m->is_default) {
1067 return m;
1068 }
1069 }
1070 return NULL;
1071}
1072
8a7ddc38
FB
1073/***********************************************************/
1074/* main execution loop */
1075
9596ebb7 1076static void gui_update(void *opaque)
8a7ddc38 1077{
7d957bd8 1078 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1079 DisplayState *ds = opaque;
7d957bd8
AL
1080 DisplayChangeListener *dcl = ds->listeners;
1081
62a2744c 1082 qemu_flush_coalesced_mmio_buffer();
7d957bd8
AL
1083 dpy_refresh(ds);
1084
1085 while (dcl != NULL) {
1086 if (dcl->gui_timer_interval &&
1087 dcl->gui_timer_interval < interval)
1088 interval = dcl->gui_timer_interval;
1089 dcl = dcl->next;
1090 }
7bd427d8 1091 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
8a7ddc38
FB
1092}
1093
9043b62d
BS
1094static void nographic_update(void *opaque)
1095{
1096 uint64_t interval = GUI_REFRESH_INTERVAL;
1097
62a2744c 1098 qemu_flush_coalesced_mmio_buffer();
7bd427d8 1099 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
9043b62d
BS
1100}
1101
0bd48850
FB
1102struct vm_change_state_entry {
1103 VMChangeStateHandler *cb;
1104 void *opaque;
72cf2d4f 1105 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1106};
1107
72cf2d4f 1108static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1109
1110VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1111 void *opaque)
1112{
1113 VMChangeStateEntry *e;
1114
1115 e = qemu_mallocz(sizeof (*e));
0bd48850
FB
1116
1117 e->cb = cb;
1118 e->opaque = opaque;
72cf2d4f 1119 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1120 return e;
1121}
1122
1123void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1124{
72cf2d4f 1125 QLIST_REMOVE (e, entries);
0bd48850
FB
1126 qemu_free (e);
1127}
1128
296af7c9 1129void vm_state_notify(int running, int reason)
0bd48850
FB
1130{
1131 VMChangeStateEntry *e;
1132
94b0b5ff
SH
1133 trace_vm_state_notify(running, reason);
1134
0bd48850 1135 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
9781e040 1136 e->cb(e->opaque, running, reason);
0bd48850
FB
1137 }
1138}
1139
8a7ddc38
FB
1140void vm_start(void)
1141{
1142 if (!vm_running) {
1143 cpu_enable_ticks();
1144 vm_running = 1;
9781e040 1145 vm_state_notify(1, 0);
d6dc3d42 1146 resume_all_vcpus();
6ed2c484 1147 monitor_protocol_event(QEVENT_RESUME, NULL);
8a7ddc38
FB
1148 }
1149}
1150
bb0c6722
FB
1151/* reset/shutdown handler */
1152
1153typedef struct QEMUResetEntry {
72cf2d4f 1154 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
1155 QEMUResetHandler *func;
1156 void *opaque;
bb0c6722
FB
1157} QEMUResetEntry;
1158
72cf2d4f
BS
1159static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1160 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722 1161static int reset_requested;
f64622c4
GN
1162static int shutdown_requested, shutdown_signal = -1;
1163static pid_t shutdown_pid;
3475187d 1164static int powerdown_requested;
8cf71710
JK
1165static int debug_requested;
1166static int vmstop_requested;
bb0c6722 1167
1291eb35
AP
1168int qemu_shutdown_requested_get(void)
1169{
1170 return shutdown_requested;
1171}
1172
1173int qemu_reset_requested_get(void)
1174{
1175 return reset_requested;
1176}
1177
cf7a2fe2
AJ
1178int qemu_shutdown_requested(void)
1179{
1180 int r = shutdown_requested;
1181 shutdown_requested = 0;
1182 return r;
1183}
1184
f64622c4
GN
1185void qemu_kill_report(void)
1186{
1187 if (shutdown_signal != -1) {
f1d3fb04
PM
1188 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1189 if (shutdown_pid == 0) {
1190 /* This happens for eg ^C at the terminal, so it's worth
1191 * avoiding printing an odd message in that case.
1192 */
1193 fputc('\n', stderr);
1194 } else {
1195 fprintf(stderr, " from pid %d\n", shutdown_pid);
1196 }
f64622c4
GN
1197 shutdown_signal = -1;
1198 }
1199}
1200
cf7a2fe2
AJ
1201int qemu_reset_requested(void)
1202{
1203 int r = reset_requested;
1204 reset_requested = 0;
1205 return r;
1206}
1207
1208int qemu_powerdown_requested(void)
1209{
1210 int r = powerdown_requested;
1211 powerdown_requested = 0;
1212 return r;
1213}
1214
e568902a
AL
1215static int qemu_debug_requested(void)
1216{
1217 int r = debug_requested;
1218 debug_requested = 0;
1219 return r;
1220}
1221
6e29f5da
AL
1222static int qemu_vmstop_requested(void)
1223{
1224 int r = vmstop_requested;
1225 vmstop_requested = 0;
1226 return r;
1227}
1228
a08d4367 1229void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 1230{
55ddfe8e 1231 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bb0c6722 1232
bb0c6722
FB
1233 re->func = func;
1234 re->opaque = opaque;
72cf2d4f 1235 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
1236}
1237
dda9b29f 1238void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
1239{
1240 QEMUResetEntry *re;
1241
72cf2d4f 1242 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 1243 if (re->func == func && re->opaque == opaque) {
72cf2d4f 1244 QTAILQ_REMOVE(&reset_handlers, re, entry);
dda9b29f
JK
1245 qemu_free(re);
1246 return;
1247 }
1248 }
1249}
1250
1251void qemu_system_reset(void)
1252{
1253 QEMUResetEntry *re, *nre;
1254
1255 /* reset all devices */
72cf2d4f 1256 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
1257 re->func(re->opaque);
1258 }
81d9b784 1259 monitor_protocol_event(QEVENT_RESET, NULL);
ea375f9a 1260 cpu_synchronize_all_post_reset();
bb0c6722
FB
1261}
1262
1263void qemu_system_reset_request(void)
1264{
d1beab82
FB
1265 if (no_reboot) {
1266 shutdown_requested = 1;
1267 } else {
1268 reset_requested = 1;
1269 }
b4a3d965 1270 cpu_stop_current();
d9f75a4e 1271 qemu_notify_event();
bb0c6722
FB
1272}
1273
f64622c4
GN
1274void qemu_system_killed(int signal, pid_t pid)
1275{
1276 shutdown_signal = signal;
1277 shutdown_pid = pid;
1278 qemu_system_shutdown_request();
1279}
1280
bb0c6722
FB
1281void qemu_system_shutdown_request(void)
1282{
1283 shutdown_requested = 1;
d9f75a4e 1284 qemu_notify_event();
bb0c6722
FB
1285}
1286
3475187d
FB
1287void qemu_system_powerdown_request(void)
1288{
1289 powerdown_requested = 1;
d9f75a4e
AL
1290 qemu_notify_event();
1291}
1292
8cf71710
JK
1293void qemu_system_debug_request(void)
1294{
1295 debug_requested = 1;
83f338f7 1296 qemu_notify_event();
8cf71710
JK
1297}
1298
1299void qemu_system_vmstop_request(int reason)
1300{
1301 vmstop_requested = reason;
1302 qemu_notify_event();
1303}
1304
d6f4ade2 1305void main_loop_wait(int nonblocking)
56f3a5d0 1306{
56f3a5d0
AL
1307 fd_set rfds, wfds, xfds;
1308 int ret, nfds;
1309 struct timeval tv;
d6f4ade2 1310 int timeout;
56f3a5d0 1311
d6f4ade2
PB
1312 if (nonblocking)
1313 timeout = 0;
1314 else {
1315 timeout = qemu_calculate_timeout();
1316 qemu_bh_update_timeout(&timeout);
1317 }
56f3a5d0 1318
0d93ca7c 1319 os_host_main_loop_wait(&timeout);
56f3a5d0 1320
02981419
PB
1321 tv.tv_sec = timeout / 1000;
1322 tv.tv_usec = (timeout % 1000) * 1000;
1323
fd1dff4b
FB
1324 /* poll any events */
1325 /* XXX: separate device handlers from system ones */
6abfbd79 1326 nfds = -1;
fd1dff4b
FB
1327 FD_ZERO(&rfds);
1328 FD_ZERO(&wfds);
e035649e 1329 FD_ZERO(&xfds);
02981419 1330 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
d918f23e
JK
1331 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1332
4870852c 1333 qemu_mutex_unlock_iothread();
e035649e 1334 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4870852c 1335 qemu_mutex_lock_iothread();
d918f23e 1336
02981419 1337 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
d918f23e 1338 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 1339
b6964827 1340 qemu_run_all_timers();
21d5d12b 1341
423f0742
PB
1342 /* Check bottom-halves last in case any of the earlier events triggered
1343 them. */
1344 qemu_bh_poll();
3b46e624 1345
5905b2e5
FB
1346}
1347
46481d39
JK
1348#ifndef CONFIG_IOTHREAD
1349static int vm_request_pending(void)
43b96858 1350{
46481d39
JK
1351 return powerdown_requested ||
1352 reset_requested ||
1353 shutdown_requested ||
1354 debug_requested ||
1355 vmstop_requested;
43b96858 1356}
46481d39 1357#endif
43b96858 1358
d9c32310
BS
1359qemu_irq qemu_system_powerdown;
1360
43b96858
AL
1361static void main_loop(void)
1362{
8e1b90ec
JK
1363 bool nonblocking = false;
1364#ifdef CONFIG_PROFILER
1365 int64_t ti;
1366#endif
6e29f5da 1367 int r;
e6e35b1e 1368
7277e027 1369 qemu_main_loop_start();
d6dc3d42 1370
6e29f5da 1371 for (;;) {
d6dc3d42 1372#ifndef CONFIG_IOTHREAD
46481d39
JK
1373 nonblocking = cpu_exec_all();
1374 if (vm_request_pending()) {
1375 nonblocking = true;
1376 }
d6dc3d42 1377#endif
89bfc105 1378#ifdef CONFIG_PROFILER
46481d39 1379 ti = profile_getclock();
89bfc105 1380#endif
46481d39 1381 main_loop_wait(nonblocking);
89bfc105 1382#ifdef CONFIG_PROFILER
46481d39 1383 dev_time += profile_getclock() - ti;
89bfc105 1384#endif
43b96858 1385
8cf71710
JK
1386 if (qemu_debug_requested()) {
1387 vm_stop(VMSTOP_DEBUG);
b1a15e7e 1388 }
43b96858 1389 if (qemu_shutdown_requested()) {
f64622c4 1390 qemu_kill_report();
242cd003 1391 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858 1392 if (no_shutdown) {
e07bbac5 1393 vm_stop(VMSTOP_SHUTDOWN);
43b96858
AL
1394 no_shutdown = 0;
1395 } else
1396 break;
1397 }
d6dc3d42
AL
1398 if (qemu_reset_requested()) {
1399 pause_all_vcpus();
a7ada151 1400 cpu_synchronize_all_states();
43b96858 1401 qemu_system_reset();
d6dc3d42
AL
1402 resume_all_vcpus();
1403 }
d9c32310 1404 if (qemu_powerdown_requested()) {
242cd003 1405 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
1406 qemu_irq_raise(qemu_system_powerdown);
1407 }
b1a15e7e 1408 if ((r = qemu_vmstop_requested())) {
6e29f5da 1409 vm_stop(r);
b1a15e7e 1410 }
b4608c04 1411 }
2bc93fed 1412 bdrv_close_all();
d6dc3d42 1413 pause_all_vcpus();
b4608c04
FB
1414}
1415
9bd7e6d9
PB
1416static void version(void)
1417{
f75ca1ae 1418 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1419}
1420
15f82208 1421static void help(int exitcode)
0824d6fc 1422{
e8105ebb 1423 const char *options_help =
ad96090a
BS
1424#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1425 opt_help
5824d651 1426#define DEFHEADING(text) stringify(text) "\n"
9f16732a 1427#include "qemu-options.def"
5824d651
BS
1428#undef DEF
1429#undef DEFHEADING
1430#undef GEN_DOCS
e8105ebb
PB
1431 ;
1432 version();
1433 printf("usage: %s [options] [disk_image]\n"
1434 "\n"
f75ca1ae 1435 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
3f020d70 1436 "\n"
e8105ebb 1437 "%s\n"
3f020d70 1438 "During emulation, the following keys are useful:\n"
1439 "ctrl-alt-f toggle full screen\n"
1440 "ctrl-alt-n switch to virtual console 'n'\n"
1441 "ctrl-alt toggle mouse and keyboard grab\n"
1442 "\n"
e8105ebb
PB
1443 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1444 "qemu",
1445 options_help);
15f82208 1446 exit(exitcode);
0824d6fc
FB
1447}
1448
cd6f1169
FB
1449#define HAS_ARG 0x0001
1450
cd6f1169
FB
1451typedef struct QEMUOption {
1452 const char *name;
1453 int flags;
1454 int index;
ad96090a 1455 uint32_t arch_mask;
cd6f1169
FB
1456} QEMUOption;
1457
dbed7e40 1458static const QEMUOption qemu_options[] = {
ad96090a
BS
1459 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1460#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1461 { option, opt_arg, opt_enum, arch_mask },
5824d651 1462#define DEFHEADING(text)
9f16732a 1463#include "qemu-options.def"
5824d651
BS
1464#undef DEF
1465#undef DEFHEADING
1466#undef GEN_DOCS
cd6f1169 1467 { NULL },
fc01f7e7 1468};
3893c124 1469static void select_vgahw (const char *p)
1470{
1471 const char *opts;
1472
64465297 1473 default_vga = 0;
86176759 1474 vga_interface_type = VGA_NONE;
3893c124 1475 if (strstart(p, "std", &opts)) {
86176759 1476 vga_interface_type = VGA_STD;
3893c124 1477 } else if (strstart(p, "cirrus", &opts)) {
86176759 1478 vga_interface_type = VGA_CIRRUS;
3893c124 1479 } else if (strstart(p, "vmware", &opts)) {
86176759 1480 vga_interface_type = VGA_VMWARE;
94909d9f 1481 } else if (strstart(p, "xenfb", &opts)) {
86176759 1482 vga_interface_type = VGA_XENFB;
a19cbfb3
GH
1483 } else if (strstart(p, "qxl", &opts)) {
1484 vga_interface_type = VGA_QXL;
28b85ed8 1485 } else if (!strstart(p, "none", &opts)) {
3893c124 1486 invalid_vga:
1487 fprintf(stderr, "Unknown vga type: %s\n", p);
1488 exit(1);
1489 }
cb5a7aa8 1490 while (*opts) {
1491 const char *nextopt;
1492
1493 if (strstart(opts, ",retrace=", &nextopt)) {
1494 opts = nextopt;
1495 if (strstart(opts, "dumb", &nextopt))
1496 vga_retrace_method = VGA_RETRACE_DUMB;
1497 else if (strstart(opts, "precise", &nextopt))
1498 vga_retrace_method = VGA_RETRACE_PRECISE;
1499 else goto invalid_vga;
1500 } else goto invalid_vga;
1501 opts = nextopt;
1502 }
3893c124 1503}
1504
1472a95b
JS
1505static DisplayType select_display(const char *p)
1506{
1507 const char *opts;
1508 DisplayType display = DT_DEFAULT;
1509
1510 if (strstart(p, "sdl", &opts)) {
1511#ifdef CONFIG_SDL
1512 display = DT_SDL;
1513 while (*opts) {
1514 const char *nextopt;
1515
1516 if (strstart(opts, ",frame=", &nextopt)) {
1517 opts = nextopt;
1518 if (strstart(opts, "on", &nextopt)) {
1519 no_frame = 0;
1520 } else if (strstart(opts, "off", &nextopt)) {
1521 no_frame = 1;
1522 } else {
05175535 1523 goto invalid_sdl_args;
1472a95b
JS
1524 }
1525 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1526 opts = nextopt;
1527 if (strstart(opts, "on", &nextopt)) {
1528 alt_grab = 1;
1529 } else if (strstart(opts, "off", &nextopt)) {
1530 alt_grab = 0;
1531 } else {
05175535 1532 goto invalid_sdl_args;
1472a95b
JS
1533 }
1534 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1535 opts = nextopt;
1536 if (strstart(opts, "on", &nextopt)) {
1537 ctrl_grab = 1;
1538 } else if (strstart(opts, "off", &nextopt)) {
1539 ctrl_grab = 0;
1540 } else {
05175535 1541 goto invalid_sdl_args;
1472a95b
JS
1542 }
1543 } else if (strstart(opts, ",window_close=", &nextopt)) {
1544 opts = nextopt;
1545 if (strstart(opts, "on", &nextopt)) {
1546 no_quit = 0;
1547 } else if (strstart(opts, "off", &nextopt)) {
1548 no_quit = 1;
1549 } else {
05175535 1550 goto invalid_sdl_args;
1472a95b
JS
1551 }
1552 } else {
05175535
PM
1553 invalid_sdl_args:
1554 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1555 exit(1);
1472a95b
JS
1556 }
1557 opts = nextopt;
1558 }
1559#else
1560 fprintf(stderr, "SDL support is disabled\n");
1561 exit(1);
1562#endif
3264ff12 1563 } else if (strstart(p, "vnc", &opts)) {
821601ea 1564#ifdef CONFIG_VNC
3264ff12
JS
1565 display_remote++;
1566
1567 if (*opts) {
1568 const char *nextopt;
1569
1570 if (strstart(opts, "=", &nextopt)) {
1571 vnc_display = nextopt;
1572 }
1573 }
1574 if (!vnc_display) {
1575 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1576 exit(1);
1577 }
821601ea
JS
1578#else
1579 fprintf(stderr, "VNC support is disabled\n");
1580 exit(1);
1581#endif
1472a95b
JS
1582 } else if (strstart(p, "curses", &opts)) {
1583#ifdef CONFIG_CURSES
1584 display = DT_CURSES;
1585#else
1586 fprintf(stderr, "Curses support is disabled\n");
1587 exit(1);
1588#endif
4171d32e
JS
1589 } else if (strstart(p, "none", &opts)) {
1590 display = DT_NONE;
1472a95b 1591 } else {
1472a95b
JS
1592 fprintf(stderr, "Unknown display type: %s\n", p);
1593 exit(1);
1594 }
1595
1596 return display;
1597}
1598
7d4c3d53
MA
1599static int balloon_parse(const char *arg)
1600{
382f0743 1601 QemuOpts *opts;
7d4c3d53 1602
382f0743
GH
1603 if (strcmp(arg, "none") == 0) {
1604 return 0;
1605 }
1606
1607 if (!strncmp(arg, "virtio", 6)) {
1608 if (arg[6] == ',') {
1609 /* have params -> parse them */
3329f07b 1610 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1611 if (!opts)
1612 return -1;
1613 } else {
1614 /* create empty opts */
3329f07b 1615 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
7d4c3d53 1616 }
29f82b37 1617 qemu_opt_set(opts, "driver", "virtio-balloon");
382f0743 1618 return 0;
7d4c3d53 1619 }
382f0743
GH
1620
1621 return -1;
7d4c3d53 1622}
7d4c3d53 1623
5cea8590
PB
1624char *qemu_find_file(int type, const char *name)
1625{
1626 int len;
1627 const char *subdir;
1628 char *buf;
1629
1630 /* If name contains path separators then try it as a straight path. */
1631 if ((strchr(name, '/') || strchr(name, '\\'))
1632 && access(name, R_OK) == 0) {
73ffc805 1633 return qemu_strdup(name);
5cea8590
PB
1634 }
1635 switch (type) {
1636 case QEMU_FILE_TYPE_BIOS:
1637 subdir = "";
1638 break;
1639 case QEMU_FILE_TYPE_KEYMAP:
1640 subdir = "keymaps/";
1641 break;
1642 default:
1643 abort();
1644 }
1645 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1646 buf = qemu_mallocz(len);
3a41759d 1647 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
1648 if (access(buf, R_OK)) {
1649 qemu_free(buf);
1650 return NULL;
1651 }
1652 return buf;
1653}
1654
ff952ba2
MA
1655static int device_help_func(QemuOpts *opts, void *opaque)
1656{
1657 return qdev_device_help(opts);
1658}
1659
f31d07d1
GH
1660static int device_init_func(QemuOpts *opts, void *opaque)
1661{
1662 DeviceState *dev;
1663
1664 dev = qdev_device_add(opts);
1665 if (!dev)
1666 return -1;
1667 return 0;
1668}
1669
1a688d3b
GH
1670static int chardev_init_func(QemuOpts *opts, void *opaque)
1671{
1672 CharDriverState *chr;
1673
1674 chr = qemu_chr_open_opts(opts, NULL);
1675 if (!chr)
1676 return -1;
1677 return 0;
1678}
1679
758e8e38 1680#ifdef CONFIG_VIRTFS
74db920c
GS
1681static int fsdev_init_func(QemuOpts *opts, void *opaque)
1682{
1683 int ret;
1684 ret = qemu_fsdev_add(opts);
1685
1686 return ret;
1687}
1688#endif
1689
88589343
GH
1690static int mon_init_func(QemuOpts *opts, void *opaque)
1691{
1692 CharDriverState *chr;
1693 const char *chardev;
1694 const char *mode;
1695 int flags;
1696
1697 mode = qemu_opt_get(opts, "mode");
1698 if (mode == NULL) {
1699 mode = "readline";
1700 }
1701 if (strcmp(mode, "readline") == 0) {
1702 flags = MONITOR_USE_READLINE;
1703 } else if (strcmp(mode, "control") == 0) {
1704 flags = MONITOR_USE_CONTROL;
1705 } else {
1706 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1707 exit(1);
1708 }
1709
39eaab9a
DB
1710 if (qemu_opt_get_bool(opts, "pretty", 0))
1711 flags |= MONITOR_USE_PRETTY;
1712
88589343
GH
1713 if (qemu_opt_get_bool(opts, "default", 0))
1714 flags |= MONITOR_IS_DEFAULT;
1715
1716 chardev = qemu_opt_get(opts, "chardev");
1717 chr = qemu_chr_find(chardev);
1718 if (chr == NULL) {
1719 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1720 exit(1);
1721 }
1722
1723 monitor_init(chr, flags);
1724 return 0;
1725}
1726
6ca5582d 1727static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1728{
1729 static int monitor_device_index = 0;
1730 QemuOpts *opts;
1731 const char *p;
1732 char label[32];
1733 int def = 0;
1734
1735 if (strstart(optarg, "chardev:", &p)) {
1736 snprintf(label, sizeof(label), "%s", p);
1737 } else {
140e065d
JK
1738 snprintf(label, sizeof(label), "compat_monitor%d",
1739 monitor_device_index);
1740 if (monitor_device_index == 0) {
88589343
GH
1741 def = 1;
1742 }
1743 opts = qemu_chr_parse_compat(label, optarg);
1744 if (!opts) {
1745 fprintf(stderr, "parse error: %s\n", optarg);
1746 exit(1);
1747 }
1748 }
1749
3329f07b 1750 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
88589343
GH
1751 if (!opts) {
1752 fprintf(stderr, "duplicate chardev: %s\n", label);
1753 exit(1);
1754 }
6ca5582d 1755 qemu_opt_set(opts, "mode", mode);
88589343
GH
1756 qemu_opt_set(opts, "chardev", label);
1757 if (def)
1758 qemu_opt_set(opts, "default", "on");
1759 monitor_device_index++;
1760}
1761
bd3c948d
GH
1762struct device_config {
1763 enum {
aee1b935
GH
1764 DEV_USB, /* -usbdevice */
1765 DEV_BT, /* -bt */
1766 DEV_SERIAL, /* -serial */
1767 DEV_PARALLEL, /* -parallel */
1768 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1769 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
1770 } type;
1771 const char *cmdline;
72cf2d4f 1772 QTAILQ_ENTRY(device_config) next;
bd3c948d 1773};
72cf2d4f 1774QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1775
1776static void add_device_config(int type, const char *cmdline)
1777{
1778 struct device_config *conf;
1779
1780 conf = qemu_mallocz(sizeof(*conf));
1781 conf->type = type;
1782 conf->cmdline = cmdline;
72cf2d4f 1783 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1784}
1785
1786static int foreach_device_config(int type, int (*func)(const char *cmdline))
1787{
1788 struct device_config *conf;
1789 int rc;
1790
72cf2d4f 1791 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1792 if (conf->type != type)
1793 continue;
1794 rc = func(conf->cmdline);
1795 if (0 != rc)
1796 return rc;
1797 }
1798 return 0;
1799}
1800
998bbd74
GH
1801static int serial_parse(const char *devname)
1802{
1803 static int index = 0;
1804 char label[32];
1805
1806 if (strcmp(devname, "none") == 0)
1807 return 0;
1808 if (index == MAX_SERIAL_PORTS) {
1809 fprintf(stderr, "qemu: too many serial ports\n");
1810 exit(1);
1811 }
1812 snprintf(label, sizeof(label), "serial%d", index);
1813 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1814 if (!serial_hds[index]) {
1815 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1816 devname, strerror(errno));
1817 return -1;
1818 }
1819 index++;
1820 return 0;
1821}
1822
6a5e8b0e
GH
1823static int parallel_parse(const char *devname)
1824{
1825 static int index = 0;
1826 char label[32];
1827
1828 if (strcmp(devname, "none") == 0)
1829 return 0;
1830 if (index == MAX_PARALLEL_PORTS) {
1831 fprintf(stderr, "qemu: too many parallel ports\n");
1832 exit(1);
1833 }
1834 snprintf(label, sizeof(label), "parallel%d", index);
1835 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1836 if (!parallel_hds[index]) {
1837 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1838 devname, strerror(errno));
1839 return -1;
1840 }
1841 index++;
1842 return 0;
1843}
1844
aee1b935
GH
1845static int virtcon_parse(const char *devname)
1846{
3329f07b 1847 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
1848 static int index = 0;
1849 char label[32];
392ecf54 1850 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
1851
1852 if (strcmp(devname, "none") == 0)
1853 return 0;
1854 if (index == MAX_VIRTIO_CONSOLES) {
1855 fprintf(stderr, "qemu: too many virtio consoles\n");
1856 exit(1);
1857 }
392ecf54 1858
3329f07b 1859 bus_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1860 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1861
3329f07b 1862 dev_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1863 qemu_opt_set(dev_opts, "driver", "virtconsole");
1864
aee1b935
GH
1865 snprintf(label, sizeof(label), "virtcon%d", index);
1866 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1867 if (!virtcon_hds[index]) {
1868 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1869 devname, strerror(errno));
1870 return -1;
1871 }
392ecf54
AS
1872 qemu_opt_set(dev_opts, "chardev", label);
1873
aee1b935
GH
1874 index++;
1875 return 0;
1876}
1877
c9f398e5
PA
1878static int debugcon_parse(const char *devname)
1879{
1880 QemuOpts *opts;
1881
1882 if (!qemu_chr_open("debugcon", devname, NULL)) {
1883 exit(1);
1884 }
3329f07b 1885 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
c9f398e5
PA
1886 if (!opts) {
1887 fprintf(stderr, "qemu: already have a debugcon device\n");
1888 exit(1);
1889 }
1890 qemu_opt_set(opts, "driver", "isa-debugcon");
1891 qemu_opt_set(opts, "chardev", "debugcon");
1892 return 0;
1893}
1894
303d4e86
AP
1895static int tcg_init(void)
1896{
1897 return 0;
1898}
1899
1900static struct {
1901 const char *opt_name;
1902 const char *name;
1903 int (*available)(void);
1904 int (*init)(void);
1905 int *allowed;
1906} accel_list[] = {
1907 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
3285cf4f 1908 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
303d4e86
AP
1909 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1910};
1911
1912static int configure_accelerator(void)
1913{
1914 const char *p = NULL;
1915 char buf[10];
1916 int i, ret;
1917 bool accel_initalised = 0;
1918 bool init_failed = 0;
1919
1920 QemuOptsList *list = qemu_find_opts("machine");
1921 if (!QTAILQ_EMPTY(&list->head)) {
1922 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1923 }
1924
1925 if (p == NULL) {
1926 /* Use the default "accelerator", tcg */
1927 p = "tcg";
1928 }
1929
1930 while (!accel_initalised && *p != '\0') {
1931 if (*p == ':') {
1932 p++;
1933 }
1934 p = get_opt_name(buf, sizeof (buf), p, ':');
1935 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1936 if (strcmp(accel_list[i].opt_name, buf) == 0) {
a16c53b1 1937 *(accel_list[i].allowed) = 1;
303d4e86
AP
1938 ret = accel_list[i].init();
1939 if (ret < 0) {
1940 init_failed = 1;
1941 if (!accel_list[i].available()) {
1942 printf("%s not supported for this target\n",
1943 accel_list[i].name);
1944 } else {
1945 fprintf(stderr, "failed to initialize %s: %s\n",
1946 accel_list[i].name,
1947 strerror(-ret));
1948 }
a16c53b1 1949 *(accel_list[i].allowed) = 0;
303d4e86
AP
1950 } else {
1951 accel_initalised = 1;
303d4e86
AP
1952 }
1953 break;
1954 }
1955 }
1956 if (i == ARRAY_SIZE(accel_list)) {
1957 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1958 }
1959 }
1960
1961 if (!accel_initalised) {
1962 fprintf(stderr, "No accelerator found!\n");
1963 exit(1);
1964 }
1965
1966 if (init_failed) {
1967 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1968 }
1969
1970 return !accel_initalised;
1971}
1972
fd42deeb
GH
1973void qemu_add_exit_notifier(Notifier *notify)
1974{
1975 notifier_list_add(&exit_notifiers, notify);
1976}
1977
1978void qemu_remove_exit_notifier(Notifier *notify)
1979{
1980 notifier_list_remove(&exit_notifiers, notify);
1981}
1982
1983static void qemu_run_exit_notifiers(void)
1984{
1985 notifier_list_notify(&exit_notifiers);
1986}
1987
4cab946a
GN
1988void qemu_add_machine_init_done_notifier(Notifier *notify)
1989{
1990 notifier_list_add(&machine_init_done_notifiers, notify);
1991}
1992
1993static void qemu_run_machine_init_done_notifiers(void)
1994{
1995 notifier_list_notify(&machine_init_done_notifiers);
1996}
1997
6530a97b
AL
1998static const QEMUOption *lookup_opt(int argc, char **argv,
1999 const char **poptarg, int *poptind)
2000{
2001 const QEMUOption *popt;
2002 int optind = *poptind;
2003 char *r = argv[optind];
2004 const char *optarg;
2005
0f0bc3f1 2006 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2007 optind++;
2008 /* Treat --foo the same as -foo. */
2009 if (r[1] == '-')
2010 r++;
2011 popt = qemu_options;
2012 for(;;) {
2013 if (!popt->name) {
0f0bc3f1 2014 error_report("invalid option");
6530a97b
AL
2015 exit(1);
2016 }
2017 if (!strcmp(popt->name, r + 1))
2018 break;
2019 popt++;
2020 }
2021 if (popt->flags & HAS_ARG) {
2022 if (optind >= argc) {
0f0bc3f1 2023 error_report("requires an argument");
6530a97b
AL
2024 exit(1);
2025 }
2026 optarg = argv[optind++];
0f0bc3f1 2027 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2028 } else {
2029 optarg = NULL;
2030 }
2031
2032 *poptarg = optarg;
2033 *poptind = optind;
2034
2035 return popt;
2036}
2037
902b3d5c 2038int main(int argc, char **argv, char **envp)
0824d6fc 2039{
59030a8c 2040 const char *gdbstub_dev = NULL;
e4bcb14c 2041 int i;
da1fcfda 2042 int snapshot, linux_boot;
4e3de9e9 2043 const char *icount_option = NULL;
7f7f9873 2044 const char *initrd_filename;
a20dd508 2045 const char *kernel_filename, *kernel_cmdline;
195325a4 2046 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 2047 DisplayState *ds;
7d957bd8 2048 DisplayChangeListener *dcl;
46d4767d 2049 int cyls, heads, secs, translation;
f31d07d1 2050 QemuOpts *hda_opts = NULL, *opts;
03b0ba70 2051 QemuOptsList *olist;
cd6f1169 2052 int optind;
6530a97b 2053 const char *optarg;
d63d307f 2054 const char *loadvm = NULL;
cc1daa40 2055 QEMUMachine *machine;
94fc95cd 2056 const char *cpu_model;
26a5f13b 2057 int tb_size;
93815bc2 2058 const char *pid_file = NULL;
5bb7910a 2059 const char *incoming = NULL;
821601ea 2060#ifdef CONFIG_VNC
993fbfdb 2061 int show_vnc_port = 0;
821601ea 2062#endif
292444cb 2063 int defconfig = 1;
ab6540d5 2064 const char *trace_file = NULL;
0b5538c3 2065
fd42deeb 2066 atexit(qemu_run_exit_notifiers);
65abca0a
MA
2067 error_set_progname(argv[0]);
2068
6875204c
JK
2069 init_clocks();
2070
902b3d5c 2071 qemu_cache_utils_init(envp);
2072
72cf2d4f 2073 QLIST_INIT (&vm_change_state_head);
fe98ac14 2074 os_setup_early_signal_handling();
be995c27 2075
f80f9ec9 2076 module_call_init(MODULE_INIT_MACHINE);
0c257437 2077 machine = find_default_machine();
94fc95cd 2078 cpu_model = NULL;
fc01f7e7 2079 initrd_filename = NULL;
4fc5d071 2080 ram_size = 0;
33e3963e 2081 snapshot = 0;
a20dd508
FB
2082 kernel_filename = NULL;
2083 kernel_cmdline = "";
c4b1fcc0 2084 cyls = heads = secs = 0;
46d4767d 2085 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 2086
268a362c
AL
2087 for (i = 0; i < MAX_NODES; i++) {
2088 node_mem[i] = 0;
2089 node_cpumask[i] = 0;
2090 }
2091
268a362c 2092 nb_numa_nodes = 0;
7c9d8e07 2093 nb_nics = 0;
3b46e624 2094
26a5f13b 2095 tb_size = 0;
41bd639b
BS
2096 autostart= 1;
2097
292444cb
AL
2098 /* first pass of option parsing */
2099 optind = 1;
2100 while (optind < argc) {
2101 if (argv[optind][0] != '-') {
2102 /* disk image */
28e68d68 2103 optind++;
292444cb
AL
2104 continue;
2105 } else {
2106 const QEMUOption *popt;
2107
2108 popt = lookup_opt(argc, argv, &optarg, &optind);
2109 switch (popt->index) {
2110 case QEMU_OPTION_nodefconfig:
2111 defconfig=0;
2112 break;
2113 }
2114 }
2115 }
2116
2117 if (defconfig) {
dcfb0939 2118 int ret;
cf5a65aa 2119
dcfb0939 2120 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 2121 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2122 exit(1);
292444cb
AL
2123 }
2124
dcfb0939 2125 ret = qemu_read_config_file(arch_config_name);
019e78ba 2126 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2127 exit(1);
292444cb
AL
2128 }
2129 }
de06f8d1 2130 cpudef_init();
292444cb
AL
2131
2132 /* second pass of option parsing */
cd6f1169 2133 optind = 1;
0824d6fc 2134 for(;;) {
cd6f1169 2135 if (optind >= argc)
0824d6fc 2136 break;
6530a97b 2137 if (argv[optind][0] != '-') {
2292ddae 2138 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2139 } else {
2140 const QEMUOption *popt;
2141
6530a97b 2142 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
2143 if (!(popt->arch_mask & arch_type)) {
2144 printf("Option %s not supported for this target\n", popt->name);
2145 exit(1);
2146 }
cd6f1169 2147 switch(popt->index) {
cc1daa40
FB
2148 case QEMU_OPTION_M:
2149 machine = find_machine(optarg);
2150 if (!machine) {
2151 QEMUMachine *m;
2152 printf("Supported machines are:\n");
2153 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
2154 if (m->alias)
2155 printf("%-10s %s (alias of %s)\n",
2156 m->alias, m->desc, m->name);
cc1daa40 2157 printf("%-10s %s%s\n",
5fafdf24 2158 m->name, m->desc,
0c257437 2159 m->is_default ? " (default)" : "");
cc1daa40 2160 }
15f82208 2161 exit(*optarg != '?');
cc1daa40
FB
2162 }
2163 break;
94fc95cd
JM
2164 case QEMU_OPTION_cpu:
2165 /* hw initialization will check this */
15f82208 2166 if (*optarg == '?') {
262353cb 2167 list_cpus(stdout, &fprintf, optarg);
15f82208 2168 exit(0);
94fc95cd
JM
2169 } else {
2170 cpu_model = optarg;
2171 }
2172 break;
cd6f1169 2173 case QEMU_OPTION_initrd:
fc01f7e7
FB
2174 initrd_filename = optarg;
2175 break;
cd6f1169 2176 case QEMU_OPTION_hda:
5645b0f4
MA
2177 {
2178 char buf[256];
2179 if (cyls == 0)
2180 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2181 else
2182 snprintf(buf, sizeof(buf),
2183 "%s,cyls=%d,heads=%d,secs=%d%s",
2184 HD_OPTS , cyls, heads, secs,
2185 translation == BIOS_ATA_TRANSLATION_LBA ?
e4bcb14c 2186 ",trans=lba" :
5645b0f4 2187 translation == BIOS_ATA_TRANSLATION_NONE ?
e4bcb14c 2188 ",trans=none" : "");
5645b0f4
MA
2189 drive_add(IF_DEFAULT, 0, optarg, buf);
2190 break;
2191 }
cd6f1169 2192 case QEMU_OPTION_hdb:
cc1daa40
FB
2193 case QEMU_OPTION_hdc:
2194 case QEMU_OPTION_hdd:
2292ddae
MA
2195 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2196 HD_OPTS);
fc01f7e7 2197 break;
e4bcb14c 2198 case QEMU_OPTION_drive:
e2982c3a
MT
2199 if (drive_def(optarg) == NULL) {
2200 exit(1);
2201 }
e4bcb14c 2202 break;
d058fe03
GH
2203 case QEMU_OPTION_set:
2204 if (qemu_set_option(optarg) != 0)
2205 exit(1);
2206 break;
d0fef6fb
GH
2207 case QEMU_OPTION_global:
2208 if (qemu_global_option(optarg) != 0)
2209 exit(1);
2210 break;
3e3d5815 2211 case QEMU_OPTION_mtdblock:
2292ddae 2212 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2213 break;
a1bb27b1 2214 case QEMU_OPTION_sd:
2292ddae 2215 drive_add(IF_SD, 0, optarg, SD_OPTS);
a1bb27b1 2216 break;
86f55663 2217 case QEMU_OPTION_pflash:
2292ddae 2218 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2219 break;
cd6f1169 2220 case QEMU_OPTION_snapshot:
33e3963e
FB
2221 snapshot = 1;
2222 break;
cd6f1169 2223 case QEMU_OPTION_hdachs:
330d0414 2224 {
330d0414
FB
2225 const char *p;
2226 p = optarg;
2227 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2228 if (cyls < 1 || cyls > 16383)
2229 goto chs_fail;
330d0414
FB
2230 if (*p != ',')
2231 goto chs_fail;
2232 p++;
2233 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2234 if (heads < 1 || heads > 16)
2235 goto chs_fail;
330d0414
FB
2236 if (*p != ',')
2237 goto chs_fail;
2238 p++;
2239 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2240 if (secs < 1 || secs > 63)
2241 goto chs_fail;
2242 if (*p == ',') {
2243 p++;
2244 if (!strcmp(p, "none"))
2245 translation = BIOS_ATA_TRANSLATION_NONE;
2246 else if (!strcmp(p, "lba"))
2247 translation = BIOS_ATA_TRANSLATION_LBA;
2248 else if (!strcmp(p, "auto"))
2249 translation = BIOS_ATA_TRANSLATION_AUTO;
2250 else
2251 goto chs_fail;
2252 } else if (*p != '\0') {
c4b1fcc0 2253 chs_fail:
46d4767d
FB
2254 fprintf(stderr, "qemu: invalid physical CHS format\n");
2255 exit(1);
c4b1fcc0 2256 }
9dfd7c7a
GH
2257 if (hda_opts != NULL) {
2258 char num[16];
2259 snprintf(num, sizeof(num), "%d", cyls);
2260 qemu_opt_set(hda_opts, "cyls", num);
2261 snprintf(num, sizeof(num), "%d", heads);
2262 qemu_opt_set(hda_opts, "heads", num);
2263 snprintf(num, sizeof(num), "%d", secs);
2264 qemu_opt_set(hda_opts, "secs", num);
2265 if (translation == BIOS_ATA_TRANSLATION_LBA)
2266 qemu_opt_set(hda_opts, "trans", "lba");
2267 if (translation == BIOS_ATA_TRANSLATION_NONE)
2268 qemu_opt_set(hda_opts, "trans", "none");
2269 }
330d0414
FB
2270 }
2271 break;
268a362c
AL
2272 case QEMU_OPTION_numa:
2273 if (nb_numa_nodes >= MAX_NODES) {
2274 fprintf(stderr, "qemu: too many NUMA nodes\n");
2275 exit(1);
2276 }
2277 numa_add(optarg);
2278 break;
1472a95b
JS
2279 case QEMU_OPTION_display:
2280 display_type = select_display(optarg);
2281 break;
cd6f1169 2282 case QEMU_OPTION_nographic:
993fbfdb 2283 display_type = DT_NOGRAPHIC;
a20dd508 2284 break;
4d3b6f6e 2285 case QEMU_OPTION_curses:
47b05369 2286#ifdef CONFIG_CURSES
993fbfdb 2287 display_type = DT_CURSES;
47b05369
JS
2288#else
2289 fprintf(stderr, "Curses support is disabled\n");
2290 exit(1);
4d3b6f6e 2291#endif
47b05369 2292 break;
a171fe39
AZ
2293 case QEMU_OPTION_portrait:
2294 graphic_rotate = 1;
2295 break;
cd6f1169 2296 case QEMU_OPTION_kernel:
a20dd508
FB
2297 kernel_filename = optarg;
2298 break;
cd6f1169 2299 case QEMU_OPTION_append:
a20dd508 2300 kernel_cmdline = optarg;
313aa567 2301 break;
cd6f1169 2302 case QEMU_OPTION_cdrom:
2292ddae 2303 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2304 break;
cd6f1169 2305 case QEMU_OPTION_boot:
28c5af54 2306 {
ef3adf68 2307 static const char * const params[] = {
95387491 2308 "order", "once", "menu", NULL
ef3adf68
JK
2309 };
2310 char buf[sizeof(boot_devices)];
e0f084bf 2311 char *standard_boot_devices;
ef3adf68
JK
2312 int legacy = 0;
2313
2314 if (!strchr(optarg, '=')) {
2315 legacy = 1;
2316 pstrcpy(buf, sizeof(buf), optarg);
2317 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2318 fprintf(stderr,
2319 "qemu: unknown boot parameter '%s' in '%s'\n",
2320 buf, optarg);
2321 exit(1);
2322 }
2323
2324 if (legacy ||
2325 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2326 validate_bootdevices(buf);
ef3adf68 2327 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2328 }
e0f084bf
JK
2329 if (!legacy) {
2330 if (get_param_value(buf, sizeof(buf),
2331 "once", optarg)) {
4e9e9d6e 2332 validate_bootdevices(buf);
e0f084bf
JK
2333 standard_boot_devices = qemu_strdup(boot_devices);
2334 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2335 qemu_register_reset(restore_boot_devices,
2336 standard_boot_devices);
2337 }
95387491
JK
2338 if (get_param_value(buf, sizeof(buf),
2339 "menu", optarg)) {
2340 if (!strcmp(buf, "on")) {
2341 boot_menu = 1;
2342 } else if (!strcmp(buf, "off")) {
2343 boot_menu = 0;
2344 } else {
2345 fprintf(stderr,
2346 "qemu: invalid option value '%s'\n",
2347 buf);
2348 exit(1);
2349 }
2350 }
e0f084bf 2351 }
36b486bb
FB
2352 }
2353 break;
cd6f1169 2354 case QEMU_OPTION_fda:
cd6f1169 2355 case QEMU_OPTION_fdb:
2292ddae
MA
2356 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2357 optarg, FD_OPTS);
c45886db 2358 break;
52ca8d6a
FB
2359 case QEMU_OPTION_no_fd_bootchk:
2360 fd_bootchk = 0;
2361 break;
a1ea458f 2362 case QEMU_OPTION_netdev:
3329f07b 2363 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2364 exit(1);
2365 }
2366 break;
7c9d8e07 2367 case QEMU_OPTION_net:
3329f07b 2368 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2369 exit(1);
2370 }
702c651c 2371 break;
c7f74643
FB
2372#ifdef CONFIG_SLIRP
2373 case QEMU_OPTION_tftp:
ad196a9d 2374 legacy_tftp_prefix = optarg;
9bf05444 2375 break;
47d5d01a 2376 case QEMU_OPTION_bootp:
ad196a9d 2377 legacy_bootp_filename = optarg;
47d5d01a 2378 break;
9bf05444 2379 case QEMU_OPTION_redir:
0752706d
MA
2380 if (net_slirp_redir(optarg) < 0)
2381 exit(1);
9bf05444 2382 break;
c7f74643 2383#endif
dc72ac14 2384 case QEMU_OPTION_bt:
bd3c948d 2385 add_device_config(DEV_BT, optarg);
dc72ac14 2386 break;
1d14ffa9 2387 case QEMU_OPTION_audio_help:
ad96090a
BS
2388 if (!(audio_available())) {
2389 printf("Option %s not supported for this target\n", popt->name);
2390 exit(1);
2391 }
1d14ffa9
FB
2392 AUD_help ();
2393 exit (0);
2394 break;
2395 case QEMU_OPTION_soundhw:
ad96090a
BS
2396 if (!(audio_available())) {
2397 printf("Option %s not supported for this target\n", popt->name);
2398 exit(1);
2399 }
1d14ffa9
FB
2400 select_soundhw (optarg);
2401 break;
cd6f1169 2402 case QEMU_OPTION_h:
15f82208 2403 help(0);
cd6f1169 2404 break;
9bd7e6d9
PB
2405 case QEMU_OPTION_version:
2406 version();
2407 exit(0);
2408 break;
00f82b8a 2409 case QEMU_OPTION_m: {
70b4f4bb 2410 int64_t value;
9f9b17a4
JS
2411
2412 value = strtosz(optarg, NULL);
2413 if (value < 0) {
00f82b8a 2414 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2415 exit(1);
2416 }
00f82b8a
AJ
2417
2418 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 2419 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
2420 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2421 exit(1);
2422 }
c227f099 2423 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2424 fprintf(stderr, "qemu: ram size too large\n");
2425 exit(1);
2426 }
2427 ram_size = value;
cd6f1169 2428 break;
00f82b8a 2429 }
c902760f
MT
2430 case QEMU_OPTION_mempath:
2431 mem_path = optarg;
2432 break;
2433#ifdef MAP_POPULATE
2434 case QEMU_OPTION_mem_prealloc:
2435 mem_prealloc = 1;
2436 break;
2437#endif
cd6f1169 2438 case QEMU_OPTION_d:
de06f8d1 2439 set_cpu_log(optarg);
cd6f1169 2440 break;
cd6f1169 2441 case QEMU_OPTION_s:
59030a8c 2442 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 2443 break;
59030a8c
AL
2444 case QEMU_OPTION_gdb:
2445 gdbstub_dev = optarg;
cd6f1169 2446 break;
cd6f1169 2447 case QEMU_OPTION_L:
5cea8590 2448 data_dir = optarg;
cd6f1169 2449 break;
1192dad8
JM
2450 case QEMU_OPTION_bios:
2451 bios_name = optarg;
2452 break;
1b530a6d
AJ
2453 case QEMU_OPTION_singlestep:
2454 singlestep = 1;
2455 break;
cd6f1169 2456 case QEMU_OPTION_S:
3c07f8e8 2457 autostart = 0;
cd6f1169 2458 break;
3d11d0eb
FB
2459 case QEMU_OPTION_k:
2460 keyboard_layout = optarg;
2461 break;
ee22c2f7
FB
2462 case QEMU_OPTION_localtime:
2463 rtc_utc = 0;
2464 break;
3893c124 2465 case QEMU_OPTION_vga:
2466 select_vgahw (optarg);
1bfe856e 2467 break;
e9b137c2
FB
2468 case QEMU_OPTION_g:
2469 {
2470 const char *p;
2471 int w, h, depth;
2472 p = optarg;
2473 w = strtol(p, (char **)&p, 10);
2474 if (w <= 0) {
2475 graphic_error:
2476 fprintf(stderr, "qemu: invalid resolution or depth\n");
2477 exit(1);
2478 }
2479 if (*p != 'x')
2480 goto graphic_error;
2481 p++;
2482 h = strtol(p, (char **)&p, 10);
2483 if (h <= 0)
2484 goto graphic_error;
2485 if (*p == 'x') {
2486 p++;
2487 depth = strtol(p, (char **)&p, 10);
5fafdf24 2488 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2489 depth != 24 && depth != 32)
2490 goto graphic_error;
2491 } else if (*p == '\0') {
2492 depth = graphic_depth;
2493 } else {
2494 goto graphic_error;
2495 }
3b46e624 2496
e9b137c2
FB
2497 graphic_width = w;
2498 graphic_height = h;
2499 graphic_depth = depth;
2500 }
2501 break;
20d8a3ed
TS
2502 case QEMU_OPTION_echr:
2503 {
2504 char *r;
2505 term_escape_char = strtol(optarg, &r, 0);
2506 if (r == optarg)
2507 printf("Bad argument to echr\n");
2508 break;
2509 }
82c643ff 2510 case QEMU_OPTION_monitor:
6ca5582d
GH
2511 monitor_parse(optarg, "readline");
2512 default_monitor = 0;
2513 break;
2514 case QEMU_OPTION_qmp:
2515 monitor_parse(optarg, "control");
2d114dc1 2516 default_monitor = 0;
82c643ff 2517 break;
22a0e04b 2518 case QEMU_OPTION_mon:
3329f07b 2519 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2520 if (!opts) {
22a0e04b
GH
2521 exit(1);
2522 }
2d114dc1 2523 default_monitor = 0;
22a0e04b 2524 break;
191bc01b 2525 case QEMU_OPTION_chardev:
3329f07b 2526 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2527 if (!opts) {
191bc01b
GH
2528 exit(1);
2529 }
191bc01b 2530 break;
74db920c 2531 case QEMU_OPTION_fsdev:
03b0ba70
GH
2532 olist = qemu_find_opts("fsdev");
2533 if (!olist) {
2534 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2535 exit(1);
2536 }
2537 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2538 if (!opts) {
2539 fprintf(stderr, "parse error: %s\n", optarg);
2540 exit(1);
2541 }
2542 break;
3d54abc7 2543 case QEMU_OPTION_virtfs: {
e14ea479
SH
2544 QemuOpts *fsdev;
2545 QemuOpts *device;
3d54abc7 2546
03b0ba70
GH
2547 olist = qemu_find_opts("virtfs");
2548 if (!olist) {
2549 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2550 exit(1);
2551 }
2552 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2553 if (!opts) {
2554 fprintf(stderr, "parse error: %s\n", optarg);
2555 exit(1);
2556 }
2557
9ce56db6
VJJ
2558 if (qemu_opt_get(opts, "fstype") == NULL ||
2559 qemu_opt_get(opts, "mount_tag") == NULL ||
2560 qemu_opt_get(opts, "path") == NULL ||
2561 qemu_opt_get(opts, "security_model") == NULL) {
2562 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
12848bfc 2563 "security_model=[mapped|passthrough|none],"
e14ea479 2564 "mount_tag=tag.\n");
9ce56db6
VJJ
2565 exit(1);
2566 }
2567
e14ea479
SH
2568 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2569 qemu_opt_get(opts, "mount_tag"), 1);
2570 if (!fsdev) {
2571 fprintf(stderr, "duplicate fsdev id: %s\n",
2572 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2573 exit(1);
2574 }
e14ea479
SH
2575 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2576 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2577 qemu_opt_set(fsdev, "security_model",
2578 qemu_opt_get(opts, "security_model"));
2579
2580 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2581 qemu_opt_set(device, "driver", "virtio-9p-pci");
2582 qemu_opt_set(device, "fsdev",
2583 qemu_opt_get(opts, "mount_tag"));
2584 qemu_opt_set(device, "mount_tag",
2585 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2586 break;
2587 }
82c643ff 2588 case QEMU_OPTION_serial:
998bbd74
GH
2589 add_device_config(DEV_SERIAL, optarg);
2590 default_serial = 0;
18141ed6
JK
2591 if (strncmp(optarg, "mon:", 4) == 0) {
2592 default_monitor = 0;
2593 }
82c643ff 2594 break;
9dd986cc 2595 case QEMU_OPTION_watchdog:
09aaa160
MA
2596 if (watchdog) {
2597 fprintf(stderr,
2598 "qemu: only one watchdog option may be given\n");
2599 return 1;
2600 }
2601 watchdog = optarg;
9dd986cc
RJ
2602 break;
2603 case QEMU_OPTION_watchdog_action:
2604 if (select_watchdog_action(optarg) == -1) {
2605 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2606 exit(1);
2607 }
2608 break;
51ecf136 2609 case QEMU_OPTION_virtiocon:
aee1b935
GH
2610 add_device_config(DEV_VIRTCON, optarg);
2611 default_virtcon = 0;
18141ed6
JK
2612 if (strncmp(optarg, "mon:", 4) == 0) {
2613 default_monitor = 0;
2614 }
51ecf136 2615 break;
6508fe59 2616 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2617 add_device_config(DEV_PARALLEL, optarg);
2618 default_parallel = 0;
18141ed6
JK
2619 if (strncmp(optarg, "mon:", 4) == 0) {
2620 default_monitor = 0;
2621 }
6508fe59 2622 break;
c9f398e5
PA
2623 case QEMU_OPTION_debugcon:
2624 add_device_config(DEV_DEBUGCON, optarg);
2625 break;
d63d307f
FB
2626 case QEMU_OPTION_loadvm:
2627 loadvm = optarg;
2628 break;
2629 case QEMU_OPTION_full_screen:
2630 full_screen = 1;
2631 break;
667accab 2632#ifdef CONFIG_SDL
43523e93
TS
2633 case QEMU_OPTION_no_frame:
2634 no_frame = 1;
2635 break;
3780e197
TS
2636 case QEMU_OPTION_alt_grab:
2637 alt_grab = 1;
2638 break;
0ca9f8a4
DK
2639 case QEMU_OPTION_ctrl_grab:
2640 ctrl_grab = 1;
2641 break;
667accab
TS
2642 case QEMU_OPTION_no_quit:
2643 no_quit = 1;
2644 break;
7d957bd8 2645 case QEMU_OPTION_sdl:
993fbfdb 2646 display_type = DT_SDL;
7d957bd8 2647 break;
58fc096c
JS
2648#else
2649 case QEMU_OPTION_no_frame:
2650 case QEMU_OPTION_alt_grab:
2651 case QEMU_OPTION_ctrl_grab:
2652 case QEMU_OPTION_no_quit:
2653 case QEMU_OPTION_sdl:
2654 fprintf(stderr, "SDL support is disabled\n");
2655 exit(1);
667accab 2656#endif
f7cce898 2657 case QEMU_OPTION_pidfile:
93815bc2 2658 pid_file = optarg;
f7cce898 2659 break;
a09db21f
FB
2660 case QEMU_OPTION_win2k_hack:
2661 win2k_install_hack = 1;
2662 break;
73822ec8
AL
2663 case QEMU_OPTION_rtc_td_hack:
2664 rtc_td_hack = 1;
2665 break;
8a92ea2f 2666 case QEMU_OPTION_acpitable:
de06f8d1 2667 do_acpitable_option(optarg);
8a92ea2f 2668 break;
b6f6e3d3 2669 case QEMU_OPTION_smbios:
de06f8d1 2670 do_smbios_option(optarg);
b6f6e3d3 2671 break;
7ba1e619 2672 case QEMU_OPTION_enable_kvm:
303d4e86
AP
2673 olist = qemu_find_opts("machine");
2674 qemu_opts_reset(olist);
2675 qemu_opts_parse(olist, "accel=kvm", 0);
2676 break;
2677 case QEMU_OPTION_machine:
2678 olist = qemu_find_opts("machine");
2679 qemu_opts_reset(olist);
2680 opts = qemu_opts_parse(olist, optarg, 0);
2681 if (!opts) {
2682 fprintf(stderr, "parse error: %s\n", optarg);
2683 exit(1);
2684 }
7ba1e619 2685 break;
bb36d470
FB
2686 case QEMU_OPTION_usb:
2687 usb_enabled = 1;
2688 break;
a594cfbf
FB
2689 case QEMU_OPTION_usbdevice:
2690 usb_enabled = 1;
bd3c948d
GH
2691 add_device_config(DEV_USB, optarg);
2692 break;
2693 case QEMU_OPTION_device:
3329f07b 2694 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2695 exit(1);
2696 }
a594cfbf 2697 break;
6a00d601 2698 case QEMU_OPTION_smp:
dc6b1c09 2699 smp_parse(optarg);
b2097003 2700 if (smp_cpus < 1) {
6a00d601
FB
2701 fprintf(stderr, "Invalid number of CPUs\n");
2702 exit(1);
2703 }
6be68d7e
JS
2704 if (max_cpus < smp_cpus) {
2705 fprintf(stderr, "maxcpus must be equal to or greater than "
2706 "smp\n");
2707 exit(1);
2708 }
2709 if (max_cpus > 255) {
2710 fprintf(stderr, "Unsupported number of maxcpus\n");
2711 exit(1);
2712 }
6a00d601 2713 break;
24236869 2714 case QEMU_OPTION_vnc:
821601ea 2715#ifdef CONFIG_VNC
6b62dc2d 2716 display_remote++;
821601ea
JS
2717 vnc_display = optarg;
2718#else
2719 fprintf(stderr, "VNC support is disabled\n");
2720 exit(1);
2721#endif
2722 break;
6515b203
FB
2723 case QEMU_OPTION_no_acpi:
2724 acpi_enabled = 0;
2725 break;
16b29ae1
AL
2726 case QEMU_OPTION_no_hpet:
2727 no_hpet = 1;
2728 break;
7d4c3d53
MA
2729 case QEMU_OPTION_balloon:
2730 if (balloon_parse(optarg) < 0) {
2731 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2732 exit(1);
2733 }
df97b920 2734 break;
d1beab82
FB
2735 case QEMU_OPTION_no_reboot:
2736 no_reboot = 1;
2737 break;
b2f76161
AJ
2738 case QEMU_OPTION_no_shutdown:
2739 no_shutdown = 1;
2740 break;
9467cd46
AZ
2741 case QEMU_OPTION_show_cursor:
2742 cursor_hide = 0;
2743 break;
8fcb1b90
BS
2744 case QEMU_OPTION_uuid:
2745 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2746 fprintf(stderr, "Fail to parse UUID string."
2747 " Wrong format.\n");
2748 exit(1);
2749 }
2750 break;
9ae02555
TS
2751 case QEMU_OPTION_option_rom:
2752 if (nb_option_roms >= MAX_OPTION_ROMS) {
2753 fprintf(stderr, "Too many option ROMs\n");
2754 exit(1);
2755 }
2e55e842
GN
2756 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2757 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2758 option_rom[nb_option_roms].bootindex =
2759 qemu_opt_get_number(opts, "bootindex", -1);
2760 if (!option_rom[nb_option_roms].name) {
2761 fprintf(stderr, "Option ROM file is not specified\n");
2762 exit(1);
2763 }
9ae02555
TS
2764 nb_option_roms++;
2765 break;
8e71621f
PB
2766 case QEMU_OPTION_semihosting:
2767 semihosting_enabled = 1;
2768 break;
c35734b2 2769 case QEMU_OPTION_name:
1889465a
AK
2770 qemu_name = qemu_strdup(optarg);
2771 {
2772 char *p = strchr(qemu_name, ',');
2773 if (p != NULL) {
2774 *p++ = 0;
2775 if (strncmp(p, "process=", 8)) {
5697f6ae 2776 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
2777 exit(1);
2778 }
2779 p += 8;
ce798cf2 2780 os_set_proc_name(p);
1889465a
AK
2781 }
2782 }
c35734b2 2783 break;
66508601
BS
2784 case QEMU_OPTION_prom_env:
2785 if (nb_prom_envs >= MAX_PROM_ENVS) {
2786 fprintf(stderr, "Too many prom variables\n");
2787 exit(1);
2788 }
2789 prom_envs[nb_prom_envs] = optarg;
2790 nb_prom_envs++;
2791 break;
2b8f2d41
AZ
2792 case QEMU_OPTION_old_param:
2793 old_param = 1;
05ebd537 2794 break;
f3dcfada
TS
2795 case QEMU_OPTION_clock:
2796 configure_alarms(optarg);
2797 break;
7e0af5d0 2798 case QEMU_OPTION_startdate:
1ed2fc1f
JK
2799 configure_rtc_date_offset(optarg, 1);
2800 break;
2801 case QEMU_OPTION_rtc:
3329f07b 2802 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 2803 if (!opts) {
1ed2fc1f 2804 exit(1);
7e0af5d0 2805 }
1ed2fc1f 2806 configure_rtc(opts);
7e0af5d0 2807 break;
26a5f13b
FB
2808 case QEMU_OPTION_tb_size:
2809 tb_size = strtol(optarg, NULL, 0);
2810 if (tb_size < 0)
2811 tb_size = 0;
2812 break;
2e70f6ef 2813 case QEMU_OPTION_icount:
4e3de9e9 2814 icount_option = optarg;
2e70f6ef 2815 break;
5bb7910a
AL
2816 case QEMU_OPTION_incoming:
2817 incoming = optarg;
8e84865e 2818 incoming_expected = true;
5bb7910a 2819 break;
d8c208dd
GH
2820 case QEMU_OPTION_nodefaults:
2821 default_serial = 0;
2822 default_parallel = 0;
aee1b935 2823 default_virtcon = 0;
d8c208dd
GH
2824 default_monitor = 0;
2825 default_vga = 0;
cb4522cc 2826 default_net = 0;
ac33f8fa
GH
2827 default_floppy = 0;
2828 default_cdrom = 0;
2829 default_sdcard = 0;
d8c208dd 2830 break;
e37630ca 2831 case QEMU_OPTION_xen_domid:
ad96090a
BS
2832 if (!(xen_available())) {
2833 printf("Option %s not supported for this target\n", popt->name);
2834 exit(1);
2835 }
e37630ca
AL
2836 xen_domid = atoi(optarg);
2837 break;
2838 case QEMU_OPTION_xen_create:
ad96090a
BS
2839 if (!(xen_available())) {
2840 printf("Option %s not supported for this target\n", popt->name);
2841 exit(1);
2842 }
e37630ca
AL
2843 xen_mode = XEN_CREATE;
2844 break;
2845 case QEMU_OPTION_xen_attach:
ad96090a
BS
2846 if (!(xen_available())) {
2847 printf("Option %s not supported for this target\n", popt->name);
2848 exit(1);
2849 }
e37630ca
AL
2850 xen_mode = XEN_ATTACH;
2851 break;
ab6540d5
PS
2852#ifdef CONFIG_SIMPLE_TRACE
2853 case QEMU_OPTION_trace:
2854 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2855 if (opts) {
2856 trace_file = qemu_opt_get(opts, "file");
2857 }
2858 break;
2859#endif
715a664a
GH
2860 case QEMU_OPTION_readconfig:
2861 {
dcfb0939
KW
2862 int ret = qemu_read_config_file(optarg);
2863 if (ret < 0) {
2864 fprintf(stderr, "read config %s: %s\n", optarg,
2865 strerror(-ret));
715a664a
GH
2866 exit(1);
2867 }
715a664a
GH
2868 break;
2869 }
29b0040b
GH
2870 case QEMU_OPTION_spice:
2871 olist = qemu_find_opts("spice");
2872 if (!olist) {
2873 fprintf(stderr, "spice is not supported by this qemu build.\n");
2874 exit(1);
2875 }
2876 opts = qemu_opts_parse(olist, optarg, 0);
2877 if (!opts) {
2878 fprintf(stderr, "parse error: %s\n", optarg);
2879 exit(1);
2880 }
2881 break;
715a664a
GH
2882 case QEMU_OPTION_writeconfig:
2883 {
2884 FILE *fp;
2885 if (strcmp(optarg, "-") == 0) {
2886 fp = stdout;
2887 } else {
2888 fp = fopen(optarg, "w");
2889 if (fp == NULL) {
2890 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2891 exit(1);
2892 }
2893 }
2894 qemu_config_write(fp);
2895 fclose(fp);
2896 break;
2897 }
59a5264b
JS
2898 default:
2899 os_parse_cmd_args(popt->index, optarg);
cd6f1169 2900 }
0824d6fc
FB
2901 }
2902 }
0f0bc3f1 2903 loc_set_none();
330d0414 2904
31d3c9b8
SH
2905 if (!st_init(trace_file)) {
2906 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2907 }
0b5538c3 2908
5cea8590
PB
2909 /* If no data_dir is specified then try to find it relative to the
2910 executable path. */
2911 if (!data_dir) {
6170540b 2912 data_dir = os_find_datadir(argv[0]);
5cea8590
PB
2913 }
2914 /* If all else fails use the install patch specified when building. */
2915 if (!data_dir) {
1dabe05c 2916 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
2917 }
2918
6be68d7e
JS
2919 /*
2920 * Default to max_cpus = smp_cpus, in case the user doesn't
2921 * specify a max_cpus value.
2922 */
2923 if (!max_cpus)
2924 max_cpus = smp_cpus;
2925
3d878caa 2926 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
2927 if (smp_cpus > machine->max_cpus) {
2928 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2929 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2930 machine->max_cpus);
2931 exit(1);
2932 }
2933
67b724e6
AP
2934 /*
2935 * Get the default machine options from the machine if it is not already
2936 * specified either by the configuration file or by the command line.
2937 */
2938 if (machine->default_machine_opts) {
2939 QemuOptsList *list = qemu_find_opts("machine");
2940 const char *p = NULL;
2941
2942 if (!QTAILQ_EMPTY(&list->head)) {
2943 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2944 }
2945 if (p == NULL) {
2946 opts = qemu_opts_parse(qemu_find_opts("machine"),
2947 machine->default_machine_opts, 0);
2948 if (!opts) {
2949 fprintf(stderr, "parse error for machine %s: %s\n",
2950 machine->name, machine->default_machine_opts);
2951 exit(1);
2952 }
2953 }
2954 }
2955
3329f07b
GH
2956 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2957 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 2958
986c5f78
GH
2959 if (machine->no_serial) {
2960 default_serial = 0;
2961 }
2962 if (machine->no_parallel) {
2963 default_parallel = 0;
2964 }
2965 if (!machine->use_virtcon) {
2966 default_virtcon = 0;
2967 }
2968 if (machine->no_vga) {
2969 default_vga = 0;
2970 }
ac33f8fa
GH
2971 if (machine->no_floppy) {
2972 default_floppy = 0;
2973 }
2974 if (machine->no_cdrom) {
2975 default_cdrom = 0;
2976 }
2977 if (machine->no_sdcard) {
2978 default_sdcard = 0;
2979 }
986c5f78 2980
993fbfdb 2981 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
2982 if (default_parallel)
2983 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
2984 if (default_serial && default_monitor) {
2985 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
2986 } else if (default_virtcon && default_monitor) {
2987 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
2988 } else {
2989 if (default_serial)
2990 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
2991 if (default_virtcon)
2992 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 2993 if (default_monitor)
6ca5582d 2994 monitor_parse("stdio", "readline");
e1c09175 2995 }
998bbd74
GH
2996 } else {
2997 if (default_serial)
2998 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
2999 if (default_parallel)
3000 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 3001 if (default_monitor)
6ca5582d 3002 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
3003 if (default_virtcon)
3004 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 3005 }
64465297
GH
3006 if (default_vga)
3007 vga_interface_type = VGA_CIRRUS;
bc0129d9 3008
a5829fd9
T
3009 socket_init();
3010
3329f07b 3011 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 3012 exit(1);
758e8e38 3013#ifdef CONFIG_VIRTFS
3329f07b 3014 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
3015 exit(1);
3016 }
3017#endif
1a688d3b 3018
eb505be1 3019 os_daemonize();
71e3ceb8 3020
aa26bb2d 3021 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 3022 os_pidfile_error();
93815bc2
TS
3023 exit(1);
3024 }
3025
303d4e86 3026 configure_accelerator();
214910a7 3027
3fcf7b6b
AL
3028 if (qemu_init_main_loop()) {
3029 fprintf(stderr, "qemu_init_main_loop failed\n");
3030 exit(1);
3031 }
a20dd508 3032 linux_boot = (kernel_filename != NULL);
6c41b272 3033
f8d39c01
TS
3034 if (!linux_boot && *kernel_cmdline != '\0') {
3035 fprintf(stderr, "-append only allowed with -kernel option\n");
3036 exit(1);
3037 }
3038
3039 if (!linux_boot && initrd_filename != NULL) {
3040 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3041 exit(1);
3042 }
3043
9156d763 3044 os_set_line_buffering();
3b46e624 3045
7183b4b4
AL
3046 if (init_timer_alarm() < 0) {
3047 fprintf(stderr, "could not initialize alarm timer\n");
3048 exit(1);
3049 }
4e3de9e9 3050 configure_icount(icount_option);
634fce96 3051
dc1c9fe8
MM
3052 if (net_init_clients() < 0) {
3053 exit(1);
702c651c 3054 }
f1510b2c 3055
dc72ac14 3056 /* init the bluetooth world */
bd3c948d
GH
3057 if (foreach_device_config(DEV_BT, bt_parse))
3058 exit(1);
dc72ac14 3059
0824d6fc 3060 /* init the memory */
94a6b54f
PB
3061 if (ram_size == 0)
3062 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 3063
26a5f13b
FB
3064 /* init the dynamic translator */
3065 cpu_exec_init_all(tb_size * 1024 * 1024);
3066
eb852011 3067 bdrv_init_with_whitelist();
c4b1fcc0 3068
c163b5ca 3069 blk_mig_init();
3070
e4bcb14c 3071 /* open the virtual block devices */
9dfd7c7a 3072 if (snapshot)
3329f07b
GH
3073 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3074 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 3075 exit(1);
3e3d5815 3076
4e5d9b57
MA
3077 default_drive(default_cdrom, snapshot, machine->use_scsi,
3078 IF_DEFAULT, 2, CDROM_OPTS);
3079 default_drive(default_floppy, snapshot, machine->use_scsi,
3080 IF_FLOPPY, 0, FD_OPTS);
3081 default_drive(default_sdcard, snapshot, machine->use_scsi,
3082 IF_SD, 0, SD_OPTS);
3083
97ab12d4 3084 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
c163b5ca 3085 ram_load, NULL);
8a7ddc38 3086
268a362c
AL
3087 if (nb_numa_nodes > 0) {
3088 int i;
3089
3090 if (nb_numa_nodes > smp_cpus) {
3091 nb_numa_nodes = smp_cpus;
3092 }
3093
3094 /* If no memory size if given for any node, assume the default case
3095 * and distribute the available memory equally across all nodes
3096 */
3097 for (i = 0; i < nb_numa_nodes; i++) {
3098 if (node_mem[i] != 0)
3099 break;
3100 }
3101 if (i == nb_numa_nodes) {
3102 uint64_t usedmem = 0;
3103
3104 /* On Linux, the each node's border has to be 8MB aligned,
3105 * the final node gets the rest.
3106 */
3107 for (i = 0; i < nb_numa_nodes - 1; i++) {
3108 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3109 usedmem += node_mem[i];
3110 }
3111 node_mem[i] = ram_size - usedmem;
3112 }
3113
3114 for (i = 0; i < nb_numa_nodes; i++) {
3115 if (node_cpumask[i] != 0)
3116 break;
3117 }
3118 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3119 * must cope with this anyway, because there are BIOSes out there in
3120 * real machines which also use this scheme.
3121 */
3122 if (i == nb_numa_nodes) {
3123 for (i = 0; i < smp_cpus; i++) {
3124 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3125 }
3126 }
3127 }
3128
3329f07b 3129 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
3130 exit(1);
3131 }
3132
998bbd74
GH
3133 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3134 exit(1);
6a5e8b0e
GH
3135 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3136 exit(1);
aee1b935
GH
3137 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3138 exit(1);
c9f398e5
PA
3139 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3140 exit(1);
2796dae0 3141
aae9460e
PB
3142 module_call_init(MODULE_INIT_DEVICE);
3143
3329f07b 3144 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
3145 exit(0);
3146
09aaa160
MA
3147 if (watchdog) {
3148 i = select_watchdog(watchdog);
3149 if (i > 0)
3150 exit (i == 1 ? 1 : 0);
3151 }
3152
b6b61144 3153 if (machine->compat_props) {
458fb679 3154 qdev_prop_register_global_list(machine->compat_props);
b6b61144 3155 }
d0fef6fb
GH
3156 qemu_add_globals();
3157
fbe1b595 3158 machine->init(ram_size, boot_devices,
3023f332
AL
3159 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3160
ea375f9a 3161 cpu_synchronize_all_post_init();
268a362c 3162
87d0a28e 3163 set_numa_modes();
268a362c 3164
6f338c34
AL
3165 current_machine = machine;
3166
3023f332
AL
3167 /* init USB devices */
3168 if (usb_enabled) {
0752706d
MA
3169 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3170 exit(1);
3023f332
AL
3171 }
3172
bd3c948d 3173 /* init generic devices */
3329f07b 3174 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3175 exit(1);
3176
668680f7
MA
3177 net_check_clients();
3178
3023f332 3179 /* just use the first displaystate for the moment */
b473df6e 3180 ds = get_displaystate();
993fbfdb 3181
a3e22260
GH
3182 if (using_spice)
3183 display_remote++;
6b62dc2d 3184 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3185#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3186 display_type = DT_SDL;
821601ea 3187#elif defined(CONFIG_VNC)
993fbfdb
AL
3188 vnc_display = "localhost:0,to=99";
3189 show_vnc_port = 1;
821601ea
JS
3190#else
3191 display_type = DT_NONE;
993fbfdb
AL
3192#endif
3193 }
821601ea 3194
993fbfdb 3195
6b62dc2d 3196 /* init local displays */
993fbfdb
AL
3197 switch (display_type) {
3198 case DT_NOGRAPHIC:
3199 break;
4d3b6f6e 3200#if defined(CONFIG_CURSES)
993fbfdb
AL
3201 case DT_CURSES:
3202 curses_display_init(ds, full_screen);
3203 break;
4d3b6f6e 3204#endif
5b0753e0 3205#if defined(CONFIG_SDL)
993fbfdb
AL
3206 case DT_SDL:
3207 sdl_display_init(ds, full_screen, no_frame);
3208 break;
5b0753e0 3209#elif defined(CONFIG_COCOA)
993fbfdb
AL
3210 case DT_SDL:
3211 cocoa_display_init(ds, full_screen);
3212 break;
313aa567 3213#endif
6b62dc2d
GH
3214 default:
3215 break;
3216 }
3217
0ce235a7
GN
3218 /* must be after terminal init, SDL library changes signal handlers */
3219 os_setup_signal_handling();
3220
821601ea 3221#ifdef CONFIG_VNC
6b62dc2d
GH
3222 /* init remote displays */
3223 if (vnc_display) {
993fbfdb
AL
3224 vnc_display_init(ds);
3225 if (vnc_display_open(ds, vnc_display) < 0)
3226 exit(1);
f92f8afe 3227
993fbfdb
AL
3228 if (show_vnc_port) {
3229 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3230 }
313aa567 3231 }
821601ea 3232#endif
a3e22260 3233#ifdef CONFIG_SPICE
a19cbfb3 3234 if (using_spice && !qxl_enabled) {
a3e22260
GH
3235 qemu_spice_display_init(ds);
3236 }
3237#endif
5b08fc10 3238
6b62dc2d
GH
3239 /* display setup */
3240 dpy_resize(ds);
3023f332
AL
3241 dcl = ds->listeners;
3242 while (dcl != NULL) {
3243 if (dcl->dpy_refresh != NULL) {
7bd427d8
PB
3244 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3245 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
0f2ad63f 3246 break;
20d8a3ed 3247 }
3023f332 3248 dcl = dcl->next;
20d8a3ed 3249 }
6b62dc2d 3250 if (ds->gui_timer == NULL) {
7bd427d8
PB
3251 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3252 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
9043b62d 3253 }
b473df6e 3254 text_consoles_set_display(ds);
2796dae0 3255
59030a8c
AL
3256 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3257 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3258 gdbstub_dev);
3259 exit(1);
45669e00 3260 }
45669e00 3261
3418bd25
GH
3262 qdev_machine_creation_done();
3263
15ff7705
GH
3264 if (rom_load_all() != 0) {
3265 fprintf(stderr, "rom loading failed\n");
3266 exit(1);
3267 }
45a50b16 3268
80376c3f
IY
3269 /* TODO: once all bus devices are qdevified, this should be done
3270 * when bus is created by qdev.c */
3271 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3272 qemu_run_machine_init_done_notifiers();
3273
504c2948 3274 qemu_system_reset();
05f2401e 3275 if (loadvm) {
03cd4655 3276 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3277 autostart = 0;
3278 }
3279 }
d63d307f 3280
2bb8c10c 3281 if (incoming) {
8ca5e801
JQ
3282 int ret = qemu_start_incoming_migration(incoming);
3283 if (ret < 0) {
3284 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3285 incoming, ret);
3286 exit(ret);
3287 }
6b99dadc 3288 } else if (autostart) {
c0f4ce77 3289 vm_start();
6b99dadc 3290 }
ffd843bc 3291
eb505be1 3292 os_setup_post();
71e3ceb8 3293
8a7ddc38 3294 main_loop();
40c3bac3 3295 quit_timers();
63a01ef8 3296 net_cleanup();
b46a8906 3297
0824d6fc
FB
3298 return 0;
3299}