blob: ca568b2bdee47f6b9d0139e2ecc5ca37ae160005 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * 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.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
Andi Kleen18894652009-07-02 09:34:17 +020062#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000063
bellarde57a8c02005-11-10 23:58:52 +000064#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000065#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000066#endif
67#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000068#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000071#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>
Blue Swirl8d32cf02009-10-02 19:32:12 +000081/* 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);
bellard67b915a2004-03-31 23:37:16 +000084#endif
bellard7d3505c2004-05-12 19:32:15 +000085#endif
bellardec530c82006-04-25 22:36:06 +000086#endif
bellard67b915a2004-03-31 23:37:16 +000087
blueswir19892fbf2008-08-24 10:34:20 +000088#if defined(__OpenBSD__)
89#include <util.h>
90#endif
91
ths8a16d272008-07-19 09:56:24 +000092#if defined(CONFIG_VDE)
93#include <libvdeplug.h>
94#endif
95
bellard67b915a2004-03-31 23:37:16 +000096#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000097#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000098#endif
99
bellard73332e52004-04-04 20:22:28 +0000100#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200101#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200102#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000103int qemu_main(int argc, char **argv, char **envp);
104int main(int argc, char **argv)
105{
Stefan Weil59a36a22009-06-18 20:11:03 +0200106 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000107}
108#undef main
109#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000110#endif
bellard73332e52004-04-04 20:22:28 +0000111#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000112
bellard5b0753e2005-03-01 21:37:28 +0000113#ifdef CONFIG_COCOA
114#undef main
115#define main qemu_main
116#endif /* CONFIG_COCOA */
117
blueswir1511d2b12009-03-07 15:32:56 +0000118#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"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100127#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000128#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000129#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200130#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200131#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000132#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000134#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000135#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"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200143#include "block_int.h"
144#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000145#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000146#include "audio/audio.h"
147#include "migration.h"
148#include "kvm.h"
149#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200150#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200151#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200152#include "qemu-objects.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
bellard8a7ddc32004-03-31 19:00:16 +0000156#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000157
blueswir1511d2b12009-03-07 15:32:56 +0000158#include "qemu_socket.h"
159
Jan Kiszkad918f232009-06-24 14:42:30 +0200160#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000161
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
163
blueswir19dc63a12008-10-04 07:25:46 +0000164//#define DEBUG_NET
165//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000166
bellard1bfe8562004-07-08 21:17:50 +0000167#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000168
Amit Shah98b19252010-01-20 00:36:52 +0530169#define MAX_VIRTIO_CONSOLES 1
170
Paul Brook5cea8592009-05-30 00:52:44 +0100171static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000172const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000173/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000174 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000175struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
176struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000179const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500180ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300181const char *mem_path = NULL;
182#ifdef MAP_POPULATE
183int mem_prealloc = 0; /* force preallocation of physical target memory */
184#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000185int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000186NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000187int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200188int autostart;
balrogf6503052008-02-17 11:42:19 +0000189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200191QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100192int vga_interface_type = VGA_NONE;
bellardd8272202005-04-06 20:32:23 +0000193#ifdef TARGET_SPARC
194int graphic_width = 1024;
195int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000196int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000197#else
bellard1bfe8562004-07-08 21:17:50 +0000198int graphic_width = 800;
199int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000200int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000201#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000202static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000203#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000204static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000205#endif
ths667acca2006-12-11 02:08:05 +0000206int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000207CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000208CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000209CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000210int win2k_install_hack = 0;
Blue Swirlf7736b92010-03-27 06:20:53 +0000211#ifdef TARGET_I386
aliguori73822ec2009-01-15 20:11:34 +0000212int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000213#endif
bellardbb36d472005-11-05 14:22:28 +0000214int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000215int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000216int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200217int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200218int smp_cores = 1;
219int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000220const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000221int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000222int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000223int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000224int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000225int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000226int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000227int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200228uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000229#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000230int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000231#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200232const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000233const char *option_rom[MAX_OPTION_ROMS];
234int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000235int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000236#ifdef TARGET_ARM
237int old_param = 0;
238#endif
thsc35734b2007-03-19 15:17:08 +0000239const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000240int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500241int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000242#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000243unsigned int nb_prom_envs = 0;
244const char *prom_envs[MAX_PROM_ENVS];
245#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200246int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000247
aliguori268a3622009-04-21 22:30:27 +0000248int nb_numa_nodes;
249uint64_t node_mem[MAX_NODES];
250uint64_t node_cpumask[MAX_NODES];
251
balrogee5605e2007-12-03 03:01:40 +0000252static CPUState *cur_cpu;
253static CPUState *next_cpu;
blueswir19043b622009-01-21 19:28:13 +0000254static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000255
blueswir18fcb1b92008-09-18 18:29:08 +0000256uint8_t qemu_uuid[16];
257
Jan Kiszka76e30d02009-07-02 00:19:02 +0200258static QEMUBootSetHandler *boot_set_handler;
259static void *boot_set_opaque;
260
Marcelo Tosatticc84de92010-02-17 20:14:42 -0200261#ifdef SIGRTMIN
262#define SIG_IPI (SIGRTMIN+4)
263#else
264#define SIG_IPI SIGUSR1
265#endif
266
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100267static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100268static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100269static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100270static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100271static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100272static int default_floppy = 1;
273static int default_cdrom = 1;
274static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100275
276static struct {
277 const char *driver;
278 int *flag;
279} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100280 { .driver = "isa-serial", .flag = &default_serial },
281 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100282 { .driver = "isa-fdc", .flag = &default_floppy },
283 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530284 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
285 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
286 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100287 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100288 { .driver = "cirrus-vga", .flag = &default_vga },
289 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100290};
291
292static int default_driver_check(QemuOpts *opts, void *opaque)
293{
294 const char *driver = qemu_opt_get(opts, "driver");
295 int i;
296
297 if (!driver)
298 return 0;
299 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
300 if (strcmp(default_list[i].driver, driver) != 0)
301 continue;
302 *(default_list[i].flag) = 0;
303 }
304 return 0;
305}
306
bellard0824d6f2003-06-24 13:42:40 +0000307/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000308/* x86 ISA bus support */
309
Anthony Liguoric227f092009-10-01 16:12:16 -0500310target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000311PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000312
bellard0824d6f2003-06-24 13:42:40 +0000313/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000314void hw_error(const char *fmt, ...)
315{
316 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000317 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000318
319 va_start(ap, fmt);
320 fprintf(stderr, "qemu: hardware error: ");
321 vfprintf(stderr, fmt, ap);
322 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000323 for(env = first_cpu; env != NULL; env = env->next_cpu) {
324 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000325#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000326 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000327#else
bellard6a00d602005-11-21 23:25:50 +0000328 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000329#endif
bellard6a00d602005-11-21 23:25:50 +0000330 }
bellard0824d6f2003-06-24 13:42:40 +0000331 va_end(ap);
332 abort();
333}
Andi Kleen18894652009-07-02 09:34:17 +0200334
335static void set_proc_name(const char *s)
336{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700337#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200338 char name[16];
339 if (!s)
340 return;
341 name[sizeof(name) - 1] = 0;
342 strncpy(name, s, sizeof(name));
343 /* Could rewrite argv[0] too, but that's a bit more complicated.
344 This simple way is enough for `top'. */
345 prctl(PR_SET_NAME, name);
346#endif
347}
aliguoridf751fa2008-12-04 20:19:35 +0000348
349/***************/
350/* ballooning */
351
352static QEMUBalloonEvent *qemu_balloon_event;
353void *qemu_balloon_event_opaque;
354
355void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
356{
357 qemu_balloon_event = func;
358 qemu_balloon_event_opaque = opaque;
359}
360
Adam Litke625a5be2010-01-26 14:17:35 -0600361int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000362{
Adam Litke625a5be2010-01-26 14:17:35 -0600363 if (qemu_balloon_event) {
364 qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
365 return 1;
366 } else {
367 return 0;
368 }
aliguoridf751fa2008-12-04 20:19:35 +0000369}
370
Adam Litke625a5be2010-01-26 14:17:35 -0600371int qemu_balloon_status(MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000372{
Adam Litke625a5be2010-01-26 14:17:35 -0600373 if (qemu_balloon_event) {
374 qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
375 return 1;
376 } else {
377 return 0;
378 }
aliguoridf751fa2008-12-04 20:19:35 +0000379}
bellard0824d6f2003-06-24 13:42:40 +0000380
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100381
bellard8a7ddc32004-03-31 19:00:16 +0000382/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100383/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000384
bellard87858c82003-06-27 12:01:39 +0000385/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000386uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000387{
388 union {
389 uint64_t ll;
390 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200391#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000392 uint32_t high, low;
393#else
394 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000395#endif
bellard87858c82003-06-27 12:01:39 +0000396 } l;
397 } u, res;
398 uint64_t rl, rh;
399
400 u.ll = a;
401 rl = (uint64_t)u.l.low * (uint64_t)b;
402 rh = (uint64_t)u.l.high * (uint64_t)b;
403 rh += (rl >> 32);
404 res.l.high = rh / c;
405 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
406 return res.ll;
407}
408
bellardc4b1fcc2004-03-14 21:44:30 +0000409/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000410/* host time/date access */
411void qemu_get_timedate(struct tm *tm, int offset)
412{
413 time_t ti;
414 struct tm *ret;
415
416 time(&ti);
417 ti += offset;
418 if (rtc_date_offset == -1) {
419 if (rtc_utc)
420 ret = gmtime(&ti);
421 else
422 ret = localtime(&ti);
423 } else {
424 ti -= rtc_date_offset;
425 ret = gmtime(&ti);
426 }
427
428 memcpy(tm, ret, sizeof(struct tm));
429}
430
431int qemu_timedate_diff(struct tm *tm)
432{
433 time_t seconds;
434
435 if (rtc_date_offset == -1)
436 if (rtc_utc)
437 seconds = mktimegm(tm);
438 else
439 seconds = mktime(tm);
440 else
441 seconds = mktimegm(tm) + rtc_date_offset;
442
443 return seconds - time(NULL);
444}
445
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300446void rtc_change_mon_event(struct tm *tm)
447{
448 QObject *data;
449
450 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
451 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
452 qobject_decref(data);
453}
454
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200455static void configure_rtc_date_offset(const char *startdate, int legacy)
456{
457 time_t rtc_start_date;
458 struct tm tm;
459
460 if (!strcmp(startdate, "now") && legacy) {
461 rtc_date_offset = -1;
462 } else {
463 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
464 &tm.tm_year,
465 &tm.tm_mon,
466 &tm.tm_mday,
467 &tm.tm_hour,
468 &tm.tm_min,
469 &tm.tm_sec) == 6) {
470 /* OK */
471 } else if (sscanf(startdate, "%d-%d-%d",
472 &tm.tm_year,
473 &tm.tm_mon,
474 &tm.tm_mday) == 3) {
475 tm.tm_hour = 0;
476 tm.tm_min = 0;
477 tm.tm_sec = 0;
478 } else {
479 goto date_fail;
480 }
481 tm.tm_year -= 1900;
482 tm.tm_mon--;
483 rtc_start_date = mktimegm(&tm);
484 if (rtc_start_date == -1) {
485 date_fail:
486 fprintf(stderr, "Invalid date format. Valid formats are:\n"
487 "'2006-06-17T16:01:21' or '2006-06-17'\n");
488 exit(1);
489 }
490 rtc_date_offset = time(NULL) - rtc_start_date;
491 }
492}
493
494static void configure_rtc(QemuOpts *opts)
495{
496 const char *value;
497
498 value = qemu_opt_get(opts, "base");
499 if (value) {
500 if (!strcmp(value, "utc")) {
501 rtc_utc = 1;
502 } else if (!strcmp(value, "localtime")) {
503 rtc_utc = 0;
504 } else {
505 configure_rtc_date_offset(value, 0);
506 }
507 }
Jan Kiszka68752042009-09-15 13:36:04 +0200508 value = qemu_opt_get(opts, "clock");
509 if (value) {
510 if (!strcmp(value, "host")) {
511 rtc_clock = host_clock;
512 } else if (!strcmp(value, "vm")) {
513 rtc_clock = vm_clock;
514 } else {
515 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
516 exit(1);
517 }
518 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200519#ifdef CONFIG_TARGET_I386
520 value = qemu_opt_get(opts, "driftfix");
521 if (value) {
522 if (!strcmp(buf, "slew")) {
523 rtc_td_hack = 1;
524 } else if (!strcmp(buf, "none")) {
525 rtc_td_hack = 0;
526 } else {
527 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
528 exit(1);
529 }
530 }
531#endif
532}
533
bellardfd1dff42006-02-01 21:29:26 +0000534#ifdef _WIN32
Anthony Liguori28695482010-03-21 14:13:02 -0500535static void socket_cleanup(void)
bellardfd1dff42006-02-01 21:29:26 +0000536{
537 WSACleanup();
538}
bellard82c643f2004-07-14 17:28:13 +0000539
bellardfd1dff42006-02-01 21:29:26 +0000540static int socket_init(void)
541{
542 WSADATA Data;
543 int ret, err;
544
545 ret = WSAStartup(MAKEWORD(2,2), &Data);
546 if (ret != 0) {
547 err = WSAGetLastError();
548 fprintf(stderr, "WSAStartup: %d\n", err);
549 return -1;
550 }
Anthony Liguori28695482010-03-21 14:13:02 -0500551 atexit(socket_cleanup);
bellardfd1dff42006-02-01 21:29:26 +0000552 return 0;
553}
aurel3264b7b732008-05-05 10:05:31 +0000554#endif
555
balrog1ae26a12008-09-28 23:19:47 +0000556/***********************************************************/
557/* Bluetooth support */
558static int nb_hcis;
559static int cur_hci;
560static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000561
balrog1ae26a12008-09-28 23:19:47 +0000562static struct bt_vlan_s {
563 struct bt_scatternet_s net;
564 int id;
565 struct bt_vlan_s *next;
566} *first_bt_vlan;
567
568/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000569static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000570{
571 struct bt_vlan_s **pvlan, *vlan;
572 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
573 if (vlan->id == id)
574 return &vlan->net;
575 }
576 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
577 vlan->id = id;
578 pvlan = &first_bt_vlan;
579 while (*pvlan != NULL)
580 pvlan = &(*pvlan)->next;
581 *pvlan = vlan;
582 return &vlan->net;
583}
584
585static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
586{
587}
588
589static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
590{
591 return -ENOTSUP;
592}
593
594static struct HCIInfo null_hci = {
595 .cmd_send = null_hci_send,
596 .sco_send = null_hci_send,
597 .acl_send = null_hci_send,
598 .bdaddr_set = null_hci_addr_set,
599};
600
601struct HCIInfo *qemu_next_hci(void)
602{
603 if (cur_hci == nb_hcis)
604 return &null_hci;
605
606 return hci_table[cur_hci++];
607}
608
balrogdc72ac12008-11-09 00:04:26 +0000609static struct HCIInfo *hci_init(const char *str)
610{
611 char *endp;
612 struct bt_scatternet_s *vlan = 0;
613
614 if (!strcmp(str, "null"))
615 /* null */
616 return &null_hci;
617 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
618 /* host[:hciN] */
619 return bt_host_hci(str[4] ? str + 5 : "hci0");
620 else if (!strncmp(str, "hci", 3)) {
621 /* hci[,vlan=n] */
622 if (str[3]) {
623 if (!strncmp(str + 3, ",vlan=", 6)) {
624 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
625 if (*endp)
626 vlan = 0;
627 }
628 } else
629 vlan = qemu_find_bt_vlan(0);
630 if (vlan)
631 return bt_new_hci(vlan);
632 }
633
634 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
635
636 return 0;
637}
638
639static int bt_hci_parse(const char *str)
640{
641 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500642 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000643
644 if (nb_hcis >= MAX_NICS) {
645 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
646 return -1;
647 }
648
649 hci = hci_init(str);
650 if (!hci)
651 return -1;
652
653 bdaddr.b[0] = 0x52;
654 bdaddr.b[1] = 0x54;
655 bdaddr.b[2] = 0x00;
656 bdaddr.b[3] = 0x12;
657 bdaddr.b[4] = 0x34;
658 bdaddr.b[5] = 0x56 + nb_hcis;
659 hci->bdaddr_set(hci, bdaddr.b);
660
661 hci_table[nb_hcis++] = hci;
662
663 return 0;
664}
665
666static void bt_vhci_add(int vlan_id)
667{
668 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
669
670 if (!vlan->slave)
671 fprintf(stderr, "qemu: warning: adding a VHCI to "
672 "an empty scatternet %i\n", vlan_id);
673
674 bt_vhci_init(bt_new_hci(vlan));
675}
676
677static struct bt_device_s *bt_device_add(const char *opt)
678{
679 struct bt_scatternet_s *vlan;
680 int vlan_id = 0;
681 char *endp = strstr(opt, ",vlan=");
682 int len = (endp ? endp - opt : strlen(opt)) + 1;
683 char devname[10];
684
685 pstrcpy(devname, MIN(sizeof(devname), len), opt);
686
687 if (endp) {
688 vlan_id = strtol(endp + 6, &endp, 0);
689 if (*endp) {
690 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
691 return 0;
692 }
693 }
694
695 vlan = qemu_find_bt_vlan(vlan_id);
696
697 if (!vlan->slave)
698 fprintf(stderr, "qemu: warning: adding a slave device to "
699 "an empty scatternet %i\n", vlan_id);
700
701 if (!strcmp(devname, "keyboard"))
702 return bt_keyboard_init(vlan);
703
704 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
705 return 0;
706}
707
708static int bt_parse(const char *opt)
709{
710 const char *endp, *p;
711 int vlan;
712
713 if (strstart(opt, "hci", &endp)) {
714 if (!*endp || *endp == ',') {
715 if (*endp)
716 if (!strstart(endp, ",vlan=", 0))
717 opt = endp + 1;
718
719 return bt_hci_parse(opt);
720 }
721 } else if (strstart(opt, "vhci", &endp)) {
722 if (!*endp || *endp == ',') {
723 if (*endp) {
724 if (strstart(endp, ",vlan=", &p)) {
725 vlan = strtol(p, (char **) &endp, 0);
726 if (*endp) {
727 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
728 return 1;
729 }
730 } else {
731 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
732 return 1;
733 }
734 } else
735 vlan = 0;
736
737 bt_vhci_add(vlan);
738 return 0;
739 }
740 } else if (strstart(opt, "device:", &endp))
741 return !bt_device_add(endp);
742
743 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
744 return 1;
745}
746
balrog1ae26a12008-09-28 23:19:47 +0000747/***********************************************************/
748/* QEMU Block devices */
749
balrog609497a2008-01-14 02:56:53 +0000750#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000751#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000752#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000753#define PFLASH_ALIAS "if=pflash"
754#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000755#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000756
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200757QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +0000758{
759 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200760 char optstr[1024];
761 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +0000762
thse4bcb142007-12-02 04:51:10 +0000763 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200764 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +0000765 va_end(ap);
766
Markus Armbruster8212c642010-02-10 19:52:18 +0100767 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200768 if (!opts) {
769 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
770 __FUNCTION__, optstr);
771 return NULL;
772 }
773 if (file)
774 qemu_opt_set(opts, "file", file);
775 return opts;
aliguorib01b1112009-02-11 15:20:20 +0000776}
777
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200778DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +0000779{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200780 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000781
782 /* seek interface, bus and unit */
783
Blue Swirl72cf2d42009-09-12 07:36:22 +0000784 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200785 if (dinfo->type == type &&
786 dinfo->bus == bus &&
787 dinfo->unit == unit)
788 return dinfo;
789 }
thse4bcb142007-12-02 04:51:10 +0000790
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200791 return NULL;
thse4bcb142007-12-02 04:51:10 +0000792}
793
Gerd Hoffmann2e810b32009-07-31 12:25:38 +0200794DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200795{
796 DriveInfo *dinfo;
797
Blue Swirl72cf2d42009-09-12 07:36:22 +0000798 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200799 if (strcmp(id, dinfo->id))
800 continue;
801 return dinfo;
802 }
803 return NULL;
804}
805
thsf60d39b2007-12-17 03:55:57 +0000806int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +0000807{
808 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200809 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000810
811 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000812 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200813 if(dinfo->type == type &&
814 dinfo->bus > max_bus)
815 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +0000816 }
817 return max_bus;
818}
819
aliguorifa879c62009-01-07 17:32:33 +0000820const char *drive_get_serial(BlockDriverState *bdrv)
821{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200822 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +0000823
Blue Swirl72cf2d42009-09-12 07:36:22 +0000824 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200825 if (dinfo->bdrv == bdrv)
826 return dinfo->serial;
827 }
aliguorifa879c62009-01-07 17:32:33 +0000828
829 return "\0";
830}
831
Kevin Wolff7850092009-11-27 13:25:36 +0100832BlockInterfaceErrorAction drive_get_on_error(
833 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +0000834{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200835 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +0000836
Blue Swirl72cf2d42009-09-12 07:36:22 +0000837 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200838 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100839 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200840 }
aliguori428c5702009-01-21 18:59:04 +0000841
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100842 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +0000843}
844
aurel32a1620fa2008-04-29 05:58:01 +0000845static void bdrv_format_print(void *opaque, const char *name)
846{
847 fprintf(stderr, " %s", name);
848}
849
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200850void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +0000851{
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200852 qemu_opts_del(dinfo->opts);
853 bdrv_delete(dinfo->bdrv);
854 QTAILQ_REMOVE(&drives, dinfo, next);
855 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +0000856}
857
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100858static int parse_block_error_action(const char *buf, int is_read)
859{
860 if (!strcmp(buf, "ignore")) {
861 return BLOCK_ERR_IGNORE;
862 } else if (!is_read && !strcmp(buf, "enospc")) {
863 return BLOCK_ERR_STOP_ENOSPC;
864 } else if (!strcmp(buf, "stop")) {
865 return BLOCK_ERR_STOP_ANY;
866 } else if (!strcmp(buf, "report")) {
867 return BLOCK_ERR_REPORT;
868 } else {
869 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
870 buf, is_read ? "read" : "write");
871 return -1;
872 }
873}
874
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200875DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200876 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +0000877{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200878 const char *buf;
879 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +0000880 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200881 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +0000882 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +0000883 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +0000884 enum { MEDIA_DISK, MEDIA_CDROM } media;
885 int bus_id, unit_id;
886 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +0000887 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +0000888 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +0000889 int max_devs;
890 int index;
balrog33f00272007-12-24 14:33:24 +0000891 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200892 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200893 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100894 int bdrv_flags;
895 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200896 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200897 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200898 int snapshot = 0;
899
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200900 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +0000901
thse4bcb142007-12-02 04:51:10 +0000902 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +0200903 cache = 1;
thse4bcb142007-12-02 04:51:10 +0000904
Gerd Hoffmann4d007812009-08-31 14:23:57 +0200905 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +0000906 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000907 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000908 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +0000909 } else {
thsf60d39b2007-12-17 03:55:57 +0000910 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000911 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000912 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +0000913 }
914 media = MEDIA_DISK;
915
916 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200917 bus_id = qemu_opt_get_number(opts, "bus", 0);
918 unit_id = qemu_opt_get_number(opts, "unit", -1);
919 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +0000920
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200921 cyls = qemu_opt_get_number(opts, "cyls", 0);
922 heads = qemu_opt_get_number(opts, "heads", 0);
923 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +0000924
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200925 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +0200926 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +0000927
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200928 file = qemu_opt_get(opts, "file");
929 serial = qemu_opt_get(opts, "serial");
930
931 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +0000932 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +0000933 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +0000934 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000935 max_devs = MAX_IDE_DEVS;
936 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +0000937 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000938 max_devs = MAX_SCSI_DEVS;
939 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +0000940 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +0000941 max_devs = 0;
942 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +0000943 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +0000944 max_devs = 0;
945 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +0000946 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +0000947 max_devs = 0;
948 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +0000949 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +0000950 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +0000951 } else if (!strcmp(buf, "virtio")) {
952 type = IF_VIRTIO;
953 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000954 } else if (!strcmp(buf, "xen")) {
955 type = IF_XEN;
956 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +0200957 } else if (!strcmp(buf, "none")) {
958 type = IF_NONE;
959 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000960 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200961 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200962 return NULL;
thse4bcb142007-12-02 04:51:10 +0000963 }
964 }
965
thse4bcb142007-12-02 04:51:10 +0000966 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +0000967 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200968 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200969 return NULL;
thse4bcb142007-12-02 04:51:10 +0000970 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000971 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200972 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200973 return NULL;
thse4bcb142007-12-02 04:51:10 +0000974 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000975 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200976 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200977 return NULL;
thse4bcb142007-12-02 04:51:10 +0000978 }
979 }
980
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200981 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000982 if (!cyls) {
983 fprintf(stderr,
984 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200985 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200986 return NULL;
thse4bcb142007-12-02 04:51:10 +0000987 }
988 if (!strcmp(buf, "none"))
989 translation = BIOS_ATA_TRANSLATION_NONE;
990 else if (!strcmp(buf, "lba"))
991 translation = BIOS_ATA_TRANSLATION_LBA;
992 else if (!strcmp(buf, "auto"))
993 translation = BIOS_ATA_TRANSLATION_AUTO;
994 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200995 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200996 return NULL;
thse4bcb142007-12-02 04:51:10 +0000997 }
998 }
999
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001000 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001001 if (!strcmp(buf, "disk")) {
1002 media = MEDIA_DISK;
1003 } else if (!strcmp(buf, "cdrom")) {
1004 if (cyls || secs || heads) {
1005 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001006 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001007 return NULL;
thse4bcb142007-12-02 04:51:10 +00001008 }
1009 media = MEDIA_CDROM;
1010 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001011 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001012 return NULL;
thse4bcb142007-12-02 04:51:10 +00001013 }
1014 }
1015
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001016 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00001017 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00001018 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00001019 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00001020 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00001021 else if (!strcmp(buf, "writeback"))
1022 cache = 2;
balrog33f00272007-12-24 14:33:24 +00001023 else {
1024 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001025 return NULL;
balrog33f00272007-12-24 14:33:24 +00001026 }
1027 }
1028
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001029#ifdef CONFIG_LINUX_AIO
1030 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
1031 if (!strcmp(buf, "threads"))
1032 aio = 0;
1033 else if (!strcmp(buf, "native"))
1034 aio = 1;
1035 else {
1036 fprintf(stderr, "qemu: invalid aio option\n");
1037 return NULL;
1038 }
1039 }
1040#endif
1041
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001042 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00001043 if (strcmp(buf, "?") == 0) {
1044 fprintf(stderr, "qemu: Supported formats:");
1045 bdrv_iterate_format(bdrv_format_print, NULL);
1046 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001047 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00001048 }
Markus Armbrustereb852012009-10-27 18:41:44 +01001049 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00001050 if (!drv) {
1051 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001052 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00001053 }
1054 }
1055
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001056 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001057 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00001058 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00001059 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001060 return NULL;
aliguori428c5702009-01-21 18:59:04 +00001061 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001062
1063 on_write_error = parse_block_error_action(buf, 0);
1064 if (on_write_error < 0) {
1065 return NULL;
1066 }
1067 }
1068
1069 on_read_error = BLOCK_ERR_REPORT;
1070 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01001071 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001072 fprintf(stderr, "rerror is no supported by this format\n");
1073 return NULL;
1074 }
1075
1076 on_read_error = parse_block_error_action(buf, 1);
1077 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001078 return NULL;
aliguori428c5702009-01-21 18:59:04 +00001079 }
1080 }
1081
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001082 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001083 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001084 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001085 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001086 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001087 }
1088
thse4bcb142007-12-02 04:51:10 +00001089 /* compute bus and unit according index */
1090
1091 if (index != -1) {
1092 if (bus_id != 0 || unit_id != -1) {
1093 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001094 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001095 return NULL;
thse4bcb142007-12-02 04:51:10 +00001096 }
1097 if (max_devs == 0)
1098 {
1099 unit_id = index;
1100 bus_id = 0;
1101 } else {
1102 unit_id = index % max_devs;
1103 bus_id = index / max_devs;
1104 }
1105 }
1106
1107 /* if user doesn't specify a unit_id,
1108 * try to find the first free
1109 */
1110
1111 if (unit_id == -1) {
1112 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001113 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001114 unit_id++;
1115 if (max_devs && unit_id >= max_devs) {
1116 unit_id -= max_devs;
1117 bus_id++;
1118 }
1119 }
1120 }
1121
1122 /* check unit id */
1123
1124 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001125 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1126 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001127 return NULL;
thse4bcb142007-12-02 04:51:10 +00001128 }
1129
1130 /*
1131 * ignore multiple definitions
1132 */
1133
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001134 if (drive_get(type, bus_id, unit_id) != NULL) {
1135 *fatal_error = 0;
1136 return NULL;
1137 }
thse4bcb142007-12-02 04:51:10 +00001138
1139 /* init */
1140
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001141 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02001142 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001143 dinfo->id = qemu_strdup(buf);
1144 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001145 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001146 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001147 if (type == IF_IDE || type == IF_SCSI)
1148 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1149 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001150 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001151 devname, bus_id, mediastr, unit_id);
1152 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001153 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001154 devname, mediastr, unit_id);
1155 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001156 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001157 dinfo->devaddr = devaddr;
1158 dinfo->type = type;
1159 dinfo->bus = bus_id;
1160 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001161 dinfo->on_read_error = on_read_error;
1162 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001163 dinfo->opts = opts;
1164 if (serial)
1165 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001166 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00001167
thsf60d39b2007-12-17 03:55:57 +00001168 switch(type) {
thse4bcb142007-12-02 04:51:10 +00001169 case IF_IDE:
1170 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00001171 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00001172 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00001173 switch(media) {
1174 case MEDIA_DISK:
1175 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001176 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1177 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00001178 }
1179 break;
1180 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001181 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00001182 break;
1183 }
1184 break;
1185 case IF_SD:
1186 /* FIXME: This isn't really a floppy, but it's a reasonable
1187 approximation. */
1188 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001189 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00001190 break;
1191 case IF_PFLASH:
1192 case IF_MTD:
1193 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02001194 case IF_VIRTIO:
1195 /* add virtio block device */
1196 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1197 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1198 qemu_opt_set(opts, "drive", dinfo->id);
1199 if (devaddr)
1200 qemu_opt_set(opts, "addr", devaddr);
1201 break;
Paul Brookaae94602009-05-14 22:35:06 +01001202 case IF_COUNT:
1203 abort();
thse4bcb142007-12-02 04:51:10 +00001204 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001205 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001206 *fatal_error = 0;
1207 return NULL;
1208 }
balrog33f00272007-12-24 14:33:24 +00001209 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00001210 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00001211 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00001212 cache = 2; /* always use write-back with snapshot */
1213 }
1214 if (cache == 0) /* no caching */
1215 bdrv_flags |= BDRV_O_NOCACHE;
1216 else if (cache == 2) /* write-back */
1217 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001218
1219 if (aio == 1) {
1220 bdrv_flags |= BDRV_O_NATIVE_AIO;
1221 } else {
1222 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
1223 }
1224
Naphtali Sprei59f26892009-10-26 16:25:16 +02001225 if (ro == 1) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001226 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
1227 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02001228 return NULL;
1229 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02001230 }
Naphtali Spreib196b152010-01-17 16:48:12 +02001231 /*
1232 * cdrom is read-only. Set it now, after above interface checking
1233 * since readonly attribute not explicitly required, so no error.
1234 */
1235 if (media == MEDIA_CDROM) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001236 ro = 1;
Naphtali Spreib196b152010-01-17 16:48:12 +02001237 }
Naphtali Spreif5edb012010-01-17 16:48:13 +02001238 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001239
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001240 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05001241 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1242 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001243 return NULL;
thse4bcb142007-12-02 04:51:10 +00001244 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001245
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001246 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00001247 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001248 *fatal_error = 0;
1249 return dinfo;
thse4bcb142007-12-02 04:51:10 +00001250}
1251
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001252static int drive_init_func(QemuOpts *opts, void *opaque)
1253{
1254 QEMUMachine *machine = opaque;
1255 int fatal_error = 0;
1256
1257 if (drive_init(opts, machine, &fatal_error) == NULL) {
1258 if (fatal_error)
1259 return 1;
1260 }
1261 return 0;
1262}
1263
1264static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1265{
1266 if (NULL == qemu_opt_get(opts, "snapshot")) {
1267 qemu_opt_set(opts, "snapshot", "on");
1268 }
1269 return 0;
1270}
1271
Jan Kiszka76e30d02009-07-02 00:19:02 +02001272void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1273{
1274 boot_set_handler = func;
1275 boot_set_opaque = opaque;
1276}
1277
1278int qemu_boot_set(const char *boot_devices)
1279{
1280 if (!boot_set_handler) {
1281 return -EINVAL;
1282 }
1283 return boot_set_handler(boot_set_opaque, boot_devices);
1284}
1285
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001286static int parse_bootdevices(char *devices)
1287{
1288 /* We just do some generic consistency checks */
1289 const char *p;
1290 int bitmap = 0;
1291
1292 for (p = devices; *p != '\0'; p++) {
1293 /* Allowed boot devices are:
1294 * a-b: floppy disk drives
1295 * c-f: IDE disk drives
1296 * g-m: machine implementation dependant drives
1297 * n-p: network devices
1298 * It's up to each machine implementation to check if the given boot
1299 * devices match the actual hardware implementation and firmware
1300 * features.
1301 */
1302 if (*p < 'a' || *p > 'p') {
1303 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1304 exit(1);
1305 }
1306 if (bitmap & (1 << (*p - 'a'))) {
1307 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1308 exit(1);
1309 }
1310 bitmap |= 1 << (*p - 'a');
1311 }
1312 return bitmap;
1313}
1314
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001315static void restore_boot_devices(void *opaque)
1316{
1317 char *standard_boot_devices = opaque;
1318
1319 qemu_boot_set(standard_boot_devices);
1320
1321 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1322 qemu_free(standard_boot_devices);
1323}
1324
aliguori268a3622009-04-21 22:30:27 +00001325static void numa_add(const char *optarg)
1326{
1327 char option[128];
1328 char *endptr;
1329 unsigned long long value, endvalue;
1330 int nodenr;
1331
1332 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1333 if (!strcmp(option, "node")) {
1334 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1335 nodenr = nb_numa_nodes;
1336 } else {
1337 nodenr = strtoull(option, NULL, 10);
1338 }
1339
1340 if (get_param_value(option, 128, "mem", optarg) == 0) {
1341 node_mem[nodenr] = 0;
1342 } else {
1343 value = strtoull(option, &endptr, 0);
1344 switch (*endptr) {
1345 case 0: case 'M': case 'm':
1346 value <<= 20;
1347 break;
1348 case 'G': case 'g':
1349 value <<= 30;
1350 break;
1351 }
1352 node_mem[nodenr] = value;
1353 }
1354 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1355 node_cpumask[nodenr] = 0;
1356 } else {
1357 value = strtoull(option, &endptr, 10);
1358 if (value >= 64) {
1359 value = 63;
1360 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1361 } else {
1362 if (*endptr == '-') {
1363 endvalue = strtoull(endptr+1, &endptr, 10);
1364 if (endvalue >= 63) {
1365 endvalue = 62;
1366 fprintf(stderr,
1367 "only 63 CPUs in NUMA mode supported.\n");
1368 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001369 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00001370 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001371 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00001372 }
1373 }
1374 node_cpumask[nodenr] = value;
1375 }
1376 nb_numa_nodes++;
1377 }
1378 return;
1379}
1380
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001381static void smp_parse(const char *optarg)
1382{
1383 int smp, sockets = 0, threads = 0, cores = 0;
1384 char *endptr;
1385 char option[128];
1386
1387 smp = strtoul(optarg, &endptr, 10);
1388 if (endptr != optarg) {
1389 if (*endptr == ',') {
1390 endptr++;
1391 }
1392 }
1393 if (get_param_value(option, 128, "sockets", endptr) != 0)
1394 sockets = strtoull(option, NULL, 10);
1395 if (get_param_value(option, 128, "cores", endptr) != 0)
1396 cores = strtoull(option, NULL, 10);
1397 if (get_param_value(option, 128, "threads", endptr) != 0)
1398 threads = strtoull(option, NULL, 10);
1399 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1400 max_cpus = strtoull(option, NULL, 10);
1401
1402 /* compute missing values, prefer sockets over cores over threads */
1403 if (smp == 0 || sockets == 0) {
1404 sockets = sockets > 0 ? sockets : 1;
1405 cores = cores > 0 ? cores : 1;
1406 threads = threads > 0 ? threads : 1;
1407 if (smp == 0) {
1408 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001409 }
1410 } else {
1411 if (cores == 0) {
1412 threads = threads > 0 ? threads : 1;
1413 cores = smp / (sockets * threads);
1414 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05301415 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001416 threads = smp / (cores * sockets);
1417 }
1418 }
1419 }
1420 smp_cpus = smp;
1421 smp_cores = cores > 0 ? cores : 1;
1422 smp_threads = threads > 0 ? threads : 1;
1423 if (max_cpus == 0)
1424 max_cpus = smp_cpus;
1425}
1426
bellard330d0412003-07-26 18:11:40 +00001427/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001428/* USB devices */
1429
aliguoric0f4ce72009-03-05 23:01:01 +00001430static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00001431{
1432 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001433 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001434
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001435 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001436 return -1;
1437
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001438 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1439 dev = usbdevice_create(devname);
1440 if (dev)
1441 goto done;
1442
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001443 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00001444 if (strstart(devname, "host:", &p)) {
1445 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00001446 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1447 dev = usb_bt_init(devname[2] ? hci_init(p) :
1448 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001449 } else {
1450 return -1;
1451 }
pbrook0d92ed32006-05-21 16:30:15 +00001452 if (!dev)
1453 return -1;
1454
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001455done:
bellarda594cfb2005-11-06 16:13:29 +00001456 return 0;
1457}
1458
aliguori1f3870a2008-08-21 19:27:48 +00001459static int usb_device_del(const char *devname)
1460{
1461 int bus_num, addr;
1462 const char *p;
1463
aliguori5d0c5752008-09-14 01:07:41 +00001464 if (strstart(devname, "host:", &p))
1465 return usb_host_device_close(p);
1466
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001467 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001468 return -1;
1469
1470 p = strchr(devname, '.');
1471 if (!p)
1472 return -1;
1473 bus_num = strtoul(devname, NULL, 0);
1474 addr = strtoul(p + 1, NULL, 0);
1475
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001476 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001477}
1478
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001479static int usb_parse(const char *cmdline)
1480{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001481 int r;
1482 r = usb_device_add(cmdline, 0);
1483 if (r < 0) {
1484 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1485 }
1486 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001487}
1488
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001489void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001490{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001491 const char *devname = qdict_get_str(qdict, "devname");
1492 if (usb_device_add(devname, 1) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001493 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001494 }
bellarda594cfb2005-11-06 16:13:29 +00001495}
1496
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001497void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001498{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001499 const char *devname = qdict_get_str(qdict, "devname");
1500 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001501 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001502 }
bellarda594cfb2005-11-06 16:13:29 +00001503}
1504
bellardf7cce892004-12-08 22:21:25 +00001505/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001506/* PCMCIA/Cardbus */
1507
1508static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001509 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001510 struct pcmcia_socket_entry_s *next;
1511} *pcmcia_sockets = 0;
1512
Paul Brookbc24a222009-05-10 01:44:56 +01001513void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001514{
1515 struct pcmcia_socket_entry_s *entry;
1516
1517 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1518 entry->socket = socket;
1519 entry->next = pcmcia_sockets;
1520 pcmcia_sockets = entry;
1521}
1522
Paul Brookbc24a222009-05-10 01:44:56 +01001523void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001524{
1525 struct pcmcia_socket_entry_s *entry, **ptr;
1526
1527 ptr = &pcmcia_sockets;
1528 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1529 if (entry->socket == socket) {
1530 *ptr = entry->next;
1531 qemu_free(entry);
1532 }
1533}
1534
aliguori376253e2009-03-05 23:01:23 +00001535void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001536{
1537 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001538
balrog201a51f2007-04-30 00:51:09 +00001539 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001540 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001541
1542 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001543 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1544 iter->socket->attached ? iter->socket->card_string :
1545 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001546}
1547
1548/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001549/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001550
bellardc4b1fcc2004-03-14 21:44:30 +00001551typedef struct IOHandlerRecord {
1552 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001553 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001554 IOHandler *fd_read;
1555 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001556 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001557 void *opaque;
1558 /* temporary data */
1559 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001560 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001561} IOHandlerRecord;
1562
Juan Quintela31d4ee62010-03-11 17:55:37 +01001563static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1564 QLIST_HEAD_INITIALIZER(io_handlers);
1565
bellardc4b1fcc2004-03-14 21:44:30 +00001566
bellard7c9d8e02005-11-15 22:16:05 +00001567/* XXX: fd_read_poll should be suppressed, but an API change is
1568 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001569int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001570 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001571 IOHandler *fd_read,
1572 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001573 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001574{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001575 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001576
bellard7c9d8e02005-11-15 22:16:05 +00001577 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001578 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001579 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001580 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001581 break;
1582 }
bellard8a7ddc32004-03-31 19:00:16 +00001583 }
bellard7c9d8e02005-11-15 22:16:05 +00001584 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001585 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001586 if (ioh->fd == fd)
1587 goto found;
1588 }
1589 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001590 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001591 found:
1592 ioh->fd = fd;
1593 ioh->fd_read_poll = fd_read_poll;
1594 ioh->fd_read = fd_read;
1595 ioh->fd_write = fd_write;
1596 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001597 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001598 }
bellard7c9d8e02005-11-15 22:16:05 +00001599 return 0;
1600}
1601
ths5fafdf22007-09-16 21:08:06 +00001602int qemu_set_fd_handler(int fd,
1603 IOHandler *fd_read,
1604 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001605 void *opaque)
1606{
1607 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001608}
1609
aliguori56f3a5d2008-10-31 18:07:17 +00001610#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00001611/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00001612/* Polling handling */
1613
1614typedef struct PollingEntry {
1615 PollingFunc *func;
1616 void *opaque;
1617 struct PollingEntry *next;
1618} PollingEntry;
1619
1620static PollingEntry *first_polling_entry;
1621
1622int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1623{
1624 PollingEntry **ppe, *pe;
1625 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00001626 pe->func = func;
1627 pe->opaque = opaque;
1628 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1629 *ppe = pe;
1630 return 0;
1631}
1632
1633void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1634{
1635 PollingEntry **ppe, *pe;
1636 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1637 pe = *ppe;
1638 if (pe->func == func && pe->opaque == opaque) {
1639 *ppe = pe->next;
1640 qemu_free(pe);
1641 break;
1642 }
1643 }
1644}
1645
bellarda18e5242006-06-25 17:18:27 +00001646/***********************************************************/
1647/* Wait objects support */
1648typedef struct WaitObjects {
1649 int num;
1650 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1651 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1652 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1653} WaitObjects;
1654
1655static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00001656
bellarda18e5242006-06-25 17:18:27 +00001657int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1658{
1659 WaitObjects *w = &wait_objects;
1660
1661 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1662 return -1;
1663 w->events[w->num] = handle;
1664 w->func[w->num] = func;
1665 w->opaque[w->num] = opaque;
1666 w->num++;
1667 return 0;
1668}
1669
1670void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1671{
1672 int i, found;
1673 WaitObjects *w = &wait_objects;
1674
1675 found = 0;
1676 for (i = 0; i < w->num; i++) {
1677 if (w->events[i] == handle)
1678 found = 1;
1679 if (found) {
1680 w->events[i] = w->events[i + 1];
1681 w->func[i] = w->func[i + 1];
1682 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00001683 }
bellarda18e5242006-06-25 17:18:27 +00001684 }
1685 if (found)
1686 w->num--;
1687}
1688#endif
1689
bellard8a7ddc32004-03-31 19:00:16 +00001690/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001691/* ram save/restore */
1692
Juan Quintela94fb0902009-09-10 03:04:23 +02001693#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00001694#define RAM_SAVE_FLAG_COMPRESS 0x02
1695#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1696#define RAM_SAVE_FLAG_PAGE 0x08
1697#define RAM_SAVE_FLAG_EOS 0x10
1698
1699static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00001700{
aliguori475e4272008-10-06 20:21:51 +00001701 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1702 uint32_t *array = (uint32_t *)page;
1703 int i;
ths3b46e622007-09-17 08:09:54 +00001704
aliguori475e4272008-10-06 20:21:51 +00001705 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1706 if (array[i] != val)
1707 return 0;
bellardc88676f2006-08-06 13:36:11 +00001708 }
aliguori475e4272008-10-06 20:21:51 +00001709
1710 return 1;
bellardc88676f2006-08-06 13:36:11 +00001711}
1712
aliguori475e4272008-10-06 20:21:51 +00001713static int ram_save_block(QEMUFile *f)
1714{
Anthony Liguoric227f092009-10-01 16:12:16 -05001715 static ram_addr_t current_addr = 0;
1716 ram_addr_t saved_addr = current_addr;
1717 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00001718 int found = 0;
1719
pbrook94a6b542009-04-11 17:15:54 +00001720 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00001721 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00001722 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00001723
1724 cpu_physical_memory_reset_dirty(current_addr,
1725 current_addr + TARGET_PAGE_SIZE,
1726 MIGRATION_DIRTY_FLAG);
1727
pbrook5579c7f2009-04-11 14:47:08 +00001728 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00001729
pbrook5579c7f2009-04-11 14:47:08 +00001730 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00001731 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00001732 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00001733 } else {
1734 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00001735 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00001736 }
1737
1738 found = 1;
1739 break;
1740 }
1741 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00001742 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00001743 }
1744
1745 return found;
1746}
1747
Jan Kiszka84307932009-12-02 11:29:38 +01001748static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00001749
Anthony Liguoric227f092009-10-01 16:12:16 -05001750static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00001751{
Anthony Liguoric227f092009-10-01 16:12:16 -05001752 ram_addr_t addr;
1753 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00001754
pbrook94a6b542009-04-11 17:15:54 +00001755 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00001756 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1757 count++;
1758 }
1759
1760 return count;
1761}
1762
Glauber Costa9f9e28c2009-05-21 17:38:01 -04001763uint64_t ram_bytes_remaining(void)
1764{
1765 return ram_save_remaining() * TARGET_PAGE_SIZE;
1766}
1767
1768uint64_t ram_bytes_transferred(void)
1769{
1770 return bytes_transferred;
1771}
1772
1773uint64_t ram_bytes_total(void)
1774{
1775 return last_ram_offset;
1776}
1777
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001778static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00001779{
Anthony Liguoric227f092009-10-01 16:12:16 -05001780 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04001781 uint64_t bytes_transferred_last;
1782 double bwidth = 0;
1783 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00001784
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001785 if (stage < 0) {
1786 cpu_physical_memory_set_dirty_tracking(0);
1787 return 0;
1788 }
1789
Jan Kiszka9fa06382009-05-22 23:51:45 +02001790 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02001791 qemu_file_set_error(f);
1792 return 0;
1793 }
1794
aliguori475e4272008-10-06 20:21:51 +00001795 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01001796 bytes_transferred = 0;
1797
aliguori475e4272008-10-06 20:21:51 +00001798 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00001799 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00001800 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1801 cpu_physical_memory_set_dirty(addr);
1802 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02001803
aliguori475e4272008-10-06 20:21:51 +00001804 /* Enable dirty memory tracking */
1805 cpu_physical_memory_set_dirty_tracking(1);
1806
pbrook94a6b542009-04-11 17:15:54 +00001807 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00001808 }
1809
Glauber Costaa0a3fd62009-05-28 15:22:57 -04001810 bytes_transferred_last = bytes_transferred;
Paolo Bonzini41c872b2010-01-26 10:31:46 +02001811 bwidth = qemu_get_clock_ns(rt_clock);
Glauber Costaa0a3fd62009-05-28 15:22:57 -04001812
aliguori475e4272008-10-06 20:21:51 +00001813 while (!qemu_file_rate_limit(f)) {
1814 int ret;
1815
1816 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04001817 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00001818 if (ret == 0) /* no more blocks */
1819 break;
1820 }
1821
Paolo Bonzini41c872b2010-01-26 10:31:46 +02001822 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04001823 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
1824
1825 /* if we haven't transferred anything this round, force expected_time to a
1826 * a very high value, but without crashing */
1827 if (bwidth == 0)
1828 bwidth = 0.000001;
1829
aliguori475e4272008-10-06 20:21:51 +00001830 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00001831 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00001832 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04001833 while (ram_save_block(f) != 0) {
1834 bytes_transferred += TARGET_PAGE_SIZE;
1835 }
aliguori8215e912009-04-05 19:30:55 +00001836 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00001837 }
1838
1839 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1840
Glauber Costaa0a3fd62009-05-28 15:22:57 -04001841 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
1842
1843 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00001844}
1845
aliguori475e4272008-10-06 20:21:51 +00001846static int ram_load(QEMUFile *f, void *opaque, int version_id)
1847{
Anthony Liguoric227f092009-10-01 16:12:16 -05001848 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00001849 int flags;
1850
aliguori475e4272008-10-06 20:21:51 +00001851 if (version_id != 3)
1852 return -EINVAL;
1853
1854 do {
1855 addr = qemu_get_be64(f);
1856
1857 flags = addr & ~TARGET_PAGE_MASK;
1858 addr &= TARGET_PAGE_MASK;
1859
1860 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00001861 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00001862 return -EINVAL;
1863 }
1864
aliguori475e4272008-10-06 20:21:51 +00001865 if (flags & RAM_SAVE_FLAG_COMPRESS) {
1866 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05001867 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
1868#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05001869 if (ch == 0 &&
1870 (!kvm_enabled() || kvm_has_sync_mmu())) {
1871 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05001872 }
Anthony Liguori30868442009-06-17 16:46:12 -05001873#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01001874 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00001875 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01001876 }
1877 if (qemu_file_has_error(f)) {
1878 return -EIO;
1879 }
aliguori475e4272008-10-06 20:21:51 +00001880 } while (!(flags & RAM_SAVE_FLAG_EOS));
1881
bellardc88676f2006-08-06 13:36:11 +00001882 return 0;
1883}
1884
aliguori9e472e12008-10-08 19:50:24 +00001885void qemu_service_io(void)
1886{
aliguorid9f75a42009-04-24 18:03:11 +00001887 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00001888}
1889
bellard8a7ddc32004-03-31 19:00:16 +00001890/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001891/* machine registration */
1892
blueswir1bdaf78e2008-10-04 07:24:27 +00001893static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001894QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001895
1896int qemu_register_machine(QEMUMachine *m)
1897{
1898 QEMUMachine **pm;
1899 pm = &first_machine;
1900 while (*pm != NULL)
1901 pm = &(*pm)->next;
1902 m->next = NULL;
1903 *pm = m;
1904 return 0;
1905}
1906
pbrook9596ebb2007-11-18 01:44:38 +00001907static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001908{
1909 QEMUMachine *m;
1910
1911 for(m = first_machine; m != NULL; m = m->next) {
1912 if (!strcmp(m->name, name))
1913 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001914 if (m->alias && !strcmp(m->alias, name))
1915 return m;
bellardcc1daa42005-06-05 14:49:17 +00001916 }
1917 return NULL;
1918}
1919
Anthony Liguori0c257432009-05-21 20:41:01 -05001920static QEMUMachine *find_default_machine(void)
1921{
1922 QEMUMachine *m;
1923
1924 for(m = first_machine; m != NULL; m = m->next) {
1925 if (m->is_default) {
1926 return m;
1927 }
1928 }
1929 return NULL;
1930}
1931
bellardcc1daa42005-06-05 14:49:17 +00001932/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001933/* main execution loop */
1934
pbrook9596ebb2007-11-18 01:44:38 +00001935static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001936{
aliguori7d957bd2009-01-15 22:14:11 +00001937 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001938 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001939 DisplayChangeListener *dcl = ds->listeners;
1940
Sheng Yang62a27442010-01-26 19:21:16 +08001941 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001942 dpy_refresh(ds);
1943
1944 while (dcl != NULL) {
1945 if (dcl->gui_timer_interval &&
1946 dcl->gui_timer_interval < interval)
1947 interval = dcl->gui_timer_interval;
1948 dcl = dcl->next;
1949 }
1950 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001951}
1952
blueswir19043b622009-01-21 19:28:13 +00001953static void nographic_update(void *opaque)
1954{
1955 uint64_t interval = GUI_REFRESH_INTERVAL;
1956
Sheng Yang62a27442010-01-26 19:21:16 +08001957 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001958 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1959}
1960
Jan Kiszkaea375f92010-03-01 19:10:30 +01001961void cpu_synchronize_all_states(void)
1962{
1963 CPUState *cpu;
1964
1965 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1966 cpu_synchronize_state(cpu);
1967 }
1968}
1969
1970void cpu_synchronize_all_post_reset(void)
1971{
1972 CPUState *cpu;
1973
1974 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1975 cpu_synchronize_post_reset(cpu);
1976 }
1977}
1978
1979void cpu_synchronize_all_post_init(void)
1980{
1981 CPUState *cpu;
1982
1983 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
1984 cpu_synchronize_post_init(cpu);
1985 }
1986}
1987
bellard0bd48852005-11-11 00:00:47 +00001988struct vm_change_state_entry {
1989 VMChangeStateHandler *cb;
1990 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001991 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001992};
1993
Blue Swirl72cf2d42009-09-12 07:36:22 +00001994static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001995
1996VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1997 void *opaque)
1998{
1999 VMChangeStateEntry *e;
2000
2001 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00002002
2003 e->cb = cb;
2004 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002005 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00002006 return e;
2007}
2008
2009void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2010{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002011 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00002012 qemu_free (e);
2013}
2014
aliguori9781e042009-01-22 17:15:29 +00002015static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00002016{
2017 VMChangeStateEntry *e;
2018
2019 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00002020 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00002021 }
2022}
2023
aliguorid6dc3d42009-04-24 18:04:07 +00002024static void resume_all_vcpus(void);
2025static void pause_all_vcpus(void);
2026
bellard8a7ddc32004-03-31 19:00:16 +00002027void vm_start(void)
2028{
2029 if (!vm_running) {
2030 cpu_enable_ticks();
2031 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00002032 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00002033 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00002034 }
2035}
2036
bellardbb0c6722004-06-20 12:37:32 +00002037/* reset/shutdown handler */
2038
2039typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002040 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00002041 QEMUResetHandler *func;
2042 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00002043} QEMUResetEntry;
2044
Blue Swirl72cf2d42009-09-12 07:36:22 +00002045static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
2046 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00002047static int reset_requested;
2048static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00002049static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00002050static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00002051static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00002052
aurel32cf7a2fe2008-03-18 06:53:05 +00002053int qemu_shutdown_requested(void)
2054{
2055 int r = shutdown_requested;
2056 shutdown_requested = 0;
2057 return r;
2058}
2059
2060int qemu_reset_requested(void)
2061{
2062 int r = reset_requested;
2063 reset_requested = 0;
2064 return r;
2065}
2066
2067int qemu_powerdown_requested(void)
2068{
2069 int r = powerdown_requested;
2070 powerdown_requested = 0;
2071 return r;
2072}
2073
aliguorie5689022009-04-24 18:03:54 +00002074static int qemu_debug_requested(void)
2075{
2076 int r = debug_requested;
2077 debug_requested = 0;
2078 return r;
2079}
2080
aliguori6e29f5d2009-04-24 18:04:02 +00002081static int qemu_vmstop_requested(void)
2082{
2083 int r = vmstop_requested;
2084 vmstop_requested = 0;
2085 return r;
2086}
2087
2088static void do_vm_stop(int reason)
2089{
2090 if (vm_running) {
2091 cpu_disable_ticks();
2092 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00002093 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00002094 vm_state_notify(0, reason);
Luiz Capitulino6b8f8ff2010-02-25 12:06:59 -03002095 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00002096 }
2097}
2098
Jan Kiszkaa08d4362009-06-27 09:25:07 +02002099void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00002100{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02002101 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00002102
bellardbb0c6722004-06-20 12:37:32 +00002103 re->func = func;
2104 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002105 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00002106}
2107
Jan Kiszkadda9b292009-07-02 00:19:02 +02002108void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00002109{
2110 QEMUResetEntry *re;
2111
Blue Swirl72cf2d42009-09-12 07:36:22 +00002112 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02002113 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002114 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02002115 qemu_free(re);
2116 return;
2117 }
2118 }
2119}
2120
2121void qemu_system_reset(void)
2122{
2123 QEMUResetEntry *re, *nre;
2124
2125 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002126 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00002127 re->func(re->opaque);
2128 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06002129 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01002130 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00002131}
2132
2133void qemu_system_reset_request(void)
2134{
bellardd1beab82006-10-02 19:44:22 +00002135 if (no_reboot) {
2136 shutdown_requested = 1;
2137 } else {
2138 reset_requested = 1;
2139 }
aliguorid9f75a42009-04-24 18:03:11 +00002140 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00002141}
2142
2143void qemu_system_shutdown_request(void)
2144{
2145 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00002146 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00002147}
2148
bellard34751872005-07-02 14:31:34 +00002149void qemu_system_powerdown_request(void)
2150{
2151 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00002152 qemu_notify_event();
2153}
2154
Paolo Bonzini16b151c2010-03-10 11:38:53 +01002155static int cpu_can_run(CPUState *env)
aliguorid9f75a42009-04-24 18:03:11 +00002156{
Paolo Bonzini16b151c2010-03-10 11:38:53 +01002157 if (env->stop)
2158 return 0;
2159 if (env->stopped)
2160 return 0;
2161 if (!vm_running)
2162 return 0;
2163 return 1;
bellardbb0c6722004-06-20 12:37:32 +00002164}
Paolo Bonzini16b151c2010-03-10 11:38:53 +01002165
2166static int cpu_has_work(CPUState *env)
2167{
2168 if (env->stop)
2169 return 1;
2170 if (env->stopped)
2171 return 0;
2172 if (!env->halted)
2173 return 1;
2174 if (qemu_cpu_has_work(env))
2175 return 1;
2176 return 0;
2177}
2178
2179static int tcg_has_work(void)
2180{
2181 CPUState *env;
2182
2183 for (env = first_cpu; env != NULL; env = env->next_cpu)
2184 if (cpu_has_work(env))
2185 return 1;
2186 return 0;
2187}
bellardbb0c6722004-06-20 12:37:32 +00002188
aliguori50317c72009-04-24 18:03:29 +00002189#ifndef _WIN32
2190static int io_thread_fd = -1;
2191
2192static void qemu_event_increment(void)
2193{
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01002194 /* Write 8 bytes to be compatible with eventfd. */
2195 static uint64_t val = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01002196 ssize_t ret;
aliguori50317c72009-04-24 18:03:29 +00002197
2198 if (io_thread_fd == -1)
2199 return;
2200
Paolo Bonzini652ce2d2010-02-02 20:33:10 +01002201 do {
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01002202 ret = write(io_thread_fd, &val, sizeof(val));
Paolo Bonzini652ce2d2010-02-02 20:33:10 +01002203 } while (ret < 0 && errno == EINTR);
2204
2205 /* EAGAIN is fine, a read must be pending. */
2206 if (ret < 0 && errno != EAGAIN) {
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01002207 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
2208 strerror(errno));
2209 exit (1);
2210 }
aliguori50317c72009-04-24 18:03:29 +00002211}
2212
2213static void qemu_event_read(void *opaque)
2214{
2215 int fd = (unsigned long)opaque;
2216 ssize_t len;
Paolo Bonzini1d0f0d92010-02-02 20:33:09 +01002217 char buffer[512];
aliguori50317c72009-04-24 18:03:29 +00002218
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01002219 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
aliguori50317c72009-04-24 18:03:29 +00002220 do {
aliguori50317c72009-04-24 18:03:29 +00002221 len = read(fd, buffer, sizeof(buffer));
Paolo Bonzini1d0f0d92010-02-02 20:33:09 +01002222 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
aliguori50317c72009-04-24 18:03:29 +00002223}
2224
2225static int qemu_event_init(void)
2226{
2227 int err;
2228 int fds[2];
2229
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01002230 err = qemu_eventfd(fds);
aliguori50317c72009-04-24 18:03:29 +00002231 if (err == -1)
2232 return -errno;
2233
2234 err = fcntl_setfl(fds[0], O_NONBLOCK);
2235 if (err < 0)
2236 goto fail;
2237
2238 err = fcntl_setfl(fds[1], O_NONBLOCK);
2239 if (err < 0)
2240 goto fail;
2241
2242 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2243 (void *)(unsigned long)fds[0]);
2244
2245 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00002246 return 0;
2247
aliguori50317c72009-04-24 18:03:29 +00002248fail:
2249 close(fds[0]);
2250 close(fds[1]);
2251 return err;
2252}
2253#else
2254HANDLE qemu_event_handle;
2255
2256static void dummy_event_handler(void *opaque)
2257{
2258}
2259
2260static int qemu_event_init(void)
2261{
2262 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2263 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00002264 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00002265 return -1;
2266 }
2267 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2268 return 0;
2269}
2270
2271static void qemu_event_increment(void)
2272{
malcde1c90c2009-09-27 14:38:18 +04002273 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00002274 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04002275 GetLastError());
2276 exit (1);
2277 }
aliguori50317c72009-04-24 18:03:29 +00002278}
2279#endif
2280
aliguorid6dc3d42009-04-24 18:04:07 +00002281#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00002282static int qemu_init_main_loop(void)
2283{
aliguori50317c72009-04-24 18:03:29 +00002284 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00002285}
2286
aliguori0bf46a42009-04-24 18:03:41 +00002287void qemu_init_vcpu(void *_env)
2288{
2289 CPUState *env = _env;
2290
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002291 env->nr_cores = smp_cores;
2292 env->nr_threads = smp_threads;
Jiri Denemark3f7638e2010-01-05 17:26:34 +01002293 if (kvm_enabled())
2294 kvm_init_vcpu(env);
aliguori0bf46a42009-04-24 18:03:41 +00002295 return;
2296}
2297
aliguori8edac962009-04-24 18:03:45 +00002298int qemu_cpu_self(void *env)
2299{
2300 return 1;
2301}
2302
aliguorid6dc3d42009-04-24 18:04:07 +00002303static void resume_all_vcpus(void)
2304{
2305}
2306
2307static void pause_all_vcpus(void)
2308{
2309}
2310
aliguori8edac962009-04-24 18:03:45 +00002311void qemu_cpu_kick(void *env)
2312{
2313 return;
2314}
2315
aliguorid6dc3d42009-04-24 18:04:07 +00002316void qemu_notify_event(void)
2317{
2318 CPUState *env = cpu_single_env;
2319
Paolo Bonzini7a5e5832010-03-10 11:38:42 +01002320 qemu_event_increment ();
aliguorid6dc3d42009-04-24 18:04:07 +00002321 if (env) {
2322 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002323 }
Paolo Bonzini1db89e92010-03-10 11:38:43 +01002324 if (next_cpu && env != next_cpu) {
2325 cpu_exit(next_cpu);
2326 }
aliguorid6dc3d42009-04-24 18:04:07 +00002327}
2328
Glauber Costad549db52009-10-07 16:38:03 -03002329void qemu_mutex_lock_iothread(void) {}
2330void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00002331
aliguori6e29f5d2009-04-24 18:04:02 +00002332void vm_stop(int reason)
2333{
2334 do_vm_stop(reason);
2335}
2336
aliguorid6dc3d42009-04-24 18:04:07 +00002337#else /* CONFIG_IOTHREAD */
2338
2339#include "qemu-thread.h"
2340
2341QemuMutex qemu_global_mutex;
2342static QemuMutex qemu_fair_mutex;
2343
2344static QemuThread io_thread;
2345
2346static QemuThread *tcg_cpu_thread;
2347static QemuCond *tcg_halt_cond;
2348
2349static int qemu_system_ready;
2350/* cpu creation */
2351static QemuCond qemu_cpu_cond;
2352/* system init */
2353static QemuCond qemu_system_cond;
2354static QemuCond qemu_pause_cond;
2355
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002356static void tcg_block_io_signals(void);
2357static void kvm_block_io_signals(CPUState *env);
aliguorid6dc3d42009-04-24 18:04:07 +00002358static void unblock_io_signals(void);
aliguorid6dc3d42009-04-24 18:04:07 +00002359
2360static int qemu_init_main_loop(void)
2361{
2362 int ret;
2363
2364 ret = qemu_event_init();
2365 if (ret)
2366 return ret;
2367
2368 qemu_cond_init(&qemu_pause_cond);
2369 qemu_mutex_init(&qemu_fair_mutex);
2370 qemu_mutex_init(&qemu_global_mutex);
2371 qemu_mutex_lock(&qemu_global_mutex);
2372
2373 unblock_io_signals();
2374 qemu_thread_self(&io_thread);
2375
2376 return 0;
2377}
2378
Marcelo Tosattifed6c342010-02-17 20:14:41 -02002379static void qemu_wait_io_event_common(CPUState *env)
2380{
2381 if (env->stop) {
2382 env->stop = 0;
2383 env->stopped = 1;
2384 qemu_cond_signal(&qemu_pause_cond);
2385 }
2386}
2387
aliguorid6dc3d42009-04-24 18:04:07 +00002388static void qemu_wait_io_event(CPUState *env)
2389{
2390 while (!tcg_has_work())
2391 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2392
2393 qemu_mutex_unlock(&qemu_global_mutex);
2394
2395 /*
2396 * Users of qemu_global_mutex can be starved, having no chance
2397 * to acquire it since this path will get to it first.
2398 * So use another lock to provide fairness.
2399 */
2400 qemu_mutex_lock(&qemu_fair_mutex);
2401 qemu_mutex_unlock(&qemu_fair_mutex);
2402
2403 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02002404 qemu_wait_io_event_common(env);
2405}
2406
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002407static void qemu_kvm_eat_signal(CPUState *env, int timeout)
2408{
2409 struct timespec ts;
2410 int r, e;
2411 siginfo_t siginfo;
2412 sigset_t waitset;
2413
2414 ts.tv_sec = timeout / 1000;
2415 ts.tv_nsec = (timeout % 1000) * 1000000;
2416
2417 sigemptyset(&waitset);
2418 sigaddset(&waitset, SIG_IPI);
2419
2420 qemu_mutex_unlock(&qemu_global_mutex);
2421 r = sigtimedwait(&waitset, &siginfo, &ts);
2422 e = errno;
2423 qemu_mutex_lock(&qemu_global_mutex);
2424
2425 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
2426 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
2427 exit(1);
aliguorid6dc3d42009-04-24 18:04:07 +00002428 }
2429}
2430
Marcelo Tosattifed6c342010-02-17 20:14:41 -02002431static void qemu_kvm_wait_io_event(CPUState *env)
2432{
2433 while (!cpu_has_work(env))
2434 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2435
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002436 qemu_kvm_eat_signal(env, 0);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02002437 qemu_wait_io_event_common(env);
aliguorid6dc3d42009-04-24 18:04:07 +00002438}
2439
2440static int qemu_cpu_exec(CPUState *env);
2441
2442static void *kvm_cpu_thread_fn(void *arg)
2443{
2444 CPUState *env = arg;
2445
aliguorid6dc3d42009-04-24 18:04:07 +00002446 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02002447 if (kvm_enabled())
2448 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00002449
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002450 kvm_block_io_signals(env);
2451
aliguorid6dc3d42009-04-24 18:04:07 +00002452 /* signal CPU creation */
2453 qemu_mutex_lock(&qemu_global_mutex);
2454 env->created = 1;
2455 qemu_cond_signal(&qemu_cpu_cond);
2456
2457 /* and wait for machine initialization */
2458 while (!qemu_system_ready)
2459 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2460
2461 while (1) {
2462 if (cpu_can_run(env))
2463 qemu_cpu_exec(env);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02002464 qemu_kvm_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00002465 }
2466
2467 return NULL;
2468}
2469
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002470static bool tcg_cpu_exec(void);
aliguorid6dc3d42009-04-24 18:04:07 +00002471
2472static void *tcg_cpu_thread_fn(void *arg)
2473{
2474 CPUState *env = arg;
2475
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002476 tcg_block_io_signals();
aliguorid6dc3d42009-04-24 18:04:07 +00002477 qemu_thread_self(env->thread);
2478
2479 /* signal CPU creation */
2480 qemu_mutex_lock(&qemu_global_mutex);
2481 for (env = first_cpu; env != NULL; env = env->next_cpu)
2482 env->created = 1;
2483 qemu_cond_signal(&qemu_cpu_cond);
2484
2485 /* and wait for machine initialization */
2486 while (!qemu_system_ready)
2487 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2488
2489 while (1) {
2490 tcg_cpu_exec();
2491 qemu_wait_io_event(cur_cpu);
2492 }
2493
2494 return NULL;
2495}
2496
2497void qemu_cpu_kick(void *_env)
2498{
2499 CPUState *env = _env;
2500 qemu_cond_broadcast(env->halt_cond);
2501 if (kvm_enabled())
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002502 qemu_thread_signal(env->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002503}
2504
Glauber Costae5bc2012009-09-28 15:27:44 -03002505int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00002506{
Glauber Costae5bc2012009-09-28 15:27:44 -03002507 CPUState *env = _env;
2508 QemuThread this;
2509
2510 qemu_thread_self(&this);
2511
2512 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00002513}
2514
2515static void cpu_signal(int sig)
2516{
2517 if (cpu_single_env)
2518 cpu_exit(cpu_single_env);
2519}
2520
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002521static void tcg_block_io_signals(void)
aliguorid6dc3d42009-04-24 18:04:07 +00002522{
2523 sigset_t set;
2524 struct sigaction sigact;
2525
2526 sigemptyset(&set);
2527 sigaddset(&set, SIGUSR2);
2528 sigaddset(&set, SIGIO);
2529 sigaddset(&set, SIGALRM);
Marcelo Tosattiadc8c962010-02-17 20:14:40 -02002530 sigaddset(&set, SIGCHLD);
aliguorid6dc3d42009-04-24 18:04:07 +00002531 pthread_sigmask(SIG_BLOCK, &set, NULL);
2532
2533 sigemptyset(&set);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002534 sigaddset(&set, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002535 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2536
2537 memset(&sigact, 0, sizeof(sigact));
2538 sigact.sa_handler = cpu_signal;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002539 sigaction(SIG_IPI, &sigact, NULL);
2540}
2541
2542static void dummy_signal(int sig)
2543{
2544}
2545
2546static void kvm_block_io_signals(CPUState *env)
2547{
2548 int r;
2549 sigset_t set;
2550 struct sigaction sigact;
2551
2552 sigemptyset(&set);
2553 sigaddset(&set, SIGUSR2);
2554 sigaddset(&set, SIGIO);
2555 sigaddset(&set, SIGALRM);
2556 sigaddset(&set, SIGCHLD);
2557 sigaddset(&set, SIG_IPI);
2558 pthread_sigmask(SIG_BLOCK, &set, NULL);
2559
2560 pthread_sigmask(SIG_BLOCK, NULL, &set);
2561 sigdelset(&set, SIG_IPI);
2562
2563 memset(&sigact, 0, sizeof(sigact));
2564 sigact.sa_handler = dummy_signal;
2565 sigaction(SIG_IPI, &sigact, NULL);
2566
2567 r = kvm_set_signal_mask(env, &set);
2568 if (r) {
2569 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
2570 exit(1);
2571 }
aliguorid6dc3d42009-04-24 18:04:07 +00002572}
2573
2574static void unblock_io_signals(void)
2575{
2576 sigset_t set;
2577
2578 sigemptyset(&set);
2579 sigaddset(&set, SIGUSR2);
2580 sigaddset(&set, SIGIO);
2581 sigaddset(&set, SIGALRM);
2582 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2583
2584 sigemptyset(&set);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002585 sigaddset(&set, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002586 pthread_sigmask(SIG_BLOCK, &set, NULL);
2587}
2588
2589static void qemu_signal_lock(unsigned int msecs)
2590{
2591 qemu_mutex_lock(&qemu_fair_mutex);
2592
2593 while (qemu_mutex_trylock(&qemu_global_mutex)) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002594 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002595 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2596 break;
2597 }
2598 qemu_mutex_unlock(&qemu_fair_mutex);
2599}
2600
Glauber Costad549db52009-10-07 16:38:03 -03002601void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00002602{
2603 if (kvm_enabled()) {
2604 qemu_mutex_lock(&qemu_fair_mutex);
2605 qemu_mutex_lock(&qemu_global_mutex);
2606 qemu_mutex_unlock(&qemu_fair_mutex);
2607 } else
2608 qemu_signal_lock(100);
2609}
2610
Glauber Costad549db52009-10-07 16:38:03 -03002611void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00002612{
2613 qemu_mutex_unlock(&qemu_global_mutex);
2614}
2615
2616static int all_vcpus_paused(void)
2617{
2618 CPUState *penv = first_cpu;
2619
2620 while (penv) {
2621 if (!penv->stopped)
2622 return 0;
2623 penv = (CPUState *)penv->next_cpu;
2624 }
2625
2626 return 1;
2627}
2628
2629static void pause_all_vcpus(void)
2630{
2631 CPUState *penv = first_cpu;
2632
2633 while (penv) {
2634 penv->stop = 1;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002635 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002636 qemu_cpu_kick(penv);
2637 penv = (CPUState *)penv->next_cpu;
2638 }
2639
2640 while (!all_vcpus_paused()) {
2641 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2642 penv = first_cpu;
2643 while (penv) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002644 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002645 penv = (CPUState *)penv->next_cpu;
2646 }
2647 }
2648}
2649
2650static void resume_all_vcpus(void)
2651{
2652 CPUState *penv = first_cpu;
2653
2654 while (penv) {
2655 penv->stop = 0;
2656 penv->stopped = 0;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02002657 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00002658 qemu_cpu_kick(penv);
2659 penv = (CPUState *)penv->next_cpu;
2660 }
2661}
2662
2663static void tcg_init_vcpu(void *_env)
2664{
2665 CPUState *env = _env;
2666 /* share a single thread for all cpus with TCG */
2667 if (!tcg_cpu_thread) {
2668 env->thread = qemu_mallocz(sizeof(QemuThread));
2669 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2670 qemu_cond_init(env->halt_cond);
2671 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2672 while (env->created == 0)
2673 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2674 tcg_cpu_thread = env->thread;
2675 tcg_halt_cond = env->halt_cond;
2676 } else {
2677 env->thread = tcg_cpu_thread;
2678 env->halt_cond = tcg_halt_cond;
2679 }
2680}
2681
2682static void kvm_start_vcpu(CPUState *env)
2683{
aliguorid6dc3d42009-04-24 18:04:07 +00002684 env->thread = qemu_mallocz(sizeof(QemuThread));
2685 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2686 qemu_cond_init(env->halt_cond);
2687 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2688 while (env->created == 0)
2689 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2690}
2691
2692void qemu_init_vcpu(void *_env)
2693{
2694 CPUState *env = _env;
2695
Jiri Denemark3f7638e2010-01-05 17:26:34 +01002696 env->nr_cores = smp_cores;
2697 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00002698 if (kvm_enabled())
2699 kvm_start_vcpu(env);
2700 else
2701 tcg_init_vcpu(env);
2702}
2703
2704void qemu_notify_event(void)
2705{
2706 qemu_event_increment();
2707}
2708
Paolo Bonzini16b151c2010-03-10 11:38:53 +01002709static void qemu_system_vmstop_request(int reason)
2710{
2711 vmstop_requested = reason;
2712 qemu_notify_event();
2713}
2714
aliguorid6dc3d42009-04-24 18:04:07 +00002715void vm_stop(int reason)
2716{
2717 QemuThread me;
2718 qemu_thread_self(&me);
2719
2720 if (!qemu_thread_equal(&me, &io_thread)) {
2721 qemu_system_vmstop_request(reason);
2722 /*
2723 * FIXME: should not return to device code in case
2724 * vm_stop() has been requested.
2725 */
2726 if (cpu_single_env) {
2727 cpu_exit(cpu_single_env);
2728 cpu_single_env->stop = 1;
2729 }
2730 return;
2731 }
2732 do_vm_stop(reason);
2733}
2734
2735#endif
2736
2737
ths877cf882007-04-18 18:11:47 +00002738#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00002739static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00002740{
2741 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00002742 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00002743
bellardf3311102006-04-12 20:21:17 +00002744
2745 /* XXX: need to suppress polling by better using win32 events */
2746 ret = 0;
2747 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2748 ret |= pe->func(pe->opaque);
2749 }
thse6b1e552007-04-18 17:56:02 +00002750 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00002751 int err;
2752 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00002753
aliguori56f3a5d2008-10-31 18:07:17 +00002754 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00002755 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2756 if (w->func[ret - WAIT_OBJECT_0])
2757 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00002758
ths5fafdf22007-09-16 21:08:06 +00002759 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00002760 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00002761
thse6b1e552007-04-18 17:56:02 +00002762 /* Check if event is signaled */
2763 ret2 = WaitForSingleObject(w->events[i], 0);
2764 if(ret2 == WAIT_OBJECT_0) {
2765 if (w->func[i])
2766 w->func[i](w->opaque[i]);
2767 } else if (ret2 == WAIT_TIMEOUT) {
2768 } else {
2769 err = GetLastError();
2770 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00002771 }
2772 }
bellarda18e5242006-06-25 17:18:27 +00002773 } else if (ret == WAIT_TIMEOUT) {
2774 } else {
2775 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00002776 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00002777 }
bellardf3311102006-04-12 20:21:17 +00002778 }
aliguori56f3a5d2008-10-31 18:07:17 +00002779
2780 *timeout = 0;
2781}
2782#else
blueswir169d64512008-12-07 19:30:18 +00002783static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00002784{
2785}
bellardfd1dff42006-02-01 21:29:26 +00002786#endif
aliguori56f3a5d2008-10-31 18:07:17 +00002787
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002788void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00002789{
2790 IOHandlerRecord *ioh;
2791 fd_set rfds, wfds, xfds;
2792 int ret, nfds;
2793 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002794 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00002795
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002796 if (nonblocking)
2797 timeout = 0;
2798 else {
2799 timeout = qemu_calculate_timeout();
2800 qemu_bh_update_timeout(&timeout);
2801 }
aliguori56f3a5d2008-10-31 18:07:17 +00002802
2803 host_main_loop_wait(&timeout);
2804
bellardfd1dff42006-02-01 21:29:26 +00002805 /* poll any events */
2806 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00002807 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00002808 FD_ZERO(&rfds);
2809 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00002810 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01002811 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00002812 if (ioh->deleted)
2813 continue;
bellardfd1dff42006-02-01 21:29:26 +00002814 if (ioh->fd_read &&
2815 (!ioh->fd_read_poll ||
2816 ioh->fd_read_poll(ioh->opaque) != 0)) {
2817 FD_SET(ioh->fd, &rfds);
2818 if (ioh->fd > nfds)
2819 nfds = ioh->fd;
2820 }
2821 if (ioh->fd_write) {
2822 FD_SET(ioh->fd, &wfds);
2823 if (ioh->fd > nfds)
2824 nfds = ioh->fd;
2825 }
2826 }
ths3b46e622007-09-17 08:09:54 +00002827
aliguori56f3a5d2008-10-31 18:07:17 +00002828 tv.tv_sec = timeout / 1000;
2829 tv.tv_usec = (timeout % 1000) * 1000;
2830
Jan Kiszkad918f232009-06-24 14:42:30 +02002831 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2832
aliguori48708522009-04-24 18:03:49 +00002833 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00002834 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00002835 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00002836 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01002837 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00002838
Juan Quintela31d4ee62010-03-11 17:55:37 +01002839 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00002840 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01002841 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00002842 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01002843 continue;
2844 }
2845 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2846 ioh->fd_read(ioh->opaque);
2847 }
2848 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2849 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01002850 }
thscafffd42007-02-28 21:59:44 +00002851 }
bellardfd1dff42006-02-01 21:29:26 +00002852 }
Jan Kiszkad918f232009-06-24 14:42:30 +02002853
2854 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00002855
Paolo Bonzinib6964822010-03-10 11:38:45 +01002856 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02002857
pbrook423f0742007-05-23 00:06:54 +00002858 /* Check bottom-halves last in case any of the earlier events triggered
2859 them. */
2860 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00002861
bellard5905b2e2004-08-01 21:53:26 +00002862}
2863
aliguori43b96852009-04-24 18:03:33 +00002864static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00002865{
aliguori43b96852009-04-24 18:03:33 +00002866 int ret;
bellard89bfc102006-02-08 22:46:31 +00002867#ifdef CONFIG_PROFILER
2868 int64_t ti;
2869#endif
aliguori43b96852009-04-24 18:03:33 +00002870
2871#ifdef CONFIG_PROFILER
2872 ti = profile_getclock();
2873#endif
2874 if (use_icount) {
2875 int64_t count;
2876 int decr;
2877 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
2878 env->icount_decr.u16.low = 0;
2879 env->icount_extra = 0;
Paolo Bonzinie2a7bb42010-03-10 11:38:49 +01002880 count = qemu_icount_round (qemu_next_deadline());
aliguori43b96852009-04-24 18:03:33 +00002881 qemu_icount += count;
2882 decr = (count > 0xffff) ? 0xffff : count;
2883 count -= decr;
2884 env->icount_decr.u16.low = decr;
2885 env->icount_extra = count;
2886 }
2887 ret = cpu_exec(env);
2888#ifdef CONFIG_PROFILER
2889 qemu_time += profile_getclock() - ti;
2890#endif
2891 if (use_icount) {
2892 /* Fold pending instructions back into the
2893 instruction counter, and clear the interrupt flag. */
2894 qemu_icount -= (env->icount_decr.u16.low
2895 + env->icount_extra);
2896 env->icount_decr.u32 = 0;
2897 env->icount_extra = 0;
2898 }
2899 return ret;
2900}
2901
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002902static bool tcg_cpu_exec(void)
aliguorie6e35b12009-04-24 18:03:57 +00002903{
aliguorid6dc3d42009-04-24 18:04:07 +00002904 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00002905
2906 if (next_cpu == NULL)
2907 next_cpu = first_cpu;
2908 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
2909 CPUState *env = cur_cpu = next_cpu;
2910
Paolo Bonzini972abbe2010-03-10 11:38:47 +01002911 qemu_clock_enable(vm_clock,
2912 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
2913
Paolo Bonzini63255042010-03-10 11:38:50 +01002914 if (qemu_alarm_pending())
aliguorie6e35b12009-04-24 18:03:57 +00002915 break;
aliguorid6dc3d42009-04-24 18:04:07 +00002916 if (cpu_can_run(env))
2917 ret = qemu_cpu_exec(env);
Marcelo Tosattic37cc7b2010-02-09 12:49:04 -02002918 else if (env->stop)
2919 break;
2920
aliguorie6e35b12009-04-24 18:03:57 +00002921 if (ret == EXCP_DEBUG) {
2922 gdb_set_stop_cpu(env);
2923 debug_requested = 1;
2924 break;
2925 }
2926 }
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002927 return tcg_has_work();
aliguorie6e35b12009-04-24 18:03:57 +00002928}
2929
Blue Swirl87d0a282010-03-27 18:24:45 +00002930static void set_numa_modes(void)
2931{
2932 CPUState *env;
2933 int i;
2934
2935 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2936 for (i = 0; i < nb_numa_nodes; i++) {
2937 if (node_cpumask[i] & (1 << env->cpu_index)) {
2938 env->numa_node = i;
2939 }
2940 }
2941 }
2942}
2943
aliguori43b96852009-04-24 18:03:33 +00002944static int vm_can_run(void)
2945{
2946 if (powerdown_requested)
2947 return 0;
2948 if (reset_requested)
2949 return 0;
2950 if (shutdown_requested)
2951 return 0;
aliguorie5689022009-04-24 18:03:54 +00002952 if (debug_requested)
2953 return 0;
aliguori43b96852009-04-24 18:03:33 +00002954 return 1;
2955}
2956
Blue Swirld9c32312009-08-09 08:42:19 +00002957qemu_irq qemu_system_powerdown;
2958
aliguori43b96852009-04-24 18:03:33 +00002959static void main_loop(void)
2960{
aliguori6e29f5d2009-04-24 18:04:02 +00002961 int r;
aliguori43b96852009-04-24 18:03:33 +00002962
aliguorid6dc3d42009-04-24 18:04:07 +00002963#ifdef CONFIG_IOTHREAD
2964 qemu_system_ready = 1;
2965 qemu_cond_broadcast(&qemu_system_cond);
2966#endif
2967
aliguori6e29f5d2009-04-24 18:04:02 +00002968 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00002969 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002970 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00002971#ifdef CONFIG_PROFILER
2972 int64_t ti;
2973#endif
aliguorid6dc3d42009-04-24 18:04:07 +00002974#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002975 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00002976#endif
aliguori43b96852009-04-24 18:03:33 +00002977#ifdef CONFIG_PROFILER
2978 ti = profile_getclock();
2979#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002980 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002981#ifdef CONFIG_PROFILER
2982 dev_time += profile_getclock() - ti;
2983#endif
aliguorie5689022009-04-24 18:03:54 +00002984 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00002985
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02002986 if (qemu_debug_requested()) {
aliguori43b96852009-04-24 18:03:33 +00002987 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02002988 }
aliguori43b96852009-04-24 18:03:33 +00002989 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00002990 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00002991 if (no_shutdown) {
2992 vm_stop(0);
2993 no_shutdown = 0;
2994 } else
2995 break;
2996 }
aliguorid6dc3d42009-04-24 18:04:07 +00002997 if (qemu_reset_requested()) {
2998 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00002999 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00003000 resume_all_vcpus();
3001 }
Blue Swirld9c32312009-08-09 08:42:19 +00003002 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00003003 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00003004 qemu_irq_raise(qemu_system_powerdown);
3005 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02003006 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00003007 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02003008 }
aliguori43b96852009-04-24 18:03:33 +00003009 }
aliguorid6dc3d42009-04-24 18:04:07 +00003010 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00003011}
3012
pbrook9bd7e6d2009-04-07 22:58:45 +00003013static void version(void)
3014{
pbrook4a19f1e2009-04-07 23:17:49 +00003015 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00003016}
3017
ths15f82202007-06-29 23:26:08 +00003018static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003019{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01003020 const char *options_help =
blueswir15824d652009-03-28 06:44:27 +00003021#define DEF(option, opt_arg, opt_enum, opt_help) \
3022 opt_help
3023#define DEFHEADING(text) stringify(text) "\n"
3024#include "qemu-options.h"
3025#undef DEF
3026#undef DEFHEADING
3027#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01003028 ;
3029 version();
3030 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03003031 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01003032 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3033 "\n"
3034 "%s\n"
malc3f020d72010-02-08 12:04:56 +03003035 "During emulation, the following keys are useful:\n"
3036 "ctrl-alt-f toggle full screen\n"
3037 "ctrl-alt-n switch to virtual console 'n'\n"
3038 "ctrl-alt toggle mouse and keyboard grab\n"
3039 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01003040 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3041 "qemu",
3042 options_help);
ths15f82202007-06-29 23:26:08 +00003043 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00003044}
3045
bellardcd6f1162004-05-13 22:02:20 +00003046#define HAS_ARG 0x0001
3047
3048enum {
blueswir15824d652009-03-28 06:44:27 +00003049#define DEF(option, opt_arg, opt_enum, opt_help) \
3050 opt_enum,
3051#define DEFHEADING(text)
3052#include "qemu-options.h"
3053#undef DEF
3054#undef DEFHEADING
3055#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00003056};
3057
3058typedef struct QEMUOption {
3059 const char *name;
3060 int flags;
3061 int index;
3062} QEMUOption;
3063
blueswir1dbed7e42008-10-01 19:38:09 +00003064static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00003065 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00003066#define DEF(option, opt_arg, opt_enum, opt_help) \
3067 { option, opt_arg, opt_enum },
3068#define DEFHEADING(text)
3069#include "qemu-options.h"
3070#undef DEF
3071#undef DEFHEADING
3072#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00003073 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00003074};
3075
bellard1d14ffa2005-10-30 18:58:22 +00003076#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00003077struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00003078#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00003079#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00003080 {
3081 "pcspk",
3082 "PC speaker",
3083 0,
3084 1,
3085 { .init_isa = pcspk_audio_init }
3086 },
3087#endif
malc4c9b53e2009-01-09 10:46:34 +00003088
3089#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00003090 {
3091 "sb16",
3092 "Creative Sound Blaster 16",
3093 0,
3094 1,
3095 { .init_isa = SB16_init }
3096 },
malc4c9b53e2009-01-09 10:46:34 +00003097#endif
bellard6a36d842005-12-18 20:34:32 +00003098
malccc53d262008-06-13 10:48:22 +00003099#ifdef CONFIG_CS4231A
3100 {
3101 "cs4231a",
3102 "CS4231A",
3103 0,
3104 1,
3105 { .init_isa = cs4231a_init }
3106 },
3107#endif
3108
bellard6a36d842005-12-18 20:34:32 +00003109#ifdef CONFIG_ADLIB
3110 {
3111 "adlib",
3112#ifdef HAS_YMF262
3113 "Yamaha YMF262 (OPL3)",
3114#else
3115 "Yamaha YM3812 (OPL2)",
3116#endif
3117 0,
3118 1,
3119 { .init_isa = Adlib_init }
3120 },
3121#endif
3122
3123#ifdef CONFIG_GUS
3124 {
3125 "gus",
3126 "Gravis Ultrasound GF1",
3127 0,
3128 1,
3129 { .init_isa = GUS_init }
3130 },
3131#endif
3132
malc4c9b53e2009-01-09 10:46:34 +00003133#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00003134 {
3135 "ac97",
3136 "Intel 82801AA AC97 Audio",
3137 0,
3138 0,
3139 { .init_pci = ac97_init }
3140 },
malc4c9b53e2009-01-09 10:46:34 +00003141#endif
balroge5c9a132008-01-14 04:27:55 +00003142
malc4c9b53e2009-01-09 10:46:34 +00003143#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00003144 {
3145 "es1370",
3146 "ENSONIQ AudioPCI ES1370",
3147 0,
3148 0,
3149 { .init_pci = es1370_init }
3150 },
balrogb00052e2007-04-30 02:22:06 +00003151#endif
bellard6a36d842005-12-18 20:34:32 +00003152
malc4c9b53e2009-01-09 10:46:34 +00003153#endif /* HAS_AUDIO_CHOICE */
3154
bellard6a36d842005-12-18 20:34:32 +00003155 { NULL, NULL, 0, 0, { NULL } }
3156};
3157
bellard1d14ffa2005-10-30 18:58:22 +00003158static void select_soundhw (const char *optarg)
3159{
bellard6a36d842005-12-18 20:34:32 +00003160 struct soundhw *c;
3161
bellard1d14ffa2005-10-30 18:58:22 +00003162 if (*optarg == '?') {
3163 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00003164
bellard1d14ffa2005-10-30 18:58:22 +00003165 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00003166 for (c = soundhw; c->name; ++c) {
3167 printf ("%-11s %s\n", c->name, c->descr);
3168 }
3169 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00003170 exit (*optarg != '?');
3171 }
3172 else {
bellard6a36d842005-12-18 20:34:32 +00003173 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00003174 const char *p;
3175 char *e;
3176 int bad_card = 0;
3177
bellard6a36d842005-12-18 20:34:32 +00003178 if (!strcmp (optarg, "all")) {
3179 for (c = soundhw; c->name; ++c) {
3180 c->enabled = 1;
3181 }
3182 return;
3183 }
bellard1d14ffa2005-10-30 18:58:22 +00003184
bellard6a36d842005-12-18 20:34:32 +00003185 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00003186 while (*p) {
3187 e = strchr (p, ',');
3188 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00003189
3190 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04003191 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00003192 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00003193 break;
3194 }
3195 }
bellard6a36d842005-12-18 20:34:32 +00003196
3197 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00003198 if (l > 80) {
3199 fprintf (stderr,
3200 "Unknown sound card name (too big to show)\n");
3201 }
3202 else {
3203 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3204 (int) l, p);
3205 }
3206 bad_card = 1;
3207 }
3208 p += l + (e != NULL);
3209 }
3210
3211 if (bad_card)
3212 goto show_valid_cards;
3213 }
3214}
3215#endif
3216
malc3893c122008-09-28 00:42:05 +00003217static void select_vgahw (const char *p)
3218{
3219 const char *opts;
3220
Gerd Hoffmann64465292009-12-08 13:11:45 +01003221 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10003222 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00003223 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10003224 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00003225 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10003226 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00003227 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10003228 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00003229 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10003230 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00003231 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00003232 invalid_vga:
3233 fprintf(stderr, "Unknown vga type: %s\n", p);
3234 exit(1);
3235 }
malccb5a7aa2008-09-28 00:42:12 +00003236 while (*opts) {
3237 const char *nextopt;
3238
3239 if (strstart(opts, ",retrace=", &nextopt)) {
3240 opts = nextopt;
3241 if (strstart(opts, "dumb", &nextopt))
3242 vga_retrace_method = VGA_RETRACE_DUMB;
3243 else if (strstart(opts, "precise", &nextopt))
3244 vga_retrace_method = VGA_RETRACE_PRECISE;
3245 else goto invalid_vga;
3246 } else goto invalid_vga;
3247 opts = nextopt;
3248 }
malc3893c122008-09-28 00:42:05 +00003249}
3250
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003251#ifdef TARGET_I386
3252static int balloon_parse(const char *arg)
3253{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02003254 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003255
Gerd Hoffmann382f0742009-08-14 10:34:22 +02003256 if (strcmp(arg, "none") == 0) {
3257 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003258 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02003259
3260 if (!strncmp(arg, "virtio", 6)) {
3261 if (arg[6] == ',') {
3262 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01003263 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02003264 if (!opts)
3265 return -1;
3266 } else {
3267 /* create empty opts */
3268 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
3269 }
3270 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
3271 return 0;
3272 }
3273
3274 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003275}
3276#endif
3277
bellard3587d7e2006-06-26 20:03:44 +00003278#ifdef _WIN32
3279static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3280{
3281 exit(STATUS_CONTROL_C_EXIT);
3282 return TRUE;
3283}
3284#endif
3285
aliguoric4be29f2009-04-17 18:58:14 +00003286int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00003287{
3288 int ret;
3289
3290 if(strlen(str) != 36)
3291 return -1;
3292
3293 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3294 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3295 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3296
3297 if(ret != 16)
3298 return -1;
3299
aliguorib6f6e3d2009-04-17 18:59:56 +00003300#ifdef TARGET_I386
3301 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3302#endif
3303
blueswir18fcb1b92008-09-18 18:29:08 +00003304 return 0;
3305}
3306
aliguori5b08fc12008-08-21 20:08:03 +00003307#ifndef _WIN32
3308
3309static void termsig_handler(int signal)
3310{
3311 qemu_system_shutdown_request();
3312}
3313
Jan Kiszka7c3370d2009-05-08 12:34:17 +02003314static void sigchld_handler(int signal)
3315{
3316 waitpid(-1, NULL, WNOHANG);
3317}
3318
3319static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00003320{
3321 struct sigaction act;
3322
3323 memset(&act, 0, sizeof(act));
3324 act.sa_handler = termsig_handler;
3325 sigaction(SIGINT, &act, NULL);
3326 sigaction(SIGHUP, &act, NULL);
3327 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02003328
3329 act.sa_handler = sigchld_handler;
3330 act.sa_flags = SA_NOCLDSTOP;
3331 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00003332}
3333
3334#endif
3335
Paul Brook5cea8592009-05-30 00:52:44 +01003336#ifdef _WIN32
3337/* Look for support files in the same directory as the executable. */
3338static char *find_datadir(const char *argv0)
3339{
3340 char *p;
3341 char buf[MAX_PATH];
3342 DWORD len;
3343
3344 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3345 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03003346 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01003347 }
3348
3349 buf[len] = 0;
3350 p = buf + len - 1;
3351 while (p != buf && *p != '\\')
3352 p--;
3353 *p = 0;
3354 if (access(buf, R_OK) == 0) {
3355 return qemu_strdup(buf);
3356 }
3357 return NULL;
3358}
3359#else /* !_WIN32 */
3360
3361/* Find a likely location for support files using the location of the binary.
3362 For installed binaries this will be "$bindir/../share/qemu". When
3363 running from the build tree this will be "$bindir/../pc-bios". */
3364#define SHARE_SUFFIX "/share/qemu"
3365#define BUILD_SUFFIX "/pc-bios"
3366static char *find_datadir(const char *argv0)
3367{
3368 char *dir;
3369 char *p = NULL;
3370 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01003371 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00003372 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01003373
3374#if defined(__linux__)
3375 {
3376 int len;
3377 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3378 if (len > 0) {
3379 buf[len] = 0;
3380 p = buf;
3381 }
3382 }
3383#elif defined(__FreeBSD__)
3384 {
3385 int len;
3386 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3387 if (len > 0) {
3388 buf[len] = 0;
3389 p = buf;
3390 }
3391 }
3392#endif
3393 /* If we don't have any way of figuring out the actual executable
3394 location then try argv[0]. */
3395 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02003396 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01003397 if (!p) {
3398 return NULL;
3399 }
3400 }
3401 dir = dirname(p);
3402 dir = dirname(dir);
3403
Blue Swirl3a417592009-06-09 19:12:21 +00003404 max_len = strlen(dir) +
3405 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3406 res = qemu_mallocz(max_len);
3407 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01003408 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00003409 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01003410 if (access(res, R_OK)) {
3411 qemu_free(res);
3412 res = NULL;
3413 }
3414 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02003415
Paul Brook5cea8592009-05-30 00:52:44 +01003416 return res;
3417}
3418#undef SHARE_SUFFIX
3419#undef BUILD_SUFFIX
3420#endif
3421
3422char *qemu_find_file(int type, const char *name)
3423{
3424 int len;
3425 const char *subdir;
3426 char *buf;
3427
3428 /* If name contains path separators then try it as a straight path. */
3429 if ((strchr(name, '/') || strchr(name, '\\'))
3430 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02003431 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01003432 }
3433 switch (type) {
3434 case QEMU_FILE_TYPE_BIOS:
3435 subdir = "";
3436 break;
3437 case QEMU_FILE_TYPE_KEYMAP:
3438 subdir = "keymaps/";
3439 break;
3440 default:
3441 abort();
3442 }
3443 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3444 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00003445 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01003446 if (access(buf, R_OK)) {
3447 qemu_free(buf);
3448 return NULL;
3449 }
3450 return buf;
3451}
3452
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003453static int device_help_func(QemuOpts *opts, void *opaque)
3454{
3455 return qdev_device_help(opts);
3456}
3457
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003458static int device_init_func(QemuOpts *opts, void *opaque)
3459{
3460 DeviceState *dev;
3461
3462 dev = qdev_device_add(opts);
3463 if (!dev)
3464 return -1;
3465 return 0;
3466}
3467
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003468static int chardev_init_func(QemuOpts *opts, void *opaque)
3469{
3470 CharDriverState *chr;
3471
3472 chr = qemu_chr_open_opts(opts, NULL);
3473 if (!chr)
3474 return -1;
3475 return 0;
3476}
3477
Gerd Hoffmann88589342009-12-08 13:11:50 +01003478static int mon_init_func(QemuOpts *opts, void *opaque)
3479{
3480 CharDriverState *chr;
3481 const char *chardev;
3482 const char *mode;
3483 int flags;
3484
3485 mode = qemu_opt_get(opts, "mode");
3486 if (mode == NULL) {
3487 mode = "readline";
3488 }
3489 if (strcmp(mode, "readline") == 0) {
3490 flags = MONITOR_USE_READLINE;
3491 } else if (strcmp(mode, "control") == 0) {
3492 flags = MONITOR_USE_CONTROL;
3493 } else {
3494 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
3495 exit(1);
3496 }
3497
3498 if (qemu_opt_get_bool(opts, "default", 0))
3499 flags |= MONITOR_IS_DEFAULT;
3500
3501 chardev = qemu_opt_get(opts, "chardev");
3502 chr = qemu_chr_find(chardev);
3503 if (chr == NULL) {
3504 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
3505 exit(1);
3506 }
3507
3508 monitor_init(chr, flags);
3509 return 0;
3510}
3511
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003512static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01003513{
3514 static int monitor_device_index = 0;
3515 QemuOpts *opts;
3516 const char *p;
3517 char label[32];
3518 int def = 0;
3519
3520 if (strstart(optarg, "chardev:", &p)) {
3521 snprintf(label, sizeof(label), "%s", p);
3522 } else {
3523 if (monitor_device_index) {
3524 snprintf(label, sizeof(label), "monitor%d",
3525 monitor_device_index);
3526 } else {
3527 snprintf(label, sizeof(label), "monitor");
3528 def = 1;
3529 }
3530 opts = qemu_chr_parse_compat(label, optarg);
3531 if (!opts) {
3532 fprintf(stderr, "parse error: %s\n", optarg);
3533 exit(1);
3534 }
3535 }
3536
3537 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
3538 if (!opts) {
3539 fprintf(stderr, "duplicate chardev: %s\n", label);
3540 exit(1);
3541 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003542 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01003543 qemu_opt_set(opts, "chardev", label);
3544 if (def)
3545 qemu_opt_set(opts, "default", "on");
3546 monitor_device_index++;
3547}
3548
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003549struct device_config {
3550 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003551 DEV_USB, /* -usbdevice */
3552 DEV_BT, /* -bt */
3553 DEV_SERIAL, /* -serial */
3554 DEV_PARALLEL, /* -parallel */
3555 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003556 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003557 } type;
3558 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003559 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003560};
Blue Swirl72cf2d42009-09-12 07:36:22 +00003561QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003562
3563static void add_device_config(int type, const char *cmdline)
3564{
3565 struct device_config *conf;
3566
3567 conf = qemu_mallocz(sizeof(*conf));
3568 conf->type = type;
3569 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003570 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003571}
3572
3573static int foreach_device_config(int type, int (*func)(const char *cmdline))
3574{
3575 struct device_config *conf;
3576 int rc;
3577
Blue Swirl72cf2d42009-09-12 07:36:22 +00003578 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003579 if (conf->type != type)
3580 continue;
3581 rc = func(conf->cmdline);
3582 if (0 != rc)
3583 return rc;
3584 }
3585 return 0;
3586}
3587
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003588static int serial_parse(const char *devname)
3589{
3590 static int index = 0;
3591 char label[32];
3592
3593 if (strcmp(devname, "none") == 0)
3594 return 0;
3595 if (index == MAX_SERIAL_PORTS) {
3596 fprintf(stderr, "qemu: too many serial ports\n");
3597 exit(1);
3598 }
3599 snprintf(label, sizeof(label), "serial%d", index);
3600 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3601 if (!serial_hds[index]) {
3602 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
3603 devname, strerror(errno));
3604 return -1;
3605 }
3606 index++;
3607 return 0;
3608}
3609
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003610static int parallel_parse(const char *devname)
3611{
3612 static int index = 0;
3613 char label[32];
3614
3615 if (strcmp(devname, "none") == 0)
3616 return 0;
3617 if (index == MAX_PARALLEL_PORTS) {
3618 fprintf(stderr, "qemu: too many parallel ports\n");
3619 exit(1);
3620 }
3621 snprintf(label, sizeof(label), "parallel%d", index);
3622 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
3623 if (!parallel_hds[index]) {
3624 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
3625 devname, strerror(errno));
3626 return -1;
3627 }
3628 index++;
3629 return 0;
3630}
3631
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003632static int virtcon_parse(const char *devname)
3633{
3634 static int index = 0;
3635 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05303636 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003637
3638 if (strcmp(devname, "none") == 0)
3639 return 0;
3640 if (index == MAX_VIRTIO_CONSOLES) {
3641 fprintf(stderr, "qemu: too many virtio consoles\n");
3642 exit(1);
3643 }
Amit Shah392ecf52010-01-21 16:19:23 +05303644
3645 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
3646 qemu_opt_set(bus_opts, "driver", "virtio-serial");
3647
3648 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
3649 qemu_opt_set(dev_opts, "driver", "virtconsole");
3650
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003651 snprintf(label, sizeof(label), "virtcon%d", index);
3652 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
3653 if (!virtcon_hds[index]) {
3654 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
3655 devname, strerror(errno));
3656 return -1;
3657 }
Amit Shah392ecf52010-01-21 16:19:23 +05303658 qemu_opt_set(dev_opts, "chardev", label);
3659
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003660 index++;
3661 return 0;
3662}
3663
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003664static int debugcon_parse(const char *devname)
3665{
3666 QemuOpts *opts;
3667
3668 if (!qemu_chr_open("debugcon", devname, NULL)) {
3669 exit(1);
3670 }
3671 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
3672 if (!opts) {
3673 fprintf(stderr, "qemu: already have a debugcon device\n");
3674 exit(1);
3675 }
3676 qemu_opt_set(opts, "driver", "isa-debugcon");
3677 qemu_opt_set(opts, "chardev", "debugcon");
3678 return 0;
3679}
3680
Anthony Liguori6530a972010-01-22 09:18:06 -06003681static const QEMUOption *lookup_opt(int argc, char **argv,
3682 const char **poptarg, int *poptind)
3683{
3684 const QEMUOption *popt;
3685 int optind = *poptind;
3686 char *r = argv[optind];
3687 const char *optarg;
3688
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003689 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06003690 optind++;
3691 /* Treat --foo the same as -foo. */
3692 if (r[1] == '-')
3693 r++;
3694 popt = qemu_options;
3695 for(;;) {
3696 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003697 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06003698 exit(1);
3699 }
3700 if (!strcmp(popt->name, r + 1))
3701 break;
3702 popt++;
3703 }
3704 if (popt->flags & HAS_ARG) {
3705 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003706 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06003707 exit(1);
3708 }
3709 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003710 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06003711 } else {
3712 optarg = NULL;
3713 }
3714
3715 *poptarg = optarg;
3716 *poptind = optind;
3717
3718 return popt;
3719}
3720
malc902b3d52008-12-10 19:18:40 +00003721int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00003722{
aliguori59030a82009-04-05 18:43:41 +00003723 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00003724 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00003725 int i;
j_mayer28c5af52007-11-11 01:50:45 +00003726 int snapshot, linux_boot, net_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003727 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00003728 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00003729 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05003730 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00003731 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00003732 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00003733 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003734 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00003735 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06003736 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00003737 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003738 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00003739 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00003740#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003741 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00003742#endif
bellard26a5f132008-05-28 12:30:31 +00003743 int tb_size;
ths93815bc2007-03-19 15:58:31 +00003744 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00003745 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00003746#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00003747 int fd = 0;
3748 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00003749 const char *chroot_dir = NULL;
3750 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00003751#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003752 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06003753 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00003754
Markus Armbruster65abca02010-02-24 14:37:14 +01003755 error_set_progname(argv[0]);
3756
Jan Kiszka68752042009-09-15 13:36:04 +02003757 init_clocks();
3758
malc902b3d52008-12-10 19:18:40 +00003759 qemu_cache_utils_init(envp);
3760
Blue Swirl72cf2d42009-09-12 07:36:22 +00003761 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00003762#ifndef _WIN32
3763 {
3764 struct sigaction act;
3765 sigfillset(&act.sa_mask);
3766 act.sa_flags = 0;
3767 act.sa_handler = SIG_IGN;
3768 sigaction(SIGPIPE, &act, NULL);
3769 }
bellard3587d7e2006-06-26 20:03:44 +00003770#else
3771 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00003772 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3773 QEMU to run on a single CPU */
3774 {
3775 HANDLE h;
3776 DWORD mask, smask;
3777 int i;
3778 h = GetCurrentProcess();
3779 if (GetProcessAffinityMask(h, &mask, &smask)) {
3780 for(i = 0; i < 32; i++) {
3781 if (mask & (1 << i))
3782 break;
3783 }
3784 if (i != 32) {
3785 mask = 1 << i;
3786 SetProcessAffinityMask(h, mask);
3787 }
3788 }
3789 }
bellard67b915a2004-03-31 23:37:16 +00003790#endif
bellardbe995c22006-06-25 16:25:21 +00003791
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05003792 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05003793 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00003794 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00003795 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00003796 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00003797 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00003798 kernel_filename = NULL;
3799 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00003800 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00003801 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00003802
aliguori268a3622009-04-21 22:30:27 +00003803 for (i = 0; i < MAX_NODES; i++) {
3804 node_mem[i] = 0;
3805 node_cpumask[i] = 0;
3806 }
3807
aliguori268a3622009-04-21 22:30:27 +00003808 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00003809 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00003810
bellard26a5f132008-05-28 12:30:31 +00003811 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00003812 autostart= 1;
3813
Anthony Liguori292444c2010-01-21 10:57:58 -06003814 /* first pass of option parsing */
3815 optind = 1;
3816 while (optind < argc) {
3817 if (argv[optind][0] != '-') {
3818 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06003819 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06003820 continue;
3821 } else {
3822 const QEMUOption *popt;
3823
3824 popt = lookup_opt(argc, argv, &optarg, &optind);
3825 switch (popt->index) {
3826 case QEMU_OPTION_nodefconfig:
3827 defconfig=0;
3828 break;
3829 }
3830 }
3831 }
3832
3833 if (defconfig) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003834 const char *fname;
Anthony Liguori292444c2010-01-21 10:57:58 -06003835 FILE *fp;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003836
3837 fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
3838 fp = fopen(fname, "r");
Anthony Liguori292444c2010-01-21 10:57:58 -06003839 if (fp) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003840 if (qemu_config_parse(fp, fname) != 0) {
Anthony Liguori292444c2010-01-21 10:57:58 -06003841 exit(1);
3842 }
3843 fclose(fp);
3844 }
3845
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003846 fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
3847 fp = fopen(fname, "r");
Anthony Liguori292444c2010-01-21 10:57:58 -06003848 if (fp) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003849 if (qemu_config_parse(fp, fname) != 0) {
Anthony Liguori292444c2010-01-21 10:57:58 -06003850 exit(1);
3851 }
3852 fclose(fp);
3853 }
3854 }
john cooperb5ec5ce2010-02-20 11:14:59 -06003855#if defined(cpudef_setup)
3856 cpudef_setup(); /* parse cpu definitions in target config file */
3857#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06003858
3859 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00003860 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00003861 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00003862 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00003863 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06003864 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003865 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00003866 } else {
3867 const QEMUOption *popt;
3868
Anthony Liguori6530a972010-01-22 09:18:06 -06003869 popt = lookup_opt(argc, argv, &optarg, &optind);
bellardcd6f1162004-05-13 22:02:20 +00003870 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00003871 case QEMU_OPTION_M:
3872 machine = find_machine(optarg);
3873 if (!machine) {
3874 QEMUMachine *m;
3875 printf("Supported machines are:\n");
3876 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003877 if (m->alias)
3878 printf("%-10s %s (alias of %s)\n",
3879 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00003880 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00003881 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05003882 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00003883 }
ths15f82202007-06-29 23:26:08 +00003884 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00003885 }
3886 break;
j_mayer94fc95c2007-03-05 19:44:02 +00003887 case QEMU_OPTION_cpu:
3888 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00003889 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00003890/* XXX: implement xxx_cpu_list for targets that still miss it */
john cooperb5ec5ce2010-02-20 11:14:59 -06003891#if defined(cpu_list_id)
3892 cpu_list_id(stdout, &fprintf, optarg);
3893#elif defined(cpu_list)
3894 cpu_list(stdout, &fprintf); /* deprecated */
j_mayer94fc95c2007-03-05 19:44:02 +00003895#endif
ths15f82202007-06-29 23:26:08 +00003896 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00003897 } else {
3898 cpu_model = optarg;
3899 }
3900 break;
bellardcd6f1162004-05-13 22:02:20 +00003901 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00003902 initrd_filename = optarg;
3903 break;
bellardcd6f1162004-05-13 22:02:20 +00003904 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00003905 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003906 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00003907 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003908 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00003909 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00003910 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00003911 translation == BIOS_ATA_TRANSLATION_LBA ?
3912 ",trans=lba" :
3913 translation == BIOS_ATA_TRANSLATION_NONE ?
3914 ",trans=none" : "");
3915 break;
bellardcd6f1162004-05-13 22:02:20 +00003916 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003917 case QEMU_OPTION_hdc:
3918 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00003919 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00003920 break;
thse4bcb142007-12-02 04:51:10 +00003921 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00003922 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00003923 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02003924 case QEMU_OPTION_set:
3925 if (qemu_set_option(optarg) != 0)
3926 exit(1);
3927 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003928 case QEMU_OPTION_global:
3929 if (qemu_global_option(optarg) != 0)
3930 exit(1);
3931 break;
balrog3e3d5812007-04-30 02:09:25 +00003932 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00003933 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00003934 break;
pbrooka1bb27b2007-04-06 16:49:48 +00003935 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00003936 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00003937 break;
j_mayer86f55662007-04-24 06:52:59 +00003938 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00003939 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00003940 break;
bellardcd6f1162004-05-13 22:02:20 +00003941 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003942 snapshot = 1;
3943 break;
bellardcd6f1162004-05-13 22:02:20 +00003944 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003945 {
bellard330d0412003-07-26 18:11:40 +00003946 const char *p;
3947 p = optarg;
3948 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003949 if (cyls < 1 || cyls > 16383)
3950 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003951 if (*p != ',')
3952 goto chs_fail;
3953 p++;
3954 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003955 if (heads < 1 || heads > 16)
3956 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003957 if (*p != ',')
3958 goto chs_fail;
3959 p++;
3960 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003961 if (secs < 1 || secs > 63)
3962 goto chs_fail;
3963 if (*p == ',') {
3964 p++;
3965 if (!strcmp(p, "none"))
3966 translation = BIOS_ATA_TRANSLATION_NONE;
3967 else if (!strcmp(p, "lba"))
3968 translation = BIOS_ATA_TRANSLATION_LBA;
3969 else if (!strcmp(p, "auto"))
3970 translation = BIOS_ATA_TRANSLATION_AUTO;
3971 else
3972 goto chs_fail;
3973 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003974 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003975 fprintf(stderr, "qemu: invalid physical CHS format\n");
3976 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003977 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003978 if (hda_opts != NULL) {
3979 char num[16];
3980 snprintf(num, sizeof(num), "%d", cyls);
3981 qemu_opt_set(hda_opts, "cyls", num);
3982 snprintf(num, sizeof(num), "%d", heads);
3983 qemu_opt_set(hda_opts, "heads", num);
3984 snprintf(num, sizeof(num), "%d", secs);
3985 qemu_opt_set(hda_opts, "secs", num);
3986 if (translation == BIOS_ATA_TRANSLATION_LBA)
3987 qemu_opt_set(hda_opts, "trans", "lba");
3988 if (translation == BIOS_ATA_TRANSLATION_NONE)
3989 qemu_opt_set(hda_opts, "trans", "none");
3990 }
bellard330d0412003-07-26 18:11:40 +00003991 }
3992 break;
aliguori268a3622009-04-21 22:30:27 +00003993 case QEMU_OPTION_numa:
3994 if (nb_numa_nodes >= MAX_NODES) {
3995 fprintf(stderr, "qemu: too many NUMA nodes\n");
3996 exit(1);
3997 }
3998 numa_add(optarg);
3999 break;
bellardcd6f1162004-05-13 22:02:20 +00004000 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004001 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00004002 break;
balrog4d3b6f62008-02-10 16:33:14 +00004003#ifdef CONFIG_CURSES
4004 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004005 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00004006 break;
4007#endif
balroga171fe32007-04-30 01:48:07 +00004008 case QEMU_OPTION_portrait:
4009 graphic_rotate = 1;
4010 break;
bellardcd6f1162004-05-13 22:02:20 +00004011 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004012 kernel_filename = optarg;
4013 break;
bellardcd6f1162004-05-13 22:02:20 +00004014 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004015 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004016 break;
bellardcd6f1162004-05-13 22:02:20 +00004017 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004018 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004019 break;
bellardcd6f1162004-05-13 22:02:20 +00004020 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004021 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004022 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02004023 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004024 };
4025 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004026 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004027 int legacy = 0;
4028
4029 if (!strchr(optarg, '=')) {
4030 legacy = 1;
4031 pstrcpy(buf, sizeof(buf), optarg);
4032 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
4033 fprintf(stderr,
4034 "qemu: unknown boot parameter '%s' in '%s'\n",
4035 buf, optarg);
4036 exit(1);
4037 }
4038
4039 if (legacy ||
4040 get_param_value(buf, sizeof(buf), "order", optarg)) {
4041 boot_devices_bitmap = parse_bootdevices(buf);
4042 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00004043 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004044 if (!legacy) {
4045 if (get_param_value(buf, sizeof(buf),
4046 "once", optarg)) {
4047 boot_devices_bitmap |= parse_bootdevices(buf);
4048 standard_boot_devices = qemu_strdup(boot_devices);
4049 pstrcpy(boot_devices, sizeof(boot_devices), buf);
4050 qemu_register_reset(restore_boot_devices,
4051 standard_boot_devices);
4052 }
Jan Kiszka95387492009-07-02 00:19:02 +02004053 if (get_param_value(buf, sizeof(buf),
4054 "menu", optarg)) {
4055 if (!strcmp(buf, "on")) {
4056 boot_menu = 1;
4057 } else if (!strcmp(buf, "off")) {
4058 boot_menu = 0;
4059 } else {
4060 fprintf(stderr,
4061 "qemu: invalid option value '%s'\n",
4062 buf);
4063 exit(1);
4064 }
4065 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004066 }
bellard36b486b2003-11-11 13:36:08 +00004067 }
4068 break;
bellardcd6f1162004-05-13 22:02:20 +00004069 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004070 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004071 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004072 break;
bellard52ca8d62006-06-14 16:03:05 +00004073#ifdef TARGET_I386
4074 case QEMU_OPTION_no_fd_bootchk:
4075 fd_bootchk = 0;
4076 break;
4077#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01004078 case QEMU_OPTION_netdev:
4079 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
4080 exit(1);
4081 }
4082 break;
bellard7c9d8e02005-11-15 22:16:05 +00004083 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01004084 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00004085 exit(1);
4086 }
bellard702c6512004-04-02 21:21:32 +00004087 break;
bellardc7f74642004-08-24 21:57:12 +00004088#ifdef CONFIG_SLIRP
4089 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02004090 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004091 break;
ths47d5d012007-02-20 00:05:08 +00004092 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02004093 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00004094 break;
bellardc94c8d62004-09-13 21:37:34 +00004095#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004096 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01004097 if (net_slirp_smb(optarg) < 0)
4098 exit(1);
bellard9d728e82004-09-05 23:09:03 +00004099 break;
bellardc94c8d62004-09-13 21:37:34 +00004100#endif
bellard9bf05442004-08-25 22:12:49 +00004101 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01004102 if (net_slirp_redir(optarg) < 0)
4103 exit(1);
bellard9bf05442004-08-25 22:12:49 +00004104 break;
bellardc7f74642004-08-24 21:57:12 +00004105#endif
balrogdc72ac12008-11-09 00:04:26 +00004106 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004107 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00004108 break;
bellard1d14ffa2005-10-30 18:58:22 +00004109#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004110 case QEMU_OPTION_audio_help:
4111 AUD_help ();
4112 exit (0);
4113 break;
4114 case QEMU_OPTION_soundhw:
4115 select_soundhw (optarg);
4116 break;
4117#endif
bellardcd6f1162004-05-13 22:02:20 +00004118 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004119 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004120 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00004121 case QEMU_OPTION_version:
4122 version();
4123 exit(0);
4124 break;
aurel3200f82b82008-04-27 21:12:55 +00004125 case QEMU_OPTION_m: {
4126 uint64_t value;
4127 char *ptr;
4128
4129 value = strtoul(optarg, &ptr, 10);
4130 switch (*ptr) {
4131 case 0: case 'M': case 'm':
4132 value <<= 20;
4133 break;
4134 case 'G': case 'g':
4135 value <<= 30;
4136 break;
4137 default:
4138 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004139 exit(1);
4140 }
aurel3200f82b82008-04-27 21:12:55 +00004141
4142 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05004143 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00004144 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4145 exit(1);
4146 }
Anthony Liguoric227f092009-10-01 16:12:16 -05004147 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00004148 fprintf(stderr, "qemu: ram size too large\n");
4149 exit(1);
4150 }
4151 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004152 break;
aurel3200f82b82008-04-27 21:12:55 +00004153 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03004154 case QEMU_OPTION_mempath:
4155 mem_path = optarg;
4156 break;
4157#ifdef MAP_POPULATE
4158 case QEMU_OPTION_mem_prealloc:
4159 mem_prealloc = 1;
4160 break;
4161#endif
bellardcd6f1162004-05-13 22:02:20 +00004162 case QEMU_OPTION_d:
4163 {
4164 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004165 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004166
bellardcd6f1162004-05-13 22:02:20 +00004167 mask = cpu_str_to_log_mask(optarg);
4168 if (!mask) {
4169 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004170 for(item = cpu_log_items; item->mask != 0; item++) {
4171 printf("%-10s %s\n", item->name, item->help);
4172 }
4173 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004174 }
4175 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004176 }
bellardcd6f1162004-05-13 22:02:20 +00004177 break;
bellardcd6f1162004-05-13 22:02:20 +00004178 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00004179 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00004180 break;
aliguori59030a82009-04-05 18:43:41 +00004181 case QEMU_OPTION_gdb:
4182 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004183 break;
bellardcd6f1162004-05-13 22:02:20 +00004184 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01004185 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004186 break;
j_mayer1192dad2007-10-05 13:08:35 +00004187 case QEMU_OPTION_bios:
4188 bios_name = optarg;
4189 break;
aurel321b530a62009-04-05 20:08:59 +00004190 case QEMU_OPTION_singlestep:
4191 singlestep = 1;
4192 break;
bellardcd6f1162004-05-13 22:02:20 +00004193 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004194 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004195 break;
bellard3d11d0e2004-12-12 16:56:30 +00004196 case QEMU_OPTION_k:
4197 keyboard_layout = optarg;
4198 break;
bellardee22c2f2004-06-03 12:49:50 +00004199 case QEMU_OPTION_localtime:
4200 rtc_utc = 0;
4201 break;
malc3893c122008-09-28 00:42:05 +00004202 case QEMU_OPTION_vga:
4203 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004204 break;
blueswir15824d652009-03-28 06:44:27 +00004205#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004206 case QEMU_OPTION_g:
4207 {
4208 const char *p;
4209 int w, h, depth;
4210 p = optarg;
4211 w = strtol(p, (char **)&p, 10);
4212 if (w <= 0) {
4213 graphic_error:
4214 fprintf(stderr, "qemu: invalid resolution or depth\n");
4215 exit(1);
4216 }
4217 if (*p != 'x')
4218 goto graphic_error;
4219 p++;
4220 h = strtol(p, (char **)&p, 10);
4221 if (h <= 0)
4222 goto graphic_error;
4223 if (*p == 'x') {
4224 p++;
4225 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004226 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004227 depth != 24 && depth != 32)
4228 goto graphic_error;
4229 } else if (*p == '\0') {
4230 depth = graphic_depth;
4231 } else {
4232 goto graphic_error;
4233 }
ths3b46e622007-09-17 08:09:54 +00004234
bellarde9b137c2004-06-21 16:46:10 +00004235 graphic_width = w;
4236 graphic_height = h;
4237 graphic_depth = depth;
4238 }
4239 break;
blueswir15824d652009-03-28 06:44:27 +00004240#endif
ths20d8a3e2007-02-18 17:04:49 +00004241 case QEMU_OPTION_echr:
4242 {
4243 char *r;
4244 term_escape_char = strtol(optarg, &r, 0);
4245 if (r == optarg)
4246 printf("Bad argument to echr\n");
4247 break;
4248 }
bellard82c643f2004-07-14 17:28:13 +00004249 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004250 monitor_parse(optarg, "readline");
4251 default_monitor = 0;
4252 break;
4253 case QEMU_OPTION_qmp:
4254 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05004255 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00004256 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01004257 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01004258 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01004259 if (!opts) {
4260 fprintf(stderr, "parse error: %s\n", optarg);
4261 exit(1);
4262 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05004263 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01004264 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02004265 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01004266 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02004267 if (!opts) {
4268 fprintf(stderr, "parse error: %s\n", optarg);
4269 exit(1);
4270 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02004271 break;
bellard82c643f2004-07-14 17:28:13 +00004272 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004273 add_device_config(DEV_SERIAL, optarg);
4274 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01004275 if (strncmp(optarg, "mon:", 4) == 0) {
4276 default_monitor = 0;
4277 }
bellard82c643f2004-07-14 17:28:13 +00004278 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004279 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02004280 if (watchdog) {
4281 fprintf(stderr,
4282 "qemu: only one watchdog option may be given\n");
4283 return 1;
4284 }
4285 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004286 break;
4287 case QEMU_OPTION_watchdog_action:
4288 if (select_watchdog_action(optarg) == -1) {
4289 fprintf(stderr, "Unknown -watchdog-action parameter\n");
4290 exit(1);
4291 }
4292 break;
aliguori51ecf132009-01-15 20:06:40 +00004293 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004294 add_device_config(DEV_VIRTCON, optarg);
4295 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01004296 if (strncmp(optarg, "mon:", 4) == 0) {
4297 default_monitor = 0;
4298 }
aliguori51ecf132009-01-15 20:06:40 +00004299 break;
bellard6508fe52005-01-15 12:02:56 +00004300 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004301 add_device_config(DEV_PARALLEL, optarg);
4302 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01004303 if (strncmp(optarg, "mon:", 4) == 0) {
4304 default_monitor = 0;
4305 }
bellard6508fe52005-01-15 12:02:56 +00004306 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004307 case QEMU_OPTION_debugcon:
4308 add_device_config(DEV_DEBUGCON, optarg);
4309 break;
bellardd63d3072004-10-03 13:29:03 +00004310 case QEMU_OPTION_loadvm:
4311 loadvm = optarg;
4312 break;
4313 case QEMU_OPTION_full_screen:
4314 full_screen = 1;
4315 break;
ths667acca2006-12-11 02:08:05 +00004316#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004317 case QEMU_OPTION_no_frame:
4318 no_frame = 1;
4319 break;
ths3780e192007-06-21 21:08:02 +00004320 case QEMU_OPTION_alt_grab:
4321 alt_grab = 1;
4322 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05004323 case QEMU_OPTION_ctrl_grab:
4324 ctrl_grab = 1;
4325 break;
ths667acca2006-12-11 02:08:05 +00004326 case QEMU_OPTION_no_quit:
4327 no_quit = 1;
4328 break;
aliguori7d957bd2009-01-15 22:14:11 +00004329 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004330 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00004331 break;
ths667acca2006-12-11 02:08:05 +00004332#endif
bellardf7cce892004-12-08 22:21:25 +00004333 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004334 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004335 break;
bellarda09db212005-04-30 16:10:35 +00004336#ifdef TARGET_I386
4337 case QEMU_OPTION_win2k_hack:
4338 win2k_install_hack = 1;
4339 break;
aliguori73822ec2009-01-15 20:11:34 +00004340 case QEMU_OPTION_rtc_td_hack:
4341 rtc_td_hack = 1;
4342 break;
aliguori8a92ea22009-02-27 20:12:36 +00004343 case QEMU_OPTION_acpitable:
4344 if(acpi_table_add(optarg) < 0) {
4345 fprintf(stderr, "Wrong acpi table provided\n");
4346 exit(1);
4347 }
4348 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00004349 case QEMU_OPTION_smbios:
4350 if(smbios_entry_add(optarg) < 0) {
4351 fprintf(stderr, "Wrong smbios provided\n");
4352 exit(1);
4353 }
4354 break;
bellarda09db212005-04-30 16:10:35 +00004355#endif
aliguori7ba1e612008-11-05 16:04:33 +00004356#ifdef CONFIG_KVM
4357 case QEMU_OPTION_enable_kvm:
4358 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00004359 break;
4360#endif
bellardbb36d472005-11-05 14:22:28 +00004361 case QEMU_OPTION_usb:
4362 usb_enabled = 1;
4363 break;
bellarda594cfb2005-11-06 16:13:29 +00004364 case QEMU_OPTION_usbdevice:
4365 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004366 add_device_config(DEV_USB, optarg);
4367 break;
4368 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01004369 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004370 exit(1);
4371 }
bellarda594cfb2005-11-06 16:13:29 +00004372 break;
bellard6a00d602005-11-21 23:25:50 +00004373 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02004374 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00004375 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00004376 fprintf(stderr, "Invalid number of CPUs\n");
4377 exit(1);
4378 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02004379 if (max_cpus < smp_cpus) {
4380 fprintf(stderr, "maxcpus must be equal to or greater than "
4381 "smp\n");
4382 exit(1);
4383 }
4384 if (max_cpus > 255) {
4385 fprintf(stderr, "Unsupported number of maxcpus\n");
4386 exit(1);
4387 }
bellard6a00d602005-11-21 23:25:50 +00004388 break;
bellard24236862006-04-30 21:28:36 +00004389 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004390 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00004391 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00004392 break;
blueswir15824d652009-03-28 06:44:27 +00004393#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004394 case QEMU_OPTION_no_acpi:
4395 acpi_enabled = 0;
4396 break;
aliguori16b29ae2008-12-17 23:28:44 +00004397 case QEMU_OPTION_no_hpet:
4398 no_hpet = 1;
4399 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004400 case QEMU_OPTION_balloon:
4401 if (balloon_parse(optarg) < 0) {
4402 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
4403 exit(1);
4404 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03004405 break;
blueswir15824d652009-03-28 06:44:27 +00004406#endif
bellardd1beab82006-10-02 19:44:22 +00004407 case QEMU_OPTION_no_reboot:
4408 no_reboot = 1;
4409 break;
aurel32b2f76162008-04-11 21:35:52 +00004410 case QEMU_OPTION_no_shutdown:
4411 no_shutdown = 1;
4412 break;
balrog9467cd42007-05-01 01:34:14 +00004413 case QEMU_OPTION_show_cursor:
4414 cursor_hide = 0;
4415 break;
blueswir18fcb1b92008-09-18 18:29:08 +00004416 case QEMU_OPTION_uuid:
4417 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4418 fprintf(stderr, "Fail to parse UUID string."
4419 " Wrong format.\n");
4420 exit(1);
4421 }
4422 break;
blueswir15824d652009-03-28 06:44:27 +00004423#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004424 case QEMU_OPTION_daemonize:
4425 daemonize = 1;
4426 break;
blueswir15824d652009-03-28 06:44:27 +00004427#endif
ths9ae02552007-01-05 17:39:04 +00004428 case QEMU_OPTION_option_rom:
4429 if (nb_option_roms >= MAX_OPTION_ROMS) {
4430 fprintf(stderr, "Too many option ROMs\n");
4431 exit(1);
4432 }
4433 option_rom[nb_option_roms] = optarg;
4434 nb_option_roms++;
4435 break;
blueswir15824d652009-03-28 06:44:27 +00004436#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004437 case QEMU_OPTION_semihosting:
4438 semihosting_enabled = 1;
4439 break;
blueswir15824d652009-03-28 06:44:27 +00004440#endif
thsc35734b2007-03-19 15:17:08 +00004441 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02004442 qemu_name = qemu_strdup(optarg);
4443 {
4444 char *p = strchr(qemu_name, ',');
4445 if (p != NULL) {
4446 *p++ = 0;
4447 if (strncmp(p, "process=", 8)) {
4448 fprintf(stderr, "Unknown subargument %s to -name", p);
4449 exit(1);
4450 }
4451 p += 8;
4452 set_proc_name(p);
4453 }
4454 }
thsc35734b2007-03-19 15:17:08 +00004455 break;
blueswir195efd112008-12-24 20:26:14 +00004456#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004457 case QEMU_OPTION_prom_env:
4458 if (nb_prom_envs >= MAX_PROM_ENVS) {
4459 fprintf(stderr, "Too many prom variables\n");
4460 exit(1);
4461 }
4462 prom_envs[nb_prom_envs] = optarg;
4463 nb_prom_envs++;
4464 break;
4465#endif
balrog2b8f2d42007-07-27 22:08:46 +00004466#ifdef TARGET_ARM
4467 case QEMU_OPTION_old_param:
4468 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00004469 break;
balrog2b8f2d42007-07-27 22:08:46 +00004470#endif
thsf3dcfad2007-08-24 01:26:02 +00004471 case QEMU_OPTION_clock:
4472 configure_alarms(optarg);
4473 break;
bellard7e0af5d02007-11-07 16:24:33 +00004474 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02004475 configure_rtc_date_offset(optarg, 1);
4476 break;
4477 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01004478 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02004479 if (!opts) {
4480 fprintf(stderr, "parse error: %s\n", optarg);
4481 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00004482 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02004483 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00004484 break;
bellard26a5f132008-05-28 12:30:31 +00004485 case QEMU_OPTION_tb_size:
4486 tb_size = strtol(optarg, NULL, 0);
4487 if (tb_size < 0)
4488 tb_size = 0;
4489 break;
pbrook2e70f6e2008-06-29 01:03:05 +00004490 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01004491 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00004492 break;
aliguori5bb79102008-10-13 03:12:02 +00004493 case QEMU_OPTION_incoming:
4494 incoming = optarg;
4495 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01004496 case QEMU_OPTION_nodefaults:
4497 default_serial = 0;
4498 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004499 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01004500 default_monitor = 0;
4501 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01004502 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004503 default_floppy = 0;
4504 default_cdrom = 0;
4505 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01004506 break;
blueswir15824d652009-03-28 06:44:27 +00004507#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00004508 case QEMU_OPTION_chroot:
4509 chroot_dir = optarg;
4510 break;
4511 case QEMU_OPTION_runas:
4512 run_as = optarg;
4513 break;
blueswir15824d652009-03-28 06:44:27 +00004514#endif
aliguorie37630c2009-04-22 15:19:10 +00004515#ifdef CONFIG_XEN
4516 case QEMU_OPTION_xen_domid:
4517 xen_domid = atoi(optarg);
4518 break;
4519 case QEMU_OPTION_xen_create:
4520 xen_mode = XEN_CREATE;
4521 break;
4522 case QEMU_OPTION_xen_attach:
4523 xen_mode = XEN_ATTACH;
4524 break;
4525#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02004526 case QEMU_OPTION_readconfig:
4527 {
4528 FILE *fp;
4529 fp = fopen(optarg, "r");
4530 if (fp == NULL) {
4531 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
4532 exit(1);
4533 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01004534 if (qemu_config_parse(fp, optarg) != 0) {
Gerd Hoffmann715a6642009-10-14 10:39:28 +02004535 exit(1);
4536 }
4537 fclose(fp);
4538 break;
4539 }
4540 case QEMU_OPTION_writeconfig:
4541 {
4542 FILE *fp;
4543 if (strcmp(optarg, "-") == 0) {
4544 fp = stdout;
4545 } else {
4546 fp = fopen(optarg, "w");
4547 if (fp == NULL) {
4548 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
4549 exit(1);
4550 }
4551 }
4552 qemu_config_write(fp);
4553 fclose(fp);
4554 break;
4555 }
bellardcd6f1162004-05-13 22:02:20 +00004556 }
bellard0824d6f2003-06-24 13:42:40 +00004557 }
4558 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01004559 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00004560
Paul Brook5cea8592009-05-30 00:52:44 +01004561 /* If no data_dir is specified then try to find it relative to the
4562 executable path. */
4563 if (!data_dir) {
4564 data_dir = find_datadir(argv[0]);
4565 }
4566 /* If all else fails use the install patch specified when building. */
4567 if (!data_dir) {
4568 data_dir = CONFIG_QEMU_SHAREDIR;
4569 }
4570
Jes Sorensen6be68d72009-07-23 17:03:42 +02004571 /*
4572 * Default to max_cpus = smp_cpus, in case the user doesn't
4573 * specify a max_cpus value.
4574 */
4575 if (!max_cpus)
4576 max_cpus = smp_cpus;
4577
balrog3d878ca2008-10-28 10:59:59 +00004578 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00004579 if (smp_cpus > machine->max_cpus) {
4580 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4581 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
4582 machine->max_cpus);
4583 exit(1);
4584 }
4585
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004586 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01004587 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004588
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004589 if (machine->no_serial) {
4590 default_serial = 0;
4591 }
4592 if (machine->no_parallel) {
4593 default_parallel = 0;
4594 }
4595 if (!machine->use_virtcon) {
4596 default_virtcon = 0;
4597 }
4598 if (machine->no_vga) {
4599 default_vga = 0;
4600 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004601 if (machine->no_floppy) {
4602 default_floppy = 0;
4603 }
4604 if (machine->no_cdrom) {
4605 default_cdrom = 0;
4606 }
4607 if (machine->no_sdcard) {
4608 default_sdcard = 0;
4609 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004610
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004611 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004612 if (default_parallel)
4613 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004614 if (default_serial && default_monitor) {
4615 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004616 } else if (default_virtcon && default_monitor) {
4617 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004618 } else {
4619 if (default_serial)
4620 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01004621 if (default_virtcon)
4622 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004623 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004624 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01004625 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004626 } else {
4627 if (default_serial)
4628 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004629 if (default_parallel)
4630 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004631 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004632 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01004633 if (default_virtcon)
4634 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00004635 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01004636 if (default_vga)
4637 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00004638
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004639 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
4640 exit(1);
4641
ths71e3ceb2006-12-22 02:11:31 +00004642#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004643 if (daemonize) {
4644 pid_t pid;
4645
4646 if (pipe(fds) == -1)
4647 exit(1);
4648
4649 pid = fork();
4650 if (pid > 0) {
4651 uint8_t status;
4652 ssize_t len;
4653
4654 close(fds[1]);
4655
4656 again:
ths93815bc2007-03-19 15:58:31 +00004657 len = read(fds[0], &status, 1);
4658 if (len == -1 && (errno == EINTR))
4659 goto again;
4660
4661 if (len != 1)
4662 exit(1);
4663 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05004664 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00004665 exit(1);
4666 } else
4667 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00004668 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00004669 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00004670
Kevin Wolf40ff6d72009-12-02 12:24:42 +01004671 close(fds[0]);
4672 qemu_set_cloexec(fds[1]);
4673
ths71e3ceb2006-12-22 02:11:31 +00004674 setsid();
4675
4676 pid = fork();
4677 if (pid > 0)
4678 exit(0);
4679 else if (pid < 0)
4680 exit(1);
4681
4682 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00004683
4684 signal(SIGTSTP, SIG_IGN);
4685 signal(SIGTTOU, SIG_IGN);
4686 signal(SIGTTIN, SIG_IGN);
4687 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02004688#endif
ths71e3ceb2006-12-22 02:11:31 +00004689
thsaa26bb22007-03-25 21:33:06 +00004690 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02004691#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00004692 if (daemonize) {
4693 uint8_t status = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01004694 if (write(fds[1], &status, 1) != 1) {
4695 perror("daemonize. Writing to pipe\n");
4696 }
ths93815bc2007-03-19 15:58:31 +00004697 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02004698#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05004699 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00004700 exit(1);
4701 }
4702
Marcelo Tosatti214910a2009-09-18 02:41:23 -03004703 if (kvm_enabled()) {
4704 int ret;
4705
4706 ret = kvm_init(smp_cpus);
4707 if (ret < 0) {
4708 fprintf(stderr, "failed to initialize KVM\n");
4709 exit(1);
4710 }
4711 }
4712
aliguori3fcf7b62009-04-24 18:03:25 +00004713 if (qemu_init_main_loop()) {
4714 fprintf(stderr, "qemu_init_main_loop failed\n");
4715 exit(1);
4716 }
bellarda20dd502003-09-30 21:07:02 +00004717 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00004718
thsf8d39c02008-07-03 10:01:15 +00004719 if (!linux_boot && *kernel_cmdline != '\0') {
4720 fprintf(stderr, "-append only allowed with -kernel option\n");
4721 exit(1);
4722 }
4723
4724 if (!linux_boot && initrd_filename != NULL) {
4725 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4726 exit(1);
4727 }
4728
Filip Navarabf65f532009-07-27 10:02:04 -05004729#ifndef _WIN32
4730 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00004731 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05004732#endif
ths3b46e622007-09-17 08:09:54 +00004733
aliguori7183b4b2008-11-05 20:40:18 +00004734 if (init_timer_alarm() < 0) {
4735 fprintf(stderr, "could not initialize alarm timer\n");
4736 exit(1);
4737 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01004738 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00004739
bellardfd1dff42006-02-01 21:29:26 +00004740#ifdef _WIN32
4741 socket_init();
4742#endif
4743
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01004744 if (net_init_clients() < 0) {
4745 exit(1);
bellard702c6512004-04-02 21:21:32 +00004746 }
bellardf1510b22003-06-25 00:07:40 +00004747
Glauber Costa406c8df2009-06-17 09:05:30 -04004748 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
4749 net_set_boot_mask(net_boot);
4750
balrogdc72ac12008-11-09 00:04:26 +00004751 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004752 if (foreach_device_config(DEV_BT, bt_parse))
4753 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00004754
bellard0824d6f2003-06-24 13:42:40 +00004755 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00004756 if (ram_size == 0)
4757 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00004758
bellard26a5f132008-05-28 12:30:31 +00004759 /* init the dynamic translator */
4760 cpu_exec_init_all(tb_size * 1024 * 1024);
4761
Markus Armbrustereb852012009-10-27 18:41:44 +01004762 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00004763
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004764 blk_mig_init();
4765
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004766 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01004767 /* we always create the cdrom drive, even if no disk is there */
4768 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004769 }
thse4bcb142007-12-02 04:51:10 +00004770
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004771 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01004772 /* we always create at least one floppy */
4773 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004774 }
bellardc4b1fcc2004-03-14 21:44:30 +00004775
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01004776 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01004777 /* we always create one sd slot, even if no card is in it */
4778 drive_add(NULL, SD_ALIAS);
4779 }
balrog9d413d12007-12-04 00:10:34 +00004780
ths96d30e42007-01-07 20:42:14 +00004781 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004782 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02004783 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
4784 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004785 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00004786
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02004787 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
4788 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00004789
aliguori268a3622009-04-21 22:30:27 +00004790 if (nb_numa_nodes > 0) {
4791 int i;
4792
4793 if (nb_numa_nodes > smp_cpus) {
4794 nb_numa_nodes = smp_cpus;
4795 }
4796
4797 /* If no memory size if given for any node, assume the default case
4798 * and distribute the available memory equally across all nodes
4799 */
4800 for (i = 0; i < nb_numa_nodes; i++) {
4801 if (node_mem[i] != 0)
4802 break;
4803 }
4804 if (i == nb_numa_nodes) {
4805 uint64_t usedmem = 0;
4806
4807 /* On Linux, the each node's border has to be 8MB aligned,
4808 * the final node gets the rest.
4809 */
4810 for (i = 0; i < nb_numa_nodes - 1; i++) {
4811 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4812 usedmem += node_mem[i];
4813 }
4814 node_mem[i] = ram_size - usedmem;
4815 }
4816
4817 for (i = 0; i < nb_numa_nodes; i++) {
4818 if (node_cpumask[i] != 0)
4819 break;
4820 }
4821 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4822 * must cope with this anyway, because there are BIOSes out there in
4823 * real machines which also use this scheme.
4824 */
4825 if (i == nb_numa_nodes) {
4826 for (i = 0; i < smp_cpus; i++) {
4827 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4828 }
4829 }
4830 }
4831
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004832 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4833 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004834 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4835 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004836 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4837 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004838 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4839 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00004840
Paul Brookaae94602009-05-14 22:35:06 +01004841 module_call_init(MODULE_INIT_DEVICE);
4842
Markus Armbrusterff952ba2010-01-29 19:48:57 +01004843 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
4844 exit(0);
4845
Markus Armbruster09aaa162009-08-21 10:31:34 +02004846 if (watchdog) {
4847 i = select_watchdog(watchdog);
4848 if (i > 0)
4849 exit (i == 1 ? 1 : 0);
4850 }
4851
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004852 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01004853 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02004854 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004855 qemu_add_globals();
4856
Paul Brookfbe1b592009-05-13 17:56:25 +01004857 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00004858 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
4859
Jan Kiszkaea375f92010-03-01 19:10:30 +01004860 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00004861
Juan Quintela67b3b712009-08-28 19:25:15 +02004862#ifndef _WIN32
4863 /* must be after terminal init, SDL library changes signal handlers */
4864 sighandler_setup();
4865#endif
4866
Blue Swirl87d0a282010-03-27 18:24:45 +00004867 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00004868
aliguori6f338c32009-02-11 15:21:54 +00004869 current_machine = machine;
4870
aliguori3023f332009-01-16 19:04:14 +00004871 /* init USB devices */
4872 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01004873 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4874 exit(1);
aliguori3023f332009-01-16 19:04:14 +00004875 }
4876
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004877 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004878 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004879 exit(1);
4880
Markus Armbruster668680f2010-02-11 14:44:58 +01004881 net_check_clients();
4882
aliguori3023f332009-01-16 19:04:14 +00004883 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01004884 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004885
4886 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004887#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004888 display_type = DT_SDL;
4889#else
4890 display_type = DT_VNC;
4891 vnc_display = "localhost:0,to=99";
4892 show_vnc_port = 1;
4893#endif
4894 }
4895
4896
4897 switch (display_type) {
4898 case DT_NOGRAPHIC:
4899 break;
4900#if defined(CONFIG_CURSES)
4901 case DT_CURSES:
4902 curses_display_init(ds, full_screen);
4903 break;
4904#endif
bellard5b0753e2005-03-01 21:37:28 +00004905#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004906 case DT_SDL:
4907 sdl_display_init(ds, full_screen, no_frame);
4908 break;
bellard5b0753e2005-03-01 21:37:28 +00004909#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004910 case DT_SDL:
4911 cocoa_display_init(ds, full_screen);
4912 break;
bellard313aa562003-08-10 21:52:11 +00004913#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004914 case DT_VNC:
4915 vnc_display_init(ds);
4916 if (vnc_display_open(ds, vnc_display) < 0)
4917 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004918
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004919 if (show_vnc_port) {
4920 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05004921 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004922 break;
4923 default:
4924 break;
bellard313aa562003-08-10 21:52:11 +00004925 }
aliguori7d957bd2009-01-15 22:14:11 +00004926 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00004927
aliguori3023f332009-01-16 19:04:14 +00004928 dcl = ds->listeners;
4929 while (dcl != NULL) {
4930 if (dcl->dpy_refresh != NULL) {
4931 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
4932 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00004933 }
aliguori3023f332009-01-16 19:04:14 +00004934 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00004935 }
aliguori3023f332009-01-16 19:04:14 +00004936
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004937 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00004938 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
4939 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
4940 }
4941
Paolo Bonzinib473df62010-02-11 00:29:55 +01004942 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00004943
Gerd Hoffmann88589342009-12-08 13:11:50 +01004944 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
4945 exit(1);
bellard82c643f2004-07-14 17:28:13 +00004946
aliguori59030a82009-04-05 18:43:41 +00004947 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
4948 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
4949 gdbstub_dev);
4950 exit(1);
balrog45669e02007-07-02 13:20:17 +00004951 }
balrog45669e02007-07-02 13:20:17 +00004952
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02004953 qdev_machine_creation_done();
4954
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01004955 if (rom_load_all() != 0) {
4956 fprintf(stderr, "rom loading failed\n");
4957 exit(1);
4958 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02004959
Juan Quintela504c2942009-11-12 00:39:13 +01004960 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02004961 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01004962 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02004963 autostart = 0;
4964 }
4965 }
bellardd63d3072004-10-03 13:29:03 +00004966
Glauber Costa2bb8c102009-07-24 16:20:23 -04004967 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00004968 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03004969 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00004970 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03004971 }
thsffd843b2006-12-21 19:46:43 +00004972
blueswir1b9e82a52009-04-05 18:03:31 +00004973#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004974 if (daemonize) {
4975 uint8_t status = 0;
4976 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00004977
4978 again1:
4979 len = write(fds[1], &status, 1);
4980 if (len == -1 && (errno == EINTR))
4981 goto again1;
4982
4983 if (len != 1)
4984 exit(1);
4985
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01004986 if (chdir("/")) {
4987 perror("not able to chdir to /");
4988 exit(1);
4989 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01004990 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00004991 if (fd == -1)
4992 exit(1);
aliguori08585322009-02-27 22:09:45 +00004993 }
ths71e3ceb2006-12-22 02:11:31 +00004994
aliguori08585322009-02-27 22:09:45 +00004995 if (run_as) {
4996 pwd = getpwnam(run_as);
4997 if (!pwd) {
4998 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
4999 exit(1);
5000 }
5001 }
ths71e3ceb2006-12-22 02:11:31 +00005002
aliguori08585322009-02-27 22:09:45 +00005003 if (chroot_dir) {
5004 if (chroot(chroot_dir) < 0) {
5005 fprintf(stderr, "chroot failed\n");
5006 exit(1);
5007 }
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01005008 if (chdir("/")) {
5009 perror("not able to chdir to /");
5010 exit(1);
5011 }
aliguori08585322009-02-27 22:09:45 +00005012 }
5013
5014 if (run_as) {
5015 if (setgid(pwd->pw_gid) < 0) {
5016 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5017 exit(1);
5018 }
5019 if (setuid(pwd->pw_uid) < 0) {
5020 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5021 exit(1);
5022 }
5023 if (setuid(0) != -1) {
5024 fprintf(stderr, "Dropping privileges failed\n");
5025 exit(1);
5026 }
5027 }
aliguori08585322009-02-27 22:09:45 +00005028
5029 if (daemonize) {
5030 dup2(fd, 0);
5031 dup2(fd, 1);
5032 dup2(fd, 2);
5033
5034 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005035 }
blueswir1b9e82a52009-04-05 18:03:31 +00005036#endif
ths71e3ceb2006-12-22 02:11:31 +00005037
bellard8a7ddc32004-03-31 19:00:16 +00005038 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005039 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005040 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005041
bellard0824d6f2003-06-24 13:42:40 +00005042 return 0;
5043}