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