]> git.proxmox.com Git - qemu.git/blame - vl.c
virtio-9p: Introduces an option to specify the security model.
[qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
0824d6fc 23 */
0824d6fc 24#include <unistd.h>
0824d6fc
FB
25#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
0824d6fc 28#include <errno.h>
67b915a5 29#include <sys/time.h>
c88676f8 30#include <zlib.h>
67b915a5 31
71e72a19 32/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
33#include "config-host.h"
34
67b915a5 35#ifndef _WIN32
5cea8590 36#include <libgen.h>
67b915a5 37#include <sys/times.h>
f1510b2c 38#include <sys/wait.h>
67b915a5 39#include <termios.h>
67b915a5 40#include <sys/mman.h>
f1510b2c 41#include <sys/ioctl.h>
24646c7e 42#include <sys/resource.h>
f1510b2c 43#include <sys/socket.h>
c94c8d64 44#include <netinet/in.h>
24646c7e 45#include <net/if.h>
24646c7e 46#include <arpa/inet.h>
9d728e8c 47#include <dirent.h>
7c9d8e07 48#include <netdb.h>
cb4b976b 49#include <sys/select.h>
71e72a19 50#ifdef CONFIG_BSD
7d3505c5 51#include <sys/stat.h>
a167ba50 52#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
7d3505c5 53#include <libutil.h>
92c0e657 54#include <sys/sysctl.h>
24646c7e
BS
55#else
56#include <util.h>
128ab2ff 57#endif
bbe813a2 58#else
223f0d72 59#ifdef __linux__
7d3505c5
FB
60#include <pty.h>
61#include <malloc.h>
bd494f4c 62
e57a8c0e 63#include <linux/ppdev.h>
5867c88a 64#include <linux/parport.h>
223f0d72
BS
65#endif
66#ifdef __sun__
d5d10bc3
TS
67#include <sys/stat.h>
68#include <sys/ethernet.h>
69#include <sys/sockio.h>
d5d10bc3 70#include <netinet/arp.h>
d5d10bc3
TS
71#include <netinet/in_systm.h>
72#include <netinet/ip.h>
73#include <netinet/ip_icmp.h> // must come after ip.h
74#include <netinet/udp.h>
75#include <netinet/tcp.h>
76#include <net/if.h>
77#include <syslog.h>
78#include <stropts.h>
8d32cf0e
BS
79/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81extern int madvise(caddr_t, size_t, int);
67b915a5 82#endif
7d3505c5 83#endif
ec530c81 84#endif
67b915a5 85
9892fbfb
BS
86#if defined(__OpenBSD__)
87#include <util.h>
88#endif
89
8a16d273
TS
90#if defined(CONFIG_VDE)
91#include <libvdeplug.h>
92#endif
93
67b915a5 94#ifdef _WIN32
49dc768d 95#include <windows.h>
67b915a5
FB
96#endif
97
73332e5c 98#ifdef CONFIG_SDL
59a36a2f 99#if defined(__APPLE__) || defined(main)
6693665a 100#include <SDL.h>
880fec5d 101int qemu_main(int argc, char **argv, char **envp);
102int main(int argc, char **argv)
103{
59a36a2f 104 return qemu_main(argc, argv, NULL);
880fec5d 105}
106#undef main
107#define main qemu_main
96bcd4f8 108#endif
73332e5c 109#endif /* CONFIG_SDL */
0824d6fc 110
5b0753e0
FB
111#ifdef CONFIG_COCOA
112#undef main
113#define main qemu_main
114#endif /* CONFIG_COCOA */
115
511d2b14
BS
116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
511d2b14
BS
121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
9dd986cc 124#include "hw/watchdog.h"
b6f6e3d3 125#include "hw/smbios.h"
e37630ca 126#include "hw/xen.h"
bd3c948d 127#include "hw/qdev.h"
45a50b16 128#include "hw/loader.h"
5ef4efa4 129#include "bt-host.h"
511d2b14 130#include "net.h"
68ac40d2 131#include "net/slirp.h"
511d2b14
BS
132#include "monitor.h"
133#include "console.h"
134#include "sysemu.h"
135#include "gdbstub.h"
136#include "qemu-timer.h"
137#include "qemu-char.h"
138#include "cache-utils.h"
139#include "block.h"
666daa68 140#include "blockdev.h"
c163b5ca 141#include "block-migration.h"
a718acec 142#include "dma.h"
511d2b14
BS
143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
d3f24367 146#include "qemu-option.h"
7282a033 147#include "qemu-config.h"
e78c48ec 148#include "qemu-objects.h"
59a5264b 149#include "qemu-options.h"
74db920c
GS
150#ifdef CONFIG_LINUX
151#include "fsdev/qemu-fsdev.h"
152#endif
511d2b14 153
0824d6fc 154#include "disas.h"
fc01f7e7 155
511d2b14
BS
156#include "qemu_socket.h"
157
d918f23e 158#include "slirp/libslirp.h"
511d2b14 159
72cf2d4f 160#include "qemu-queue.h"
296af7c9 161#include "cpus.h"
ad96090a 162#include "arch_init.h"
72cf2d4f 163
9dc63a1e
BS
164//#define DEBUG_NET
165//#define DEBUG_SLIRP
330d0414 166
1bfe856e 167#define DEFAULT_RAM_SIZE 128
313aa567 168
98b19252
AS
169#define MAX_VIRTIO_CONSOLES 1
170
5cea8590 171static const char *data_dir;
1192dad8 172const char *bios_name = NULL;
cb5a7aa8 173enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 174DisplayType display_type = DT_DEFAULT;
3d11d0eb 175const char* keyboard_layout = NULL;
c227f099 176ram_addr_t ram_size;
c902760f
MT
177const char *mem_path = NULL;
178#ifdef MAP_POPULATE
179int mem_prealloc = 0; /* force preallocation of physical target memory */
180#endif
c4b1fcc0 181int nb_nics;
7c9d8e07 182NICInfo nd_table[MAX_NICS];
8a7ddc38 183int vm_running;
d399f677 184int autostart;
f6503059
AZ
185static int rtc_utc = 1;
186static int rtc_date_offset = -1; /* -1 means no change */
6875204c 187QEMUClock *rtc_clock;
64465297 188int vga_interface_type = VGA_NONE;
dbed7e40 189static int full_screen = 0;
634a21f6 190#ifdef CONFIG_SDL
dbed7e40 191static int no_frame = 0;
634a21f6 192#endif
667accab 193int no_quit = 0;
8d11df9e 194CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 195CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 196CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 197int win2k_install_hack = 0;
73822ec8 198int rtc_td_hack = 0;
bb36d470 199int usb_enabled = 0;
1b530a6d 200int singlestep = 0;
6a00d601 201int smp_cpus = 1;
6be68d7e 202int max_cpus = 0;
dc6b1c09
AP
203int smp_cores = 1;
204int smp_threads = 1;
73fc9742 205const char *vnc_display;
6515b203 206int acpi_enabled = 1;
16b29ae1 207int no_hpet = 0;
52ca8d6a 208int fd_bootchk = 1;
d1beab82 209int no_reboot = 0;
b2f76161 210int no_shutdown = 0;
9467cd46 211int cursor_hide = 1;
a171fe39 212int graphic_rotate = 0;
6b35e7bf 213uint8_t irq0override = 1;
09aaa160 214const char *watchdog;
9ae02555
TS
215const char *option_rom[MAX_OPTION_ROMS];
216int nb_option_roms;
8e71621f 217int semihosting_enabled = 0;
2b8f2d41 218int old_param = 0;
c35734b2 219const char *qemu_name;
3780e197 220int alt_grab = 0;
0ca9f8a4 221int ctrl_grab = 0;
66508601
BS
222unsigned int nb_prom_envs = 0;
223const char *prom_envs[MAX_PROM_ENVS];
95387491 224int boot_menu;
0824d6fc 225
268a362c
AL
226int nb_numa_nodes;
227uint64_t node_mem[MAX_NODES];
228uint64_t node_cpumask[MAX_NODES];
229
9043b62d 230static QEMUTimer *nographic_timer;
ee5605e5 231
8fcb1b90
BS
232uint8_t qemu_uuid[16];
233
76e30d0f
JK
234static QEMUBootSetHandler *boot_set_handler;
235static void *boot_set_opaque;
236
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 {
5cdc9b76 804 if (sockets) {
dc6b1c09
AP
805 threads = smp / (cores * sockets);
806 }
807 }
808 }
809 smp_cpus = smp;
810 smp_cores = cores > 0 ? cores : 1;
811 smp_threads = threads > 0 ? threads : 1;
812 if (max_cpus == 0)
813 max_cpus = smp_cpus;
814}
815
a594cfbf
FB
816/***********************************************************/
817/* USB devices */
818
fb08000c 819static int usb_device_add(const char *devname)
a594cfbf
FB
820{
821 const char *p;
a5d2f727 822 USBDevice *dev = NULL;
a594cfbf 823
a5d2f727 824 if (!usb_enabled)
a594cfbf
FB
825 return -1;
826
0958b4cc
GH
827 /* drivers with .usbdevice_name entry in USBDeviceInfo */
828 dev = usbdevice_create(devname);
829 if (dev)
830 goto done;
831
a5d2f727 832 /* the other ones */
a594cfbf
FB
833 if (strstart(devname, "host:", &p)) {
834 dev = usb_host_device_open(p);
dc72ac14
AZ
835 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
836 dev = usb_bt_init(devname[2] ? hci_init(p) :
837 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
838 } else {
839 return -1;
840 }
0d92ed30
PB
841 if (!dev)
842 return -1;
843
a5d2f727 844done:
a594cfbf
FB
845 return 0;
846}
847
1f3870ab
AL
848static int usb_device_del(const char *devname)
849{
850 int bus_num, addr;
851 const char *p;
852
5d0c5750
AL
853 if (strstart(devname, "host:", &p))
854 return usb_host_device_close(p);
855
a5d2f727 856 if (!usb_enabled)
1f3870ab
AL
857 return -1;
858
859 p = strchr(devname, '.');
860 if (!p)
861 return -1;
862 bus_num = strtoul(devname, NULL, 0);
863 addr = strtoul(p + 1, NULL, 0);
864
a5d2f727 865 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
866}
867
bd3c948d
GH
868static int usb_parse(const char *cmdline)
869{
59d1c1c2 870 int r;
fb08000c 871 r = usb_device_add(cmdline);
59d1c1c2
ST
872 if (r < 0) {
873 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
874 }
875 return r;
bd3c948d
GH
876}
877
d54908a5 878void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 879{
59d1c1c2 880 const char *devname = qdict_get_str(qdict, "devname");
fb08000c 881 if (usb_device_add(devname) < 0) {
1ecda02b 882 error_report("could not add USB device '%s'", devname);
59d1c1c2 883 }
a594cfbf
FB
884}
885
d54908a5 886void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 887{
59d1c1c2
ST
888 const char *devname = qdict_get_str(qdict, "devname");
889 if (usb_device_del(devname) < 0) {
1ecda02b 890 error_report("could not delete USB device '%s'", devname);
59d1c1c2 891 }
a594cfbf
FB
892}
893
201a51fc
AZ
894/***********************************************************/
895/* PCMCIA/Cardbus */
896
897static struct pcmcia_socket_entry_s {
bc24a225 898 PCMCIASocket *socket;
201a51fc
AZ
899 struct pcmcia_socket_entry_s *next;
900} *pcmcia_sockets = 0;
901
bc24a225 902void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
903{
904 struct pcmcia_socket_entry_s *entry;
905
906 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
907 entry->socket = socket;
908 entry->next = pcmcia_sockets;
909 pcmcia_sockets = entry;
910}
911
bc24a225 912void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
913{
914 struct pcmcia_socket_entry_s *entry, **ptr;
915
916 ptr = &pcmcia_sockets;
917 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
918 if (entry->socket == socket) {
919 *ptr = entry->next;
920 qemu_free(entry);
921 }
922}
923
376253ec 924void pcmcia_info(Monitor *mon)
201a51fc
AZ
925{
926 struct pcmcia_socket_entry_s *iter;
376253ec 927
201a51fc 928 if (!pcmcia_sockets)
376253ec 929 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
930
931 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
932 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
933 iter->socket->attached ? iter->socket->card_string :
934 "Empty");
201a51fc
AZ
935}
936
2ff89790 937/***********************************************************/
8a7ddc38 938/* I/O handling */
0824d6fc 939
c4b1fcc0
FB
940typedef struct IOHandlerRecord {
941 int fd;
7b27a769 942 IOCanReadHandler *fd_read_poll;
7c9d8e07
FB
943 IOHandler *fd_read;
944 IOHandler *fd_write;
cafffd40 945 int deleted;
c4b1fcc0
FB
946 void *opaque;
947 /* temporary data */
948 struct pollfd *ufd;
31d4ee6c 949 QLIST_ENTRY(IOHandlerRecord) next;
c4b1fcc0
FB
950} IOHandlerRecord;
951
31d4ee6c
JQ
952static QLIST_HEAD(, IOHandlerRecord) io_handlers =
953 QLIST_HEAD_INITIALIZER(io_handlers);
954
c4b1fcc0 955
7c9d8e07
FB
956/* XXX: fd_read_poll should be suppressed, but an API change is
957 necessary in the character devices to suppress fd_can_read(). */
5fafdf24 958int qemu_set_fd_handler2(int fd,
7b27a769 959 IOCanReadHandler *fd_read_poll,
5fafdf24
TS
960 IOHandler *fd_read,
961 IOHandler *fd_write,
7c9d8e07 962 void *opaque)
c4b1fcc0 963{
31d4ee6c 964 IOHandlerRecord *ioh;
c4b1fcc0 965
7c9d8e07 966 if (!fd_read && !fd_write) {
31d4ee6c 967 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07 968 if (ioh->fd == fd) {
cafffd40 969 ioh->deleted = 1;
7c9d8e07
FB
970 break;
971 }
7c9d8e07
FB
972 }
973 } else {
31d4ee6c 974 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07
FB
975 if (ioh->fd == fd)
976 goto found;
977 }
978 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
31d4ee6c 979 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
7c9d8e07
FB
980 found:
981 ioh->fd = fd;
982 ioh->fd_read_poll = fd_read_poll;
983 ioh->fd_read = fd_read;
984 ioh->fd_write = fd_write;
985 ioh->opaque = opaque;
cafffd40 986 ioh->deleted = 0;
7c9d8e07 987 }
c4b1fcc0
FB
988 return 0;
989}
990
5fafdf24
TS
991int qemu_set_fd_handler(int fd,
992 IOHandler *fd_read,
993 IOHandler *fd_write,
7c9d8e07 994 void *opaque)
8a7ddc38 995{
7c9d8e07 996 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
8a7ddc38
FB
997}
998
cc1daa40
FB
999/***********************************************************/
1000/* machine registration */
1001
bdaf78e0 1002static QEMUMachine *first_machine = NULL;
6f338c34 1003QEMUMachine *current_machine = NULL;
cc1daa40
FB
1004
1005int qemu_register_machine(QEMUMachine *m)
1006{
1007 QEMUMachine **pm;
1008 pm = &first_machine;
1009 while (*pm != NULL)
1010 pm = &(*pm)->next;
1011 m->next = NULL;
1012 *pm = m;
1013 return 0;
1014}
1015
9596ebb7 1016static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1017{
1018 QEMUMachine *m;
1019
1020 for(m = first_machine; m != NULL; m = m->next) {
1021 if (!strcmp(m->name, name))
1022 return m;
3f6599e6
MM
1023 if (m->alias && !strcmp(m->alias, name))
1024 return m;
cc1daa40
FB
1025 }
1026 return NULL;
1027}
1028
0c257437
AL
1029static QEMUMachine *find_default_machine(void)
1030{
1031 QEMUMachine *m;
1032
1033 for(m = first_machine; m != NULL; m = m->next) {
1034 if (m->is_default) {
1035 return m;
1036 }
1037 }
1038 return NULL;
1039}
1040
8a7ddc38
FB
1041/***********************************************************/
1042/* main execution loop */
1043
9596ebb7 1044static void gui_update(void *opaque)
8a7ddc38 1045{
7d957bd8 1046 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1047 DisplayState *ds = opaque;
7d957bd8
AL
1048 DisplayChangeListener *dcl = ds->listeners;
1049
62a2744c 1050 qemu_flush_coalesced_mmio_buffer();
7d957bd8
AL
1051 dpy_refresh(ds);
1052
1053 while (dcl != NULL) {
1054 if (dcl->gui_timer_interval &&
1055 dcl->gui_timer_interval < interval)
1056 interval = dcl->gui_timer_interval;
1057 dcl = dcl->next;
1058 }
1059 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
8a7ddc38
FB
1060}
1061
9043b62d
BS
1062static void nographic_update(void *opaque)
1063{
1064 uint64_t interval = GUI_REFRESH_INTERVAL;
1065
62a2744c 1066 qemu_flush_coalesced_mmio_buffer();
9043b62d
BS
1067 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1068}
1069
0bd48850
FB
1070struct vm_change_state_entry {
1071 VMChangeStateHandler *cb;
1072 void *opaque;
72cf2d4f 1073 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1074};
1075
72cf2d4f 1076static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1077
1078VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1079 void *opaque)
1080{
1081 VMChangeStateEntry *e;
1082
1083 e = qemu_mallocz(sizeof (*e));
0bd48850
FB
1084
1085 e->cb = cb;
1086 e->opaque = opaque;
72cf2d4f 1087 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1088 return e;
1089}
1090
1091void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1092{
72cf2d4f 1093 QLIST_REMOVE (e, entries);
0bd48850
FB
1094 qemu_free (e);
1095}
1096
296af7c9 1097void vm_state_notify(int running, int reason)
0bd48850
FB
1098{
1099 VMChangeStateEntry *e;
1100
1101 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
9781e040 1102 e->cb(e->opaque, running, reason);
0bd48850
FB
1103 }
1104}
1105
8a7ddc38
FB
1106void vm_start(void)
1107{
1108 if (!vm_running) {
1109 cpu_enable_ticks();
1110 vm_running = 1;
9781e040 1111 vm_state_notify(1, 0);
d6dc3d42 1112 resume_all_vcpus();
6ed2c484 1113 monitor_protocol_event(QEVENT_RESUME, NULL);
8a7ddc38
FB
1114 }
1115}
1116
bb0c6722
FB
1117/* reset/shutdown handler */
1118
1119typedef struct QEMUResetEntry {
72cf2d4f 1120 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
1121 QEMUResetHandler *func;
1122 void *opaque;
bb0c6722
FB
1123} QEMUResetEntry;
1124
72cf2d4f
BS
1125static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1126 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722
FB
1127static int reset_requested;
1128static int shutdown_requested;
3475187d 1129static int powerdown_requested;
296af7c9 1130int debug_requested;
7277e027 1131int vmstop_requested;
bb0c6722 1132
cf7a2fe2
AJ
1133int qemu_shutdown_requested(void)
1134{
1135 int r = shutdown_requested;
1136 shutdown_requested = 0;
1137 return r;
1138}
1139
1140int qemu_reset_requested(void)
1141{
1142 int r = reset_requested;
1143 reset_requested = 0;
1144 return r;
1145}
1146
1147int qemu_powerdown_requested(void)
1148{
1149 int r = powerdown_requested;
1150 powerdown_requested = 0;
1151 return r;
1152}
1153
e568902a
AL
1154static int qemu_debug_requested(void)
1155{
1156 int r = debug_requested;
1157 debug_requested = 0;
1158 return r;
1159}
1160
6e29f5da
AL
1161static int qemu_vmstop_requested(void)
1162{
1163 int r = vmstop_requested;
1164 vmstop_requested = 0;
1165 return r;
1166}
1167
a08d4367 1168void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 1169{
55ddfe8e 1170 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bb0c6722 1171
bb0c6722
FB
1172 re->func = func;
1173 re->opaque = opaque;
72cf2d4f 1174 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
1175}
1176
dda9b29f 1177void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
1178{
1179 QEMUResetEntry *re;
1180
72cf2d4f 1181 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 1182 if (re->func == func && re->opaque == opaque) {
72cf2d4f 1183 QTAILQ_REMOVE(&reset_handlers, re, entry);
dda9b29f
JK
1184 qemu_free(re);
1185 return;
1186 }
1187 }
1188}
1189
1190void qemu_system_reset(void)
1191{
1192 QEMUResetEntry *re, *nre;
1193
1194 /* reset all devices */
72cf2d4f 1195 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
1196 re->func(re->opaque);
1197 }
81d9b784 1198 monitor_protocol_event(QEVENT_RESET, NULL);
ea375f9a 1199 cpu_synchronize_all_post_reset();
bb0c6722
FB
1200}
1201
1202void qemu_system_reset_request(void)
1203{
d1beab82
FB
1204 if (no_reboot) {
1205 shutdown_requested = 1;
1206 } else {
1207 reset_requested = 1;
1208 }
d9f75a4e 1209 qemu_notify_event();
bb0c6722
FB
1210}
1211
1212void qemu_system_shutdown_request(void)
1213{
1214 shutdown_requested = 1;
d9f75a4e 1215 qemu_notify_event();
bb0c6722
FB
1216}
1217
3475187d
FB
1218void qemu_system_powerdown_request(void)
1219{
1220 powerdown_requested = 1;
d9f75a4e
AL
1221 qemu_notify_event();
1222}
1223
d6f4ade2 1224void main_loop_wait(int nonblocking)
56f3a5d0
AL
1225{
1226 IOHandlerRecord *ioh;
1227 fd_set rfds, wfds, xfds;
1228 int ret, nfds;
1229 struct timeval tv;
d6f4ade2 1230 int timeout;
56f3a5d0 1231
d6f4ade2
PB
1232 if (nonblocking)
1233 timeout = 0;
1234 else {
1235 timeout = qemu_calculate_timeout();
1236 qemu_bh_update_timeout(&timeout);
1237 }
56f3a5d0 1238
0d93ca7c 1239 os_host_main_loop_wait(&timeout);
56f3a5d0 1240
fd1dff4b
FB
1241 /* poll any events */
1242 /* XXX: separate device handlers from system ones */
6abfbd79 1243 nfds = -1;
fd1dff4b
FB
1244 FD_ZERO(&rfds);
1245 FD_ZERO(&wfds);
e035649e 1246 FD_ZERO(&xfds);
31d4ee6c 1247 QLIST_FOREACH(ioh, &io_handlers, next) {
cafffd40
TS
1248 if (ioh->deleted)
1249 continue;
fd1dff4b
FB
1250 if (ioh->fd_read &&
1251 (!ioh->fd_read_poll ||
1252 ioh->fd_read_poll(ioh->opaque) != 0)) {
1253 FD_SET(ioh->fd, &rfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1256 }
1257 if (ioh->fd_write) {
1258 FD_SET(ioh->fd, &wfds);
1259 if (ioh->fd > nfds)
1260 nfds = ioh->fd;
1261 }
1262 }
3b46e624 1263
56f3a5d0
AL
1264 tv.tv_sec = timeout / 1000;
1265 tv.tv_usec = (timeout % 1000) * 1000;
1266
d918f23e
JK
1267 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1268
4870852c 1269 qemu_mutex_unlock_iothread();
e035649e 1270 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4870852c 1271 qemu_mutex_lock_iothread();
fd1dff4b 1272 if (ret > 0) {
31d4ee6c 1273 IOHandlerRecord *pioh;
cafffd40 1274
31d4ee6c 1275 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
cafffd40 1276 if (ioh->deleted) {
31d4ee6c 1277 QLIST_REMOVE(ioh, next);
cafffd40 1278 qemu_free(ioh);
4bed9837
JQ
1279 continue;
1280 }
1281 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1282 ioh->fd_read(ioh->opaque);
1283 }
1284 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1285 ioh->fd_write(ioh->opaque);
31d4ee6c 1286 }
cafffd40 1287 }
fd1dff4b 1288 }
d918f23e
JK
1289
1290 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 1291
b6964827 1292 qemu_run_all_timers();
21d5d12b 1293
423f0742
PB
1294 /* Check bottom-halves last in case any of the earlier events triggered
1295 them. */
1296 qemu_bh_poll();
3b46e624 1297
5905b2e5
FB
1298}
1299
43b96858
AL
1300static int vm_can_run(void)
1301{
1302 if (powerdown_requested)
1303 return 0;
1304 if (reset_requested)
1305 return 0;
1306 if (shutdown_requested)
1307 return 0;
e568902a
AL
1308 if (debug_requested)
1309 return 0;
43b96858
AL
1310 return 1;
1311}
1312
d9c32310
BS
1313qemu_irq qemu_system_powerdown;
1314
43b96858
AL
1315static void main_loop(void)
1316{
6e29f5da 1317 int r;
e6e35b1e 1318
7277e027 1319 qemu_main_loop_start();
d6dc3d42 1320
6e29f5da 1321 for (;;) {
43b96858 1322 do {
d6f4ade2 1323 bool nonblocking = false;
e6e35b1e
AL
1324#ifdef CONFIG_PROFILER
1325 int64_t ti;
1326#endif
d6dc3d42 1327#ifndef CONFIG_IOTHREAD
d6f4ade2 1328 nonblocking = tcg_cpu_exec();
d6dc3d42 1329#endif
89bfc105 1330#ifdef CONFIG_PROFILER
43b96858 1331 ti = profile_getclock();
89bfc105 1332#endif
d6f4ade2 1333 main_loop_wait(nonblocking);
89bfc105 1334#ifdef CONFIG_PROFILER
43b96858 1335 dev_time += profile_getclock() - ti;
89bfc105 1336#endif
e568902a 1337 } while (vm_can_run());
43b96858 1338
54fc6ea9
BS
1339 if ((r = qemu_debug_requested())) {
1340 vm_stop(r);
b1a15e7e 1341 }
43b96858 1342 if (qemu_shutdown_requested()) {
242cd003 1343 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858
AL
1344 if (no_shutdown) {
1345 vm_stop(0);
1346 no_shutdown = 0;
1347 } else
1348 break;
1349 }
d6dc3d42
AL
1350 if (qemu_reset_requested()) {
1351 pause_all_vcpus();
43b96858 1352 qemu_system_reset();
d6dc3d42
AL
1353 resume_all_vcpus();
1354 }
d9c32310 1355 if (qemu_powerdown_requested()) {
242cd003 1356 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
1357 qemu_irq_raise(qemu_system_powerdown);
1358 }
b1a15e7e 1359 if ((r = qemu_vmstop_requested())) {
6e29f5da 1360 vm_stop(r);
b1a15e7e 1361 }
b4608c04 1362 }
2bc93fed 1363 bdrv_close_all();
d6dc3d42 1364 pause_all_vcpus();
b4608c04
FB
1365}
1366
9bd7e6d9
PB
1367static void version(void)
1368{
f75ca1ae 1369 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1370}
1371
15f82208 1372static void help(int exitcode)
0824d6fc 1373{
e8105ebb 1374 const char *options_help =
ad96090a
BS
1375#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1376 opt_help
5824d651 1377#define DEFHEADING(text) stringify(text) "\n"
9f16732a 1378#include "qemu-options.def"
5824d651
BS
1379#undef DEF
1380#undef DEFHEADING
1381#undef GEN_DOCS
e8105ebb
PB
1382 ;
1383 version();
1384 printf("usage: %s [options] [disk_image]\n"
1385 "\n"
f75ca1ae 1386 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
3f020d70 1387 "\n"
e8105ebb 1388 "%s\n"
3f020d70 1389 "During emulation, the following keys are useful:\n"
1390 "ctrl-alt-f toggle full screen\n"
1391 "ctrl-alt-n switch to virtual console 'n'\n"
1392 "ctrl-alt toggle mouse and keyboard grab\n"
1393 "\n"
e8105ebb
PB
1394 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395 "qemu",
1396 options_help);
15f82208 1397 exit(exitcode);
0824d6fc
FB
1398}
1399
cd6f1169
FB
1400#define HAS_ARG 0x0001
1401
cd6f1169
FB
1402typedef struct QEMUOption {
1403 const char *name;
1404 int flags;
1405 int index;
ad96090a 1406 uint32_t arch_mask;
cd6f1169
FB
1407} QEMUOption;
1408
dbed7e40 1409static const QEMUOption qemu_options[] = {
ad96090a
BS
1410 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 { option, opt_arg, opt_enum, arch_mask },
5824d651 1413#define DEFHEADING(text)
9f16732a 1414#include "qemu-options.def"
5824d651
BS
1415#undef DEF
1416#undef DEFHEADING
1417#undef GEN_DOCS
cd6f1169 1418 { NULL },
fc01f7e7 1419};
3893c124 1420static void select_vgahw (const char *p)
1421{
1422 const char *opts;
1423
64465297 1424 default_vga = 0;
86176759 1425 vga_interface_type = VGA_NONE;
3893c124 1426 if (strstart(p, "std", &opts)) {
86176759 1427 vga_interface_type = VGA_STD;
3893c124 1428 } else if (strstart(p, "cirrus", &opts)) {
86176759 1429 vga_interface_type = VGA_CIRRUS;
3893c124 1430 } else if (strstart(p, "vmware", &opts)) {
86176759 1431 vga_interface_type = VGA_VMWARE;
94909d9f 1432 } else if (strstart(p, "xenfb", &opts)) {
86176759 1433 vga_interface_type = VGA_XENFB;
28b85ed8 1434 } else if (!strstart(p, "none", &opts)) {
3893c124 1435 invalid_vga:
1436 fprintf(stderr, "Unknown vga type: %s\n", p);
1437 exit(1);
1438 }
cb5a7aa8 1439 while (*opts) {
1440 const char *nextopt;
1441
1442 if (strstart(opts, ",retrace=", &nextopt)) {
1443 opts = nextopt;
1444 if (strstart(opts, "dumb", &nextopt))
1445 vga_retrace_method = VGA_RETRACE_DUMB;
1446 else if (strstart(opts, "precise", &nextopt))
1447 vga_retrace_method = VGA_RETRACE_PRECISE;
1448 else goto invalid_vga;
1449 } else goto invalid_vga;
1450 opts = nextopt;
1451 }
3893c124 1452}
1453
7d4c3d53
MA
1454static int balloon_parse(const char *arg)
1455{
382f0743 1456 QemuOpts *opts;
7d4c3d53 1457
382f0743
GH
1458 if (strcmp(arg, "none") == 0) {
1459 return 0;
1460 }
1461
1462 if (!strncmp(arg, "virtio", 6)) {
1463 if (arg[6] == ',') {
1464 /* have params -> parse them */
8212c64f 1465 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
382f0743
GH
1466 if (!opts)
1467 return -1;
1468 } else {
1469 /* create empty opts */
1470 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
7d4c3d53 1471 }
382f0743
GH
1472 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473 return 0;
7d4c3d53 1474 }
382f0743
GH
1475
1476 return -1;
7d4c3d53 1477}
7d4c3d53 1478
5cea8590
PB
1479char *qemu_find_file(int type, const char *name)
1480{
1481 int len;
1482 const char *subdir;
1483 char *buf;
1484
1485 /* If name contains path separators then try it as a straight path. */
1486 if ((strchr(name, '/') || strchr(name, '\\'))
1487 && access(name, R_OK) == 0) {
73ffc805 1488 return qemu_strdup(name);
5cea8590
PB
1489 }
1490 switch (type) {
1491 case QEMU_FILE_TYPE_BIOS:
1492 subdir = "";
1493 break;
1494 case QEMU_FILE_TYPE_KEYMAP:
1495 subdir = "keymaps/";
1496 break;
1497 default:
1498 abort();
1499 }
1500 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501 buf = qemu_mallocz(len);
3a41759d 1502 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
1503 if (access(buf, R_OK)) {
1504 qemu_free(buf);
1505 return NULL;
1506 }
1507 return buf;
1508}
1509
ff952ba2
MA
1510static int device_help_func(QemuOpts *opts, void *opaque)
1511{
1512 return qdev_device_help(opts);
1513}
1514
f31d07d1
GH
1515static int device_init_func(QemuOpts *opts, void *opaque)
1516{
1517 DeviceState *dev;
1518
1519 dev = qdev_device_add(opts);
1520 if (!dev)
1521 return -1;
1522 return 0;
1523}
1524
1a688d3b
GH
1525static int chardev_init_func(QemuOpts *opts, void *opaque)
1526{
1527 CharDriverState *chr;
1528
1529 chr = qemu_chr_open_opts(opts, NULL);
1530 if (!chr)
1531 return -1;
1532 return 0;
1533}
1534
74db920c
GS
1535#ifdef CONFIG_LINUX
1536static int fsdev_init_func(QemuOpts *opts, void *opaque)
1537{
1538 int ret;
1539 ret = qemu_fsdev_add(opts);
1540
1541 return ret;
1542}
1543#endif
1544
88589343
GH
1545static int mon_init_func(QemuOpts *opts, void *opaque)
1546{
1547 CharDriverState *chr;
1548 const char *chardev;
1549 const char *mode;
1550 int flags;
1551
1552 mode = qemu_opt_get(opts, "mode");
1553 if (mode == NULL) {
1554 mode = "readline";
1555 }
1556 if (strcmp(mode, "readline") == 0) {
1557 flags = MONITOR_USE_READLINE;
1558 } else if (strcmp(mode, "control") == 0) {
1559 flags = MONITOR_USE_CONTROL;
1560 } else {
1561 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562 exit(1);
1563 }
1564
1565 if (qemu_opt_get_bool(opts, "default", 0))
1566 flags |= MONITOR_IS_DEFAULT;
1567
1568 chardev = qemu_opt_get(opts, "chardev");
1569 chr = qemu_chr_find(chardev);
1570 if (chr == NULL) {
1571 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1572 exit(1);
1573 }
1574
1575 monitor_init(chr, flags);
1576 return 0;
1577}
1578
6ca5582d 1579static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1580{
1581 static int monitor_device_index = 0;
1582 QemuOpts *opts;
1583 const char *p;
1584 char label[32];
1585 int def = 0;
1586
1587 if (strstart(optarg, "chardev:", &p)) {
1588 snprintf(label, sizeof(label), "%s", p);
1589 } else {
140e065d
JK
1590 snprintf(label, sizeof(label), "compat_monitor%d",
1591 monitor_device_index);
1592 if (monitor_device_index == 0) {
88589343
GH
1593 def = 1;
1594 }
1595 opts = qemu_chr_parse_compat(label, optarg);
1596 if (!opts) {
1597 fprintf(stderr, "parse error: %s\n", optarg);
1598 exit(1);
1599 }
1600 }
1601
1602 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1603 if (!opts) {
1604 fprintf(stderr, "duplicate chardev: %s\n", label);
1605 exit(1);
1606 }
6ca5582d 1607 qemu_opt_set(opts, "mode", mode);
88589343
GH
1608 qemu_opt_set(opts, "chardev", label);
1609 if (def)
1610 qemu_opt_set(opts, "default", "on");
1611 monitor_device_index++;
1612}
1613
bd3c948d
GH
1614struct device_config {
1615 enum {
aee1b935
GH
1616 DEV_USB, /* -usbdevice */
1617 DEV_BT, /* -bt */
1618 DEV_SERIAL, /* -serial */
1619 DEV_PARALLEL, /* -parallel */
1620 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1621 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
1622 } type;
1623 const char *cmdline;
72cf2d4f 1624 QTAILQ_ENTRY(device_config) next;
bd3c948d 1625};
72cf2d4f 1626QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1627
1628static void add_device_config(int type, const char *cmdline)
1629{
1630 struct device_config *conf;
1631
1632 conf = qemu_mallocz(sizeof(*conf));
1633 conf->type = type;
1634 conf->cmdline = cmdline;
72cf2d4f 1635 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1636}
1637
1638static int foreach_device_config(int type, int (*func)(const char *cmdline))
1639{
1640 struct device_config *conf;
1641 int rc;
1642
72cf2d4f 1643 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1644 if (conf->type != type)
1645 continue;
1646 rc = func(conf->cmdline);
1647 if (0 != rc)
1648 return rc;
1649 }
1650 return 0;
1651}
1652
998bbd74
GH
1653static int serial_parse(const char *devname)
1654{
1655 static int index = 0;
1656 char label[32];
1657
1658 if (strcmp(devname, "none") == 0)
1659 return 0;
1660 if (index == MAX_SERIAL_PORTS) {
1661 fprintf(stderr, "qemu: too many serial ports\n");
1662 exit(1);
1663 }
1664 snprintf(label, sizeof(label), "serial%d", index);
1665 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1666 if (!serial_hds[index]) {
1667 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1668 devname, strerror(errno));
1669 return -1;
1670 }
1671 index++;
1672 return 0;
1673}
1674
6a5e8b0e
GH
1675static int parallel_parse(const char *devname)
1676{
1677 static int index = 0;
1678 char label[32];
1679
1680 if (strcmp(devname, "none") == 0)
1681 return 0;
1682 if (index == MAX_PARALLEL_PORTS) {
1683 fprintf(stderr, "qemu: too many parallel ports\n");
1684 exit(1);
1685 }
1686 snprintf(label, sizeof(label), "parallel%d", index);
1687 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1688 if (!parallel_hds[index]) {
1689 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1690 devname, strerror(errno));
1691 return -1;
1692 }
1693 index++;
1694 return 0;
1695}
1696
aee1b935
GH
1697static int virtcon_parse(const char *devname)
1698{
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
AS
1709
1710 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1711 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1712
1713 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
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 }
1736 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
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
292444cb
AL
1857 /* first pass of option parsing */
1858 optind = 1;
1859 while (optind < argc) {
1860 if (argv[optind][0] != '-') {
1861 /* disk image */
28e68d68 1862 optind++;
292444cb
AL
1863 continue;
1864 } else {
1865 const QEMUOption *popt;
1866
1867 popt = lookup_opt(argc, argv, &optarg, &optind);
1868 switch (popt->index) {
1869 case QEMU_OPTION_nodefconfig:
1870 defconfig=0;
1871 break;
1872 }
1873 }
1874 }
1875
1876 if (defconfig) {
dcfb0939 1877 int ret;
cf5a65aa 1878
dcfb0939 1879 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 1880 if (ret < 0 && ret != -ENOENT) {
dcfb0939 1881 exit(1);
292444cb
AL
1882 }
1883
dcfb0939 1884 ret = qemu_read_config_file(arch_config_name);
019e78ba 1885 if (ret < 0 && ret != -ENOENT) {
dcfb0939 1886 exit(1);
292444cb
AL
1887 }
1888 }
de06f8d1 1889 cpudef_init();
292444cb
AL
1890
1891 /* second pass of option parsing */
cd6f1169 1892 optind = 1;
0824d6fc 1893 for(;;) {
cd6f1169 1894 if (optind >= argc)
0824d6fc 1895 break;
6530a97b 1896 if (argv[optind][0] != '-') {
9dfd7c7a 1897 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
cd6f1169
FB
1898 } else {
1899 const QEMUOption *popt;
1900
6530a97b 1901 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
1902 if (!(popt->arch_mask & arch_type)) {
1903 printf("Option %s not supported for this target\n", popt->name);
1904 exit(1);
1905 }
cd6f1169 1906 switch(popt->index) {
cc1daa40
FB
1907 case QEMU_OPTION_M:
1908 machine = find_machine(optarg);
1909 if (!machine) {
1910 QEMUMachine *m;
1911 printf("Supported machines are:\n");
1912 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
1913 if (m->alias)
1914 printf("%-10s %s (alias of %s)\n",
1915 m->alias, m->desc, m->name);
cc1daa40 1916 printf("%-10s %s%s\n",
5fafdf24 1917 m->name, m->desc,
0c257437 1918 m->is_default ? " (default)" : "");
cc1daa40 1919 }
15f82208 1920 exit(*optarg != '?');
cc1daa40
FB
1921 }
1922 break;
94fc95cd
JM
1923 case QEMU_OPTION_cpu:
1924 /* hw initialization will check this */
15f82208 1925 if (*optarg == '?') {
262353cb 1926 list_cpus(stdout, &fprintf, optarg);
15f82208 1927 exit(0);
94fc95cd
JM
1928 } else {
1929 cpu_model = optarg;
1930 }
1931 break;
cd6f1169 1932 case QEMU_OPTION_initrd:
fc01f7e7
FB
1933 initrd_filename = optarg;
1934 break;
cd6f1169 1935 case QEMU_OPTION_hda:
e4bcb14c 1936 if (cyls == 0)
9dfd7c7a 1937 hda_opts = drive_add(optarg, HD_ALIAS, 0);
e4bcb14c 1938 else
9dfd7c7a 1939 hda_opts = drive_add(optarg, HD_ALIAS
e4bcb14c 1940 ",cyls=%d,heads=%d,secs=%d%s",
609497ab 1941 0, cyls, heads, secs,
e4bcb14c
TS
1942 translation == BIOS_ATA_TRANSLATION_LBA ?
1943 ",trans=lba" :
1944 translation == BIOS_ATA_TRANSLATION_NONE ?
1945 ",trans=none" : "");
1946 break;
cd6f1169 1947 case QEMU_OPTION_hdb:
cc1daa40
FB
1948 case QEMU_OPTION_hdc:
1949 case QEMU_OPTION_hdd:
609497ab 1950 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
fc01f7e7 1951 break;
e4bcb14c 1952 case QEMU_OPTION_drive:
609497ab 1953 drive_add(NULL, "%s", optarg);
e4bcb14c 1954 break;
d058fe03
GH
1955 case QEMU_OPTION_set:
1956 if (qemu_set_option(optarg) != 0)
1957 exit(1);
1958 break;
d0fef6fb
GH
1959 case QEMU_OPTION_global:
1960 if (qemu_global_option(optarg) != 0)
1961 exit(1);
1962 break;
3e3d5815 1963 case QEMU_OPTION_mtdblock:
609497ab 1964 drive_add(optarg, MTD_ALIAS);
3e3d5815 1965 break;
a1bb27b1 1966 case QEMU_OPTION_sd:
609497ab 1967 drive_add(optarg, SD_ALIAS);
a1bb27b1 1968 break;
86f55663 1969 case QEMU_OPTION_pflash:
609497ab 1970 drive_add(optarg, PFLASH_ALIAS);
86f55663 1971 break;
cd6f1169 1972 case QEMU_OPTION_snapshot:
33e3963e
FB
1973 snapshot = 1;
1974 break;
cd6f1169 1975 case QEMU_OPTION_hdachs:
330d0414 1976 {
330d0414
FB
1977 const char *p;
1978 p = optarg;
1979 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
1980 if (cyls < 1 || cyls > 16383)
1981 goto chs_fail;
330d0414
FB
1982 if (*p != ',')
1983 goto chs_fail;
1984 p++;
1985 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
1986 if (heads < 1 || heads > 16)
1987 goto chs_fail;
330d0414
FB
1988 if (*p != ',')
1989 goto chs_fail;
1990 p++;
1991 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
1992 if (secs < 1 || secs > 63)
1993 goto chs_fail;
1994 if (*p == ',') {
1995 p++;
1996 if (!strcmp(p, "none"))
1997 translation = BIOS_ATA_TRANSLATION_NONE;
1998 else if (!strcmp(p, "lba"))
1999 translation = BIOS_ATA_TRANSLATION_LBA;
2000 else if (!strcmp(p, "auto"))
2001 translation = BIOS_ATA_TRANSLATION_AUTO;
2002 else
2003 goto chs_fail;
2004 } else if (*p != '\0') {
c4b1fcc0 2005 chs_fail:
46d4767d
FB
2006 fprintf(stderr, "qemu: invalid physical CHS format\n");
2007 exit(1);
c4b1fcc0 2008 }
9dfd7c7a
GH
2009 if (hda_opts != NULL) {
2010 char num[16];
2011 snprintf(num, sizeof(num), "%d", cyls);
2012 qemu_opt_set(hda_opts, "cyls", num);
2013 snprintf(num, sizeof(num), "%d", heads);
2014 qemu_opt_set(hda_opts, "heads", num);
2015 snprintf(num, sizeof(num), "%d", secs);
2016 qemu_opt_set(hda_opts, "secs", num);
2017 if (translation == BIOS_ATA_TRANSLATION_LBA)
2018 qemu_opt_set(hda_opts, "trans", "lba");
2019 if (translation == BIOS_ATA_TRANSLATION_NONE)
2020 qemu_opt_set(hda_opts, "trans", "none");
2021 }
330d0414
FB
2022 }
2023 break;
268a362c
AL
2024 case QEMU_OPTION_numa:
2025 if (nb_numa_nodes >= MAX_NODES) {
2026 fprintf(stderr, "qemu: too many NUMA nodes\n");
2027 exit(1);
2028 }
2029 numa_add(optarg);
2030 break;
cd6f1169 2031 case QEMU_OPTION_nographic:
993fbfdb 2032 display_type = DT_NOGRAPHIC;
a20dd508 2033 break;
4d3b6f6e
AZ
2034#ifdef CONFIG_CURSES
2035 case QEMU_OPTION_curses:
993fbfdb 2036 display_type = DT_CURSES;
4d3b6f6e
AZ
2037 break;
2038#endif
a171fe39
AZ
2039 case QEMU_OPTION_portrait:
2040 graphic_rotate = 1;
2041 break;
cd6f1169 2042 case QEMU_OPTION_kernel:
a20dd508
FB
2043 kernel_filename = optarg;
2044 break;
cd6f1169 2045 case QEMU_OPTION_append:
a20dd508 2046 kernel_cmdline = optarg;
313aa567 2047 break;
cd6f1169 2048 case QEMU_OPTION_cdrom:
609497ab 2049 drive_add(optarg, CDROM_ALIAS);
36b486bb 2050 break;
cd6f1169 2051 case QEMU_OPTION_boot:
28c5af54 2052 {
ef3adf68 2053 static const char * const params[] = {
95387491 2054 "order", "once", "menu", NULL
ef3adf68
JK
2055 };
2056 char buf[sizeof(boot_devices)];
e0f084bf 2057 char *standard_boot_devices;
ef3adf68
JK
2058 int legacy = 0;
2059
2060 if (!strchr(optarg, '=')) {
2061 legacy = 1;
2062 pstrcpy(buf, sizeof(buf), optarg);
2063 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2064 fprintf(stderr,
2065 "qemu: unknown boot parameter '%s' in '%s'\n",
2066 buf, optarg);
2067 exit(1);
2068 }
2069
2070 if (legacy ||
2071 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2072 validate_bootdevices(buf);
ef3adf68 2073 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2074 }
e0f084bf
JK
2075 if (!legacy) {
2076 if (get_param_value(buf, sizeof(buf),
2077 "once", optarg)) {
4e9e9d6e 2078 validate_bootdevices(buf);
e0f084bf
JK
2079 standard_boot_devices = qemu_strdup(boot_devices);
2080 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2081 qemu_register_reset(restore_boot_devices,
2082 standard_boot_devices);
2083 }
95387491
JK
2084 if (get_param_value(buf, sizeof(buf),
2085 "menu", optarg)) {
2086 if (!strcmp(buf, "on")) {
2087 boot_menu = 1;
2088 } else if (!strcmp(buf, "off")) {
2089 boot_menu = 0;
2090 } else {
2091 fprintf(stderr,
2092 "qemu: invalid option value '%s'\n",
2093 buf);
2094 exit(1);
2095 }
2096 }
e0f084bf 2097 }
36b486bb
FB
2098 }
2099 break;
cd6f1169 2100 case QEMU_OPTION_fda:
cd6f1169 2101 case QEMU_OPTION_fdb:
609497ab 2102 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
c45886db 2103 break;
52ca8d6a
FB
2104 case QEMU_OPTION_no_fd_bootchk:
2105 fd_bootchk = 0;
2106 break;
a1ea458f
MM
2107 case QEMU_OPTION_netdev:
2108 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2109 exit(1);
2110 }
2111 break;
7c9d8e07 2112 case QEMU_OPTION_net:
7f161aae 2113 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
c4b1fcc0
FB
2114 exit(1);
2115 }
702c651c 2116 break;
c7f74643
FB
2117#ifdef CONFIG_SLIRP
2118 case QEMU_OPTION_tftp:
ad196a9d 2119 legacy_tftp_prefix = optarg;
9bf05444 2120 break;
47d5d01a 2121 case QEMU_OPTION_bootp:
ad196a9d 2122 legacy_bootp_filename = optarg;
47d5d01a 2123 break;
9bf05444 2124 case QEMU_OPTION_redir:
0752706d
MA
2125 if (net_slirp_redir(optarg) < 0)
2126 exit(1);
9bf05444 2127 break;
c7f74643 2128#endif
dc72ac14 2129 case QEMU_OPTION_bt:
bd3c948d 2130 add_device_config(DEV_BT, optarg);
dc72ac14 2131 break;
1d14ffa9 2132 case QEMU_OPTION_audio_help:
ad96090a
BS
2133 if (!(audio_available())) {
2134 printf("Option %s not supported for this target\n", popt->name);
2135 exit(1);
2136 }
1d14ffa9
FB
2137 AUD_help ();
2138 exit (0);
2139 break;
2140 case QEMU_OPTION_soundhw:
ad96090a
BS
2141 if (!(audio_available())) {
2142 printf("Option %s not supported for this target\n", popt->name);
2143 exit(1);
2144 }
1d14ffa9
FB
2145 select_soundhw (optarg);
2146 break;
cd6f1169 2147 case QEMU_OPTION_h:
15f82208 2148 help(0);
cd6f1169 2149 break;
9bd7e6d9
PB
2150 case QEMU_OPTION_version:
2151 version();
2152 exit(0);
2153 break;
00f82b8a
AJ
2154 case QEMU_OPTION_m: {
2155 uint64_t value;
2156 char *ptr;
2157
2158 value = strtoul(optarg, &ptr, 10);
2159 switch (*ptr) {
2160 case 0: case 'M': case 'm':
2161 value <<= 20;
2162 break;
2163 case 'G': case 'g':
2164 value <<= 30;
2165 break;
2166 default:
2167 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2168 exit(1);
2169 }
00f82b8a
AJ
2170
2171 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 2172 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
2173 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2174 exit(1);
2175 }
c227f099 2176 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2177 fprintf(stderr, "qemu: ram size too large\n");
2178 exit(1);
2179 }
2180 ram_size = value;
cd6f1169 2181 break;
00f82b8a 2182 }
c902760f
MT
2183 case QEMU_OPTION_mempath:
2184 mem_path = optarg;
2185 break;
2186#ifdef MAP_POPULATE
2187 case QEMU_OPTION_mem_prealloc:
2188 mem_prealloc = 1;
2189 break;
2190#endif
cd6f1169 2191 case QEMU_OPTION_d:
de06f8d1 2192 set_cpu_log(optarg);
cd6f1169 2193 break;
cd6f1169 2194 case QEMU_OPTION_s:
59030a8c 2195 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 2196 break;
59030a8c
AL
2197 case QEMU_OPTION_gdb:
2198 gdbstub_dev = optarg;
cd6f1169 2199 break;
cd6f1169 2200 case QEMU_OPTION_L:
5cea8590 2201 data_dir = optarg;
cd6f1169 2202 break;
1192dad8
JM
2203 case QEMU_OPTION_bios:
2204 bios_name = optarg;
2205 break;
1b530a6d
AJ
2206 case QEMU_OPTION_singlestep:
2207 singlestep = 1;
2208 break;
cd6f1169 2209 case QEMU_OPTION_S:
3c07f8e8 2210 autostart = 0;
cd6f1169 2211 break;
3d11d0eb
FB
2212 case QEMU_OPTION_k:
2213 keyboard_layout = optarg;
2214 break;
ee22c2f7
FB
2215 case QEMU_OPTION_localtime:
2216 rtc_utc = 0;
2217 break;
3893c124 2218 case QEMU_OPTION_vga:
2219 select_vgahw (optarg);
1bfe856e 2220 break;
e9b137c2
FB
2221 case QEMU_OPTION_g:
2222 {
2223 const char *p;
2224 int w, h, depth;
2225 p = optarg;
2226 w = strtol(p, (char **)&p, 10);
2227 if (w <= 0) {
2228 graphic_error:
2229 fprintf(stderr, "qemu: invalid resolution or depth\n");
2230 exit(1);
2231 }
2232 if (*p != 'x')
2233 goto graphic_error;
2234 p++;
2235 h = strtol(p, (char **)&p, 10);
2236 if (h <= 0)
2237 goto graphic_error;
2238 if (*p == 'x') {
2239 p++;
2240 depth = strtol(p, (char **)&p, 10);
5fafdf24 2241 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2242 depth != 24 && depth != 32)
2243 goto graphic_error;
2244 } else if (*p == '\0') {
2245 depth = graphic_depth;
2246 } else {
2247 goto graphic_error;
2248 }
3b46e624 2249
e9b137c2
FB
2250 graphic_width = w;
2251 graphic_height = h;
2252 graphic_depth = depth;
2253 }
2254 break;
20d8a3ed
TS
2255 case QEMU_OPTION_echr:
2256 {
2257 char *r;
2258 term_escape_char = strtol(optarg, &r, 0);
2259 if (r == optarg)
2260 printf("Bad argument to echr\n");
2261 break;
2262 }
82c643ff 2263 case QEMU_OPTION_monitor:
6ca5582d
GH
2264 monitor_parse(optarg, "readline");
2265 default_monitor = 0;
2266 break;
2267 case QEMU_OPTION_qmp:
2268 monitor_parse(optarg, "control");
2d114dc1 2269 default_monitor = 0;
82c643ff 2270 break;
22a0e04b 2271 case QEMU_OPTION_mon:
8212c64f 2272 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
22a0e04b 2273 if (!opts) {
22a0e04b
GH
2274 exit(1);
2275 }
2d114dc1 2276 default_monitor = 0;
22a0e04b 2277 break;
191bc01b 2278 case QEMU_OPTION_chardev:
8212c64f 2279 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
191bc01b 2280 if (!opts) {
191bc01b
GH
2281 exit(1);
2282 }
191bc01b 2283 break;
74db920c
GS
2284#ifdef CONFIG_LINUX
2285 case QEMU_OPTION_fsdev:
2286 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2287 if (!opts) {
2288 fprintf(stderr, "parse error: %s\n", optarg);
2289 exit(1);
2290 }
2291 break;
3d54abc7
GS
2292 case QEMU_OPTION_virtfs: {
2293 char *arg_fsdev = NULL;
2294 char *arg_9p = NULL;
2295 int len = 0;
2296
2297 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2298 if (!opts) {
2299 fprintf(stderr, "parse error: %s\n", optarg);
2300 exit(1);
2301 }
2302
9ce56db6
VJJ
2303 if (qemu_opt_get(opts, "fstype") == NULL ||
2304 qemu_opt_get(opts, "mount_tag") == NULL ||
2305 qemu_opt_get(opts, "path") == NULL ||
2306 qemu_opt_get(opts, "security_model") == NULL) {
2307 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2308 "security_model=[mapped|passthrough],"
2309 "mnt_tag=tag.\n");
2310 exit(1);
2311 }
2312
2313 len = strlen(",id=,path=,security_model=");
3d54abc7
GS
2314 len += strlen(qemu_opt_get(opts, "fstype"));
2315 len += strlen(qemu_opt_get(opts, "mount_tag"));
2316 len += strlen(qemu_opt_get(opts, "path"));
9ce56db6 2317 len += strlen(qemu_opt_get(opts, "security_model"));
3d54abc7
GS
2318 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2319
2320 if (!arg_fsdev) {
2321 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2322 optarg);
2323 exit(1);
2324 }
2325
9ce56db6 2326 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
3d54abc7
GS
2327 qemu_opt_get(opts, "fstype"),
2328 qemu_opt_get(opts, "mount_tag"),
9ce56db6
VJJ
2329 qemu_opt_get(opts, "path"),
2330 qemu_opt_get(opts, "security_model"));
3d54abc7
GS
2331
2332 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2333 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2334 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2335
2336 if (!arg_9p) {
2337 fprintf(stderr, "No memory to parse -device for %s\n",
2338 optarg);
2339 exit(1);
2340 }
2341
2342 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2343 qemu_opt_get(opts, "mount_tag"),
2344 qemu_opt_get(opts, "mount_tag"));
2345
2346 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2347 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2348 exit(1);
2349 }
2350
2351 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2352 fprintf(stderr, "parse error [device]: %s\n", optarg);
2353 exit(1);
2354 }
2355
2356 qemu_free(arg_fsdev);
2357 qemu_free(arg_9p);
2358 break;
2359 }
74db920c 2360#endif
82c643ff 2361 case QEMU_OPTION_serial:
998bbd74
GH
2362 add_device_config(DEV_SERIAL, optarg);
2363 default_serial = 0;
18141ed6
JK
2364 if (strncmp(optarg, "mon:", 4) == 0) {
2365 default_monitor = 0;
2366 }
82c643ff 2367 break;
9dd986cc 2368 case QEMU_OPTION_watchdog:
09aaa160
MA
2369 if (watchdog) {
2370 fprintf(stderr,
2371 "qemu: only one watchdog option may be given\n");
2372 return 1;
2373 }
2374 watchdog = optarg;
9dd986cc
RJ
2375 break;
2376 case QEMU_OPTION_watchdog_action:
2377 if (select_watchdog_action(optarg) == -1) {
2378 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2379 exit(1);
2380 }
2381 break;
51ecf136 2382 case QEMU_OPTION_virtiocon:
aee1b935
GH
2383 add_device_config(DEV_VIRTCON, optarg);
2384 default_virtcon = 0;
18141ed6
JK
2385 if (strncmp(optarg, "mon:", 4) == 0) {
2386 default_monitor = 0;
2387 }
51ecf136 2388 break;
6508fe59 2389 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2390 add_device_config(DEV_PARALLEL, optarg);
2391 default_parallel = 0;
18141ed6
JK
2392 if (strncmp(optarg, "mon:", 4) == 0) {
2393 default_monitor = 0;
2394 }
6508fe59 2395 break;
c9f398e5
PA
2396 case QEMU_OPTION_debugcon:
2397 add_device_config(DEV_DEBUGCON, optarg);
2398 break;
d63d307f
FB
2399 case QEMU_OPTION_loadvm:
2400 loadvm = optarg;
2401 break;
2402 case QEMU_OPTION_full_screen:
2403 full_screen = 1;
2404 break;
667accab 2405#ifdef CONFIG_SDL
43523e93
TS
2406 case QEMU_OPTION_no_frame:
2407 no_frame = 1;
2408 break;
3780e197
TS
2409 case QEMU_OPTION_alt_grab:
2410 alt_grab = 1;
2411 break;
0ca9f8a4
DK
2412 case QEMU_OPTION_ctrl_grab:
2413 ctrl_grab = 1;
2414 break;
667accab
TS
2415 case QEMU_OPTION_no_quit:
2416 no_quit = 1;
2417 break;
7d957bd8 2418 case QEMU_OPTION_sdl:
993fbfdb 2419 display_type = DT_SDL;
7d957bd8 2420 break;
667accab 2421#endif
f7cce898 2422 case QEMU_OPTION_pidfile:
93815bc2 2423 pid_file = optarg;
f7cce898 2424 break;
a09db21f
FB
2425 case QEMU_OPTION_win2k_hack:
2426 win2k_install_hack = 1;
2427 break;
73822ec8
AL
2428 case QEMU_OPTION_rtc_td_hack:
2429 rtc_td_hack = 1;
2430 break;
8a92ea2f 2431 case QEMU_OPTION_acpitable:
de06f8d1 2432 do_acpitable_option(optarg);
8a92ea2f 2433 break;
b6f6e3d3 2434 case QEMU_OPTION_smbios:
de06f8d1 2435 do_smbios_option(optarg);
b6f6e3d3 2436 break;
7ba1e619
AL
2437 case QEMU_OPTION_enable_kvm:
2438 kvm_allowed = 1;
7ba1e619 2439 break;
bb36d470
FB
2440 case QEMU_OPTION_usb:
2441 usb_enabled = 1;
2442 break;
a594cfbf
FB
2443 case QEMU_OPTION_usbdevice:
2444 usb_enabled = 1;
bd3c948d
GH
2445 add_device_config(DEV_USB, optarg);
2446 break;
2447 case QEMU_OPTION_device:
8212c64f 2448 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
f31d07d1
GH
2449 exit(1);
2450 }
a594cfbf 2451 break;
6a00d601 2452 case QEMU_OPTION_smp:
dc6b1c09 2453 smp_parse(optarg);
b2097003 2454 if (smp_cpus < 1) {
6a00d601
FB
2455 fprintf(stderr, "Invalid number of CPUs\n");
2456 exit(1);
2457 }
6be68d7e
JS
2458 if (max_cpus < smp_cpus) {
2459 fprintf(stderr, "maxcpus must be equal to or greater than "
2460 "smp\n");
2461 exit(1);
2462 }
2463 if (max_cpus > 255) {
2464 fprintf(stderr, "Unsupported number of maxcpus\n");
2465 exit(1);
2466 }
6a00d601 2467 break;
24236869 2468 case QEMU_OPTION_vnc:
993fbfdb 2469 display_type = DT_VNC;
73fc9742 2470 vnc_display = optarg;
24236869 2471 break;
6515b203
FB
2472 case QEMU_OPTION_no_acpi:
2473 acpi_enabled = 0;
2474 break;
16b29ae1
AL
2475 case QEMU_OPTION_no_hpet:
2476 no_hpet = 1;
2477 break;
7d4c3d53
MA
2478 case QEMU_OPTION_balloon:
2479 if (balloon_parse(optarg) < 0) {
2480 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2481 exit(1);
2482 }
df97b920 2483 break;
d1beab82
FB
2484 case QEMU_OPTION_no_reboot:
2485 no_reboot = 1;
2486 break;
b2f76161
AJ
2487 case QEMU_OPTION_no_shutdown:
2488 no_shutdown = 1;
2489 break;
9467cd46
AZ
2490 case QEMU_OPTION_show_cursor:
2491 cursor_hide = 0;
2492 break;
8fcb1b90
BS
2493 case QEMU_OPTION_uuid:
2494 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2495 fprintf(stderr, "Fail to parse UUID string."
2496 " Wrong format.\n");
2497 exit(1);
2498 }
2499 break;
9ae02555
TS
2500 case QEMU_OPTION_option_rom:
2501 if (nb_option_roms >= MAX_OPTION_ROMS) {
2502 fprintf(stderr, "Too many option ROMs\n");
2503 exit(1);
2504 }
2505 option_rom[nb_option_roms] = optarg;
2506 nb_option_roms++;
2507 break;
8e71621f
PB
2508 case QEMU_OPTION_semihosting:
2509 semihosting_enabled = 1;
2510 break;
c35734b2 2511 case QEMU_OPTION_name:
1889465a
AK
2512 qemu_name = qemu_strdup(optarg);
2513 {
2514 char *p = strchr(qemu_name, ',');
2515 if (p != NULL) {
2516 *p++ = 0;
2517 if (strncmp(p, "process=", 8)) {
2518 fprintf(stderr, "Unknown subargument %s to -name", p);
2519 exit(1);
2520 }
2521 p += 8;
ce798cf2 2522 os_set_proc_name(p);
1889465a
AK
2523 }
2524 }
c35734b2 2525 break;
66508601
BS
2526 case QEMU_OPTION_prom_env:
2527 if (nb_prom_envs >= MAX_PROM_ENVS) {
2528 fprintf(stderr, "Too many prom variables\n");
2529 exit(1);
2530 }
2531 prom_envs[nb_prom_envs] = optarg;
2532 nb_prom_envs++;
2533 break;
2b8f2d41
AZ
2534 case QEMU_OPTION_old_param:
2535 old_param = 1;
05ebd537 2536 break;
f3dcfada
TS
2537 case QEMU_OPTION_clock:
2538 configure_alarms(optarg);
2539 break;
7e0af5d0 2540 case QEMU_OPTION_startdate:
1ed2fc1f
JK
2541 configure_rtc_date_offset(optarg, 1);
2542 break;
2543 case QEMU_OPTION_rtc:
8212c64f 2544 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
1ed2fc1f 2545 if (!opts) {
1ed2fc1f 2546 exit(1);
7e0af5d0 2547 }
1ed2fc1f 2548 configure_rtc(opts);
7e0af5d0 2549 break;
26a5f13b
FB
2550 case QEMU_OPTION_tb_size:
2551 tb_size = strtol(optarg, NULL, 0);
2552 if (tb_size < 0)
2553 tb_size = 0;
2554 break;
2e70f6ef 2555 case QEMU_OPTION_icount:
4e3de9e9 2556 icount_option = optarg;
2e70f6ef 2557 break;
5bb7910a
AL
2558 case QEMU_OPTION_incoming:
2559 incoming = optarg;
2560 break;
d8c208dd
GH
2561 case QEMU_OPTION_nodefaults:
2562 default_serial = 0;
2563 default_parallel = 0;
aee1b935 2564 default_virtcon = 0;
d8c208dd
GH
2565 default_monitor = 0;
2566 default_vga = 0;
cb4522cc 2567 default_net = 0;
ac33f8fa
GH
2568 default_floppy = 0;
2569 default_cdrom = 0;
2570 default_sdcard = 0;
d8c208dd 2571 break;
e37630ca 2572 case QEMU_OPTION_xen_domid:
ad96090a
BS
2573 if (!(xen_available())) {
2574 printf("Option %s not supported for this target\n", popt->name);
2575 exit(1);
2576 }
e37630ca
AL
2577 xen_domid = atoi(optarg);
2578 break;
2579 case QEMU_OPTION_xen_create:
ad96090a
BS
2580 if (!(xen_available())) {
2581 printf("Option %s not supported for this target\n", popt->name);
2582 exit(1);
2583 }
e37630ca
AL
2584 xen_mode = XEN_CREATE;
2585 break;
2586 case QEMU_OPTION_xen_attach:
ad96090a
BS
2587 if (!(xen_available())) {
2588 printf("Option %s not supported for this target\n", popt->name);
2589 exit(1);
2590 }
e37630ca
AL
2591 xen_mode = XEN_ATTACH;
2592 break;
715a664a
GH
2593 case QEMU_OPTION_readconfig:
2594 {
dcfb0939
KW
2595 int ret = qemu_read_config_file(optarg);
2596 if (ret < 0) {
2597 fprintf(stderr, "read config %s: %s\n", optarg,
2598 strerror(-ret));
715a664a
GH
2599 exit(1);
2600 }
715a664a
GH
2601 break;
2602 }
2603 case QEMU_OPTION_writeconfig:
2604 {
2605 FILE *fp;
2606 if (strcmp(optarg, "-") == 0) {
2607 fp = stdout;
2608 } else {
2609 fp = fopen(optarg, "w");
2610 if (fp == NULL) {
2611 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2612 exit(1);
2613 }
2614 }
2615 qemu_config_write(fp);
2616 fclose(fp);
2617 break;
2618 }
59a5264b
JS
2619 default:
2620 os_parse_cmd_args(popt->index, optarg);
cd6f1169 2621 }
0824d6fc
FB
2622 }
2623 }
0f0bc3f1 2624 loc_set_none();
330d0414 2625
5cea8590
PB
2626 /* If no data_dir is specified then try to find it relative to the
2627 executable path. */
2628 if (!data_dir) {
6170540b 2629 data_dir = os_find_datadir(argv[0]);
5cea8590
PB
2630 }
2631 /* If all else fails use the install patch specified when building. */
2632 if (!data_dir) {
1dabe05c 2633 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
2634 }
2635
6be68d7e
JS
2636 /*
2637 * Default to max_cpus = smp_cpus, in case the user doesn't
2638 * specify a max_cpus value.
2639 */
2640 if (!max_cpus)
2641 max_cpus = smp_cpus;
2642
3d878caa 2643 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
2644 if (smp_cpus > machine->max_cpus) {
2645 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2646 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2647 machine->max_cpus);
2648 exit(1);
2649 }
2650
998bbd74 2651 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
d8bcbabf 2652 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
998bbd74 2653
986c5f78
GH
2654 if (machine->no_serial) {
2655 default_serial = 0;
2656 }
2657 if (machine->no_parallel) {
2658 default_parallel = 0;
2659 }
2660 if (!machine->use_virtcon) {
2661 default_virtcon = 0;
2662 }
2663 if (machine->no_vga) {
2664 default_vga = 0;
2665 }
ac33f8fa
GH
2666 if (machine->no_floppy) {
2667 default_floppy = 0;
2668 }
2669 if (machine->no_cdrom) {
2670 default_cdrom = 0;
2671 }
2672 if (machine->no_sdcard) {
2673 default_sdcard = 0;
2674 }
986c5f78 2675
993fbfdb 2676 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
2677 if (default_parallel)
2678 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
2679 if (default_serial && default_monitor) {
2680 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
2681 } else if (default_virtcon && default_monitor) {
2682 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
2683 } else {
2684 if (default_serial)
2685 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
2686 if (default_virtcon)
2687 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 2688 if (default_monitor)
6ca5582d 2689 monitor_parse("stdio", "readline");
e1c09175 2690 }
998bbd74
GH
2691 } else {
2692 if (default_serial)
2693 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
2694 if (default_parallel)
2695 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 2696 if (default_monitor)
6ca5582d 2697 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
2698 if (default_virtcon)
2699 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 2700 }
64465297
GH
2701 if (default_vga)
2702 vga_interface_type = VGA_CIRRUS;
bc0129d9 2703
a5829fd9
T
2704 socket_init();
2705
1a688d3b
GH
2706 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2707 exit(1);
74db920c
GS
2708#ifdef CONFIG_LINUX
2709 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2710 exit(1);
2711 }
2712#endif
1a688d3b 2713
eb505be1 2714 os_daemonize();
71e3ceb8 2715
aa26bb2d 2716 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 2717 os_pidfile_error();
93815bc2
TS
2718 exit(1);
2719 }
2720
98c8573e
PB
2721 if (kvm_allowed) {
2722 int ret = kvm_init(smp_cpus);
214910a7 2723 if (ret < 0) {
98c8573e
PB
2724 if (!kvm_available()) {
2725 printf("KVM not supported for this target\n");
2726 } else {
2727 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2728 }
214910a7
MT
2729 exit(1);
2730 }
2731 }
2732
3fcf7b6b
AL
2733 if (qemu_init_main_loop()) {
2734 fprintf(stderr, "qemu_init_main_loop failed\n");
2735 exit(1);
2736 }
a20dd508 2737 linux_boot = (kernel_filename != NULL);
6c41b272 2738
f8d39c01
TS
2739 if (!linux_boot && *kernel_cmdline != '\0') {
2740 fprintf(stderr, "-append only allowed with -kernel option\n");
2741 exit(1);
2742 }
2743
2744 if (!linux_boot && initrd_filename != NULL) {
2745 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2746 exit(1);
2747 }
2748
9156d763 2749 os_set_line_buffering();
3b46e624 2750
7183b4b4
AL
2751 if (init_timer_alarm() < 0) {
2752 fprintf(stderr, "could not initialize alarm timer\n");
2753 exit(1);
2754 }
4e3de9e9 2755 configure_icount(icount_option);
634fce96 2756
dc1c9fe8
MM
2757 if (net_init_clients() < 0) {
2758 exit(1);
702c651c 2759 }
f1510b2c 2760
dc72ac14 2761 /* init the bluetooth world */
bd3c948d
GH
2762 if (foreach_device_config(DEV_BT, bt_parse))
2763 exit(1);
dc72ac14 2764
0824d6fc 2765 /* init the memory */
94a6b54f
PB
2766 if (ram_size == 0)
2767 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 2768
26a5f13b
FB
2769 /* init the dynamic translator */
2770 cpu_exec_init_all(tb_size * 1024 * 1024);
2771
eb852011 2772 bdrv_init_with_whitelist();
c4b1fcc0 2773
c163b5ca 2774 blk_mig_init();
2775
ac33f8fa 2776 if (default_cdrom) {
aa40fc9c
GH
2777 /* we always create the cdrom drive, even if no disk is there */
2778 drive_add(NULL, CDROM_ALIAS);
ac33f8fa 2779 }
c4b1fcc0 2780
ac33f8fa 2781 if (default_floppy) {
aa40fc9c
GH
2782 /* we always create at least one floppy */
2783 drive_add(NULL, FD_ALIAS, 0);
ac33f8fa 2784 }
86f55663 2785
ac33f8fa 2786 if (default_sdcard) {
aa40fc9c
GH
2787 /* we always create one sd slot, even if no card is in it */
2788 drive_add(NULL, SD_ALIAS);
2789 }
9d413d1d 2790
e4bcb14c 2791 /* open the virtual block devices */
9dfd7c7a 2792 if (snapshot)
7282a033 2793 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
a803cb8e 2794 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 2795 exit(1);
3e3d5815 2796
c163b5ca 2797 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2798 ram_load, NULL);
8a7ddc38 2799
268a362c
AL
2800 if (nb_numa_nodes > 0) {
2801 int i;
2802
2803 if (nb_numa_nodes > smp_cpus) {
2804 nb_numa_nodes = smp_cpus;
2805 }
2806
2807 /* If no memory size if given for any node, assume the default case
2808 * and distribute the available memory equally across all nodes
2809 */
2810 for (i = 0; i < nb_numa_nodes; i++) {
2811 if (node_mem[i] != 0)
2812 break;
2813 }
2814 if (i == nb_numa_nodes) {
2815 uint64_t usedmem = 0;
2816
2817 /* On Linux, the each node's border has to be 8MB aligned,
2818 * the final node gets the rest.
2819 */
2820 for (i = 0; i < nb_numa_nodes - 1; i++) {
2821 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2822 usedmem += node_mem[i];
2823 }
2824 node_mem[i] = ram_size - usedmem;
2825 }
2826
2827 for (i = 0; i < nb_numa_nodes; i++) {
2828 if (node_cpumask[i] != 0)
2829 break;
2830 }
2831 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2832 * must cope with this anyway, because there are BIOSes out there in
2833 * real machines which also use this scheme.
2834 */
2835 if (i == nb_numa_nodes) {
2836 for (i = 0; i < smp_cpus; i++) {
2837 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2838 }
2839 }
2840 }
2841
157b9319
JK
2842 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2843 exit(1);
2844 }
2845
998bbd74
GH
2846 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2847 exit(1);
6a5e8b0e
GH
2848 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2849 exit(1);
aee1b935
GH
2850 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2851 exit(1);
c9f398e5
PA
2852 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2853 exit(1);
2796dae0 2854
aae9460e
PB
2855 module_call_init(MODULE_INIT_DEVICE);
2856
ff952ba2
MA
2857 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2858 exit(0);
2859
09aaa160
MA
2860 if (watchdog) {
2861 i = select_watchdog(watchdog);
2862 if (i > 0)
2863 exit (i == 1 ? 1 : 0);
2864 }
2865
b6b61144 2866 if (machine->compat_props) {
458fb679 2867 qdev_prop_register_global_list(machine->compat_props);
b6b61144 2868 }
d0fef6fb
GH
2869 qemu_add_globals();
2870
fbe1b595 2871 machine->init(ram_size, boot_devices,
3023f332
AL
2872 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2873
ea375f9a 2874 cpu_synchronize_all_post_init();
268a362c 2875
67b3b71d 2876 /* must be after terminal init, SDL library changes signal handlers */
8d963e6a 2877 os_setup_signal_handling();
67b3b71d 2878
87d0a28e 2879 set_numa_modes();
268a362c 2880
6f338c34
AL
2881 current_machine = machine;
2882
3023f332
AL
2883 /* init USB devices */
2884 if (usb_enabled) {
0752706d
MA
2885 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2886 exit(1);
3023f332
AL
2887 }
2888
bd3c948d 2889 /* init generic devices */
f31d07d1 2890 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
bd3c948d
GH
2891 exit(1);
2892
668680f7
MA
2893 net_check_clients();
2894
3023f332 2895 /* just use the first displaystate for the moment */
b473df6e 2896 ds = get_displaystate();
993fbfdb
AL
2897
2898 if (display_type == DT_DEFAULT) {
2899#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2900 display_type = DT_SDL;
2901#else
2902 display_type = DT_VNC;
2903 vnc_display = "localhost:0,to=99";
2904 show_vnc_port = 1;
2905#endif
2906 }
2907
2908
2909 switch (display_type) {
2910 case DT_NOGRAPHIC:
2911 break;
4d3b6f6e 2912#if defined(CONFIG_CURSES)
993fbfdb
AL
2913 case DT_CURSES:
2914 curses_display_init(ds, full_screen);
2915 break;
4d3b6f6e 2916#endif
5b0753e0 2917#if defined(CONFIG_SDL)
993fbfdb
AL
2918 case DT_SDL:
2919 sdl_display_init(ds, full_screen, no_frame);
2920 break;
5b0753e0 2921#elif defined(CONFIG_COCOA)
993fbfdb
AL
2922 case DT_SDL:
2923 cocoa_display_init(ds, full_screen);
2924 break;
313aa567 2925#endif
993fbfdb
AL
2926 case DT_VNC:
2927 vnc_display_init(ds);
2928 if (vnc_display_open(ds, vnc_display) < 0)
2929 exit(1);
f92f8afe 2930
993fbfdb
AL
2931 if (show_vnc_port) {
2932 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 2933 }
993fbfdb
AL
2934 break;
2935 default:
2936 break;
313aa567 2937 }
7d957bd8 2938 dpy_resize(ds);
5b08fc10 2939
3023f332
AL
2940 dcl = ds->listeners;
2941 while (dcl != NULL) {
2942 if (dcl->dpy_refresh != NULL) {
2943 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2944 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
0f2ad63f 2945 break;
20d8a3ed 2946 }
3023f332 2947 dcl = dcl->next;
20d8a3ed 2948 }
3023f332 2949
993fbfdb 2950 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
9043b62d
BS
2951 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2952 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2953 }
2954
b473df6e 2955 text_consoles_set_display(ds);
2796dae0 2956
59030a8c
AL
2957 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2958 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2959 gdbstub_dev);
2960 exit(1);
45669e00 2961 }
45669e00 2962
3418bd25
GH
2963 qdev_machine_creation_done();
2964
15ff7705
GH
2965 if (rom_load_all() != 0) {
2966 fprintf(stderr, "rom loading failed\n");
2967 exit(1);
2968 }
45a50b16 2969
504c2948 2970 qemu_system_reset();
05f2401e 2971 if (loadvm) {
03cd4655 2972 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
2973 autostart = 0;
2974 }
2975 }
d63d307f 2976
2bb8c10c 2977 if (incoming) {
5bb7910a 2978 qemu_start_incoming_migration(incoming);
6b99dadc 2979 } else if (autostart) {
c0f4ce77 2980 vm_start();
6b99dadc 2981 }
ffd843bc 2982
eb505be1 2983 os_setup_post();
71e3ceb8 2984
8a7ddc38 2985 main_loop();
40c3bac3 2986 quit_timers();
63a01ef8 2987 net_cleanup();
b46a8906 2988
0824d6fc
FB
2989 return 0;
2990}