]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
Refactor a few architecture dependent pieces in vl.c
[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>
0858532e 37#include <pwd.h>
67b915a5 38#include <sys/times.h>
f1510b2c 39#include <sys/wait.h>
67b915a5 40#include <termios.h>
67b915a5 41#include <sys/mman.h>
f1510b2c 42#include <sys/ioctl.h>
24646c7e 43#include <sys/resource.h>
f1510b2c 44#include <sys/socket.h>
c94c8d64 45#include <netinet/in.h>
24646c7e 46#include <net/if.h>
24646c7e 47#include <arpa/inet.h>
9d728e8c 48#include <dirent.h>
7c9d8e07 49#include <netdb.h>
cb4b976b 50#include <sys/select.h>
71e72a19 51#ifdef CONFIG_BSD
7d3505c5 52#include <sys/stat.h>
a167ba50 53#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
7d3505c5 54#include <libutil.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
TS
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
78#include <net/if.h>
79#include <syslog.h>
80#include <stropts.h>
8d32cf0e
BS
81/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83extern int madvise(caddr_t, size_t, int);
67b915a5 84#endif
7d3505c5 85#endif
ec530c81 86#endif
67b915a5 87
9892fbfb
BS
88#if defined(__OpenBSD__)
89#include <util.h>
90#endif
91
8a16d273
TS
92#if defined(CONFIG_VDE)
93#include <libvdeplug.h>
94#endif
95
67b915a5 96#ifdef _WIN32
49dc768d 97#include <windows.h>
67b915a5
FB
98#endif
99
73332e5c 100#ifdef CONFIG_SDL
59a36a2f 101#if defined(__APPLE__) || defined(main)
6693665a 102#include <SDL.h>
880fec5d 103int qemu_main(int argc, char **argv, char **envp);
104int main(int argc, char **argv)
105{
59a36a2f 106 return qemu_main(argc, argv, NULL);
880fec5d 107}
108#undef main
109#define main qemu_main
96bcd4f8 110#endif
73332e5c 111#endif /* CONFIG_SDL */
0824d6fc 112
5b0753e0
FB
113#ifdef CONFIG_COCOA
114#undef main
115#define main qemu_main
116#endif /* CONFIG_COCOA */
117
511d2b14
BS
118#include "hw/hw.h"
119#include "hw/boards.h"
120#include "hw/usb.h"
121#include "hw/pcmcia.h"
122#include "hw/pc.h"
123#include "hw/audiodev.h"
124#include "hw/isa.h"
125#include "hw/baum.h"
126#include "hw/bt.h"
9dd986cc 127#include "hw/watchdog.h"
b6f6e3d3 128#include "hw/smbios.h"
e37630ca 129#include "hw/xen.h"
bd3c948d 130#include "hw/qdev.h"
45a50b16 131#include "hw/loader.h"
5ef4efa4 132#include "bt-host.h"
511d2b14 133#include "net.h"
68ac40d2 134#include "net/slirp.h"
511d2b14
BS
135#include "monitor.h"
136#include "console.h"
137#include "sysemu.h"
138#include "gdbstub.h"
139#include "qemu-timer.h"
140#include "qemu-char.h"
141#include "cache-utils.h"
142#include "block.h"
c163b5ca
LS
143#include "block_int.h"
144#include "block-migration.h"
a718acec 145#include "dma.h"
511d2b14
BS
146#include "audio/audio.h"
147#include "migration.h"
148#include "kvm.h"
149#include "balloon.h"
d3f24367 150#include "qemu-option.h"
7282a033 151#include "qemu-config.h"
e78c48ec 152#include "qemu-objects.h"
511d2b14 153
0824d6fc 154#include "disas.h"
fc01f7e7 155
8a7ddc38 156#include "exec-all.h"
0824d6fc 157
511d2b14
BS
158#include "qemu_socket.h"
159
d918f23e 160#include "slirp/libslirp.h"
511d2b14 161
72cf2d4f
BS
162#include "qemu-queue.h"
163
9dc63a1e
BS
164//#define DEBUG_NET
165//#define DEBUG_SLIRP
330d0414 166
1bfe856e 167#define DEFAULT_RAM_SIZE 128
313aa567 168
98b19252
AS
169#define MAX_VIRTIO_CONSOLES 1
170
5cea8590 171static const char *data_dir;
1192dad8 172const char *bios_name = NULL;
e4bcb14c 173/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
faea38e7 174 to store the VM snapshots */
72cf2d4f
BS
175struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
176struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
cb5a7aa8 177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 178DisplayType display_type = DT_DEFAULT;
3d11d0eb 179const char* keyboard_layout = NULL;
c227f099 180ram_addr_t ram_size;
c902760f
MT
181const char *mem_path = NULL;
182#ifdef MAP_POPULATE
183int mem_prealloc = 0; /* force preallocation of physical target memory */
184#endif
c4b1fcc0 185int nb_nics;
7c9d8e07 186NICInfo nd_table[MAX_NICS];
8a7ddc38 187int vm_running;
d399f677 188int autostart;
f6503059
AZ
189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
6875204c 191QEMUClock *rtc_clock;
64465297 192int vga_interface_type = VGA_NONE;
d827220b
FB
193#ifdef TARGET_SPARC
194int graphic_width = 1024;
195int graphic_height = 768;
eee0b836 196int graphic_depth = 8;
d827220b 197#else
1bfe856e
FB
198int graphic_width = 800;
199int graphic_height = 600;
e9b137c2 200int graphic_depth = 15;
eee0b836 201#endif
dbed7e40 202static int full_screen = 0;
634a21f6 203#ifdef CONFIG_SDL
dbed7e40 204static int no_frame = 0;
634a21f6 205#endif
667accab 206int no_quit = 0;
8d11df9e 207CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 208CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 209CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 210int win2k_install_hack = 0;
f7736b91 211#ifdef TARGET_I386
73822ec8 212int rtc_td_hack = 0;
a09db21f 213#endif
bb36d470 214int usb_enabled = 0;
1b530a6d 215int singlestep = 0;
6a00d601 216int smp_cpus = 1;
6be68d7e 217int max_cpus = 0;
dc6b1c09
AP
218int smp_cores = 1;
219int smp_threads = 1;
73fc9742 220const char *vnc_display;
6515b203 221int acpi_enabled = 1;
16b29ae1 222int no_hpet = 0;
52ca8d6a 223int fd_bootchk = 1;
d1beab82 224int no_reboot = 0;
b2f76161 225int no_shutdown = 0;
9467cd46 226int cursor_hide = 1;
a171fe39 227int graphic_rotate = 0;
6b35e7bf 228uint8_t irq0override = 1;
b9e82a59 229#ifndef _WIN32
71e3ceb8 230int daemonize = 0;
b9e82a59 231#endif
09aaa160 232const char *watchdog;
9ae02555
TS
233const char *option_rom[MAX_OPTION_ROMS];
234int nb_option_roms;
8e71621f 235int semihosting_enabled = 0;
2b8f2d41
AZ
236#ifdef TARGET_ARM
237int old_param = 0;
238#endif
c35734b2 239const char *qemu_name;
3780e197 240int alt_grab = 0;
0ca9f8a4 241int ctrl_grab = 0;
95efd11c 242#if defined(TARGET_SPARC) || defined(TARGET_PPC)
66508601
BS
243unsigned int nb_prom_envs = 0;
244const char *prom_envs[MAX_PROM_ENVS];
245#endif
95387491 246int boot_menu;
0824d6fc 247
268a362c
AL
248int nb_numa_nodes;
249uint64_t node_mem[MAX_NODES];
250uint64_t node_cpumask[MAX_NODES];
251
ee5605e5
AZ
252static CPUState *cur_cpu;
253static CPUState *next_cpu;
9043b62d 254static QEMUTimer *nographic_timer;
ee5605e5 255
8fcb1b90
BS
256uint8_t qemu_uuid[16];
257
76e30d0f
JK
258static QEMUBootSetHandler *boot_set_handler;
259static void *boot_set_opaque;
260
d745bef8
BS
261int kvm_allowed = 0;
262uint32_t xen_domid;
263enum xen_mode xen_mode = XEN_EMULATE;
264
cc84de95
MT
265#ifdef SIGRTMIN
266#define SIG_IPI (SIGRTMIN+4)
267#else
268#define SIG_IPI SIGUSR1
269#endif
270
998bbd74 271static int default_serial = 1;
6a5e8b0e 272static int default_parallel = 1;
986c5f78 273static int default_virtcon = 1;
abdeed06 274static int default_monitor = 1;
64465297 275static int default_vga = 1;
ac33f8fa
GH
276static int default_floppy = 1;
277static int default_cdrom = 1;
278static int default_sdcard = 1;
998bbd74
GH
279
280static struct {
281 const char *driver;
282 int *flag;
283} default_list[] = {
6a5e8b0e
GH
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf
GH
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-drive", .flag = &default_cdrom },
392ecf54
AS
288 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
289 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
290 { .driver = "virtio-serial", .flag = &default_virtcon },
64465297 291 { .driver = "VGA", .flag = &default_vga },
69fd02ee
GH
292 { .driver = "cirrus-vga", .flag = &default_vga },
293 { .driver = "vmware-svga", .flag = &default_vga },
998bbd74
GH
294};
295
296static int default_driver_check(QemuOpts *opts, void *opaque)
297{
298 const char *driver = qemu_opt_get(opts, "driver");
299 int i;
300
301 if (!driver)
302 return 0;
303 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
304 if (strcmp(default_list[i].driver, driver) != 0)
305 continue;
306 *(default_list[i].flag) = 0;
307 }
308 return 0;
309}
310
0824d6fc 311/***********************************************************/
26aa7d72
FB
312/* x86 ISA bus support */
313
c227f099 314target_phys_addr_t isa_mem_base = 0;
3de388f6 315PicState2 *isa_pic;
0824d6fc 316
0824d6fc 317/***********************************************************/
0824d6fc
FB
318void hw_error(const char *fmt, ...)
319{
320 va_list ap;
6a00d601 321 CPUState *env;
0824d6fc
FB
322
323 va_start(ap, fmt);
324 fprintf(stderr, "qemu: hardware error: ");
325 vfprintf(stderr, fmt, ap);
326 fprintf(stderr, "\n");
6a00d601
FB
327 for(env = first_cpu; env != NULL; env = env->next_cpu) {
328 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
0824d6fc 329#ifdef TARGET_I386
6a00d601 330 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
c45886db 331#else
6a00d601 332 cpu_dump_state(env, stderr, fprintf, 0);
0824d6fc 333#endif
6a00d601 334 }
0824d6fc
FB
335 va_end(ap);
336 abort();
337}
1889465a
AK
338
339static void set_proc_name(const char *s)
340{
6ca8d0fd 341#if defined(__linux__) && defined(PR_SET_NAME)
1889465a
AK
342 char name[16];
343 if (!s)
344 return;
345 name[sizeof(name) - 1] = 0;
346 strncpy(name, s, sizeof(name));
347 /* Could rewrite argv[0] too, but that's a bit more complicated.
348 This simple way is enough for `top'. */
349 prctl(PR_SET_NAME, name);
350#endif
351}
df751fa8
AL
352
353/***************/
354/* ballooning */
355
356static QEMUBalloonEvent *qemu_balloon_event;
357void *qemu_balloon_event_opaque;
358
359void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
360{
361 qemu_balloon_event = func;
362 qemu_balloon_event_opaque = opaque;
363}
364
625a5bef 365int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
df751fa8 366{
625a5bef
AL
367 if (qemu_balloon_event) {
368 qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
369 return 1;
370 } else {
371 return 0;
372 }
df751fa8
AL
373}
374
625a5bef 375int qemu_balloon_status(MonitorCompletion cb, void *opaque)
df751fa8 376{
625a5bef
AL
377 if (qemu_balloon_event) {
378 qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
379 return 1;
380 } else {
381 return 0;
382 }
df751fa8 383}
0824d6fc 384
455204eb 385
8f0056b7
PB
386/***********************************************************/
387/* real time host monotonic timer */
09b26c5e 388
1dce7c3c
FB
389/* compute with 96 bit intermediate result: (a*b)/c */
390uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
0824d6fc 391{
1dce7c3c
FB
392 union {
393 uint64_t ll;
394 struct {
e2542fe2 395#ifdef HOST_WORDS_BIGENDIAN
1dce7c3c
FB
396 uint32_t high, low;
397#else
398 uint32_t low, high;
3b46e624 399#endif
1dce7c3c
FB
400 } l;
401 } u, res;
402 uint64_t rl, rh;
0824d6fc 403
1dce7c3c
FB
404 u.ll = a;
405 rl = (uint64_t)u.l.low * (uint64_t)b;
406 rh = (uint64_t)u.l.high * (uint64_t)b;
407 rh += (rl >> 32);
408 res.l.high = rh / c;
409 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
410 return res.ll;
34865134
FB
411}
412
f6503059
AZ
413/***********************************************************/
414/* host time/date access */
415void qemu_get_timedate(struct tm *tm, int offset)
416{
417 time_t ti;
418 struct tm *ret;
419
420 time(&ti);
421 ti += offset;
422 if (rtc_date_offset == -1) {
423 if (rtc_utc)
424 ret = gmtime(&ti);
425 else
426 ret = localtime(&ti);
427 } else {
428 ti -= rtc_date_offset;
429 ret = gmtime(&ti);
430 }
431
432 memcpy(tm, ret, sizeof(struct tm));
433}
434
435int qemu_timedate_diff(struct tm *tm)
436{
437 time_t seconds;
438
439 if (rtc_date_offset == -1)
440 if (rtc_utc)
441 seconds = mktimegm(tm);
442 else
443 seconds = mktime(tm);
444 else
445 seconds = mktimegm(tm) + rtc_date_offset;
446
447 return seconds - time(NULL);
448}
449
80cd3478
LC
450void rtc_change_mon_event(struct tm *tm)
451{
452 QObject *data;
453
454 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
455 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
456 qobject_decref(data);
457}
458
1ed2fc1f
JK
459static void configure_rtc_date_offset(const char *startdate, int legacy)
460{
461 time_t rtc_start_date;
462 struct tm tm;
463
464 if (!strcmp(startdate, "now") && legacy) {
465 rtc_date_offset = -1;
466 } else {
467 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
468 &tm.tm_year,
469 &tm.tm_mon,
470 &tm.tm_mday,
471 &tm.tm_hour,
472 &tm.tm_min,
473 &tm.tm_sec) == 6) {
474 /* OK */
475 } else if (sscanf(startdate, "%d-%d-%d",
476 &tm.tm_year,
477 &tm.tm_mon,
478 &tm.tm_mday) == 3) {
479 tm.tm_hour = 0;
480 tm.tm_min = 0;
481 tm.tm_sec = 0;
482 } else {
483 goto date_fail;
484 }
485 tm.tm_year -= 1900;
486 tm.tm_mon--;
487 rtc_start_date = mktimegm(&tm);
488 if (rtc_start_date == -1) {
489 date_fail:
490 fprintf(stderr, "Invalid date format. Valid formats are:\n"
491 "'2006-06-17T16:01:21' or '2006-06-17'\n");
492 exit(1);
493 }
494 rtc_date_offset = time(NULL) - rtc_start_date;
495 }
496}
497
498static void configure_rtc(QemuOpts *opts)
499{
500 const char *value;
501
502 value = qemu_opt_get(opts, "base");
503 if (value) {
504 if (!strcmp(value, "utc")) {
505 rtc_utc = 1;
506 } else if (!strcmp(value, "localtime")) {
507 rtc_utc = 0;
508 } else {
509 configure_rtc_date_offset(value, 0);
510 }
511 }
6875204c
JK
512 value = qemu_opt_get(opts, "clock");
513 if (value) {
514 if (!strcmp(value, "host")) {
515 rtc_clock = host_clock;
516 } else if (!strcmp(value, "vm")) {
517 rtc_clock = vm_clock;
518 } else {
519 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
520 exit(1);
521 }
522 }
7e4c0336 523#ifdef TARGET_I386
1ed2fc1f
JK
524 value = qemu_opt_get(opts, "driftfix");
525 if (value) {
7e4c0336 526 if (!strcmp(value, "slew")) {
1ed2fc1f 527 rtc_td_hack = 1;
7e4c0336 528 } else if (!strcmp(value, "none")) {
1ed2fc1f
JK
529 rtc_td_hack = 0;
530 } else {
531 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
532 exit(1);
533 }
534 }
535#endif
536}
537
fd1dff4b 538#ifdef _WIN32
28695489 539static void socket_cleanup(void)
fd1dff4b
FB
540{
541 WSACleanup();
542}
82c643ff 543
fd1dff4b
FB
544static int socket_init(void)
545{
546 WSADATA Data;
547 int ret, err;
548
549 ret = WSAStartup(MAKEWORD(2,2), &Data);
550 if (ret != 0) {
551 err = WSAGetLastError();
552 fprintf(stderr, "WSAStartup: %d\n", err);
553 return -1;
554 }
28695489 555 atexit(socket_cleanup);
fd1dff4b
FB
556 return 0;
557}
64b7b733
AJ
558#endif
559
1ae26a18
AZ
560/***********************************************************/
561/* Bluetooth support */
562static int nb_hcis;
563static int cur_hci;
564static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 565
1ae26a18
AZ
566static struct bt_vlan_s {
567 struct bt_scatternet_s net;
568 int id;
569 struct bt_vlan_s *next;
570} *first_bt_vlan;
571
572/* find or alloc a new bluetooth "VLAN" */
674bb261 573static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
574{
575 struct bt_vlan_s **pvlan, *vlan;
576 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
577 if (vlan->id == id)
578 return &vlan->net;
579 }
580 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
581 vlan->id = id;
582 pvlan = &first_bt_vlan;
583 while (*pvlan != NULL)
584 pvlan = &(*pvlan)->next;
585 *pvlan = vlan;
586 return &vlan->net;
587}
588
589static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
590{
591}
592
593static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
594{
595 return -ENOTSUP;
596}
597
598static struct HCIInfo null_hci = {
599 .cmd_send = null_hci_send,
600 .sco_send = null_hci_send,
601 .acl_send = null_hci_send,
602 .bdaddr_set = null_hci_addr_set,
603};
604
605struct HCIInfo *qemu_next_hci(void)
606{
607 if (cur_hci == nb_hcis)
608 return &null_hci;
609
610 return hci_table[cur_hci++];
611}
612
dc72ac14
AZ
613static struct HCIInfo *hci_init(const char *str)
614{
615 char *endp;
616 struct bt_scatternet_s *vlan = 0;
617
618 if (!strcmp(str, "null"))
619 /* null */
620 return &null_hci;
621 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
622 /* host[:hciN] */
623 return bt_host_hci(str[4] ? str + 5 : "hci0");
624 else if (!strncmp(str, "hci", 3)) {
625 /* hci[,vlan=n] */
626 if (str[3]) {
627 if (!strncmp(str + 3, ",vlan=", 6)) {
628 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
629 if (*endp)
630 vlan = 0;
631 }
632 } else
633 vlan = qemu_find_bt_vlan(0);
634 if (vlan)
635 return bt_new_hci(vlan);
636 }
637
638 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
639
640 return 0;
641}
642
643static int bt_hci_parse(const char *str)
644{
645 struct HCIInfo *hci;
c227f099 646 bdaddr_t bdaddr;
dc72ac14
AZ
647
648 if (nb_hcis >= MAX_NICS) {
649 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
650 return -1;
651 }
652
653 hci = hci_init(str);
654 if (!hci)
655 return -1;
656
657 bdaddr.b[0] = 0x52;
658 bdaddr.b[1] = 0x54;
659 bdaddr.b[2] = 0x00;
660 bdaddr.b[3] = 0x12;
661 bdaddr.b[4] = 0x34;
662 bdaddr.b[5] = 0x56 + nb_hcis;
663 hci->bdaddr_set(hci, bdaddr.b);
664
665 hci_table[nb_hcis++] = hci;
666
667 return 0;
668}
669
670static void bt_vhci_add(int vlan_id)
671{
672 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
673
674 if (!vlan->slave)
675 fprintf(stderr, "qemu: warning: adding a VHCI to "
676 "an empty scatternet %i\n", vlan_id);
677
678 bt_vhci_init(bt_new_hci(vlan));
679}
680
681static struct bt_device_s *bt_device_add(const char *opt)
682{
683 struct bt_scatternet_s *vlan;
684 int vlan_id = 0;
685 char *endp = strstr(opt, ",vlan=");
686 int len = (endp ? endp - opt : strlen(opt)) + 1;
687 char devname[10];
688
689 pstrcpy(devname, MIN(sizeof(devname), len), opt);
690
691 if (endp) {
692 vlan_id = strtol(endp + 6, &endp, 0);
693 if (*endp) {
694 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
695 return 0;
696 }
697 }
698
699 vlan = qemu_find_bt_vlan(vlan_id);
700
701 if (!vlan->slave)
702 fprintf(stderr, "qemu: warning: adding a slave device to "
703 "an empty scatternet %i\n", vlan_id);
704
705 if (!strcmp(devname, "keyboard"))
706 return bt_keyboard_init(vlan);
707
708 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
709 return 0;
710}
711
712static int bt_parse(const char *opt)
713{
714 const char *endp, *p;
715 int vlan;
716
717 if (strstart(opt, "hci", &endp)) {
718 if (!*endp || *endp == ',') {
719 if (*endp)
720 if (!strstart(endp, ",vlan=", 0))
721 opt = endp + 1;
722
723 return bt_hci_parse(opt);
724 }
725 } else if (strstart(opt, "vhci", &endp)) {
726 if (!*endp || *endp == ',') {
727 if (*endp) {
728 if (strstart(endp, ",vlan=", &p)) {
729 vlan = strtol(p, (char **) &endp, 0);
730 if (*endp) {
731 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
732 return 1;
733 }
734 } else {
735 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
736 return 1;
737 }
738 } else
739 vlan = 0;
740
741 bt_vhci_add(vlan);
742 return 0;
743 }
744 } else if (strstart(opt, "device:", &endp))
745 return !bt_device_add(endp);
746
747 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
748 return 1;
749}
750
1ae26a18
AZ
751/***********************************************************/
752/* QEMU Block devices */
753
609497ab 754#define HD_ALIAS "index=%d,media=disk"
e4bcb14c 755#define CDROM_ALIAS "index=2,media=cdrom"
e4bcb14c 756#define FD_ALIAS "index=%d,if=floppy"
609497ab
AZ
757#define PFLASH_ALIAS "if=pflash"
758#define MTD_ALIAS "if=mtd"
9d413d1d 759#define SD_ALIAS "index=0,if=sd"
e4bcb14c 760
9dfd7c7a 761QemuOpts *drive_add(const char *file, const char *fmt, ...)
e4bcb14c
TS
762{
763 va_list ap;
9dfd7c7a
GH
764 char optstr[1024];
765 QemuOpts *opts;
e4bcb14c 766
e4bcb14c 767 va_start(ap, fmt);
9dfd7c7a 768 vsnprintf(optstr, sizeof(optstr), fmt, ap);
e4bcb14c
TS
769 va_end(ap);
770
8212c64f 771 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
9dfd7c7a
GH
772 if (!opts) {
773 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
774 __FUNCTION__, optstr);
775 return NULL;
776 }
777 if (file)
778 qemu_opt_set(opts, "file", file);
779 return opts;
b01b1111
AL
780}
781
751c6a17 782DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
e4bcb14c 783{
751c6a17 784 DriveInfo *dinfo;
e4bcb14c
TS
785
786 /* seek interface, bus and unit */
787
72cf2d4f 788 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
789 if (dinfo->type == type &&
790 dinfo->bus == bus &&
791 dinfo->unit == unit)
792 return dinfo;
793 }
e4bcb14c 794
751c6a17 795 return NULL;
e4bcb14c
TS
796}
797
2e810b36 798DriveInfo *drive_get_by_id(const char *id)
1dae12e6
GH
799{
800 DriveInfo *dinfo;
801
72cf2d4f 802 QTAILQ_FOREACH(dinfo, &drives, next) {
1dae12e6
GH
803 if (strcmp(id, dinfo->id))
804 continue;
805 return dinfo;
806 }
807 return NULL;
808}
809
f60d39bc 810int drive_get_max_bus(BlockInterfaceType type)
e4bcb14c
TS
811{
812 int max_bus;
751c6a17 813 DriveInfo *dinfo;
e4bcb14c
TS
814
815 max_bus = -1;
72cf2d4f 816 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
817 if(dinfo->type == type &&
818 dinfo->bus > max_bus)
819 max_bus = dinfo->bus;
e4bcb14c
TS
820 }
821 return max_bus;
822}
823
fa879c64
AL
824const char *drive_get_serial(BlockDriverState *bdrv)
825{
751c6a17 826 DriveInfo *dinfo;
fa879c64 827
72cf2d4f 828 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
829 if (dinfo->bdrv == bdrv)
830 return dinfo->serial;
831 }
fa879c64
AL
832
833 return "\0";
834}
835
f7850099
KW
836BlockInterfaceErrorAction drive_get_on_error(
837 BlockDriverState *bdrv, int is_read)
428c5705 838{
751c6a17 839 DriveInfo *dinfo;
428c5705 840
72cf2d4f 841 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 842 if (dinfo->bdrv == bdrv)
e9b2e818 843 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
751c6a17 844 }
428c5705 845
e9b2e818 846 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
428c5705
AL
847}
848
a1620fac
AJ
849static void bdrv_format_print(void *opaque, const char *name)
850{
851 fprintf(stderr, " %s", name);
852}
853
56a14938 854void drive_uninit(DriveInfo *dinfo)
b01b1111 855{
56a14938
GH
856 qemu_opts_del(dinfo->opts);
857 bdrv_delete(dinfo->bdrv);
858 QTAILQ_REMOVE(&drives, dinfo, next);
859 qemu_free(dinfo);
b01b1111
AL
860}
861
e9b2e818
KW
862static int parse_block_error_action(const char *buf, int is_read)
863{
864 if (!strcmp(buf, "ignore")) {
865 return BLOCK_ERR_IGNORE;
866 } else if (!is_read && !strcmp(buf, "enospc")) {
867 return BLOCK_ERR_STOP_ENOSPC;
868 } else if (!strcmp(buf, "stop")) {
869 return BLOCK_ERR_STOP_ANY;
870 } else if (!strcmp(buf, "report")) {
871 return BLOCK_ERR_REPORT;
872 } else {
873 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
874 buf, is_read ? "read" : "write");
875 return -1;
876 }
877}
878
9dfd7c7a 879DriveInfo *drive_init(QemuOpts *opts, void *opaque,
751c6a17 880 int *fatal_error)
e4bcb14c 881{
9dfd7c7a
GH
882 const char *buf;
883 const char *file = NULL;
c8522bdf 884 char devname[128];
9dfd7c7a 885 const char *serial;
c8522bdf 886 const char *mediastr = "";
f60d39bc 887 BlockInterfaceType type;
e4bcb14c
TS
888 enum { MEDIA_DISK, MEDIA_CDROM } media;
889 int bus_id, unit_id;
890 int cyls, heads, secs, translation;
1e72d3b7 891 BlockDriver *drv = NULL;
4d73cd3b 892 QEMUMachine *machine = opaque;
e4bcb14c
TS
893 int max_devs;
894 int index;
33f00271 895 int cache;
5c6c3a6c 896 int aio = 0;
59f2689d 897 int ro = 0;
e9b2e818
KW
898 int bdrv_flags;
899 int on_read_error, on_write_error;
c2cc47a4 900 const char *devaddr;
751c6a17 901 DriveInfo *dinfo;
9dfd7c7a 902 int snapshot = 0;
e4bcb14c 903
9dfd7c7a 904 *fatal_error = 1;
e4bcb14c 905
e4bcb14c 906 translation = BIOS_ATA_TRANSLATION_AUTO;
0aa217e4 907 cache = 1;
e4bcb14c 908
4d007814 909 if (machine && machine->use_scsi) {
f60d39bc 910 type = IF_SCSI;
e4bcb14c 911 max_devs = MAX_SCSI_DEVS;
363a37d5 912 pstrcpy(devname, sizeof(devname), "scsi");
e4bcb14c 913 } else {
f60d39bc 914 type = IF_IDE;
e4bcb14c 915 max_devs = MAX_IDE_DEVS;
363a37d5 916 pstrcpy(devname, sizeof(devname), "ide");
e4bcb14c
TS
917 }
918 media = MEDIA_DISK;
919
920 /* extract parameters */
9dfd7c7a
GH
921 bus_id = qemu_opt_get_number(opts, "bus", 0);
922 unit_id = qemu_opt_get_number(opts, "unit", -1);
923 index = qemu_opt_get_number(opts, "index", -1);
e4bcb14c 924
9dfd7c7a
GH
925 cyls = qemu_opt_get_number(opts, "cyls", 0);
926 heads = qemu_opt_get_number(opts, "heads", 0);
927 secs = qemu_opt_get_number(opts, "secs", 0);
e4bcb14c 928
9dfd7c7a 929 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
59f2689d 930 ro = qemu_opt_get_bool(opts, "readonly", 0);
e4bcb14c 931
9dfd7c7a
GH
932 file = qemu_opt_get(opts, "file");
933 serial = qemu_opt_get(opts, "serial");
934
935 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
ae45d369 936 pstrcpy(devname, sizeof(devname), buf);
e4bcb14c 937 if (!strcmp(buf, "ide")) {
f60d39bc 938 type = IF_IDE;
e4bcb14c
TS
939 max_devs = MAX_IDE_DEVS;
940 } else if (!strcmp(buf, "scsi")) {
f60d39bc 941 type = IF_SCSI;
e4bcb14c
TS
942 max_devs = MAX_SCSI_DEVS;
943 } else if (!strcmp(buf, "floppy")) {
f60d39bc 944 type = IF_FLOPPY;
e4bcb14c
TS
945 max_devs = 0;
946 } else if (!strcmp(buf, "pflash")) {
f60d39bc 947 type = IF_PFLASH;
e4bcb14c
TS
948 max_devs = 0;
949 } else if (!strcmp(buf, "mtd")) {
f60d39bc 950 type = IF_MTD;
e4bcb14c
TS
951 max_devs = 0;
952 } else if (!strcmp(buf, "sd")) {
f60d39bc 953 type = IF_SD;
e4bcb14c 954 max_devs = 0;
6e02c38d
AL
955 } else if (!strcmp(buf, "virtio")) {
956 type = IF_VIRTIO;
957 max_devs = 0;
62d23efa
AL
958 } else if (!strcmp(buf, "xen")) {
959 type = IF_XEN;
960 max_devs = 0;
a8659e90
GH
961 } else if (!strcmp(buf, "none")) {
962 type = IF_NONE;
963 max_devs = 0;
62d23efa 964 } else {
9dfd7c7a 965 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
751c6a17 966 return NULL;
e4bcb14c
TS
967 }
968 }
969
e4bcb14c 970 if (cyls || heads || secs) {
5afe3f04 971 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
9dfd7c7a 972 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
751c6a17 973 return NULL;
e4bcb14c 974 }
5afe3f04 975 if (heads < 1 || (type == IF_IDE && heads > 16)) {
9dfd7c7a 976 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
751c6a17 977 return NULL;
e4bcb14c 978 }
5afe3f04 979 if (secs < 1 || (type == IF_IDE && secs > 63)) {
9dfd7c7a 980 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
751c6a17 981 return NULL;
e4bcb14c
TS
982 }
983 }
984
9dfd7c7a 985 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
e4bcb14c
TS
986 if (!cyls) {
987 fprintf(stderr,
988 "qemu: '%s' trans must be used with cyls,heads and secs\n",
9dfd7c7a 989 buf);
751c6a17 990 return NULL;
e4bcb14c
TS
991 }
992 if (!strcmp(buf, "none"))
993 translation = BIOS_ATA_TRANSLATION_NONE;
994 else if (!strcmp(buf, "lba"))
995 translation = BIOS_ATA_TRANSLATION_LBA;
996 else if (!strcmp(buf, "auto"))
997 translation = BIOS_ATA_TRANSLATION_AUTO;
998 else {
9dfd7c7a 999 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
751c6a17 1000 return NULL;
e4bcb14c
TS
1001 }
1002 }
1003
9dfd7c7a 1004 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
e4bcb14c
TS
1005 if (!strcmp(buf, "disk")) {
1006 media = MEDIA_DISK;
1007 } else if (!strcmp(buf, "cdrom")) {
1008 if (cyls || secs || heads) {
1009 fprintf(stderr,
9dfd7c7a 1010 "qemu: '%s' invalid physical CHS format\n", buf);
751c6a17 1011 return NULL;
e4bcb14c
TS
1012 }
1013 media = MEDIA_CDROM;
1014 } else {
9dfd7c7a 1015 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
751c6a17 1016 return NULL;
e4bcb14c
TS
1017 }
1018 }
1019
9dfd7c7a 1020 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
9f7965c7 1021 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
33f00271 1022 cache = 0;
9f7965c7 1023 else if (!strcmp(buf, "writethrough"))
33f00271 1024 cache = 1;
9f7965c7
AL
1025 else if (!strcmp(buf, "writeback"))
1026 cache = 2;
33f00271
AZ
1027 else {
1028 fprintf(stderr, "qemu: invalid cache option\n");
751c6a17 1029 return NULL;
33f00271
AZ
1030 }
1031 }
1032
5c6c3a6c
CH
1033#ifdef CONFIG_LINUX_AIO
1034 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
1035 if (!strcmp(buf, "threads"))
1036 aio = 0;
1037 else if (!strcmp(buf, "native"))
1038 aio = 1;
1039 else {
1040 fprintf(stderr, "qemu: invalid aio option\n");
1041 return NULL;
1042 }
1043 }
1044#endif
1045
9dfd7c7a 1046 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
a1620fac
AJ
1047 if (strcmp(buf, "?") == 0) {
1048 fprintf(stderr, "qemu: Supported formats:");
1049 bdrv_iterate_format(bdrv_format_print, NULL);
1050 fprintf(stderr, "\n");
751c6a17 1051 return NULL;
a1620fac 1052 }
eb852011 1053 drv = bdrv_find_whitelisted_format(buf);
1e72d3b7
AJ
1054 if (!drv) {
1055 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
751c6a17 1056 return NULL;
1e72d3b7
AJ
1057 }
1058 }
1059
e9b2e818 1060 on_write_error = BLOCK_ERR_STOP_ENOSPC;
9dfd7c7a 1061 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
869a5c6d 1062 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
ea8a5d7f 1063 fprintf(stderr, "werror is no supported by this format\n");
751c6a17 1064 return NULL;
428c5705 1065 }
e9b2e818
KW
1066
1067 on_write_error = parse_block_error_action(buf, 0);
1068 if (on_write_error < 0) {
1069 return NULL;
1070 }
1071 }
1072
1073 on_read_error = BLOCK_ERR_REPORT;
1074 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
f35d68f0 1075 if (type != IF_IDE && type != IF_VIRTIO) {
e9b2e818
KW
1076 fprintf(stderr, "rerror is no supported by this format\n");
1077 return NULL;
1078 }
1079
1080 on_read_error = parse_block_error_action(buf, 1);
1081 if (on_read_error < 0) {
751c6a17 1082 return NULL;
428c5705
AL
1083 }
1084 }
1085
9dfd7c7a 1086 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
c2cc47a4 1087 if (type != IF_VIRTIO) {
9dfd7c7a 1088 fprintf(stderr, "addr is not supported\n");
751c6a17 1089 return NULL;
c2cc47a4 1090 }
c2cc47a4
MA
1091 }
1092
e4bcb14c
TS
1093 /* compute bus and unit according index */
1094
1095 if (index != -1) {
1096 if (bus_id != 0 || unit_id != -1) {
1097 fprintf(stderr,
9dfd7c7a 1098 "qemu: index cannot be used with bus and unit\n");
751c6a17 1099 return NULL;
e4bcb14c
TS
1100 }
1101 if (max_devs == 0)
1102 {
1103 unit_id = index;
1104 bus_id = 0;
1105 } else {
1106 unit_id = index % max_devs;
1107 bus_id = index / max_devs;
1108 }
1109 }
1110
1111 /* if user doesn't specify a unit_id,
1112 * try to find the first free
1113 */
1114
1115 if (unit_id == -1) {
1116 unit_id = 0;
751c6a17 1117 while (drive_get(type, bus_id, unit_id) != NULL) {
e4bcb14c
TS
1118 unit_id++;
1119 if (max_devs && unit_id >= max_devs) {
1120 unit_id -= max_devs;
1121 bus_id++;
1122 }
1123 }
1124 }
1125
1126 /* check unit id */
1127
1128 if (max_devs && unit_id >= max_devs) {
9dfd7c7a
GH
1129 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1130 unit_id, max_devs - 1);
751c6a17 1131 return NULL;
e4bcb14c
TS
1132 }
1133
1134 /*
1135 * ignore multiple definitions
1136 */
1137
751c6a17
GH
1138 if (drive_get(type, bus_id, unit_id) != NULL) {
1139 *fatal_error = 0;
1140 return NULL;
1141 }
e4bcb14c
TS
1142
1143 /* init */
1144
751c6a17 1145 dinfo = qemu_mallocz(sizeof(*dinfo));
e23d9c4d 1146 if ((buf = qemu_opts_id(opts)) != NULL) {
9dfd7c7a
GH
1147 dinfo->id = qemu_strdup(buf);
1148 } else {
1dae12e6 1149 /* no id supplied -> create one */
9dfd7c7a 1150 dinfo->id = qemu_mallocz(32);
1dae12e6
GH
1151 if (type == IF_IDE || type == IF_SCSI)
1152 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1153 if (max_devs)
9dfd7c7a 1154 snprintf(dinfo->id, 32, "%s%i%s%i",
1dae12e6
GH
1155 devname, bus_id, mediastr, unit_id);
1156 else
9dfd7c7a 1157 snprintf(dinfo->id, 32, "%s%s%i",
1dae12e6
GH
1158 devname, mediastr, unit_id);
1159 }
1dae12e6 1160 dinfo->bdrv = bdrv_new(dinfo->id);
751c6a17
GH
1161 dinfo->devaddr = devaddr;
1162 dinfo->type = type;
1163 dinfo->bus = bus_id;
1164 dinfo->unit = unit_id;
e9b2e818
KW
1165 dinfo->on_read_error = on_read_error;
1166 dinfo->on_write_error = on_write_error;
9dfd7c7a
GH
1167 dinfo->opts = opts;
1168 if (serial)
1169 strncpy(dinfo->serial, serial, sizeof(serial));
72cf2d4f 1170 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
e4bcb14c 1171
f60d39bc 1172 switch(type) {
e4bcb14c
TS
1173 case IF_IDE:
1174 case IF_SCSI:
62d23efa 1175 case IF_XEN:
c219331e 1176 case IF_NONE:
e4bcb14c
TS
1177 switch(media) {
1178 case MEDIA_DISK:
1179 if (cyls != 0) {
1dae12e6
GH
1180 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1181 bdrv_set_translation_hint(dinfo->bdrv, translation);
e4bcb14c
TS
1182 }
1183 break;
1184 case MEDIA_CDROM:
1dae12e6 1185 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
e4bcb14c
TS
1186 break;
1187 }
1188 break;
1189 case IF_SD:
1190 /* FIXME: This isn't really a floppy, but it's a reasonable
1191 approximation. */
1192 case IF_FLOPPY:
1dae12e6 1193 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
e4bcb14c
TS
1194 break;
1195 case IF_PFLASH:
1196 case IF_MTD:
1197 break;
d176c495
GH
1198 case IF_VIRTIO:
1199 /* add virtio block device */
1200 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1201 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1202 qemu_opt_set(opts, "drive", dinfo->id);
1203 if (devaddr)
1204 qemu_opt_set(opts, "addr", devaddr);
1205 break;
aae9460e
PB
1206 case IF_COUNT:
1207 abort();
e4bcb14c 1208 }
9dfd7c7a 1209 if (!file) {
751c6a17
GH
1210 *fatal_error = 0;
1211 return NULL;
1212 }
33f00271 1213 bdrv_flags = 0;
9f7965c7 1214 if (snapshot) {
33f00271 1215 bdrv_flags |= BDRV_O_SNAPSHOT;
9f7965c7
AL
1216 cache = 2; /* always use write-back with snapshot */
1217 }
1218 if (cache == 0) /* no caching */
1219 bdrv_flags |= BDRV_O_NOCACHE;
1220 else if (cache == 2) /* write-back */
1221 bdrv_flags |= BDRV_O_CACHE_WB;
5c6c3a6c
CH
1222
1223 if (aio == 1) {
1224 bdrv_flags |= BDRV_O_NATIVE_AIO;
1225 } else {
1226 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
1227 }
1228
59f2689d 1229 if (ro == 1) {
f5edb014
NS
1230 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
1231 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
59f2689d
NS
1232 return NULL;
1233 }
b196b153
NS
1234 }
1235 /*
1236 * cdrom is read-only. Set it now, after above interface checking
1237 * since readonly attribute not explicitly required, so no error.
1238 */
1239 if (media == MEDIA_CDROM) {
f5edb014 1240 ro = 1;
59f2689d 1241 }
f5edb014 1242 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
59f2689d 1243
1dae12e6 1244 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
850810d0
JF
1245 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1246 file, strerror(errno));
751c6a17 1247 return NULL;
e4bcb14c 1248 }
5c6c3a6c 1249
1dae12e6 1250 if (bdrv_key_required(dinfo->bdrv))
c0f4ce77 1251 autostart = 0;
751c6a17
GH
1252 *fatal_error = 0;
1253 return dinfo;
e4bcb14c
TS
1254}
1255
9dfd7c7a
GH
1256static int drive_init_func(QemuOpts *opts, void *opaque)
1257{
1258 QEMUMachine *machine = opaque;
1259 int fatal_error = 0;
1260
1261 if (drive_init(opts, machine, &fatal_error) == NULL) {
1262 if (fatal_error)
1263 return 1;
1264 }
1265 return 0;
1266}
1267
1268static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1269{
1270 if (NULL == qemu_opt_get(opts, "snapshot")) {
1271 qemu_opt_set(opts, "snapshot", "on");
1272 }
1273 return 0;
1274}
1275
76e30d0f
JK
1276void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1277{
1278 boot_set_handler = func;
1279 boot_set_opaque = opaque;
1280}
1281
1282int qemu_boot_set(const char *boot_devices)
1283{
1284 if (!boot_set_handler) {
1285 return -EINVAL;
1286 }
1287 return boot_set_handler(boot_set_opaque, boot_devices);
1288}
1289
ef3adf68
JK
1290static int parse_bootdevices(char *devices)
1291{
1292 /* We just do some generic consistency checks */
1293 const char *p;
1294 int bitmap = 0;
1295
1296 for (p = devices; *p != '\0'; p++) {
1297 /* Allowed boot devices are:
1298 * a-b: floppy disk drives
1299 * c-f: IDE disk drives
1300 * g-m: machine implementation dependant drives
1301 * n-p: network devices
1302 * It's up to each machine implementation to check if the given boot
1303 * devices match the actual hardware implementation and firmware
1304 * features.
1305 */
1306 if (*p < 'a' || *p > 'p') {
1307 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1308 exit(1);
1309 }
1310 if (bitmap & (1 << (*p - 'a'))) {
1311 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1312 exit(1);
1313 }
1314 bitmap |= 1 << (*p - 'a');
1315 }
1316 return bitmap;
1317}
1318
e0f084bf
JK
1319static void restore_boot_devices(void *opaque)
1320{
1321 char *standard_boot_devices = opaque;
1322
1323 qemu_boot_set(standard_boot_devices);
1324
1325 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1326 qemu_free(standard_boot_devices);
1327}
1328
268a362c
AL
1329static void numa_add(const char *optarg)
1330{
1331 char option[128];
1332 char *endptr;
1333 unsigned long long value, endvalue;
1334 int nodenr;
1335
1336 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1337 if (!strcmp(option, "node")) {
1338 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1339 nodenr = nb_numa_nodes;
1340 } else {
1341 nodenr = strtoull(option, NULL, 10);
1342 }
1343
1344 if (get_param_value(option, 128, "mem", optarg) == 0) {
1345 node_mem[nodenr] = 0;
1346 } else {
1347 value = strtoull(option, &endptr, 0);
1348 switch (*endptr) {
1349 case 0: case 'M': case 'm':
1350 value <<= 20;
1351 break;
1352 case 'G': case 'g':
1353 value <<= 30;
1354 break;
1355 }
1356 node_mem[nodenr] = value;
1357 }
1358 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1359 node_cpumask[nodenr] = 0;
1360 } else {
1361 value = strtoull(option, &endptr, 10);
1362 if (value >= 64) {
1363 value = 63;
1364 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1365 } else {
1366 if (*endptr == '-') {
1367 endvalue = strtoull(endptr+1, &endptr, 10);
1368 if (endvalue >= 63) {
1369 endvalue = 62;
1370 fprintf(stderr,
1371 "only 63 CPUs in NUMA mode supported.\n");
1372 }
0dfbd514 1373 value = (2ULL << endvalue) - (1ULL << value);
268a362c 1374 } else {
0dfbd514 1375 value = 1ULL << value;
268a362c
AL
1376 }
1377 }
1378 node_cpumask[nodenr] = value;
1379 }
1380 nb_numa_nodes++;
1381 }
1382 return;
1383}
1384
dc6b1c09
AP
1385static void smp_parse(const char *optarg)
1386{
1387 int smp, sockets = 0, threads = 0, cores = 0;
1388 char *endptr;
1389 char option[128];
1390
1391 smp = strtoul(optarg, &endptr, 10);
1392 if (endptr != optarg) {
1393 if (*endptr == ',') {
1394 endptr++;
1395 }
1396 }
1397 if (get_param_value(option, 128, "sockets", endptr) != 0)
1398 sockets = strtoull(option, NULL, 10);
1399 if (get_param_value(option, 128, "cores", endptr) != 0)
1400 cores = strtoull(option, NULL, 10);
1401 if (get_param_value(option, 128, "threads", endptr) != 0)
1402 threads = strtoull(option, NULL, 10);
1403 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1404 max_cpus = strtoull(option, NULL, 10);
1405
1406 /* compute missing values, prefer sockets over cores over threads */
1407 if (smp == 0 || sockets == 0) {
1408 sockets = sockets > 0 ? sockets : 1;
1409 cores = cores > 0 ? cores : 1;
1410 threads = threads > 0 ? threads : 1;
1411 if (smp == 0) {
1412 smp = cores * threads * sockets;
dc6b1c09
AP
1413 }
1414 } else {
1415 if (cores == 0) {
1416 threads = threads > 0 ? threads : 1;
1417 cores = smp / (sockets * threads);
1418 } else {
5cdc9b76 1419 if (sockets) {
dc6b1c09
AP
1420 threads = smp / (cores * sockets);
1421 }
1422 }
1423 }
1424 smp_cpus = smp;
1425 smp_cores = cores > 0 ? cores : 1;
1426 smp_threads = threads > 0 ? threads : 1;
1427 if (max_cpus == 0)
1428 max_cpus = smp_cpus;
1429}
1430
a594cfbf
FB
1431/***********************************************************/
1432/* USB devices */
1433
c0f4ce77 1434static int usb_device_add(const char *devname, int is_hotplug)
a594cfbf
FB
1435{
1436 const char *p;
a5d2f727 1437 USBDevice *dev = NULL;
a594cfbf 1438
a5d2f727 1439 if (!usb_enabled)
a594cfbf
FB
1440 return -1;
1441
0958b4cc
GH
1442 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1443 dev = usbdevice_create(devname);
1444 if (dev)
1445 goto done;
1446
a5d2f727 1447 /* the other ones */
a594cfbf
FB
1448 if (strstart(devname, "host:", &p)) {
1449 dev = usb_host_device_open(p);
dc72ac14
AZ
1450 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1451 dev = usb_bt_init(devname[2] ? hci_init(p) :
1452 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
1453 } else {
1454 return -1;
1455 }
0d92ed30
PB
1456 if (!dev)
1457 return -1;
1458
a5d2f727 1459done:
a594cfbf
FB
1460 return 0;
1461}
1462
1f3870ab
AL
1463static int usb_device_del(const char *devname)
1464{
1465 int bus_num, addr;
1466 const char *p;
1467
5d0c5750
AL
1468 if (strstart(devname, "host:", &p))
1469 return usb_host_device_close(p);
1470
a5d2f727 1471 if (!usb_enabled)
1f3870ab
AL
1472 return -1;
1473
1474 p = strchr(devname, '.');
1475 if (!p)
1476 return -1;
1477 bus_num = strtoul(devname, NULL, 0);
1478 addr = strtoul(p + 1, NULL, 0);
1479
a5d2f727 1480 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
1481}
1482
bd3c948d
GH
1483static int usb_parse(const char *cmdline)
1484{
59d1c1c2
ST
1485 int r;
1486 r = usb_device_add(cmdline, 0);
1487 if (r < 0) {
1488 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1489 }
1490 return r;
bd3c948d
GH
1491}
1492
d54908a5 1493void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 1494{
59d1c1c2
ST
1495 const char *devname = qdict_get_str(qdict, "devname");
1496 if (usb_device_add(devname, 1) < 0) {
1ecda02b 1497 error_report("could not add USB device '%s'", devname);
59d1c1c2 1498 }
a594cfbf
FB
1499}
1500
d54908a5 1501void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 1502{
59d1c1c2
ST
1503 const char *devname = qdict_get_str(qdict, "devname");
1504 if (usb_device_del(devname) < 0) {
1ecda02b 1505 error_report("could not delete USB device '%s'", devname);
59d1c1c2 1506 }
a594cfbf
FB
1507}
1508
201a51fc
AZ
1509/***********************************************************/
1510/* PCMCIA/Cardbus */
1511
1512static struct pcmcia_socket_entry_s {
bc24a225 1513 PCMCIASocket *socket;
201a51fc
AZ
1514 struct pcmcia_socket_entry_s *next;
1515} *pcmcia_sockets = 0;
1516
bc24a225 1517void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
1518{
1519 struct pcmcia_socket_entry_s *entry;
1520
1521 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1522 entry->socket = socket;
1523 entry->next = pcmcia_sockets;
1524 pcmcia_sockets = entry;
1525}
1526
bc24a225 1527void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
1528{
1529 struct pcmcia_socket_entry_s *entry, **ptr;
1530
1531 ptr = &pcmcia_sockets;
1532 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1533 if (entry->socket == socket) {
1534 *ptr = entry->next;
1535 qemu_free(entry);
1536 }
1537}
1538
376253ec 1539void pcmcia_info(Monitor *mon)
201a51fc
AZ
1540{
1541 struct pcmcia_socket_entry_s *iter;
376253ec 1542
201a51fc 1543 if (!pcmcia_sockets)
376253ec 1544 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
1545
1546 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
1547 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1548 iter->socket->attached ? iter->socket->card_string :
1549 "Empty");
201a51fc
AZ
1550}
1551
2ff89790 1552/***********************************************************/
8a7ddc38 1553/* I/O handling */
0824d6fc 1554
c4b1fcc0
FB
1555typedef struct IOHandlerRecord {
1556 int fd;
7b27a769 1557 IOCanReadHandler *fd_read_poll;
7c9d8e07
FB
1558 IOHandler *fd_read;
1559 IOHandler *fd_write;
cafffd40 1560 int deleted;
c4b1fcc0
FB
1561 void *opaque;
1562 /* temporary data */
1563 struct pollfd *ufd;
31d4ee6c 1564 QLIST_ENTRY(IOHandlerRecord) next;
c4b1fcc0
FB
1565} IOHandlerRecord;
1566
31d4ee6c
JQ
1567static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1568 QLIST_HEAD_INITIALIZER(io_handlers);
1569
c4b1fcc0 1570
7c9d8e07
FB
1571/* XXX: fd_read_poll should be suppressed, but an API change is
1572 necessary in the character devices to suppress fd_can_read(). */
5fafdf24 1573int qemu_set_fd_handler2(int fd,
7b27a769 1574 IOCanReadHandler *fd_read_poll,
5fafdf24
TS
1575 IOHandler *fd_read,
1576 IOHandler *fd_write,
7c9d8e07 1577 void *opaque)
c4b1fcc0 1578{
31d4ee6c 1579 IOHandlerRecord *ioh;
c4b1fcc0 1580
7c9d8e07 1581 if (!fd_read && !fd_write) {
31d4ee6c 1582 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07 1583 if (ioh->fd == fd) {
cafffd40 1584 ioh->deleted = 1;
7c9d8e07
FB
1585 break;
1586 }
7c9d8e07
FB
1587 }
1588 } else {
31d4ee6c 1589 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07
FB
1590 if (ioh->fd == fd)
1591 goto found;
1592 }
1593 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
31d4ee6c 1594 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
7c9d8e07
FB
1595 found:
1596 ioh->fd = fd;
1597 ioh->fd_read_poll = fd_read_poll;
1598 ioh->fd_read = fd_read;
1599 ioh->fd_write = fd_write;
1600 ioh->opaque = opaque;
cafffd40 1601 ioh->deleted = 0;
7c9d8e07 1602 }
c4b1fcc0
FB
1603 return 0;
1604}
1605
5fafdf24
TS
1606int qemu_set_fd_handler(int fd,
1607 IOHandler *fd_read,
1608 IOHandler *fd_write,
7c9d8e07 1609 void *opaque)
8a7ddc38 1610{
7c9d8e07 1611 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
8a7ddc38
FB
1612}
1613
56f3a5d0 1614#ifdef _WIN32
f331110f
FB
1615/***********************************************************/
1616/* Polling handling */
1617
1618typedef struct PollingEntry {
1619 PollingFunc *func;
1620 void *opaque;
1621 struct PollingEntry *next;
1622} PollingEntry;
1623
1624static PollingEntry *first_polling_entry;
1625
1626int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1627{
1628 PollingEntry **ppe, *pe;
1629 pe = qemu_mallocz(sizeof(PollingEntry));
f331110f
FB
1630 pe->func = func;
1631 pe->opaque = opaque;
1632 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1633 *ppe = pe;
1634 return 0;
1635}
1636
1637void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1638{
1639 PollingEntry **ppe, *pe;
1640 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1641 pe = *ppe;
1642 if (pe->func == func && pe->opaque == opaque) {
1643 *ppe = pe->next;
1644 qemu_free(pe);
1645 break;
1646 }
1647 }
1648}
1649
a18e524a
FB
1650/***********************************************************/
1651/* Wait objects support */
1652typedef struct WaitObjects {
1653 int num;
1654 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1655 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1656 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1657} WaitObjects;
1658
1659static WaitObjects wait_objects = {0};
3b46e624 1660
a18e524a
FB
1661int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1662{
1663 WaitObjects *w = &wait_objects;
1664
1665 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1666 return -1;
1667 w->events[w->num] = handle;
1668 w->func[w->num] = func;
1669 w->opaque[w->num] = opaque;
1670 w->num++;
1671 return 0;
1672}
1673
1674void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1675{
1676 int i, found;
1677 WaitObjects *w = &wait_objects;
1678
1679 found = 0;
1680 for (i = 0; i < w->num; i++) {
1681 if (w->events[i] == handle)
1682 found = 1;
1683 if (found) {
1684 w->events[i] = w->events[i + 1];
1685 w->func[i] = w->func[i + 1];
1686 w->opaque[i] = w->opaque[i + 1];
3b46e624 1687 }
a18e524a
FB
1688 }
1689 if (found)
1690 w->num--;
1691}
1692#endif
1693
8a7ddc38
FB
1694/***********************************************************/
1695/* ram save/restore */
1696
94fb0909 1697#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
475e4277
AL
1698#define RAM_SAVE_FLAG_COMPRESS 0x02
1699#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1700#define RAM_SAVE_FLAG_PAGE 0x08
1701#define RAM_SAVE_FLAG_EOS 0x10
1702
1703static int is_dup_page(uint8_t *page, uint8_t ch)
8a7ddc38 1704{
475e4277
AL
1705 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1706 uint32_t *array = (uint32_t *)page;
1707 int i;
3b46e624 1708
475e4277
AL
1709 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1710 if (array[i] != val)
1711 return 0;
1712 }
1713
1714 return 1;
1715}
1716
1717static int ram_save_block(QEMUFile *f)
1718{
c227f099
AL
1719 static ram_addr_t current_addr = 0;
1720 ram_addr_t saved_addr = current_addr;
1721 ram_addr_t addr = 0;
475e4277
AL
1722 int found = 0;
1723
94a6b54f 1724 while (addr < last_ram_offset) {
475e4277 1725 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
5579c7f3 1726 uint8_t *p;
475e4277
AL
1727
1728 cpu_physical_memory_reset_dirty(current_addr,
1729 current_addr + TARGET_PAGE_SIZE,
1730 MIGRATION_DIRTY_FLAG);
1731
5579c7f3 1732 p = qemu_get_ram_ptr(current_addr);
475e4277 1733
5579c7f3 1734 if (is_dup_page(p, *p)) {
475e4277 1735 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
5579c7f3 1736 qemu_put_byte(f, *p);
475e4277
AL
1737 } else {
1738 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
5579c7f3 1739 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
c88676f8 1740 }
475e4277
AL
1741
1742 found = 1;
1743 break;
c88676f8 1744 }
475e4277 1745 addr += TARGET_PAGE_SIZE;
94a6b54f 1746 current_addr = (saved_addr + addr) % last_ram_offset;
8a7ddc38 1747 }
475e4277
AL
1748
1749 return found;
8a7ddc38
FB
1750}
1751
84307938 1752static uint64_t bytes_transferred;
475e4277 1753
c227f099 1754static ram_addr_t ram_save_remaining(void)
475e4277 1755{
c227f099
AL
1756 ram_addr_t addr;
1757 ram_addr_t count = 0;
475e4277 1758
94a6b54f 1759 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
475e4277
AL
1760 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1761 count++;
1762 }
1763
1764 return count;
1765}
1766
9f9e28cd
GC
1767uint64_t ram_bytes_remaining(void)
1768{
1769 return ram_save_remaining() * TARGET_PAGE_SIZE;
1770}
1771
1772uint64_t ram_bytes_transferred(void)
1773{
1774 return bytes_transferred;
1775}
1776
1777uint64_t ram_bytes_total(void)
1778{
1779 return last_ram_offset;
1780}
1781
f327aa0c 1782static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
475e4277 1783{
c227f099 1784 ram_addr_t addr;
a0a3fd60
GC
1785 uint64_t bytes_transferred_last;
1786 double bwidth = 0;
1787 uint64_t expected_time = 0;
475e4277 1788
4ec7fcc7
JK
1789 if (stage < 0) {
1790 cpu_physical_memory_set_dirty_tracking(0);
1791 return 0;
1792 }
1793
9fa06385 1794 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
b0a46a33
JK
1795 qemu_file_set_error(f);
1796 return 0;
1797 }
1798
475e4277 1799 if (stage == 1) {
84307938
JK
1800 bytes_transferred = 0;
1801
475e4277 1802 /* Make sure all dirty bits are set */
94a6b54f 1803 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
475e4277
AL
1804 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1805 cpu_physical_memory_set_dirty(addr);
1806 }
b0a46a33 1807
475e4277
AL
1808 /* Enable dirty memory tracking */
1809 cpu_physical_memory_set_dirty_tracking(1);
1810
94a6b54f 1811 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
475e4277
AL
1812 }
1813
a0a3fd60 1814 bytes_transferred_last = bytes_transferred;
41c872b6 1815 bwidth = qemu_get_clock_ns(rt_clock);
a0a3fd60 1816
475e4277
AL
1817 while (!qemu_file_rate_limit(f)) {
1818 int ret;
1819
1820 ret = ram_save_block(f);
9f9e28cd 1821 bytes_transferred += ret * TARGET_PAGE_SIZE;
475e4277
AL
1822 if (ret == 0) /* no more blocks */
1823 break;
1824 }
1825
41c872b6 1826 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
a0a3fd60
GC
1827 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
1828
1829 /* if we haven't transferred anything this round, force expected_time to a
1830 * a very high value, but without crashing */
1831 if (bwidth == 0)
1832 bwidth = 0.000001;
1833
475e4277 1834 /* try transferring iterative blocks of memory */
475e4277 1835 if (stage == 3) {
475e4277 1836 /* flush all remaining blocks regardless of rate limiting */
9f9e28cd
GC
1837 while (ram_save_block(f) != 0) {
1838 bytes_transferred += TARGET_PAGE_SIZE;
1839 }
8215e914 1840 cpu_physical_memory_set_dirty_tracking(0);
475e4277
AL
1841 }
1842
1843 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1844
a0a3fd60
GC
1845 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
1846
1847 return (stage == 2) && (expected_time <= migrate_max_downtime());
475e4277
AL
1848}
1849
475e4277
AL
1850static int ram_load(QEMUFile *f, void *opaque, int version_id)
1851{
c227f099 1852 ram_addr_t addr;
475e4277
AL
1853 int flags;
1854
475e4277
AL
1855 if (version_id != 3)
1856 return -EINVAL;
1857
1858 do {
1859 addr = qemu_get_be64(f);
1860
1861 flags = addr & ~TARGET_PAGE_MASK;
1862 addr &= TARGET_PAGE_MASK;
1863
1864 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
94a6b54f 1865 if (addr != last_ram_offset)
475e4277
AL
1866 return -EINVAL;
1867 }
1868
475e4277
AL
1869 if (flags & RAM_SAVE_FLAG_COMPRESS) {
1870 uint8_t ch = qemu_get_byte(f);
779c6bef
AL
1871 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
1872#ifndef _WIN32
30868442
AL
1873 if (ch == 0 &&
1874 (!kvm_enabled() || kvm_has_sync_mmu())) {
1875 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
779c6bef 1876 }
30868442 1877#endif
9a743e5b 1878 } else if (flags & RAM_SAVE_FLAG_PAGE) {
5579c7f3 1879 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
9a743e5b
JK
1880 }
1881 if (qemu_file_has_error(f)) {
1882 return -EIO;
1883 }
475e4277
AL
1884 } while (!(flags & RAM_SAVE_FLAG_EOS));
1885
8a7ddc38
FB
1886 return 0;
1887}
1888
9e472e10
AL
1889void qemu_service_io(void)
1890{
d9f75a4e 1891 qemu_notify_event();
9e472e10
AL
1892}
1893
cc1daa40
FB
1894/***********************************************************/
1895/* machine registration */
1896
bdaf78e0 1897static QEMUMachine *first_machine = NULL;
6f338c34 1898QEMUMachine *current_machine = NULL;
cc1daa40
FB
1899
1900int qemu_register_machine(QEMUMachine *m)
1901{
1902 QEMUMachine **pm;
1903 pm = &first_machine;
1904 while (*pm != NULL)
1905 pm = &(*pm)->next;
1906 m->next = NULL;
1907 *pm = m;
1908 return 0;
1909}
1910
9596ebb7 1911static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1912{
1913 QEMUMachine *m;
1914
1915 for(m = first_machine; m != NULL; m = m->next) {
1916 if (!strcmp(m->name, name))
1917 return m;
3f6599e6
MM
1918 if (m->alias && !strcmp(m->alias, name))
1919 return m;
cc1daa40
FB
1920 }
1921 return NULL;
1922}
1923
0c257437
AL
1924static QEMUMachine *find_default_machine(void)
1925{
1926 QEMUMachine *m;
1927
1928 for(m = first_machine; m != NULL; m = m->next) {
1929 if (m->is_default) {
1930 return m;
1931 }
1932 }
1933 return NULL;
1934}
1935
8a7ddc38
FB
1936/***********************************************************/
1937/* main execution loop */
1938
9596ebb7 1939static void gui_update(void *opaque)
8a7ddc38 1940{
7d957bd8 1941 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1942 DisplayState *ds = opaque;
7d957bd8
AL
1943 DisplayChangeListener *dcl = ds->listeners;
1944
62a2744c 1945 qemu_flush_coalesced_mmio_buffer();
7d957bd8
AL
1946 dpy_refresh(ds);
1947
1948 while (dcl != NULL) {
1949 if (dcl->gui_timer_interval &&
1950 dcl->gui_timer_interval < interval)
1951 interval = dcl->gui_timer_interval;
1952 dcl = dcl->next;
1953 }
1954 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
8a7ddc38
FB
1955}
1956
9043b62d
BS
1957static void nographic_update(void *opaque)
1958{
1959 uint64_t interval = GUI_REFRESH_INTERVAL;
1960
62a2744c 1961 qemu_flush_coalesced_mmio_buffer();
9043b62d
BS
1962 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1963}
1964
ea375f9a
JK
1965void cpu_synchronize_all_states(void)
1966{
1967 CPUState *cpu;
1968
1969 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1970 cpu_synchronize_state(cpu);
1971 }
1972}
1973
1974void cpu_synchronize_all_post_reset(void)
1975{
1976 CPUState *cpu;
1977
1978 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1979 cpu_synchronize_post_reset(cpu);
1980 }
1981}
1982
1983void cpu_synchronize_all_post_init(void)
1984{
1985 CPUState *cpu;
1986
1987 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1988 cpu_synchronize_post_init(cpu);
1989 }
1990}
1991
0bd48850
FB
1992struct vm_change_state_entry {
1993 VMChangeStateHandler *cb;
1994 void *opaque;
72cf2d4f 1995 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1996};
1997
72cf2d4f 1998static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1999
2000VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2001 void *opaque)
2002{
2003 VMChangeStateEntry *e;
2004
2005 e = qemu_mallocz(sizeof (*e));
0bd48850
FB
2006
2007 e->cb = cb;
2008 e->opaque = opaque;
72cf2d4f 2009 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
2010 return e;
2011}
2012
2013void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2014{
72cf2d4f 2015 QLIST_REMOVE (e, entries);
0bd48850
FB
2016 qemu_free (e);
2017}
2018
9781e040 2019static void vm_state_notify(int running, int reason)
0bd48850
FB
2020{
2021 VMChangeStateEntry *e;
2022
2023 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
9781e040 2024 e->cb(e->opaque, running, reason);
0bd48850
FB
2025 }
2026}
2027
d6dc3d42
AL
2028static void resume_all_vcpus(void);
2029static void pause_all_vcpus(void);
2030
8a7ddc38
FB
2031void vm_start(void)
2032{
2033 if (!vm_running) {
2034 cpu_enable_ticks();
2035 vm_running = 1;
9781e040 2036 vm_state_notify(1, 0);
d6dc3d42 2037 resume_all_vcpus();
8a7ddc38
FB
2038 }
2039}
2040
bb0c6722
FB
2041/* reset/shutdown handler */
2042
2043typedef struct QEMUResetEntry {
72cf2d4f 2044 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
2045 QEMUResetHandler *func;
2046 void *opaque;
bb0c6722
FB
2047} QEMUResetEntry;
2048
72cf2d4f
BS
2049static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
2050 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722
FB
2051static int reset_requested;
2052static int shutdown_requested;
3475187d 2053static int powerdown_requested;
e568902a 2054static int debug_requested;
6e29f5da 2055static int vmstop_requested;
bb0c6722 2056
cf7a2fe2
AJ
2057int qemu_shutdown_requested(void)
2058{
2059 int r = shutdown_requested;
2060 shutdown_requested = 0;
2061 return r;
2062}
2063
2064int qemu_reset_requested(void)
2065{
2066 int r = reset_requested;
2067 reset_requested = 0;
2068 return r;
2069}
2070
2071int qemu_powerdown_requested(void)
2072{
2073 int r = powerdown_requested;
2074 powerdown_requested = 0;
2075 return r;
2076}
2077
e568902a
AL
2078static int qemu_debug_requested(void)
2079{
2080 int r = debug_requested;
2081 debug_requested = 0;
2082 return r;
2083}
2084
6e29f5da
AL
2085static int qemu_vmstop_requested(void)
2086{
2087 int r = vmstop_requested;
2088 vmstop_requested = 0;
2089 return r;
2090}
2091
2092static void do_vm_stop(int reason)
2093{
2094 if (vm_running) {
2095 cpu_disable_ticks();
2096 vm_running = 0;
d6dc3d42 2097 pause_all_vcpus();
6e29f5da 2098 vm_state_notify(0, reason);
6b8f8fff 2099 monitor_protocol_event(QEVENT_STOP, NULL);
6e29f5da
AL
2100 }
2101}
2102
a08d4367 2103void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 2104{
55ddfe8e 2105 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bb0c6722 2106
bb0c6722
FB
2107 re->func = func;
2108 re->opaque = opaque;
72cf2d4f 2109 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
2110}
2111
dda9b29f 2112void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
2113{
2114 QEMUResetEntry *re;
2115
72cf2d4f 2116 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 2117 if (re->func == func && re->opaque == opaque) {
72cf2d4f 2118 QTAILQ_REMOVE(&reset_handlers, re, entry);
dda9b29f
JK
2119 qemu_free(re);
2120 return;
2121 }
2122 }
2123}
2124
2125void qemu_system_reset(void)
2126{
2127 QEMUResetEntry *re, *nre;
2128
2129 /* reset all devices */
72cf2d4f 2130 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
2131 re->func(re->opaque);
2132 }
81d9b784 2133 monitor_protocol_event(QEVENT_RESET, NULL);
ea375f9a 2134 cpu_synchronize_all_post_reset();
bb0c6722
FB
2135}
2136
2137void qemu_system_reset_request(void)
2138{
d1beab82
FB
2139 if (no_reboot) {
2140 shutdown_requested = 1;
2141 } else {
2142 reset_requested = 1;
2143 }
d9f75a4e 2144 qemu_notify_event();
bb0c6722
FB
2145}
2146
2147void qemu_system_shutdown_request(void)
2148{
2149 shutdown_requested = 1;
d9f75a4e 2150 qemu_notify_event();
bb0c6722
FB
2151}
2152
3475187d
FB
2153void qemu_system_powerdown_request(void)
2154{
2155 powerdown_requested = 1;
d9f75a4e
AL
2156 qemu_notify_event();
2157}
2158
16b151c3 2159static int cpu_can_run(CPUState *env)
d9f75a4e 2160{
16b151c3
PB
2161 if (env->stop)
2162 return 0;
2163 if (env->stopped)
2164 return 0;
2165 if (!vm_running)
2166 return 0;
2167 return 1;
2168}
2169
2170static int cpu_has_work(CPUState *env)
2171{
2172 if (env->stop)
2173 return 1;
2174 if (env->stopped)
2175 return 0;
2176 if (!env->halted)
2177 return 1;
2178 if (qemu_cpu_has_work(env))
2179 return 1;
2180 return 0;
2181}
2182
2183static int tcg_has_work(void)
2184{
2185 CPUState *env;
2186
2187 for (env = first_cpu; env != NULL; env = env->next_cpu)
2188 if (cpu_has_work(env))
2189 return 1;
2190 return 0;
bb0c6722
FB
2191}
2192
50317c7f
AL
2193#ifndef _WIN32
2194static int io_thread_fd = -1;
2195
2196static void qemu_event_increment(void)
3fcf7b6b 2197{
f3dfda61
PB
2198 /* Write 8 bytes to be compatible with eventfd. */
2199 static uint64_t val = 1;
dc330e28 2200 ssize_t ret;
50317c7f
AL
2201
2202 if (io_thread_fd == -1)
2203 return;
2204
652ce2d4 2205 do {
f3dfda61 2206 ret = write(io_thread_fd, &val, sizeof(val));
652ce2d4
PB
2207 } while (ret < 0 && errno == EINTR);
2208
2209 /* EAGAIN is fine, a read must be pending. */
2210 if (ret < 0 && errno != EAGAIN) {
dc330e28
KS
2211 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
2212 strerror(errno));
2213 exit (1);
2214 }
50317c7f
AL
2215}
2216
2217static void qemu_event_read(void *opaque)
2218{
2219 int fd = (unsigned long)opaque;
2220 ssize_t len;
1d0f0d91 2221 char buffer[512];
50317c7f 2222
f3dfda61 2223 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
50317c7f 2224 do {
50317c7f 2225 len = read(fd, buffer, sizeof(buffer));
1d0f0d91 2226 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
50317c7f
AL
2227}
2228
2229static int qemu_event_init(void)
2230{
2231 int err;
2232 int fds[2];
2233
f3dfda61 2234 err = qemu_eventfd(fds);
50317c7f
AL
2235 if (err == -1)
2236 return -errno;
2237
2238 err = fcntl_setfl(fds[0], O_NONBLOCK);
2239 if (err < 0)
2240 goto fail;
2241
2242 err = fcntl_setfl(fds[1], O_NONBLOCK);
2243 if (err < 0)
2244 goto fail;
2245
2246 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2247 (void *)(unsigned long)fds[0]);
2248
2249 io_thread_fd = fds[1];
a7e21219
JK
2250 return 0;
2251
50317c7f
AL
2252fail:
2253 close(fds[0]);
2254 close(fds[1]);
2255 return err;
2256}
2257#else
2258HANDLE qemu_event_handle;
2259
2260static void dummy_event_handler(void *opaque)
2261{
2262}
2263
2264static int qemu_event_init(void)
2265{
2266 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2267 if (!qemu_event_handle) {
20889d4e 2268 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
50317c7f
AL
2269 return -1;
2270 }
2271 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3fcf7b6b
AL
2272 return 0;
2273}
2274
50317c7f
AL
2275static void qemu_event_increment(void)
2276{
de1c90cf 2277 if (!SetEvent(qemu_event_handle)) {
20889d4e 2278 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
de1c90cf 2279 GetLastError());
2280 exit (1);
2281 }
50317c7f
AL
2282}
2283#endif
2284
d6dc3d42 2285#ifndef CONFIG_IOTHREAD
50317c7f
AL
2286static int qemu_init_main_loop(void)
2287{
2288 return qemu_event_init();
2289}
2290
0bf46a40
AL
2291void qemu_init_vcpu(void *_env)
2292{
2293 CPUState *env = _env;
2294
dc6b1c09
AP
2295 env->nr_cores = smp_cores;
2296 env->nr_threads = smp_threads;
3f7638ec
JD
2297 if (kvm_enabled())
2298 kvm_init_vcpu(env);
0bf46a40
AL
2299 return;
2300}
2301
8edac960
AL
2302int qemu_cpu_self(void *env)
2303{
2304 return 1;
2305}
2306
d6dc3d42
AL
2307static void resume_all_vcpus(void)
2308{
2309}
2310
2311static void pause_all_vcpus(void)
2312{
2313}
2314
8edac960
AL
2315void qemu_cpu_kick(void *env)
2316{
2317 return;
2318}
2319
d6dc3d42
AL
2320void qemu_notify_event(void)
2321{
2322 CPUState *env = cpu_single_env;
2323
7a5e5838 2324 qemu_event_increment ();
d6dc3d42
AL
2325 if (env) {
2326 cpu_exit(env);
4a1418e0 2327 }
1db89e91
PB
2328 if (next_cpu && env != next_cpu) {
2329 cpu_exit(next_cpu);
2330 }
d6dc3d42
AL
2331}
2332
d549db5a
GC
2333void qemu_mutex_lock_iothread(void) {}
2334void qemu_mutex_unlock_iothread(void) {}
4870852c 2335
6e29f5da
AL
2336void vm_stop(int reason)
2337{
2338 do_vm_stop(reason);
2339}
2340
d6dc3d42
AL
2341#else /* CONFIG_IOTHREAD */
2342
2343#include "qemu-thread.h"
2344
2345QemuMutex qemu_global_mutex;
2346static QemuMutex qemu_fair_mutex;
2347
2348static QemuThread io_thread;
2349
2350static QemuThread *tcg_cpu_thread;
2351static QemuCond *tcg_halt_cond;
2352
2353static int qemu_system_ready;
2354/* cpu creation */
2355static QemuCond qemu_cpu_cond;
2356/* system init */
2357static QemuCond qemu_system_cond;
2358static QemuCond qemu_pause_cond;
2359
cc84de95
MT
2360static void tcg_block_io_signals(void);
2361static void kvm_block_io_signals(CPUState *env);
d6dc3d42 2362static void unblock_io_signals(void);
d6dc3d42
AL
2363
2364static int qemu_init_main_loop(void)
2365{
2366 int ret;
2367
2368 ret = qemu_event_init();
2369 if (ret)
2370 return ret;
2371
2372 qemu_cond_init(&qemu_pause_cond);
2373 qemu_mutex_init(&qemu_fair_mutex);
2374 qemu_mutex_init(&qemu_global_mutex);
2375 qemu_mutex_lock(&qemu_global_mutex);
2376
2377 unblock_io_signals();
2378 qemu_thread_self(&io_thread);
2379
2380 return 0;
2381}
2382
fed6c344
MT
2383static void qemu_wait_io_event_common(CPUState *env)
2384{
2385 if (env->stop) {
2386 env->stop = 0;
2387 env->stopped = 1;
2388 qemu_cond_signal(&qemu_pause_cond);
2389 }
2390}
2391
d6dc3d42
AL
2392static void qemu_wait_io_event(CPUState *env)
2393{
2394 while (!tcg_has_work())
2395 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2396
2397 qemu_mutex_unlock(&qemu_global_mutex);
2398
2399 /*
2400 * Users of qemu_global_mutex can be starved, having no chance
2401 * to acquire it since this path will get to it first.
2402 * So use another lock to provide fairness.
2403 */
2404 qemu_mutex_lock(&qemu_fair_mutex);
2405 qemu_mutex_unlock(&qemu_fair_mutex);
2406
2407 qemu_mutex_lock(&qemu_global_mutex);
fed6c344
MT
2408 qemu_wait_io_event_common(env);
2409}
2410
cc84de95
MT
2411static void qemu_kvm_eat_signal(CPUState *env, int timeout)
2412{
2413 struct timespec ts;
2414 int r, e;
2415 siginfo_t siginfo;
2416 sigset_t waitset;
2417
2418 ts.tv_sec = timeout / 1000;
2419 ts.tv_nsec = (timeout % 1000) * 1000000;
2420
2421 sigemptyset(&waitset);
2422 sigaddset(&waitset, SIG_IPI);
2423
2424 qemu_mutex_unlock(&qemu_global_mutex);
2425 r = sigtimedwait(&waitset, &siginfo, &ts);
2426 e = errno;
2427 qemu_mutex_lock(&qemu_global_mutex);
2428
2429 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
2430 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
2431 exit(1);
d6dc3d42
AL
2432 }
2433}
2434
fed6c344
MT
2435static void qemu_kvm_wait_io_event(CPUState *env)
2436{
2437 while (!cpu_has_work(env))
2438 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2439
cc84de95 2440 qemu_kvm_eat_signal(env, 0);
fed6c344 2441 qemu_wait_io_event_common(env);
d6dc3d42
AL
2442}
2443
2444static int qemu_cpu_exec(CPUState *env);
2445
2446static void *kvm_cpu_thread_fn(void *arg)
2447{
2448 CPUState *env = arg;
2449
d6dc3d42 2450 qemu_thread_self(env->thread);
321c1cb1
JCD
2451 if (kvm_enabled())
2452 kvm_init_vcpu(env);
d6dc3d42 2453
cc84de95
MT
2454 kvm_block_io_signals(env);
2455
d6dc3d42
AL
2456 /* signal CPU creation */
2457 qemu_mutex_lock(&qemu_global_mutex);
2458 env->created = 1;
2459 qemu_cond_signal(&qemu_cpu_cond);
2460
2461 /* and wait for machine initialization */
2462 while (!qemu_system_ready)
2463 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2464
2465 while (1) {
2466 if (cpu_can_run(env))
2467 qemu_cpu_exec(env);
fed6c344 2468 qemu_kvm_wait_io_event(env);
d6dc3d42
AL
2469 }
2470
2471 return NULL;
2472}
2473
d6f4ade2 2474static bool tcg_cpu_exec(void);
d6dc3d42
AL
2475
2476static void *tcg_cpu_thread_fn(void *arg)
2477{
2478 CPUState *env = arg;
2479
cc84de95 2480 tcg_block_io_signals();
d6dc3d42
AL
2481 qemu_thread_self(env->thread);
2482
2483 /* signal CPU creation */
2484 qemu_mutex_lock(&qemu_global_mutex);
2485 for (env = first_cpu; env != NULL; env = env->next_cpu)
2486 env->created = 1;
2487 qemu_cond_signal(&qemu_cpu_cond);
2488
2489 /* and wait for machine initialization */
2490 while (!qemu_system_ready)
2491 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2492
2493 while (1) {
2494 tcg_cpu_exec();
2495 qemu_wait_io_event(cur_cpu);
2496 }
2497
2498 return NULL;
2499}
2500
2501void qemu_cpu_kick(void *_env)
2502{
2503 CPUState *env = _env;
2504 qemu_cond_broadcast(env->halt_cond);
2505 if (kvm_enabled())
cc84de95 2506 qemu_thread_signal(env->thread, SIG_IPI);
d6dc3d42
AL
2507}
2508
e5bc201d 2509int qemu_cpu_self(void *_env)
d6dc3d42 2510{
e5bc201d
GC
2511 CPUState *env = _env;
2512 QemuThread this;
2513
2514 qemu_thread_self(&this);
2515
2516 return qemu_thread_equal(&this, env->thread);
d6dc3d42
AL
2517}
2518
2519static void cpu_signal(int sig)
2520{
2521 if (cpu_single_env)
2522 cpu_exit(cpu_single_env);
2523}
2524
cc84de95 2525static void tcg_block_io_signals(void)
d6dc3d42
AL
2526{
2527 sigset_t set;
2528 struct sigaction sigact;
2529
2530 sigemptyset(&set);
2531 sigaddset(&set, SIGUSR2);
2532 sigaddset(&set, SIGIO);
2533 sigaddset(&set, SIGALRM);
adc8c965 2534 sigaddset(&set, SIGCHLD);
d6dc3d42
AL
2535 pthread_sigmask(SIG_BLOCK, &set, NULL);
2536
2537 sigemptyset(&set);
cc84de95 2538 sigaddset(&set, SIG_IPI);
d6dc3d42
AL
2539 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2540
2541 memset(&sigact, 0, sizeof(sigact));
2542 sigact.sa_handler = cpu_signal;
cc84de95
MT
2543 sigaction(SIG_IPI, &sigact, NULL);
2544}
2545
2546static void dummy_signal(int sig)
2547{
2548}
2549
2550static void kvm_block_io_signals(CPUState *env)
2551{
2552 int r;
2553 sigset_t set;
2554 struct sigaction sigact;
2555
2556 sigemptyset(&set);
2557 sigaddset(&set, SIGUSR2);
2558 sigaddset(&set, SIGIO);
2559 sigaddset(&set, SIGALRM);
2560 sigaddset(&set, SIGCHLD);
2561 sigaddset(&set, SIG_IPI);
2562 pthread_sigmask(SIG_BLOCK, &set, NULL);
2563
2564 pthread_sigmask(SIG_BLOCK, NULL, &set);
2565 sigdelset(&set, SIG_IPI);
2566
2567 memset(&sigact, 0, sizeof(sigact));
2568 sigact.sa_handler = dummy_signal;
2569 sigaction(SIG_IPI, &sigact, NULL);
2570
2571 r = kvm_set_signal_mask(env, &set);
2572 if (r) {
2573 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
2574 exit(1);
2575 }
d6dc3d42
AL
2576}
2577
2578static void unblock_io_signals(void)
2579{
2580 sigset_t set;
2581
2582 sigemptyset(&set);
2583 sigaddset(&set, SIGUSR2);
2584 sigaddset(&set, SIGIO);
2585 sigaddset(&set, SIGALRM);
2586 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2587
2588 sigemptyset(&set);
cc84de95 2589 sigaddset(&set, SIG_IPI);
d6dc3d42
AL
2590 pthread_sigmask(SIG_BLOCK, &set, NULL);
2591}
2592
2593static void qemu_signal_lock(unsigned int msecs)
2594{
2595 qemu_mutex_lock(&qemu_fair_mutex);
2596
2597 while (qemu_mutex_trylock(&qemu_global_mutex)) {
cc84de95 2598 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
d6dc3d42
AL
2599 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2600 break;
2601 }
2602 qemu_mutex_unlock(&qemu_fair_mutex);
2603}
2604
d549db5a 2605void qemu_mutex_lock_iothread(void)
d6dc3d42
AL
2606{
2607 if (kvm_enabled()) {
2608 qemu_mutex_lock(&qemu_fair_mutex);
2609 qemu_mutex_lock(&qemu_global_mutex);
2610 qemu_mutex_unlock(&qemu_fair_mutex);
2611 } else
2612 qemu_signal_lock(100);
2613}
2614
d549db5a 2615void qemu_mutex_unlock_iothread(void)
d6dc3d42
AL
2616{
2617 qemu_mutex_unlock(&qemu_global_mutex);
2618}
2619
2620static int all_vcpus_paused(void)
2621{
2622 CPUState *penv = first_cpu;
2623
2624 while (penv) {
2625 if (!penv->stopped)
2626 return 0;
2627 penv = (CPUState *)penv->next_cpu;
2628 }
2629
2630 return 1;
2631}
2632
2633static void pause_all_vcpus(void)
2634{
2635 CPUState *penv = first_cpu;
2636
2637 while (penv) {
2638 penv->stop = 1;
cc84de95 2639 qemu_thread_signal(penv->thread, SIG_IPI);
d6dc3d42
AL
2640 qemu_cpu_kick(penv);
2641 penv = (CPUState *)penv->next_cpu;
2642 }
2643
2644 while (!all_vcpus_paused()) {
2645 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2646 penv = first_cpu;
2647 while (penv) {
cc84de95 2648 qemu_thread_signal(penv->thread, SIG_IPI);
d6dc3d42
AL
2649 penv = (CPUState *)penv->next_cpu;
2650 }
2651 }
2652}
2653
2654static void resume_all_vcpus(void)
2655{
2656 CPUState *penv = first_cpu;
2657
2658 while (penv) {
2659 penv->stop = 0;
2660 penv->stopped = 0;
cc84de95 2661 qemu_thread_signal(penv->thread, SIG_IPI);
d6dc3d42
AL
2662 qemu_cpu_kick(penv);
2663 penv = (CPUState *)penv->next_cpu;
2664 }
2665}
2666
2667static void tcg_init_vcpu(void *_env)
2668{
2669 CPUState *env = _env;
2670 /* share a single thread for all cpus with TCG */
2671 if (!tcg_cpu_thread) {
2672 env->thread = qemu_mallocz(sizeof(QemuThread));
2673 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2674 qemu_cond_init(env->halt_cond);
2675 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2676 while (env->created == 0)
2677 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2678 tcg_cpu_thread = env->thread;
2679 tcg_halt_cond = env->halt_cond;
2680 } else {
2681 env->thread = tcg_cpu_thread;
2682 env->halt_cond = tcg_halt_cond;
2683 }
2684}
2685
2686static void kvm_start_vcpu(CPUState *env)
2687{
d6dc3d42
AL
2688 env->thread = qemu_mallocz(sizeof(QemuThread));
2689 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2690 qemu_cond_init(env->halt_cond);
2691 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2692 while (env->created == 0)
2693 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2694}
2695
2696void qemu_init_vcpu(void *_env)
2697{
2698 CPUState *env = _env;
2699
3f7638ec
JD
2700 env->nr_cores = smp_cores;
2701 env->nr_threads = smp_threads;
d6dc3d42
AL
2702 if (kvm_enabled())
2703 kvm_start_vcpu(env);
2704 else
2705 tcg_init_vcpu(env);
2706}
2707
2708void qemu_notify_event(void)
2709{
2710 qemu_event_increment();
2711}
2712
16b151c3
PB
2713static void qemu_system_vmstop_request(int reason)
2714{
2715 vmstop_requested = reason;
2716 qemu_notify_event();
2717}
2718
d6dc3d42
AL
2719void vm_stop(int reason)
2720{
2721 QemuThread me;
2722 qemu_thread_self(&me);
2723
2724 if (!qemu_thread_equal(&me, &io_thread)) {
2725 qemu_system_vmstop_request(reason);
2726 /*
2727 * FIXME: should not return to device code in case
2728 * vm_stop() has been requested.
2729 */
2730 if (cpu_single_env) {
2731 cpu_exit(cpu_single_env);
2732 cpu_single_env->stop = 1;
2733 }
2734 return;
2735 }
2736 do_vm_stop(reason);
2737}
2738
2739#endif
2740
2741
877cf882 2742#ifdef _WIN32
69d6451c 2743static void host_main_loop_wait(int *timeout)
56f3a5d0
AL
2744{
2745 int ret, ret2, i;
f331110f
FB
2746 PollingEntry *pe;
2747
c4b1fcc0 2748
f331110f
FB
2749 /* XXX: need to suppress polling by better using win32 events */
2750 ret = 0;
2751 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2752 ret |= pe->func(pe->opaque);
2753 }
e6b1e558 2754 if (ret == 0) {
a18e524a
FB
2755 int err;
2756 WaitObjects *w = &wait_objects;
3b46e624 2757
56f3a5d0 2758 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
a18e524a
FB
2759 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2760 if (w->func[ret - WAIT_OBJECT_0])
2761 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
3b46e624 2762
5fafdf24 2763 /* Check for additional signaled events */
e6b1e558 2764 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
3b46e624 2765
e6b1e558
TS
2766 /* Check if event is signaled */
2767 ret2 = WaitForSingleObject(w->events[i], 0);
2768 if(ret2 == WAIT_OBJECT_0) {
2769 if (w->func[i])
2770 w->func[i](w->opaque[i]);
2771 } else if (ret2 == WAIT_TIMEOUT) {
2772 } else {
2773 err = GetLastError();
2774 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
3b46e624
TS
2775 }
2776 }
a18e524a
FB
2777 } else if (ret == WAIT_TIMEOUT) {
2778 } else {
2779 err = GetLastError();
e6b1e558 2780 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
a18e524a 2781 }
f331110f 2782 }
56f3a5d0
AL
2783
2784 *timeout = 0;
2785}
2786#else
69d6451c 2787static void host_main_loop_wait(int *timeout)
56f3a5d0
AL
2788{
2789}
fd1dff4b 2790#endif
56f3a5d0 2791
d6f4ade2 2792void main_loop_wait(int nonblocking)
56f3a5d0
AL
2793{
2794 IOHandlerRecord *ioh;
2795 fd_set rfds, wfds, xfds;
2796 int ret, nfds;
2797 struct timeval tv;
d6f4ade2 2798 int timeout;
56f3a5d0 2799
d6f4ade2
PB
2800 if (nonblocking)
2801 timeout = 0;
2802 else {
2803 timeout = qemu_calculate_timeout();
2804 qemu_bh_update_timeout(&timeout);
2805 }
56f3a5d0
AL
2806
2807 host_main_loop_wait(&timeout);
2808
fd1dff4b
FB
2809 /* poll any events */
2810 /* XXX: separate device handlers from system ones */
6abfbd79 2811 nfds = -1;
fd1dff4b
FB
2812 FD_ZERO(&rfds);
2813 FD_ZERO(&wfds);
e035649e 2814 FD_ZERO(&xfds);
31d4ee6c 2815 QLIST_FOREACH(ioh, &io_handlers, next) {
cafffd40
TS
2816 if (ioh->deleted)
2817 continue;
fd1dff4b
FB
2818 if (ioh->fd_read &&
2819 (!ioh->fd_read_poll ||
2820 ioh->fd_read_poll(ioh->opaque) != 0)) {
2821 FD_SET(ioh->fd, &rfds);
2822 if (ioh->fd > nfds)
2823 nfds = ioh->fd;
2824 }
2825 if (ioh->fd_write) {
2826 FD_SET(ioh->fd, &wfds);
2827 if (ioh->fd > nfds)
2828 nfds = ioh->fd;
2829 }
2830 }
3b46e624 2831
56f3a5d0
AL
2832 tv.tv_sec = timeout / 1000;
2833 tv.tv_usec = (timeout % 1000) * 1000;
2834
d918f23e
JK
2835 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2836
4870852c 2837 qemu_mutex_unlock_iothread();
e035649e 2838 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4870852c 2839 qemu_mutex_lock_iothread();
fd1dff4b 2840 if (ret > 0) {
31d4ee6c 2841 IOHandlerRecord *pioh;
cafffd40 2842
31d4ee6c 2843 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
cafffd40 2844 if (ioh->deleted) {
31d4ee6c 2845 QLIST_REMOVE(ioh, next);
cafffd40 2846 qemu_free(ioh);
4bed9837
JQ
2847 continue;
2848 }
2849 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2850 ioh->fd_read(ioh->opaque);
2851 }
2852 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2853 ioh->fd_write(ioh->opaque);
31d4ee6c 2854 }
cafffd40 2855 }
fd1dff4b 2856 }
d918f23e
JK
2857
2858 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 2859
b6964827 2860 qemu_run_all_timers();
21d5d12b 2861
423f0742
PB
2862 /* Check bottom-halves last in case any of the earlier events triggered
2863 them. */
2864 qemu_bh_poll();
3b46e624 2865
5905b2e5
FB
2866}
2867
43b96858 2868static int qemu_cpu_exec(CPUState *env)
5905b2e5 2869{
43b96858 2870 int ret;
89bfc105
FB
2871#ifdef CONFIG_PROFILER
2872 int64_t ti;
2873#endif
5905b2e5 2874
89bfc105 2875#ifdef CONFIG_PROFILER
43b96858 2876 ti = profile_getclock();
89bfc105 2877#endif
43b96858
AL
2878 if (use_icount) {
2879 int64_t count;
2880 int decr;
2881 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
2882 env->icount_decr.u16.low = 0;
2883 env->icount_extra = 0;
e2a7bb4b 2884 count = qemu_icount_round (qemu_next_deadline());
43b96858
AL
2885 qemu_icount += count;
2886 decr = (count > 0xffff) ? 0xffff : count;
2887 count -= decr;
2888 env->icount_decr.u16.low = decr;
2889 env->icount_extra = count;
2890 }
2891 ret = cpu_exec(env);
89bfc105 2892#ifdef CONFIG_PROFILER
43b96858 2893 qemu_time += profile_getclock() - ti;
89bfc105 2894#endif
43b96858
AL
2895 if (use_icount) {
2896 /* Fold pending instructions back into the
2897 instruction counter, and clear the interrupt flag. */
2898 qemu_icount -= (env->icount_decr.u16.low
2899 + env->icount_extra);
2900 env->icount_decr.u32 = 0;
2901 env->icount_extra = 0;
2902 }
2903 return ret;
2904}
2905
d6f4ade2 2906static bool tcg_cpu_exec(void)
e6e35b1e 2907{
d6dc3d42 2908 int ret = 0;
e6e35b1e
AL
2909
2910 if (next_cpu == NULL)
2911 next_cpu = first_cpu;
2912 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
2913 CPUState *env = cur_cpu = next_cpu;
2914
972abbe0
PB
2915 qemu_clock_enable(vm_clock,
2916 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
2917
63255043 2918 if (qemu_alarm_pending())
e6e35b1e 2919 break;
d6dc3d42
AL
2920 if (cpu_can_run(env))
2921 ret = qemu_cpu_exec(env);
c37cc7b0
MT
2922 else if (env->stop)
2923 break;
2924
e6e35b1e
AL
2925 if (ret == EXCP_DEBUG) {
2926 gdb_set_stop_cpu(env);
54fc6ea9 2927 debug_requested = EXCP_DEBUG;
e6e35b1e
AL
2928 break;
2929 }
2930 }
d6f4ade2 2931 return tcg_has_work();
e6e35b1e
AL
2932}
2933
87d0a28e
BS
2934static void set_numa_modes(void)
2935{
2936 CPUState *env;
2937 int i;
2938
2939 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2940 for (i = 0; i < nb_numa_nodes; i++) {
2941 if (node_cpumask[i] & (1 << env->cpu_index)) {
2942 env->numa_node = i;
2943 }
2944 }
2945 }
2946}
2947
de06f8d1
BS
2948static void set_cpu_log(const char *optarg)
2949{
2950 int mask;
2951 const CPULogItem *item;
2952
2953 mask = cpu_str_to_log_mask(optarg);
2954 if (!mask) {
2955 printf("Log items (comma separated):\n");
2956 for (item = cpu_log_items; item->mask != 0; item++) {
2957 printf("%-10s %s\n", item->name, item->help);
2958 }
2959 exit(1);
2960 }
2961 cpu_set_log(mask);
2962}
2963
43b96858
AL
2964static int vm_can_run(void)
2965{
2966 if (powerdown_requested)
2967 return 0;
2968 if (reset_requested)
2969 return 0;
2970 if (shutdown_requested)
2971 return 0;
e568902a
AL
2972 if (debug_requested)
2973 return 0;
43b96858
AL
2974 return 1;
2975}
2976
d9c32310
BS
2977qemu_irq qemu_system_powerdown;
2978
43b96858
AL
2979static void main_loop(void)
2980{
6e29f5da 2981 int r;
e6e35b1e 2982
d6dc3d42
AL
2983#ifdef CONFIG_IOTHREAD
2984 qemu_system_ready = 1;
2985 qemu_cond_broadcast(&qemu_system_cond);
2986#endif
2987
6e29f5da 2988 for (;;) {
43b96858 2989 do {
d6f4ade2 2990 bool nonblocking = false;
e6e35b1e
AL
2991#ifdef CONFIG_PROFILER
2992 int64_t ti;
2993#endif
d6dc3d42 2994#ifndef CONFIG_IOTHREAD
d6f4ade2 2995 nonblocking = tcg_cpu_exec();
d6dc3d42 2996#endif
89bfc105 2997#ifdef CONFIG_PROFILER
43b96858 2998 ti = profile_getclock();
89bfc105 2999#endif
d6f4ade2 3000 main_loop_wait(nonblocking);
89bfc105 3001#ifdef CONFIG_PROFILER
43b96858 3002 dev_time += profile_getclock() - ti;
89bfc105 3003#endif
e568902a 3004 } while (vm_can_run());
43b96858 3005
54fc6ea9
BS
3006 if ((r = qemu_debug_requested())) {
3007 vm_stop(r);
b1a15e7e 3008 }
43b96858 3009 if (qemu_shutdown_requested()) {
242cd003 3010 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858
AL
3011 if (no_shutdown) {
3012 vm_stop(0);
3013 no_shutdown = 0;
3014 } else
3015 break;
3016 }
d6dc3d42
AL
3017 if (qemu_reset_requested()) {
3018 pause_all_vcpus();
43b96858 3019 qemu_system_reset();
d6dc3d42
AL
3020 resume_all_vcpus();
3021 }
d9c32310 3022 if (qemu_powerdown_requested()) {
242cd003 3023 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
3024 qemu_irq_raise(qemu_system_powerdown);
3025 }
b1a15e7e 3026 if ((r = qemu_vmstop_requested())) {
6e29f5da 3027 vm_stop(r);
b1a15e7e 3028 }
b4608c04 3029 }
d6dc3d42 3030 pause_all_vcpus();
b4608c04
FB
3031}
3032
9bd7e6d9
PB
3033static void version(void)
3034{
4a19f1ec 3035 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
3036}
3037
15f82208 3038static void help(int exitcode)
0824d6fc 3039{
e8105ebb 3040 const char *options_help =
5824d651
BS
3041#define DEF(option, opt_arg, opt_enum, opt_help) \
3042 opt_help
3043#define DEFHEADING(text) stringify(text) "\n"
3044#include "qemu-options.h"
3045#undef DEF
3046#undef DEFHEADING
3047#undef GEN_DOCS
e8105ebb
PB
3048 ;
3049 version();
3050 printf("usage: %s [options] [disk_image]\n"
3051 "\n"
3052 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3f020d70 3053 "\n"
e8105ebb 3054 "%s\n"
3f020d70 3055 "During emulation, the following keys are useful:\n"
3056 "ctrl-alt-f toggle full screen\n"
3057 "ctrl-alt-n switch to virtual console 'n'\n"
3058 "ctrl-alt toggle mouse and keyboard grab\n"
3059 "\n"
e8105ebb
PB
3060 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3061 "qemu",
3062 options_help);
15f82208 3063 exit(exitcode);
0824d6fc
FB
3064}
3065
cd6f1169
FB
3066#define HAS_ARG 0x0001
3067
3068enum {
5824d651
BS
3069#define DEF(option, opt_arg, opt_enum, opt_help) \
3070 opt_enum,
3071#define DEFHEADING(text)
3072#include "qemu-options.h"
3073#undef DEF
3074#undef DEFHEADING
3075#undef GEN_DOCS
cd6f1169
FB
3076};
3077
3078typedef struct QEMUOption {
3079 const char *name;
3080 int flags;
3081 int index;
3082} QEMUOption;
3083
dbed7e40 3084static const QEMUOption qemu_options[] = {
cd6f1169 3085 { "h", 0, QEMU_OPTION_h },
5824d651
BS
3086#define DEF(option, opt_arg, opt_enum, opt_help) \
3087 { option, opt_arg, opt_enum },
3088#define DEFHEADING(text)
3089#include "qemu-options.h"
3090#undef DEF
3091#undef DEFHEADING
3092#undef GEN_DOCS
cd6f1169 3093 { NULL },
fc01f7e7
FB
3094};
3095
1d14ffa9 3096#ifdef HAS_AUDIO
6a36d84e 3097struct soundhw soundhw[] = {
b00052e4 3098#ifdef HAS_AUDIO_CHOICE
4ce7ff6e 3099#if defined(TARGET_I386) || defined(TARGET_MIPS)
fd06c375
FB
3100 {
3101 "pcspk",
3102 "PC speaker",
3103 0,
3104 1,
3105 { .init_isa = pcspk_audio_init }
3106 },
3107#endif
4c9b53e3 3108
3109#ifdef CONFIG_SB16
6a36d84e
FB
3110 {
3111 "sb16",
3112 "Creative Sound Blaster 16",
3113 0,
3114 1,
3115 { .init_isa = SB16_init }
3116 },
4c9b53e3 3117#endif
6a36d84e 3118
cc53d26d 3119#ifdef CONFIG_CS4231A
3120 {
3121 "cs4231a",
3122 "CS4231A",
3123 0,
3124 1,
3125 { .init_isa = cs4231a_init }
3126 },
3127#endif
3128
1d14ffa9 3129#ifdef CONFIG_ADLIB
6a36d84e
FB
3130 {
3131 "adlib",
1d14ffa9 3132#ifdef HAS_YMF262
6a36d84e 3133 "Yamaha YMF262 (OPL3)",
1d14ffa9 3134#else
6a36d84e 3135 "Yamaha YM3812 (OPL2)",
1d14ffa9 3136#endif
6a36d84e
FB
3137 0,
3138 1,
3139 { .init_isa = Adlib_init }
3140 },
1d14ffa9 3141#endif
6a36d84e 3142
1d14ffa9 3143#ifdef CONFIG_GUS
6a36d84e
FB
3144 {
3145 "gus",
3146 "Gravis Ultrasound GF1",
3147 0,
3148 1,
3149 { .init_isa = GUS_init }
3150 },
1d14ffa9 3151#endif
6a36d84e 3152
4c9b53e3 3153#ifdef CONFIG_AC97
e5c9a13e
AZ
3154 {
3155 "ac97",
3156 "Intel 82801AA AC97 Audio",
3157 0,
3158 0,
3159 { .init_pci = ac97_init }
3160 },
4c9b53e3 3161#endif
e5c9a13e 3162
4c9b53e3 3163#ifdef CONFIG_ES1370
6a36d84e
FB
3164 {
3165 "es1370",
3166 "ENSONIQ AudioPCI ES1370",
3167 0,
3168 0,
3169 { .init_pci = es1370_init }
3170 },
b00052e4 3171#endif
6a36d84e 3172
4c9b53e3 3173#endif /* HAS_AUDIO_CHOICE */
3174
6a36d84e
FB
3175 { NULL, NULL, 0, 0, { NULL } }
3176};
3177
3178static void select_soundhw (const char *optarg)
3179{
3180 struct soundhw *c;
3181
3182 if (*optarg == '?') {
3183 show_valid_cards:
3184
3185 printf ("Valid sound card names (comma separated):\n");
3186 for (c = soundhw; c->name; ++c) {
3187 printf ("%-11s %s\n", c->name, c->descr);
3188 }
3189 printf ("\n-soundhw all will enable all of the above\n");
1d14ffa9
FB
3190 exit (*optarg != '?');
3191 }
3192 else {
6a36d84e 3193 size_t l;
1d14ffa9
FB
3194 const char *p;
3195 char *e;
3196 int bad_card = 0;
3197
6a36d84e
FB
3198 if (!strcmp (optarg, "all")) {
3199 for (c = soundhw; c->name; ++c) {
3200 c->enabled = 1;
3201 }
3202 return;
3203 }
1d14ffa9 3204
6a36d84e 3205 p = optarg;
1d14ffa9
FB
3206 while (*p) {
3207 e = strchr (p, ',');
3208 l = !e ? strlen (p) : (size_t) (e - p);
6a36d84e
FB
3209
3210 for (c = soundhw; c->name; ++c) {
b3d6fb4a 3211 if (!strncmp (c->name, p, l) && !c->name[l]) {
6a36d84e 3212 c->enabled = 1;
1d14ffa9
FB
3213 break;
3214 }
3215 }
6a36d84e
FB
3216
3217 if (!c->name) {
1d14ffa9
FB
3218 if (l > 80) {
3219 fprintf (stderr,
3220 "Unknown sound card name (too big to show)\n");
3221 }
3222 else {
3223 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3224 (int) l, p);
3225 }
3226 bad_card = 1;
3227 }
3228 p += l + (e != NULL);
3229 }
3230
3231 if (bad_card)
3232 goto show_valid_cards;
3233 }
3234}
3235#endif
3236
3893c124 3237static void select_vgahw (const char *p)
3238{
3239 const char *opts;
3240
64465297 3241 default_vga = 0;
86176759 3242 vga_interface_type = VGA_NONE;
3893c124 3243 if (strstart(p, "std", &opts)) {
86176759 3244 vga_interface_type = VGA_STD;
3893c124 3245 } else if (strstart(p, "cirrus", &opts)) {
86176759 3246 vga_interface_type = VGA_CIRRUS;
3893c124 3247 } else if (strstart(p, "vmware", &opts)) {
86176759 3248 vga_interface_type = VGA_VMWARE;
94909d9f 3249 } else if (strstart(p, "xenfb", &opts)) {
86176759 3250 vga_interface_type = VGA_XENFB;
28b85ed8 3251 } else if (!strstart(p, "none", &opts)) {
3893c124 3252 invalid_vga:
3253 fprintf(stderr, "Unknown vga type: %s\n", p);
3254 exit(1);
3255 }
cb5a7aa8 3256 while (*opts) {
3257 const char *nextopt;
3258
3259 if (strstart(opts, ",retrace=", &nextopt)) {
3260 opts = nextopt;
3261 if (strstart(opts, "dumb", &nextopt))
3262 vga_retrace_method = VGA_RETRACE_DUMB;
3263 else if (strstart(opts, "precise", &nextopt))
3264 vga_retrace_method = VGA_RETRACE_PRECISE;
3265 else goto invalid_vga;
3266 } else goto invalid_vga;
3267 opts = nextopt;
3268 }
3893c124 3269}
3270
7d4c3d53
MA
3271#ifdef TARGET_I386
3272static int balloon_parse(const char *arg)
3273{
382f0743 3274 QemuOpts *opts;
7d4c3d53 3275
382f0743
GH
3276 if (strcmp(arg, "none") == 0) {
3277 return 0;
3278 }
3279
3280 if (!strncmp(arg, "virtio", 6)) {
3281 if (arg[6] == ',') {
3282 /* have params -> parse them */
8212c64f 3283 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
382f0743
GH
3284 if (!opts)
3285 return -1;
3286 } else {
3287 /* create empty opts */
3288 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
7d4c3d53 3289 }
382f0743
GH
3290 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
3291 return 0;
7d4c3d53 3292 }
382f0743
GH
3293
3294 return -1;
7d4c3d53
MA
3295}
3296#endif
3297
3587d7e6
FB
3298#ifdef _WIN32
3299static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3300{
3301 exit(STATUS_CONTROL_C_EXIT);
3302 return TRUE;
3303}
3304#endif
3305
c4be29ff 3306int qemu_uuid_parse(const char *str, uint8_t *uuid)
8fcb1b90
BS
3307{
3308 int ret;
3309
3310 if(strlen(str) != 36)
3311 return -1;
3312
3313 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3314 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3315 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3316
3317 if(ret != 16)
3318 return -1;
3319
b6f6e3d3
AL
3320#ifdef TARGET_I386
3321 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3322#endif
3323
8fcb1b90
BS
3324 return 0;
3325}
3326
de06f8d1
BS
3327#ifdef TARGET_I386
3328static void do_acpitable_option(const char *optarg)
3329{
3330 if (acpi_table_add(optarg) < 0) {
3331 fprintf(stderr, "Wrong acpi table provided\n");
3332 exit(1);
3333 }
3334}
3335#endif
3336
3337#ifdef TARGET_I386
3338static void do_smbios_option(const char *optarg)
3339{
3340 if (smbios_entry_add(optarg) < 0) {
3341 fprintf(stderr, "Wrong smbios provided\n");
3342 exit(1);
3343 }
3344}
3345#endif
3346
3347static void cpudef_init(void)
3348{
3349#if defined(cpudef_setup)
3350 cpudef_setup(); /* parse cpu definitions in target config file */
3351#endif
3352}
3353
5b08fc10
AL
3354#ifndef _WIN32
3355
3356static void termsig_handler(int signal)
3357{
3358 qemu_system_shutdown_request();
3359}
3360
7c3370d4
JK
3361static void sigchld_handler(int signal)
3362{
3363 waitpid(-1, NULL, WNOHANG);
3364}
3365
3366static void sighandler_setup(void)
5b08fc10
AL
3367{
3368 struct sigaction act;
3369
3370 memset(&act, 0, sizeof(act));
3371 act.sa_handler = termsig_handler;
3372 sigaction(SIGINT, &act, NULL);
3373 sigaction(SIGHUP, &act, NULL);
3374 sigaction(SIGTERM, &act, NULL);
7c3370d4
JK
3375
3376 act.sa_handler = sigchld_handler;
3377 act.sa_flags = SA_NOCLDSTOP;
3378 sigaction(SIGCHLD, &act, NULL);
5b08fc10
AL
3379}
3380
3381#endif
3382
5cea8590
PB
3383#ifdef _WIN32
3384/* Look for support files in the same directory as the executable. */
3385static char *find_datadir(const char *argv0)
3386{
3387 char *p;
3388 char buf[MAX_PATH];
3389 DWORD len;
3390
3391 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3392 if (len == 0) {
c5947808 3393 return NULL;
5cea8590
PB
3394 }
3395
3396 buf[len] = 0;
3397 p = buf + len - 1;
3398 while (p != buf && *p != '\\')
3399 p--;
3400 *p = 0;
3401 if (access(buf, R_OK) == 0) {
3402 return qemu_strdup(buf);
3403 }
3404 return NULL;
3405}
3406#else /* !_WIN32 */
3407
3408/* Find a likely location for support files using the location of the binary.
3409 For installed binaries this will be "$bindir/../share/qemu". When
3410 running from the build tree this will be "$bindir/../pc-bios". */
3411#define SHARE_SUFFIX "/share/qemu"
3412#define BUILD_SUFFIX "/pc-bios"
3413static char *find_datadir(const char *argv0)
3414{
3415 char *dir;
3416 char *p = NULL;
3417 char *res;
5cea8590 3418 char buf[PATH_MAX];
3a41759d 3419 size_t max_len;
5cea8590
PB
3420
3421#if defined(__linux__)
3422 {
3423 int len;
3424 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3425 if (len > 0) {
3426 buf[len] = 0;
3427 p = buf;
3428 }
3429 }
3430#elif defined(__FreeBSD__)
3431 {
3432 int len;
3433 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3434 if (len > 0) {
3435 buf[len] = 0;
3436 p = buf;
3437 }
3438 }
3439#endif
3440 /* If we don't have any way of figuring out the actual executable
3441 location then try argv[0]. */
3442 if (!p) {
4d224196 3443 p = realpath(argv0, buf);
5cea8590
PB
3444 if (!p) {
3445 return NULL;
3446 }
3447 }
3448 dir = dirname(p);
3449 dir = dirname(dir);
3450
3a41759d
BS
3451 max_len = strlen(dir) +
3452 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3453 res = qemu_mallocz(max_len);
3454 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
5cea8590 3455 if (access(res, R_OK)) {
3a41759d 3456 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
5cea8590
PB
3457 if (access(res, R_OK)) {
3458 qemu_free(res);
3459 res = NULL;
3460 }
3461 }
4d224196 3462
5cea8590
PB
3463 return res;
3464}
3465#undef SHARE_SUFFIX
3466#undef BUILD_SUFFIX
3467#endif
3468
3469char *qemu_find_file(int type, const char *name)
3470{
3471 int len;
3472 const char *subdir;
3473 char *buf;
3474
3475 /* If name contains path separators then try it as a straight path. */
3476 if ((strchr(name, '/') || strchr(name, '\\'))
3477 && access(name, R_OK) == 0) {
73ffc805 3478 return qemu_strdup(name);
5cea8590
PB
3479 }
3480 switch (type) {
3481 case QEMU_FILE_TYPE_BIOS:
3482 subdir = "";
3483 break;
3484 case QEMU_FILE_TYPE_KEYMAP:
3485 subdir = "keymaps/";
3486 break;
3487 default:
3488 abort();
3489 }
3490 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3491 buf = qemu_mallocz(len);
3a41759d 3492 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
3493 if (access(buf, R_OK)) {
3494 qemu_free(buf);
3495 return NULL;
3496 }
3497 return buf;
3498}
3499
ff952ba2
MA
3500static int device_help_func(QemuOpts *opts, void *opaque)
3501{
3502 return qdev_device_help(opts);
3503}
3504
f31d07d1
GH
3505static int device_init_func(QemuOpts *opts, void *opaque)
3506{
3507 DeviceState *dev;
3508
3509 dev = qdev_device_add(opts);
3510 if (!dev)
3511 return -1;
3512 return 0;
3513}
3514
1a688d3b
GH
3515static int chardev_init_func(QemuOpts *opts, void *opaque)
3516{
3517 CharDriverState *chr;
3518
3519 chr = qemu_chr_open_opts(opts, NULL);
3520 if (!chr)
3521 return -1;
3522 return 0;
3523}
3524
88589343
GH
3525static int mon_init_func(QemuOpts *opts, void *opaque)
3526{
3527 CharDriverState *chr;
3528 const char *chardev;
3529 const char *mode;
3530 int flags;
3531
3532 mode = qemu_opt_get(opts, "mode");
3533 if (mode == NULL) {
3534 mode = "readline";
3535 }
3536 if (strcmp(mode, "readline") == 0) {
3537 flags = MONITOR_USE_READLINE;
3538 } else if (strcmp(mode, "control") == 0) {
3539 flags = MONITOR_USE_CONTROL;
3540 } else {
3541 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
3542 exit(1);
3543 }
3544
3545 if (qemu_opt_get_bool(opts, "default", 0))
3546 flags |= MONITOR_IS_DEFAULT;
3547
3548 chardev = qemu_opt_get(opts, "chardev");
3549 chr = qemu_chr_find(chardev);
3550 if (chr == NULL) {
3551 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
3552 exit(1);
3553 }
3554
3555 monitor_init(chr, flags);
3556 return 0;
3557}
3558
6ca5582d 3559static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
3560{
3561 static int monitor_device_index = 0;
3562 QemuOpts *opts;
3563 const char *p;
3564 char label[32];
3565 int def = 0;
3566
3567 if (strstart(optarg, "chardev:", &p)) {
3568 snprintf(label, sizeof(label), "%s", p);
3569 } else {
3570 if (monitor_device_index) {
3571 snprintf(label, sizeof(label), "monitor%d",
3572 monitor_device_index);
3573 } else {
3574 snprintf(label, sizeof(label), "monitor");
3575 def = 1;
3576 }
3577 opts = qemu_chr_parse_compat(label, optarg);
3578 if (!opts) {
3579 fprintf(stderr, "parse error: %s\n", optarg);
3580 exit(1);
3581 }
3582 }
3583
3584 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
3585 if (!opts) {
3586 fprintf(stderr, "duplicate chardev: %s\n", label);
3587 exit(1);
3588 }
6ca5582d 3589 qemu_opt_set(opts, "mode", mode);
88589343
GH
3590 qemu_opt_set(opts, "chardev", label);
3591 if (def)
3592 qemu_opt_set(opts, "default", "on");
3593 monitor_device_index++;
3594}
3595
bd3c948d
GH
3596struct device_config {
3597 enum {
aee1b935
GH
3598 DEV_USB, /* -usbdevice */
3599 DEV_BT, /* -bt */
3600 DEV_SERIAL, /* -serial */
3601 DEV_PARALLEL, /* -parallel */
3602 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 3603 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
3604 } type;
3605 const char *cmdline;
72cf2d4f 3606 QTAILQ_ENTRY(device_config) next;
bd3c948d 3607};
72cf2d4f 3608QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
3609
3610static void add_device_config(int type, const char *cmdline)
3611{
3612 struct device_config *conf;
3613
3614 conf = qemu_mallocz(sizeof(*conf));
3615 conf->type = type;
3616 conf->cmdline = cmdline;
72cf2d4f 3617 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
3618}
3619
3620static int foreach_device_config(int type, int (*func)(const char *cmdline))
3621{
3622 struct device_config *conf;
3623 int rc;
3624
72cf2d4f 3625 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
3626 if (conf->type != type)
3627 continue;
3628 rc = func(conf->cmdline);
3629 if (0 != rc)
3630 return rc;
3631 }
3632 return 0;
3633}
3634
998bbd74
GH
3635static int serial_parse(const char *devname)
3636{
3637 static int index = 0;
3638 char label[32];
3639
3640 if (strcmp(devname, "none") == 0)
3641 return 0;
3642 if (index == MAX_SERIAL_PORTS) {
3643 fprintf(stderr, "qemu: too many serial ports\n");
3644 exit(1);
3645 }
3646 snprintf(label, sizeof(label), "serial%d", index);
3647 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3648 if (!serial_hds[index]) {
3649 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
3650 devname, strerror(errno));
3651 return -1;
3652 }
3653 index++;
3654 return 0;
3655}
3656
6a5e8b0e
GH
3657static int parallel_parse(const char *devname)
3658{
3659 static int index = 0;
3660 char label[32];
3661
3662 if (strcmp(devname, "none") == 0)
3663 return 0;
3664 if (index == MAX_PARALLEL_PORTS) {
3665 fprintf(stderr, "qemu: too many parallel ports\n");
3666 exit(1);
3667 }
3668 snprintf(label, sizeof(label), "parallel%d", index);
3669 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
3670 if (!parallel_hds[index]) {
3671 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
3672 devname, strerror(errno));
3673 return -1;
3674 }
3675 index++;
3676 return 0;
3677}
3678
aee1b935
GH
3679static int virtcon_parse(const char *devname)
3680{
3681 static int index = 0;
3682 char label[32];
392ecf54 3683 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
3684
3685 if (strcmp(devname, "none") == 0)
3686 return 0;
3687 if (index == MAX_VIRTIO_CONSOLES) {
3688 fprintf(stderr, "qemu: too many virtio consoles\n");
3689 exit(1);
3690 }
392ecf54
AS
3691
3692 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
3693 qemu_opt_set(bus_opts, "driver", "virtio-serial");
3694
3695 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
3696 qemu_opt_set(dev_opts, "driver", "virtconsole");
3697
aee1b935
GH
3698 snprintf(label, sizeof(label), "virtcon%d", index);
3699 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
3700 if (!virtcon_hds[index]) {
3701 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
3702 devname, strerror(errno));
3703 return -1;
3704 }
392ecf54
AS
3705 qemu_opt_set(dev_opts, "chardev", label);
3706
aee1b935
GH
3707 index++;
3708 return 0;
3709}
3710
c9f398e5
PA
3711static int debugcon_parse(const char *devname)
3712{
3713 QemuOpts *opts;
3714
3715 if (!qemu_chr_open("debugcon", devname, NULL)) {
3716 exit(1);
3717 }
3718 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
3719 if (!opts) {
3720 fprintf(stderr, "qemu: already have a debugcon device\n");
3721 exit(1);
3722 }
3723 qemu_opt_set(opts, "driver", "isa-debugcon");
3724 qemu_opt_set(opts, "chardev", "debugcon");
3725 return 0;
3726}
3727
6530a97b
AL
3728static const QEMUOption *lookup_opt(int argc, char **argv,
3729 const char **poptarg, int *poptind)
3730{
3731 const QEMUOption *popt;
3732 int optind = *poptind;
3733 char *r = argv[optind];
3734 const char *optarg;
3735
0f0bc3f1 3736 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
3737 optind++;
3738 /* Treat --foo the same as -foo. */
3739 if (r[1] == '-')
3740 r++;
3741 popt = qemu_options;
3742 for(;;) {
3743 if (!popt->name) {
0f0bc3f1 3744 error_report("invalid option");
6530a97b
AL
3745 exit(1);
3746 }
3747 if (!strcmp(popt->name, r + 1))
3748 break;
3749 popt++;
3750 }
3751 if (popt->flags & HAS_ARG) {
3752 if (optind >= argc) {
0f0bc3f1 3753 error_report("requires an argument");
6530a97b
AL
3754 exit(1);
3755 }
3756 optarg = argv[optind++];
0f0bc3f1 3757 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
3758 } else {
3759 optarg = NULL;
3760 }
3761
3762 *poptarg = optarg;
3763 *poptind = optind;
3764
3765 return popt;
3766}
3767
902b3d5c 3768int main(int argc, char **argv, char **envp)
0824d6fc 3769{
59030a8c 3770 const char *gdbstub_dev = NULL;
28c5af54 3771 uint32_t boot_devices_bitmap = 0;
e4bcb14c 3772 int i;
28c5af54 3773 int snapshot, linux_boot, net_boot;
4e3de9e9 3774 const char *icount_option = NULL;
7f7f9873 3775 const char *initrd_filename;
a20dd508 3776 const char *kernel_filename, *kernel_cmdline;
195325a4 3777 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 3778 DisplayState *ds;
7d957bd8 3779 DisplayChangeListener *dcl;
46d4767d 3780 int cyls, heads, secs, translation;
f31d07d1 3781 QemuOpts *hda_opts = NULL, *opts;
cd6f1169 3782 int optind;
6530a97b 3783 const char *optarg;
d63d307f 3784 const char *loadvm = NULL;
cc1daa40 3785 QEMUMachine *machine;
94fc95cd 3786 const char *cpu_model;
b9e82a59 3787#ifndef _WIN32
71e3ceb8 3788 int fds[2];
b9e82a59 3789#endif
26a5f13b 3790 int tb_size;
93815bc2 3791 const char *pid_file = NULL;
5bb7910a 3792 const char *incoming = NULL;
b9e82a59 3793#ifndef _WIN32
54042bcf
AL
3794 int fd = 0;
3795 struct passwd *pwd = NULL;
0858532e
AL
3796 const char *chroot_dir = NULL;
3797 const char *run_as = NULL;
b9e82a59 3798#endif
993fbfdb 3799 int show_vnc_port = 0;
292444cb 3800 int defconfig = 1;
0bd48850 3801
65abca0a
MA
3802 error_set_progname(argv[0]);
3803
6875204c
JK
3804 init_clocks();
3805
902b3d5c 3806 qemu_cache_utils_init(envp);
3807
72cf2d4f 3808 QLIST_INIT (&vm_change_state_head);
be995c27
FB
3809#ifndef _WIN32
3810 {
3811 struct sigaction act;
3812 sigfillset(&act.sa_mask);
3813 act.sa_flags = 0;
3814 act.sa_handler = SIG_IGN;
3815 sigaction(SIGPIPE, &act, NULL);
3816 }
3587d7e6
FB
3817#else
3818 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
a8e5ac33
FB
3819 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3820 QEMU to run on a single CPU */
3821 {
3822 HANDLE h;
3823 DWORD mask, smask;
3824 int i;
3825 h = GetCurrentProcess();
3826 if (GetProcessAffinityMask(h, &mask, &smask)) {
3827 for(i = 0; i < 32; i++) {
3828 if (mask & (1 << i))
3829 break;
3830 }
3831 if (i != 32) {
3832 mask = 1 << i;
3833 SetProcessAffinityMask(h, mask);
3834 }
3835 }
3836 }
67b915a5 3837#endif
be995c27 3838
f80f9ec9 3839 module_call_init(MODULE_INIT_MACHINE);
0c257437 3840 machine = find_default_machine();
94fc95cd 3841 cpu_model = NULL;
fc01f7e7 3842 initrd_filename = NULL;
4fc5d071 3843 ram_size = 0;
33e3963e 3844 snapshot = 0;
a20dd508
FB
3845 kernel_filename = NULL;
3846 kernel_cmdline = "";
c4b1fcc0 3847 cyls = heads = secs = 0;
46d4767d 3848 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 3849
268a362c
AL
3850 for (i = 0; i < MAX_NODES; i++) {
3851 node_mem[i] = 0;
3852 node_cpumask[i] = 0;
3853 }
3854
268a362c 3855 nb_numa_nodes = 0;
7c9d8e07 3856 nb_nics = 0;
3b46e624 3857
26a5f13b 3858 tb_size = 0;
41bd639b
BS
3859 autostart= 1;
3860
292444cb
AL
3861 /* first pass of option parsing */
3862 optind = 1;
3863 while (optind < argc) {
3864 if (argv[optind][0] != '-') {
3865 /* disk image */
28e68d68 3866 optind++;
292444cb
AL
3867 continue;
3868 } else {
3869 const QEMUOption *popt;
3870
3871 popt = lookup_opt(argc, argv, &optarg, &optind);
3872 switch (popt->index) {
3873 case QEMU_OPTION_nodefconfig:
3874 defconfig=0;
3875 break;
3876 }
3877 }
3878 }
3879
3880 if (defconfig) {
cf5a65aa 3881 const char *fname;
292444cb 3882 FILE *fp;
cf5a65aa
MA
3883
3884 fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
3885 fp = fopen(fname, "r");
292444cb 3886 if (fp) {
cf5a65aa 3887 if (qemu_config_parse(fp, fname) != 0) {
292444cb
AL
3888 exit(1);
3889 }
3890 fclose(fp);
3891 }
3892
cf5a65aa
MA
3893 fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
3894 fp = fopen(fname, "r");
292444cb 3895 if (fp) {
cf5a65aa 3896 if (qemu_config_parse(fp, fname) != 0) {
292444cb
AL
3897 exit(1);
3898 }
3899 fclose(fp);
3900 }
3901 }
de06f8d1 3902 cpudef_init();
292444cb
AL
3903
3904 /* second pass of option parsing */
cd6f1169 3905 optind = 1;
0824d6fc 3906 for(;;) {
cd6f1169 3907 if (optind >= argc)
0824d6fc 3908 break;
6530a97b 3909 if (argv[optind][0] != '-') {
9dfd7c7a 3910 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
cd6f1169
FB
3911 } else {
3912 const QEMUOption *popt;
3913
6530a97b 3914 popt = lookup_opt(argc, argv, &optarg, &optind);
cd6f1169 3915 switch(popt->index) {
cc1daa40
FB
3916 case QEMU_OPTION_M:
3917 machine = find_machine(optarg);
3918 if (!machine) {
3919 QEMUMachine *m;
3920 printf("Supported machines are:\n");
3921 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
3922 if (m->alias)
3923 printf("%-10s %s (alias of %s)\n",
3924 m->alias, m->desc, m->name);
cc1daa40 3925 printf("%-10s %s%s\n",
5fafdf24 3926 m->name, m->desc,
0c257437 3927 m->is_default ? " (default)" : "");
cc1daa40 3928 }
15f82208 3929 exit(*optarg != '?');
cc1daa40
FB
3930 }
3931 break;
94fc95cd
JM
3932 case QEMU_OPTION_cpu:
3933 /* hw initialization will check this */
15f82208 3934 if (*optarg == '?') {
c732abe2 3935/* XXX: implement xxx_cpu_list for targets that still miss it */
b5ec5ce0 3936#if defined(cpu_list_id)
3937 cpu_list_id(stdout, &fprintf, optarg);
3938#elif defined(cpu_list)
3939 cpu_list(stdout, &fprintf); /* deprecated */
94fc95cd 3940#endif
15f82208 3941 exit(0);
94fc95cd
JM
3942 } else {
3943 cpu_model = optarg;
3944 }
3945 break;
cd6f1169 3946 case QEMU_OPTION_initrd:
fc01f7e7
FB
3947 initrd_filename = optarg;
3948 break;
cd6f1169 3949 case QEMU_OPTION_hda:
e4bcb14c 3950 if (cyls == 0)
9dfd7c7a 3951 hda_opts = drive_add(optarg, HD_ALIAS, 0);
e4bcb14c 3952 else
9dfd7c7a 3953 hda_opts = drive_add(optarg, HD_ALIAS
e4bcb14c 3954 ",cyls=%d,heads=%d,secs=%d%s",
609497ab 3955 0, cyls, heads, secs,
e4bcb14c
TS
3956 translation == BIOS_ATA_TRANSLATION_LBA ?
3957 ",trans=lba" :
3958 translation == BIOS_ATA_TRANSLATION_NONE ?
3959 ",trans=none" : "");
3960 break;
cd6f1169 3961 case QEMU_OPTION_hdb:
cc1daa40
FB
3962 case QEMU_OPTION_hdc:
3963 case QEMU_OPTION_hdd:
609497ab 3964 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
fc01f7e7 3965 break;
e4bcb14c 3966 case QEMU_OPTION_drive:
609497ab 3967 drive_add(NULL, "%s", optarg);
e4bcb14c 3968 break;
d058fe03
GH
3969 case QEMU_OPTION_set:
3970 if (qemu_set_option(optarg) != 0)
3971 exit(1);
3972 break;
d0fef6fb
GH
3973 case QEMU_OPTION_global:
3974 if (qemu_global_option(optarg) != 0)
3975 exit(1);
3976 break;
3e3d5815 3977 case QEMU_OPTION_mtdblock:
609497ab 3978 drive_add(optarg, MTD_ALIAS);
3e3d5815 3979 break;
a1bb27b1 3980 case QEMU_OPTION_sd:
609497ab 3981 drive_add(optarg, SD_ALIAS);
a1bb27b1 3982 break;
86f55663 3983 case QEMU_OPTION_pflash:
609497ab 3984 drive_add(optarg, PFLASH_ALIAS);
86f55663 3985 break;
cd6f1169 3986 case QEMU_OPTION_snapshot:
33e3963e
FB
3987 snapshot = 1;
3988 break;
cd6f1169 3989 case QEMU_OPTION_hdachs:
330d0414 3990 {
330d0414
FB
3991 const char *p;
3992 p = optarg;
3993 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
3994 if (cyls < 1 || cyls > 16383)
3995 goto chs_fail;
330d0414
FB
3996 if (*p != ',')
3997 goto chs_fail;
3998 p++;
3999 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
4000 if (heads < 1 || heads > 16)
4001 goto chs_fail;
330d0414
FB
4002 if (*p != ',')
4003 goto chs_fail;
4004 p++;
4005 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
4006 if (secs < 1 || secs > 63)
4007 goto chs_fail;
4008 if (*p == ',') {
4009 p++;
4010 if (!strcmp(p, "none"))
4011 translation = BIOS_ATA_TRANSLATION_NONE;
4012 else if (!strcmp(p, "lba"))
4013 translation = BIOS_ATA_TRANSLATION_LBA;
4014 else if (!strcmp(p, "auto"))
4015 translation = BIOS_ATA_TRANSLATION_AUTO;
4016 else
4017 goto chs_fail;
4018 } else if (*p != '\0') {
c4b1fcc0 4019 chs_fail:
46d4767d
FB
4020 fprintf(stderr, "qemu: invalid physical CHS format\n");
4021 exit(1);
c4b1fcc0 4022 }
9dfd7c7a
GH
4023 if (hda_opts != NULL) {
4024 char num[16];
4025 snprintf(num, sizeof(num), "%d", cyls);
4026 qemu_opt_set(hda_opts, "cyls", num);
4027 snprintf(num, sizeof(num), "%d", heads);
4028 qemu_opt_set(hda_opts, "heads", num);
4029 snprintf(num, sizeof(num), "%d", secs);
4030 qemu_opt_set(hda_opts, "secs", num);
4031 if (translation == BIOS_ATA_TRANSLATION_LBA)
4032 qemu_opt_set(hda_opts, "trans", "lba");
4033 if (translation == BIOS_ATA_TRANSLATION_NONE)
4034 qemu_opt_set(hda_opts, "trans", "none");
4035 }
330d0414
FB
4036 }
4037 break;
268a362c
AL
4038 case QEMU_OPTION_numa:
4039 if (nb_numa_nodes >= MAX_NODES) {
4040 fprintf(stderr, "qemu: too many NUMA nodes\n");
4041 exit(1);
4042 }
4043 numa_add(optarg);
4044 break;
cd6f1169 4045 case QEMU_OPTION_nographic:
993fbfdb 4046 display_type = DT_NOGRAPHIC;
a20dd508 4047 break;
4d3b6f6e
AZ
4048#ifdef CONFIG_CURSES
4049 case QEMU_OPTION_curses:
993fbfdb 4050 display_type = DT_CURSES;
4d3b6f6e
AZ
4051 break;
4052#endif
a171fe39
AZ
4053 case QEMU_OPTION_portrait:
4054 graphic_rotate = 1;
4055 break;
cd6f1169 4056 case QEMU_OPTION_kernel:
a20dd508
FB
4057 kernel_filename = optarg;
4058 break;
cd6f1169 4059 case QEMU_OPTION_append:
a20dd508 4060 kernel_cmdline = optarg;
313aa567 4061 break;
cd6f1169 4062 case QEMU_OPTION_cdrom:
609497ab 4063 drive_add(optarg, CDROM_ALIAS);
36b486bb 4064 break;
cd6f1169 4065 case QEMU_OPTION_boot:
28c5af54 4066 {
ef3adf68 4067 static const char * const params[] = {
95387491 4068 "order", "once", "menu", NULL
ef3adf68
JK
4069 };
4070 char buf[sizeof(boot_devices)];
e0f084bf 4071 char *standard_boot_devices;
ef3adf68
JK
4072 int legacy = 0;
4073
4074 if (!strchr(optarg, '=')) {
4075 legacy = 1;
4076 pstrcpy(buf, sizeof(buf), optarg);
4077 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
4078 fprintf(stderr,
4079 "qemu: unknown boot parameter '%s' in '%s'\n",
4080 buf, optarg);
4081 exit(1);
4082 }
4083
4084 if (legacy ||
4085 get_param_value(buf, sizeof(buf), "order", optarg)) {
4086 boot_devices_bitmap = parse_bootdevices(buf);
4087 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 4088 }
e0f084bf
JK
4089 if (!legacy) {
4090 if (get_param_value(buf, sizeof(buf),
4091 "once", optarg)) {
4092 boot_devices_bitmap |= parse_bootdevices(buf);
4093 standard_boot_devices = qemu_strdup(boot_devices);
4094 pstrcpy(boot_devices, sizeof(boot_devices), buf);
4095 qemu_register_reset(restore_boot_devices,
4096 standard_boot_devices);
4097 }
95387491
JK
4098 if (get_param_value(buf, sizeof(buf),
4099 "menu", optarg)) {
4100 if (!strcmp(buf, "on")) {
4101 boot_menu = 1;
4102 } else if (!strcmp(buf, "off")) {
4103 boot_menu = 0;
4104 } else {
4105 fprintf(stderr,
4106 "qemu: invalid option value '%s'\n",
4107 buf);
4108 exit(1);
4109 }
4110 }
e0f084bf 4111 }
36b486bb
FB
4112 }
4113 break;
cd6f1169 4114 case QEMU_OPTION_fda:
cd6f1169 4115 case QEMU_OPTION_fdb:
609497ab 4116 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
c45886db 4117 break;
52ca8d6a
FB
4118#ifdef TARGET_I386
4119 case QEMU_OPTION_no_fd_bootchk:
4120 fd_bootchk = 0;
4121 break;
4122#endif
a1ea458f
MM
4123 case QEMU_OPTION_netdev:
4124 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
4125 exit(1);
4126 }
4127 break;
7c9d8e07 4128 case QEMU_OPTION_net:
7f161aae 4129 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
c4b1fcc0
FB
4130 exit(1);
4131 }
702c651c 4132 break;
c7f74643
FB
4133#ifdef CONFIG_SLIRP
4134 case QEMU_OPTION_tftp:
ad196a9d 4135 legacy_tftp_prefix = optarg;
9bf05444 4136 break;
47d5d01a 4137 case QEMU_OPTION_bootp:
ad196a9d 4138 legacy_bootp_filename = optarg;
47d5d01a 4139 break;
c94c8d64 4140#ifndef _WIN32
9d728e8c 4141 case QEMU_OPTION_smb:
0752706d
MA
4142 if (net_slirp_smb(optarg) < 0)
4143 exit(1);
9d728e8c 4144 break;
c94c8d64 4145#endif
9bf05444 4146 case QEMU_OPTION_redir:
0752706d
MA
4147 if (net_slirp_redir(optarg) < 0)
4148 exit(1);
9bf05444 4149 break;
c7f74643 4150#endif
dc72ac14 4151 case QEMU_OPTION_bt:
bd3c948d 4152 add_device_config(DEV_BT, optarg);
dc72ac14 4153 break;
1d14ffa9 4154#ifdef HAS_AUDIO
1d14ffa9
FB
4155 case QEMU_OPTION_audio_help:
4156 AUD_help ();
4157 exit (0);
4158 break;
4159 case QEMU_OPTION_soundhw:
4160 select_soundhw (optarg);
4161 break;
4162#endif
cd6f1169 4163 case QEMU_OPTION_h:
15f82208 4164 help(0);
cd6f1169 4165 break;
9bd7e6d9
PB
4166 case QEMU_OPTION_version:
4167 version();
4168 exit(0);
4169 break;
00f82b8a
AJ
4170 case QEMU_OPTION_m: {
4171 uint64_t value;
4172 char *ptr;
4173
4174 value = strtoul(optarg, &ptr, 10);
4175 switch (*ptr) {
4176 case 0: case 'M': case 'm':
4177 value <<= 20;
4178 break;
4179 case 'G': case 'g':
4180 value <<= 30;
4181 break;
4182 default:
4183 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
4184 exit(1);
4185 }
00f82b8a
AJ
4186
4187 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 4188 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
4189 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4190 exit(1);
4191 }
c227f099 4192 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
4193 fprintf(stderr, "qemu: ram size too large\n");
4194 exit(1);
4195 }
4196 ram_size = value;
cd6f1169 4197 break;
00f82b8a 4198 }
c902760f
MT
4199 case QEMU_OPTION_mempath:
4200 mem_path = optarg;
4201 break;
4202#ifdef MAP_POPULATE
4203 case QEMU_OPTION_mem_prealloc:
4204 mem_prealloc = 1;
4205 break;
4206#endif
cd6f1169 4207 case QEMU_OPTION_d:
de06f8d1 4208 set_cpu_log(optarg);
cd6f1169 4209 break;
cd6f1169 4210 case QEMU_OPTION_s:
59030a8c 4211 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 4212 break;
59030a8c
AL
4213 case QEMU_OPTION_gdb:
4214 gdbstub_dev = optarg;
cd6f1169 4215 break;
cd6f1169 4216 case QEMU_OPTION_L:
5cea8590 4217 data_dir = optarg;
cd6f1169 4218 break;
1192dad8
JM
4219 case QEMU_OPTION_bios:
4220 bios_name = optarg;
4221 break;
1b530a6d
AJ
4222 case QEMU_OPTION_singlestep:
4223 singlestep = 1;
4224 break;
cd6f1169 4225 case QEMU_OPTION_S:
3c07f8e8 4226 autostart = 0;
cd6f1169 4227 break;
3d11d0eb
FB
4228 case QEMU_OPTION_k:
4229 keyboard_layout = optarg;
4230 break;
ee22c2f7
FB
4231 case QEMU_OPTION_localtime:
4232 rtc_utc = 0;
4233 break;
3893c124 4234 case QEMU_OPTION_vga:
4235 select_vgahw (optarg);
1bfe856e 4236 break;
5824d651 4237#if defined(TARGET_PPC) || defined(TARGET_SPARC)
e9b137c2
FB
4238 case QEMU_OPTION_g:
4239 {
4240 const char *p;
4241 int w, h, depth;
4242 p = optarg;
4243 w = strtol(p, (char **)&p, 10);
4244 if (w <= 0) {
4245 graphic_error:
4246 fprintf(stderr, "qemu: invalid resolution or depth\n");
4247 exit(1);
4248 }
4249 if (*p != 'x')
4250 goto graphic_error;
4251 p++;
4252 h = strtol(p, (char **)&p, 10);
4253 if (h <= 0)
4254 goto graphic_error;
4255 if (*p == 'x') {
4256 p++;
4257 depth = strtol(p, (char **)&p, 10);
5fafdf24 4258 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
4259 depth != 24 && depth != 32)
4260 goto graphic_error;
4261 } else if (*p == '\0') {
4262 depth = graphic_depth;
4263 } else {
4264 goto graphic_error;
4265 }
3b46e624 4266
e9b137c2
FB
4267 graphic_width = w;
4268 graphic_height = h;
4269 graphic_depth = depth;
4270 }
4271 break;
5824d651 4272#endif
20d8a3ed
TS
4273 case QEMU_OPTION_echr:
4274 {
4275 char *r;
4276 term_escape_char = strtol(optarg, &r, 0);
4277 if (r == optarg)
4278 printf("Bad argument to echr\n");
4279 break;
4280 }
82c643ff 4281 case QEMU_OPTION_monitor:
6ca5582d
GH
4282 monitor_parse(optarg, "readline");
4283 default_monitor = 0;
4284 break;
4285 case QEMU_OPTION_qmp:
4286 monitor_parse(optarg, "control");
2d114dc1 4287 default_monitor = 0;
82c643ff 4288 break;
22a0e04b 4289 case QEMU_OPTION_mon:
8212c64f 4290 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
22a0e04b
GH
4291 if (!opts) {
4292 fprintf(stderr, "parse error: %s\n", optarg);
4293 exit(1);
4294 }
2d114dc1 4295 default_monitor = 0;
22a0e04b 4296 break;
191bc01b 4297 case QEMU_OPTION_chardev:
8212c64f 4298 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
191bc01b
GH
4299 if (!opts) {
4300 fprintf(stderr, "parse error: %s\n", optarg);
4301 exit(1);
4302 }
191bc01b 4303 break;
82c643ff 4304 case QEMU_OPTION_serial:
998bbd74
GH
4305 add_device_config(DEV_SERIAL, optarg);
4306 default_serial = 0;
18141ed6
JK
4307 if (strncmp(optarg, "mon:", 4) == 0) {
4308 default_monitor = 0;
4309 }
82c643ff 4310 break;
9dd986cc 4311 case QEMU_OPTION_watchdog:
09aaa160
MA
4312 if (watchdog) {
4313 fprintf(stderr,
4314 "qemu: only one watchdog option may be given\n");
4315 return 1;
4316 }
4317 watchdog = optarg;
9dd986cc
RJ
4318 break;
4319 case QEMU_OPTION_watchdog_action:
4320 if (select_watchdog_action(optarg) == -1) {
4321 fprintf(stderr, "Unknown -watchdog-action parameter\n");
4322 exit(1);
4323 }
4324 break;
51ecf136 4325 case QEMU_OPTION_virtiocon:
aee1b935
GH
4326 add_device_config(DEV_VIRTCON, optarg);
4327 default_virtcon = 0;
18141ed6
JK
4328 if (strncmp(optarg, "mon:", 4) == 0) {
4329 default_monitor = 0;
4330 }
51ecf136 4331 break;
6508fe59 4332 case QEMU_OPTION_parallel:
6a5e8b0e
GH
4333 add_device_config(DEV_PARALLEL, optarg);
4334 default_parallel = 0;
18141ed6
JK
4335 if (strncmp(optarg, "mon:", 4) == 0) {
4336 default_monitor = 0;
4337 }
6508fe59 4338 break;
c9f398e5
PA
4339 case QEMU_OPTION_debugcon:
4340 add_device_config(DEV_DEBUGCON, optarg);
4341 break;
d63d307f
FB
4342 case QEMU_OPTION_loadvm:
4343 loadvm = optarg;
4344 break;
4345 case QEMU_OPTION_full_screen:
4346 full_screen = 1;
4347 break;
667accab 4348#ifdef CONFIG_SDL
43523e93
TS
4349 case QEMU_OPTION_no_frame:
4350 no_frame = 1;
4351 break;
3780e197
TS
4352 case QEMU_OPTION_alt_grab:
4353 alt_grab = 1;
4354 break;
0ca9f8a4
DK
4355 case QEMU_OPTION_ctrl_grab:
4356 ctrl_grab = 1;
4357 break;
667accab
TS
4358 case QEMU_OPTION_no_quit:
4359 no_quit = 1;
4360 break;
7d957bd8 4361 case QEMU_OPTION_sdl:
993fbfdb 4362 display_type = DT_SDL;
7d957bd8 4363 break;
667accab 4364#endif
f7cce898 4365 case QEMU_OPTION_pidfile:
93815bc2 4366 pid_file = optarg;
f7cce898 4367 break;
a09db21f
FB
4368#ifdef TARGET_I386
4369 case QEMU_OPTION_win2k_hack:
4370 win2k_install_hack = 1;
4371 break;
73822ec8
AL
4372 case QEMU_OPTION_rtc_td_hack:
4373 rtc_td_hack = 1;
4374 break;
8a92ea2f 4375 case QEMU_OPTION_acpitable:
de06f8d1 4376 do_acpitable_option(optarg);
8a92ea2f 4377 break;
b6f6e3d3 4378 case QEMU_OPTION_smbios:
de06f8d1 4379 do_smbios_option(optarg);
b6f6e3d3 4380 break;
a09db21f 4381#endif
7ba1e619
AL
4382#ifdef CONFIG_KVM
4383 case QEMU_OPTION_enable_kvm:
4384 kvm_allowed = 1;
7ba1e619 4385 break;
d993e026 4386#endif
bb36d470
FB
4387 case QEMU_OPTION_usb:
4388 usb_enabled = 1;
4389 break;
a594cfbf
FB
4390 case QEMU_OPTION_usbdevice:
4391 usb_enabled = 1;
bd3c948d
GH
4392 add_device_config(DEV_USB, optarg);
4393 break;
4394 case QEMU_OPTION_device:
8212c64f 4395 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
f31d07d1
GH
4396 exit(1);
4397 }
a594cfbf 4398 break;
6a00d601 4399 case QEMU_OPTION_smp:
dc6b1c09 4400 smp_parse(optarg);
b2097003 4401 if (smp_cpus < 1) {
6a00d601
FB
4402 fprintf(stderr, "Invalid number of CPUs\n");
4403 exit(1);
4404 }
6be68d7e
JS
4405 if (max_cpus < smp_cpus) {
4406 fprintf(stderr, "maxcpus must be equal to or greater than "
4407 "smp\n");
4408 exit(1);
4409 }
4410 if (max_cpus > 255) {
4411 fprintf(stderr, "Unsupported number of maxcpus\n");
4412 exit(1);
4413 }
6a00d601 4414 break;
24236869 4415 case QEMU_OPTION_vnc:
993fbfdb 4416 display_type = DT_VNC;
73fc9742 4417 vnc_display = optarg;
24236869 4418 break;
5824d651 4419#ifdef TARGET_I386
6515b203
FB
4420 case QEMU_OPTION_no_acpi:
4421 acpi_enabled = 0;
4422 break;
16b29ae1
AL
4423 case QEMU_OPTION_no_hpet:
4424 no_hpet = 1;
4425 break;
7d4c3d53
MA
4426 case QEMU_OPTION_balloon:
4427 if (balloon_parse(optarg) < 0) {
4428 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
4429 exit(1);
4430 }
df97b920 4431 break;
5824d651 4432#endif
d1beab82
FB
4433 case QEMU_OPTION_no_reboot:
4434 no_reboot = 1;
4435 break;
b2f76161
AJ
4436 case QEMU_OPTION_no_shutdown:
4437 no_shutdown = 1;
4438 break;
9467cd46
AZ
4439 case QEMU_OPTION_show_cursor:
4440 cursor_hide = 0;
4441 break;
8fcb1b90
BS
4442 case QEMU_OPTION_uuid:
4443 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4444 fprintf(stderr, "Fail to parse UUID string."
4445 " Wrong format.\n");
4446 exit(1);
4447 }
4448 break;
5824d651 4449#ifndef _WIN32
71e3ceb8
TS
4450 case QEMU_OPTION_daemonize:
4451 daemonize = 1;
4452 break;
5824d651 4453#endif
9ae02555
TS
4454 case QEMU_OPTION_option_rom:
4455 if (nb_option_roms >= MAX_OPTION_ROMS) {
4456 fprintf(stderr, "Too many option ROMs\n");
4457 exit(1);
4458 }
4459 option_rom[nb_option_roms] = optarg;
4460 nb_option_roms++;
4461 break;
5824d651 4462#if defined(TARGET_ARM) || defined(TARGET_M68K)
8e71621f
PB
4463 case QEMU_OPTION_semihosting:
4464 semihosting_enabled = 1;
4465 break;
5824d651 4466#endif
c35734b2 4467 case QEMU_OPTION_name:
1889465a
AK
4468 qemu_name = qemu_strdup(optarg);
4469 {
4470 char *p = strchr(qemu_name, ',');
4471 if (p != NULL) {
4472 *p++ = 0;
4473 if (strncmp(p, "process=", 8)) {
4474 fprintf(stderr, "Unknown subargument %s to -name", p);
4475 exit(1);
4476 }
4477 p += 8;
4478 set_proc_name(p);
4479 }
4480 }
c35734b2 4481 break;
95efd11c 4482#if defined(TARGET_SPARC) || defined(TARGET_PPC)
66508601
BS
4483 case QEMU_OPTION_prom_env:
4484 if (nb_prom_envs >= MAX_PROM_ENVS) {
4485 fprintf(stderr, "Too many prom variables\n");
4486 exit(1);
4487 }
4488 prom_envs[nb_prom_envs] = optarg;
4489 nb_prom_envs++;
4490 break;
2b8f2d41
AZ
4491#endif
4492#ifdef TARGET_ARM
4493 case QEMU_OPTION_old_param:
4494 old_param = 1;
05ebd537 4495 break;
66508601 4496#endif
f3dcfada
TS
4497 case QEMU_OPTION_clock:
4498 configure_alarms(optarg);
4499 break;
7e0af5d0 4500 case QEMU_OPTION_startdate:
1ed2fc1f
JK
4501 configure_rtc_date_offset(optarg, 1);
4502 break;
4503 case QEMU_OPTION_rtc:
8212c64f 4504 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
1ed2fc1f
JK
4505 if (!opts) {
4506 fprintf(stderr, "parse error: %s\n", optarg);
4507 exit(1);
7e0af5d0 4508 }
1ed2fc1f 4509 configure_rtc(opts);
7e0af5d0 4510 break;
26a5f13b
FB
4511 case QEMU_OPTION_tb_size:
4512 tb_size = strtol(optarg, NULL, 0);
4513 if (tb_size < 0)
4514 tb_size = 0;
4515 break;
2e70f6ef 4516 case QEMU_OPTION_icount:
4e3de9e9 4517 icount_option = optarg;
2e70f6ef 4518 break;
5bb7910a
AL
4519 case QEMU_OPTION_incoming:
4520 incoming = optarg;
4521 break;
d8c208dd
GH
4522 case QEMU_OPTION_nodefaults:
4523 default_serial = 0;
4524 default_parallel = 0;
aee1b935 4525 default_virtcon = 0;
d8c208dd
GH
4526 default_monitor = 0;
4527 default_vga = 0;
cb4522cc 4528 default_net = 0;
ac33f8fa
GH
4529 default_floppy = 0;
4530 default_cdrom = 0;
4531 default_sdcard = 0;
d8c208dd 4532 break;
5824d651 4533#ifndef _WIN32
0858532e
AL
4534 case QEMU_OPTION_chroot:
4535 chroot_dir = optarg;
4536 break;
4537 case QEMU_OPTION_runas:
4538 run_as = optarg;
4539 break;
e37630ca
AL
4540#endif
4541#ifdef CONFIG_XEN
4542 case QEMU_OPTION_xen_domid:
4543 xen_domid = atoi(optarg);
4544 break;
4545 case QEMU_OPTION_xen_create:
4546 xen_mode = XEN_CREATE;
4547 break;
4548 case QEMU_OPTION_xen_attach:
4549 xen_mode = XEN_ATTACH;
4550 break;
5824d651 4551#endif
715a664a
GH
4552 case QEMU_OPTION_readconfig:
4553 {
4554 FILE *fp;
4555 fp = fopen(optarg, "r");
4556 if (fp == NULL) {
4557 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
4558 exit(1);
4559 }
cf5a65aa 4560 if (qemu_config_parse(fp, optarg) != 0) {
715a664a
GH
4561 exit(1);
4562 }
4563 fclose(fp);
4564 break;
4565 }
4566 case QEMU_OPTION_writeconfig:
4567 {
4568 FILE *fp;
4569 if (strcmp(optarg, "-") == 0) {
4570 fp = stdout;
4571 } else {
4572 fp = fopen(optarg, "w");
4573 if (fp == NULL) {
4574 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
4575 exit(1);
4576 }
4577 }
4578 qemu_config_write(fp);
4579 fclose(fp);
4580 break;
4581 }
cd6f1169 4582 }
0824d6fc
FB
4583 }
4584 }
0f0bc3f1 4585 loc_set_none();
330d0414 4586
5cea8590
PB
4587 /* If no data_dir is specified then try to find it relative to the
4588 executable path. */
4589 if (!data_dir) {
4590 data_dir = find_datadir(argv[0]);
4591 }
4592 /* If all else fails use the install patch specified when building. */
4593 if (!data_dir) {
4594 data_dir = CONFIG_QEMU_SHAREDIR;
4595 }
4596
6be68d7e
JS
4597 /*
4598 * Default to max_cpus = smp_cpus, in case the user doesn't
4599 * specify a max_cpus value.
4600 */
4601 if (!max_cpus)
4602 max_cpus = smp_cpus;
4603
3d878caa 4604 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
4605 if (smp_cpus > machine->max_cpus) {
4606 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4607 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
4608 machine->max_cpus);
4609 exit(1);
4610 }
4611
998bbd74 4612 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
d8bcbabf 4613 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
998bbd74 4614
986c5f78
GH
4615 if (machine->no_serial) {
4616 default_serial = 0;
4617 }
4618 if (machine->no_parallel) {
4619 default_parallel = 0;
4620 }
4621 if (!machine->use_virtcon) {
4622 default_virtcon = 0;
4623 }
4624 if (machine->no_vga) {
4625 default_vga = 0;
4626 }
ac33f8fa
GH
4627 if (machine->no_floppy) {
4628 default_floppy = 0;
4629 }
4630 if (machine->no_cdrom) {
4631 default_cdrom = 0;
4632 }
4633 if (machine->no_sdcard) {
4634 default_sdcard = 0;
4635 }
986c5f78 4636
993fbfdb 4637 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
4638 if (default_parallel)
4639 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
4640 if (default_serial && default_monitor) {
4641 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
4642 } else if (default_virtcon && default_monitor) {
4643 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
4644 } else {
4645 if (default_serial)
4646 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
4647 if (default_virtcon)
4648 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 4649 if (default_monitor)
6ca5582d 4650 monitor_parse("stdio", "readline");
e1c09175 4651 }
998bbd74
GH
4652 } else {
4653 if (default_serial)
4654 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
4655 if (default_parallel)
4656 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 4657 if (default_monitor)
6ca5582d 4658 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
4659 if (default_virtcon)
4660 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 4661 }
64465297
GH
4662 if (default_vga)
4663 vga_interface_type = VGA_CIRRUS;
bc0129d9 4664
1a688d3b
GH
4665 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
4666 exit(1);
4667
71e3ceb8 4668#ifndef _WIN32
71e3ceb8
TS
4669 if (daemonize) {
4670 pid_t pid;
4671
4672 if (pipe(fds) == -1)
4673 exit(1);
4674
4675 pid = fork();
4676 if (pid > 0) {
4677 uint8_t status;
4678 ssize_t len;
4679
4680 close(fds[1]);
4681
4682 again:
93815bc2
TS
4683 len = read(fds[0], &status, 1);
4684 if (len == -1 && (errno == EINTR))
4685 goto again;
4686
4687 if (len != 1)
4688 exit(1);
4689 else if (status == 1) {
850810d0 4690 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
93815bc2
TS
4691 exit(1);
4692 } else
4693 exit(0);
71e3ceb8 4694 } else if (pid < 0)
93815bc2 4695 exit(1);
71e3ceb8 4696
40ff6d7e
KW
4697 close(fds[0]);
4698 qemu_set_cloexec(fds[1]);
4699
71e3ceb8
TS
4700 setsid();
4701
4702 pid = fork();
4703 if (pid > 0)
4704 exit(0);
4705 else if (pid < 0)
4706 exit(1);
4707
4708 umask(027);
71e3ceb8
TS
4709
4710 signal(SIGTSTP, SIG_IGN);
4711 signal(SIGTTOU, SIG_IGN);
4712 signal(SIGTTIN, SIG_IGN);
4713 }
099fe236 4714#endif
71e3ceb8 4715
aa26bb2d 4716 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
099fe236 4717#ifndef _WIN32
93815bc2
TS
4718 if (daemonize) {
4719 uint8_t status = 1;
dc330e28
KS
4720 if (write(fds[1], &status, 1) != 1) {
4721 perror("daemonize. Writing to pipe\n");
4722 }
93815bc2 4723 } else
099fe236 4724#endif
850810d0 4725 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
93815bc2
TS
4726 exit(1);
4727 }
4728
214910a7
MT
4729 if (kvm_enabled()) {
4730 int ret;
4731
4732 ret = kvm_init(smp_cpus);
4733 if (ret < 0) {
4734 fprintf(stderr, "failed to initialize KVM\n");
4735 exit(1);
4736 }
4737 }
4738
3fcf7b6b
AL
4739 if (qemu_init_main_loop()) {
4740 fprintf(stderr, "qemu_init_main_loop failed\n");
4741 exit(1);
4742 }
a20dd508 4743 linux_boot = (kernel_filename != NULL);
6c41b272 4744
f8d39c01
TS
4745 if (!linux_boot && *kernel_cmdline != '\0') {
4746 fprintf(stderr, "-append only allowed with -kernel option\n");
4747 exit(1);
4748 }
4749
4750 if (!linux_boot && initrd_filename != NULL) {
4751 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4752 exit(1);
4753 }
4754
bf65f53f
FN
4755#ifndef _WIN32
4756 /* Win32 doesn't support line-buffering and requires size >= 2 */
b118d61e 4757 setvbuf(stdout, NULL, _IOLBF, 0);
bf65f53f 4758#endif
3b46e624 4759
7183b4b4
AL
4760 if (init_timer_alarm() < 0) {
4761 fprintf(stderr, "could not initialize alarm timer\n");
4762 exit(1);
4763 }
4e3de9e9 4764 configure_icount(icount_option);
634fce96 4765
fd1dff4b
FB
4766#ifdef _WIN32
4767 socket_init();
4768#endif
4769
dc1c9fe8
MM
4770 if (net_init_clients() < 0) {
4771 exit(1);
702c651c 4772 }
f1510b2c 4773
406c8df3
GC
4774 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
4775 net_set_boot_mask(net_boot);
4776
dc72ac14 4777 /* init the bluetooth world */
bd3c948d
GH
4778 if (foreach_device_config(DEV_BT, bt_parse))
4779 exit(1);
dc72ac14 4780
0824d6fc 4781 /* init the memory */
94a6b54f
PB
4782 if (ram_size == 0)
4783 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 4784
26a5f13b
FB
4785 /* init the dynamic translator */
4786 cpu_exec_init_all(tb_size * 1024 * 1024);
4787
eb852011 4788 bdrv_init_with_whitelist();
c4b1fcc0 4789
c163b5ca
LS
4790 blk_mig_init();
4791
ac33f8fa 4792 if (default_cdrom) {
aa40fc9c
GH
4793 /* we always create the cdrom drive, even if no disk is there */
4794 drive_add(NULL, CDROM_ALIAS);
ac33f8fa 4795 }
c4b1fcc0 4796
ac33f8fa 4797 if (default_floppy) {
aa40fc9c
GH
4798 /* we always create at least one floppy */
4799 drive_add(NULL, FD_ALIAS, 0);
ac33f8fa 4800 }
86f55663 4801
ac33f8fa 4802 if (default_sdcard) {
aa40fc9c
GH
4803 /* we always create one sd slot, even if no card is in it */
4804 drive_add(NULL, SD_ALIAS);
4805 }
9d413d1d 4806
e4bcb14c 4807 /* open the virtual block devices */
9dfd7c7a 4808 if (snapshot)
7282a033
GH
4809 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
4810 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
9dfd7c7a 4811 exit(1);
3e3d5815 4812
c163b5ca
LS
4813 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
4814 ram_load, NULL);
8a7ddc38 4815
268a362c
AL
4816 if (nb_numa_nodes > 0) {
4817 int i;
4818
4819 if (nb_numa_nodes > smp_cpus) {
4820 nb_numa_nodes = smp_cpus;
4821 }
4822
4823 /* If no memory size if given for any node, assume the default case
4824 * and distribute the available memory equally across all nodes
4825 */
4826 for (i = 0; i < nb_numa_nodes; i++) {
4827 if (node_mem[i] != 0)
4828 break;
4829 }
4830 if (i == nb_numa_nodes) {
4831 uint64_t usedmem = 0;
4832
4833 /* On Linux, the each node's border has to be 8MB aligned,
4834 * the final node gets the rest.
4835 */
4836 for (i = 0; i < nb_numa_nodes - 1; i++) {
4837 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4838 usedmem += node_mem[i];
4839 }
4840 node_mem[i] = ram_size - usedmem;
4841 }
4842
4843 for (i = 0; i < nb_numa_nodes; i++) {
4844 if (node_cpumask[i] != 0)
4845 break;
4846 }
4847 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4848 * must cope with this anyway, because there are BIOSes out there in
4849 * real machines which also use this scheme.
4850 */
4851 if (i == nb_numa_nodes) {
4852 for (i = 0; i < smp_cpus; i++) {
4853 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4854 }
4855 }
4856 }
4857
998bbd74
GH
4858 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4859 exit(1);
6a5e8b0e
GH
4860 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4861 exit(1);
aee1b935
GH
4862 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4863 exit(1);
c9f398e5
PA
4864 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4865 exit(1);
2796dae0 4866
aae9460e
PB
4867 module_call_init(MODULE_INIT_DEVICE);
4868
ff952ba2
MA
4869 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
4870 exit(0);
4871
09aaa160
MA
4872 if (watchdog) {
4873 i = select_watchdog(watchdog);
4874 if (i > 0)
4875 exit (i == 1 ? 1 : 0);
4876 }
4877
b6b61144 4878 if (machine->compat_props) {
458fb679 4879 qdev_prop_register_global_list(machine->compat_props);
b6b61144 4880 }
d0fef6fb
GH
4881 qemu_add_globals();
4882
fbe1b595 4883 machine->init(ram_size, boot_devices,
3023f332
AL
4884 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
4885
ea375f9a 4886 cpu_synchronize_all_post_init();
268a362c 4887
67b3b71d
JQ
4888#ifndef _WIN32
4889 /* must be after terminal init, SDL library changes signal handlers */
4890 sighandler_setup();
4891#endif
4892
87d0a28e 4893 set_numa_modes();
268a362c 4894
6f338c34
AL
4895 current_machine = machine;
4896
3023f332
AL
4897 /* init USB devices */
4898 if (usb_enabled) {
0752706d
MA
4899 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4900 exit(1);
3023f332
AL
4901 }
4902
bd3c948d 4903 /* init generic devices */
f31d07d1 4904 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
bd3c948d
GH
4905 exit(1);
4906
668680f7
MA
4907 net_check_clients();
4908
3023f332 4909 /* just use the first displaystate for the moment */
b473df6e 4910 ds = get_displaystate();
993fbfdb
AL
4911
4912 if (display_type == DT_DEFAULT) {
4913#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4914 display_type = DT_SDL;
4915#else
4916 display_type = DT_VNC;
4917 vnc_display = "localhost:0,to=99";
4918 show_vnc_port = 1;
4919#endif
4920 }
4921
4922
4923 switch (display_type) {
4924 case DT_NOGRAPHIC:
4925 break;
4d3b6f6e 4926#if defined(CONFIG_CURSES)
993fbfdb
AL
4927 case DT_CURSES:
4928 curses_display_init(ds, full_screen);
4929 break;
4d3b6f6e 4930#endif
5b0753e0 4931#if defined(CONFIG_SDL)
993fbfdb
AL
4932 case DT_SDL:
4933 sdl_display_init(ds, full_screen, no_frame);
4934 break;
5b0753e0 4935#elif defined(CONFIG_COCOA)
993fbfdb
AL
4936 case DT_SDL:
4937 cocoa_display_init(ds, full_screen);
4938 break;
313aa567 4939#endif
993fbfdb
AL
4940 case DT_VNC:
4941 vnc_display_init(ds);
4942 if (vnc_display_open(ds, vnc_display) < 0)
4943 exit(1);
f92f8afe 4944
993fbfdb
AL
4945 if (show_vnc_port) {
4946 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 4947 }
993fbfdb
AL
4948 break;
4949 default:
4950 break;
313aa567 4951 }
7d957bd8 4952 dpy_resize(ds);
5b08fc10 4953
3023f332
AL
4954 dcl = ds->listeners;
4955 while (dcl != NULL) {
4956 if (dcl->dpy_refresh != NULL) {
4957 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
4958 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
20d8a3ed 4959 }
3023f332 4960 dcl = dcl->next;
20d8a3ed 4961 }
3023f332 4962
993fbfdb 4963 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
9043b62d
BS
4964 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
4965 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
4966 }
4967
b473df6e 4968 text_consoles_set_display(ds);
2796dae0 4969
88589343
GH
4970 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
4971 exit(1);
82c643ff 4972
59030a8c
AL
4973 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
4974 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
4975 gdbstub_dev);
4976 exit(1);
45669e00 4977 }
45669e00 4978
3418bd25
GH
4979 qdev_machine_creation_done();
4980
15ff7705
GH
4981 if (rom_load_all() != 0) {
4982 fprintf(stderr, "rom loading failed\n");
4983 exit(1);
4984 }
45a50b16 4985
504c2948 4986 qemu_system_reset();
05f2401e 4987 if (loadvm) {
03cd4655 4988 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
4989 autostart = 0;
4990 }
4991 }
d63d307f 4992
2bb8c10c 4993 if (incoming) {
5bb7910a 4994 qemu_start_incoming_migration(incoming);
6b99dadc 4995 } else if (autostart) {
c0f4ce77 4996 vm_start();
6b99dadc 4997 }
ffd843bc 4998
b9e82a59 4999#ifndef _WIN32
71e3ceb8
TS
5000 if (daemonize) {
5001 uint8_t status = 0;
5002 ssize_t len;
71e3ceb8
TS
5003
5004 again1:
5005 len = write(fds[1], &status, 1);
5006 if (len == -1 && (errno == EINTR))
5007 goto again1;
5008
5009 if (len != 1)
5010 exit(1);
5011
dc330e28
KS
5012 if (chdir("/")) {
5013 perror("not able to chdir to /");
5014 exit(1);
5015 }
40ff6d7e 5016 TFR(fd = qemu_open("/dev/null", O_RDWR));
71e3ceb8
TS
5017 if (fd == -1)
5018 exit(1);
0858532e 5019 }
71e3ceb8 5020
0858532e
AL
5021 if (run_as) {
5022 pwd = getpwnam(run_as);
5023 if (!pwd) {
5024 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5025 exit(1);
5026 }
5027 }
5028
5029 if (chroot_dir) {
5030 if (chroot(chroot_dir) < 0) {
5031 fprintf(stderr, "chroot failed\n");
5032 exit(1);
5033 }
dc330e28
KS
5034 if (chdir("/")) {
5035 perror("not able to chdir to /");
5036 exit(1);
5037 }
0858532e
AL
5038 }
5039
5040 if (run_as) {
5041 if (setgid(pwd->pw_gid) < 0) {
5042 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5043 exit(1);
5044 }
5045 if (setuid(pwd->pw_uid) < 0) {
5046 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5047 exit(1);
5048 }
5049 if (setuid(0) != -1) {
5050 fprintf(stderr, "Dropping privileges failed\n");
5051 exit(1);
5052 }
5053 }
0858532e
AL
5054
5055 if (daemonize) {
5056 dup2(fd, 0);
5057 dup2(fd, 1);
5058 dup2(fd, 2);
71e3ceb8 5059
0858532e 5060 close(fd);
71e3ceb8 5061 }
b9e82a59 5062#endif
71e3ceb8 5063
8a7ddc38 5064 main_loop();
40c3bac3 5065 quit_timers();
63a01ef8 5066 net_cleanup();
b46a8906 5067
0824d6fc
FB
5068 return 0;
5069}