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