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