blob: aa678ad3805e949ee36d53317f2ebe4a7f960fef [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000062#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
65/* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67/* #include <linux/hpet.h> */
68#include "hpet.h"
69
bellarde57a8c02005-11-10 23:58:52 +000070#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000071#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000072#endif
73#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000074#include <sys/stat.h>
75#include <sys/ethernet.h>
76#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000077#include <netinet/arp.h>
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_icmp.h> // must come after ip.h
82#include <netinet/udp.h>
83#include <netinet/tcp.h>
84#include <net/if.h>
85#include <syslog.h>
86#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000087/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000090#endif
bellard7d3505c2004-05-12 19:32:15 +000091#endif
bellardec530c82006-04-25 22:36:06 +000092#endif
bellard67b915a2004-03-31 23:37:16 +000093
blueswir19892fbf2008-08-24 10:34:20 +000094#if defined(__OpenBSD__)
95#include <util.h>
96#endif
97
ths8a16d272008-07-19 09:56:24 +000098#if defined(CONFIG_VDE)
99#include <libvdeplug.h>
100#endif
101
bellard67b915a2004-03-31 23:37:16 +0000102#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000103#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000104#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000105#endif
106
bellard73332e52004-04-04 20:22:28 +0000107#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200108#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200109#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000110int qemu_main(int argc, char **argv, char **envp);
111int main(int argc, char **argv)
112{
Stefan Weil59a36a22009-06-18 20:11:03 +0200113 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000114}
115#undef main
116#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000117#endif
bellard73332e52004-04-04 20:22:28 +0000118#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000119
bellard5b0753e2005-03-01 21:37:28 +0000120#ifdef CONFIG_COCOA
121#undef main
122#define main qemu_main
123#endif /* CONFIG_COCOA */
124
blueswir1511d2b12009-03-07 15:32:56 +0000125#include "hw/hw.h"
126#include "hw/boards.h"
127#include "hw/usb.h"
128#include "hw/pcmcia.h"
129#include "hw/pc.h"
130#include "hw/audiodev.h"
131#include "hw/isa.h"
132#include "hw/baum.h"
133#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100134#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000135#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000136#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200137#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200138#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000139#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000140#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000141#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000142#include "monitor.h"
143#include "console.h"
144#include "sysemu.h"
145#include "gdbstub.h"
146#include "qemu-timer.h"
147#include "qemu-char.h"
148#include "cache-utils.h"
149#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200150#include "block_int.h"
151#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000152#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153#include "audio/audio.h"
154#include "migration.h"
155#include "kvm.h"
156#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200157#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200158#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
bellard0824d6f2003-06-24 13:42:40 +0000160#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000161
bellard8a7ddc32004-03-31 19:00:16 +0000162#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000163
blueswir1511d2b12009-03-07 15:32:56 +0000164#include "qemu_socket.h"
165
Jan Kiszkad918f232009-06-24 14:42:30 +0200166#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000167
Blue Swirl72cf2d42009-09-12 07:36:22 +0000168#include "qemu-queue.h"
169
blueswir19dc63a12008-10-04 07:25:46 +0000170//#define DEBUG_NET
171//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000172
bellard1bfe8562004-07-08 21:17:50 +0000173#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000174
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +0200175/* Maximum number of monitor devices */
176#define MAX_MONITOR_DEVICES 10
177
Paul Brook5cea8592009-05-30 00:52:44 +0100178static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000179const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000180/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000181 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000182struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
183struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000184enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000185static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500186DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000187const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500188ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000189int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000190NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000191int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200192int autostart;
balrogf6503052008-02-17 11:42:19 +0000193static int rtc_utc = 1;
194static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200195QEMUClock *rtc_clock;
Zachary Amsden86176752009-07-30 00:15:02 -1000196int vga_interface_type = VGA_CIRRUS;
bellardd8272202005-04-06 20:32:23 +0000197#ifdef TARGET_SPARC
198int graphic_width = 1024;
199int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000200int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000201#else
bellard1bfe8562004-07-08 21:17:50 +0000202int graphic_width = 800;
203int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000204int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000205#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000206static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000207#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000208static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000209#endif
ths667acca2006-12-11 02:08:05 +0000210int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000211CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000212CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000213CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000214#ifdef TARGET_I386
215int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000216int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000217#endif
bellardbb36d472005-11-05 14:22:28 +0000218int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000219int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000220int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200221int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200222int smp_cores = 1;
223int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000224const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000225int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000226int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000227int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000228int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000229int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000230int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000231int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200232uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000233#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000234int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000235#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200236const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000237const char *option_rom[MAX_OPTION_ROMS];
238int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000239int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000240#ifdef TARGET_ARM
241int old_param = 0;
242#endif
thsc35734b2007-03-19 15:17:08 +0000243const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000244int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500245int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000246#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000247unsigned int nb_prom_envs = 0;
248const char *prom_envs[MAX_PROM_ENVS];
249#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200250int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000251
aliguori268a3622009-04-21 22:30:27 +0000252int nb_numa_nodes;
253uint64_t node_mem[MAX_NODES];
254uint64_t node_cpumask[MAX_NODES];
255
balrogee5605e2007-12-03 03:01:40 +0000256static CPUState *cur_cpu;
257static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000258static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000259/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000260static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000261/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000262#define MAX_ICOUNT_SHIFT 10
263/* Compensate for varying guest execution speed. */
264static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000265static QEMUTimer *icount_rt_timer;
266static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000267static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000268
blueswir18fcb1b92008-09-18 18:29:08 +0000269uint8_t qemu_uuid[16];
270
Jan Kiszka76e30d02009-07-02 00:19:02 +0200271static QEMUBootSetHandler *boot_set_handler;
272static void *boot_set_opaque;
273
bellard0824d6f2003-06-24 13:42:40 +0000274/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000275/* x86 ISA bus support */
276
Anthony Liguoric227f092009-10-01 16:12:16 -0500277target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000278PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000279
bellard0824d6f2003-06-24 13:42:40 +0000280/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000281void hw_error(const char *fmt, ...)
282{
283 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000284 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000285
286 va_start(ap, fmt);
287 fprintf(stderr, "qemu: hardware error: ");
288 vfprintf(stderr, fmt, ap);
289 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000290 for(env = first_cpu; env != NULL; env = env->next_cpu) {
291 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000292#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000293 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000294#else
bellard6a00d602005-11-21 23:25:50 +0000295 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000296#endif
bellard6a00d602005-11-21 23:25:50 +0000297 }
bellard0824d6f2003-06-24 13:42:40 +0000298 va_end(ap);
299 abort();
300}
Andi Kleen18894652009-07-02 09:34:17 +0200301
302static void set_proc_name(const char *s)
303{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700304#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200305 char name[16];
306 if (!s)
307 return;
308 name[sizeof(name) - 1] = 0;
309 strncpy(name, s, sizeof(name));
310 /* Could rewrite argv[0] too, but that's a bit more complicated.
311 This simple way is enough for `top'. */
312 prctl(PR_SET_NAME, name);
313#endif
314}
aliguoridf751fa2008-12-04 20:19:35 +0000315
316/***************/
317/* ballooning */
318
319static QEMUBalloonEvent *qemu_balloon_event;
320void *qemu_balloon_event_opaque;
321
322void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
323{
324 qemu_balloon_event = func;
325 qemu_balloon_event_opaque = opaque;
326}
327
Anthony Liguoric227f092009-10-01 16:12:16 -0500328void qemu_balloon(ram_addr_t target)
aliguoridf751fa2008-12-04 20:19:35 +0000329{
330 if (qemu_balloon_event)
331 qemu_balloon_event(qemu_balloon_event_opaque, target);
332}
333
Anthony Liguoric227f092009-10-01 16:12:16 -0500334ram_addr_t qemu_balloon_status(void)
aliguoridf751fa2008-12-04 20:19:35 +0000335{
336 if (qemu_balloon_event)
337 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
338 return 0;
339}
bellard0824d6f2003-06-24 13:42:40 +0000340
bellard8a7ddc32004-03-31 19:00:16 +0000341/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000342/* keyboard/mouse */
343
344static QEMUPutKBDEvent *qemu_put_kbd_event;
345static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000346static QEMUPutMouseEntry *qemu_put_mouse_event_head;
347static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000348
349void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
350{
351 qemu_put_kbd_event_opaque = opaque;
352 qemu_put_kbd_event = func;
353}
354
ths455204e2007-01-05 16:42:13 +0000355QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
356 void *opaque, int absolute,
357 const char *name)
bellard63066f42004-06-03 18:45:02 +0000358{
ths455204e2007-01-05 16:42:13 +0000359 QEMUPutMouseEntry *s, *cursor;
360
361 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000362
363 s->qemu_put_mouse_event = func;
364 s->qemu_put_mouse_event_opaque = opaque;
365 s->qemu_put_mouse_event_absolute = absolute;
366 s->qemu_put_mouse_event_name = qemu_strdup(name);
367 s->next = NULL;
368
369 if (!qemu_put_mouse_event_head) {
370 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
371 return s;
372 }
373
374 cursor = qemu_put_mouse_event_head;
375 while (cursor->next != NULL)
376 cursor = cursor->next;
377
378 cursor->next = s;
379 qemu_put_mouse_event_current = s;
380
381 return s;
382}
383
384void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
385{
386 QEMUPutMouseEntry *prev = NULL, *cursor;
387
388 if (!qemu_put_mouse_event_head || entry == NULL)
389 return;
390
391 cursor = qemu_put_mouse_event_head;
392 while (cursor != NULL && cursor != entry) {
393 prev = cursor;
394 cursor = cursor->next;
395 }
396
397 if (cursor == NULL) // does not exist or list empty
398 return;
399 else if (prev == NULL) { // entry is head
400 qemu_put_mouse_event_head = cursor->next;
401 if (qemu_put_mouse_event_current == entry)
402 qemu_put_mouse_event_current = cursor->next;
403 qemu_free(entry->qemu_put_mouse_event_name);
404 qemu_free(entry);
405 return;
406 }
407
408 prev->next = entry->next;
409
410 if (qemu_put_mouse_event_current == entry)
411 qemu_put_mouse_event_current = prev;
412
413 qemu_free(entry->qemu_put_mouse_event_name);
414 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000415}
416
417void kbd_put_keycode(int keycode)
418{
419 if (qemu_put_kbd_event) {
420 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
421 }
422}
423
424void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
425{
ths455204e2007-01-05 16:42:13 +0000426 QEMUPutMouseEvent *mouse_event;
427 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000428 int width;
ths455204e2007-01-05 16:42:13 +0000429
430 if (!qemu_put_mouse_event_current) {
431 return;
432 }
433
434 mouse_event =
435 qemu_put_mouse_event_current->qemu_put_mouse_event;
436 mouse_event_opaque =
437 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
438
439 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000440 if (graphic_rotate) {
441 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
442 width = 0x7fff;
443 else
aurel32b94ed572008-03-10 19:34:27 +0000444 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000445 mouse_event(mouse_event_opaque,
446 width - dy, dx, dz, buttons_state);
447 } else
448 mouse_event(mouse_event_opaque,
449 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000450 }
451}
452
bellard09b26c52006-04-12 21:09:08 +0000453int kbd_mouse_is_absolute(void)
454{
ths455204e2007-01-05 16:42:13 +0000455 if (!qemu_put_mouse_event_current)
456 return 0;
457
458 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
459}
460
aliguori376253e2009-03-05 23:01:23 +0000461void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000462{
463 QEMUPutMouseEntry *cursor;
464 int index = 0;
465
466 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000467 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000468 return;
469 }
470
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000472 cursor = qemu_put_mouse_event_head;
473 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "%c Mouse #%d: %s\n",
475 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
476 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000477 index++;
478 cursor = cursor->next;
479 }
480}
481
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300482void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000483{
484 QEMUPutMouseEntry *cursor;
485 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300486 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000487
488 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000489 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000490 return;
491 }
492
493 cursor = qemu_put_mouse_event_head;
494 while (cursor != NULL && index != i) {
495 i++;
496 cursor = cursor->next;
497 }
498
499 if (cursor != NULL)
500 qemu_put_mouse_event_current = cursor;
501 else
aliguori376253e2009-03-05 23:01:23 +0000502 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000503}
504
bellard87858c82003-06-27 12:01:39 +0000505/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000506uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000507{
508 union {
509 uint64_t ll;
510 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200511#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000512 uint32_t high, low;
513#else
514 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000515#endif
bellard87858c82003-06-27 12:01:39 +0000516 } l;
517 } u, res;
518 uint64_t rl, rh;
519
520 u.ll = a;
521 rl = (uint64_t)u.l.low * (uint64_t)b;
522 rh = (uint64_t)u.l.high * (uint64_t)b;
523 rh += (rl >> 32);
524 res.l.high = rh / c;
525 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
526 return res.ll;
527}
528
bellard1dce7c32006-07-13 23:20:22 +0000529/***********************************************************/
530/* real time host monotonic timer */
531
Jan Kiszka21d5d122009-09-15 13:36:04 +0200532static int64_t get_clock_realtime(void)
533{
534 struct timeval tv;
535
536 gettimeofday(&tv, NULL);
537 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
538}
539
bellard1dce7c32006-07-13 23:20:22 +0000540#ifdef WIN32
541
542static int64_t clock_freq;
543
544static void init_get_clock(void)
545{
bellarda8e5ac32006-07-14 09:36:13 +0000546 LARGE_INTEGER freq;
547 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000548 ret = QueryPerformanceFrequency(&freq);
549 if (ret == 0) {
550 fprintf(stderr, "Could not calibrate ticks\n");
551 exit(1);
552 }
553 clock_freq = freq.QuadPart;
554}
555
556static int64_t get_clock(void)
557{
558 LARGE_INTEGER ti;
559 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500560 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000561}
562
563#else
564
565static int use_rt_clock;
566
567static void init_get_clock(void)
568{
569 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000570#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100571 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000572 {
573 struct timespec ts;
574 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
575 use_rt_clock = 1;
576 }
577 }
578#endif
579}
580
581static int64_t get_clock(void)
582{
blueswir1c5e97232009-03-07 20:06:23 +0000583#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100584 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000585 if (use_rt_clock) {
586 struct timespec ts;
587 clock_gettime(CLOCK_MONOTONIC, &ts);
588 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000589 } else
bellard1dce7c32006-07-13 23:20:22 +0000590#endif
591 {
592 /* XXX: using gettimeofday leads to problems if the date
593 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200594 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000595 }
596}
bellard1dce7c32006-07-13 23:20:22 +0000597#endif
598
pbrook2e70f6e2008-06-29 01:03:05 +0000599/* Return the virtual CPU time, based on the instruction counter. */
600static int64_t cpu_get_icount(void)
601{
602 int64_t icount;
603 CPUState *env = cpu_single_env;;
604 icount = qemu_icount;
605 if (env) {
606 if (!can_do_io(env))
607 fprintf(stderr, "Bad clock read\n");
608 icount -= (env->icount_decr.u16.low + env->icount_extra);
609 }
610 return qemu_icount_bias + (icount << icount_time_shift);
611}
612
bellard1dce7c32006-07-13 23:20:22 +0000613/***********************************************************/
614/* guest cycle counter */
615
Juan Quintela6f68e332009-09-10 03:04:27 +0200616typedef struct TimersState {
617 int64_t cpu_ticks_prev;
618 int64_t cpu_ticks_offset;
619 int64_t cpu_clock_offset;
620 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500621 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200622} TimersState;
623
624TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000625
626/* return the host CPU cycle counter and handle stop/restart */
627int64_t cpu_get_ticks(void)
628{
pbrook2e70f6e2008-06-29 01:03:05 +0000629 if (use_icount) {
630 return cpu_get_icount();
631 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200632 if (!timers_state.cpu_ticks_enabled) {
633 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000634 } else {
635 int64_t ticks;
636 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200637 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000638 /* Note: non increasing ticks may happen if the host uses
639 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200640 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000641 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200642 timers_state.cpu_ticks_prev = ticks;
643 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000644 }
645}
646
647/* return the host CPU monotonic timer and handle stop/restart */
648static int64_t cpu_get_clock(void)
649{
650 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200651 if (!timers_state.cpu_ticks_enabled) {
652 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000653 } else {
654 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200655 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000656 }
657}
658
659/* enable cpu_get_ticks() */
660void cpu_enable_ticks(void)
661{
Juan Quintela6f68e332009-09-10 03:04:27 +0200662 if (!timers_state.cpu_ticks_enabled) {
663 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
664 timers_state.cpu_clock_offset -= get_clock();
665 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000666 }
667}
668
669/* disable cpu_get_ticks() : the clock is stopped. You must not call
670 cpu_get_ticks() after that. */
671void cpu_disable_ticks(void)
672{
Juan Quintela6f68e332009-09-10 03:04:27 +0200673 if (timers_state.cpu_ticks_enabled) {
674 timers_state.cpu_ticks_offset = cpu_get_ticks();
675 timers_state.cpu_clock_offset = cpu_get_clock();
676 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000677 }
678}
679
680/***********************************************************/
681/* timers */
ths5fafdf22007-09-16 21:08:06 +0000682
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200683#define QEMU_CLOCK_REALTIME 0
684#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200685#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000686
687struct QEMUClock {
688 int type;
689 /* XXX: add frequency */
690};
691
692struct QEMUTimer {
693 QEMUClock *clock;
694 int64_t expire_time;
695 QEMUTimerCB *cb;
696 void *opaque;
697 struct QEMUTimer *next;
698};
699
thsc8994012007-08-19 21:56:03 +0000700struct qemu_alarm_timer {
701 char const *name;
thsefe75412007-08-24 01:36:32 +0000702 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000703
704 int (*start)(struct qemu_alarm_timer *t);
705 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000706 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000707 void *priv;
708};
709
thsefe75412007-08-24 01:36:32 +0000710#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000711#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000712
713static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
714{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200715 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000716}
717
718static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
719{
720 if (!alarm_has_dynticks(t))
721 return;
722
723 t->rearm(t);
724}
725
726/* TODO: MIN_TIMER_REARM_US should be optimized */
727#define MIN_TIMER_REARM_US 250
728
thsc8994012007-08-19 21:56:03 +0000729static struct qemu_alarm_timer *alarm_timer;
730
731#ifdef _WIN32
732
733struct qemu_alarm_win32 {
734 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000735 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000736} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000737
738static int win32_start_timer(struct qemu_alarm_timer *t);
739static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000740static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000741
742#else
743
744static int unix_start_timer(struct qemu_alarm_timer *t);
745static void unix_stop_timer(struct qemu_alarm_timer *t);
746
ths231c6582007-08-26 17:29:15 +0000747#ifdef __linux__
748
thsefe75412007-08-24 01:36:32 +0000749static int dynticks_start_timer(struct qemu_alarm_timer *t);
750static void dynticks_stop_timer(struct qemu_alarm_timer *t);
751static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
752
thsc40ec5a2007-08-19 22:09:40 +0000753static int hpet_start_timer(struct qemu_alarm_timer *t);
754static void hpet_stop_timer(struct qemu_alarm_timer *t);
755
thsc8994012007-08-19 21:56:03 +0000756static int rtc_start_timer(struct qemu_alarm_timer *t);
757static void rtc_stop_timer(struct qemu_alarm_timer *t);
758
thsefe75412007-08-24 01:36:32 +0000759#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000760
761#endif /* _WIN32 */
762
pbrook2e70f6e2008-06-29 01:03:05 +0000763/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000764 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000765 When the guest is idle real and virtual time will be aligned in
766 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500767#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000768
769static void icount_adjust(void)
770{
771 int64_t cur_time;
772 int64_t cur_icount;
773 int64_t delta;
774 static int64_t last_delta;
775 /* If the VM is not running, then do nothing. */
776 if (!vm_running)
777 return;
778
779 cur_time = cpu_get_clock();
780 cur_icount = qemu_get_clock(vm_clock);
781 delta = cur_icount - cur_time;
782 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
783 if (delta > 0
784 && last_delta + ICOUNT_WOBBLE < delta * 2
785 && icount_time_shift > 0) {
786 /* The guest is getting too far ahead. Slow time down. */
787 icount_time_shift--;
788 }
789 if (delta < 0
790 && last_delta - ICOUNT_WOBBLE > delta * 2
791 && icount_time_shift < MAX_ICOUNT_SHIFT) {
792 /* The guest is getting too far behind. Speed time up. */
793 icount_time_shift++;
794 }
795 last_delta = delta;
796 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
797}
798
799static void icount_adjust_rt(void * opaque)
800{
801 qemu_mod_timer(icount_rt_timer,
802 qemu_get_clock(rt_clock) + 1000);
803 icount_adjust();
804}
805
806static void icount_adjust_vm(void * opaque)
807{
808 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500809 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000810 icount_adjust();
811}
812
813static void init_icount_adjust(void)
814{
815 /* Have both realtime and virtual time triggers for speed adjustment.
816 The realtime trigger catches emulated time passing too slowly,
817 the virtual time trigger catches emulated time passing too fast.
818 Realtime triggers occur even when idle, so use them less frequently
819 than VM triggers. */
820 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
821 qemu_mod_timer(icount_rt_timer,
822 qemu_get_clock(rt_clock) + 1000);
823 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
824 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500825 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000826}
827
thsc8994012007-08-19 21:56:03 +0000828static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000829#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000830#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000831 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
832 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000833 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000834 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000835 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000836 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000837#endif
thsefe75412007-08-24 01:36:32 +0000838 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000839#else
thsefe75412007-08-24 01:36:32 +0000840 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
841 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
842 {"win32", 0, win32_start_timer,
843 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000844#endif
845 {NULL, }
846};
847
blueswir13f47aa82008-03-09 06:59:01 +0000848static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000849{
850 int i;
851
852 printf("Available alarm timers, in order of precedence:\n");
853 for (i = 0; alarm_timers[i].name; i++)
854 printf("%s\n", alarm_timers[i].name);
855}
856
857static void configure_alarms(char const *opt)
858{
859 int i;
860 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000861 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000862 char *arg;
863 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000864 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000865
aurel323adda042008-03-09 23:43:49 +0000866 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000867 show_available_alarms();
868 exit(0);
869 }
870
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200871 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000872
873 /* Reorder the array */
874 name = strtok(arg, ",");
875 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000876 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000877 if (!strcmp(alarm_timers[i].name, name))
878 break;
879 }
880
881 if (i == count) {
882 fprintf(stderr, "Unknown clock %s\n", name);
883 goto next;
884 }
885
886 if (i < cur)
887 /* Ignore */
888 goto next;
889
890 /* Swap */
891 tmp = alarm_timers[i];
892 alarm_timers[i] = alarm_timers[cur];
893 alarm_timers[cur] = tmp;
894
895 cur++;
896next:
897 name = strtok(NULL, ",");
898 }
899
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200900 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000901
902 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000903 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000904 for (i = cur; i < count; i++)
905 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000906 } else {
907 show_available_alarms();
908 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000909 }
thsf3dcfad2007-08-24 01:26:02 +0000910}
911
Jan Kiszka21d5d122009-09-15 13:36:04 +0200912#define QEMU_NUM_CLOCKS 3
913
bellard8a7ddc32004-03-31 19:00:16 +0000914QEMUClock *rt_clock;
915QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +0200916QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +0000917
Jan Kiszka21d5d122009-09-15 13:36:04 +0200918static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +0000919
pbrook9596ebb2007-11-18 01:44:38 +0000920static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000921{
922 QEMUClock *clock;
923 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000924 clock->type = type;
925 return clock;
926}
927
928QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
929{
930 QEMUTimer *ts;
931
932 ts = qemu_mallocz(sizeof(QEMUTimer));
933 ts->clock = clock;
934 ts->cb = cb;
935 ts->opaque = opaque;
936 return ts;
937}
938
939void qemu_free_timer(QEMUTimer *ts)
940{
941 qemu_free(ts);
942}
943
944/* stop a timer, but do not dealloc it */
945void qemu_del_timer(QEMUTimer *ts)
946{
947 QEMUTimer **pt, *t;
948
949 /* NOTE: this code must be signal safe because
950 qemu_timer_expired() can be called from a signal. */
951 pt = &active_timers[ts->clock->type];
952 for(;;) {
953 t = *pt;
954 if (!t)
955 break;
956 if (t == ts) {
957 *pt = t->next;
958 break;
959 }
960 pt = &t->next;
961 }
962}
963
964/* modify the current timer so that it will be fired when current_time
965 >= expire_time. The corresponding callback will be called. */
966void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
967{
968 QEMUTimer **pt, *t;
969
970 qemu_del_timer(ts);
971
972 /* add the timer in the sorted list */
973 /* NOTE: this code must be signal safe because
974 qemu_timer_expired() can be called from a signal. */
975 pt = &active_timers[ts->clock->type];
976 for(;;) {
977 t = *pt;
978 if (!t)
979 break;
ths5fafdf22007-09-16 21:08:06 +0000980 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000981 break;
982 pt = &t->next;
983 }
984 ts->expire_time = expire_time;
985 ts->next = *pt;
986 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000987
988 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000989 if (pt == &active_timers[ts->clock->type]) {
990 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
991 qemu_rearm_alarm_timer(alarm_timer);
992 }
993 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000994 if (use_icount)
995 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000996 }
bellard8a7ddc32004-03-31 19:00:16 +0000997}
998
999int qemu_timer_pending(QEMUTimer *ts)
1000{
1001 QEMUTimer *t;
1002 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1003 if (t == ts)
1004 return 1;
1005 }
1006 return 0;
1007}
1008
Stefano Stabellini2430ffe2009-08-03 10:56:01 +01001009int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001010{
1011 if (!timer_head)
1012 return 0;
1013 return (timer_head->expire_time <= current_time);
1014}
1015
1016static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1017{
1018 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001019
bellard8a7ddc32004-03-31 19:00:16 +00001020 for(;;) {
1021 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001022 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001023 break;
1024 /* remove timer from the list before calling the callback */
1025 *ptimer_head = ts->next;
1026 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001027
bellard8a7ddc32004-03-31 19:00:16 +00001028 /* run the callback (the timer list can be modified) */
1029 ts->cb(ts->opaque);
1030 }
1031}
1032
1033int64_t qemu_get_clock(QEMUClock *clock)
1034{
1035 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001036 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001037 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001038 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001039 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001040 if (use_icount) {
1041 return cpu_get_icount();
1042 } else {
1043 return cpu_get_clock();
1044 }
Jan Kiszka21d5d122009-09-15 13:36:04 +02001045 case QEMU_CLOCK_HOST:
1046 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +00001047 }
1048}
1049
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001050static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +00001051{
1052 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001053 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
1054 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001055 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +02001056
1057 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +00001058}
1059
bellard8a7ddc32004-03-31 19:00:16 +00001060/* save a timer */
1061void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1062{
1063 uint64_t expire_time;
1064
1065 if (qemu_timer_pending(ts)) {
1066 expire_time = ts->expire_time;
1067 } else {
1068 expire_time = -1;
1069 }
1070 qemu_put_be64(f, expire_time);
1071}
1072
1073void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1074{
1075 uint64_t expire_time;
1076
1077 expire_time = qemu_get_be64(f);
1078 if (expire_time != -1) {
1079 qemu_mod_timer(ts, expire_time);
1080 } else {
1081 qemu_del_timer(ts);
1082 }
1083}
1084
Juan Quintela2faf58c2009-09-10 03:04:28 +02001085static const VMStateDescription vmstate_timers = {
1086 .name = "timer",
1087 .version_id = 2,
1088 .minimum_version_id = 1,
1089 .minimum_version_id_old = 1,
1090 .fields = (VMStateField []) {
1091 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001092 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001093 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1094 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001095 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001096};
bellard8a7ddc32004-03-31 19:00:16 +00001097
aliguori50317c72009-04-24 18:03:29 +00001098static void qemu_event_increment(void);
1099
bellard67b915a2004-03-31 23:37:16 +00001100#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001101static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1102 DWORD_PTR dwUser, DWORD_PTR dw1,
1103 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001104#else
bellard8a7ddc32004-03-31 19:00:16 +00001105static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001106#endif
bellard8a7ddc32004-03-31 19:00:16 +00001107{
bellard02ba45c2004-06-25 14:46:23 +00001108#if 0
1109#define DISP_FREQ 1000
1110 {
1111 static int64_t delta_min = INT64_MAX;
1112 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1113 static int count;
1114 ti = qemu_get_clock(vm_clock);
1115 if (last_clock != 0) {
1116 delta = ti - last_clock;
1117 if (delta < delta_min)
1118 delta_min = delta;
1119 if (delta > delta_max)
1120 delta_max = delta;
1121 delta_cum += delta;
1122 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001123 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001124 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1125 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1126 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1127 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001128 count = 0;
1129 delta_min = INT64_MAX;
1130 delta_max = 0;
1131 delta_cum = 0;
1132 }
1133 }
1134 last_clock = ti;
1135 }
1136#endif
thsefe75412007-08-24 01:36:32 +00001137 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001138 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001139 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001140 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001141 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001142 qemu_get_clock(rt_clock)) ||
1143 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1144 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001145 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001146 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001147
aliguorid6dc3d42009-04-24 18:04:07 +00001148#ifndef CONFIG_IOTHREAD
1149 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001150 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001151 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001152 }
aliguorid6dc3d42009-04-24 18:04:07 +00001153#endif
aliguori43b96852009-04-24 18:03:33 +00001154 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001155 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001156 }
1157}
1158
pbrook2e70f6e2008-06-29 01:03:05 +00001159static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001160{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001161 /* To avoid problems with overflow limit this to 2^32. */
1162 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001163
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001164 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1165 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001166 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001167 }
1168 if (active_timers[QEMU_CLOCK_HOST]) {
1169 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1170 qemu_get_clock(host_clock);
1171 if (hdelta < delta)
1172 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001173 }
1174
pbrook2e70f6e2008-06-29 01:03:05 +00001175 if (delta < 0)
1176 delta = 0;
thsefe75412007-08-24 01:36:32 +00001177
pbrook2e70f6e2008-06-29 01:03:05 +00001178 return delta;
1179}
1180
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001181#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001182static uint64_t qemu_next_deadline_dyntick(void)
1183{
1184 int64_t delta;
1185 int64_t rtdelta;
1186
1187 if (use_icount)
1188 delta = INT32_MAX;
1189 else
1190 delta = (qemu_next_deadline() + 999) / 1000;
1191
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001192 if (active_timers[QEMU_CLOCK_REALTIME]) {
1193 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001194 qemu_get_clock(rt_clock))*1000;
1195 if (rtdelta < delta)
1196 delta = rtdelta;
1197 }
1198
1199 if (delta < MIN_TIMER_REARM_US)
1200 delta = MIN_TIMER_REARM_US;
1201
1202 return delta;
thsefe75412007-08-24 01:36:32 +00001203}
blueswir18632fb92008-09-14 13:59:34 +00001204#endif
thsefe75412007-08-24 01:36:32 +00001205
bellardfd872592004-05-12 19:11:15 +00001206#ifndef _WIN32
1207
aliguori7183b4b2008-11-05 20:40:18 +00001208/* Sets a specific flag */
1209static int fcntl_setfl(int fd, int flag)
1210{
1211 int flags;
1212
1213 flags = fcntl(fd, F_GETFL);
1214 if (flags == -1)
1215 return -errno;
1216
1217 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1218 return -errno;
1219
1220 return 0;
1221}
1222
bellard829309c2004-05-20 13:20:12 +00001223#if defined(__linux__)
1224
bellardfd872592004-05-12 19:11:15 +00001225#define RTC_FREQ 1024
1226
aurel32de9a95f2008-11-11 13:41:01 +00001227static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001228{
thsc8994012007-08-19 21:56:03 +00001229 struct sigaction act;
1230
1231 /* timer signal */
1232 sigfillset(&act.sa_mask);
1233 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001234 act.sa_handler = host_alarm_handler;
1235
1236 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001237 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001238 fcntl(fd, F_SETOWN, getpid());
1239}
1240
thsc40ec5a2007-08-19 22:09:40 +00001241static int hpet_start_timer(struct qemu_alarm_timer *t)
1242{
1243 struct hpet_info info;
1244 int r, fd;
1245
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001246 fd = qemu_open("/dev/hpet", O_RDONLY);
thsc40ec5a2007-08-19 22:09:40 +00001247 if (fd < 0)
1248 return -1;
1249
1250 /* Set frequency */
1251 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1252 if (r < 0) {
1253 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1254 "error, but for better emulation accuracy type:\n"
1255 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1256 goto fail;
1257 }
1258
1259 /* Check capabilities */
1260 r = ioctl(fd, HPET_INFO, &info);
1261 if (r < 0)
1262 goto fail;
1263
1264 /* Enable periodic mode */
1265 r = ioctl(fd, HPET_EPI, 0);
1266 if (info.hi_flags && (r < 0))
1267 goto fail;
1268
1269 /* Enable interrupt */
1270 r = ioctl(fd, HPET_IE_ON, 0);
1271 if (r < 0)
1272 goto fail;
1273
1274 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001275 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001276
1277 return 0;
1278fail:
1279 close(fd);
1280 return -1;
1281}
1282
1283static void hpet_stop_timer(struct qemu_alarm_timer *t)
1284{
pbrookfcdc2122007-08-23 20:22:22 +00001285 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001286
1287 close(fd);
1288}
1289
thsc8994012007-08-19 21:56:03 +00001290static int rtc_start_timer(struct qemu_alarm_timer *t)
1291{
1292 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001293 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001294
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001295 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001296 if (rtc_fd < 0)
1297 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001298 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1299 if (current_rtc_freq != RTC_FREQ &&
1300 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001301 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1304 goto fail;
1305 }
1306 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1307 fail:
1308 close(rtc_fd);
1309 return -1;
1310 }
thsc8994012007-08-19 21:56:03 +00001311
1312 enable_sigio_timer(rtc_fd);
1313
pbrookfcdc2122007-08-23 20:22:22 +00001314 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001315
bellardfd872592004-05-12 19:11:15 +00001316 return 0;
1317}
1318
thsc8994012007-08-19 21:56:03 +00001319static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001320{
pbrookfcdc2122007-08-23 20:22:22 +00001321 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001322
1323 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001324}
1325
thsefe75412007-08-24 01:36:32 +00001326static int dynticks_start_timer(struct qemu_alarm_timer *t)
1327{
1328 struct sigevent ev;
1329 timer_t host_timer;
1330 struct sigaction act;
1331
1332 sigfillset(&act.sa_mask);
1333 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001334 act.sa_handler = host_alarm_handler;
1335
1336 sigaction(SIGALRM, &act, NULL);
1337
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001338 /*
1339 * Initialize ev struct to 0 to avoid valgrind complaining
1340 * about uninitialized data in timer_create call
1341 */
1342 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001343 ev.sigev_value.sival_int = 0;
1344 ev.sigev_notify = SIGEV_SIGNAL;
1345 ev.sigev_signo = SIGALRM;
1346
1347 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1348 perror("timer_create");
1349
1350 /* disable dynticks */
1351 fprintf(stderr, "Dynamic Ticks disabled\n");
1352
1353 return -1;
1354 }
1355
blueswir10399bfe2008-11-16 11:37:18 +00001356 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001357
1358 return 0;
1359}
1360
1361static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1362{
blueswir10399bfe2008-11-16 11:37:18 +00001363 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001364
1365 timer_delete(host_timer);
1366}
1367
1368static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1369{
blueswir10399bfe2008-11-16 11:37:18 +00001370 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001371 struct itimerspec timeout;
1372 int64_t nearest_delta_us = INT64_MAX;
1373 int64_t current_us;
1374
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001375 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001376 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1377 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001378 return;
thsefe75412007-08-24 01:36:32 +00001379
pbrook2e70f6e2008-06-29 01:03:05 +00001380 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001381
1382 /* check whether a timer is already running */
1383 if (timer_gettime(host_timer, &timeout)) {
1384 perror("gettime");
1385 fprintf(stderr, "Internal timer error: aborting\n");
1386 exit(1);
1387 }
1388 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1389 if (current_us && current_us <= nearest_delta_us)
1390 return;
1391
1392 timeout.it_interval.tv_sec = 0;
1393 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1394 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1395 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1396 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1397 perror("settime");
1398 fprintf(stderr, "Internal timer error: aborting\n");
1399 exit(1);
1400 }
1401}
1402
ths70744b32007-08-26 17:31:30 +00001403#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001404
thsc8994012007-08-19 21:56:03 +00001405static int unix_start_timer(struct qemu_alarm_timer *t)
1406{
1407 struct sigaction act;
1408 struct itimerval itv;
1409 int err;
1410
1411 /* timer signal */
1412 sigfillset(&act.sa_mask);
1413 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001414 act.sa_handler = host_alarm_handler;
1415
1416 sigaction(SIGALRM, &act, NULL);
1417
1418 itv.it_interval.tv_sec = 0;
1419 /* for i386 kernel 2.6 to get 1 ms */
1420 itv.it_interval.tv_usec = 999;
1421 itv.it_value.tv_sec = 0;
1422 itv.it_value.tv_usec = 10 * 1000;
1423
1424 err = setitimer(ITIMER_REAL, &itv, NULL);
1425 if (err)
1426 return -1;
1427
1428 return 0;
1429}
1430
1431static void unix_stop_timer(struct qemu_alarm_timer *t)
1432{
1433 struct itimerval itv;
1434
1435 memset(&itv, 0, sizeof(itv));
1436 setitimer(ITIMER_REAL, &itv, NULL);
1437}
1438
bellard829309c2004-05-20 13:20:12 +00001439#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001440
aliguorif49e58d2008-11-05 21:22:34 +00001441
thsc8994012007-08-19 21:56:03 +00001442#ifdef _WIN32
1443
1444static int win32_start_timer(struct qemu_alarm_timer *t)
1445{
1446 TIMECAPS tc;
1447 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001448 UINT flags;
thsc8994012007-08-19 21:56:03 +00001449
thsc8994012007-08-19 21:56:03 +00001450 memset(&tc, 0, sizeof(tc));
1451 timeGetDevCaps(&tc, sizeof(tc));
1452
1453 if (data->period < tc.wPeriodMin)
1454 data->period = tc.wPeriodMin;
1455
1456 timeBeginPeriod(data->period);
1457
thsefe75412007-08-24 01:36:32 +00001458 flags = TIME_CALLBACK_FUNCTION;
1459 if (alarm_has_dynticks(t))
1460 flags |= TIME_ONESHOT;
1461 else
1462 flags |= TIME_PERIODIC;
1463
thsc8994012007-08-19 21:56:03 +00001464 data->timerId = timeSetEvent(1, // interval (ms)
1465 data->period, // resolution
1466 host_alarm_handler, // function
1467 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001468 flags);
thsc8994012007-08-19 21:56:03 +00001469
1470 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001471 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001472 GetLastError());
thsc8994012007-08-19 21:56:03 +00001473 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001474 return -1;
1475 }
1476
thsc8994012007-08-19 21:56:03 +00001477 return 0;
1478}
1479
1480static void win32_stop_timer(struct qemu_alarm_timer *t)
1481{
1482 struct qemu_alarm_win32 *data = t->priv;
1483
1484 timeKillEvent(data->timerId);
1485 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001486}
1487
thsefe75412007-08-24 01:36:32 +00001488static void win32_rearm_timer(struct qemu_alarm_timer *t)
1489{
1490 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001491
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001492 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001493 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1494 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001495 return;
thsefe75412007-08-24 01:36:32 +00001496
thsefe75412007-08-24 01:36:32 +00001497 timeKillEvent(data->timerId);
1498
1499 data->timerId = timeSetEvent(1,
1500 data->period,
1501 host_alarm_handler,
1502 (DWORD)t,
1503 TIME_ONESHOT | TIME_PERIODIC);
1504
1505 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001506 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001507 GetLastError());
thsefe75412007-08-24 01:36:32 +00001508
1509 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001510 exit(1);
1511 }
1512}
1513
thsc8994012007-08-19 21:56:03 +00001514#endif /* _WIN32 */
1515
aliguori7183b4b2008-11-05 20:40:18 +00001516static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001517{
blueswir1223f0d72008-09-30 18:12:18 +00001518 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001519 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001520
thsc8994012007-08-19 21:56:03 +00001521 for (i = 0; alarm_timers[i].name; i++) {
1522 t = &alarm_timers[i];
1523
thsc8994012007-08-19 21:56:03 +00001524 err = t->start(t);
1525 if (!err)
1526 break;
bellard67b915a2004-03-31 23:37:16 +00001527 }
bellardfd872592004-05-12 19:11:15 +00001528
thsc8994012007-08-19 21:56:03 +00001529 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001530 err = -ENOENT;
1531 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001532 }
thsc8994012007-08-19 21:56:03 +00001533
1534 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001535
aliguori6abfbd72008-11-05 20:49:37 +00001536 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001537
1538fail:
aliguori7183b4b2008-11-05 20:40:18 +00001539 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001540}
1541
pbrook9596ebb2007-11-18 01:44:38 +00001542static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001543{
thsc8994012007-08-19 21:56:03 +00001544 alarm_timer->stop(alarm_timer);
1545 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001546}
1547
bellardc4b1fcc2004-03-14 21:44:30 +00001548/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001549/* host time/date access */
1550void qemu_get_timedate(struct tm *tm, int offset)
1551{
1552 time_t ti;
1553 struct tm *ret;
1554
1555 time(&ti);
1556 ti += offset;
1557 if (rtc_date_offset == -1) {
1558 if (rtc_utc)
1559 ret = gmtime(&ti);
1560 else
1561 ret = localtime(&ti);
1562 } else {
1563 ti -= rtc_date_offset;
1564 ret = gmtime(&ti);
1565 }
1566
1567 memcpy(tm, ret, sizeof(struct tm));
1568}
1569
1570int qemu_timedate_diff(struct tm *tm)
1571{
1572 time_t seconds;
1573
1574 if (rtc_date_offset == -1)
1575 if (rtc_utc)
1576 seconds = mktimegm(tm);
1577 else
1578 seconds = mktime(tm);
1579 else
1580 seconds = mktimegm(tm) + rtc_date_offset;
1581
1582 return seconds - time(NULL);
1583}
1584
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001585static void configure_rtc_date_offset(const char *startdate, int legacy)
1586{
1587 time_t rtc_start_date;
1588 struct tm tm;
1589
1590 if (!strcmp(startdate, "now") && legacy) {
1591 rtc_date_offset = -1;
1592 } else {
1593 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1594 &tm.tm_year,
1595 &tm.tm_mon,
1596 &tm.tm_mday,
1597 &tm.tm_hour,
1598 &tm.tm_min,
1599 &tm.tm_sec) == 6) {
1600 /* OK */
1601 } else if (sscanf(startdate, "%d-%d-%d",
1602 &tm.tm_year,
1603 &tm.tm_mon,
1604 &tm.tm_mday) == 3) {
1605 tm.tm_hour = 0;
1606 tm.tm_min = 0;
1607 tm.tm_sec = 0;
1608 } else {
1609 goto date_fail;
1610 }
1611 tm.tm_year -= 1900;
1612 tm.tm_mon--;
1613 rtc_start_date = mktimegm(&tm);
1614 if (rtc_start_date == -1) {
1615 date_fail:
1616 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1617 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1618 exit(1);
1619 }
1620 rtc_date_offset = time(NULL) - rtc_start_date;
1621 }
1622}
1623
1624static void configure_rtc(QemuOpts *opts)
1625{
1626 const char *value;
1627
1628 value = qemu_opt_get(opts, "base");
1629 if (value) {
1630 if (!strcmp(value, "utc")) {
1631 rtc_utc = 1;
1632 } else if (!strcmp(value, "localtime")) {
1633 rtc_utc = 0;
1634 } else {
1635 configure_rtc_date_offset(value, 0);
1636 }
1637 }
Jan Kiszka68752042009-09-15 13:36:04 +02001638 value = qemu_opt_get(opts, "clock");
1639 if (value) {
1640 if (!strcmp(value, "host")) {
1641 rtc_clock = host_clock;
1642 } else if (!strcmp(value, "vm")) {
1643 rtc_clock = vm_clock;
1644 } else {
1645 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1646 exit(1);
1647 }
1648 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001649#ifdef CONFIG_TARGET_I386
1650 value = qemu_opt_get(opts, "driftfix");
1651 if (value) {
1652 if (!strcmp(buf, "slew")) {
1653 rtc_td_hack = 1;
1654 } else if (!strcmp(buf, "none")) {
1655 rtc_td_hack = 0;
1656 } else {
1657 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1658 exit(1);
1659 }
1660 }
1661#endif
1662}
1663
bellardfd1dff42006-02-01 21:29:26 +00001664#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001665static void socket_cleanup(void)
1666{
1667 WSACleanup();
1668}
bellard82c643f2004-07-14 17:28:13 +00001669
bellardfd1dff42006-02-01 21:29:26 +00001670static int socket_init(void)
1671{
1672 WSADATA Data;
1673 int ret, err;
1674
1675 ret = WSAStartup(MAKEWORD(2,2), &Data);
1676 if (ret != 0) {
1677 err = WSAGetLastError();
1678 fprintf(stderr, "WSAStartup: %d\n", err);
1679 return -1;
1680 }
1681 atexit(socket_cleanup);
1682 return 0;
1683}
aurel3264b7b732008-05-05 10:05:31 +00001684#endif
1685
balrog1ae26a12008-09-28 23:19:47 +00001686/***********************************************************/
1687/* Bluetooth support */
1688static int nb_hcis;
1689static int cur_hci;
1690static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001691
balrog1ae26a12008-09-28 23:19:47 +00001692static struct bt_vlan_s {
1693 struct bt_scatternet_s net;
1694 int id;
1695 struct bt_vlan_s *next;
1696} *first_bt_vlan;
1697
1698/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001699static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001700{
1701 struct bt_vlan_s **pvlan, *vlan;
1702 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1703 if (vlan->id == id)
1704 return &vlan->net;
1705 }
1706 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1707 vlan->id = id;
1708 pvlan = &first_bt_vlan;
1709 while (*pvlan != NULL)
1710 pvlan = &(*pvlan)->next;
1711 *pvlan = vlan;
1712 return &vlan->net;
1713}
1714
1715static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1716{
1717}
1718
1719static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1720{
1721 return -ENOTSUP;
1722}
1723
1724static struct HCIInfo null_hci = {
1725 .cmd_send = null_hci_send,
1726 .sco_send = null_hci_send,
1727 .acl_send = null_hci_send,
1728 .bdaddr_set = null_hci_addr_set,
1729};
1730
1731struct HCIInfo *qemu_next_hci(void)
1732{
1733 if (cur_hci == nb_hcis)
1734 return &null_hci;
1735
1736 return hci_table[cur_hci++];
1737}
1738
balrogdc72ac12008-11-09 00:04:26 +00001739static struct HCIInfo *hci_init(const char *str)
1740{
1741 char *endp;
1742 struct bt_scatternet_s *vlan = 0;
1743
1744 if (!strcmp(str, "null"))
1745 /* null */
1746 return &null_hci;
1747 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1748 /* host[:hciN] */
1749 return bt_host_hci(str[4] ? str + 5 : "hci0");
1750 else if (!strncmp(str, "hci", 3)) {
1751 /* hci[,vlan=n] */
1752 if (str[3]) {
1753 if (!strncmp(str + 3, ",vlan=", 6)) {
1754 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1755 if (*endp)
1756 vlan = 0;
1757 }
1758 } else
1759 vlan = qemu_find_bt_vlan(0);
1760 if (vlan)
1761 return bt_new_hci(vlan);
1762 }
1763
1764 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1765
1766 return 0;
1767}
1768
1769static int bt_hci_parse(const char *str)
1770{
1771 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001772 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001773
1774 if (nb_hcis >= MAX_NICS) {
1775 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1776 return -1;
1777 }
1778
1779 hci = hci_init(str);
1780 if (!hci)
1781 return -1;
1782
1783 bdaddr.b[0] = 0x52;
1784 bdaddr.b[1] = 0x54;
1785 bdaddr.b[2] = 0x00;
1786 bdaddr.b[3] = 0x12;
1787 bdaddr.b[4] = 0x34;
1788 bdaddr.b[5] = 0x56 + nb_hcis;
1789 hci->bdaddr_set(hci, bdaddr.b);
1790
1791 hci_table[nb_hcis++] = hci;
1792
1793 return 0;
1794}
1795
1796static void bt_vhci_add(int vlan_id)
1797{
1798 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1799
1800 if (!vlan->slave)
1801 fprintf(stderr, "qemu: warning: adding a VHCI to "
1802 "an empty scatternet %i\n", vlan_id);
1803
1804 bt_vhci_init(bt_new_hci(vlan));
1805}
1806
1807static struct bt_device_s *bt_device_add(const char *opt)
1808{
1809 struct bt_scatternet_s *vlan;
1810 int vlan_id = 0;
1811 char *endp = strstr(opt, ",vlan=");
1812 int len = (endp ? endp - opt : strlen(opt)) + 1;
1813 char devname[10];
1814
1815 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1816
1817 if (endp) {
1818 vlan_id = strtol(endp + 6, &endp, 0);
1819 if (*endp) {
1820 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1821 return 0;
1822 }
1823 }
1824
1825 vlan = qemu_find_bt_vlan(vlan_id);
1826
1827 if (!vlan->slave)
1828 fprintf(stderr, "qemu: warning: adding a slave device to "
1829 "an empty scatternet %i\n", vlan_id);
1830
1831 if (!strcmp(devname, "keyboard"))
1832 return bt_keyboard_init(vlan);
1833
1834 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1835 return 0;
1836}
1837
1838static int bt_parse(const char *opt)
1839{
1840 const char *endp, *p;
1841 int vlan;
1842
1843 if (strstart(opt, "hci", &endp)) {
1844 if (!*endp || *endp == ',') {
1845 if (*endp)
1846 if (!strstart(endp, ",vlan=", 0))
1847 opt = endp + 1;
1848
1849 return bt_hci_parse(opt);
1850 }
1851 } else if (strstart(opt, "vhci", &endp)) {
1852 if (!*endp || *endp == ',') {
1853 if (*endp) {
1854 if (strstart(endp, ",vlan=", &p)) {
1855 vlan = strtol(p, (char **) &endp, 0);
1856 if (*endp) {
1857 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1858 return 1;
1859 }
1860 } else {
1861 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1862 return 1;
1863 }
1864 } else
1865 vlan = 0;
1866
1867 bt_vhci_add(vlan);
1868 return 0;
1869 }
1870 } else if (strstart(opt, "device:", &endp))
1871 return !bt_device_add(endp);
1872
1873 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1874 return 1;
1875}
1876
balrog1ae26a12008-09-28 23:19:47 +00001877/***********************************************************/
1878/* QEMU Block devices */
1879
balrog609497a2008-01-14 02:56:53 +00001880#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001881#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001882#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001883#define PFLASH_ALIAS "if=pflash"
1884#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001885#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001886
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001887QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001888{
1889 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001890 char optstr[1024];
1891 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001892
thse4bcb142007-12-02 04:51:10 +00001893 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001894 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001895 va_end(ap);
1896
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001897 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001898 if (!opts) {
1899 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1900 __FUNCTION__, optstr);
1901 return NULL;
1902 }
1903 if (file)
1904 qemu_opt_set(opts, "file", file);
1905 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001906}
1907
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001908DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001909{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001910 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001911
1912 /* seek interface, bus and unit */
1913
Blue Swirl72cf2d42009-09-12 07:36:22 +00001914 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001915 if (dinfo->type == type &&
1916 dinfo->bus == bus &&
1917 dinfo->unit == unit)
1918 return dinfo;
1919 }
thse4bcb142007-12-02 04:51:10 +00001920
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001921 return NULL;
thse4bcb142007-12-02 04:51:10 +00001922}
1923
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001924DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001925{
1926 DriveInfo *dinfo;
1927
Blue Swirl72cf2d42009-09-12 07:36:22 +00001928 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001929 if (strcmp(id, dinfo->id))
1930 continue;
1931 return dinfo;
1932 }
1933 return NULL;
1934}
1935
thsf60d39b2007-12-17 03:55:57 +00001936int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001937{
1938 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001939 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001940
1941 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001942 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001943 if(dinfo->type == type &&
1944 dinfo->bus > max_bus)
1945 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001946 }
1947 return max_bus;
1948}
1949
aliguorifa879c62009-01-07 17:32:33 +00001950const char *drive_get_serial(BlockDriverState *bdrv)
1951{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001952 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001953
Blue Swirl72cf2d42009-09-12 07:36:22 +00001954 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001955 if (dinfo->bdrv == bdrv)
1956 return dinfo->serial;
1957 }
aliguorifa879c62009-01-07 17:32:33 +00001958
1959 return "\0";
1960}
1961
Kevin Wolff7850092009-11-27 13:25:36 +01001962BlockInterfaceErrorAction drive_get_on_error(
1963 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +00001964{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001965 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001966
Kevin Wolffc072ec2009-11-27 13:25:36 +01001967 if (is_read) {
1968 return BLOCK_ERR_REPORT;
1969 }
1970
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)
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001973 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001974 }
aliguori428c5702009-01-21 18:59:04 +00001975
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001976 return is_read ? BLOCK_ERR_REPORT : 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
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001992static int parse_block_error_action(const char *buf, int is_read)
1993{
1994 if (!strcmp(buf, "ignore")) {
1995 return BLOCK_ERR_IGNORE;
1996 } else if (!is_read && !strcmp(buf, "enospc")) {
1997 return BLOCK_ERR_STOP_ENOSPC;
1998 } else if (!strcmp(buf, "stop")) {
1999 return BLOCK_ERR_STOP_ANY;
2000 } else if (!strcmp(buf, "report")) {
2001 return BLOCK_ERR_REPORT;
2002 } else {
2003 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
2004 buf, is_read ? "read" : "write");
2005 return -1;
2006 }
2007}
2008
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002009DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002010 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00002011{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002012 const char *buf;
2013 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00002014 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002015 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00002016 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002017 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002018 enum { MEDIA_DISK, MEDIA_CDROM } media;
2019 int bus_id, unit_id;
2020 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00002021 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002022 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002023 int max_devs;
2024 int index;
balrog33f00272007-12-24 14:33:24 +00002025 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002026 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002027 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002028 int bdrv_flags;
2029 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002030 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002031 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002032 int snapshot = 0;
2033
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002034 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002035
thse4bcb142007-12-02 04:51:10 +00002036 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002037 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002038
Gerd Hoffmann4d007812009-08-31 14:23:57 +02002039 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002040 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002041 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002042 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002043 } else {
thsf60d39b2007-12-17 03:55:57 +00002044 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002045 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002046 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002047 }
2048 media = MEDIA_DISK;
2049
2050 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002051 bus_id = qemu_opt_get_number(opts, "bus", 0);
2052 unit_id = qemu_opt_get_number(opts, "unit", -1);
2053 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002054
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002055 cyls = qemu_opt_get_number(opts, "cyls", 0);
2056 heads = qemu_opt_get_number(opts, "heads", 0);
2057 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002058
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002059 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +02002060 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +00002061
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002062 file = qemu_opt_get(opts, "file");
2063 serial = qemu_opt_get(opts, "serial");
2064
2065 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002066 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002067 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002068 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002069 max_devs = MAX_IDE_DEVS;
2070 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002071 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002072 max_devs = MAX_SCSI_DEVS;
2073 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002074 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002075 max_devs = 0;
2076 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002077 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002078 max_devs = 0;
2079 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002080 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002081 max_devs = 0;
2082 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002083 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002084 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002085 } else if (!strcmp(buf, "virtio")) {
2086 type = IF_VIRTIO;
2087 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002088 } else if (!strcmp(buf, "xen")) {
2089 type = IF_XEN;
2090 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002091 } else if (!strcmp(buf, "none")) {
2092 type = IF_NONE;
2093 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002094 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002095 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002096 return NULL;
thse4bcb142007-12-02 04:51:10 +00002097 }
2098 }
2099
thse4bcb142007-12-02 04:51:10 +00002100 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +00002101 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002102 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002103 return NULL;
thse4bcb142007-12-02 04:51:10 +00002104 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002105 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002106 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002107 return NULL;
thse4bcb142007-12-02 04:51:10 +00002108 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002109 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002110 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002111 return NULL;
thse4bcb142007-12-02 04:51:10 +00002112 }
2113 }
2114
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002115 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002116 if (!cyls) {
2117 fprintf(stderr,
2118 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002119 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002120 return NULL;
thse4bcb142007-12-02 04:51:10 +00002121 }
2122 if (!strcmp(buf, "none"))
2123 translation = BIOS_ATA_TRANSLATION_NONE;
2124 else if (!strcmp(buf, "lba"))
2125 translation = BIOS_ATA_TRANSLATION_LBA;
2126 else if (!strcmp(buf, "auto"))
2127 translation = BIOS_ATA_TRANSLATION_AUTO;
2128 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002129 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002130 return NULL;
thse4bcb142007-12-02 04:51:10 +00002131 }
2132 }
2133
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002134 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002135 if (!strcmp(buf, "disk")) {
2136 media = MEDIA_DISK;
2137 } else if (!strcmp(buf, "cdrom")) {
2138 if (cyls || secs || heads) {
2139 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002140 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002141 return NULL;
thse4bcb142007-12-02 04:51:10 +00002142 }
2143 media = MEDIA_CDROM;
2144 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002145 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002146 return NULL;
thse4bcb142007-12-02 04:51:10 +00002147 }
2148 }
2149
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002150 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002151 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002152 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002153 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002154 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002155 else if (!strcmp(buf, "writeback"))
2156 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002157 else {
2158 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002159 return NULL;
balrog33f00272007-12-24 14:33:24 +00002160 }
2161 }
2162
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002163#ifdef CONFIG_LINUX_AIO
2164 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2165 if (!strcmp(buf, "threads"))
2166 aio = 0;
2167 else if (!strcmp(buf, "native"))
2168 aio = 1;
2169 else {
2170 fprintf(stderr, "qemu: invalid aio option\n");
2171 return NULL;
2172 }
2173 }
2174#endif
2175
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002176 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002177 if (strcmp(buf, "?") == 0) {
2178 fprintf(stderr, "qemu: Supported formats:");
2179 bdrv_iterate_format(bdrv_format_print, NULL);
2180 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002181 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002182 }
Markus Armbrustereb852012009-10-27 18:41:44 +01002183 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00002184 if (!drv) {
2185 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002186 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002187 }
2188 }
2189
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002190 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002191 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002192 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002193 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002194 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002195 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002196
2197 on_write_error = parse_block_error_action(buf, 0);
2198 if (on_write_error < 0) {
2199 return NULL;
2200 }
2201 }
2202
2203 on_read_error = BLOCK_ERR_REPORT;
2204 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01002205 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002206 fprintf(stderr, "rerror is no supported by this format\n");
2207 return NULL;
2208 }
2209
2210 on_read_error = parse_block_error_action(buf, 1);
2211 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002212 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002213 }
2214 }
2215
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002216 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002217 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002218 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002219 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002220 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002221 }
2222
thse4bcb142007-12-02 04:51:10 +00002223 /* compute bus and unit according index */
2224
2225 if (index != -1) {
2226 if (bus_id != 0 || unit_id != -1) {
2227 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002228 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002229 return NULL;
thse4bcb142007-12-02 04:51:10 +00002230 }
2231 if (max_devs == 0)
2232 {
2233 unit_id = index;
2234 bus_id = 0;
2235 } else {
2236 unit_id = index % max_devs;
2237 bus_id = index / max_devs;
2238 }
2239 }
2240
2241 /* if user doesn't specify a unit_id,
2242 * try to find the first free
2243 */
2244
2245 if (unit_id == -1) {
2246 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002247 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002248 unit_id++;
2249 if (max_devs && unit_id >= max_devs) {
2250 unit_id -= max_devs;
2251 bus_id++;
2252 }
2253 }
2254 }
2255
2256 /* check unit id */
2257
2258 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002259 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2260 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002261 return NULL;
thse4bcb142007-12-02 04:51:10 +00002262 }
2263
2264 /*
2265 * ignore multiple definitions
2266 */
2267
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002268 if (drive_get(type, bus_id, unit_id) != NULL) {
2269 *fatal_error = 0;
2270 return NULL;
2271 }
thse4bcb142007-12-02 04:51:10 +00002272
2273 /* init */
2274
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002275 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002276 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002277 dinfo->id = qemu_strdup(buf);
2278 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002279 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002280 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002281 if (type == IF_IDE || type == IF_SCSI)
2282 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2283 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002284 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002285 devname, bus_id, mediastr, unit_id);
2286 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002287 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002288 devname, mediastr, unit_id);
2289 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002290 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002291 dinfo->devaddr = devaddr;
2292 dinfo->type = type;
2293 dinfo->bus = bus_id;
2294 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002295 dinfo->on_read_error = on_read_error;
2296 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002297 dinfo->opts = opts;
2298 if (serial)
2299 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002300 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002301
thsf60d39b2007-12-17 03:55:57 +00002302 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002303 case IF_IDE:
2304 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002305 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002306 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002307 switch(media) {
2308 case MEDIA_DISK:
2309 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002310 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2311 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002312 }
2313 break;
2314 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002315 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002316 break;
2317 }
2318 break;
2319 case IF_SD:
2320 /* FIXME: This isn't really a floppy, but it's a reasonable
2321 approximation. */
2322 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002323 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002324 break;
2325 case IF_PFLASH:
2326 case IF_MTD:
2327 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002328 case IF_VIRTIO:
2329 /* add virtio block device */
2330 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2331 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2332 qemu_opt_set(opts, "drive", dinfo->id);
2333 if (devaddr)
2334 qemu_opt_set(opts, "addr", devaddr);
2335 break;
Paul Brookaae94602009-05-14 22:35:06 +01002336 case IF_COUNT:
2337 abort();
thse4bcb142007-12-02 04:51:10 +00002338 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002339 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002340 *fatal_error = 0;
2341 return NULL;
2342 }
balrog33f00272007-12-24 14:33:24 +00002343 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002344 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002345 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002346 cache = 2; /* always use write-back with snapshot */
2347 }
2348 if (cache == 0) /* no caching */
2349 bdrv_flags |= BDRV_O_NOCACHE;
2350 else if (cache == 2) /* write-back */
2351 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002352
2353 if (aio == 1) {
2354 bdrv_flags |= BDRV_O_NATIVE_AIO;
2355 } else {
2356 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2357 }
2358
Naphtali Sprei59f26892009-10-26 16:25:16 +02002359 if (ro == 1) {
2360 if (type == IF_IDE) {
2361 fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
2362 return NULL;
2363 }
2364 (void)bdrv_set_read_only(dinfo->bdrv, 1);
2365 }
2366
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002367 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002368 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2369 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002370 return NULL;
thse4bcb142007-12-02 04:51:10 +00002371 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002372
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002373 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002374 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002375 *fatal_error = 0;
2376 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002377}
2378
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002379static int drive_init_func(QemuOpts *opts, void *opaque)
2380{
2381 QEMUMachine *machine = opaque;
2382 int fatal_error = 0;
2383
2384 if (drive_init(opts, machine, &fatal_error) == NULL) {
2385 if (fatal_error)
2386 return 1;
2387 }
2388 return 0;
2389}
2390
2391static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2392{
2393 if (NULL == qemu_opt_get(opts, "snapshot")) {
2394 qemu_opt_set(opts, "snapshot", "on");
2395 }
2396 return 0;
2397}
2398
Jan Kiszka76e30d02009-07-02 00:19:02 +02002399void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2400{
2401 boot_set_handler = func;
2402 boot_set_opaque = opaque;
2403}
2404
2405int qemu_boot_set(const char *boot_devices)
2406{
2407 if (!boot_set_handler) {
2408 return -EINVAL;
2409 }
2410 return boot_set_handler(boot_set_opaque, boot_devices);
2411}
2412
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002413static int parse_bootdevices(char *devices)
2414{
2415 /* We just do some generic consistency checks */
2416 const char *p;
2417 int bitmap = 0;
2418
2419 for (p = devices; *p != '\0'; p++) {
2420 /* Allowed boot devices are:
2421 * a-b: floppy disk drives
2422 * c-f: IDE disk drives
2423 * g-m: machine implementation dependant drives
2424 * n-p: network devices
2425 * It's up to each machine implementation to check if the given boot
2426 * devices match the actual hardware implementation and firmware
2427 * features.
2428 */
2429 if (*p < 'a' || *p > 'p') {
2430 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2431 exit(1);
2432 }
2433 if (bitmap & (1 << (*p - 'a'))) {
2434 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2435 exit(1);
2436 }
2437 bitmap |= 1 << (*p - 'a');
2438 }
2439 return bitmap;
2440}
2441
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002442static void restore_boot_devices(void *opaque)
2443{
2444 char *standard_boot_devices = opaque;
2445
2446 qemu_boot_set(standard_boot_devices);
2447
2448 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2449 qemu_free(standard_boot_devices);
2450}
2451
aliguori268a3622009-04-21 22:30:27 +00002452static void numa_add(const char *optarg)
2453{
2454 char option[128];
2455 char *endptr;
2456 unsigned long long value, endvalue;
2457 int nodenr;
2458
2459 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2460 if (!strcmp(option, "node")) {
2461 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2462 nodenr = nb_numa_nodes;
2463 } else {
2464 nodenr = strtoull(option, NULL, 10);
2465 }
2466
2467 if (get_param_value(option, 128, "mem", optarg) == 0) {
2468 node_mem[nodenr] = 0;
2469 } else {
2470 value = strtoull(option, &endptr, 0);
2471 switch (*endptr) {
2472 case 0: case 'M': case 'm':
2473 value <<= 20;
2474 break;
2475 case 'G': case 'g':
2476 value <<= 30;
2477 break;
2478 }
2479 node_mem[nodenr] = value;
2480 }
2481 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2482 node_cpumask[nodenr] = 0;
2483 } else {
2484 value = strtoull(option, &endptr, 10);
2485 if (value >= 64) {
2486 value = 63;
2487 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2488 } else {
2489 if (*endptr == '-') {
2490 endvalue = strtoull(endptr+1, &endptr, 10);
2491 if (endvalue >= 63) {
2492 endvalue = 62;
2493 fprintf(stderr,
2494 "only 63 CPUs in NUMA mode supported.\n");
2495 }
2496 value = (1 << (endvalue + 1)) - (1 << value);
2497 } else {
2498 value = 1 << value;
2499 }
2500 }
2501 node_cpumask[nodenr] = value;
2502 }
2503 nb_numa_nodes++;
2504 }
2505 return;
2506}
2507
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002508static void smp_parse(const char *optarg)
2509{
2510 int smp, sockets = 0, threads = 0, cores = 0;
2511 char *endptr;
2512 char option[128];
2513
2514 smp = strtoul(optarg, &endptr, 10);
2515 if (endptr != optarg) {
2516 if (*endptr == ',') {
2517 endptr++;
2518 }
2519 }
2520 if (get_param_value(option, 128, "sockets", endptr) != 0)
2521 sockets = strtoull(option, NULL, 10);
2522 if (get_param_value(option, 128, "cores", endptr) != 0)
2523 cores = strtoull(option, NULL, 10);
2524 if (get_param_value(option, 128, "threads", endptr) != 0)
2525 threads = strtoull(option, NULL, 10);
2526 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2527 max_cpus = strtoull(option, NULL, 10);
2528
2529 /* compute missing values, prefer sockets over cores over threads */
2530 if (smp == 0 || sockets == 0) {
2531 sockets = sockets > 0 ? sockets : 1;
2532 cores = cores > 0 ? cores : 1;
2533 threads = threads > 0 ? threads : 1;
2534 if (smp == 0) {
2535 smp = cores * threads * sockets;
2536 } else {
2537 sockets = smp / (cores * threads);
2538 }
2539 } else {
2540 if (cores == 0) {
2541 threads = threads > 0 ? threads : 1;
2542 cores = smp / (sockets * threads);
2543 } else {
2544 if (sockets == 0) {
2545 sockets = smp / (cores * threads);
2546 } else {
2547 threads = smp / (cores * sockets);
2548 }
2549 }
2550 }
2551 smp_cpus = smp;
2552 smp_cores = cores > 0 ? cores : 1;
2553 smp_threads = threads > 0 ? threads : 1;
2554 if (max_cpus == 0)
2555 max_cpus = smp_cpus;
2556}
2557
bellard330d0412003-07-26 18:11:40 +00002558/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002559/* USB devices */
2560
aliguoric0f4ce72009-03-05 23:01:01 +00002561static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002562{
2563 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002564 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00002565
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002566 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002567 return -1;
2568
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01002569 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2570 dev = usbdevice_create(devname);
2571 if (dev)
2572 goto done;
2573
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002574 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002575 if (strstart(devname, "host:", &p)) {
2576 dev = usb_host_device_open(p);
balrog6c9f8862008-07-17 20:47:13 +00002577 } else if (strstart(devname, "net:", &p)) {
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002578 QemuOpts *opts;
2579 int idx;
balrog6c9f8862008-07-17 20:47:13 +00002580
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002581 opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
2582 if (!opts) {
balrog6c9f8862008-07-17 20:47:13 +00002583 return -1;
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002584 }
2585
2586 qemu_opt_set(opts, "type", "nic");
2587 qemu_opt_set(opts, "model", "usb");
2588
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01002589 idx = net_client_init(NULL, opts, 0);
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002590 if (idx == -1) {
2591 return -1;
2592 }
2593
2594 dev = usb_net_init(&nd_table[idx]);
balrogdc72ac12008-11-09 00:04:26 +00002595 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2596 dev = usb_bt_init(devname[2] ? hci_init(p) :
2597 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002598 } else {
2599 return -1;
2600 }
pbrook0d92ed32006-05-21 16:30:15 +00002601 if (!dev)
2602 return -1;
2603
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002604done:
bellarda594cfb2005-11-06 16:13:29 +00002605 return 0;
2606}
2607
aliguori1f3870a2008-08-21 19:27:48 +00002608static int usb_device_del(const char *devname)
2609{
2610 int bus_num, addr;
2611 const char *p;
2612
aliguori5d0c5752008-09-14 01:07:41 +00002613 if (strstart(devname, "host:", &p))
2614 return usb_host_device_close(p);
2615
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002616 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002617 return -1;
2618
2619 p = strchr(devname, '.');
2620 if (!p)
2621 return -1;
2622 bus_num = strtoul(devname, NULL, 0);
2623 addr = strtoul(p + 1, NULL, 0);
2624
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002625 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002626}
2627
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002628static int usb_parse(const char *cmdline)
2629{
2630 return usb_device_add(cmdline, 0);
2631}
2632
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002633void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002634{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002635 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002636}
2637
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002638void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002639{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002640 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002641}
2642
bellardf7cce892004-12-08 22:21:25 +00002643/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002644/* PCMCIA/Cardbus */
2645
2646static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002647 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002648 struct pcmcia_socket_entry_s *next;
2649} *pcmcia_sockets = 0;
2650
Paul Brookbc24a222009-05-10 01:44:56 +01002651void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002652{
2653 struct pcmcia_socket_entry_s *entry;
2654
2655 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2656 entry->socket = socket;
2657 entry->next = pcmcia_sockets;
2658 pcmcia_sockets = entry;
2659}
2660
Paul Brookbc24a222009-05-10 01:44:56 +01002661void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002662{
2663 struct pcmcia_socket_entry_s *entry, **ptr;
2664
2665 ptr = &pcmcia_sockets;
2666 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2667 if (entry->socket == socket) {
2668 *ptr = entry->next;
2669 qemu_free(entry);
2670 }
2671}
2672
aliguori376253e2009-03-05 23:01:23 +00002673void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002674{
2675 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002676
balrog201a51f2007-04-30 00:51:09 +00002677 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002678 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002679
2680 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002681 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2682 iter->socket->attached ? iter->socket->card_string :
2683 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002684}
2685
2686/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002687/* register display */
2688
aliguori7b5d76d2009-03-13 15:02:13 +00002689struct DisplayAllocator default_allocator = {
2690 defaultallocator_create_displaysurface,
2691 defaultallocator_resize_displaysurface,
2692 defaultallocator_free_displaysurface
2693};
2694
aliguori3023f332009-01-16 19:04:14 +00002695void register_displaystate(DisplayState *ds)
2696{
2697 DisplayState **s;
2698 s = &display_state;
2699 while (*s != NULL)
2700 s = &(*s)->next;
2701 ds->next = NULL;
2702 *s = ds;
2703}
2704
2705DisplayState *get_displaystate(void)
2706{
2707 return display_state;
2708}
2709
aliguori7b5d76d2009-03-13 15:02:13 +00002710DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2711{
2712 if(ds->allocator == &default_allocator) ds->allocator = da;
2713 return ds->allocator;
2714}
2715
ths2ff89792007-06-21 23:34:19 +00002716/* dumb display */
2717
aliguori8f391ab2009-01-19 16:34:10 +00002718static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002719{
aliguori8f391ab2009-01-19 16:34:10 +00002720 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002721 ds->allocator = &default_allocator;
2722 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002723 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002724}
2725
2726/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002727/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002728
bellardc4b1fcc2004-03-14 21:44:30 +00002729typedef struct IOHandlerRecord {
2730 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002731 IOCanRWHandler *fd_read_poll;
2732 IOHandler *fd_read;
2733 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002734 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002735 void *opaque;
2736 /* temporary data */
2737 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002738 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002739} IOHandlerRecord;
2740
bellard8a7ddc32004-03-31 19:00:16 +00002741static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002742
bellard7c9d8e02005-11-15 22:16:05 +00002743/* XXX: fd_read_poll should be suppressed, but an API change is
2744 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002745int qemu_set_fd_handler2(int fd,
2746 IOCanRWHandler *fd_read_poll,
2747 IOHandler *fd_read,
2748 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002749 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002750{
bellard8a7ddc32004-03-31 19:00:16 +00002751 IOHandlerRecord **pioh, *ioh;
2752
bellard7c9d8e02005-11-15 22:16:05 +00002753 if (!fd_read && !fd_write) {
2754 pioh = &first_io_handler;
2755 for(;;) {
2756 ioh = *pioh;
2757 if (ioh == NULL)
2758 break;
2759 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002760 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002761 break;
2762 }
2763 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002764 }
bellard7c9d8e02005-11-15 22:16:05 +00002765 } else {
2766 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2767 if (ioh->fd == fd)
2768 goto found;
2769 }
2770 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002771 ioh->next = first_io_handler;
2772 first_io_handler = ioh;
2773 found:
2774 ioh->fd = fd;
2775 ioh->fd_read_poll = fd_read_poll;
2776 ioh->fd_read = fd_read;
2777 ioh->fd_write = fd_write;
2778 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002779 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002780 }
bellard7c9d8e02005-11-15 22:16:05 +00002781 return 0;
2782}
2783
ths5fafdf22007-09-16 21:08:06 +00002784int qemu_set_fd_handler(int fd,
2785 IOHandler *fd_read,
2786 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002787 void *opaque)
2788{
2789 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002790}
2791
aliguori56f3a5d2008-10-31 18:07:17 +00002792#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002793/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002794/* Polling handling */
2795
2796typedef struct PollingEntry {
2797 PollingFunc *func;
2798 void *opaque;
2799 struct PollingEntry *next;
2800} PollingEntry;
2801
2802static PollingEntry *first_polling_entry;
2803
2804int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2805{
2806 PollingEntry **ppe, *pe;
2807 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002808 pe->func = func;
2809 pe->opaque = opaque;
2810 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2811 *ppe = pe;
2812 return 0;
2813}
2814
2815void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2816{
2817 PollingEntry **ppe, *pe;
2818 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2819 pe = *ppe;
2820 if (pe->func == func && pe->opaque == opaque) {
2821 *ppe = pe->next;
2822 qemu_free(pe);
2823 break;
2824 }
2825 }
2826}
2827
bellarda18e5242006-06-25 17:18:27 +00002828/***********************************************************/
2829/* Wait objects support */
2830typedef struct WaitObjects {
2831 int num;
2832 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2833 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2834 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2835} WaitObjects;
2836
2837static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002838
bellarda18e5242006-06-25 17:18:27 +00002839int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2840{
2841 WaitObjects *w = &wait_objects;
2842
2843 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2844 return -1;
2845 w->events[w->num] = handle;
2846 w->func[w->num] = func;
2847 w->opaque[w->num] = opaque;
2848 w->num++;
2849 return 0;
2850}
2851
2852void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2853{
2854 int i, found;
2855 WaitObjects *w = &wait_objects;
2856
2857 found = 0;
2858 for (i = 0; i < w->num; i++) {
2859 if (w->events[i] == handle)
2860 found = 1;
2861 if (found) {
2862 w->events[i] = w->events[i + 1];
2863 w->func[i] = w->func[i + 1];
2864 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002865 }
bellarda18e5242006-06-25 17:18:27 +00002866 }
2867 if (found)
2868 w->num--;
2869}
2870#endif
2871
bellard8a7ddc32004-03-31 19:00:16 +00002872/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002873/* ram save/restore */
2874
Juan Quintela94fb0902009-09-10 03:04:23 +02002875#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002876#define RAM_SAVE_FLAG_COMPRESS 0x02
2877#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2878#define RAM_SAVE_FLAG_PAGE 0x08
2879#define RAM_SAVE_FLAG_EOS 0x10
2880
2881static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002882{
aliguori475e4272008-10-06 20:21:51 +00002883 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2884 uint32_t *array = (uint32_t *)page;
2885 int i;
ths3b46e622007-09-17 08:09:54 +00002886
aliguori475e4272008-10-06 20:21:51 +00002887 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2888 if (array[i] != val)
2889 return 0;
bellardc88676f2006-08-06 13:36:11 +00002890 }
aliguori475e4272008-10-06 20:21:51 +00002891
2892 return 1;
bellardc88676f2006-08-06 13:36:11 +00002893}
2894
aliguori475e4272008-10-06 20:21:51 +00002895static int ram_save_block(QEMUFile *f)
2896{
Anthony Liguoric227f092009-10-01 16:12:16 -05002897 static ram_addr_t current_addr = 0;
2898 ram_addr_t saved_addr = current_addr;
2899 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002900 int found = 0;
2901
pbrook94a6b542009-04-11 17:15:54 +00002902 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002903 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002904 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002905
2906 cpu_physical_memory_reset_dirty(current_addr,
2907 current_addr + TARGET_PAGE_SIZE,
2908 MIGRATION_DIRTY_FLAG);
2909
pbrook5579c7f2009-04-11 14:47:08 +00002910 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002911
pbrook5579c7f2009-04-11 14:47:08 +00002912 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002913 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002914 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002915 } else {
2916 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002917 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002918 }
2919
2920 found = 1;
2921 break;
2922 }
2923 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002924 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002925 }
2926
2927 return found;
2928}
2929
Jan Kiszka84307932009-12-02 11:29:38 +01002930static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00002931
Anthony Liguoric227f092009-10-01 16:12:16 -05002932static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002933{
Anthony Liguoric227f092009-10-01 16:12:16 -05002934 ram_addr_t addr;
2935 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002936
pbrook94a6b542009-04-11 17:15:54 +00002937 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002938 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2939 count++;
2940 }
2941
2942 return count;
2943}
2944
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002945uint64_t ram_bytes_remaining(void)
2946{
2947 return ram_save_remaining() * TARGET_PAGE_SIZE;
2948}
2949
2950uint64_t ram_bytes_transferred(void)
2951{
2952 return bytes_transferred;
2953}
2954
2955uint64_t ram_bytes_total(void)
2956{
2957 return last_ram_offset;
2958}
2959
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002960static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00002961{
Anthony Liguoric227f092009-10-01 16:12:16 -05002962 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002963 uint64_t bytes_transferred_last;
2964 double bwidth = 0;
2965 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002966
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002967 if (stage < 0) {
2968 cpu_physical_memory_set_dirty_tracking(0);
2969 return 0;
2970 }
2971
Jan Kiszka9fa06382009-05-22 23:51:45 +02002972 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002973 qemu_file_set_error(f);
2974 return 0;
2975 }
2976
aliguori475e4272008-10-06 20:21:51 +00002977 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01002978 bytes_transferred = 0;
2979
aliguori475e4272008-10-06 20:21:51 +00002980 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002981 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002982 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2983 cpu_physical_memory_set_dirty(addr);
2984 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002985
aliguori475e4272008-10-06 20:21:51 +00002986 /* Enable dirty memory tracking */
2987 cpu_physical_memory_set_dirty_tracking(1);
2988
pbrook94a6b542009-04-11 17:15:54 +00002989 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002990 }
2991
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002992 bytes_transferred_last = bytes_transferred;
2993 bwidth = get_clock();
2994
aliguori475e4272008-10-06 20:21:51 +00002995 while (!qemu_file_rate_limit(f)) {
2996 int ret;
2997
2998 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002999 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003000 if (ret == 0) /* no more blocks */
3001 break;
3002 }
3003
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003004 bwidth = get_clock() - bwidth;
3005 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3006
3007 /* if we haven't transferred anything this round, force expected_time to a
3008 * a very high value, but without crashing */
3009 if (bwidth == 0)
3010 bwidth = 0.000001;
3011
aliguori475e4272008-10-06 20:21:51 +00003012 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00003013 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003014 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003015 while (ram_save_block(f) != 0) {
3016 bytes_transferred += TARGET_PAGE_SIZE;
3017 }
aliguori8215e912009-04-05 19:30:55 +00003018 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003019 }
3020
3021 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3022
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003023 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3024
3025 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003026}
3027
aliguori475e4272008-10-06 20:21:51 +00003028static int ram_load(QEMUFile *f, void *opaque, int version_id)
3029{
Anthony Liguoric227f092009-10-01 16:12:16 -05003030 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003031 int flags;
3032
aliguori475e4272008-10-06 20:21:51 +00003033 if (version_id != 3)
3034 return -EINVAL;
3035
3036 do {
3037 addr = qemu_get_be64(f);
3038
3039 flags = addr & ~TARGET_PAGE_MASK;
3040 addr &= TARGET_PAGE_MASK;
3041
3042 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003043 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003044 return -EINVAL;
3045 }
3046
aliguori475e4272008-10-06 20:21:51 +00003047 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3048 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003049 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3050#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003051 if (ch == 0 &&
3052 (!kvm_enabled() || kvm_has_sync_mmu())) {
3053 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003054 }
Anthony Liguori30868442009-06-17 16:46:12 -05003055#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01003056 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00003057 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01003058 }
3059 if (qemu_file_has_error(f)) {
3060 return -EIO;
3061 }
aliguori475e4272008-10-06 20:21:51 +00003062 } while (!(flags & RAM_SAVE_FLAG_EOS));
3063
bellardc88676f2006-08-06 13:36:11 +00003064 return 0;
3065}
3066
aliguori9e472e12008-10-08 19:50:24 +00003067void qemu_service_io(void)
3068{
aliguorid9f75a42009-04-24 18:03:11 +00003069 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003070}
3071
bellard8a7ddc32004-03-31 19:00:16 +00003072/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003073/* machine registration */
3074
blueswir1bdaf78e2008-10-04 07:24:27 +00003075static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003076QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003077
3078int qemu_register_machine(QEMUMachine *m)
3079{
3080 QEMUMachine **pm;
3081 pm = &first_machine;
3082 while (*pm != NULL)
3083 pm = &(*pm)->next;
3084 m->next = NULL;
3085 *pm = m;
3086 return 0;
3087}
3088
pbrook9596ebb2007-11-18 01:44:38 +00003089static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003090{
3091 QEMUMachine *m;
3092
3093 for(m = first_machine; m != NULL; m = m->next) {
3094 if (!strcmp(m->name, name))
3095 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003096 if (m->alias && !strcmp(m->alias, name))
3097 return m;
bellardcc1daa42005-06-05 14:49:17 +00003098 }
3099 return NULL;
3100}
3101
Anthony Liguori0c257432009-05-21 20:41:01 -05003102static QEMUMachine *find_default_machine(void)
3103{
3104 QEMUMachine *m;
3105
3106 for(m = first_machine; m != NULL; m = m->next) {
3107 if (m->is_default) {
3108 return m;
3109 }
3110 }
3111 return NULL;
3112}
3113
bellardcc1daa42005-06-05 14:49:17 +00003114/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003115/* main execution loop */
3116
pbrook9596ebb2007-11-18 01:44:38 +00003117static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003118{
aliguori7d957bd2009-01-15 22:14:11 +00003119 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003120 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003121 DisplayChangeListener *dcl = ds->listeners;
3122
3123 dpy_refresh(ds);
3124
3125 while (dcl != NULL) {
3126 if (dcl->gui_timer_interval &&
3127 dcl->gui_timer_interval < interval)
3128 interval = dcl->gui_timer_interval;
3129 dcl = dcl->next;
3130 }
3131 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003132}
3133
blueswir19043b622009-01-21 19:28:13 +00003134static void nographic_update(void *opaque)
3135{
3136 uint64_t interval = GUI_REFRESH_INTERVAL;
3137
3138 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3139}
3140
bellard0bd48852005-11-11 00:00:47 +00003141struct vm_change_state_entry {
3142 VMChangeStateHandler *cb;
3143 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003144 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003145};
3146
Blue Swirl72cf2d42009-09-12 07:36:22 +00003147static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003148
3149VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3150 void *opaque)
3151{
3152 VMChangeStateEntry *e;
3153
3154 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003155
3156 e->cb = cb;
3157 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003158 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003159 return e;
3160}
3161
3162void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3163{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003164 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003165 qemu_free (e);
3166}
3167
aliguori9781e042009-01-22 17:15:29 +00003168static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003169{
3170 VMChangeStateEntry *e;
3171
3172 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003173 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003174 }
3175}
3176
aliguorid6dc3d42009-04-24 18:04:07 +00003177static void resume_all_vcpus(void);
3178static void pause_all_vcpus(void);
3179
bellard8a7ddc32004-03-31 19:00:16 +00003180void vm_start(void)
3181{
3182 if (!vm_running) {
3183 cpu_enable_ticks();
3184 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003185 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003186 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003187 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003188 }
3189}
3190
bellardbb0c6722004-06-20 12:37:32 +00003191/* reset/shutdown handler */
3192
3193typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003194 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003195 QEMUResetHandler *func;
3196 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003197} QEMUResetEntry;
3198
Blue Swirl72cf2d42009-09-12 07:36:22 +00003199static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3200 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003201static int reset_requested;
3202static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003203static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003204static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003205static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003206
aurel32cf7a2fe2008-03-18 06:53:05 +00003207int qemu_shutdown_requested(void)
3208{
3209 int r = shutdown_requested;
3210 shutdown_requested = 0;
3211 return r;
3212}
3213
3214int qemu_reset_requested(void)
3215{
3216 int r = reset_requested;
3217 reset_requested = 0;
3218 return r;
3219}
3220
3221int qemu_powerdown_requested(void)
3222{
3223 int r = powerdown_requested;
3224 powerdown_requested = 0;
3225 return r;
3226}
3227
aliguorie5689022009-04-24 18:03:54 +00003228static int qemu_debug_requested(void)
3229{
3230 int r = debug_requested;
3231 debug_requested = 0;
3232 return r;
3233}
3234
aliguori6e29f5d2009-04-24 18:04:02 +00003235static int qemu_vmstop_requested(void)
3236{
3237 int r = vmstop_requested;
3238 vmstop_requested = 0;
3239 return r;
3240}
3241
3242static void do_vm_stop(int reason)
3243{
3244 if (vm_running) {
3245 cpu_disable_ticks();
3246 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003247 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003248 vm_state_notify(0, reason);
3249 }
3250}
3251
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003252void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003253{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003254 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003255
bellardbb0c6722004-06-20 12:37:32 +00003256 re->func = func;
3257 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003258 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003259}
3260
Jan Kiszkadda9b292009-07-02 00:19:02 +02003261void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003262{
3263 QEMUResetEntry *re;
3264
Blue Swirl72cf2d42009-09-12 07:36:22 +00003265 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003266 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003267 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003268 qemu_free(re);
3269 return;
3270 }
3271 }
3272}
3273
3274void qemu_system_reset(void)
3275{
3276 QEMUResetEntry *re, *nre;
3277
3278 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003279 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003280 re->func(re->opaque);
3281 }
3282}
3283
3284void qemu_system_reset_request(void)
3285{
bellardd1beab82006-10-02 19:44:22 +00003286 if (no_reboot) {
3287 shutdown_requested = 1;
3288 } else {
3289 reset_requested = 1;
3290 }
aliguorid9f75a42009-04-24 18:03:11 +00003291 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003292}
3293
3294void qemu_system_shutdown_request(void)
3295{
3296 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003297 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003298}
3299
bellard34751872005-07-02 14:31:34 +00003300void qemu_system_powerdown_request(void)
3301{
3302 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003303 qemu_notify_event();
3304}
3305
aliguorid6dc3d42009-04-24 18:04:07 +00003306#ifdef CONFIG_IOTHREAD
3307static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003308{
aliguorid6dc3d42009-04-24 18:04:07 +00003309 vmstop_requested = reason;
3310 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003311}
aliguorid6dc3d42009-04-24 18:04:07 +00003312#endif
bellardbb0c6722004-06-20 12:37:32 +00003313
aliguori50317c72009-04-24 18:03:29 +00003314#ifndef _WIN32
3315static int io_thread_fd = -1;
3316
3317static void qemu_event_increment(void)
3318{
3319 static const char byte = 0;
3320
3321 if (io_thread_fd == -1)
3322 return;
3323
3324 write(io_thread_fd, &byte, sizeof(byte));
3325}
3326
3327static void qemu_event_read(void *opaque)
3328{
3329 int fd = (unsigned long)opaque;
3330 ssize_t len;
3331
3332 /* Drain the notify pipe */
3333 do {
3334 char buffer[512];
3335 len = read(fd, buffer, sizeof(buffer));
3336 } while ((len == -1 && errno == EINTR) || len > 0);
3337}
3338
3339static int qemu_event_init(void)
3340{
3341 int err;
3342 int fds[2];
3343
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003344 err = qemu_pipe(fds);
aliguori50317c72009-04-24 18:03:29 +00003345 if (err == -1)
3346 return -errno;
3347
3348 err = fcntl_setfl(fds[0], O_NONBLOCK);
3349 if (err < 0)
3350 goto fail;
3351
3352 err = fcntl_setfl(fds[1], O_NONBLOCK);
3353 if (err < 0)
3354 goto fail;
3355
3356 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3357 (void *)(unsigned long)fds[0]);
3358
3359 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003360 return 0;
3361
aliguori50317c72009-04-24 18:03:29 +00003362fail:
3363 close(fds[0]);
3364 close(fds[1]);
3365 return err;
3366}
3367#else
3368HANDLE qemu_event_handle;
3369
3370static void dummy_event_handler(void *opaque)
3371{
3372}
3373
3374static int qemu_event_init(void)
3375{
3376 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3377 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003378 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003379 return -1;
3380 }
3381 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3382 return 0;
3383}
3384
3385static void qemu_event_increment(void)
3386{
malcde1c90c2009-09-27 14:38:18 +04003387 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003388 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003389 GetLastError());
3390 exit (1);
3391 }
aliguori50317c72009-04-24 18:03:29 +00003392}
3393#endif
3394
aliguorid6dc3d42009-04-24 18:04:07 +00003395static int cpu_can_run(CPUState *env)
3396{
3397 if (env->stop)
3398 return 0;
3399 if (env->stopped)
3400 return 0;
3401 return 1;
3402}
3403
3404#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003405static int qemu_init_main_loop(void)
3406{
aliguori50317c72009-04-24 18:03:29 +00003407 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003408}
3409
aliguori0bf46a42009-04-24 18:03:41 +00003410void qemu_init_vcpu(void *_env)
3411{
3412 CPUState *env = _env;
3413
3414 if (kvm_enabled())
3415 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003416 env->nr_cores = smp_cores;
3417 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003418 return;
3419}
3420
aliguori8edac962009-04-24 18:03:45 +00003421int qemu_cpu_self(void *env)
3422{
3423 return 1;
3424}
3425
aliguorid6dc3d42009-04-24 18:04:07 +00003426static void resume_all_vcpus(void)
3427{
3428}
3429
3430static void pause_all_vcpus(void)
3431{
3432}
3433
aliguori8edac962009-04-24 18:03:45 +00003434void qemu_cpu_kick(void *env)
3435{
3436 return;
3437}
3438
aliguorid6dc3d42009-04-24 18:04:07 +00003439void qemu_notify_event(void)
3440{
3441 CPUState *env = cpu_single_env;
3442
3443 if (env) {
3444 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003445 }
aliguorid6dc3d42009-04-24 18:04:07 +00003446}
3447
Glauber Costad549db52009-10-07 16:38:03 -03003448void qemu_mutex_lock_iothread(void) {}
3449void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00003450
aliguori6e29f5d2009-04-24 18:04:02 +00003451void vm_stop(int reason)
3452{
3453 do_vm_stop(reason);
3454}
3455
aliguorid6dc3d42009-04-24 18:04:07 +00003456#else /* CONFIG_IOTHREAD */
3457
3458#include "qemu-thread.h"
3459
3460QemuMutex qemu_global_mutex;
3461static QemuMutex qemu_fair_mutex;
3462
3463static QemuThread io_thread;
3464
3465static QemuThread *tcg_cpu_thread;
3466static QemuCond *tcg_halt_cond;
3467
3468static int qemu_system_ready;
3469/* cpu creation */
3470static QemuCond qemu_cpu_cond;
3471/* system init */
3472static QemuCond qemu_system_cond;
3473static QemuCond qemu_pause_cond;
3474
3475static void block_io_signals(void);
3476static void unblock_io_signals(void);
3477static int tcg_has_work(void);
3478
3479static int qemu_init_main_loop(void)
3480{
3481 int ret;
3482
3483 ret = qemu_event_init();
3484 if (ret)
3485 return ret;
3486
3487 qemu_cond_init(&qemu_pause_cond);
3488 qemu_mutex_init(&qemu_fair_mutex);
3489 qemu_mutex_init(&qemu_global_mutex);
3490 qemu_mutex_lock(&qemu_global_mutex);
3491
3492 unblock_io_signals();
3493 qemu_thread_self(&io_thread);
3494
3495 return 0;
3496}
3497
3498static void qemu_wait_io_event(CPUState *env)
3499{
3500 while (!tcg_has_work())
3501 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3502
3503 qemu_mutex_unlock(&qemu_global_mutex);
3504
3505 /*
3506 * Users of qemu_global_mutex can be starved, having no chance
3507 * to acquire it since this path will get to it first.
3508 * So use another lock to provide fairness.
3509 */
3510 qemu_mutex_lock(&qemu_fair_mutex);
3511 qemu_mutex_unlock(&qemu_fair_mutex);
3512
3513 qemu_mutex_lock(&qemu_global_mutex);
3514 if (env->stop) {
3515 env->stop = 0;
3516 env->stopped = 1;
3517 qemu_cond_signal(&qemu_pause_cond);
3518 }
3519}
3520
3521static int qemu_cpu_exec(CPUState *env);
3522
3523static void *kvm_cpu_thread_fn(void *arg)
3524{
3525 CPUState *env = arg;
3526
3527 block_io_signals();
3528 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003529 if (kvm_enabled())
3530 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003531
3532 /* signal CPU creation */
3533 qemu_mutex_lock(&qemu_global_mutex);
3534 env->created = 1;
3535 qemu_cond_signal(&qemu_cpu_cond);
3536
3537 /* and wait for machine initialization */
3538 while (!qemu_system_ready)
3539 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3540
3541 while (1) {
3542 if (cpu_can_run(env))
3543 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003544 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003545 }
3546
3547 return NULL;
3548}
3549
3550static void tcg_cpu_exec(void);
3551
3552static void *tcg_cpu_thread_fn(void *arg)
3553{
3554 CPUState *env = arg;
3555
3556 block_io_signals();
3557 qemu_thread_self(env->thread);
3558
3559 /* signal CPU creation */
3560 qemu_mutex_lock(&qemu_global_mutex);
3561 for (env = first_cpu; env != NULL; env = env->next_cpu)
3562 env->created = 1;
3563 qemu_cond_signal(&qemu_cpu_cond);
3564
3565 /* and wait for machine initialization */
3566 while (!qemu_system_ready)
3567 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3568
3569 while (1) {
3570 tcg_cpu_exec();
3571 qemu_wait_io_event(cur_cpu);
3572 }
3573
3574 return NULL;
3575}
3576
3577void qemu_cpu_kick(void *_env)
3578{
3579 CPUState *env = _env;
3580 qemu_cond_broadcast(env->halt_cond);
3581 if (kvm_enabled())
3582 qemu_thread_signal(env->thread, SIGUSR1);
3583}
3584
Glauber Costae5bc2012009-09-28 15:27:44 -03003585int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003586{
Glauber Costae5bc2012009-09-28 15:27:44 -03003587 CPUState *env = _env;
3588 QemuThread this;
3589
3590 qemu_thread_self(&this);
3591
3592 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003593}
3594
3595static void cpu_signal(int sig)
3596{
3597 if (cpu_single_env)
3598 cpu_exit(cpu_single_env);
3599}
3600
3601static void block_io_signals(void)
3602{
3603 sigset_t set;
3604 struct sigaction sigact;
3605
3606 sigemptyset(&set);
3607 sigaddset(&set, SIGUSR2);
3608 sigaddset(&set, SIGIO);
3609 sigaddset(&set, SIGALRM);
3610 pthread_sigmask(SIG_BLOCK, &set, NULL);
3611
3612 sigemptyset(&set);
3613 sigaddset(&set, SIGUSR1);
3614 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3615
3616 memset(&sigact, 0, sizeof(sigact));
3617 sigact.sa_handler = cpu_signal;
3618 sigaction(SIGUSR1, &sigact, NULL);
3619}
3620
3621static void unblock_io_signals(void)
3622{
3623 sigset_t set;
3624
3625 sigemptyset(&set);
3626 sigaddset(&set, SIGUSR2);
3627 sigaddset(&set, SIGIO);
3628 sigaddset(&set, SIGALRM);
3629 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3630
3631 sigemptyset(&set);
3632 sigaddset(&set, SIGUSR1);
3633 pthread_sigmask(SIG_BLOCK, &set, NULL);
3634}
3635
3636static void qemu_signal_lock(unsigned int msecs)
3637{
3638 qemu_mutex_lock(&qemu_fair_mutex);
3639
3640 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3641 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3642 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3643 break;
3644 }
3645 qemu_mutex_unlock(&qemu_fair_mutex);
3646}
3647
Glauber Costad549db52009-10-07 16:38:03 -03003648void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003649{
3650 if (kvm_enabled()) {
3651 qemu_mutex_lock(&qemu_fair_mutex);
3652 qemu_mutex_lock(&qemu_global_mutex);
3653 qemu_mutex_unlock(&qemu_fair_mutex);
3654 } else
3655 qemu_signal_lock(100);
3656}
3657
Glauber Costad549db52009-10-07 16:38:03 -03003658void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003659{
3660 qemu_mutex_unlock(&qemu_global_mutex);
3661}
3662
3663static int all_vcpus_paused(void)
3664{
3665 CPUState *penv = first_cpu;
3666
3667 while (penv) {
3668 if (!penv->stopped)
3669 return 0;
3670 penv = (CPUState *)penv->next_cpu;
3671 }
3672
3673 return 1;
3674}
3675
3676static void pause_all_vcpus(void)
3677{
3678 CPUState *penv = first_cpu;
3679
3680 while (penv) {
3681 penv->stop = 1;
3682 qemu_thread_signal(penv->thread, SIGUSR1);
3683 qemu_cpu_kick(penv);
3684 penv = (CPUState *)penv->next_cpu;
3685 }
3686
3687 while (!all_vcpus_paused()) {
3688 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3689 penv = first_cpu;
3690 while (penv) {
3691 qemu_thread_signal(penv->thread, SIGUSR1);
3692 penv = (CPUState *)penv->next_cpu;
3693 }
3694 }
3695}
3696
3697static void resume_all_vcpus(void)
3698{
3699 CPUState *penv = first_cpu;
3700
3701 while (penv) {
3702 penv->stop = 0;
3703 penv->stopped = 0;
3704 qemu_thread_signal(penv->thread, SIGUSR1);
3705 qemu_cpu_kick(penv);
3706 penv = (CPUState *)penv->next_cpu;
3707 }
3708}
3709
3710static void tcg_init_vcpu(void *_env)
3711{
3712 CPUState *env = _env;
3713 /* share a single thread for all cpus with TCG */
3714 if (!tcg_cpu_thread) {
3715 env->thread = qemu_mallocz(sizeof(QemuThread));
3716 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3717 qemu_cond_init(env->halt_cond);
3718 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3719 while (env->created == 0)
3720 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3721 tcg_cpu_thread = env->thread;
3722 tcg_halt_cond = env->halt_cond;
3723 } else {
3724 env->thread = tcg_cpu_thread;
3725 env->halt_cond = tcg_halt_cond;
3726 }
3727}
3728
3729static void kvm_start_vcpu(CPUState *env)
3730{
aliguorid6dc3d42009-04-24 18:04:07 +00003731 env->thread = qemu_mallocz(sizeof(QemuThread));
3732 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3733 qemu_cond_init(env->halt_cond);
3734 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3735 while (env->created == 0)
3736 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3737}
3738
3739void qemu_init_vcpu(void *_env)
3740{
3741 CPUState *env = _env;
3742
3743 if (kvm_enabled())
3744 kvm_start_vcpu(env);
3745 else
3746 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003747 env->nr_cores = smp_cores;
3748 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003749}
3750
3751void qemu_notify_event(void)
3752{
3753 qemu_event_increment();
3754}
3755
3756void vm_stop(int reason)
3757{
3758 QemuThread me;
3759 qemu_thread_self(&me);
3760
3761 if (!qemu_thread_equal(&me, &io_thread)) {
3762 qemu_system_vmstop_request(reason);
3763 /*
3764 * FIXME: should not return to device code in case
3765 * vm_stop() has been requested.
3766 */
3767 if (cpu_single_env) {
3768 cpu_exit(cpu_single_env);
3769 cpu_single_env->stop = 1;
3770 }
3771 return;
3772 }
3773 do_vm_stop(reason);
3774}
3775
3776#endif
3777
3778
ths877cf882007-04-18 18:11:47 +00003779#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003780static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003781{
3782 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003783 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003784
bellardf3311102006-04-12 20:21:17 +00003785
3786 /* XXX: need to suppress polling by better using win32 events */
3787 ret = 0;
3788 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3789 ret |= pe->func(pe->opaque);
3790 }
thse6b1e552007-04-18 17:56:02 +00003791 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003792 int err;
3793 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003794
aliguori56f3a5d2008-10-31 18:07:17 +00003795 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003796 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3797 if (w->func[ret - WAIT_OBJECT_0])
3798 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003799
ths5fafdf22007-09-16 21:08:06 +00003800 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003801 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003802
thse6b1e552007-04-18 17:56:02 +00003803 /* Check if event is signaled */
3804 ret2 = WaitForSingleObject(w->events[i], 0);
3805 if(ret2 == WAIT_OBJECT_0) {
3806 if (w->func[i])
3807 w->func[i](w->opaque[i]);
3808 } else if (ret2 == WAIT_TIMEOUT) {
3809 } else {
3810 err = GetLastError();
3811 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003812 }
3813 }
bellarda18e5242006-06-25 17:18:27 +00003814 } else if (ret == WAIT_TIMEOUT) {
3815 } else {
3816 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003817 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003818 }
bellardf3311102006-04-12 20:21:17 +00003819 }
aliguori56f3a5d2008-10-31 18:07:17 +00003820
3821 *timeout = 0;
3822}
3823#else
blueswir169d64512008-12-07 19:30:18 +00003824static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003825{
3826}
bellardfd1dff42006-02-01 21:29:26 +00003827#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003828
3829void main_loop_wait(int timeout)
3830{
3831 IOHandlerRecord *ioh;
3832 fd_set rfds, wfds, xfds;
3833 int ret, nfds;
3834 struct timeval tv;
3835
3836 qemu_bh_update_timeout(&timeout);
3837
3838 host_main_loop_wait(&timeout);
3839
bellardfd1dff42006-02-01 21:29:26 +00003840 /* poll any events */
3841 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003842 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003843 FD_ZERO(&rfds);
3844 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003845 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003846 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003847 if (ioh->deleted)
3848 continue;
bellardfd1dff42006-02-01 21:29:26 +00003849 if (ioh->fd_read &&
3850 (!ioh->fd_read_poll ||
3851 ioh->fd_read_poll(ioh->opaque) != 0)) {
3852 FD_SET(ioh->fd, &rfds);
3853 if (ioh->fd > nfds)
3854 nfds = ioh->fd;
3855 }
3856 if (ioh->fd_write) {
3857 FD_SET(ioh->fd, &wfds);
3858 if (ioh->fd > nfds)
3859 nfds = ioh->fd;
3860 }
3861 }
ths3b46e622007-09-17 08:09:54 +00003862
aliguori56f3a5d2008-10-31 18:07:17 +00003863 tv.tv_sec = timeout / 1000;
3864 tv.tv_usec = (timeout % 1000) * 1000;
3865
Jan Kiszkad918f232009-06-24 14:42:30 +02003866 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3867
aliguori48708522009-04-24 18:03:49 +00003868 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003869 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003870 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003871 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003872 IOHandlerRecord **pioh;
3873
3874 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003875 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003876 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003877 }
ths6ab43fd2007-08-25 01:34:19 +00003878 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003879 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003880 }
3881 }
thscafffd42007-02-28 21:59:44 +00003882
3883 /* remove deleted IO handlers */
3884 pioh = &first_io_handler;
3885 while (*pioh) {
3886 ioh = *pioh;
3887 if (ioh->deleted) {
3888 *pioh = ioh->next;
3889 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003890 } else
thscafffd42007-02-28 21:59:44 +00003891 pioh = &ioh->next;
3892 }
bellardfd1dff42006-02-01 21:29:26 +00003893 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003894
3895 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003896
aliguori50317c72009-04-24 18:03:29 +00003897 /* rearm timer, if not periodic */
3898 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3899 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3900 qemu_rearm_alarm_timer(alarm_timer);
3901 }
3902
aliguori357c6922008-11-25 17:26:09 +00003903 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003904 if (vm_running) {
3905 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003906 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
3907 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00003908 }
aliguori357c6922008-11-25 17:26:09 +00003909
3910 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003911 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00003912 qemu_get_clock(rt_clock));
3913
Jan Kiszka21d5d122009-09-15 13:36:04 +02003914 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
3915 qemu_get_clock(host_clock));
3916
pbrook423f0742007-05-23 00:06:54 +00003917 /* Check bottom-halves last in case any of the earlier events triggered
3918 them. */
3919 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003920
bellard5905b2e2004-08-01 21:53:26 +00003921}
3922
aliguori43b96852009-04-24 18:03:33 +00003923static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003924{
aliguori43b96852009-04-24 18:03:33 +00003925 int ret;
bellard89bfc102006-02-08 22:46:31 +00003926#ifdef CONFIG_PROFILER
3927 int64_t ti;
3928#endif
aliguori43b96852009-04-24 18:03:33 +00003929
3930#ifdef CONFIG_PROFILER
3931 ti = profile_getclock();
3932#endif
3933 if (use_icount) {
3934 int64_t count;
3935 int decr;
3936 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3937 env->icount_decr.u16.low = 0;
3938 env->icount_extra = 0;
3939 count = qemu_next_deadline();
3940 count = (count + (1 << icount_time_shift) - 1)
3941 >> icount_time_shift;
3942 qemu_icount += count;
3943 decr = (count > 0xffff) ? 0xffff : count;
3944 count -= decr;
3945 env->icount_decr.u16.low = decr;
3946 env->icount_extra = count;
3947 }
3948 ret = cpu_exec(env);
3949#ifdef CONFIG_PROFILER
3950 qemu_time += profile_getclock() - ti;
3951#endif
3952 if (use_icount) {
3953 /* Fold pending instructions back into the
3954 instruction counter, and clear the interrupt flag. */
3955 qemu_icount -= (env->icount_decr.u16.low
3956 + env->icount_extra);
3957 env->icount_decr.u32 = 0;
3958 env->icount_extra = 0;
3959 }
3960 return ret;
3961}
3962
aliguorie6e35b12009-04-24 18:03:57 +00003963static void tcg_cpu_exec(void)
3964{
aliguorid6dc3d42009-04-24 18:04:07 +00003965 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00003966
3967 if (next_cpu == NULL)
3968 next_cpu = first_cpu;
3969 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3970 CPUState *env = cur_cpu = next_cpu;
3971
3972 if (!vm_running)
3973 break;
3974 if (timer_alarm_pending) {
3975 timer_alarm_pending = 0;
3976 break;
3977 }
aliguorid6dc3d42009-04-24 18:04:07 +00003978 if (cpu_can_run(env))
3979 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00003980 if (ret == EXCP_DEBUG) {
3981 gdb_set_stop_cpu(env);
3982 debug_requested = 1;
3983 break;
3984 }
3985 }
3986}
3987
aliguori43b96852009-04-24 18:03:33 +00003988static int cpu_has_work(CPUState *env)
3989{
aliguorid6dc3d42009-04-24 18:04:07 +00003990 if (env->stop)
3991 return 1;
3992 if (env->stopped)
3993 return 0;
aliguori43b96852009-04-24 18:03:33 +00003994 if (!env->halted)
3995 return 1;
3996 if (qemu_cpu_has_work(env))
3997 return 1;
3998 return 0;
3999}
4000
4001static int tcg_has_work(void)
4002{
bellard6a00d602005-11-21 23:25:50 +00004003 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004004
aliguori43b96852009-04-24 18:03:33 +00004005 for (env = first_cpu; env != NULL; env = env->next_cpu)
4006 if (cpu_has_work(env))
4007 return 1;
4008 return 0;
4009}
bellard15a76442005-11-23 21:01:03 +00004010
aliguori43b96852009-04-24 18:03:33 +00004011static int qemu_calculate_timeout(void)
4012{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004013#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004014 int timeout;
bellard15a76442005-11-23 21:01:03 +00004015
aliguori43b96852009-04-24 18:03:33 +00004016 if (!vm_running)
4017 timeout = 5000;
4018 else if (tcg_has_work())
4019 timeout = 0;
4020 else if (!use_icount)
4021 timeout = 5000;
4022 else {
4023 /* XXX: use timeout computed from timers */
4024 int64_t add;
4025 int64_t delta;
4026 /* Advance virtual time to the next event. */
4027 if (use_icount == 1) {
4028 /* When not using an adaptive execution frequency
4029 we tend to get badly out of sync with real time,
4030 so just delay for a reasonable amount of time. */
4031 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004032 } else {
aliguori43b96852009-04-24 18:03:33 +00004033 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004034 }
aliguori43b96852009-04-24 18:03:33 +00004035 if (delta > 0) {
4036 /* If virtual time is ahead of real time then just
4037 wait for IO. */
4038 timeout = (delta / 1000000) + 1;
4039 } else {
4040 /* Wait for either IO to occur or the next
4041 timer event. */
4042 add = qemu_next_deadline();
4043 /* We advance the timer before checking for IO.
4044 Limit the amount we advance so that early IO
4045 activity won't get the guest too far ahead. */
4046 if (add > 10000000)
4047 add = 10000000;
4048 delta += add;
4049 add = (add + (1 << icount_time_shift) - 1)
4050 >> icount_time_shift;
4051 qemu_icount += add;
4052 timeout = delta / 1000000;
4053 if (timeout < 0)
4054 timeout = 0;
4055 }
bellardb4608c02003-06-27 17:34:32 +00004056 }
aliguori43b96852009-04-24 18:03:33 +00004057
4058 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004059#else /* CONFIG_IOTHREAD */
4060 return 1000;
4061#endif
aliguori43b96852009-04-24 18:03:33 +00004062}
4063
4064static int vm_can_run(void)
4065{
4066 if (powerdown_requested)
4067 return 0;
4068 if (reset_requested)
4069 return 0;
4070 if (shutdown_requested)
4071 return 0;
aliguorie5689022009-04-24 18:03:54 +00004072 if (debug_requested)
4073 return 0;
aliguori43b96852009-04-24 18:03:33 +00004074 return 1;
4075}
4076
Blue Swirld9c32312009-08-09 08:42:19 +00004077qemu_irq qemu_system_powerdown;
4078
aliguori43b96852009-04-24 18:03:33 +00004079static void main_loop(void)
4080{
aliguori6e29f5d2009-04-24 18:04:02 +00004081 int r;
aliguori43b96852009-04-24 18:03:33 +00004082
aliguorid6dc3d42009-04-24 18:04:07 +00004083#ifdef CONFIG_IOTHREAD
4084 qemu_system_ready = 1;
4085 qemu_cond_broadcast(&qemu_system_cond);
4086#endif
4087
aliguori6e29f5d2009-04-24 18:04:02 +00004088 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004089 do {
4090#ifdef CONFIG_PROFILER
4091 int64_t ti;
4092#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004093#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004094 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004095#endif
aliguori43b96852009-04-24 18:03:33 +00004096#ifdef CONFIG_PROFILER
4097 ti = profile_getclock();
4098#endif
4099 main_loop_wait(qemu_calculate_timeout());
4100#ifdef CONFIG_PROFILER
4101 dev_time += profile_getclock() - ti;
4102#endif
aliguorie5689022009-04-24 18:03:54 +00004103 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004104
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004105 if (qemu_debug_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004106 monitor_protocol_event(QEVENT_DEBUG, NULL);
aliguori43b96852009-04-24 18:03:33 +00004107 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004108 }
aliguori43b96852009-04-24 18:03:33 +00004109 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004110 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00004111 if (no_shutdown) {
4112 vm_stop(0);
4113 no_shutdown = 0;
4114 } else
4115 break;
4116 }
aliguorid6dc3d42009-04-24 18:04:07 +00004117 if (qemu_reset_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004118 monitor_protocol_event(QEVENT_RESET, NULL);
aliguorid6dc3d42009-04-24 18:04:07 +00004119 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004120 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004121 resume_all_vcpus();
4122 }
Blue Swirld9c32312009-08-09 08:42:19 +00004123 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004124 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00004125 qemu_irq_raise(qemu_system_powerdown);
4126 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004127 if ((r = qemu_vmstop_requested())) {
Blue Swirl242cd002009-12-04 18:05:45 +00004128 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00004129 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004130 }
aliguori43b96852009-04-24 18:03:33 +00004131 }
aliguorid6dc3d42009-04-24 18:04:07 +00004132 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004133}
4134
pbrook9bd7e6d2009-04-07 22:58:45 +00004135static void version(void)
4136{
pbrook4a19f1e2009-04-07 23:17:49 +00004137 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004138}
4139
ths15f82202007-06-29 23:26:08 +00004140static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004141{
pbrook9bd7e6d2009-04-07 22:58:45 +00004142 version();
4143 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004144 "\n"
bellarda20dd502003-09-30 21:07:02 +00004145 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004146 "\n"
blueswir15824d652009-03-28 06:44:27 +00004147#define DEF(option, opt_arg, opt_enum, opt_help) \
4148 opt_help
4149#define DEFHEADING(text) stringify(text) "\n"
4150#include "qemu-options.h"
4151#undef DEF
4152#undef DEFHEADING
4153#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004154 "\n"
bellard82c643f2004-07-14 17:28:13 +00004155 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004156 "ctrl-alt-f toggle full screen\n"
4157 "ctrl-alt-n switch to virtual console 'n'\n"
4158 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004159 "\n"
4160 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4161 ,
bellard0db63472003-10-27 21:37:46 +00004162 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004163 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004164#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004165 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004166 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004167#endif
bellard6e44ba72004-01-18 21:56:49 +00004168 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004169 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004170 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004171}
4172
bellardcd6f1162004-05-13 22:02:20 +00004173#define HAS_ARG 0x0001
4174
4175enum {
blueswir15824d652009-03-28 06:44:27 +00004176#define DEF(option, opt_arg, opt_enum, opt_help) \
4177 opt_enum,
4178#define DEFHEADING(text)
4179#include "qemu-options.h"
4180#undef DEF
4181#undef DEFHEADING
4182#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004183};
4184
4185typedef struct QEMUOption {
4186 const char *name;
4187 int flags;
4188 int index;
4189} QEMUOption;
4190
blueswir1dbed7e42008-10-01 19:38:09 +00004191static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004192 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004193#define DEF(option, opt_arg, opt_enum, opt_help) \
4194 { option, opt_arg, opt_enum },
4195#define DEFHEADING(text)
4196#include "qemu-options.h"
4197#undef DEF
4198#undef DEFHEADING
4199#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004200 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004201};
4202
bellard1d14ffa2005-10-30 18:58:22 +00004203#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004204struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004205#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004206#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004207 {
4208 "pcspk",
4209 "PC speaker",
4210 0,
4211 1,
4212 { .init_isa = pcspk_audio_init }
4213 },
4214#endif
malc4c9b53e2009-01-09 10:46:34 +00004215
4216#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004217 {
4218 "sb16",
4219 "Creative Sound Blaster 16",
4220 0,
4221 1,
4222 { .init_isa = SB16_init }
4223 },
malc4c9b53e2009-01-09 10:46:34 +00004224#endif
bellard6a36d842005-12-18 20:34:32 +00004225
malccc53d262008-06-13 10:48:22 +00004226#ifdef CONFIG_CS4231A
4227 {
4228 "cs4231a",
4229 "CS4231A",
4230 0,
4231 1,
4232 { .init_isa = cs4231a_init }
4233 },
4234#endif
4235
bellard6a36d842005-12-18 20:34:32 +00004236#ifdef CONFIG_ADLIB
4237 {
4238 "adlib",
4239#ifdef HAS_YMF262
4240 "Yamaha YMF262 (OPL3)",
4241#else
4242 "Yamaha YM3812 (OPL2)",
4243#endif
4244 0,
4245 1,
4246 { .init_isa = Adlib_init }
4247 },
4248#endif
4249
4250#ifdef CONFIG_GUS
4251 {
4252 "gus",
4253 "Gravis Ultrasound GF1",
4254 0,
4255 1,
4256 { .init_isa = GUS_init }
4257 },
4258#endif
4259
malc4c9b53e2009-01-09 10:46:34 +00004260#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004261 {
4262 "ac97",
4263 "Intel 82801AA AC97 Audio",
4264 0,
4265 0,
4266 { .init_pci = ac97_init }
4267 },
malc4c9b53e2009-01-09 10:46:34 +00004268#endif
balroge5c9a132008-01-14 04:27:55 +00004269
malc4c9b53e2009-01-09 10:46:34 +00004270#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004271 {
4272 "es1370",
4273 "ENSONIQ AudioPCI ES1370",
4274 0,
4275 0,
4276 { .init_pci = es1370_init }
4277 },
balrogb00052e2007-04-30 02:22:06 +00004278#endif
bellard6a36d842005-12-18 20:34:32 +00004279
malc4c9b53e2009-01-09 10:46:34 +00004280#endif /* HAS_AUDIO_CHOICE */
4281
bellard6a36d842005-12-18 20:34:32 +00004282 { NULL, NULL, 0, 0, { NULL } }
4283};
4284
bellard1d14ffa2005-10-30 18:58:22 +00004285static void select_soundhw (const char *optarg)
4286{
bellard6a36d842005-12-18 20:34:32 +00004287 struct soundhw *c;
4288
bellard1d14ffa2005-10-30 18:58:22 +00004289 if (*optarg == '?') {
4290 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004291
bellard1d14ffa2005-10-30 18:58:22 +00004292 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004293 for (c = soundhw; c->name; ++c) {
4294 printf ("%-11s %s\n", c->name, c->descr);
4295 }
4296 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004297 exit (*optarg != '?');
4298 }
4299 else {
bellard6a36d842005-12-18 20:34:32 +00004300 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004301 const char *p;
4302 char *e;
4303 int bad_card = 0;
4304
bellard6a36d842005-12-18 20:34:32 +00004305 if (!strcmp (optarg, "all")) {
4306 for (c = soundhw; c->name; ++c) {
4307 c->enabled = 1;
4308 }
4309 return;
4310 }
bellard1d14ffa2005-10-30 18:58:22 +00004311
bellard6a36d842005-12-18 20:34:32 +00004312 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004313 while (*p) {
4314 e = strchr (p, ',');
4315 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004316
4317 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004318 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004319 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004320 break;
4321 }
4322 }
bellard6a36d842005-12-18 20:34:32 +00004323
4324 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004325 if (l > 80) {
4326 fprintf (stderr,
4327 "Unknown sound card name (too big to show)\n");
4328 }
4329 else {
4330 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4331 (int) l, p);
4332 }
4333 bad_card = 1;
4334 }
4335 p += l + (e != NULL);
4336 }
4337
4338 if (bad_card)
4339 goto show_valid_cards;
4340 }
4341}
4342#endif
4343
malc3893c122008-09-28 00:42:05 +00004344static void select_vgahw (const char *p)
4345{
4346 const char *opts;
4347
Zachary Amsden86176752009-07-30 00:15:02 -10004348 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004349 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004350 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004351 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004352 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004353 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004354 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004355 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004356 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004357 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004358 invalid_vga:
4359 fprintf(stderr, "Unknown vga type: %s\n", p);
4360 exit(1);
4361 }
malccb5a7aa2008-09-28 00:42:12 +00004362 while (*opts) {
4363 const char *nextopt;
4364
4365 if (strstart(opts, ",retrace=", &nextopt)) {
4366 opts = nextopt;
4367 if (strstart(opts, "dumb", &nextopt))
4368 vga_retrace_method = VGA_RETRACE_DUMB;
4369 else if (strstart(opts, "precise", &nextopt))
4370 vga_retrace_method = VGA_RETRACE_PRECISE;
4371 else goto invalid_vga;
4372 } else goto invalid_vga;
4373 opts = nextopt;
4374 }
malc3893c122008-09-28 00:42:05 +00004375}
4376
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004377#ifdef TARGET_I386
4378static int balloon_parse(const char *arg)
4379{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004380 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004381
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004382 if (strcmp(arg, "none") == 0) {
4383 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004384 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004385
4386 if (!strncmp(arg, "virtio", 6)) {
4387 if (arg[6] == ',') {
4388 /* have params -> parse them */
4389 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4390 if (!opts)
4391 return -1;
4392 } else {
4393 /* create empty opts */
4394 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4395 }
4396 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4397 return 0;
4398 }
4399
4400 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004401}
4402#endif
4403
bellard3587d7e2006-06-26 20:03:44 +00004404#ifdef _WIN32
4405static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4406{
4407 exit(STATUS_CONTROL_C_EXIT);
4408 return TRUE;
4409}
4410#endif
4411
aliguoric4be29f2009-04-17 18:58:14 +00004412int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004413{
4414 int ret;
4415
4416 if(strlen(str) != 36)
4417 return -1;
4418
4419 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4420 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4421 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4422
4423 if(ret != 16)
4424 return -1;
4425
aliguorib6f6e3d2009-04-17 18:59:56 +00004426#ifdef TARGET_I386
4427 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4428#endif
4429
blueswir18fcb1b92008-09-18 18:29:08 +00004430 return 0;
4431}
4432
aliguori5b08fc12008-08-21 20:08:03 +00004433#ifndef _WIN32
4434
4435static void termsig_handler(int signal)
4436{
4437 qemu_system_shutdown_request();
4438}
4439
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004440static void sigchld_handler(int signal)
4441{
4442 waitpid(-1, NULL, WNOHANG);
4443}
4444
4445static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004446{
4447 struct sigaction act;
4448
4449 memset(&act, 0, sizeof(act));
4450 act.sa_handler = termsig_handler;
4451 sigaction(SIGINT, &act, NULL);
4452 sigaction(SIGHUP, &act, NULL);
4453 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004454
4455 act.sa_handler = sigchld_handler;
4456 act.sa_flags = SA_NOCLDSTOP;
4457 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004458}
4459
4460#endif
4461
Paul Brook5cea8592009-05-30 00:52:44 +01004462#ifdef _WIN32
4463/* Look for support files in the same directory as the executable. */
4464static char *find_datadir(const char *argv0)
4465{
4466 char *p;
4467 char buf[MAX_PATH];
4468 DWORD len;
4469
4470 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4471 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004472 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004473 }
4474
4475 buf[len] = 0;
4476 p = buf + len - 1;
4477 while (p != buf && *p != '\\')
4478 p--;
4479 *p = 0;
4480 if (access(buf, R_OK) == 0) {
4481 return qemu_strdup(buf);
4482 }
4483 return NULL;
4484}
4485#else /* !_WIN32 */
4486
4487/* Find a likely location for support files using the location of the binary.
4488 For installed binaries this will be "$bindir/../share/qemu". When
4489 running from the build tree this will be "$bindir/../pc-bios". */
4490#define SHARE_SUFFIX "/share/qemu"
4491#define BUILD_SUFFIX "/pc-bios"
4492static char *find_datadir(const char *argv0)
4493{
4494 char *dir;
4495 char *p = NULL;
4496 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004497 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004498 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004499
4500#if defined(__linux__)
4501 {
4502 int len;
4503 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4504 if (len > 0) {
4505 buf[len] = 0;
4506 p = buf;
4507 }
4508 }
4509#elif defined(__FreeBSD__)
4510 {
4511 int len;
4512 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4513 if (len > 0) {
4514 buf[len] = 0;
4515 p = buf;
4516 }
4517 }
4518#endif
4519 /* If we don't have any way of figuring out the actual executable
4520 location then try argv[0]. */
4521 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004522 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004523 if (!p) {
4524 return NULL;
4525 }
4526 }
4527 dir = dirname(p);
4528 dir = dirname(dir);
4529
Blue Swirl3a417592009-06-09 19:12:21 +00004530 max_len = strlen(dir) +
4531 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4532 res = qemu_mallocz(max_len);
4533 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004534 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004535 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004536 if (access(res, R_OK)) {
4537 qemu_free(res);
4538 res = NULL;
4539 }
4540 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004541
Paul Brook5cea8592009-05-30 00:52:44 +01004542 return res;
4543}
4544#undef SHARE_SUFFIX
4545#undef BUILD_SUFFIX
4546#endif
4547
4548char *qemu_find_file(int type, const char *name)
4549{
4550 int len;
4551 const char *subdir;
4552 char *buf;
4553
4554 /* If name contains path separators then try it as a straight path. */
4555 if ((strchr(name, '/') || strchr(name, '\\'))
4556 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004557 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004558 }
4559 switch (type) {
4560 case QEMU_FILE_TYPE_BIOS:
4561 subdir = "";
4562 break;
4563 case QEMU_FILE_TYPE_KEYMAP:
4564 subdir = "keymaps/";
4565 break;
4566 default:
4567 abort();
4568 }
4569 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4570 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004571 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004572 if (access(buf, R_OK)) {
4573 qemu_free(buf);
4574 return NULL;
4575 }
4576 return buf;
4577}
4578
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004579static int device_init_func(QemuOpts *opts, void *opaque)
4580{
4581 DeviceState *dev;
4582
4583 dev = qdev_device_add(opts);
4584 if (!dev)
4585 return -1;
4586 return 0;
4587}
4588
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004589static int chardev_init_func(QemuOpts *opts, void *opaque)
4590{
4591 CharDriverState *chr;
4592
4593 chr = qemu_chr_open_opts(opts, NULL);
4594 if (!chr)
4595 return -1;
4596 return 0;
4597}
4598
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004599struct device_config {
4600 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004601 DEV_USB, /* -usbdevice */
4602 DEV_BT, /* -bt */
4603 } type;
4604 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004605 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004606};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004607QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004608
4609static void add_device_config(int type, const char *cmdline)
4610{
4611 struct device_config *conf;
4612
4613 conf = qemu_mallocz(sizeof(*conf));
4614 conf->type = type;
4615 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004616 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004617}
4618
4619static int foreach_device_config(int type, int (*func)(const char *cmdline))
4620{
4621 struct device_config *conf;
4622 int rc;
4623
Blue Swirl72cf2d42009-09-12 07:36:22 +00004624 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004625 if (conf->type != type)
4626 continue;
4627 rc = func(conf->cmdline);
4628 if (0 != rc)
4629 return rc;
4630 }
4631 return 0;
4632}
4633
malc902b3d52008-12-10 19:18:40 +00004634int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004635{
aliguori59030a82009-04-05 18:43:41 +00004636 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004637 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004638 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004639 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004640 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004641 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05004642 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004643 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004644 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004645 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004646 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004647 int optind;
4648 const char *r, *optarg;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004649 CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
4650 const char *monitor_devices[MAX_MONITOR_DEVICES];
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02004651 int monitor_flags[MAX_MONITOR_DEVICES];
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004652 int monitor_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004653 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004654 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004655 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004656 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004657 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4658 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004659 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004660 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004661 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004662#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004663 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004664#endif
bellard26a5f132008-05-28 12:30:31 +00004665 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004666 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004667 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004668#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004669 int fd = 0;
4670 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004671 const char *chroot_dir = NULL;
4672 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004673#endif
aliguori268a3622009-04-21 22:30:27 +00004674 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004675 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004676
Jan Kiszka68752042009-09-15 13:36:04 +02004677 init_clocks();
4678
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004679 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004680 qemu_cache_utils_init(envp);
4681
Blue Swirl72cf2d42009-09-12 07:36:22 +00004682 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004683#ifndef _WIN32
4684 {
4685 struct sigaction act;
4686 sigfillset(&act.sa_mask);
4687 act.sa_flags = 0;
4688 act.sa_handler = SIG_IGN;
4689 sigaction(SIGPIPE, &act, NULL);
4690 }
bellard3587d7e2006-06-26 20:03:44 +00004691#else
4692 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004693 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4694 QEMU to run on a single CPU */
4695 {
4696 HANDLE h;
4697 DWORD mask, smask;
4698 int i;
4699 h = GetCurrentProcess();
4700 if (GetProcessAffinityMask(h, &mask, &smask)) {
4701 for(i = 0; i < 32; i++) {
4702 if (mask & (1 << i))
4703 break;
4704 }
4705 if (i != 32) {
4706 mask = 1 << i;
4707 SetProcessAffinityMask(h, mask);
4708 }
4709 }
4710 }
bellard67b915a2004-03-31 23:37:16 +00004711#endif
bellardbe995c22006-06-25 16:25:21 +00004712
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004713 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004714 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004715 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004716 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004717 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004718 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004719 kernel_filename = NULL;
4720 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004721 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004722 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004723
Alexander Graf93d434b2009-12-05 12:44:30 +01004724#ifdef TARGET_S390X
4725 for(i = 0; i < MAX_SERIAL_PORTS; i++)
4726 serial_devices[i] = NULL;
4727 serial_device_index = 0;
4728
4729 for(i = 0; i < MAX_PARALLEL_PORTS; i++)
4730 parallel_devices[i] = NULL;
4731 parallel_device_index = 0;
4732
4733 virtio_consoles[0] = "mon:stdio";
4734 for(i = 1; i < MAX_VIRTIO_CONSOLES; i++)
4735 virtio_consoles[i] = NULL;
4736 virtio_console_index = 0;
4737#else
aurel32c75a8232008-05-04 00:50:34 +00004738 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004739 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004740 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004741 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004742
aliguori8290edd2009-02-27 20:14:29 +00004743 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004744 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004745 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004746 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004747
aliguori1b8fc812009-02-27 20:01:39 +00004748 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004749 virtio_consoles[i] = NULL;
4750 virtio_console_index = 0;
Alexander Graf93d434b2009-12-05 12:44:30 +01004751#endif
aliguori9ede2fd2009-01-15 20:05:25 +00004752
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004753 monitor_devices[0] = "vc:80Cx24C";
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02004754 monitor_flags[0] = MONITOR_IS_DEFAULT | MONITOR_USE_READLINE;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004755 for (i = 1; i < MAX_MONITOR_DEVICES; i++) {
4756 monitor_devices[i] = NULL;
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02004757 monitor_flags[i] = MONITOR_USE_READLINE;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004758 }
4759 monitor_device_index = 0;
4760
aliguori268a3622009-04-21 22:30:27 +00004761 for (i = 0; i < MAX_NODES; i++) {
4762 node_mem[i] = 0;
4763 node_cpumask[i] = 0;
4764 }
4765
aliguori268a3622009-04-21 22:30:27 +00004766 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004767 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004768
bellard26a5f132008-05-28 12:30:31 +00004769 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004770 autostart= 1;
4771
bellardcd6f1162004-05-13 22:02:20 +00004772 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004773 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004774 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004775 break;
bellardcd6f1162004-05-13 22:02:20 +00004776 r = argv[optind];
4777 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004778 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004779 } else {
4780 const QEMUOption *popt;
4781
4782 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004783 /* Treat --foo the same as -foo. */
4784 if (r[1] == '-')
4785 r++;
bellardcd6f1162004-05-13 22:02:20 +00004786 popt = qemu_options;
4787 for(;;) {
4788 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004789 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004790 argv[0], r);
4791 exit(1);
4792 }
4793 if (!strcmp(popt->name, r + 1))
4794 break;
4795 popt++;
4796 }
4797 if (popt->flags & HAS_ARG) {
4798 if (optind >= argc) {
4799 fprintf(stderr, "%s: option '%s' requires an argument\n",
4800 argv[0], r);
4801 exit(1);
4802 }
4803 optarg = argv[optind++];
4804 } else {
4805 optarg = NULL;
4806 }
4807
4808 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004809 case QEMU_OPTION_M:
4810 machine = find_machine(optarg);
4811 if (!machine) {
4812 QEMUMachine *m;
4813 printf("Supported machines are:\n");
4814 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004815 if (m->alias)
4816 printf("%-10s %s (alias of %s)\n",
4817 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004818 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004819 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004820 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004821 }
ths15f82202007-06-29 23:26:08 +00004822 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004823 }
4824 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004825 case QEMU_OPTION_cpu:
4826 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004827 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004828/* XXX: implement xxx_cpu_list for targets that still miss it */
4829#if defined(cpu_list)
4830 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004831#endif
ths15f82202007-06-29 23:26:08 +00004832 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004833 } else {
4834 cpu_model = optarg;
4835 }
4836 break;
bellardcd6f1162004-05-13 22:02:20 +00004837 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004838 initrd_filename = optarg;
4839 break;
bellardcd6f1162004-05-13 22:02:20 +00004840 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004841 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004842 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004843 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004844 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004845 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004846 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004847 translation == BIOS_ATA_TRANSLATION_LBA ?
4848 ",trans=lba" :
4849 translation == BIOS_ATA_TRANSLATION_NONE ?
4850 ",trans=none" : "");
4851 break;
bellardcd6f1162004-05-13 22:02:20 +00004852 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004853 case QEMU_OPTION_hdc:
4854 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004855 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004856 break;
thse4bcb142007-12-02 04:51:10 +00004857 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004858 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004859 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004860 case QEMU_OPTION_set:
4861 if (qemu_set_option(optarg) != 0)
4862 exit(1);
4863 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004864 case QEMU_OPTION_global:
4865 if (qemu_global_option(optarg) != 0)
4866 exit(1);
4867 break;
balrog3e3d5812007-04-30 02:09:25 +00004868 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004869 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004870 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004871 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004872 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004873 break;
j_mayer86f55662007-04-24 06:52:59 +00004874 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004875 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004876 break;
bellardcd6f1162004-05-13 22:02:20 +00004877 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004878 snapshot = 1;
4879 break;
bellardcd6f1162004-05-13 22:02:20 +00004880 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004881 {
bellard330d0412003-07-26 18:11:40 +00004882 const char *p;
4883 p = optarg;
4884 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004885 if (cyls < 1 || cyls > 16383)
4886 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004887 if (*p != ',')
4888 goto chs_fail;
4889 p++;
4890 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004891 if (heads < 1 || heads > 16)
4892 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004893 if (*p != ',')
4894 goto chs_fail;
4895 p++;
4896 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004897 if (secs < 1 || secs > 63)
4898 goto chs_fail;
4899 if (*p == ',') {
4900 p++;
4901 if (!strcmp(p, "none"))
4902 translation = BIOS_ATA_TRANSLATION_NONE;
4903 else if (!strcmp(p, "lba"))
4904 translation = BIOS_ATA_TRANSLATION_LBA;
4905 else if (!strcmp(p, "auto"))
4906 translation = BIOS_ATA_TRANSLATION_AUTO;
4907 else
4908 goto chs_fail;
4909 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004910 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004911 fprintf(stderr, "qemu: invalid physical CHS format\n");
4912 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004913 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004914 if (hda_opts != NULL) {
4915 char num[16];
4916 snprintf(num, sizeof(num), "%d", cyls);
4917 qemu_opt_set(hda_opts, "cyls", num);
4918 snprintf(num, sizeof(num), "%d", heads);
4919 qemu_opt_set(hda_opts, "heads", num);
4920 snprintf(num, sizeof(num), "%d", secs);
4921 qemu_opt_set(hda_opts, "secs", num);
4922 if (translation == BIOS_ATA_TRANSLATION_LBA)
4923 qemu_opt_set(hda_opts, "trans", "lba");
4924 if (translation == BIOS_ATA_TRANSLATION_NONE)
4925 qemu_opt_set(hda_opts, "trans", "none");
4926 }
bellard330d0412003-07-26 18:11:40 +00004927 }
4928 break;
aliguori268a3622009-04-21 22:30:27 +00004929 case QEMU_OPTION_numa:
4930 if (nb_numa_nodes >= MAX_NODES) {
4931 fprintf(stderr, "qemu: too many NUMA nodes\n");
4932 exit(1);
4933 }
4934 numa_add(optarg);
4935 break;
bellardcd6f1162004-05-13 22:02:20 +00004936 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004937 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00004938 break;
balrog4d3b6f62008-02-10 16:33:14 +00004939#ifdef CONFIG_CURSES
4940 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004941 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00004942 break;
4943#endif
balroga171fe32007-04-30 01:48:07 +00004944 case QEMU_OPTION_portrait:
4945 graphic_rotate = 1;
4946 break;
bellardcd6f1162004-05-13 22:02:20 +00004947 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004948 kernel_filename = optarg;
4949 break;
bellardcd6f1162004-05-13 22:02:20 +00004950 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004951 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004952 break;
bellardcd6f1162004-05-13 22:02:20 +00004953 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004954 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004955 break;
bellardcd6f1162004-05-13 22:02:20 +00004956 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004957 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004958 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02004959 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004960 };
4961 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004962 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004963 int legacy = 0;
4964
4965 if (!strchr(optarg, '=')) {
4966 legacy = 1;
4967 pstrcpy(buf, sizeof(buf), optarg);
4968 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
4969 fprintf(stderr,
4970 "qemu: unknown boot parameter '%s' in '%s'\n",
4971 buf, optarg);
4972 exit(1);
4973 }
4974
4975 if (legacy ||
4976 get_param_value(buf, sizeof(buf), "order", optarg)) {
4977 boot_devices_bitmap = parse_bootdevices(buf);
4978 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00004979 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004980 if (!legacy) {
4981 if (get_param_value(buf, sizeof(buf),
4982 "once", optarg)) {
4983 boot_devices_bitmap |= parse_bootdevices(buf);
4984 standard_boot_devices = qemu_strdup(boot_devices);
4985 pstrcpy(boot_devices, sizeof(boot_devices), buf);
4986 qemu_register_reset(restore_boot_devices,
4987 standard_boot_devices);
4988 }
Jan Kiszka95387492009-07-02 00:19:02 +02004989 if (get_param_value(buf, sizeof(buf),
4990 "menu", optarg)) {
4991 if (!strcmp(buf, "on")) {
4992 boot_menu = 1;
4993 } else if (!strcmp(buf, "off")) {
4994 boot_menu = 0;
4995 } else {
4996 fprintf(stderr,
4997 "qemu: invalid option value '%s'\n",
4998 buf);
4999 exit(1);
5000 }
5001 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005002 }
bellard36b486b2003-11-11 13:36:08 +00005003 }
5004 break;
bellardcd6f1162004-05-13 22:02:20 +00005005 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005006 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005007 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005008 break;
bellard52ca8d62006-06-14 16:03:05 +00005009#ifdef TARGET_I386
5010 case QEMU_OPTION_no_fd_bootchk:
5011 fd_bootchk = 0;
5012 break;
5013#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01005014 case QEMU_OPTION_netdev:
5015 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5016 exit(1);
5017 }
5018 break;
bellard7c9d8e02005-11-15 22:16:05 +00005019 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01005020 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005021 exit(1);
5022 }
bellard702c6512004-04-02 21:21:32 +00005023 break;
bellardc7f74642004-08-24 21:57:12 +00005024#ifdef CONFIG_SLIRP
5025 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005026 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005027 break;
ths47d5d012007-02-20 00:05:08 +00005028 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005029 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005030 break;
bellardc94c8d62004-09-13 21:37:34 +00005031#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005032 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005033 if (net_slirp_smb(optarg) < 0)
5034 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005035 break;
bellardc94c8d62004-09-13 21:37:34 +00005036#endif
bellard9bf05442004-08-25 22:12:49 +00005037 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005038 if (net_slirp_redir(optarg) < 0)
5039 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005040 break;
bellardc7f74642004-08-24 21:57:12 +00005041#endif
balrogdc72ac12008-11-09 00:04:26 +00005042 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005043 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005044 break;
bellard1d14ffa2005-10-30 18:58:22 +00005045#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005046 case QEMU_OPTION_audio_help:
5047 AUD_help ();
5048 exit (0);
5049 break;
5050 case QEMU_OPTION_soundhw:
5051 select_soundhw (optarg);
5052 break;
5053#endif
bellardcd6f1162004-05-13 22:02:20 +00005054 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005055 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005056 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005057 case QEMU_OPTION_version:
5058 version();
5059 exit(0);
5060 break;
aurel3200f82b82008-04-27 21:12:55 +00005061 case QEMU_OPTION_m: {
5062 uint64_t value;
5063 char *ptr;
5064
5065 value = strtoul(optarg, &ptr, 10);
5066 switch (*ptr) {
5067 case 0: case 'M': case 'm':
5068 value <<= 20;
5069 break;
5070 case 'G': case 'g':
5071 value <<= 30;
5072 break;
5073 default:
5074 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005075 exit(1);
5076 }
aurel3200f82b82008-04-27 21:12:55 +00005077
5078 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005079 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005080 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5081 exit(1);
5082 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005083 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005084 fprintf(stderr, "qemu: ram size too large\n");
5085 exit(1);
5086 }
5087 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005088 break;
aurel3200f82b82008-04-27 21:12:55 +00005089 }
bellardcd6f1162004-05-13 22:02:20 +00005090 case QEMU_OPTION_d:
5091 {
5092 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005093 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005094
bellardcd6f1162004-05-13 22:02:20 +00005095 mask = cpu_str_to_log_mask(optarg);
5096 if (!mask) {
5097 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005098 for(item = cpu_log_items; item->mask != 0; item++) {
5099 printf("%-10s %s\n", item->name, item->help);
5100 }
5101 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005102 }
5103 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005104 }
bellardcd6f1162004-05-13 22:02:20 +00005105 break;
bellardcd6f1162004-05-13 22:02:20 +00005106 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005107 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005108 break;
aliguori59030a82009-04-05 18:43:41 +00005109 case QEMU_OPTION_gdb:
5110 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005111 break;
bellardcd6f1162004-05-13 22:02:20 +00005112 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005113 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005114 break;
j_mayer1192dad2007-10-05 13:08:35 +00005115 case QEMU_OPTION_bios:
5116 bios_name = optarg;
5117 break;
aurel321b530a62009-04-05 20:08:59 +00005118 case QEMU_OPTION_singlestep:
5119 singlestep = 1;
5120 break;
bellardcd6f1162004-05-13 22:02:20 +00005121 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005122 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005123 break;
bellard3d11d0e2004-12-12 16:56:30 +00005124 case QEMU_OPTION_k:
5125 keyboard_layout = optarg;
5126 break;
bellardee22c2f2004-06-03 12:49:50 +00005127 case QEMU_OPTION_localtime:
5128 rtc_utc = 0;
5129 break;
malc3893c122008-09-28 00:42:05 +00005130 case QEMU_OPTION_vga:
5131 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005132 break;
blueswir15824d652009-03-28 06:44:27 +00005133#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005134 case QEMU_OPTION_g:
5135 {
5136 const char *p;
5137 int w, h, depth;
5138 p = optarg;
5139 w = strtol(p, (char **)&p, 10);
5140 if (w <= 0) {
5141 graphic_error:
5142 fprintf(stderr, "qemu: invalid resolution or depth\n");
5143 exit(1);
5144 }
5145 if (*p != 'x')
5146 goto graphic_error;
5147 p++;
5148 h = strtol(p, (char **)&p, 10);
5149 if (h <= 0)
5150 goto graphic_error;
5151 if (*p == 'x') {
5152 p++;
5153 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005154 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005155 depth != 24 && depth != 32)
5156 goto graphic_error;
5157 } else if (*p == '\0') {
5158 depth = graphic_depth;
5159 } else {
5160 goto graphic_error;
5161 }
ths3b46e622007-09-17 08:09:54 +00005162
bellarde9b137c2004-06-21 16:46:10 +00005163 graphic_width = w;
5164 graphic_height = h;
5165 graphic_depth = depth;
5166 }
5167 break;
blueswir15824d652009-03-28 06:44:27 +00005168#endif
ths20d8a3e2007-02-18 17:04:49 +00005169 case QEMU_OPTION_echr:
5170 {
5171 char *r;
5172 term_escape_char = strtol(optarg, &r, 0);
5173 if (r == optarg)
5174 printf("Bad argument to echr\n");
5175 break;
5176 }
bellard82c643f2004-07-14 17:28:13 +00005177 case QEMU_OPTION_monitor:
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005178 if (monitor_device_index >= MAX_MONITOR_DEVICES) {
5179 fprintf(stderr, "qemu: too many monitor devices\n");
5180 exit(1);
5181 }
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02005182 monitor_devices[monitor_device_index] =
5183 monitor_cmdline_parse(optarg,
5184 &monitor_flags[monitor_device_index]);
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005185 monitor_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005186 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005187 case QEMU_OPTION_chardev:
5188 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5189 if (!opts) {
5190 fprintf(stderr, "parse error: %s\n", optarg);
5191 exit(1);
5192 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005193 break;
bellard82c643f2004-07-14 17:28:13 +00005194 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005195 if (serial_device_index >= MAX_SERIAL_PORTS) {
5196 fprintf(stderr, "qemu: too many serial ports\n");
5197 exit(1);
5198 }
pbrookfd5f3932008-03-26 20:55:43 +00005199 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005200 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005201 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005202 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005203 if (watchdog) {
5204 fprintf(stderr,
5205 "qemu: only one watchdog option may be given\n");
5206 return 1;
5207 }
5208 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005209 break;
5210 case QEMU_OPTION_watchdog_action:
5211 if (select_watchdog_action(optarg) == -1) {
5212 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5213 exit(1);
5214 }
5215 break;
aliguori51ecf132009-01-15 20:06:40 +00005216 case QEMU_OPTION_virtiocon:
5217 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5218 fprintf(stderr, "qemu: too many virtio consoles\n");
5219 exit(1);
5220 }
5221 virtio_consoles[virtio_console_index] = optarg;
5222 virtio_console_index++;
5223 break;
bellard6508fe52005-01-15 12:02:56 +00005224 case QEMU_OPTION_parallel:
5225 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5226 fprintf(stderr, "qemu: too many parallel ports\n");
5227 exit(1);
5228 }
pbrookfd5f3932008-03-26 20:55:43 +00005229 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005230 parallel_device_index++;
5231 break;
bellardd63d3072004-10-03 13:29:03 +00005232 case QEMU_OPTION_loadvm:
5233 loadvm = optarg;
5234 break;
5235 case QEMU_OPTION_full_screen:
5236 full_screen = 1;
5237 break;
ths667acca2006-12-11 02:08:05 +00005238#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005239 case QEMU_OPTION_no_frame:
5240 no_frame = 1;
5241 break;
ths3780e192007-06-21 21:08:02 +00005242 case QEMU_OPTION_alt_grab:
5243 alt_grab = 1;
5244 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005245 case QEMU_OPTION_ctrl_grab:
5246 ctrl_grab = 1;
5247 break;
ths667acca2006-12-11 02:08:05 +00005248 case QEMU_OPTION_no_quit:
5249 no_quit = 1;
5250 break;
aliguori7d957bd2009-01-15 22:14:11 +00005251 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005252 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005253 break;
ths667acca2006-12-11 02:08:05 +00005254#endif
bellardf7cce892004-12-08 22:21:25 +00005255 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005256 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005257 break;
bellarda09db212005-04-30 16:10:35 +00005258#ifdef TARGET_I386
5259 case QEMU_OPTION_win2k_hack:
5260 win2k_install_hack = 1;
5261 break;
aliguori73822ec2009-01-15 20:11:34 +00005262 case QEMU_OPTION_rtc_td_hack:
5263 rtc_td_hack = 1;
5264 break;
aliguori8a92ea22009-02-27 20:12:36 +00005265 case QEMU_OPTION_acpitable:
5266 if(acpi_table_add(optarg) < 0) {
5267 fprintf(stderr, "Wrong acpi table provided\n");
5268 exit(1);
5269 }
5270 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005271 case QEMU_OPTION_smbios:
5272 if(smbios_entry_add(optarg) < 0) {
5273 fprintf(stderr, "Wrong smbios provided\n");
5274 exit(1);
5275 }
5276 break;
bellarda09db212005-04-30 16:10:35 +00005277#endif
aliguori7ba1e612008-11-05 16:04:33 +00005278#ifdef CONFIG_KVM
5279 case QEMU_OPTION_enable_kvm:
5280 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005281 break;
5282#endif
bellardbb36d472005-11-05 14:22:28 +00005283 case QEMU_OPTION_usb:
5284 usb_enabled = 1;
5285 break;
bellarda594cfb2005-11-06 16:13:29 +00005286 case QEMU_OPTION_usbdevice:
5287 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005288 add_device_config(DEV_USB, optarg);
5289 break;
5290 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005291 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005292 exit(1);
5293 }
bellarda594cfb2005-11-06 16:13:29 +00005294 break;
bellard6a00d602005-11-21 23:25:50 +00005295 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005296 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005297 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005298 fprintf(stderr, "Invalid number of CPUs\n");
5299 exit(1);
5300 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005301 if (max_cpus < smp_cpus) {
5302 fprintf(stderr, "maxcpus must be equal to or greater than "
5303 "smp\n");
5304 exit(1);
5305 }
5306 if (max_cpus > 255) {
5307 fprintf(stderr, "Unsupported number of maxcpus\n");
5308 exit(1);
5309 }
bellard6a00d602005-11-21 23:25:50 +00005310 break;
bellard24236862006-04-30 21:28:36 +00005311 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005312 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005313 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005314 break;
blueswir15824d652009-03-28 06:44:27 +00005315#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005316 case QEMU_OPTION_no_acpi:
5317 acpi_enabled = 0;
5318 break;
aliguori16b29ae2008-12-17 23:28:44 +00005319 case QEMU_OPTION_no_hpet:
5320 no_hpet = 1;
5321 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005322 case QEMU_OPTION_balloon:
5323 if (balloon_parse(optarg) < 0) {
5324 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5325 exit(1);
5326 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005327 break;
blueswir15824d652009-03-28 06:44:27 +00005328#endif
bellardd1beab82006-10-02 19:44:22 +00005329 case QEMU_OPTION_no_reboot:
5330 no_reboot = 1;
5331 break;
aurel32b2f76162008-04-11 21:35:52 +00005332 case QEMU_OPTION_no_shutdown:
5333 no_shutdown = 1;
5334 break;
balrog9467cd42007-05-01 01:34:14 +00005335 case QEMU_OPTION_show_cursor:
5336 cursor_hide = 0;
5337 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005338 case QEMU_OPTION_uuid:
5339 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5340 fprintf(stderr, "Fail to parse UUID string."
5341 " Wrong format.\n");
5342 exit(1);
5343 }
5344 break;
blueswir15824d652009-03-28 06:44:27 +00005345#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005346 case QEMU_OPTION_daemonize:
5347 daemonize = 1;
5348 break;
blueswir15824d652009-03-28 06:44:27 +00005349#endif
ths9ae02552007-01-05 17:39:04 +00005350 case QEMU_OPTION_option_rom:
5351 if (nb_option_roms >= MAX_OPTION_ROMS) {
5352 fprintf(stderr, "Too many option ROMs\n");
5353 exit(1);
5354 }
5355 option_rom[nb_option_roms] = optarg;
5356 nb_option_roms++;
5357 break;
blueswir15824d652009-03-28 06:44:27 +00005358#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005359 case QEMU_OPTION_semihosting:
5360 semihosting_enabled = 1;
5361 break;
blueswir15824d652009-03-28 06:44:27 +00005362#endif
thsc35734b2007-03-19 15:17:08 +00005363 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005364 qemu_name = qemu_strdup(optarg);
5365 {
5366 char *p = strchr(qemu_name, ',');
5367 if (p != NULL) {
5368 *p++ = 0;
5369 if (strncmp(p, "process=", 8)) {
5370 fprintf(stderr, "Unknown subargument %s to -name", p);
5371 exit(1);
5372 }
5373 p += 8;
5374 set_proc_name(p);
5375 }
5376 }
thsc35734b2007-03-19 15:17:08 +00005377 break;
blueswir195efd112008-12-24 20:26:14 +00005378#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005379 case QEMU_OPTION_prom_env:
5380 if (nb_prom_envs >= MAX_PROM_ENVS) {
5381 fprintf(stderr, "Too many prom variables\n");
5382 exit(1);
5383 }
5384 prom_envs[nb_prom_envs] = optarg;
5385 nb_prom_envs++;
5386 break;
5387#endif
balrog2b8f2d42007-07-27 22:08:46 +00005388#ifdef TARGET_ARM
5389 case QEMU_OPTION_old_param:
5390 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005391 break;
balrog2b8f2d42007-07-27 22:08:46 +00005392#endif
thsf3dcfad2007-08-24 01:26:02 +00005393 case QEMU_OPTION_clock:
5394 configure_alarms(optarg);
5395 break;
bellard7e0af5d02007-11-07 16:24:33 +00005396 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005397 configure_rtc_date_offset(optarg, 1);
5398 break;
5399 case QEMU_OPTION_rtc:
5400 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5401 if (!opts) {
5402 fprintf(stderr, "parse error: %s\n", optarg);
5403 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005404 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005405 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005406 break;
bellard26a5f132008-05-28 12:30:31 +00005407 case QEMU_OPTION_tb_size:
5408 tb_size = strtol(optarg, NULL, 0);
5409 if (tb_size < 0)
5410 tb_size = 0;
5411 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005412 case QEMU_OPTION_icount:
5413 use_icount = 1;
5414 if (strcmp(optarg, "auto") == 0) {
5415 icount_time_shift = -1;
5416 } else {
5417 icount_time_shift = strtol(optarg, NULL, 0);
5418 }
5419 break;
aliguori5bb79102008-10-13 03:12:02 +00005420 case QEMU_OPTION_incoming:
5421 incoming = optarg;
5422 break;
blueswir15824d652009-03-28 06:44:27 +00005423#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005424 case QEMU_OPTION_chroot:
5425 chroot_dir = optarg;
5426 break;
5427 case QEMU_OPTION_runas:
5428 run_as = optarg;
5429 break;
blueswir15824d652009-03-28 06:44:27 +00005430#endif
aliguorie37630c2009-04-22 15:19:10 +00005431#ifdef CONFIG_XEN
5432 case QEMU_OPTION_xen_domid:
5433 xen_domid = atoi(optarg);
5434 break;
5435 case QEMU_OPTION_xen_create:
5436 xen_mode = XEN_CREATE;
5437 break;
5438 case QEMU_OPTION_xen_attach:
5439 xen_mode = XEN_ATTACH;
5440 break;
5441#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02005442 case QEMU_OPTION_readconfig:
5443 {
5444 FILE *fp;
5445 fp = fopen(optarg, "r");
5446 if (fp == NULL) {
5447 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5448 exit(1);
5449 }
5450 if (qemu_config_parse(fp) != 0) {
5451 exit(1);
5452 }
5453 fclose(fp);
5454 break;
5455 }
5456 case QEMU_OPTION_writeconfig:
5457 {
5458 FILE *fp;
5459 if (strcmp(optarg, "-") == 0) {
5460 fp = stdout;
5461 } else {
5462 fp = fopen(optarg, "w");
5463 if (fp == NULL) {
5464 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5465 exit(1);
5466 }
5467 }
5468 qemu_config_write(fp);
5469 fclose(fp);
5470 break;
5471 }
bellardcd6f1162004-05-13 22:02:20 +00005472 }
bellard0824d6f2003-06-24 13:42:40 +00005473 }
5474 }
bellard330d0412003-07-26 18:11:40 +00005475
Paul Brook5cea8592009-05-30 00:52:44 +01005476 /* If no data_dir is specified then try to find it relative to the
5477 executable path. */
5478 if (!data_dir) {
5479 data_dir = find_datadir(argv[0]);
5480 }
5481 /* If all else fails use the install patch specified when building. */
5482 if (!data_dir) {
5483 data_dir = CONFIG_QEMU_SHAREDIR;
5484 }
5485
Jes Sorensen6be68d72009-07-23 17:03:42 +02005486 /*
5487 * Default to max_cpus = smp_cpus, in case the user doesn't
5488 * specify a max_cpus value.
5489 */
5490 if (!max_cpus)
5491 max_cpus = smp_cpus;
5492
balrog3d878ca2008-10-28 10:59:59 +00005493 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005494 if (smp_cpus > machine->max_cpus) {
5495 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5496 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5497 machine->max_cpus);
5498 exit(1);
5499 }
5500
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005501 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005502 if (serial_device_index == 0)
5503 serial_devices[0] = "stdio";
5504 if (parallel_device_index == 0)
5505 parallel_devices[0] = "null";
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005506 if (strncmp(monitor_devices[0], "vc", 2) == 0) {
5507 monitor_devices[0] = "stdio";
5508 }
aliguoribc0129d2008-08-01 15:12:34 +00005509 }
5510
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01005511 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5512 exit(1);
5513
ths71e3ceb2006-12-22 02:11:31 +00005514#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005515 if (daemonize) {
5516 pid_t pid;
5517
5518 if (pipe(fds) == -1)
5519 exit(1);
5520
5521 pid = fork();
5522 if (pid > 0) {
5523 uint8_t status;
5524 ssize_t len;
5525
5526 close(fds[1]);
5527
5528 again:
ths93815bc2007-03-19 15:58:31 +00005529 len = read(fds[0], &status, 1);
5530 if (len == -1 && (errno == EINTR))
5531 goto again;
5532
5533 if (len != 1)
5534 exit(1);
5535 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005536 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005537 exit(1);
5538 } else
5539 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005540 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005541 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005542
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005543 close(fds[0]);
5544 qemu_set_cloexec(fds[1]);
5545
ths71e3ceb2006-12-22 02:11:31 +00005546 setsid();
5547
5548 pid = fork();
5549 if (pid > 0)
5550 exit(0);
5551 else if (pid < 0)
5552 exit(1);
5553
5554 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005555
5556 signal(SIGTSTP, SIG_IGN);
5557 signal(SIGTTOU, SIG_IGN);
5558 signal(SIGTTIN, SIG_IGN);
5559 }
ths71e3ceb2006-12-22 02:11:31 +00005560
thsaa26bb22007-03-25 21:33:06 +00005561 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005562 if (daemonize) {
5563 uint8_t status = 1;
5564 write(fds[1], &status, 1);
5565 } else
Justin M. Forbes850810d2009-10-01 09:42:56 -05005566 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005567 exit(1);
5568 }
blueswir1b9e82a52009-04-05 18:03:31 +00005569#endif
ths93815bc2007-03-19 15:58:31 +00005570
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005571 if (kvm_enabled()) {
5572 int ret;
5573
5574 ret = kvm_init(smp_cpus);
5575 if (ret < 0) {
5576 fprintf(stderr, "failed to initialize KVM\n");
5577 exit(1);
5578 }
5579 }
5580
aliguori3fcf7b62009-04-24 18:03:25 +00005581 if (qemu_init_main_loop()) {
5582 fprintf(stderr, "qemu_init_main_loop failed\n");
5583 exit(1);
5584 }
bellarda20dd502003-09-30 21:07:02 +00005585 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005586
thsf8d39c02008-07-03 10:01:15 +00005587 if (!linux_boot && *kernel_cmdline != '\0') {
5588 fprintf(stderr, "-append only allowed with -kernel option\n");
5589 exit(1);
5590 }
5591
5592 if (!linux_boot && initrd_filename != NULL) {
5593 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5594 exit(1);
5595 }
5596
Filip Navarabf65f532009-07-27 10:02:04 -05005597#ifndef _WIN32
5598 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005599 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005600#endif
ths3b46e622007-09-17 08:09:54 +00005601
aliguori7183b4b2008-11-05 20:40:18 +00005602 if (init_timer_alarm() < 0) {
5603 fprintf(stderr, "could not initialize alarm timer\n");
5604 exit(1);
5605 }
pbrook2e70f6e2008-06-29 01:03:05 +00005606 if (use_icount && icount_time_shift < 0) {
5607 use_icount = 2;
5608 /* 125MIPS seems a reasonable initial guess at the guest speed.
5609 It will be corrected fairly quickly anyway. */
5610 icount_time_shift = 3;
5611 init_icount_adjust();
5612 }
pbrook634fce92006-07-15 17:40:09 +00005613
bellardfd1dff42006-02-01 21:29:26 +00005614#ifdef _WIN32
5615 socket_init();
5616#endif
5617
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005618 if (net_init_clients() < 0) {
5619 exit(1);
bellard702c6512004-04-02 21:21:32 +00005620 }
bellardf1510b22003-06-25 00:07:40 +00005621
Glauber Costa406c8df2009-06-17 09:05:30 -04005622 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5623 net_set_boot_mask(net_boot);
5624
balrogdc72ac12008-11-09 00:04:26 +00005625 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005626 if (foreach_device_config(DEV_BT, bt_parse))
5627 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005628
bellard0824d6f2003-06-24 13:42:40 +00005629 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005630 if (ram_size == 0)
5631 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005632
bellard26a5f132008-05-28 12:30:31 +00005633 /* init the dynamic translator */
5634 cpu_exec_init_all(tb_size * 1024 * 1024);
5635
Markus Armbrustereb852012009-10-27 18:41:44 +01005636 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00005637
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005638 blk_mig_init();
5639
thse4bcb142007-12-02 04:51:10 +00005640 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005641 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005642
balrog9d413d12007-12-04 00:10:34 +00005643 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005644 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005645
balrog9d413d12007-12-04 00:10:34 +00005646 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005647 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005648
ths96d30e42007-01-07 20:42:14 +00005649 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005650 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005651 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5652 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005653 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005654
Juan Quintela2faf58c2009-09-10 03:04:28 +02005655 vmstate_register(0, &vmstate_timers ,&timers_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005656 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
5657 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005658
aliguori3023f332009-01-16 19:04:14 +00005659 /* Maintain compatibility with multiple stdio monitors */
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005660 if (!strcmp(monitor_devices[0],"stdio")) {
aliguori3023f332009-01-16 19:04:14 +00005661 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5662 const char *devname = serial_devices[i];
5663 if (devname && !strcmp(devname,"mon:stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005664 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005665 break;
5666 } else if (devname && !strcmp(devname,"stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005667 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005668 serial_devices[i] = "mon:stdio";
5669 break;
5670 }
5671 }
Alexander Graf93d434b2009-12-05 12:44:30 +01005672 for (i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5673 const char *devname = virtio_consoles[i];
5674 if (devname && !strcmp(devname,"mon:stdio")) {
5675 monitor_devices[0] = NULL;
5676 break;
5677 } else if (devname && !strcmp(devname,"stdio")) {
5678 monitor_devices[0] = NULL;
5679 virtio_consoles[i] = "mon:stdio";
5680 break;
5681 }
5682 }
aliguori3023f332009-01-16 19:04:14 +00005683 }
5684
aliguori268a3622009-04-21 22:30:27 +00005685 if (nb_numa_nodes > 0) {
5686 int i;
5687
5688 if (nb_numa_nodes > smp_cpus) {
5689 nb_numa_nodes = smp_cpus;
5690 }
5691
5692 /* If no memory size if given for any node, assume the default case
5693 * and distribute the available memory equally across all nodes
5694 */
5695 for (i = 0; i < nb_numa_nodes; i++) {
5696 if (node_mem[i] != 0)
5697 break;
5698 }
5699 if (i == nb_numa_nodes) {
5700 uint64_t usedmem = 0;
5701
5702 /* On Linux, the each node's border has to be 8MB aligned,
5703 * the final node gets the rest.
5704 */
5705 for (i = 0; i < nb_numa_nodes - 1; i++) {
5706 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5707 usedmem += node_mem[i];
5708 }
5709 node_mem[i] = ram_size - usedmem;
5710 }
5711
5712 for (i = 0; i < nb_numa_nodes; i++) {
5713 if (node_cpumask[i] != 0)
5714 break;
5715 }
5716 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5717 * must cope with this anyway, because there are BIOSes out there in
5718 * real machines which also use this scheme.
5719 */
5720 if (i == nb_numa_nodes) {
5721 for (i = 0; i < smp_cpus; i++) {
5722 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5723 }
5724 }
5725 }
5726
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005727 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5728 const char *devname = monitor_devices[i];
5729 if (devname && strcmp(devname, "none")) {
5730 char label[32];
5731 if (i == 0) {
5732 snprintf(label, sizeof(label), "monitor");
5733 } else {
5734 snprintf(label, sizeof(label), "monitor%d", i);
5735 }
5736 monitor_hds[i] = qemu_chr_open(label, devname, NULL);
5737 if (!monitor_hds[i]) {
5738 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
5739 devname);
5740 exit(1);
5741 }
aliguori4c621802009-01-16 21:48:20 +00005742 }
5743 }
5744
aliguori2796dae2009-01-16 20:23:27 +00005745 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5746 const char *devname = serial_devices[i];
5747 if (devname && strcmp(devname, "none")) {
Aurelien Jarno324a8022009-09-14 23:49:43 +02005748 char label[32];
5749 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005750 serial_hds[i] = qemu_chr_open(label, devname, NULL);
Aurelien Jarno324a8022009-09-14 23:49:43 +02005751 if (!serial_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005752 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
5753 devname, strerror(errno));
Aurelien Jarno324a8022009-09-14 23:49:43 +02005754 exit(1);
5755 }
aliguori2796dae2009-01-16 20:23:27 +00005756 }
5757 }
5758
5759 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5760 const char *devname = parallel_devices[i];
5761 if (devname && strcmp(devname, "none")) {
5762 char label[32];
5763 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005764 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005765 if (!parallel_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005766 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
5767 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005768 exit(1);
5769 }
5770 }
5771 }
5772
5773 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5774 const char *devname = virtio_consoles[i];
5775 if (devname && strcmp(devname, "none")) {
5776 char label[32];
5777 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005778 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005779 if (!virtcon_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005780 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
5781 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005782 exit(1);
5783 }
5784 }
5785 }
5786
Paul Brookaae94602009-05-14 22:35:06 +01005787 module_call_init(MODULE_INIT_DEVICE);
5788
Markus Armbruster09aaa162009-08-21 10:31:34 +02005789 if (watchdog) {
5790 i = select_watchdog(watchdog);
5791 if (i > 0)
5792 exit (i == 1 ? 1 : 0);
5793 }
5794
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005795 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01005796 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005797 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005798 qemu_add_globals();
5799
Paul Brookfbe1b592009-05-13 17:56:25 +01005800 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005801 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5802
aliguori268a3622009-04-21 22:30:27 +00005803
Juan Quintela67b3b712009-08-28 19:25:15 +02005804#ifndef _WIN32
5805 /* must be after terminal init, SDL library changes signal handlers */
5806 sighandler_setup();
5807#endif
5808
aliguori268a3622009-04-21 22:30:27 +00005809 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5810 for (i = 0; i < nb_numa_nodes; i++) {
5811 if (node_cpumask[i] & (1 << env->cpu_index)) {
5812 env->numa_node = i;
5813 }
5814 }
5815 }
5816
aliguori6f338c32009-02-11 15:21:54 +00005817 current_machine = machine;
5818
aliguori3023f332009-01-16 19:04:14 +00005819 /* init USB devices */
5820 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005821 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5822 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005823 }
5824
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005825 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005826 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005827 exit(1);
5828
aliguori8f391ab2009-01-19 16:34:10 +00005829 if (!display_state)
5830 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005831 /* just use the first displaystate for the moment */
5832 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005833
5834 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005835#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005836 display_type = DT_SDL;
5837#else
5838 display_type = DT_VNC;
5839 vnc_display = "localhost:0,to=99";
5840 show_vnc_port = 1;
5841#endif
5842 }
5843
5844
5845 switch (display_type) {
5846 case DT_NOGRAPHIC:
5847 break;
5848#if defined(CONFIG_CURSES)
5849 case DT_CURSES:
5850 curses_display_init(ds, full_screen);
5851 break;
5852#endif
bellard5b0753e2005-03-01 21:37:28 +00005853#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005854 case DT_SDL:
5855 sdl_display_init(ds, full_screen, no_frame);
5856 break;
bellard5b0753e2005-03-01 21:37:28 +00005857#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005858 case DT_SDL:
5859 cocoa_display_init(ds, full_screen);
5860 break;
bellard313aa562003-08-10 21:52:11 +00005861#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005862 case DT_VNC:
5863 vnc_display_init(ds);
5864 if (vnc_display_open(ds, vnc_display) < 0)
5865 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005866
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005867 if (show_vnc_port) {
5868 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005869 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005870 break;
5871 default:
5872 break;
bellard313aa562003-08-10 21:52:11 +00005873 }
aliguori7d957bd2009-01-15 22:14:11 +00005874 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005875
aliguori3023f332009-01-16 19:04:14 +00005876 dcl = ds->listeners;
5877 while (dcl != NULL) {
5878 if (dcl->dpy_refresh != NULL) {
5879 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5880 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005881 }
aliguori3023f332009-01-16 19:04:14 +00005882 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005883 }
aliguori3023f332009-01-16 19:04:14 +00005884
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005885 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005886 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5887 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5888 }
5889
aliguori2796dae2009-01-16 20:23:27 +00005890 text_consoles_set_display(display_state);
5891
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005892 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5893 if (monitor_devices[i] && monitor_hds[i]) {
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02005894 monitor_init(monitor_hds[i], monitor_flags[i]);
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005895 }
5896 }
bellard82c643f2004-07-14 17:28:13 +00005897
bellard8d11df92004-08-24 21:13:40 +00005898 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005899 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005900 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005901 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005902 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005903 }
bellard82c643f2004-07-14 17:28:13 +00005904 }
bellard82c643f2004-07-14 17:28:13 +00005905
bellard6508fe52005-01-15 12:02:56 +00005906 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005907 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005908 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005909 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005910 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005911 }
5912 }
5913
aliguori9ede2fd2009-01-15 20:05:25 +00005914 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5915 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005916 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005917 if (strstart(devname, "vc", 0))
5918 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5919 }
5920 }
5921
aliguori59030a82009-04-05 18:43:41 +00005922 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5923 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5924 gdbstub_dev);
5925 exit(1);
balrog45669e02007-07-02 13:20:17 +00005926 }
balrog45669e02007-07-02 13:20:17 +00005927
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02005928 qdev_machine_creation_done();
5929
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02005930 rom_load_all();
5931
Juan Quintela504c2942009-11-12 00:39:13 +01005932 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02005933 if (loadvm) {
5934 if (load_vmstate(cur_mon, loadvm) < 0) {
5935 autostart = 0;
5936 }
5937 }
bellardd63d3072004-10-03 13:29:03 +00005938
Glauber Costa2bb8c102009-07-24 16:20:23 -04005939 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005940 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005941 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005942 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005943 }
thsffd843b2006-12-21 19:46:43 +00005944
blueswir1b9e82a52009-04-05 18:03:31 +00005945#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005946 if (daemonize) {
5947 uint8_t status = 0;
5948 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005949
5950 again1:
5951 len = write(fds[1], &status, 1);
5952 if (len == -1 && (errno == EINTR))
5953 goto again1;
5954
5955 if (len != 1)
5956 exit(1);
5957
aliguoribd54b862008-07-23 00:58:33 +00005958 chdir("/");
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005959 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005960 if (fd == -1)
5961 exit(1);
aliguori08585322009-02-27 22:09:45 +00005962 }
ths71e3ceb2006-12-22 02:11:31 +00005963
aliguori08585322009-02-27 22:09:45 +00005964 if (run_as) {
5965 pwd = getpwnam(run_as);
5966 if (!pwd) {
5967 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5968 exit(1);
5969 }
5970 }
ths71e3ceb2006-12-22 02:11:31 +00005971
aliguori08585322009-02-27 22:09:45 +00005972 if (chroot_dir) {
5973 if (chroot(chroot_dir) < 0) {
5974 fprintf(stderr, "chroot failed\n");
5975 exit(1);
5976 }
5977 chdir("/");
5978 }
5979
5980 if (run_as) {
5981 if (setgid(pwd->pw_gid) < 0) {
5982 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5983 exit(1);
5984 }
5985 if (setuid(pwd->pw_uid) < 0) {
5986 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5987 exit(1);
5988 }
5989 if (setuid(0) != -1) {
5990 fprintf(stderr, "Dropping privileges failed\n");
5991 exit(1);
5992 }
5993 }
aliguori08585322009-02-27 22:09:45 +00005994
5995 if (daemonize) {
5996 dup2(fd, 0);
5997 dup2(fd, 1);
5998 dup2(fd, 2);
5999
6000 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006001 }
blueswir1b9e82a52009-04-05 18:03:31 +00006002#endif
ths71e3ceb2006-12-22 02:11:31 +00006003
bellard8a7ddc32004-03-31 19:00:16 +00006004 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006005 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006006 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006007
bellard0824d6f2003-06-24 13:42:40 +00006008 return 0;
6009}