blob: 2152f6ae53a7b707a9e11b4cb28a9e9a78d2c840 [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
blueswir1179a2c12009-03-08 08:23:32 +000032/* Needed early for HOST_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
Blue Swirl96555a92009-07-17 11:01:45 +000034/* Needed early to override system queue definitions on BSD */
35#include "sys-queue.h"
blueswir1d40cdb12009-03-07 16:52:02 +000036
bellard67b915a2004-03-31 23:37:16 +000037#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010038#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000039#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000042#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000043#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000046#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000047#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
49#if defined(__NetBSD__)
50#include <net/if_tap.h>
51#endif
52#ifdef __linux__
53#include <linux/if_tun.h>
54#endif
55#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000056#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000057#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000058#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000059#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000060#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000061#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000062#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000063#else
64#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000065#endif
ths5c40d2b2007-06-23 16:03:36 +000066#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000068#else
blueswir1223f0d72008-09-30 18:12:18 +000069#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000070#include <pty.h>
71#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000072#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020073#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000074
75/* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77/* #include <linux/hpet.h> */
78#include "hpet.h"
79
bellarde57a8c02005-11-10 23:58:52 +000080#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000081#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000082#endif
83#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000084#include <sys/stat.h>
85#include <sys/ethernet.h>
86#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000087#include <netinet/arp.h>
88#include <netinet/in.h>
89#include <netinet/in_systm.h>
90#include <netinet/ip.h>
91#include <netinet/ip_icmp.h> // must come after ip.h
92#include <netinet/udp.h>
93#include <netinet/tcp.h>
94#include <net/if.h>
95#include <syslog.h>
96#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
bellard7d3505c2004-05-12 19:32:15 +000098#endif
bellardec530c82006-04-25 22:36:06 +000099#endif
bellard67b915a2004-03-31 23:37:16 +0000100
blueswir19892fbf2008-08-24 10:34:20 +0000101#if defined(__OpenBSD__)
102#include <util.h>
103#endif
104
ths8a16d272008-07-19 09:56:24 +0000105#if defined(CONFIG_VDE)
106#include <libvdeplug.h>
107#endif
108
bellard67b915a2004-03-31 23:37:16 +0000109#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000110#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000111#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000112#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000113#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000114#define getopt_long_only getopt_long
115#define memalign(align, size) malloc(size)
116#endif
117
bellard73332e52004-04-04 20:22:28 +0000118#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200119#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200120#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000121int qemu_main(int argc, char **argv, char **envp);
122int main(int argc, char **argv)
123{
Stefan Weil59a36a22009-06-18 20:11:03 +0200124 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000125}
126#undef main
127#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000128#endif
bellard73332e52004-04-04 20:22:28 +0000129#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000130
bellard5b0753e2005-03-01 21:37:28 +0000131#ifdef CONFIG_COCOA
132#undef main
133#define main qemu_main
134#endif /* CONFIG_COCOA */
135
blueswir1511d2b12009-03-07 15:32:56 +0000136#include "hw/hw.h"
137#include "hw/boards.h"
138#include "hw/usb.h"
139#include "hw/pcmcia.h"
140#include "hw/pc.h"
141#include "hw/audiodev.h"
142#include "hw/isa.h"
143#include "hw/baum.h"
144#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100145#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000146#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000147#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200148#include "hw/qdev.h"
aurel325ef4efa2009-03-10 21:43:35 +0000149#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000150#include "net.h"
151#include "monitor.h"
152#include "console.h"
153#include "sysemu.h"
154#include "gdbstub.h"
155#include "qemu-timer.h"
156#include "qemu-char.h"
157#include "cache-utils.h"
158#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000159#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160#include "audio/audio.h"
161#include "migration.h"
162#include "kvm.h"
163#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200164#include "qemu-option.h"
blueswir1511d2b12009-03-07 15:32:56 +0000165
bellard0824d6f2003-06-24 13:42:40 +0000166#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000167
bellard8a7ddc32004-03-31 19:00:16 +0000168#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000169
blueswir1511d2b12009-03-07 15:32:56 +0000170#include "qemu_socket.h"
171
Jan Kiszkad918f232009-06-24 14:42:30 +0200172#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000173
blueswir19dc63a12008-10-04 07:25:46 +0000174//#define DEBUG_NET
175//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000176
bellard1bfe8562004-07-08 21:17:50 +0000177#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000178
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 */
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200183struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives);
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +0200184struct driveoptlist driveopts = TAILQ_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;
bellard313aa562003-08-10 21:52:11 +0000189int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000190ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000191int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000192NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000193int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000194static int autostart;
balrogf6503052008-02-17 11:42:19 +0000195static int rtc_utc = 1;
196static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000197int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000198int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000199int vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +0000200int xenfb_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000201#ifdef TARGET_SPARC
202int graphic_width = 1024;
203int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000204int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000205#else
bellard1bfe8562004-07-08 21:17:50 +0000206int graphic_width = 800;
207int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000208int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000209#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000210static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000211#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000212static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000213#endif
ths667acca2006-12-11 02:08:05 +0000214int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000215CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000216CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000217CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000218#ifdef TARGET_I386
219int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000220int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000221#endif
bellardbb36d472005-11-05 14:22:28 +0000222int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000223int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000224int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200225int max_cpus = 0;
ths73fc9742006-12-22 02:09:07 +0000226const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000227int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000228int no_hpet = 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +0200229int virtio_balloon = 1;
230const char *virtio_balloon_devaddr;
bellard52ca8d62006-06-14 16:03:05 +0000231int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000232int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000233int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000234int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000235int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000236#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000237int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000238#endif
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100239WatchdogTimerModel *watchdog = NULL;
240int watchdog_action = WDT_RESET;
ths9ae02552007-01-05 17:39:04 +0000241const char *option_rom[MAX_OPTION_ROMS];
242int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000243int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000244#ifdef TARGET_ARM
245int old_param = 0;
246#endif
thsc35734b2007-03-19 15:17:08 +0000247const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000248int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000249#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000250unsigned int nb_prom_envs = 0;
251const char *prom_envs[MAX_PROM_ENVS];
252#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200253int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000254
aliguori268a3622009-04-21 22:30:27 +0000255int nb_numa_nodes;
256uint64_t node_mem[MAX_NODES];
257uint64_t node_cpumask[MAX_NODES];
258
balrogee5605e2007-12-03 03:01:40 +0000259static CPUState *cur_cpu;
260static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000261static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000262/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000263static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000264/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000265#define MAX_ICOUNT_SHIFT 10
266/* Compensate for varying guest execution speed. */
267static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000268static QEMUTimer *icount_rt_timer;
269static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000270static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000271
blueswir18fcb1b92008-09-18 18:29:08 +0000272uint8_t qemu_uuid[16];
273
Jan Kiszka76e30d02009-07-02 00:19:02 +0200274static QEMUBootSetHandler *boot_set_handler;
275static void *boot_set_opaque;
276
bellard0824d6f2003-06-24 13:42:40 +0000277/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000278/* x86 ISA bus support */
279
280target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000281PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000282
bellard0824d6f2003-06-24 13:42:40 +0000283/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000284void hw_error(const char *fmt, ...)
285{
286 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000287 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000288
289 va_start(ap, fmt);
290 fprintf(stderr, "qemu: hardware error: ");
291 vfprintf(stderr, fmt, ap);
292 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000293 for(env = first_cpu; env != NULL; env = env->next_cpu) {
294 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000295#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000296 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000297#else
bellard6a00d602005-11-21 23:25:50 +0000298 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000299#endif
bellard6a00d602005-11-21 23:25:50 +0000300 }
bellard0824d6f2003-06-24 13:42:40 +0000301 va_end(ap);
302 abort();
303}
Andi Kleen18894652009-07-02 09:34:17 +0200304
305static void set_proc_name(const char *s)
306{
307#ifdef __linux__
308 char name[16];
309 if (!s)
310 return;
311 name[sizeof(name) - 1] = 0;
312 strncpy(name, s, sizeof(name));
313 /* Could rewrite argv[0] too, but that's a bit more complicated.
314 This simple way is enough for `top'. */
315 prctl(PR_SET_NAME, name);
316#endif
317}
aliguoridf751fa2008-12-04 20:19:35 +0000318
319/***************/
320/* ballooning */
321
322static QEMUBalloonEvent *qemu_balloon_event;
323void *qemu_balloon_event_opaque;
324
325void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
326{
327 qemu_balloon_event = func;
328 qemu_balloon_event_opaque = opaque;
329}
330
331void qemu_balloon(ram_addr_t target)
332{
333 if (qemu_balloon_event)
334 qemu_balloon_event(qemu_balloon_event_opaque, target);
335}
336
337ram_addr_t qemu_balloon_status(void)
338{
339 if (qemu_balloon_event)
340 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
341 return 0;
342}
bellard0824d6f2003-06-24 13:42:40 +0000343
bellard8a7ddc32004-03-31 19:00:16 +0000344/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000345/* keyboard/mouse */
346
347static QEMUPutKBDEvent *qemu_put_kbd_event;
348static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000349static QEMUPutMouseEntry *qemu_put_mouse_event_head;
350static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000351
352void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
353{
354 qemu_put_kbd_event_opaque = opaque;
355 qemu_put_kbd_event = func;
356}
357
ths455204e2007-01-05 16:42:13 +0000358QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
359 void *opaque, int absolute,
360 const char *name)
bellard63066f42004-06-03 18:45:02 +0000361{
ths455204e2007-01-05 16:42:13 +0000362 QEMUPutMouseEntry *s, *cursor;
363
364 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000365
366 s->qemu_put_mouse_event = func;
367 s->qemu_put_mouse_event_opaque = opaque;
368 s->qemu_put_mouse_event_absolute = absolute;
369 s->qemu_put_mouse_event_name = qemu_strdup(name);
370 s->next = NULL;
371
372 if (!qemu_put_mouse_event_head) {
373 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
374 return s;
375 }
376
377 cursor = qemu_put_mouse_event_head;
378 while (cursor->next != NULL)
379 cursor = cursor->next;
380
381 cursor->next = s;
382 qemu_put_mouse_event_current = s;
383
384 return s;
385}
386
387void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
388{
389 QEMUPutMouseEntry *prev = NULL, *cursor;
390
391 if (!qemu_put_mouse_event_head || entry == NULL)
392 return;
393
394 cursor = qemu_put_mouse_event_head;
395 while (cursor != NULL && cursor != entry) {
396 prev = cursor;
397 cursor = cursor->next;
398 }
399
400 if (cursor == NULL) // does not exist or list empty
401 return;
402 else if (prev == NULL) { // entry is head
403 qemu_put_mouse_event_head = cursor->next;
404 if (qemu_put_mouse_event_current == entry)
405 qemu_put_mouse_event_current = cursor->next;
406 qemu_free(entry->qemu_put_mouse_event_name);
407 qemu_free(entry);
408 return;
409 }
410
411 prev->next = entry->next;
412
413 if (qemu_put_mouse_event_current == entry)
414 qemu_put_mouse_event_current = prev;
415
416 qemu_free(entry->qemu_put_mouse_event_name);
417 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000418}
419
420void kbd_put_keycode(int keycode)
421{
422 if (qemu_put_kbd_event) {
423 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
424 }
425}
426
427void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
428{
ths455204e2007-01-05 16:42:13 +0000429 QEMUPutMouseEvent *mouse_event;
430 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000431 int width;
ths455204e2007-01-05 16:42:13 +0000432
433 if (!qemu_put_mouse_event_current) {
434 return;
435 }
436
437 mouse_event =
438 qemu_put_mouse_event_current->qemu_put_mouse_event;
439 mouse_event_opaque =
440 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
441
442 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000443 if (graphic_rotate) {
444 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
445 width = 0x7fff;
446 else
aurel32b94ed572008-03-10 19:34:27 +0000447 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000448 mouse_event(mouse_event_opaque,
449 width - dy, dx, dz, buttons_state);
450 } else
451 mouse_event(mouse_event_opaque,
452 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000453 }
454}
455
bellard09b26c52006-04-12 21:09:08 +0000456int kbd_mouse_is_absolute(void)
457{
ths455204e2007-01-05 16:42:13 +0000458 if (!qemu_put_mouse_event_current)
459 return 0;
460
461 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
462}
463
aliguori376253e2009-03-05 23:01:23 +0000464void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000465{
466 QEMUPutMouseEntry *cursor;
467 int index = 0;
468
469 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000471 return;
472 }
473
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000475 cursor = qemu_put_mouse_event_head;
476 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_printf(mon, "%c Mouse #%d: %s\n",
478 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
479 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000480 index++;
481 cursor = cursor->next;
482 }
483}
484
aliguori376253e2009-03-05 23:01:23 +0000485void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000486{
487 QEMUPutMouseEntry *cursor;
488 int i = 0;
489
490 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000491 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000492 return;
493 }
494
495 cursor = qemu_put_mouse_event_head;
496 while (cursor != NULL && index != i) {
497 i++;
498 cursor = cursor->next;
499 }
500
501 if (cursor != NULL)
502 qemu_put_mouse_event_current = cursor;
503 else
aliguori376253e2009-03-05 23:01:23 +0000504 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000505}
506
bellard87858c82003-06-27 12:01:39 +0000507/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000508uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000509{
510 union {
511 uint64_t ll;
512 struct {
513#ifdef WORDS_BIGENDIAN
514 uint32_t high, low;
515#else
516 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000517#endif
bellard87858c82003-06-27 12:01:39 +0000518 } l;
519 } u, res;
520 uint64_t rl, rh;
521
522 u.ll = a;
523 rl = (uint64_t)u.l.low * (uint64_t)b;
524 rh = (uint64_t)u.l.high * (uint64_t)b;
525 rh += (rl >> 32);
526 res.l.high = rh / c;
527 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
528 return res.ll;
529}
530
bellard1dce7c32006-07-13 23:20:22 +0000531/***********************************************************/
532/* real time host monotonic timer */
533
534#define QEMU_TIMER_BASE 1000000000LL
535
536#ifdef WIN32
537
538static int64_t clock_freq;
539
540static void init_get_clock(void)
541{
bellarda8e5ac32006-07-14 09:36:13 +0000542 LARGE_INTEGER freq;
543 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000544 ret = QueryPerformanceFrequency(&freq);
545 if (ret == 0) {
546 fprintf(stderr, "Could not calibrate ticks\n");
547 exit(1);
548 }
549 clock_freq = freq.QuadPart;
550}
551
552static int64_t get_clock(void)
553{
554 LARGE_INTEGER ti;
555 QueryPerformanceCounter(&ti);
556 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
557}
558
559#else
560
561static int use_rt_clock;
562
563static void init_get_clock(void)
564{
565 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000566#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
567 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000568 {
569 struct timespec ts;
570 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
571 use_rt_clock = 1;
572 }
573 }
574#endif
575}
576
577static int64_t get_clock(void)
578{
blueswir1c5e97232009-03-07 20:06:23 +0000579#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
580 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000581 if (use_rt_clock) {
582 struct timespec ts;
583 clock_gettime(CLOCK_MONOTONIC, &ts);
584 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000585 } else
bellard1dce7c32006-07-13 23:20:22 +0000586#endif
587 {
588 /* XXX: using gettimeofday leads to problems if the date
589 changes, so it should be avoided. */
590 struct timeval tv;
591 gettimeofday(&tv, NULL);
592 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
593 }
594}
bellard1dce7c32006-07-13 23:20:22 +0000595#endif
596
pbrook2e70f6e2008-06-29 01:03:05 +0000597/* Return the virtual CPU time, based on the instruction counter. */
598static int64_t cpu_get_icount(void)
599{
600 int64_t icount;
601 CPUState *env = cpu_single_env;;
602 icount = qemu_icount;
603 if (env) {
604 if (!can_do_io(env))
605 fprintf(stderr, "Bad clock read\n");
606 icount -= (env->icount_decr.u16.low + env->icount_extra);
607 }
608 return qemu_icount_bias + (icount << icount_time_shift);
609}
610
bellard1dce7c32006-07-13 23:20:22 +0000611/***********************************************************/
612/* guest cycle counter */
613
614static int64_t cpu_ticks_prev;
615static int64_t cpu_ticks_offset;
616static int64_t cpu_clock_offset;
617static int cpu_ticks_enabled;
618
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 }
bellard1dce7c32006-07-13 23:20:22 +0000625 if (!cpu_ticks_enabled) {
626 return cpu_ticks_offset;
627 } else {
628 int64_t ticks;
629 ticks = cpu_get_real_ticks();
630 if (cpu_ticks_prev > ticks) {
631 /* Note: non increasing ticks may happen if the host uses
632 software suspend */
633 cpu_ticks_offset += cpu_ticks_prev - ticks;
634 }
635 cpu_ticks_prev = ticks;
636 return ticks + cpu_ticks_offset;
637 }
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;
644 if (!cpu_ticks_enabled) {
645 return cpu_clock_offset;
646 } else {
647 ti = get_clock();
648 return ti + cpu_clock_offset;
649 }
650}
651
652/* enable cpu_get_ticks() */
653void cpu_enable_ticks(void)
654{
655 if (!cpu_ticks_enabled) {
656 cpu_ticks_offset -= cpu_get_real_ticks();
657 cpu_clock_offset -= get_clock();
658 cpu_ticks_enabled = 1;
659 }
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{
666 if (cpu_ticks_enabled) {
667 cpu_ticks_offset = cpu_get_ticks();
668 cpu_clock_offset = cpu_get_clock();
669 cpu_ticks_enabled = 0;
670 }
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. */
759#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
760
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,
801 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
802 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,
817 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
818}
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
863 arg = strdup(opt);
864
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
892 free(arg);
893
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
998static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
999{
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();
1040 ticks_per_sec = QEMU_TIMER_BASE;
1041 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1042 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1043}
1044
bellard8a7ddc32004-03-31 19:00:16 +00001045/* save a timer */
1046void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1047{
1048 uint64_t expire_time;
1049
1050 if (qemu_timer_pending(ts)) {
1051 expire_time = ts->expire_time;
1052 } else {
1053 expire_time = -1;
1054 }
1055 qemu_put_be64(f, expire_time);
1056}
1057
1058void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1059{
1060 uint64_t expire_time;
1061
1062 expire_time = qemu_get_be64(f);
1063 if (expire_time != -1) {
1064 qemu_mod_timer(ts, expire_time);
1065 } else {
1066 qemu_del_timer(ts);
1067 }
1068}
1069
1070static void timer_save(QEMUFile *f, void *opaque)
1071{
1072 if (cpu_ticks_enabled) {
1073 hw_error("cannot save state if virtual timers are running");
1074 }
thsbee8d682007-12-16 23:41:11 +00001075 qemu_put_be64(f, cpu_ticks_offset);
1076 qemu_put_be64(f, ticks_per_sec);
1077 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001078}
1079
1080static int timer_load(QEMUFile *f, void *opaque, int version_id)
1081{
bellardc88676f2006-08-06 13:36:11 +00001082 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001083 return -EINVAL;
1084 if (cpu_ticks_enabled) {
1085 return -EINVAL;
1086 }
thsbee8d682007-12-16 23:41:11 +00001087 cpu_ticks_offset=qemu_get_be64(f);
1088 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001089 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001090 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001091 }
bellard8a7ddc32004-03-31 19:00:16 +00001092 return 0;
1093}
1094
aliguori50317c72009-04-24 18:03:29 +00001095static void qemu_event_increment(void);
1096
bellard67b915a2004-03-31 23:37:16 +00001097#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001098static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1099 DWORD_PTR dwUser, DWORD_PTR dw1,
1100 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001101#else
bellard8a7ddc32004-03-31 19:00:16 +00001102static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001103#endif
bellard8a7ddc32004-03-31 19:00:16 +00001104{
bellard02ba45c2004-06-25 14:46:23 +00001105#if 0
1106#define DISP_FREQ 1000
1107 {
1108 static int64_t delta_min = INT64_MAX;
1109 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1110 static int count;
1111 ti = qemu_get_clock(vm_clock);
1112 if (last_clock != 0) {
1113 delta = ti - last_clock;
1114 if (delta < delta_min)
1115 delta_min = delta;
1116 if (delta > delta_max)
1117 delta_max = delta;
1118 delta_cum += delta;
1119 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001120 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001121 muldiv64(delta_min, 1000000, ticks_per_sec),
1122 muldiv64(delta_max, 1000000, ticks_per_sec),
1123 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1124 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1125 count = 0;
1126 delta_min = INT64_MAX;
1127 delta_max = 0;
1128 delta_cum = 0;
1129 }
1130 }
1131 last_clock = ti;
1132 }
1133#endif
thsefe75412007-08-24 01:36:32 +00001134 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001135 (!use_icount &&
1136 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1137 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001138 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1139 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001140 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001141 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001142
aliguorid6dc3d42009-04-24 18:04:07 +00001143#ifndef CONFIG_IOTHREAD
1144 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001145 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001146 cpu_exit(next_cpu);
blueswir1640f42e2009-04-19 10:18:01 +00001147#ifdef CONFIG_KQEMU
aliguorid6dc3d42009-04-24 18:04:07 +00001148 if (next_cpu->kqemu_enabled) {
1149 kqemu_cpu_interrupt(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001150 }
balrogee5605e2007-12-03 03:01:40 +00001151#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001152 }
aliguorid6dc3d42009-04-24 18:04:07 +00001153#endif
aliguori43b96852009-04-24 18:03:33 +00001154 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001155 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001156 }
1157}
1158
pbrook2e70f6e2008-06-29 01:03:05 +00001159static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001160{
pbrook2e70f6e2008-06-29 01:03:05 +00001161 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001162
1163 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001164 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1165 qemu_get_clock(vm_clock);
1166 } else {
1167 /* To avoid problems with overflow limit this to 2^32. */
1168 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001169 }
1170
pbrook2e70f6e2008-06-29 01:03:05 +00001171 if (delta < 0)
1172 delta = 0;
thsefe75412007-08-24 01:36:32 +00001173
pbrook2e70f6e2008-06-29 01:03:05 +00001174 return delta;
1175}
1176
blueswir18632fb92008-09-14 13:59:34 +00001177#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001178static uint64_t qemu_next_deadline_dyntick(void)
1179{
1180 int64_t delta;
1181 int64_t rtdelta;
1182
1183 if (use_icount)
1184 delta = INT32_MAX;
1185 else
1186 delta = (qemu_next_deadline() + 999) / 1000;
1187
1188 if (active_timers[QEMU_TIMER_REALTIME]) {
1189 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1190 qemu_get_clock(rt_clock))*1000;
1191 if (rtdelta < delta)
1192 delta = rtdelta;
1193 }
1194
1195 if (delta < MIN_TIMER_REARM_US)
1196 delta = MIN_TIMER_REARM_US;
1197
1198 return delta;
thsefe75412007-08-24 01:36:32 +00001199}
blueswir18632fb92008-09-14 13:59:34 +00001200#endif
thsefe75412007-08-24 01:36:32 +00001201
bellardfd872592004-05-12 19:11:15 +00001202#ifndef _WIN32
1203
aliguori7183b4b2008-11-05 20:40:18 +00001204/* Sets a specific flag */
1205static int fcntl_setfl(int fd, int flag)
1206{
1207 int flags;
1208
1209 flags = fcntl(fd, F_GETFL);
1210 if (flags == -1)
1211 return -errno;
1212
1213 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1214 return -errno;
1215
1216 return 0;
1217}
1218
bellard829309c2004-05-20 13:20:12 +00001219#if defined(__linux__)
1220
bellardfd872592004-05-12 19:11:15 +00001221#define RTC_FREQ 1024
1222
aurel32de9a95f2008-11-11 13:41:01 +00001223static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001224{
thsc8994012007-08-19 21:56:03 +00001225 struct sigaction act;
1226
1227 /* timer signal */
1228 sigfillset(&act.sa_mask);
1229 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001230 act.sa_handler = host_alarm_handler;
1231
1232 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001233 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001234 fcntl(fd, F_SETOWN, getpid());
1235}
1236
thsc40ec5a2007-08-19 22:09:40 +00001237static int hpet_start_timer(struct qemu_alarm_timer *t)
1238{
1239 struct hpet_info info;
1240 int r, fd;
1241
1242 fd = open("/dev/hpet", O_RDONLY);
1243 if (fd < 0)
1244 return -1;
1245
1246 /* Set frequency */
1247 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1248 if (r < 0) {
1249 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1250 "error, but for better emulation accuracy type:\n"
1251 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1252 goto fail;
1253 }
1254
1255 /* Check capabilities */
1256 r = ioctl(fd, HPET_INFO, &info);
1257 if (r < 0)
1258 goto fail;
1259
1260 /* Enable periodic mode */
1261 r = ioctl(fd, HPET_EPI, 0);
1262 if (info.hi_flags && (r < 0))
1263 goto fail;
1264
1265 /* Enable interrupt */
1266 r = ioctl(fd, HPET_IE_ON, 0);
1267 if (r < 0)
1268 goto fail;
1269
1270 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001271 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001272
1273 return 0;
1274fail:
1275 close(fd);
1276 return -1;
1277}
1278
1279static void hpet_stop_timer(struct qemu_alarm_timer *t)
1280{
pbrookfcdc2122007-08-23 20:22:22 +00001281 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001282
1283 close(fd);
1284}
1285
thsc8994012007-08-19 21:56:03 +00001286static int rtc_start_timer(struct qemu_alarm_timer *t)
1287{
1288 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001289 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001290
balrogaeb30be2007-07-02 15:03:13 +00001291 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001292 if (rtc_fd < 0)
1293 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001294 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1295 if (current_rtc_freq != RTC_FREQ &&
1296 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001297 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1298 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1299 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1300 goto fail;
1301 }
1302 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1303 fail:
1304 close(rtc_fd);
1305 return -1;
1306 }
thsc8994012007-08-19 21:56:03 +00001307
1308 enable_sigio_timer(rtc_fd);
1309
pbrookfcdc2122007-08-23 20:22:22 +00001310 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001311
bellardfd872592004-05-12 19:11:15 +00001312 return 0;
1313}
1314
thsc8994012007-08-19 21:56:03 +00001315static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001316{
pbrookfcdc2122007-08-23 20:22:22 +00001317 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001318
1319 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001320}
1321
thsefe75412007-08-24 01:36:32 +00001322static int dynticks_start_timer(struct qemu_alarm_timer *t)
1323{
1324 struct sigevent ev;
1325 timer_t host_timer;
1326 struct sigaction act;
1327
1328 sigfillset(&act.sa_mask);
1329 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001330 act.sa_handler = host_alarm_handler;
1331
1332 sigaction(SIGALRM, &act, NULL);
1333
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001334 /*
1335 * Initialize ev struct to 0 to avoid valgrind complaining
1336 * about uninitialized data in timer_create call
1337 */
1338 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001339 ev.sigev_value.sival_int = 0;
1340 ev.sigev_notify = SIGEV_SIGNAL;
1341 ev.sigev_signo = SIGALRM;
1342
1343 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1344 perror("timer_create");
1345
1346 /* disable dynticks */
1347 fprintf(stderr, "Dynamic Ticks disabled\n");
1348
1349 return -1;
1350 }
1351
blueswir10399bfe2008-11-16 11:37:18 +00001352 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001353
1354 return 0;
1355}
1356
1357static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1358{
blueswir10399bfe2008-11-16 11:37:18 +00001359 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001360
1361 timer_delete(host_timer);
1362}
1363
1364static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1365{
blueswir10399bfe2008-11-16 11:37:18 +00001366 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001367 struct itimerspec timeout;
1368 int64_t nearest_delta_us = INT64_MAX;
1369 int64_t current_us;
1370
1371 if (!active_timers[QEMU_TIMER_REALTIME] &&
1372 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001373 return;
thsefe75412007-08-24 01:36:32 +00001374
pbrook2e70f6e2008-06-29 01:03:05 +00001375 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001376
1377 /* check whether a timer is already running */
1378 if (timer_gettime(host_timer, &timeout)) {
1379 perror("gettime");
1380 fprintf(stderr, "Internal timer error: aborting\n");
1381 exit(1);
1382 }
1383 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1384 if (current_us && current_us <= nearest_delta_us)
1385 return;
1386
1387 timeout.it_interval.tv_sec = 0;
1388 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1389 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1390 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1391 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1392 perror("settime");
1393 fprintf(stderr, "Internal timer error: aborting\n");
1394 exit(1);
1395 }
1396}
1397
ths70744b32007-08-26 17:31:30 +00001398#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001399
thsc8994012007-08-19 21:56:03 +00001400static int unix_start_timer(struct qemu_alarm_timer *t)
1401{
1402 struct sigaction act;
1403 struct itimerval itv;
1404 int err;
1405
1406 /* timer signal */
1407 sigfillset(&act.sa_mask);
1408 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001409 act.sa_handler = host_alarm_handler;
1410
1411 sigaction(SIGALRM, &act, NULL);
1412
1413 itv.it_interval.tv_sec = 0;
1414 /* for i386 kernel 2.6 to get 1 ms */
1415 itv.it_interval.tv_usec = 999;
1416 itv.it_value.tv_sec = 0;
1417 itv.it_value.tv_usec = 10 * 1000;
1418
1419 err = setitimer(ITIMER_REAL, &itv, NULL);
1420 if (err)
1421 return -1;
1422
1423 return 0;
1424}
1425
1426static void unix_stop_timer(struct qemu_alarm_timer *t)
1427{
1428 struct itimerval itv;
1429
1430 memset(&itv, 0, sizeof(itv));
1431 setitimer(ITIMER_REAL, &itv, NULL);
1432}
1433
bellard829309c2004-05-20 13:20:12 +00001434#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001435
aliguorif49e58d2008-11-05 21:22:34 +00001436
thsc8994012007-08-19 21:56:03 +00001437#ifdef _WIN32
1438
1439static int win32_start_timer(struct qemu_alarm_timer *t)
1440{
1441 TIMECAPS tc;
1442 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001443 UINT flags;
thsc8994012007-08-19 21:56:03 +00001444
thsc8994012007-08-19 21:56:03 +00001445 memset(&tc, 0, sizeof(tc));
1446 timeGetDevCaps(&tc, sizeof(tc));
1447
1448 if (data->period < tc.wPeriodMin)
1449 data->period = tc.wPeriodMin;
1450
1451 timeBeginPeriod(data->period);
1452
thsefe75412007-08-24 01:36:32 +00001453 flags = TIME_CALLBACK_FUNCTION;
1454 if (alarm_has_dynticks(t))
1455 flags |= TIME_ONESHOT;
1456 else
1457 flags |= TIME_PERIODIC;
1458
thsc8994012007-08-19 21:56:03 +00001459 data->timerId = timeSetEvent(1, // interval (ms)
1460 data->period, // resolution
1461 host_alarm_handler, // function
1462 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001463 flags);
thsc8994012007-08-19 21:56:03 +00001464
1465 if (!data->timerId) {
1466 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001467 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001468 return -1;
1469 }
1470
thsc8994012007-08-19 21:56:03 +00001471 return 0;
1472}
1473
1474static void win32_stop_timer(struct qemu_alarm_timer *t)
1475{
1476 struct qemu_alarm_win32 *data = t->priv;
1477
1478 timeKillEvent(data->timerId);
1479 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001480}
1481
thsefe75412007-08-24 01:36:32 +00001482static void win32_rearm_timer(struct qemu_alarm_timer *t)
1483{
1484 struct qemu_alarm_win32 *data = t->priv;
1485 uint64_t nearest_delta_us;
1486
1487 if (!active_timers[QEMU_TIMER_REALTIME] &&
1488 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001489 return;
thsefe75412007-08-24 01:36:32 +00001490
pbrook2e70f6e2008-06-29 01:03:05 +00001491 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001492 nearest_delta_us /= 1000;
1493
1494 timeKillEvent(data->timerId);
1495
1496 data->timerId = timeSetEvent(1,
1497 data->period,
1498 host_alarm_handler,
1499 (DWORD)t,
1500 TIME_ONESHOT | TIME_PERIODIC);
1501
1502 if (!data->timerId) {
1503 perror("Failed to re-arm win32 alarm timer");
1504
1505 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001506 exit(1);
1507 }
1508}
1509
thsc8994012007-08-19 21:56:03 +00001510#endif /* _WIN32 */
1511
aliguori7183b4b2008-11-05 20:40:18 +00001512static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001513{
blueswir1223f0d72008-09-30 18:12:18 +00001514 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001515 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001516
thsc8994012007-08-19 21:56:03 +00001517 for (i = 0; alarm_timers[i].name; i++) {
1518 t = &alarm_timers[i];
1519
thsc8994012007-08-19 21:56:03 +00001520 err = t->start(t);
1521 if (!err)
1522 break;
bellard67b915a2004-03-31 23:37:16 +00001523 }
bellardfd872592004-05-12 19:11:15 +00001524
thsc8994012007-08-19 21:56:03 +00001525 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001526 err = -ENOENT;
1527 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001528 }
thsc8994012007-08-19 21:56:03 +00001529
1530 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001531
aliguori6abfbd72008-11-05 20:49:37 +00001532 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001533
1534fail:
aliguori7183b4b2008-11-05 20:40:18 +00001535 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001536}
1537
pbrook9596ebb2007-11-18 01:44:38 +00001538static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001539{
thsc8994012007-08-19 21:56:03 +00001540 alarm_timer->stop(alarm_timer);
1541 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001542}
1543
bellardc4b1fcc2004-03-14 21:44:30 +00001544/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001545/* host time/date access */
1546void qemu_get_timedate(struct tm *tm, int offset)
1547{
1548 time_t ti;
1549 struct tm *ret;
1550
1551 time(&ti);
1552 ti += offset;
1553 if (rtc_date_offset == -1) {
1554 if (rtc_utc)
1555 ret = gmtime(&ti);
1556 else
1557 ret = localtime(&ti);
1558 } else {
1559 ti -= rtc_date_offset;
1560 ret = gmtime(&ti);
1561 }
1562
1563 memcpy(tm, ret, sizeof(struct tm));
1564}
1565
1566int qemu_timedate_diff(struct tm *tm)
1567{
1568 time_t seconds;
1569
1570 if (rtc_date_offset == -1)
1571 if (rtc_utc)
1572 seconds = mktimegm(tm);
1573 else
1574 seconds = mktime(tm);
1575 else
1576 seconds = mktimegm(tm) + rtc_date_offset;
1577
1578 return seconds - time(NULL);
1579}
1580
bellardfd1dff42006-02-01 21:29:26 +00001581#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001582static void socket_cleanup(void)
1583{
1584 WSACleanup();
1585}
bellard82c643f2004-07-14 17:28:13 +00001586
bellardfd1dff42006-02-01 21:29:26 +00001587static int socket_init(void)
1588{
1589 WSADATA Data;
1590 int ret, err;
1591
1592 ret = WSAStartup(MAKEWORD(2,2), &Data);
1593 if (ret != 0) {
1594 err = WSAGetLastError();
1595 fprintf(stderr, "WSAStartup: %d\n", err);
1596 return -1;
1597 }
1598 atexit(socket_cleanup);
1599 return 0;
1600}
aurel3264b7b732008-05-05 10:05:31 +00001601#endif
1602
balrog1ae26a12008-09-28 23:19:47 +00001603/***********************************************************/
1604/* Bluetooth support */
1605static int nb_hcis;
1606static int cur_hci;
1607static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001608
balrog1ae26a12008-09-28 23:19:47 +00001609static struct bt_vlan_s {
1610 struct bt_scatternet_s net;
1611 int id;
1612 struct bt_vlan_s *next;
1613} *first_bt_vlan;
1614
1615/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001616static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001617{
1618 struct bt_vlan_s **pvlan, *vlan;
1619 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1620 if (vlan->id == id)
1621 return &vlan->net;
1622 }
1623 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1624 vlan->id = id;
1625 pvlan = &first_bt_vlan;
1626 while (*pvlan != NULL)
1627 pvlan = &(*pvlan)->next;
1628 *pvlan = vlan;
1629 return &vlan->net;
1630}
1631
1632static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1633{
1634}
1635
1636static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1637{
1638 return -ENOTSUP;
1639}
1640
1641static struct HCIInfo null_hci = {
1642 .cmd_send = null_hci_send,
1643 .sco_send = null_hci_send,
1644 .acl_send = null_hci_send,
1645 .bdaddr_set = null_hci_addr_set,
1646};
1647
1648struct HCIInfo *qemu_next_hci(void)
1649{
1650 if (cur_hci == nb_hcis)
1651 return &null_hci;
1652
1653 return hci_table[cur_hci++];
1654}
1655
balrogdc72ac12008-11-09 00:04:26 +00001656static struct HCIInfo *hci_init(const char *str)
1657{
1658 char *endp;
1659 struct bt_scatternet_s *vlan = 0;
1660
1661 if (!strcmp(str, "null"))
1662 /* null */
1663 return &null_hci;
1664 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1665 /* host[:hciN] */
1666 return bt_host_hci(str[4] ? str + 5 : "hci0");
1667 else if (!strncmp(str, "hci", 3)) {
1668 /* hci[,vlan=n] */
1669 if (str[3]) {
1670 if (!strncmp(str + 3, ",vlan=", 6)) {
1671 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1672 if (*endp)
1673 vlan = 0;
1674 }
1675 } else
1676 vlan = qemu_find_bt_vlan(0);
1677 if (vlan)
1678 return bt_new_hci(vlan);
1679 }
1680
1681 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1682
1683 return 0;
1684}
1685
1686static int bt_hci_parse(const char *str)
1687{
1688 struct HCIInfo *hci;
1689 bdaddr_t bdaddr;
1690
1691 if (nb_hcis >= MAX_NICS) {
1692 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1693 return -1;
1694 }
1695
1696 hci = hci_init(str);
1697 if (!hci)
1698 return -1;
1699
1700 bdaddr.b[0] = 0x52;
1701 bdaddr.b[1] = 0x54;
1702 bdaddr.b[2] = 0x00;
1703 bdaddr.b[3] = 0x12;
1704 bdaddr.b[4] = 0x34;
1705 bdaddr.b[5] = 0x56 + nb_hcis;
1706 hci->bdaddr_set(hci, bdaddr.b);
1707
1708 hci_table[nb_hcis++] = hci;
1709
1710 return 0;
1711}
1712
1713static void bt_vhci_add(int vlan_id)
1714{
1715 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1716
1717 if (!vlan->slave)
1718 fprintf(stderr, "qemu: warning: adding a VHCI to "
1719 "an empty scatternet %i\n", vlan_id);
1720
1721 bt_vhci_init(bt_new_hci(vlan));
1722}
1723
1724static struct bt_device_s *bt_device_add(const char *opt)
1725{
1726 struct bt_scatternet_s *vlan;
1727 int vlan_id = 0;
1728 char *endp = strstr(opt, ",vlan=");
1729 int len = (endp ? endp - opt : strlen(opt)) + 1;
1730 char devname[10];
1731
1732 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1733
1734 if (endp) {
1735 vlan_id = strtol(endp + 6, &endp, 0);
1736 if (*endp) {
1737 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1738 return 0;
1739 }
1740 }
1741
1742 vlan = qemu_find_bt_vlan(vlan_id);
1743
1744 if (!vlan->slave)
1745 fprintf(stderr, "qemu: warning: adding a slave device to "
1746 "an empty scatternet %i\n", vlan_id);
1747
1748 if (!strcmp(devname, "keyboard"))
1749 return bt_keyboard_init(vlan);
1750
1751 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1752 return 0;
1753}
1754
1755static int bt_parse(const char *opt)
1756{
1757 const char *endp, *p;
1758 int vlan;
1759
1760 if (strstart(opt, "hci", &endp)) {
1761 if (!*endp || *endp == ',') {
1762 if (*endp)
1763 if (!strstart(endp, ",vlan=", 0))
1764 opt = endp + 1;
1765
1766 return bt_hci_parse(opt);
1767 }
1768 } else if (strstart(opt, "vhci", &endp)) {
1769 if (!*endp || *endp == ',') {
1770 if (*endp) {
1771 if (strstart(endp, ",vlan=", &p)) {
1772 vlan = strtol(p, (char **) &endp, 0);
1773 if (*endp) {
1774 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1775 return 1;
1776 }
1777 } else {
1778 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1779 return 1;
1780 }
1781 } else
1782 vlan = 0;
1783
1784 bt_vhci_add(vlan);
1785 return 0;
1786 }
1787 } else if (strstart(opt, "device:", &endp))
1788 return !bt_device_add(endp);
1789
1790 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1791 return 1;
1792}
1793
balrog1ae26a12008-09-28 23:19:47 +00001794/***********************************************************/
1795/* QEMU Block devices */
1796
balrog609497a2008-01-14 02:56:53 +00001797#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001798#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001799#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001800#define PFLASH_ALIAS "if=pflash"
1801#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001802#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001803
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001804static QemuOptsList drive_opt_list = {
1805 .name = "drive",
1806 .head = TAILQ_HEAD_INITIALIZER(drive_opt_list.head),
1807 .desc = {
1808 {
1809 .name = "bus",
1810 .type = QEMU_OPT_NUMBER,
1811 .help = "bus number",
1812 },{
1813 .name = "unit",
1814 .type = QEMU_OPT_NUMBER,
1815 .help = "unit number (i.e. lun for scsi)",
1816 },{
1817 .name = "if",
1818 .type = QEMU_OPT_STRING,
1819 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1820 },{
1821 .name = "index",
1822 .type = QEMU_OPT_NUMBER,
1823 },{
1824 .name = "cyls",
1825 .type = QEMU_OPT_NUMBER,
1826 .help = "number of cylinders (ide disk geometry)",
1827 },{
1828 .name = "heads",
1829 .type = QEMU_OPT_NUMBER,
1830 .help = "number of heads (ide disk geometry)",
1831 },{
1832 .name = "secs",
1833 .type = QEMU_OPT_NUMBER,
1834 .help = "number of sectors (ide disk geometry)",
1835 },{
1836 .name = "trans",
1837 .type = QEMU_OPT_STRING,
1838 .help = "chs translation (auto, lba. none)",
1839 },{
1840 .name = "media",
1841 .type = QEMU_OPT_STRING,
1842 .help = "media type (disk, cdrom)",
1843 },{
1844 .name = "snapshot",
1845 .type = QEMU_OPT_BOOL,
1846 },{
1847 .name = "file",
1848 .type = QEMU_OPT_STRING,
1849 .help = "disk image",
1850 },{
1851 .name = "cache",
1852 .type = QEMU_OPT_STRING,
1853 .help = "host cache usage (none, writeback, writethrough)",
1854 },{
1855 .name = "format",
1856 .type = QEMU_OPT_STRING,
1857 .help = "disk format (raw, qcow2, ...)",
1858 },{
1859 .name = "serial",
1860 .type = QEMU_OPT_STRING,
1861 },{
1862 .name = "werror",
1863 .type = QEMU_OPT_STRING,
1864 },{
1865 .name = "addr",
1866 .type = QEMU_OPT_STRING,
1867 .help = "pci address (virtio only)",
1868 },
1869 { /* end if list */ }
1870 },
1871};
1872
1873QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001874{
1875 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001876 char optstr[1024];
1877 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001878
thse4bcb142007-12-02 04:51:10 +00001879 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001880 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001881 va_end(ap);
1882
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001883 opts = qemu_opts_parse(&drive_opt_list, optstr, NULL);
1884 if (!opts) {
1885 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1886 __FUNCTION__, optstr);
1887 return NULL;
1888 }
1889 if (file)
1890 qemu_opt_set(opts, "file", file);
1891 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001892}
1893
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001894DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001895{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001896 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001897
1898 /* seek interface, bus and unit */
1899
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001900 TAILQ_FOREACH(dinfo, &drives, next) {
1901 if (dinfo->type == type &&
1902 dinfo->bus == bus &&
1903 dinfo->unit == unit)
1904 return dinfo;
1905 }
thse4bcb142007-12-02 04:51:10 +00001906
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001907 return NULL;
thse4bcb142007-12-02 04:51:10 +00001908}
1909
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001910DriveInfo *drive_get_by_id(char *id)
1911{
1912 DriveInfo *dinfo;
1913
1914 TAILQ_FOREACH(dinfo, &drives, next) {
1915 if (strcmp(id, dinfo->id))
1916 continue;
1917 return dinfo;
1918 }
1919 return NULL;
1920}
1921
thsf60d39b2007-12-17 03:55:57 +00001922int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001923{
1924 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001925 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001926
1927 max_bus = -1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001928 TAILQ_FOREACH(dinfo, &drives, next) {
1929 if(dinfo->type == type &&
1930 dinfo->bus > max_bus)
1931 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001932 }
1933 return max_bus;
1934}
1935
aliguorifa879c62009-01-07 17:32:33 +00001936const char *drive_get_serial(BlockDriverState *bdrv)
1937{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001938 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001939
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001940 TAILQ_FOREACH(dinfo, &drives, next) {
1941 if (dinfo->bdrv == bdrv)
1942 return dinfo->serial;
1943 }
aliguorifa879c62009-01-07 17:32:33 +00001944
1945 return "\0";
1946}
1947
aliguori428c5702009-01-21 18:59:04 +00001948BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1949{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001950 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001951
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001952 TAILQ_FOREACH(dinfo, &drives, next) {
1953 if (dinfo->bdrv == bdrv)
1954 return dinfo->onerror;
1955 }
aliguori428c5702009-01-21 18:59:04 +00001956
aliguoricdad4bd2009-02-28 16:51:01 +00001957 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001958}
1959
aurel32a1620fa2008-04-29 05:58:01 +00001960static void bdrv_format_print(void *opaque, const char *name)
1961{
1962 fprintf(stderr, " %s", name);
1963}
1964
aliguorib01b1112009-02-11 15:20:20 +00001965void drive_uninit(BlockDriverState *bdrv)
1966{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001967 DriveInfo *dinfo;
aliguorib01b1112009-02-11 15:20:20 +00001968
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001969 TAILQ_FOREACH(dinfo, &drives, next) {
1970 if (dinfo->bdrv != bdrv)
1971 continue;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001972 qemu_opts_del(dinfo->opts);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001973 TAILQ_REMOVE(&drives, dinfo, next);
1974 qemu_free(dinfo);
1975 break;
1976 }
aliguorib01b1112009-02-11 15:20:20 +00001977}
1978
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001979DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001980 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00001981{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001982 const char *buf;
1983 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00001984 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001985 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00001986 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00001987 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00001988 enum { MEDIA_DISK, MEDIA_CDROM } media;
1989 int bus_id, unit_id;
1990 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00001991 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00001992 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00001993 int max_devs;
1994 int index;
balrog33f00272007-12-24 14:33:24 +00001995 int cache;
aliguori428c5702009-01-21 18:59:04 +00001996 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001997 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001998 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001999 int snapshot = 0;
2000
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002001 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002002
thse4bcb142007-12-02 04:51:10 +00002003 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002004 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002005
blueswir1c9b1ae22008-09-28 18:55:17 +00002006 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002007 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002008 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002009 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002010 } else {
thsf60d39b2007-12-17 03:55:57 +00002011 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002012 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002013 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002014 }
2015 media = MEDIA_DISK;
2016
2017 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002018 bus_id = qemu_opt_get_number(opts, "bus", 0);
2019 unit_id = qemu_opt_get_number(opts, "unit", -1);
2020 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002021
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002022 cyls = qemu_opt_get_number(opts, "cyls", 0);
2023 heads = qemu_opt_get_number(opts, "heads", 0);
2024 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002025
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002026 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
thse4bcb142007-12-02 04:51:10 +00002027
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002028 file = qemu_opt_get(opts, "file");
2029 serial = qemu_opt_get(opts, "serial");
2030
2031 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002032 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002033 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002034 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002035 max_devs = MAX_IDE_DEVS;
2036 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002037 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002038 max_devs = MAX_SCSI_DEVS;
2039 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002040 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002041 max_devs = 0;
2042 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002043 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002044 max_devs = 0;
2045 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002046 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002047 max_devs = 0;
2048 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002049 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002050 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002051 } else if (!strcmp(buf, "virtio")) {
2052 type = IF_VIRTIO;
2053 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002054 } else if (!strcmp(buf, "xen")) {
2055 type = IF_XEN;
2056 max_devs = 0;
2057 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002058 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002059 return NULL;
thse4bcb142007-12-02 04:51:10 +00002060 }
2061 }
2062
thse4bcb142007-12-02 04:51:10 +00002063 if (cyls || heads || secs) {
2064 if (cyls < 1 || cyls > 16383) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002065 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002066 return NULL;
thse4bcb142007-12-02 04:51:10 +00002067 }
2068 if (heads < 1 || heads > 16) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002069 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002070 return NULL;
thse4bcb142007-12-02 04:51:10 +00002071 }
2072 if (secs < 1 || secs > 63) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002073 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002074 return NULL;
thse4bcb142007-12-02 04:51:10 +00002075 }
2076 }
2077
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002078 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002079 if (!cyls) {
2080 fprintf(stderr,
2081 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002082 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002083 return NULL;
thse4bcb142007-12-02 04:51:10 +00002084 }
2085 if (!strcmp(buf, "none"))
2086 translation = BIOS_ATA_TRANSLATION_NONE;
2087 else if (!strcmp(buf, "lba"))
2088 translation = BIOS_ATA_TRANSLATION_LBA;
2089 else if (!strcmp(buf, "auto"))
2090 translation = BIOS_ATA_TRANSLATION_AUTO;
2091 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002092 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002093 return NULL;
thse4bcb142007-12-02 04:51:10 +00002094 }
2095 }
2096
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002097 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002098 if (!strcmp(buf, "disk")) {
2099 media = MEDIA_DISK;
2100 } else if (!strcmp(buf, "cdrom")) {
2101 if (cyls || secs || heads) {
2102 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002103 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002104 return NULL;
thse4bcb142007-12-02 04:51:10 +00002105 }
2106 media = MEDIA_CDROM;
2107 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002108 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002109 return NULL;
thse4bcb142007-12-02 04:51:10 +00002110 }
2111 }
2112
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002113 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002114 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002115 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002116 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002117 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002118 else if (!strcmp(buf, "writeback"))
2119 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002120 else {
2121 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002122 return NULL;
balrog33f00272007-12-24 14:33:24 +00002123 }
2124 }
2125
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002126 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002127 if (strcmp(buf, "?") == 0) {
2128 fprintf(stderr, "qemu: Supported formats:");
2129 bdrv_iterate_format(bdrv_format_print, NULL);
2130 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002131 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002132 }
aurel321e72d3b2008-04-28 20:26:45 +00002133 drv = bdrv_find_format(buf);
2134 if (!drv) {
2135 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002136 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002137 }
2138 }
2139
aliguoricdad4bd2009-02-28 16:51:01 +00002140 onerror = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002141 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002142 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002143 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002144 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002145 }
2146 if (!strcmp(buf, "ignore"))
2147 onerror = BLOCK_ERR_IGNORE;
2148 else if (!strcmp(buf, "enospc"))
2149 onerror = BLOCK_ERR_STOP_ENOSPC;
2150 else if (!strcmp(buf, "stop"))
2151 onerror = BLOCK_ERR_STOP_ANY;
2152 else if (!strcmp(buf, "report"))
2153 onerror = BLOCK_ERR_REPORT;
2154 else {
2155 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002156 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002157 }
2158 }
2159
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002160 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002161 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002162 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002163 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002164 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002165 }
2166
thse4bcb142007-12-02 04:51:10 +00002167 /* compute bus and unit according index */
2168
2169 if (index != -1) {
2170 if (bus_id != 0 || unit_id != -1) {
2171 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002172 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002173 return NULL;
thse4bcb142007-12-02 04:51:10 +00002174 }
2175 if (max_devs == 0)
2176 {
2177 unit_id = index;
2178 bus_id = 0;
2179 } else {
2180 unit_id = index % max_devs;
2181 bus_id = index / max_devs;
2182 }
2183 }
2184
2185 /* if user doesn't specify a unit_id,
2186 * try to find the first free
2187 */
2188
2189 if (unit_id == -1) {
2190 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002191 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002192 unit_id++;
2193 if (max_devs && unit_id >= max_devs) {
2194 unit_id -= max_devs;
2195 bus_id++;
2196 }
2197 }
2198 }
2199
2200 /* check unit id */
2201
2202 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002203 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2204 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002205 return NULL;
thse4bcb142007-12-02 04:51:10 +00002206 }
2207
2208 /*
2209 * ignore multiple definitions
2210 */
2211
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002212 if (drive_get(type, bus_id, unit_id) != NULL) {
2213 *fatal_error = 0;
2214 return NULL;
2215 }
thse4bcb142007-12-02 04:51:10 +00002216
2217 /* init */
2218
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002219 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002220 if ((buf = qemu_opt_get(opts, "id")) != NULL) {
2221 dinfo->id = qemu_strdup(buf);
2222 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002223 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002224 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002225 if (type == IF_IDE || type == IF_SCSI)
2226 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2227 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002228 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002229 devname, bus_id, mediastr, unit_id);
2230 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002231 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002232 devname, mediastr, unit_id);
2233 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002234 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002235 dinfo->devaddr = devaddr;
2236 dinfo->type = type;
2237 dinfo->bus = bus_id;
2238 dinfo->unit = unit_id;
2239 dinfo->onerror = onerror;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002240 dinfo->opts = opts;
2241 if (serial)
2242 strncpy(dinfo->serial, serial, sizeof(serial));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002243 TAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002244
thsf60d39b2007-12-17 03:55:57 +00002245 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002246 case IF_IDE:
2247 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002248 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002249 switch(media) {
2250 case MEDIA_DISK:
2251 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002252 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2253 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002254 }
2255 break;
2256 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002257 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002258 break;
2259 }
2260 break;
2261 case IF_SD:
2262 /* FIXME: This isn't really a floppy, but it's a reasonable
2263 approximation. */
2264 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002265 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002266 break;
2267 case IF_PFLASH:
2268 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002269 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002270 break;
Paul Brookaae94602009-05-14 22:35:06 +01002271 case IF_COUNT:
2272 abort();
thse4bcb142007-12-02 04:51:10 +00002273 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002274 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002275 *fatal_error = 0;
2276 return NULL;
2277 }
balrog33f00272007-12-24 14:33:24 +00002278 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002279 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002280 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002281 cache = 2; /* always use write-back with snapshot */
2282 }
2283 if (cache == 0) /* no caching */
2284 bdrv_flags |= BDRV_O_NOCACHE;
2285 else if (cache == 2) /* write-back */
2286 bdrv_flags |= BDRV_O_CACHE_WB;
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002287 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002288 fprintf(stderr, "qemu: could not open disk image %s\n",
2289 file);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002290 return NULL;
thse4bcb142007-12-02 04:51:10 +00002291 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002292 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002293 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002294 *fatal_error = 0;
2295 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002296}
2297
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002298static int drive_init_func(QemuOpts *opts, void *opaque)
2299{
2300 QEMUMachine *machine = opaque;
2301 int fatal_error = 0;
2302
2303 if (drive_init(opts, machine, &fatal_error) == NULL) {
2304 if (fatal_error)
2305 return 1;
2306 }
2307 return 0;
2308}
2309
2310static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2311{
2312 if (NULL == qemu_opt_get(opts, "snapshot")) {
2313 qemu_opt_set(opts, "snapshot", "on");
2314 }
2315 return 0;
2316}
2317
Jan Kiszka76e30d02009-07-02 00:19:02 +02002318void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2319{
2320 boot_set_handler = func;
2321 boot_set_opaque = opaque;
2322}
2323
2324int qemu_boot_set(const char *boot_devices)
2325{
2326 if (!boot_set_handler) {
2327 return -EINVAL;
2328 }
2329 return boot_set_handler(boot_set_opaque, boot_devices);
2330}
2331
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002332static int parse_bootdevices(char *devices)
2333{
2334 /* We just do some generic consistency checks */
2335 const char *p;
2336 int bitmap = 0;
2337
2338 for (p = devices; *p != '\0'; p++) {
2339 /* Allowed boot devices are:
2340 * a-b: floppy disk drives
2341 * c-f: IDE disk drives
2342 * g-m: machine implementation dependant drives
2343 * n-p: network devices
2344 * It's up to each machine implementation to check if the given boot
2345 * devices match the actual hardware implementation and firmware
2346 * features.
2347 */
2348 if (*p < 'a' || *p > 'p') {
2349 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2350 exit(1);
2351 }
2352 if (bitmap & (1 << (*p - 'a'))) {
2353 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2354 exit(1);
2355 }
2356 bitmap |= 1 << (*p - 'a');
2357 }
2358 return bitmap;
2359}
2360
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002361static void restore_boot_devices(void *opaque)
2362{
2363 char *standard_boot_devices = opaque;
2364
2365 qemu_boot_set(standard_boot_devices);
2366
2367 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2368 qemu_free(standard_boot_devices);
2369}
2370
aliguori268a3622009-04-21 22:30:27 +00002371static void numa_add(const char *optarg)
2372{
2373 char option[128];
2374 char *endptr;
2375 unsigned long long value, endvalue;
2376 int nodenr;
2377
2378 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2379 if (!strcmp(option, "node")) {
2380 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2381 nodenr = nb_numa_nodes;
2382 } else {
2383 nodenr = strtoull(option, NULL, 10);
2384 }
2385
2386 if (get_param_value(option, 128, "mem", optarg) == 0) {
2387 node_mem[nodenr] = 0;
2388 } else {
2389 value = strtoull(option, &endptr, 0);
2390 switch (*endptr) {
2391 case 0: case 'M': case 'm':
2392 value <<= 20;
2393 break;
2394 case 'G': case 'g':
2395 value <<= 30;
2396 break;
2397 }
2398 node_mem[nodenr] = value;
2399 }
2400 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2401 node_cpumask[nodenr] = 0;
2402 } else {
2403 value = strtoull(option, &endptr, 10);
2404 if (value >= 64) {
2405 value = 63;
2406 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2407 } else {
2408 if (*endptr == '-') {
2409 endvalue = strtoull(endptr+1, &endptr, 10);
2410 if (endvalue >= 63) {
2411 endvalue = 62;
2412 fprintf(stderr,
2413 "only 63 CPUs in NUMA mode supported.\n");
2414 }
2415 value = (1 << (endvalue + 1)) - (1 << value);
2416 } else {
2417 value = 1 << value;
2418 }
2419 }
2420 node_cpumask[nodenr] = value;
2421 }
2422 nb_numa_nodes++;
2423 }
2424 return;
2425}
2426
bellard330d0412003-07-26 18:11:40 +00002427/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002428/* USB devices */
2429
pbrook0d92ed32006-05-21 16:30:15 +00002430static USBPort *used_usb_ports;
2431static USBPort *free_usb_ports;
2432
2433/* ??? Maybe change this to register a hub to keep track of the topology. */
2434void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2435 usb_attachfn attach)
2436{
2437 port->opaque = opaque;
2438 port->index = index;
2439 port->attach = attach;
2440 port->next = free_usb_ports;
2441 free_usb_ports = port;
2442}
2443
aliguori4b096fc2008-08-21 19:28:55 +00002444int usb_device_add_dev(USBDevice *dev)
2445{
2446 USBPort *port;
2447
2448 /* Find a USB port to add the device to. */
2449 port = free_usb_ports;
2450 if (!port->next) {
2451 USBDevice *hub;
2452
2453 /* Create a new hub and chain it on. */
2454 free_usb_ports = NULL;
2455 port->next = used_usb_ports;
2456 used_usb_ports = port;
2457
2458 hub = usb_hub_init(VM_USB_HUB_SIZE);
2459 usb_attach(port, hub);
2460 port = free_usb_ports;
2461 }
2462
2463 free_usb_ports = port->next;
2464 port->next = used_usb_ports;
2465 used_usb_ports = port;
2466 usb_attach(port, dev);
2467 return 0;
2468}
2469
aliguoribb5fc202009-03-05 23:01:15 +00002470static void usb_msd_password_cb(void *opaque, int err)
2471{
2472 USBDevice *dev = opaque;
2473
2474 if (!err)
2475 usb_device_add_dev(dev);
2476 else
2477 dev->handle_destroy(dev);
2478}
2479
aliguoric0f4ce72009-03-05 23:01:01 +00002480static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002481{
2482 const char *p;
2483 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002484
pbrook0d92ed32006-05-21 16:30:15 +00002485 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002486 return -1;
2487
2488 if (strstart(devname, "host:", &p)) {
2489 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002490 } else if (!strcmp(devname, "mouse")) {
2491 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002492 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002493 dev = usb_tablet_init();
2494 } else if (!strcmp(devname, "keyboard")) {
2495 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002496 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002497 BlockDriverState *bs;
2498
aliguoribb5fc202009-03-05 23:01:15 +00002499 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002500 if (!dev)
2501 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002502 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002503 if (bdrv_key_required(bs)) {
2504 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002505 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002506 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2507 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002508 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002509 }
2510 }
balrogf6d2a312007-06-10 19:21:04 +00002511 } else if (!strcmp(devname, "wacom-tablet")) {
2512 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002513 } else if (strstart(devname, "serial:", &p)) {
2514 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002515#ifdef CONFIG_BRLAPI
2516 } else if (!strcmp(devname, "braille")) {
2517 dev = usb_baum_init();
2518#endif
balrog6c9f8862008-07-17 20:47:13 +00002519 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002520 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002521
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002522 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002523 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002524 nd_table[nic].model = "usb";
2525 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002526 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2527 dev = usb_bt_init(devname[2] ? hci_init(p) :
2528 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002529 } else {
2530 return -1;
2531 }
pbrook0d92ed32006-05-21 16:30:15 +00002532 if (!dev)
2533 return -1;
2534
aliguori4b096fc2008-08-21 19:28:55 +00002535 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002536}
2537
aliguori1f3870a2008-08-21 19:27:48 +00002538int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002539{
pbrook0d92ed32006-05-21 16:30:15 +00002540 USBPort *port;
2541 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002542 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002543
pbrook0d92ed32006-05-21 16:30:15 +00002544 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002545 return -1;
2546
bellarda594cfb2005-11-06 16:13:29 +00002547 if (bus_num != 0)
2548 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002549
2550 lastp = &used_usb_ports;
2551 port = used_usb_ports;
2552 while (port && port->dev->addr != addr) {
2553 lastp = &port->next;
2554 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002555 }
pbrook0d92ed32006-05-21 16:30:15 +00002556
2557 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002558 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002559
bellard059809e2006-07-19 18:06:15 +00002560 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002561 *lastp = port->next;
2562 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002563 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002564 port->next = free_usb_ports;
2565 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002566 return 0;
2567}
2568
aliguori1f3870a2008-08-21 19:27:48 +00002569static int usb_device_del(const char *devname)
2570{
2571 int bus_num, addr;
2572 const char *p;
2573
aliguori5d0c5752008-09-14 01:07:41 +00002574 if (strstart(devname, "host:", &p))
2575 return usb_host_device_close(p);
2576
aliguori1f3870a2008-08-21 19:27:48 +00002577 if (!used_usb_ports)
2578 return -1;
2579
2580 p = strchr(devname, '.');
2581 if (!p)
2582 return -1;
2583 bus_num = strtoul(devname, NULL, 0);
2584 addr = strtoul(p + 1, NULL, 0);
2585
2586 return usb_device_del_addr(bus_num, addr);
2587}
2588
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002589static int usb_parse(const char *cmdline)
2590{
2591 return usb_device_add(cmdline, 0);
2592}
2593
aliguori376253e2009-03-05 23:01:23 +00002594void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002595{
aliguoric0f4ce72009-03-05 23:01:01 +00002596 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002597}
2598
aliguori376253e2009-03-05 23:01:23 +00002599void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002600{
aliguori4b096fc2008-08-21 19:28:55 +00002601 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002602}
2603
aliguori376253e2009-03-05 23:01:23 +00002604void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002605{
2606 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002607 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002608 const char *speed_str;
2609
pbrook0d92ed32006-05-21 16:30:15 +00002610 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002611 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002612 return;
2613 }
2614
pbrook0d92ed32006-05-21 16:30:15 +00002615 for (port = used_usb_ports; port; port = port->next) {
2616 dev = port->dev;
2617 if (!dev)
2618 continue;
2619 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002620 case USB_SPEED_LOW:
2621 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002622 break;
ths5fafdf22007-09-16 21:08:06 +00002623 case USB_SPEED_FULL:
2624 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002625 break;
ths5fafdf22007-09-16 21:08:06 +00002626 case USB_SPEED_HIGH:
2627 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002628 break;
2629 default:
ths5fafdf22007-09-16 21:08:06 +00002630 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002631 break;
bellarda594cfb2005-11-06 16:13:29 +00002632 }
aliguori376253e2009-03-05 23:01:23 +00002633 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2634 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002635 }
2636}
2637
bellardf7cce892004-12-08 22:21:25 +00002638/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002639/* PCMCIA/Cardbus */
2640
2641static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002642 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002643 struct pcmcia_socket_entry_s *next;
2644} *pcmcia_sockets = 0;
2645
Paul Brookbc24a222009-05-10 01:44:56 +01002646void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002647{
2648 struct pcmcia_socket_entry_s *entry;
2649
2650 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2651 entry->socket = socket;
2652 entry->next = pcmcia_sockets;
2653 pcmcia_sockets = entry;
2654}
2655
Paul Brookbc24a222009-05-10 01:44:56 +01002656void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002657{
2658 struct pcmcia_socket_entry_s *entry, **ptr;
2659
2660 ptr = &pcmcia_sockets;
2661 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2662 if (entry->socket == socket) {
2663 *ptr = entry->next;
2664 qemu_free(entry);
2665 }
2666}
2667
aliguori376253e2009-03-05 23:01:23 +00002668void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002669{
2670 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002671
balrog201a51f2007-04-30 00:51:09 +00002672 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002673 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002674
2675 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002676 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2677 iter->socket->attached ? iter->socket->card_string :
2678 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002679}
2680
2681/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002682/* register display */
2683
aliguori7b5d76d2009-03-13 15:02:13 +00002684struct DisplayAllocator default_allocator = {
2685 defaultallocator_create_displaysurface,
2686 defaultallocator_resize_displaysurface,
2687 defaultallocator_free_displaysurface
2688};
2689
aliguori3023f332009-01-16 19:04:14 +00002690void register_displaystate(DisplayState *ds)
2691{
2692 DisplayState **s;
2693 s = &display_state;
2694 while (*s != NULL)
2695 s = &(*s)->next;
2696 ds->next = NULL;
2697 *s = ds;
2698}
2699
2700DisplayState *get_displaystate(void)
2701{
2702 return display_state;
2703}
2704
aliguori7b5d76d2009-03-13 15:02:13 +00002705DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2706{
2707 if(ds->allocator == &default_allocator) ds->allocator = da;
2708 return ds->allocator;
2709}
2710
ths2ff89792007-06-21 23:34:19 +00002711/* dumb display */
2712
aliguori8f391ab2009-01-19 16:34:10 +00002713static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002714{
aliguori8f391ab2009-01-19 16:34:10 +00002715 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002716 ds->allocator = &default_allocator;
2717 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002718 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002719}
2720
2721/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002722/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002723
bellardc4b1fcc2004-03-14 21:44:30 +00002724typedef struct IOHandlerRecord {
2725 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002726 IOCanRWHandler *fd_read_poll;
2727 IOHandler *fd_read;
2728 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002729 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002730 void *opaque;
2731 /* temporary data */
2732 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002733 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002734} IOHandlerRecord;
2735
bellard8a7ddc32004-03-31 19:00:16 +00002736static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002737
bellard7c9d8e02005-11-15 22:16:05 +00002738/* XXX: fd_read_poll should be suppressed, but an API change is
2739 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002740int qemu_set_fd_handler2(int fd,
2741 IOCanRWHandler *fd_read_poll,
2742 IOHandler *fd_read,
2743 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002744 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002745{
bellard8a7ddc32004-03-31 19:00:16 +00002746 IOHandlerRecord **pioh, *ioh;
2747
bellard7c9d8e02005-11-15 22:16:05 +00002748 if (!fd_read && !fd_write) {
2749 pioh = &first_io_handler;
2750 for(;;) {
2751 ioh = *pioh;
2752 if (ioh == NULL)
2753 break;
2754 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002755 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002756 break;
2757 }
2758 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002759 }
bellard7c9d8e02005-11-15 22:16:05 +00002760 } else {
2761 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2762 if (ioh->fd == fd)
2763 goto found;
2764 }
2765 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002766 ioh->next = first_io_handler;
2767 first_io_handler = ioh;
2768 found:
2769 ioh->fd = fd;
2770 ioh->fd_read_poll = fd_read_poll;
2771 ioh->fd_read = fd_read;
2772 ioh->fd_write = fd_write;
2773 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002774 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002775 }
bellard7c9d8e02005-11-15 22:16:05 +00002776 return 0;
2777}
2778
ths5fafdf22007-09-16 21:08:06 +00002779int qemu_set_fd_handler(int fd,
2780 IOHandler *fd_read,
2781 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002782 void *opaque)
2783{
2784 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002785}
2786
aliguori56f3a5d2008-10-31 18:07:17 +00002787#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002788/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002789/* Polling handling */
2790
2791typedef struct PollingEntry {
2792 PollingFunc *func;
2793 void *opaque;
2794 struct PollingEntry *next;
2795} PollingEntry;
2796
2797static PollingEntry *first_polling_entry;
2798
2799int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2800{
2801 PollingEntry **ppe, *pe;
2802 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002803 pe->func = func;
2804 pe->opaque = opaque;
2805 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2806 *ppe = pe;
2807 return 0;
2808}
2809
2810void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2811{
2812 PollingEntry **ppe, *pe;
2813 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2814 pe = *ppe;
2815 if (pe->func == func && pe->opaque == opaque) {
2816 *ppe = pe->next;
2817 qemu_free(pe);
2818 break;
2819 }
2820 }
2821}
2822
bellarda18e5242006-06-25 17:18:27 +00002823/***********************************************************/
2824/* Wait objects support */
2825typedef struct WaitObjects {
2826 int num;
2827 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2828 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2829 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2830} WaitObjects;
2831
2832static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002833
bellarda18e5242006-06-25 17:18:27 +00002834int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2835{
2836 WaitObjects *w = &wait_objects;
2837
2838 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2839 return -1;
2840 w->events[w->num] = handle;
2841 w->func[w->num] = func;
2842 w->opaque[w->num] = opaque;
2843 w->num++;
2844 return 0;
2845}
2846
2847void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2848{
2849 int i, found;
2850 WaitObjects *w = &wait_objects;
2851
2852 found = 0;
2853 for (i = 0; i < w->num; i++) {
2854 if (w->events[i] == handle)
2855 found = 1;
2856 if (found) {
2857 w->events[i] = w->events[i + 1];
2858 w->func[i] = w->func[i + 1];
2859 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002860 }
bellarda18e5242006-06-25 17:18:27 +00002861 }
2862 if (found)
2863 w->num--;
2864}
2865#endif
2866
bellard8a7ddc32004-03-31 19:00:16 +00002867/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002868/* ram save/restore */
2869
bellard8a7ddc32004-03-31 19:00:16 +00002870static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2871{
2872 int v;
2873
2874 v = qemu_get_byte(f);
2875 switch(v) {
2876 case 0:
2877 if (qemu_get_buffer(f, buf, len) != len)
2878 return -EIO;
2879 break;
2880 case 1:
2881 v = qemu_get_byte(f);
2882 memset(buf, v, len);
2883 break;
2884 default:
2885 return -EINVAL;
2886 }
aliguori871d2f02008-10-13 03:07:56 +00002887
2888 if (qemu_file_has_error(f))
2889 return -EIO;
2890
bellard8a7ddc32004-03-31 19:00:16 +00002891 return 0;
2892}
2893
bellardc88676f2006-08-06 13:36:11 +00002894static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002895{
aurel3200f82b82008-04-27 21:12:55 +00002896 int ret;
2897 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002898
pbrook94a6b542009-04-11 17:15:54 +00002899 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00002900 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00002901 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00002902 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00002903 if (ret)
2904 return ret;
2905 }
2906 return 0;
2907}
2908
bellardc88676f2006-08-06 13:36:11 +00002909#define BDRV_HASH_BLOCK_SIZE 1024
2910#define IOBUF_SIZE 4096
2911#define RAM_CBLOCK_MAGIC 0xfabe
2912
bellardc88676f2006-08-06 13:36:11 +00002913typedef struct RamDecompressState {
2914 z_stream zstream;
2915 QEMUFile *f;
2916 uint8_t buf[IOBUF_SIZE];
2917} RamDecompressState;
2918
2919static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2920{
2921 int ret;
2922 memset(s, 0, sizeof(*s));
2923 s->f = f;
2924 ret = inflateInit(&s->zstream);
2925 if (ret != Z_OK)
2926 return -1;
2927 return 0;
2928}
2929
2930static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2931{
2932 int ret, clen;
2933
2934 s->zstream.avail_out = len;
2935 s->zstream.next_out = buf;
2936 while (s->zstream.avail_out > 0) {
2937 if (s->zstream.avail_in == 0) {
2938 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
2939 return -1;
2940 clen = qemu_get_be16(s->f);
2941 if (clen > IOBUF_SIZE)
2942 return -1;
2943 qemu_get_buffer(s->f, s->buf, clen);
2944 s->zstream.avail_in = clen;
2945 s->zstream.next_in = s->buf;
2946 }
2947 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
2948 if (ret != Z_OK && ret != Z_STREAM_END) {
2949 return -1;
2950 }
2951 }
2952 return 0;
2953}
2954
2955static void ram_decompress_close(RamDecompressState *s)
2956{
2957 inflateEnd(&s->zstream);
2958}
2959
aliguori475e4272008-10-06 20:21:51 +00002960#define RAM_SAVE_FLAG_FULL 0x01
2961#define RAM_SAVE_FLAG_COMPRESS 0x02
2962#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2963#define RAM_SAVE_FLAG_PAGE 0x08
2964#define RAM_SAVE_FLAG_EOS 0x10
2965
2966static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002967{
aliguori475e4272008-10-06 20:21:51 +00002968 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2969 uint32_t *array = (uint32_t *)page;
2970 int i;
ths3b46e622007-09-17 08:09:54 +00002971
aliguori475e4272008-10-06 20:21:51 +00002972 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2973 if (array[i] != val)
2974 return 0;
bellardc88676f2006-08-06 13:36:11 +00002975 }
aliguori475e4272008-10-06 20:21:51 +00002976
2977 return 1;
bellardc88676f2006-08-06 13:36:11 +00002978}
2979
aliguori475e4272008-10-06 20:21:51 +00002980static int ram_save_block(QEMUFile *f)
2981{
2982 static ram_addr_t current_addr = 0;
2983 ram_addr_t saved_addr = current_addr;
2984 ram_addr_t addr = 0;
2985 int found = 0;
2986
pbrook94a6b542009-04-11 17:15:54 +00002987 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002988 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002989 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002990
2991 cpu_physical_memory_reset_dirty(current_addr,
2992 current_addr + TARGET_PAGE_SIZE,
2993 MIGRATION_DIRTY_FLAG);
2994
pbrook5579c7f2009-04-11 14:47:08 +00002995 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002996
pbrook5579c7f2009-04-11 14:47:08 +00002997 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002998 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002999 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003000 } else {
3001 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003002 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003003 }
3004
3005 found = 1;
3006 break;
3007 }
3008 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003009 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003010 }
3011
3012 return found;
3013}
3014
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003015static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00003016
3017static ram_addr_t ram_save_remaining(void)
3018{
3019 ram_addr_t addr;
3020 ram_addr_t count = 0;
3021
pbrook94a6b542009-04-11 17:15:54 +00003022 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003023 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3024 count++;
3025 }
3026
3027 return count;
3028}
3029
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003030uint64_t ram_bytes_remaining(void)
3031{
3032 return ram_save_remaining() * TARGET_PAGE_SIZE;
3033}
3034
3035uint64_t ram_bytes_transferred(void)
3036{
3037 return bytes_transferred;
3038}
3039
3040uint64_t ram_bytes_total(void)
3041{
3042 return last_ram_offset;
3043}
3044
aliguori475e4272008-10-06 20:21:51 +00003045static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3046{
3047 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003048 uint64_t bytes_transferred_last;
3049 double bwidth = 0;
3050 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00003051
Jan Kiszka9fa06382009-05-22 23:51:45 +02003052 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003053 qemu_file_set_error(f);
3054 return 0;
3055 }
3056
aliguori475e4272008-10-06 20:21:51 +00003057 if (stage == 1) {
3058 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003059 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003060 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3061 cpu_physical_memory_set_dirty(addr);
3062 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003063
aliguori475e4272008-10-06 20:21:51 +00003064 /* Enable dirty memory tracking */
3065 cpu_physical_memory_set_dirty_tracking(1);
3066
pbrook94a6b542009-04-11 17:15:54 +00003067 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003068 }
3069
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003070 bytes_transferred_last = bytes_transferred;
3071 bwidth = get_clock();
3072
aliguori475e4272008-10-06 20:21:51 +00003073 while (!qemu_file_rate_limit(f)) {
3074 int ret;
3075
3076 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003077 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003078 if (ret == 0) /* no more blocks */
3079 break;
3080 }
3081
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003082 bwidth = get_clock() - bwidth;
3083 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3084
3085 /* if we haven't transferred anything this round, force expected_time to a
3086 * a very high value, but without crashing */
3087 if (bwidth == 0)
3088 bwidth = 0.000001;
3089
aliguori475e4272008-10-06 20:21:51 +00003090 /* try transferring iterative blocks of memory */
3091
3092 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003093
3094 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003095 while (ram_save_block(f) != 0) {
3096 bytes_transferred += TARGET_PAGE_SIZE;
3097 }
aliguori8215e912009-04-05 19:30:55 +00003098 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003099 }
3100
3101 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3102
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003103 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3104
3105 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003106}
3107
3108static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003109{
3110 RamDecompressState s1, *s = &s1;
3111 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003112 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003113
bellardc88676f2006-08-06 13:36:11 +00003114 if (ram_decompress_open(s, f) < 0)
3115 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003116 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003117 if (ram_decompress_buf(s, buf, 1) < 0) {
3118 fprintf(stderr, "Error while reading ram block header\n");
3119 goto error;
3120 }
3121 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003122 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3123 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003124 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003125 goto error;
3126 }
aliguori475e4272008-10-06 20:21:51 +00003127 } else {
bellardc88676f2006-08-06 13:36:11 +00003128 error:
3129 printf("Error block header\n");
3130 return -EINVAL;
3131 }
3132 }
3133 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003134
3135 return 0;
3136}
3137
3138static int ram_load(QEMUFile *f, void *opaque, int version_id)
3139{
3140 ram_addr_t addr;
3141 int flags;
3142
3143 if (version_id == 1)
3144 return ram_load_v1(f, opaque);
3145
3146 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003147 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003148 return -EINVAL;
3149 return ram_load_dead(f, opaque);
3150 }
3151
3152 if (version_id != 3)
3153 return -EINVAL;
3154
3155 do {
3156 addr = qemu_get_be64(f);
3157
3158 flags = addr & ~TARGET_PAGE_MASK;
3159 addr &= TARGET_PAGE_MASK;
3160
3161 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003162 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003163 return -EINVAL;
3164 }
3165
3166 if (flags & RAM_SAVE_FLAG_FULL) {
3167 if (ram_load_dead(f, opaque) < 0)
3168 return -EINVAL;
3169 }
3170
3171 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3172 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003173 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3174#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003175 if (ch == 0 &&
3176 (!kvm_enabled() || kvm_has_sync_mmu())) {
3177 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003178 }
Anthony Liguori30868442009-06-17 16:46:12 -05003179#endif
aliguori475e4272008-10-06 20:21:51 +00003180 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003181 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003182 } while (!(flags & RAM_SAVE_FLAG_EOS));
3183
bellardc88676f2006-08-06 13:36:11 +00003184 return 0;
3185}
3186
aliguori9e472e12008-10-08 19:50:24 +00003187void qemu_service_io(void)
3188{
aliguorid9f75a42009-04-24 18:03:11 +00003189 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003190}
3191
bellard8a7ddc32004-03-31 19:00:16 +00003192/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003193/* bottom halves (can be seen as timers which expire ASAP) */
3194
3195struct QEMUBH {
3196 QEMUBHFunc *cb;
3197 void *opaque;
3198 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003199 int idle;
3200 int deleted;
bellard83f64092006-08-01 16:21:11 +00003201 QEMUBH *next;
3202};
3203
3204static QEMUBH *first_bh = NULL;
3205
3206QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3207{
3208 QEMUBH *bh;
3209 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003210 bh->cb = cb;
3211 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003212 bh->next = first_bh;
3213 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003214 return bh;
3215}
3216
bellard6eb57332006-08-06 09:51:25 +00003217int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003218{
aliguori1b435b12008-10-31 17:24:21 +00003219 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003220 int ret;
bellard83f64092006-08-01 16:21:11 +00003221
bellard6eb57332006-08-06 09:51:25 +00003222 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003223 for (bh = first_bh; bh; bh = bh->next) {
3224 if (!bh->deleted && bh->scheduled) {
3225 bh->scheduled = 0;
3226 if (!bh->idle)
3227 ret = 1;
3228 bh->idle = 0;
3229 bh->cb(bh->opaque);
3230 }
bellard83f64092006-08-01 16:21:11 +00003231 }
aliguori1b435b12008-10-31 17:24:21 +00003232
3233 /* remove deleted bhs */
3234 bhp = &first_bh;
3235 while (*bhp) {
3236 bh = *bhp;
3237 if (bh->deleted) {
3238 *bhp = bh->next;
3239 qemu_free(bh);
3240 } else
3241 bhp = &bh->next;
3242 }
3243
bellard6eb57332006-08-06 09:51:25 +00003244 return ret;
bellard83f64092006-08-01 16:21:11 +00003245}
3246
aliguori1b435b12008-10-31 17:24:21 +00003247void qemu_bh_schedule_idle(QEMUBH *bh)
3248{
3249 if (bh->scheduled)
3250 return;
3251 bh->scheduled = 1;
3252 bh->idle = 1;
3253}
3254
bellard83f64092006-08-01 16:21:11 +00003255void qemu_bh_schedule(QEMUBH *bh)
3256{
bellard83f64092006-08-01 16:21:11 +00003257 if (bh->scheduled)
3258 return;
3259 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003260 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003261 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003262 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003263}
3264
3265void qemu_bh_cancel(QEMUBH *bh)
3266{
aliguori1b435b12008-10-31 17:24:21 +00003267 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003268}
3269
3270void qemu_bh_delete(QEMUBH *bh)
3271{
aliguori1b435b12008-10-31 17:24:21 +00003272 bh->scheduled = 0;
3273 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003274}
3275
aliguori56f3a5d2008-10-31 18:07:17 +00003276static void qemu_bh_update_timeout(int *timeout)
3277{
3278 QEMUBH *bh;
3279
3280 for (bh = first_bh; bh; bh = bh->next) {
3281 if (!bh->deleted && bh->scheduled) {
3282 if (bh->idle) {
3283 /* idle bottom halves will be polled at least
3284 * every 10ms */
3285 *timeout = MIN(10, *timeout);
3286 } else {
3287 /* non-idle bottom halves will be executed
3288 * immediately */
3289 *timeout = 0;
3290 break;
3291 }
3292 }
3293 }
3294}
3295
bellard83f64092006-08-01 16:21:11 +00003296/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003297/* machine registration */
3298
blueswir1bdaf78e2008-10-04 07:24:27 +00003299static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003300QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003301
3302int qemu_register_machine(QEMUMachine *m)
3303{
3304 QEMUMachine **pm;
3305 pm = &first_machine;
3306 while (*pm != NULL)
3307 pm = &(*pm)->next;
3308 m->next = NULL;
3309 *pm = m;
3310 return 0;
3311}
3312
pbrook9596ebb2007-11-18 01:44:38 +00003313static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003314{
3315 QEMUMachine *m;
3316
3317 for(m = first_machine; m != NULL; m = m->next) {
3318 if (!strcmp(m->name, name))
3319 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003320 if (m->alias && !strcmp(m->alias, name))
3321 return m;
bellardcc1daa42005-06-05 14:49:17 +00003322 }
3323 return NULL;
3324}
3325
Anthony Liguori0c257432009-05-21 20:41:01 -05003326static QEMUMachine *find_default_machine(void)
3327{
3328 QEMUMachine *m;
3329
3330 for(m = first_machine; m != NULL; m = m->next) {
3331 if (m->is_default) {
3332 return m;
3333 }
3334 }
3335 return NULL;
3336}
3337
bellardcc1daa42005-06-05 14:49:17 +00003338/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003339/* main execution loop */
3340
pbrook9596ebb2007-11-18 01:44:38 +00003341static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003342{
aliguori7d957bd2009-01-15 22:14:11 +00003343 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003344 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003345 DisplayChangeListener *dcl = ds->listeners;
3346
3347 dpy_refresh(ds);
3348
3349 while (dcl != NULL) {
3350 if (dcl->gui_timer_interval &&
3351 dcl->gui_timer_interval < interval)
3352 interval = dcl->gui_timer_interval;
3353 dcl = dcl->next;
3354 }
3355 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003356}
3357
blueswir19043b622009-01-21 19:28:13 +00003358static void nographic_update(void *opaque)
3359{
3360 uint64_t interval = GUI_REFRESH_INTERVAL;
3361
3362 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3363}
3364
bellard0bd48852005-11-11 00:00:47 +00003365struct vm_change_state_entry {
3366 VMChangeStateHandler *cb;
3367 void *opaque;
3368 LIST_ENTRY (vm_change_state_entry) entries;
3369};
3370
3371static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3372
3373VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3374 void *opaque)
3375{
3376 VMChangeStateEntry *e;
3377
3378 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003379
3380 e->cb = cb;
3381 e->opaque = opaque;
3382 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3383 return e;
3384}
3385
3386void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3387{
3388 LIST_REMOVE (e, entries);
3389 qemu_free (e);
3390}
3391
aliguori9781e042009-01-22 17:15:29 +00003392static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003393{
3394 VMChangeStateEntry *e;
3395
3396 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003397 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003398 }
3399}
3400
aliguorid6dc3d42009-04-24 18:04:07 +00003401static void resume_all_vcpus(void);
3402static void pause_all_vcpus(void);
3403
bellard8a7ddc32004-03-31 19:00:16 +00003404void vm_start(void)
3405{
3406 if (!vm_running) {
3407 cpu_enable_ticks();
3408 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003409 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003410 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003411 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003412 }
3413}
3414
bellardbb0c6722004-06-20 12:37:32 +00003415/* reset/shutdown handler */
3416
3417typedef struct QEMUResetEntry {
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003418 TAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003419 QEMUResetHandler *func;
3420 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003421} QEMUResetEntry;
3422
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003423static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3424 TAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003425static int reset_requested;
3426static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003427static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003428static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003429static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003430
aurel32cf7a2fe2008-03-18 06:53:05 +00003431int qemu_shutdown_requested(void)
3432{
3433 int r = shutdown_requested;
3434 shutdown_requested = 0;
3435 return r;
3436}
3437
3438int qemu_reset_requested(void)
3439{
3440 int r = reset_requested;
3441 reset_requested = 0;
3442 return r;
3443}
3444
3445int qemu_powerdown_requested(void)
3446{
3447 int r = powerdown_requested;
3448 powerdown_requested = 0;
3449 return r;
3450}
3451
aliguorie5689022009-04-24 18:03:54 +00003452static int qemu_debug_requested(void)
3453{
3454 int r = debug_requested;
3455 debug_requested = 0;
3456 return r;
3457}
3458
aliguori6e29f5d2009-04-24 18:04:02 +00003459static int qemu_vmstop_requested(void)
3460{
3461 int r = vmstop_requested;
3462 vmstop_requested = 0;
3463 return r;
3464}
3465
3466static void do_vm_stop(int reason)
3467{
3468 if (vm_running) {
3469 cpu_disable_ticks();
3470 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003471 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003472 vm_state_notify(0, reason);
3473 }
3474}
3475
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003476void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003477{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003478 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003479
bellardbb0c6722004-06-20 12:37:32 +00003480 re->func = func;
3481 re->opaque = opaque;
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003482 TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003483}
3484
Jan Kiszkadda9b292009-07-02 00:19:02 +02003485void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003486{
3487 QEMUResetEntry *re;
3488
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003489 TAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003490 if (re->func == func && re->opaque == opaque) {
3491 TAILQ_REMOVE(&reset_handlers, re, entry);
3492 qemu_free(re);
3493 return;
3494 }
3495 }
3496}
3497
3498void qemu_system_reset(void)
3499{
3500 QEMUResetEntry *re, *nre;
3501
3502 /* reset all devices */
3503 TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003504 re->func(re->opaque);
3505 }
3506}
3507
3508void qemu_system_reset_request(void)
3509{
bellardd1beab82006-10-02 19:44:22 +00003510 if (no_reboot) {
3511 shutdown_requested = 1;
3512 } else {
3513 reset_requested = 1;
3514 }
aliguorid9f75a42009-04-24 18:03:11 +00003515 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003516}
3517
3518void qemu_system_shutdown_request(void)
3519{
3520 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003521 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003522}
3523
bellard34751872005-07-02 14:31:34 +00003524void qemu_system_powerdown_request(void)
3525{
3526 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003527 qemu_notify_event();
3528}
3529
aliguorid6dc3d42009-04-24 18:04:07 +00003530#ifdef CONFIG_IOTHREAD
3531static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003532{
aliguorid6dc3d42009-04-24 18:04:07 +00003533 vmstop_requested = reason;
3534 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003535}
aliguorid6dc3d42009-04-24 18:04:07 +00003536#endif
bellardbb0c6722004-06-20 12:37:32 +00003537
aliguori50317c72009-04-24 18:03:29 +00003538#ifndef _WIN32
3539static int io_thread_fd = -1;
3540
3541static void qemu_event_increment(void)
3542{
3543 static const char byte = 0;
3544
3545 if (io_thread_fd == -1)
3546 return;
3547
3548 write(io_thread_fd, &byte, sizeof(byte));
3549}
3550
3551static void qemu_event_read(void *opaque)
3552{
3553 int fd = (unsigned long)opaque;
3554 ssize_t len;
3555
3556 /* Drain the notify pipe */
3557 do {
3558 char buffer[512];
3559 len = read(fd, buffer, sizeof(buffer));
3560 } while ((len == -1 && errno == EINTR) || len > 0);
3561}
3562
3563static int qemu_event_init(void)
3564{
3565 int err;
3566 int fds[2];
3567
3568 err = pipe(fds);
3569 if (err == -1)
3570 return -errno;
3571
3572 err = fcntl_setfl(fds[0], O_NONBLOCK);
3573 if (err < 0)
3574 goto fail;
3575
3576 err = fcntl_setfl(fds[1], O_NONBLOCK);
3577 if (err < 0)
3578 goto fail;
3579
3580 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3581 (void *)(unsigned long)fds[0]);
3582
3583 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003584 return 0;
3585
aliguori50317c72009-04-24 18:03:29 +00003586fail:
3587 close(fds[0]);
3588 close(fds[1]);
3589 return err;
3590}
3591#else
3592HANDLE qemu_event_handle;
3593
3594static void dummy_event_handler(void *opaque)
3595{
3596}
3597
3598static int qemu_event_init(void)
3599{
3600 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3601 if (!qemu_event_handle) {
3602 perror("Failed CreateEvent");
3603 return -1;
3604 }
3605 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3606 return 0;
3607}
3608
3609static void qemu_event_increment(void)
3610{
3611 SetEvent(qemu_event_handle);
3612}
3613#endif
3614
aliguorid6dc3d42009-04-24 18:04:07 +00003615static int cpu_can_run(CPUState *env)
3616{
3617 if (env->stop)
3618 return 0;
3619 if (env->stopped)
3620 return 0;
3621 return 1;
3622}
3623
3624#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003625static int qemu_init_main_loop(void)
3626{
aliguori50317c72009-04-24 18:03:29 +00003627 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003628}
3629
aliguori0bf46a42009-04-24 18:03:41 +00003630void qemu_init_vcpu(void *_env)
3631{
3632 CPUState *env = _env;
3633
3634 if (kvm_enabled())
3635 kvm_init_vcpu(env);
3636 return;
3637}
3638
aliguori8edac962009-04-24 18:03:45 +00003639int qemu_cpu_self(void *env)
3640{
3641 return 1;
3642}
3643
aliguorid6dc3d42009-04-24 18:04:07 +00003644static void resume_all_vcpus(void)
3645{
3646}
3647
3648static void pause_all_vcpus(void)
3649{
3650}
3651
aliguori8edac962009-04-24 18:03:45 +00003652void qemu_cpu_kick(void *env)
3653{
3654 return;
3655}
3656
aliguorid6dc3d42009-04-24 18:04:07 +00003657void qemu_notify_event(void)
3658{
3659 CPUState *env = cpu_single_env;
3660
3661 if (env) {
3662 cpu_exit(env);
3663#ifdef USE_KQEMU
3664 if (env->kqemu_enabled)
3665 kqemu_cpu_interrupt(env);
3666#endif
3667 }
3668}
3669
aliguori48708522009-04-24 18:03:49 +00003670#define qemu_mutex_lock_iothread() do { } while (0)
3671#define qemu_mutex_unlock_iothread() do { } while (0)
3672
aliguori6e29f5d2009-04-24 18:04:02 +00003673void vm_stop(int reason)
3674{
3675 do_vm_stop(reason);
3676}
3677
aliguorid6dc3d42009-04-24 18:04:07 +00003678#else /* CONFIG_IOTHREAD */
3679
3680#include "qemu-thread.h"
3681
3682QemuMutex qemu_global_mutex;
3683static QemuMutex qemu_fair_mutex;
3684
3685static QemuThread io_thread;
3686
3687static QemuThread *tcg_cpu_thread;
3688static QemuCond *tcg_halt_cond;
3689
3690static int qemu_system_ready;
3691/* cpu creation */
3692static QemuCond qemu_cpu_cond;
3693/* system init */
3694static QemuCond qemu_system_cond;
3695static QemuCond qemu_pause_cond;
3696
3697static void block_io_signals(void);
3698static void unblock_io_signals(void);
3699static int tcg_has_work(void);
3700
3701static int qemu_init_main_loop(void)
3702{
3703 int ret;
3704
3705 ret = qemu_event_init();
3706 if (ret)
3707 return ret;
3708
3709 qemu_cond_init(&qemu_pause_cond);
3710 qemu_mutex_init(&qemu_fair_mutex);
3711 qemu_mutex_init(&qemu_global_mutex);
3712 qemu_mutex_lock(&qemu_global_mutex);
3713
3714 unblock_io_signals();
3715 qemu_thread_self(&io_thread);
3716
3717 return 0;
3718}
3719
3720static void qemu_wait_io_event(CPUState *env)
3721{
3722 while (!tcg_has_work())
3723 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3724
3725 qemu_mutex_unlock(&qemu_global_mutex);
3726
3727 /*
3728 * Users of qemu_global_mutex can be starved, having no chance
3729 * to acquire it since this path will get to it first.
3730 * So use another lock to provide fairness.
3731 */
3732 qemu_mutex_lock(&qemu_fair_mutex);
3733 qemu_mutex_unlock(&qemu_fair_mutex);
3734
3735 qemu_mutex_lock(&qemu_global_mutex);
3736 if (env->stop) {
3737 env->stop = 0;
3738 env->stopped = 1;
3739 qemu_cond_signal(&qemu_pause_cond);
3740 }
3741}
3742
3743static int qemu_cpu_exec(CPUState *env);
3744
3745static void *kvm_cpu_thread_fn(void *arg)
3746{
3747 CPUState *env = arg;
3748
3749 block_io_signals();
3750 qemu_thread_self(env->thread);
3751
3752 /* signal CPU creation */
3753 qemu_mutex_lock(&qemu_global_mutex);
3754 env->created = 1;
3755 qemu_cond_signal(&qemu_cpu_cond);
3756
3757 /* and wait for machine initialization */
3758 while (!qemu_system_ready)
3759 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3760
3761 while (1) {
3762 if (cpu_can_run(env))
3763 qemu_cpu_exec(env);
3764 qemu_wait_io_event(env);
3765 }
3766
3767 return NULL;
3768}
3769
3770static void tcg_cpu_exec(void);
3771
3772static void *tcg_cpu_thread_fn(void *arg)
3773{
3774 CPUState *env = arg;
3775
3776 block_io_signals();
3777 qemu_thread_self(env->thread);
3778
3779 /* signal CPU creation */
3780 qemu_mutex_lock(&qemu_global_mutex);
3781 for (env = first_cpu; env != NULL; env = env->next_cpu)
3782 env->created = 1;
3783 qemu_cond_signal(&qemu_cpu_cond);
3784
3785 /* and wait for machine initialization */
3786 while (!qemu_system_ready)
3787 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3788
3789 while (1) {
3790 tcg_cpu_exec();
3791 qemu_wait_io_event(cur_cpu);
3792 }
3793
3794 return NULL;
3795}
3796
3797void qemu_cpu_kick(void *_env)
3798{
3799 CPUState *env = _env;
3800 qemu_cond_broadcast(env->halt_cond);
3801 if (kvm_enabled())
3802 qemu_thread_signal(env->thread, SIGUSR1);
3803}
3804
3805int qemu_cpu_self(void *env)
3806{
3807 return (cpu_single_env != NULL);
3808}
3809
3810static void cpu_signal(int sig)
3811{
3812 if (cpu_single_env)
3813 cpu_exit(cpu_single_env);
3814}
3815
3816static void block_io_signals(void)
3817{
3818 sigset_t set;
3819 struct sigaction sigact;
3820
3821 sigemptyset(&set);
3822 sigaddset(&set, SIGUSR2);
3823 sigaddset(&set, SIGIO);
3824 sigaddset(&set, SIGALRM);
3825 pthread_sigmask(SIG_BLOCK, &set, NULL);
3826
3827 sigemptyset(&set);
3828 sigaddset(&set, SIGUSR1);
3829 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3830
3831 memset(&sigact, 0, sizeof(sigact));
3832 sigact.sa_handler = cpu_signal;
3833 sigaction(SIGUSR1, &sigact, NULL);
3834}
3835
3836static void unblock_io_signals(void)
3837{
3838 sigset_t set;
3839
3840 sigemptyset(&set);
3841 sigaddset(&set, SIGUSR2);
3842 sigaddset(&set, SIGIO);
3843 sigaddset(&set, SIGALRM);
3844 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3845
3846 sigemptyset(&set);
3847 sigaddset(&set, SIGUSR1);
3848 pthread_sigmask(SIG_BLOCK, &set, NULL);
3849}
3850
3851static void qemu_signal_lock(unsigned int msecs)
3852{
3853 qemu_mutex_lock(&qemu_fair_mutex);
3854
3855 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3856 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3857 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3858 break;
3859 }
3860 qemu_mutex_unlock(&qemu_fair_mutex);
3861}
3862
3863static void qemu_mutex_lock_iothread(void)
3864{
3865 if (kvm_enabled()) {
3866 qemu_mutex_lock(&qemu_fair_mutex);
3867 qemu_mutex_lock(&qemu_global_mutex);
3868 qemu_mutex_unlock(&qemu_fair_mutex);
3869 } else
3870 qemu_signal_lock(100);
3871}
3872
3873static void qemu_mutex_unlock_iothread(void)
3874{
3875 qemu_mutex_unlock(&qemu_global_mutex);
3876}
3877
3878static int all_vcpus_paused(void)
3879{
3880 CPUState *penv = first_cpu;
3881
3882 while (penv) {
3883 if (!penv->stopped)
3884 return 0;
3885 penv = (CPUState *)penv->next_cpu;
3886 }
3887
3888 return 1;
3889}
3890
3891static void pause_all_vcpus(void)
3892{
3893 CPUState *penv = first_cpu;
3894
3895 while (penv) {
3896 penv->stop = 1;
3897 qemu_thread_signal(penv->thread, SIGUSR1);
3898 qemu_cpu_kick(penv);
3899 penv = (CPUState *)penv->next_cpu;
3900 }
3901
3902 while (!all_vcpus_paused()) {
3903 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3904 penv = first_cpu;
3905 while (penv) {
3906 qemu_thread_signal(penv->thread, SIGUSR1);
3907 penv = (CPUState *)penv->next_cpu;
3908 }
3909 }
3910}
3911
3912static void resume_all_vcpus(void)
3913{
3914 CPUState *penv = first_cpu;
3915
3916 while (penv) {
3917 penv->stop = 0;
3918 penv->stopped = 0;
3919 qemu_thread_signal(penv->thread, SIGUSR1);
3920 qemu_cpu_kick(penv);
3921 penv = (CPUState *)penv->next_cpu;
3922 }
3923}
3924
3925static void tcg_init_vcpu(void *_env)
3926{
3927 CPUState *env = _env;
3928 /* share a single thread for all cpus with TCG */
3929 if (!tcg_cpu_thread) {
3930 env->thread = qemu_mallocz(sizeof(QemuThread));
3931 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3932 qemu_cond_init(env->halt_cond);
3933 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3934 while (env->created == 0)
3935 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3936 tcg_cpu_thread = env->thread;
3937 tcg_halt_cond = env->halt_cond;
3938 } else {
3939 env->thread = tcg_cpu_thread;
3940 env->halt_cond = tcg_halt_cond;
3941 }
3942}
3943
3944static void kvm_start_vcpu(CPUState *env)
3945{
3946 kvm_init_vcpu(env);
3947 env->thread = qemu_mallocz(sizeof(QemuThread));
3948 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3949 qemu_cond_init(env->halt_cond);
3950 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3951 while (env->created == 0)
3952 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3953}
3954
3955void qemu_init_vcpu(void *_env)
3956{
3957 CPUState *env = _env;
3958
3959 if (kvm_enabled())
3960 kvm_start_vcpu(env);
3961 else
3962 tcg_init_vcpu(env);
3963}
3964
3965void qemu_notify_event(void)
3966{
3967 qemu_event_increment();
3968}
3969
3970void vm_stop(int reason)
3971{
3972 QemuThread me;
3973 qemu_thread_self(&me);
3974
3975 if (!qemu_thread_equal(&me, &io_thread)) {
3976 qemu_system_vmstop_request(reason);
3977 /*
3978 * FIXME: should not return to device code in case
3979 * vm_stop() has been requested.
3980 */
3981 if (cpu_single_env) {
3982 cpu_exit(cpu_single_env);
3983 cpu_single_env->stop = 1;
3984 }
3985 return;
3986 }
3987 do_vm_stop(reason);
3988}
3989
3990#endif
3991
3992
ths877cf882007-04-18 18:11:47 +00003993#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003994static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003995{
3996 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003997 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003998
bellardf3311102006-04-12 20:21:17 +00003999
4000 /* XXX: need to suppress polling by better using win32 events */
4001 ret = 0;
4002 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4003 ret |= pe->func(pe->opaque);
4004 }
thse6b1e552007-04-18 17:56:02 +00004005 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00004006 int err;
4007 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00004008
aliguori56f3a5d2008-10-31 18:07:17 +00004009 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00004010 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4011 if (w->func[ret - WAIT_OBJECT_0])
4012 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00004013
ths5fafdf22007-09-16 21:08:06 +00004014 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00004015 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00004016
thse6b1e552007-04-18 17:56:02 +00004017 /* Check if event is signaled */
4018 ret2 = WaitForSingleObject(w->events[i], 0);
4019 if(ret2 == WAIT_OBJECT_0) {
4020 if (w->func[i])
4021 w->func[i](w->opaque[i]);
4022 } else if (ret2 == WAIT_TIMEOUT) {
4023 } else {
4024 err = GetLastError();
4025 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00004026 }
4027 }
bellarda18e5242006-06-25 17:18:27 +00004028 } else if (ret == WAIT_TIMEOUT) {
4029 } else {
4030 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00004031 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00004032 }
bellardf3311102006-04-12 20:21:17 +00004033 }
aliguori56f3a5d2008-10-31 18:07:17 +00004034
4035 *timeout = 0;
4036}
4037#else
blueswir169d64512008-12-07 19:30:18 +00004038static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004039{
4040}
bellardfd1dff42006-02-01 21:29:26 +00004041#endif
aliguori56f3a5d2008-10-31 18:07:17 +00004042
4043void main_loop_wait(int timeout)
4044{
4045 IOHandlerRecord *ioh;
4046 fd_set rfds, wfds, xfds;
4047 int ret, nfds;
4048 struct timeval tv;
4049
4050 qemu_bh_update_timeout(&timeout);
4051
4052 host_main_loop_wait(&timeout);
4053
bellardfd1dff42006-02-01 21:29:26 +00004054 /* poll any events */
4055 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00004056 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00004057 FD_ZERO(&rfds);
4058 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00004059 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00004060 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00004061 if (ioh->deleted)
4062 continue;
bellardfd1dff42006-02-01 21:29:26 +00004063 if (ioh->fd_read &&
4064 (!ioh->fd_read_poll ||
4065 ioh->fd_read_poll(ioh->opaque) != 0)) {
4066 FD_SET(ioh->fd, &rfds);
4067 if (ioh->fd > nfds)
4068 nfds = ioh->fd;
4069 }
4070 if (ioh->fd_write) {
4071 FD_SET(ioh->fd, &wfds);
4072 if (ioh->fd > nfds)
4073 nfds = ioh->fd;
4074 }
4075 }
ths3b46e622007-09-17 08:09:54 +00004076
aliguori56f3a5d2008-10-31 18:07:17 +00004077 tv.tv_sec = timeout / 1000;
4078 tv.tv_usec = (timeout % 1000) * 1000;
4079
Jan Kiszkad918f232009-06-24 14:42:30 +02004080 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4081
aliguori48708522009-04-24 18:03:49 +00004082 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00004083 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00004084 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00004085 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004086 IOHandlerRecord **pioh;
4087
4088 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004089 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004090 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004091 }
ths6ab43fd2007-08-25 01:34:19 +00004092 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004093 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004094 }
4095 }
thscafffd42007-02-28 21:59:44 +00004096
4097 /* remove deleted IO handlers */
4098 pioh = &first_io_handler;
4099 while (*pioh) {
4100 ioh = *pioh;
4101 if (ioh->deleted) {
4102 *pioh = ioh->next;
4103 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004104 } else
thscafffd42007-02-28 21:59:44 +00004105 pioh = &ioh->next;
4106 }
bellardfd1dff42006-02-01 21:29:26 +00004107 }
Jan Kiszkad918f232009-06-24 14:42:30 +02004108
4109 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004110
aliguori50317c72009-04-24 18:03:29 +00004111 /* rearm timer, if not periodic */
4112 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4113 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4114 qemu_rearm_alarm_timer(alarm_timer);
4115 }
4116
aliguori357c6922008-11-25 17:26:09 +00004117 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004118 if (vm_running) {
4119 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4120 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4121 qemu_get_clock(vm_clock));
4122 }
aliguori357c6922008-11-25 17:26:09 +00004123
4124 /* real time timers */
4125 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4126 qemu_get_clock(rt_clock));
4127
pbrook423f0742007-05-23 00:06:54 +00004128 /* Check bottom-halves last in case any of the earlier events triggered
4129 them. */
4130 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004131
bellard5905b2e2004-08-01 21:53:26 +00004132}
4133
aliguori43b96852009-04-24 18:03:33 +00004134static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004135{
aliguori43b96852009-04-24 18:03:33 +00004136 int ret;
bellard89bfc102006-02-08 22:46:31 +00004137#ifdef CONFIG_PROFILER
4138 int64_t ti;
4139#endif
aliguori43b96852009-04-24 18:03:33 +00004140
4141#ifdef CONFIG_PROFILER
4142 ti = profile_getclock();
4143#endif
4144 if (use_icount) {
4145 int64_t count;
4146 int decr;
4147 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4148 env->icount_decr.u16.low = 0;
4149 env->icount_extra = 0;
4150 count = qemu_next_deadline();
4151 count = (count + (1 << icount_time_shift) - 1)
4152 >> icount_time_shift;
4153 qemu_icount += count;
4154 decr = (count > 0xffff) ? 0xffff : count;
4155 count -= decr;
4156 env->icount_decr.u16.low = decr;
4157 env->icount_extra = count;
4158 }
4159 ret = cpu_exec(env);
4160#ifdef CONFIG_PROFILER
4161 qemu_time += profile_getclock() - ti;
4162#endif
4163 if (use_icount) {
4164 /* Fold pending instructions back into the
4165 instruction counter, and clear the interrupt flag. */
4166 qemu_icount -= (env->icount_decr.u16.low
4167 + env->icount_extra);
4168 env->icount_decr.u32 = 0;
4169 env->icount_extra = 0;
4170 }
4171 return ret;
4172}
4173
aliguorie6e35b12009-04-24 18:03:57 +00004174static void tcg_cpu_exec(void)
4175{
aliguorid6dc3d42009-04-24 18:04:07 +00004176 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004177
4178 if (next_cpu == NULL)
4179 next_cpu = first_cpu;
4180 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4181 CPUState *env = cur_cpu = next_cpu;
4182
4183 if (!vm_running)
4184 break;
4185 if (timer_alarm_pending) {
4186 timer_alarm_pending = 0;
4187 break;
4188 }
aliguorid6dc3d42009-04-24 18:04:07 +00004189 if (cpu_can_run(env))
4190 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004191 if (ret == EXCP_DEBUG) {
4192 gdb_set_stop_cpu(env);
4193 debug_requested = 1;
4194 break;
4195 }
4196 }
4197}
4198
aliguori43b96852009-04-24 18:03:33 +00004199static int cpu_has_work(CPUState *env)
4200{
aliguorid6dc3d42009-04-24 18:04:07 +00004201 if (env->stop)
4202 return 1;
4203 if (env->stopped)
4204 return 0;
aliguori43b96852009-04-24 18:03:33 +00004205 if (!env->halted)
4206 return 1;
4207 if (qemu_cpu_has_work(env))
4208 return 1;
4209 return 0;
4210}
4211
4212static int tcg_has_work(void)
4213{
bellard6a00d602005-11-21 23:25:50 +00004214 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004215
aliguori43b96852009-04-24 18:03:33 +00004216 for (env = first_cpu; env != NULL; env = env->next_cpu)
4217 if (cpu_has_work(env))
4218 return 1;
4219 return 0;
4220}
bellard15a76442005-11-23 21:01:03 +00004221
aliguori43b96852009-04-24 18:03:33 +00004222static int qemu_calculate_timeout(void)
4223{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004224#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004225 int timeout;
bellard15a76442005-11-23 21:01:03 +00004226
aliguori43b96852009-04-24 18:03:33 +00004227 if (!vm_running)
4228 timeout = 5000;
4229 else if (tcg_has_work())
4230 timeout = 0;
4231 else if (!use_icount)
4232 timeout = 5000;
4233 else {
4234 /* XXX: use timeout computed from timers */
4235 int64_t add;
4236 int64_t delta;
4237 /* Advance virtual time to the next event. */
4238 if (use_icount == 1) {
4239 /* When not using an adaptive execution frequency
4240 we tend to get badly out of sync with real time,
4241 so just delay for a reasonable amount of time. */
4242 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004243 } else {
aliguori43b96852009-04-24 18:03:33 +00004244 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004245 }
aliguori43b96852009-04-24 18:03:33 +00004246 if (delta > 0) {
4247 /* If virtual time is ahead of real time then just
4248 wait for IO. */
4249 timeout = (delta / 1000000) + 1;
4250 } else {
4251 /* Wait for either IO to occur or the next
4252 timer event. */
4253 add = qemu_next_deadline();
4254 /* We advance the timer before checking for IO.
4255 Limit the amount we advance so that early IO
4256 activity won't get the guest too far ahead. */
4257 if (add > 10000000)
4258 add = 10000000;
4259 delta += add;
4260 add = (add + (1 << icount_time_shift) - 1)
4261 >> icount_time_shift;
4262 qemu_icount += add;
4263 timeout = delta / 1000000;
4264 if (timeout < 0)
4265 timeout = 0;
4266 }
bellardb4608c02003-06-27 17:34:32 +00004267 }
aliguori43b96852009-04-24 18:03:33 +00004268
4269 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004270#else /* CONFIG_IOTHREAD */
4271 return 1000;
4272#endif
aliguori43b96852009-04-24 18:03:33 +00004273}
4274
4275static int vm_can_run(void)
4276{
4277 if (powerdown_requested)
4278 return 0;
4279 if (reset_requested)
4280 return 0;
4281 if (shutdown_requested)
4282 return 0;
aliguorie5689022009-04-24 18:03:54 +00004283 if (debug_requested)
4284 return 0;
aliguori43b96852009-04-24 18:03:33 +00004285 return 1;
4286}
4287
4288static void main_loop(void)
4289{
aliguori6e29f5d2009-04-24 18:04:02 +00004290 int r;
aliguori43b96852009-04-24 18:03:33 +00004291
aliguorid6dc3d42009-04-24 18:04:07 +00004292#ifdef CONFIG_IOTHREAD
4293 qemu_system_ready = 1;
4294 qemu_cond_broadcast(&qemu_system_cond);
4295#endif
4296
aliguori6e29f5d2009-04-24 18:04:02 +00004297 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004298 do {
4299#ifdef CONFIG_PROFILER
4300 int64_t ti;
4301#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004302#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004303 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004304#endif
aliguori43b96852009-04-24 18:03:33 +00004305#ifdef CONFIG_PROFILER
4306 ti = profile_getclock();
4307#endif
4308 main_loop_wait(qemu_calculate_timeout());
4309#ifdef CONFIG_PROFILER
4310 dev_time += profile_getclock() - ti;
4311#endif
aliguorie5689022009-04-24 18:03:54 +00004312 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004313
aliguorie5689022009-04-24 18:03:54 +00004314 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004315 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004316 if (qemu_shutdown_requested()) {
4317 if (no_shutdown) {
4318 vm_stop(0);
4319 no_shutdown = 0;
4320 } else
4321 break;
4322 }
aliguorid6dc3d42009-04-24 18:04:07 +00004323 if (qemu_reset_requested()) {
4324 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004325 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004326 resume_all_vcpus();
4327 }
aliguori43b96852009-04-24 18:03:33 +00004328 if (qemu_powerdown_requested())
4329 qemu_system_powerdown();
aliguori6e29f5d2009-04-24 18:04:02 +00004330 if ((r = qemu_vmstop_requested()))
4331 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004332 }
aliguorid6dc3d42009-04-24 18:04:07 +00004333 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004334}
4335
pbrook9bd7e6d2009-04-07 22:58:45 +00004336static void version(void)
4337{
pbrook4a19f1e2009-04-07 23:17:49 +00004338 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004339}
4340
ths15f82202007-06-29 23:26:08 +00004341static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004342{
pbrook9bd7e6d2009-04-07 22:58:45 +00004343 version();
4344 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004345 "\n"
bellarda20dd502003-09-30 21:07:02 +00004346 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004347 "\n"
blueswir15824d652009-03-28 06:44:27 +00004348#define DEF(option, opt_arg, opt_enum, opt_help) \
4349 opt_help
4350#define DEFHEADING(text) stringify(text) "\n"
4351#include "qemu-options.h"
4352#undef DEF
4353#undef DEFHEADING
4354#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004355 "\n"
bellard82c643f2004-07-14 17:28:13 +00004356 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004357 "ctrl-alt-f toggle full screen\n"
4358 "ctrl-alt-n switch to virtual console 'n'\n"
4359 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004360 "\n"
4361 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4362 ,
bellard0db63472003-10-27 21:37:46 +00004363 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004364 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004365#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004366 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004367 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004368#endif
bellard6e44ba72004-01-18 21:56:49 +00004369 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004370 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004371 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004372}
4373
bellardcd6f1162004-05-13 22:02:20 +00004374#define HAS_ARG 0x0001
4375
4376enum {
blueswir15824d652009-03-28 06:44:27 +00004377#define DEF(option, opt_arg, opt_enum, opt_help) \
4378 opt_enum,
4379#define DEFHEADING(text)
4380#include "qemu-options.h"
4381#undef DEF
4382#undef DEFHEADING
4383#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004384};
4385
4386typedef struct QEMUOption {
4387 const char *name;
4388 int flags;
4389 int index;
4390} QEMUOption;
4391
blueswir1dbed7e42008-10-01 19:38:09 +00004392static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004393 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004394#define DEF(option, opt_arg, opt_enum, opt_help) \
4395 { option, opt_arg, opt_enum },
4396#define DEFHEADING(text)
4397#include "qemu-options.h"
4398#undef DEF
4399#undef DEFHEADING
4400#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004401 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004402};
4403
bellard1d14ffa2005-10-30 18:58:22 +00004404#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004405struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004406#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004407#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004408 {
4409 "pcspk",
4410 "PC speaker",
4411 0,
4412 1,
4413 { .init_isa = pcspk_audio_init }
4414 },
4415#endif
malc4c9b53e2009-01-09 10:46:34 +00004416
4417#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004418 {
4419 "sb16",
4420 "Creative Sound Blaster 16",
4421 0,
4422 1,
4423 { .init_isa = SB16_init }
4424 },
malc4c9b53e2009-01-09 10:46:34 +00004425#endif
bellard6a36d842005-12-18 20:34:32 +00004426
malccc53d262008-06-13 10:48:22 +00004427#ifdef CONFIG_CS4231A
4428 {
4429 "cs4231a",
4430 "CS4231A",
4431 0,
4432 1,
4433 { .init_isa = cs4231a_init }
4434 },
4435#endif
4436
bellard6a36d842005-12-18 20:34:32 +00004437#ifdef CONFIG_ADLIB
4438 {
4439 "adlib",
4440#ifdef HAS_YMF262
4441 "Yamaha YMF262 (OPL3)",
4442#else
4443 "Yamaha YM3812 (OPL2)",
4444#endif
4445 0,
4446 1,
4447 { .init_isa = Adlib_init }
4448 },
4449#endif
4450
4451#ifdef CONFIG_GUS
4452 {
4453 "gus",
4454 "Gravis Ultrasound GF1",
4455 0,
4456 1,
4457 { .init_isa = GUS_init }
4458 },
4459#endif
4460
malc4c9b53e2009-01-09 10:46:34 +00004461#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004462 {
4463 "ac97",
4464 "Intel 82801AA AC97 Audio",
4465 0,
4466 0,
4467 { .init_pci = ac97_init }
4468 },
malc4c9b53e2009-01-09 10:46:34 +00004469#endif
balroge5c9a132008-01-14 04:27:55 +00004470
malc4c9b53e2009-01-09 10:46:34 +00004471#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004472 {
4473 "es1370",
4474 "ENSONIQ AudioPCI ES1370",
4475 0,
4476 0,
4477 { .init_pci = es1370_init }
4478 },
balrogb00052e2007-04-30 02:22:06 +00004479#endif
bellard6a36d842005-12-18 20:34:32 +00004480
malc4c9b53e2009-01-09 10:46:34 +00004481#endif /* HAS_AUDIO_CHOICE */
4482
bellard6a36d842005-12-18 20:34:32 +00004483 { NULL, NULL, 0, 0, { NULL } }
4484};
4485
bellard1d14ffa2005-10-30 18:58:22 +00004486static void select_soundhw (const char *optarg)
4487{
bellard6a36d842005-12-18 20:34:32 +00004488 struct soundhw *c;
4489
bellard1d14ffa2005-10-30 18:58:22 +00004490 if (*optarg == '?') {
4491 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004492
bellard1d14ffa2005-10-30 18:58:22 +00004493 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004494 for (c = soundhw; c->name; ++c) {
4495 printf ("%-11s %s\n", c->name, c->descr);
4496 }
4497 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004498 exit (*optarg != '?');
4499 }
4500 else {
bellard6a36d842005-12-18 20:34:32 +00004501 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004502 const char *p;
4503 char *e;
4504 int bad_card = 0;
4505
bellard6a36d842005-12-18 20:34:32 +00004506 if (!strcmp (optarg, "all")) {
4507 for (c = soundhw; c->name; ++c) {
4508 c->enabled = 1;
4509 }
4510 return;
4511 }
bellard1d14ffa2005-10-30 18:58:22 +00004512
bellard6a36d842005-12-18 20:34:32 +00004513 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004514 while (*p) {
4515 e = strchr (p, ',');
4516 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004517
4518 for (c = soundhw; c->name; ++c) {
4519 if (!strncmp (c->name, p, l)) {
4520 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004521 break;
4522 }
4523 }
bellard6a36d842005-12-18 20:34:32 +00004524
4525 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004526 if (l > 80) {
4527 fprintf (stderr,
4528 "Unknown sound card name (too big to show)\n");
4529 }
4530 else {
4531 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4532 (int) l, p);
4533 }
4534 bad_card = 1;
4535 }
4536 p += l + (e != NULL);
4537 }
4538
4539 if (bad_card)
4540 goto show_valid_cards;
4541 }
4542}
4543#endif
4544
malc3893c122008-09-28 00:42:05 +00004545static void select_vgahw (const char *p)
4546{
4547 const char *opts;
4548
aliguori28b85ed2009-04-22 15:19:48 +00004549 cirrus_vga_enabled = 0;
4550 std_vga_enabled = 0;
4551 vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +00004552 xenfb_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004553 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004554 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004555 } else if (strstart(p, "cirrus", &opts)) {
4556 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004557 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004558 vmsvga_enabled = 1;
aliguori94909d92009-04-22 15:19:53 +00004559 } else if (strstart(p, "xenfb", &opts)) {
4560 xenfb_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004561 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004562 invalid_vga:
4563 fprintf(stderr, "Unknown vga type: %s\n", p);
4564 exit(1);
4565 }
malccb5a7aa2008-09-28 00:42:12 +00004566 while (*opts) {
4567 const char *nextopt;
4568
4569 if (strstart(opts, ",retrace=", &nextopt)) {
4570 opts = nextopt;
4571 if (strstart(opts, "dumb", &nextopt))
4572 vga_retrace_method = VGA_RETRACE_DUMB;
4573 else if (strstart(opts, "precise", &nextopt))
4574 vga_retrace_method = VGA_RETRACE_PRECISE;
4575 else goto invalid_vga;
4576 } else goto invalid_vga;
4577 opts = nextopt;
4578 }
malc3893c122008-09-28 00:42:05 +00004579}
4580
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004581#ifdef TARGET_I386
4582static int balloon_parse(const char *arg)
4583{
4584 char buf[128];
4585 const char *p;
4586
4587 if (!strcmp(arg, "none")) {
4588 virtio_balloon = 0;
4589 } else if (!strncmp(arg, "virtio", 6)) {
4590 virtio_balloon = 1;
4591 if (arg[6] == ',') {
4592 p = arg + 7;
4593 if (get_param_value(buf, sizeof(buf), "addr", p)) {
4594 virtio_balloon_devaddr = strdup(buf);
4595 }
4596 }
4597 } else {
4598 return -1;
4599 }
4600 return 0;
4601}
4602#endif
4603
bellard3587d7e2006-06-26 20:03:44 +00004604#ifdef _WIN32
4605static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4606{
4607 exit(STATUS_CONTROL_C_EXIT);
4608 return TRUE;
4609}
4610#endif
4611
aliguoric4be29f2009-04-17 18:58:14 +00004612int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004613{
4614 int ret;
4615
4616 if(strlen(str) != 36)
4617 return -1;
4618
4619 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4620 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4621 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4622
4623 if(ret != 16)
4624 return -1;
4625
aliguorib6f6e3d2009-04-17 18:59:56 +00004626#ifdef TARGET_I386
4627 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4628#endif
4629
blueswir18fcb1b92008-09-18 18:29:08 +00004630 return 0;
4631}
4632
bellard7c9d8e02005-11-15 22:16:05 +00004633#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004634
aliguori5b08fc12008-08-21 20:08:03 +00004635#ifndef _WIN32
4636
4637static void termsig_handler(int signal)
4638{
4639 qemu_system_shutdown_request();
4640}
4641
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004642static void sigchld_handler(int signal)
4643{
4644 waitpid(-1, NULL, WNOHANG);
4645}
4646
4647static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004648{
4649 struct sigaction act;
4650
4651 memset(&act, 0, sizeof(act));
4652 act.sa_handler = termsig_handler;
4653 sigaction(SIGINT, &act, NULL);
4654 sigaction(SIGHUP, &act, NULL);
4655 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004656
4657 act.sa_handler = sigchld_handler;
4658 act.sa_flags = SA_NOCLDSTOP;
4659 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004660}
4661
4662#endif
4663
Paul Brook5cea8592009-05-30 00:52:44 +01004664#ifdef _WIN32
4665/* Look for support files in the same directory as the executable. */
4666static char *find_datadir(const char *argv0)
4667{
4668 char *p;
4669 char buf[MAX_PATH];
4670 DWORD len;
4671
4672 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4673 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004674 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004675 }
4676
4677 buf[len] = 0;
4678 p = buf + len - 1;
4679 while (p != buf && *p != '\\')
4680 p--;
4681 *p = 0;
4682 if (access(buf, R_OK) == 0) {
4683 return qemu_strdup(buf);
4684 }
4685 return NULL;
4686}
4687#else /* !_WIN32 */
4688
4689/* Find a likely location for support files using the location of the binary.
4690 For installed binaries this will be "$bindir/../share/qemu". When
4691 running from the build tree this will be "$bindir/../pc-bios". */
4692#define SHARE_SUFFIX "/share/qemu"
4693#define BUILD_SUFFIX "/pc-bios"
4694static char *find_datadir(const char *argv0)
4695{
4696 char *dir;
4697 char *p = NULL;
4698 char *res;
4699#ifdef PATH_MAX
4700 char buf[PATH_MAX];
4701#endif
Blue Swirl3a417592009-06-09 19:12:21 +00004702 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004703
4704#if defined(__linux__)
4705 {
4706 int len;
4707 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4708 if (len > 0) {
4709 buf[len] = 0;
4710 p = buf;
4711 }
4712 }
4713#elif defined(__FreeBSD__)
4714 {
4715 int len;
4716 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4717 if (len > 0) {
4718 buf[len] = 0;
4719 p = buf;
4720 }
4721 }
4722#endif
4723 /* If we don't have any way of figuring out the actual executable
4724 location then try argv[0]. */
4725 if (!p) {
4726#ifdef PATH_MAX
4727 p = buf;
4728#endif
4729 p = realpath(argv0, p);
4730 if (!p) {
4731 return NULL;
4732 }
4733 }
4734 dir = dirname(p);
4735 dir = dirname(dir);
4736
Blue Swirl3a417592009-06-09 19:12:21 +00004737 max_len = strlen(dir) +
4738 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4739 res = qemu_mallocz(max_len);
4740 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004741 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004742 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004743 if (access(res, R_OK)) {
4744 qemu_free(res);
4745 res = NULL;
4746 }
4747 }
4748#ifndef PATH_MAX
4749 free(p);
4750#endif
4751 return res;
4752}
4753#undef SHARE_SUFFIX
4754#undef BUILD_SUFFIX
4755#endif
4756
4757char *qemu_find_file(int type, const char *name)
4758{
4759 int len;
4760 const char *subdir;
4761 char *buf;
4762
4763 /* If name contains path separators then try it as a straight path. */
4764 if ((strchr(name, '/') || strchr(name, '\\'))
4765 && access(name, R_OK) == 0) {
4766 return strdup(name);
4767 }
4768 switch (type) {
4769 case QEMU_FILE_TYPE_BIOS:
4770 subdir = "";
4771 break;
4772 case QEMU_FILE_TYPE_KEYMAP:
4773 subdir = "keymaps/";
4774 break;
4775 default:
4776 abort();
4777 }
4778 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4779 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004780 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004781 if (access(buf, R_OK)) {
4782 qemu_free(buf);
4783 return NULL;
4784 }
4785 return buf;
4786}
4787
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004788struct device_config {
4789 enum {
4790 DEV_GENERIC, /* -device */
4791 DEV_USB, /* -usbdevice */
4792 DEV_BT, /* -bt */
4793 } type;
4794 const char *cmdline;
4795 TAILQ_ENTRY(device_config) next;
4796};
4797TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs);
4798
4799static void add_device_config(int type, const char *cmdline)
4800{
4801 struct device_config *conf;
4802
4803 conf = qemu_mallocz(sizeof(*conf));
4804 conf->type = type;
4805 conf->cmdline = cmdline;
4806 TAILQ_INSERT_TAIL(&device_configs, conf, next);
4807}
4808
4809static int foreach_device_config(int type, int (*func)(const char *cmdline))
4810{
4811 struct device_config *conf;
4812 int rc;
4813
4814 TAILQ_FOREACH(conf, &device_configs, next) {
4815 if (conf->type != type)
4816 continue;
4817 rc = func(conf->cmdline);
4818 if (0 != rc)
4819 return rc;
4820 }
4821 return 0;
4822}
4823
4824static int generic_parse(const char *cmdline)
4825{
4826 DeviceState *dev;
4827
4828 dev = qdev_device_add(cmdline);
4829 if (!dev)
4830 return -1;
4831 return 0;
4832}
4833
malc902b3d52008-12-10 19:18:40 +00004834int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004835{
aliguori59030a82009-04-05 18:43:41 +00004836 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004837 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004838 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004839 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004840 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004841 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004842 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004843 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004844 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004845 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004846 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004847 int nb_net_clients;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004848 QemuOpts *hda_opts = NULL;
bellardcd6f1162004-05-13 22:02:20 +00004849 int optind;
4850 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004851 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004852 const char *monitor_device;
4853 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004854 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004855 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004856 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004857 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4858 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004859 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004860 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004861 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004862#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004863 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004864#endif
bellard26a5f132008-05-28 12:30:31 +00004865 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004866 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004867 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004868#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004869 int fd = 0;
4870 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004871 const char *chroot_dir = NULL;
4872 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004873#endif
aliguori268a3622009-04-21 22:30:27 +00004874 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004875 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004876
malc902b3d52008-12-10 19:18:40 +00004877 qemu_cache_utils_init(envp);
4878
bellard0bd48852005-11-11 00:00:47 +00004879 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004880#ifndef _WIN32
4881 {
4882 struct sigaction act;
4883 sigfillset(&act.sa_mask);
4884 act.sa_flags = 0;
4885 act.sa_handler = SIG_IGN;
4886 sigaction(SIGPIPE, &act, NULL);
4887 }
bellard3587d7e2006-06-26 20:03:44 +00004888#else
4889 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004890 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4891 QEMU to run on a single CPU */
4892 {
4893 HANDLE h;
4894 DWORD mask, smask;
4895 int i;
4896 h = GetCurrentProcess();
4897 if (GetProcessAffinityMask(h, &mask, &smask)) {
4898 for(i = 0; i < 32; i++) {
4899 if (mask & (1 << i))
4900 break;
4901 }
4902 if (i != 32) {
4903 mask = 1 << i;
4904 SetProcessAffinityMask(h, mask);
4905 }
4906 }
4907 }
bellard67b915a2004-03-31 23:37:16 +00004908#endif
bellardbe995c22006-06-25 16:25:21 +00004909
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004910 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004911 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004912 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004913 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004914 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004915 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004916 kernel_filename = NULL;
4917 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004918 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004919 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004920 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004921
aurel32c75a8232008-05-04 00:50:34 +00004922 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004923 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004924 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004925 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004926
aliguori8290edd2009-02-27 20:14:29 +00004927 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004928 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004929 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004930 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004931
aliguori1b8fc812009-02-27 20:01:39 +00004932 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004933 virtio_consoles[i] = NULL;
4934 virtio_console_index = 0;
4935
aliguori268a3622009-04-21 22:30:27 +00004936 for (i = 0; i < MAX_NODES; i++) {
4937 node_mem[i] = 0;
4938 node_cpumask[i] = 0;
4939 }
4940
bellard7c9d8e02005-11-15 22:16:05 +00004941 nb_net_clients = 0;
aliguori268a3622009-04-21 22:30:27 +00004942 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004943 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004944
bellard26a5f132008-05-28 12:30:31 +00004945 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004946 autostart= 1;
4947
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004948 register_watchdogs();
4949
bellardcd6f1162004-05-13 22:02:20 +00004950 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004951 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004952 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004953 break;
bellardcd6f1162004-05-13 22:02:20 +00004954 r = argv[optind];
4955 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004956 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004957 } else {
4958 const QEMUOption *popt;
4959
4960 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004961 /* Treat --foo the same as -foo. */
4962 if (r[1] == '-')
4963 r++;
bellardcd6f1162004-05-13 22:02:20 +00004964 popt = qemu_options;
4965 for(;;) {
4966 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004967 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004968 argv[0], r);
4969 exit(1);
4970 }
4971 if (!strcmp(popt->name, r + 1))
4972 break;
4973 popt++;
4974 }
4975 if (popt->flags & HAS_ARG) {
4976 if (optind >= argc) {
4977 fprintf(stderr, "%s: option '%s' requires an argument\n",
4978 argv[0], r);
4979 exit(1);
4980 }
4981 optarg = argv[optind++];
4982 } else {
4983 optarg = NULL;
4984 }
4985
4986 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004987 case QEMU_OPTION_M:
4988 machine = find_machine(optarg);
4989 if (!machine) {
4990 QEMUMachine *m;
4991 printf("Supported machines are:\n");
4992 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004993 if (m->alias)
4994 printf("%-10s %s (alias of %s)\n",
4995 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004996 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004997 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004998 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004999 }
ths15f82202007-06-29 23:26:08 +00005000 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00005001 }
5002 break;
j_mayer94fc95c2007-03-05 19:44:02 +00005003 case QEMU_OPTION_cpu:
5004 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00005005 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00005006/* XXX: implement xxx_cpu_list for targets that still miss it */
5007#if defined(cpu_list)
5008 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00005009#endif
ths15f82202007-06-29 23:26:08 +00005010 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005011 } else {
5012 cpu_model = optarg;
5013 }
5014 break;
bellardcd6f1162004-05-13 22:02:20 +00005015 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005016 initrd_filename = optarg;
5017 break;
bellardcd6f1162004-05-13 22:02:20 +00005018 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005019 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005020 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005021 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005022 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005023 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005024 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005025 translation == BIOS_ATA_TRANSLATION_LBA ?
5026 ",trans=lba" :
5027 translation == BIOS_ATA_TRANSLATION_NONE ?
5028 ",trans=none" : "");
5029 break;
bellardcd6f1162004-05-13 22:02:20 +00005030 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005031 case QEMU_OPTION_hdc:
5032 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005033 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005034 break;
thse4bcb142007-12-02 04:51:10 +00005035 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005036 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005037 break;
balrog3e3d5812007-04-30 02:09:25 +00005038 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005039 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005040 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005041 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005042 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005043 break;
j_mayer86f55662007-04-24 06:52:59 +00005044 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005045 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005046 break;
bellardcd6f1162004-05-13 22:02:20 +00005047 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005048 snapshot = 1;
5049 break;
bellardcd6f1162004-05-13 22:02:20 +00005050 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005051 {
bellard330d0412003-07-26 18:11:40 +00005052 const char *p;
5053 p = optarg;
5054 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005055 if (cyls < 1 || cyls > 16383)
5056 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005057 if (*p != ',')
5058 goto chs_fail;
5059 p++;
5060 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005061 if (heads < 1 || heads > 16)
5062 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005063 if (*p != ',')
5064 goto chs_fail;
5065 p++;
5066 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005067 if (secs < 1 || secs > 63)
5068 goto chs_fail;
5069 if (*p == ',') {
5070 p++;
5071 if (!strcmp(p, "none"))
5072 translation = BIOS_ATA_TRANSLATION_NONE;
5073 else if (!strcmp(p, "lba"))
5074 translation = BIOS_ATA_TRANSLATION_LBA;
5075 else if (!strcmp(p, "auto"))
5076 translation = BIOS_ATA_TRANSLATION_AUTO;
5077 else
5078 goto chs_fail;
5079 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005080 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005081 fprintf(stderr, "qemu: invalid physical CHS format\n");
5082 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005083 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005084 if (hda_opts != NULL) {
5085 char num[16];
5086 snprintf(num, sizeof(num), "%d", cyls);
5087 qemu_opt_set(hda_opts, "cyls", num);
5088 snprintf(num, sizeof(num), "%d", heads);
5089 qemu_opt_set(hda_opts, "heads", num);
5090 snprintf(num, sizeof(num), "%d", secs);
5091 qemu_opt_set(hda_opts, "secs", num);
5092 if (translation == BIOS_ATA_TRANSLATION_LBA)
5093 qemu_opt_set(hda_opts, "trans", "lba");
5094 if (translation == BIOS_ATA_TRANSLATION_NONE)
5095 qemu_opt_set(hda_opts, "trans", "none");
5096 }
bellard330d0412003-07-26 18:11:40 +00005097 }
5098 break;
aliguori268a3622009-04-21 22:30:27 +00005099 case QEMU_OPTION_numa:
5100 if (nb_numa_nodes >= MAX_NODES) {
5101 fprintf(stderr, "qemu: too many NUMA nodes\n");
5102 exit(1);
5103 }
5104 numa_add(optarg);
5105 break;
bellardcd6f1162004-05-13 22:02:20 +00005106 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005107 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005108 break;
balrog4d3b6f62008-02-10 16:33:14 +00005109#ifdef CONFIG_CURSES
5110 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005111 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005112 break;
5113#endif
balroga171fe32007-04-30 01:48:07 +00005114 case QEMU_OPTION_portrait:
5115 graphic_rotate = 1;
5116 break;
bellardcd6f1162004-05-13 22:02:20 +00005117 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005118 kernel_filename = optarg;
5119 break;
bellardcd6f1162004-05-13 22:02:20 +00005120 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005121 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005122 break;
bellardcd6f1162004-05-13 22:02:20 +00005123 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005124 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005125 break;
bellardcd6f1162004-05-13 22:02:20 +00005126 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005127 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005128 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005129 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005130 };
5131 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005132 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005133 int legacy = 0;
5134
5135 if (!strchr(optarg, '=')) {
5136 legacy = 1;
5137 pstrcpy(buf, sizeof(buf), optarg);
5138 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5139 fprintf(stderr,
5140 "qemu: unknown boot parameter '%s' in '%s'\n",
5141 buf, optarg);
5142 exit(1);
5143 }
5144
5145 if (legacy ||
5146 get_param_value(buf, sizeof(buf), "order", optarg)) {
5147 boot_devices_bitmap = parse_bootdevices(buf);
5148 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005149 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005150 if (!legacy) {
5151 if (get_param_value(buf, sizeof(buf),
5152 "once", optarg)) {
5153 boot_devices_bitmap |= parse_bootdevices(buf);
5154 standard_boot_devices = qemu_strdup(boot_devices);
5155 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5156 qemu_register_reset(restore_boot_devices,
5157 standard_boot_devices);
5158 }
Jan Kiszka95387492009-07-02 00:19:02 +02005159 if (get_param_value(buf, sizeof(buf),
5160 "menu", optarg)) {
5161 if (!strcmp(buf, "on")) {
5162 boot_menu = 1;
5163 } else if (!strcmp(buf, "off")) {
5164 boot_menu = 0;
5165 } else {
5166 fprintf(stderr,
5167 "qemu: invalid option value '%s'\n",
5168 buf);
5169 exit(1);
5170 }
5171 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005172 }
bellard36b486b2003-11-11 13:36:08 +00005173 }
5174 break;
bellardcd6f1162004-05-13 22:02:20 +00005175 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005176 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005177 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005178 break;
bellard52ca8d62006-06-14 16:03:05 +00005179#ifdef TARGET_I386
5180 case QEMU_OPTION_no_fd_bootchk:
5181 fd_bootchk = 0;
5182 break;
5183#endif
bellard7c9d8e02005-11-15 22:16:05 +00005184 case QEMU_OPTION_net:
5185 if (nb_net_clients >= MAX_NET_CLIENTS) {
5186 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005187 exit(1);
5188 }
pbrookfd5f3932008-03-26 20:55:43 +00005189 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005190 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005191 break;
bellardc7f74642004-08-24 21:57:12 +00005192#ifdef CONFIG_SLIRP
5193 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005194 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005195 break;
ths47d5d012007-02-20 00:05:08 +00005196 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005197 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005198 break;
bellardc94c8d62004-09-13 21:37:34 +00005199#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005200 case QEMU_OPTION_smb:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005201 net_slirp_smb(optarg);
bellard9d728e82004-09-05 23:09:03 +00005202 break;
bellardc94c8d62004-09-13 21:37:34 +00005203#endif
bellard9bf05442004-08-25 22:12:49 +00005204 case QEMU_OPTION_redir:
Jan Kiszkaf3546de2009-06-24 14:42:28 +02005205 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00005206 break;
bellardc7f74642004-08-24 21:57:12 +00005207#endif
balrogdc72ac12008-11-09 00:04:26 +00005208 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005209 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005210 break;
bellard1d14ffa2005-10-30 18:58:22 +00005211#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005212 case QEMU_OPTION_audio_help:
5213 AUD_help ();
5214 exit (0);
5215 break;
5216 case QEMU_OPTION_soundhw:
5217 select_soundhw (optarg);
5218 break;
5219#endif
bellardcd6f1162004-05-13 22:02:20 +00005220 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005221 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005222 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005223 case QEMU_OPTION_version:
5224 version();
5225 exit(0);
5226 break;
aurel3200f82b82008-04-27 21:12:55 +00005227 case QEMU_OPTION_m: {
5228 uint64_t value;
5229 char *ptr;
5230
5231 value = strtoul(optarg, &ptr, 10);
5232 switch (*ptr) {
5233 case 0: case 'M': case 'm':
5234 value <<= 20;
5235 break;
5236 case 'G': case 'g':
5237 value <<= 30;
5238 break;
5239 default:
5240 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005241 exit(1);
5242 }
aurel3200f82b82008-04-27 21:12:55 +00005243
5244 /* On 32-bit hosts, QEMU is limited by virtual address space */
5245 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00005246#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00005247 && HOST_LONG_BITS == 32
5248#endif
5249 ) {
5250 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5251 exit(1);
5252 }
5253 if (value != (uint64_t)(ram_addr_t)value) {
5254 fprintf(stderr, "qemu: ram size too large\n");
5255 exit(1);
5256 }
5257 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005258 break;
aurel3200f82b82008-04-27 21:12:55 +00005259 }
bellardcd6f1162004-05-13 22:02:20 +00005260 case QEMU_OPTION_d:
5261 {
5262 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005263 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005264
bellardcd6f1162004-05-13 22:02:20 +00005265 mask = cpu_str_to_log_mask(optarg);
5266 if (!mask) {
5267 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005268 for(item = cpu_log_items; item->mask != 0; item++) {
5269 printf("%-10s %s\n", item->name, item->help);
5270 }
5271 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005272 }
5273 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005274 }
bellardcd6f1162004-05-13 22:02:20 +00005275 break;
bellardcd6f1162004-05-13 22:02:20 +00005276 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005277 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005278 break;
aliguori59030a82009-04-05 18:43:41 +00005279 case QEMU_OPTION_gdb:
5280 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005281 break;
bellardcd6f1162004-05-13 22:02:20 +00005282 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005283 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005284 break;
j_mayer1192dad2007-10-05 13:08:35 +00005285 case QEMU_OPTION_bios:
5286 bios_name = optarg;
5287 break;
aurel321b530a62009-04-05 20:08:59 +00005288 case QEMU_OPTION_singlestep:
5289 singlestep = 1;
5290 break;
bellardcd6f1162004-05-13 22:02:20 +00005291 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005292 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005293 break;
blueswir15824d652009-03-28 06:44:27 +00005294#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005295 case QEMU_OPTION_k:
5296 keyboard_layout = optarg;
5297 break;
blueswir15824d652009-03-28 06:44:27 +00005298#endif
bellardee22c2f2004-06-03 12:49:50 +00005299 case QEMU_OPTION_localtime:
5300 rtc_utc = 0;
5301 break;
malc3893c122008-09-28 00:42:05 +00005302 case QEMU_OPTION_vga:
5303 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005304 break;
blueswir15824d652009-03-28 06:44:27 +00005305#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005306 case QEMU_OPTION_g:
5307 {
5308 const char *p;
5309 int w, h, depth;
5310 p = optarg;
5311 w = strtol(p, (char **)&p, 10);
5312 if (w <= 0) {
5313 graphic_error:
5314 fprintf(stderr, "qemu: invalid resolution or depth\n");
5315 exit(1);
5316 }
5317 if (*p != 'x')
5318 goto graphic_error;
5319 p++;
5320 h = strtol(p, (char **)&p, 10);
5321 if (h <= 0)
5322 goto graphic_error;
5323 if (*p == 'x') {
5324 p++;
5325 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005326 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005327 depth != 24 && depth != 32)
5328 goto graphic_error;
5329 } else if (*p == '\0') {
5330 depth = graphic_depth;
5331 } else {
5332 goto graphic_error;
5333 }
ths3b46e622007-09-17 08:09:54 +00005334
bellarde9b137c2004-06-21 16:46:10 +00005335 graphic_width = w;
5336 graphic_height = h;
5337 graphic_depth = depth;
5338 }
5339 break;
blueswir15824d652009-03-28 06:44:27 +00005340#endif
ths20d8a3e2007-02-18 17:04:49 +00005341 case QEMU_OPTION_echr:
5342 {
5343 char *r;
5344 term_escape_char = strtol(optarg, &r, 0);
5345 if (r == optarg)
5346 printf("Bad argument to echr\n");
5347 break;
5348 }
bellard82c643f2004-07-14 17:28:13 +00005349 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005350 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005351 break;
5352 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005353 if (serial_device_index >= MAX_SERIAL_PORTS) {
5354 fprintf(stderr, "qemu: too many serial ports\n");
5355 exit(1);
5356 }
pbrookfd5f3932008-03-26 20:55:43 +00005357 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005358 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005359 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005360 case QEMU_OPTION_watchdog:
5361 i = select_watchdog(optarg);
5362 if (i > 0)
5363 exit (i == 1 ? 1 : 0);
5364 break;
5365 case QEMU_OPTION_watchdog_action:
5366 if (select_watchdog_action(optarg) == -1) {
5367 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5368 exit(1);
5369 }
5370 break;
aliguori51ecf132009-01-15 20:06:40 +00005371 case QEMU_OPTION_virtiocon:
5372 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5373 fprintf(stderr, "qemu: too many virtio consoles\n");
5374 exit(1);
5375 }
5376 virtio_consoles[virtio_console_index] = optarg;
5377 virtio_console_index++;
5378 break;
bellard6508fe52005-01-15 12:02:56 +00005379 case QEMU_OPTION_parallel:
5380 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5381 fprintf(stderr, "qemu: too many parallel ports\n");
5382 exit(1);
5383 }
pbrookfd5f3932008-03-26 20:55:43 +00005384 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005385 parallel_device_index++;
5386 break;
bellardd63d3072004-10-03 13:29:03 +00005387 case QEMU_OPTION_loadvm:
5388 loadvm = optarg;
5389 break;
5390 case QEMU_OPTION_full_screen:
5391 full_screen = 1;
5392 break;
ths667acca2006-12-11 02:08:05 +00005393#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005394 case QEMU_OPTION_no_frame:
5395 no_frame = 1;
5396 break;
ths3780e192007-06-21 21:08:02 +00005397 case QEMU_OPTION_alt_grab:
5398 alt_grab = 1;
5399 break;
ths667acca2006-12-11 02:08:05 +00005400 case QEMU_OPTION_no_quit:
5401 no_quit = 1;
5402 break;
aliguori7d957bd2009-01-15 22:14:11 +00005403 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005404 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005405 break;
ths667acca2006-12-11 02:08:05 +00005406#endif
bellardf7cce892004-12-08 22:21:25 +00005407 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005408 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005409 break;
bellarda09db212005-04-30 16:10:35 +00005410#ifdef TARGET_I386
5411 case QEMU_OPTION_win2k_hack:
5412 win2k_install_hack = 1;
5413 break;
aliguori73822ec2009-01-15 20:11:34 +00005414 case QEMU_OPTION_rtc_td_hack:
5415 rtc_td_hack = 1;
5416 break;
aliguori8a92ea22009-02-27 20:12:36 +00005417 case QEMU_OPTION_acpitable:
5418 if(acpi_table_add(optarg) < 0) {
5419 fprintf(stderr, "Wrong acpi table provided\n");
5420 exit(1);
5421 }
5422 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005423 case QEMU_OPTION_smbios:
5424 if(smbios_entry_add(optarg) < 0) {
5425 fprintf(stderr, "Wrong smbios provided\n");
5426 exit(1);
5427 }
5428 break;
bellarda09db212005-04-30 16:10:35 +00005429#endif
blueswir1640f42e2009-04-19 10:18:01 +00005430#ifdef CONFIG_KQEMU
Anthony Liguori52249f02009-07-09 17:28:08 -05005431 case QEMU_OPTION_enable_kqemu:
5432 kqemu_allowed = 1;
bellardd993e022005-02-10 22:00:06 +00005433 break;
bellard89bfc102006-02-08 22:46:31 +00005434 case QEMU_OPTION_kernel_kqemu:
5435 kqemu_allowed = 2;
5436 break;
bellardd993e022005-02-10 22:00:06 +00005437#endif
aliguori7ba1e612008-11-05 16:04:33 +00005438#ifdef CONFIG_KVM
5439 case QEMU_OPTION_enable_kvm:
5440 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00005441#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00005442 kqemu_allowed = 0;
5443#endif
5444 break;
5445#endif
bellardbb36d472005-11-05 14:22:28 +00005446 case QEMU_OPTION_usb:
5447 usb_enabled = 1;
5448 break;
bellarda594cfb2005-11-06 16:13:29 +00005449 case QEMU_OPTION_usbdevice:
5450 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005451 add_device_config(DEV_USB, optarg);
5452 break;
5453 case QEMU_OPTION_device:
5454 add_device_config(DEV_GENERIC, optarg);
bellarda594cfb2005-11-06 16:13:29 +00005455 break;
bellard6a00d602005-11-21 23:25:50 +00005456 case QEMU_OPTION_smp:
Jes Sorensen6be68d72009-07-23 17:03:42 +02005457 {
5458 char *p;
5459 char option[128];
5460 smp_cpus = strtol(optarg, &p, 10);
aliguorib2097002008-10-07 20:39:39 +00005461 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005462 fprintf(stderr, "Invalid number of CPUs\n");
5463 exit(1);
5464 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005465 if (*p++ != ',')
5466 break;
5467 if (get_param_value(option, 128, "maxcpus", p))
5468 max_cpus = strtol(option, NULL, 0);
5469 if (max_cpus < smp_cpus) {
5470 fprintf(stderr, "maxcpus must be equal to or greater than "
5471 "smp\n");
5472 exit(1);
5473 }
5474 if (max_cpus > 255) {
5475 fprintf(stderr, "Unsupported number of maxcpus\n");
5476 exit(1);
5477 }
bellard6a00d602005-11-21 23:25:50 +00005478 break;
Jes Sorensen6be68d72009-07-23 17:03:42 +02005479 }
bellard24236862006-04-30 21:28:36 +00005480 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005481 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005482 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005483 break;
blueswir15824d652009-03-28 06:44:27 +00005484#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005485 case QEMU_OPTION_no_acpi:
5486 acpi_enabled = 0;
5487 break;
aliguori16b29ae2008-12-17 23:28:44 +00005488 case QEMU_OPTION_no_hpet:
5489 no_hpet = 1;
5490 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005491 case QEMU_OPTION_balloon:
5492 if (balloon_parse(optarg) < 0) {
5493 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5494 exit(1);
5495 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005496 break;
blueswir15824d652009-03-28 06:44:27 +00005497#endif
bellardd1beab82006-10-02 19:44:22 +00005498 case QEMU_OPTION_no_reboot:
5499 no_reboot = 1;
5500 break;
aurel32b2f76162008-04-11 21:35:52 +00005501 case QEMU_OPTION_no_shutdown:
5502 no_shutdown = 1;
5503 break;
balrog9467cd42007-05-01 01:34:14 +00005504 case QEMU_OPTION_show_cursor:
5505 cursor_hide = 0;
5506 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005507 case QEMU_OPTION_uuid:
5508 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5509 fprintf(stderr, "Fail to parse UUID string."
5510 " Wrong format.\n");
5511 exit(1);
5512 }
5513 break;
blueswir15824d652009-03-28 06:44:27 +00005514#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005515 case QEMU_OPTION_daemonize:
5516 daemonize = 1;
5517 break;
blueswir15824d652009-03-28 06:44:27 +00005518#endif
ths9ae02552007-01-05 17:39:04 +00005519 case QEMU_OPTION_option_rom:
5520 if (nb_option_roms >= MAX_OPTION_ROMS) {
5521 fprintf(stderr, "Too many option ROMs\n");
5522 exit(1);
5523 }
5524 option_rom[nb_option_roms] = optarg;
5525 nb_option_roms++;
5526 break;
blueswir15824d652009-03-28 06:44:27 +00005527#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005528 case QEMU_OPTION_semihosting:
5529 semihosting_enabled = 1;
5530 break;
blueswir15824d652009-03-28 06:44:27 +00005531#endif
thsc35734b2007-03-19 15:17:08 +00005532 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005533 qemu_name = qemu_strdup(optarg);
5534 {
5535 char *p = strchr(qemu_name, ',');
5536 if (p != NULL) {
5537 *p++ = 0;
5538 if (strncmp(p, "process=", 8)) {
5539 fprintf(stderr, "Unknown subargument %s to -name", p);
5540 exit(1);
5541 }
5542 p += 8;
5543 set_proc_name(p);
5544 }
5545 }
thsc35734b2007-03-19 15:17:08 +00005546 break;
blueswir195efd112008-12-24 20:26:14 +00005547#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005548 case QEMU_OPTION_prom_env:
5549 if (nb_prom_envs >= MAX_PROM_ENVS) {
5550 fprintf(stderr, "Too many prom variables\n");
5551 exit(1);
5552 }
5553 prom_envs[nb_prom_envs] = optarg;
5554 nb_prom_envs++;
5555 break;
5556#endif
balrog2b8f2d42007-07-27 22:08:46 +00005557#ifdef TARGET_ARM
5558 case QEMU_OPTION_old_param:
5559 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005560 break;
balrog2b8f2d42007-07-27 22:08:46 +00005561#endif
thsf3dcfad2007-08-24 01:26:02 +00005562 case QEMU_OPTION_clock:
5563 configure_alarms(optarg);
5564 break;
bellard7e0af5d02007-11-07 16:24:33 +00005565 case QEMU_OPTION_startdate:
5566 {
5567 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005568 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005569 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005570 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005571 } else {
5572 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5573 &tm.tm_year,
5574 &tm.tm_mon,
5575 &tm.tm_mday,
5576 &tm.tm_hour,
5577 &tm.tm_min,
5578 &tm.tm_sec) == 6) {
5579 /* OK */
5580 } else if (sscanf(optarg, "%d-%d-%d",
5581 &tm.tm_year,
5582 &tm.tm_mon,
5583 &tm.tm_mday) == 3) {
5584 tm.tm_hour = 0;
5585 tm.tm_min = 0;
5586 tm.tm_sec = 0;
5587 } else {
5588 goto date_fail;
5589 }
5590 tm.tm_year -= 1900;
5591 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005592 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005593 if (rtc_start_date == -1) {
5594 date_fail:
5595 fprintf(stderr, "Invalid date format. Valid format are:\n"
5596 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5597 exit(1);
5598 }
balrogf6503052008-02-17 11:42:19 +00005599 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005600 }
5601 }
5602 break;
bellard26a5f132008-05-28 12:30:31 +00005603 case QEMU_OPTION_tb_size:
5604 tb_size = strtol(optarg, NULL, 0);
5605 if (tb_size < 0)
5606 tb_size = 0;
5607 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005608 case QEMU_OPTION_icount:
5609 use_icount = 1;
5610 if (strcmp(optarg, "auto") == 0) {
5611 icount_time_shift = -1;
5612 } else {
5613 icount_time_shift = strtol(optarg, NULL, 0);
5614 }
5615 break;
aliguori5bb79102008-10-13 03:12:02 +00005616 case QEMU_OPTION_incoming:
5617 incoming = optarg;
5618 break;
blueswir15824d652009-03-28 06:44:27 +00005619#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005620 case QEMU_OPTION_chroot:
5621 chroot_dir = optarg;
5622 break;
5623 case QEMU_OPTION_runas:
5624 run_as = optarg;
5625 break;
blueswir15824d652009-03-28 06:44:27 +00005626#endif
aliguorie37630c2009-04-22 15:19:10 +00005627#ifdef CONFIG_XEN
5628 case QEMU_OPTION_xen_domid:
5629 xen_domid = atoi(optarg);
5630 break;
5631 case QEMU_OPTION_xen_create:
5632 xen_mode = XEN_CREATE;
5633 break;
5634 case QEMU_OPTION_xen_attach:
5635 xen_mode = XEN_ATTACH;
5636 break;
5637#endif
bellardcd6f1162004-05-13 22:02:20 +00005638 }
bellard0824d6f2003-06-24 13:42:40 +00005639 }
5640 }
bellard330d0412003-07-26 18:11:40 +00005641
Paul Brook5cea8592009-05-30 00:52:44 +01005642 /* If no data_dir is specified then try to find it relative to the
5643 executable path. */
5644 if (!data_dir) {
5645 data_dir = find_datadir(argv[0]);
5646 }
5647 /* If all else fails use the install patch specified when building. */
5648 if (!data_dir) {
5649 data_dir = CONFIG_QEMU_SHAREDIR;
5650 }
5651
blueswir1640f42e2009-04-19 10:18:01 +00005652#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005653 if (kvm_allowed && kqemu_allowed) {
5654 fprintf(stderr,
5655 "You can not enable both KVM and kqemu at the same time\n");
5656 exit(1);
5657 }
5658#endif
5659
Jes Sorensen6be68d72009-07-23 17:03:42 +02005660 /*
5661 * Default to max_cpus = smp_cpus, in case the user doesn't
5662 * specify a max_cpus value.
5663 */
5664 if (!max_cpus)
5665 max_cpus = smp_cpus;
5666
balrog3d878ca2008-10-28 10:59:59 +00005667 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005668 if (smp_cpus > machine->max_cpus) {
5669 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5670 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5671 machine->max_cpus);
5672 exit(1);
5673 }
5674
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005675 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005676 if (serial_device_index == 0)
5677 serial_devices[0] = "stdio";
5678 if (parallel_device_index == 0)
5679 parallel_devices[0] = "null";
5680 if (strncmp(monitor_device, "vc", 2) == 0)
5681 monitor_device = "stdio";
5682 }
5683
ths71e3ceb2006-12-22 02:11:31 +00005684#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005685 if (daemonize) {
5686 pid_t pid;
5687
5688 if (pipe(fds) == -1)
5689 exit(1);
5690
5691 pid = fork();
5692 if (pid > 0) {
5693 uint8_t status;
5694 ssize_t len;
5695
5696 close(fds[1]);
5697
5698 again:
ths93815bc2007-03-19 15:58:31 +00005699 len = read(fds[0], &status, 1);
5700 if (len == -1 && (errno == EINTR))
5701 goto again;
5702
5703 if (len != 1)
5704 exit(1);
5705 else if (status == 1) {
5706 fprintf(stderr, "Could not acquire pidfile\n");
5707 exit(1);
5708 } else
5709 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005710 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005711 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005712
5713 setsid();
5714
5715 pid = fork();
5716 if (pid > 0)
5717 exit(0);
5718 else if (pid < 0)
5719 exit(1);
5720
5721 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005722
5723 signal(SIGTSTP, SIG_IGN);
5724 signal(SIGTTOU, SIG_IGN);
5725 signal(SIGTTIN, SIG_IGN);
5726 }
ths71e3ceb2006-12-22 02:11:31 +00005727
thsaa26bb22007-03-25 21:33:06 +00005728 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005729 if (daemonize) {
5730 uint8_t status = 1;
5731 write(fds[1], &status, 1);
5732 } else
5733 fprintf(stderr, "Could not acquire pid file\n");
5734 exit(1);
5735 }
blueswir1b9e82a52009-04-05 18:03:31 +00005736#endif
ths93815bc2007-03-19 15:58:31 +00005737
blueswir1640f42e2009-04-19 10:18:01 +00005738#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005739 if (smp_cpus > 1)
5740 kqemu_allowed = 0;
5741#endif
aliguori3fcf7b62009-04-24 18:03:25 +00005742 if (qemu_init_main_loop()) {
5743 fprintf(stderr, "qemu_init_main_loop failed\n");
5744 exit(1);
5745 }
bellarda20dd502003-09-30 21:07:02 +00005746 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005747
thsf8d39c02008-07-03 10:01:15 +00005748 if (!linux_boot && *kernel_cmdline != '\0') {
5749 fprintf(stderr, "-append only allowed with -kernel option\n");
5750 exit(1);
5751 }
5752
5753 if (!linux_boot && initrd_filename != NULL) {
5754 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5755 exit(1);
5756 }
5757
bellardb118d612003-06-30 23:36:21 +00005758 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005759
pbrook634fce92006-07-15 17:40:09 +00005760 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005761 if (init_timer_alarm() < 0) {
5762 fprintf(stderr, "could not initialize alarm timer\n");
5763 exit(1);
5764 }
pbrook2e70f6e2008-06-29 01:03:05 +00005765 if (use_icount && icount_time_shift < 0) {
5766 use_icount = 2;
5767 /* 125MIPS seems a reasonable initial guess at the guest speed.
5768 It will be corrected fairly quickly anyway. */
5769 icount_time_shift = 3;
5770 init_icount_adjust();
5771 }
pbrook634fce92006-07-15 17:40:09 +00005772
bellardfd1dff42006-02-01 21:29:26 +00005773#ifdef _WIN32
5774 socket_init();
5775#endif
5776
bellard7c9d8e02005-11-15 22:16:05 +00005777 /* init network clients */
5778 if (nb_net_clients == 0) {
5779 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005780 net_clients[nb_net_clients++] = "nic";
5781#ifdef CONFIG_SLIRP
5782 net_clients[nb_net_clients++] = "user";
5783#endif
bellardc20709a2004-04-21 23:27:19 +00005784 }
5785
bellard7c9d8e02005-11-15 22:16:05 +00005786 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005787 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005788 exit(1);
bellard702c6512004-04-02 21:21:32 +00005789 }
bellardf1510b22003-06-25 00:07:40 +00005790
Glauber Costa406c8df2009-06-17 09:05:30 -04005791 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5792 net_set_boot_mask(net_boot);
5793
5794 net_client_check();
thseec85c22007-01-05 17:41:07 +00005795
balrogdc72ac12008-11-09 00:04:26 +00005796 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005797 if (foreach_device_config(DEV_BT, bt_parse))
5798 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005799
bellard0824d6f2003-06-24 13:42:40 +00005800 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005801 if (ram_size == 0)
5802 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005803
blueswir1640f42e2009-04-19 10:18:01 +00005804#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005805 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5806 guest ram allocation. It needs to go away. */
5807 if (kqemu_allowed) {
Paul Brook4cfce482009-05-13 18:08:17 +01005808 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
pbrook94a6b542009-04-11 17:15:54 +00005809 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5810 if (!kqemu_phys_ram_base) {
5811 fprintf(stderr, "Could not allocate physical memory\n");
5812 exit(1);
5813 }
balrog7fb4fdc2008-04-24 17:59:27 +00005814 }
pbrook94a6b542009-04-11 17:15:54 +00005815#endif
bellard0824d6f2003-06-24 13:42:40 +00005816
bellard26a5f132008-05-28 12:30:31 +00005817 /* init the dynamic translator */
5818 cpu_exec_init_all(tb_size * 1024 * 1024);
5819
bellard5905b2e2004-08-01 21:53:26 +00005820 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005821
5822 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005823 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005824
balrog9d413d12007-12-04 00:10:34 +00005825 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005826 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005827
balrog9d413d12007-12-04 00:10:34 +00005828 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005829 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005830
ths96d30e42007-01-07 20:42:14 +00005831 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005832 if (snapshot)
5833 qemu_opts_foreach(&drive_opt_list, drive_enable_snapshot, NULL, 0);
5834 if (qemu_opts_foreach(&drive_opt_list, drive_init_func, machine, 1) != 0)
5835 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005836
bellardc88676f2006-08-06 13:36:11 +00005837 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005838 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005839
aliguori3023f332009-01-16 19:04:14 +00005840#ifndef _WIN32
5841 /* must be after terminal init, SDL library changes signal handlers */
Jan Kiszka7c3370d2009-05-08 12:34:17 +02005842 sighandler_setup();
aliguori3023f332009-01-16 19:04:14 +00005843#endif
5844
5845 /* Maintain compatibility with multiple stdio monitors */
5846 if (!strcmp(monitor_device,"stdio")) {
5847 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5848 const char *devname = serial_devices[i];
5849 if (devname && !strcmp(devname,"mon:stdio")) {
5850 monitor_device = NULL;
5851 break;
5852 } else if (devname && !strcmp(devname,"stdio")) {
5853 monitor_device = NULL;
5854 serial_devices[i] = "mon:stdio";
5855 break;
5856 }
5857 }
5858 }
5859
aliguori268a3622009-04-21 22:30:27 +00005860 if (nb_numa_nodes > 0) {
5861 int i;
5862
5863 if (nb_numa_nodes > smp_cpus) {
5864 nb_numa_nodes = smp_cpus;
5865 }
5866
5867 /* If no memory size if given for any node, assume the default case
5868 * and distribute the available memory equally across all nodes
5869 */
5870 for (i = 0; i < nb_numa_nodes; i++) {
5871 if (node_mem[i] != 0)
5872 break;
5873 }
5874 if (i == nb_numa_nodes) {
5875 uint64_t usedmem = 0;
5876
5877 /* On Linux, the each node's border has to be 8MB aligned,
5878 * the final node gets the rest.
5879 */
5880 for (i = 0; i < nb_numa_nodes - 1; i++) {
5881 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5882 usedmem += node_mem[i];
5883 }
5884 node_mem[i] = ram_size - usedmem;
5885 }
5886
5887 for (i = 0; i < nb_numa_nodes; i++) {
5888 if (node_cpumask[i] != 0)
5889 break;
5890 }
5891 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5892 * must cope with this anyway, because there are BIOSes out there in
5893 * real machines which also use this scheme.
5894 */
5895 if (i == nb_numa_nodes) {
5896 for (i = 0; i < smp_cpus; i++) {
5897 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5898 }
5899 }
5900 }
5901
aliguori3023f332009-01-16 19:04:14 +00005902 if (kvm_enabled()) {
5903 int ret;
5904
5905 ret = kvm_init(smp_cpus);
5906 if (ret < 0) {
5907 fprintf(stderr, "failed to initialize KVM\n");
5908 exit(1);
5909 }
5910 }
5911
aliguori4c621802009-01-16 21:48:20 +00005912 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005913 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005914 if (!monitor_hd) {
5915 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5916 exit(1);
5917 }
5918 }
5919
aliguori2796dae2009-01-16 20:23:27 +00005920 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5921 const char *devname = serial_devices[i];
5922 if (devname && strcmp(devname, "none")) {
5923 char label[32];
5924 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005925 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005926 if (!serial_hds[i]) {
5927 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5928 devname);
5929 exit(1);
5930 }
5931 }
5932 }
5933
5934 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5935 const char *devname = parallel_devices[i];
5936 if (devname && strcmp(devname, "none")) {
5937 char label[32];
5938 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005939 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005940 if (!parallel_hds[i]) {
5941 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5942 devname);
5943 exit(1);
5944 }
5945 }
5946 }
5947
5948 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5949 const char *devname = virtio_consoles[i];
5950 if (devname && strcmp(devname, "none")) {
5951 char label[32];
5952 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005953 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005954 if (!virtcon_hds[i]) {
5955 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5956 devname);
5957 exit(1);
5958 }
5959 }
5960 }
5961
Paul Brookaae94602009-05-14 22:35:06 +01005962 module_call_init(MODULE_INIT_DEVICE);
5963
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005964 if (machine->compat_props) {
5965 qdev_prop_register_compat(machine->compat_props);
5966 }
Paul Brookfbe1b592009-05-13 17:56:25 +01005967 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005968 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5969
aliguori268a3622009-04-21 22:30:27 +00005970
5971 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5972 for (i = 0; i < nb_numa_nodes; i++) {
5973 if (node_cpumask[i] & (1 << env->cpu_index)) {
5974 env->numa_node = i;
5975 }
5976 }
5977 }
5978
aliguori6f338c32009-02-11 15:21:54 +00005979 current_machine = machine;
5980
aliguori3023f332009-01-16 19:04:14 +00005981 /* init USB devices */
5982 if (usb_enabled) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005983 foreach_device_config(DEV_USB, usb_parse);
aliguori3023f332009-01-16 19:04:14 +00005984 }
5985
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005986 /* init generic devices */
5987 if (foreach_device_config(DEV_GENERIC, generic_parse))
5988 exit(1);
5989
aliguori8f391ab2009-01-19 16:34:10 +00005990 if (!display_state)
5991 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005992 /* just use the first displaystate for the moment */
5993 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005994
5995 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005996#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005997 display_type = DT_SDL;
5998#else
5999 display_type = DT_VNC;
6000 vnc_display = "localhost:0,to=99";
6001 show_vnc_port = 1;
6002#endif
6003 }
6004
6005
6006 switch (display_type) {
6007 case DT_NOGRAPHIC:
6008 break;
6009#if defined(CONFIG_CURSES)
6010 case DT_CURSES:
6011 curses_display_init(ds, full_screen);
6012 break;
6013#endif
bellard5b0753e2005-03-01 21:37:28 +00006014#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006015 case DT_SDL:
6016 sdl_display_init(ds, full_screen, no_frame);
6017 break;
bellard5b0753e2005-03-01 21:37:28 +00006018#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006019 case DT_SDL:
6020 cocoa_display_init(ds, full_screen);
6021 break;
bellard313aa562003-08-10 21:52:11 +00006022#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006023 case DT_VNC:
6024 vnc_display_init(ds);
6025 if (vnc_display_open(ds, vnc_display) < 0)
6026 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006027
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006028 if (show_vnc_port) {
6029 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006030 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006031 break;
6032 default:
6033 break;
bellard313aa562003-08-10 21:52:11 +00006034 }
aliguori7d957bd2009-01-15 22:14:11 +00006035 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006036
aliguori3023f332009-01-16 19:04:14 +00006037 dcl = ds->listeners;
6038 while (dcl != NULL) {
6039 if (dcl->dpy_refresh != NULL) {
6040 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6041 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006042 }
aliguori3023f332009-01-16 19:04:14 +00006043 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006044 }
aliguori3023f332009-01-16 19:04:14 +00006045
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006046 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006047 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6048 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6049 }
6050
aliguori2796dae2009-01-16 20:23:27 +00006051 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00006052 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00006053
aliguori4c621802009-01-16 21:48:20 +00006054 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00006055 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00006056
bellard8d11df92004-08-24 21:13:40 +00006057 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006058 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006059 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006060 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006061 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006062 }
bellard82c643f2004-07-14 17:28:13 +00006063 }
bellard82c643f2004-07-14 17:28:13 +00006064
bellard6508fe52005-01-15 12:02:56 +00006065 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006066 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006067 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006068 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006069 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006070 }
6071 }
6072
aliguori9ede2fd2009-01-15 20:05:25 +00006073 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6074 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00006075 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00006076 if (strstart(devname, "vc", 0))
6077 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6078 }
6079 }
6080
aliguori59030a82009-04-05 18:43:41 +00006081 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6082 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6083 gdbstub_dev);
6084 exit(1);
balrog45669e02007-07-02 13:20:17 +00006085 }
balrog45669e02007-07-02 13:20:17 +00006086
bellardd63d3072004-10-03 13:29:03 +00006087 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00006088 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00006089
Paolo Bonzini89befdd2009-07-14 18:07:50 +02006090 if (incoming)
aliguori5bb79102008-10-13 03:12:02 +00006091 qemu_start_incoming_migration(incoming);
aliguori5bb79102008-10-13 03:12:02 +00006092
aliguoric0f4ce72009-03-05 23:01:01 +00006093 if (autostart)
6094 vm_start();
thsffd843b2006-12-21 19:46:43 +00006095
blueswir1b9e82a52009-04-05 18:03:31 +00006096#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006097 if (daemonize) {
6098 uint8_t status = 0;
6099 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006100
6101 again1:
6102 len = write(fds[1], &status, 1);
6103 if (len == -1 && (errno == EINTR))
6104 goto again1;
6105
6106 if (len != 1)
6107 exit(1);
6108
aliguoribd54b862008-07-23 00:58:33 +00006109 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006110 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006111 if (fd == -1)
6112 exit(1);
aliguori08585322009-02-27 22:09:45 +00006113 }
ths71e3ceb2006-12-22 02:11:31 +00006114
aliguori08585322009-02-27 22:09:45 +00006115 if (run_as) {
6116 pwd = getpwnam(run_as);
6117 if (!pwd) {
6118 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6119 exit(1);
6120 }
6121 }
ths71e3ceb2006-12-22 02:11:31 +00006122
aliguori08585322009-02-27 22:09:45 +00006123 if (chroot_dir) {
6124 if (chroot(chroot_dir) < 0) {
6125 fprintf(stderr, "chroot failed\n");
6126 exit(1);
6127 }
6128 chdir("/");
6129 }
6130
6131 if (run_as) {
6132 if (setgid(pwd->pw_gid) < 0) {
6133 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6134 exit(1);
6135 }
6136 if (setuid(pwd->pw_uid) < 0) {
6137 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6138 exit(1);
6139 }
6140 if (setuid(0) != -1) {
6141 fprintf(stderr, "Dropping privileges failed\n");
6142 exit(1);
6143 }
6144 }
aliguori08585322009-02-27 22:09:45 +00006145
6146 if (daemonize) {
6147 dup2(fd, 0);
6148 dup2(fd, 1);
6149 dup2(fd, 2);
6150
6151 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006152 }
blueswir1b9e82a52009-04-05 18:03:31 +00006153#endif
ths71e3ceb2006-12-22 02:11:31 +00006154
bellard8a7ddc32004-03-31 19:00:16 +00006155 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006156 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006157 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006158
bellard0824d6f2003-06-24 13:42:40 +00006159 return 0;
6160}