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