]> git.proxmox.com Git - qemu.git/blame - vl.c
mips_r4k: convert to memory API
[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 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 302static void res_free(void)
303{
304 if (boot_splash_filedata != NULL) {
7267c094 305 g_free(boot_splash_filedata);
3d3b8303 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;
d9389b96 1299 no_shutdown = 0;
f64622c4
GN
1300 qemu_system_shutdown_request();
1301}
1302
bb0c6722
FB
1303void qemu_system_shutdown_request(void)
1304{
1305 shutdown_requested = 1;
d9f75a4e 1306 qemu_notify_event();
bb0c6722
FB
1307}
1308
3475187d
FB
1309void qemu_system_powerdown_request(void)
1310{
1311 powerdown_requested = 1;
d9f75a4e
AL
1312 qemu_notify_event();
1313}
1314
8cf71710
JK
1315void qemu_system_debug_request(void)
1316{
1317 debug_requested = 1;
83f338f7 1318 qemu_notify_event();
8cf71710
JK
1319}
1320
1321void qemu_system_vmstop_request(int reason)
1322{
1323 vmstop_requested = reason;
1324 qemu_notify_event();
1325}
1326
69e5bb68
AL
1327static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1328static int n_poll_fds;
1329static int max_priority;
1330
1331static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1332 fd_set *xfds, struct timeval *tv)
1333{
1334 GMainContext *context = g_main_context_default();
1335 int i;
1336 int timeout = 0, cur_timeout;
1337
1338 g_main_context_prepare(context, &max_priority);
1339
1340 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1341 poll_fds, ARRAY_SIZE(poll_fds));
1342 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1343
1344 for (i = 0; i < n_poll_fds; i++) {
1345 GPollFD *p = &poll_fds[i];
1346
1347 if ((p->events & G_IO_IN)) {
1348 FD_SET(p->fd, rfds);
1349 *max_fd = MAX(*max_fd, p->fd);
1350 }
1351 if ((p->events & G_IO_OUT)) {
1352 FD_SET(p->fd, wfds);
1353 *max_fd = MAX(*max_fd, p->fd);
1354 }
1355 if ((p->events & G_IO_ERR)) {
1356 FD_SET(p->fd, xfds);
1357 *max_fd = MAX(*max_fd, p->fd);
1358 }
1359 }
1360
1361 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1362 if (timeout >= 0 && timeout < cur_timeout) {
1363 tv->tv_sec = timeout / 1000;
1364 tv->tv_usec = (timeout % 1000) * 1000;
1365 }
1366}
1367
1368static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1369 bool err)
1370{
1371 GMainContext *context = g_main_context_default();
1372
1373 if (!err) {
1374 int i;
1375
1376 for (i = 0; i < n_poll_fds; i++) {
1377 GPollFD *p = &poll_fds[i];
1378
1379 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1380 p->revents |= G_IO_IN;
1381 }
1382 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1383 p->revents |= G_IO_OUT;
1384 }
1385 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1386 p->revents |= G_IO_ERR;
1387 }
1388 }
1389 }
1390
1391 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1392 g_main_context_dispatch(context);
1393 }
1394}
1395
c9f711a5 1396int main_loop_wait(int nonblocking)
56f3a5d0 1397{
56f3a5d0
AL
1398 fd_set rfds, wfds, xfds;
1399 int ret, nfds;
1400 struct timeval tv;
d6f4ade2 1401 int timeout;
56f3a5d0 1402
d6f4ade2
PB
1403 if (nonblocking)
1404 timeout = 0;
1405 else {
1406 timeout = qemu_calculate_timeout();
1407 qemu_bh_update_timeout(&timeout);
1408 }
56f3a5d0 1409
0d93ca7c 1410 os_host_main_loop_wait(&timeout);
56f3a5d0 1411
02981419
PB
1412 tv.tv_sec = timeout / 1000;
1413 tv.tv_usec = (timeout % 1000) * 1000;
1414
fd1dff4b
FB
1415 /* poll any events */
1416 /* XXX: separate device handlers from system ones */
6abfbd79 1417 nfds = -1;
fd1dff4b
FB
1418 FD_ZERO(&rfds);
1419 FD_ZERO(&wfds);
e035649e 1420 FD_ZERO(&xfds);
69e5bb68 1421
02981419 1422 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
d918f23e 1423 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
69e5bb68 1424 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
d918f23e 1425
200668ba
JK
1426 if (timeout > 0) {
1427 qemu_mutex_unlock_iothread();
1428 }
1429
e035649e 1430 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
200668ba
JK
1431
1432 if (timeout > 0) {
1433 qemu_mutex_lock_iothread();
1434 }
d918f23e 1435
02981419 1436 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
d918f23e 1437 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
69e5bb68 1438 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 1439
b6964827 1440 qemu_run_all_timers();
21d5d12b 1441
423f0742
PB
1442 /* Check bottom-halves last in case any of the earlier events triggered
1443 them. */
1444 qemu_bh_poll();
3b46e624 1445
c9f711a5 1446 return ret;
5905b2e5
FB
1447}
1448
d9c32310
BS
1449qemu_irq qemu_system_powerdown;
1450
43b96858
AL
1451static void main_loop(void)
1452{
c9f711a5
JK
1453 bool nonblocking;
1454 int last_io __attribute__ ((unused)) = 0;
8e1b90ec
JK
1455#ifdef CONFIG_PROFILER
1456 int64_t ti;
1457#endif
6e29f5da 1458 int r;
e6e35b1e 1459
7277e027 1460 qemu_main_loop_start();
d6dc3d42 1461
6e29f5da 1462 for (;;) {
c9f711a5 1463 nonblocking = !kvm_enabled() && last_io > 0;
89bfc105 1464#ifdef CONFIG_PROFILER
46481d39 1465 ti = profile_getclock();
89bfc105 1466#endif
c9f711a5 1467 last_io = main_loop_wait(nonblocking);
89bfc105 1468#ifdef CONFIG_PROFILER
46481d39 1469 dev_time += profile_getclock() - ti;
89bfc105 1470#endif
43b96858 1471
8cf71710
JK
1472 if (qemu_debug_requested()) {
1473 vm_stop(VMSTOP_DEBUG);
b1a15e7e 1474 }
43b96858 1475 if (qemu_shutdown_requested()) {
f64622c4 1476 qemu_kill_report();
242cd003 1477 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858 1478 if (no_shutdown) {
e07bbac5 1479 vm_stop(VMSTOP_SHUTDOWN);
43b96858
AL
1480 } else
1481 break;
1482 }
d6dc3d42
AL
1483 if (qemu_reset_requested()) {
1484 pause_all_vcpus();
a7ada151 1485 cpu_synchronize_all_states();
e063eb1f 1486 qemu_system_reset(VMRESET_REPORT);
d6dc3d42
AL
1487 resume_all_vcpus();
1488 }
d9c32310 1489 if (qemu_powerdown_requested()) {
242cd003 1490 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
1491 qemu_irq_raise(qemu_system_powerdown);
1492 }
b1a15e7e 1493 if ((r = qemu_vmstop_requested())) {
6e29f5da 1494 vm_stop(r);
b1a15e7e 1495 }
b4608c04 1496 }
2bc93fed 1497 bdrv_close_all();
d6dc3d42 1498 pause_all_vcpus();
b4608c04
FB
1499}
1500
9bd7e6d9
PB
1501static void version(void)
1502{
f75ca1ae 1503 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1504}
1505
15f82208 1506static void help(int exitcode)
0824d6fc 1507{
e8105ebb 1508 const char *options_help =
ad96090a
BS
1509#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1510 opt_help
5824d651 1511#define DEFHEADING(text) stringify(text) "\n"
9f16732a 1512#include "qemu-options.def"
5824d651
BS
1513#undef DEF
1514#undef DEFHEADING
1515#undef GEN_DOCS
e8105ebb
PB
1516 ;
1517 version();
1518 printf("usage: %s [options] [disk_image]\n"
1519 "\n"
f75ca1ae 1520 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
3f020d70 1521 "\n"
e8105ebb 1522 "%s\n"
3f020d70 1523 "During emulation, the following keys are useful:\n"
1524 "ctrl-alt-f toggle full screen\n"
1525 "ctrl-alt-n switch to virtual console 'n'\n"
1526 "ctrl-alt toggle mouse and keyboard grab\n"
1527 "\n"
e8105ebb
PB
1528 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1529 "qemu",
1530 options_help);
15f82208 1531 exit(exitcode);
0824d6fc
FB
1532}
1533
cd6f1169
FB
1534#define HAS_ARG 0x0001
1535
cd6f1169
FB
1536typedef struct QEMUOption {
1537 const char *name;
1538 int flags;
1539 int index;
ad96090a 1540 uint32_t arch_mask;
cd6f1169
FB
1541} QEMUOption;
1542
dbed7e40 1543static const QEMUOption qemu_options[] = {
ad96090a
BS
1544 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1545#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1546 { option, opt_arg, opt_enum, arch_mask },
5824d651 1547#define DEFHEADING(text)
9f16732a 1548#include "qemu-options.def"
5824d651
BS
1549#undef DEF
1550#undef DEFHEADING
1551#undef GEN_DOCS
cd6f1169 1552 { NULL },
fc01f7e7 1553};
3893c124 1554static void select_vgahw (const char *p)
1555{
1556 const char *opts;
1557
64465297 1558 default_vga = 0;
86176759 1559 vga_interface_type = VGA_NONE;
3893c124 1560 if (strstart(p, "std", &opts)) {
86176759 1561 vga_interface_type = VGA_STD;
3893c124 1562 } else if (strstart(p, "cirrus", &opts)) {
86176759 1563 vga_interface_type = VGA_CIRRUS;
3893c124 1564 } else if (strstart(p, "vmware", &opts)) {
86176759 1565 vga_interface_type = VGA_VMWARE;
94909d9f 1566 } else if (strstart(p, "xenfb", &opts)) {
86176759 1567 vga_interface_type = VGA_XENFB;
a19cbfb3
GH
1568 } else if (strstart(p, "qxl", &opts)) {
1569 vga_interface_type = VGA_QXL;
28b85ed8 1570 } else if (!strstart(p, "none", &opts)) {
3893c124 1571 invalid_vga:
1572 fprintf(stderr, "Unknown vga type: %s\n", p);
1573 exit(1);
1574 }
cb5a7aa8 1575 while (*opts) {
1576 const char *nextopt;
1577
1578 if (strstart(opts, ",retrace=", &nextopt)) {
1579 opts = nextopt;
1580 if (strstart(opts, "dumb", &nextopt))
1581 vga_retrace_method = VGA_RETRACE_DUMB;
1582 else if (strstart(opts, "precise", &nextopt))
1583 vga_retrace_method = VGA_RETRACE_PRECISE;
1584 else goto invalid_vga;
1585 } else goto invalid_vga;
1586 opts = nextopt;
1587 }
3893c124 1588}
1589
1472a95b
JS
1590static DisplayType select_display(const char *p)
1591{
1592 const char *opts;
1593 DisplayType display = DT_DEFAULT;
1594
1595 if (strstart(p, "sdl", &opts)) {
1596#ifdef CONFIG_SDL
1597 display = DT_SDL;
1598 while (*opts) {
1599 const char *nextopt;
1600
1601 if (strstart(opts, ",frame=", &nextopt)) {
1602 opts = nextopt;
1603 if (strstart(opts, "on", &nextopt)) {
1604 no_frame = 0;
1605 } else if (strstart(opts, "off", &nextopt)) {
1606 no_frame = 1;
1607 } else {
05175535 1608 goto invalid_sdl_args;
1472a95b
JS
1609 }
1610 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1611 opts = nextopt;
1612 if (strstart(opts, "on", &nextopt)) {
1613 alt_grab = 1;
1614 } else if (strstart(opts, "off", &nextopt)) {
1615 alt_grab = 0;
1616 } else {
05175535 1617 goto invalid_sdl_args;
1472a95b
JS
1618 }
1619 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1620 opts = nextopt;
1621 if (strstart(opts, "on", &nextopt)) {
1622 ctrl_grab = 1;
1623 } else if (strstart(opts, "off", &nextopt)) {
1624 ctrl_grab = 0;
1625 } else {
05175535 1626 goto invalid_sdl_args;
1472a95b
JS
1627 }
1628 } else if (strstart(opts, ",window_close=", &nextopt)) {
1629 opts = nextopt;
1630 if (strstart(opts, "on", &nextopt)) {
1631 no_quit = 0;
1632 } else if (strstart(opts, "off", &nextopt)) {
1633 no_quit = 1;
1634 } else {
05175535 1635 goto invalid_sdl_args;
1472a95b
JS
1636 }
1637 } else {
05175535
PM
1638 invalid_sdl_args:
1639 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1640 exit(1);
1472a95b
JS
1641 }
1642 opts = nextopt;
1643 }
1644#else
1645 fprintf(stderr, "SDL support is disabled\n");
1646 exit(1);
1647#endif
3264ff12 1648 } else if (strstart(p, "vnc", &opts)) {
821601ea 1649#ifdef CONFIG_VNC
3264ff12
JS
1650 display_remote++;
1651
1652 if (*opts) {
1653 const char *nextopt;
1654
1655 if (strstart(opts, "=", &nextopt)) {
1656 vnc_display = nextopt;
1657 }
1658 }
1659 if (!vnc_display) {
1660 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1661 exit(1);
1662 }
821601ea
JS
1663#else
1664 fprintf(stderr, "VNC support is disabled\n");
1665 exit(1);
1666#endif
1472a95b
JS
1667 } else if (strstart(p, "curses", &opts)) {
1668#ifdef CONFIG_CURSES
1669 display = DT_CURSES;
1670#else
1671 fprintf(stderr, "Curses support is disabled\n");
1672 exit(1);
1673#endif
4171d32e
JS
1674 } else if (strstart(p, "none", &opts)) {
1675 display = DT_NONE;
1472a95b 1676 } else {
1472a95b
JS
1677 fprintf(stderr, "Unknown display type: %s\n", p);
1678 exit(1);
1679 }
1680
1681 return display;
1682}
1683
7d4c3d53
MA
1684static int balloon_parse(const char *arg)
1685{
382f0743 1686 QemuOpts *opts;
7d4c3d53 1687
382f0743
GH
1688 if (strcmp(arg, "none") == 0) {
1689 return 0;
1690 }
1691
1692 if (!strncmp(arg, "virtio", 6)) {
1693 if (arg[6] == ',') {
1694 /* have params -> parse them */
3329f07b 1695 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1696 if (!opts)
1697 return -1;
1698 } else {
1699 /* create empty opts */
3329f07b 1700 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
7d4c3d53 1701 }
29f82b37 1702 qemu_opt_set(opts, "driver", "virtio-balloon");
382f0743 1703 return 0;
7d4c3d53 1704 }
382f0743
GH
1705
1706 return -1;
7d4c3d53 1707}
7d4c3d53 1708
5cea8590
PB
1709char *qemu_find_file(int type, const char *name)
1710{
1711 int len;
1712 const char *subdir;
1713 char *buf;
1714
1715 /* If name contains path separators then try it as a straight path. */
1716 if ((strchr(name, '/') || strchr(name, '\\'))
1717 && access(name, R_OK) == 0) {
7267c094 1718 return g_strdup(name);
5cea8590
PB
1719 }
1720 switch (type) {
1721 case QEMU_FILE_TYPE_BIOS:
1722 subdir = "";
1723 break;
1724 case QEMU_FILE_TYPE_KEYMAP:
1725 subdir = "keymaps/";
1726 break;
1727 default:
1728 abort();
1729 }
1730 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
7267c094 1731 buf = g_malloc0(len);
3a41759d 1732 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590 1733 if (access(buf, R_OK)) {
7267c094 1734 g_free(buf);
5cea8590
PB
1735 return NULL;
1736 }
1737 return buf;
1738}
1739
ff952ba2
MA
1740static int device_help_func(QemuOpts *opts, void *opaque)
1741{
1742 return qdev_device_help(opts);
1743}
1744
f31d07d1
GH
1745static int device_init_func(QemuOpts *opts, void *opaque)
1746{
1747 DeviceState *dev;
1748
1749 dev = qdev_device_add(opts);
1750 if (!dev)
1751 return -1;
1752 return 0;
1753}
1754
1a688d3b
GH
1755static int chardev_init_func(QemuOpts *opts, void *opaque)
1756{
1757 CharDriverState *chr;
1758
f69554b9 1759 chr = qemu_chr_new_from_opts(opts, NULL);
1a688d3b
GH
1760 if (!chr)
1761 return -1;
1762 return 0;
1763}
1764
758e8e38 1765#ifdef CONFIG_VIRTFS
74db920c
GS
1766static int fsdev_init_func(QemuOpts *opts, void *opaque)
1767{
1768 int ret;
1769 ret = qemu_fsdev_add(opts);
1770
1771 return ret;
1772}
1773#endif
1774
88589343
GH
1775static int mon_init_func(QemuOpts *opts, void *opaque)
1776{
1777 CharDriverState *chr;
1778 const char *chardev;
1779 const char *mode;
1780 int flags;
1781
1782 mode = qemu_opt_get(opts, "mode");
1783 if (mode == NULL) {
1784 mode = "readline";
1785 }
1786 if (strcmp(mode, "readline") == 0) {
1787 flags = MONITOR_USE_READLINE;
1788 } else if (strcmp(mode, "control") == 0) {
1789 flags = MONITOR_USE_CONTROL;
1790 } else {
1791 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1792 exit(1);
1793 }
1794
39eaab9a
DB
1795 if (qemu_opt_get_bool(opts, "pretty", 0))
1796 flags |= MONITOR_USE_PRETTY;
1797
88589343
GH
1798 if (qemu_opt_get_bool(opts, "default", 0))
1799 flags |= MONITOR_IS_DEFAULT;
1800
1801 chardev = qemu_opt_get(opts, "chardev");
1802 chr = qemu_chr_find(chardev);
1803 if (chr == NULL) {
1804 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1805 exit(1);
1806 }
1807
1808 monitor_init(chr, flags);
1809 return 0;
1810}
1811
6ca5582d 1812static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1813{
1814 static int monitor_device_index = 0;
1815 QemuOpts *opts;
1816 const char *p;
1817 char label[32];
1818 int def = 0;
1819
1820 if (strstart(optarg, "chardev:", &p)) {
1821 snprintf(label, sizeof(label), "%s", p);
1822 } else {
140e065d
JK
1823 snprintf(label, sizeof(label), "compat_monitor%d",
1824 monitor_device_index);
1825 if (monitor_device_index == 0) {
88589343
GH
1826 def = 1;
1827 }
1828 opts = qemu_chr_parse_compat(label, optarg);
1829 if (!opts) {
1830 fprintf(stderr, "parse error: %s\n", optarg);
1831 exit(1);
1832 }
1833 }
1834
3329f07b 1835 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
88589343
GH
1836 if (!opts) {
1837 fprintf(stderr, "duplicate chardev: %s\n", label);
1838 exit(1);
1839 }
6ca5582d 1840 qemu_opt_set(opts, "mode", mode);
88589343
GH
1841 qemu_opt_set(opts, "chardev", label);
1842 if (def)
1843 qemu_opt_set(opts, "default", "on");
1844 monitor_device_index++;
1845}
1846
bd3c948d
GH
1847struct device_config {
1848 enum {
aee1b935
GH
1849 DEV_USB, /* -usbdevice */
1850 DEV_BT, /* -bt */
1851 DEV_SERIAL, /* -serial */
1852 DEV_PARALLEL, /* -parallel */
1853 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1854 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
1855 } type;
1856 const char *cmdline;
72cf2d4f 1857 QTAILQ_ENTRY(device_config) next;
bd3c948d 1858};
72cf2d4f 1859QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1860
1861static void add_device_config(int type, const char *cmdline)
1862{
1863 struct device_config *conf;
1864
7267c094 1865 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
1866 conf->type = type;
1867 conf->cmdline = cmdline;
72cf2d4f 1868 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1869}
1870
1871static int foreach_device_config(int type, int (*func)(const char *cmdline))
1872{
1873 struct device_config *conf;
1874 int rc;
1875
72cf2d4f 1876 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1877 if (conf->type != type)
1878 continue;
1879 rc = func(conf->cmdline);
1880 if (0 != rc)
1881 return rc;
1882 }
1883 return 0;
1884}
1885
998bbd74
GH
1886static int serial_parse(const char *devname)
1887{
1888 static int index = 0;
1889 char label[32];
1890
1891 if (strcmp(devname, "none") == 0)
1892 return 0;
1893 if (index == MAX_SERIAL_PORTS) {
1894 fprintf(stderr, "qemu: too many serial ports\n");
1895 exit(1);
1896 }
1897 snprintf(label, sizeof(label), "serial%d", index);
27143a44 1898 serial_hds[index] = qemu_chr_new(label, devname, NULL);
998bbd74
GH
1899 if (!serial_hds[index]) {
1900 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1901 devname, strerror(errno));
1902 return -1;
1903 }
1904 index++;
1905 return 0;
1906}
1907
6a5e8b0e
GH
1908static int parallel_parse(const char *devname)
1909{
1910 static int index = 0;
1911 char label[32];
1912
1913 if (strcmp(devname, "none") == 0)
1914 return 0;
1915 if (index == MAX_PARALLEL_PORTS) {
1916 fprintf(stderr, "qemu: too many parallel ports\n");
1917 exit(1);
1918 }
1919 snprintf(label, sizeof(label), "parallel%d", index);
27143a44 1920 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
6a5e8b0e
GH
1921 if (!parallel_hds[index]) {
1922 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1923 devname, strerror(errno));
1924 return -1;
1925 }
1926 index++;
1927 return 0;
1928}
1929
aee1b935
GH
1930static int virtcon_parse(const char *devname)
1931{
3329f07b 1932 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
1933 static int index = 0;
1934 char label[32];
392ecf54 1935 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
1936
1937 if (strcmp(devname, "none") == 0)
1938 return 0;
1939 if (index == MAX_VIRTIO_CONSOLES) {
1940 fprintf(stderr, "qemu: too many virtio consoles\n");
1941 exit(1);
1942 }
392ecf54 1943
3329f07b 1944 bus_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1945 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1946
3329f07b 1947 dev_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1948 qemu_opt_set(dev_opts, "driver", "virtconsole");
1949
aee1b935 1950 snprintf(label, sizeof(label), "virtcon%d", index);
27143a44 1951 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
aee1b935
GH
1952 if (!virtcon_hds[index]) {
1953 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1954 devname, strerror(errno));
1955 return -1;
1956 }
392ecf54
AS
1957 qemu_opt_set(dev_opts, "chardev", label);
1958
aee1b935
GH
1959 index++;
1960 return 0;
1961}
1962
c9f398e5
PA
1963static int debugcon_parse(const char *devname)
1964{
1965 QemuOpts *opts;
1966
27143a44 1967 if (!qemu_chr_new("debugcon", devname, NULL)) {
c9f398e5
PA
1968 exit(1);
1969 }
3329f07b 1970 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
c9f398e5
PA
1971 if (!opts) {
1972 fprintf(stderr, "qemu: already have a debugcon device\n");
1973 exit(1);
1974 }
1975 qemu_opt_set(opts, "driver", "isa-debugcon");
1976 qemu_opt_set(opts, "chardev", "debugcon");
1977 return 0;
1978}
1979
9052ea6b
JK
1980static QEMUMachine *machine_parse(const char *name)
1981{
1982 QEMUMachine *m, *machine = NULL;
1983
1984 if (name) {
1985 machine = find_machine(name);
1986 }
1987 if (machine) {
1988 return machine;
1989 }
1990 printf("Supported machines are:\n");
1991 for (m = first_machine; m != NULL; m = m->next) {
1992 if (m->alias) {
1993 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1994 }
1995 printf("%-10s %s%s\n", m->name, m->desc,
1996 m->is_default ? " (default)" : "");
1997 }
1998 exit(!name || *name != '?');
1999}
2000
303d4e86
AP
2001static int tcg_init(void)
2002{
d5ab9713 2003 tcg_exec_init(tcg_tb_size * 1024 * 1024);
303d4e86
AP
2004 return 0;
2005}
2006
2007static struct {
2008 const char *opt_name;
2009 const char *name;
2010 int (*available)(void);
2011 int (*init)(void);
2012 int *allowed;
2013} accel_list[] = {
2014 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
3285cf4f 2015 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
303d4e86
AP
2016 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2017};
2018
2019static int configure_accelerator(void)
2020{
2021 const char *p = NULL;
2022 char buf[10];
2023 int i, ret;
2024 bool accel_initalised = 0;
2025 bool init_failed = 0;
2026
2027 QemuOptsList *list = qemu_find_opts("machine");
2028 if (!QTAILQ_EMPTY(&list->head)) {
2029 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2030 }
2031
2032 if (p == NULL) {
2033 /* Use the default "accelerator", tcg */
2034 p = "tcg";
2035 }
2036
2037 while (!accel_initalised && *p != '\0') {
2038 if (*p == ':') {
2039 p++;
2040 }
2041 p = get_opt_name(buf, sizeof (buf), p, ':');
2042 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2043 if (strcmp(accel_list[i].opt_name, buf) == 0) {
a16c53b1 2044 *(accel_list[i].allowed) = 1;
303d4e86
AP
2045 ret = accel_list[i].init();
2046 if (ret < 0) {
2047 init_failed = 1;
2048 if (!accel_list[i].available()) {
2049 printf("%s not supported for this target\n",
2050 accel_list[i].name);
2051 } else {
2052 fprintf(stderr, "failed to initialize %s: %s\n",
2053 accel_list[i].name,
2054 strerror(-ret));
2055 }
a16c53b1 2056 *(accel_list[i].allowed) = 0;
303d4e86
AP
2057 } else {
2058 accel_initalised = 1;
303d4e86
AP
2059 }
2060 break;
2061 }
2062 }
2063 if (i == ARRAY_SIZE(accel_list)) {
2064 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2065 }
2066 }
2067
2068 if (!accel_initalised) {
2069 fprintf(stderr, "No accelerator found!\n");
2070 exit(1);
2071 }
2072
2073 if (init_failed) {
2074 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2075 }
2076
2077 return !accel_initalised;
2078}
2079
fd42deeb
GH
2080void qemu_add_exit_notifier(Notifier *notify)
2081{
2082 notifier_list_add(&exit_notifiers, notify);
2083}
2084
2085void qemu_remove_exit_notifier(Notifier *notify)
2086{
2087 notifier_list_remove(&exit_notifiers, notify);
2088}
2089
2090static void qemu_run_exit_notifiers(void)
2091{
9e8dd451 2092 notifier_list_notify(&exit_notifiers, NULL);
fd42deeb
GH
2093}
2094
4cab946a
GN
2095void qemu_add_machine_init_done_notifier(Notifier *notify)
2096{
2097 notifier_list_add(&machine_init_done_notifiers, notify);
2098}
2099
2100static void qemu_run_machine_init_done_notifiers(void)
2101{
9e8dd451 2102 notifier_list_notify(&machine_init_done_notifiers, NULL);
4cab946a
GN
2103}
2104
6530a97b
AL
2105static const QEMUOption *lookup_opt(int argc, char **argv,
2106 const char **poptarg, int *poptind)
2107{
2108 const QEMUOption *popt;
2109 int optind = *poptind;
2110 char *r = argv[optind];
2111 const char *optarg;
2112
0f0bc3f1 2113 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2114 optind++;
2115 /* Treat --foo the same as -foo. */
2116 if (r[1] == '-')
2117 r++;
2118 popt = qemu_options;
2119 for(;;) {
2120 if (!popt->name) {
0f0bc3f1 2121 error_report("invalid option");
6530a97b
AL
2122 exit(1);
2123 }
2124 if (!strcmp(popt->name, r + 1))
2125 break;
2126 popt++;
2127 }
2128 if (popt->flags & HAS_ARG) {
2129 if (optind >= argc) {
0f0bc3f1 2130 error_report("requires an argument");
6530a97b
AL
2131 exit(1);
2132 }
2133 optarg = argv[optind++];
0f0bc3f1 2134 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2135 } else {
2136 optarg = NULL;
2137 }
2138
2139 *poptarg = optarg;
2140 *poptind = optind;
2141
2142 return popt;
2143}
2144
0750112a
AL
2145static gpointer malloc_and_trace(gsize n_bytes)
2146{
2147 void *ptr = malloc(n_bytes);
a74cd8cc 2148 trace_g_malloc(n_bytes, ptr);
0750112a
AL
2149 return ptr;
2150}
2151
2152static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2153{
2154 void *ptr = realloc(mem, n_bytes);
a74cd8cc 2155 trace_g_realloc(mem, n_bytes, ptr);
0750112a
AL
2156 return ptr;
2157}
2158
2159static void free_and_trace(gpointer mem)
2160{
a74cd8cc 2161 trace_g_free(mem);
0750112a
AL
2162 free(mem);
2163}
2164
902b3d5c 2165int main(int argc, char **argv, char **envp)
0824d6fc 2166{
59030a8c 2167 const char *gdbstub_dev = NULL;
e4bcb14c 2168 int i;
da1fcfda 2169 int snapshot, linux_boot;
4e3de9e9 2170 const char *icount_option = NULL;
7f7f9873 2171 const char *initrd_filename;
a20dd508 2172 const char *kernel_filename, *kernel_cmdline;
195325a4 2173 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 2174 DisplayState *ds;
7d957bd8 2175 DisplayChangeListener *dcl;
46d4767d 2176 int cyls, heads, secs, translation;
f31d07d1 2177 QemuOpts *hda_opts = NULL, *opts;
03b0ba70 2178 QemuOptsList *olist;
cd6f1169 2179 int optind;
6530a97b 2180 const char *optarg;
d63d307f 2181 const char *loadvm = NULL;
cc1daa40 2182 QEMUMachine *machine;
94fc95cd 2183 const char *cpu_model;
93815bc2 2184 const char *pid_file = NULL;
5bb7910a 2185 const char *incoming = NULL;
821601ea 2186#ifdef CONFIG_VNC
993fbfdb 2187 int show_vnc_port = 0;
821601ea 2188#endif
292444cb 2189 int defconfig = 1;
c235d738
MF
2190 const char *log_mask = NULL;
2191 const char *log_file = NULL;
0750112a
AL
2192 GMemVTable mem_trace = {
2193 .malloc = malloc_and_trace,
2194 .realloc = realloc_and_trace,
2195 .free = free_and_trace,
2196 };
23d15e86 2197 const char *trace_events = NULL;
e4858974 2198 const char *trace_file = NULL;
0b5538c3 2199
fd42deeb 2200 atexit(qemu_run_exit_notifiers);
65abca0a
MA
2201 error_set_progname(argv[0]);
2202
0750112a 2203 g_mem_set_vtable(&mem_trace);
ad8b8186 2204 g_thread_init(NULL);
0750112a 2205
6875204c
JK
2206 init_clocks();
2207
902b3d5c 2208 qemu_cache_utils_init(envp);
2209
72cf2d4f 2210 QLIST_INIT (&vm_change_state_head);
fe98ac14 2211 os_setup_early_signal_handling();
be995c27 2212
f80f9ec9 2213 module_call_init(MODULE_INIT_MACHINE);
0c257437 2214 machine = find_default_machine();
94fc95cd 2215 cpu_model = NULL;
fc01f7e7 2216 initrd_filename = NULL;
4fc5d071 2217 ram_size = 0;
33e3963e 2218 snapshot = 0;
a20dd508
FB
2219 kernel_filename = NULL;
2220 kernel_cmdline = "";
c4b1fcc0 2221 cyls = heads = secs = 0;
46d4767d 2222 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 2223
268a362c
AL
2224 for (i = 0; i < MAX_NODES; i++) {
2225 node_mem[i] = 0;
2226 node_cpumask[i] = 0;
2227 }
2228
268a362c 2229 nb_numa_nodes = 0;
7c9d8e07 2230 nb_nics = 0;
3b46e624 2231
41bd639b
BS
2232 autostart= 1;
2233
292444cb
AL
2234 /* first pass of option parsing */
2235 optind = 1;
2236 while (optind < argc) {
2237 if (argv[optind][0] != '-') {
2238 /* disk image */
28e68d68 2239 optind++;
292444cb
AL
2240 continue;
2241 } else {
2242 const QEMUOption *popt;
2243
2244 popt = lookup_opt(argc, argv, &optarg, &optind);
2245 switch (popt->index) {
2246 case QEMU_OPTION_nodefconfig:
2247 defconfig=0;
2248 break;
2249 }
2250 }
2251 }
2252
2253 if (defconfig) {
dcfb0939 2254 int ret;
cf5a65aa 2255
dcfb0939 2256 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 2257 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2258 exit(1);
292444cb
AL
2259 }
2260
dcfb0939 2261 ret = qemu_read_config_file(arch_config_name);
019e78ba 2262 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2263 exit(1);
292444cb
AL
2264 }
2265 }
de06f8d1 2266 cpudef_init();
292444cb
AL
2267
2268 /* second pass of option parsing */
cd6f1169 2269 optind = 1;
0824d6fc 2270 for(;;) {
cd6f1169 2271 if (optind >= argc)
0824d6fc 2272 break;
6530a97b 2273 if (argv[optind][0] != '-') {
2292ddae 2274 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2275 } else {
2276 const QEMUOption *popt;
2277
6530a97b 2278 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
2279 if (!(popt->arch_mask & arch_type)) {
2280 printf("Option %s not supported for this target\n", popt->name);
2281 exit(1);
2282 }
cd6f1169 2283 switch(popt->index) {
cc1daa40 2284 case QEMU_OPTION_M:
9052ea6b 2285 machine = machine_parse(optarg);
cc1daa40 2286 break;
94fc95cd
JM
2287 case QEMU_OPTION_cpu:
2288 /* hw initialization will check this */
15f82208 2289 if (*optarg == '?') {
262353cb 2290 list_cpus(stdout, &fprintf, optarg);
15f82208 2291 exit(0);
94fc95cd
JM
2292 } else {
2293 cpu_model = optarg;
2294 }
2295 break;
cd6f1169 2296 case QEMU_OPTION_initrd:
fc01f7e7
FB
2297 initrd_filename = optarg;
2298 break;
cd6f1169 2299 case QEMU_OPTION_hda:
5645b0f4
MA
2300 {
2301 char buf[256];
2302 if (cyls == 0)
2303 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2304 else
2305 snprintf(buf, sizeof(buf),
2306 "%s,cyls=%d,heads=%d,secs=%d%s",
2307 HD_OPTS , cyls, heads, secs,
2308 translation == BIOS_ATA_TRANSLATION_LBA ?
e4bcb14c 2309 ",trans=lba" :
5645b0f4 2310 translation == BIOS_ATA_TRANSLATION_NONE ?
e4bcb14c 2311 ",trans=none" : "");
5645b0f4
MA
2312 drive_add(IF_DEFAULT, 0, optarg, buf);
2313 break;
2314 }
cd6f1169 2315 case QEMU_OPTION_hdb:
cc1daa40
FB
2316 case QEMU_OPTION_hdc:
2317 case QEMU_OPTION_hdd:
2292ddae
MA
2318 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2319 HD_OPTS);
fc01f7e7 2320 break;
e4bcb14c 2321 case QEMU_OPTION_drive:
e2982c3a
MT
2322 if (drive_def(optarg) == NULL) {
2323 exit(1);
2324 }
e4bcb14c 2325 break;
d058fe03
GH
2326 case QEMU_OPTION_set:
2327 if (qemu_set_option(optarg) != 0)
2328 exit(1);
2329 break;
d0fef6fb
GH
2330 case QEMU_OPTION_global:
2331 if (qemu_global_option(optarg) != 0)
2332 exit(1);
2333 break;
3e3d5815 2334 case QEMU_OPTION_mtdblock:
2292ddae 2335 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2336 break;
a1bb27b1 2337 case QEMU_OPTION_sd:
2292ddae 2338 drive_add(IF_SD, 0, optarg, SD_OPTS);
a1bb27b1 2339 break;
86f55663 2340 case QEMU_OPTION_pflash:
2292ddae 2341 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2342 break;
cd6f1169 2343 case QEMU_OPTION_snapshot:
33e3963e
FB
2344 snapshot = 1;
2345 break;
cd6f1169 2346 case QEMU_OPTION_hdachs:
330d0414 2347 {
330d0414
FB
2348 const char *p;
2349 p = optarg;
2350 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2351 if (cyls < 1 || cyls > 16383)
2352 goto chs_fail;
330d0414
FB
2353 if (*p != ',')
2354 goto chs_fail;
2355 p++;
2356 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2357 if (heads < 1 || heads > 16)
2358 goto chs_fail;
330d0414
FB
2359 if (*p != ',')
2360 goto chs_fail;
2361 p++;
2362 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2363 if (secs < 1 || secs > 63)
2364 goto chs_fail;
2365 if (*p == ',') {
2366 p++;
2367 if (!strcmp(p, "none"))
2368 translation = BIOS_ATA_TRANSLATION_NONE;
2369 else if (!strcmp(p, "lba"))
2370 translation = BIOS_ATA_TRANSLATION_LBA;
2371 else if (!strcmp(p, "auto"))
2372 translation = BIOS_ATA_TRANSLATION_AUTO;
2373 else
2374 goto chs_fail;
2375 } else if (*p != '\0') {
c4b1fcc0 2376 chs_fail:
46d4767d
FB
2377 fprintf(stderr, "qemu: invalid physical CHS format\n");
2378 exit(1);
c4b1fcc0 2379 }
9dfd7c7a
GH
2380 if (hda_opts != NULL) {
2381 char num[16];
2382 snprintf(num, sizeof(num), "%d", cyls);
2383 qemu_opt_set(hda_opts, "cyls", num);
2384 snprintf(num, sizeof(num), "%d", heads);
2385 qemu_opt_set(hda_opts, "heads", num);
2386 snprintf(num, sizeof(num), "%d", secs);
2387 qemu_opt_set(hda_opts, "secs", num);
2388 if (translation == BIOS_ATA_TRANSLATION_LBA)
2389 qemu_opt_set(hda_opts, "trans", "lba");
2390 if (translation == BIOS_ATA_TRANSLATION_NONE)
2391 qemu_opt_set(hda_opts, "trans", "none");
2392 }
330d0414
FB
2393 }
2394 break;
268a362c
AL
2395 case QEMU_OPTION_numa:
2396 if (nb_numa_nodes >= MAX_NODES) {
2397 fprintf(stderr, "qemu: too many NUMA nodes\n");
2398 exit(1);
2399 }
2400 numa_add(optarg);
2401 break;
1472a95b
JS
2402 case QEMU_OPTION_display:
2403 display_type = select_display(optarg);
2404 break;
cd6f1169 2405 case QEMU_OPTION_nographic:
993fbfdb 2406 display_type = DT_NOGRAPHIC;
a20dd508 2407 break;
4d3b6f6e 2408 case QEMU_OPTION_curses:
47b05369 2409#ifdef CONFIG_CURSES
993fbfdb 2410 display_type = DT_CURSES;
47b05369
JS
2411#else
2412 fprintf(stderr, "Curses support is disabled\n");
2413 exit(1);
4d3b6f6e 2414#endif
47b05369 2415 break;
a171fe39 2416 case QEMU_OPTION_portrait:
9312805d
VK
2417 graphic_rotate = 90;
2418 break;
2419 case QEMU_OPTION_rotate:
2420 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2421 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2422 graphic_rotate != 180 && graphic_rotate != 270) {
2423 fprintf(stderr,
2424 "qemu: only 90, 180, 270 deg rotation is available\n");
2425 exit(1);
2426 }
a171fe39 2427 break;
cd6f1169 2428 case QEMU_OPTION_kernel:
a20dd508
FB
2429 kernel_filename = optarg;
2430 break;
cd6f1169 2431 case QEMU_OPTION_append:
a20dd508 2432 kernel_cmdline = optarg;
313aa567 2433 break;
cd6f1169 2434 case QEMU_OPTION_cdrom:
2292ddae 2435 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2436 break;
cd6f1169 2437 case QEMU_OPTION_boot:
28c5af54 2438 {
ef3adf68 2439 static const char * const params[] = {
3d3b8303 2440 "order", "once", "menu",
2441 "splash", "splash-time", NULL
ef3adf68
JK
2442 };
2443 char buf[sizeof(boot_devices)];
e0f084bf 2444 char *standard_boot_devices;
ef3adf68
JK
2445 int legacy = 0;
2446
2447 if (!strchr(optarg, '=')) {
2448 legacy = 1;
2449 pstrcpy(buf, sizeof(buf), optarg);
2450 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2451 fprintf(stderr,
2452 "qemu: unknown boot parameter '%s' in '%s'\n",
2453 buf, optarg);
2454 exit(1);
2455 }
2456
2457 if (legacy ||
2458 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2459 validate_bootdevices(buf);
ef3adf68 2460 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2461 }
e0f084bf
JK
2462 if (!legacy) {
2463 if (get_param_value(buf, sizeof(buf),
2464 "once", optarg)) {
4e9e9d6e 2465 validate_bootdevices(buf);
7267c094 2466 standard_boot_devices = g_strdup(boot_devices);
e0f084bf
JK
2467 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2468 qemu_register_reset(restore_boot_devices,
2469 standard_boot_devices);
2470 }
95387491
JK
2471 if (get_param_value(buf, sizeof(buf),
2472 "menu", optarg)) {
2473 if (!strcmp(buf, "on")) {
2474 boot_menu = 1;
2475 } else if (!strcmp(buf, "off")) {
2476 boot_menu = 0;
2477 } else {
2478 fprintf(stderr,
2479 "qemu: invalid option value '%s'\n",
2480 buf);
2481 exit(1);
2482 }
2483 }
3d3b8303 2484 qemu_opts_parse(qemu_find_opts("boot-opts"),
2485 optarg, 0);
e0f084bf 2486 }
36b486bb
FB
2487 }
2488 break;
cd6f1169 2489 case QEMU_OPTION_fda:
cd6f1169 2490 case QEMU_OPTION_fdb:
2292ddae
MA
2491 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2492 optarg, FD_OPTS);
c45886db 2493 break;
52ca8d6a
FB
2494 case QEMU_OPTION_no_fd_bootchk:
2495 fd_bootchk = 0;
2496 break;
a1ea458f 2497 case QEMU_OPTION_netdev:
3329f07b 2498 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2499 exit(1);
2500 }
2501 break;
7c9d8e07 2502 case QEMU_OPTION_net:
3329f07b 2503 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2504 exit(1);
2505 }
702c651c 2506 break;
c7f74643
FB
2507#ifdef CONFIG_SLIRP
2508 case QEMU_OPTION_tftp:
ad196a9d 2509 legacy_tftp_prefix = optarg;
9bf05444 2510 break;
47d5d01a 2511 case QEMU_OPTION_bootp:
ad196a9d 2512 legacy_bootp_filename = optarg;
47d5d01a 2513 break;
9bf05444 2514 case QEMU_OPTION_redir:
0752706d
MA
2515 if (net_slirp_redir(optarg) < 0)
2516 exit(1);
9bf05444 2517 break;
c7f74643 2518#endif
dc72ac14 2519 case QEMU_OPTION_bt:
bd3c948d 2520 add_device_config(DEV_BT, optarg);
dc72ac14 2521 break;
1d14ffa9 2522 case QEMU_OPTION_audio_help:
ad96090a
BS
2523 if (!(audio_available())) {
2524 printf("Option %s not supported for this target\n", popt->name);
2525 exit(1);
2526 }
1d14ffa9
FB
2527 AUD_help ();
2528 exit (0);
2529 break;
2530 case QEMU_OPTION_soundhw:
ad96090a
BS
2531 if (!(audio_available())) {
2532 printf("Option %s not supported for this target\n", popt->name);
2533 exit(1);
2534 }
1d14ffa9
FB
2535 select_soundhw (optarg);
2536 break;
cd6f1169 2537 case QEMU_OPTION_h:
15f82208 2538 help(0);
cd6f1169 2539 break;
9bd7e6d9
PB
2540 case QEMU_OPTION_version:
2541 version();
2542 exit(0);
2543 break;
00f82b8a 2544 case QEMU_OPTION_m: {
70b4f4bb 2545 int64_t value;
9f9b17a4
JS
2546
2547 value = strtosz(optarg, NULL);
2548 if (value < 0) {
00f82b8a 2549 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2550 exit(1);
2551 }
00f82b8a 2552
c227f099 2553 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2554 fprintf(stderr, "qemu: ram size too large\n");
2555 exit(1);
2556 }
2557 ram_size = value;
cd6f1169 2558 break;
00f82b8a 2559 }
c902760f
MT
2560 case QEMU_OPTION_mempath:
2561 mem_path = optarg;
2562 break;
2563#ifdef MAP_POPULATE
2564 case QEMU_OPTION_mem_prealloc:
2565 mem_prealloc = 1;
2566 break;
2567#endif
cd6f1169 2568 case QEMU_OPTION_d:
c235d738
MF
2569 log_mask = optarg;
2570 break;
2571 case QEMU_OPTION_D:
2572 log_file = optarg;
cd6f1169 2573 break;
cd6f1169 2574 case QEMU_OPTION_s:
59030a8c 2575 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 2576 break;
59030a8c
AL
2577 case QEMU_OPTION_gdb:
2578 gdbstub_dev = optarg;
cd6f1169 2579 break;
cd6f1169 2580 case QEMU_OPTION_L:
5cea8590 2581 data_dir = optarg;
cd6f1169 2582 break;
1192dad8
JM
2583 case QEMU_OPTION_bios:
2584 bios_name = optarg;
2585 break;
1b530a6d
AJ
2586 case QEMU_OPTION_singlestep:
2587 singlestep = 1;
2588 break;
cd6f1169 2589 case QEMU_OPTION_S:
3c07f8e8 2590 autostart = 0;
cd6f1169 2591 break;
3d11d0eb
FB
2592 case QEMU_OPTION_k:
2593 keyboard_layout = optarg;
2594 break;
ee22c2f7
FB
2595 case QEMU_OPTION_localtime:
2596 rtc_utc = 0;
2597 break;
3893c124 2598 case QEMU_OPTION_vga:
2599 select_vgahw (optarg);
1bfe856e 2600 break;
e9b137c2
FB
2601 case QEMU_OPTION_g:
2602 {
2603 const char *p;
2604 int w, h, depth;
2605 p = optarg;
2606 w = strtol(p, (char **)&p, 10);
2607 if (w <= 0) {
2608 graphic_error:
2609 fprintf(stderr, "qemu: invalid resolution or depth\n");
2610 exit(1);
2611 }
2612 if (*p != 'x')
2613 goto graphic_error;
2614 p++;
2615 h = strtol(p, (char **)&p, 10);
2616 if (h <= 0)
2617 goto graphic_error;
2618 if (*p == 'x') {
2619 p++;
2620 depth = strtol(p, (char **)&p, 10);
5fafdf24 2621 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2622 depth != 24 && depth != 32)
2623 goto graphic_error;
2624 } else if (*p == '\0') {
2625 depth = graphic_depth;
2626 } else {
2627 goto graphic_error;
2628 }
3b46e624 2629
e9b137c2
FB
2630 graphic_width = w;
2631 graphic_height = h;
2632 graphic_depth = depth;
2633 }
2634 break;
20d8a3ed
TS
2635 case QEMU_OPTION_echr:
2636 {
2637 char *r;
2638 term_escape_char = strtol(optarg, &r, 0);
2639 if (r == optarg)
2640 printf("Bad argument to echr\n");
2641 break;
2642 }
82c643ff 2643 case QEMU_OPTION_monitor:
6ca5582d
GH
2644 monitor_parse(optarg, "readline");
2645 default_monitor = 0;
2646 break;
2647 case QEMU_OPTION_qmp:
2648 monitor_parse(optarg, "control");
2d114dc1 2649 default_monitor = 0;
82c643ff 2650 break;
22a0e04b 2651 case QEMU_OPTION_mon:
3329f07b 2652 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2653 if (!opts) {
22a0e04b
GH
2654 exit(1);
2655 }
2d114dc1 2656 default_monitor = 0;
22a0e04b 2657 break;
191bc01b 2658 case QEMU_OPTION_chardev:
3329f07b 2659 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2660 if (!opts) {
191bc01b
GH
2661 exit(1);
2662 }
191bc01b 2663 break;
74db920c 2664 case QEMU_OPTION_fsdev:
03b0ba70
GH
2665 olist = qemu_find_opts("fsdev");
2666 if (!olist) {
2667 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2668 exit(1);
2669 }
2670 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2671 if (!opts) {
2672 fprintf(stderr, "parse error: %s\n", optarg);
2673 exit(1);
2674 }
2675 break;
3d54abc7 2676 case QEMU_OPTION_virtfs: {
e14ea479
SH
2677 QemuOpts *fsdev;
2678 QemuOpts *device;
3d54abc7 2679
03b0ba70
GH
2680 olist = qemu_find_opts("virtfs");
2681 if (!olist) {
2682 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2683 exit(1);
2684 }
2685 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2686 if (!opts) {
2687 fprintf(stderr, "parse error: %s\n", optarg);
2688 exit(1);
2689 }
2690
9ce56db6
VJJ
2691 if (qemu_opt_get(opts, "fstype") == NULL ||
2692 qemu_opt_get(opts, "mount_tag") == NULL ||
2693 qemu_opt_get(opts, "path") == NULL ||
2694 qemu_opt_get(opts, "security_model") == NULL) {
2695 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
12848bfc 2696 "security_model=[mapped|passthrough|none],"
e14ea479 2697 "mount_tag=tag.\n");
9ce56db6
VJJ
2698 exit(1);
2699 }
2700
e14ea479
SH
2701 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2702 qemu_opt_get(opts, "mount_tag"), 1);
2703 if (!fsdev) {
2704 fprintf(stderr, "duplicate fsdev id: %s\n",
2705 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2706 exit(1);
2707 }
e14ea479
SH
2708 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2709 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2710 qemu_opt_set(fsdev, "security_model",
2711 qemu_opt_get(opts, "security_model"));
2712
2713 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2714 qemu_opt_set(device, "driver", "virtio-9p-pci");
2715 qemu_opt_set(device, "fsdev",
2716 qemu_opt_get(opts, "mount_tag"));
2717 qemu_opt_set(device, "mount_tag",
2718 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2719 break;
2720 }
82c643ff 2721 case QEMU_OPTION_serial:
998bbd74
GH
2722 add_device_config(DEV_SERIAL, optarg);
2723 default_serial = 0;
18141ed6
JK
2724 if (strncmp(optarg, "mon:", 4) == 0) {
2725 default_monitor = 0;
2726 }
82c643ff 2727 break;
9dd986cc 2728 case QEMU_OPTION_watchdog:
09aaa160
MA
2729 if (watchdog) {
2730 fprintf(stderr,
2731 "qemu: only one watchdog option may be given\n");
2732 return 1;
2733 }
2734 watchdog = optarg;
9dd986cc
RJ
2735 break;
2736 case QEMU_OPTION_watchdog_action:
2737 if (select_watchdog_action(optarg) == -1) {
2738 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2739 exit(1);
2740 }
2741 break;
51ecf136 2742 case QEMU_OPTION_virtiocon:
aee1b935
GH
2743 add_device_config(DEV_VIRTCON, optarg);
2744 default_virtcon = 0;
18141ed6
JK
2745 if (strncmp(optarg, "mon:", 4) == 0) {
2746 default_monitor = 0;
2747 }
51ecf136 2748 break;
6508fe59 2749 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2750 add_device_config(DEV_PARALLEL, optarg);
2751 default_parallel = 0;
18141ed6
JK
2752 if (strncmp(optarg, "mon:", 4) == 0) {
2753 default_monitor = 0;
2754 }
6508fe59 2755 break;
c9f398e5
PA
2756 case QEMU_OPTION_debugcon:
2757 add_device_config(DEV_DEBUGCON, optarg);
2758 break;
d63d307f
FB
2759 case QEMU_OPTION_loadvm:
2760 loadvm = optarg;
2761 break;
2762 case QEMU_OPTION_full_screen:
2763 full_screen = 1;
2764 break;
667accab 2765#ifdef CONFIG_SDL
43523e93
TS
2766 case QEMU_OPTION_no_frame:
2767 no_frame = 1;
2768 break;
3780e197
TS
2769 case QEMU_OPTION_alt_grab:
2770 alt_grab = 1;
2771 break;
0ca9f8a4
DK
2772 case QEMU_OPTION_ctrl_grab:
2773 ctrl_grab = 1;
2774 break;
667accab
TS
2775 case QEMU_OPTION_no_quit:
2776 no_quit = 1;
2777 break;
7d957bd8 2778 case QEMU_OPTION_sdl:
993fbfdb 2779 display_type = DT_SDL;
7d957bd8 2780 break;
58fc096c
JS
2781#else
2782 case QEMU_OPTION_no_frame:
2783 case QEMU_OPTION_alt_grab:
2784 case QEMU_OPTION_ctrl_grab:
2785 case QEMU_OPTION_no_quit:
2786 case QEMU_OPTION_sdl:
2787 fprintf(stderr, "SDL support is disabled\n");
2788 exit(1);
667accab 2789#endif
f7cce898 2790 case QEMU_OPTION_pidfile:
93815bc2 2791 pid_file = optarg;
f7cce898 2792 break;
a09db21f
FB
2793 case QEMU_OPTION_win2k_hack:
2794 win2k_install_hack = 1;
2795 break;
73822ec8
AL
2796 case QEMU_OPTION_rtc_td_hack:
2797 rtc_td_hack = 1;
2798 break;
8a92ea2f 2799 case QEMU_OPTION_acpitable:
de06f8d1 2800 do_acpitable_option(optarg);
8a92ea2f 2801 break;
b6f6e3d3 2802 case QEMU_OPTION_smbios:
de06f8d1 2803 do_smbios_option(optarg);
b6f6e3d3 2804 break;
7ba1e619 2805 case QEMU_OPTION_enable_kvm:
303d4e86
AP
2806 olist = qemu_find_opts("machine");
2807 qemu_opts_reset(olist);
2808 qemu_opts_parse(olist, "accel=kvm", 0);
2809 break;
2810 case QEMU_OPTION_machine:
2811 olist = qemu_find_opts("machine");
2812 qemu_opts_reset(olist);
9052ea6b 2813 opts = qemu_opts_parse(olist, optarg, 1);
303d4e86
AP
2814 if (!opts) {
2815 fprintf(stderr, "parse error: %s\n", optarg);
2816 exit(1);
2817 }
2645c6dc
JK
2818 optarg = qemu_opt_get(opts, "type");
2819 if (optarg) {
2820 machine = machine_parse(optarg);
2821 }
7ba1e619 2822 break;
bb36d470
FB
2823 case QEMU_OPTION_usb:
2824 usb_enabled = 1;
2825 break;
a594cfbf
FB
2826 case QEMU_OPTION_usbdevice:
2827 usb_enabled = 1;
bd3c948d
GH
2828 add_device_config(DEV_USB, optarg);
2829 break;
2830 case QEMU_OPTION_device:
3329f07b 2831 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2832 exit(1);
2833 }
a594cfbf 2834 break;
6a00d601 2835 case QEMU_OPTION_smp:
dc6b1c09 2836 smp_parse(optarg);
b2097003 2837 if (smp_cpus < 1) {
6a00d601
FB
2838 fprintf(stderr, "Invalid number of CPUs\n");
2839 exit(1);
2840 }
6be68d7e
JS
2841 if (max_cpus < smp_cpus) {
2842 fprintf(stderr, "maxcpus must be equal to or greater than "
2843 "smp\n");
2844 exit(1);
2845 }
2846 if (max_cpus > 255) {
2847 fprintf(stderr, "Unsupported number of maxcpus\n");
2848 exit(1);
2849 }
6a00d601 2850 break;
24236869 2851 case QEMU_OPTION_vnc:
821601ea 2852#ifdef CONFIG_VNC
6b62dc2d 2853 display_remote++;
821601ea
JS
2854 vnc_display = optarg;
2855#else
2856 fprintf(stderr, "VNC support is disabled\n");
2857 exit(1);
2858#endif
2859 break;
6515b203
FB
2860 case QEMU_OPTION_no_acpi:
2861 acpi_enabled = 0;
2862 break;
16b29ae1
AL
2863 case QEMU_OPTION_no_hpet:
2864 no_hpet = 1;
2865 break;
7d4c3d53
MA
2866 case QEMU_OPTION_balloon:
2867 if (balloon_parse(optarg) < 0) {
2868 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2869 exit(1);
2870 }
df97b920 2871 break;
d1beab82
FB
2872 case QEMU_OPTION_no_reboot:
2873 no_reboot = 1;
2874 break;
b2f76161
AJ
2875 case QEMU_OPTION_no_shutdown:
2876 no_shutdown = 1;
2877 break;
9467cd46
AZ
2878 case QEMU_OPTION_show_cursor:
2879 cursor_hide = 0;
2880 break;
8fcb1b90
BS
2881 case QEMU_OPTION_uuid:
2882 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2883 fprintf(stderr, "Fail to parse UUID string."
2884 " Wrong format.\n");
2885 exit(1);
2886 }
2887 break;
9ae02555
TS
2888 case QEMU_OPTION_option_rom:
2889 if (nb_option_roms >= MAX_OPTION_ROMS) {
2890 fprintf(stderr, "Too many option ROMs\n");
2891 exit(1);
2892 }
2e55e842
GN
2893 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2894 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2895 option_rom[nb_option_roms].bootindex =
2896 qemu_opt_get_number(opts, "bootindex", -1);
2897 if (!option_rom[nb_option_roms].name) {
2898 fprintf(stderr, "Option ROM file is not specified\n");
2899 exit(1);
2900 }
9ae02555
TS
2901 nb_option_roms++;
2902 break;
8e71621f
PB
2903 case QEMU_OPTION_semihosting:
2904 semihosting_enabled = 1;
2905 break;
c35734b2 2906 case QEMU_OPTION_name:
7267c094 2907 qemu_name = g_strdup(optarg);
1889465a
AK
2908 {
2909 char *p = strchr(qemu_name, ',');
2910 if (p != NULL) {
2911 *p++ = 0;
2912 if (strncmp(p, "process=", 8)) {
5697f6ae 2913 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
2914 exit(1);
2915 }
2916 p += 8;
ce798cf2 2917 os_set_proc_name(p);
1889465a
AK
2918 }
2919 }
c35734b2 2920 break;
66508601
BS
2921 case QEMU_OPTION_prom_env:
2922 if (nb_prom_envs >= MAX_PROM_ENVS) {
2923 fprintf(stderr, "Too many prom variables\n");
2924 exit(1);
2925 }
2926 prom_envs[nb_prom_envs] = optarg;
2927 nb_prom_envs++;
2928 break;
2b8f2d41
AZ
2929 case QEMU_OPTION_old_param:
2930 old_param = 1;
05ebd537 2931 break;
f3dcfada
TS
2932 case QEMU_OPTION_clock:
2933 configure_alarms(optarg);
2934 break;
7e0af5d0 2935 case QEMU_OPTION_startdate:
1ed2fc1f
JK
2936 configure_rtc_date_offset(optarg, 1);
2937 break;
2938 case QEMU_OPTION_rtc:
3329f07b 2939 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 2940 if (!opts) {
1ed2fc1f 2941 exit(1);
7e0af5d0 2942 }
1ed2fc1f 2943 configure_rtc(opts);
7e0af5d0 2944 break;
26a5f13b 2945 case QEMU_OPTION_tb_size:
d5ab9713
JK
2946 tcg_tb_size = strtol(optarg, NULL, 0);
2947 if (tcg_tb_size < 0) {
2948 tcg_tb_size = 0;
2949 }
26a5f13b 2950 break;
2e70f6ef 2951 case QEMU_OPTION_icount:
4e3de9e9 2952 icount_option = optarg;
2e70f6ef 2953 break;
5bb7910a
AL
2954 case QEMU_OPTION_incoming:
2955 incoming = optarg;
8e84865e 2956 incoming_expected = true;
5bb7910a 2957 break;
d8c208dd
GH
2958 case QEMU_OPTION_nodefaults:
2959 default_serial = 0;
2960 default_parallel = 0;
aee1b935 2961 default_virtcon = 0;
d8c208dd
GH
2962 default_monitor = 0;
2963 default_vga = 0;
cb4522cc 2964 default_net = 0;
ac33f8fa
GH
2965 default_floppy = 0;
2966 default_cdrom = 0;
2967 default_sdcard = 0;
d8c208dd 2968 break;
e37630ca 2969 case QEMU_OPTION_xen_domid:
ad96090a
BS
2970 if (!(xen_available())) {
2971 printf("Option %s not supported for this target\n", popt->name);
2972 exit(1);
2973 }
e37630ca
AL
2974 xen_domid = atoi(optarg);
2975 break;
2976 case QEMU_OPTION_xen_create:
ad96090a
BS
2977 if (!(xen_available())) {
2978 printf("Option %s not supported for this target\n", popt->name);
2979 exit(1);
2980 }
e37630ca
AL
2981 xen_mode = XEN_CREATE;
2982 break;
2983 case QEMU_OPTION_xen_attach:
ad96090a
BS
2984 if (!(xen_available())) {
2985 printf("Option %s not supported for this target\n", popt->name);
2986 exit(1);
2987 }
e37630ca
AL
2988 xen_mode = XEN_ATTACH;
2989 break;
ab6540d5 2990 case QEMU_OPTION_trace:
e4858974 2991 {
ab6540d5 2992 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
e4858974
L
2993 if (!opts) {
2994 exit(1);
ab6540d5 2995 }
23d15e86 2996 trace_events = qemu_opt_get(opts, "events");
e4858974 2997 trace_file = qemu_opt_get(opts, "file");
ab6540d5 2998 break;
e4858974 2999 }
715a664a
GH
3000 case QEMU_OPTION_readconfig:
3001 {
dcfb0939
KW
3002 int ret = qemu_read_config_file(optarg);
3003 if (ret < 0) {
3004 fprintf(stderr, "read config %s: %s\n", optarg,
3005 strerror(-ret));
715a664a
GH
3006 exit(1);
3007 }
715a664a
GH
3008 break;
3009 }
29b0040b
GH
3010 case QEMU_OPTION_spice:
3011 olist = qemu_find_opts("spice");
3012 if (!olist) {
3013 fprintf(stderr, "spice is not supported by this qemu build.\n");
3014 exit(1);
3015 }
3016 opts = qemu_opts_parse(olist, optarg, 0);
3017 if (!opts) {
3018 fprintf(stderr, "parse error: %s\n", optarg);
3019 exit(1);
3020 }
3021 break;
715a664a
GH
3022 case QEMU_OPTION_writeconfig:
3023 {
3024 FILE *fp;
3025 if (strcmp(optarg, "-") == 0) {
3026 fp = stdout;
3027 } else {
3028 fp = fopen(optarg, "w");
3029 if (fp == NULL) {
3030 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3031 exit(1);
3032 }
3033 }
3034 qemu_config_write(fp);
3035 fclose(fp);
3036 break;
3037 }
59a5264b
JS
3038 default:
3039 os_parse_cmd_args(popt->index, optarg);
cd6f1169 3040 }
0824d6fc
FB
3041 }
3042 }
0f0bc3f1 3043 loc_set_none();
c235d738
MF
3044
3045 /* Open the logfile at this point, if necessary. We can't open the logfile
3046 * when encountering either of the logging options (-d or -D) because the
3047 * other one may be encountered later on the command line, changing the
3048 * location or level of logging.
3049 */
3050 if (log_mask) {
3051 if (log_file) {
3052 set_cpu_log_filename(log_file);
3053 }
3054 set_cpu_log(log_mask);
3055 }
330d0414 3056
23d15e86 3057 if (!trace_backend_init(trace_events, trace_file)) {
e4858974 3058 exit(1);
31d3c9b8 3059 }
0b5538c3 3060
5cea8590
PB
3061 /* If no data_dir is specified then try to find it relative to the
3062 executable path. */
3063 if (!data_dir) {
6170540b 3064 data_dir = os_find_datadir(argv[0]);
5cea8590 3065 }
60474fb5 3066 /* If all else fails use the install path specified when building. */
5cea8590 3067 if (!data_dir) {
1dabe05c 3068 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
3069 }
3070
6be68d7e
JS
3071 /*
3072 * Default to max_cpus = smp_cpus, in case the user doesn't
3073 * specify a max_cpus value.
3074 */
3075 if (!max_cpus)
3076 max_cpus = smp_cpus;
3077
3d878caa 3078 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
3079 if (smp_cpus > machine->max_cpus) {
3080 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3081 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3082 machine->max_cpus);
3083 exit(1);
3084 }
3085
67b724e6
AP
3086 /*
3087 * Get the default machine options from the machine if it is not already
3088 * specified either by the configuration file or by the command line.
3089 */
3090 if (machine->default_machine_opts) {
3091 QemuOptsList *list = qemu_find_opts("machine");
3092 const char *p = NULL;
3093
3094 if (!QTAILQ_EMPTY(&list->head)) {
3095 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3096 }
3097 if (p == NULL) {
9052ea6b
JK
3098 qemu_opts_reset(list);
3099 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
67b724e6
AP
3100 if (!opts) {
3101 fprintf(stderr, "parse error for machine %s: %s\n",
3102 machine->name, machine->default_machine_opts);
3103 exit(1);
3104 }
3105 }
3106 }
3107
3329f07b
GH
3108 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3109 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 3110
986c5f78
GH
3111 if (machine->no_serial) {
3112 default_serial = 0;
3113 }
3114 if (machine->no_parallel) {
3115 default_parallel = 0;
3116 }
3117 if (!machine->use_virtcon) {
3118 default_virtcon = 0;
3119 }
3120 if (machine->no_vga) {
3121 default_vga = 0;
3122 }
ac33f8fa
GH
3123 if (machine->no_floppy) {
3124 default_floppy = 0;
3125 }
3126 if (machine->no_cdrom) {
3127 default_cdrom = 0;
3128 }
3129 if (machine->no_sdcard) {
3130 default_sdcard = 0;
3131 }
986c5f78 3132
993fbfdb 3133 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
3134 if (default_parallel)
3135 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
3136 if (default_serial && default_monitor) {
3137 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
3138 } else if (default_virtcon && default_monitor) {
3139 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
3140 } else {
3141 if (default_serial)
3142 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
3143 if (default_virtcon)
3144 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 3145 if (default_monitor)
6ca5582d 3146 monitor_parse("stdio", "readline");
e1c09175 3147 }
998bbd74
GH
3148 } else {
3149 if (default_serial)
3150 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
3151 if (default_parallel)
3152 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 3153 if (default_monitor)
6ca5582d 3154 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
3155 if (default_virtcon)
3156 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 3157 }
64465297
GH
3158 if (default_vga)
3159 vga_interface_type = VGA_CIRRUS;
bc0129d9 3160
a5829fd9
T
3161 socket_init();
3162
3329f07b 3163 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 3164 exit(1);
758e8e38 3165#ifdef CONFIG_VIRTFS
3329f07b 3166 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
3167 exit(1);
3168 }
3169#endif
1a688d3b 3170
eb505be1 3171 os_daemonize();
71e3ceb8 3172
aa26bb2d 3173 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 3174 os_pidfile_error();
93815bc2
TS
3175 exit(1);
3176 }
3177
0ac543de
JK
3178 /* init the memory */
3179 if (ram_size == 0) {
3180 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3181 }
3182
303d4e86 3183 configure_accelerator();
214910a7 3184
3fcf7b6b
AL
3185 if (qemu_init_main_loop()) {
3186 fprintf(stderr, "qemu_init_main_loop failed\n");
3187 exit(1);
3188 }
a20dd508 3189 linux_boot = (kernel_filename != NULL);
6c41b272 3190
f8d39c01
TS
3191 if (!linux_boot && *kernel_cmdline != '\0') {
3192 fprintf(stderr, "-append only allowed with -kernel option\n");
3193 exit(1);
3194 }
3195
3196 if (!linux_boot && initrd_filename != NULL) {
3197 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3198 exit(1);
3199 }
3200
9156d763 3201 os_set_line_buffering();
3b46e624 3202
7183b4b4
AL
3203 if (init_timer_alarm() < 0) {
3204 fprintf(stderr, "could not initialize alarm timer\n");
3205 exit(1);
3206 }
4e3de9e9 3207 configure_icount(icount_option);
634fce96 3208
dc1c9fe8
MM
3209 if (net_init_clients() < 0) {
3210 exit(1);
702c651c 3211 }
f1510b2c 3212
dc72ac14 3213 /* init the bluetooth world */
bd3c948d
GH
3214 if (foreach_device_config(DEV_BT, bt_parse))
3215 exit(1);
dc72ac14 3216
834e76ea
AP
3217 if (!xen_enabled()) {
3218 /* On 32-bit hosts, QEMU is limited by virtual address space */
3219 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3220 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3221 exit(1);
3222 }
3223 }
9ae02555 3224
d5ab9713 3225 cpu_exec_init_all();
26a5f13b 3226
eb852011 3227 bdrv_init_with_whitelist();
c4b1fcc0 3228
c163b5ca 3229 blk_mig_init();
3230
e4bcb14c 3231 /* open the virtual block devices */
9dfd7c7a 3232 if (snapshot)
3329f07b
GH
3233 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3234 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 3235 exit(1);
3e3d5815 3236
4e5d9b57
MA
3237 default_drive(default_cdrom, snapshot, machine->use_scsi,
3238 IF_DEFAULT, 2, CDROM_OPTS);
3239 default_drive(default_floppy, snapshot, machine->use_scsi,
3240 IF_FLOPPY, 0, FD_OPTS);
3241 default_drive(default_sdcard, snapshot, machine->use_scsi,
3242 IF_SD, 0, SD_OPTS);
3243
97ab12d4 3244 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
c163b5ca 3245 ram_load, NULL);
8a7ddc38 3246
268a362c
AL
3247 if (nb_numa_nodes > 0) {
3248 int i;
3249
ea0e5418
SL
3250 if (nb_numa_nodes > MAX_NODES) {
3251 nb_numa_nodes = MAX_NODES;
268a362c
AL
3252 }
3253
3254 /* If no memory size if given for any node, assume the default case
3255 * and distribute the available memory equally across all nodes
3256 */
3257 for (i = 0; i < nb_numa_nodes; i++) {
3258 if (node_mem[i] != 0)
3259 break;
3260 }
3261 if (i == nb_numa_nodes) {
3262 uint64_t usedmem = 0;
3263
3264 /* On Linux, the each node's border has to be 8MB aligned,
3265 * the final node gets the rest.
3266 */
3267 for (i = 0; i < nb_numa_nodes - 1; i++) {
3268 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3269 usedmem += node_mem[i];
3270 }
3271 node_mem[i] = ram_size - usedmem;
3272 }
3273
3274 for (i = 0; i < nb_numa_nodes; i++) {
3275 if (node_cpumask[i] != 0)
3276 break;
3277 }
3278 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3279 * must cope with this anyway, because there are BIOSes out there in
3280 * real machines which also use this scheme.
3281 */
3282 if (i == nb_numa_nodes) {
3283 for (i = 0; i < smp_cpus; i++) {
3284 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3285 }
3286 }
3287 }
3288
3329f07b 3289 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
3290 exit(1);
3291 }
3292
998bbd74
GH
3293 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3294 exit(1);
6a5e8b0e
GH
3295 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3296 exit(1);
aee1b935
GH
3297 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3298 exit(1);
c9f398e5
PA
3299 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3300 exit(1);
2796dae0 3301
aae9460e
PB
3302 module_call_init(MODULE_INIT_DEVICE);
3303
3329f07b 3304 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
3305 exit(0);
3306
09aaa160
MA
3307 if (watchdog) {
3308 i = select_watchdog(watchdog);
3309 if (i > 0)
3310 exit (i == 1 ? 1 : 0);
3311 }
3312
b6b61144 3313 if (machine->compat_props) {
458fb679 3314 qdev_prop_register_global_list(machine->compat_props);
b6b61144 3315 }
d0fef6fb
GH
3316 qemu_add_globals();
3317
fbe1b595 3318 machine->init(ram_size, boot_devices,
3023f332
AL
3319 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3320
ea375f9a 3321 cpu_synchronize_all_post_init();
268a362c 3322
87d0a28e 3323 set_numa_modes();
268a362c 3324
6f338c34
AL
3325 current_machine = machine;
3326
3023f332
AL
3327 /* init USB devices */
3328 if (usb_enabled) {
0752706d
MA
3329 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3330 exit(1);
3023f332
AL
3331 }
3332
bd3c948d 3333 /* init generic devices */
3329f07b 3334 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3335 exit(1);
3336
668680f7
MA
3337 net_check_clients();
3338
3023f332 3339 /* just use the first displaystate for the moment */
b473df6e 3340 ds = get_displaystate();
993fbfdb 3341
a3e22260
GH
3342 if (using_spice)
3343 display_remote++;
6b62dc2d 3344 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3345#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3346 display_type = DT_SDL;
821601ea 3347#elif defined(CONFIG_VNC)
993fbfdb
AL
3348 vnc_display = "localhost:0,to=99";
3349 show_vnc_port = 1;
821601ea
JS
3350#else
3351 display_type = DT_NONE;
993fbfdb
AL
3352#endif
3353 }
821601ea 3354
993fbfdb 3355
6b62dc2d 3356 /* init local displays */
993fbfdb
AL
3357 switch (display_type) {
3358 case DT_NOGRAPHIC:
3359 break;
4d3b6f6e 3360#if defined(CONFIG_CURSES)
993fbfdb
AL
3361 case DT_CURSES:
3362 curses_display_init(ds, full_screen);
3363 break;
4d3b6f6e 3364#endif
5b0753e0 3365#if defined(CONFIG_SDL)
993fbfdb
AL
3366 case DT_SDL:
3367 sdl_display_init(ds, full_screen, no_frame);
3368 break;
5b0753e0 3369#elif defined(CONFIG_COCOA)
993fbfdb
AL
3370 case DT_SDL:
3371 cocoa_display_init(ds, full_screen);
3372 break;
313aa567 3373#endif
6b62dc2d
GH
3374 default:
3375 break;
3376 }
3377
0ce235a7
GN
3378 /* must be after terminal init, SDL library changes signal handlers */
3379 os_setup_signal_handling();
3380
821601ea 3381#ifdef CONFIG_VNC
6b62dc2d
GH
3382 /* init remote displays */
3383 if (vnc_display) {
993fbfdb
AL
3384 vnc_display_init(ds);
3385 if (vnc_display_open(ds, vnc_display) < 0)
3386 exit(1);
f92f8afe 3387
993fbfdb
AL
3388 if (show_vnc_port) {
3389 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3390 }
313aa567 3391 }
821601ea 3392#endif
a3e22260 3393#ifdef CONFIG_SPICE
a19cbfb3 3394 if (using_spice && !qxl_enabled) {
a3e22260
GH
3395 qemu_spice_display_init(ds);
3396 }
3397#endif
5b08fc10 3398
6b62dc2d
GH
3399 /* display setup */
3400 dpy_resize(ds);
3023f332
AL
3401 dcl = ds->listeners;
3402 while (dcl != NULL) {
3403 if (dcl->dpy_refresh != NULL) {
7bd427d8
PB
3404 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3405 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
0f2ad63f 3406 break;
20d8a3ed 3407 }
3023f332 3408 dcl = dcl->next;
20d8a3ed 3409 }
6b62dc2d 3410 if (ds->gui_timer == NULL) {
7bd427d8
PB
3411 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3412 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
9043b62d 3413 }
b473df6e 3414 text_consoles_set_display(ds);
2796dae0 3415
59030a8c
AL
3416 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3417 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3418 gdbstub_dev);
3419 exit(1);
45669e00 3420 }
45669e00 3421
3418bd25
GH
3422 qdev_machine_creation_done();
3423
15ff7705
GH
3424 if (rom_load_all() != 0) {
3425 fprintf(stderr, "rom loading failed\n");
3426 exit(1);
3427 }
45a50b16 3428
80376c3f
IY
3429 /* TODO: once all bus devices are qdevified, this should be done
3430 * when bus is created by qdev.c */
3431 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3432 qemu_run_machine_init_done_notifiers();
3433
e063eb1f 3434 qemu_system_reset(VMRESET_SILENT);
05f2401e 3435 if (loadvm) {
03cd4655 3436 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3437 autostart = 0;
3438 }
3439 }
d63d307f 3440
2bb8c10c 3441 if (incoming) {
8ca5e801
JQ
3442 int ret = qemu_start_incoming_migration(incoming);
3443 if (ret < 0) {
3444 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3445 incoming, ret);
3446 exit(ret);
3447 }
6b99dadc 3448 } else if (autostart) {
c0f4ce77 3449 vm_start();
6b99dadc 3450 }
ffd843bc 3451
eb505be1 3452 os_setup_post();
71e3ceb8 3453
8a7ddc38 3454 main_loop();
40c3bac3 3455 quit_timers();
63a01ef8 3456 net_cleanup();
3d3b8303 3457 res_free();
b46a8906 3458
0824d6fc
FB
3459 return 0;
3460}