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