blob: e12f1d3ee08759de7956d2fa26dde1f79fbc8ffe [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
47#if defined(__NetBSD__)
48#include <net/if_tap.h>
49#endif
50#ifdef __linux__
51#include <linux/if_tun.h>
52#endif
53#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000054#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000055#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000056#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020057#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000058#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000059#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000060#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000061#else
62#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000063#endif
ths5c40d2b2007-06-23 16:03:36 +000064#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000066#else
blueswir1223f0d72008-09-30 18:12:18 +000067#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000068#include <pty.h>
69#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000070#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020071#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000072
73/* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75/* #include <linux/hpet.h> */
76#include "hpet.h"
77
bellarde57a8c02005-11-10 23:58:52 +000078#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000079#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000080#endif
81#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000082#include <sys/stat.h>
83#include <sys/ethernet.h>
84#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000085#include <netinet/arp.h>
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
89#include <netinet/ip_icmp.h> // must come after ip.h
90#include <netinet/udp.h>
91#include <netinet/tcp.h>
92#include <net/if.h>
93#include <syslog.h>
94#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000095#endif
bellard7d3505c2004-05-12 19:32:15 +000096#endif
bellardec530c82006-04-25 22:36:06 +000097#endif
bellard67b915a2004-03-31 23:37:16 +000098
blueswir19892fbf2008-08-24 10:34:20 +000099#if defined(__OpenBSD__)
100#include <util.h>
101#endif
102
ths8a16d272008-07-19 09:56:24 +0000103#if defined(CONFIG_VDE)
104#include <libvdeplug.h>
105#endif
106
bellard67b915a2004-03-31 23:37:16 +0000107#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000108#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000109#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000110#endif
111
bellard73332e52004-04-04 20:22:28 +0000112#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200113#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200114#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000115int qemu_main(int argc, char **argv, char **envp);
116int main(int argc, char **argv)
117{
Stefan Weil59a36a22009-06-18 20:11:03 +0200118 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000119}
120#undef main
121#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000122#endif
bellard73332e52004-04-04 20:22:28 +0000123#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000124
bellard5b0753e2005-03-01 21:37:28 +0000125#ifdef CONFIG_COCOA
126#undef main
127#define main qemu_main
128#endif /* CONFIG_COCOA */
129
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "hw/hw.h"
131#include "hw/boards.h"
132#include "hw/usb.h"
133#include "hw/pcmcia.h"
134#include "hw/pc.h"
135#include "hw/audiodev.h"
136#include "hw/isa.h"
137#include "hw/baum.h"
138#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100139#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000140#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000141#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200142#include "hw/qdev.h"
aurel325ef4efa2009-03-10 21:43:35 +0000143#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "net.h"
145#include "monitor.h"
146#include "console.h"
147#include "sysemu.h"
148#include "gdbstub.h"
149#include "qemu-timer.h"
150#include "qemu-char.h"
151#include "cache-utils.h"
152#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000153#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "audio/audio.h"
155#include "migration.h"
156#include "kvm.h"
157#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200158#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200159#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
bellard0824d6f2003-06-24 13:42:40 +0000161#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000162
bellard8a7ddc32004-03-31 19:00:16 +0000163#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000164
blueswir1511d2b12009-03-07 15:32:56 +0000165#include "qemu_socket.h"
166
Jan Kiszkad918f232009-06-24 14:42:30 +0200167#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000168
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169#include "qemu-queue.h"
170
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
bellard1bfe8562004-07-08 21:17:50 +0000174#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000175
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +0200176/* Maximum number of monitor devices */
177#define MAX_MONITOR_DEVICES 10
178
Paul Brook5cea8592009-05-30 00:52:44 +0100179static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000180const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000181/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000182 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000183struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
184struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000185enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000186static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500187DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000188const char* keyboard_layout = NULL;
aurel3200f82b82008-04-27 21:12:55 +0000189ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000190int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000191NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000192int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200193int autostart;
balrogf6503052008-02-17 11:42:19 +0000194static int rtc_utc = 1;
195static int rtc_date_offset = -1; /* -1 means no change */
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;
blueswir195efd112008-12-24 20:26:14 +0000245#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000246unsigned int nb_prom_envs = 0;
247const char *prom_envs[MAX_PROM_ENVS];
248#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200249int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000250
aliguori268a3622009-04-21 22:30:27 +0000251int nb_numa_nodes;
252uint64_t node_mem[MAX_NODES];
253uint64_t node_cpumask[MAX_NODES];
254
balrogee5605e2007-12-03 03:01:40 +0000255static CPUState *cur_cpu;
256static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000257static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000258/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000259static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000260/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000261#define MAX_ICOUNT_SHIFT 10
262/* Compensate for varying guest execution speed. */
263static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000264static QEMUTimer *icount_rt_timer;
265static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000266static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000267
blueswir18fcb1b92008-09-18 18:29:08 +0000268uint8_t qemu_uuid[16];
269
Jan Kiszka76e30d02009-07-02 00:19:02 +0200270static QEMUBootSetHandler *boot_set_handler;
271static void *boot_set_opaque;
272
bellard0824d6f2003-06-24 13:42:40 +0000273/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000274/* x86 ISA bus support */
275
276target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000277PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000278
bellard0824d6f2003-06-24 13:42:40 +0000279/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000280void hw_error(const char *fmt, ...)
281{
282 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000283 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000284
285 va_start(ap, fmt);
286 fprintf(stderr, "qemu: hardware error: ");
287 vfprintf(stderr, fmt, ap);
288 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000289 for(env = first_cpu; env != NULL; env = env->next_cpu) {
290 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000291#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000292 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000293#else
bellard6a00d602005-11-21 23:25:50 +0000294 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000295#endif
bellard6a00d602005-11-21 23:25:50 +0000296 }
bellard0824d6f2003-06-24 13:42:40 +0000297 va_end(ap);
298 abort();
299}
Andi Kleen18894652009-07-02 09:34:17 +0200300
301static void set_proc_name(const char *s)
302{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700303#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200304 char name[16];
305 if (!s)
306 return;
307 name[sizeof(name) - 1] = 0;
308 strncpy(name, s, sizeof(name));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME, name);
312#endif
313}
aliguoridf751fa2008-12-04 20:19:35 +0000314
315/***************/
316/* ballooning */
317
318static QEMUBalloonEvent *qemu_balloon_event;
319void *qemu_balloon_event_opaque;
320
321void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
322{
323 qemu_balloon_event = func;
324 qemu_balloon_event_opaque = opaque;
325}
326
327void qemu_balloon(ram_addr_t target)
328{
329 if (qemu_balloon_event)
330 qemu_balloon_event(qemu_balloon_event_opaque, target);
331}
332
333ram_addr_t qemu_balloon_status(void)
334{
335 if (qemu_balloon_event)
336 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
337 return 0;
338}
bellard0824d6f2003-06-24 13:42:40 +0000339
bellard8a7ddc32004-03-31 19:00:16 +0000340/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000341/* keyboard/mouse */
342
343static QEMUPutKBDEvent *qemu_put_kbd_event;
344static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000345static QEMUPutMouseEntry *qemu_put_mouse_event_head;
346static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000347
348void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
349{
350 qemu_put_kbd_event_opaque = opaque;
351 qemu_put_kbd_event = func;
352}
353
ths455204e2007-01-05 16:42:13 +0000354QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
355 void *opaque, int absolute,
356 const char *name)
bellard63066f42004-06-03 18:45:02 +0000357{
ths455204e2007-01-05 16:42:13 +0000358 QEMUPutMouseEntry *s, *cursor;
359
360 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000361
362 s->qemu_put_mouse_event = func;
363 s->qemu_put_mouse_event_opaque = opaque;
364 s->qemu_put_mouse_event_absolute = absolute;
365 s->qemu_put_mouse_event_name = qemu_strdup(name);
366 s->next = NULL;
367
368 if (!qemu_put_mouse_event_head) {
369 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
370 return s;
371 }
372
373 cursor = qemu_put_mouse_event_head;
374 while (cursor->next != NULL)
375 cursor = cursor->next;
376
377 cursor->next = s;
378 qemu_put_mouse_event_current = s;
379
380 return s;
381}
382
383void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
384{
385 QEMUPutMouseEntry *prev = NULL, *cursor;
386
387 if (!qemu_put_mouse_event_head || entry == NULL)
388 return;
389
390 cursor = qemu_put_mouse_event_head;
391 while (cursor != NULL && cursor != entry) {
392 prev = cursor;
393 cursor = cursor->next;
394 }
395
396 if (cursor == NULL) // does not exist or list empty
397 return;
398 else if (prev == NULL) { // entry is head
399 qemu_put_mouse_event_head = cursor->next;
400 if (qemu_put_mouse_event_current == entry)
401 qemu_put_mouse_event_current = cursor->next;
402 qemu_free(entry->qemu_put_mouse_event_name);
403 qemu_free(entry);
404 return;
405 }
406
407 prev->next = entry->next;
408
409 if (qemu_put_mouse_event_current == entry)
410 qemu_put_mouse_event_current = prev;
411
412 qemu_free(entry->qemu_put_mouse_event_name);
413 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000414}
415
416void kbd_put_keycode(int keycode)
417{
418 if (qemu_put_kbd_event) {
419 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
420 }
421}
422
423void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
424{
ths455204e2007-01-05 16:42:13 +0000425 QEMUPutMouseEvent *mouse_event;
426 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000427 int width;
ths455204e2007-01-05 16:42:13 +0000428
429 if (!qemu_put_mouse_event_current) {
430 return;
431 }
432
433 mouse_event =
434 qemu_put_mouse_event_current->qemu_put_mouse_event;
435 mouse_event_opaque =
436 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
437
438 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000439 if (graphic_rotate) {
440 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
441 width = 0x7fff;
442 else
aurel32b94ed572008-03-10 19:34:27 +0000443 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000444 mouse_event(mouse_event_opaque,
445 width - dy, dx, dz, buttons_state);
446 } else
447 mouse_event(mouse_event_opaque,
448 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000449 }
450}
451
bellard09b26c52006-04-12 21:09:08 +0000452int kbd_mouse_is_absolute(void)
453{
ths455204e2007-01-05 16:42:13 +0000454 if (!qemu_put_mouse_event_current)
455 return 0;
456
457 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
458}
459
aliguori376253e2009-03-05 23:01:23 +0000460void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000461{
462 QEMUPutMouseEntry *cursor;
463 int index = 0;
464
465 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000466 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000467 return;
468 }
469
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000471 cursor = qemu_put_mouse_event_head;
472 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000473 monitor_printf(mon, "%c Mouse #%d: %s\n",
474 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
475 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000476 index++;
477 cursor = cursor->next;
478 }
479}
480
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300481void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000482{
483 QEMUPutMouseEntry *cursor;
484 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300485 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000486
487 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000488 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000489 return;
490 }
491
492 cursor = qemu_put_mouse_event_head;
493 while (cursor != NULL && index != i) {
494 i++;
495 cursor = cursor->next;
496 }
497
498 if (cursor != NULL)
499 qemu_put_mouse_event_current = cursor;
500 else
aliguori376253e2009-03-05 23:01:23 +0000501 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000502}
503
bellard87858c82003-06-27 12:01:39 +0000504/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000505uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000506{
507 union {
508 uint64_t ll;
509 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200510#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000511 uint32_t high, low;
512#else
513 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000514#endif
bellard87858c82003-06-27 12:01:39 +0000515 } l;
516 } u, res;
517 uint64_t rl, rh;
518
519 u.ll = a;
520 rl = (uint64_t)u.l.low * (uint64_t)b;
521 rh = (uint64_t)u.l.high * (uint64_t)b;
522 rh += (rl >> 32);
523 res.l.high = rh / c;
524 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
525 return res.ll;
526}
527
bellard1dce7c32006-07-13 23:20:22 +0000528/***********************************************************/
529/* real time host monotonic timer */
530
bellard1dce7c32006-07-13 23:20:22 +0000531#ifdef WIN32
532
533static int64_t clock_freq;
534
535static void init_get_clock(void)
536{
bellarda8e5ac32006-07-14 09:36:13 +0000537 LARGE_INTEGER freq;
538 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000539 ret = QueryPerformanceFrequency(&freq);
540 if (ret == 0) {
541 fprintf(stderr, "Could not calibrate ticks\n");
542 exit(1);
543 }
544 clock_freq = freq.QuadPart;
545}
546
547static int64_t get_clock(void)
548{
549 LARGE_INTEGER ti;
550 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500551 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000552}
553
554#else
555
556static int use_rt_clock;
557
558static void init_get_clock(void)
559{
560 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000561#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
562 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000563 {
564 struct timespec ts;
565 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
566 use_rt_clock = 1;
567 }
568 }
569#endif
570}
571
572static int64_t get_clock(void)
573{
blueswir1c5e97232009-03-07 20:06:23 +0000574#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
575 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000576 if (use_rt_clock) {
577 struct timespec ts;
578 clock_gettime(CLOCK_MONOTONIC, &ts);
579 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000580 } else
bellard1dce7c32006-07-13 23:20:22 +0000581#endif
582 {
583 /* XXX: using gettimeofday leads to problems if the date
584 changes, so it should be avoided. */
585 struct timeval tv;
586 gettimeofday(&tv, NULL);
587 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
588 }
589}
bellard1dce7c32006-07-13 23:20:22 +0000590#endif
591
pbrook2e70f6e2008-06-29 01:03:05 +0000592/* Return the virtual CPU time, based on the instruction counter. */
593static int64_t cpu_get_icount(void)
594{
595 int64_t icount;
596 CPUState *env = cpu_single_env;;
597 icount = qemu_icount;
598 if (env) {
599 if (!can_do_io(env))
600 fprintf(stderr, "Bad clock read\n");
601 icount -= (env->icount_decr.u16.low + env->icount_extra);
602 }
603 return qemu_icount_bias + (icount << icount_time_shift);
604}
605
bellard1dce7c32006-07-13 23:20:22 +0000606/***********************************************************/
607/* guest cycle counter */
608
Juan Quintela6f68e332009-09-10 03:04:27 +0200609typedef struct TimersState {
610 int64_t cpu_ticks_prev;
611 int64_t cpu_ticks_offset;
612 int64_t cpu_clock_offset;
613 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500614 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200615} TimersState;
616
617TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000618
619/* return the host CPU cycle counter and handle stop/restart */
620int64_t cpu_get_ticks(void)
621{
pbrook2e70f6e2008-06-29 01:03:05 +0000622 if (use_icount) {
623 return cpu_get_icount();
624 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200625 if (!timers_state.cpu_ticks_enabled) {
626 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000627 } else {
628 int64_t ticks;
629 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200630 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000631 /* Note: non increasing ticks may happen if the host uses
632 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200633 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000634 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200635 timers_state.cpu_ticks_prev = ticks;
636 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000637 }
638}
639
640/* return the host CPU monotonic timer and handle stop/restart */
641static int64_t cpu_get_clock(void)
642{
643 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200644 if (!timers_state.cpu_ticks_enabled) {
645 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000646 } else {
647 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200648 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000649 }
650}
651
652/* enable cpu_get_ticks() */
653void cpu_enable_ticks(void)
654{
Juan Quintela6f68e332009-09-10 03:04:27 +0200655 if (!timers_state.cpu_ticks_enabled) {
656 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
657 timers_state.cpu_clock_offset -= get_clock();
658 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000659 }
660}
661
662/* disable cpu_get_ticks() : the clock is stopped. You must not call
663 cpu_get_ticks() after that. */
664void cpu_disable_ticks(void)
665{
Juan Quintela6f68e332009-09-10 03:04:27 +0200666 if (timers_state.cpu_ticks_enabled) {
667 timers_state.cpu_ticks_offset = cpu_get_ticks();
668 timers_state.cpu_clock_offset = cpu_get_clock();
669 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000670 }
671}
672
673/***********************************************************/
674/* timers */
ths5fafdf22007-09-16 21:08:06 +0000675
bellard8a7ddc32004-03-31 19:00:16 +0000676#define QEMU_TIMER_REALTIME 0
677#define QEMU_TIMER_VIRTUAL 1
678
679struct QEMUClock {
680 int type;
681 /* XXX: add frequency */
682};
683
684struct QEMUTimer {
685 QEMUClock *clock;
686 int64_t expire_time;
687 QEMUTimerCB *cb;
688 void *opaque;
689 struct QEMUTimer *next;
690};
691
thsc8994012007-08-19 21:56:03 +0000692struct qemu_alarm_timer {
693 char const *name;
thsefe75412007-08-24 01:36:32 +0000694 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000695
696 int (*start)(struct qemu_alarm_timer *t);
697 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000698 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000699 void *priv;
700};
701
thsefe75412007-08-24 01:36:32 +0000702#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000703#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000704
705static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
706{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200707 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000708}
709
710static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
711{
712 if (!alarm_has_dynticks(t))
713 return;
714
715 t->rearm(t);
716}
717
718/* TODO: MIN_TIMER_REARM_US should be optimized */
719#define MIN_TIMER_REARM_US 250
720
thsc8994012007-08-19 21:56:03 +0000721static struct qemu_alarm_timer *alarm_timer;
722
723#ifdef _WIN32
724
725struct qemu_alarm_win32 {
726 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000727 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000728} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000729
730static int win32_start_timer(struct qemu_alarm_timer *t);
731static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000732static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000733
734#else
735
736static int unix_start_timer(struct qemu_alarm_timer *t);
737static void unix_stop_timer(struct qemu_alarm_timer *t);
738
ths231c6582007-08-26 17:29:15 +0000739#ifdef __linux__
740
thsefe75412007-08-24 01:36:32 +0000741static int dynticks_start_timer(struct qemu_alarm_timer *t);
742static void dynticks_stop_timer(struct qemu_alarm_timer *t);
743static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
744
thsc40ec5a2007-08-19 22:09:40 +0000745static int hpet_start_timer(struct qemu_alarm_timer *t);
746static void hpet_stop_timer(struct qemu_alarm_timer *t);
747
thsc8994012007-08-19 21:56:03 +0000748static int rtc_start_timer(struct qemu_alarm_timer *t);
749static void rtc_stop_timer(struct qemu_alarm_timer *t);
750
thsefe75412007-08-24 01:36:32 +0000751#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000752
753#endif /* _WIN32 */
754
pbrook2e70f6e2008-06-29 01:03:05 +0000755/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000756 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000757 When the guest is idle real and virtual time will be aligned in
758 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500759#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000760
761static void icount_adjust(void)
762{
763 int64_t cur_time;
764 int64_t cur_icount;
765 int64_t delta;
766 static int64_t last_delta;
767 /* If the VM is not running, then do nothing. */
768 if (!vm_running)
769 return;
770
771 cur_time = cpu_get_clock();
772 cur_icount = qemu_get_clock(vm_clock);
773 delta = cur_icount - cur_time;
774 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
775 if (delta > 0
776 && last_delta + ICOUNT_WOBBLE < delta * 2
777 && icount_time_shift > 0) {
778 /* The guest is getting too far ahead. Slow time down. */
779 icount_time_shift--;
780 }
781 if (delta < 0
782 && last_delta - ICOUNT_WOBBLE > delta * 2
783 && icount_time_shift < MAX_ICOUNT_SHIFT) {
784 /* The guest is getting too far behind. Speed time up. */
785 icount_time_shift++;
786 }
787 last_delta = delta;
788 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
789}
790
791static void icount_adjust_rt(void * opaque)
792{
793 qemu_mod_timer(icount_rt_timer,
794 qemu_get_clock(rt_clock) + 1000);
795 icount_adjust();
796}
797
798static void icount_adjust_vm(void * opaque)
799{
800 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500801 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000802 icount_adjust();
803}
804
805static void init_icount_adjust(void)
806{
807 /* Have both realtime and virtual time triggers for speed adjustment.
808 The realtime trigger catches emulated time passing too slowly,
809 the virtual time trigger catches emulated time passing too fast.
810 Realtime triggers occur even when idle, so use them less frequently
811 than VM triggers. */
812 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
813 qemu_mod_timer(icount_rt_timer,
814 qemu_get_clock(rt_clock) + 1000);
815 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
816 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500817 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000818}
819
thsc8994012007-08-19 21:56:03 +0000820static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000821#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000822#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000823 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
824 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000825 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000826 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000827 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000828 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000829#endif
thsefe75412007-08-24 01:36:32 +0000830 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000831#else
thsefe75412007-08-24 01:36:32 +0000832 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
833 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
834 {"win32", 0, win32_start_timer,
835 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000836#endif
837 {NULL, }
838};
839
blueswir13f47aa82008-03-09 06:59:01 +0000840static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000841{
842 int i;
843
844 printf("Available alarm timers, in order of precedence:\n");
845 for (i = 0; alarm_timers[i].name; i++)
846 printf("%s\n", alarm_timers[i].name);
847}
848
849static void configure_alarms(char const *opt)
850{
851 int i;
852 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000853 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000854 char *arg;
855 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000856 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000857
aurel323adda042008-03-09 23:43:49 +0000858 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000859 show_available_alarms();
860 exit(0);
861 }
862
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200863 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000864
865 /* Reorder the array */
866 name = strtok(arg, ",");
867 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000868 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000869 if (!strcmp(alarm_timers[i].name, name))
870 break;
871 }
872
873 if (i == count) {
874 fprintf(stderr, "Unknown clock %s\n", name);
875 goto next;
876 }
877
878 if (i < cur)
879 /* Ignore */
880 goto next;
881
882 /* Swap */
883 tmp = alarm_timers[i];
884 alarm_timers[i] = alarm_timers[cur];
885 alarm_timers[cur] = tmp;
886
887 cur++;
888next:
889 name = strtok(NULL, ",");
890 }
891
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200892 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000893
894 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000895 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000896 for (i = cur; i < count; i++)
897 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000898 } else {
899 show_available_alarms();
900 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000901 }
thsf3dcfad2007-08-24 01:26:02 +0000902}
903
bellard8a7ddc32004-03-31 19:00:16 +0000904QEMUClock *rt_clock;
905QEMUClock *vm_clock;
906
907static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000908
pbrook9596ebb2007-11-18 01:44:38 +0000909static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000910{
911 QEMUClock *clock;
912 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000913 clock->type = type;
914 return clock;
915}
916
917QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
918{
919 QEMUTimer *ts;
920
921 ts = qemu_mallocz(sizeof(QEMUTimer));
922 ts->clock = clock;
923 ts->cb = cb;
924 ts->opaque = opaque;
925 return ts;
926}
927
928void qemu_free_timer(QEMUTimer *ts)
929{
930 qemu_free(ts);
931}
932
933/* stop a timer, but do not dealloc it */
934void qemu_del_timer(QEMUTimer *ts)
935{
936 QEMUTimer **pt, *t;
937
938 /* NOTE: this code must be signal safe because
939 qemu_timer_expired() can be called from a signal. */
940 pt = &active_timers[ts->clock->type];
941 for(;;) {
942 t = *pt;
943 if (!t)
944 break;
945 if (t == ts) {
946 *pt = t->next;
947 break;
948 }
949 pt = &t->next;
950 }
951}
952
953/* modify the current timer so that it will be fired when current_time
954 >= expire_time. The corresponding callback will be called. */
955void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
956{
957 QEMUTimer **pt, *t;
958
959 qemu_del_timer(ts);
960
961 /* add the timer in the sorted list */
962 /* NOTE: this code must be signal safe because
963 qemu_timer_expired() can be called from a signal. */
964 pt = &active_timers[ts->clock->type];
965 for(;;) {
966 t = *pt;
967 if (!t)
968 break;
ths5fafdf22007-09-16 21:08:06 +0000969 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000970 break;
971 pt = &t->next;
972 }
973 ts->expire_time = expire_time;
974 ts->next = *pt;
975 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000976
977 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000978 if (pt == &active_timers[ts->clock->type]) {
979 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
980 qemu_rearm_alarm_timer(alarm_timer);
981 }
982 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000983 if (use_icount)
984 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000985 }
bellard8a7ddc32004-03-31 19:00:16 +0000986}
987
988int qemu_timer_pending(QEMUTimer *ts)
989{
990 QEMUTimer *t;
991 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
992 if (t == ts)
993 return 1;
994 }
995 return 0;
996}
997
Stefano Stabellini2430ffe2009-08-03 10:56:01 +0100998int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +0000999{
1000 if (!timer_head)
1001 return 0;
1002 return (timer_head->expire_time <= current_time);
1003}
1004
1005static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1006{
1007 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001008
bellard8a7ddc32004-03-31 19:00:16 +00001009 for(;;) {
1010 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001011 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001012 break;
1013 /* remove timer from the list before calling the callback */
1014 *ptimer_head = ts->next;
1015 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001016
bellard8a7ddc32004-03-31 19:00:16 +00001017 /* run the callback (the timer list can be modified) */
1018 ts->cb(ts->opaque);
1019 }
1020}
1021
1022int64_t qemu_get_clock(QEMUClock *clock)
1023{
1024 switch(clock->type) {
1025 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001026 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001027 default:
1028 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001029 if (use_icount) {
1030 return cpu_get_icount();
1031 } else {
1032 return cpu_get_clock();
1033 }
bellard8a7ddc32004-03-31 19:00:16 +00001034 }
1035}
1036
bellard1dce7c32006-07-13 23:20:22 +00001037static void init_timers(void)
1038{
1039 init_get_clock();
bellard1dce7c32006-07-13 23:20:22 +00001040 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1041 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1042}
1043
bellard8a7ddc32004-03-31 19:00:16 +00001044/* save a timer */
1045void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1046{
1047 uint64_t expire_time;
1048
1049 if (qemu_timer_pending(ts)) {
1050 expire_time = ts->expire_time;
1051 } else {
1052 expire_time = -1;
1053 }
1054 qemu_put_be64(f, expire_time);
1055}
1056
1057void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1058{
1059 uint64_t expire_time;
1060
1061 expire_time = qemu_get_be64(f);
1062 if (expire_time != -1) {
1063 qemu_mod_timer(ts, expire_time);
1064 } else {
1065 qemu_del_timer(ts);
1066 }
1067}
1068
Juan Quintela2faf58c2009-09-10 03:04:28 +02001069static const VMStateDescription vmstate_timers = {
1070 .name = "timer",
1071 .version_id = 2,
1072 .minimum_version_id = 1,
1073 .minimum_version_id_old = 1,
1074 .fields = (VMStateField []) {
1075 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001076 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001077 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1078 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001079 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001080};
bellard8a7ddc32004-03-31 19:00:16 +00001081
aliguori50317c72009-04-24 18:03:29 +00001082static void qemu_event_increment(void);
1083
bellard67b915a2004-03-31 23:37:16 +00001084#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001085static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1086 DWORD_PTR dwUser, DWORD_PTR dw1,
1087 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001088#else
bellard8a7ddc32004-03-31 19:00:16 +00001089static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001090#endif
bellard8a7ddc32004-03-31 19:00:16 +00001091{
bellard02ba45c2004-06-25 14:46:23 +00001092#if 0
1093#define DISP_FREQ 1000
1094 {
1095 static int64_t delta_min = INT64_MAX;
1096 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1097 static int count;
1098 ti = qemu_get_clock(vm_clock);
1099 if (last_clock != 0) {
1100 delta = ti - last_clock;
1101 if (delta < delta_min)
1102 delta_min = delta;
1103 if (delta > delta_max)
1104 delta_max = delta;
1105 delta_cum += delta;
1106 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001107 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001108 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1109 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1110 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1111 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001112 count = 0;
1113 delta_min = INT64_MAX;
1114 delta_max = 0;
1115 delta_cum = 0;
1116 }
1117 }
1118 last_clock = ti;
1119 }
1120#endif
thsefe75412007-08-24 01:36:32 +00001121 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001122 (!use_icount &&
1123 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1124 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001125 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1126 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001127 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001128 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001129
aliguorid6dc3d42009-04-24 18:04:07 +00001130#ifndef CONFIG_IOTHREAD
1131 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001132 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001133 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001134 }
aliguorid6dc3d42009-04-24 18:04:07 +00001135#endif
aliguori43b96852009-04-24 18:03:33 +00001136 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001137 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001138 }
1139}
1140
pbrook2e70f6e2008-06-29 01:03:05 +00001141static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001142{
pbrook2e70f6e2008-06-29 01:03:05 +00001143 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001144
1145 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001146 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1147 qemu_get_clock(vm_clock);
1148 } else {
1149 /* To avoid problems with overflow limit this to 2^32. */
1150 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001151 }
1152
pbrook2e70f6e2008-06-29 01:03:05 +00001153 if (delta < 0)
1154 delta = 0;
thsefe75412007-08-24 01:36:32 +00001155
pbrook2e70f6e2008-06-29 01:03:05 +00001156 return delta;
1157}
1158
blueswir18632fb92008-09-14 13:59:34 +00001159#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001160static uint64_t qemu_next_deadline_dyntick(void)
1161{
1162 int64_t delta;
1163 int64_t rtdelta;
1164
1165 if (use_icount)
1166 delta = INT32_MAX;
1167 else
1168 delta = (qemu_next_deadline() + 999) / 1000;
1169
1170 if (active_timers[QEMU_TIMER_REALTIME]) {
1171 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1172 qemu_get_clock(rt_clock))*1000;
1173 if (rtdelta < delta)
1174 delta = rtdelta;
1175 }
1176
1177 if (delta < MIN_TIMER_REARM_US)
1178 delta = MIN_TIMER_REARM_US;
1179
1180 return delta;
thsefe75412007-08-24 01:36:32 +00001181}
blueswir18632fb92008-09-14 13:59:34 +00001182#endif
thsefe75412007-08-24 01:36:32 +00001183
bellardfd872592004-05-12 19:11:15 +00001184#ifndef _WIN32
1185
aliguori7183b4b2008-11-05 20:40:18 +00001186/* Sets a specific flag */
1187static int fcntl_setfl(int fd, int flag)
1188{
1189 int flags;
1190
1191 flags = fcntl(fd, F_GETFL);
1192 if (flags == -1)
1193 return -errno;
1194
1195 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1196 return -errno;
1197
1198 return 0;
1199}
1200
bellard829309c2004-05-20 13:20:12 +00001201#if defined(__linux__)
1202
bellardfd872592004-05-12 19:11:15 +00001203#define RTC_FREQ 1024
1204
aurel32de9a95f2008-11-11 13:41:01 +00001205static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001206{
thsc8994012007-08-19 21:56:03 +00001207 struct sigaction act;
1208
1209 /* timer signal */
1210 sigfillset(&act.sa_mask);
1211 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001212 act.sa_handler = host_alarm_handler;
1213
1214 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001215 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001216 fcntl(fd, F_SETOWN, getpid());
1217}
1218
thsc40ec5a2007-08-19 22:09:40 +00001219static int hpet_start_timer(struct qemu_alarm_timer *t)
1220{
1221 struct hpet_info info;
1222 int r, fd;
1223
1224 fd = open("/dev/hpet", O_RDONLY);
1225 if (fd < 0)
1226 return -1;
1227
1228 /* Set frequency */
1229 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1230 if (r < 0) {
1231 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1232 "error, but for better emulation accuracy type:\n"
1233 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1234 goto fail;
1235 }
1236
1237 /* Check capabilities */
1238 r = ioctl(fd, HPET_INFO, &info);
1239 if (r < 0)
1240 goto fail;
1241
1242 /* Enable periodic mode */
1243 r = ioctl(fd, HPET_EPI, 0);
1244 if (info.hi_flags && (r < 0))
1245 goto fail;
1246
1247 /* Enable interrupt */
1248 r = ioctl(fd, HPET_IE_ON, 0);
1249 if (r < 0)
1250 goto fail;
1251
1252 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001253 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001254
1255 return 0;
1256fail:
1257 close(fd);
1258 return -1;
1259}
1260
1261static void hpet_stop_timer(struct qemu_alarm_timer *t)
1262{
pbrookfcdc2122007-08-23 20:22:22 +00001263 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001264
1265 close(fd);
1266}
1267
thsc8994012007-08-19 21:56:03 +00001268static int rtc_start_timer(struct qemu_alarm_timer *t)
1269{
1270 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001271 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001272
balrogaeb30be2007-07-02 15:03:13 +00001273 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001274 if (rtc_fd < 0)
1275 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001276 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1277 if (current_rtc_freq != RTC_FREQ &&
1278 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001279 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1280 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1281 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1282 goto fail;
1283 }
1284 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1285 fail:
1286 close(rtc_fd);
1287 return -1;
1288 }
thsc8994012007-08-19 21:56:03 +00001289
1290 enable_sigio_timer(rtc_fd);
1291
pbrookfcdc2122007-08-23 20:22:22 +00001292 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001293
bellardfd872592004-05-12 19:11:15 +00001294 return 0;
1295}
1296
thsc8994012007-08-19 21:56:03 +00001297static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001298{
pbrookfcdc2122007-08-23 20:22:22 +00001299 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001300
1301 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001302}
1303
thsefe75412007-08-24 01:36:32 +00001304static int dynticks_start_timer(struct qemu_alarm_timer *t)
1305{
1306 struct sigevent ev;
1307 timer_t host_timer;
1308 struct sigaction act;
1309
1310 sigfillset(&act.sa_mask);
1311 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001312 act.sa_handler = host_alarm_handler;
1313
1314 sigaction(SIGALRM, &act, NULL);
1315
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001316 /*
1317 * Initialize ev struct to 0 to avoid valgrind complaining
1318 * about uninitialized data in timer_create call
1319 */
1320 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001321 ev.sigev_value.sival_int = 0;
1322 ev.sigev_notify = SIGEV_SIGNAL;
1323 ev.sigev_signo = SIGALRM;
1324
1325 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1326 perror("timer_create");
1327
1328 /* disable dynticks */
1329 fprintf(stderr, "Dynamic Ticks disabled\n");
1330
1331 return -1;
1332 }
1333
blueswir10399bfe2008-11-16 11:37:18 +00001334 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001335
1336 return 0;
1337}
1338
1339static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1340{
blueswir10399bfe2008-11-16 11:37:18 +00001341 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001342
1343 timer_delete(host_timer);
1344}
1345
1346static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1347{
blueswir10399bfe2008-11-16 11:37:18 +00001348 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001349 struct itimerspec timeout;
1350 int64_t nearest_delta_us = INT64_MAX;
1351 int64_t current_us;
1352
1353 if (!active_timers[QEMU_TIMER_REALTIME] &&
1354 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001355 return;
thsefe75412007-08-24 01:36:32 +00001356
pbrook2e70f6e2008-06-29 01:03:05 +00001357 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001358
1359 /* check whether a timer is already running */
1360 if (timer_gettime(host_timer, &timeout)) {
1361 perror("gettime");
1362 fprintf(stderr, "Internal timer error: aborting\n");
1363 exit(1);
1364 }
1365 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1366 if (current_us && current_us <= nearest_delta_us)
1367 return;
1368
1369 timeout.it_interval.tv_sec = 0;
1370 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1371 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1372 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1373 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1374 perror("settime");
1375 fprintf(stderr, "Internal timer error: aborting\n");
1376 exit(1);
1377 }
1378}
1379
ths70744b32007-08-26 17:31:30 +00001380#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001381
thsc8994012007-08-19 21:56:03 +00001382static int unix_start_timer(struct qemu_alarm_timer *t)
1383{
1384 struct sigaction act;
1385 struct itimerval itv;
1386 int err;
1387
1388 /* timer signal */
1389 sigfillset(&act.sa_mask);
1390 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001391 act.sa_handler = host_alarm_handler;
1392
1393 sigaction(SIGALRM, &act, NULL);
1394
1395 itv.it_interval.tv_sec = 0;
1396 /* for i386 kernel 2.6 to get 1 ms */
1397 itv.it_interval.tv_usec = 999;
1398 itv.it_value.tv_sec = 0;
1399 itv.it_value.tv_usec = 10 * 1000;
1400
1401 err = setitimer(ITIMER_REAL, &itv, NULL);
1402 if (err)
1403 return -1;
1404
1405 return 0;
1406}
1407
1408static void unix_stop_timer(struct qemu_alarm_timer *t)
1409{
1410 struct itimerval itv;
1411
1412 memset(&itv, 0, sizeof(itv));
1413 setitimer(ITIMER_REAL, &itv, NULL);
1414}
1415
bellard829309c2004-05-20 13:20:12 +00001416#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001417
aliguorif49e58d2008-11-05 21:22:34 +00001418
thsc8994012007-08-19 21:56:03 +00001419#ifdef _WIN32
1420
1421static int win32_start_timer(struct qemu_alarm_timer *t)
1422{
1423 TIMECAPS tc;
1424 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001425 UINT flags;
thsc8994012007-08-19 21:56:03 +00001426
thsc8994012007-08-19 21:56:03 +00001427 memset(&tc, 0, sizeof(tc));
1428 timeGetDevCaps(&tc, sizeof(tc));
1429
1430 if (data->period < tc.wPeriodMin)
1431 data->period = tc.wPeriodMin;
1432
1433 timeBeginPeriod(data->period);
1434
thsefe75412007-08-24 01:36:32 +00001435 flags = TIME_CALLBACK_FUNCTION;
1436 if (alarm_has_dynticks(t))
1437 flags |= TIME_ONESHOT;
1438 else
1439 flags |= TIME_PERIODIC;
1440
thsc8994012007-08-19 21:56:03 +00001441 data->timerId = timeSetEvent(1, // interval (ms)
1442 data->period, // resolution
1443 host_alarm_handler, // function
1444 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001445 flags);
thsc8994012007-08-19 21:56:03 +00001446
1447 if (!data->timerId) {
1448 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001449 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001450 return -1;
1451 }
1452
thsc8994012007-08-19 21:56:03 +00001453 return 0;
1454}
1455
1456static void win32_stop_timer(struct qemu_alarm_timer *t)
1457{
1458 struct qemu_alarm_win32 *data = t->priv;
1459
1460 timeKillEvent(data->timerId);
1461 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001462}
1463
thsefe75412007-08-24 01:36:32 +00001464static void win32_rearm_timer(struct qemu_alarm_timer *t)
1465{
1466 struct qemu_alarm_win32 *data = t->priv;
1467 uint64_t nearest_delta_us;
1468
1469 if (!active_timers[QEMU_TIMER_REALTIME] &&
1470 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001471 return;
thsefe75412007-08-24 01:36:32 +00001472
pbrook2e70f6e2008-06-29 01:03:05 +00001473 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001474 nearest_delta_us /= 1000;
1475
1476 timeKillEvent(data->timerId);
1477
1478 data->timerId = timeSetEvent(1,
1479 data->period,
1480 host_alarm_handler,
1481 (DWORD)t,
1482 TIME_ONESHOT | TIME_PERIODIC);
1483
1484 if (!data->timerId) {
1485 perror("Failed to re-arm win32 alarm timer");
1486
1487 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001488 exit(1);
1489 }
1490}
1491
thsc8994012007-08-19 21:56:03 +00001492#endif /* _WIN32 */
1493
aliguori7183b4b2008-11-05 20:40:18 +00001494static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001495{
blueswir1223f0d72008-09-30 18:12:18 +00001496 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001497 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001498
thsc8994012007-08-19 21:56:03 +00001499 for (i = 0; alarm_timers[i].name; i++) {
1500 t = &alarm_timers[i];
1501
thsc8994012007-08-19 21:56:03 +00001502 err = t->start(t);
1503 if (!err)
1504 break;
bellard67b915a2004-03-31 23:37:16 +00001505 }
bellardfd872592004-05-12 19:11:15 +00001506
thsc8994012007-08-19 21:56:03 +00001507 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001508 err = -ENOENT;
1509 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001510 }
thsc8994012007-08-19 21:56:03 +00001511
1512 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001513
aliguori6abfbd72008-11-05 20:49:37 +00001514 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001515
1516fail:
aliguori7183b4b2008-11-05 20:40:18 +00001517 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001518}
1519
pbrook9596ebb2007-11-18 01:44:38 +00001520static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001521{
thsc8994012007-08-19 21:56:03 +00001522 alarm_timer->stop(alarm_timer);
1523 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001524}
1525
bellardc4b1fcc2004-03-14 21:44:30 +00001526/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001527/* host time/date access */
1528void qemu_get_timedate(struct tm *tm, int offset)
1529{
1530 time_t ti;
1531 struct tm *ret;
1532
1533 time(&ti);
1534 ti += offset;
1535 if (rtc_date_offset == -1) {
1536 if (rtc_utc)
1537 ret = gmtime(&ti);
1538 else
1539 ret = localtime(&ti);
1540 } else {
1541 ti -= rtc_date_offset;
1542 ret = gmtime(&ti);
1543 }
1544
1545 memcpy(tm, ret, sizeof(struct tm));
1546}
1547
1548int qemu_timedate_diff(struct tm *tm)
1549{
1550 time_t seconds;
1551
1552 if (rtc_date_offset == -1)
1553 if (rtc_utc)
1554 seconds = mktimegm(tm);
1555 else
1556 seconds = mktime(tm);
1557 else
1558 seconds = mktimegm(tm) + rtc_date_offset;
1559
1560 return seconds - time(NULL);
1561}
1562
bellardfd1dff42006-02-01 21:29:26 +00001563#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001564static void socket_cleanup(void)
1565{
1566 WSACleanup();
1567}
bellard82c643f2004-07-14 17:28:13 +00001568
bellardfd1dff42006-02-01 21:29:26 +00001569static int socket_init(void)
1570{
1571 WSADATA Data;
1572 int ret, err;
1573
1574 ret = WSAStartup(MAKEWORD(2,2), &Data);
1575 if (ret != 0) {
1576 err = WSAGetLastError();
1577 fprintf(stderr, "WSAStartup: %d\n", err);
1578 return -1;
1579 }
1580 atexit(socket_cleanup);
1581 return 0;
1582}
aurel3264b7b732008-05-05 10:05:31 +00001583#endif
1584
balrog1ae26a12008-09-28 23:19:47 +00001585/***********************************************************/
1586/* Bluetooth support */
1587static int nb_hcis;
1588static int cur_hci;
1589static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001590
balrog1ae26a12008-09-28 23:19:47 +00001591static struct bt_vlan_s {
1592 struct bt_scatternet_s net;
1593 int id;
1594 struct bt_vlan_s *next;
1595} *first_bt_vlan;
1596
1597/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001598static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001599{
1600 struct bt_vlan_s **pvlan, *vlan;
1601 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1602 if (vlan->id == id)
1603 return &vlan->net;
1604 }
1605 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1606 vlan->id = id;
1607 pvlan = &first_bt_vlan;
1608 while (*pvlan != NULL)
1609 pvlan = &(*pvlan)->next;
1610 *pvlan = vlan;
1611 return &vlan->net;
1612}
1613
1614static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1615{
1616}
1617
1618static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1619{
1620 return -ENOTSUP;
1621}
1622
1623static struct HCIInfo null_hci = {
1624 .cmd_send = null_hci_send,
1625 .sco_send = null_hci_send,
1626 .acl_send = null_hci_send,
1627 .bdaddr_set = null_hci_addr_set,
1628};
1629
1630struct HCIInfo *qemu_next_hci(void)
1631{
1632 if (cur_hci == nb_hcis)
1633 return &null_hci;
1634
1635 return hci_table[cur_hci++];
1636}
1637
balrogdc72ac12008-11-09 00:04:26 +00001638static struct HCIInfo *hci_init(const char *str)
1639{
1640 char *endp;
1641 struct bt_scatternet_s *vlan = 0;
1642
1643 if (!strcmp(str, "null"))
1644 /* null */
1645 return &null_hci;
1646 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1647 /* host[:hciN] */
1648 return bt_host_hci(str[4] ? str + 5 : "hci0");
1649 else if (!strncmp(str, "hci", 3)) {
1650 /* hci[,vlan=n] */
1651 if (str[3]) {
1652 if (!strncmp(str + 3, ",vlan=", 6)) {
1653 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1654 if (*endp)
1655 vlan = 0;
1656 }
1657 } else
1658 vlan = qemu_find_bt_vlan(0);
1659 if (vlan)
1660 return bt_new_hci(vlan);
1661 }
1662
1663 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1664
1665 return 0;
1666}
1667
1668static int bt_hci_parse(const char *str)
1669{
1670 struct HCIInfo *hci;
1671 bdaddr_t bdaddr;
1672
1673 if (nb_hcis >= MAX_NICS) {
1674 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1675 return -1;
1676 }
1677
1678 hci = hci_init(str);
1679 if (!hci)
1680 return -1;
1681
1682 bdaddr.b[0] = 0x52;
1683 bdaddr.b[1] = 0x54;
1684 bdaddr.b[2] = 0x00;
1685 bdaddr.b[3] = 0x12;
1686 bdaddr.b[4] = 0x34;
1687 bdaddr.b[5] = 0x56 + nb_hcis;
1688 hci->bdaddr_set(hci, bdaddr.b);
1689
1690 hci_table[nb_hcis++] = hci;
1691
1692 return 0;
1693}
1694
1695static void bt_vhci_add(int vlan_id)
1696{
1697 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1698
1699 if (!vlan->slave)
1700 fprintf(stderr, "qemu: warning: adding a VHCI to "
1701 "an empty scatternet %i\n", vlan_id);
1702
1703 bt_vhci_init(bt_new_hci(vlan));
1704}
1705
1706static struct bt_device_s *bt_device_add(const char *opt)
1707{
1708 struct bt_scatternet_s *vlan;
1709 int vlan_id = 0;
1710 char *endp = strstr(opt, ",vlan=");
1711 int len = (endp ? endp - opt : strlen(opt)) + 1;
1712 char devname[10];
1713
1714 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1715
1716 if (endp) {
1717 vlan_id = strtol(endp + 6, &endp, 0);
1718 if (*endp) {
1719 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1720 return 0;
1721 }
1722 }
1723
1724 vlan = qemu_find_bt_vlan(vlan_id);
1725
1726 if (!vlan->slave)
1727 fprintf(stderr, "qemu: warning: adding a slave device to "
1728 "an empty scatternet %i\n", vlan_id);
1729
1730 if (!strcmp(devname, "keyboard"))
1731 return bt_keyboard_init(vlan);
1732
1733 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1734 return 0;
1735}
1736
1737static int bt_parse(const char *opt)
1738{
1739 const char *endp, *p;
1740 int vlan;
1741
1742 if (strstart(opt, "hci", &endp)) {
1743 if (!*endp || *endp == ',') {
1744 if (*endp)
1745 if (!strstart(endp, ",vlan=", 0))
1746 opt = endp + 1;
1747
1748 return bt_hci_parse(opt);
1749 }
1750 } else if (strstart(opt, "vhci", &endp)) {
1751 if (!*endp || *endp == ',') {
1752 if (*endp) {
1753 if (strstart(endp, ",vlan=", &p)) {
1754 vlan = strtol(p, (char **) &endp, 0);
1755 if (*endp) {
1756 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1757 return 1;
1758 }
1759 } else {
1760 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1761 return 1;
1762 }
1763 } else
1764 vlan = 0;
1765
1766 bt_vhci_add(vlan);
1767 return 0;
1768 }
1769 } else if (strstart(opt, "device:", &endp))
1770 return !bt_device_add(endp);
1771
1772 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1773 return 1;
1774}
1775
balrog1ae26a12008-09-28 23:19:47 +00001776/***********************************************************/
1777/* QEMU Block devices */
1778
balrog609497a2008-01-14 02:56:53 +00001779#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001780#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001781#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001782#define PFLASH_ALIAS "if=pflash"
1783#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001784#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001785
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001786QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001787{
1788 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001789 char optstr[1024];
1790 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001791
thse4bcb142007-12-02 04:51:10 +00001792 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001793 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001794 va_end(ap);
1795
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001796 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001797 if (!opts) {
1798 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1799 __FUNCTION__, optstr);
1800 return NULL;
1801 }
1802 if (file)
1803 qemu_opt_set(opts, "file", file);
1804 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001805}
1806
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001807DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001808{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001809 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001810
1811 /* seek interface, bus and unit */
1812
Blue Swirl72cf2d42009-09-12 07:36:22 +00001813 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001814 if (dinfo->type == type &&
1815 dinfo->bus == bus &&
1816 dinfo->unit == unit)
1817 return dinfo;
1818 }
thse4bcb142007-12-02 04:51:10 +00001819
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001820 return NULL;
thse4bcb142007-12-02 04:51:10 +00001821}
1822
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001823DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001824{
1825 DriveInfo *dinfo;
1826
Blue Swirl72cf2d42009-09-12 07:36:22 +00001827 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001828 if (strcmp(id, dinfo->id))
1829 continue;
1830 return dinfo;
1831 }
1832 return NULL;
1833}
1834
thsf60d39b2007-12-17 03:55:57 +00001835int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001836{
1837 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001838 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001839
1840 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001841 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001842 if(dinfo->type == type &&
1843 dinfo->bus > max_bus)
1844 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001845 }
1846 return max_bus;
1847}
1848
aliguorifa879c62009-01-07 17:32:33 +00001849const char *drive_get_serial(BlockDriverState *bdrv)
1850{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001851 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001852
Blue Swirl72cf2d42009-09-12 07:36:22 +00001853 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001854 if (dinfo->bdrv == bdrv)
1855 return dinfo->serial;
1856 }
aliguorifa879c62009-01-07 17:32:33 +00001857
1858 return "\0";
1859}
1860
aliguori428c5702009-01-21 18:59:04 +00001861BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1862{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001863 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001864
Blue Swirl72cf2d42009-09-12 07:36:22 +00001865 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001866 if (dinfo->bdrv == bdrv)
1867 return dinfo->onerror;
1868 }
aliguori428c5702009-01-21 18:59:04 +00001869
aliguoricdad4bd2009-02-28 16:51:01 +00001870 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001871}
1872
aurel32a1620fa2008-04-29 05:58:01 +00001873static void bdrv_format_print(void *opaque, const char *name)
1874{
1875 fprintf(stderr, " %s", name);
1876}
1877
aliguorib01b1112009-02-11 15:20:20 +00001878void drive_uninit(BlockDriverState *bdrv)
1879{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001880 DriveInfo *dinfo;
aliguorib01b1112009-02-11 15:20:20 +00001881
Blue Swirl72cf2d42009-09-12 07:36:22 +00001882 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001883 if (dinfo->bdrv != bdrv)
1884 continue;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001885 qemu_opts_del(dinfo->opts);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001886 QTAILQ_REMOVE(&drives, dinfo, next);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001887 qemu_free(dinfo);
1888 break;
1889 }
aliguorib01b1112009-02-11 15:20:20 +00001890}
1891
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001892DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001893 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00001894{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001895 const char *buf;
1896 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00001897 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001898 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00001899 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00001900 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00001901 enum { MEDIA_DISK, MEDIA_CDROM } media;
1902 int bus_id, unit_id;
1903 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00001904 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00001905 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00001906 int max_devs;
1907 int index;
balrog33f00272007-12-24 14:33:24 +00001908 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001909 int aio = 0;
aliguori428c5702009-01-21 18:59:04 +00001910 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001911 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001912 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001913 int snapshot = 0;
1914
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001915 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00001916
thse4bcb142007-12-02 04:51:10 +00001917 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02001918 cache = 1;
thse4bcb142007-12-02 04:51:10 +00001919
Gerd Hoffmann4d007812009-08-31 14:23:57 +02001920 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00001921 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001922 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001923 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00001924 } else {
thsf60d39b2007-12-17 03:55:57 +00001925 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001926 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001927 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00001928 }
1929 media = MEDIA_DISK;
1930
1931 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001932 bus_id = qemu_opt_get_number(opts, "bus", 0);
1933 unit_id = qemu_opt_get_number(opts, "unit", -1);
1934 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00001935
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001936 cyls = qemu_opt_get_number(opts, "cyls", 0);
1937 heads = qemu_opt_get_number(opts, "heads", 0);
1938 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00001939
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001940 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
thse4bcb142007-12-02 04:51:10 +00001941
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001942 file = qemu_opt_get(opts, "file");
1943 serial = qemu_opt_get(opts, "serial");
1944
1945 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00001946 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00001947 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00001948 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001949 max_devs = MAX_IDE_DEVS;
1950 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00001951 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001952 max_devs = MAX_SCSI_DEVS;
1953 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00001954 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00001955 max_devs = 0;
1956 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00001957 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00001958 max_devs = 0;
1959 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00001960 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00001961 max_devs = 0;
1962 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00001963 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00001964 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00001965 } else if (!strcmp(buf, "virtio")) {
1966 type = IF_VIRTIO;
1967 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00001968 } else if (!strcmp(buf, "xen")) {
1969 type = IF_XEN;
1970 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02001971 } else if (!strcmp(buf, "none")) {
1972 type = IF_NONE;
1973 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00001974 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001975 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001976 return NULL;
thse4bcb142007-12-02 04:51:10 +00001977 }
1978 }
1979
thse4bcb142007-12-02 04:51:10 +00001980 if (cyls || heads || secs) {
1981 if (cyls < 1 || cyls > 16383) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001982 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001983 return NULL;
thse4bcb142007-12-02 04:51:10 +00001984 }
1985 if (heads < 1 || heads > 16) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001986 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001987 return NULL;
thse4bcb142007-12-02 04:51:10 +00001988 }
1989 if (secs < 1 || secs > 63) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001990 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001991 return NULL;
thse4bcb142007-12-02 04:51:10 +00001992 }
1993 }
1994
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001995 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001996 if (!cyls) {
1997 fprintf(stderr,
1998 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001999 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002000 return NULL;
thse4bcb142007-12-02 04:51:10 +00002001 }
2002 if (!strcmp(buf, "none"))
2003 translation = BIOS_ATA_TRANSLATION_NONE;
2004 else if (!strcmp(buf, "lba"))
2005 translation = BIOS_ATA_TRANSLATION_LBA;
2006 else if (!strcmp(buf, "auto"))
2007 translation = BIOS_ATA_TRANSLATION_AUTO;
2008 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002009 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002010 return NULL;
thse4bcb142007-12-02 04:51:10 +00002011 }
2012 }
2013
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002014 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002015 if (!strcmp(buf, "disk")) {
2016 media = MEDIA_DISK;
2017 } else if (!strcmp(buf, "cdrom")) {
2018 if (cyls || secs || heads) {
2019 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002020 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002021 return NULL;
thse4bcb142007-12-02 04:51:10 +00002022 }
2023 media = MEDIA_CDROM;
2024 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002025 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002026 return NULL;
thse4bcb142007-12-02 04:51:10 +00002027 }
2028 }
2029
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002030 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002031 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002032 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002033 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002034 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002035 else if (!strcmp(buf, "writeback"))
2036 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002037 else {
2038 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002039 return NULL;
balrog33f00272007-12-24 14:33:24 +00002040 }
2041 }
2042
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002043#ifdef CONFIG_LINUX_AIO
2044 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2045 if (!strcmp(buf, "threads"))
2046 aio = 0;
2047 else if (!strcmp(buf, "native"))
2048 aio = 1;
2049 else {
2050 fprintf(stderr, "qemu: invalid aio option\n");
2051 return NULL;
2052 }
2053 }
2054#endif
2055
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002056 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002057 if (strcmp(buf, "?") == 0) {
2058 fprintf(stderr, "qemu: Supported formats:");
2059 bdrv_iterate_format(bdrv_format_print, NULL);
2060 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002061 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002062 }
aurel321e72d3b2008-04-28 20:26:45 +00002063 drv = bdrv_find_format(buf);
2064 if (!drv) {
2065 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002066 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002067 }
2068 }
2069
aliguoricdad4bd2009-02-28 16:51:01 +00002070 onerror = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002071 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002072 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002073 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002074 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002075 }
2076 if (!strcmp(buf, "ignore"))
2077 onerror = BLOCK_ERR_IGNORE;
2078 else if (!strcmp(buf, "enospc"))
2079 onerror = BLOCK_ERR_STOP_ENOSPC;
2080 else if (!strcmp(buf, "stop"))
2081 onerror = BLOCK_ERR_STOP_ANY;
2082 else if (!strcmp(buf, "report"))
2083 onerror = BLOCK_ERR_REPORT;
2084 else {
2085 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002086 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002087 }
2088 }
2089
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002090 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002091 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002092 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002093 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002094 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002095 }
2096
thse4bcb142007-12-02 04:51:10 +00002097 /* compute bus and unit according index */
2098
2099 if (index != -1) {
2100 if (bus_id != 0 || unit_id != -1) {
2101 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002102 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002103 return NULL;
thse4bcb142007-12-02 04:51:10 +00002104 }
2105 if (max_devs == 0)
2106 {
2107 unit_id = index;
2108 bus_id = 0;
2109 } else {
2110 unit_id = index % max_devs;
2111 bus_id = index / max_devs;
2112 }
2113 }
2114
2115 /* if user doesn't specify a unit_id,
2116 * try to find the first free
2117 */
2118
2119 if (unit_id == -1) {
2120 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002121 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002122 unit_id++;
2123 if (max_devs && unit_id >= max_devs) {
2124 unit_id -= max_devs;
2125 bus_id++;
2126 }
2127 }
2128 }
2129
2130 /* check unit id */
2131
2132 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002133 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2134 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002135 return NULL;
thse4bcb142007-12-02 04:51:10 +00002136 }
2137
2138 /*
2139 * ignore multiple definitions
2140 */
2141
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002142 if (drive_get(type, bus_id, unit_id) != NULL) {
2143 *fatal_error = 0;
2144 return NULL;
2145 }
thse4bcb142007-12-02 04:51:10 +00002146
2147 /* init */
2148
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002149 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002150 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002151 dinfo->id = qemu_strdup(buf);
2152 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002153 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002154 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002155 if (type == IF_IDE || type == IF_SCSI)
2156 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2157 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002158 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002159 devname, bus_id, mediastr, unit_id);
2160 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002161 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002162 devname, mediastr, unit_id);
2163 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002164 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002165 dinfo->devaddr = devaddr;
2166 dinfo->type = type;
2167 dinfo->bus = bus_id;
2168 dinfo->unit = unit_id;
2169 dinfo->onerror = onerror;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002170 dinfo->opts = opts;
2171 if (serial)
2172 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002173 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002174
thsf60d39b2007-12-17 03:55:57 +00002175 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002176 case IF_IDE:
2177 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002178 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002179 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002180 switch(media) {
2181 case MEDIA_DISK:
2182 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002183 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2184 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002185 }
2186 break;
2187 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002188 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002189 break;
2190 }
2191 break;
2192 case IF_SD:
2193 /* FIXME: This isn't really a floppy, but it's a reasonable
2194 approximation. */
2195 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002196 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002197 break;
2198 case IF_PFLASH:
2199 case IF_MTD:
2200 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002201 case IF_VIRTIO:
2202 /* add virtio block device */
2203 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2204 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2205 qemu_opt_set(opts, "drive", dinfo->id);
2206 if (devaddr)
2207 qemu_opt_set(opts, "addr", devaddr);
2208 break;
Paul Brookaae94602009-05-14 22:35:06 +01002209 case IF_COUNT:
2210 abort();
thse4bcb142007-12-02 04:51:10 +00002211 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002212 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002213 *fatal_error = 0;
2214 return NULL;
2215 }
balrog33f00272007-12-24 14:33:24 +00002216 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002217 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002218 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002219 cache = 2; /* always use write-back with snapshot */
2220 }
2221 if (cache == 0) /* no caching */
2222 bdrv_flags |= BDRV_O_NOCACHE;
2223 else if (cache == 2) /* write-back */
2224 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002225
2226 if (aio == 1) {
2227 bdrv_flags |= BDRV_O_NATIVE_AIO;
2228 } else {
2229 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2230 }
2231
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002232 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002233 fprintf(stderr, "qemu: could not open disk image %s\n",
2234 file);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002235 return NULL;
thse4bcb142007-12-02 04:51:10 +00002236 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002237
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002238 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002239 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002240 *fatal_error = 0;
2241 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002242}
2243
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002244static int drive_init_func(QemuOpts *opts, void *opaque)
2245{
2246 QEMUMachine *machine = opaque;
2247 int fatal_error = 0;
2248
2249 if (drive_init(opts, machine, &fatal_error) == NULL) {
2250 if (fatal_error)
2251 return 1;
2252 }
2253 return 0;
2254}
2255
2256static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2257{
2258 if (NULL == qemu_opt_get(opts, "snapshot")) {
2259 qemu_opt_set(opts, "snapshot", "on");
2260 }
2261 return 0;
2262}
2263
Jan Kiszka76e30d02009-07-02 00:19:02 +02002264void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2265{
2266 boot_set_handler = func;
2267 boot_set_opaque = opaque;
2268}
2269
2270int qemu_boot_set(const char *boot_devices)
2271{
2272 if (!boot_set_handler) {
2273 return -EINVAL;
2274 }
2275 return boot_set_handler(boot_set_opaque, boot_devices);
2276}
2277
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002278static int parse_bootdevices(char *devices)
2279{
2280 /* We just do some generic consistency checks */
2281 const char *p;
2282 int bitmap = 0;
2283
2284 for (p = devices; *p != '\0'; p++) {
2285 /* Allowed boot devices are:
2286 * a-b: floppy disk drives
2287 * c-f: IDE disk drives
2288 * g-m: machine implementation dependant drives
2289 * n-p: network devices
2290 * It's up to each machine implementation to check if the given boot
2291 * devices match the actual hardware implementation and firmware
2292 * features.
2293 */
2294 if (*p < 'a' || *p > 'p') {
2295 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2296 exit(1);
2297 }
2298 if (bitmap & (1 << (*p - 'a'))) {
2299 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2300 exit(1);
2301 }
2302 bitmap |= 1 << (*p - 'a');
2303 }
2304 return bitmap;
2305}
2306
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002307static void restore_boot_devices(void *opaque)
2308{
2309 char *standard_boot_devices = opaque;
2310
2311 qemu_boot_set(standard_boot_devices);
2312
2313 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2314 qemu_free(standard_boot_devices);
2315}
2316
aliguori268a3622009-04-21 22:30:27 +00002317static void numa_add(const char *optarg)
2318{
2319 char option[128];
2320 char *endptr;
2321 unsigned long long value, endvalue;
2322 int nodenr;
2323
2324 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2325 if (!strcmp(option, "node")) {
2326 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2327 nodenr = nb_numa_nodes;
2328 } else {
2329 nodenr = strtoull(option, NULL, 10);
2330 }
2331
2332 if (get_param_value(option, 128, "mem", optarg) == 0) {
2333 node_mem[nodenr] = 0;
2334 } else {
2335 value = strtoull(option, &endptr, 0);
2336 switch (*endptr) {
2337 case 0: case 'M': case 'm':
2338 value <<= 20;
2339 break;
2340 case 'G': case 'g':
2341 value <<= 30;
2342 break;
2343 }
2344 node_mem[nodenr] = value;
2345 }
2346 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2347 node_cpumask[nodenr] = 0;
2348 } else {
2349 value = strtoull(option, &endptr, 10);
2350 if (value >= 64) {
2351 value = 63;
2352 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2353 } else {
2354 if (*endptr == '-') {
2355 endvalue = strtoull(endptr+1, &endptr, 10);
2356 if (endvalue >= 63) {
2357 endvalue = 62;
2358 fprintf(stderr,
2359 "only 63 CPUs in NUMA mode supported.\n");
2360 }
2361 value = (1 << (endvalue + 1)) - (1 << value);
2362 } else {
2363 value = 1 << value;
2364 }
2365 }
2366 node_cpumask[nodenr] = value;
2367 }
2368 nb_numa_nodes++;
2369 }
2370 return;
2371}
2372
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002373static void smp_parse(const char *optarg)
2374{
2375 int smp, sockets = 0, threads = 0, cores = 0;
2376 char *endptr;
2377 char option[128];
2378
2379 smp = strtoul(optarg, &endptr, 10);
2380 if (endptr != optarg) {
2381 if (*endptr == ',') {
2382 endptr++;
2383 }
2384 }
2385 if (get_param_value(option, 128, "sockets", endptr) != 0)
2386 sockets = strtoull(option, NULL, 10);
2387 if (get_param_value(option, 128, "cores", endptr) != 0)
2388 cores = strtoull(option, NULL, 10);
2389 if (get_param_value(option, 128, "threads", endptr) != 0)
2390 threads = strtoull(option, NULL, 10);
2391 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2392 max_cpus = strtoull(option, NULL, 10);
2393
2394 /* compute missing values, prefer sockets over cores over threads */
2395 if (smp == 0 || sockets == 0) {
2396 sockets = sockets > 0 ? sockets : 1;
2397 cores = cores > 0 ? cores : 1;
2398 threads = threads > 0 ? threads : 1;
2399 if (smp == 0) {
2400 smp = cores * threads * sockets;
2401 } else {
2402 sockets = smp / (cores * threads);
2403 }
2404 } else {
2405 if (cores == 0) {
2406 threads = threads > 0 ? threads : 1;
2407 cores = smp / (sockets * threads);
2408 } else {
2409 if (sockets == 0) {
2410 sockets = smp / (cores * threads);
2411 } else {
2412 threads = smp / (cores * sockets);
2413 }
2414 }
2415 }
2416 smp_cpus = smp;
2417 smp_cores = cores > 0 ? cores : 1;
2418 smp_threads = threads > 0 ? threads : 1;
2419 if (max_cpus == 0)
2420 max_cpus = smp_cpus;
2421}
2422
bellard330d0412003-07-26 18:11:40 +00002423/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002424/* USB devices */
2425
aliguoribb5fc202009-03-05 23:01:15 +00002426static void usb_msd_password_cb(void *opaque, int err)
2427{
2428 USBDevice *dev = opaque;
2429
2430 if (!err)
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002431 usb_device_attach(dev);
aliguoribb5fc202009-03-05 23:01:15 +00002432 else
Gerd Hoffmann806b6022009-08-31 14:23:59 +02002433 dev->info->handle_destroy(dev);
aliguoribb5fc202009-03-05 23:01:15 +00002434}
2435
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002436static struct {
2437 const char *name;
2438 const char *qdev;
2439} usbdevs[] = {
2440 {
2441 .name = "mouse",
2442 .qdev = "QEMU USB Mouse",
2443 },{
2444 .name = "tablet",
2445 .qdev = "QEMU USB Tablet",
2446 },{
2447 .name = "keyboard",
2448 .qdev = "QEMU USB Keyboard",
2449 },{
2450 .name = "wacom-tablet",
2451 .qdev = "QEMU PenPartner Tablet",
2452 }
2453};
2454
aliguoric0f4ce72009-03-05 23:01:01 +00002455static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002456{
2457 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002458 USBBus *bus = usb_bus_find(-1 /* any */);
2459 USBDevice *dev = NULL;
2460 int i;
bellarda594cfb2005-11-06 16:13:29 +00002461
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002462 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002463 return -1;
2464
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002465 /* simple devices which don't need extra care */
2466 for (i = 0; i < ARRAY_SIZE(usbdevs); i++) {
2467 if (strcmp(devname, usbdevs[i].name) != 0)
2468 continue;
2469 dev = usb_create_simple(bus, usbdevs[i].qdev);
2470 goto done;
2471 }
2472
2473 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002474 if (strstart(devname, "host:", &p)) {
2475 dev = usb_host_device_open(p);
pbrook2e5d83b2006-05-25 23:58:51 +00002476 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002477 BlockDriverState *bs;
2478
aliguoribb5fc202009-03-05 23:01:15 +00002479 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002480 if (!dev)
2481 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002482 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002483 if (bdrv_key_required(bs)) {
2484 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002485 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002486 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2487 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002488 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002489 }
2490 }
balroga7954212008-01-14 03:41:02 +00002491 } else if (strstart(devname, "serial:", &p)) {
2492 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002493#ifdef CONFIG_BRLAPI
2494 } else if (!strcmp(devname, "braille")) {
2495 dev = usb_baum_init();
2496#endif
balrog6c9f8862008-07-17 20:47:13 +00002497 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002498 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002499
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002500 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002501 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002502 nd_table[nic].model = "usb";
2503 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002504 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2505 dev = usb_bt_init(devname[2] ? hci_init(p) :
2506 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002507 } else {
2508 return -1;
2509 }
pbrook0d92ed32006-05-21 16:30:15 +00002510 if (!dev)
2511 return -1;
2512
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002513done:
bellarda594cfb2005-11-06 16:13:29 +00002514 return 0;
2515}
2516
aliguori1f3870a2008-08-21 19:27:48 +00002517static int usb_device_del(const char *devname)
2518{
2519 int bus_num, addr;
2520 const char *p;
2521
aliguori5d0c5752008-09-14 01:07:41 +00002522 if (strstart(devname, "host:", &p))
2523 return usb_host_device_close(p);
2524
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002525 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002526 return -1;
2527
2528 p = strchr(devname, '.');
2529 if (!p)
2530 return -1;
2531 bus_num = strtoul(devname, NULL, 0);
2532 addr = strtoul(p + 1, NULL, 0);
2533
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002534 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002535}
2536
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002537static int usb_parse(const char *cmdline)
2538{
2539 return usb_device_add(cmdline, 0);
2540}
2541
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002542void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002543{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002544 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002545}
2546
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002547void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002548{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002549 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002550}
2551
bellardf7cce892004-12-08 22:21:25 +00002552/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002553/* PCMCIA/Cardbus */
2554
2555static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002556 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002557 struct pcmcia_socket_entry_s *next;
2558} *pcmcia_sockets = 0;
2559
Paul Brookbc24a222009-05-10 01:44:56 +01002560void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002561{
2562 struct pcmcia_socket_entry_s *entry;
2563
2564 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2565 entry->socket = socket;
2566 entry->next = pcmcia_sockets;
2567 pcmcia_sockets = entry;
2568}
2569
Paul Brookbc24a222009-05-10 01:44:56 +01002570void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002571{
2572 struct pcmcia_socket_entry_s *entry, **ptr;
2573
2574 ptr = &pcmcia_sockets;
2575 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2576 if (entry->socket == socket) {
2577 *ptr = entry->next;
2578 qemu_free(entry);
2579 }
2580}
2581
aliguori376253e2009-03-05 23:01:23 +00002582void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002583{
2584 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002585
balrog201a51f2007-04-30 00:51:09 +00002586 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002587 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002588
2589 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002590 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2591 iter->socket->attached ? iter->socket->card_string :
2592 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002593}
2594
2595/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002596/* register display */
2597
aliguori7b5d76d2009-03-13 15:02:13 +00002598struct DisplayAllocator default_allocator = {
2599 defaultallocator_create_displaysurface,
2600 defaultallocator_resize_displaysurface,
2601 defaultallocator_free_displaysurface
2602};
2603
aliguori3023f332009-01-16 19:04:14 +00002604void register_displaystate(DisplayState *ds)
2605{
2606 DisplayState **s;
2607 s = &display_state;
2608 while (*s != NULL)
2609 s = &(*s)->next;
2610 ds->next = NULL;
2611 *s = ds;
2612}
2613
2614DisplayState *get_displaystate(void)
2615{
2616 return display_state;
2617}
2618
aliguori7b5d76d2009-03-13 15:02:13 +00002619DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2620{
2621 if(ds->allocator == &default_allocator) ds->allocator = da;
2622 return ds->allocator;
2623}
2624
ths2ff89792007-06-21 23:34:19 +00002625/* dumb display */
2626
aliguori8f391ab2009-01-19 16:34:10 +00002627static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002628{
aliguori8f391ab2009-01-19 16:34:10 +00002629 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002630 ds->allocator = &default_allocator;
2631 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002632 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002633}
2634
2635/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002636/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002637
bellardc4b1fcc2004-03-14 21:44:30 +00002638typedef struct IOHandlerRecord {
2639 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002640 IOCanRWHandler *fd_read_poll;
2641 IOHandler *fd_read;
2642 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002643 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002644 void *opaque;
2645 /* temporary data */
2646 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002647 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002648} IOHandlerRecord;
2649
bellard8a7ddc32004-03-31 19:00:16 +00002650static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002651
bellard7c9d8e02005-11-15 22:16:05 +00002652/* XXX: fd_read_poll should be suppressed, but an API change is
2653 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002654int qemu_set_fd_handler2(int fd,
2655 IOCanRWHandler *fd_read_poll,
2656 IOHandler *fd_read,
2657 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002658 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002659{
bellard8a7ddc32004-03-31 19:00:16 +00002660 IOHandlerRecord **pioh, *ioh;
2661
bellard7c9d8e02005-11-15 22:16:05 +00002662 if (!fd_read && !fd_write) {
2663 pioh = &first_io_handler;
2664 for(;;) {
2665 ioh = *pioh;
2666 if (ioh == NULL)
2667 break;
2668 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002669 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002670 break;
2671 }
2672 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002673 }
bellard7c9d8e02005-11-15 22:16:05 +00002674 } else {
2675 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2676 if (ioh->fd == fd)
2677 goto found;
2678 }
2679 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002680 ioh->next = first_io_handler;
2681 first_io_handler = ioh;
2682 found:
2683 ioh->fd = fd;
2684 ioh->fd_read_poll = fd_read_poll;
2685 ioh->fd_read = fd_read;
2686 ioh->fd_write = fd_write;
2687 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002688 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002689 }
bellard7c9d8e02005-11-15 22:16:05 +00002690 return 0;
2691}
2692
ths5fafdf22007-09-16 21:08:06 +00002693int qemu_set_fd_handler(int fd,
2694 IOHandler *fd_read,
2695 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002696 void *opaque)
2697{
2698 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002699}
2700
aliguori56f3a5d2008-10-31 18:07:17 +00002701#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002702/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002703/* Polling handling */
2704
2705typedef struct PollingEntry {
2706 PollingFunc *func;
2707 void *opaque;
2708 struct PollingEntry *next;
2709} PollingEntry;
2710
2711static PollingEntry *first_polling_entry;
2712
2713int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2714{
2715 PollingEntry **ppe, *pe;
2716 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002717 pe->func = func;
2718 pe->opaque = opaque;
2719 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2720 *ppe = pe;
2721 return 0;
2722}
2723
2724void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2725{
2726 PollingEntry **ppe, *pe;
2727 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2728 pe = *ppe;
2729 if (pe->func == func && pe->opaque == opaque) {
2730 *ppe = pe->next;
2731 qemu_free(pe);
2732 break;
2733 }
2734 }
2735}
2736
bellarda18e5242006-06-25 17:18:27 +00002737/***********************************************************/
2738/* Wait objects support */
2739typedef struct WaitObjects {
2740 int num;
2741 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2742 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2743 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2744} WaitObjects;
2745
2746static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002747
bellarda18e5242006-06-25 17:18:27 +00002748int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2749{
2750 WaitObjects *w = &wait_objects;
2751
2752 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2753 return -1;
2754 w->events[w->num] = handle;
2755 w->func[w->num] = func;
2756 w->opaque[w->num] = opaque;
2757 w->num++;
2758 return 0;
2759}
2760
2761void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2762{
2763 int i, found;
2764 WaitObjects *w = &wait_objects;
2765
2766 found = 0;
2767 for (i = 0; i < w->num; i++) {
2768 if (w->events[i] == handle)
2769 found = 1;
2770 if (found) {
2771 w->events[i] = w->events[i + 1];
2772 w->func[i] = w->func[i + 1];
2773 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002774 }
bellarda18e5242006-06-25 17:18:27 +00002775 }
2776 if (found)
2777 w->num--;
2778}
2779#endif
2780
bellard8a7ddc32004-03-31 19:00:16 +00002781/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002782/* ram save/restore */
2783
Juan Quintela94fb0902009-09-10 03:04:23 +02002784#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002785#define RAM_SAVE_FLAG_COMPRESS 0x02
2786#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2787#define RAM_SAVE_FLAG_PAGE 0x08
2788#define RAM_SAVE_FLAG_EOS 0x10
2789
2790static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002791{
aliguori475e4272008-10-06 20:21:51 +00002792 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2793 uint32_t *array = (uint32_t *)page;
2794 int i;
ths3b46e622007-09-17 08:09:54 +00002795
aliguori475e4272008-10-06 20:21:51 +00002796 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2797 if (array[i] != val)
2798 return 0;
bellardc88676f2006-08-06 13:36:11 +00002799 }
aliguori475e4272008-10-06 20:21:51 +00002800
2801 return 1;
bellardc88676f2006-08-06 13:36:11 +00002802}
2803
aliguori475e4272008-10-06 20:21:51 +00002804static int ram_save_block(QEMUFile *f)
2805{
2806 static ram_addr_t current_addr = 0;
2807 ram_addr_t saved_addr = current_addr;
2808 ram_addr_t addr = 0;
2809 int found = 0;
2810
pbrook94a6b542009-04-11 17:15:54 +00002811 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002812 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002813 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002814
2815 cpu_physical_memory_reset_dirty(current_addr,
2816 current_addr + TARGET_PAGE_SIZE,
2817 MIGRATION_DIRTY_FLAG);
2818
pbrook5579c7f2009-04-11 14:47:08 +00002819 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002820
pbrook5579c7f2009-04-11 14:47:08 +00002821 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002822 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002823 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002824 } else {
2825 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002826 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002827 }
2828
2829 found = 1;
2830 break;
2831 }
2832 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002833 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002834 }
2835
2836 return found;
2837}
2838
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002839static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00002840
2841static ram_addr_t ram_save_remaining(void)
2842{
2843 ram_addr_t addr;
2844 ram_addr_t count = 0;
2845
pbrook94a6b542009-04-11 17:15:54 +00002846 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002847 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2848 count++;
2849 }
2850
2851 return count;
2852}
2853
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002854uint64_t ram_bytes_remaining(void)
2855{
2856 return ram_save_remaining() * TARGET_PAGE_SIZE;
2857}
2858
2859uint64_t ram_bytes_transferred(void)
2860{
2861 return bytes_transferred;
2862}
2863
2864uint64_t ram_bytes_total(void)
2865{
2866 return last_ram_offset;
2867}
2868
aliguori475e4272008-10-06 20:21:51 +00002869static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2870{
2871 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002872 uint64_t bytes_transferred_last;
2873 double bwidth = 0;
2874 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002875
Jan Kiszka9fa06382009-05-22 23:51:45 +02002876 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002877 qemu_file_set_error(f);
2878 return 0;
2879 }
2880
aliguori475e4272008-10-06 20:21:51 +00002881 if (stage == 1) {
2882 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002883 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002884 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2885 cpu_physical_memory_set_dirty(addr);
2886 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002887
aliguori475e4272008-10-06 20:21:51 +00002888 /* Enable dirty memory tracking */
2889 cpu_physical_memory_set_dirty_tracking(1);
2890
pbrook94a6b542009-04-11 17:15:54 +00002891 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002892 }
2893
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002894 bytes_transferred_last = bytes_transferred;
2895 bwidth = get_clock();
2896
aliguori475e4272008-10-06 20:21:51 +00002897 while (!qemu_file_rate_limit(f)) {
2898 int ret;
2899
2900 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002901 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00002902 if (ret == 0) /* no more blocks */
2903 break;
2904 }
2905
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002906 bwidth = get_clock() - bwidth;
2907 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2908
2909 /* if we haven't transferred anything this round, force expected_time to a
2910 * a very high value, but without crashing */
2911 if (bwidth == 0)
2912 bwidth = 0.000001;
2913
aliguori475e4272008-10-06 20:21:51 +00002914 /* try transferring iterative blocks of memory */
2915
2916 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00002917
2918 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002919 while (ram_save_block(f) != 0) {
2920 bytes_transferred += TARGET_PAGE_SIZE;
2921 }
aliguori8215e912009-04-05 19:30:55 +00002922 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00002923 }
2924
2925 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2926
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002927 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2928
2929 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00002930}
2931
aliguori475e4272008-10-06 20:21:51 +00002932static int ram_load(QEMUFile *f, void *opaque, int version_id)
2933{
2934 ram_addr_t addr;
2935 int flags;
2936
aliguori475e4272008-10-06 20:21:51 +00002937 if (version_id != 3)
2938 return -EINVAL;
2939
2940 do {
2941 addr = qemu_get_be64(f);
2942
2943 flags = addr & ~TARGET_PAGE_MASK;
2944 addr &= TARGET_PAGE_MASK;
2945
2946 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00002947 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00002948 return -EINVAL;
2949 }
2950
aliguori475e4272008-10-06 20:21:51 +00002951 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2952 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05002953 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2954#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05002955 if (ch == 0 &&
2956 (!kvm_enabled() || kvm_has_sync_mmu())) {
2957 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05002958 }
Anthony Liguori30868442009-06-17 16:46:12 -05002959#endif
aliguori475e4272008-10-06 20:21:51 +00002960 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00002961 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002962 } while (!(flags & RAM_SAVE_FLAG_EOS));
2963
bellardc88676f2006-08-06 13:36:11 +00002964 return 0;
2965}
2966
aliguori9e472e12008-10-08 19:50:24 +00002967void qemu_service_io(void)
2968{
aliguorid9f75a42009-04-24 18:03:11 +00002969 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00002970}
2971
bellard8a7ddc32004-03-31 19:00:16 +00002972/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00002973/* bottom halves (can be seen as timers which expire ASAP) */
2974
2975struct QEMUBH {
2976 QEMUBHFunc *cb;
2977 void *opaque;
2978 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00002979 int idle;
2980 int deleted;
bellard83f64092006-08-01 16:21:11 +00002981 QEMUBH *next;
2982};
2983
2984static QEMUBH *first_bh = NULL;
2985
2986QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
2987{
2988 QEMUBH *bh;
2989 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00002990 bh->cb = cb;
2991 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00002992 bh->next = first_bh;
2993 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00002994 return bh;
2995}
2996
bellard6eb57332006-08-06 09:51:25 +00002997int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00002998{
aliguori1b435b12008-10-31 17:24:21 +00002999 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003000 int ret;
bellard83f64092006-08-01 16:21:11 +00003001
bellard6eb57332006-08-06 09:51:25 +00003002 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003003 for (bh = first_bh; bh; bh = bh->next) {
3004 if (!bh->deleted && bh->scheduled) {
3005 bh->scheduled = 0;
3006 if (!bh->idle)
3007 ret = 1;
3008 bh->idle = 0;
3009 bh->cb(bh->opaque);
3010 }
bellard83f64092006-08-01 16:21:11 +00003011 }
aliguori1b435b12008-10-31 17:24:21 +00003012
3013 /* remove deleted bhs */
3014 bhp = &first_bh;
3015 while (*bhp) {
3016 bh = *bhp;
3017 if (bh->deleted) {
3018 *bhp = bh->next;
3019 qemu_free(bh);
3020 } else
3021 bhp = &bh->next;
3022 }
3023
bellard6eb57332006-08-06 09:51:25 +00003024 return ret;
bellard83f64092006-08-01 16:21:11 +00003025}
3026
aliguori1b435b12008-10-31 17:24:21 +00003027void qemu_bh_schedule_idle(QEMUBH *bh)
3028{
3029 if (bh->scheduled)
3030 return;
3031 bh->scheduled = 1;
3032 bh->idle = 1;
3033}
3034
bellard83f64092006-08-01 16:21:11 +00003035void qemu_bh_schedule(QEMUBH *bh)
3036{
bellard83f64092006-08-01 16:21:11 +00003037 if (bh->scheduled)
3038 return;
3039 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003040 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003041 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003042 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003043}
3044
3045void qemu_bh_cancel(QEMUBH *bh)
3046{
aliguori1b435b12008-10-31 17:24:21 +00003047 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003048}
3049
3050void qemu_bh_delete(QEMUBH *bh)
3051{
aliguori1b435b12008-10-31 17:24:21 +00003052 bh->scheduled = 0;
3053 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003054}
3055
aliguori56f3a5d2008-10-31 18:07:17 +00003056static void qemu_bh_update_timeout(int *timeout)
3057{
3058 QEMUBH *bh;
3059
3060 for (bh = first_bh; bh; bh = bh->next) {
3061 if (!bh->deleted && bh->scheduled) {
3062 if (bh->idle) {
3063 /* idle bottom halves will be polled at least
3064 * every 10ms */
3065 *timeout = MIN(10, *timeout);
3066 } else {
3067 /* non-idle bottom halves will be executed
3068 * immediately */
3069 *timeout = 0;
3070 break;
3071 }
3072 }
3073 }
3074}
3075
bellard83f64092006-08-01 16:21:11 +00003076/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003077/* machine registration */
3078
blueswir1bdaf78e2008-10-04 07:24:27 +00003079static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003080QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003081
3082int qemu_register_machine(QEMUMachine *m)
3083{
3084 QEMUMachine **pm;
3085 pm = &first_machine;
3086 while (*pm != NULL)
3087 pm = &(*pm)->next;
3088 m->next = NULL;
3089 *pm = m;
3090 return 0;
3091}
3092
pbrook9596ebb2007-11-18 01:44:38 +00003093static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003094{
3095 QEMUMachine *m;
3096
3097 for(m = first_machine; m != NULL; m = m->next) {
3098 if (!strcmp(m->name, name))
3099 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003100 if (m->alias && !strcmp(m->alias, name))
3101 return m;
bellardcc1daa42005-06-05 14:49:17 +00003102 }
3103 return NULL;
3104}
3105
Anthony Liguori0c257432009-05-21 20:41:01 -05003106static QEMUMachine *find_default_machine(void)
3107{
3108 QEMUMachine *m;
3109
3110 for(m = first_machine; m != NULL; m = m->next) {
3111 if (m->is_default) {
3112 return m;
3113 }
3114 }
3115 return NULL;
3116}
3117
bellardcc1daa42005-06-05 14:49:17 +00003118/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003119/* main execution loop */
3120
pbrook9596ebb2007-11-18 01:44:38 +00003121static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003122{
aliguori7d957bd2009-01-15 22:14:11 +00003123 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003124 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003125 DisplayChangeListener *dcl = ds->listeners;
3126
3127 dpy_refresh(ds);
3128
3129 while (dcl != NULL) {
3130 if (dcl->gui_timer_interval &&
3131 dcl->gui_timer_interval < interval)
3132 interval = dcl->gui_timer_interval;
3133 dcl = dcl->next;
3134 }
3135 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003136}
3137
blueswir19043b622009-01-21 19:28:13 +00003138static void nographic_update(void *opaque)
3139{
3140 uint64_t interval = GUI_REFRESH_INTERVAL;
3141
3142 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3143}
3144
bellard0bd48852005-11-11 00:00:47 +00003145struct vm_change_state_entry {
3146 VMChangeStateHandler *cb;
3147 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003148 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003149};
3150
Blue Swirl72cf2d42009-09-12 07:36:22 +00003151static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003152
3153VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3154 void *opaque)
3155{
3156 VMChangeStateEntry *e;
3157
3158 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003159
3160 e->cb = cb;
3161 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003162 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003163 return e;
3164}
3165
3166void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3167{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003168 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003169 qemu_free (e);
3170}
3171
aliguori9781e042009-01-22 17:15:29 +00003172static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003173{
3174 VMChangeStateEntry *e;
3175
3176 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003177 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003178 }
3179}
3180
aliguorid6dc3d42009-04-24 18:04:07 +00003181static void resume_all_vcpus(void);
3182static void pause_all_vcpus(void);
3183
bellard8a7ddc32004-03-31 19:00:16 +00003184void vm_start(void)
3185{
3186 if (!vm_running) {
3187 cpu_enable_ticks();
3188 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003189 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003190 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003191 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003192 }
3193}
3194
bellardbb0c6722004-06-20 12:37:32 +00003195/* reset/shutdown handler */
3196
3197typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003198 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003199 QEMUResetHandler *func;
3200 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003201} QEMUResetEntry;
3202
Blue Swirl72cf2d42009-09-12 07:36:22 +00003203static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3204 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003205static int reset_requested;
3206static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003207static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003208static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003209static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003210
aurel32cf7a2fe2008-03-18 06:53:05 +00003211int qemu_shutdown_requested(void)
3212{
3213 int r = shutdown_requested;
3214 shutdown_requested = 0;
3215 return r;
3216}
3217
3218int qemu_reset_requested(void)
3219{
3220 int r = reset_requested;
3221 reset_requested = 0;
3222 return r;
3223}
3224
3225int qemu_powerdown_requested(void)
3226{
3227 int r = powerdown_requested;
3228 powerdown_requested = 0;
3229 return r;
3230}
3231
aliguorie5689022009-04-24 18:03:54 +00003232static int qemu_debug_requested(void)
3233{
3234 int r = debug_requested;
3235 debug_requested = 0;
3236 return r;
3237}
3238
aliguori6e29f5d2009-04-24 18:04:02 +00003239static int qemu_vmstop_requested(void)
3240{
3241 int r = vmstop_requested;
3242 vmstop_requested = 0;
3243 return r;
3244}
3245
3246static void do_vm_stop(int reason)
3247{
3248 if (vm_running) {
3249 cpu_disable_ticks();
3250 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003251 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003252 vm_state_notify(0, reason);
3253 }
3254}
3255
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003256void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003257{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003258 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003259
bellardbb0c6722004-06-20 12:37:32 +00003260 re->func = func;
3261 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003262 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003263}
3264
Jan Kiszkadda9b292009-07-02 00:19:02 +02003265void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003266{
3267 QEMUResetEntry *re;
3268
Blue Swirl72cf2d42009-09-12 07:36:22 +00003269 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003270 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003271 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003272 qemu_free(re);
3273 return;
3274 }
3275 }
3276}
3277
3278void qemu_system_reset(void)
3279{
3280 QEMUResetEntry *re, *nre;
3281
3282 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003283 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003284 re->func(re->opaque);
3285 }
3286}
3287
3288void qemu_system_reset_request(void)
3289{
bellardd1beab82006-10-02 19:44:22 +00003290 if (no_reboot) {
3291 shutdown_requested = 1;
3292 } else {
3293 reset_requested = 1;
3294 }
aliguorid9f75a42009-04-24 18:03:11 +00003295 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003296}
3297
3298void qemu_system_shutdown_request(void)
3299{
3300 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003301 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003302}
3303
bellard34751872005-07-02 14:31:34 +00003304void qemu_system_powerdown_request(void)
3305{
3306 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003307 qemu_notify_event();
3308}
3309
aliguorid6dc3d42009-04-24 18:04:07 +00003310#ifdef CONFIG_IOTHREAD
3311static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003312{
aliguorid6dc3d42009-04-24 18:04:07 +00003313 vmstop_requested = reason;
3314 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003315}
aliguorid6dc3d42009-04-24 18:04:07 +00003316#endif
bellardbb0c6722004-06-20 12:37:32 +00003317
aliguori50317c72009-04-24 18:03:29 +00003318#ifndef _WIN32
3319static int io_thread_fd = -1;
3320
3321static void qemu_event_increment(void)
3322{
3323 static const char byte = 0;
3324
3325 if (io_thread_fd == -1)
3326 return;
3327
3328 write(io_thread_fd, &byte, sizeof(byte));
3329}
3330
3331static void qemu_event_read(void *opaque)
3332{
3333 int fd = (unsigned long)opaque;
3334 ssize_t len;
3335
3336 /* Drain the notify pipe */
3337 do {
3338 char buffer[512];
3339 len = read(fd, buffer, sizeof(buffer));
3340 } while ((len == -1 && errno == EINTR) || len > 0);
3341}
3342
3343static int qemu_event_init(void)
3344{
3345 int err;
3346 int fds[2];
3347
3348 err = pipe(fds);
3349 if (err == -1)
3350 return -errno;
3351
3352 err = fcntl_setfl(fds[0], O_NONBLOCK);
3353 if (err < 0)
3354 goto fail;
3355
3356 err = fcntl_setfl(fds[1], O_NONBLOCK);
3357 if (err < 0)
3358 goto fail;
3359
3360 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3361 (void *)(unsigned long)fds[0]);
3362
3363 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003364 return 0;
3365
aliguori50317c72009-04-24 18:03:29 +00003366fail:
3367 close(fds[0]);
3368 close(fds[1]);
3369 return err;
3370}
3371#else
3372HANDLE qemu_event_handle;
3373
3374static void dummy_event_handler(void *opaque)
3375{
3376}
3377
3378static int qemu_event_init(void)
3379{
3380 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3381 if (!qemu_event_handle) {
3382 perror("Failed CreateEvent");
3383 return -1;
3384 }
3385 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3386 return 0;
3387}
3388
3389static void qemu_event_increment(void)
3390{
3391 SetEvent(qemu_event_handle);
3392}
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
aliguori48708522009-04-24 18:03:49 +00003448#define qemu_mutex_lock_iothread() do { } while (0)
3449#define qemu_mutex_unlock_iothread() do { } while (0)
3450
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
3585int qemu_cpu_self(void *env)
3586{
3587 return (cpu_single_env != NULL);
3588}
3589
3590static void cpu_signal(int sig)
3591{
3592 if (cpu_single_env)
3593 cpu_exit(cpu_single_env);
3594}
3595
3596static void block_io_signals(void)
3597{
3598 sigset_t set;
3599 struct sigaction sigact;
3600
3601 sigemptyset(&set);
3602 sigaddset(&set, SIGUSR2);
3603 sigaddset(&set, SIGIO);
3604 sigaddset(&set, SIGALRM);
3605 pthread_sigmask(SIG_BLOCK, &set, NULL);
3606
3607 sigemptyset(&set);
3608 sigaddset(&set, SIGUSR1);
3609 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3610
3611 memset(&sigact, 0, sizeof(sigact));
3612 sigact.sa_handler = cpu_signal;
3613 sigaction(SIGUSR1, &sigact, NULL);
3614}
3615
3616static void unblock_io_signals(void)
3617{
3618 sigset_t set;
3619
3620 sigemptyset(&set);
3621 sigaddset(&set, SIGUSR2);
3622 sigaddset(&set, SIGIO);
3623 sigaddset(&set, SIGALRM);
3624 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3625
3626 sigemptyset(&set);
3627 sigaddset(&set, SIGUSR1);
3628 pthread_sigmask(SIG_BLOCK, &set, NULL);
3629}
3630
3631static void qemu_signal_lock(unsigned int msecs)
3632{
3633 qemu_mutex_lock(&qemu_fair_mutex);
3634
3635 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3636 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3637 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3638 break;
3639 }
3640 qemu_mutex_unlock(&qemu_fair_mutex);
3641}
3642
3643static void qemu_mutex_lock_iothread(void)
3644{
3645 if (kvm_enabled()) {
3646 qemu_mutex_lock(&qemu_fair_mutex);
3647 qemu_mutex_lock(&qemu_global_mutex);
3648 qemu_mutex_unlock(&qemu_fair_mutex);
3649 } else
3650 qemu_signal_lock(100);
3651}
3652
3653static void qemu_mutex_unlock_iothread(void)
3654{
3655 qemu_mutex_unlock(&qemu_global_mutex);
3656}
3657
3658static int all_vcpus_paused(void)
3659{
3660 CPUState *penv = first_cpu;
3661
3662 while (penv) {
3663 if (!penv->stopped)
3664 return 0;
3665 penv = (CPUState *)penv->next_cpu;
3666 }
3667
3668 return 1;
3669}
3670
3671static void pause_all_vcpus(void)
3672{
3673 CPUState *penv = first_cpu;
3674
3675 while (penv) {
3676 penv->stop = 1;
3677 qemu_thread_signal(penv->thread, SIGUSR1);
3678 qemu_cpu_kick(penv);
3679 penv = (CPUState *)penv->next_cpu;
3680 }
3681
3682 while (!all_vcpus_paused()) {
3683 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3684 penv = first_cpu;
3685 while (penv) {
3686 qemu_thread_signal(penv->thread, SIGUSR1);
3687 penv = (CPUState *)penv->next_cpu;
3688 }
3689 }
3690}
3691
3692static void resume_all_vcpus(void)
3693{
3694 CPUState *penv = first_cpu;
3695
3696 while (penv) {
3697 penv->stop = 0;
3698 penv->stopped = 0;
3699 qemu_thread_signal(penv->thread, SIGUSR1);
3700 qemu_cpu_kick(penv);
3701 penv = (CPUState *)penv->next_cpu;
3702 }
3703}
3704
3705static void tcg_init_vcpu(void *_env)
3706{
3707 CPUState *env = _env;
3708 /* share a single thread for all cpus with TCG */
3709 if (!tcg_cpu_thread) {
3710 env->thread = qemu_mallocz(sizeof(QemuThread));
3711 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3712 qemu_cond_init(env->halt_cond);
3713 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3714 while (env->created == 0)
3715 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3716 tcg_cpu_thread = env->thread;
3717 tcg_halt_cond = env->halt_cond;
3718 } else {
3719 env->thread = tcg_cpu_thread;
3720 env->halt_cond = tcg_halt_cond;
3721 }
3722}
3723
3724static void kvm_start_vcpu(CPUState *env)
3725{
aliguorid6dc3d42009-04-24 18:04:07 +00003726 env->thread = qemu_mallocz(sizeof(QemuThread));
3727 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3728 qemu_cond_init(env->halt_cond);
3729 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3730 while (env->created == 0)
3731 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3732}
3733
3734void qemu_init_vcpu(void *_env)
3735{
3736 CPUState *env = _env;
3737
3738 if (kvm_enabled())
3739 kvm_start_vcpu(env);
3740 else
3741 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003742 env->nr_cores = smp_cores;
3743 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003744}
3745
3746void qemu_notify_event(void)
3747{
3748 qemu_event_increment();
3749}
3750
3751void vm_stop(int reason)
3752{
3753 QemuThread me;
3754 qemu_thread_self(&me);
3755
3756 if (!qemu_thread_equal(&me, &io_thread)) {
3757 qemu_system_vmstop_request(reason);
3758 /*
3759 * FIXME: should not return to device code in case
3760 * vm_stop() has been requested.
3761 */
3762 if (cpu_single_env) {
3763 cpu_exit(cpu_single_env);
3764 cpu_single_env->stop = 1;
3765 }
3766 return;
3767 }
3768 do_vm_stop(reason);
3769}
3770
3771#endif
3772
3773
ths877cf882007-04-18 18:11:47 +00003774#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003775static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003776{
3777 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003778 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003779
bellardf3311102006-04-12 20:21:17 +00003780
3781 /* XXX: need to suppress polling by better using win32 events */
3782 ret = 0;
3783 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3784 ret |= pe->func(pe->opaque);
3785 }
thse6b1e552007-04-18 17:56:02 +00003786 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003787 int err;
3788 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003789
aliguori56f3a5d2008-10-31 18:07:17 +00003790 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003791 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3792 if (w->func[ret - WAIT_OBJECT_0])
3793 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003794
ths5fafdf22007-09-16 21:08:06 +00003795 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003796 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003797
thse6b1e552007-04-18 17:56:02 +00003798 /* Check if event is signaled */
3799 ret2 = WaitForSingleObject(w->events[i], 0);
3800 if(ret2 == WAIT_OBJECT_0) {
3801 if (w->func[i])
3802 w->func[i](w->opaque[i]);
3803 } else if (ret2 == WAIT_TIMEOUT) {
3804 } else {
3805 err = GetLastError();
3806 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003807 }
3808 }
bellarda18e5242006-06-25 17:18:27 +00003809 } else if (ret == WAIT_TIMEOUT) {
3810 } else {
3811 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003812 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003813 }
bellardf3311102006-04-12 20:21:17 +00003814 }
aliguori56f3a5d2008-10-31 18:07:17 +00003815
3816 *timeout = 0;
3817}
3818#else
blueswir169d64512008-12-07 19:30:18 +00003819static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003820{
3821}
bellardfd1dff42006-02-01 21:29:26 +00003822#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003823
3824void main_loop_wait(int timeout)
3825{
3826 IOHandlerRecord *ioh;
3827 fd_set rfds, wfds, xfds;
3828 int ret, nfds;
3829 struct timeval tv;
3830
3831 qemu_bh_update_timeout(&timeout);
3832
3833 host_main_loop_wait(&timeout);
3834
bellardfd1dff42006-02-01 21:29:26 +00003835 /* poll any events */
3836 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003837 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003838 FD_ZERO(&rfds);
3839 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003840 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003841 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003842 if (ioh->deleted)
3843 continue;
bellardfd1dff42006-02-01 21:29:26 +00003844 if (ioh->fd_read &&
3845 (!ioh->fd_read_poll ||
3846 ioh->fd_read_poll(ioh->opaque) != 0)) {
3847 FD_SET(ioh->fd, &rfds);
3848 if (ioh->fd > nfds)
3849 nfds = ioh->fd;
3850 }
3851 if (ioh->fd_write) {
3852 FD_SET(ioh->fd, &wfds);
3853 if (ioh->fd > nfds)
3854 nfds = ioh->fd;
3855 }
3856 }
ths3b46e622007-09-17 08:09:54 +00003857
aliguori56f3a5d2008-10-31 18:07:17 +00003858 tv.tv_sec = timeout / 1000;
3859 tv.tv_usec = (timeout % 1000) * 1000;
3860
Jan Kiszkad918f232009-06-24 14:42:30 +02003861 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3862
aliguori48708522009-04-24 18:03:49 +00003863 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003864 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003865 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003866 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003867 IOHandlerRecord **pioh;
3868
3869 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003870 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003871 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003872 }
ths6ab43fd2007-08-25 01:34:19 +00003873 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003874 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003875 }
3876 }
thscafffd42007-02-28 21:59:44 +00003877
3878 /* remove deleted IO handlers */
3879 pioh = &first_io_handler;
3880 while (*pioh) {
3881 ioh = *pioh;
3882 if (ioh->deleted) {
3883 *pioh = ioh->next;
3884 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003885 } else
thscafffd42007-02-28 21:59:44 +00003886 pioh = &ioh->next;
3887 }
bellardfd1dff42006-02-01 21:29:26 +00003888 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003889
3890 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003891
aliguori50317c72009-04-24 18:03:29 +00003892 /* rearm timer, if not periodic */
3893 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3894 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3895 qemu_rearm_alarm_timer(alarm_timer);
3896 }
3897
aliguori357c6922008-11-25 17:26:09 +00003898 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003899 if (vm_running) {
3900 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3901 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3902 qemu_get_clock(vm_clock));
3903 }
aliguori357c6922008-11-25 17:26:09 +00003904
3905 /* real time timers */
3906 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3907 qemu_get_clock(rt_clock));
3908
pbrook423f0742007-05-23 00:06:54 +00003909 /* Check bottom-halves last in case any of the earlier events triggered
3910 them. */
3911 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003912
bellard5905b2e2004-08-01 21:53:26 +00003913}
3914
aliguori43b96852009-04-24 18:03:33 +00003915static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003916{
aliguori43b96852009-04-24 18:03:33 +00003917 int ret;
bellard89bfc102006-02-08 22:46:31 +00003918#ifdef CONFIG_PROFILER
3919 int64_t ti;
3920#endif
aliguori43b96852009-04-24 18:03:33 +00003921
3922#ifdef CONFIG_PROFILER
3923 ti = profile_getclock();
3924#endif
3925 if (use_icount) {
3926 int64_t count;
3927 int decr;
3928 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3929 env->icount_decr.u16.low = 0;
3930 env->icount_extra = 0;
3931 count = qemu_next_deadline();
3932 count = (count + (1 << icount_time_shift) - 1)
3933 >> icount_time_shift;
3934 qemu_icount += count;
3935 decr = (count > 0xffff) ? 0xffff : count;
3936 count -= decr;
3937 env->icount_decr.u16.low = decr;
3938 env->icount_extra = count;
3939 }
3940 ret = cpu_exec(env);
3941#ifdef CONFIG_PROFILER
3942 qemu_time += profile_getclock() - ti;
3943#endif
3944 if (use_icount) {
3945 /* Fold pending instructions back into the
3946 instruction counter, and clear the interrupt flag. */
3947 qemu_icount -= (env->icount_decr.u16.low
3948 + env->icount_extra);
3949 env->icount_decr.u32 = 0;
3950 env->icount_extra = 0;
3951 }
3952 return ret;
3953}
3954
aliguorie6e35b12009-04-24 18:03:57 +00003955static void tcg_cpu_exec(void)
3956{
aliguorid6dc3d42009-04-24 18:04:07 +00003957 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00003958
3959 if (next_cpu == NULL)
3960 next_cpu = first_cpu;
3961 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3962 CPUState *env = cur_cpu = next_cpu;
3963
3964 if (!vm_running)
3965 break;
3966 if (timer_alarm_pending) {
3967 timer_alarm_pending = 0;
3968 break;
3969 }
aliguorid6dc3d42009-04-24 18:04:07 +00003970 if (cpu_can_run(env))
3971 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00003972 if (ret == EXCP_DEBUG) {
3973 gdb_set_stop_cpu(env);
3974 debug_requested = 1;
3975 break;
3976 }
3977 }
3978}
3979
aliguori43b96852009-04-24 18:03:33 +00003980static int cpu_has_work(CPUState *env)
3981{
aliguorid6dc3d42009-04-24 18:04:07 +00003982 if (env->stop)
3983 return 1;
3984 if (env->stopped)
3985 return 0;
aliguori43b96852009-04-24 18:03:33 +00003986 if (!env->halted)
3987 return 1;
3988 if (qemu_cpu_has_work(env))
3989 return 1;
3990 return 0;
3991}
3992
3993static int tcg_has_work(void)
3994{
bellard6a00d602005-11-21 23:25:50 +00003995 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003996
aliguori43b96852009-04-24 18:03:33 +00003997 for (env = first_cpu; env != NULL; env = env->next_cpu)
3998 if (cpu_has_work(env))
3999 return 1;
4000 return 0;
4001}
bellard15a76442005-11-23 21:01:03 +00004002
aliguori43b96852009-04-24 18:03:33 +00004003static int qemu_calculate_timeout(void)
4004{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004005#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004006 int timeout;
bellard15a76442005-11-23 21:01:03 +00004007
aliguori43b96852009-04-24 18:03:33 +00004008 if (!vm_running)
4009 timeout = 5000;
4010 else if (tcg_has_work())
4011 timeout = 0;
4012 else if (!use_icount)
4013 timeout = 5000;
4014 else {
4015 /* XXX: use timeout computed from timers */
4016 int64_t add;
4017 int64_t delta;
4018 /* Advance virtual time to the next event. */
4019 if (use_icount == 1) {
4020 /* When not using an adaptive execution frequency
4021 we tend to get badly out of sync with real time,
4022 so just delay for a reasonable amount of time. */
4023 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004024 } else {
aliguori43b96852009-04-24 18:03:33 +00004025 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004026 }
aliguori43b96852009-04-24 18:03:33 +00004027 if (delta > 0) {
4028 /* If virtual time is ahead of real time then just
4029 wait for IO. */
4030 timeout = (delta / 1000000) + 1;
4031 } else {
4032 /* Wait for either IO to occur or the next
4033 timer event. */
4034 add = qemu_next_deadline();
4035 /* We advance the timer before checking for IO.
4036 Limit the amount we advance so that early IO
4037 activity won't get the guest too far ahead. */
4038 if (add > 10000000)
4039 add = 10000000;
4040 delta += add;
4041 add = (add + (1 << icount_time_shift) - 1)
4042 >> icount_time_shift;
4043 qemu_icount += add;
4044 timeout = delta / 1000000;
4045 if (timeout < 0)
4046 timeout = 0;
4047 }
bellardb4608c02003-06-27 17:34:32 +00004048 }
aliguori43b96852009-04-24 18:03:33 +00004049
4050 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004051#else /* CONFIG_IOTHREAD */
4052 return 1000;
4053#endif
aliguori43b96852009-04-24 18:03:33 +00004054}
4055
4056static int vm_can_run(void)
4057{
4058 if (powerdown_requested)
4059 return 0;
4060 if (reset_requested)
4061 return 0;
4062 if (shutdown_requested)
4063 return 0;
aliguorie5689022009-04-24 18:03:54 +00004064 if (debug_requested)
4065 return 0;
aliguori43b96852009-04-24 18:03:33 +00004066 return 1;
4067}
4068
Blue Swirld9c32312009-08-09 08:42:19 +00004069qemu_irq qemu_system_powerdown;
4070
aliguori43b96852009-04-24 18:03:33 +00004071static void main_loop(void)
4072{
aliguori6e29f5d2009-04-24 18:04:02 +00004073 int r;
aliguori43b96852009-04-24 18:03:33 +00004074
aliguorid6dc3d42009-04-24 18:04:07 +00004075#ifdef CONFIG_IOTHREAD
4076 qemu_system_ready = 1;
4077 qemu_cond_broadcast(&qemu_system_cond);
4078#endif
4079
aliguori6e29f5d2009-04-24 18:04:02 +00004080 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004081 do {
4082#ifdef CONFIG_PROFILER
4083 int64_t ti;
4084#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004085#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004086 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004087#endif
aliguori43b96852009-04-24 18:03:33 +00004088#ifdef CONFIG_PROFILER
4089 ti = profile_getclock();
4090#endif
4091 main_loop_wait(qemu_calculate_timeout());
4092#ifdef CONFIG_PROFILER
4093 dev_time += profile_getclock() - ti;
4094#endif
aliguorie5689022009-04-24 18:03:54 +00004095 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004096
aliguorie5689022009-04-24 18:03:54 +00004097 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004098 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004099 if (qemu_shutdown_requested()) {
4100 if (no_shutdown) {
4101 vm_stop(0);
4102 no_shutdown = 0;
4103 } else
4104 break;
4105 }
aliguorid6dc3d42009-04-24 18:04:07 +00004106 if (qemu_reset_requested()) {
4107 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004108 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004109 resume_all_vcpus();
4110 }
Blue Swirld9c32312009-08-09 08:42:19 +00004111 if (qemu_powerdown_requested()) {
4112 qemu_irq_raise(qemu_system_powerdown);
4113 }
aliguori6e29f5d2009-04-24 18:04:02 +00004114 if ((r = qemu_vmstop_requested()))
4115 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004116 }
aliguorid6dc3d42009-04-24 18:04:07 +00004117 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004118}
4119
pbrook9bd7e6d2009-04-07 22:58:45 +00004120static void version(void)
4121{
pbrook4a19f1e2009-04-07 23:17:49 +00004122 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004123}
4124
ths15f82202007-06-29 23:26:08 +00004125static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004126{
pbrook9bd7e6d2009-04-07 22:58:45 +00004127 version();
4128 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004129 "\n"
bellarda20dd502003-09-30 21:07:02 +00004130 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004131 "\n"
blueswir15824d652009-03-28 06:44:27 +00004132#define DEF(option, opt_arg, opt_enum, opt_help) \
4133 opt_help
4134#define DEFHEADING(text) stringify(text) "\n"
4135#include "qemu-options.h"
4136#undef DEF
4137#undef DEFHEADING
4138#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004139 "\n"
bellard82c643f2004-07-14 17:28:13 +00004140 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004141 "ctrl-alt-f toggle full screen\n"
4142 "ctrl-alt-n switch to virtual console 'n'\n"
4143 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004144 "\n"
4145 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4146 ,
bellard0db63472003-10-27 21:37:46 +00004147 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004148 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004149#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004150 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004151 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004152#endif
bellard6e44ba72004-01-18 21:56:49 +00004153 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004154 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004155 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004156}
4157
bellardcd6f1162004-05-13 22:02:20 +00004158#define HAS_ARG 0x0001
4159
4160enum {
blueswir15824d652009-03-28 06:44:27 +00004161#define DEF(option, opt_arg, opt_enum, opt_help) \
4162 opt_enum,
4163#define DEFHEADING(text)
4164#include "qemu-options.h"
4165#undef DEF
4166#undef DEFHEADING
4167#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004168};
4169
4170typedef struct QEMUOption {
4171 const char *name;
4172 int flags;
4173 int index;
4174} QEMUOption;
4175
blueswir1dbed7e42008-10-01 19:38:09 +00004176static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004177 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004178#define DEF(option, opt_arg, opt_enum, opt_help) \
4179 { option, opt_arg, opt_enum },
4180#define DEFHEADING(text)
4181#include "qemu-options.h"
4182#undef DEF
4183#undef DEFHEADING
4184#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004185 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004186};
4187
bellard1d14ffa2005-10-30 18:58:22 +00004188#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004189struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004190#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004191#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004192 {
4193 "pcspk",
4194 "PC speaker",
4195 0,
4196 1,
4197 { .init_isa = pcspk_audio_init }
4198 },
4199#endif
malc4c9b53e2009-01-09 10:46:34 +00004200
4201#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004202 {
4203 "sb16",
4204 "Creative Sound Blaster 16",
4205 0,
4206 1,
4207 { .init_isa = SB16_init }
4208 },
malc4c9b53e2009-01-09 10:46:34 +00004209#endif
bellard6a36d842005-12-18 20:34:32 +00004210
malccc53d262008-06-13 10:48:22 +00004211#ifdef CONFIG_CS4231A
4212 {
4213 "cs4231a",
4214 "CS4231A",
4215 0,
4216 1,
4217 { .init_isa = cs4231a_init }
4218 },
4219#endif
4220
bellard6a36d842005-12-18 20:34:32 +00004221#ifdef CONFIG_ADLIB
4222 {
4223 "adlib",
4224#ifdef HAS_YMF262
4225 "Yamaha YMF262 (OPL3)",
4226#else
4227 "Yamaha YM3812 (OPL2)",
4228#endif
4229 0,
4230 1,
4231 { .init_isa = Adlib_init }
4232 },
4233#endif
4234
4235#ifdef CONFIG_GUS
4236 {
4237 "gus",
4238 "Gravis Ultrasound GF1",
4239 0,
4240 1,
4241 { .init_isa = GUS_init }
4242 },
4243#endif
4244
malc4c9b53e2009-01-09 10:46:34 +00004245#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004246 {
4247 "ac97",
4248 "Intel 82801AA AC97 Audio",
4249 0,
4250 0,
4251 { .init_pci = ac97_init }
4252 },
malc4c9b53e2009-01-09 10:46:34 +00004253#endif
balroge5c9a132008-01-14 04:27:55 +00004254
malc4c9b53e2009-01-09 10:46:34 +00004255#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004256 {
4257 "es1370",
4258 "ENSONIQ AudioPCI ES1370",
4259 0,
4260 0,
4261 { .init_pci = es1370_init }
4262 },
balrogb00052e2007-04-30 02:22:06 +00004263#endif
bellard6a36d842005-12-18 20:34:32 +00004264
malc4c9b53e2009-01-09 10:46:34 +00004265#endif /* HAS_AUDIO_CHOICE */
4266
bellard6a36d842005-12-18 20:34:32 +00004267 { NULL, NULL, 0, 0, { NULL } }
4268};
4269
bellard1d14ffa2005-10-30 18:58:22 +00004270static void select_soundhw (const char *optarg)
4271{
bellard6a36d842005-12-18 20:34:32 +00004272 struct soundhw *c;
4273
bellard1d14ffa2005-10-30 18:58:22 +00004274 if (*optarg == '?') {
4275 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004276
bellard1d14ffa2005-10-30 18:58:22 +00004277 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004278 for (c = soundhw; c->name; ++c) {
4279 printf ("%-11s %s\n", c->name, c->descr);
4280 }
4281 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004282 exit (*optarg != '?');
4283 }
4284 else {
bellard6a36d842005-12-18 20:34:32 +00004285 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004286 const char *p;
4287 char *e;
4288 int bad_card = 0;
4289
bellard6a36d842005-12-18 20:34:32 +00004290 if (!strcmp (optarg, "all")) {
4291 for (c = soundhw; c->name; ++c) {
4292 c->enabled = 1;
4293 }
4294 return;
4295 }
bellard1d14ffa2005-10-30 18:58:22 +00004296
bellard6a36d842005-12-18 20:34:32 +00004297 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004298 while (*p) {
4299 e = strchr (p, ',');
4300 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004301
4302 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004303 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004304 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004305 break;
4306 }
4307 }
bellard6a36d842005-12-18 20:34:32 +00004308
4309 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004310 if (l > 80) {
4311 fprintf (stderr,
4312 "Unknown sound card name (too big to show)\n");
4313 }
4314 else {
4315 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4316 (int) l, p);
4317 }
4318 bad_card = 1;
4319 }
4320 p += l + (e != NULL);
4321 }
4322
4323 if (bad_card)
4324 goto show_valid_cards;
4325 }
4326}
4327#endif
4328
malc3893c122008-09-28 00:42:05 +00004329static void select_vgahw (const char *p)
4330{
4331 const char *opts;
4332
Zachary Amsden86176752009-07-30 00:15:02 -10004333 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004334 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004335 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004336 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004337 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004338 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004339 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004340 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004341 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004342 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004343 invalid_vga:
4344 fprintf(stderr, "Unknown vga type: %s\n", p);
4345 exit(1);
4346 }
malccb5a7aa2008-09-28 00:42:12 +00004347 while (*opts) {
4348 const char *nextopt;
4349
4350 if (strstart(opts, ",retrace=", &nextopt)) {
4351 opts = nextopt;
4352 if (strstart(opts, "dumb", &nextopt))
4353 vga_retrace_method = VGA_RETRACE_DUMB;
4354 else if (strstart(opts, "precise", &nextopt))
4355 vga_retrace_method = VGA_RETRACE_PRECISE;
4356 else goto invalid_vga;
4357 } else goto invalid_vga;
4358 opts = nextopt;
4359 }
malc3893c122008-09-28 00:42:05 +00004360}
4361
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004362#ifdef TARGET_I386
4363static int balloon_parse(const char *arg)
4364{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004365 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004366
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004367 if (strcmp(arg, "none") == 0) {
4368 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004369 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004370
4371 if (!strncmp(arg, "virtio", 6)) {
4372 if (arg[6] == ',') {
4373 /* have params -> parse them */
4374 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4375 if (!opts)
4376 return -1;
4377 } else {
4378 /* create empty opts */
4379 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4380 }
4381 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4382 return 0;
4383 }
4384
4385 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004386}
4387#endif
4388
bellard3587d7e2006-06-26 20:03:44 +00004389#ifdef _WIN32
4390static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4391{
4392 exit(STATUS_CONTROL_C_EXIT);
4393 return TRUE;
4394}
4395#endif
4396
aliguoric4be29f2009-04-17 18:58:14 +00004397int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004398{
4399 int ret;
4400
4401 if(strlen(str) != 36)
4402 return -1;
4403
4404 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4405 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4406 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4407
4408 if(ret != 16)
4409 return -1;
4410
aliguorib6f6e3d2009-04-17 18:59:56 +00004411#ifdef TARGET_I386
4412 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4413#endif
4414
blueswir18fcb1b92008-09-18 18:29:08 +00004415 return 0;
4416}
4417
bellard7c9d8e02005-11-15 22:16:05 +00004418#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004419
aliguori5b08fc12008-08-21 20:08:03 +00004420#ifndef _WIN32
4421
4422static void termsig_handler(int signal)
4423{
4424 qemu_system_shutdown_request();
4425}
4426
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004427static void sigchld_handler(int signal)
4428{
4429 waitpid(-1, NULL, WNOHANG);
4430}
4431
4432static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004433{
4434 struct sigaction act;
4435
4436 memset(&act, 0, sizeof(act));
4437 act.sa_handler = termsig_handler;
4438 sigaction(SIGINT, &act, NULL);
4439 sigaction(SIGHUP, &act, NULL);
4440 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004441
4442 act.sa_handler = sigchld_handler;
4443 act.sa_flags = SA_NOCLDSTOP;
4444 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004445}
4446
4447#endif
4448
Paul Brook5cea8592009-05-30 00:52:44 +01004449#ifdef _WIN32
4450/* Look for support files in the same directory as the executable. */
4451static char *find_datadir(const char *argv0)
4452{
4453 char *p;
4454 char buf[MAX_PATH];
4455 DWORD len;
4456
4457 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4458 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004459 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004460 }
4461
4462 buf[len] = 0;
4463 p = buf + len - 1;
4464 while (p != buf && *p != '\\')
4465 p--;
4466 *p = 0;
4467 if (access(buf, R_OK) == 0) {
4468 return qemu_strdup(buf);
4469 }
4470 return NULL;
4471}
4472#else /* !_WIN32 */
4473
4474/* Find a likely location for support files using the location of the binary.
4475 For installed binaries this will be "$bindir/../share/qemu". When
4476 running from the build tree this will be "$bindir/../pc-bios". */
4477#define SHARE_SUFFIX "/share/qemu"
4478#define BUILD_SUFFIX "/pc-bios"
4479static char *find_datadir(const char *argv0)
4480{
4481 char *dir;
4482 char *p = NULL;
4483 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004484 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004485 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004486
4487#if defined(__linux__)
4488 {
4489 int len;
4490 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4491 if (len > 0) {
4492 buf[len] = 0;
4493 p = buf;
4494 }
4495 }
4496#elif defined(__FreeBSD__)
4497 {
4498 int len;
4499 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4500 if (len > 0) {
4501 buf[len] = 0;
4502 p = buf;
4503 }
4504 }
4505#endif
4506 /* If we don't have any way of figuring out the actual executable
4507 location then try argv[0]. */
4508 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004509 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004510 if (!p) {
4511 return NULL;
4512 }
4513 }
4514 dir = dirname(p);
4515 dir = dirname(dir);
4516
Blue Swirl3a417592009-06-09 19:12:21 +00004517 max_len = strlen(dir) +
4518 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4519 res = qemu_mallocz(max_len);
4520 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004521 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004522 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004523 if (access(res, R_OK)) {
4524 qemu_free(res);
4525 res = NULL;
4526 }
4527 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004528
Paul Brook5cea8592009-05-30 00:52:44 +01004529 return res;
4530}
4531#undef SHARE_SUFFIX
4532#undef BUILD_SUFFIX
4533#endif
4534
4535char *qemu_find_file(int type, const char *name)
4536{
4537 int len;
4538 const char *subdir;
4539 char *buf;
4540
4541 /* If name contains path separators then try it as a straight path. */
4542 if ((strchr(name, '/') || strchr(name, '\\'))
4543 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004544 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004545 }
4546 switch (type) {
4547 case QEMU_FILE_TYPE_BIOS:
4548 subdir = "";
4549 break;
4550 case QEMU_FILE_TYPE_KEYMAP:
4551 subdir = "keymaps/";
4552 break;
4553 default:
4554 abort();
4555 }
4556 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4557 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004558 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004559 if (access(buf, R_OK)) {
4560 qemu_free(buf);
4561 return NULL;
4562 }
4563 return buf;
4564}
4565
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004566static int device_init_func(QemuOpts *opts, void *opaque)
4567{
4568 DeviceState *dev;
4569
4570 dev = qdev_device_add(opts);
4571 if (!dev)
4572 return -1;
4573 return 0;
4574}
4575
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004576struct device_config {
4577 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004578 DEV_USB, /* -usbdevice */
4579 DEV_BT, /* -bt */
4580 } type;
4581 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004582 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004583};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004584QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004585
4586static void add_device_config(int type, const char *cmdline)
4587{
4588 struct device_config *conf;
4589
4590 conf = qemu_mallocz(sizeof(*conf));
4591 conf->type = type;
4592 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004593 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004594}
4595
4596static int foreach_device_config(int type, int (*func)(const char *cmdline))
4597{
4598 struct device_config *conf;
4599 int rc;
4600
Blue Swirl72cf2d42009-09-12 07:36:22 +00004601 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004602 if (conf->type != type)
4603 continue;
4604 rc = func(conf->cmdline);
4605 if (0 != rc)
4606 return rc;
4607 }
4608 return 0;
4609}
4610
malc902b3d52008-12-10 19:18:40 +00004611int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004612{
aliguori59030a82009-04-05 18:43:41 +00004613 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004614 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004615 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004616 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004617 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004618 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004619 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004620 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004621 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004622 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004623 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004624 int nb_net_clients;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004625 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004626 int optind;
4627 const char *r, *optarg;
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004628 CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
4629 const char *monitor_devices[MAX_MONITOR_DEVICES];
4630 int monitor_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004631 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004632 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004633 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004634 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004635 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4636 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004637 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004638 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004639 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004640#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004641 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004642#endif
bellard26a5f132008-05-28 12:30:31 +00004643 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004644 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004645 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004646#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004647 int fd = 0;
4648 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004649 const char *chroot_dir = NULL;
4650 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004651#endif
aliguori268a3622009-04-21 22:30:27 +00004652 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004653 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004654
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004655 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004656 qemu_cache_utils_init(envp);
4657
Blue Swirl72cf2d42009-09-12 07:36:22 +00004658 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004659#ifndef _WIN32
4660 {
4661 struct sigaction act;
4662 sigfillset(&act.sa_mask);
4663 act.sa_flags = 0;
4664 act.sa_handler = SIG_IGN;
4665 sigaction(SIGPIPE, &act, NULL);
4666 }
bellard3587d7e2006-06-26 20:03:44 +00004667#else
4668 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004669 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4670 QEMU to run on a single CPU */
4671 {
4672 HANDLE h;
4673 DWORD mask, smask;
4674 int i;
4675 h = GetCurrentProcess();
4676 if (GetProcessAffinityMask(h, &mask, &smask)) {
4677 for(i = 0; i < 32; i++) {
4678 if (mask & (1 << i))
4679 break;
4680 }
4681 if (i != 32) {
4682 mask = 1 << i;
4683 SetProcessAffinityMask(h, mask);
4684 }
4685 }
4686 }
bellard67b915a2004-03-31 23:37:16 +00004687#endif
bellardbe995c22006-06-25 16:25:21 +00004688
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004689 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004690 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004691 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004692 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004693 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004694 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004695 kernel_filename = NULL;
4696 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004697 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004698 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004699
aurel32c75a8232008-05-04 00:50:34 +00004700 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004701 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004702 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004703 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004704
aliguori8290edd2009-02-27 20:14:29 +00004705 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004706 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004707 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004708 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004709
aliguori1b8fc812009-02-27 20:01:39 +00004710 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004711 virtio_consoles[i] = NULL;
4712 virtio_console_index = 0;
4713
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02004714 monitor_devices[0] = "vc:80Cx24C";
4715 for (i = 1; i < MAX_MONITOR_DEVICES; i++) {
4716 monitor_devices[i] = NULL;
4717 }
4718 monitor_device_index = 0;
4719
aliguori268a3622009-04-21 22:30:27 +00004720 for (i = 0; i < MAX_NODES; i++) {
4721 node_mem[i] = 0;
4722 node_cpumask[i] = 0;
4723 }
4724
bellard7c9d8e02005-11-15 22:16:05 +00004725 nb_net_clients = 0;
aliguori268a3622009-04-21 22:30:27 +00004726 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004727 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004728
bellard26a5f132008-05-28 12:30:31 +00004729 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004730 autostart= 1;
4731
bellardcd6f1162004-05-13 22:02:20 +00004732 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004733 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004734 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004735 break;
bellardcd6f1162004-05-13 22:02:20 +00004736 r = argv[optind];
4737 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004738 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004739 } else {
4740 const QEMUOption *popt;
4741
4742 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004743 /* Treat --foo the same as -foo. */
4744 if (r[1] == '-')
4745 r++;
bellardcd6f1162004-05-13 22:02:20 +00004746 popt = qemu_options;
4747 for(;;) {
4748 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004749 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004750 argv[0], r);
4751 exit(1);
4752 }
4753 if (!strcmp(popt->name, r + 1))
4754 break;
4755 popt++;
4756 }
4757 if (popt->flags & HAS_ARG) {
4758 if (optind >= argc) {
4759 fprintf(stderr, "%s: option '%s' requires an argument\n",
4760 argv[0], r);
4761 exit(1);
4762 }
4763 optarg = argv[optind++];
4764 } else {
4765 optarg = NULL;
4766 }
4767
4768 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004769 case QEMU_OPTION_M:
4770 machine = find_machine(optarg);
4771 if (!machine) {
4772 QEMUMachine *m;
4773 printf("Supported machines are:\n");
4774 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004775 if (m->alias)
4776 printf("%-10s %s (alias of %s)\n",
4777 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004778 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004779 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004780 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004781 }
ths15f82202007-06-29 23:26:08 +00004782 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004783 }
4784 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004785 case QEMU_OPTION_cpu:
4786 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004787 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004788/* XXX: implement xxx_cpu_list for targets that still miss it */
4789#if defined(cpu_list)
4790 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004791#endif
ths15f82202007-06-29 23:26:08 +00004792 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004793 } else {
4794 cpu_model = optarg;
4795 }
4796 break;
bellardcd6f1162004-05-13 22:02:20 +00004797 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004798 initrd_filename = optarg;
4799 break;
bellardcd6f1162004-05-13 22:02:20 +00004800 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004801 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004802 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004803 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004804 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004805 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004806 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004807 translation == BIOS_ATA_TRANSLATION_LBA ?
4808 ",trans=lba" :
4809 translation == BIOS_ATA_TRANSLATION_NONE ?
4810 ",trans=none" : "");
4811 break;
bellardcd6f1162004-05-13 22:02:20 +00004812 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004813 case QEMU_OPTION_hdc:
4814 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004815 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004816 break;
thse4bcb142007-12-02 04:51:10 +00004817 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004818 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004819 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004820 case QEMU_OPTION_set:
4821 if (qemu_set_option(optarg) != 0)
4822 exit(1);
4823 break;
balrog3e3d5812007-04-30 02:09:25 +00004824 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004825 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004826 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004827 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004828 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004829 break;
j_mayer86f55662007-04-24 06:52:59 +00004830 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004831 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004832 break;
bellardcd6f1162004-05-13 22:02:20 +00004833 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004834 snapshot = 1;
4835 break;
bellardcd6f1162004-05-13 22:02:20 +00004836 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004837 {
bellard330d0412003-07-26 18:11:40 +00004838 const char *p;
4839 p = optarg;
4840 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004841 if (cyls < 1 || cyls > 16383)
4842 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004843 if (*p != ',')
4844 goto chs_fail;
4845 p++;
4846 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004847 if (heads < 1 || heads > 16)
4848 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004849 if (*p != ',')
4850 goto chs_fail;
4851 p++;
4852 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004853 if (secs < 1 || secs > 63)
4854 goto chs_fail;
4855 if (*p == ',') {
4856 p++;
4857 if (!strcmp(p, "none"))
4858 translation = BIOS_ATA_TRANSLATION_NONE;
4859 else if (!strcmp(p, "lba"))
4860 translation = BIOS_ATA_TRANSLATION_LBA;
4861 else if (!strcmp(p, "auto"))
4862 translation = BIOS_ATA_TRANSLATION_AUTO;
4863 else
4864 goto chs_fail;
4865 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004866 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004867 fprintf(stderr, "qemu: invalid physical CHS format\n");
4868 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004869 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004870 if (hda_opts != NULL) {
4871 char num[16];
4872 snprintf(num, sizeof(num), "%d", cyls);
4873 qemu_opt_set(hda_opts, "cyls", num);
4874 snprintf(num, sizeof(num), "%d", heads);
4875 qemu_opt_set(hda_opts, "heads", num);
4876 snprintf(num, sizeof(num), "%d", secs);
4877 qemu_opt_set(hda_opts, "secs", num);
4878 if (translation == BIOS_ATA_TRANSLATION_LBA)
4879 qemu_opt_set(hda_opts, "trans", "lba");
4880 if (translation == BIOS_ATA_TRANSLATION_NONE)
4881 qemu_opt_set(hda_opts, "trans", "none");
4882 }
bellard330d0412003-07-26 18:11:40 +00004883 }
4884 break;
aliguori268a3622009-04-21 22:30:27 +00004885 case QEMU_OPTION_numa:
4886 if (nb_numa_nodes >= MAX_NODES) {
4887 fprintf(stderr, "qemu: too many NUMA nodes\n");
4888 exit(1);
4889 }
4890 numa_add(optarg);
4891 break;
bellardcd6f1162004-05-13 22:02:20 +00004892 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004893 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00004894 break;
balrog4d3b6f62008-02-10 16:33:14 +00004895#ifdef CONFIG_CURSES
4896 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004897 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00004898 break;
4899#endif
balroga171fe32007-04-30 01:48:07 +00004900 case QEMU_OPTION_portrait:
4901 graphic_rotate = 1;
4902 break;
bellardcd6f1162004-05-13 22:02:20 +00004903 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004904 kernel_filename = optarg;
4905 break;
bellardcd6f1162004-05-13 22:02:20 +00004906 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004907 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004908 break;
bellardcd6f1162004-05-13 22:02:20 +00004909 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004910 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004911 break;
bellardcd6f1162004-05-13 22:02:20 +00004912 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004913 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004914 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02004915 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004916 };
4917 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004918 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004919 int legacy = 0;
4920
4921 if (!strchr(optarg, '=')) {
4922 legacy = 1;
4923 pstrcpy(buf, sizeof(buf), optarg);
4924 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
4925 fprintf(stderr,
4926 "qemu: unknown boot parameter '%s' in '%s'\n",
4927 buf, optarg);
4928 exit(1);
4929 }
4930
4931 if (legacy ||
4932 get_param_value(buf, sizeof(buf), "order", optarg)) {
4933 boot_devices_bitmap = parse_bootdevices(buf);
4934 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00004935 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004936 if (!legacy) {
4937 if (get_param_value(buf, sizeof(buf),
4938 "once", optarg)) {
4939 boot_devices_bitmap |= parse_bootdevices(buf);
4940 standard_boot_devices = qemu_strdup(boot_devices);
4941 pstrcpy(boot_devices, sizeof(boot_devices), buf);
4942 qemu_register_reset(restore_boot_devices,
4943 standard_boot_devices);
4944 }
Jan Kiszka95387492009-07-02 00:19:02 +02004945 if (get_param_value(buf, sizeof(buf),
4946 "menu", optarg)) {
4947 if (!strcmp(buf, "on")) {
4948 boot_menu = 1;
4949 } else if (!strcmp(buf, "off")) {
4950 boot_menu = 0;
4951 } else {
4952 fprintf(stderr,
4953 "qemu: invalid option value '%s'\n",
4954 buf);
4955 exit(1);
4956 }
4957 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02004958 }
bellard36b486b2003-11-11 13:36:08 +00004959 }
4960 break;
bellardcd6f1162004-05-13 22:02:20 +00004961 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004962 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004963 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004964 break;
bellard52ca8d62006-06-14 16:03:05 +00004965#ifdef TARGET_I386
4966 case QEMU_OPTION_no_fd_bootchk:
4967 fd_bootchk = 0;
4968 break;
4969#endif
bellard7c9d8e02005-11-15 22:16:05 +00004970 case QEMU_OPTION_net:
4971 if (nb_net_clients >= MAX_NET_CLIENTS) {
4972 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004973 exit(1);
4974 }
pbrookfd5f3932008-03-26 20:55:43 +00004975 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004976 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004977 break;
bellardc7f74642004-08-24 21:57:12 +00004978#ifdef CONFIG_SLIRP
4979 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02004980 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004981 break;
ths47d5d012007-02-20 00:05:08 +00004982 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02004983 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00004984 break;
bellardc94c8d62004-09-13 21:37:34 +00004985#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004986 case QEMU_OPTION_smb:
Jan Kiszkaad196a92009-06-24 14:42:28 +02004987 net_slirp_smb(optarg);
bellard9d728e82004-09-05 23:09:03 +00004988 break;
bellardc94c8d62004-09-13 21:37:34 +00004989#endif
bellard9bf05442004-08-25 22:12:49 +00004990 case QEMU_OPTION_redir:
Jan Kiszkaf3546de2009-06-24 14:42:28 +02004991 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004992 break;
bellardc7f74642004-08-24 21:57:12 +00004993#endif
balrogdc72ac12008-11-09 00:04:26 +00004994 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004995 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00004996 break;
bellard1d14ffa2005-10-30 18:58:22 +00004997#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004998 case QEMU_OPTION_audio_help:
4999 AUD_help ();
5000 exit (0);
5001 break;
5002 case QEMU_OPTION_soundhw:
5003 select_soundhw (optarg);
5004 break;
5005#endif
bellardcd6f1162004-05-13 22:02:20 +00005006 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005007 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005008 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005009 case QEMU_OPTION_version:
5010 version();
5011 exit(0);
5012 break;
aurel3200f82b82008-04-27 21:12:55 +00005013 case QEMU_OPTION_m: {
5014 uint64_t value;
5015 char *ptr;
5016
5017 value = strtoul(optarg, &ptr, 10);
5018 switch (*ptr) {
5019 case 0: case 'M': case 'm':
5020 value <<= 20;
5021 break;
5022 case 'G': case 'g':
5023 value <<= 30;
5024 break;
5025 default:
5026 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005027 exit(1);
5028 }
aurel3200f82b82008-04-27 21:12:55 +00005029
5030 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005031 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005032 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5033 exit(1);
5034 }
5035 if (value != (uint64_t)(ram_addr_t)value) {
5036 fprintf(stderr, "qemu: ram size too large\n");
5037 exit(1);
5038 }
5039 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005040 break;
aurel3200f82b82008-04-27 21:12:55 +00005041 }
bellardcd6f1162004-05-13 22:02:20 +00005042 case QEMU_OPTION_d:
5043 {
5044 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005045 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005046
bellardcd6f1162004-05-13 22:02:20 +00005047 mask = cpu_str_to_log_mask(optarg);
5048 if (!mask) {
5049 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005050 for(item = cpu_log_items; item->mask != 0; item++) {
5051 printf("%-10s %s\n", item->name, item->help);
5052 }
5053 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005054 }
5055 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005056 }
bellardcd6f1162004-05-13 22:02:20 +00005057 break;
bellardcd6f1162004-05-13 22:02:20 +00005058 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005059 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005060 break;
aliguori59030a82009-04-05 18:43:41 +00005061 case QEMU_OPTION_gdb:
5062 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005063 break;
bellardcd6f1162004-05-13 22:02:20 +00005064 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005065 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005066 break;
j_mayer1192dad2007-10-05 13:08:35 +00005067 case QEMU_OPTION_bios:
5068 bios_name = optarg;
5069 break;
aurel321b530a62009-04-05 20:08:59 +00005070 case QEMU_OPTION_singlestep:
5071 singlestep = 1;
5072 break;
bellardcd6f1162004-05-13 22:02:20 +00005073 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005074 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005075 break;
blueswir15824d652009-03-28 06:44:27 +00005076#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005077 case QEMU_OPTION_k:
5078 keyboard_layout = optarg;
5079 break;
blueswir15824d652009-03-28 06:44:27 +00005080#endif
bellardee22c2f2004-06-03 12:49:50 +00005081 case QEMU_OPTION_localtime:
5082 rtc_utc = 0;
5083 break;
malc3893c122008-09-28 00:42:05 +00005084 case QEMU_OPTION_vga:
5085 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005086 break;
blueswir15824d652009-03-28 06:44:27 +00005087#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005088 case QEMU_OPTION_g:
5089 {
5090 const char *p;
5091 int w, h, depth;
5092 p = optarg;
5093 w = strtol(p, (char **)&p, 10);
5094 if (w <= 0) {
5095 graphic_error:
5096 fprintf(stderr, "qemu: invalid resolution or depth\n");
5097 exit(1);
5098 }
5099 if (*p != 'x')
5100 goto graphic_error;
5101 p++;
5102 h = strtol(p, (char **)&p, 10);
5103 if (h <= 0)
5104 goto graphic_error;
5105 if (*p == 'x') {
5106 p++;
5107 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005108 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005109 depth != 24 && depth != 32)
5110 goto graphic_error;
5111 } else if (*p == '\0') {
5112 depth = graphic_depth;
5113 } else {
5114 goto graphic_error;
5115 }
ths3b46e622007-09-17 08:09:54 +00005116
bellarde9b137c2004-06-21 16:46:10 +00005117 graphic_width = w;
5118 graphic_height = h;
5119 graphic_depth = depth;
5120 }
5121 break;
blueswir15824d652009-03-28 06:44:27 +00005122#endif
ths20d8a3e2007-02-18 17:04:49 +00005123 case QEMU_OPTION_echr:
5124 {
5125 char *r;
5126 term_escape_char = strtol(optarg, &r, 0);
5127 if (r == optarg)
5128 printf("Bad argument to echr\n");
5129 break;
5130 }
bellard82c643f2004-07-14 17:28:13 +00005131 case QEMU_OPTION_monitor:
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005132 if (monitor_device_index >= MAX_MONITOR_DEVICES) {
5133 fprintf(stderr, "qemu: too many monitor devices\n");
5134 exit(1);
5135 }
5136 monitor_devices[monitor_device_index] = optarg;
5137 monitor_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005138 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005139 case QEMU_OPTION_chardev:
5140 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5141 if (!opts) {
5142 fprintf(stderr, "parse error: %s\n", optarg);
5143 exit(1);
5144 }
Mark McLoughlin3df04ac2009-09-23 11:24:05 +01005145 if (qemu_chr_open_opts(opts, NULL) == NULL) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005146 exit(1);
5147 }
5148 break;
bellard82c643f2004-07-14 17:28:13 +00005149 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005150 if (serial_device_index >= MAX_SERIAL_PORTS) {
5151 fprintf(stderr, "qemu: too many serial ports\n");
5152 exit(1);
5153 }
pbrookfd5f3932008-03-26 20:55:43 +00005154 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005155 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005156 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005157 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005158 if (watchdog) {
5159 fprintf(stderr,
5160 "qemu: only one watchdog option may be given\n");
5161 return 1;
5162 }
5163 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005164 break;
5165 case QEMU_OPTION_watchdog_action:
5166 if (select_watchdog_action(optarg) == -1) {
5167 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5168 exit(1);
5169 }
5170 break;
aliguori51ecf132009-01-15 20:06:40 +00005171 case QEMU_OPTION_virtiocon:
5172 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5173 fprintf(stderr, "qemu: too many virtio consoles\n");
5174 exit(1);
5175 }
5176 virtio_consoles[virtio_console_index] = optarg;
5177 virtio_console_index++;
5178 break;
bellard6508fe52005-01-15 12:02:56 +00005179 case QEMU_OPTION_parallel:
5180 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5181 fprintf(stderr, "qemu: too many parallel ports\n");
5182 exit(1);
5183 }
pbrookfd5f3932008-03-26 20:55:43 +00005184 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005185 parallel_device_index++;
5186 break;
bellardd63d3072004-10-03 13:29:03 +00005187 case QEMU_OPTION_loadvm:
5188 loadvm = optarg;
5189 break;
5190 case QEMU_OPTION_full_screen:
5191 full_screen = 1;
5192 break;
ths667acca2006-12-11 02:08:05 +00005193#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005194 case QEMU_OPTION_no_frame:
5195 no_frame = 1;
5196 break;
ths3780e192007-06-21 21:08:02 +00005197 case QEMU_OPTION_alt_grab:
5198 alt_grab = 1;
5199 break;
ths667acca2006-12-11 02:08:05 +00005200 case QEMU_OPTION_no_quit:
5201 no_quit = 1;
5202 break;
aliguori7d957bd2009-01-15 22:14:11 +00005203 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005204 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005205 break;
ths667acca2006-12-11 02:08:05 +00005206#endif
bellardf7cce892004-12-08 22:21:25 +00005207 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005208 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005209 break;
bellarda09db212005-04-30 16:10:35 +00005210#ifdef TARGET_I386
5211 case QEMU_OPTION_win2k_hack:
5212 win2k_install_hack = 1;
5213 break;
aliguori73822ec2009-01-15 20:11:34 +00005214 case QEMU_OPTION_rtc_td_hack:
5215 rtc_td_hack = 1;
5216 break;
aliguori8a92ea22009-02-27 20:12:36 +00005217 case QEMU_OPTION_acpitable:
5218 if(acpi_table_add(optarg) < 0) {
5219 fprintf(stderr, "Wrong acpi table provided\n");
5220 exit(1);
5221 }
5222 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005223 case QEMU_OPTION_smbios:
5224 if(smbios_entry_add(optarg) < 0) {
5225 fprintf(stderr, "Wrong smbios provided\n");
5226 exit(1);
5227 }
5228 break;
bellarda09db212005-04-30 16:10:35 +00005229#endif
aliguori7ba1e612008-11-05 16:04:33 +00005230#ifdef CONFIG_KVM
5231 case QEMU_OPTION_enable_kvm:
5232 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005233 break;
5234#endif
bellardbb36d472005-11-05 14:22:28 +00005235 case QEMU_OPTION_usb:
5236 usb_enabled = 1;
5237 break;
bellarda594cfb2005-11-06 16:13:29 +00005238 case QEMU_OPTION_usbdevice:
5239 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005240 add_device_config(DEV_USB, optarg);
5241 break;
5242 case QEMU_OPTION_device:
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005243 opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
5244 if (!opts) {
5245 fprintf(stderr, "parse error: %s\n", optarg);
5246 exit(1);
5247 }
bellarda594cfb2005-11-06 16:13:29 +00005248 break;
bellard6a00d602005-11-21 23:25:50 +00005249 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005250 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005251 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005252 fprintf(stderr, "Invalid number of CPUs\n");
5253 exit(1);
5254 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005255 if (max_cpus < smp_cpus) {
5256 fprintf(stderr, "maxcpus must be equal to or greater than "
5257 "smp\n");
5258 exit(1);
5259 }
5260 if (max_cpus > 255) {
5261 fprintf(stderr, "Unsupported number of maxcpus\n");
5262 exit(1);
5263 }
bellard6a00d602005-11-21 23:25:50 +00005264 break;
bellard24236862006-04-30 21:28:36 +00005265 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005266 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005267 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005268 break;
blueswir15824d652009-03-28 06:44:27 +00005269#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005270 case QEMU_OPTION_no_acpi:
5271 acpi_enabled = 0;
5272 break;
aliguori16b29ae2008-12-17 23:28:44 +00005273 case QEMU_OPTION_no_hpet:
5274 no_hpet = 1;
5275 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005276 case QEMU_OPTION_balloon:
5277 if (balloon_parse(optarg) < 0) {
5278 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5279 exit(1);
5280 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005281 break;
blueswir15824d652009-03-28 06:44:27 +00005282#endif
bellardd1beab82006-10-02 19:44:22 +00005283 case QEMU_OPTION_no_reboot:
5284 no_reboot = 1;
5285 break;
aurel32b2f76162008-04-11 21:35:52 +00005286 case QEMU_OPTION_no_shutdown:
5287 no_shutdown = 1;
5288 break;
balrog9467cd42007-05-01 01:34:14 +00005289 case QEMU_OPTION_show_cursor:
5290 cursor_hide = 0;
5291 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005292 case QEMU_OPTION_uuid:
5293 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5294 fprintf(stderr, "Fail to parse UUID string."
5295 " Wrong format.\n");
5296 exit(1);
5297 }
5298 break;
blueswir15824d652009-03-28 06:44:27 +00005299#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005300 case QEMU_OPTION_daemonize:
5301 daemonize = 1;
5302 break;
blueswir15824d652009-03-28 06:44:27 +00005303#endif
ths9ae02552007-01-05 17:39:04 +00005304 case QEMU_OPTION_option_rom:
5305 if (nb_option_roms >= MAX_OPTION_ROMS) {
5306 fprintf(stderr, "Too many option ROMs\n");
5307 exit(1);
5308 }
5309 option_rom[nb_option_roms] = optarg;
5310 nb_option_roms++;
5311 break;
blueswir15824d652009-03-28 06:44:27 +00005312#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005313 case QEMU_OPTION_semihosting:
5314 semihosting_enabled = 1;
5315 break;
blueswir15824d652009-03-28 06:44:27 +00005316#endif
thsc35734b2007-03-19 15:17:08 +00005317 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005318 qemu_name = qemu_strdup(optarg);
5319 {
5320 char *p = strchr(qemu_name, ',');
5321 if (p != NULL) {
5322 *p++ = 0;
5323 if (strncmp(p, "process=", 8)) {
5324 fprintf(stderr, "Unknown subargument %s to -name", p);
5325 exit(1);
5326 }
5327 p += 8;
5328 set_proc_name(p);
5329 }
5330 }
thsc35734b2007-03-19 15:17:08 +00005331 break;
blueswir195efd112008-12-24 20:26:14 +00005332#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005333 case QEMU_OPTION_prom_env:
5334 if (nb_prom_envs >= MAX_PROM_ENVS) {
5335 fprintf(stderr, "Too many prom variables\n");
5336 exit(1);
5337 }
5338 prom_envs[nb_prom_envs] = optarg;
5339 nb_prom_envs++;
5340 break;
5341#endif
balrog2b8f2d42007-07-27 22:08:46 +00005342#ifdef TARGET_ARM
5343 case QEMU_OPTION_old_param:
5344 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005345 break;
balrog2b8f2d42007-07-27 22:08:46 +00005346#endif
thsf3dcfad2007-08-24 01:26:02 +00005347 case QEMU_OPTION_clock:
5348 configure_alarms(optarg);
5349 break;
bellard7e0af5d02007-11-07 16:24:33 +00005350 case QEMU_OPTION_startdate:
5351 {
5352 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005353 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005354 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005355 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005356 } else {
5357 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5358 &tm.tm_year,
5359 &tm.tm_mon,
5360 &tm.tm_mday,
5361 &tm.tm_hour,
5362 &tm.tm_min,
5363 &tm.tm_sec) == 6) {
5364 /* OK */
5365 } else if (sscanf(optarg, "%d-%d-%d",
5366 &tm.tm_year,
5367 &tm.tm_mon,
5368 &tm.tm_mday) == 3) {
5369 tm.tm_hour = 0;
5370 tm.tm_min = 0;
5371 tm.tm_sec = 0;
5372 } else {
5373 goto date_fail;
5374 }
5375 tm.tm_year -= 1900;
5376 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005377 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005378 if (rtc_start_date == -1) {
5379 date_fail:
5380 fprintf(stderr, "Invalid date format. Valid format are:\n"
5381 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5382 exit(1);
5383 }
balrogf6503052008-02-17 11:42:19 +00005384 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005385 }
5386 }
5387 break;
bellard26a5f132008-05-28 12:30:31 +00005388 case QEMU_OPTION_tb_size:
5389 tb_size = strtol(optarg, NULL, 0);
5390 if (tb_size < 0)
5391 tb_size = 0;
5392 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005393 case QEMU_OPTION_icount:
5394 use_icount = 1;
5395 if (strcmp(optarg, "auto") == 0) {
5396 icount_time_shift = -1;
5397 } else {
5398 icount_time_shift = strtol(optarg, NULL, 0);
5399 }
5400 break;
aliguori5bb79102008-10-13 03:12:02 +00005401 case QEMU_OPTION_incoming:
5402 incoming = optarg;
5403 break;
blueswir15824d652009-03-28 06:44:27 +00005404#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005405 case QEMU_OPTION_chroot:
5406 chroot_dir = optarg;
5407 break;
5408 case QEMU_OPTION_runas:
5409 run_as = optarg;
5410 break;
blueswir15824d652009-03-28 06:44:27 +00005411#endif
aliguorie37630c2009-04-22 15:19:10 +00005412#ifdef CONFIG_XEN
5413 case QEMU_OPTION_xen_domid:
5414 xen_domid = atoi(optarg);
5415 break;
5416 case QEMU_OPTION_xen_create:
5417 xen_mode = XEN_CREATE;
5418 break;
5419 case QEMU_OPTION_xen_attach:
5420 xen_mode = XEN_ATTACH;
5421 break;
5422#endif
bellardcd6f1162004-05-13 22:02:20 +00005423 }
bellard0824d6f2003-06-24 13:42:40 +00005424 }
5425 }
bellard330d0412003-07-26 18:11:40 +00005426
Paul Brook5cea8592009-05-30 00:52:44 +01005427 /* If no data_dir is specified then try to find it relative to the
5428 executable path. */
5429 if (!data_dir) {
5430 data_dir = find_datadir(argv[0]);
5431 }
5432 /* If all else fails use the install patch specified when building. */
5433 if (!data_dir) {
5434 data_dir = CONFIG_QEMU_SHAREDIR;
5435 }
5436
Jes Sorensen6be68d72009-07-23 17:03:42 +02005437 /*
5438 * Default to max_cpus = smp_cpus, in case the user doesn't
5439 * specify a max_cpus value.
5440 */
5441 if (!max_cpus)
5442 max_cpus = smp_cpus;
5443
balrog3d878ca2008-10-28 10:59:59 +00005444 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005445 if (smp_cpus > machine->max_cpus) {
5446 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5447 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5448 machine->max_cpus);
5449 exit(1);
5450 }
5451
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005452 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005453 if (serial_device_index == 0)
5454 serial_devices[0] = "stdio";
5455 if (parallel_device_index == 0)
5456 parallel_devices[0] = "null";
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005457 if (strncmp(monitor_devices[0], "vc", 2) == 0) {
5458 monitor_devices[0] = "stdio";
5459 }
aliguoribc0129d2008-08-01 15:12:34 +00005460 }
5461
ths71e3ceb2006-12-22 02:11:31 +00005462#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005463 if (daemonize) {
5464 pid_t pid;
5465
5466 if (pipe(fds) == -1)
5467 exit(1);
5468
5469 pid = fork();
5470 if (pid > 0) {
5471 uint8_t status;
5472 ssize_t len;
5473
5474 close(fds[1]);
5475
5476 again:
ths93815bc2007-03-19 15:58:31 +00005477 len = read(fds[0], &status, 1);
5478 if (len == -1 && (errno == EINTR))
5479 goto again;
5480
5481 if (len != 1)
5482 exit(1);
5483 else if (status == 1) {
5484 fprintf(stderr, "Could not acquire pidfile\n");
5485 exit(1);
5486 } else
5487 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005488 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005489 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005490
5491 setsid();
5492
5493 pid = fork();
5494 if (pid > 0)
5495 exit(0);
5496 else if (pid < 0)
5497 exit(1);
5498
5499 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005500
5501 signal(SIGTSTP, SIG_IGN);
5502 signal(SIGTTOU, SIG_IGN);
5503 signal(SIGTTIN, SIG_IGN);
5504 }
ths71e3ceb2006-12-22 02:11:31 +00005505
thsaa26bb22007-03-25 21:33:06 +00005506 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005507 if (daemonize) {
5508 uint8_t status = 1;
5509 write(fds[1], &status, 1);
5510 } else
5511 fprintf(stderr, "Could not acquire pid file\n");
5512 exit(1);
5513 }
blueswir1b9e82a52009-04-05 18:03:31 +00005514#endif
ths93815bc2007-03-19 15:58:31 +00005515
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005516 if (kvm_enabled()) {
5517 int ret;
5518
5519 ret = kvm_init(smp_cpus);
5520 if (ret < 0) {
5521 fprintf(stderr, "failed to initialize KVM\n");
5522 exit(1);
5523 }
5524 }
5525
aliguori3fcf7b62009-04-24 18:03:25 +00005526 if (qemu_init_main_loop()) {
5527 fprintf(stderr, "qemu_init_main_loop failed\n");
5528 exit(1);
5529 }
bellarda20dd502003-09-30 21:07:02 +00005530 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005531
thsf8d39c02008-07-03 10:01:15 +00005532 if (!linux_boot && *kernel_cmdline != '\0') {
5533 fprintf(stderr, "-append only allowed with -kernel option\n");
5534 exit(1);
5535 }
5536
5537 if (!linux_boot && initrd_filename != NULL) {
5538 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5539 exit(1);
5540 }
5541
Filip Navarabf65f532009-07-27 10:02:04 -05005542#ifndef _WIN32
5543 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005544 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005545#endif
ths3b46e622007-09-17 08:09:54 +00005546
pbrook634fce92006-07-15 17:40:09 +00005547 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005548 if (init_timer_alarm() < 0) {
5549 fprintf(stderr, "could not initialize alarm timer\n");
5550 exit(1);
5551 }
pbrook2e70f6e2008-06-29 01:03:05 +00005552 if (use_icount && icount_time_shift < 0) {
5553 use_icount = 2;
5554 /* 125MIPS seems a reasonable initial guess at the guest speed.
5555 It will be corrected fairly quickly anyway. */
5556 icount_time_shift = 3;
5557 init_icount_adjust();
5558 }
pbrook634fce92006-07-15 17:40:09 +00005559
bellardfd1dff42006-02-01 21:29:26 +00005560#ifdef _WIN32
5561 socket_init();
5562#endif
5563
bellard7c9d8e02005-11-15 22:16:05 +00005564 /* init network clients */
5565 if (nb_net_clients == 0) {
5566 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005567 net_clients[nb_net_clients++] = "nic";
5568#ifdef CONFIG_SLIRP
5569 net_clients[nb_net_clients++] = "user";
5570#endif
bellardc20709a2004-04-21 23:27:19 +00005571 }
5572
bellard7c9d8e02005-11-15 22:16:05 +00005573 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005574 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005575 exit(1);
bellard702c6512004-04-02 21:21:32 +00005576 }
bellardf1510b22003-06-25 00:07:40 +00005577
Glauber Costa406c8df2009-06-17 09:05:30 -04005578 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5579 net_set_boot_mask(net_boot);
5580
5581 net_client_check();
thseec85c22007-01-05 17:41:07 +00005582
balrogdc72ac12008-11-09 00:04:26 +00005583 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005584 if (foreach_device_config(DEV_BT, bt_parse))
5585 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005586
bellard0824d6f2003-06-24 13:42:40 +00005587 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005588 if (ram_size == 0)
5589 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005590
bellard26a5f132008-05-28 12:30:31 +00005591 /* init the dynamic translator */
5592 cpu_exec_init_all(tb_size * 1024 * 1024);
5593
bellard5905b2e2004-08-01 21:53:26 +00005594 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005595
5596 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005597 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005598
balrog9d413d12007-12-04 00:10:34 +00005599 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005600 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005601
balrog9d413d12007-12-04 00:10:34 +00005602 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005603 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005604
ths96d30e42007-01-07 20:42:14 +00005605 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005606 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005607 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5608 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005609 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005610
Juan Quintela2faf58c2009-09-10 03:04:28 +02005611 vmstate_register(0, &vmstate_timers ,&timers_state);
aliguori475e4272008-10-06 20:21:51 +00005612 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005613
aliguori3023f332009-01-16 19:04:14 +00005614 /* Maintain compatibility with multiple stdio monitors */
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005615 if (!strcmp(monitor_devices[0],"stdio")) {
aliguori3023f332009-01-16 19:04:14 +00005616 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5617 const char *devname = serial_devices[i];
5618 if (devname && !strcmp(devname,"mon:stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005619 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005620 break;
5621 } else if (devname && !strcmp(devname,"stdio")) {
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005622 monitor_devices[0] = NULL;
aliguori3023f332009-01-16 19:04:14 +00005623 serial_devices[i] = "mon:stdio";
5624 break;
5625 }
5626 }
5627 }
5628
aliguori268a3622009-04-21 22:30:27 +00005629 if (nb_numa_nodes > 0) {
5630 int i;
5631
5632 if (nb_numa_nodes > smp_cpus) {
5633 nb_numa_nodes = smp_cpus;
5634 }
5635
5636 /* If no memory size if given for any node, assume the default case
5637 * and distribute the available memory equally across all nodes
5638 */
5639 for (i = 0; i < nb_numa_nodes; i++) {
5640 if (node_mem[i] != 0)
5641 break;
5642 }
5643 if (i == nb_numa_nodes) {
5644 uint64_t usedmem = 0;
5645
5646 /* On Linux, the each node's border has to be 8MB aligned,
5647 * the final node gets the rest.
5648 */
5649 for (i = 0; i < nb_numa_nodes - 1; i++) {
5650 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5651 usedmem += node_mem[i];
5652 }
5653 node_mem[i] = ram_size - usedmem;
5654 }
5655
5656 for (i = 0; i < nb_numa_nodes; i++) {
5657 if (node_cpumask[i] != 0)
5658 break;
5659 }
5660 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5661 * must cope with this anyway, because there are BIOSes out there in
5662 * real machines which also use this scheme.
5663 */
5664 if (i == nb_numa_nodes) {
5665 for (i = 0; i < smp_cpus; i++) {
5666 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5667 }
5668 }
5669 }
5670
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005671 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5672 const char *devname = monitor_devices[i];
5673 if (devname && strcmp(devname, "none")) {
5674 char label[32];
5675 if (i == 0) {
5676 snprintf(label, sizeof(label), "monitor");
5677 } else {
5678 snprintf(label, sizeof(label), "monitor%d", i);
5679 }
5680 monitor_hds[i] = qemu_chr_open(label, devname, NULL);
5681 if (!monitor_hds[i]) {
5682 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
5683 devname);
5684 exit(1);
5685 }
aliguori4c621802009-01-16 21:48:20 +00005686 }
5687 }
5688
aliguori2796dae2009-01-16 20:23:27 +00005689 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5690 const char *devname = serial_devices[i];
5691 if (devname && strcmp(devname, "none")) {
Aurelien Jarno324a8022009-09-14 23:49:43 +02005692 char label[32];
5693 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005694 serial_hds[i] = qemu_chr_open(label, devname, NULL);
Aurelien Jarno324a8022009-09-14 23:49:43 +02005695 if (!serial_hds[i]) {
5696 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5697 devname);
5698 exit(1);
5699 }
aliguori2796dae2009-01-16 20:23:27 +00005700 }
5701 }
5702
5703 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5704 const char *devname = parallel_devices[i];
5705 if (devname && strcmp(devname, "none")) {
5706 char label[32];
5707 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005708 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005709 if (!parallel_hds[i]) {
5710 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5711 devname);
5712 exit(1);
5713 }
5714 }
5715 }
5716
5717 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5718 const char *devname = virtio_consoles[i];
5719 if (devname && strcmp(devname, "none")) {
5720 char label[32];
5721 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005722 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005723 if (!virtcon_hds[i]) {
5724 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5725 devname);
5726 exit(1);
5727 }
5728 }
5729 }
5730
Paul Brookaae94602009-05-14 22:35:06 +01005731 module_call_init(MODULE_INIT_DEVICE);
5732
Markus Armbruster09aaa162009-08-21 10:31:34 +02005733 if (watchdog) {
5734 i = select_watchdog(watchdog);
5735 if (i > 0)
5736 exit (i == 1 ? 1 : 0);
5737 }
5738
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005739 if (machine->compat_props) {
5740 qdev_prop_register_compat(machine->compat_props);
5741 }
Paul Brookfbe1b592009-05-13 17:56:25 +01005742 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005743 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5744
aliguori268a3622009-04-21 22:30:27 +00005745
Juan Quintela67b3b712009-08-28 19:25:15 +02005746#ifndef _WIN32
5747 /* must be after terminal init, SDL library changes signal handlers */
5748 sighandler_setup();
5749#endif
5750
aliguori268a3622009-04-21 22:30:27 +00005751 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5752 for (i = 0; i < nb_numa_nodes; i++) {
5753 if (node_cpumask[i] & (1 << env->cpu_index)) {
5754 env->numa_node = i;
5755 }
5756 }
5757 }
5758
aliguori6f338c32009-02-11 15:21:54 +00005759 current_machine = machine;
5760
aliguori3023f332009-01-16 19:04:14 +00005761 /* init USB devices */
5762 if (usb_enabled) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005763 foreach_device_config(DEV_USB, usb_parse);
aliguori3023f332009-01-16 19:04:14 +00005764 }
5765
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005766 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005767 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005768 exit(1);
5769
aliguori8f391ab2009-01-19 16:34:10 +00005770 if (!display_state)
5771 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005772 /* just use the first displaystate for the moment */
5773 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005774
5775 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005776#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005777 display_type = DT_SDL;
5778#else
5779 display_type = DT_VNC;
5780 vnc_display = "localhost:0,to=99";
5781 show_vnc_port = 1;
5782#endif
5783 }
5784
5785
5786 switch (display_type) {
5787 case DT_NOGRAPHIC:
5788 break;
5789#if defined(CONFIG_CURSES)
5790 case DT_CURSES:
5791 curses_display_init(ds, full_screen);
5792 break;
5793#endif
bellard5b0753e2005-03-01 21:37:28 +00005794#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005795 case DT_SDL:
5796 sdl_display_init(ds, full_screen, no_frame);
5797 break;
bellard5b0753e2005-03-01 21:37:28 +00005798#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005799 case DT_SDL:
5800 cocoa_display_init(ds, full_screen);
5801 break;
bellard313aa562003-08-10 21:52:11 +00005802#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005803 case DT_VNC:
5804 vnc_display_init(ds);
5805 if (vnc_display_open(ds, vnc_display) < 0)
5806 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005807
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005808 if (show_vnc_port) {
5809 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005810 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005811 break;
5812 default:
5813 break;
bellard313aa562003-08-10 21:52:11 +00005814 }
aliguori7d957bd2009-01-15 22:14:11 +00005815 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005816
aliguori3023f332009-01-16 19:04:14 +00005817 dcl = ds->listeners;
5818 while (dcl != NULL) {
5819 if (dcl->dpy_refresh != NULL) {
5820 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5821 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005822 }
aliguori3023f332009-01-16 19:04:14 +00005823 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005824 }
aliguori3023f332009-01-16 19:04:14 +00005825
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005826 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005827 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5828 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5829 }
5830
aliguori2796dae2009-01-16 20:23:27 +00005831 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005832 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005833
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005834 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
5835 if (monitor_devices[i] && monitor_hds[i]) {
5836 monitor_init(monitor_hds[i],
5837 MONITOR_USE_READLINE |
5838 ((i == 0) ? MONITOR_IS_DEFAULT : 0));
5839 }
5840 }
bellard82c643f2004-07-14 17:28:13 +00005841
bellard8d11df92004-08-24 21:13:40 +00005842 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005843 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005844 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005845 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005846 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005847 }
bellard82c643f2004-07-14 17:28:13 +00005848 }
bellard82c643f2004-07-14 17:28:13 +00005849
bellard6508fe52005-01-15 12:02:56 +00005850 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005851 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005852 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00005853 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005854 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005855 }
5856 }
5857
aliguori9ede2fd2009-01-15 20:05:25 +00005858 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5859 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005860 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005861 if (strstart(devname, "vc", 0))
5862 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5863 }
5864 }
5865
aliguori59030a82009-04-05 18:43:41 +00005866 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5867 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5868 gdbstub_dev);
5869 exit(1);
balrog45669e02007-07-02 13:20:17 +00005870 }
balrog45669e02007-07-02 13:20:17 +00005871
Juan Quintela05f24012009-08-20 19:42:22 +02005872 if (loadvm) {
5873 if (load_vmstate(cur_mon, loadvm) < 0) {
5874 autostart = 0;
5875 }
5876 }
bellardd63d3072004-10-03 13:29:03 +00005877
Glauber Costa2bb8c102009-07-24 16:20:23 -04005878 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005879 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005880 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005881 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005882 }
thsffd843b2006-12-21 19:46:43 +00005883
blueswir1b9e82a52009-04-05 18:03:31 +00005884#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005885 if (daemonize) {
5886 uint8_t status = 0;
5887 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005888
5889 again1:
5890 len = write(fds[1], &status, 1);
5891 if (len == -1 && (errno == EINTR))
5892 goto again1;
5893
5894 if (len != 1)
5895 exit(1);
5896
aliguoribd54b862008-07-23 00:58:33 +00005897 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005898 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005899 if (fd == -1)
5900 exit(1);
aliguori08585322009-02-27 22:09:45 +00005901 }
ths71e3ceb2006-12-22 02:11:31 +00005902
aliguori08585322009-02-27 22:09:45 +00005903 if (run_as) {
5904 pwd = getpwnam(run_as);
5905 if (!pwd) {
5906 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5907 exit(1);
5908 }
5909 }
ths71e3ceb2006-12-22 02:11:31 +00005910
aliguori08585322009-02-27 22:09:45 +00005911 if (chroot_dir) {
5912 if (chroot(chroot_dir) < 0) {
5913 fprintf(stderr, "chroot failed\n");
5914 exit(1);
5915 }
5916 chdir("/");
5917 }
5918
5919 if (run_as) {
5920 if (setgid(pwd->pw_gid) < 0) {
5921 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5922 exit(1);
5923 }
5924 if (setuid(pwd->pw_uid) < 0) {
5925 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5926 exit(1);
5927 }
5928 if (setuid(0) != -1) {
5929 fprintf(stderr, "Dropping privileges failed\n");
5930 exit(1);
5931 }
5932 }
aliguori08585322009-02-27 22:09:45 +00005933
5934 if (daemonize) {
5935 dup2(fd, 0);
5936 dup2(fd, 1);
5937 dup2(fd, 2);
5938
5939 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005940 }
blueswir1b9e82a52009-04-05 18:03:31 +00005941#endif
ths71e3ceb2006-12-22 02:11:31 +00005942
bellard8a7ddc32004-03-31 19:00:16 +00005943 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005944 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005945 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005946
bellard0824d6f2003-06-24 13:42:40 +00005947 return 0;
5948}