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