blob: 374f85be33c9a85d98800718ac2abf69fd63a082 [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)) {
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002598 QemuOpts *opts;
2599 int idx;
balrog6c9f8862008-07-17 20:47:13 +00002600
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002601 opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
2602 if (!opts) {
balrog6c9f8862008-07-17 20:47:13 +00002603 return -1;
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002604 }
2605
2606 qemu_opt_set(opts, "type", "nic");
2607 qemu_opt_set(opts, "model", "usb");
2608
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002609 idx = net_client_init(NULL, opts);
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002610 if (idx == -1) {
2611 return -1;
2612 }
2613
2614 dev = usb_net_init(&nd_table[idx]);
balrogdc72ac12008-11-09 00:04:26 +00002615 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2616 dev = usb_bt_init(devname[2] ? hci_init(p) :
2617 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002618 } else {
2619 return -1;
2620 }
pbrook0d92ed32006-05-21 16:30:15 +00002621 if (!dev)
2622 return -1;
2623
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002624done:
bellarda594cfb2005-11-06 16:13:29 +00002625 return 0;
2626}
2627
aliguori1f3870a2008-08-21 19:27:48 +00002628static int usb_device_del(const char *devname)
2629{
2630 int bus_num, addr;
2631 const char *p;
2632
aliguori5d0c5752008-09-14 01:07:41 +00002633 if (strstart(devname, "host:", &p))
2634 return usb_host_device_close(p);
2635
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002636 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002637 return -1;
2638
2639 p = strchr(devname, '.');
2640 if (!p)
2641 return -1;
2642 bus_num = strtoul(devname, NULL, 0);
2643 addr = strtoul(p + 1, NULL, 0);
2644
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002645 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002646}
2647
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002648static int usb_parse(const char *cmdline)
2649{
2650 return usb_device_add(cmdline, 0);
2651}
2652
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002653void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002654{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002655 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002656}
2657
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002658void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002659{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002660 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002661}
2662
bellardf7cce892004-12-08 22:21:25 +00002663/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002664/* PCMCIA/Cardbus */
2665
2666static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002667 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002668 struct pcmcia_socket_entry_s *next;
2669} *pcmcia_sockets = 0;
2670
Paul Brookbc24a222009-05-10 01:44:56 +01002671void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002672{
2673 struct pcmcia_socket_entry_s *entry;
2674
2675 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2676 entry->socket = socket;
2677 entry->next = pcmcia_sockets;
2678 pcmcia_sockets = entry;
2679}
2680
Paul Brookbc24a222009-05-10 01:44:56 +01002681void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002682{
2683 struct pcmcia_socket_entry_s *entry, **ptr;
2684
2685 ptr = &pcmcia_sockets;
2686 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2687 if (entry->socket == socket) {
2688 *ptr = entry->next;
2689 qemu_free(entry);
2690 }
2691}
2692
aliguori376253e2009-03-05 23:01:23 +00002693void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002694{
2695 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002696
balrog201a51f2007-04-30 00:51:09 +00002697 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002698 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002699
2700 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002701 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2702 iter->socket->attached ? iter->socket->card_string :
2703 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002704}
2705
2706/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002707/* register display */
2708
aliguori7b5d76d2009-03-13 15:02:13 +00002709struct DisplayAllocator default_allocator = {
2710 defaultallocator_create_displaysurface,
2711 defaultallocator_resize_displaysurface,
2712 defaultallocator_free_displaysurface
2713};
2714
aliguori3023f332009-01-16 19:04:14 +00002715void register_displaystate(DisplayState *ds)
2716{
2717 DisplayState **s;
2718 s = &display_state;
2719 while (*s != NULL)
2720 s = &(*s)->next;
2721 ds->next = NULL;
2722 *s = ds;
2723}
2724
2725DisplayState *get_displaystate(void)
2726{
2727 return display_state;
2728}
2729
aliguori7b5d76d2009-03-13 15:02:13 +00002730DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2731{
2732 if(ds->allocator == &default_allocator) ds->allocator = da;
2733 return ds->allocator;
2734}
2735
ths2ff89792007-06-21 23:34:19 +00002736/* dumb display */
2737
aliguori8f391ab2009-01-19 16:34:10 +00002738static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002739{
aliguori8f391ab2009-01-19 16:34:10 +00002740 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002741 ds->allocator = &default_allocator;
2742 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002743 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002744}
2745
2746/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002747/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002748
bellardc4b1fcc2004-03-14 21:44:30 +00002749typedef struct IOHandlerRecord {
2750 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002751 IOCanRWHandler *fd_read_poll;
2752 IOHandler *fd_read;
2753 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002754 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002755 void *opaque;
2756 /* temporary data */
2757 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002758 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002759} IOHandlerRecord;
2760
bellard8a7ddc32004-03-31 19:00:16 +00002761static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002762
bellard7c9d8e02005-11-15 22:16:05 +00002763/* XXX: fd_read_poll should be suppressed, but an API change is
2764 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002765int qemu_set_fd_handler2(int fd,
2766 IOCanRWHandler *fd_read_poll,
2767 IOHandler *fd_read,
2768 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002769 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002770{
bellard8a7ddc32004-03-31 19:00:16 +00002771 IOHandlerRecord **pioh, *ioh;
2772
bellard7c9d8e02005-11-15 22:16:05 +00002773 if (!fd_read && !fd_write) {
2774 pioh = &first_io_handler;
2775 for(;;) {
2776 ioh = *pioh;
2777 if (ioh == NULL)
2778 break;
2779 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002780 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002781 break;
2782 }
2783 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002784 }
bellard7c9d8e02005-11-15 22:16:05 +00002785 } else {
2786 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2787 if (ioh->fd == fd)
2788 goto found;
2789 }
2790 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002791 ioh->next = first_io_handler;
2792 first_io_handler = ioh;
2793 found:
2794 ioh->fd = fd;
2795 ioh->fd_read_poll = fd_read_poll;
2796 ioh->fd_read = fd_read;
2797 ioh->fd_write = fd_write;
2798 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002799 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002800 }
bellard7c9d8e02005-11-15 22:16:05 +00002801 return 0;
2802}
2803
ths5fafdf22007-09-16 21:08:06 +00002804int qemu_set_fd_handler(int fd,
2805 IOHandler *fd_read,
2806 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002807 void *opaque)
2808{
2809 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002810}
2811
aliguori56f3a5d2008-10-31 18:07:17 +00002812#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002813/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002814/* Polling handling */
2815
2816typedef struct PollingEntry {
2817 PollingFunc *func;
2818 void *opaque;
2819 struct PollingEntry *next;
2820} PollingEntry;
2821
2822static PollingEntry *first_polling_entry;
2823
2824int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2825{
2826 PollingEntry **ppe, *pe;
2827 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002828 pe->func = func;
2829 pe->opaque = opaque;
2830 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2831 *ppe = pe;
2832 return 0;
2833}
2834
2835void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2836{
2837 PollingEntry **ppe, *pe;
2838 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2839 pe = *ppe;
2840 if (pe->func == func && pe->opaque == opaque) {
2841 *ppe = pe->next;
2842 qemu_free(pe);
2843 break;
2844 }
2845 }
2846}
2847
bellarda18e5242006-06-25 17:18:27 +00002848/***********************************************************/
2849/* Wait objects support */
2850typedef struct WaitObjects {
2851 int num;
2852 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2853 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2854 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2855} WaitObjects;
2856
2857static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002858
bellarda18e5242006-06-25 17:18:27 +00002859int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2860{
2861 WaitObjects *w = &wait_objects;
2862
2863 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2864 return -1;
2865 w->events[w->num] = handle;
2866 w->func[w->num] = func;
2867 w->opaque[w->num] = opaque;
2868 w->num++;
2869 return 0;
2870}
2871
2872void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2873{
2874 int i, found;
2875 WaitObjects *w = &wait_objects;
2876
2877 found = 0;
2878 for (i = 0; i < w->num; i++) {
2879 if (w->events[i] == handle)
2880 found = 1;
2881 if (found) {
2882 w->events[i] = w->events[i + 1];
2883 w->func[i] = w->func[i + 1];
2884 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002885 }
bellarda18e5242006-06-25 17:18:27 +00002886 }
2887 if (found)
2888 w->num--;
2889}
2890#endif
2891
bellard8a7ddc32004-03-31 19:00:16 +00002892/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002893/* ram save/restore */
2894
Juan Quintela94fb0902009-09-10 03:04:23 +02002895#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002896#define RAM_SAVE_FLAG_COMPRESS 0x02
2897#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2898#define RAM_SAVE_FLAG_PAGE 0x08
2899#define RAM_SAVE_FLAG_EOS 0x10
2900
2901static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002902{
aliguori475e4272008-10-06 20:21:51 +00002903 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2904 uint32_t *array = (uint32_t *)page;
2905 int i;
ths3b46e622007-09-17 08:09:54 +00002906
aliguori475e4272008-10-06 20:21:51 +00002907 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2908 if (array[i] != val)
2909 return 0;
bellardc88676f2006-08-06 13:36:11 +00002910 }
aliguori475e4272008-10-06 20:21:51 +00002911
2912 return 1;
bellardc88676f2006-08-06 13:36:11 +00002913}
2914
aliguori475e4272008-10-06 20:21:51 +00002915static int ram_save_block(QEMUFile *f)
2916{
Anthony Liguoric227f092009-10-01 16:12:16 -05002917 static ram_addr_t current_addr = 0;
2918 ram_addr_t saved_addr = current_addr;
2919 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002920 int found = 0;
2921
pbrook94a6b542009-04-11 17:15:54 +00002922 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002923 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002924 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002925
2926 cpu_physical_memory_reset_dirty(current_addr,
2927 current_addr + TARGET_PAGE_SIZE,
2928 MIGRATION_DIRTY_FLAG);
2929
pbrook5579c7f2009-04-11 14:47:08 +00002930 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002931
pbrook5579c7f2009-04-11 14:47:08 +00002932 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002933 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002934 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002935 } else {
2936 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002937 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002938 }
2939
2940 found = 1;
2941 break;
2942 }
2943 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002944 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002945 }
2946
2947 return found;
2948}
2949
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002950static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00002951
Anthony Liguoric227f092009-10-01 16:12:16 -05002952static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002953{
Anthony Liguoric227f092009-10-01 16:12:16 -05002954 ram_addr_t addr;
2955 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002956
pbrook94a6b542009-04-11 17:15:54 +00002957 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002958 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2959 count++;
2960 }
2961
2962 return count;
2963}
2964
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002965uint64_t ram_bytes_remaining(void)
2966{
2967 return ram_save_remaining() * TARGET_PAGE_SIZE;
2968}
2969
2970uint64_t ram_bytes_transferred(void)
2971{
2972 return bytes_transferred;
2973}
2974
2975uint64_t ram_bytes_total(void)
2976{
2977 return last_ram_offset;
2978}
2979
aliguori475e4272008-10-06 20:21:51 +00002980static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2981{
Anthony Liguoric227f092009-10-01 16:12:16 -05002982 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002983 uint64_t bytes_transferred_last;
2984 double bwidth = 0;
2985 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002986
Jan Kiszka9fa06382009-05-22 23:51:45 +02002987 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002988 qemu_file_set_error(f);
2989 return 0;
2990 }
2991
aliguori475e4272008-10-06 20:21:51 +00002992 if (stage == 1) {
2993 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002994 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002995 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2996 cpu_physical_memory_set_dirty(addr);
2997 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002998
aliguori475e4272008-10-06 20:21:51 +00002999 /* Enable dirty memory tracking */
3000 cpu_physical_memory_set_dirty_tracking(1);
3001
pbrook94a6b542009-04-11 17:15:54 +00003002 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003003 }
3004
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003005 bytes_transferred_last = bytes_transferred;
3006 bwidth = get_clock();
3007
aliguori475e4272008-10-06 20:21:51 +00003008 while (!qemu_file_rate_limit(f)) {
3009 int ret;
3010
3011 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003012 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003013 if (ret == 0) /* no more blocks */
3014 break;
3015 }
3016
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003017 bwidth = get_clock() - bwidth;
3018 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3019
3020 /* if we haven't transferred anything this round, force expected_time to a
3021 * a very high value, but without crashing */
3022 if (bwidth == 0)
3023 bwidth = 0.000001;
3024
aliguori475e4272008-10-06 20:21:51 +00003025 /* try transferring iterative blocks of memory */
3026
3027 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003028
3029 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003030 while (ram_save_block(f) != 0) {
3031 bytes_transferred += TARGET_PAGE_SIZE;
3032 }
aliguori8215e912009-04-05 19:30:55 +00003033 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003034 }
3035
3036 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3037
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003038 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3039
3040 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003041}
3042
aliguori475e4272008-10-06 20:21:51 +00003043static int ram_load(QEMUFile *f, void *opaque, int version_id)
3044{
Anthony Liguoric227f092009-10-01 16:12:16 -05003045 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003046 int flags;
3047
aliguori475e4272008-10-06 20:21:51 +00003048 if (version_id != 3)
3049 return -EINVAL;
3050
3051 do {
3052 addr = qemu_get_be64(f);
3053
3054 flags = addr & ~TARGET_PAGE_MASK;
3055 addr &= TARGET_PAGE_MASK;
3056
3057 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003058 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003059 return -EINVAL;
3060 }
3061
aliguori475e4272008-10-06 20:21:51 +00003062 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3063 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003064 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3065#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003066 if (ch == 0 &&
3067 (!kvm_enabled() || kvm_has_sync_mmu())) {
3068 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003069 }
Anthony Liguori30868442009-06-17 16:46:12 -05003070#endif
aliguori475e4272008-10-06 20:21:51 +00003071 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003072 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003073 } while (!(flags & RAM_SAVE_FLAG_EOS));
3074
bellardc88676f2006-08-06 13:36:11 +00003075 return 0;
3076}
3077
aliguori9e472e12008-10-08 19:50:24 +00003078void qemu_service_io(void)
3079{
aliguorid9f75a42009-04-24 18:03:11 +00003080 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003081}
3082
bellard8a7ddc32004-03-31 19:00:16 +00003083/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003084/* bottom halves (can be seen as timers which expire ASAP) */
3085
3086struct QEMUBH {
3087 QEMUBHFunc *cb;
3088 void *opaque;
3089 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003090 int idle;
3091 int deleted;
bellard83f64092006-08-01 16:21:11 +00003092 QEMUBH *next;
3093};
3094
3095static QEMUBH *first_bh = NULL;
3096
3097QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3098{
3099 QEMUBH *bh;
3100 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003101 bh->cb = cb;
3102 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003103 bh->next = first_bh;
3104 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003105 return bh;
3106}
3107
bellard6eb57332006-08-06 09:51:25 +00003108int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003109{
aliguori1b435b12008-10-31 17:24:21 +00003110 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003111 int ret;
bellard83f64092006-08-01 16:21:11 +00003112
bellard6eb57332006-08-06 09:51:25 +00003113 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003114 for (bh = first_bh; bh; bh = bh->next) {
3115 if (!bh->deleted && bh->scheduled) {
3116 bh->scheduled = 0;
3117 if (!bh->idle)
3118 ret = 1;
3119 bh->idle = 0;
3120 bh->cb(bh->opaque);
3121 }
bellard83f64092006-08-01 16:21:11 +00003122 }
aliguori1b435b12008-10-31 17:24:21 +00003123
3124 /* remove deleted bhs */
3125 bhp = &first_bh;
3126 while (*bhp) {
3127 bh = *bhp;
3128 if (bh->deleted) {
3129 *bhp = bh->next;
3130 qemu_free(bh);
3131 } else
3132 bhp = &bh->next;
3133 }
3134
bellard6eb57332006-08-06 09:51:25 +00003135 return ret;
bellard83f64092006-08-01 16:21:11 +00003136}
3137
aliguori1b435b12008-10-31 17:24:21 +00003138void qemu_bh_schedule_idle(QEMUBH *bh)
3139{
3140 if (bh->scheduled)
3141 return;
3142 bh->scheduled = 1;
3143 bh->idle = 1;
3144}
3145
bellard83f64092006-08-01 16:21:11 +00003146void qemu_bh_schedule(QEMUBH *bh)
3147{
bellard83f64092006-08-01 16:21:11 +00003148 if (bh->scheduled)
3149 return;
3150 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003151 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003152 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003153 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003154}
3155
3156void qemu_bh_cancel(QEMUBH *bh)
3157{
aliguori1b435b12008-10-31 17:24:21 +00003158 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003159}
3160
3161void qemu_bh_delete(QEMUBH *bh)
3162{
aliguori1b435b12008-10-31 17:24:21 +00003163 bh->scheduled = 0;
3164 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003165}
3166
aliguori56f3a5d2008-10-31 18:07:17 +00003167static void qemu_bh_update_timeout(int *timeout)
3168{
3169 QEMUBH *bh;
3170
3171 for (bh = first_bh; bh; bh = bh->next) {
3172 if (!bh->deleted && bh->scheduled) {
3173 if (bh->idle) {
3174 /* idle bottom halves will be polled at least
3175 * every 10ms */
3176 *timeout = MIN(10, *timeout);
3177 } else {
3178 /* non-idle bottom halves will be executed
3179 * immediately */
3180 *timeout = 0;
3181 break;
3182 }
3183 }
3184 }
3185}
3186
bellard83f64092006-08-01 16:21:11 +00003187/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003188/* machine registration */
3189
blueswir1bdaf78e2008-10-04 07:24:27 +00003190static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003191QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003192
3193int qemu_register_machine(QEMUMachine *m)
3194{
3195 QEMUMachine **pm;
3196 pm = &first_machine;
3197 while (*pm != NULL)
3198 pm = &(*pm)->next;
3199 m->next = NULL;
3200 *pm = m;
3201 return 0;
3202}
3203
pbrook9596ebb2007-11-18 01:44:38 +00003204static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003205{
3206 QEMUMachine *m;
3207
3208 for(m = first_machine; m != NULL; m = m->next) {
3209 if (!strcmp(m->name, name))
3210 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003211 if (m->alias && !strcmp(m->alias, name))
3212 return m;
bellardcc1daa42005-06-05 14:49:17 +00003213 }
3214 return NULL;
3215}
3216
Anthony Liguori0c257432009-05-21 20:41:01 -05003217static QEMUMachine *find_default_machine(void)
3218{
3219 QEMUMachine *m;
3220
3221 for(m = first_machine; m != NULL; m = m->next) {
3222 if (m->is_default) {
3223 return m;
3224 }
3225 }
3226 return NULL;
3227}
3228
bellardcc1daa42005-06-05 14:49:17 +00003229/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003230/* main execution loop */
3231
pbrook9596ebb2007-11-18 01:44:38 +00003232static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003233{
aliguori7d957bd2009-01-15 22:14:11 +00003234 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003235 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003236 DisplayChangeListener *dcl = ds->listeners;
3237
3238 dpy_refresh(ds);
3239
3240 while (dcl != NULL) {
3241 if (dcl->gui_timer_interval &&
3242 dcl->gui_timer_interval < interval)
3243 interval = dcl->gui_timer_interval;
3244 dcl = dcl->next;
3245 }
3246 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003247}
3248
blueswir19043b622009-01-21 19:28:13 +00003249static void nographic_update(void *opaque)
3250{
3251 uint64_t interval = GUI_REFRESH_INTERVAL;
3252
3253 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3254}
3255
bellard0bd48852005-11-11 00:00:47 +00003256struct vm_change_state_entry {
3257 VMChangeStateHandler *cb;
3258 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003259 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003260};
3261
Blue Swirl72cf2d42009-09-12 07:36:22 +00003262static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003263
3264VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3265 void *opaque)
3266{
3267 VMChangeStateEntry *e;
3268
3269 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003270
3271 e->cb = cb;
3272 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003273 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003274 return e;
3275}
3276
3277void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3278{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003279 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003280 qemu_free (e);
3281}
3282
aliguori9781e042009-01-22 17:15:29 +00003283static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003284{
3285 VMChangeStateEntry *e;
3286
3287 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003288 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003289 }
3290}
3291
aliguorid6dc3d42009-04-24 18:04:07 +00003292static void resume_all_vcpus(void);
3293static void pause_all_vcpus(void);
3294
bellard8a7ddc32004-03-31 19:00:16 +00003295void vm_start(void)
3296{
3297 if (!vm_running) {
3298 cpu_enable_ticks();
3299 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003300 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003301 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003302 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003303 }
3304}
3305
bellardbb0c6722004-06-20 12:37:32 +00003306/* reset/shutdown handler */
3307
3308typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003309 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003310 QEMUResetHandler *func;
3311 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003312} QEMUResetEntry;
3313
Blue Swirl72cf2d42009-09-12 07:36:22 +00003314static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3315 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003316static int reset_requested;
3317static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003318static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003319static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003320static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003321
aurel32cf7a2fe2008-03-18 06:53:05 +00003322int qemu_shutdown_requested(void)
3323{
3324 int r = shutdown_requested;
3325 shutdown_requested = 0;
3326 return r;
3327}
3328
3329int qemu_reset_requested(void)
3330{
3331 int r = reset_requested;
3332 reset_requested = 0;
3333 return r;
3334}
3335
3336int qemu_powerdown_requested(void)
3337{
3338 int r = powerdown_requested;
3339 powerdown_requested = 0;
3340 return r;
3341}
3342
aliguorie5689022009-04-24 18:03:54 +00003343static int qemu_debug_requested(void)
3344{
3345 int r = debug_requested;
3346 debug_requested = 0;
3347 return r;
3348}
3349
aliguori6e29f5d2009-04-24 18:04:02 +00003350static int qemu_vmstop_requested(void)
3351{
3352 int r = vmstop_requested;
3353 vmstop_requested = 0;
3354 return r;
3355}
3356
3357static void do_vm_stop(int reason)
3358{
3359 if (vm_running) {
3360 cpu_disable_ticks();
3361 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003362 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003363 vm_state_notify(0, reason);
3364 }
3365}
3366
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003367void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003368{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003369 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003370
bellardbb0c6722004-06-20 12:37:32 +00003371 re->func = func;
3372 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003373 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003374}
3375
Jan Kiszkadda9b292009-07-02 00:19:02 +02003376void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003377{
3378 QEMUResetEntry *re;
3379
Blue Swirl72cf2d42009-09-12 07:36:22 +00003380 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003381 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003382 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003383 qemu_free(re);
3384 return;
3385 }
3386 }
3387}
3388
3389void qemu_system_reset(void)
3390{
3391 QEMUResetEntry *re, *nre;
3392
3393 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003394 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003395 re->func(re->opaque);
3396 }
3397}
3398
3399void qemu_system_reset_request(void)
3400{
bellardd1beab82006-10-02 19:44:22 +00003401 if (no_reboot) {
3402 shutdown_requested = 1;
3403 } else {
3404 reset_requested = 1;
3405 }
aliguorid9f75a42009-04-24 18:03:11 +00003406 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003407}
3408
3409void qemu_system_shutdown_request(void)
3410{
3411 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003412 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003413}
3414
bellard34751872005-07-02 14:31:34 +00003415void qemu_system_powerdown_request(void)
3416{
3417 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003418 qemu_notify_event();
3419}
3420
aliguorid6dc3d42009-04-24 18:04:07 +00003421#ifdef CONFIG_IOTHREAD
3422static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003423{
aliguorid6dc3d42009-04-24 18:04:07 +00003424 vmstop_requested = reason;
3425 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003426}
aliguorid6dc3d42009-04-24 18:04:07 +00003427#endif
bellardbb0c6722004-06-20 12:37:32 +00003428
aliguori50317c72009-04-24 18:03:29 +00003429#ifndef _WIN32
3430static int io_thread_fd = -1;
3431
3432static void qemu_event_increment(void)
3433{
3434 static const char byte = 0;
3435
3436 if (io_thread_fd == -1)
3437 return;
3438
3439 write(io_thread_fd, &byte, sizeof(byte));
3440}
3441
3442static void qemu_event_read(void *opaque)
3443{
3444 int fd = (unsigned long)opaque;
3445 ssize_t len;
3446
3447 /* Drain the notify pipe */
3448 do {
3449 char buffer[512];
3450 len = read(fd, buffer, sizeof(buffer));
3451 } while ((len == -1 && errno == EINTR) || len > 0);
3452}
3453
3454static int qemu_event_init(void)
3455{
3456 int err;
3457 int fds[2];
3458
3459 err = pipe(fds);
3460 if (err == -1)
3461 return -errno;
3462
3463 err = fcntl_setfl(fds[0], O_NONBLOCK);
3464 if (err < 0)
3465 goto fail;
3466
3467 err = fcntl_setfl(fds[1], O_NONBLOCK);
3468 if (err < 0)
3469 goto fail;
3470
3471 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3472 (void *)(unsigned long)fds[0]);
3473
3474 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003475 return 0;
3476
aliguori50317c72009-04-24 18:03:29 +00003477fail:
3478 close(fds[0]);
3479 close(fds[1]);
3480 return err;
3481}
3482#else
3483HANDLE qemu_event_handle;
3484
3485static void dummy_event_handler(void *opaque)
3486{
3487}
3488
3489static int qemu_event_init(void)
3490{
3491 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3492 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003493 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003494 return -1;
3495 }
3496 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3497 return 0;
3498}
3499
3500static void qemu_event_increment(void)
3501{
malcde1c90c2009-09-27 14:38:18 +04003502 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003503 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003504 GetLastError());
3505 exit (1);
3506 }
aliguori50317c72009-04-24 18:03:29 +00003507}
3508#endif
3509
aliguorid6dc3d42009-04-24 18:04:07 +00003510static int cpu_can_run(CPUState *env)
3511{
3512 if (env->stop)
3513 return 0;
3514 if (env->stopped)
3515 return 0;
3516 return 1;
3517}
3518
3519#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003520static int qemu_init_main_loop(void)
3521{
aliguori50317c72009-04-24 18:03:29 +00003522 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003523}
3524
aliguori0bf46a42009-04-24 18:03:41 +00003525void qemu_init_vcpu(void *_env)
3526{
3527 CPUState *env = _env;
3528
3529 if (kvm_enabled())
3530 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003531 env->nr_cores = smp_cores;
3532 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003533 return;
3534}
3535
aliguori8edac962009-04-24 18:03:45 +00003536int qemu_cpu_self(void *env)
3537{
3538 return 1;
3539}
3540
aliguorid6dc3d42009-04-24 18:04:07 +00003541static void resume_all_vcpus(void)
3542{
3543}
3544
3545static void pause_all_vcpus(void)
3546{
3547}
3548
aliguori8edac962009-04-24 18:03:45 +00003549void qemu_cpu_kick(void *env)
3550{
3551 return;
3552}
3553
aliguorid6dc3d42009-04-24 18:04:07 +00003554void qemu_notify_event(void)
3555{
3556 CPUState *env = cpu_single_env;
3557
3558 if (env) {
3559 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003560 }
aliguorid6dc3d42009-04-24 18:04:07 +00003561}
3562
aliguori48708522009-04-24 18:03:49 +00003563#define qemu_mutex_lock_iothread() do { } while (0)
3564#define qemu_mutex_unlock_iothread() do { } while (0)
3565
aliguori6e29f5d2009-04-24 18:04:02 +00003566void vm_stop(int reason)
3567{
3568 do_vm_stop(reason);
3569}
3570
aliguorid6dc3d42009-04-24 18:04:07 +00003571#else /* CONFIG_IOTHREAD */
3572
3573#include "qemu-thread.h"
3574
3575QemuMutex qemu_global_mutex;
3576static QemuMutex qemu_fair_mutex;
3577
3578static QemuThread io_thread;
3579
3580static QemuThread *tcg_cpu_thread;
3581static QemuCond *tcg_halt_cond;
3582
3583static int qemu_system_ready;
3584/* cpu creation */
3585static QemuCond qemu_cpu_cond;
3586/* system init */
3587static QemuCond qemu_system_cond;
3588static QemuCond qemu_pause_cond;
3589
3590static void block_io_signals(void);
3591static void unblock_io_signals(void);
3592static int tcg_has_work(void);
3593
3594static int qemu_init_main_loop(void)
3595{
3596 int ret;
3597
3598 ret = qemu_event_init();
3599 if (ret)
3600 return ret;
3601
3602 qemu_cond_init(&qemu_pause_cond);
3603 qemu_mutex_init(&qemu_fair_mutex);
3604 qemu_mutex_init(&qemu_global_mutex);
3605 qemu_mutex_lock(&qemu_global_mutex);
3606
3607 unblock_io_signals();
3608 qemu_thread_self(&io_thread);
3609
3610 return 0;
3611}
3612
3613static void qemu_wait_io_event(CPUState *env)
3614{
3615 while (!tcg_has_work())
3616 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3617
3618 qemu_mutex_unlock(&qemu_global_mutex);
3619
3620 /*
3621 * Users of qemu_global_mutex can be starved, having no chance
3622 * to acquire it since this path will get to it first.
3623 * So use another lock to provide fairness.
3624 */
3625 qemu_mutex_lock(&qemu_fair_mutex);
3626 qemu_mutex_unlock(&qemu_fair_mutex);
3627
3628 qemu_mutex_lock(&qemu_global_mutex);
3629 if (env->stop) {
3630 env->stop = 0;
3631 env->stopped = 1;
3632 qemu_cond_signal(&qemu_pause_cond);
3633 }
3634}
3635
3636static int qemu_cpu_exec(CPUState *env);
3637
3638static void *kvm_cpu_thread_fn(void *arg)
3639{
3640 CPUState *env = arg;
3641
3642 block_io_signals();
3643 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003644 if (kvm_enabled())
3645 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003646
3647 /* signal CPU creation */
3648 qemu_mutex_lock(&qemu_global_mutex);
3649 env->created = 1;
3650 qemu_cond_signal(&qemu_cpu_cond);
3651
3652 /* and wait for machine initialization */
3653 while (!qemu_system_ready)
3654 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3655
3656 while (1) {
3657 if (cpu_can_run(env))
3658 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003659 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003660 }
3661
3662 return NULL;
3663}
3664
3665static void tcg_cpu_exec(void);
3666
3667static void *tcg_cpu_thread_fn(void *arg)
3668{
3669 CPUState *env = arg;
3670
3671 block_io_signals();
3672 qemu_thread_self(env->thread);
3673
3674 /* signal CPU creation */
3675 qemu_mutex_lock(&qemu_global_mutex);
3676 for (env = first_cpu; env != NULL; env = env->next_cpu)
3677 env->created = 1;
3678 qemu_cond_signal(&qemu_cpu_cond);
3679
3680 /* and wait for machine initialization */
3681 while (!qemu_system_ready)
3682 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3683
3684 while (1) {
3685 tcg_cpu_exec();
3686 qemu_wait_io_event(cur_cpu);
3687 }
3688
3689 return NULL;
3690}
3691
3692void qemu_cpu_kick(void *_env)
3693{
3694 CPUState *env = _env;
3695 qemu_cond_broadcast(env->halt_cond);
3696 if (kvm_enabled())
3697 qemu_thread_signal(env->thread, SIGUSR1);
3698}
3699
Glauber Costae5bc2012009-09-28 15:27:44 -03003700int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003701{
Glauber Costae5bc2012009-09-28 15:27:44 -03003702 CPUState *env = _env;
3703 QemuThread this;
3704
3705 qemu_thread_self(&this);
3706
3707 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003708}
3709
3710static void cpu_signal(int sig)
3711{
3712 if (cpu_single_env)
3713 cpu_exit(cpu_single_env);
3714}
3715
3716static void block_io_signals(void)
3717{
3718 sigset_t set;
3719 struct sigaction sigact;
3720
3721 sigemptyset(&set);
3722 sigaddset(&set, SIGUSR2);
3723 sigaddset(&set, SIGIO);
3724 sigaddset(&set, SIGALRM);
3725 pthread_sigmask(SIG_BLOCK, &set, NULL);
3726
3727 sigemptyset(&set);
3728 sigaddset(&set, SIGUSR1);
3729 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3730
3731 memset(&sigact, 0, sizeof(sigact));
3732 sigact.sa_handler = cpu_signal;
3733 sigaction(SIGUSR1, &sigact, NULL);
3734}
3735
3736static void unblock_io_signals(void)
3737{
3738 sigset_t set;
3739
3740 sigemptyset(&set);
3741 sigaddset(&set, SIGUSR2);
3742 sigaddset(&set, SIGIO);
3743 sigaddset(&set, SIGALRM);
3744 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3745
3746 sigemptyset(&set);
3747 sigaddset(&set, SIGUSR1);
3748 pthread_sigmask(SIG_BLOCK, &set, NULL);
3749}
3750
3751static void qemu_signal_lock(unsigned int msecs)
3752{
3753 qemu_mutex_lock(&qemu_fair_mutex);
3754
3755 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3756 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3757 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3758 break;
3759 }
3760 qemu_mutex_unlock(&qemu_fair_mutex);
3761}
3762
3763static void qemu_mutex_lock_iothread(void)
3764{
3765 if (kvm_enabled()) {
3766 qemu_mutex_lock(&qemu_fair_mutex);
3767 qemu_mutex_lock(&qemu_global_mutex);
3768 qemu_mutex_unlock(&qemu_fair_mutex);
3769 } else
3770 qemu_signal_lock(100);
3771}
3772
3773static void qemu_mutex_unlock_iothread(void)
3774{
3775 qemu_mutex_unlock(&qemu_global_mutex);
3776}
3777
3778static int all_vcpus_paused(void)
3779{
3780 CPUState *penv = first_cpu;
3781
3782 while (penv) {
3783 if (!penv->stopped)
3784 return 0;
3785 penv = (CPUState *)penv->next_cpu;
3786 }
3787
3788 return 1;
3789}
3790
3791static void pause_all_vcpus(void)
3792{
3793 CPUState *penv = first_cpu;
3794
3795 while (penv) {
3796 penv->stop = 1;
3797 qemu_thread_signal(penv->thread, SIGUSR1);
3798 qemu_cpu_kick(penv);
3799 penv = (CPUState *)penv->next_cpu;
3800 }
3801
3802 while (!all_vcpus_paused()) {
3803 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3804 penv = first_cpu;
3805 while (penv) {
3806 qemu_thread_signal(penv->thread, SIGUSR1);
3807 penv = (CPUState *)penv->next_cpu;
3808 }
3809 }
3810}
3811
3812static void resume_all_vcpus(void)
3813{
3814 CPUState *penv = first_cpu;
3815
3816 while (penv) {
3817 penv->stop = 0;
3818 penv->stopped = 0;
3819 qemu_thread_signal(penv->thread, SIGUSR1);
3820 qemu_cpu_kick(penv);
3821 penv = (CPUState *)penv->next_cpu;
3822 }
3823}
3824
3825static void tcg_init_vcpu(void *_env)
3826{
3827 CPUState *env = _env;
3828 /* share a single thread for all cpus with TCG */
3829 if (!tcg_cpu_thread) {
3830 env->thread = qemu_mallocz(sizeof(QemuThread));
3831 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3832 qemu_cond_init(env->halt_cond);
3833 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3834 while (env->created == 0)
3835 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3836 tcg_cpu_thread = env->thread;
3837 tcg_halt_cond = env->halt_cond;
3838 } else {
3839 env->thread = tcg_cpu_thread;
3840 env->halt_cond = tcg_halt_cond;
3841 }
3842}
3843
3844static void kvm_start_vcpu(CPUState *env)
3845{
aliguorid6dc3d42009-04-24 18:04:07 +00003846 env->thread = qemu_mallocz(sizeof(QemuThread));
3847 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3848 qemu_cond_init(env->halt_cond);
3849 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3850 while (env->created == 0)
3851 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3852}
3853
3854void qemu_init_vcpu(void *_env)
3855{
3856 CPUState *env = _env;
3857
3858 if (kvm_enabled())
3859 kvm_start_vcpu(env);
3860 else
3861 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003862 env->nr_cores = smp_cores;
3863 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003864}
3865
3866void qemu_notify_event(void)
3867{
3868 qemu_event_increment();
3869}
3870
3871void vm_stop(int reason)
3872{
3873 QemuThread me;
3874 qemu_thread_self(&me);
3875
3876 if (!qemu_thread_equal(&me, &io_thread)) {
3877 qemu_system_vmstop_request(reason);
3878 /*
3879 * FIXME: should not return to device code in case
3880 * vm_stop() has been requested.
3881 */
3882 if (cpu_single_env) {
3883 cpu_exit(cpu_single_env);
3884 cpu_single_env->stop = 1;
3885 }
3886 return;
3887 }
3888 do_vm_stop(reason);
3889}
3890
3891#endif
3892
3893
ths877cf882007-04-18 18:11:47 +00003894#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003895static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003896{
3897 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003898 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003899
bellardf3311102006-04-12 20:21:17 +00003900
3901 /* XXX: need to suppress polling by better using win32 events */
3902 ret = 0;
3903 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3904 ret |= pe->func(pe->opaque);
3905 }
thse6b1e552007-04-18 17:56:02 +00003906 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003907 int err;
3908 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003909
aliguori56f3a5d2008-10-31 18:07:17 +00003910 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003911 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3912 if (w->func[ret - WAIT_OBJECT_0])
3913 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003914
ths5fafdf22007-09-16 21:08:06 +00003915 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003916 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003917
thse6b1e552007-04-18 17:56:02 +00003918 /* Check if event is signaled */
3919 ret2 = WaitForSingleObject(w->events[i], 0);
3920 if(ret2 == WAIT_OBJECT_0) {
3921 if (w->func[i])
3922 w->func[i](w->opaque[i]);
3923 } else if (ret2 == WAIT_TIMEOUT) {
3924 } else {
3925 err = GetLastError();
3926 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003927 }
3928 }
bellarda18e5242006-06-25 17:18:27 +00003929 } else if (ret == WAIT_TIMEOUT) {
3930 } else {
3931 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003932 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003933 }
bellardf3311102006-04-12 20:21:17 +00003934 }
aliguori56f3a5d2008-10-31 18:07:17 +00003935
3936 *timeout = 0;
3937}
3938#else
blueswir169d64512008-12-07 19:30:18 +00003939static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003940{
3941}
bellardfd1dff42006-02-01 21:29:26 +00003942#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003943
3944void main_loop_wait(int timeout)
3945{
3946 IOHandlerRecord *ioh;
3947 fd_set rfds, wfds, xfds;
3948 int ret, nfds;
3949 struct timeval tv;
3950
3951 qemu_bh_update_timeout(&timeout);
3952
3953 host_main_loop_wait(&timeout);
3954
bellardfd1dff42006-02-01 21:29:26 +00003955 /* poll any events */
3956 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003957 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003958 FD_ZERO(&rfds);
3959 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003960 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003961 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003962 if (ioh->deleted)
3963 continue;
bellardfd1dff42006-02-01 21:29:26 +00003964 if (ioh->fd_read &&
3965 (!ioh->fd_read_poll ||
3966 ioh->fd_read_poll(ioh->opaque) != 0)) {
3967 FD_SET(ioh->fd, &rfds);
3968 if (ioh->fd > nfds)
3969 nfds = ioh->fd;
3970 }
3971 if (ioh->fd_write) {
3972 FD_SET(ioh->fd, &wfds);
3973 if (ioh->fd > nfds)
3974 nfds = ioh->fd;
3975 }
3976 }
ths3b46e622007-09-17 08:09:54 +00003977
aliguori56f3a5d2008-10-31 18:07:17 +00003978 tv.tv_sec = timeout / 1000;
3979 tv.tv_usec = (timeout % 1000) * 1000;
3980
Jan Kiszkad918f232009-06-24 14:42:30 +02003981 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3982
aliguori48708522009-04-24 18:03:49 +00003983 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003984 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003985 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003986 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003987 IOHandlerRecord **pioh;
3988
3989 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003990 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003991 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003992 }
ths6ab43fd2007-08-25 01:34:19 +00003993 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003994 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003995 }
3996 }
thscafffd42007-02-28 21:59:44 +00003997
3998 /* remove deleted IO handlers */
3999 pioh = &first_io_handler;
4000 while (*pioh) {
4001 ioh = *pioh;
4002 if (ioh->deleted) {
4003 *pioh = ioh->next;
4004 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004005 } else
thscafffd42007-02-28 21:59:44 +00004006 pioh = &ioh->next;
4007 }
bellardfd1dff42006-02-01 21:29:26 +00004008 }
Jan Kiszkad918f232009-06-24 14:42:30 +02004009
4010 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004011
aliguori50317c72009-04-24 18:03:29 +00004012 /* rearm timer, if not periodic */
4013 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4014 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4015 qemu_rearm_alarm_timer(alarm_timer);
4016 }
4017
aliguori357c6922008-11-25 17:26:09 +00004018 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004019 if (vm_running) {
4020 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02004021 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
4022 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00004023 }
aliguori357c6922008-11-25 17:26:09 +00004024
4025 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02004026 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00004027 qemu_get_clock(rt_clock));
4028
Jan Kiszka21d5d122009-09-15 13:36:04 +02004029 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
4030 qemu_get_clock(host_clock));
4031
pbrook423f0742007-05-23 00:06:54 +00004032 /* Check bottom-halves last in case any of the earlier events triggered
4033 them. */
4034 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004035
bellard5905b2e2004-08-01 21:53:26 +00004036}
4037
aliguori43b96852009-04-24 18:03:33 +00004038static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004039{
aliguori43b96852009-04-24 18:03:33 +00004040 int ret;
bellard89bfc102006-02-08 22:46:31 +00004041#ifdef CONFIG_PROFILER
4042 int64_t ti;
4043#endif
aliguori43b96852009-04-24 18:03:33 +00004044
4045#ifdef CONFIG_PROFILER
4046 ti = profile_getclock();
4047#endif
4048 if (use_icount) {
4049 int64_t count;
4050 int decr;
4051 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4052 env->icount_decr.u16.low = 0;
4053 env->icount_extra = 0;
4054 count = qemu_next_deadline();
4055 count = (count + (1 << icount_time_shift) - 1)
4056 >> icount_time_shift;
4057 qemu_icount += count;
4058 decr = (count > 0xffff) ? 0xffff : count;
4059 count -= decr;
4060 env->icount_decr.u16.low = decr;
4061 env->icount_extra = count;
4062 }
4063 ret = cpu_exec(env);
4064#ifdef CONFIG_PROFILER
4065 qemu_time += profile_getclock() - ti;
4066#endif
4067 if (use_icount) {
4068 /* Fold pending instructions back into the
4069 instruction counter, and clear the interrupt flag. */
4070 qemu_icount -= (env->icount_decr.u16.low
4071 + env->icount_extra);
4072 env->icount_decr.u32 = 0;
4073 env->icount_extra = 0;
4074 }
4075 return ret;
4076}
4077
aliguorie6e35b12009-04-24 18:03:57 +00004078static void tcg_cpu_exec(void)
4079{
aliguorid6dc3d42009-04-24 18:04:07 +00004080 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004081
4082 if (next_cpu == NULL)
4083 next_cpu = first_cpu;
4084 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4085 CPUState *env = cur_cpu = next_cpu;
4086
4087 if (!vm_running)
4088 break;
4089 if (timer_alarm_pending) {
4090 timer_alarm_pending = 0;
4091 break;
4092 }
aliguorid6dc3d42009-04-24 18:04:07 +00004093 if (cpu_can_run(env))
4094 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004095 if (ret == EXCP_DEBUG) {
4096 gdb_set_stop_cpu(env);
4097 debug_requested = 1;
4098 break;
4099 }
4100 }
4101}
4102
aliguori43b96852009-04-24 18:03:33 +00004103static int cpu_has_work(CPUState *env)
4104{
aliguorid6dc3d42009-04-24 18:04:07 +00004105 if (env->stop)
4106 return 1;
4107 if (env->stopped)
4108 return 0;
aliguori43b96852009-04-24 18:03:33 +00004109 if (!env->halted)
4110 return 1;
4111 if (qemu_cpu_has_work(env))
4112 return 1;
4113 return 0;
4114}
4115
4116static int tcg_has_work(void)
4117{
bellard6a00d602005-11-21 23:25:50 +00004118 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004119
aliguori43b96852009-04-24 18:03:33 +00004120 for (env = first_cpu; env != NULL; env = env->next_cpu)
4121 if (cpu_has_work(env))
4122 return 1;
4123 return 0;
4124}
bellard15a76442005-11-23 21:01:03 +00004125
aliguori43b96852009-04-24 18:03:33 +00004126static int qemu_calculate_timeout(void)
4127{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004128#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004129 int timeout;
bellard15a76442005-11-23 21:01:03 +00004130
aliguori43b96852009-04-24 18:03:33 +00004131 if (!vm_running)
4132 timeout = 5000;
4133 else if (tcg_has_work())
4134 timeout = 0;
4135 else if (!use_icount)
4136 timeout = 5000;
4137 else {
4138 /* XXX: use timeout computed from timers */
4139 int64_t add;
4140 int64_t delta;
4141 /* Advance virtual time to the next event. */
4142 if (use_icount == 1) {
4143 /* When not using an adaptive execution frequency
4144 we tend to get badly out of sync with real time,
4145 so just delay for a reasonable amount of time. */
4146 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004147 } else {
aliguori43b96852009-04-24 18:03:33 +00004148 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004149 }
aliguori43b96852009-04-24 18:03:33 +00004150 if (delta > 0) {
4151 /* If virtual time is ahead of real time then just
4152 wait for IO. */
4153 timeout = (delta / 1000000) + 1;
4154 } else {
4155 /* Wait for either IO to occur or the next
4156 timer event. */
4157 add = qemu_next_deadline();
4158 /* We advance the timer before checking for IO.
4159 Limit the amount we advance so that early IO
4160 activity won't get the guest too far ahead. */
4161 if (add > 10000000)
4162 add = 10000000;
4163 delta += add;
4164 add = (add + (1 << icount_time_shift) - 1)
4165 >> icount_time_shift;
4166 qemu_icount += add;
4167 timeout = delta / 1000000;
4168 if (timeout < 0)
4169 timeout = 0;
4170 }
bellardb4608c02003-06-27 17:34:32 +00004171 }
aliguori43b96852009-04-24 18:03:33 +00004172
4173 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004174#else /* CONFIG_IOTHREAD */
4175 return 1000;
4176#endif
aliguori43b96852009-04-24 18:03:33 +00004177}
4178
4179static int vm_can_run(void)
4180{
4181 if (powerdown_requested)
4182 return 0;
4183 if (reset_requested)
4184 return 0;
4185 if (shutdown_requested)
4186 return 0;
aliguorie5689022009-04-24 18:03:54 +00004187 if (debug_requested)
4188 return 0;
aliguori43b96852009-04-24 18:03:33 +00004189 return 1;
4190}
4191
Blue Swirld9c32312009-08-09 08:42:19 +00004192qemu_irq qemu_system_powerdown;
4193
aliguori43b96852009-04-24 18:03:33 +00004194static void main_loop(void)
4195{
aliguori6e29f5d2009-04-24 18:04:02 +00004196 int r;
aliguori43b96852009-04-24 18:03:33 +00004197
aliguorid6dc3d42009-04-24 18:04:07 +00004198#ifdef CONFIG_IOTHREAD
4199 qemu_system_ready = 1;
4200 qemu_cond_broadcast(&qemu_system_cond);
4201#endif
4202
aliguori6e29f5d2009-04-24 18:04:02 +00004203 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004204 do {
4205#ifdef CONFIG_PROFILER
4206 int64_t ti;
4207#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004208#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004209 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004210#endif
aliguori43b96852009-04-24 18:03:33 +00004211#ifdef CONFIG_PROFILER
4212 ti = profile_getclock();
4213#endif
4214 main_loop_wait(qemu_calculate_timeout());
4215#ifdef CONFIG_PROFILER
4216 dev_time += profile_getclock() - ti;
4217#endif
aliguorie5689022009-04-24 18:03:54 +00004218 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004219
aliguorie5689022009-04-24 18:03:54 +00004220 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004221 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004222 if (qemu_shutdown_requested()) {
4223 if (no_shutdown) {
4224 vm_stop(0);
4225 no_shutdown = 0;
4226 } else
4227 break;
4228 }
aliguorid6dc3d42009-04-24 18:04:07 +00004229 if (qemu_reset_requested()) {
4230 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004231 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004232 resume_all_vcpus();
4233 }
Blue Swirld9c32312009-08-09 08:42:19 +00004234 if (qemu_powerdown_requested()) {
4235 qemu_irq_raise(qemu_system_powerdown);
4236 }
aliguori6e29f5d2009-04-24 18:04:02 +00004237 if ((r = qemu_vmstop_requested()))
4238 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004239 }
aliguorid6dc3d42009-04-24 18:04:07 +00004240 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004241}
4242
pbrook9bd7e6d2009-04-07 22:58:45 +00004243static void version(void)
4244{
pbrook4a19f1e2009-04-07 23:17:49 +00004245 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004246}
4247
ths15f82202007-06-29 23:26:08 +00004248static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004249{
pbrook9bd7e6d2009-04-07 22:58:45 +00004250 version();
4251 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004252 "\n"
bellarda20dd502003-09-30 21:07:02 +00004253 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004254 "\n"
blueswir15824d652009-03-28 06:44:27 +00004255#define DEF(option, opt_arg, opt_enum, opt_help) \
4256 opt_help
4257#define DEFHEADING(text) stringify(text) "\n"
4258#include "qemu-options.h"
4259#undef DEF
4260#undef DEFHEADING
4261#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004262 "\n"
bellard82c643f2004-07-14 17:28:13 +00004263 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004264 "ctrl-alt-f toggle full screen\n"
4265 "ctrl-alt-n switch to virtual console 'n'\n"
4266 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004267 "\n"
4268 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4269 ,
bellard0db63472003-10-27 21:37:46 +00004270 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004271 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004272#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004273 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004274 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004275#endif
bellard6e44ba72004-01-18 21:56:49 +00004276 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004277 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004278 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004279}
4280
bellardcd6f1162004-05-13 22:02:20 +00004281#define HAS_ARG 0x0001
4282
4283enum {
blueswir15824d652009-03-28 06:44:27 +00004284#define DEF(option, opt_arg, opt_enum, opt_help) \
4285 opt_enum,
4286#define DEFHEADING(text)
4287#include "qemu-options.h"
4288#undef DEF
4289#undef DEFHEADING
4290#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004291};
4292
4293typedef struct QEMUOption {
4294 const char *name;
4295 int flags;
4296 int index;
4297} QEMUOption;
4298
blueswir1dbed7e42008-10-01 19:38:09 +00004299static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004300 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004301#define DEF(option, opt_arg, opt_enum, opt_help) \
4302 { option, opt_arg, opt_enum },
4303#define DEFHEADING(text)
4304#include "qemu-options.h"
4305#undef DEF
4306#undef DEFHEADING
4307#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004308 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004309};
4310
bellard1d14ffa2005-10-30 18:58:22 +00004311#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004312struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004313#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004314#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004315 {
4316 "pcspk",
4317 "PC speaker",
4318 0,
4319 1,
4320 { .init_isa = pcspk_audio_init }
4321 },
4322#endif
malc4c9b53e2009-01-09 10:46:34 +00004323
4324#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004325 {
4326 "sb16",
4327 "Creative Sound Blaster 16",
4328 0,
4329 1,
4330 { .init_isa = SB16_init }
4331 },
malc4c9b53e2009-01-09 10:46:34 +00004332#endif
bellard6a36d842005-12-18 20:34:32 +00004333
malccc53d262008-06-13 10:48:22 +00004334#ifdef CONFIG_CS4231A
4335 {
4336 "cs4231a",
4337 "CS4231A",
4338 0,
4339 1,
4340 { .init_isa = cs4231a_init }
4341 },
4342#endif
4343
bellard6a36d842005-12-18 20:34:32 +00004344#ifdef CONFIG_ADLIB
4345 {
4346 "adlib",
4347#ifdef HAS_YMF262
4348 "Yamaha YMF262 (OPL3)",
4349#else
4350 "Yamaha YM3812 (OPL2)",
4351#endif
4352 0,
4353 1,
4354 { .init_isa = Adlib_init }
4355 },
4356#endif
4357
4358#ifdef CONFIG_GUS
4359 {
4360 "gus",
4361 "Gravis Ultrasound GF1",
4362 0,
4363 1,
4364 { .init_isa = GUS_init }
4365 },
4366#endif
4367
malc4c9b53e2009-01-09 10:46:34 +00004368#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004369 {
4370 "ac97",
4371 "Intel 82801AA AC97 Audio",
4372 0,
4373 0,
4374 { .init_pci = ac97_init }
4375 },
malc4c9b53e2009-01-09 10:46:34 +00004376#endif
balroge5c9a132008-01-14 04:27:55 +00004377
malc4c9b53e2009-01-09 10:46:34 +00004378#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004379 {
4380 "es1370",
4381 "ENSONIQ AudioPCI ES1370",
4382 0,
4383 0,
4384 { .init_pci = es1370_init }
4385 },
balrogb00052e2007-04-30 02:22:06 +00004386#endif
bellard6a36d842005-12-18 20:34:32 +00004387
malc4c9b53e2009-01-09 10:46:34 +00004388#endif /* HAS_AUDIO_CHOICE */
4389
bellard6a36d842005-12-18 20:34:32 +00004390 { NULL, NULL, 0, 0, { NULL } }
4391};
4392
bellard1d14ffa2005-10-30 18:58:22 +00004393static void select_soundhw (const char *optarg)
4394{
bellard6a36d842005-12-18 20:34:32 +00004395 struct soundhw *c;
4396
bellard1d14ffa2005-10-30 18:58:22 +00004397 if (*optarg == '?') {
4398 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004399
bellard1d14ffa2005-10-30 18:58:22 +00004400 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004401 for (c = soundhw; c->name; ++c) {
4402 printf ("%-11s %s\n", c->name, c->descr);
4403 }
4404 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004405 exit (*optarg != '?');
4406 }
4407 else {
bellard6a36d842005-12-18 20:34:32 +00004408 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004409 const char *p;
4410 char *e;
4411 int bad_card = 0;
4412
bellard6a36d842005-12-18 20:34:32 +00004413 if (!strcmp (optarg, "all")) {
4414 for (c = soundhw; c->name; ++c) {
4415 c->enabled = 1;
4416 }
4417 return;
4418 }
bellard1d14ffa2005-10-30 18:58:22 +00004419
bellard6a36d842005-12-18 20:34:32 +00004420 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004421 while (*p) {
4422 e = strchr (p, ',');
4423 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004424
4425 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004426 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004427 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004428 break;
4429 }
4430 }
bellard6a36d842005-12-18 20:34:32 +00004431
4432 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004433 if (l > 80) {
4434 fprintf (stderr,
4435 "Unknown sound card name (too big to show)\n");
4436 }
4437 else {
4438 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4439 (int) l, p);
4440 }
4441 bad_card = 1;
4442 }
4443 p += l + (e != NULL);
4444 }
4445
4446 if (bad_card)
4447 goto show_valid_cards;
4448 }
4449}
4450#endif
4451
malc3893c122008-09-28 00:42:05 +00004452static void select_vgahw (const char *p)
4453{
4454 const char *opts;
4455
Zachary Amsden86176752009-07-30 00:15:02 -10004456 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004457 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004458 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004459 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004460 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004461 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004462 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004463 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004464 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004465 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004466 invalid_vga:
4467 fprintf(stderr, "Unknown vga type: %s\n", p);
4468 exit(1);
4469 }
malccb5a7aa2008-09-28 00:42:12 +00004470 while (*opts) {
4471 const char *nextopt;
4472
4473 if (strstart(opts, ",retrace=", &nextopt)) {
4474 opts = nextopt;
4475 if (strstart(opts, "dumb", &nextopt))
4476 vga_retrace_method = VGA_RETRACE_DUMB;
4477 else if (strstart(opts, "precise", &nextopt))
4478 vga_retrace_method = VGA_RETRACE_PRECISE;
4479 else goto invalid_vga;
4480 } else goto invalid_vga;
4481 opts = nextopt;
4482 }
malc3893c122008-09-28 00:42:05 +00004483}
4484
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004485#ifdef TARGET_I386
4486static int balloon_parse(const char *arg)
4487{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004488 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004489
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004490 if (strcmp(arg, "none") == 0) {
4491 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004492 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004493
4494 if (!strncmp(arg, "virtio", 6)) {
4495 if (arg[6] == ',') {
4496 /* have params -> parse them */
4497 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4498 if (!opts)
4499 return -1;
4500 } else {
4501 /* create empty opts */
4502 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4503 }
4504 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4505 return 0;
4506 }
4507
4508 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004509}
4510#endif
4511
bellard3587d7e2006-06-26 20:03:44 +00004512#ifdef _WIN32
4513static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4514{
4515 exit(STATUS_CONTROL_C_EXIT);
4516 return TRUE;
4517}
4518#endif
4519
aliguoric4be29f2009-04-17 18:58:14 +00004520int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004521{
4522 int ret;
4523
4524 if(strlen(str) != 36)
4525 return -1;
4526
4527 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4528 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4529 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4530
4531 if(ret != 16)
4532 return -1;
4533
aliguorib6f6e3d2009-04-17 18:59:56 +00004534#ifdef TARGET_I386
4535 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4536#endif
4537
blueswir18fcb1b92008-09-18 18:29:08 +00004538 return 0;
4539}
4540
aliguori5b08fc12008-08-21 20:08:03 +00004541#ifndef _WIN32
4542
4543static void termsig_handler(int signal)
4544{
4545 qemu_system_shutdown_request();
4546}
4547
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004548static void sigchld_handler(int signal)
4549{
4550 waitpid(-1, NULL, WNOHANG);
4551}
4552
4553static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004554{
4555 struct sigaction act;
4556
4557 memset(&act, 0, sizeof(act));
4558 act.sa_handler = termsig_handler;
4559 sigaction(SIGINT, &act, NULL);
4560 sigaction(SIGHUP, &act, NULL);
4561 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004562
4563 act.sa_handler = sigchld_handler;
4564 act.sa_flags = SA_NOCLDSTOP;
4565 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004566}
4567
4568#endif
4569
Paul Brook5cea8592009-05-30 00:52:44 +01004570#ifdef _WIN32
4571/* Look for support files in the same directory as the executable. */
4572static char *find_datadir(const char *argv0)
4573{
4574 char *p;
4575 char buf[MAX_PATH];
4576 DWORD len;
4577
4578 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4579 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004580 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004581 }
4582
4583 buf[len] = 0;
4584 p = buf + len - 1;
4585 while (p != buf && *p != '\\')
4586 p--;
4587 *p = 0;
4588 if (access(buf, R_OK) == 0) {
4589 return qemu_strdup(buf);
4590 }
4591 return NULL;
4592}
4593#else /* !_WIN32 */
4594
4595/* Find a likely location for support files using the location of the binary.
4596 For installed binaries this will be "$bindir/../share/qemu". When
4597 running from the build tree this will be "$bindir/../pc-bios". */
4598#define SHARE_SUFFIX "/share/qemu"
4599#define BUILD_SUFFIX "/pc-bios"
4600static char *find_datadir(const char *argv0)
4601{
4602 char *dir;
4603 char *p = NULL;
4604 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004605 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004606 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004607
4608#if defined(__linux__)
4609 {
4610 int len;
4611 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4612 if (len > 0) {
4613 buf[len] = 0;
4614 p = buf;
4615 }
4616 }
4617#elif defined(__FreeBSD__)
4618 {
4619 int len;
4620 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4621 if (len > 0) {
4622 buf[len] = 0;
4623 p = buf;
4624 }
4625 }
4626#endif
4627 /* If we don't have any way of figuring out the actual executable
4628 location then try argv[0]. */
4629 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004630 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004631 if (!p) {
4632 return NULL;
4633 }
4634 }
4635 dir = dirname(p);
4636 dir = dirname(dir);
4637
Blue Swirl3a417592009-06-09 19:12:21 +00004638 max_len = strlen(dir) +
4639 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4640 res = qemu_mallocz(max_len);
4641 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004642 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004643 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004644 if (access(res, R_OK)) {
4645 qemu_free(res);
4646 res = NULL;
4647 }
4648 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004649
Paul Brook5cea8592009-05-30 00:52:44 +01004650 return res;
4651}
4652#undef SHARE_SUFFIX
4653#undef BUILD_SUFFIX
4654#endif
4655
4656char *qemu_find_file(int type, const char *name)
4657{
4658 int len;
4659 const char *subdir;
4660 char *buf;
4661
4662 /* If name contains path separators then try it as a straight path. */
4663 if ((strchr(name, '/') || strchr(name, '\\'))
4664 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004665 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004666 }
4667 switch (type) {
4668 case QEMU_FILE_TYPE_BIOS:
4669 subdir = "";
4670 break;
4671 case QEMU_FILE_TYPE_KEYMAP:
4672 subdir = "keymaps/";
4673 break;
4674 default:
4675 abort();
4676 }
4677 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4678 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004679 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004680 if (access(buf, R_OK)) {
4681 qemu_free(buf);
4682 return NULL;
4683 }
4684 return buf;
4685}
4686
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004687static int device_init_func(QemuOpts *opts, void *opaque)
4688{
4689 DeviceState *dev;
4690
4691 dev = qdev_device_add(opts);
4692 if (!dev)
4693 return -1;
4694 return 0;
4695}
4696
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004697struct device_config {
4698 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004699 DEV_USB, /* -usbdevice */
4700 DEV_BT, /* -bt */
4701 } type;
4702 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004703 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004704};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004705QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004706
4707static void add_device_config(int type, const char *cmdline)
4708{
4709 struct device_config *conf;
4710
4711 conf = qemu_mallocz(sizeof(*conf));
4712 conf->type = type;
4713 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004714 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004715}
4716
4717static int foreach_device_config(int type, int (*func)(const char *cmdline))
4718{
4719 struct device_config *conf;
4720 int rc;
4721
Blue Swirl72cf2d42009-09-12 07:36:22 +00004722 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004723 if (conf->type != type)
4724 continue;
4725 rc = func(conf->cmdline);
4726 if (0 != rc)
4727 return rc;
4728 }
4729 return 0;
4730}
4731
malc902b3d52008-12-10 19:18:40 +00004732int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004733{
aliguori59030a82009-04-05 18:43:41 +00004734 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004735 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004736 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004737 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004738 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004739 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004740 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004741 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004742 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004743 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004744 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004745 int optind;
4746 const char *r, *optarg;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004747 CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
4748 const char *monitor_devices[MAX_MONITOR_DEVICES];
4749 int monitor_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004750 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004751 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004752 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004753 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004754 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4755 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004756 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004757 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004758 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004759#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004760 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004761#endif
bellard26a5f132008-05-28 12:30:31 +00004762 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004763 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004764 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004765#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004766 int fd = 0;
4767 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004768 const char *chroot_dir = NULL;
4769 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004770#endif
aliguori268a3622009-04-21 22:30:27 +00004771 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004772 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004773
Jan Kiszka68752042009-09-15 13:36:04 +02004774 init_clocks();
4775
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004776 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004777 qemu_cache_utils_init(envp);
4778
Blue Swirl72cf2d42009-09-12 07:36:22 +00004779 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004780#ifndef _WIN32
4781 {
4782 struct sigaction act;
4783 sigfillset(&act.sa_mask);
4784 act.sa_flags = 0;
4785 act.sa_handler = SIG_IGN;
4786 sigaction(SIGPIPE, &act, NULL);
4787 }
bellard3587d7e2006-06-26 20:03:44 +00004788#else
4789 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004790 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4791 QEMU to run on a single CPU */
4792 {
4793 HANDLE h;
4794 DWORD mask, smask;
4795 int i;
4796 h = GetCurrentProcess();
4797 if (GetProcessAffinityMask(h, &mask, &smask)) {
4798 for(i = 0; i < 32; i++) {
4799 if (mask & (1 << i))
4800 break;
4801 }
4802 if (i != 32) {
4803 mask = 1 << i;
4804 SetProcessAffinityMask(h, mask);
4805 }
4806 }
4807 }
bellard67b915a2004-03-31 23:37:16 +00004808#endif
bellardbe995c22006-06-25 16:25:21 +00004809
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004810 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004811 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004812 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004813 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004814 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004815 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004816 kernel_filename = NULL;
4817 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004818 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004819 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004820
aurel32c75a8232008-05-04 00:50:34 +00004821 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004822 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004823 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004824 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004825
aliguori8290edd2009-02-27 20:14:29 +00004826 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004827 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004828 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004829 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004830
aliguori1b8fc812009-02-27 20:01:39 +00004831 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004832 virtio_consoles[i] = NULL;
4833 virtio_console_index = 0;
4834
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004835 monitor_devices[0] = "vc:80Cx24C";
4836 for (i = 1; i < MAX_MONITOR_DEVICES; i++) {
4837 monitor_devices[i] = NULL;
4838 }
4839 monitor_device_index = 0;
4840
aliguori268a3622009-04-21 22:30:27 +00004841 for (i = 0; i < MAX_NODES; i++) {
4842 node_mem[i] = 0;
4843 node_cpumask[i] = 0;
4844 }
4845
aliguori268a3622009-04-21 22:30:27 +00004846 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004847 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004848
bellard26a5f132008-05-28 12:30:31 +00004849 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004850 autostart= 1;
4851
bellardcd6f1162004-05-13 22:02:20 +00004852 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004853 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004854 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004855 break;
bellardcd6f1162004-05-13 22:02:20 +00004856 r = argv[optind];
4857 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004858 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004859 } else {
4860 const QEMUOption *popt;
4861
4862 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004863 /* Treat --foo the same as -foo. */
4864 if (r[1] == '-')
4865 r++;
bellardcd6f1162004-05-13 22:02:20 +00004866 popt = qemu_options;
4867 for(;;) {
4868 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004869 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004870 argv[0], r);
4871 exit(1);
4872 }
4873 if (!strcmp(popt->name, r + 1))
4874 break;
4875 popt++;
4876 }
4877 if (popt->flags & HAS_ARG) {
4878 if (optind >= argc) {
4879 fprintf(stderr, "%s: option '%s' requires an argument\n",
4880 argv[0], r);
4881 exit(1);
4882 }
4883 optarg = argv[optind++];
4884 } else {
4885 optarg = NULL;
4886 }
4887
4888 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004889 case QEMU_OPTION_M:
4890 machine = find_machine(optarg);
4891 if (!machine) {
4892 QEMUMachine *m;
4893 printf("Supported machines are:\n");
4894 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004895 if (m->alias)
4896 printf("%-10s %s (alias of %s)\n",
4897 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004898 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004899 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004900 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004901 }
ths15f82202007-06-29 23:26:08 +00004902 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004903 }
4904 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004905 case QEMU_OPTION_cpu:
4906 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004907 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004908/* XXX: implement xxx_cpu_list for targets that still miss it */
4909#if defined(cpu_list)
4910 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004911#endif
ths15f82202007-06-29 23:26:08 +00004912 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004913 } else {
4914 cpu_model = optarg;
4915 }
4916 break;
bellardcd6f1162004-05-13 22:02:20 +00004917 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004918 initrd_filename = optarg;
4919 break;
bellardcd6f1162004-05-13 22:02:20 +00004920 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004921 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004922 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004923 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004924 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004925 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004926 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004927 translation == BIOS_ATA_TRANSLATION_LBA ?
4928 ",trans=lba" :
4929 translation == BIOS_ATA_TRANSLATION_NONE ?
4930 ",trans=none" : "");
4931 break;
bellardcd6f1162004-05-13 22:02:20 +00004932 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004933 case QEMU_OPTION_hdc:
4934 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004935 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004936 break;
thse4bcb142007-12-02 04:51:10 +00004937 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004938 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004939 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004940 case QEMU_OPTION_set:
4941 if (qemu_set_option(optarg) != 0)
4942 exit(1);
4943 break;
balrog3e3d5812007-04-30 02:09:25 +00004944 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004945 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004946 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004947 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004948 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004949 break;
j_mayer86f55662007-04-24 06:52:59 +00004950 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004951 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004952 break;
bellardcd6f1162004-05-13 22:02:20 +00004953 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004954 snapshot = 1;
4955 break;
bellardcd6f1162004-05-13 22:02:20 +00004956 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004957 {
bellard330d0412003-07-26 18:11:40 +00004958 const char *p;
4959 p = optarg;
4960 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004961 if (cyls < 1 || cyls > 16383)
4962 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004963 if (*p != ',')
4964 goto chs_fail;
4965 p++;
4966 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004967 if (heads < 1 || heads > 16)
4968 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004969 if (*p != ',')
4970 goto chs_fail;
4971 p++;
4972 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004973 if (secs < 1 || secs > 63)
4974 goto chs_fail;
4975 if (*p == ',') {
4976 p++;
4977 if (!strcmp(p, "none"))
4978 translation = BIOS_ATA_TRANSLATION_NONE;
4979 else if (!strcmp(p, "lba"))
4980 translation = BIOS_ATA_TRANSLATION_LBA;
4981 else if (!strcmp(p, "auto"))
4982 translation = BIOS_ATA_TRANSLATION_AUTO;
4983 else
4984 goto chs_fail;
4985 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004986 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004987 fprintf(stderr, "qemu: invalid physical CHS format\n");
4988 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004989 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004990 if (hda_opts != NULL) {
4991 char num[16];
4992 snprintf(num, sizeof(num), "%d", cyls);
4993 qemu_opt_set(hda_opts, "cyls", num);
4994 snprintf(num, sizeof(num), "%d", heads);
4995 qemu_opt_set(hda_opts, "heads", num);
4996 snprintf(num, sizeof(num), "%d", secs);
4997 qemu_opt_set(hda_opts, "secs", num);
4998 if (translation == BIOS_ATA_TRANSLATION_LBA)
4999 qemu_opt_set(hda_opts, "trans", "lba");
5000 if (translation == BIOS_ATA_TRANSLATION_NONE)
5001 qemu_opt_set(hda_opts, "trans", "none");
5002 }
bellard330d0412003-07-26 18:11:40 +00005003 }
5004 break;
aliguori268a3622009-04-21 22:30:27 +00005005 case QEMU_OPTION_numa:
5006 if (nb_numa_nodes >= MAX_NODES) {
5007 fprintf(stderr, "qemu: too many NUMA nodes\n");
5008 exit(1);
5009 }
5010 numa_add(optarg);
5011 break;
bellardcd6f1162004-05-13 22:02:20 +00005012 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005013 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005014 break;
balrog4d3b6f62008-02-10 16:33:14 +00005015#ifdef CONFIG_CURSES
5016 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005017 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005018 break;
5019#endif
balroga171fe32007-04-30 01:48:07 +00005020 case QEMU_OPTION_portrait:
5021 graphic_rotate = 1;
5022 break;
bellardcd6f1162004-05-13 22:02:20 +00005023 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005024 kernel_filename = optarg;
5025 break;
bellardcd6f1162004-05-13 22:02:20 +00005026 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005027 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005028 break;
bellardcd6f1162004-05-13 22:02:20 +00005029 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005030 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005031 break;
bellardcd6f1162004-05-13 22:02:20 +00005032 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005033 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005034 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005035 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005036 };
5037 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005038 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005039 int legacy = 0;
5040
5041 if (!strchr(optarg, '=')) {
5042 legacy = 1;
5043 pstrcpy(buf, sizeof(buf), optarg);
5044 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5045 fprintf(stderr,
5046 "qemu: unknown boot parameter '%s' in '%s'\n",
5047 buf, optarg);
5048 exit(1);
5049 }
5050
5051 if (legacy ||
5052 get_param_value(buf, sizeof(buf), "order", optarg)) {
5053 boot_devices_bitmap = parse_bootdevices(buf);
5054 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005055 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005056 if (!legacy) {
5057 if (get_param_value(buf, sizeof(buf),
5058 "once", optarg)) {
5059 boot_devices_bitmap |= parse_bootdevices(buf);
5060 standard_boot_devices = qemu_strdup(boot_devices);
5061 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5062 qemu_register_reset(restore_boot_devices,
5063 standard_boot_devices);
5064 }
Jan Kiszka95387492009-07-02 00:19:02 +02005065 if (get_param_value(buf, sizeof(buf),
5066 "menu", optarg)) {
5067 if (!strcmp(buf, "on")) {
5068 boot_menu = 1;
5069 } else if (!strcmp(buf, "off")) {
5070 boot_menu = 0;
5071 } else {
5072 fprintf(stderr,
5073 "qemu: invalid option value '%s'\n",
5074 buf);
5075 exit(1);
5076 }
5077 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005078 }
bellard36b486b2003-11-11 13:36:08 +00005079 }
5080 break;
bellardcd6f1162004-05-13 22:02:20 +00005081 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005082 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005083 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005084 break;
bellard52ca8d62006-06-14 16:03:05 +00005085#ifdef TARGET_I386
5086 case QEMU_OPTION_no_fd_bootchk:
5087 fd_bootchk = 0;
5088 break;
5089#endif
bellard7c9d8e02005-11-15 22:16:05 +00005090 case QEMU_OPTION_net:
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005091 if (net_client_parse(optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005092 exit(1);
5093 }
bellard702c6512004-04-02 21:21:32 +00005094 break;
bellardc7f74642004-08-24 21:57:12 +00005095#ifdef CONFIG_SLIRP
5096 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005097 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005098 break;
ths47d5d012007-02-20 00:05:08 +00005099 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005100 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005101 break;
bellardc94c8d62004-09-13 21:37:34 +00005102#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005103 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005104 if (net_slirp_smb(optarg) < 0)
5105 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005106 break;
bellardc94c8d62004-09-13 21:37:34 +00005107#endif
bellard9bf05442004-08-25 22:12:49 +00005108 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005109 if (net_slirp_redir(optarg) < 0)
5110 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005111 break;
bellardc7f74642004-08-24 21:57:12 +00005112#endif
balrogdc72ac12008-11-09 00:04:26 +00005113 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005114 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005115 break;
bellard1d14ffa2005-10-30 18:58:22 +00005116#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005117 case QEMU_OPTION_audio_help:
5118 AUD_help ();
5119 exit (0);
5120 break;
5121 case QEMU_OPTION_soundhw:
5122 select_soundhw (optarg);
5123 break;
5124#endif
bellardcd6f1162004-05-13 22:02:20 +00005125 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005126 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005127 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005128 case QEMU_OPTION_version:
5129 version();
5130 exit(0);
5131 break;
aurel3200f82b82008-04-27 21:12:55 +00005132 case QEMU_OPTION_m: {
5133 uint64_t value;
5134 char *ptr;
5135
5136 value = strtoul(optarg, &ptr, 10);
5137 switch (*ptr) {
5138 case 0: case 'M': case 'm':
5139 value <<= 20;
5140 break;
5141 case 'G': case 'g':
5142 value <<= 30;
5143 break;
5144 default:
5145 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005146 exit(1);
5147 }
aurel3200f82b82008-04-27 21:12:55 +00005148
5149 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005150 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005151 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5152 exit(1);
5153 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005154 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005155 fprintf(stderr, "qemu: ram size too large\n");
5156 exit(1);
5157 }
5158 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005159 break;
aurel3200f82b82008-04-27 21:12:55 +00005160 }
bellardcd6f1162004-05-13 22:02:20 +00005161 case QEMU_OPTION_d:
5162 {
5163 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005164 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005165
bellardcd6f1162004-05-13 22:02:20 +00005166 mask = cpu_str_to_log_mask(optarg);
5167 if (!mask) {
5168 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005169 for(item = cpu_log_items; item->mask != 0; item++) {
5170 printf("%-10s %s\n", item->name, item->help);
5171 }
5172 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005173 }
5174 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005175 }
bellardcd6f1162004-05-13 22:02:20 +00005176 break;
bellardcd6f1162004-05-13 22:02:20 +00005177 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005178 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005179 break;
aliguori59030a82009-04-05 18:43:41 +00005180 case QEMU_OPTION_gdb:
5181 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005182 break;
bellardcd6f1162004-05-13 22:02:20 +00005183 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005184 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005185 break;
j_mayer1192dad2007-10-05 13:08:35 +00005186 case QEMU_OPTION_bios:
5187 bios_name = optarg;
5188 break;
aurel321b530a62009-04-05 20:08:59 +00005189 case QEMU_OPTION_singlestep:
5190 singlestep = 1;
5191 break;
bellardcd6f1162004-05-13 22:02:20 +00005192 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005193 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005194 break;
blueswir15824d652009-03-28 06:44:27 +00005195#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005196 case QEMU_OPTION_k:
5197 keyboard_layout = optarg;
5198 break;
blueswir15824d652009-03-28 06:44:27 +00005199#endif
bellardee22c2f2004-06-03 12:49:50 +00005200 case QEMU_OPTION_localtime:
5201 rtc_utc = 0;
5202 break;
malc3893c122008-09-28 00:42:05 +00005203 case QEMU_OPTION_vga:
5204 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005205 break;
blueswir15824d652009-03-28 06:44:27 +00005206#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005207 case QEMU_OPTION_g:
5208 {
5209 const char *p;
5210 int w, h, depth;
5211 p = optarg;
5212 w = strtol(p, (char **)&p, 10);
5213 if (w <= 0) {
5214 graphic_error:
5215 fprintf(stderr, "qemu: invalid resolution or depth\n");
5216 exit(1);
5217 }
5218 if (*p != 'x')
5219 goto graphic_error;
5220 p++;
5221 h = strtol(p, (char **)&p, 10);
5222 if (h <= 0)
5223 goto graphic_error;
5224 if (*p == 'x') {
5225 p++;
5226 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005227 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005228 depth != 24 && depth != 32)
5229 goto graphic_error;
5230 } else if (*p == '\0') {
5231 depth = graphic_depth;
5232 } else {
5233 goto graphic_error;
5234 }
ths3b46e622007-09-17 08:09:54 +00005235
bellarde9b137c2004-06-21 16:46:10 +00005236 graphic_width = w;
5237 graphic_height = h;
5238 graphic_depth = depth;
5239 }
5240 break;
blueswir15824d652009-03-28 06:44:27 +00005241#endif
ths20d8a3e2007-02-18 17:04:49 +00005242 case QEMU_OPTION_echr:
5243 {
5244 char *r;
5245 term_escape_char = strtol(optarg, &r, 0);
5246 if (r == optarg)
5247 printf("Bad argument to echr\n");
5248 break;
5249 }
bellard82c643f2004-07-14 17:28:13 +00005250 case QEMU_OPTION_monitor:
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005251 if (monitor_device_index >= MAX_MONITOR_DEVICES) {
5252 fprintf(stderr, "qemu: too many monitor devices\n");
5253 exit(1);
5254 }
5255 monitor_devices[monitor_device_index] = optarg;
5256 monitor_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005257 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005258 case QEMU_OPTION_chardev:
5259 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5260 if (!opts) {
5261 fprintf(stderr, "parse error: %s\n", optarg);
5262 exit(1);
5263 }
Mark McLoughlin3df04ac2009-09-23 11:24:05 +01005264 if (qemu_chr_open_opts(opts, NULL) == NULL) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005265 exit(1);
5266 }
5267 break;
bellard82c643f2004-07-14 17:28:13 +00005268 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005269 if (serial_device_index >= MAX_SERIAL_PORTS) {
5270 fprintf(stderr, "qemu: too many serial ports\n");
5271 exit(1);
5272 }
pbrookfd5f3932008-03-26 20:55:43 +00005273 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005274 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005275 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005276 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005277 if (watchdog) {
5278 fprintf(stderr,
5279 "qemu: only one watchdog option may be given\n");
5280 return 1;
5281 }
5282 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005283 break;
5284 case QEMU_OPTION_watchdog_action:
5285 if (select_watchdog_action(optarg) == -1) {
5286 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5287 exit(1);
5288 }
5289 break;
aliguori51ecf132009-01-15 20:06:40 +00005290 case QEMU_OPTION_virtiocon:
5291 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5292 fprintf(stderr, "qemu: too many virtio consoles\n");
5293 exit(1);
5294 }
5295 virtio_consoles[virtio_console_index] = optarg;
5296 virtio_console_index++;
5297 break;
bellard6508fe52005-01-15 12:02:56 +00005298 case QEMU_OPTION_parallel:
5299 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5300 fprintf(stderr, "qemu: too many parallel ports\n");
5301 exit(1);
5302 }
pbrookfd5f3932008-03-26 20:55:43 +00005303 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005304 parallel_device_index++;
5305 break;
bellardd63d3072004-10-03 13:29:03 +00005306 case QEMU_OPTION_loadvm:
5307 loadvm = optarg;
5308 break;
5309 case QEMU_OPTION_full_screen:
5310 full_screen = 1;
5311 break;
ths667acca2006-12-11 02:08:05 +00005312#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005313 case QEMU_OPTION_no_frame:
5314 no_frame = 1;
5315 break;
ths3780e192007-06-21 21:08:02 +00005316 case QEMU_OPTION_alt_grab:
5317 alt_grab = 1;
5318 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005319 case QEMU_OPTION_ctrl_grab:
5320 ctrl_grab = 1;
5321 break;
ths667acca2006-12-11 02:08:05 +00005322 case QEMU_OPTION_no_quit:
5323 no_quit = 1;
5324 break;
aliguori7d957bd2009-01-15 22:14:11 +00005325 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005326 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005327 break;
ths667acca2006-12-11 02:08:05 +00005328#endif
bellardf7cce892004-12-08 22:21:25 +00005329 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005330 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005331 break;
bellarda09db212005-04-30 16:10:35 +00005332#ifdef TARGET_I386
5333 case QEMU_OPTION_win2k_hack:
5334 win2k_install_hack = 1;
5335 break;
aliguori73822ec2009-01-15 20:11:34 +00005336 case QEMU_OPTION_rtc_td_hack:
5337 rtc_td_hack = 1;
5338 break;
aliguori8a92ea22009-02-27 20:12:36 +00005339 case QEMU_OPTION_acpitable:
5340 if(acpi_table_add(optarg) < 0) {
5341 fprintf(stderr, "Wrong acpi table provided\n");
5342 exit(1);
5343 }
5344 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005345 case QEMU_OPTION_smbios:
5346 if(smbios_entry_add(optarg) < 0) {
5347 fprintf(stderr, "Wrong smbios provided\n");
5348 exit(1);
5349 }
5350 break;
bellarda09db212005-04-30 16:10:35 +00005351#endif
aliguori7ba1e612008-11-05 16:04:33 +00005352#ifdef CONFIG_KVM
5353 case QEMU_OPTION_enable_kvm:
5354 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005355 break;
5356#endif
bellardbb36d472005-11-05 14:22:28 +00005357 case QEMU_OPTION_usb:
5358 usb_enabled = 1;
5359 break;
bellarda594cfb2005-11-06 16:13:29 +00005360 case QEMU_OPTION_usbdevice:
5361 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005362 add_device_config(DEV_USB, optarg);
5363 break;
5364 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005365 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005366 exit(1);
5367 }
bellarda594cfb2005-11-06 16:13:29 +00005368 break;
bellard6a00d602005-11-21 23:25:50 +00005369 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005370 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005371 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005372 fprintf(stderr, "Invalid number of CPUs\n");
5373 exit(1);
5374 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005375 if (max_cpus < smp_cpus) {
5376 fprintf(stderr, "maxcpus must be equal to or greater than "
5377 "smp\n");
5378 exit(1);
5379 }
5380 if (max_cpus > 255) {
5381 fprintf(stderr, "Unsupported number of maxcpus\n");
5382 exit(1);
5383 }
bellard6a00d602005-11-21 23:25:50 +00005384 break;
bellard24236862006-04-30 21:28:36 +00005385 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005386 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005387 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005388 break;
blueswir15824d652009-03-28 06:44:27 +00005389#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005390 case QEMU_OPTION_no_acpi:
5391 acpi_enabled = 0;
5392 break;
aliguori16b29ae2008-12-17 23:28:44 +00005393 case QEMU_OPTION_no_hpet:
5394 no_hpet = 1;
5395 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005396 case QEMU_OPTION_balloon:
5397 if (balloon_parse(optarg) < 0) {
5398 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5399 exit(1);
5400 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005401 break;
blueswir15824d652009-03-28 06:44:27 +00005402#endif
bellardd1beab82006-10-02 19:44:22 +00005403 case QEMU_OPTION_no_reboot:
5404 no_reboot = 1;
5405 break;
aurel32b2f76162008-04-11 21:35:52 +00005406 case QEMU_OPTION_no_shutdown:
5407 no_shutdown = 1;
5408 break;
balrog9467cd42007-05-01 01:34:14 +00005409 case QEMU_OPTION_show_cursor:
5410 cursor_hide = 0;
5411 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005412 case QEMU_OPTION_uuid:
5413 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5414 fprintf(stderr, "Fail to parse UUID string."
5415 " Wrong format.\n");
5416 exit(1);
5417 }
5418 break;
blueswir15824d652009-03-28 06:44:27 +00005419#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005420 case QEMU_OPTION_daemonize:
5421 daemonize = 1;
5422 break;
blueswir15824d652009-03-28 06:44:27 +00005423#endif
ths9ae02552007-01-05 17:39:04 +00005424 case QEMU_OPTION_option_rom:
5425 if (nb_option_roms >= MAX_OPTION_ROMS) {
5426 fprintf(stderr, "Too many option ROMs\n");
5427 exit(1);
5428 }
5429 option_rom[nb_option_roms] = optarg;
5430 nb_option_roms++;
5431 break;
blueswir15824d652009-03-28 06:44:27 +00005432#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005433 case QEMU_OPTION_semihosting:
5434 semihosting_enabled = 1;
5435 break;
blueswir15824d652009-03-28 06:44:27 +00005436#endif
thsc35734b2007-03-19 15:17:08 +00005437 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005438 qemu_name = qemu_strdup(optarg);
5439 {
5440 char *p = strchr(qemu_name, ',');
5441 if (p != NULL) {
5442 *p++ = 0;
5443 if (strncmp(p, "process=", 8)) {
5444 fprintf(stderr, "Unknown subargument %s to -name", p);
5445 exit(1);
5446 }
5447 p += 8;
5448 set_proc_name(p);
5449 }
5450 }
thsc35734b2007-03-19 15:17:08 +00005451 break;
blueswir195efd112008-12-24 20:26:14 +00005452#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005453 case QEMU_OPTION_prom_env:
5454 if (nb_prom_envs >= MAX_PROM_ENVS) {
5455 fprintf(stderr, "Too many prom variables\n");
5456 exit(1);
5457 }
5458 prom_envs[nb_prom_envs] = optarg;
5459 nb_prom_envs++;
5460 break;
5461#endif
balrog2b8f2d42007-07-27 22:08:46 +00005462#ifdef TARGET_ARM
5463 case QEMU_OPTION_old_param:
5464 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005465 break;
balrog2b8f2d42007-07-27 22:08:46 +00005466#endif
thsf3dcfad2007-08-24 01:26:02 +00005467 case QEMU_OPTION_clock:
5468 configure_alarms(optarg);
5469 break;
bellard7e0af5d02007-11-07 16:24:33 +00005470 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005471 configure_rtc_date_offset(optarg, 1);
5472 break;
5473 case QEMU_OPTION_rtc:
5474 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5475 if (!opts) {
5476 fprintf(stderr, "parse error: %s\n", optarg);
5477 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005478 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005479 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005480 break;
bellard26a5f132008-05-28 12:30:31 +00005481 case QEMU_OPTION_tb_size:
5482 tb_size = strtol(optarg, NULL, 0);
5483 if (tb_size < 0)
5484 tb_size = 0;
5485 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005486 case QEMU_OPTION_icount:
5487 use_icount = 1;
5488 if (strcmp(optarg, "auto") == 0) {
5489 icount_time_shift = -1;
5490 } else {
5491 icount_time_shift = strtol(optarg, NULL, 0);
5492 }
5493 break;
aliguori5bb79102008-10-13 03:12:02 +00005494 case QEMU_OPTION_incoming:
5495 incoming = optarg;
5496 break;
blueswir15824d652009-03-28 06:44:27 +00005497#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005498 case QEMU_OPTION_chroot:
5499 chroot_dir = optarg;
5500 break;
5501 case QEMU_OPTION_runas:
5502 run_as = optarg;
5503 break;
blueswir15824d652009-03-28 06:44:27 +00005504#endif
aliguorie37630c2009-04-22 15:19:10 +00005505#ifdef CONFIG_XEN
5506 case QEMU_OPTION_xen_domid:
5507 xen_domid = atoi(optarg);
5508 break;
5509 case QEMU_OPTION_xen_create:
5510 xen_mode = XEN_CREATE;
5511 break;
5512 case QEMU_OPTION_xen_attach:
5513 xen_mode = XEN_ATTACH;
5514 break;
5515#endif
bellardcd6f1162004-05-13 22:02:20 +00005516 }
bellard0824d6f2003-06-24 13:42:40 +00005517 }
5518 }
bellard330d0412003-07-26 18:11:40 +00005519
Paul Brook5cea8592009-05-30 00:52:44 +01005520 /* If no data_dir is specified then try to find it relative to the
5521 executable path. */
5522 if (!data_dir) {
5523 data_dir = find_datadir(argv[0]);
5524 }
5525 /* If all else fails use the install patch specified when building. */
5526 if (!data_dir) {
5527 data_dir = CONFIG_QEMU_SHAREDIR;
5528 }
5529
Jes Sorensen6be68d72009-07-23 17:03:42 +02005530 /*
5531 * Default to max_cpus = smp_cpus, in case the user doesn't
5532 * specify a max_cpus value.
5533 */
5534 if (!max_cpus)
5535 max_cpus = smp_cpus;
5536
balrog3d878ca2008-10-28 10:59:59 +00005537 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005538 if (smp_cpus > machine->max_cpus) {
5539 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5540 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5541 machine->max_cpus);
5542 exit(1);
5543 }
5544
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005545 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005546 if (serial_device_index == 0)
5547 serial_devices[0] = "stdio";
5548 if (parallel_device_index == 0)
5549 parallel_devices[0] = "null";
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005550 if (strncmp(monitor_devices[0], "vc", 2) == 0) {
5551 monitor_devices[0] = "stdio";
5552 }
aliguoribc0129d2008-08-01 15:12:34 +00005553 }
5554
ths71e3ceb2006-12-22 02:11:31 +00005555#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005556 if (daemonize) {
5557 pid_t pid;
5558
5559 if (pipe(fds) == -1)
5560 exit(1);
5561
5562 pid = fork();
5563 if (pid > 0) {
5564 uint8_t status;
5565 ssize_t len;
5566
5567 close(fds[1]);
5568
5569 again:
ths93815bc2007-03-19 15:58:31 +00005570 len = read(fds[0], &status, 1);
5571 if (len == -1 && (errno == EINTR))
5572 goto again;
5573
5574 if (len != 1)
5575 exit(1);
5576 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005577 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005578 exit(1);
5579 } else
5580 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005581 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005582 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005583
5584 setsid();
5585
5586 pid = fork();
5587 if (pid > 0)
5588 exit(0);
5589 else if (pid < 0)
5590 exit(1);
5591
5592 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005593
5594 signal(SIGTSTP, SIG_IGN);
5595 signal(SIGTTOU, SIG_IGN);
5596 signal(SIGTTIN, SIG_IGN);
5597 }
ths71e3ceb2006-12-22 02:11:31 +00005598
thsaa26bb22007-03-25 21:33:06 +00005599 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005600 if (daemonize) {
5601 uint8_t status = 1;
5602 write(fds[1], &status, 1);
5603 } else
Justin M. Forbes850810d2009-10-01 09:42:56 -05005604 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005605 exit(1);
5606 }
blueswir1b9e82a52009-04-05 18:03:31 +00005607#endif
ths93815bc2007-03-19 15:58:31 +00005608
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005609 if (kvm_enabled()) {
5610 int ret;
5611
5612 ret = kvm_init(smp_cpus);
5613 if (ret < 0) {
5614 fprintf(stderr, "failed to initialize KVM\n");
5615 exit(1);
5616 }
5617 }
5618
aliguori3fcf7b62009-04-24 18:03:25 +00005619 if (qemu_init_main_loop()) {
5620 fprintf(stderr, "qemu_init_main_loop failed\n");
5621 exit(1);
5622 }
bellarda20dd502003-09-30 21:07:02 +00005623 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005624
thsf8d39c02008-07-03 10:01:15 +00005625 if (!linux_boot && *kernel_cmdline != '\0') {
5626 fprintf(stderr, "-append only allowed with -kernel option\n");
5627 exit(1);
5628 }
5629
5630 if (!linux_boot && initrd_filename != NULL) {
5631 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5632 exit(1);
5633 }
5634
Filip Navarabf65f532009-07-27 10:02:04 -05005635#ifndef _WIN32
5636 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005637 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005638#endif
ths3b46e622007-09-17 08:09:54 +00005639
aliguori7183b4b2008-11-05 20:40:18 +00005640 if (init_timer_alarm() < 0) {
5641 fprintf(stderr, "could not initialize alarm timer\n");
5642 exit(1);
5643 }
pbrook2e70f6e2008-06-29 01:03:05 +00005644 if (use_icount && icount_time_shift < 0) {
5645 use_icount = 2;
5646 /* 125MIPS seems a reasonable initial guess at the guest speed.
5647 It will be corrected fairly quickly anyway. */
5648 icount_time_shift = 3;
5649 init_icount_adjust();
5650 }
pbrook634fce92006-07-15 17:40:09 +00005651
bellardfd1dff42006-02-01 21:29:26 +00005652#ifdef _WIN32
5653 socket_init();
5654#endif
5655
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005656 if (net_init_clients() < 0) {
5657 exit(1);
bellard702c6512004-04-02 21:21:32 +00005658 }
bellardf1510b22003-06-25 00:07:40 +00005659
Glauber Costa406c8df2009-06-17 09:05:30 -04005660 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5661 net_set_boot_mask(net_boot);
5662
balrogdc72ac12008-11-09 00:04:26 +00005663 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005664 if (foreach_device_config(DEV_BT, bt_parse))
5665 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005666
bellard0824d6f2003-06-24 13:42:40 +00005667 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005668 if (ram_size == 0)
5669 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005670
bellard26a5f132008-05-28 12:30:31 +00005671 /* init the dynamic translator */
5672 cpu_exec_init_all(tb_size * 1024 * 1024);
5673
bellard5905b2e2004-08-01 21:53:26 +00005674 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005675
5676 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005677 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005678
balrog9d413d12007-12-04 00:10:34 +00005679 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005680 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005681
balrog9d413d12007-12-04 00:10:34 +00005682 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005683 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005684
ths96d30e42007-01-07 20:42:14 +00005685 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005686 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005687 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5688 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005689 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005690
Juan Quintela2faf58c2009-09-10 03:04:28 +02005691 vmstate_register(0, &vmstate_timers ,&timers_state);
aliguori475e4272008-10-06 20:21:51 +00005692 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005693
aliguori3023f332009-01-16 19:04:14 +00005694 /* Maintain compatibility with multiple stdio monitors */
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005695 if (!strcmp(monitor_devices[0],"stdio")) {
aliguori3023f332009-01-16 19:04:14 +00005696 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5697 const char *devname = serial_devices[i];
5698 if (devname && !strcmp(devname,"mon:stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005699 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005700 break;
5701 } else if (devname && !strcmp(devname,"stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005702 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005703 serial_devices[i] = "mon:stdio";
5704 break;
5705 }
5706 }
5707 }
5708
aliguori268a3622009-04-21 22:30:27 +00005709 if (nb_numa_nodes > 0) {
5710 int i;
5711
5712 if (nb_numa_nodes > smp_cpus) {
5713 nb_numa_nodes = smp_cpus;
5714 }
5715
5716 /* If no memory size if given for any node, assume the default case
5717 * and distribute the available memory equally across all nodes
5718 */
5719 for (i = 0; i < nb_numa_nodes; i++) {
5720 if (node_mem[i] != 0)
5721 break;
5722 }
5723 if (i == nb_numa_nodes) {
5724 uint64_t usedmem = 0;
5725
5726 /* On Linux, the each node's border has to be 8MB aligned,
5727 * the final node gets the rest.
5728 */
5729 for (i = 0; i < nb_numa_nodes - 1; i++) {
5730 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5731 usedmem += node_mem[i];
5732 }
5733 node_mem[i] = ram_size - usedmem;
5734 }
5735
5736 for (i = 0; i < nb_numa_nodes; i++) {
5737 if (node_cpumask[i] != 0)
5738 break;
5739 }
5740 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5741 * must cope with this anyway, because there are BIOSes out there in
5742 * real machines which also use this scheme.
5743 */
5744 if (i == nb_numa_nodes) {
5745 for (i = 0; i < smp_cpus; i++) {
5746 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5747 }
5748 }
5749 }
5750
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005751 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5752 const char *devname = monitor_devices[i];
5753 if (devname && strcmp(devname, "none")) {
5754 char label[32];
5755 if (i == 0) {
5756 snprintf(label, sizeof(label), "monitor");
5757 } else {
5758 snprintf(label, sizeof(label), "monitor%d", i);
5759 }
5760 monitor_hds[i] = qemu_chr_open(label, devname, NULL);
5761 if (!monitor_hds[i]) {
5762 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
5763 devname);
5764 exit(1);
5765 }
aliguori4c621802009-01-16 21:48:20 +00005766 }
5767 }
5768
aliguori2796dae2009-01-16 20:23:27 +00005769 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5770 const char *devname = serial_devices[i];
5771 if (devname && strcmp(devname, "none")) {
Aurelien Jarno324a8022009-09-14 23:49:43 +02005772 char label[32];
5773 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005774 serial_hds[i] = qemu_chr_open(label, devname, NULL);
Aurelien Jarno324a8022009-09-14 23:49:43 +02005775 if (!serial_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005776 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
5777 devname, strerror(errno));
Aurelien Jarno324a8022009-09-14 23:49:43 +02005778 exit(1);
5779 }
aliguori2796dae2009-01-16 20:23:27 +00005780 }
5781 }
5782
5783 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5784 const char *devname = parallel_devices[i];
5785 if (devname && strcmp(devname, "none")) {
5786 char label[32];
5787 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005788 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005789 if (!parallel_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005790 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
5791 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005792 exit(1);
5793 }
5794 }
5795 }
5796
5797 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5798 const char *devname = virtio_consoles[i];
5799 if (devname && strcmp(devname, "none")) {
5800 char label[32];
5801 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005802 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005803 if (!virtcon_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005804 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
5805 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005806 exit(1);
5807 }
5808 }
5809 }
5810
Paul Brookaae94602009-05-14 22:35:06 +01005811 module_call_init(MODULE_INIT_DEVICE);
5812
Markus Armbruster09aaa162009-08-21 10:31:34 +02005813 if (watchdog) {
5814 i = select_watchdog(watchdog);
5815 if (i > 0)
5816 exit (i == 1 ? 1 : 0);
5817 }
5818
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005819 if (machine->compat_props) {
5820 qdev_prop_register_compat(machine->compat_props);
5821 }
Paul Brookfbe1b592009-05-13 17:56:25 +01005822 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005823 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5824
aliguori268a3622009-04-21 22:30:27 +00005825
Juan Quintela67b3b712009-08-28 19:25:15 +02005826#ifndef _WIN32
5827 /* must be after terminal init, SDL library changes signal handlers */
5828 sighandler_setup();
5829#endif
5830
aliguori268a3622009-04-21 22:30:27 +00005831 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5832 for (i = 0; i < nb_numa_nodes; i++) {
5833 if (node_cpumask[i] & (1 << env->cpu_index)) {
5834 env->numa_node = i;
5835 }
5836 }
5837 }
5838
aliguori6f338c32009-02-11 15:21:54 +00005839 current_machine = machine;
5840
aliguori3023f332009-01-16 19:04:14 +00005841 /* init USB devices */
5842 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005843 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5844 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005845 }
5846
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005847 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005848 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005849 exit(1);
5850
aliguori8f391ab2009-01-19 16:34:10 +00005851 if (!display_state)
5852 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005853 /* just use the first displaystate for the moment */
5854 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005855
5856 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005857#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005858 display_type = DT_SDL;
5859#else
5860 display_type = DT_VNC;
5861 vnc_display = "localhost:0,to=99";
5862 show_vnc_port = 1;
5863#endif
5864 }
5865
5866
5867 switch (display_type) {
5868 case DT_NOGRAPHIC:
5869 break;
5870#if defined(CONFIG_CURSES)
5871 case DT_CURSES:
5872 curses_display_init(ds, full_screen);
5873 break;
5874#endif
bellard5b0753e2005-03-01 21:37:28 +00005875#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005876 case DT_SDL:
5877 sdl_display_init(ds, full_screen, no_frame);
5878 break;
bellard5b0753e2005-03-01 21:37:28 +00005879#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005880 case DT_SDL:
5881 cocoa_display_init(ds, full_screen);
5882 break;
bellard313aa562003-08-10 21:52:11 +00005883#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005884 case DT_VNC:
5885 vnc_display_init(ds);
5886 if (vnc_display_open(ds, vnc_display) < 0)
5887 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005888
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005889 if (show_vnc_port) {
5890 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005891 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005892 break;
5893 default:
5894 break;
bellard313aa562003-08-10 21:52:11 +00005895 }
aliguori7d957bd2009-01-15 22:14:11 +00005896 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005897
aliguori3023f332009-01-16 19:04:14 +00005898 dcl = ds->listeners;
5899 while (dcl != NULL) {
5900 if (dcl->dpy_refresh != NULL) {
5901 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5902 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005903 }
aliguori3023f332009-01-16 19:04:14 +00005904 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005905 }
aliguori3023f332009-01-16 19:04:14 +00005906
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005907 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005908 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5909 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5910 }
5911
aliguori2796dae2009-01-16 20:23:27 +00005912 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005913 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005914
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005915 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5916 if (monitor_devices[i] && monitor_hds[i]) {
5917 monitor_init(monitor_hds[i],
5918 MONITOR_USE_READLINE |
5919 ((i == 0) ? MONITOR_IS_DEFAULT : 0));
5920 }
5921 }
bellard82c643f2004-07-14 17:28:13 +00005922
bellard8d11df92004-08-24 21:13:40 +00005923 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005924 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005925 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005926 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005927 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005928 }
bellard82c643f2004-07-14 17:28:13 +00005929 }
bellard82c643f2004-07-14 17:28:13 +00005930
bellard6508fe52005-01-15 12:02:56 +00005931 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005932 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005933 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005934 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005935 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005936 }
5937 }
5938
aliguori9ede2fd2009-01-15 20:05:25 +00005939 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5940 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005941 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005942 if (strstart(devname, "vc", 0))
5943 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5944 }
5945 }
5946
aliguori59030a82009-04-05 18:43:41 +00005947 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5948 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5949 gdbstub_dev);
5950 exit(1);
balrog45669e02007-07-02 13:20:17 +00005951 }
balrog45669e02007-07-02 13:20:17 +00005952
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02005953 qdev_machine_creation_done();
5954
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02005955 rom_load_all();
5956
Juan Quintela05f24012009-08-20 19:42:22 +02005957 if (loadvm) {
5958 if (load_vmstate(cur_mon, loadvm) < 0) {
5959 autostart = 0;
5960 }
5961 }
bellardd63d3072004-10-03 13:29:03 +00005962
Glauber Costa2bb8c102009-07-24 16:20:23 -04005963 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005964 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005965 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005966 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005967 }
thsffd843b2006-12-21 19:46:43 +00005968
blueswir1b9e82a52009-04-05 18:03:31 +00005969#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005970 if (daemonize) {
5971 uint8_t status = 0;
5972 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005973
5974 again1:
5975 len = write(fds[1], &status, 1);
5976 if (len == -1 && (errno == EINTR))
5977 goto again1;
5978
5979 if (len != 1)
5980 exit(1);
5981
aliguoribd54b862008-07-23 00:58:33 +00005982 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005983 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005984 if (fd == -1)
5985 exit(1);
aliguori08585322009-02-27 22:09:45 +00005986 }
ths71e3ceb2006-12-22 02:11:31 +00005987
aliguori08585322009-02-27 22:09:45 +00005988 if (run_as) {
5989 pwd = getpwnam(run_as);
5990 if (!pwd) {
5991 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5992 exit(1);
5993 }
5994 }
ths71e3ceb2006-12-22 02:11:31 +00005995
aliguori08585322009-02-27 22:09:45 +00005996 if (chroot_dir) {
5997 if (chroot(chroot_dir) < 0) {
5998 fprintf(stderr, "chroot failed\n");
5999 exit(1);
6000 }
6001 chdir("/");
6002 }
6003
6004 if (run_as) {
6005 if (setgid(pwd->pw_gid) < 0) {
6006 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6007 exit(1);
6008 }
6009 if (setuid(pwd->pw_uid) < 0) {
6010 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6011 exit(1);
6012 }
6013 if (setuid(0) != -1) {
6014 fprintf(stderr, "Dropping privileges failed\n");
6015 exit(1);
6016 }
6017 }
aliguori08585322009-02-27 22:09:45 +00006018
6019 if (daemonize) {
6020 dup2(fd, 0);
6021 dup2(fd, 1);
6022 dup2(fd, 2);
6023
6024 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006025 }
blueswir1b9e82a52009-04-05 18:03:31 +00006026#endif
ths71e3ceb2006-12-22 02:11:31 +00006027
bellard8a7ddc32004-03-31 19:00:16 +00006028 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006029 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006030 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006031
bellard0824d6f2003-06-24 13:42:40 +00006032 return 0;
6033}