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