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