blob: 75b640a6bec59e4309f55c46c4ace5472876576a [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>
47#if defined(__NetBSD__)
48#include <net/if_tap.h>
49#endif
50#ifdef __linux__
51#include <linux/if_tun.h>
52#endif
53#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000054#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000055#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000056#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000058#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000059#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000060#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000061#else
62#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000063#endif
ths5c40d2b2007-06-23 16:03:36 +000064#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000066#else
blueswir1223f0d72008-09-30 18:12:18 +000067#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000068#include <pty.h>
69#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000070#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020071#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000072
73/* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75/* #include <linux/hpet.h> */
76#include "hpet.h"
77
bellarde57a8c02005-11-10 23:58:52 +000078#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000079#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000080#endif
81#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000082#include <sys/stat.h>
83#include <sys/ethernet.h>
84#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000085#include <netinet/arp.h>
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
89#include <netinet/ip_icmp.h> // must come after ip.h
90#include <netinet/udp.h>
91#include <netinet/tcp.h>
92#include <net/if.h>
93#include <syslog.h>
94#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000095/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
96 discussion about Solaris header problems */
97extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000098#endif
bellard7d3505c2004-05-12 19:32:15 +000099#endif
bellardec530c82006-04-25 22:36:06 +0000100#endif
bellard67b915a2004-03-31 23:37:16 +0000101
blueswir19892fbf2008-08-24 10:34:20 +0000102#if defined(__OpenBSD__)
103#include <util.h>
104#endif
105
ths8a16d272008-07-19 09:56:24 +0000106#if defined(CONFIG_VDE)
107#include <libvdeplug.h>
108#endif
109
bellard67b915a2004-03-31 23:37:16 +0000110#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000111#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000112#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000113#endif
114
bellard73332e52004-04-04 20:22:28 +0000115#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200116#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200117#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000118int qemu_main(int argc, char **argv, char **envp);
119int main(int argc, char **argv)
120{
Stefan Weil59a36a22009-06-18 20:11:03 +0200121 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000122}
123#undef main
124#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000125#endif
bellard73332e52004-04-04 20:22:28 +0000126#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000127
bellard5b0753e2005-03-01 21:37:28 +0000128#ifdef CONFIG_COCOA
129#undef main
130#define main qemu_main
131#endif /* CONFIG_COCOA */
132
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "hw/hw.h"
134#include "hw/boards.h"
135#include "hw/usb.h"
136#include "hw/pcmcia.h"
137#include "hw/pc.h"
138#include "hw/audiodev.h"
139#include "hw/isa.h"
140#include "hw/baum.h"
141#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100142#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000143#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000144#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200145#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200146#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000147#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000148#include "net.h"
149#include "monitor.h"
150#include "console.h"
151#include "sysemu.h"
152#include "gdbstub.h"
153#include "qemu-timer.h"
154#include "qemu-char.h"
155#include "cache-utils.h"
156#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000157#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000158#include "audio/audio.h"
159#include "migration.h"
160#include "kvm.h"
161#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200162#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200163#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000164
bellard0824d6f2003-06-24 13:42:40 +0000165#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000166
bellard8a7ddc32004-03-31 19:00:16 +0000167#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000168
blueswir1511d2b12009-03-07 15:32:56 +0000169#include "qemu_socket.h"
170
Jan Kiszkad918f232009-06-24 14:42:30 +0200171#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000172
Blue Swirl72cf2d42009-09-12 07:36:22 +0000173#include "qemu-queue.h"
174
blueswir19dc63a12008-10-04 07:25:46 +0000175//#define DEBUG_NET
176//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000177
bellard1bfe8562004-07-08 21:17:50 +0000178#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000179
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +0200180/* Maximum number of monitor devices */
181#define MAX_MONITOR_DEVICES 10
182
Paul Brook5cea8592009-05-30 00:52:44 +0100183static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000184const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000185/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000186 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000187struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
188struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000189enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000190static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500191DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000192const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500193ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000194int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000195NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000196int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200197int autostart;
balrogf6503052008-02-17 11:42:19 +0000198static int rtc_utc = 1;
199static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200200QEMUClock *rtc_clock;
Zachary Amsden86176752009-07-30 00:15:02 -1000201int vga_interface_type = VGA_CIRRUS;
bellardd8272202005-04-06 20:32:23 +0000202#ifdef TARGET_SPARC
203int graphic_width = 1024;
204int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000205int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000206#else
bellard1bfe8562004-07-08 21:17:50 +0000207int graphic_width = 800;
208int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000209int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000210#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000211static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000212#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000213static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000214#endif
ths667acca2006-12-11 02:08:05 +0000215int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000216CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000217CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000218CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000219#ifdef TARGET_I386
220int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000221int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000222#endif
bellardbb36d472005-11-05 14:22:28 +0000223int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000224int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000225int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200226int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200227int smp_cores = 1;
228int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000229const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000230int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000231int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000232int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000233int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000234int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000235int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000236int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200237uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000238#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000239int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000240#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200241const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000242const char *option_rom[MAX_OPTION_ROMS];
243int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000244int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000245#ifdef TARGET_ARM
246int old_param = 0;
247#endif
thsc35734b2007-03-19 15:17:08 +0000248const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000249int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500250int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000251#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000252unsigned int nb_prom_envs = 0;
253const char *prom_envs[MAX_PROM_ENVS];
254#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200255int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000256
aliguori268a3622009-04-21 22:30:27 +0000257int nb_numa_nodes;
258uint64_t node_mem[MAX_NODES];
259uint64_t node_cpumask[MAX_NODES];
260
balrogee5605e2007-12-03 03:01:40 +0000261static CPUState *cur_cpu;
262static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000263static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000264/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000265static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000266/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000267#define MAX_ICOUNT_SHIFT 10
268/* Compensate for varying guest execution speed. */
269static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000270static QEMUTimer *icount_rt_timer;
271static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000272static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000273
blueswir18fcb1b92008-09-18 18:29:08 +0000274uint8_t qemu_uuid[16];
275
Jan Kiszka76e30d02009-07-02 00:19:02 +0200276static QEMUBootSetHandler *boot_set_handler;
277static void *boot_set_opaque;
278
bellard0824d6f2003-06-24 13:42:40 +0000279/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000280/* x86 ISA bus support */
281
Anthony Liguoric227f092009-10-01 16:12:16 -0500282target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000283PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000284
bellard0824d6f2003-06-24 13:42:40 +0000285/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000286void hw_error(const char *fmt, ...)
287{
288 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000289 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000290
291 va_start(ap, fmt);
292 fprintf(stderr, "qemu: hardware error: ");
293 vfprintf(stderr, fmt, ap);
294 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000295 for(env = first_cpu; env != NULL; env = env->next_cpu) {
296 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000297#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000298 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000299#else
bellard6a00d602005-11-21 23:25:50 +0000300 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000301#endif
bellard6a00d602005-11-21 23:25:50 +0000302 }
bellard0824d6f2003-06-24 13:42:40 +0000303 va_end(ap);
304 abort();
305}
Andi Kleen18894652009-07-02 09:34:17 +0200306
307static void set_proc_name(const char *s)
308{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700309#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200310 char name[16];
311 if (!s)
312 return;
313 name[sizeof(name) - 1] = 0;
314 strncpy(name, s, sizeof(name));
315 /* Could rewrite argv[0] too, but that's a bit more complicated.
316 This simple way is enough for `top'. */
317 prctl(PR_SET_NAME, name);
318#endif
319}
aliguoridf751fa2008-12-04 20:19:35 +0000320
321/***************/
322/* ballooning */
323
324static QEMUBalloonEvent *qemu_balloon_event;
325void *qemu_balloon_event_opaque;
326
327void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
328{
329 qemu_balloon_event = func;
330 qemu_balloon_event_opaque = opaque;
331}
332
Anthony Liguoric227f092009-10-01 16:12:16 -0500333void qemu_balloon(ram_addr_t target)
aliguoridf751fa2008-12-04 20:19:35 +0000334{
335 if (qemu_balloon_event)
336 qemu_balloon_event(qemu_balloon_event_opaque, target);
337}
338
Anthony Liguoric227f092009-10-01 16:12:16 -0500339ram_addr_t qemu_balloon_status(void)
aliguoridf751fa2008-12-04 20:19:35 +0000340{
341 if (qemu_balloon_event)
342 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
343 return 0;
344}
bellard0824d6f2003-06-24 13:42:40 +0000345
bellard8a7ddc32004-03-31 19:00:16 +0000346/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000347/* keyboard/mouse */
348
349static QEMUPutKBDEvent *qemu_put_kbd_event;
350static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000351static QEMUPutMouseEntry *qemu_put_mouse_event_head;
352static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000353
354void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
355{
356 qemu_put_kbd_event_opaque = opaque;
357 qemu_put_kbd_event = func;
358}
359
ths455204e2007-01-05 16:42:13 +0000360QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
361 void *opaque, int absolute,
362 const char *name)
bellard63066f42004-06-03 18:45:02 +0000363{
ths455204e2007-01-05 16:42:13 +0000364 QEMUPutMouseEntry *s, *cursor;
365
366 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000367
368 s->qemu_put_mouse_event = func;
369 s->qemu_put_mouse_event_opaque = opaque;
370 s->qemu_put_mouse_event_absolute = absolute;
371 s->qemu_put_mouse_event_name = qemu_strdup(name);
372 s->next = NULL;
373
374 if (!qemu_put_mouse_event_head) {
375 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
376 return s;
377 }
378
379 cursor = qemu_put_mouse_event_head;
380 while (cursor->next != NULL)
381 cursor = cursor->next;
382
383 cursor->next = s;
384 qemu_put_mouse_event_current = s;
385
386 return s;
387}
388
389void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
390{
391 QEMUPutMouseEntry *prev = NULL, *cursor;
392
393 if (!qemu_put_mouse_event_head || entry == NULL)
394 return;
395
396 cursor = qemu_put_mouse_event_head;
397 while (cursor != NULL && cursor != entry) {
398 prev = cursor;
399 cursor = cursor->next;
400 }
401
402 if (cursor == NULL) // does not exist or list empty
403 return;
404 else if (prev == NULL) { // entry is head
405 qemu_put_mouse_event_head = cursor->next;
406 if (qemu_put_mouse_event_current == entry)
407 qemu_put_mouse_event_current = cursor->next;
408 qemu_free(entry->qemu_put_mouse_event_name);
409 qemu_free(entry);
410 return;
411 }
412
413 prev->next = entry->next;
414
415 if (qemu_put_mouse_event_current == entry)
416 qemu_put_mouse_event_current = prev;
417
418 qemu_free(entry->qemu_put_mouse_event_name);
419 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000420}
421
422void kbd_put_keycode(int keycode)
423{
424 if (qemu_put_kbd_event) {
425 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
426 }
427}
428
429void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
430{
ths455204e2007-01-05 16:42:13 +0000431 QEMUPutMouseEvent *mouse_event;
432 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000433 int width;
ths455204e2007-01-05 16:42:13 +0000434
435 if (!qemu_put_mouse_event_current) {
436 return;
437 }
438
439 mouse_event =
440 qemu_put_mouse_event_current->qemu_put_mouse_event;
441 mouse_event_opaque =
442 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
443
444 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000445 if (graphic_rotate) {
446 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
447 width = 0x7fff;
448 else
aurel32b94ed572008-03-10 19:34:27 +0000449 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000450 mouse_event(mouse_event_opaque,
451 width - dy, dx, dz, buttons_state);
452 } else
453 mouse_event(mouse_event_opaque,
454 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000455 }
456}
457
bellard09b26c52006-04-12 21:09:08 +0000458int kbd_mouse_is_absolute(void)
459{
ths455204e2007-01-05 16:42:13 +0000460 if (!qemu_put_mouse_event_current)
461 return 0;
462
463 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
464}
465
aliguori376253e2009-03-05 23:01:23 +0000466void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000467{
468 QEMUPutMouseEntry *cursor;
469 int index = 0;
470
471 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000472 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000473 return;
474 }
475
aliguori376253e2009-03-05 23:01:23 +0000476 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000477 cursor = qemu_put_mouse_event_head;
478 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000479 monitor_printf(mon, "%c Mouse #%d: %s\n",
480 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
481 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000482 index++;
483 cursor = cursor->next;
484 }
485}
486
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300487void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000488{
489 QEMUPutMouseEntry *cursor;
490 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300491 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000492
493 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000495 return;
496 }
497
498 cursor = qemu_put_mouse_event_head;
499 while (cursor != NULL && index != i) {
500 i++;
501 cursor = cursor->next;
502 }
503
504 if (cursor != NULL)
505 qemu_put_mouse_event_current = cursor;
506 else
aliguori376253e2009-03-05 23:01:23 +0000507 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000508}
509
bellard87858c82003-06-27 12:01:39 +0000510/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000511uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000512{
513 union {
514 uint64_t ll;
515 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200516#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000517 uint32_t high, low;
518#else
519 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000520#endif
bellard87858c82003-06-27 12:01:39 +0000521 } l;
522 } u, res;
523 uint64_t rl, rh;
524
525 u.ll = a;
526 rl = (uint64_t)u.l.low * (uint64_t)b;
527 rh = (uint64_t)u.l.high * (uint64_t)b;
528 rh += (rl >> 32);
529 res.l.high = rh / c;
530 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
531 return res.ll;
532}
533
bellard1dce7c32006-07-13 23:20:22 +0000534/***********************************************************/
535/* real time host monotonic timer */
536
Jan Kiszka21d5d122009-09-15 13:36:04 +0200537static int64_t get_clock_realtime(void)
538{
539 struct timeval tv;
540
541 gettimeofday(&tv, NULL);
542 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
543}
544
bellard1dce7c32006-07-13 23:20:22 +0000545#ifdef WIN32
546
547static int64_t clock_freq;
548
549static void init_get_clock(void)
550{
bellarda8e5ac32006-07-14 09:36:13 +0000551 LARGE_INTEGER freq;
552 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000553 ret = QueryPerformanceFrequency(&freq);
554 if (ret == 0) {
555 fprintf(stderr, "Could not calibrate ticks\n");
556 exit(1);
557 }
558 clock_freq = freq.QuadPart;
559}
560
561static int64_t get_clock(void)
562{
563 LARGE_INTEGER ti;
564 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500565 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000566}
567
568#else
569
570static int use_rt_clock;
571
572static void init_get_clock(void)
573{
574 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000575#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
576 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000577 {
578 struct timespec ts;
579 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
580 use_rt_clock = 1;
581 }
582 }
583#endif
584}
585
586static int64_t get_clock(void)
587{
blueswir1c5e97232009-03-07 20:06:23 +0000588#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
589 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000590 if (use_rt_clock) {
591 struct timespec ts;
592 clock_gettime(CLOCK_MONOTONIC, &ts);
593 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000594 } else
bellard1dce7c32006-07-13 23:20:22 +0000595#endif
596 {
597 /* XXX: using gettimeofday leads to problems if the date
598 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200599 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000600 }
601}
bellard1dce7c32006-07-13 23:20:22 +0000602#endif
603
pbrook2e70f6e2008-06-29 01:03:05 +0000604/* Return the virtual CPU time, based on the instruction counter. */
605static int64_t cpu_get_icount(void)
606{
607 int64_t icount;
608 CPUState *env = cpu_single_env;;
609 icount = qemu_icount;
610 if (env) {
611 if (!can_do_io(env))
612 fprintf(stderr, "Bad clock read\n");
613 icount -= (env->icount_decr.u16.low + env->icount_extra);
614 }
615 return qemu_icount_bias + (icount << icount_time_shift);
616}
617
bellard1dce7c32006-07-13 23:20:22 +0000618/***********************************************************/
619/* guest cycle counter */
620
Juan Quintela6f68e332009-09-10 03:04:27 +0200621typedef struct TimersState {
622 int64_t cpu_ticks_prev;
623 int64_t cpu_ticks_offset;
624 int64_t cpu_clock_offset;
625 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500626 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200627} TimersState;
628
629TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000630
631/* return the host CPU cycle counter and handle stop/restart */
632int64_t cpu_get_ticks(void)
633{
pbrook2e70f6e2008-06-29 01:03:05 +0000634 if (use_icount) {
635 return cpu_get_icount();
636 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200637 if (!timers_state.cpu_ticks_enabled) {
638 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000639 } else {
640 int64_t ticks;
641 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200642 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000643 /* Note: non increasing ticks may happen if the host uses
644 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200645 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000646 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200647 timers_state.cpu_ticks_prev = ticks;
648 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000649 }
650}
651
652/* return the host CPU monotonic timer and handle stop/restart */
653static int64_t cpu_get_clock(void)
654{
655 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200656 if (!timers_state.cpu_ticks_enabled) {
657 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000658 } else {
659 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200660 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000661 }
662}
663
664/* enable cpu_get_ticks() */
665void cpu_enable_ticks(void)
666{
Juan Quintela6f68e332009-09-10 03:04:27 +0200667 if (!timers_state.cpu_ticks_enabled) {
668 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
669 timers_state.cpu_clock_offset -= get_clock();
670 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000671 }
672}
673
674/* disable cpu_get_ticks() : the clock is stopped. You must not call
675 cpu_get_ticks() after that. */
676void cpu_disable_ticks(void)
677{
Juan Quintela6f68e332009-09-10 03:04:27 +0200678 if (timers_state.cpu_ticks_enabled) {
679 timers_state.cpu_ticks_offset = cpu_get_ticks();
680 timers_state.cpu_clock_offset = cpu_get_clock();
681 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000682 }
683}
684
685/***********************************************************/
686/* timers */
ths5fafdf22007-09-16 21:08:06 +0000687
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200688#define QEMU_CLOCK_REALTIME 0
689#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200690#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000691
692struct QEMUClock {
693 int type;
694 /* XXX: add frequency */
695};
696
697struct QEMUTimer {
698 QEMUClock *clock;
699 int64_t expire_time;
700 QEMUTimerCB *cb;
701 void *opaque;
702 struct QEMUTimer *next;
703};
704
thsc8994012007-08-19 21:56:03 +0000705struct qemu_alarm_timer {
706 char const *name;
thsefe75412007-08-24 01:36:32 +0000707 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000708
709 int (*start)(struct qemu_alarm_timer *t);
710 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000711 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000712 void *priv;
713};
714
thsefe75412007-08-24 01:36:32 +0000715#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000716#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000717
718static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
719{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200720 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000721}
722
723static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
724{
725 if (!alarm_has_dynticks(t))
726 return;
727
728 t->rearm(t);
729}
730
731/* TODO: MIN_TIMER_REARM_US should be optimized */
732#define MIN_TIMER_REARM_US 250
733
thsc8994012007-08-19 21:56:03 +0000734static struct qemu_alarm_timer *alarm_timer;
735
736#ifdef _WIN32
737
738struct qemu_alarm_win32 {
739 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000740 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000741} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000742
743static int win32_start_timer(struct qemu_alarm_timer *t);
744static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000745static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000746
747#else
748
749static int unix_start_timer(struct qemu_alarm_timer *t);
750static void unix_stop_timer(struct qemu_alarm_timer *t);
751
ths231c6582007-08-26 17:29:15 +0000752#ifdef __linux__
753
thsefe75412007-08-24 01:36:32 +0000754static int dynticks_start_timer(struct qemu_alarm_timer *t);
755static void dynticks_stop_timer(struct qemu_alarm_timer *t);
756static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
757
thsc40ec5a2007-08-19 22:09:40 +0000758static int hpet_start_timer(struct qemu_alarm_timer *t);
759static void hpet_stop_timer(struct qemu_alarm_timer *t);
760
thsc8994012007-08-19 21:56:03 +0000761static int rtc_start_timer(struct qemu_alarm_timer *t);
762static void rtc_stop_timer(struct qemu_alarm_timer *t);
763
thsefe75412007-08-24 01:36:32 +0000764#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000765
766#endif /* _WIN32 */
767
pbrook2e70f6e2008-06-29 01:03:05 +0000768/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000769 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000770 When the guest is idle real and virtual time will be aligned in
771 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500772#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000773
774static void icount_adjust(void)
775{
776 int64_t cur_time;
777 int64_t cur_icount;
778 int64_t delta;
779 static int64_t last_delta;
780 /* If the VM is not running, then do nothing. */
781 if (!vm_running)
782 return;
783
784 cur_time = cpu_get_clock();
785 cur_icount = qemu_get_clock(vm_clock);
786 delta = cur_icount - cur_time;
787 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
788 if (delta > 0
789 && last_delta + ICOUNT_WOBBLE < delta * 2
790 && icount_time_shift > 0) {
791 /* The guest is getting too far ahead. Slow time down. */
792 icount_time_shift--;
793 }
794 if (delta < 0
795 && last_delta - ICOUNT_WOBBLE > delta * 2
796 && icount_time_shift < MAX_ICOUNT_SHIFT) {
797 /* The guest is getting too far behind. Speed time up. */
798 icount_time_shift++;
799 }
800 last_delta = delta;
801 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
802}
803
804static void icount_adjust_rt(void * opaque)
805{
806 qemu_mod_timer(icount_rt_timer,
807 qemu_get_clock(rt_clock) + 1000);
808 icount_adjust();
809}
810
811static void icount_adjust_vm(void * opaque)
812{
813 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500814 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000815 icount_adjust();
816}
817
818static void init_icount_adjust(void)
819{
820 /* Have both realtime and virtual time triggers for speed adjustment.
821 The realtime trigger catches emulated time passing too slowly,
822 the virtual time trigger catches emulated time passing too fast.
823 Realtime triggers occur even when idle, so use them less frequently
824 than VM triggers. */
825 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
826 qemu_mod_timer(icount_rt_timer,
827 qemu_get_clock(rt_clock) + 1000);
828 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
829 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500830 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000831}
832
thsc8994012007-08-19 21:56:03 +0000833static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000834#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000835#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000836 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
837 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000838 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000839 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000840 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000841 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000842#endif
thsefe75412007-08-24 01:36:32 +0000843 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000844#else
thsefe75412007-08-24 01:36:32 +0000845 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
846 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
847 {"win32", 0, win32_start_timer,
848 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000849#endif
850 {NULL, }
851};
852
blueswir13f47aa82008-03-09 06:59:01 +0000853static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000854{
855 int i;
856
857 printf("Available alarm timers, in order of precedence:\n");
858 for (i = 0; alarm_timers[i].name; i++)
859 printf("%s\n", alarm_timers[i].name);
860}
861
862static void configure_alarms(char const *opt)
863{
864 int i;
865 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000866 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000867 char *arg;
868 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000869 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000870
aurel323adda042008-03-09 23:43:49 +0000871 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000872 show_available_alarms();
873 exit(0);
874 }
875
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200876 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000877
878 /* Reorder the array */
879 name = strtok(arg, ",");
880 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000881 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000882 if (!strcmp(alarm_timers[i].name, name))
883 break;
884 }
885
886 if (i == count) {
887 fprintf(stderr, "Unknown clock %s\n", name);
888 goto next;
889 }
890
891 if (i < cur)
892 /* Ignore */
893 goto next;
894
895 /* Swap */
896 tmp = alarm_timers[i];
897 alarm_timers[i] = alarm_timers[cur];
898 alarm_timers[cur] = tmp;
899
900 cur++;
901next:
902 name = strtok(NULL, ",");
903 }
904
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200905 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000906
907 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000908 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000909 for (i = cur; i < count; i++)
910 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000911 } else {
912 show_available_alarms();
913 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000914 }
thsf3dcfad2007-08-24 01:26:02 +0000915}
916
Jan Kiszka21d5d122009-09-15 13:36:04 +0200917#define QEMU_NUM_CLOCKS 3
918
bellard8a7ddc32004-03-31 19:00:16 +0000919QEMUClock *rt_clock;
920QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +0200921QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +0000922
Jan Kiszka21d5d122009-09-15 13:36:04 +0200923static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +0000924
pbrook9596ebb2007-11-18 01:44:38 +0000925static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000926{
927 QEMUClock *clock;
928 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000929 clock->type = type;
930 return clock;
931}
932
933QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
934{
935 QEMUTimer *ts;
936
937 ts = qemu_mallocz(sizeof(QEMUTimer));
938 ts->clock = clock;
939 ts->cb = cb;
940 ts->opaque = opaque;
941 return ts;
942}
943
944void qemu_free_timer(QEMUTimer *ts)
945{
946 qemu_free(ts);
947}
948
949/* stop a timer, but do not dealloc it */
950void qemu_del_timer(QEMUTimer *ts)
951{
952 QEMUTimer **pt, *t;
953
954 /* NOTE: this code must be signal safe because
955 qemu_timer_expired() can be called from a signal. */
956 pt = &active_timers[ts->clock->type];
957 for(;;) {
958 t = *pt;
959 if (!t)
960 break;
961 if (t == ts) {
962 *pt = t->next;
963 break;
964 }
965 pt = &t->next;
966 }
967}
968
969/* modify the current timer so that it will be fired when current_time
970 >= expire_time. The corresponding callback will be called. */
971void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
972{
973 QEMUTimer **pt, *t;
974
975 qemu_del_timer(ts);
976
977 /* add the timer in the sorted list */
978 /* NOTE: this code must be signal safe because
979 qemu_timer_expired() can be called from a signal. */
980 pt = &active_timers[ts->clock->type];
981 for(;;) {
982 t = *pt;
983 if (!t)
984 break;
ths5fafdf22007-09-16 21:08:06 +0000985 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000986 break;
987 pt = &t->next;
988 }
989 ts->expire_time = expire_time;
990 ts->next = *pt;
991 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000992
993 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000994 if (pt == &active_timers[ts->clock->type]) {
995 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
996 qemu_rearm_alarm_timer(alarm_timer);
997 }
998 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000999 if (use_icount)
1000 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001001 }
bellard8a7ddc32004-03-31 19:00:16 +00001002}
1003
1004int qemu_timer_pending(QEMUTimer *ts)
1005{
1006 QEMUTimer *t;
1007 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1008 if (t == ts)
1009 return 1;
1010 }
1011 return 0;
1012}
1013
Stefano Stabellini2430ffe2009-08-03 10:56:01 +01001014int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001015{
1016 if (!timer_head)
1017 return 0;
1018 return (timer_head->expire_time <= current_time);
1019}
1020
1021static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1022{
1023 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001024
bellard8a7ddc32004-03-31 19:00:16 +00001025 for(;;) {
1026 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001027 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001028 break;
1029 /* remove timer from the list before calling the callback */
1030 *ptimer_head = ts->next;
1031 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001032
bellard8a7ddc32004-03-31 19:00:16 +00001033 /* run the callback (the timer list can be modified) */
1034 ts->cb(ts->opaque);
1035 }
1036}
1037
1038int64_t qemu_get_clock(QEMUClock *clock)
1039{
1040 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001041 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001042 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001043 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001044 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001045 if (use_icount) {
1046 return cpu_get_icount();
1047 } else {
1048 return cpu_get_clock();
1049 }
Jan Kiszka21d5d122009-09-15 13:36:04 +02001050 case QEMU_CLOCK_HOST:
1051 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +00001052 }
1053}
1054
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001055static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +00001056{
1057 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001058 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
1059 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001060 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +02001061
1062 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +00001063}
1064
bellard8a7ddc32004-03-31 19:00:16 +00001065/* save a timer */
1066void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1067{
1068 uint64_t expire_time;
1069
1070 if (qemu_timer_pending(ts)) {
1071 expire_time = ts->expire_time;
1072 } else {
1073 expire_time = -1;
1074 }
1075 qemu_put_be64(f, expire_time);
1076}
1077
1078void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1079{
1080 uint64_t expire_time;
1081
1082 expire_time = qemu_get_be64(f);
1083 if (expire_time != -1) {
1084 qemu_mod_timer(ts, expire_time);
1085 } else {
1086 qemu_del_timer(ts);
1087 }
1088}
1089
Juan Quintela2faf58c2009-09-10 03:04:28 +02001090static const VMStateDescription vmstate_timers = {
1091 .name = "timer",
1092 .version_id = 2,
1093 .minimum_version_id = 1,
1094 .minimum_version_id_old = 1,
1095 .fields = (VMStateField []) {
1096 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001097 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001098 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1099 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001100 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001101};
bellard8a7ddc32004-03-31 19:00:16 +00001102
aliguori50317c72009-04-24 18:03:29 +00001103static void qemu_event_increment(void);
1104
bellard67b915a2004-03-31 23:37:16 +00001105#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001106static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1107 DWORD_PTR dwUser, DWORD_PTR dw1,
1108 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001109#else
bellard8a7ddc32004-03-31 19:00:16 +00001110static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001111#endif
bellard8a7ddc32004-03-31 19:00:16 +00001112{
bellard02ba45c2004-06-25 14:46:23 +00001113#if 0
1114#define DISP_FREQ 1000
1115 {
1116 static int64_t delta_min = INT64_MAX;
1117 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1118 static int count;
1119 ti = qemu_get_clock(vm_clock);
1120 if (last_clock != 0) {
1121 delta = ti - last_clock;
1122 if (delta < delta_min)
1123 delta_min = delta;
1124 if (delta > delta_max)
1125 delta_max = delta;
1126 delta_cum += delta;
1127 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001128 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001129 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1130 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1131 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1132 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001133 count = 0;
1134 delta_min = INT64_MAX;
1135 delta_max = 0;
1136 delta_cum = 0;
1137 }
1138 }
1139 last_clock = ti;
1140 }
1141#endif
thsefe75412007-08-24 01:36:32 +00001142 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001143 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001144 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001145 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001146 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001147 qemu_get_clock(rt_clock)) ||
1148 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1149 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001150 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001151 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001152
aliguorid6dc3d42009-04-24 18:04:07 +00001153#ifndef CONFIG_IOTHREAD
1154 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001155 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001156 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001157 }
aliguorid6dc3d42009-04-24 18:04:07 +00001158#endif
aliguori43b96852009-04-24 18:03:33 +00001159 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001160 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001161 }
1162}
1163
pbrook2e70f6e2008-06-29 01:03:05 +00001164static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001165{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001166 /* To avoid problems with overflow limit this to 2^32. */
1167 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001168
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001169 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1170 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001171 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001172 }
1173 if (active_timers[QEMU_CLOCK_HOST]) {
1174 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1175 qemu_get_clock(host_clock);
1176 if (hdelta < delta)
1177 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001178 }
1179
pbrook2e70f6e2008-06-29 01:03:05 +00001180 if (delta < 0)
1181 delta = 0;
thsefe75412007-08-24 01:36:32 +00001182
pbrook2e70f6e2008-06-29 01:03:05 +00001183 return delta;
1184}
1185
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001186#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001187static uint64_t qemu_next_deadline_dyntick(void)
1188{
1189 int64_t delta;
1190 int64_t rtdelta;
1191
1192 if (use_icount)
1193 delta = INT32_MAX;
1194 else
1195 delta = (qemu_next_deadline() + 999) / 1000;
1196
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001197 if (active_timers[QEMU_CLOCK_REALTIME]) {
1198 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001199 qemu_get_clock(rt_clock))*1000;
1200 if (rtdelta < delta)
1201 delta = rtdelta;
1202 }
1203
1204 if (delta < MIN_TIMER_REARM_US)
1205 delta = MIN_TIMER_REARM_US;
1206
1207 return delta;
thsefe75412007-08-24 01:36:32 +00001208}
blueswir18632fb92008-09-14 13:59:34 +00001209#endif
thsefe75412007-08-24 01:36:32 +00001210
bellardfd872592004-05-12 19:11:15 +00001211#ifndef _WIN32
1212
aliguori7183b4b2008-11-05 20:40:18 +00001213/* Sets a specific flag */
1214static int fcntl_setfl(int fd, int flag)
1215{
1216 int flags;
1217
1218 flags = fcntl(fd, F_GETFL);
1219 if (flags == -1)
1220 return -errno;
1221
1222 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1223 return -errno;
1224
1225 return 0;
1226}
1227
bellard829309c2004-05-20 13:20:12 +00001228#if defined(__linux__)
1229
bellardfd872592004-05-12 19:11:15 +00001230#define RTC_FREQ 1024
1231
aurel32de9a95f2008-11-11 13:41:01 +00001232static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001233{
thsc8994012007-08-19 21:56:03 +00001234 struct sigaction act;
1235
1236 /* timer signal */
1237 sigfillset(&act.sa_mask);
1238 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001239 act.sa_handler = host_alarm_handler;
1240
1241 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001242 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001243 fcntl(fd, F_SETOWN, getpid());
1244}
1245
thsc40ec5a2007-08-19 22:09:40 +00001246static int hpet_start_timer(struct qemu_alarm_timer *t)
1247{
1248 struct hpet_info info;
1249 int r, fd;
1250
1251 fd = open("/dev/hpet", O_RDONLY);
1252 if (fd < 0)
1253 return -1;
1254
1255 /* Set frequency */
1256 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1257 if (r < 0) {
1258 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1259 "error, but for better emulation accuracy type:\n"
1260 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1261 goto fail;
1262 }
1263
1264 /* Check capabilities */
1265 r = ioctl(fd, HPET_INFO, &info);
1266 if (r < 0)
1267 goto fail;
1268
1269 /* Enable periodic mode */
1270 r = ioctl(fd, HPET_EPI, 0);
1271 if (info.hi_flags && (r < 0))
1272 goto fail;
1273
1274 /* Enable interrupt */
1275 r = ioctl(fd, HPET_IE_ON, 0);
1276 if (r < 0)
1277 goto fail;
1278
1279 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001280 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001281
1282 return 0;
1283fail:
1284 close(fd);
1285 return -1;
1286}
1287
1288static void hpet_stop_timer(struct qemu_alarm_timer *t)
1289{
pbrookfcdc2122007-08-23 20:22:22 +00001290 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001291
1292 close(fd);
1293}
1294
thsc8994012007-08-19 21:56:03 +00001295static int rtc_start_timer(struct qemu_alarm_timer *t)
1296{
1297 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001298 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001299
balrogaeb30be2007-07-02 15:03:13 +00001300 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001301 if (rtc_fd < 0)
1302 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001303 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1304 if (current_rtc_freq != RTC_FREQ &&
1305 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001306 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1307 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1308 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1309 goto fail;
1310 }
1311 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1312 fail:
1313 close(rtc_fd);
1314 return -1;
1315 }
thsc8994012007-08-19 21:56:03 +00001316
1317 enable_sigio_timer(rtc_fd);
1318
pbrookfcdc2122007-08-23 20:22:22 +00001319 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001320
bellardfd872592004-05-12 19:11:15 +00001321 return 0;
1322}
1323
thsc8994012007-08-19 21:56:03 +00001324static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001325{
pbrookfcdc2122007-08-23 20:22:22 +00001326 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001327
1328 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001329}
1330
thsefe75412007-08-24 01:36:32 +00001331static int dynticks_start_timer(struct qemu_alarm_timer *t)
1332{
1333 struct sigevent ev;
1334 timer_t host_timer;
1335 struct sigaction act;
1336
1337 sigfillset(&act.sa_mask);
1338 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001339 act.sa_handler = host_alarm_handler;
1340
1341 sigaction(SIGALRM, &act, NULL);
1342
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001343 /*
1344 * Initialize ev struct to 0 to avoid valgrind complaining
1345 * about uninitialized data in timer_create call
1346 */
1347 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001348 ev.sigev_value.sival_int = 0;
1349 ev.sigev_notify = SIGEV_SIGNAL;
1350 ev.sigev_signo = SIGALRM;
1351
1352 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1353 perror("timer_create");
1354
1355 /* disable dynticks */
1356 fprintf(stderr, "Dynamic Ticks disabled\n");
1357
1358 return -1;
1359 }
1360
blueswir10399bfe2008-11-16 11:37:18 +00001361 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001362
1363 return 0;
1364}
1365
1366static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1367{
blueswir10399bfe2008-11-16 11:37:18 +00001368 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001369
1370 timer_delete(host_timer);
1371}
1372
1373static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1374{
blueswir10399bfe2008-11-16 11:37:18 +00001375 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001376 struct itimerspec timeout;
1377 int64_t nearest_delta_us = INT64_MAX;
1378 int64_t current_us;
1379
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001380 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001381 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1382 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001383 return;
thsefe75412007-08-24 01:36:32 +00001384
pbrook2e70f6e2008-06-29 01:03:05 +00001385 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001386
1387 /* check whether a timer is already running */
1388 if (timer_gettime(host_timer, &timeout)) {
1389 perror("gettime");
1390 fprintf(stderr, "Internal timer error: aborting\n");
1391 exit(1);
1392 }
1393 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1394 if (current_us && current_us <= nearest_delta_us)
1395 return;
1396
1397 timeout.it_interval.tv_sec = 0;
1398 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1399 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1400 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1401 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1402 perror("settime");
1403 fprintf(stderr, "Internal timer error: aborting\n");
1404 exit(1);
1405 }
1406}
1407
ths70744b32007-08-26 17:31:30 +00001408#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001409
thsc8994012007-08-19 21:56:03 +00001410static int unix_start_timer(struct qemu_alarm_timer *t)
1411{
1412 struct sigaction act;
1413 struct itimerval itv;
1414 int err;
1415
1416 /* timer signal */
1417 sigfillset(&act.sa_mask);
1418 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001419 act.sa_handler = host_alarm_handler;
1420
1421 sigaction(SIGALRM, &act, NULL);
1422
1423 itv.it_interval.tv_sec = 0;
1424 /* for i386 kernel 2.6 to get 1 ms */
1425 itv.it_interval.tv_usec = 999;
1426 itv.it_value.tv_sec = 0;
1427 itv.it_value.tv_usec = 10 * 1000;
1428
1429 err = setitimer(ITIMER_REAL, &itv, NULL);
1430 if (err)
1431 return -1;
1432
1433 return 0;
1434}
1435
1436static void unix_stop_timer(struct qemu_alarm_timer *t)
1437{
1438 struct itimerval itv;
1439
1440 memset(&itv, 0, sizeof(itv));
1441 setitimer(ITIMER_REAL, &itv, NULL);
1442}
1443
bellard829309c2004-05-20 13:20:12 +00001444#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001445
aliguorif49e58d2008-11-05 21:22:34 +00001446
thsc8994012007-08-19 21:56:03 +00001447#ifdef _WIN32
1448
1449static int win32_start_timer(struct qemu_alarm_timer *t)
1450{
1451 TIMECAPS tc;
1452 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001453 UINT flags;
thsc8994012007-08-19 21:56:03 +00001454
thsc8994012007-08-19 21:56:03 +00001455 memset(&tc, 0, sizeof(tc));
1456 timeGetDevCaps(&tc, sizeof(tc));
1457
1458 if (data->period < tc.wPeriodMin)
1459 data->period = tc.wPeriodMin;
1460
1461 timeBeginPeriod(data->period);
1462
thsefe75412007-08-24 01:36:32 +00001463 flags = TIME_CALLBACK_FUNCTION;
1464 if (alarm_has_dynticks(t))
1465 flags |= TIME_ONESHOT;
1466 else
1467 flags |= TIME_PERIODIC;
1468
thsc8994012007-08-19 21:56:03 +00001469 data->timerId = timeSetEvent(1, // interval (ms)
1470 data->period, // resolution
1471 host_alarm_handler, // function
1472 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001473 flags);
thsc8994012007-08-19 21:56:03 +00001474
1475 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001476 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001477 GetLastError());
thsc8994012007-08-19 21:56:03 +00001478 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001479 return -1;
1480 }
1481
thsc8994012007-08-19 21:56:03 +00001482 return 0;
1483}
1484
1485static void win32_stop_timer(struct qemu_alarm_timer *t)
1486{
1487 struct qemu_alarm_win32 *data = t->priv;
1488
1489 timeKillEvent(data->timerId);
1490 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001491}
1492
thsefe75412007-08-24 01:36:32 +00001493static void win32_rearm_timer(struct qemu_alarm_timer *t)
1494{
1495 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001496
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001497 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001498 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1499 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001500 return;
thsefe75412007-08-24 01:36:32 +00001501
thsefe75412007-08-24 01:36:32 +00001502 timeKillEvent(data->timerId);
1503
1504 data->timerId = timeSetEvent(1,
1505 data->period,
1506 host_alarm_handler,
1507 (DWORD)t,
1508 TIME_ONESHOT | TIME_PERIODIC);
1509
1510 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001511 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001512 GetLastError());
thsefe75412007-08-24 01:36:32 +00001513
1514 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001515 exit(1);
1516 }
1517}
1518
thsc8994012007-08-19 21:56:03 +00001519#endif /* _WIN32 */
1520
aliguori7183b4b2008-11-05 20:40:18 +00001521static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001522{
blueswir1223f0d72008-09-30 18:12:18 +00001523 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001524 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001525
thsc8994012007-08-19 21:56:03 +00001526 for (i = 0; alarm_timers[i].name; i++) {
1527 t = &alarm_timers[i];
1528
thsc8994012007-08-19 21:56:03 +00001529 err = t->start(t);
1530 if (!err)
1531 break;
bellard67b915a2004-03-31 23:37:16 +00001532 }
bellardfd872592004-05-12 19:11:15 +00001533
thsc8994012007-08-19 21:56:03 +00001534 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001535 err = -ENOENT;
1536 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001537 }
thsc8994012007-08-19 21:56:03 +00001538
1539 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001540
aliguori6abfbd72008-11-05 20:49:37 +00001541 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001542
1543fail:
aliguori7183b4b2008-11-05 20:40:18 +00001544 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001545}
1546
pbrook9596ebb2007-11-18 01:44:38 +00001547static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001548{
thsc8994012007-08-19 21:56:03 +00001549 alarm_timer->stop(alarm_timer);
1550 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001551}
1552
bellardc4b1fcc2004-03-14 21:44:30 +00001553/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001554/* host time/date access */
1555void qemu_get_timedate(struct tm *tm, int offset)
1556{
1557 time_t ti;
1558 struct tm *ret;
1559
1560 time(&ti);
1561 ti += offset;
1562 if (rtc_date_offset == -1) {
1563 if (rtc_utc)
1564 ret = gmtime(&ti);
1565 else
1566 ret = localtime(&ti);
1567 } else {
1568 ti -= rtc_date_offset;
1569 ret = gmtime(&ti);
1570 }
1571
1572 memcpy(tm, ret, sizeof(struct tm));
1573}
1574
1575int qemu_timedate_diff(struct tm *tm)
1576{
1577 time_t seconds;
1578
1579 if (rtc_date_offset == -1)
1580 if (rtc_utc)
1581 seconds = mktimegm(tm);
1582 else
1583 seconds = mktime(tm);
1584 else
1585 seconds = mktimegm(tm) + rtc_date_offset;
1586
1587 return seconds - time(NULL);
1588}
1589
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001590static void configure_rtc_date_offset(const char *startdate, int legacy)
1591{
1592 time_t rtc_start_date;
1593 struct tm tm;
1594
1595 if (!strcmp(startdate, "now") && legacy) {
1596 rtc_date_offset = -1;
1597 } else {
1598 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1599 &tm.tm_year,
1600 &tm.tm_mon,
1601 &tm.tm_mday,
1602 &tm.tm_hour,
1603 &tm.tm_min,
1604 &tm.tm_sec) == 6) {
1605 /* OK */
1606 } else if (sscanf(startdate, "%d-%d-%d",
1607 &tm.tm_year,
1608 &tm.tm_mon,
1609 &tm.tm_mday) == 3) {
1610 tm.tm_hour = 0;
1611 tm.tm_min = 0;
1612 tm.tm_sec = 0;
1613 } else {
1614 goto date_fail;
1615 }
1616 tm.tm_year -= 1900;
1617 tm.tm_mon--;
1618 rtc_start_date = mktimegm(&tm);
1619 if (rtc_start_date == -1) {
1620 date_fail:
1621 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1622 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1623 exit(1);
1624 }
1625 rtc_date_offset = time(NULL) - rtc_start_date;
1626 }
1627}
1628
1629static void configure_rtc(QemuOpts *opts)
1630{
1631 const char *value;
1632
1633 value = qemu_opt_get(opts, "base");
1634 if (value) {
1635 if (!strcmp(value, "utc")) {
1636 rtc_utc = 1;
1637 } else if (!strcmp(value, "localtime")) {
1638 rtc_utc = 0;
1639 } else {
1640 configure_rtc_date_offset(value, 0);
1641 }
1642 }
Jan Kiszka68752042009-09-15 13:36:04 +02001643 value = qemu_opt_get(opts, "clock");
1644 if (value) {
1645 if (!strcmp(value, "host")) {
1646 rtc_clock = host_clock;
1647 } else if (!strcmp(value, "vm")) {
1648 rtc_clock = vm_clock;
1649 } else {
1650 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1651 exit(1);
1652 }
1653 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001654#ifdef CONFIG_TARGET_I386
1655 value = qemu_opt_get(opts, "driftfix");
1656 if (value) {
1657 if (!strcmp(buf, "slew")) {
1658 rtc_td_hack = 1;
1659 } else if (!strcmp(buf, "none")) {
1660 rtc_td_hack = 0;
1661 } else {
1662 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1663 exit(1);
1664 }
1665 }
1666#endif
1667}
1668
bellardfd1dff42006-02-01 21:29:26 +00001669#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001670static void socket_cleanup(void)
1671{
1672 WSACleanup();
1673}
bellard82c643f2004-07-14 17:28:13 +00001674
bellardfd1dff42006-02-01 21:29:26 +00001675static int socket_init(void)
1676{
1677 WSADATA Data;
1678 int ret, err;
1679
1680 ret = WSAStartup(MAKEWORD(2,2), &Data);
1681 if (ret != 0) {
1682 err = WSAGetLastError();
1683 fprintf(stderr, "WSAStartup: %d\n", err);
1684 return -1;
1685 }
1686 atexit(socket_cleanup);
1687 return 0;
1688}
aurel3264b7b732008-05-05 10:05:31 +00001689#endif
1690
balrog1ae26a12008-09-28 23:19:47 +00001691/***********************************************************/
1692/* Bluetooth support */
1693static int nb_hcis;
1694static int cur_hci;
1695static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001696
balrog1ae26a12008-09-28 23:19:47 +00001697static struct bt_vlan_s {
1698 struct bt_scatternet_s net;
1699 int id;
1700 struct bt_vlan_s *next;
1701} *first_bt_vlan;
1702
1703/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001704static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001705{
1706 struct bt_vlan_s **pvlan, *vlan;
1707 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1708 if (vlan->id == id)
1709 return &vlan->net;
1710 }
1711 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1712 vlan->id = id;
1713 pvlan = &first_bt_vlan;
1714 while (*pvlan != NULL)
1715 pvlan = &(*pvlan)->next;
1716 *pvlan = vlan;
1717 return &vlan->net;
1718}
1719
1720static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1721{
1722}
1723
1724static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1725{
1726 return -ENOTSUP;
1727}
1728
1729static struct HCIInfo null_hci = {
1730 .cmd_send = null_hci_send,
1731 .sco_send = null_hci_send,
1732 .acl_send = null_hci_send,
1733 .bdaddr_set = null_hci_addr_set,
1734};
1735
1736struct HCIInfo *qemu_next_hci(void)
1737{
1738 if (cur_hci == nb_hcis)
1739 return &null_hci;
1740
1741 return hci_table[cur_hci++];
1742}
1743
balrogdc72ac12008-11-09 00:04:26 +00001744static struct HCIInfo *hci_init(const char *str)
1745{
1746 char *endp;
1747 struct bt_scatternet_s *vlan = 0;
1748
1749 if (!strcmp(str, "null"))
1750 /* null */
1751 return &null_hci;
1752 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1753 /* host[:hciN] */
1754 return bt_host_hci(str[4] ? str + 5 : "hci0");
1755 else if (!strncmp(str, "hci", 3)) {
1756 /* hci[,vlan=n] */
1757 if (str[3]) {
1758 if (!strncmp(str + 3, ",vlan=", 6)) {
1759 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1760 if (*endp)
1761 vlan = 0;
1762 }
1763 } else
1764 vlan = qemu_find_bt_vlan(0);
1765 if (vlan)
1766 return bt_new_hci(vlan);
1767 }
1768
1769 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1770
1771 return 0;
1772}
1773
1774static int bt_hci_parse(const char *str)
1775{
1776 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001777 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001778
1779 if (nb_hcis >= MAX_NICS) {
1780 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1781 return -1;
1782 }
1783
1784 hci = hci_init(str);
1785 if (!hci)
1786 return -1;
1787
1788 bdaddr.b[0] = 0x52;
1789 bdaddr.b[1] = 0x54;
1790 bdaddr.b[2] = 0x00;
1791 bdaddr.b[3] = 0x12;
1792 bdaddr.b[4] = 0x34;
1793 bdaddr.b[5] = 0x56 + nb_hcis;
1794 hci->bdaddr_set(hci, bdaddr.b);
1795
1796 hci_table[nb_hcis++] = hci;
1797
1798 return 0;
1799}
1800
1801static void bt_vhci_add(int vlan_id)
1802{
1803 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1804
1805 if (!vlan->slave)
1806 fprintf(stderr, "qemu: warning: adding a VHCI to "
1807 "an empty scatternet %i\n", vlan_id);
1808
1809 bt_vhci_init(bt_new_hci(vlan));
1810}
1811
1812static struct bt_device_s *bt_device_add(const char *opt)
1813{
1814 struct bt_scatternet_s *vlan;
1815 int vlan_id = 0;
1816 char *endp = strstr(opt, ",vlan=");
1817 int len = (endp ? endp - opt : strlen(opt)) + 1;
1818 char devname[10];
1819
1820 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1821
1822 if (endp) {
1823 vlan_id = strtol(endp + 6, &endp, 0);
1824 if (*endp) {
1825 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1826 return 0;
1827 }
1828 }
1829
1830 vlan = qemu_find_bt_vlan(vlan_id);
1831
1832 if (!vlan->slave)
1833 fprintf(stderr, "qemu: warning: adding a slave device to "
1834 "an empty scatternet %i\n", vlan_id);
1835
1836 if (!strcmp(devname, "keyboard"))
1837 return bt_keyboard_init(vlan);
1838
1839 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1840 return 0;
1841}
1842
1843static int bt_parse(const char *opt)
1844{
1845 const char *endp, *p;
1846 int vlan;
1847
1848 if (strstart(opt, "hci", &endp)) {
1849 if (!*endp || *endp == ',') {
1850 if (*endp)
1851 if (!strstart(endp, ",vlan=", 0))
1852 opt = endp + 1;
1853
1854 return bt_hci_parse(opt);
1855 }
1856 } else if (strstart(opt, "vhci", &endp)) {
1857 if (!*endp || *endp == ',') {
1858 if (*endp) {
1859 if (strstart(endp, ",vlan=", &p)) {
1860 vlan = strtol(p, (char **) &endp, 0);
1861 if (*endp) {
1862 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1863 return 1;
1864 }
1865 } else {
1866 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1867 return 1;
1868 }
1869 } else
1870 vlan = 0;
1871
1872 bt_vhci_add(vlan);
1873 return 0;
1874 }
1875 } else if (strstart(opt, "device:", &endp))
1876 return !bt_device_add(endp);
1877
1878 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1879 return 1;
1880}
1881
balrog1ae26a12008-09-28 23:19:47 +00001882/***********************************************************/
1883/* QEMU Block devices */
1884
balrog609497a2008-01-14 02:56:53 +00001885#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001886#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001887#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001888#define PFLASH_ALIAS "if=pflash"
1889#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001890#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001891
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001892QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001893{
1894 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001895 char optstr[1024];
1896 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001897
thse4bcb142007-12-02 04:51:10 +00001898 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001899 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001900 va_end(ap);
1901
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001902 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001903 if (!opts) {
1904 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1905 __FUNCTION__, optstr);
1906 return NULL;
1907 }
1908 if (file)
1909 qemu_opt_set(opts, "file", file);
1910 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001911}
1912
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001913DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001914{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001915 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001916
1917 /* seek interface, bus and unit */
1918
Blue Swirl72cf2d42009-09-12 07:36:22 +00001919 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001920 if (dinfo->type == type &&
1921 dinfo->bus == bus &&
1922 dinfo->unit == unit)
1923 return dinfo;
1924 }
thse4bcb142007-12-02 04:51:10 +00001925
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001926 return NULL;
thse4bcb142007-12-02 04:51:10 +00001927}
1928
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001929DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001930{
1931 DriveInfo *dinfo;
1932
Blue Swirl72cf2d42009-09-12 07:36:22 +00001933 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001934 if (strcmp(id, dinfo->id))
1935 continue;
1936 return dinfo;
1937 }
1938 return NULL;
1939}
1940
thsf60d39b2007-12-17 03:55:57 +00001941int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001942{
1943 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001944 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001945
1946 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001947 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001948 if(dinfo->type == type &&
1949 dinfo->bus > max_bus)
1950 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001951 }
1952 return max_bus;
1953}
1954
aliguorifa879c62009-01-07 17:32:33 +00001955const char *drive_get_serial(BlockDriverState *bdrv)
1956{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001957 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001958
Blue Swirl72cf2d42009-09-12 07:36:22 +00001959 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001960 if (dinfo->bdrv == bdrv)
1961 return dinfo->serial;
1962 }
aliguorifa879c62009-01-07 17:32:33 +00001963
1964 return "\0";
1965}
1966
aliguori428c5702009-01-21 18:59:04 +00001967BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1968{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001969 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001970
Blue Swirl72cf2d42009-09-12 07:36:22 +00001971 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001972 if (dinfo->bdrv == bdrv)
1973 return dinfo->onerror;
1974 }
aliguori428c5702009-01-21 18:59:04 +00001975
aliguoricdad4bd2009-02-28 16:51:01 +00001976 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001977}
1978
aurel32a1620fa2008-04-29 05:58:01 +00001979static void bdrv_format_print(void *opaque, const char *name)
1980{
1981 fprintf(stderr, " %s", name);
1982}
1983
Gerd Hoffmann56a14932009-09-25 21:42:46 +02001984void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +00001985{
Gerd Hoffmann56a14932009-09-25 21:42:46 +02001986 qemu_opts_del(dinfo->opts);
1987 bdrv_delete(dinfo->bdrv);
1988 QTAILQ_REMOVE(&drives, dinfo, next);
1989 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +00001990}
1991
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001992DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001993 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00001994{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001995 const char *buf;
1996 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00001997 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001998 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00001999 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002000 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002001 enum { MEDIA_DISK, MEDIA_CDROM } media;
2002 int bus_id, unit_id;
2003 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00002004 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002005 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002006 int max_devs;
2007 int index;
balrog33f00272007-12-24 14:33:24 +00002008 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002009 int aio = 0;
aliguori428c5702009-01-21 18:59:04 +00002010 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002011 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002012 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002013 int snapshot = 0;
2014
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002015 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002016
thse4bcb142007-12-02 04:51:10 +00002017 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002018 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002019
Gerd Hoffmann4d007812009-08-31 14:23:57 +02002020 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002021 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002022 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002023 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002024 } else {
thsf60d39b2007-12-17 03:55:57 +00002025 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002026 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002027 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002028 }
2029 media = MEDIA_DISK;
2030
2031 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002032 bus_id = qemu_opt_get_number(opts, "bus", 0);
2033 unit_id = qemu_opt_get_number(opts, "unit", -1);
2034 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002035
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002036 cyls = qemu_opt_get_number(opts, "cyls", 0);
2037 heads = qemu_opt_get_number(opts, "heads", 0);
2038 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002039
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002040 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
thse4bcb142007-12-02 04:51:10 +00002041
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002042 file = qemu_opt_get(opts, "file");
2043 serial = qemu_opt_get(opts, "serial");
2044
2045 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002046 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002047 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002048 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002049 max_devs = MAX_IDE_DEVS;
2050 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002051 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002052 max_devs = MAX_SCSI_DEVS;
2053 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002054 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002055 max_devs = 0;
2056 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002057 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002058 max_devs = 0;
2059 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002060 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002061 max_devs = 0;
2062 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002063 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002064 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002065 } else if (!strcmp(buf, "virtio")) {
2066 type = IF_VIRTIO;
2067 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002068 } else if (!strcmp(buf, "xen")) {
2069 type = IF_XEN;
2070 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002071 } else if (!strcmp(buf, "none")) {
2072 type = IF_NONE;
2073 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002074 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002075 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002076 return NULL;
thse4bcb142007-12-02 04:51:10 +00002077 }
2078 }
2079
thse4bcb142007-12-02 04:51:10 +00002080 if (cyls || heads || secs) {
2081 if (cyls < 1 || cyls > 16383) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002082 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002083 return NULL;
thse4bcb142007-12-02 04:51:10 +00002084 }
2085 if (heads < 1 || heads > 16) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002086 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002087 return NULL;
thse4bcb142007-12-02 04:51:10 +00002088 }
2089 if (secs < 1 || secs > 63) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002090 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002091 return NULL;
thse4bcb142007-12-02 04:51:10 +00002092 }
2093 }
2094
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002095 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002096 if (!cyls) {
2097 fprintf(stderr,
2098 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002099 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002100 return NULL;
thse4bcb142007-12-02 04:51:10 +00002101 }
2102 if (!strcmp(buf, "none"))
2103 translation = BIOS_ATA_TRANSLATION_NONE;
2104 else if (!strcmp(buf, "lba"))
2105 translation = BIOS_ATA_TRANSLATION_LBA;
2106 else if (!strcmp(buf, "auto"))
2107 translation = BIOS_ATA_TRANSLATION_AUTO;
2108 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002109 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002110 return NULL;
thse4bcb142007-12-02 04:51:10 +00002111 }
2112 }
2113
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002114 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002115 if (!strcmp(buf, "disk")) {
2116 media = MEDIA_DISK;
2117 } else if (!strcmp(buf, "cdrom")) {
2118 if (cyls || secs || heads) {
2119 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002120 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002121 return NULL;
thse4bcb142007-12-02 04:51:10 +00002122 }
2123 media = MEDIA_CDROM;
2124 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002125 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002126 return NULL;
thse4bcb142007-12-02 04:51:10 +00002127 }
2128 }
2129
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002130 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002131 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002132 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002133 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002134 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002135 else if (!strcmp(buf, "writeback"))
2136 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002137 else {
2138 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002139 return NULL;
balrog33f00272007-12-24 14:33:24 +00002140 }
2141 }
2142
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002143#ifdef CONFIG_LINUX_AIO
2144 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2145 if (!strcmp(buf, "threads"))
2146 aio = 0;
2147 else if (!strcmp(buf, "native"))
2148 aio = 1;
2149 else {
2150 fprintf(stderr, "qemu: invalid aio option\n");
2151 return NULL;
2152 }
2153 }
2154#endif
2155
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002156 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002157 if (strcmp(buf, "?") == 0) {
2158 fprintf(stderr, "qemu: Supported formats:");
2159 bdrv_iterate_format(bdrv_format_print, NULL);
2160 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002161 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002162 }
aurel321e72d3b2008-04-28 20:26:45 +00002163 drv = bdrv_find_format(buf);
2164 if (!drv) {
2165 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002166 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002167 }
2168 }
2169
aliguoricdad4bd2009-02-28 16:51:01 +00002170 onerror = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002171 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002172 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002173 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002174 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002175 }
2176 if (!strcmp(buf, "ignore"))
2177 onerror = BLOCK_ERR_IGNORE;
2178 else if (!strcmp(buf, "enospc"))
2179 onerror = BLOCK_ERR_STOP_ENOSPC;
2180 else if (!strcmp(buf, "stop"))
2181 onerror = BLOCK_ERR_STOP_ANY;
2182 else if (!strcmp(buf, "report"))
2183 onerror = BLOCK_ERR_REPORT;
2184 else {
2185 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002186 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002187 }
2188 }
2189
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002190 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002191 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002192 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002193 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002194 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002195 }
2196
thse4bcb142007-12-02 04:51:10 +00002197 /* compute bus and unit according index */
2198
2199 if (index != -1) {
2200 if (bus_id != 0 || unit_id != -1) {
2201 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002202 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002203 return NULL;
thse4bcb142007-12-02 04:51:10 +00002204 }
2205 if (max_devs == 0)
2206 {
2207 unit_id = index;
2208 bus_id = 0;
2209 } else {
2210 unit_id = index % max_devs;
2211 bus_id = index / max_devs;
2212 }
2213 }
2214
2215 /* if user doesn't specify a unit_id,
2216 * try to find the first free
2217 */
2218
2219 if (unit_id == -1) {
2220 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002221 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002222 unit_id++;
2223 if (max_devs && unit_id >= max_devs) {
2224 unit_id -= max_devs;
2225 bus_id++;
2226 }
2227 }
2228 }
2229
2230 /* check unit id */
2231
2232 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002233 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2234 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002235 return NULL;
thse4bcb142007-12-02 04:51:10 +00002236 }
2237
2238 /*
2239 * ignore multiple definitions
2240 */
2241
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002242 if (drive_get(type, bus_id, unit_id) != NULL) {
2243 *fatal_error = 0;
2244 return NULL;
2245 }
thse4bcb142007-12-02 04:51:10 +00002246
2247 /* init */
2248
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002249 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002250 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002251 dinfo->id = qemu_strdup(buf);
2252 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002253 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002254 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002255 if (type == IF_IDE || type == IF_SCSI)
2256 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2257 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002258 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002259 devname, bus_id, mediastr, unit_id);
2260 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002261 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002262 devname, mediastr, unit_id);
2263 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002264 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002265 dinfo->devaddr = devaddr;
2266 dinfo->type = type;
2267 dinfo->bus = bus_id;
2268 dinfo->unit = unit_id;
2269 dinfo->onerror = onerror;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002270 dinfo->opts = opts;
2271 if (serial)
2272 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002273 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002274
thsf60d39b2007-12-17 03:55:57 +00002275 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002276 case IF_IDE:
2277 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002278 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002279 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002280 switch(media) {
2281 case MEDIA_DISK:
2282 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002283 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2284 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002285 }
2286 break;
2287 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002288 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002289 break;
2290 }
2291 break;
2292 case IF_SD:
2293 /* FIXME: This isn't really a floppy, but it's a reasonable
2294 approximation. */
2295 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002296 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002297 break;
2298 case IF_PFLASH:
2299 case IF_MTD:
2300 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002301 case IF_VIRTIO:
2302 /* add virtio block device */
2303 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2304 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2305 qemu_opt_set(opts, "drive", dinfo->id);
2306 if (devaddr)
2307 qemu_opt_set(opts, "addr", devaddr);
2308 break;
Paul Brookaae94602009-05-14 22:35:06 +01002309 case IF_COUNT:
2310 abort();
thse4bcb142007-12-02 04:51:10 +00002311 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002312 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002313 *fatal_error = 0;
2314 return NULL;
2315 }
balrog33f00272007-12-24 14:33:24 +00002316 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002317 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002318 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002319 cache = 2; /* always use write-back with snapshot */
2320 }
2321 if (cache == 0) /* no caching */
2322 bdrv_flags |= BDRV_O_NOCACHE;
2323 else if (cache == 2) /* write-back */
2324 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002325
2326 if (aio == 1) {
2327 bdrv_flags |= BDRV_O_NATIVE_AIO;
2328 } else {
2329 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2330 }
2331
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002332 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002333 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2334 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002335 return NULL;
thse4bcb142007-12-02 04:51:10 +00002336 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002337
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002338 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002339 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002340 *fatal_error = 0;
2341 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002342}
2343
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002344static int drive_init_func(QemuOpts *opts, void *opaque)
2345{
2346 QEMUMachine *machine = opaque;
2347 int fatal_error = 0;
2348
2349 if (drive_init(opts, machine, &fatal_error) == NULL) {
2350 if (fatal_error)
2351 return 1;
2352 }
2353 return 0;
2354}
2355
2356static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2357{
2358 if (NULL == qemu_opt_get(opts, "snapshot")) {
2359 qemu_opt_set(opts, "snapshot", "on");
2360 }
2361 return 0;
2362}
2363
Jan Kiszka76e30d02009-07-02 00:19:02 +02002364void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2365{
2366 boot_set_handler = func;
2367 boot_set_opaque = opaque;
2368}
2369
2370int qemu_boot_set(const char *boot_devices)
2371{
2372 if (!boot_set_handler) {
2373 return -EINVAL;
2374 }
2375 return boot_set_handler(boot_set_opaque, boot_devices);
2376}
2377
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002378static int parse_bootdevices(char *devices)
2379{
2380 /* We just do some generic consistency checks */
2381 const char *p;
2382 int bitmap = 0;
2383
2384 for (p = devices; *p != '\0'; p++) {
2385 /* Allowed boot devices are:
2386 * a-b: floppy disk drives
2387 * c-f: IDE disk drives
2388 * g-m: machine implementation dependant drives
2389 * n-p: network devices
2390 * It's up to each machine implementation to check if the given boot
2391 * devices match the actual hardware implementation and firmware
2392 * features.
2393 */
2394 if (*p < 'a' || *p > 'p') {
2395 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2396 exit(1);
2397 }
2398 if (bitmap & (1 << (*p - 'a'))) {
2399 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2400 exit(1);
2401 }
2402 bitmap |= 1 << (*p - 'a');
2403 }
2404 return bitmap;
2405}
2406
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002407static void restore_boot_devices(void *opaque)
2408{
2409 char *standard_boot_devices = opaque;
2410
2411 qemu_boot_set(standard_boot_devices);
2412
2413 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2414 qemu_free(standard_boot_devices);
2415}
2416
aliguori268a3622009-04-21 22:30:27 +00002417static void numa_add(const char *optarg)
2418{
2419 char option[128];
2420 char *endptr;
2421 unsigned long long value, endvalue;
2422 int nodenr;
2423
2424 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2425 if (!strcmp(option, "node")) {
2426 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2427 nodenr = nb_numa_nodes;
2428 } else {
2429 nodenr = strtoull(option, NULL, 10);
2430 }
2431
2432 if (get_param_value(option, 128, "mem", optarg) == 0) {
2433 node_mem[nodenr] = 0;
2434 } else {
2435 value = strtoull(option, &endptr, 0);
2436 switch (*endptr) {
2437 case 0: case 'M': case 'm':
2438 value <<= 20;
2439 break;
2440 case 'G': case 'g':
2441 value <<= 30;
2442 break;
2443 }
2444 node_mem[nodenr] = value;
2445 }
2446 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2447 node_cpumask[nodenr] = 0;
2448 } else {
2449 value = strtoull(option, &endptr, 10);
2450 if (value >= 64) {
2451 value = 63;
2452 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2453 } else {
2454 if (*endptr == '-') {
2455 endvalue = strtoull(endptr+1, &endptr, 10);
2456 if (endvalue >= 63) {
2457 endvalue = 62;
2458 fprintf(stderr,
2459 "only 63 CPUs in NUMA mode supported.\n");
2460 }
2461 value = (1 << (endvalue + 1)) - (1 << value);
2462 } else {
2463 value = 1 << value;
2464 }
2465 }
2466 node_cpumask[nodenr] = value;
2467 }
2468 nb_numa_nodes++;
2469 }
2470 return;
2471}
2472
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002473static void smp_parse(const char *optarg)
2474{
2475 int smp, sockets = 0, threads = 0, cores = 0;
2476 char *endptr;
2477 char option[128];
2478
2479 smp = strtoul(optarg, &endptr, 10);
2480 if (endptr != optarg) {
2481 if (*endptr == ',') {
2482 endptr++;
2483 }
2484 }
2485 if (get_param_value(option, 128, "sockets", endptr) != 0)
2486 sockets = strtoull(option, NULL, 10);
2487 if (get_param_value(option, 128, "cores", endptr) != 0)
2488 cores = strtoull(option, NULL, 10);
2489 if (get_param_value(option, 128, "threads", endptr) != 0)
2490 threads = strtoull(option, NULL, 10);
2491 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2492 max_cpus = strtoull(option, NULL, 10);
2493
2494 /* compute missing values, prefer sockets over cores over threads */
2495 if (smp == 0 || sockets == 0) {
2496 sockets = sockets > 0 ? sockets : 1;
2497 cores = cores > 0 ? cores : 1;
2498 threads = threads > 0 ? threads : 1;
2499 if (smp == 0) {
2500 smp = cores * threads * sockets;
2501 } else {
2502 sockets = smp / (cores * threads);
2503 }
2504 } else {
2505 if (cores == 0) {
2506 threads = threads > 0 ? threads : 1;
2507 cores = smp / (sockets * threads);
2508 } else {
2509 if (sockets == 0) {
2510 sockets = smp / (cores * threads);
2511 } else {
2512 threads = smp / (cores * sockets);
2513 }
2514 }
2515 }
2516 smp_cpus = smp;
2517 smp_cores = cores > 0 ? cores : 1;
2518 smp_threads = threads > 0 ? threads : 1;
2519 if (max_cpus == 0)
2520 max_cpus = smp_cpus;
2521}
2522
bellard330d0412003-07-26 18:11:40 +00002523/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002524/* USB devices */
2525
aliguoribb5fc202009-03-05 23:01:15 +00002526static void usb_msd_password_cb(void *opaque, int err)
2527{
2528 USBDevice *dev = opaque;
2529
2530 if (!err)
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002531 usb_device_attach(dev);
aliguoribb5fc202009-03-05 23:01:15 +00002532 else
Gerd Hoffmann806b6022009-08-31 14:23:59 +02002533 dev->info->handle_destroy(dev);
aliguoribb5fc202009-03-05 23:01:15 +00002534}
2535
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002536static struct {
2537 const char *name;
2538 const char *qdev;
2539} usbdevs[] = {
2540 {
2541 .name = "mouse",
2542 .qdev = "QEMU USB Mouse",
2543 },{
2544 .name = "tablet",
2545 .qdev = "QEMU USB Tablet",
2546 },{
2547 .name = "keyboard",
2548 .qdev = "QEMU USB Keyboard",
2549 },{
2550 .name = "wacom-tablet",
2551 .qdev = "QEMU PenPartner Tablet",
2552 }
2553};
2554
aliguoric0f4ce72009-03-05 23:01:01 +00002555static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002556{
2557 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002558 USBBus *bus = usb_bus_find(-1 /* any */);
2559 USBDevice *dev = NULL;
2560 int i;
bellarda594cfb2005-11-06 16:13:29 +00002561
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002562 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002563 return -1;
2564
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002565 /* simple devices which don't need extra care */
2566 for (i = 0; i < ARRAY_SIZE(usbdevs); i++) {
2567 if (strcmp(devname, usbdevs[i].name) != 0)
2568 continue;
2569 dev = usb_create_simple(bus, usbdevs[i].qdev);
2570 goto done;
2571 }
2572
2573 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002574 if (strstart(devname, "host:", &p)) {
2575 dev = usb_host_device_open(p);
pbrook2e5d83b2006-05-25 23:58:51 +00002576 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002577 BlockDriverState *bs;
2578
aliguoribb5fc202009-03-05 23:01:15 +00002579 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002580 if (!dev)
2581 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002582 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002583 if (bdrv_key_required(bs)) {
2584 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002585 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002586 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2587 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002588 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002589 }
2590 }
balroga7954212008-01-14 03:41:02 +00002591 } else if (strstart(devname, "serial:", &p)) {
2592 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002593#ifdef CONFIG_BRLAPI
2594 } else if (!strcmp(devname, "braille")) {
2595 dev = usb_baum_init();
2596#endif
balrog6c9f8862008-07-17 20:47:13 +00002597 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002598 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002599
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002600 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002601 return -1;
Mark McLoughlin3cd67992009-10-06 12:16:52 +01002602 nd_table[nic].model = qemu_strdup("usb");
balrog9ad97e62008-07-29 13:16:31 +00002603 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002604 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2605 dev = usb_bt_init(devname[2] ? hci_init(p) :
2606 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002607 } else {
2608 return -1;
2609 }
pbrook0d92ed32006-05-21 16:30:15 +00002610 if (!dev)
2611 return -1;
2612
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002613done:
bellarda594cfb2005-11-06 16:13:29 +00002614 return 0;
2615}
2616
aliguori1f3870a2008-08-21 19:27:48 +00002617static int usb_device_del(const char *devname)
2618{
2619 int bus_num, addr;
2620 const char *p;
2621
aliguori5d0c5752008-09-14 01:07:41 +00002622 if (strstart(devname, "host:", &p))
2623 return usb_host_device_close(p);
2624
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002625 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002626 return -1;
2627
2628 p = strchr(devname, '.');
2629 if (!p)
2630 return -1;
2631 bus_num = strtoul(devname, NULL, 0);
2632 addr = strtoul(p + 1, NULL, 0);
2633
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002634 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002635}
2636
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002637static int usb_parse(const char *cmdline)
2638{
2639 return usb_device_add(cmdline, 0);
2640}
2641
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002642void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002643{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002644 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002645}
2646
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002647void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002648{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002649 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002650}
2651
bellardf7cce892004-12-08 22:21:25 +00002652/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002653/* PCMCIA/Cardbus */
2654
2655static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002656 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002657 struct pcmcia_socket_entry_s *next;
2658} *pcmcia_sockets = 0;
2659
Paul Brookbc24a222009-05-10 01:44:56 +01002660void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002661{
2662 struct pcmcia_socket_entry_s *entry;
2663
2664 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2665 entry->socket = socket;
2666 entry->next = pcmcia_sockets;
2667 pcmcia_sockets = entry;
2668}
2669
Paul Brookbc24a222009-05-10 01:44:56 +01002670void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002671{
2672 struct pcmcia_socket_entry_s *entry, **ptr;
2673
2674 ptr = &pcmcia_sockets;
2675 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2676 if (entry->socket == socket) {
2677 *ptr = entry->next;
2678 qemu_free(entry);
2679 }
2680}
2681
aliguori376253e2009-03-05 23:01:23 +00002682void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002683{
2684 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002685
balrog201a51f2007-04-30 00:51:09 +00002686 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002687 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002688
2689 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002690 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2691 iter->socket->attached ? iter->socket->card_string :
2692 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002693}
2694
2695/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002696/* register display */
2697
aliguori7b5d76d2009-03-13 15:02:13 +00002698struct DisplayAllocator default_allocator = {
2699 defaultallocator_create_displaysurface,
2700 defaultallocator_resize_displaysurface,
2701 defaultallocator_free_displaysurface
2702};
2703
aliguori3023f332009-01-16 19:04:14 +00002704void register_displaystate(DisplayState *ds)
2705{
2706 DisplayState **s;
2707 s = &display_state;
2708 while (*s != NULL)
2709 s = &(*s)->next;
2710 ds->next = NULL;
2711 *s = ds;
2712}
2713
2714DisplayState *get_displaystate(void)
2715{
2716 return display_state;
2717}
2718
aliguori7b5d76d2009-03-13 15:02:13 +00002719DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2720{
2721 if(ds->allocator == &default_allocator) ds->allocator = da;
2722 return ds->allocator;
2723}
2724
ths2ff89792007-06-21 23:34:19 +00002725/* dumb display */
2726
aliguori8f391ab2009-01-19 16:34:10 +00002727static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002728{
aliguori8f391ab2009-01-19 16:34:10 +00002729 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002730 ds->allocator = &default_allocator;
2731 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002732 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002733}
2734
2735/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002736/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002737
bellardc4b1fcc2004-03-14 21:44:30 +00002738typedef struct IOHandlerRecord {
2739 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002740 IOCanRWHandler *fd_read_poll;
2741 IOHandler *fd_read;
2742 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002743 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002744 void *opaque;
2745 /* temporary data */
2746 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002747 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002748} IOHandlerRecord;
2749
bellard8a7ddc32004-03-31 19:00:16 +00002750static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002751
bellard7c9d8e02005-11-15 22:16:05 +00002752/* XXX: fd_read_poll should be suppressed, but an API change is
2753 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002754int qemu_set_fd_handler2(int fd,
2755 IOCanRWHandler *fd_read_poll,
2756 IOHandler *fd_read,
2757 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002758 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002759{
bellard8a7ddc32004-03-31 19:00:16 +00002760 IOHandlerRecord **pioh, *ioh;
2761
bellard7c9d8e02005-11-15 22:16:05 +00002762 if (!fd_read && !fd_write) {
2763 pioh = &first_io_handler;
2764 for(;;) {
2765 ioh = *pioh;
2766 if (ioh == NULL)
2767 break;
2768 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002769 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002770 break;
2771 }
2772 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002773 }
bellard7c9d8e02005-11-15 22:16:05 +00002774 } else {
2775 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2776 if (ioh->fd == fd)
2777 goto found;
2778 }
2779 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002780 ioh->next = first_io_handler;
2781 first_io_handler = ioh;
2782 found:
2783 ioh->fd = fd;
2784 ioh->fd_read_poll = fd_read_poll;
2785 ioh->fd_read = fd_read;
2786 ioh->fd_write = fd_write;
2787 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002788 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002789 }
bellard7c9d8e02005-11-15 22:16:05 +00002790 return 0;
2791}
2792
ths5fafdf22007-09-16 21:08:06 +00002793int qemu_set_fd_handler(int fd,
2794 IOHandler *fd_read,
2795 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002796 void *opaque)
2797{
2798 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002799}
2800
aliguori56f3a5d2008-10-31 18:07:17 +00002801#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002802/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002803/* Polling handling */
2804
2805typedef struct PollingEntry {
2806 PollingFunc *func;
2807 void *opaque;
2808 struct PollingEntry *next;
2809} PollingEntry;
2810
2811static PollingEntry *first_polling_entry;
2812
2813int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2814{
2815 PollingEntry **ppe, *pe;
2816 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002817 pe->func = func;
2818 pe->opaque = opaque;
2819 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2820 *ppe = pe;
2821 return 0;
2822}
2823
2824void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2825{
2826 PollingEntry **ppe, *pe;
2827 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2828 pe = *ppe;
2829 if (pe->func == func && pe->opaque == opaque) {
2830 *ppe = pe->next;
2831 qemu_free(pe);
2832 break;
2833 }
2834 }
2835}
2836
bellarda18e5242006-06-25 17:18:27 +00002837/***********************************************************/
2838/* Wait objects support */
2839typedef struct WaitObjects {
2840 int num;
2841 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2842 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2843 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2844} WaitObjects;
2845
2846static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002847
bellarda18e5242006-06-25 17:18:27 +00002848int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2849{
2850 WaitObjects *w = &wait_objects;
2851
2852 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2853 return -1;
2854 w->events[w->num] = handle;
2855 w->func[w->num] = func;
2856 w->opaque[w->num] = opaque;
2857 w->num++;
2858 return 0;
2859}
2860
2861void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2862{
2863 int i, found;
2864 WaitObjects *w = &wait_objects;
2865
2866 found = 0;
2867 for (i = 0; i < w->num; i++) {
2868 if (w->events[i] == handle)
2869 found = 1;
2870 if (found) {
2871 w->events[i] = w->events[i + 1];
2872 w->func[i] = w->func[i + 1];
2873 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002874 }
bellarda18e5242006-06-25 17:18:27 +00002875 }
2876 if (found)
2877 w->num--;
2878}
2879#endif
2880
bellard8a7ddc32004-03-31 19:00:16 +00002881/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002882/* ram save/restore */
2883
Juan Quintela94fb0902009-09-10 03:04:23 +02002884#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002885#define RAM_SAVE_FLAG_COMPRESS 0x02
2886#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2887#define RAM_SAVE_FLAG_PAGE 0x08
2888#define RAM_SAVE_FLAG_EOS 0x10
2889
2890static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002891{
aliguori475e4272008-10-06 20:21:51 +00002892 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2893 uint32_t *array = (uint32_t *)page;
2894 int i;
ths3b46e622007-09-17 08:09:54 +00002895
aliguori475e4272008-10-06 20:21:51 +00002896 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2897 if (array[i] != val)
2898 return 0;
bellardc88676f2006-08-06 13:36:11 +00002899 }
aliguori475e4272008-10-06 20:21:51 +00002900
2901 return 1;
bellardc88676f2006-08-06 13:36:11 +00002902}
2903
aliguori475e4272008-10-06 20:21:51 +00002904static int ram_save_block(QEMUFile *f)
2905{
Anthony Liguoric227f092009-10-01 16:12:16 -05002906 static ram_addr_t current_addr = 0;
2907 ram_addr_t saved_addr = current_addr;
2908 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002909 int found = 0;
2910
pbrook94a6b542009-04-11 17:15:54 +00002911 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002912 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002913 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002914
2915 cpu_physical_memory_reset_dirty(current_addr,
2916 current_addr + TARGET_PAGE_SIZE,
2917 MIGRATION_DIRTY_FLAG);
2918
pbrook5579c7f2009-04-11 14:47:08 +00002919 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002920
pbrook5579c7f2009-04-11 14:47:08 +00002921 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002922 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002923 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002924 } else {
2925 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002926 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002927 }
2928
2929 found = 1;
2930 break;
2931 }
2932 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002933 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002934 }
2935
2936 return found;
2937}
2938
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002939static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00002940
Anthony Liguoric227f092009-10-01 16:12:16 -05002941static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002942{
Anthony Liguoric227f092009-10-01 16:12:16 -05002943 ram_addr_t addr;
2944 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002945
pbrook94a6b542009-04-11 17:15:54 +00002946 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002947 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2948 count++;
2949 }
2950
2951 return count;
2952}
2953
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002954uint64_t ram_bytes_remaining(void)
2955{
2956 return ram_save_remaining() * TARGET_PAGE_SIZE;
2957}
2958
2959uint64_t ram_bytes_transferred(void)
2960{
2961 return bytes_transferred;
2962}
2963
2964uint64_t ram_bytes_total(void)
2965{
2966 return last_ram_offset;
2967}
2968
aliguori475e4272008-10-06 20:21:51 +00002969static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2970{
Anthony Liguoric227f092009-10-01 16:12:16 -05002971 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002972 uint64_t bytes_transferred_last;
2973 double bwidth = 0;
2974 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002975
Jan Kiszka9fa06382009-05-22 23:51:45 +02002976 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002977 qemu_file_set_error(f);
2978 return 0;
2979 }
2980
aliguori475e4272008-10-06 20:21:51 +00002981 if (stage == 1) {
2982 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002983 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002984 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2985 cpu_physical_memory_set_dirty(addr);
2986 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002987
aliguori475e4272008-10-06 20:21:51 +00002988 /* Enable dirty memory tracking */
2989 cpu_physical_memory_set_dirty_tracking(1);
2990
pbrook94a6b542009-04-11 17:15:54 +00002991 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002992 }
2993
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002994 bytes_transferred_last = bytes_transferred;
2995 bwidth = get_clock();
2996
aliguori475e4272008-10-06 20:21:51 +00002997 while (!qemu_file_rate_limit(f)) {
2998 int ret;
2999
3000 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003001 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003002 if (ret == 0) /* no more blocks */
3003 break;
3004 }
3005
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003006 bwidth = get_clock() - bwidth;
3007 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3008
3009 /* if we haven't transferred anything this round, force expected_time to a
3010 * a very high value, but without crashing */
3011 if (bwidth == 0)
3012 bwidth = 0.000001;
3013
aliguori475e4272008-10-06 20:21:51 +00003014 /* try transferring iterative blocks of memory */
3015
3016 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003017
3018 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003019 while (ram_save_block(f) != 0) {
3020 bytes_transferred += TARGET_PAGE_SIZE;
3021 }
aliguori8215e912009-04-05 19:30:55 +00003022 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003023 }
3024
3025 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3026
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003027 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3028
3029 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003030}
3031
aliguori475e4272008-10-06 20:21:51 +00003032static int ram_load(QEMUFile *f, void *opaque, int version_id)
3033{
Anthony Liguoric227f092009-10-01 16:12:16 -05003034 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003035 int flags;
3036
aliguori475e4272008-10-06 20:21:51 +00003037 if (version_id != 3)
3038 return -EINVAL;
3039
3040 do {
3041 addr = qemu_get_be64(f);
3042
3043 flags = addr & ~TARGET_PAGE_MASK;
3044 addr &= TARGET_PAGE_MASK;
3045
3046 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003047 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003048 return -EINVAL;
3049 }
3050
aliguori475e4272008-10-06 20:21:51 +00003051 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3052 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003053 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3054#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003055 if (ch == 0 &&
3056 (!kvm_enabled() || kvm_has_sync_mmu())) {
3057 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003058 }
Anthony Liguori30868442009-06-17 16:46:12 -05003059#endif
aliguori475e4272008-10-06 20:21:51 +00003060 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003061 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003062 } while (!(flags & RAM_SAVE_FLAG_EOS));
3063
bellardc88676f2006-08-06 13:36:11 +00003064 return 0;
3065}
3066
aliguori9e472e12008-10-08 19:50:24 +00003067void qemu_service_io(void)
3068{
aliguorid9f75a42009-04-24 18:03:11 +00003069 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003070}
3071
bellard8a7ddc32004-03-31 19:00:16 +00003072/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003073/* bottom halves (can be seen as timers which expire ASAP) */
3074
3075struct QEMUBH {
3076 QEMUBHFunc *cb;
3077 void *opaque;
3078 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003079 int idle;
3080 int deleted;
bellard83f64092006-08-01 16:21:11 +00003081 QEMUBH *next;
3082};
3083
3084static QEMUBH *first_bh = NULL;
3085
3086QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3087{
3088 QEMUBH *bh;
3089 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003090 bh->cb = cb;
3091 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003092 bh->next = first_bh;
3093 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003094 return bh;
3095}
3096
bellard6eb57332006-08-06 09:51:25 +00003097int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003098{
aliguori1b435b12008-10-31 17:24:21 +00003099 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003100 int ret;
bellard83f64092006-08-01 16:21:11 +00003101
bellard6eb57332006-08-06 09:51:25 +00003102 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003103 for (bh = first_bh; bh; bh = bh->next) {
3104 if (!bh->deleted && bh->scheduled) {
3105 bh->scheduled = 0;
3106 if (!bh->idle)
3107 ret = 1;
3108 bh->idle = 0;
3109 bh->cb(bh->opaque);
3110 }
bellard83f64092006-08-01 16:21:11 +00003111 }
aliguori1b435b12008-10-31 17:24:21 +00003112
3113 /* remove deleted bhs */
3114 bhp = &first_bh;
3115 while (*bhp) {
3116 bh = *bhp;
3117 if (bh->deleted) {
3118 *bhp = bh->next;
3119 qemu_free(bh);
3120 } else
3121 bhp = &bh->next;
3122 }
3123
bellard6eb57332006-08-06 09:51:25 +00003124 return ret;
bellard83f64092006-08-01 16:21:11 +00003125}
3126
aliguori1b435b12008-10-31 17:24:21 +00003127void qemu_bh_schedule_idle(QEMUBH *bh)
3128{
3129 if (bh->scheduled)
3130 return;
3131 bh->scheduled = 1;
3132 bh->idle = 1;
3133}
3134
bellard83f64092006-08-01 16:21:11 +00003135void qemu_bh_schedule(QEMUBH *bh)
3136{
bellard83f64092006-08-01 16:21:11 +00003137 if (bh->scheduled)
3138 return;
3139 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003140 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003141 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003142 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003143}
3144
3145void qemu_bh_cancel(QEMUBH *bh)
3146{
aliguori1b435b12008-10-31 17:24:21 +00003147 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003148}
3149
3150void qemu_bh_delete(QEMUBH *bh)
3151{
aliguori1b435b12008-10-31 17:24:21 +00003152 bh->scheduled = 0;
3153 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003154}
3155
aliguori56f3a5d2008-10-31 18:07:17 +00003156static void qemu_bh_update_timeout(int *timeout)
3157{
3158 QEMUBH *bh;
3159
3160 for (bh = first_bh; bh; bh = bh->next) {
3161 if (!bh->deleted && bh->scheduled) {
3162 if (bh->idle) {
3163 /* idle bottom halves will be polled at least
3164 * every 10ms */
3165 *timeout = MIN(10, *timeout);
3166 } else {
3167 /* non-idle bottom halves will be executed
3168 * immediately */
3169 *timeout = 0;
3170 break;
3171 }
3172 }
3173 }
3174}
3175
bellard83f64092006-08-01 16:21:11 +00003176/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003177/* machine registration */
3178
blueswir1bdaf78e2008-10-04 07:24:27 +00003179static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003180QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003181
3182int qemu_register_machine(QEMUMachine *m)
3183{
3184 QEMUMachine **pm;
3185 pm = &first_machine;
3186 while (*pm != NULL)
3187 pm = &(*pm)->next;
3188 m->next = NULL;
3189 *pm = m;
3190 return 0;
3191}
3192
pbrook9596ebb2007-11-18 01:44:38 +00003193static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003194{
3195 QEMUMachine *m;
3196
3197 for(m = first_machine; m != NULL; m = m->next) {
3198 if (!strcmp(m->name, name))
3199 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003200 if (m->alias && !strcmp(m->alias, name))
3201 return m;
bellardcc1daa42005-06-05 14:49:17 +00003202 }
3203 return NULL;
3204}
3205
Anthony Liguori0c257432009-05-21 20:41:01 -05003206static QEMUMachine *find_default_machine(void)
3207{
3208 QEMUMachine *m;
3209
3210 for(m = first_machine; m != NULL; m = m->next) {
3211 if (m->is_default) {
3212 return m;
3213 }
3214 }
3215 return NULL;
3216}
3217
bellardcc1daa42005-06-05 14:49:17 +00003218/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003219/* main execution loop */
3220
pbrook9596ebb2007-11-18 01:44:38 +00003221static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003222{
aliguori7d957bd2009-01-15 22:14:11 +00003223 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003224 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003225 DisplayChangeListener *dcl = ds->listeners;
3226
3227 dpy_refresh(ds);
3228
3229 while (dcl != NULL) {
3230 if (dcl->gui_timer_interval &&
3231 dcl->gui_timer_interval < interval)
3232 interval = dcl->gui_timer_interval;
3233 dcl = dcl->next;
3234 }
3235 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003236}
3237
blueswir19043b622009-01-21 19:28:13 +00003238static void nographic_update(void *opaque)
3239{
3240 uint64_t interval = GUI_REFRESH_INTERVAL;
3241
3242 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3243}
3244
bellard0bd48852005-11-11 00:00:47 +00003245struct vm_change_state_entry {
3246 VMChangeStateHandler *cb;
3247 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003248 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003249};
3250
Blue Swirl72cf2d42009-09-12 07:36:22 +00003251static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003252
3253VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3254 void *opaque)
3255{
3256 VMChangeStateEntry *e;
3257
3258 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003259
3260 e->cb = cb;
3261 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003262 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003263 return e;
3264}
3265
3266void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3267{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003268 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003269 qemu_free (e);
3270}
3271
aliguori9781e042009-01-22 17:15:29 +00003272static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003273{
3274 VMChangeStateEntry *e;
3275
3276 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003277 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003278 }
3279}
3280
aliguorid6dc3d42009-04-24 18:04:07 +00003281static void resume_all_vcpus(void);
3282static void pause_all_vcpus(void);
3283
bellard8a7ddc32004-03-31 19:00:16 +00003284void vm_start(void)
3285{
3286 if (!vm_running) {
3287 cpu_enable_ticks();
3288 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003289 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003290 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003291 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003292 }
3293}
3294
bellardbb0c6722004-06-20 12:37:32 +00003295/* reset/shutdown handler */
3296
3297typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003298 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003299 QEMUResetHandler *func;
3300 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003301} QEMUResetEntry;
3302
Blue Swirl72cf2d42009-09-12 07:36:22 +00003303static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3304 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003305static int reset_requested;
3306static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003307static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003308static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003309static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003310
aurel32cf7a2fe2008-03-18 06:53:05 +00003311int qemu_shutdown_requested(void)
3312{
3313 int r = shutdown_requested;
3314 shutdown_requested = 0;
3315 return r;
3316}
3317
3318int qemu_reset_requested(void)
3319{
3320 int r = reset_requested;
3321 reset_requested = 0;
3322 return r;
3323}
3324
3325int qemu_powerdown_requested(void)
3326{
3327 int r = powerdown_requested;
3328 powerdown_requested = 0;
3329 return r;
3330}
3331
aliguorie5689022009-04-24 18:03:54 +00003332static int qemu_debug_requested(void)
3333{
3334 int r = debug_requested;
3335 debug_requested = 0;
3336 return r;
3337}
3338
aliguori6e29f5d2009-04-24 18:04:02 +00003339static int qemu_vmstop_requested(void)
3340{
3341 int r = vmstop_requested;
3342 vmstop_requested = 0;
3343 return r;
3344}
3345
3346static void do_vm_stop(int reason)
3347{
3348 if (vm_running) {
3349 cpu_disable_ticks();
3350 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003351 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003352 vm_state_notify(0, reason);
3353 }
3354}
3355
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003356void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003357{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003358 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003359
bellardbb0c6722004-06-20 12:37:32 +00003360 re->func = func;
3361 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003362 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003363}
3364
Jan Kiszkadda9b292009-07-02 00:19:02 +02003365void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003366{
3367 QEMUResetEntry *re;
3368
Blue Swirl72cf2d42009-09-12 07:36:22 +00003369 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003370 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003371 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003372 qemu_free(re);
3373 return;
3374 }
3375 }
3376}
3377
3378void qemu_system_reset(void)
3379{
3380 QEMUResetEntry *re, *nre;
3381
3382 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003383 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003384 re->func(re->opaque);
3385 }
3386}
3387
3388void qemu_system_reset_request(void)
3389{
bellardd1beab82006-10-02 19:44:22 +00003390 if (no_reboot) {
3391 shutdown_requested = 1;
3392 } else {
3393 reset_requested = 1;
3394 }
aliguorid9f75a42009-04-24 18:03:11 +00003395 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003396}
3397
3398void qemu_system_shutdown_request(void)
3399{
3400 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003401 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003402}
3403
bellard34751872005-07-02 14:31:34 +00003404void qemu_system_powerdown_request(void)
3405{
3406 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003407 qemu_notify_event();
3408}
3409
aliguorid6dc3d42009-04-24 18:04:07 +00003410#ifdef CONFIG_IOTHREAD
3411static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003412{
aliguorid6dc3d42009-04-24 18:04:07 +00003413 vmstop_requested = reason;
3414 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003415}
aliguorid6dc3d42009-04-24 18:04:07 +00003416#endif
bellardbb0c6722004-06-20 12:37:32 +00003417
aliguori50317c72009-04-24 18:03:29 +00003418#ifndef _WIN32
3419static int io_thread_fd = -1;
3420
3421static void qemu_event_increment(void)
3422{
3423 static const char byte = 0;
3424
3425 if (io_thread_fd == -1)
3426 return;
3427
3428 write(io_thread_fd, &byte, sizeof(byte));
3429}
3430
3431static void qemu_event_read(void *opaque)
3432{
3433 int fd = (unsigned long)opaque;
3434 ssize_t len;
3435
3436 /* Drain the notify pipe */
3437 do {
3438 char buffer[512];
3439 len = read(fd, buffer, sizeof(buffer));
3440 } while ((len == -1 && errno == EINTR) || len > 0);
3441}
3442
3443static int qemu_event_init(void)
3444{
3445 int err;
3446 int fds[2];
3447
3448 err = pipe(fds);
3449 if (err == -1)
3450 return -errno;
3451
3452 err = fcntl_setfl(fds[0], O_NONBLOCK);
3453 if (err < 0)
3454 goto fail;
3455
3456 err = fcntl_setfl(fds[1], O_NONBLOCK);
3457 if (err < 0)
3458 goto fail;
3459
3460 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3461 (void *)(unsigned long)fds[0]);
3462
3463 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003464 return 0;
3465
aliguori50317c72009-04-24 18:03:29 +00003466fail:
3467 close(fds[0]);
3468 close(fds[1]);
3469 return err;
3470}
3471#else
3472HANDLE qemu_event_handle;
3473
3474static void dummy_event_handler(void *opaque)
3475{
3476}
3477
3478static int qemu_event_init(void)
3479{
3480 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3481 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003482 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003483 return -1;
3484 }
3485 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3486 return 0;
3487}
3488
3489static void qemu_event_increment(void)
3490{
malcde1c90c2009-09-27 14:38:18 +04003491 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003492 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003493 GetLastError());
3494 exit (1);
3495 }
aliguori50317c72009-04-24 18:03:29 +00003496}
3497#endif
3498
aliguorid6dc3d42009-04-24 18:04:07 +00003499static int cpu_can_run(CPUState *env)
3500{
3501 if (env->stop)
3502 return 0;
3503 if (env->stopped)
3504 return 0;
3505 return 1;
3506}
3507
3508#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003509static int qemu_init_main_loop(void)
3510{
aliguori50317c72009-04-24 18:03:29 +00003511 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003512}
3513
aliguori0bf46a42009-04-24 18:03:41 +00003514void qemu_init_vcpu(void *_env)
3515{
3516 CPUState *env = _env;
3517
3518 if (kvm_enabled())
3519 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003520 env->nr_cores = smp_cores;
3521 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003522 return;
3523}
3524
aliguori8edac962009-04-24 18:03:45 +00003525int qemu_cpu_self(void *env)
3526{
3527 return 1;
3528}
3529
aliguorid6dc3d42009-04-24 18:04:07 +00003530static void resume_all_vcpus(void)
3531{
3532}
3533
3534static void pause_all_vcpus(void)
3535{
3536}
3537
aliguori8edac962009-04-24 18:03:45 +00003538void qemu_cpu_kick(void *env)
3539{
3540 return;
3541}
3542
aliguorid6dc3d42009-04-24 18:04:07 +00003543void qemu_notify_event(void)
3544{
3545 CPUState *env = cpu_single_env;
3546
3547 if (env) {
3548 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003549 }
aliguorid6dc3d42009-04-24 18:04:07 +00003550}
3551
aliguori48708522009-04-24 18:03:49 +00003552#define qemu_mutex_lock_iothread() do { } while (0)
3553#define qemu_mutex_unlock_iothread() do { } while (0)
3554
aliguori6e29f5d2009-04-24 18:04:02 +00003555void vm_stop(int reason)
3556{
3557 do_vm_stop(reason);
3558}
3559
aliguorid6dc3d42009-04-24 18:04:07 +00003560#else /* CONFIG_IOTHREAD */
3561
3562#include "qemu-thread.h"
3563
3564QemuMutex qemu_global_mutex;
3565static QemuMutex qemu_fair_mutex;
3566
3567static QemuThread io_thread;
3568
3569static QemuThread *tcg_cpu_thread;
3570static QemuCond *tcg_halt_cond;
3571
3572static int qemu_system_ready;
3573/* cpu creation */
3574static QemuCond qemu_cpu_cond;
3575/* system init */
3576static QemuCond qemu_system_cond;
3577static QemuCond qemu_pause_cond;
3578
3579static void block_io_signals(void);
3580static void unblock_io_signals(void);
3581static int tcg_has_work(void);
3582
3583static int qemu_init_main_loop(void)
3584{
3585 int ret;
3586
3587 ret = qemu_event_init();
3588 if (ret)
3589 return ret;
3590
3591 qemu_cond_init(&qemu_pause_cond);
3592 qemu_mutex_init(&qemu_fair_mutex);
3593 qemu_mutex_init(&qemu_global_mutex);
3594 qemu_mutex_lock(&qemu_global_mutex);
3595
3596 unblock_io_signals();
3597 qemu_thread_self(&io_thread);
3598
3599 return 0;
3600}
3601
3602static void qemu_wait_io_event(CPUState *env)
3603{
3604 while (!tcg_has_work())
3605 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3606
3607 qemu_mutex_unlock(&qemu_global_mutex);
3608
3609 /*
3610 * Users of qemu_global_mutex can be starved, having no chance
3611 * to acquire it since this path will get to it first.
3612 * So use another lock to provide fairness.
3613 */
3614 qemu_mutex_lock(&qemu_fair_mutex);
3615 qemu_mutex_unlock(&qemu_fair_mutex);
3616
3617 qemu_mutex_lock(&qemu_global_mutex);
3618 if (env->stop) {
3619 env->stop = 0;
3620 env->stopped = 1;
3621 qemu_cond_signal(&qemu_pause_cond);
3622 }
3623}
3624
3625static int qemu_cpu_exec(CPUState *env);
3626
3627static void *kvm_cpu_thread_fn(void *arg)
3628{
3629 CPUState *env = arg;
3630
3631 block_io_signals();
3632 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003633 if (kvm_enabled())
3634 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003635
3636 /* signal CPU creation */
3637 qemu_mutex_lock(&qemu_global_mutex);
3638 env->created = 1;
3639 qemu_cond_signal(&qemu_cpu_cond);
3640
3641 /* and wait for machine initialization */
3642 while (!qemu_system_ready)
3643 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3644
3645 while (1) {
3646 if (cpu_can_run(env))
3647 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003648 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003649 }
3650
3651 return NULL;
3652}
3653
3654static void tcg_cpu_exec(void);
3655
3656static void *tcg_cpu_thread_fn(void *arg)
3657{
3658 CPUState *env = arg;
3659
3660 block_io_signals();
3661 qemu_thread_self(env->thread);
3662
3663 /* signal CPU creation */
3664 qemu_mutex_lock(&qemu_global_mutex);
3665 for (env = first_cpu; env != NULL; env = env->next_cpu)
3666 env->created = 1;
3667 qemu_cond_signal(&qemu_cpu_cond);
3668
3669 /* and wait for machine initialization */
3670 while (!qemu_system_ready)
3671 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3672
3673 while (1) {
3674 tcg_cpu_exec();
3675 qemu_wait_io_event(cur_cpu);
3676 }
3677
3678 return NULL;
3679}
3680
3681void qemu_cpu_kick(void *_env)
3682{
3683 CPUState *env = _env;
3684 qemu_cond_broadcast(env->halt_cond);
3685 if (kvm_enabled())
3686 qemu_thread_signal(env->thread, SIGUSR1);
3687}
3688
Glauber Costae5bc2012009-09-28 15:27:44 -03003689int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003690{
Glauber Costae5bc2012009-09-28 15:27:44 -03003691 CPUState *env = _env;
3692 QemuThread this;
3693
3694 qemu_thread_self(&this);
3695
3696 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003697}
3698
3699static void cpu_signal(int sig)
3700{
3701 if (cpu_single_env)
3702 cpu_exit(cpu_single_env);
3703}
3704
3705static void block_io_signals(void)
3706{
3707 sigset_t set;
3708 struct sigaction sigact;
3709
3710 sigemptyset(&set);
3711 sigaddset(&set, SIGUSR2);
3712 sigaddset(&set, SIGIO);
3713 sigaddset(&set, SIGALRM);
3714 pthread_sigmask(SIG_BLOCK, &set, NULL);
3715
3716 sigemptyset(&set);
3717 sigaddset(&set, SIGUSR1);
3718 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3719
3720 memset(&sigact, 0, sizeof(sigact));
3721 sigact.sa_handler = cpu_signal;
3722 sigaction(SIGUSR1, &sigact, NULL);
3723}
3724
3725static void unblock_io_signals(void)
3726{
3727 sigset_t set;
3728
3729 sigemptyset(&set);
3730 sigaddset(&set, SIGUSR2);
3731 sigaddset(&set, SIGIO);
3732 sigaddset(&set, SIGALRM);
3733 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3734
3735 sigemptyset(&set);
3736 sigaddset(&set, SIGUSR1);
3737 pthread_sigmask(SIG_BLOCK, &set, NULL);
3738}
3739
3740static void qemu_signal_lock(unsigned int msecs)
3741{
3742 qemu_mutex_lock(&qemu_fair_mutex);
3743
3744 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3745 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3746 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3747 break;
3748 }
3749 qemu_mutex_unlock(&qemu_fair_mutex);
3750}
3751
3752static void qemu_mutex_lock_iothread(void)
3753{
3754 if (kvm_enabled()) {
3755 qemu_mutex_lock(&qemu_fair_mutex);
3756 qemu_mutex_lock(&qemu_global_mutex);
3757 qemu_mutex_unlock(&qemu_fair_mutex);
3758 } else
3759 qemu_signal_lock(100);
3760}
3761
3762static void qemu_mutex_unlock_iothread(void)
3763{
3764 qemu_mutex_unlock(&qemu_global_mutex);
3765}
3766
3767static int all_vcpus_paused(void)
3768{
3769 CPUState *penv = first_cpu;
3770
3771 while (penv) {
3772 if (!penv->stopped)
3773 return 0;
3774 penv = (CPUState *)penv->next_cpu;
3775 }
3776
3777 return 1;
3778}
3779
3780static void pause_all_vcpus(void)
3781{
3782 CPUState *penv = first_cpu;
3783
3784 while (penv) {
3785 penv->stop = 1;
3786 qemu_thread_signal(penv->thread, SIGUSR1);
3787 qemu_cpu_kick(penv);
3788 penv = (CPUState *)penv->next_cpu;
3789 }
3790
3791 while (!all_vcpus_paused()) {
3792 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3793 penv = first_cpu;
3794 while (penv) {
3795 qemu_thread_signal(penv->thread, SIGUSR1);
3796 penv = (CPUState *)penv->next_cpu;
3797 }
3798 }
3799}
3800
3801static void resume_all_vcpus(void)
3802{
3803 CPUState *penv = first_cpu;
3804
3805 while (penv) {
3806 penv->stop = 0;
3807 penv->stopped = 0;
3808 qemu_thread_signal(penv->thread, SIGUSR1);
3809 qemu_cpu_kick(penv);
3810 penv = (CPUState *)penv->next_cpu;
3811 }
3812}
3813
3814static void tcg_init_vcpu(void *_env)
3815{
3816 CPUState *env = _env;
3817 /* share a single thread for all cpus with TCG */
3818 if (!tcg_cpu_thread) {
3819 env->thread = qemu_mallocz(sizeof(QemuThread));
3820 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3821 qemu_cond_init(env->halt_cond);
3822 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3823 while (env->created == 0)
3824 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3825 tcg_cpu_thread = env->thread;
3826 tcg_halt_cond = env->halt_cond;
3827 } else {
3828 env->thread = tcg_cpu_thread;
3829 env->halt_cond = tcg_halt_cond;
3830 }
3831}
3832
3833static void kvm_start_vcpu(CPUState *env)
3834{
aliguorid6dc3d42009-04-24 18:04:07 +00003835 env->thread = qemu_mallocz(sizeof(QemuThread));
3836 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3837 qemu_cond_init(env->halt_cond);
3838 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3839 while (env->created == 0)
3840 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3841}
3842
3843void qemu_init_vcpu(void *_env)
3844{
3845 CPUState *env = _env;
3846
3847 if (kvm_enabled())
3848 kvm_start_vcpu(env);
3849 else
3850 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003851 env->nr_cores = smp_cores;
3852 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003853}
3854
3855void qemu_notify_event(void)
3856{
3857 qemu_event_increment();
3858}
3859
3860void vm_stop(int reason)
3861{
3862 QemuThread me;
3863 qemu_thread_self(&me);
3864
3865 if (!qemu_thread_equal(&me, &io_thread)) {
3866 qemu_system_vmstop_request(reason);
3867 /*
3868 * FIXME: should not return to device code in case
3869 * vm_stop() has been requested.
3870 */
3871 if (cpu_single_env) {
3872 cpu_exit(cpu_single_env);
3873 cpu_single_env->stop = 1;
3874 }
3875 return;
3876 }
3877 do_vm_stop(reason);
3878}
3879
3880#endif
3881
3882
ths877cf882007-04-18 18:11:47 +00003883#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003884static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003885{
3886 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003887 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003888
bellardf3311102006-04-12 20:21:17 +00003889
3890 /* XXX: need to suppress polling by better using win32 events */
3891 ret = 0;
3892 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3893 ret |= pe->func(pe->opaque);
3894 }
thse6b1e552007-04-18 17:56:02 +00003895 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003896 int err;
3897 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003898
aliguori56f3a5d2008-10-31 18:07:17 +00003899 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003900 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3901 if (w->func[ret - WAIT_OBJECT_0])
3902 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003903
ths5fafdf22007-09-16 21:08:06 +00003904 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003905 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003906
thse6b1e552007-04-18 17:56:02 +00003907 /* Check if event is signaled */
3908 ret2 = WaitForSingleObject(w->events[i], 0);
3909 if(ret2 == WAIT_OBJECT_0) {
3910 if (w->func[i])
3911 w->func[i](w->opaque[i]);
3912 } else if (ret2 == WAIT_TIMEOUT) {
3913 } else {
3914 err = GetLastError();
3915 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003916 }
3917 }
bellarda18e5242006-06-25 17:18:27 +00003918 } else if (ret == WAIT_TIMEOUT) {
3919 } else {
3920 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003921 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003922 }
bellardf3311102006-04-12 20:21:17 +00003923 }
aliguori56f3a5d2008-10-31 18:07:17 +00003924
3925 *timeout = 0;
3926}
3927#else
blueswir169d64512008-12-07 19:30:18 +00003928static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003929{
3930}
bellardfd1dff42006-02-01 21:29:26 +00003931#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003932
3933void main_loop_wait(int timeout)
3934{
3935 IOHandlerRecord *ioh;
3936 fd_set rfds, wfds, xfds;
3937 int ret, nfds;
3938 struct timeval tv;
3939
3940 qemu_bh_update_timeout(&timeout);
3941
3942 host_main_loop_wait(&timeout);
3943
bellardfd1dff42006-02-01 21:29:26 +00003944 /* poll any events */
3945 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003946 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003947 FD_ZERO(&rfds);
3948 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003949 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003950 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003951 if (ioh->deleted)
3952 continue;
bellardfd1dff42006-02-01 21:29:26 +00003953 if (ioh->fd_read &&
3954 (!ioh->fd_read_poll ||
3955 ioh->fd_read_poll(ioh->opaque) != 0)) {
3956 FD_SET(ioh->fd, &rfds);
3957 if (ioh->fd > nfds)
3958 nfds = ioh->fd;
3959 }
3960 if (ioh->fd_write) {
3961 FD_SET(ioh->fd, &wfds);
3962 if (ioh->fd > nfds)
3963 nfds = ioh->fd;
3964 }
3965 }
ths3b46e622007-09-17 08:09:54 +00003966
aliguori56f3a5d2008-10-31 18:07:17 +00003967 tv.tv_sec = timeout / 1000;
3968 tv.tv_usec = (timeout % 1000) * 1000;
3969
Jan Kiszkad918f232009-06-24 14:42:30 +02003970 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3971
aliguori48708522009-04-24 18:03:49 +00003972 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003973 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003974 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003975 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003976 IOHandlerRecord **pioh;
3977
3978 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003979 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003980 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003981 }
ths6ab43fd2007-08-25 01:34:19 +00003982 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003983 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003984 }
3985 }
thscafffd42007-02-28 21:59:44 +00003986
3987 /* remove deleted IO handlers */
3988 pioh = &first_io_handler;
3989 while (*pioh) {
3990 ioh = *pioh;
3991 if (ioh->deleted) {
3992 *pioh = ioh->next;
3993 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003994 } else
thscafffd42007-02-28 21:59:44 +00003995 pioh = &ioh->next;
3996 }
bellardfd1dff42006-02-01 21:29:26 +00003997 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003998
3999 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004000
aliguori50317c72009-04-24 18:03:29 +00004001 /* rearm timer, if not periodic */
4002 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4003 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4004 qemu_rearm_alarm_timer(alarm_timer);
4005 }
4006
aliguori357c6922008-11-25 17:26:09 +00004007 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004008 if (vm_running) {
4009 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02004010 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
4011 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00004012 }
aliguori357c6922008-11-25 17:26:09 +00004013
4014 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02004015 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00004016 qemu_get_clock(rt_clock));
4017
Jan Kiszka21d5d122009-09-15 13:36:04 +02004018 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
4019 qemu_get_clock(host_clock));
4020
pbrook423f0742007-05-23 00:06:54 +00004021 /* Check bottom-halves last in case any of the earlier events triggered
4022 them. */
4023 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004024
bellard5905b2e2004-08-01 21:53:26 +00004025}
4026
aliguori43b96852009-04-24 18:03:33 +00004027static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004028{
aliguori43b96852009-04-24 18:03:33 +00004029 int ret;
bellard89bfc102006-02-08 22:46:31 +00004030#ifdef CONFIG_PROFILER
4031 int64_t ti;
4032#endif
aliguori43b96852009-04-24 18:03:33 +00004033
4034#ifdef CONFIG_PROFILER
4035 ti = profile_getclock();
4036#endif
4037 if (use_icount) {
4038 int64_t count;
4039 int decr;
4040 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4041 env->icount_decr.u16.low = 0;
4042 env->icount_extra = 0;
4043 count = qemu_next_deadline();
4044 count = (count + (1 << icount_time_shift) - 1)
4045 >> icount_time_shift;
4046 qemu_icount += count;
4047 decr = (count > 0xffff) ? 0xffff : count;
4048 count -= decr;
4049 env->icount_decr.u16.low = decr;
4050 env->icount_extra = count;
4051 }
4052 ret = cpu_exec(env);
4053#ifdef CONFIG_PROFILER
4054 qemu_time += profile_getclock() - ti;
4055#endif
4056 if (use_icount) {
4057 /* Fold pending instructions back into the
4058 instruction counter, and clear the interrupt flag. */
4059 qemu_icount -= (env->icount_decr.u16.low
4060 + env->icount_extra);
4061 env->icount_decr.u32 = 0;
4062 env->icount_extra = 0;
4063 }
4064 return ret;
4065}
4066
aliguorie6e35b12009-04-24 18:03:57 +00004067static void tcg_cpu_exec(void)
4068{
aliguorid6dc3d42009-04-24 18:04:07 +00004069 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004070
4071 if (next_cpu == NULL)
4072 next_cpu = first_cpu;
4073 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4074 CPUState *env = cur_cpu = next_cpu;
4075
4076 if (!vm_running)
4077 break;
4078 if (timer_alarm_pending) {
4079 timer_alarm_pending = 0;
4080 break;
4081 }
aliguorid6dc3d42009-04-24 18:04:07 +00004082 if (cpu_can_run(env))
4083 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004084 if (ret == EXCP_DEBUG) {
4085 gdb_set_stop_cpu(env);
4086 debug_requested = 1;
4087 break;
4088 }
4089 }
4090}
4091
aliguori43b96852009-04-24 18:03:33 +00004092static int cpu_has_work(CPUState *env)
4093{
aliguorid6dc3d42009-04-24 18:04:07 +00004094 if (env->stop)
4095 return 1;
4096 if (env->stopped)
4097 return 0;
aliguori43b96852009-04-24 18:03:33 +00004098 if (!env->halted)
4099 return 1;
4100 if (qemu_cpu_has_work(env))
4101 return 1;
4102 return 0;
4103}
4104
4105static int tcg_has_work(void)
4106{
bellard6a00d602005-11-21 23:25:50 +00004107 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004108
aliguori43b96852009-04-24 18:03:33 +00004109 for (env = first_cpu; env != NULL; env = env->next_cpu)
4110 if (cpu_has_work(env))
4111 return 1;
4112 return 0;
4113}
bellard15a76442005-11-23 21:01:03 +00004114
aliguori43b96852009-04-24 18:03:33 +00004115static int qemu_calculate_timeout(void)
4116{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004117#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004118 int timeout;
bellard15a76442005-11-23 21:01:03 +00004119
aliguori43b96852009-04-24 18:03:33 +00004120 if (!vm_running)
4121 timeout = 5000;
4122 else if (tcg_has_work())
4123 timeout = 0;
4124 else if (!use_icount)
4125 timeout = 5000;
4126 else {
4127 /* XXX: use timeout computed from timers */
4128 int64_t add;
4129 int64_t delta;
4130 /* Advance virtual time to the next event. */
4131 if (use_icount == 1) {
4132 /* When not using an adaptive execution frequency
4133 we tend to get badly out of sync with real time,
4134 so just delay for a reasonable amount of time. */
4135 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004136 } else {
aliguori43b96852009-04-24 18:03:33 +00004137 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004138 }
aliguori43b96852009-04-24 18:03:33 +00004139 if (delta > 0) {
4140 /* If virtual time is ahead of real time then just
4141 wait for IO. */
4142 timeout = (delta / 1000000) + 1;
4143 } else {
4144 /* Wait for either IO to occur or the next
4145 timer event. */
4146 add = qemu_next_deadline();
4147 /* We advance the timer before checking for IO.
4148 Limit the amount we advance so that early IO
4149 activity won't get the guest too far ahead. */
4150 if (add > 10000000)
4151 add = 10000000;
4152 delta += add;
4153 add = (add + (1 << icount_time_shift) - 1)
4154 >> icount_time_shift;
4155 qemu_icount += add;
4156 timeout = delta / 1000000;
4157 if (timeout < 0)
4158 timeout = 0;
4159 }
bellardb4608c02003-06-27 17:34:32 +00004160 }
aliguori43b96852009-04-24 18:03:33 +00004161
4162 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004163#else /* CONFIG_IOTHREAD */
4164 return 1000;
4165#endif
aliguori43b96852009-04-24 18:03:33 +00004166}
4167
4168static int vm_can_run(void)
4169{
4170 if (powerdown_requested)
4171 return 0;
4172 if (reset_requested)
4173 return 0;
4174 if (shutdown_requested)
4175 return 0;
aliguorie5689022009-04-24 18:03:54 +00004176 if (debug_requested)
4177 return 0;
aliguori43b96852009-04-24 18:03:33 +00004178 return 1;
4179}
4180
Blue Swirld9c32312009-08-09 08:42:19 +00004181qemu_irq qemu_system_powerdown;
4182
aliguori43b96852009-04-24 18:03:33 +00004183static void main_loop(void)
4184{
aliguori6e29f5d2009-04-24 18:04:02 +00004185 int r;
aliguori43b96852009-04-24 18:03:33 +00004186
aliguorid6dc3d42009-04-24 18:04:07 +00004187#ifdef CONFIG_IOTHREAD
4188 qemu_system_ready = 1;
4189 qemu_cond_broadcast(&qemu_system_cond);
4190#endif
4191
aliguori6e29f5d2009-04-24 18:04:02 +00004192 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004193 do {
4194#ifdef CONFIG_PROFILER
4195 int64_t ti;
4196#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004197#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004198 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004199#endif
aliguori43b96852009-04-24 18:03:33 +00004200#ifdef CONFIG_PROFILER
4201 ti = profile_getclock();
4202#endif
4203 main_loop_wait(qemu_calculate_timeout());
4204#ifdef CONFIG_PROFILER
4205 dev_time += profile_getclock() - ti;
4206#endif
aliguorie5689022009-04-24 18:03:54 +00004207 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004208
aliguorie5689022009-04-24 18:03:54 +00004209 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004210 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004211 if (qemu_shutdown_requested()) {
4212 if (no_shutdown) {
4213 vm_stop(0);
4214 no_shutdown = 0;
4215 } else
4216 break;
4217 }
aliguorid6dc3d42009-04-24 18:04:07 +00004218 if (qemu_reset_requested()) {
4219 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004220 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004221 resume_all_vcpus();
4222 }
Blue Swirld9c32312009-08-09 08:42:19 +00004223 if (qemu_powerdown_requested()) {
4224 qemu_irq_raise(qemu_system_powerdown);
4225 }
aliguori6e29f5d2009-04-24 18:04:02 +00004226 if ((r = qemu_vmstop_requested()))
4227 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004228 }
aliguorid6dc3d42009-04-24 18:04:07 +00004229 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004230}
4231
pbrook9bd7e6d2009-04-07 22:58:45 +00004232static void version(void)
4233{
pbrook4a19f1e2009-04-07 23:17:49 +00004234 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004235}
4236
ths15f82202007-06-29 23:26:08 +00004237static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004238{
pbrook9bd7e6d2009-04-07 22:58:45 +00004239 version();
4240 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004241 "\n"
bellarda20dd502003-09-30 21:07:02 +00004242 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004243 "\n"
blueswir15824d652009-03-28 06:44:27 +00004244#define DEF(option, opt_arg, opt_enum, opt_help) \
4245 opt_help
4246#define DEFHEADING(text) stringify(text) "\n"
4247#include "qemu-options.h"
4248#undef DEF
4249#undef DEFHEADING
4250#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004251 "\n"
bellard82c643f2004-07-14 17:28:13 +00004252 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004253 "ctrl-alt-f toggle full screen\n"
4254 "ctrl-alt-n switch to virtual console 'n'\n"
4255 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004256 "\n"
4257 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4258 ,
bellard0db63472003-10-27 21:37:46 +00004259 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004260 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004261#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004262 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004263 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004264#endif
bellard6e44ba72004-01-18 21:56:49 +00004265 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004266 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004267 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004268}
4269
bellardcd6f1162004-05-13 22:02:20 +00004270#define HAS_ARG 0x0001
4271
4272enum {
blueswir15824d652009-03-28 06:44:27 +00004273#define DEF(option, opt_arg, opt_enum, opt_help) \
4274 opt_enum,
4275#define DEFHEADING(text)
4276#include "qemu-options.h"
4277#undef DEF
4278#undef DEFHEADING
4279#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004280};
4281
4282typedef struct QEMUOption {
4283 const char *name;
4284 int flags;
4285 int index;
4286} QEMUOption;
4287
blueswir1dbed7e42008-10-01 19:38:09 +00004288static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004289 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004290#define DEF(option, opt_arg, opt_enum, opt_help) \
4291 { option, opt_arg, opt_enum },
4292#define DEFHEADING(text)
4293#include "qemu-options.h"
4294#undef DEF
4295#undef DEFHEADING
4296#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004297 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004298};
4299
bellard1d14ffa2005-10-30 18:58:22 +00004300#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004301struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004302#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004303#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004304 {
4305 "pcspk",
4306 "PC speaker",
4307 0,
4308 1,
4309 { .init_isa = pcspk_audio_init }
4310 },
4311#endif
malc4c9b53e2009-01-09 10:46:34 +00004312
4313#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004314 {
4315 "sb16",
4316 "Creative Sound Blaster 16",
4317 0,
4318 1,
4319 { .init_isa = SB16_init }
4320 },
malc4c9b53e2009-01-09 10:46:34 +00004321#endif
bellard6a36d842005-12-18 20:34:32 +00004322
malccc53d262008-06-13 10:48:22 +00004323#ifdef CONFIG_CS4231A
4324 {
4325 "cs4231a",
4326 "CS4231A",
4327 0,
4328 1,
4329 { .init_isa = cs4231a_init }
4330 },
4331#endif
4332
bellard6a36d842005-12-18 20:34:32 +00004333#ifdef CONFIG_ADLIB
4334 {
4335 "adlib",
4336#ifdef HAS_YMF262
4337 "Yamaha YMF262 (OPL3)",
4338#else
4339 "Yamaha YM3812 (OPL2)",
4340#endif
4341 0,
4342 1,
4343 { .init_isa = Adlib_init }
4344 },
4345#endif
4346
4347#ifdef CONFIG_GUS
4348 {
4349 "gus",
4350 "Gravis Ultrasound GF1",
4351 0,
4352 1,
4353 { .init_isa = GUS_init }
4354 },
4355#endif
4356
malc4c9b53e2009-01-09 10:46:34 +00004357#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004358 {
4359 "ac97",
4360 "Intel 82801AA AC97 Audio",
4361 0,
4362 0,
4363 { .init_pci = ac97_init }
4364 },
malc4c9b53e2009-01-09 10:46:34 +00004365#endif
balroge5c9a132008-01-14 04:27:55 +00004366
malc4c9b53e2009-01-09 10:46:34 +00004367#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004368 {
4369 "es1370",
4370 "ENSONIQ AudioPCI ES1370",
4371 0,
4372 0,
4373 { .init_pci = es1370_init }
4374 },
balrogb00052e2007-04-30 02:22:06 +00004375#endif
bellard6a36d842005-12-18 20:34:32 +00004376
malc4c9b53e2009-01-09 10:46:34 +00004377#endif /* HAS_AUDIO_CHOICE */
4378
bellard6a36d842005-12-18 20:34:32 +00004379 { NULL, NULL, 0, 0, { NULL } }
4380};
4381
bellard1d14ffa2005-10-30 18:58:22 +00004382static void select_soundhw (const char *optarg)
4383{
bellard6a36d842005-12-18 20:34:32 +00004384 struct soundhw *c;
4385
bellard1d14ffa2005-10-30 18:58:22 +00004386 if (*optarg == '?') {
4387 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004388
bellard1d14ffa2005-10-30 18:58:22 +00004389 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004390 for (c = soundhw; c->name; ++c) {
4391 printf ("%-11s %s\n", c->name, c->descr);
4392 }
4393 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004394 exit (*optarg != '?');
4395 }
4396 else {
bellard6a36d842005-12-18 20:34:32 +00004397 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004398 const char *p;
4399 char *e;
4400 int bad_card = 0;
4401
bellard6a36d842005-12-18 20:34:32 +00004402 if (!strcmp (optarg, "all")) {
4403 for (c = soundhw; c->name; ++c) {
4404 c->enabled = 1;
4405 }
4406 return;
4407 }
bellard1d14ffa2005-10-30 18:58:22 +00004408
bellard6a36d842005-12-18 20:34:32 +00004409 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004410 while (*p) {
4411 e = strchr (p, ',');
4412 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004413
4414 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004415 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004416 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004417 break;
4418 }
4419 }
bellard6a36d842005-12-18 20:34:32 +00004420
4421 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004422 if (l > 80) {
4423 fprintf (stderr,
4424 "Unknown sound card name (too big to show)\n");
4425 }
4426 else {
4427 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4428 (int) l, p);
4429 }
4430 bad_card = 1;
4431 }
4432 p += l + (e != NULL);
4433 }
4434
4435 if (bad_card)
4436 goto show_valid_cards;
4437 }
4438}
4439#endif
4440
malc3893c122008-09-28 00:42:05 +00004441static void select_vgahw (const char *p)
4442{
4443 const char *opts;
4444
Zachary Amsden86176752009-07-30 00:15:02 -10004445 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004446 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004447 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004448 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004449 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004450 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004451 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004452 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004453 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004454 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004455 invalid_vga:
4456 fprintf(stderr, "Unknown vga type: %s\n", p);
4457 exit(1);
4458 }
malccb5a7aa2008-09-28 00:42:12 +00004459 while (*opts) {
4460 const char *nextopt;
4461
4462 if (strstart(opts, ",retrace=", &nextopt)) {
4463 opts = nextopt;
4464 if (strstart(opts, "dumb", &nextopt))
4465 vga_retrace_method = VGA_RETRACE_DUMB;
4466 else if (strstart(opts, "precise", &nextopt))
4467 vga_retrace_method = VGA_RETRACE_PRECISE;
4468 else goto invalid_vga;
4469 } else goto invalid_vga;
4470 opts = nextopt;
4471 }
malc3893c122008-09-28 00:42:05 +00004472}
4473
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004474#ifdef TARGET_I386
4475static int balloon_parse(const char *arg)
4476{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004477 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004478
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004479 if (strcmp(arg, "none") == 0) {
4480 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004481 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004482
4483 if (!strncmp(arg, "virtio", 6)) {
4484 if (arg[6] == ',') {
4485 /* have params -> parse them */
4486 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4487 if (!opts)
4488 return -1;
4489 } else {
4490 /* create empty opts */
4491 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4492 }
4493 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4494 return 0;
4495 }
4496
4497 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004498}
4499#endif
4500
bellard3587d7e2006-06-26 20:03:44 +00004501#ifdef _WIN32
4502static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4503{
4504 exit(STATUS_CONTROL_C_EXIT);
4505 return TRUE;
4506}
4507#endif
4508
aliguoric4be29f2009-04-17 18:58:14 +00004509int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004510{
4511 int ret;
4512
4513 if(strlen(str) != 36)
4514 return -1;
4515
4516 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4517 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4518 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4519
4520 if(ret != 16)
4521 return -1;
4522
aliguorib6f6e3d2009-04-17 18:59:56 +00004523#ifdef TARGET_I386
4524 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4525#endif
4526
blueswir18fcb1b92008-09-18 18:29:08 +00004527 return 0;
4528}
4529
bellard7c9d8e02005-11-15 22:16:05 +00004530#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004531
aliguori5b08fc12008-08-21 20:08:03 +00004532#ifndef _WIN32
4533
4534static void termsig_handler(int signal)
4535{
4536 qemu_system_shutdown_request();
4537}
4538
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004539static void sigchld_handler(int signal)
4540{
4541 waitpid(-1, NULL, WNOHANG);
4542}
4543
4544static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004545{
4546 struct sigaction act;
4547
4548 memset(&act, 0, sizeof(act));
4549 act.sa_handler = termsig_handler;
4550 sigaction(SIGINT, &act, NULL);
4551 sigaction(SIGHUP, &act, NULL);
4552 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004553
4554 act.sa_handler = sigchld_handler;
4555 act.sa_flags = SA_NOCLDSTOP;
4556 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004557}
4558
4559#endif
4560
Paul Brook5cea8592009-05-30 00:52:44 +01004561#ifdef _WIN32
4562/* Look for support files in the same directory as the executable. */
4563static char *find_datadir(const char *argv0)
4564{
4565 char *p;
4566 char buf[MAX_PATH];
4567 DWORD len;
4568
4569 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4570 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004571 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004572 }
4573
4574 buf[len] = 0;
4575 p = buf + len - 1;
4576 while (p != buf && *p != '\\')
4577 p--;
4578 *p = 0;
4579 if (access(buf, R_OK) == 0) {
4580 return qemu_strdup(buf);
4581 }
4582 return NULL;
4583}
4584#else /* !_WIN32 */
4585
4586/* Find a likely location for support files using the location of the binary.
4587 For installed binaries this will be "$bindir/../share/qemu". When
4588 running from the build tree this will be "$bindir/../pc-bios". */
4589#define SHARE_SUFFIX "/share/qemu"
4590#define BUILD_SUFFIX "/pc-bios"
4591static char *find_datadir(const char *argv0)
4592{
4593 char *dir;
4594 char *p = NULL;
4595 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004596 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004597 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004598
4599#if defined(__linux__)
4600 {
4601 int len;
4602 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4603 if (len > 0) {
4604 buf[len] = 0;
4605 p = buf;
4606 }
4607 }
4608#elif defined(__FreeBSD__)
4609 {
4610 int len;
4611 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4612 if (len > 0) {
4613 buf[len] = 0;
4614 p = buf;
4615 }
4616 }
4617#endif
4618 /* If we don't have any way of figuring out the actual executable
4619 location then try argv[0]. */
4620 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004621 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004622 if (!p) {
4623 return NULL;
4624 }
4625 }
4626 dir = dirname(p);
4627 dir = dirname(dir);
4628
Blue Swirl3a417592009-06-09 19:12:21 +00004629 max_len = strlen(dir) +
4630 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4631 res = qemu_mallocz(max_len);
4632 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004633 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004634 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004635 if (access(res, R_OK)) {
4636 qemu_free(res);
4637 res = NULL;
4638 }
4639 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004640
Paul Brook5cea8592009-05-30 00:52:44 +01004641 return res;
4642}
4643#undef SHARE_SUFFIX
4644#undef BUILD_SUFFIX
4645#endif
4646
4647char *qemu_find_file(int type, const char *name)
4648{
4649 int len;
4650 const char *subdir;
4651 char *buf;
4652
4653 /* If name contains path separators then try it as a straight path. */
4654 if ((strchr(name, '/') || strchr(name, '\\'))
4655 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004656 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004657 }
4658 switch (type) {
4659 case QEMU_FILE_TYPE_BIOS:
4660 subdir = "";
4661 break;
4662 case QEMU_FILE_TYPE_KEYMAP:
4663 subdir = "keymaps/";
4664 break;
4665 default:
4666 abort();
4667 }
4668 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4669 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004670 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004671 if (access(buf, R_OK)) {
4672 qemu_free(buf);
4673 return NULL;
4674 }
4675 return buf;
4676}
4677
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004678static int device_init_func(QemuOpts *opts, void *opaque)
4679{
4680 DeviceState *dev;
4681
4682 dev = qdev_device_add(opts);
4683 if (!dev)
4684 return -1;
4685 return 0;
4686}
4687
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004688struct device_config {
4689 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004690 DEV_USB, /* -usbdevice */
4691 DEV_BT, /* -bt */
4692 } type;
4693 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004694 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004695};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004696QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004697
4698static void add_device_config(int type, const char *cmdline)
4699{
4700 struct device_config *conf;
4701
4702 conf = qemu_mallocz(sizeof(*conf));
4703 conf->type = type;
4704 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004705 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004706}
4707
4708static int foreach_device_config(int type, int (*func)(const char *cmdline))
4709{
4710 struct device_config *conf;
4711 int rc;
4712
Blue Swirl72cf2d42009-09-12 07:36:22 +00004713 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004714 if (conf->type != type)
4715 continue;
4716 rc = func(conf->cmdline);
4717 if (0 != rc)
4718 return rc;
4719 }
4720 return 0;
4721}
4722
malc902b3d52008-12-10 19:18:40 +00004723int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004724{
aliguori59030a82009-04-05 18:43:41 +00004725 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004726 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004727 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004728 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004729 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004730 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004731 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004732 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004733 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004734 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004735 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004736 int nb_net_clients;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004737 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004738 int optind;
4739 const char *r, *optarg;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004740 CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
4741 const char *monitor_devices[MAX_MONITOR_DEVICES];
4742 int monitor_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004743 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004744 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004745 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004746 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004747 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4748 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004749 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004750 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004751 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004752#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004753 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004754#endif
bellard26a5f132008-05-28 12:30:31 +00004755 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004756 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004757 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004758#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004759 int fd = 0;
4760 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004761 const char *chroot_dir = NULL;
4762 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004763#endif
aliguori268a3622009-04-21 22:30:27 +00004764 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004765 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004766
Jan Kiszka68752042009-09-15 13:36:04 +02004767 init_clocks();
4768
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004769 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004770 qemu_cache_utils_init(envp);
4771
Blue Swirl72cf2d42009-09-12 07:36:22 +00004772 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004773#ifndef _WIN32
4774 {
4775 struct sigaction act;
4776 sigfillset(&act.sa_mask);
4777 act.sa_flags = 0;
4778 act.sa_handler = SIG_IGN;
4779 sigaction(SIGPIPE, &act, NULL);
4780 }
bellard3587d7e2006-06-26 20:03:44 +00004781#else
4782 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004783 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4784 QEMU to run on a single CPU */
4785 {
4786 HANDLE h;
4787 DWORD mask, smask;
4788 int i;
4789 h = GetCurrentProcess();
4790 if (GetProcessAffinityMask(h, &mask, &smask)) {
4791 for(i = 0; i < 32; i++) {
4792 if (mask & (1 << i))
4793 break;
4794 }
4795 if (i != 32) {
4796 mask = 1 << i;
4797 SetProcessAffinityMask(h, mask);
4798 }
4799 }
4800 }
bellard67b915a2004-03-31 23:37:16 +00004801#endif
bellardbe995c22006-06-25 16:25:21 +00004802
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004803 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004804 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004805 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004806 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004807 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004808 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004809 kernel_filename = NULL;
4810 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004811 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004812 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004813
aurel32c75a8232008-05-04 00:50:34 +00004814 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004815 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004816 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004817 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004818
aliguori8290edd2009-02-27 20:14:29 +00004819 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004820 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004821 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004822 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004823
aliguori1b8fc812009-02-27 20:01:39 +00004824 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004825 virtio_consoles[i] = NULL;
4826 virtio_console_index = 0;
4827
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004828 monitor_devices[0] = "vc:80Cx24C";
4829 for (i = 1; i < MAX_MONITOR_DEVICES; i++) {
4830 monitor_devices[i] = NULL;
4831 }
4832 monitor_device_index = 0;
4833
aliguori268a3622009-04-21 22:30:27 +00004834 for (i = 0; i < MAX_NODES; i++) {
4835 node_mem[i] = 0;
4836 node_cpumask[i] = 0;
4837 }
4838
bellard7c9d8e02005-11-15 22:16:05 +00004839 nb_net_clients = 0;
aliguori268a3622009-04-21 22:30:27 +00004840 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004841 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004842
bellard26a5f132008-05-28 12:30:31 +00004843 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004844 autostart= 1;
4845
bellardcd6f1162004-05-13 22:02:20 +00004846 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004847 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004848 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004849 break;
bellardcd6f1162004-05-13 22:02:20 +00004850 r = argv[optind];
4851 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004852 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004853 } else {
4854 const QEMUOption *popt;
4855
4856 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004857 /* Treat --foo the same as -foo. */
4858 if (r[1] == '-')
4859 r++;
bellardcd6f1162004-05-13 22:02:20 +00004860 popt = qemu_options;
4861 for(;;) {
4862 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004863 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004864 argv[0], r);
4865 exit(1);
4866 }
4867 if (!strcmp(popt->name, r + 1))
4868 break;
4869 popt++;
4870 }
4871 if (popt->flags & HAS_ARG) {
4872 if (optind >= argc) {
4873 fprintf(stderr, "%s: option '%s' requires an argument\n",
4874 argv[0], r);
4875 exit(1);
4876 }
4877 optarg = argv[optind++];
4878 } else {
4879 optarg = NULL;
4880 }
4881
4882 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004883 case QEMU_OPTION_M:
4884 machine = find_machine(optarg);
4885 if (!machine) {
4886 QEMUMachine *m;
4887 printf("Supported machines are:\n");
4888 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004889 if (m->alias)
4890 printf("%-10s %s (alias of %s)\n",
4891 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004892 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004893 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004894 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004895 }
ths15f82202007-06-29 23:26:08 +00004896 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004897 }
4898 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004899 case QEMU_OPTION_cpu:
4900 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004901 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004902/* XXX: implement xxx_cpu_list for targets that still miss it */
4903#if defined(cpu_list)
4904 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004905#endif
ths15f82202007-06-29 23:26:08 +00004906 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004907 } else {
4908 cpu_model = optarg;
4909 }
4910 break;
bellardcd6f1162004-05-13 22:02:20 +00004911 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004912 initrd_filename = optarg;
4913 break;
bellardcd6f1162004-05-13 22:02:20 +00004914 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004915 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004916 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004917 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004918 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004919 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004920 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004921 translation == BIOS_ATA_TRANSLATION_LBA ?
4922 ",trans=lba" :
4923 translation == BIOS_ATA_TRANSLATION_NONE ?
4924 ",trans=none" : "");
4925 break;
bellardcd6f1162004-05-13 22:02:20 +00004926 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004927 case QEMU_OPTION_hdc:
4928 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004929 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004930 break;
thse4bcb142007-12-02 04:51:10 +00004931 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004932 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004933 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004934 case QEMU_OPTION_set:
4935 if (qemu_set_option(optarg) != 0)
4936 exit(1);
4937 break;
balrog3e3d5812007-04-30 02:09:25 +00004938 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004939 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004940 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004941 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004942 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004943 break;
j_mayer86f55662007-04-24 06:52:59 +00004944 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004945 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004946 break;
bellardcd6f1162004-05-13 22:02:20 +00004947 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004948 snapshot = 1;
4949 break;
bellardcd6f1162004-05-13 22:02:20 +00004950 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004951 {
bellard330d0412003-07-26 18:11:40 +00004952 const char *p;
4953 p = optarg;
4954 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004955 if (cyls < 1 || cyls > 16383)
4956 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004957 if (*p != ',')
4958 goto chs_fail;
4959 p++;
4960 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004961 if (heads < 1 || heads > 16)
4962 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004963 if (*p != ',')
4964 goto chs_fail;
4965 p++;
4966 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004967 if (secs < 1 || secs > 63)
4968 goto chs_fail;
4969 if (*p == ',') {
4970 p++;
4971 if (!strcmp(p, "none"))
4972 translation = BIOS_ATA_TRANSLATION_NONE;
4973 else if (!strcmp(p, "lba"))
4974 translation = BIOS_ATA_TRANSLATION_LBA;
4975 else if (!strcmp(p, "auto"))
4976 translation = BIOS_ATA_TRANSLATION_AUTO;
4977 else
4978 goto chs_fail;
4979 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004980 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004981 fprintf(stderr, "qemu: invalid physical CHS format\n");
4982 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004983 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004984 if (hda_opts != NULL) {
4985 char num[16];
4986 snprintf(num, sizeof(num), "%d", cyls);
4987 qemu_opt_set(hda_opts, "cyls", num);
4988 snprintf(num, sizeof(num), "%d", heads);
4989 qemu_opt_set(hda_opts, "heads", num);
4990 snprintf(num, sizeof(num), "%d", secs);
4991 qemu_opt_set(hda_opts, "secs", num);
4992 if (translation == BIOS_ATA_TRANSLATION_LBA)
4993 qemu_opt_set(hda_opts, "trans", "lba");
4994 if (translation == BIOS_ATA_TRANSLATION_NONE)
4995 qemu_opt_set(hda_opts, "trans", "none");
4996 }
bellard330d0412003-07-26 18:11:40 +00004997 }
4998 break;
aliguori268a3622009-04-21 22:30:27 +00004999 case QEMU_OPTION_numa:
5000 if (nb_numa_nodes >= MAX_NODES) {
5001 fprintf(stderr, "qemu: too many NUMA nodes\n");
5002 exit(1);
5003 }
5004 numa_add(optarg);
5005 break;
bellardcd6f1162004-05-13 22:02:20 +00005006 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005007 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005008 break;
balrog4d3b6f62008-02-10 16:33:14 +00005009#ifdef CONFIG_CURSES
5010 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005011 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005012 break;
5013#endif
balroga171fe32007-04-30 01:48:07 +00005014 case QEMU_OPTION_portrait:
5015 graphic_rotate = 1;
5016 break;
bellardcd6f1162004-05-13 22:02:20 +00005017 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005018 kernel_filename = optarg;
5019 break;
bellardcd6f1162004-05-13 22:02:20 +00005020 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005021 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005022 break;
bellardcd6f1162004-05-13 22:02:20 +00005023 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005024 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005025 break;
bellardcd6f1162004-05-13 22:02:20 +00005026 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005027 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005028 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005029 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005030 };
5031 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005032 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005033 int legacy = 0;
5034
5035 if (!strchr(optarg, '=')) {
5036 legacy = 1;
5037 pstrcpy(buf, sizeof(buf), optarg);
5038 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5039 fprintf(stderr,
5040 "qemu: unknown boot parameter '%s' in '%s'\n",
5041 buf, optarg);
5042 exit(1);
5043 }
5044
5045 if (legacy ||
5046 get_param_value(buf, sizeof(buf), "order", optarg)) {
5047 boot_devices_bitmap = parse_bootdevices(buf);
5048 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005049 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005050 if (!legacy) {
5051 if (get_param_value(buf, sizeof(buf),
5052 "once", optarg)) {
5053 boot_devices_bitmap |= parse_bootdevices(buf);
5054 standard_boot_devices = qemu_strdup(boot_devices);
5055 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5056 qemu_register_reset(restore_boot_devices,
5057 standard_boot_devices);
5058 }
Jan Kiszka95387492009-07-02 00:19:02 +02005059 if (get_param_value(buf, sizeof(buf),
5060 "menu", optarg)) {
5061 if (!strcmp(buf, "on")) {
5062 boot_menu = 1;
5063 } else if (!strcmp(buf, "off")) {
5064 boot_menu = 0;
5065 } else {
5066 fprintf(stderr,
5067 "qemu: invalid option value '%s'\n",
5068 buf);
5069 exit(1);
5070 }
5071 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005072 }
bellard36b486b2003-11-11 13:36:08 +00005073 }
5074 break;
bellardcd6f1162004-05-13 22:02:20 +00005075 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005076 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005077 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005078 break;
bellard52ca8d62006-06-14 16:03:05 +00005079#ifdef TARGET_I386
5080 case QEMU_OPTION_no_fd_bootchk:
5081 fd_bootchk = 0;
5082 break;
5083#endif
bellard7c9d8e02005-11-15 22:16:05 +00005084 case QEMU_OPTION_net:
5085 if (nb_net_clients >= MAX_NET_CLIENTS) {
5086 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005087 exit(1);
5088 }
pbrookfd5f3932008-03-26 20:55:43 +00005089 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005090 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005091 break;
bellardc7f74642004-08-24 21:57:12 +00005092#ifdef CONFIG_SLIRP
5093 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005094 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005095 break;
ths47d5d012007-02-20 00:05:08 +00005096 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005097 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005098 break;
bellardc94c8d62004-09-13 21:37:34 +00005099#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005100 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005101 if (net_slirp_smb(optarg) < 0)
5102 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005103 break;
bellardc94c8d62004-09-13 21:37:34 +00005104#endif
bellard9bf05442004-08-25 22:12:49 +00005105 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005106 if (net_slirp_redir(optarg) < 0)
5107 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005108 break;
bellardc7f74642004-08-24 21:57:12 +00005109#endif
balrogdc72ac12008-11-09 00:04:26 +00005110 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005111 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005112 break;
bellard1d14ffa2005-10-30 18:58:22 +00005113#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005114 case QEMU_OPTION_audio_help:
5115 AUD_help ();
5116 exit (0);
5117 break;
5118 case QEMU_OPTION_soundhw:
5119 select_soundhw (optarg);
5120 break;
5121#endif
bellardcd6f1162004-05-13 22:02:20 +00005122 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005123 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005124 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005125 case QEMU_OPTION_version:
5126 version();
5127 exit(0);
5128 break;
aurel3200f82b82008-04-27 21:12:55 +00005129 case QEMU_OPTION_m: {
5130 uint64_t value;
5131 char *ptr;
5132
5133 value = strtoul(optarg, &ptr, 10);
5134 switch (*ptr) {
5135 case 0: case 'M': case 'm':
5136 value <<= 20;
5137 break;
5138 case 'G': case 'g':
5139 value <<= 30;
5140 break;
5141 default:
5142 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005143 exit(1);
5144 }
aurel3200f82b82008-04-27 21:12:55 +00005145
5146 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005147 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005148 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5149 exit(1);
5150 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005151 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005152 fprintf(stderr, "qemu: ram size too large\n");
5153 exit(1);
5154 }
5155 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005156 break;
aurel3200f82b82008-04-27 21:12:55 +00005157 }
bellardcd6f1162004-05-13 22:02:20 +00005158 case QEMU_OPTION_d:
5159 {
5160 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005161 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005162
bellardcd6f1162004-05-13 22:02:20 +00005163 mask = cpu_str_to_log_mask(optarg);
5164 if (!mask) {
5165 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005166 for(item = cpu_log_items; item->mask != 0; item++) {
5167 printf("%-10s %s\n", item->name, item->help);
5168 }
5169 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005170 }
5171 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005172 }
bellardcd6f1162004-05-13 22:02:20 +00005173 break;
bellardcd6f1162004-05-13 22:02:20 +00005174 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005175 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005176 break;
aliguori59030a82009-04-05 18:43:41 +00005177 case QEMU_OPTION_gdb:
5178 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005179 break;
bellardcd6f1162004-05-13 22:02:20 +00005180 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005181 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005182 break;
j_mayer1192dad2007-10-05 13:08:35 +00005183 case QEMU_OPTION_bios:
5184 bios_name = optarg;
5185 break;
aurel321b530a62009-04-05 20:08:59 +00005186 case QEMU_OPTION_singlestep:
5187 singlestep = 1;
5188 break;
bellardcd6f1162004-05-13 22:02:20 +00005189 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005190 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005191 break;
blueswir15824d652009-03-28 06:44:27 +00005192#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005193 case QEMU_OPTION_k:
5194 keyboard_layout = optarg;
5195 break;
blueswir15824d652009-03-28 06:44:27 +00005196#endif
bellardee22c2f2004-06-03 12:49:50 +00005197 case QEMU_OPTION_localtime:
5198 rtc_utc = 0;
5199 break;
malc3893c122008-09-28 00:42:05 +00005200 case QEMU_OPTION_vga:
5201 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005202 break;
blueswir15824d652009-03-28 06:44:27 +00005203#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005204 case QEMU_OPTION_g:
5205 {
5206 const char *p;
5207 int w, h, depth;
5208 p = optarg;
5209 w = strtol(p, (char **)&p, 10);
5210 if (w <= 0) {
5211 graphic_error:
5212 fprintf(stderr, "qemu: invalid resolution or depth\n");
5213 exit(1);
5214 }
5215 if (*p != 'x')
5216 goto graphic_error;
5217 p++;
5218 h = strtol(p, (char **)&p, 10);
5219 if (h <= 0)
5220 goto graphic_error;
5221 if (*p == 'x') {
5222 p++;
5223 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005224 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005225 depth != 24 && depth != 32)
5226 goto graphic_error;
5227 } else if (*p == '\0') {
5228 depth = graphic_depth;
5229 } else {
5230 goto graphic_error;
5231 }
ths3b46e622007-09-17 08:09:54 +00005232
bellarde9b137c2004-06-21 16:46:10 +00005233 graphic_width = w;
5234 graphic_height = h;
5235 graphic_depth = depth;
5236 }
5237 break;
blueswir15824d652009-03-28 06:44:27 +00005238#endif
ths20d8a3e2007-02-18 17:04:49 +00005239 case QEMU_OPTION_echr:
5240 {
5241 char *r;
5242 term_escape_char = strtol(optarg, &r, 0);
5243 if (r == optarg)
5244 printf("Bad argument to echr\n");
5245 break;
5246 }
bellard82c643f2004-07-14 17:28:13 +00005247 case QEMU_OPTION_monitor:
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005248 if (monitor_device_index >= MAX_MONITOR_DEVICES) {
5249 fprintf(stderr, "qemu: too many monitor devices\n");
5250 exit(1);
5251 }
5252 monitor_devices[monitor_device_index] = optarg;
5253 monitor_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005254 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005255 case QEMU_OPTION_chardev:
5256 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5257 if (!opts) {
5258 fprintf(stderr, "parse error: %s\n", optarg);
5259 exit(1);
5260 }
Mark McLoughlin3df04ac2009-09-23 11:24:05 +01005261 if (qemu_chr_open_opts(opts, NULL) == NULL) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005262 exit(1);
5263 }
5264 break;
bellard82c643f2004-07-14 17:28:13 +00005265 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005266 if (serial_device_index >= MAX_SERIAL_PORTS) {
5267 fprintf(stderr, "qemu: too many serial ports\n");
5268 exit(1);
5269 }
pbrookfd5f3932008-03-26 20:55:43 +00005270 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005271 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005272 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005273 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005274 if (watchdog) {
5275 fprintf(stderr,
5276 "qemu: only one watchdog option may be given\n");
5277 return 1;
5278 }
5279 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005280 break;
5281 case QEMU_OPTION_watchdog_action:
5282 if (select_watchdog_action(optarg) == -1) {
5283 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5284 exit(1);
5285 }
5286 break;
aliguori51ecf132009-01-15 20:06:40 +00005287 case QEMU_OPTION_virtiocon:
5288 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5289 fprintf(stderr, "qemu: too many virtio consoles\n");
5290 exit(1);
5291 }
5292 virtio_consoles[virtio_console_index] = optarg;
5293 virtio_console_index++;
5294 break;
bellard6508fe52005-01-15 12:02:56 +00005295 case QEMU_OPTION_parallel:
5296 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5297 fprintf(stderr, "qemu: too many parallel ports\n");
5298 exit(1);
5299 }
pbrookfd5f3932008-03-26 20:55:43 +00005300 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005301 parallel_device_index++;
5302 break;
bellardd63d3072004-10-03 13:29:03 +00005303 case QEMU_OPTION_loadvm:
5304 loadvm = optarg;
5305 break;
5306 case QEMU_OPTION_full_screen:
5307 full_screen = 1;
5308 break;
ths667acca2006-12-11 02:08:05 +00005309#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005310 case QEMU_OPTION_no_frame:
5311 no_frame = 1;
5312 break;
ths3780e192007-06-21 21:08:02 +00005313 case QEMU_OPTION_alt_grab:
5314 alt_grab = 1;
5315 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005316 case QEMU_OPTION_ctrl_grab:
5317 ctrl_grab = 1;
5318 break;
ths667acca2006-12-11 02:08:05 +00005319 case QEMU_OPTION_no_quit:
5320 no_quit = 1;
5321 break;
aliguori7d957bd2009-01-15 22:14:11 +00005322 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005323 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005324 break;
ths667acca2006-12-11 02:08:05 +00005325#endif
bellardf7cce892004-12-08 22:21:25 +00005326 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005327 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005328 break;
bellarda09db212005-04-30 16:10:35 +00005329#ifdef TARGET_I386
5330 case QEMU_OPTION_win2k_hack:
5331 win2k_install_hack = 1;
5332 break;
aliguori73822ec2009-01-15 20:11:34 +00005333 case QEMU_OPTION_rtc_td_hack:
5334 rtc_td_hack = 1;
5335 break;
aliguori8a92ea22009-02-27 20:12:36 +00005336 case QEMU_OPTION_acpitable:
5337 if(acpi_table_add(optarg) < 0) {
5338 fprintf(stderr, "Wrong acpi table provided\n");
5339 exit(1);
5340 }
5341 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005342 case QEMU_OPTION_smbios:
5343 if(smbios_entry_add(optarg) < 0) {
5344 fprintf(stderr, "Wrong smbios provided\n");
5345 exit(1);
5346 }
5347 break;
bellarda09db212005-04-30 16:10:35 +00005348#endif
aliguori7ba1e612008-11-05 16:04:33 +00005349#ifdef CONFIG_KVM
5350 case QEMU_OPTION_enable_kvm:
5351 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005352 break;
5353#endif
bellardbb36d472005-11-05 14:22:28 +00005354 case QEMU_OPTION_usb:
5355 usb_enabled = 1;
5356 break;
bellarda594cfb2005-11-06 16:13:29 +00005357 case QEMU_OPTION_usbdevice:
5358 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005359 add_device_config(DEV_USB, optarg);
5360 break;
5361 case QEMU_OPTION_device:
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005362 opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
5363 if (!opts) {
5364 fprintf(stderr, "parse error: %s\n", optarg);
5365 exit(1);
5366 }
bellarda594cfb2005-11-06 16:13:29 +00005367 break;
bellard6a00d602005-11-21 23:25:50 +00005368 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005369 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005370 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005371 fprintf(stderr, "Invalid number of CPUs\n");
5372 exit(1);
5373 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005374 if (max_cpus < smp_cpus) {
5375 fprintf(stderr, "maxcpus must be equal to or greater than "
5376 "smp\n");
5377 exit(1);
5378 }
5379 if (max_cpus > 255) {
5380 fprintf(stderr, "Unsupported number of maxcpus\n");
5381 exit(1);
5382 }
bellard6a00d602005-11-21 23:25:50 +00005383 break;
bellard24236862006-04-30 21:28:36 +00005384 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005385 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005386 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005387 break;
blueswir15824d652009-03-28 06:44:27 +00005388#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005389 case QEMU_OPTION_no_acpi:
5390 acpi_enabled = 0;
5391 break;
aliguori16b29ae2008-12-17 23:28:44 +00005392 case QEMU_OPTION_no_hpet:
5393 no_hpet = 1;
5394 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005395 case QEMU_OPTION_balloon:
5396 if (balloon_parse(optarg) < 0) {
5397 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5398 exit(1);
5399 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005400 break;
blueswir15824d652009-03-28 06:44:27 +00005401#endif
bellardd1beab82006-10-02 19:44:22 +00005402 case QEMU_OPTION_no_reboot:
5403 no_reboot = 1;
5404 break;
aurel32b2f76162008-04-11 21:35:52 +00005405 case QEMU_OPTION_no_shutdown:
5406 no_shutdown = 1;
5407 break;
balrog9467cd42007-05-01 01:34:14 +00005408 case QEMU_OPTION_show_cursor:
5409 cursor_hide = 0;
5410 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005411 case QEMU_OPTION_uuid:
5412 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5413 fprintf(stderr, "Fail to parse UUID string."
5414 " Wrong format.\n");
5415 exit(1);
5416 }
5417 break;
blueswir15824d652009-03-28 06:44:27 +00005418#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005419 case QEMU_OPTION_daemonize:
5420 daemonize = 1;
5421 break;
blueswir15824d652009-03-28 06:44:27 +00005422#endif
ths9ae02552007-01-05 17:39:04 +00005423 case QEMU_OPTION_option_rom:
5424 if (nb_option_roms >= MAX_OPTION_ROMS) {
5425 fprintf(stderr, "Too many option ROMs\n");
5426 exit(1);
5427 }
5428 option_rom[nb_option_roms] = optarg;
5429 nb_option_roms++;
5430 break;
blueswir15824d652009-03-28 06:44:27 +00005431#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005432 case QEMU_OPTION_semihosting:
5433 semihosting_enabled = 1;
5434 break;
blueswir15824d652009-03-28 06:44:27 +00005435#endif
thsc35734b2007-03-19 15:17:08 +00005436 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005437 qemu_name = qemu_strdup(optarg);
5438 {
5439 char *p = strchr(qemu_name, ',');
5440 if (p != NULL) {
5441 *p++ = 0;
5442 if (strncmp(p, "process=", 8)) {
5443 fprintf(stderr, "Unknown subargument %s to -name", p);
5444 exit(1);
5445 }
5446 p += 8;
5447 set_proc_name(p);
5448 }
5449 }
thsc35734b2007-03-19 15:17:08 +00005450 break;
blueswir195efd112008-12-24 20:26:14 +00005451#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005452 case QEMU_OPTION_prom_env:
5453 if (nb_prom_envs >= MAX_PROM_ENVS) {
5454 fprintf(stderr, "Too many prom variables\n");
5455 exit(1);
5456 }
5457 prom_envs[nb_prom_envs] = optarg;
5458 nb_prom_envs++;
5459 break;
5460#endif
balrog2b8f2d42007-07-27 22:08:46 +00005461#ifdef TARGET_ARM
5462 case QEMU_OPTION_old_param:
5463 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005464 break;
balrog2b8f2d42007-07-27 22:08:46 +00005465#endif
thsf3dcfad2007-08-24 01:26:02 +00005466 case QEMU_OPTION_clock:
5467 configure_alarms(optarg);
5468 break;
bellard7e0af5d02007-11-07 16:24:33 +00005469 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005470 configure_rtc_date_offset(optarg, 1);
5471 break;
5472 case QEMU_OPTION_rtc:
5473 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5474 if (!opts) {
5475 fprintf(stderr, "parse error: %s\n", optarg);
5476 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005477 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005478 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005479 break;
bellard26a5f132008-05-28 12:30:31 +00005480 case QEMU_OPTION_tb_size:
5481 tb_size = strtol(optarg, NULL, 0);
5482 if (tb_size < 0)
5483 tb_size = 0;
5484 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005485 case QEMU_OPTION_icount:
5486 use_icount = 1;
5487 if (strcmp(optarg, "auto") == 0) {
5488 icount_time_shift = -1;
5489 } else {
5490 icount_time_shift = strtol(optarg, NULL, 0);
5491 }
5492 break;
aliguori5bb79102008-10-13 03:12:02 +00005493 case QEMU_OPTION_incoming:
5494 incoming = optarg;
5495 break;
blueswir15824d652009-03-28 06:44:27 +00005496#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005497 case QEMU_OPTION_chroot:
5498 chroot_dir = optarg;
5499 break;
5500 case QEMU_OPTION_runas:
5501 run_as = optarg;
5502 break;
blueswir15824d652009-03-28 06:44:27 +00005503#endif
aliguorie37630c2009-04-22 15:19:10 +00005504#ifdef CONFIG_XEN
5505 case QEMU_OPTION_xen_domid:
5506 xen_domid = atoi(optarg);
5507 break;
5508 case QEMU_OPTION_xen_create:
5509 xen_mode = XEN_CREATE;
5510 break;
5511 case QEMU_OPTION_xen_attach:
5512 xen_mode = XEN_ATTACH;
5513 break;
5514#endif
bellardcd6f1162004-05-13 22:02:20 +00005515 }
bellard0824d6f2003-06-24 13:42:40 +00005516 }
5517 }
bellard330d0412003-07-26 18:11:40 +00005518
Paul Brook5cea8592009-05-30 00:52:44 +01005519 /* If no data_dir is specified then try to find it relative to the
5520 executable path. */
5521 if (!data_dir) {
5522 data_dir = find_datadir(argv[0]);
5523 }
5524 /* If all else fails use the install patch specified when building. */
5525 if (!data_dir) {
5526 data_dir = CONFIG_QEMU_SHAREDIR;
5527 }
5528
Jes Sorensen6be68d72009-07-23 17:03:42 +02005529 /*
5530 * Default to max_cpus = smp_cpus, in case the user doesn't
5531 * specify a max_cpus value.
5532 */
5533 if (!max_cpus)
5534 max_cpus = smp_cpus;
5535
balrog3d878ca2008-10-28 10:59:59 +00005536 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005537 if (smp_cpus > machine->max_cpus) {
5538 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5539 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5540 machine->max_cpus);
5541 exit(1);
5542 }
5543
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005544 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005545 if (serial_device_index == 0)
5546 serial_devices[0] = "stdio";
5547 if (parallel_device_index == 0)
5548 parallel_devices[0] = "null";
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005549 if (strncmp(monitor_devices[0], "vc", 2) == 0) {
5550 monitor_devices[0] = "stdio";
5551 }
aliguoribc0129d2008-08-01 15:12:34 +00005552 }
5553
ths71e3ceb2006-12-22 02:11:31 +00005554#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005555 if (daemonize) {
5556 pid_t pid;
5557
5558 if (pipe(fds) == -1)
5559 exit(1);
5560
5561 pid = fork();
5562 if (pid > 0) {
5563 uint8_t status;
5564 ssize_t len;
5565
5566 close(fds[1]);
5567
5568 again:
ths93815bc2007-03-19 15:58:31 +00005569 len = read(fds[0], &status, 1);
5570 if (len == -1 && (errno == EINTR))
5571 goto again;
5572
5573 if (len != 1)
5574 exit(1);
5575 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005576 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005577 exit(1);
5578 } else
5579 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005580 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005581 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005582
5583 setsid();
5584
5585 pid = fork();
5586 if (pid > 0)
5587 exit(0);
5588 else if (pid < 0)
5589 exit(1);
5590
5591 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005592
5593 signal(SIGTSTP, SIG_IGN);
5594 signal(SIGTTOU, SIG_IGN);
5595 signal(SIGTTIN, SIG_IGN);
5596 }
ths71e3ceb2006-12-22 02:11:31 +00005597
thsaa26bb22007-03-25 21:33:06 +00005598 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005599 if (daemonize) {
5600 uint8_t status = 1;
5601 write(fds[1], &status, 1);
5602 } else
Justin M. Forbes850810d2009-10-01 09:42:56 -05005603 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005604 exit(1);
5605 }
blueswir1b9e82a52009-04-05 18:03:31 +00005606#endif
ths93815bc2007-03-19 15:58:31 +00005607
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005608 if (kvm_enabled()) {
5609 int ret;
5610
5611 ret = kvm_init(smp_cpus);
5612 if (ret < 0) {
5613 fprintf(stderr, "failed to initialize KVM\n");
5614 exit(1);
5615 }
5616 }
5617
aliguori3fcf7b62009-04-24 18:03:25 +00005618 if (qemu_init_main_loop()) {
5619 fprintf(stderr, "qemu_init_main_loop failed\n");
5620 exit(1);
5621 }
bellarda20dd502003-09-30 21:07:02 +00005622 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005623
thsf8d39c02008-07-03 10:01:15 +00005624 if (!linux_boot && *kernel_cmdline != '\0') {
5625 fprintf(stderr, "-append only allowed with -kernel option\n");
5626 exit(1);
5627 }
5628
5629 if (!linux_boot && initrd_filename != NULL) {
5630 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5631 exit(1);
5632 }
5633
Filip Navarabf65f532009-07-27 10:02:04 -05005634#ifndef _WIN32
5635 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005636 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005637#endif
ths3b46e622007-09-17 08:09:54 +00005638
aliguori7183b4b2008-11-05 20:40:18 +00005639 if (init_timer_alarm() < 0) {
5640 fprintf(stderr, "could not initialize alarm timer\n");
5641 exit(1);
5642 }
pbrook2e70f6e2008-06-29 01:03:05 +00005643 if (use_icount && icount_time_shift < 0) {
5644 use_icount = 2;
5645 /* 125MIPS seems a reasonable initial guess at the guest speed.
5646 It will be corrected fairly quickly anyway. */
5647 icount_time_shift = 3;
5648 init_icount_adjust();
5649 }
pbrook634fce92006-07-15 17:40:09 +00005650
bellardfd1dff42006-02-01 21:29:26 +00005651#ifdef _WIN32
5652 socket_init();
5653#endif
5654
bellard7c9d8e02005-11-15 22:16:05 +00005655 /* init network clients */
5656 if (nb_net_clients == 0) {
5657 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005658 net_clients[nb_net_clients++] = "nic";
5659#ifdef CONFIG_SLIRP
5660 net_clients[nb_net_clients++] = "user";
5661#endif
bellardc20709a2004-04-21 23:27:19 +00005662 }
5663
bellard7c9d8e02005-11-15 22:16:05 +00005664 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005665 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005666 exit(1);
bellard702c6512004-04-02 21:21:32 +00005667 }
bellardf1510b22003-06-25 00:07:40 +00005668
Glauber Costa406c8df2009-06-17 09:05:30 -04005669 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5670 net_set_boot_mask(net_boot);
5671
5672 net_client_check();
thseec85c22007-01-05 17:41:07 +00005673
balrogdc72ac12008-11-09 00:04:26 +00005674 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005675 if (foreach_device_config(DEV_BT, bt_parse))
5676 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005677
bellard0824d6f2003-06-24 13:42:40 +00005678 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005679 if (ram_size == 0)
5680 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005681
bellard26a5f132008-05-28 12:30:31 +00005682 /* init the dynamic translator */
5683 cpu_exec_init_all(tb_size * 1024 * 1024);
5684
bellard5905b2e2004-08-01 21:53:26 +00005685 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005686
5687 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005688 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005689
balrog9d413d12007-12-04 00:10:34 +00005690 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005691 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005692
balrog9d413d12007-12-04 00:10:34 +00005693 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005694 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005695
ths96d30e42007-01-07 20:42:14 +00005696 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005697 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005698 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5699 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005700 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005701
Juan Quintela2faf58c2009-09-10 03:04:28 +02005702 vmstate_register(0, &vmstate_timers ,&timers_state);
aliguori475e4272008-10-06 20:21:51 +00005703 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005704
aliguori3023f332009-01-16 19:04:14 +00005705 /* Maintain compatibility with multiple stdio monitors */
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005706 if (!strcmp(monitor_devices[0],"stdio")) {
aliguori3023f332009-01-16 19:04:14 +00005707 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5708 const char *devname = serial_devices[i];
5709 if (devname && !strcmp(devname,"mon:stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005710 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005711 break;
5712 } else if (devname && !strcmp(devname,"stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005713 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005714 serial_devices[i] = "mon:stdio";
5715 break;
5716 }
5717 }
5718 }
5719
aliguori268a3622009-04-21 22:30:27 +00005720 if (nb_numa_nodes > 0) {
5721 int i;
5722
5723 if (nb_numa_nodes > smp_cpus) {
5724 nb_numa_nodes = smp_cpus;
5725 }
5726
5727 /* If no memory size if given for any node, assume the default case
5728 * and distribute the available memory equally across all nodes
5729 */
5730 for (i = 0; i < nb_numa_nodes; i++) {
5731 if (node_mem[i] != 0)
5732 break;
5733 }
5734 if (i == nb_numa_nodes) {
5735 uint64_t usedmem = 0;
5736
5737 /* On Linux, the each node's border has to be 8MB aligned,
5738 * the final node gets the rest.
5739 */
5740 for (i = 0; i < nb_numa_nodes - 1; i++) {
5741 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5742 usedmem += node_mem[i];
5743 }
5744 node_mem[i] = ram_size - usedmem;
5745 }
5746
5747 for (i = 0; i < nb_numa_nodes; i++) {
5748 if (node_cpumask[i] != 0)
5749 break;
5750 }
5751 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5752 * must cope with this anyway, because there are BIOSes out there in
5753 * real machines which also use this scheme.
5754 */
5755 if (i == nb_numa_nodes) {
5756 for (i = 0; i < smp_cpus; i++) {
5757 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5758 }
5759 }
5760 }
5761
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005762 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5763 const char *devname = monitor_devices[i];
5764 if (devname && strcmp(devname, "none")) {
5765 char label[32];
5766 if (i == 0) {
5767 snprintf(label, sizeof(label), "monitor");
5768 } else {
5769 snprintf(label, sizeof(label), "monitor%d", i);
5770 }
5771 monitor_hds[i] = qemu_chr_open(label, devname, NULL);
5772 if (!monitor_hds[i]) {
5773 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
5774 devname);
5775 exit(1);
5776 }
aliguori4c621802009-01-16 21:48:20 +00005777 }
5778 }
5779
aliguori2796dae2009-01-16 20:23:27 +00005780 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5781 const char *devname = serial_devices[i];
5782 if (devname && strcmp(devname, "none")) {
Aurelien Jarno324a8022009-09-14 23:49:43 +02005783 char label[32];
5784 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005785 serial_hds[i] = qemu_chr_open(label, devname, NULL);
Aurelien Jarno324a8022009-09-14 23:49:43 +02005786 if (!serial_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005787 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
5788 devname, strerror(errno));
Aurelien Jarno324a8022009-09-14 23:49:43 +02005789 exit(1);
5790 }
aliguori2796dae2009-01-16 20:23:27 +00005791 }
5792 }
5793
5794 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5795 const char *devname = parallel_devices[i];
5796 if (devname && strcmp(devname, "none")) {
5797 char label[32];
5798 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005799 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005800 if (!parallel_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005801 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
5802 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005803 exit(1);
5804 }
5805 }
5806 }
5807
5808 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5809 const char *devname = virtio_consoles[i];
5810 if (devname && strcmp(devname, "none")) {
5811 char label[32];
5812 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005813 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005814 if (!virtcon_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005815 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
5816 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005817 exit(1);
5818 }
5819 }
5820 }
5821
Paul Brookaae94602009-05-14 22:35:06 +01005822 module_call_init(MODULE_INIT_DEVICE);
5823
Markus Armbruster09aaa162009-08-21 10:31:34 +02005824 if (watchdog) {
5825 i = select_watchdog(watchdog);
5826 if (i > 0)
5827 exit (i == 1 ? 1 : 0);
5828 }
5829
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005830 if (machine->compat_props) {
5831 qdev_prop_register_compat(machine->compat_props);
5832 }
Paul Brookfbe1b592009-05-13 17:56:25 +01005833 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005834 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5835
aliguori268a3622009-04-21 22:30:27 +00005836
Juan Quintela67b3b712009-08-28 19:25:15 +02005837#ifndef _WIN32
5838 /* must be after terminal init, SDL library changes signal handlers */
5839 sighandler_setup();
5840#endif
5841
aliguori268a3622009-04-21 22:30:27 +00005842 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5843 for (i = 0; i < nb_numa_nodes; i++) {
5844 if (node_cpumask[i] & (1 << env->cpu_index)) {
5845 env->numa_node = i;
5846 }
5847 }
5848 }
5849
aliguori6f338c32009-02-11 15:21:54 +00005850 current_machine = machine;
5851
aliguori3023f332009-01-16 19:04:14 +00005852 /* init USB devices */
5853 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005854 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5855 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005856 }
5857
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005858 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005859 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005860 exit(1);
5861
aliguori8f391ab2009-01-19 16:34:10 +00005862 if (!display_state)
5863 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005864 /* just use the first displaystate for the moment */
5865 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005866
5867 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005868#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005869 display_type = DT_SDL;
5870#else
5871 display_type = DT_VNC;
5872 vnc_display = "localhost:0,to=99";
5873 show_vnc_port = 1;
5874#endif
5875 }
5876
5877
5878 switch (display_type) {
5879 case DT_NOGRAPHIC:
5880 break;
5881#if defined(CONFIG_CURSES)
5882 case DT_CURSES:
5883 curses_display_init(ds, full_screen);
5884 break;
5885#endif
bellard5b0753e2005-03-01 21:37:28 +00005886#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005887 case DT_SDL:
5888 sdl_display_init(ds, full_screen, no_frame);
5889 break;
bellard5b0753e2005-03-01 21:37:28 +00005890#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005891 case DT_SDL:
5892 cocoa_display_init(ds, full_screen);
5893 break;
bellard313aa562003-08-10 21:52:11 +00005894#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005895 case DT_VNC:
5896 vnc_display_init(ds);
5897 if (vnc_display_open(ds, vnc_display) < 0)
5898 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005899
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005900 if (show_vnc_port) {
5901 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005902 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005903 break;
5904 default:
5905 break;
bellard313aa562003-08-10 21:52:11 +00005906 }
aliguori7d957bd2009-01-15 22:14:11 +00005907 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005908
aliguori3023f332009-01-16 19:04:14 +00005909 dcl = ds->listeners;
5910 while (dcl != NULL) {
5911 if (dcl->dpy_refresh != NULL) {
5912 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5913 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005914 }
aliguori3023f332009-01-16 19:04:14 +00005915 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005916 }
aliguori3023f332009-01-16 19:04:14 +00005917
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005918 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005919 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5920 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5921 }
5922
aliguori2796dae2009-01-16 20:23:27 +00005923 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005924 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005925
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005926 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5927 if (monitor_devices[i] && monitor_hds[i]) {
5928 monitor_init(monitor_hds[i],
5929 MONITOR_USE_READLINE |
5930 ((i == 0) ? MONITOR_IS_DEFAULT : 0));
5931 }
5932 }
bellard82c643f2004-07-14 17:28:13 +00005933
bellard8d11df92004-08-24 21:13:40 +00005934 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005935 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005936 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005937 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005938 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005939 }
bellard82c643f2004-07-14 17:28:13 +00005940 }
bellard82c643f2004-07-14 17:28:13 +00005941
bellard6508fe52005-01-15 12:02:56 +00005942 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005943 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005944 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005945 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005946 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005947 }
5948 }
5949
aliguori9ede2fd2009-01-15 20:05:25 +00005950 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5951 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005952 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005953 if (strstart(devname, "vc", 0))
5954 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5955 }
5956 }
5957
aliguori59030a82009-04-05 18:43:41 +00005958 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5959 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5960 gdbstub_dev);
5961 exit(1);
balrog45669e02007-07-02 13:20:17 +00005962 }
balrog45669e02007-07-02 13:20:17 +00005963
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02005964 qdev_machine_creation_done();
5965
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02005966 rom_load_all();
5967
Juan Quintela05f24012009-08-20 19:42:22 +02005968 if (loadvm) {
5969 if (load_vmstate(cur_mon, loadvm) < 0) {
5970 autostart = 0;
5971 }
5972 }
bellardd63d3072004-10-03 13:29:03 +00005973
Glauber Costa2bb8c102009-07-24 16:20:23 -04005974 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005975 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005976 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005977 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005978 }
thsffd843b2006-12-21 19:46:43 +00005979
blueswir1b9e82a52009-04-05 18:03:31 +00005980#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005981 if (daemonize) {
5982 uint8_t status = 0;
5983 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005984
5985 again1:
5986 len = write(fds[1], &status, 1);
5987 if (len == -1 && (errno == EINTR))
5988 goto again1;
5989
5990 if (len != 1)
5991 exit(1);
5992
aliguoribd54b862008-07-23 00:58:33 +00005993 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005994 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005995 if (fd == -1)
5996 exit(1);
aliguori08585322009-02-27 22:09:45 +00005997 }
ths71e3ceb2006-12-22 02:11:31 +00005998
aliguori08585322009-02-27 22:09:45 +00005999 if (run_as) {
6000 pwd = getpwnam(run_as);
6001 if (!pwd) {
6002 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6003 exit(1);
6004 }
6005 }
ths71e3ceb2006-12-22 02:11:31 +00006006
aliguori08585322009-02-27 22:09:45 +00006007 if (chroot_dir) {
6008 if (chroot(chroot_dir) < 0) {
6009 fprintf(stderr, "chroot failed\n");
6010 exit(1);
6011 }
6012 chdir("/");
6013 }
6014
6015 if (run_as) {
6016 if (setgid(pwd->pw_gid) < 0) {
6017 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6018 exit(1);
6019 }
6020 if (setuid(pwd->pw_uid) < 0) {
6021 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6022 exit(1);
6023 }
6024 if (setuid(0) != -1) {
6025 fprintf(stderr, "Dropping privileges failed\n");
6026 exit(1);
6027 }
6028 }
aliguori08585322009-02-27 22:09:45 +00006029
6030 if (daemonize) {
6031 dup2(fd, 0);
6032 dup2(fd, 1);
6033 dup2(fd, 2);
6034
6035 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006036 }
blueswir1b9e82a52009-04-05 18:03:31 +00006037#endif
ths71e3ceb2006-12-22 02:11:31 +00006038
bellard8a7ddc32004-03-31 19:00:16 +00006039 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006040 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006041 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006042
bellard0824d6f2003-06-24 13:42:40 +00006043 return 0;
6044}