blob: 69b577fbba8189b2fcade884d68845117c7bfe6a [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000062#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
65/* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67/* #include <linux/hpet.h> */
68#include "hpet.h"
69
bellarde57a8c02005-11-10 23:58:52 +000070#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000071#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000072#endif
73#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000074#include <sys/stat.h>
75#include <sys/ethernet.h>
76#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000077#include <netinet/arp.h>
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_icmp.h> // must come after ip.h
82#include <netinet/udp.h>
83#include <netinet/tcp.h>
84#include <net/if.h>
85#include <syslog.h>
86#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000087/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000090#endif
bellard7d3505c2004-05-12 19:32:15 +000091#endif
bellardec530c82006-04-25 22:36:06 +000092#endif
bellard67b915a2004-03-31 23:37:16 +000093
blueswir19892fbf2008-08-24 10:34:20 +000094#if defined(__OpenBSD__)
95#include <util.h>
96#endif
97
ths8a16d272008-07-19 09:56:24 +000098#if defined(CONFIG_VDE)
99#include <libvdeplug.h>
100#endif
101
bellard67b915a2004-03-31 23:37:16 +0000102#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000103#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000104#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000105#endif
106
bellard73332e52004-04-04 20:22:28 +0000107#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200108#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200109#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000110int qemu_main(int argc, char **argv, char **envp);
111int main(int argc, char **argv)
112{
Stefan Weil59a36a22009-06-18 20:11:03 +0200113 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000114}
115#undef main
116#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000117#endif
bellard73332e52004-04-04 20:22:28 +0000118#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000119
bellard5b0753e2005-03-01 21:37:28 +0000120#ifdef CONFIG_COCOA
121#undef main
122#define main qemu_main
123#endif /* CONFIG_COCOA */
124
blueswir1511d2b12009-03-07 15:32:56 +0000125#include "hw/hw.h"
126#include "hw/boards.h"
127#include "hw/usb.h"
128#include "hw/pcmcia.h"
129#include "hw/pc.h"
130#include "hw/audiodev.h"
131#include "hw/isa.h"
132#include "hw/baum.h"
133#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100134#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000135#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000136#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200137#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200138#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000139#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000140#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000141#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000142#include "monitor.h"
143#include "console.h"
144#include "sysemu.h"
145#include "gdbstub.h"
146#include "qemu-timer.h"
147#include "qemu-char.h"
148#include "cache-utils.h"
149#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200150#include "block_int.h"
151#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000152#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153#include "audio/audio.h"
154#include "migration.h"
155#include "kvm.h"
156#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200157#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200158#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
bellard0824d6f2003-06-24 13:42:40 +0000160#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000161
bellard8a7ddc32004-03-31 19:00:16 +0000162#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000163
blueswir1511d2b12009-03-07 15:32:56 +0000164#include "qemu_socket.h"
165
Jan Kiszkad918f232009-06-24 14:42:30 +0200166#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000167
Blue Swirl72cf2d42009-09-12 07:36:22 +0000168#include "qemu-queue.h"
169
blueswir19dc63a12008-10-04 07:25:46 +0000170//#define DEBUG_NET
171//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000172
bellard1bfe8562004-07-08 21:17:50 +0000173#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000174
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +0200175/* Maximum number of monitor devices */
176#define MAX_MONITOR_DEVICES 10
177
Paul Brook5cea8592009-05-30 00:52:44 +0100178static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000179const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000180/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000181 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000182struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
183struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000184enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000185static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500186DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000187const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500188ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000189int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000190NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000191int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200192int autostart;
balrogf6503052008-02-17 11:42:19 +0000193static int rtc_utc = 1;
194static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200195QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100196int vga_interface_type = VGA_NONE;
bellardd8272202005-04-06 20:32:23 +0000197#ifdef TARGET_SPARC
198int graphic_width = 1024;
199int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000200int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000201#else
bellard1bfe8562004-07-08 21:17:50 +0000202int graphic_width = 800;
203int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000204int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000205#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000206static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000207#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000208static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000209#endif
ths667acca2006-12-11 02:08:05 +0000210int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000211CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000212CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000213CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100214CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
bellarda09db212005-04-30 16:10:35 +0000215#ifdef TARGET_I386
216int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000217int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000218#endif
bellardbb36d472005-11-05 14:22:28 +0000219int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000220int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000221int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200222int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200223int smp_cores = 1;
224int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000225const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000226int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000227int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000228int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000229int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000230int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000231int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000232int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200233uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000234#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000235int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000236#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200237const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000238const char *option_rom[MAX_OPTION_ROMS];
239int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000240int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000241#ifdef TARGET_ARM
242int old_param = 0;
243#endif
thsc35734b2007-03-19 15:17:08 +0000244const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000245int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500246int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000247#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000248unsigned int nb_prom_envs = 0;
249const char *prom_envs[MAX_PROM_ENVS];
250#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200251int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000252
aliguori268a3622009-04-21 22:30:27 +0000253int nb_numa_nodes;
254uint64_t node_mem[MAX_NODES];
255uint64_t node_cpumask[MAX_NODES];
256
balrogee5605e2007-12-03 03:01:40 +0000257static CPUState *cur_cpu;
258static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000259static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000260/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000261static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000262/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000263#define MAX_ICOUNT_SHIFT 10
264/* Compensate for varying guest execution speed. */
265static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000266static QEMUTimer *icount_rt_timer;
267static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000268static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000269
blueswir18fcb1b92008-09-18 18:29:08 +0000270uint8_t qemu_uuid[16];
271
Jan Kiszka76e30d02009-07-02 00:19:02 +0200272static QEMUBootSetHandler *boot_set_handler;
273static void *boot_set_opaque;
274
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100275static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100276static int default_parallel = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100277static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100278static int default_vga = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100279
280static struct {
281 const char *driver;
282 int *flag;
283} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100286 { .driver = "VGA", .flag = &default_vga },
287 { .driver = "Cirrus VGA", .flag = &default_vga },
288 { .driver = "QEMUware SVGA", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100289};
290
291static int default_driver_check(QemuOpts *opts, void *opaque)
292{
293 const char *driver = qemu_opt_get(opts, "driver");
294 int i;
295
296 if (!driver)
297 return 0;
298 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
299 if (strcmp(default_list[i].driver, driver) != 0)
300 continue;
301 *(default_list[i].flag) = 0;
302 }
303 return 0;
304}
305
bellard0824d6f2003-06-24 13:42:40 +0000306/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000307/* x86 ISA bus support */
308
Anthony Liguoric227f092009-10-01 16:12:16 -0500309target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000310PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000311
bellard0824d6f2003-06-24 13:42:40 +0000312/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000313void hw_error(const char *fmt, ...)
314{
315 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000316 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000317
318 va_start(ap, fmt);
319 fprintf(stderr, "qemu: hardware error: ");
320 vfprintf(stderr, fmt, ap);
321 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000322 for(env = first_cpu; env != NULL; env = env->next_cpu) {
323 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000324#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000325 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000326#else
bellard6a00d602005-11-21 23:25:50 +0000327 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000328#endif
bellard6a00d602005-11-21 23:25:50 +0000329 }
bellard0824d6f2003-06-24 13:42:40 +0000330 va_end(ap);
331 abort();
332}
Andi Kleen18894652009-07-02 09:34:17 +0200333
334static void set_proc_name(const char *s)
335{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700336#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200337 char name[16];
338 if (!s)
339 return;
340 name[sizeof(name) - 1] = 0;
341 strncpy(name, s, sizeof(name));
342 /* Could rewrite argv[0] too, but that's a bit more complicated.
343 This simple way is enough for `top'. */
344 prctl(PR_SET_NAME, name);
345#endif
346}
aliguoridf751fa2008-12-04 20:19:35 +0000347
348/***************/
349/* ballooning */
350
351static QEMUBalloonEvent *qemu_balloon_event;
352void *qemu_balloon_event_opaque;
353
354void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
355{
356 qemu_balloon_event = func;
357 qemu_balloon_event_opaque = opaque;
358}
359
Anthony Liguoric227f092009-10-01 16:12:16 -0500360void qemu_balloon(ram_addr_t target)
aliguoridf751fa2008-12-04 20:19:35 +0000361{
362 if (qemu_balloon_event)
363 qemu_balloon_event(qemu_balloon_event_opaque, target);
364}
365
Anthony Liguoric227f092009-10-01 16:12:16 -0500366ram_addr_t qemu_balloon_status(void)
aliguoridf751fa2008-12-04 20:19:35 +0000367{
368 if (qemu_balloon_event)
369 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
370 return 0;
371}
bellard0824d6f2003-06-24 13:42:40 +0000372
bellard8a7ddc32004-03-31 19:00:16 +0000373/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000374/* keyboard/mouse */
375
376static QEMUPutKBDEvent *qemu_put_kbd_event;
377static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000378static QEMUPutMouseEntry *qemu_put_mouse_event_head;
379static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000380
381void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
382{
383 qemu_put_kbd_event_opaque = opaque;
384 qemu_put_kbd_event = func;
385}
386
ths455204e2007-01-05 16:42:13 +0000387QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
388 void *opaque, int absolute,
389 const char *name)
bellard63066f42004-06-03 18:45:02 +0000390{
ths455204e2007-01-05 16:42:13 +0000391 QEMUPutMouseEntry *s, *cursor;
392
393 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000394
395 s->qemu_put_mouse_event = func;
396 s->qemu_put_mouse_event_opaque = opaque;
397 s->qemu_put_mouse_event_absolute = absolute;
398 s->qemu_put_mouse_event_name = qemu_strdup(name);
399 s->next = NULL;
400
401 if (!qemu_put_mouse_event_head) {
402 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
403 return s;
404 }
405
406 cursor = qemu_put_mouse_event_head;
407 while (cursor->next != NULL)
408 cursor = cursor->next;
409
410 cursor->next = s;
411 qemu_put_mouse_event_current = s;
412
413 return s;
414}
415
416void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
417{
418 QEMUPutMouseEntry *prev = NULL, *cursor;
419
420 if (!qemu_put_mouse_event_head || entry == NULL)
421 return;
422
423 cursor = qemu_put_mouse_event_head;
424 while (cursor != NULL && cursor != entry) {
425 prev = cursor;
426 cursor = cursor->next;
427 }
428
429 if (cursor == NULL) // does not exist or list empty
430 return;
431 else if (prev == NULL) { // entry is head
432 qemu_put_mouse_event_head = cursor->next;
433 if (qemu_put_mouse_event_current == entry)
434 qemu_put_mouse_event_current = cursor->next;
435 qemu_free(entry->qemu_put_mouse_event_name);
436 qemu_free(entry);
437 return;
438 }
439
440 prev->next = entry->next;
441
442 if (qemu_put_mouse_event_current == entry)
443 qemu_put_mouse_event_current = prev;
444
445 qemu_free(entry->qemu_put_mouse_event_name);
446 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000447}
448
449void kbd_put_keycode(int keycode)
450{
451 if (qemu_put_kbd_event) {
452 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
453 }
454}
455
456void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
457{
ths455204e2007-01-05 16:42:13 +0000458 QEMUPutMouseEvent *mouse_event;
459 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000460 int width;
ths455204e2007-01-05 16:42:13 +0000461
462 if (!qemu_put_mouse_event_current) {
463 return;
464 }
465
466 mouse_event =
467 qemu_put_mouse_event_current->qemu_put_mouse_event;
468 mouse_event_opaque =
469 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
470
471 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000472 if (graphic_rotate) {
473 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
474 width = 0x7fff;
475 else
aurel32b94ed572008-03-10 19:34:27 +0000476 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000477 mouse_event(mouse_event_opaque,
478 width - dy, dx, dz, buttons_state);
479 } else
480 mouse_event(mouse_event_opaque,
481 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000482 }
483}
484
bellard09b26c52006-04-12 21:09:08 +0000485int kbd_mouse_is_absolute(void)
486{
ths455204e2007-01-05 16:42:13 +0000487 if (!qemu_put_mouse_event_current)
488 return 0;
489
490 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
491}
492
aliguori376253e2009-03-05 23:01:23 +0000493void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000494{
495 QEMUPutMouseEntry *cursor;
496 int index = 0;
497
498 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000500 return;
501 }
502
aliguori376253e2009-03-05 23:01:23 +0000503 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000504 cursor = qemu_put_mouse_event_head;
505 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000506 monitor_printf(mon, "%c Mouse #%d: %s\n",
507 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
508 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000509 index++;
510 cursor = cursor->next;
511 }
512}
513
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300514void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000515{
516 QEMUPutMouseEntry *cursor;
517 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300518 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000519
520 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000522 return;
523 }
524
525 cursor = qemu_put_mouse_event_head;
526 while (cursor != NULL && index != i) {
527 i++;
528 cursor = cursor->next;
529 }
530
531 if (cursor != NULL)
532 qemu_put_mouse_event_current = cursor;
533 else
aliguori376253e2009-03-05 23:01:23 +0000534 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000535}
536
bellard87858c82003-06-27 12:01:39 +0000537/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000538uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000539{
540 union {
541 uint64_t ll;
542 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200543#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000544 uint32_t high, low;
545#else
546 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000547#endif
bellard87858c82003-06-27 12:01:39 +0000548 } l;
549 } u, res;
550 uint64_t rl, rh;
551
552 u.ll = a;
553 rl = (uint64_t)u.l.low * (uint64_t)b;
554 rh = (uint64_t)u.l.high * (uint64_t)b;
555 rh += (rl >> 32);
556 res.l.high = rh / c;
557 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
558 return res.ll;
559}
560
bellard1dce7c32006-07-13 23:20:22 +0000561/***********************************************************/
562/* real time host monotonic timer */
563
Jan Kiszka21d5d122009-09-15 13:36:04 +0200564static int64_t get_clock_realtime(void)
565{
566 struct timeval tv;
567
568 gettimeofday(&tv, NULL);
569 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
570}
571
bellard1dce7c32006-07-13 23:20:22 +0000572#ifdef WIN32
573
574static int64_t clock_freq;
575
576static void init_get_clock(void)
577{
bellarda8e5ac32006-07-14 09:36:13 +0000578 LARGE_INTEGER freq;
579 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000580 ret = QueryPerformanceFrequency(&freq);
581 if (ret == 0) {
582 fprintf(stderr, "Could not calibrate ticks\n");
583 exit(1);
584 }
585 clock_freq = freq.QuadPart;
586}
587
588static int64_t get_clock(void)
589{
590 LARGE_INTEGER ti;
591 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500592 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000593}
594
595#else
596
597static int use_rt_clock;
598
599static void init_get_clock(void)
600{
601 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000602#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100603 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000604 {
605 struct timespec ts;
606 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
607 use_rt_clock = 1;
608 }
609 }
610#endif
611}
612
613static int64_t get_clock(void)
614{
blueswir1c5e97232009-03-07 20:06:23 +0000615#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100616 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000617 if (use_rt_clock) {
618 struct timespec ts;
619 clock_gettime(CLOCK_MONOTONIC, &ts);
620 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000621 } else
bellard1dce7c32006-07-13 23:20:22 +0000622#endif
623 {
624 /* XXX: using gettimeofday leads to problems if the date
625 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200626 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000627 }
628}
bellard1dce7c32006-07-13 23:20:22 +0000629#endif
630
pbrook2e70f6e2008-06-29 01:03:05 +0000631/* Return the virtual CPU time, based on the instruction counter. */
632static int64_t cpu_get_icount(void)
633{
634 int64_t icount;
635 CPUState *env = cpu_single_env;;
636 icount = qemu_icount;
637 if (env) {
638 if (!can_do_io(env))
639 fprintf(stderr, "Bad clock read\n");
640 icount -= (env->icount_decr.u16.low + env->icount_extra);
641 }
642 return qemu_icount_bias + (icount << icount_time_shift);
643}
644
bellard1dce7c32006-07-13 23:20:22 +0000645/***********************************************************/
646/* guest cycle counter */
647
Juan Quintela6f68e332009-09-10 03:04:27 +0200648typedef struct TimersState {
649 int64_t cpu_ticks_prev;
650 int64_t cpu_ticks_offset;
651 int64_t cpu_clock_offset;
652 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500653 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200654} TimersState;
655
656TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000657
658/* return the host CPU cycle counter and handle stop/restart */
659int64_t cpu_get_ticks(void)
660{
pbrook2e70f6e2008-06-29 01:03:05 +0000661 if (use_icount) {
662 return cpu_get_icount();
663 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200664 if (!timers_state.cpu_ticks_enabled) {
665 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000666 } else {
667 int64_t ticks;
668 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200669 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000670 /* Note: non increasing ticks may happen if the host uses
671 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200672 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000673 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200674 timers_state.cpu_ticks_prev = ticks;
675 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000676 }
677}
678
679/* return the host CPU monotonic timer and handle stop/restart */
680static int64_t cpu_get_clock(void)
681{
682 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200683 if (!timers_state.cpu_ticks_enabled) {
684 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000685 } else {
686 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200687 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000688 }
689}
690
691/* enable cpu_get_ticks() */
692void cpu_enable_ticks(void)
693{
Juan Quintela6f68e332009-09-10 03:04:27 +0200694 if (!timers_state.cpu_ticks_enabled) {
695 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
696 timers_state.cpu_clock_offset -= get_clock();
697 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000698 }
699}
700
701/* disable cpu_get_ticks() : the clock is stopped. You must not call
702 cpu_get_ticks() after that. */
703void cpu_disable_ticks(void)
704{
Juan Quintela6f68e332009-09-10 03:04:27 +0200705 if (timers_state.cpu_ticks_enabled) {
706 timers_state.cpu_ticks_offset = cpu_get_ticks();
707 timers_state.cpu_clock_offset = cpu_get_clock();
708 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000709 }
710}
711
712/***********************************************************/
713/* timers */
ths5fafdf22007-09-16 21:08:06 +0000714
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200715#define QEMU_CLOCK_REALTIME 0
716#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200717#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000718
719struct QEMUClock {
720 int type;
721 /* XXX: add frequency */
722};
723
724struct QEMUTimer {
725 QEMUClock *clock;
726 int64_t expire_time;
727 QEMUTimerCB *cb;
728 void *opaque;
729 struct QEMUTimer *next;
730};
731
thsc8994012007-08-19 21:56:03 +0000732struct qemu_alarm_timer {
733 char const *name;
thsefe75412007-08-24 01:36:32 +0000734 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000735
736 int (*start)(struct qemu_alarm_timer *t);
737 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000738 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000739 void *priv;
740};
741
thsefe75412007-08-24 01:36:32 +0000742#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000743#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000744
745static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
746{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200747 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000748}
749
750static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
751{
752 if (!alarm_has_dynticks(t))
753 return;
754
755 t->rearm(t);
756}
757
758/* TODO: MIN_TIMER_REARM_US should be optimized */
759#define MIN_TIMER_REARM_US 250
760
thsc8994012007-08-19 21:56:03 +0000761static struct qemu_alarm_timer *alarm_timer;
762
763#ifdef _WIN32
764
765struct qemu_alarm_win32 {
766 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000767 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000768} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000769
770static int win32_start_timer(struct qemu_alarm_timer *t);
771static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000772static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000773
774#else
775
776static int unix_start_timer(struct qemu_alarm_timer *t);
777static void unix_stop_timer(struct qemu_alarm_timer *t);
778
ths231c6582007-08-26 17:29:15 +0000779#ifdef __linux__
780
thsefe75412007-08-24 01:36:32 +0000781static int dynticks_start_timer(struct qemu_alarm_timer *t);
782static void dynticks_stop_timer(struct qemu_alarm_timer *t);
783static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
784
thsc40ec5a2007-08-19 22:09:40 +0000785static int hpet_start_timer(struct qemu_alarm_timer *t);
786static void hpet_stop_timer(struct qemu_alarm_timer *t);
787
thsc8994012007-08-19 21:56:03 +0000788static int rtc_start_timer(struct qemu_alarm_timer *t);
789static void rtc_stop_timer(struct qemu_alarm_timer *t);
790
thsefe75412007-08-24 01:36:32 +0000791#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000792
793#endif /* _WIN32 */
794
pbrook2e70f6e2008-06-29 01:03:05 +0000795/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000796 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000797 When the guest is idle real and virtual time will be aligned in
798 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500799#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000800
801static void icount_adjust(void)
802{
803 int64_t cur_time;
804 int64_t cur_icount;
805 int64_t delta;
806 static int64_t last_delta;
807 /* If the VM is not running, then do nothing. */
808 if (!vm_running)
809 return;
810
811 cur_time = cpu_get_clock();
812 cur_icount = qemu_get_clock(vm_clock);
813 delta = cur_icount - cur_time;
814 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
815 if (delta > 0
816 && last_delta + ICOUNT_WOBBLE < delta * 2
817 && icount_time_shift > 0) {
818 /* The guest is getting too far ahead. Slow time down. */
819 icount_time_shift--;
820 }
821 if (delta < 0
822 && last_delta - ICOUNT_WOBBLE > delta * 2
823 && icount_time_shift < MAX_ICOUNT_SHIFT) {
824 /* The guest is getting too far behind. Speed time up. */
825 icount_time_shift++;
826 }
827 last_delta = delta;
828 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
829}
830
831static void icount_adjust_rt(void * opaque)
832{
833 qemu_mod_timer(icount_rt_timer,
834 qemu_get_clock(rt_clock) + 1000);
835 icount_adjust();
836}
837
838static void icount_adjust_vm(void * opaque)
839{
840 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500841 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000842 icount_adjust();
843}
844
845static void init_icount_adjust(void)
846{
847 /* Have both realtime and virtual time triggers for speed adjustment.
848 The realtime trigger catches emulated time passing too slowly,
849 the virtual time trigger catches emulated time passing too fast.
850 Realtime triggers occur even when idle, so use them less frequently
851 than VM triggers. */
852 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
853 qemu_mod_timer(icount_rt_timer,
854 qemu_get_clock(rt_clock) + 1000);
855 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
856 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500857 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000858}
859
thsc8994012007-08-19 21:56:03 +0000860static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000861#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000862#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000863 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
864 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000865 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000866 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000867 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000868 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000869#endif
thsefe75412007-08-24 01:36:32 +0000870 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000871#else
thsefe75412007-08-24 01:36:32 +0000872 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
873 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
874 {"win32", 0, win32_start_timer,
875 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000876#endif
877 {NULL, }
878};
879
blueswir13f47aa82008-03-09 06:59:01 +0000880static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000881{
882 int i;
883
884 printf("Available alarm timers, in order of precedence:\n");
885 for (i = 0; alarm_timers[i].name; i++)
886 printf("%s\n", alarm_timers[i].name);
887}
888
889static void configure_alarms(char const *opt)
890{
891 int i;
892 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000893 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000894 char *arg;
895 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000896 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000897
aurel323adda042008-03-09 23:43:49 +0000898 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000899 show_available_alarms();
900 exit(0);
901 }
902
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200903 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000904
905 /* Reorder the array */
906 name = strtok(arg, ",");
907 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000908 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000909 if (!strcmp(alarm_timers[i].name, name))
910 break;
911 }
912
913 if (i == count) {
914 fprintf(stderr, "Unknown clock %s\n", name);
915 goto next;
916 }
917
918 if (i < cur)
919 /* Ignore */
920 goto next;
921
922 /* Swap */
923 tmp = alarm_timers[i];
924 alarm_timers[i] = alarm_timers[cur];
925 alarm_timers[cur] = tmp;
926
927 cur++;
928next:
929 name = strtok(NULL, ",");
930 }
931
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200932 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000933
934 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000935 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000936 for (i = cur; i < count; i++)
937 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000938 } else {
939 show_available_alarms();
940 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000941 }
thsf3dcfad2007-08-24 01:26:02 +0000942}
943
Jan Kiszka21d5d122009-09-15 13:36:04 +0200944#define QEMU_NUM_CLOCKS 3
945
bellard8a7ddc32004-03-31 19:00:16 +0000946QEMUClock *rt_clock;
947QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +0200948QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +0000949
Jan Kiszka21d5d122009-09-15 13:36:04 +0200950static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +0000951
pbrook9596ebb2007-11-18 01:44:38 +0000952static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000953{
954 QEMUClock *clock;
955 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000956 clock->type = type;
957 return clock;
958}
959
960QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
961{
962 QEMUTimer *ts;
963
964 ts = qemu_mallocz(sizeof(QEMUTimer));
965 ts->clock = clock;
966 ts->cb = cb;
967 ts->opaque = opaque;
968 return ts;
969}
970
971void qemu_free_timer(QEMUTimer *ts)
972{
973 qemu_free(ts);
974}
975
976/* stop a timer, but do not dealloc it */
977void qemu_del_timer(QEMUTimer *ts)
978{
979 QEMUTimer **pt, *t;
980
981 /* NOTE: this code must be signal safe because
982 qemu_timer_expired() can be called from a signal. */
983 pt = &active_timers[ts->clock->type];
984 for(;;) {
985 t = *pt;
986 if (!t)
987 break;
988 if (t == ts) {
989 *pt = t->next;
990 break;
991 }
992 pt = &t->next;
993 }
994}
995
996/* modify the current timer so that it will be fired when current_time
997 >= expire_time. The corresponding callback will be called. */
998void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
999{
1000 QEMUTimer **pt, *t;
1001
1002 qemu_del_timer(ts);
1003
1004 /* add the timer in the sorted list */
1005 /* NOTE: this code must be signal safe because
1006 qemu_timer_expired() can be called from a signal. */
1007 pt = &active_timers[ts->clock->type];
1008 for(;;) {
1009 t = *pt;
1010 if (!t)
1011 break;
ths5fafdf22007-09-16 21:08:06 +00001012 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001013 break;
1014 pt = &t->next;
1015 }
1016 ts->expire_time = expire_time;
1017 ts->next = *pt;
1018 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001019
1020 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001021 if (pt == &active_timers[ts->clock->type]) {
1022 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1023 qemu_rearm_alarm_timer(alarm_timer);
1024 }
1025 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +00001026 if (use_icount)
1027 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001028 }
bellard8a7ddc32004-03-31 19:00:16 +00001029}
1030
1031int qemu_timer_pending(QEMUTimer *ts)
1032{
1033 QEMUTimer *t;
1034 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1035 if (t == ts)
1036 return 1;
1037 }
1038 return 0;
1039}
1040
Stefano Stabellini2430ffe2009-08-03 10:56:01 +01001041int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001042{
1043 if (!timer_head)
1044 return 0;
1045 return (timer_head->expire_time <= current_time);
1046}
1047
1048static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1049{
1050 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001051
bellard8a7ddc32004-03-31 19:00:16 +00001052 for(;;) {
1053 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001054 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001055 break;
1056 /* remove timer from the list before calling the callback */
1057 *ptimer_head = ts->next;
1058 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001059
bellard8a7ddc32004-03-31 19:00:16 +00001060 /* run the callback (the timer list can be modified) */
1061 ts->cb(ts->opaque);
1062 }
1063}
1064
1065int64_t qemu_get_clock(QEMUClock *clock)
1066{
1067 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001068 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001069 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001070 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001071 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001072 if (use_icount) {
1073 return cpu_get_icount();
1074 } else {
1075 return cpu_get_clock();
1076 }
Jan Kiszka21d5d122009-09-15 13:36:04 +02001077 case QEMU_CLOCK_HOST:
1078 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +00001079 }
1080}
1081
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001082static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +00001083{
1084 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001085 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
1086 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001087 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +02001088
1089 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +00001090}
1091
bellard8a7ddc32004-03-31 19:00:16 +00001092/* save a timer */
1093void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1094{
1095 uint64_t expire_time;
1096
1097 if (qemu_timer_pending(ts)) {
1098 expire_time = ts->expire_time;
1099 } else {
1100 expire_time = -1;
1101 }
1102 qemu_put_be64(f, expire_time);
1103}
1104
1105void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1106{
1107 uint64_t expire_time;
1108
1109 expire_time = qemu_get_be64(f);
1110 if (expire_time != -1) {
1111 qemu_mod_timer(ts, expire_time);
1112 } else {
1113 qemu_del_timer(ts);
1114 }
1115}
1116
Juan Quintela2faf58c2009-09-10 03:04:28 +02001117static const VMStateDescription vmstate_timers = {
1118 .name = "timer",
1119 .version_id = 2,
1120 .minimum_version_id = 1,
1121 .minimum_version_id_old = 1,
1122 .fields = (VMStateField []) {
1123 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001124 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001125 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1126 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001127 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001128};
bellard8a7ddc32004-03-31 19:00:16 +00001129
aliguori50317c72009-04-24 18:03:29 +00001130static void qemu_event_increment(void);
1131
bellard67b915a2004-03-31 23:37:16 +00001132#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001133static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1134 DWORD_PTR dwUser, DWORD_PTR dw1,
1135 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001136#else
bellard8a7ddc32004-03-31 19:00:16 +00001137static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001138#endif
bellard8a7ddc32004-03-31 19:00:16 +00001139{
bellard02ba45c2004-06-25 14:46:23 +00001140#if 0
1141#define DISP_FREQ 1000
1142 {
1143 static int64_t delta_min = INT64_MAX;
1144 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1145 static int count;
1146 ti = qemu_get_clock(vm_clock);
1147 if (last_clock != 0) {
1148 delta = ti - last_clock;
1149 if (delta < delta_min)
1150 delta_min = delta;
1151 if (delta > delta_max)
1152 delta_max = delta;
1153 delta_cum += delta;
1154 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001155 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001156 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1157 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1158 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1159 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001160 count = 0;
1161 delta_min = INT64_MAX;
1162 delta_max = 0;
1163 delta_cum = 0;
1164 }
1165 }
1166 last_clock = ti;
1167 }
1168#endif
thsefe75412007-08-24 01:36:32 +00001169 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001170 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001171 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001172 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001173 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001174 qemu_get_clock(rt_clock)) ||
1175 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1176 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001177 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001178 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001179
aliguorid6dc3d42009-04-24 18:04:07 +00001180#ifndef CONFIG_IOTHREAD
1181 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001182 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001183 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001184 }
aliguorid6dc3d42009-04-24 18:04:07 +00001185#endif
aliguori43b96852009-04-24 18:03:33 +00001186 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001187 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001188 }
1189}
1190
pbrook2e70f6e2008-06-29 01:03:05 +00001191static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001192{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001193 /* To avoid problems with overflow limit this to 2^32. */
1194 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001195
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001196 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1197 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001198 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001199 }
1200 if (active_timers[QEMU_CLOCK_HOST]) {
1201 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1202 qemu_get_clock(host_clock);
1203 if (hdelta < delta)
1204 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001205 }
1206
pbrook2e70f6e2008-06-29 01:03:05 +00001207 if (delta < 0)
1208 delta = 0;
thsefe75412007-08-24 01:36:32 +00001209
pbrook2e70f6e2008-06-29 01:03:05 +00001210 return delta;
1211}
1212
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001213#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001214static uint64_t qemu_next_deadline_dyntick(void)
1215{
1216 int64_t delta;
1217 int64_t rtdelta;
1218
1219 if (use_icount)
1220 delta = INT32_MAX;
1221 else
1222 delta = (qemu_next_deadline() + 999) / 1000;
1223
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001224 if (active_timers[QEMU_CLOCK_REALTIME]) {
1225 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001226 qemu_get_clock(rt_clock))*1000;
1227 if (rtdelta < delta)
1228 delta = rtdelta;
1229 }
1230
1231 if (delta < MIN_TIMER_REARM_US)
1232 delta = MIN_TIMER_REARM_US;
1233
1234 return delta;
thsefe75412007-08-24 01:36:32 +00001235}
blueswir18632fb92008-09-14 13:59:34 +00001236#endif
thsefe75412007-08-24 01:36:32 +00001237
bellardfd872592004-05-12 19:11:15 +00001238#ifndef _WIN32
1239
aliguori7183b4b2008-11-05 20:40:18 +00001240/* Sets a specific flag */
1241static int fcntl_setfl(int fd, int flag)
1242{
1243 int flags;
1244
1245 flags = fcntl(fd, F_GETFL);
1246 if (flags == -1)
1247 return -errno;
1248
1249 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1250 return -errno;
1251
1252 return 0;
1253}
1254
bellard829309c2004-05-20 13:20:12 +00001255#if defined(__linux__)
1256
bellardfd872592004-05-12 19:11:15 +00001257#define RTC_FREQ 1024
1258
aurel32de9a95f2008-11-11 13:41:01 +00001259static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001260{
thsc8994012007-08-19 21:56:03 +00001261 struct sigaction act;
1262
1263 /* timer signal */
1264 sigfillset(&act.sa_mask);
1265 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001266 act.sa_handler = host_alarm_handler;
1267
1268 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001269 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001270 fcntl(fd, F_SETOWN, getpid());
1271}
1272
thsc40ec5a2007-08-19 22:09:40 +00001273static int hpet_start_timer(struct qemu_alarm_timer *t)
1274{
1275 struct hpet_info info;
1276 int r, fd;
1277
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001278 fd = qemu_open("/dev/hpet", O_RDONLY);
thsc40ec5a2007-08-19 22:09:40 +00001279 if (fd < 0)
1280 return -1;
1281
1282 /* Set frequency */
1283 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1284 if (r < 0) {
1285 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1286 "error, but for better emulation accuracy type:\n"
1287 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1288 goto fail;
1289 }
1290
1291 /* Check capabilities */
1292 r = ioctl(fd, HPET_INFO, &info);
1293 if (r < 0)
1294 goto fail;
1295
1296 /* Enable periodic mode */
1297 r = ioctl(fd, HPET_EPI, 0);
1298 if (info.hi_flags && (r < 0))
1299 goto fail;
1300
1301 /* Enable interrupt */
1302 r = ioctl(fd, HPET_IE_ON, 0);
1303 if (r < 0)
1304 goto fail;
1305
1306 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001307 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001308
1309 return 0;
1310fail:
1311 close(fd);
1312 return -1;
1313}
1314
1315static void hpet_stop_timer(struct qemu_alarm_timer *t)
1316{
pbrookfcdc2122007-08-23 20:22:22 +00001317 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001318
1319 close(fd);
1320}
1321
thsc8994012007-08-19 21:56:03 +00001322static int rtc_start_timer(struct qemu_alarm_timer *t)
1323{
1324 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001325 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001326
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001327 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001328 if (rtc_fd < 0)
1329 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001330 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1331 if (current_rtc_freq != RTC_FREQ &&
1332 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001333 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1334 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1335 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1336 goto fail;
1337 }
1338 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1339 fail:
1340 close(rtc_fd);
1341 return -1;
1342 }
thsc8994012007-08-19 21:56:03 +00001343
1344 enable_sigio_timer(rtc_fd);
1345
pbrookfcdc2122007-08-23 20:22:22 +00001346 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001347
bellardfd872592004-05-12 19:11:15 +00001348 return 0;
1349}
1350
thsc8994012007-08-19 21:56:03 +00001351static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001352{
pbrookfcdc2122007-08-23 20:22:22 +00001353 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001354
1355 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001356}
1357
thsefe75412007-08-24 01:36:32 +00001358static int dynticks_start_timer(struct qemu_alarm_timer *t)
1359{
1360 struct sigevent ev;
1361 timer_t host_timer;
1362 struct sigaction act;
1363
1364 sigfillset(&act.sa_mask);
1365 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001366 act.sa_handler = host_alarm_handler;
1367
1368 sigaction(SIGALRM, &act, NULL);
1369
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001370 /*
1371 * Initialize ev struct to 0 to avoid valgrind complaining
1372 * about uninitialized data in timer_create call
1373 */
1374 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001375 ev.sigev_value.sival_int = 0;
1376 ev.sigev_notify = SIGEV_SIGNAL;
1377 ev.sigev_signo = SIGALRM;
1378
1379 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1380 perror("timer_create");
1381
1382 /* disable dynticks */
1383 fprintf(stderr, "Dynamic Ticks disabled\n");
1384
1385 return -1;
1386 }
1387
blueswir10399bfe2008-11-16 11:37:18 +00001388 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001389
1390 return 0;
1391}
1392
1393static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1394{
blueswir10399bfe2008-11-16 11:37:18 +00001395 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001396
1397 timer_delete(host_timer);
1398}
1399
1400static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1401{
blueswir10399bfe2008-11-16 11:37:18 +00001402 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001403 struct itimerspec timeout;
1404 int64_t nearest_delta_us = INT64_MAX;
1405 int64_t current_us;
1406
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001407 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001408 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1409 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001410 return;
thsefe75412007-08-24 01:36:32 +00001411
pbrook2e70f6e2008-06-29 01:03:05 +00001412 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001413
1414 /* check whether a timer is already running */
1415 if (timer_gettime(host_timer, &timeout)) {
1416 perror("gettime");
1417 fprintf(stderr, "Internal timer error: aborting\n");
1418 exit(1);
1419 }
1420 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1421 if (current_us && current_us <= nearest_delta_us)
1422 return;
1423
1424 timeout.it_interval.tv_sec = 0;
1425 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1426 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1427 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1428 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1429 perror("settime");
1430 fprintf(stderr, "Internal timer error: aborting\n");
1431 exit(1);
1432 }
1433}
1434
ths70744b32007-08-26 17:31:30 +00001435#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001436
thsc8994012007-08-19 21:56:03 +00001437static int unix_start_timer(struct qemu_alarm_timer *t)
1438{
1439 struct sigaction act;
1440 struct itimerval itv;
1441 int err;
1442
1443 /* timer signal */
1444 sigfillset(&act.sa_mask);
1445 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001446 act.sa_handler = host_alarm_handler;
1447
1448 sigaction(SIGALRM, &act, NULL);
1449
1450 itv.it_interval.tv_sec = 0;
1451 /* for i386 kernel 2.6 to get 1 ms */
1452 itv.it_interval.tv_usec = 999;
1453 itv.it_value.tv_sec = 0;
1454 itv.it_value.tv_usec = 10 * 1000;
1455
1456 err = setitimer(ITIMER_REAL, &itv, NULL);
1457 if (err)
1458 return -1;
1459
1460 return 0;
1461}
1462
1463static void unix_stop_timer(struct qemu_alarm_timer *t)
1464{
1465 struct itimerval itv;
1466
1467 memset(&itv, 0, sizeof(itv));
1468 setitimer(ITIMER_REAL, &itv, NULL);
1469}
1470
bellard829309c2004-05-20 13:20:12 +00001471#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001472
aliguorif49e58d2008-11-05 21:22:34 +00001473
thsc8994012007-08-19 21:56:03 +00001474#ifdef _WIN32
1475
1476static int win32_start_timer(struct qemu_alarm_timer *t)
1477{
1478 TIMECAPS tc;
1479 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001480 UINT flags;
thsc8994012007-08-19 21:56:03 +00001481
thsc8994012007-08-19 21:56:03 +00001482 memset(&tc, 0, sizeof(tc));
1483 timeGetDevCaps(&tc, sizeof(tc));
1484
1485 if (data->period < tc.wPeriodMin)
1486 data->period = tc.wPeriodMin;
1487
1488 timeBeginPeriod(data->period);
1489
thsefe75412007-08-24 01:36:32 +00001490 flags = TIME_CALLBACK_FUNCTION;
1491 if (alarm_has_dynticks(t))
1492 flags |= TIME_ONESHOT;
1493 else
1494 flags |= TIME_PERIODIC;
1495
thsc8994012007-08-19 21:56:03 +00001496 data->timerId = timeSetEvent(1, // interval (ms)
1497 data->period, // resolution
1498 host_alarm_handler, // function
1499 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001500 flags);
thsc8994012007-08-19 21:56:03 +00001501
1502 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001503 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001504 GetLastError());
thsc8994012007-08-19 21:56:03 +00001505 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001506 return -1;
1507 }
1508
thsc8994012007-08-19 21:56:03 +00001509 return 0;
1510}
1511
1512static void win32_stop_timer(struct qemu_alarm_timer *t)
1513{
1514 struct qemu_alarm_win32 *data = t->priv;
1515
1516 timeKillEvent(data->timerId);
1517 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001518}
1519
thsefe75412007-08-24 01:36:32 +00001520static void win32_rearm_timer(struct qemu_alarm_timer *t)
1521{
1522 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001523
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001524 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001525 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1526 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001527 return;
thsefe75412007-08-24 01:36:32 +00001528
thsefe75412007-08-24 01:36:32 +00001529 timeKillEvent(data->timerId);
1530
1531 data->timerId = timeSetEvent(1,
1532 data->period,
1533 host_alarm_handler,
1534 (DWORD)t,
1535 TIME_ONESHOT | TIME_PERIODIC);
1536
1537 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001538 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001539 GetLastError());
thsefe75412007-08-24 01:36:32 +00001540
1541 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001542 exit(1);
1543 }
1544}
1545
thsc8994012007-08-19 21:56:03 +00001546#endif /* _WIN32 */
1547
aliguori7183b4b2008-11-05 20:40:18 +00001548static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001549{
blueswir1223f0d72008-09-30 18:12:18 +00001550 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001551 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001552
thsc8994012007-08-19 21:56:03 +00001553 for (i = 0; alarm_timers[i].name; i++) {
1554 t = &alarm_timers[i];
1555
thsc8994012007-08-19 21:56:03 +00001556 err = t->start(t);
1557 if (!err)
1558 break;
bellard67b915a2004-03-31 23:37:16 +00001559 }
bellardfd872592004-05-12 19:11:15 +00001560
thsc8994012007-08-19 21:56:03 +00001561 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001562 err = -ENOENT;
1563 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001564 }
thsc8994012007-08-19 21:56:03 +00001565
1566 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001567
aliguori6abfbd72008-11-05 20:49:37 +00001568 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001569
1570fail:
aliguori7183b4b2008-11-05 20:40:18 +00001571 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001572}
1573
pbrook9596ebb2007-11-18 01:44:38 +00001574static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001575{
thsc8994012007-08-19 21:56:03 +00001576 alarm_timer->stop(alarm_timer);
1577 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001578}
1579
bellardc4b1fcc2004-03-14 21:44:30 +00001580/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001581/* host time/date access */
1582void qemu_get_timedate(struct tm *tm, int offset)
1583{
1584 time_t ti;
1585 struct tm *ret;
1586
1587 time(&ti);
1588 ti += offset;
1589 if (rtc_date_offset == -1) {
1590 if (rtc_utc)
1591 ret = gmtime(&ti);
1592 else
1593 ret = localtime(&ti);
1594 } else {
1595 ti -= rtc_date_offset;
1596 ret = gmtime(&ti);
1597 }
1598
1599 memcpy(tm, ret, sizeof(struct tm));
1600}
1601
1602int qemu_timedate_diff(struct tm *tm)
1603{
1604 time_t seconds;
1605
1606 if (rtc_date_offset == -1)
1607 if (rtc_utc)
1608 seconds = mktimegm(tm);
1609 else
1610 seconds = mktime(tm);
1611 else
1612 seconds = mktimegm(tm) + rtc_date_offset;
1613
1614 return seconds - time(NULL);
1615}
1616
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001617static void configure_rtc_date_offset(const char *startdate, int legacy)
1618{
1619 time_t rtc_start_date;
1620 struct tm tm;
1621
1622 if (!strcmp(startdate, "now") && legacy) {
1623 rtc_date_offset = -1;
1624 } else {
1625 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1626 &tm.tm_year,
1627 &tm.tm_mon,
1628 &tm.tm_mday,
1629 &tm.tm_hour,
1630 &tm.tm_min,
1631 &tm.tm_sec) == 6) {
1632 /* OK */
1633 } else if (sscanf(startdate, "%d-%d-%d",
1634 &tm.tm_year,
1635 &tm.tm_mon,
1636 &tm.tm_mday) == 3) {
1637 tm.tm_hour = 0;
1638 tm.tm_min = 0;
1639 tm.tm_sec = 0;
1640 } else {
1641 goto date_fail;
1642 }
1643 tm.tm_year -= 1900;
1644 tm.tm_mon--;
1645 rtc_start_date = mktimegm(&tm);
1646 if (rtc_start_date == -1) {
1647 date_fail:
1648 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1649 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1650 exit(1);
1651 }
1652 rtc_date_offset = time(NULL) - rtc_start_date;
1653 }
1654}
1655
1656static void configure_rtc(QemuOpts *opts)
1657{
1658 const char *value;
1659
1660 value = qemu_opt_get(opts, "base");
1661 if (value) {
1662 if (!strcmp(value, "utc")) {
1663 rtc_utc = 1;
1664 } else if (!strcmp(value, "localtime")) {
1665 rtc_utc = 0;
1666 } else {
1667 configure_rtc_date_offset(value, 0);
1668 }
1669 }
Jan Kiszka68752042009-09-15 13:36:04 +02001670 value = qemu_opt_get(opts, "clock");
1671 if (value) {
1672 if (!strcmp(value, "host")) {
1673 rtc_clock = host_clock;
1674 } else if (!strcmp(value, "vm")) {
1675 rtc_clock = vm_clock;
1676 } else {
1677 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1678 exit(1);
1679 }
1680 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001681#ifdef CONFIG_TARGET_I386
1682 value = qemu_opt_get(opts, "driftfix");
1683 if (value) {
1684 if (!strcmp(buf, "slew")) {
1685 rtc_td_hack = 1;
1686 } else if (!strcmp(buf, "none")) {
1687 rtc_td_hack = 0;
1688 } else {
1689 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1690 exit(1);
1691 }
1692 }
1693#endif
1694}
1695
bellardfd1dff42006-02-01 21:29:26 +00001696#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001697static void socket_cleanup(void)
1698{
1699 WSACleanup();
1700}
bellard82c643f2004-07-14 17:28:13 +00001701
bellardfd1dff42006-02-01 21:29:26 +00001702static int socket_init(void)
1703{
1704 WSADATA Data;
1705 int ret, err;
1706
1707 ret = WSAStartup(MAKEWORD(2,2), &Data);
1708 if (ret != 0) {
1709 err = WSAGetLastError();
1710 fprintf(stderr, "WSAStartup: %d\n", err);
1711 return -1;
1712 }
1713 atexit(socket_cleanup);
1714 return 0;
1715}
aurel3264b7b732008-05-05 10:05:31 +00001716#endif
1717
balrog1ae26a12008-09-28 23:19:47 +00001718/***********************************************************/
1719/* Bluetooth support */
1720static int nb_hcis;
1721static int cur_hci;
1722static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001723
balrog1ae26a12008-09-28 23:19:47 +00001724static struct bt_vlan_s {
1725 struct bt_scatternet_s net;
1726 int id;
1727 struct bt_vlan_s *next;
1728} *first_bt_vlan;
1729
1730/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001731static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001732{
1733 struct bt_vlan_s **pvlan, *vlan;
1734 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1735 if (vlan->id == id)
1736 return &vlan->net;
1737 }
1738 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1739 vlan->id = id;
1740 pvlan = &first_bt_vlan;
1741 while (*pvlan != NULL)
1742 pvlan = &(*pvlan)->next;
1743 *pvlan = vlan;
1744 return &vlan->net;
1745}
1746
1747static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1748{
1749}
1750
1751static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1752{
1753 return -ENOTSUP;
1754}
1755
1756static struct HCIInfo null_hci = {
1757 .cmd_send = null_hci_send,
1758 .sco_send = null_hci_send,
1759 .acl_send = null_hci_send,
1760 .bdaddr_set = null_hci_addr_set,
1761};
1762
1763struct HCIInfo *qemu_next_hci(void)
1764{
1765 if (cur_hci == nb_hcis)
1766 return &null_hci;
1767
1768 return hci_table[cur_hci++];
1769}
1770
balrogdc72ac12008-11-09 00:04:26 +00001771static struct HCIInfo *hci_init(const char *str)
1772{
1773 char *endp;
1774 struct bt_scatternet_s *vlan = 0;
1775
1776 if (!strcmp(str, "null"))
1777 /* null */
1778 return &null_hci;
1779 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1780 /* host[:hciN] */
1781 return bt_host_hci(str[4] ? str + 5 : "hci0");
1782 else if (!strncmp(str, "hci", 3)) {
1783 /* hci[,vlan=n] */
1784 if (str[3]) {
1785 if (!strncmp(str + 3, ",vlan=", 6)) {
1786 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1787 if (*endp)
1788 vlan = 0;
1789 }
1790 } else
1791 vlan = qemu_find_bt_vlan(0);
1792 if (vlan)
1793 return bt_new_hci(vlan);
1794 }
1795
1796 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1797
1798 return 0;
1799}
1800
1801static int bt_hci_parse(const char *str)
1802{
1803 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001804 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001805
1806 if (nb_hcis >= MAX_NICS) {
1807 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1808 return -1;
1809 }
1810
1811 hci = hci_init(str);
1812 if (!hci)
1813 return -1;
1814
1815 bdaddr.b[0] = 0x52;
1816 bdaddr.b[1] = 0x54;
1817 bdaddr.b[2] = 0x00;
1818 bdaddr.b[3] = 0x12;
1819 bdaddr.b[4] = 0x34;
1820 bdaddr.b[5] = 0x56 + nb_hcis;
1821 hci->bdaddr_set(hci, bdaddr.b);
1822
1823 hci_table[nb_hcis++] = hci;
1824
1825 return 0;
1826}
1827
1828static void bt_vhci_add(int vlan_id)
1829{
1830 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1831
1832 if (!vlan->slave)
1833 fprintf(stderr, "qemu: warning: adding a VHCI to "
1834 "an empty scatternet %i\n", vlan_id);
1835
1836 bt_vhci_init(bt_new_hci(vlan));
1837}
1838
1839static struct bt_device_s *bt_device_add(const char *opt)
1840{
1841 struct bt_scatternet_s *vlan;
1842 int vlan_id = 0;
1843 char *endp = strstr(opt, ",vlan=");
1844 int len = (endp ? endp - opt : strlen(opt)) + 1;
1845 char devname[10];
1846
1847 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1848
1849 if (endp) {
1850 vlan_id = strtol(endp + 6, &endp, 0);
1851 if (*endp) {
1852 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1853 return 0;
1854 }
1855 }
1856
1857 vlan = qemu_find_bt_vlan(vlan_id);
1858
1859 if (!vlan->slave)
1860 fprintf(stderr, "qemu: warning: adding a slave device to "
1861 "an empty scatternet %i\n", vlan_id);
1862
1863 if (!strcmp(devname, "keyboard"))
1864 return bt_keyboard_init(vlan);
1865
1866 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1867 return 0;
1868}
1869
1870static int bt_parse(const char *opt)
1871{
1872 const char *endp, *p;
1873 int vlan;
1874
1875 if (strstart(opt, "hci", &endp)) {
1876 if (!*endp || *endp == ',') {
1877 if (*endp)
1878 if (!strstart(endp, ",vlan=", 0))
1879 opt = endp + 1;
1880
1881 return bt_hci_parse(opt);
1882 }
1883 } else if (strstart(opt, "vhci", &endp)) {
1884 if (!*endp || *endp == ',') {
1885 if (*endp) {
1886 if (strstart(endp, ",vlan=", &p)) {
1887 vlan = strtol(p, (char **) &endp, 0);
1888 if (*endp) {
1889 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1890 return 1;
1891 }
1892 } else {
1893 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1894 return 1;
1895 }
1896 } else
1897 vlan = 0;
1898
1899 bt_vhci_add(vlan);
1900 return 0;
1901 }
1902 } else if (strstart(opt, "device:", &endp))
1903 return !bt_device_add(endp);
1904
1905 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1906 return 1;
1907}
1908
balrog1ae26a12008-09-28 23:19:47 +00001909/***********************************************************/
1910/* QEMU Block devices */
1911
balrog609497a2008-01-14 02:56:53 +00001912#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001913#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001914#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001915#define PFLASH_ALIAS "if=pflash"
1916#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001917#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001918
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001919QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001920{
1921 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001922 char optstr[1024];
1923 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001924
thse4bcb142007-12-02 04:51:10 +00001925 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001926 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001927 va_end(ap);
1928
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001929 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001930 if (!opts) {
1931 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1932 __FUNCTION__, optstr);
1933 return NULL;
1934 }
1935 if (file)
1936 qemu_opt_set(opts, "file", file);
1937 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001938}
1939
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001940DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001941{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001942 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001943
1944 /* seek interface, bus and unit */
1945
Blue Swirl72cf2d42009-09-12 07:36:22 +00001946 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001947 if (dinfo->type == type &&
1948 dinfo->bus == bus &&
1949 dinfo->unit == unit)
1950 return dinfo;
1951 }
thse4bcb142007-12-02 04:51:10 +00001952
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001953 return NULL;
thse4bcb142007-12-02 04:51:10 +00001954}
1955
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001956DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001957{
1958 DriveInfo *dinfo;
1959
Blue Swirl72cf2d42009-09-12 07:36:22 +00001960 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001961 if (strcmp(id, dinfo->id))
1962 continue;
1963 return dinfo;
1964 }
1965 return NULL;
1966}
1967
thsf60d39b2007-12-17 03:55:57 +00001968int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001969{
1970 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001971 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001972
1973 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001974 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001975 if(dinfo->type == type &&
1976 dinfo->bus > max_bus)
1977 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001978 }
1979 return max_bus;
1980}
1981
aliguorifa879c62009-01-07 17:32:33 +00001982const char *drive_get_serial(BlockDriverState *bdrv)
1983{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001984 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001985
Blue Swirl72cf2d42009-09-12 07:36:22 +00001986 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001987 if (dinfo->bdrv == bdrv)
1988 return dinfo->serial;
1989 }
aliguorifa879c62009-01-07 17:32:33 +00001990
1991 return "\0";
1992}
1993
Kevin Wolff7850092009-11-27 13:25:36 +01001994BlockInterfaceErrorAction drive_get_on_error(
1995 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +00001996{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001997 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001998
Kevin Wolffc072ec2009-11-27 13:25:36 +01001999 if (is_read) {
2000 return BLOCK_ERR_REPORT;
2001 }
2002
Blue Swirl72cf2d42009-09-12 07:36:22 +00002003 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002004 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002005 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002006 }
aliguori428c5702009-01-21 18:59:04 +00002007
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002008 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002009}
2010
aurel32a1620fa2008-04-29 05:58:01 +00002011static void bdrv_format_print(void *opaque, const char *name)
2012{
2013 fprintf(stderr, " %s", name);
2014}
2015
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002016void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +00002017{
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002018 qemu_opts_del(dinfo->opts);
2019 bdrv_delete(dinfo->bdrv);
2020 QTAILQ_REMOVE(&drives, dinfo, next);
2021 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +00002022}
2023
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002024static int parse_block_error_action(const char *buf, int is_read)
2025{
2026 if (!strcmp(buf, "ignore")) {
2027 return BLOCK_ERR_IGNORE;
2028 } else if (!is_read && !strcmp(buf, "enospc")) {
2029 return BLOCK_ERR_STOP_ENOSPC;
2030 } else if (!strcmp(buf, "stop")) {
2031 return BLOCK_ERR_STOP_ANY;
2032 } else if (!strcmp(buf, "report")) {
2033 return BLOCK_ERR_REPORT;
2034 } else {
2035 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
2036 buf, is_read ? "read" : "write");
2037 return -1;
2038 }
2039}
2040
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002041DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002042 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00002043{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002044 const char *buf;
2045 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00002046 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002047 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00002048 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002049 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002050 enum { MEDIA_DISK, MEDIA_CDROM } media;
2051 int bus_id, unit_id;
2052 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00002053 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002054 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002055 int max_devs;
2056 int index;
balrog33f00272007-12-24 14:33:24 +00002057 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002058 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002059 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002060 int bdrv_flags;
2061 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002062 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002063 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002064 int snapshot = 0;
2065
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002066 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002067
thse4bcb142007-12-02 04:51:10 +00002068 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002069 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002070
Gerd Hoffmann4d007812009-08-31 14:23:57 +02002071 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002072 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002073 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002074 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002075 } else {
thsf60d39b2007-12-17 03:55:57 +00002076 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002077 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002078 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002079 }
2080 media = MEDIA_DISK;
2081
2082 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002083 bus_id = qemu_opt_get_number(opts, "bus", 0);
2084 unit_id = qemu_opt_get_number(opts, "unit", -1);
2085 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002086
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002087 cyls = qemu_opt_get_number(opts, "cyls", 0);
2088 heads = qemu_opt_get_number(opts, "heads", 0);
2089 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002090
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002091 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +02002092 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +00002093
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002094 file = qemu_opt_get(opts, "file");
2095 serial = qemu_opt_get(opts, "serial");
2096
2097 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002098 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002099 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002100 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002101 max_devs = MAX_IDE_DEVS;
2102 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002103 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002104 max_devs = MAX_SCSI_DEVS;
2105 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002106 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002107 max_devs = 0;
2108 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002109 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002110 max_devs = 0;
2111 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002112 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002113 max_devs = 0;
2114 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002115 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002116 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002117 } else if (!strcmp(buf, "virtio")) {
2118 type = IF_VIRTIO;
2119 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002120 } else if (!strcmp(buf, "xen")) {
2121 type = IF_XEN;
2122 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002123 } else if (!strcmp(buf, "none")) {
2124 type = IF_NONE;
2125 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002126 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002127 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002128 return NULL;
thse4bcb142007-12-02 04:51:10 +00002129 }
2130 }
2131
thse4bcb142007-12-02 04:51:10 +00002132 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +00002133 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002134 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002135 return NULL;
thse4bcb142007-12-02 04:51:10 +00002136 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002137 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002138 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002139 return NULL;
thse4bcb142007-12-02 04:51:10 +00002140 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002141 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002142 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002143 return NULL;
thse4bcb142007-12-02 04:51:10 +00002144 }
2145 }
2146
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002147 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002148 if (!cyls) {
2149 fprintf(stderr,
2150 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002151 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002152 return NULL;
thse4bcb142007-12-02 04:51:10 +00002153 }
2154 if (!strcmp(buf, "none"))
2155 translation = BIOS_ATA_TRANSLATION_NONE;
2156 else if (!strcmp(buf, "lba"))
2157 translation = BIOS_ATA_TRANSLATION_LBA;
2158 else if (!strcmp(buf, "auto"))
2159 translation = BIOS_ATA_TRANSLATION_AUTO;
2160 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002161 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002162 return NULL;
thse4bcb142007-12-02 04:51:10 +00002163 }
2164 }
2165
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002166 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002167 if (!strcmp(buf, "disk")) {
2168 media = MEDIA_DISK;
2169 } else if (!strcmp(buf, "cdrom")) {
2170 if (cyls || secs || heads) {
2171 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002172 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002173 return NULL;
thse4bcb142007-12-02 04:51:10 +00002174 }
2175 media = MEDIA_CDROM;
2176 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002177 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002178 return NULL;
thse4bcb142007-12-02 04:51:10 +00002179 }
2180 }
2181
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002182 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002183 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002184 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002185 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002186 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002187 else if (!strcmp(buf, "writeback"))
2188 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002189 else {
2190 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002191 return NULL;
balrog33f00272007-12-24 14:33:24 +00002192 }
2193 }
2194
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002195#ifdef CONFIG_LINUX_AIO
2196 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2197 if (!strcmp(buf, "threads"))
2198 aio = 0;
2199 else if (!strcmp(buf, "native"))
2200 aio = 1;
2201 else {
2202 fprintf(stderr, "qemu: invalid aio option\n");
2203 return NULL;
2204 }
2205 }
2206#endif
2207
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002208 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002209 if (strcmp(buf, "?") == 0) {
2210 fprintf(stderr, "qemu: Supported formats:");
2211 bdrv_iterate_format(bdrv_format_print, NULL);
2212 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002213 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002214 }
Markus Armbrustereb852012009-10-27 18:41:44 +01002215 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00002216 if (!drv) {
2217 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002218 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002219 }
2220 }
2221
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002222 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002223 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002224 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002225 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002226 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002227 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002228
2229 on_write_error = parse_block_error_action(buf, 0);
2230 if (on_write_error < 0) {
2231 return NULL;
2232 }
2233 }
2234
2235 on_read_error = BLOCK_ERR_REPORT;
2236 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01002237 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002238 fprintf(stderr, "rerror is no supported by this format\n");
2239 return NULL;
2240 }
2241
2242 on_read_error = parse_block_error_action(buf, 1);
2243 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002244 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002245 }
2246 }
2247
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002248 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002249 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002250 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002251 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002252 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002253 }
2254
thse4bcb142007-12-02 04:51:10 +00002255 /* compute bus and unit according index */
2256
2257 if (index != -1) {
2258 if (bus_id != 0 || unit_id != -1) {
2259 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002260 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002261 return NULL;
thse4bcb142007-12-02 04:51:10 +00002262 }
2263 if (max_devs == 0)
2264 {
2265 unit_id = index;
2266 bus_id = 0;
2267 } else {
2268 unit_id = index % max_devs;
2269 bus_id = index / max_devs;
2270 }
2271 }
2272
2273 /* if user doesn't specify a unit_id,
2274 * try to find the first free
2275 */
2276
2277 if (unit_id == -1) {
2278 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002279 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002280 unit_id++;
2281 if (max_devs && unit_id >= max_devs) {
2282 unit_id -= max_devs;
2283 bus_id++;
2284 }
2285 }
2286 }
2287
2288 /* check unit id */
2289
2290 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002291 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2292 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002293 return NULL;
thse4bcb142007-12-02 04:51:10 +00002294 }
2295
2296 /*
2297 * ignore multiple definitions
2298 */
2299
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002300 if (drive_get(type, bus_id, unit_id) != NULL) {
2301 *fatal_error = 0;
2302 return NULL;
2303 }
thse4bcb142007-12-02 04:51:10 +00002304
2305 /* init */
2306
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002307 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002308 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002309 dinfo->id = qemu_strdup(buf);
2310 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002311 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002312 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002313 if (type == IF_IDE || type == IF_SCSI)
2314 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2315 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002316 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002317 devname, bus_id, mediastr, unit_id);
2318 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002319 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002320 devname, mediastr, unit_id);
2321 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002322 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002323 dinfo->devaddr = devaddr;
2324 dinfo->type = type;
2325 dinfo->bus = bus_id;
2326 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002327 dinfo->on_read_error = on_read_error;
2328 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002329 dinfo->opts = opts;
2330 if (serial)
2331 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002332 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002333
thsf60d39b2007-12-17 03:55:57 +00002334 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002335 case IF_IDE:
2336 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002337 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002338 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002339 switch(media) {
2340 case MEDIA_DISK:
2341 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002342 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2343 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002344 }
2345 break;
2346 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002347 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002348 break;
2349 }
2350 break;
2351 case IF_SD:
2352 /* FIXME: This isn't really a floppy, but it's a reasonable
2353 approximation. */
2354 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002355 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002356 break;
2357 case IF_PFLASH:
2358 case IF_MTD:
2359 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002360 case IF_VIRTIO:
2361 /* add virtio block device */
2362 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2363 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2364 qemu_opt_set(opts, "drive", dinfo->id);
2365 if (devaddr)
2366 qemu_opt_set(opts, "addr", devaddr);
2367 break;
Paul Brookaae94602009-05-14 22:35:06 +01002368 case IF_COUNT:
2369 abort();
thse4bcb142007-12-02 04:51:10 +00002370 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002371 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002372 *fatal_error = 0;
2373 return NULL;
2374 }
balrog33f00272007-12-24 14:33:24 +00002375 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002376 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002377 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002378 cache = 2; /* always use write-back with snapshot */
2379 }
2380 if (cache == 0) /* no caching */
2381 bdrv_flags |= BDRV_O_NOCACHE;
2382 else if (cache == 2) /* write-back */
2383 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002384
2385 if (aio == 1) {
2386 bdrv_flags |= BDRV_O_NATIVE_AIO;
2387 } else {
2388 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2389 }
2390
Naphtali Sprei59f26892009-10-26 16:25:16 +02002391 if (ro == 1) {
2392 if (type == IF_IDE) {
2393 fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
2394 return NULL;
2395 }
2396 (void)bdrv_set_read_only(dinfo->bdrv, 1);
2397 }
2398
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002399 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002400 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2401 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002402 return NULL;
thse4bcb142007-12-02 04:51:10 +00002403 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002404
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002405 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002406 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002407 *fatal_error = 0;
2408 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002409}
2410
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002411static int drive_init_func(QemuOpts *opts, void *opaque)
2412{
2413 QEMUMachine *machine = opaque;
2414 int fatal_error = 0;
2415
2416 if (drive_init(opts, machine, &fatal_error) == NULL) {
2417 if (fatal_error)
2418 return 1;
2419 }
2420 return 0;
2421}
2422
2423static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2424{
2425 if (NULL == qemu_opt_get(opts, "snapshot")) {
2426 qemu_opt_set(opts, "snapshot", "on");
2427 }
2428 return 0;
2429}
2430
Jan Kiszka76e30d02009-07-02 00:19:02 +02002431void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2432{
2433 boot_set_handler = func;
2434 boot_set_opaque = opaque;
2435}
2436
2437int qemu_boot_set(const char *boot_devices)
2438{
2439 if (!boot_set_handler) {
2440 return -EINVAL;
2441 }
2442 return boot_set_handler(boot_set_opaque, boot_devices);
2443}
2444
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002445static int parse_bootdevices(char *devices)
2446{
2447 /* We just do some generic consistency checks */
2448 const char *p;
2449 int bitmap = 0;
2450
2451 for (p = devices; *p != '\0'; p++) {
2452 /* Allowed boot devices are:
2453 * a-b: floppy disk drives
2454 * c-f: IDE disk drives
2455 * g-m: machine implementation dependant drives
2456 * n-p: network devices
2457 * It's up to each machine implementation to check if the given boot
2458 * devices match the actual hardware implementation and firmware
2459 * features.
2460 */
2461 if (*p < 'a' || *p > 'p') {
2462 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2463 exit(1);
2464 }
2465 if (bitmap & (1 << (*p - 'a'))) {
2466 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2467 exit(1);
2468 }
2469 bitmap |= 1 << (*p - 'a');
2470 }
2471 return bitmap;
2472}
2473
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002474static void restore_boot_devices(void *opaque)
2475{
2476 char *standard_boot_devices = opaque;
2477
2478 qemu_boot_set(standard_boot_devices);
2479
2480 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2481 qemu_free(standard_boot_devices);
2482}
2483
aliguori268a3622009-04-21 22:30:27 +00002484static void numa_add(const char *optarg)
2485{
2486 char option[128];
2487 char *endptr;
2488 unsigned long long value, endvalue;
2489 int nodenr;
2490
2491 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2492 if (!strcmp(option, "node")) {
2493 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2494 nodenr = nb_numa_nodes;
2495 } else {
2496 nodenr = strtoull(option, NULL, 10);
2497 }
2498
2499 if (get_param_value(option, 128, "mem", optarg) == 0) {
2500 node_mem[nodenr] = 0;
2501 } else {
2502 value = strtoull(option, &endptr, 0);
2503 switch (*endptr) {
2504 case 0: case 'M': case 'm':
2505 value <<= 20;
2506 break;
2507 case 'G': case 'g':
2508 value <<= 30;
2509 break;
2510 }
2511 node_mem[nodenr] = value;
2512 }
2513 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2514 node_cpumask[nodenr] = 0;
2515 } else {
2516 value = strtoull(option, &endptr, 10);
2517 if (value >= 64) {
2518 value = 63;
2519 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2520 } else {
2521 if (*endptr == '-') {
2522 endvalue = strtoull(endptr+1, &endptr, 10);
2523 if (endvalue >= 63) {
2524 endvalue = 62;
2525 fprintf(stderr,
2526 "only 63 CPUs in NUMA mode supported.\n");
2527 }
2528 value = (1 << (endvalue + 1)) - (1 << value);
2529 } else {
2530 value = 1 << value;
2531 }
2532 }
2533 node_cpumask[nodenr] = value;
2534 }
2535 nb_numa_nodes++;
2536 }
2537 return;
2538}
2539
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002540static void smp_parse(const char *optarg)
2541{
2542 int smp, sockets = 0, threads = 0, cores = 0;
2543 char *endptr;
2544 char option[128];
2545
2546 smp = strtoul(optarg, &endptr, 10);
2547 if (endptr != optarg) {
2548 if (*endptr == ',') {
2549 endptr++;
2550 }
2551 }
2552 if (get_param_value(option, 128, "sockets", endptr) != 0)
2553 sockets = strtoull(option, NULL, 10);
2554 if (get_param_value(option, 128, "cores", endptr) != 0)
2555 cores = strtoull(option, NULL, 10);
2556 if (get_param_value(option, 128, "threads", endptr) != 0)
2557 threads = strtoull(option, NULL, 10);
2558 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2559 max_cpus = strtoull(option, NULL, 10);
2560
2561 /* compute missing values, prefer sockets over cores over threads */
2562 if (smp == 0 || sockets == 0) {
2563 sockets = sockets > 0 ? sockets : 1;
2564 cores = cores > 0 ? cores : 1;
2565 threads = threads > 0 ? threads : 1;
2566 if (smp == 0) {
2567 smp = cores * threads * sockets;
2568 } else {
2569 sockets = smp / (cores * threads);
2570 }
2571 } else {
2572 if (cores == 0) {
2573 threads = threads > 0 ? threads : 1;
2574 cores = smp / (sockets * threads);
2575 } else {
2576 if (sockets == 0) {
2577 sockets = smp / (cores * threads);
2578 } else {
2579 threads = smp / (cores * sockets);
2580 }
2581 }
2582 }
2583 smp_cpus = smp;
2584 smp_cores = cores > 0 ? cores : 1;
2585 smp_threads = threads > 0 ? threads : 1;
2586 if (max_cpus == 0)
2587 max_cpus = smp_cpus;
2588}
2589
bellard330d0412003-07-26 18:11:40 +00002590/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002591/* USB devices */
2592
aliguoric0f4ce72009-03-05 23:01:01 +00002593static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002594{
2595 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002596 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00002597
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002598 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002599 return -1;
2600
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01002601 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2602 dev = usbdevice_create(devname);
2603 if (dev)
2604 goto done;
2605
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002606 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002607 if (strstart(devname, "host:", &p)) {
2608 dev = usb_host_device_open(p);
balrog6c9f8862008-07-17 20:47:13 +00002609 } else if (strstart(devname, "net:", &p)) {
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002610 QemuOpts *opts;
2611 int idx;
balrog6c9f8862008-07-17 20:47:13 +00002612
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002613 opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
2614 if (!opts) {
balrog6c9f8862008-07-17 20:47:13 +00002615 return -1;
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002616 }
2617
2618 qemu_opt_set(opts, "type", "nic");
2619 qemu_opt_set(opts, "model", "usb");
2620
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01002621 idx = net_client_init(NULL, opts, 0);
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002622 if (idx == -1) {
2623 return -1;
2624 }
2625
2626 dev = usb_net_init(&nd_table[idx]);
balrogdc72ac12008-11-09 00:04:26 +00002627 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2628 dev = usb_bt_init(devname[2] ? hci_init(p) :
2629 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002630 } else {
2631 return -1;
2632 }
pbrook0d92ed32006-05-21 16:30:15 +00002633 if (!dev)
2634 return -1;
2635
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002636done:
bellarda594cfb2005-11-06 16:13:29 +00002637 return 0;
2638}
2639
aliguori1f3870a2008-08-21 19:27:48 +00002640static int usb_device_del(const char *devname)
2641{
2642 int bus_num, addr;
2643 const char *p;
2644
aliguori5d0c5752008-09-14 01:07:41 +00002645 if (strstart(devname, "host:", &p))
2646 return usb_host_device_close(p);
2647
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002648 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002649 return -1;
2650
2651 p = strchr(devname, '.');
2652 if (!p)
2653 return -1;
2654 bus_num = strtoul(devname, NULL, 0);
2655 addr = strtoul(p + 1, NULL, 0);
2656
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002657 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002658}
2659
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002660static int usb_parse(const char *cmdline)
2661{
2662 return usb_device_add(cmdline, 0);
2663}
2664
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002665void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002666{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002667 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002668}
2669
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002670void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002671{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002672 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002673}
2674
bellardf7cce892004-12-08 22:21:25 +00002675/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002676/* PCMCIA/Cardbus */
2677
2678static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002679 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002680 struct pcmcia_socket_entry_s *next;
2681} *pcmcia_sockets = 0;
2682
Paul Brookbc24a222009-05-10 01:44:56 +01002683void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002684{
2685 struct pcmcia_socket_entry_s *entry;
2686
2687 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2688 entry->socket = socket;
2689 entry->next = pcmcia_sockets;
2690 pcmcia_sockets = entry;
2691}
2692
Paul Brookbc24a222009-05-10 01:44:56 +01002693void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002694{
2695 struct pcmcia_socket_entry_s *entry, **ptr;
2696
2697 ptr = &pcmcia_sockets;
2698 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2699 if (entry->socket == socket) {
2700 *ptr = entry->next;
2701 qemu_free(entry);
2702 }
2703}
2704
aliguori376253e2009-03-05 23:01:23 +00002705void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002706{
2707 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002708
balrog201a51f2007-04-30 00:51:09 +00002709 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002710 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002711
2712 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002713 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2714 iter->socket->attached ? iter->socket->card_string :
2715 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002716}
2717
2718/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002719/* register display */
2720
aliguori7b5d76d2009-03-13 15:02:13 +00002721struct DisplayAllocator default_allocator = {
2722 defaultallocator_create_displaysurface,
2723 defaultallocator_resize_displaysurface,
2724 defaultallocator_free_displaysurface
2725};
2726
aliguori3023f332009-01-16 19:04:14 +00002727void register_displaystate(DisplayState *ds)
2728{
2729 DisplayState **s;
2730 s = &display_state;
2731 while (*s != NULL)
2732 s = &(*s)->next;
2733 ds->next = NULL;
2734 *s = ds;
2735}
2736
2737DisplayState *get_displaystate(void)
2738{
2739 return display_state;
2740}
2741
aliguori7b5d76d2009-03-13 15:02:13 +00002742DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2743{
2744 if(ds->allocator == &default_allocator) ds->allocator = da;
2745 return ds->allocator;
2746}
2747
ths2ff89792007-06-21 23:34:19 +00002748/* dumb display */
2749
aliguori8f391ab2009-01-19 16:34:10 +00002750static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002751{
aliguori8f391ab2009-01-19 16:34:10 +00002752 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002753 ds->allocator = &default_allocator;
2754 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002755 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002756}
2757
2758/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002759/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002760
bellardc4b1fcc2004-03-14 21:44:30 +00002761typedef struct IOHandlerRecord {
2762 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002763 IOCanRWHandler *fd_read_poll;
2764 IOHandler *fd_read;
2765 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002766 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002767 void *opaque;
2768 /* temporary data */
2769 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002770 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002771} IOHandlerRecord;
2772
bellard8a7ddc32004-03-31 19:00:16 +00002773static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002774
bellard7c9d8e02005-11-15 22:16:05 +00002775/* XXX: fd_read_poll should be suppressed, but an API change is
2776 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002777int qemu_set_fd_handler2(int fd,
2778 IOCanRWHandler *fd_read_poll,
2779 IOHandler *fd_read,
2780 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002781 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002782{
bellard8a7ddc32004-03-31 19:00:16 +00002783 IOHandlerRecord **pioh, *ioh;
2784
bellard7c9d8e02005-11-15 22:16:05 +00002785 if (!fd_read && !fd_write) {
2786 pioh = &first_io_handler;
2787 for(;;) {
2788 ioh = *pioh;
2789 if (ioh == NULL)
2790 break;
2791 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002792 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002793 break;
2794 }
2795 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002796 }
bellard7c9d8e02005-11-15 22:16:05 +00002797 } else {
2798 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2799 if (ioh->fd == fd)
2800 goto found;
2801 }
2802 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002803 ioh->next = first_io_handler;
2804 first_io_handler = ioh;
2805 found:
2806 ioh->fd = fd;
2807 ioh->fd_read_poll = fd_read_poll;
2808 ioh->fd_read = fd_read;
2809 ioh->fd_write = fd_write;
2810 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002811 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002812 }
bellard7c9d8e02005-11-15 22:16:05 +00002813 return 0;
2814}
2815
ths5fafdf22007-09-16 21:08:06 +00002816int qemu_set_fd_handler(int fd,
2817 IOHandler *fd_read,
2818 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002819 void *opaque)
2820{
2821 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002822}
2823
aliguori56f3a5d2008-10-31 18:07:17 +00002824#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002825/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002826/* Polling handling */
2827
2828typedef struct PollingEntry {
2829 PollingFunc *func;
2830 void *opaque;
2831 struct PollingEntry *next;
2832} PollingEntry;
2833
2834static PollingEntry *first_polling_entry;
2835
2836int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2837{
2838 PollingEntry **ppe, *pe;
2839 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002840 pe->func = func;
2841 pe->opaque = opaque;
2842 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2843 *ppe = pe;
2844 return 0;
2845}
2846
2847void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2848{
2849 PollingEntry **ppe, *pe;
2850 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2851 pe = *ppe;
2852 if (pe->func == func && pe->opaque == opaque) {
2853 *ppe = pe->next;
2854 qemu_free(pe);
2855 break;
2856 }
2857 }
2858}
2859
bellarda18e5242006-06-25 17:18:27 +00002860/***********************************************************/
2861/* Wait objects support */
2862typedef struct WaitObjects {
2863 int num;
2864 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2865 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2866 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2867} WaitObjects;
2868
2869static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002870
bellarda18e5242006-06-25 17:18:27 +00002871int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2872{
2873 WaitObjects *w = &wait_objects;
2874
2875 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2876 return -1;
2877 w->events[w->num] = handle;
2878 w->func[w->num] = func;
2879 w->opaque[w->num] = opaque;
2880 w->num++;
2881 return 0;
2882}
2883
2884void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2885{
2886 int i, found;
2887 WaitObjects *w = &wait_objects;
2888
2889 found = 0;
2890 for (i = 0; i < w->num; i++) {
2891 if (w->events[i] == handle)
2892 found = 1;
2893 if (found) {
2894 w->events[i] = w->events[i + 1];
2895 w->func[i] = w->func[i + 1];
2896 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002897 }
bellarda18e5242006-06-25 17:18:27 +00002898 }
2899 if (found)
2900 w->num--;
2901}
2902#endif
2903
bellard8a7ddc32004-03-31 19:00:16 +00002904/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002905/* ram save/restore */
2906
Juan Quintela94fb0902009-09-10 03:04:23 +02002907#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002908#define RAM_SAVE_FLAG_COMPRESS 0x02
2909#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2910#define RAM_SAVE_FLAG_PAGE 0x08
2911#define RAM_SAVE_FLAG_EOS 0x10
2912
2913static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002914{
aliguori475e4272008-10-06 20:21:51 +00002915 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2916 uint32_t *array = (uint32_t *)page;
2917 int i;
ths3b46e622007-09-17 08:09:54 +00002918
aliguori475e4272008-10-06 20:21:51 +00002919 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2920 if (array[i] != val)
2921 return 0;
bellardc88676f2006-08-06 13:36:11 +00002922 }
aliguori475e4272008-10-06 20:21:51 +00002923
2924 return 1;
bellardc88676f2006-08-06 13:36:11 +00002925}
2926
aliguori475e4272008-10-06 20:21:51 +00002927static int ram_save_block(QEMUFile *f)
2928{
Anthony Liguoric227f092009-10-01 16:12:16 -05002929 static ram_addr_t current_addr = 0;
2930 ram_addr_t saved_addr = current_addr;
2931 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002932 int found = 0;
2933
pbrook94a6b542009-04-11 17:15:54 +00002934 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002935 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002936 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002937
2938 cpu_physical_memory_reset_dirty(current_addr,
2939 current_addr + TARGET_PAGE_SIZE,
2940 MIGRATION_DIRTY_FLAG);
2941
pbrook5579c7f2009-04-11 14:47:08 +00002942 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002943
pbrook5579c7f2009-04-11 14:47:08 +00002944 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002945 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002946 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002947 } else {
2948 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002949 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002950 }
2951
2952 found = 1;
2953 break;
2954 }
2955 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002956 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002957 }
2958
2959 return found;
2960}
2961
Jan Kiszka84307932009-12-02 11:29:38 +01002962static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00002963
Anthony Liguoric227f092009-10-01 16:12:16 -05002964static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002965{
Anthony Liguoric227f092009-10-01 16:12:16 -05002966 ram_addr_t addr;
2967 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002968
pbrook94a6b542009-04-11 17:15:54 +00002969 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002970 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2971 count++;
2972 }
2973
2974 return count;
2975}
2976
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002977uint64_t ram_bytes_remaining(void)
2978{
2979 return ram_save_remaining() * TARGET_PAGE_SIZE;
2980}
2981
2982uint64_t ram_bytes_transferred(void)
2983{
2984 return bytes_transferred;
2985}
2986
2987uint64_t ram_bytes_total(void)
2988{
2989 return last_ram_offset;
2990}
2991
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002992static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00002993{
Anthony Liguoric227f092009-10-01 16:12:16 -05002994 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002995 uint64_t bytes_transferred_last;
2996 double bwidth = 0;
2997 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002998
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002999 if (stage < 0) {
3000 cpu_physical_memory_set_dirty_tracking(0);
3001 return 0;
3002 }
3003
Jan Kiszka9fa06382009-05-22 23:51:45 +02003004 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003005 qemu_file_set_error(f);
3006 return 0;
3007 }
3008
aliguori475e4272008-10-06 20:21:51 +00003009 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01003010 bytes_transferred = 0;
3011
aliguori475e4272008-10-06 20:21:51 +00003012 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003013 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003014 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3015 cpu_physical_memory_set_dirty(addr);
3016 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003017
aliguori475e4272008-10-06 20:21:51 +00003018 /* Enable dirty memory tracking */
3019 cpu_physical_memory_set_dirty_tracking(1);
3020
pbrook94a6b542009-04-11 17:15:54 +00003021 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003022 }
3023
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003024 bytes_transferred_last = bytes_transferred;
3025 bwidth = get_clock();
3026
aliguori475e4272008-10-06 20:21:51 +00003027 while (!qemu_file_rate_limit(f)) {
3028 int ret;
3029
3030 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003031 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003032 if (ret == 0) /* no more blocks */
3033 break;
3034 }
3035
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003036 bwidth = get_clock() - bwidth;
3037 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3038
3039 /* if we haven't transferred anything this round, force expected_time to a
3040 * a very high value, but without crashing */
3041 if (bwidth == 0)
3042 bwidth = 0.000001;
3043
aliguori475e4272008-10-06 20:21:51 +00003044 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00003045 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003046 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003047 while (ram_save_block(f) != 0) {
3048 bytes_transferred += TARGET_PAGE_SIZE;
3049 }
aliguori8215e912009-04-05 19:30:55 +00003050 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003051 }
3052
3053 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3054
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003055 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3056
3057 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003058}
3059
aliguori475e4272008-10-06 20:21:51 +00003060static int ram_load(QEMUFile *f, void *opaque, int version_id)
3061{
Anthony Liguoric227f092009-10-01 16:12:16 -05003062 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003063 int flags;
3064
aliguori475e4272008-10-06 20:21:51 +00003065 if (version_id != 3)
3066 return -EINVAL;
3067
3068 do {
3069 addr = qemu_get_be64(f);
3070
3071 flags = addr & ~TARGET_PAGE_MASK;
3072 addr &= TARGET_PAGE_MASK;
3073
3074 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003075 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003076 return -EINVAL;
3077 }
3078
aliguori475e4272008-10-06 20:21:51 +00003079 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3080 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003081 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3082#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003083 if (ch == 0 &&
3084 (!kvm_enabled() || kvm_has_sync_mmu())) {
3085 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003086 }
Anthony Liguori30868442009-06-17 16:46:12 -05003087#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01003088 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00003089 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01003090 }
3091 if (qemu_file_has_error(f)) {
3092 return -EIO;
3093 }
aliguori475e4272008-10-06 20:21:51 +00003094 } while (!(flags & RAM_SAVE_FLAG_EOS));
3095
bellardc88676f2006-08-06 13:36:11 +00003096 return 0;
3097}
3098
aliguori9e472e12008-10-08 19:50:24 +00003099void qemu_service_io(void)
3100{
aliguorid9f75a42009-04-24 18:03:11 +00003101 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003102}
3103
bellard8a7ddc32004-03-31 19:00:16 +00003104/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003105/* machine registration */
3106
blueswir1bdaf78e2008-10-04 07:24:27 +00003107static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003108QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003109
3110int qemu_register_machine(QEMUMachine *m)
3111{
3112 QEMUMachine **pm;
3113 pm = &first_machine;
3114 while (*pm != NULL)
3115 pm = &(*pm)->next;
3116 m->next = NULL;
3117 *pm = m;
3118 return 0;
3119}
3120
pbrook9596ebb2007-11-18 01:44:38 +00003121static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003122{
3123 QEMUMachine *m;
3124
3125 for(m = first_machine; m != NULL; m = m->next) {
3126 if (!strcmp(m->name, name))
3127 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003128 if (m->alias && !strcmp(m->alias, name))
3129 return m;
bellardcc1daa42005-06-05 14:49:17 +00003130 }
3131 return NULL;
3132}
3133
Anthony Liguori0c257432009-05-21 20:41:01 -05003134static QEMUMachine *find_default_machine(void)
3135{
3136 QEMUMachine *m;
3137
3138 for(m = first_machine; m != NULL; m = m->next) {
3139 if (m->is_default) {
3140 return m;
3141 }
3142 }
3143 return NULL;
3144}
3145
bellardcc1daa42005-06-05 14:49:17 +00003146/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003147/* main execution loop */
3148
pbrook9596ebb2007-11-18 01:44:38 +00003149static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003150{
aliguori7d957bd2009-01-15 22:14:11 +00003151 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003152 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003153 DisplayChangeListener *dcl = ds->listeners;
3154
3155 dpy_refresh(ds);
3156
3157 while (dcl != NULL) {
3158 if (dcl->gui_timer_interval &&
3159 dcl->gui_timer_interval < interval)
3160 interval = dcl->gui_timer_interval;
3161 dcl = dcl->next;
3162 }
3163 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003164}
3165
blueswir19043b622009-01-21 19:28:13 +00003166static void nographic_update(void *opaque)
3167{
3168 uint64_t interval = GUI_REFRESH_INTERVAL;
3169
3170 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3171}
3172
bellard0bd48852005-11-11 00:00:47 +00003173struct vm_change_state_entry {
3174 VMChangeStateHandler *cb;
3175 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003176 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003177};
3178
Blue Swirl72cf2d42009-09-12 07:36:22 +00003179static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003180
3181VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3182 void *opaque)
3183{
3184 VMChangeStateEntry *e;
3185
3186 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003187
3188 e->cb = cb;
3189 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003190 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003191 return e;
3192}
3193
3194void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3195{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003196 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003197 qemu_free (e);
3198}
3199
aliguori9781e042009-01-22 17:15:29 +00003200static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003201{
3202 VMChangeStateEntry *e;
3203
3204 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003205 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003206 }
3207}
3208
aliguorid6dc3d42009-04-24 18:04:07 +00003209static void resume_all_vcpus(void);
3210static void pause_all_vcpus(void);
3211
bellard8a7ddc32004-03-31 19:00:16 +00003212void vm_start(void)
3213{
3214 if (!vm_running) {
3215 cpu_enable_ticks();
3216 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003217 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003218 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003219 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003220 }
3221}
3222
bellardbb0c6722004-06-20 12:37:32 +00003223/* reset/shutdown handler */
3224
3225typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003226 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003227 QEMUResetHandler *func;
3228 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003229} QEMUResetEntry;
3230
Blue Swirl72cf2d42009-09-12 07:36:22 +00003231static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3232 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003233static int reset_requested;
3234static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003235static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003236static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003237static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003238
aurel32cf7a2fe2008-03-18 06:53:05 +00003239int qemu_shutdown_requested(void)
3240{
3241 int r = shutdown_requested;
3242 shutdown_requested = 0;
3243 return r;
3244}
3245
3246int qemu_reset_requested(void)
3247{
3248 int r = reset_requested;
3249 reset_requested = 0;
3250 return r;
3251}
3252
3253int qemu_powerdown_requested(void)
3254{
3255 int r = powerdown_requested;
3256 powerdown_requested = 0;
3257 return r;
3258}
3259
aliguorie5689022009-04-24 18:03:54 +00003260static int qemu_debug_requested(void)
3261{
3262 int r = debug_requested;
3263 debug_requested = 0;
3264 return r;
3265}
3266
aliguori6e29f5d2009-04-24 18:04:02 +00003267static int qemu_vmstop_requested(void)
3268{
3269 int r = vmstop_requested;
3270 vmstop_requested = 0;
3271 return r;
3272}
3273
3274static void do_vm_stop(int reason)
3275{
3276 if (vm_running) {
3277 cpu_disable_ticks();
3278 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003279 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003280 vm_state_notify(0, reason);
3281 }
3282}
3283
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003284void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003285{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003286 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003287
bellardbb0c6722004-06-20 12:37:32 +00003288 re->func = func;
3289 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003290 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003291}
3292
Jan Kiszkadda9b292009-07-02 00:19:02 +02003293void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003294{
3295 QEMUResetEntry *re;
3296
Blue Swirl72cf2d42009-09-12 07:36:22 +00003297 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003298 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003299 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003300 qemu_free(re);
3301 return;
3302 }
3303 }
3304}
3305
3306void qemu_system_reset(void)
3307{
3308 QEMUResetEntry *re, *nre;
3309
3310 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003311 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003312 re->func(re->opaque);
3313 }
3314}
3315
3316void qemu_system_reset_request(void)
3317{
bellardd1beab82006-10-02 19:44:22 +00003318 if (no_reboot) {
3319 shutdown_requested = 1;
3320 } else {
3321 reset_requested = 1;
3322 }
aliguorid9f75a42009-04-24 18:03:11 +00003323 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003324}
3325
3326void qemu_system_shutdown_request(void)
3327{
3328 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003329 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003330}
3331
bellard34751872005-07-02 14:31:34 +00003332void qemu_system_powerdown_request(void)
3333{
3334 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003335 qemu_notify_event();
3336}
3337
aliguorid6dc3d42009-04-24 18:04:07 +00003338#ifdef CONFIG_IOTHREAD
3339static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003340{
aliguorid6dc3d42009-04-24 18:04:07 +00003341 vmstop_requested = reason;
3342 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003343}
aliguorid6dc3d42009-04-24 18:04:07 +00003344#endif
bellardbb0c6722004-06-20 12:37:32 +00003345
aliguori50317c72009-04-24 18:03:29 +00003346#ifndef _WIN32
3347static int io_thread_fd = -1;
3348
3349static void qemu_event_increment(void)
3350{
3351 static const char byte = 0;
3352
3353 if (io_thread_fd == -1)
3354 return;
3355
3356 write(io_thread_fd, &byte, sizeof(byte));
3357}
3358
3359static void qemu_event_read(void *opaque)
3360{
3361 int fd = (unsigned long)opaque;
3362 ssize_t len;
3363
3364 /* Drain the notify pipe */
3365 do {
3366 char buffer[512];
3367 len = read(fd, buffer, sizeof(buffer));
3368 } while ((len == -1 && errno == EINTR) || len > 0);
3369}
3370
3371static int qemu_event_init(void)
3372{
3373 int err;
3374 int fds[2];
3375
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003376 err = qemu_pipe(fds);
aliguori50317c72009-04-24 18:03:29 +00003377 if (err == -1)
3378 return -errno;
3379
3380 err = fcntl_setfl(fds[0], O_NONBLOCK);
3381 if (err < 0)
3382 goto fail;
3383
3384 err = fcntl_setfl(fds[1], O_NONBLOCK);
3385 if (err < 0)
3386 goto fail;
3387
3388 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3389 (void *)(unsigned long)fds[0]);
3390
3391 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003392 return 0;
3393
aliguori50317c72009-04-24 18:03:29 +00003394fail:
3395 close(fds[0]);
3396 close(fds[1]);
3397 return err;
3398}
3399#else
3400HANDLE qemu_event_handle;
3401
3402static void dummy_event_handler(void *opaque)
3403{
3404}
3405
3406static int qemu_event_init(void)
3407{
3408 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3409 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003410 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003411 return -1;
3412 }
3413 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3414 return 0;
3415}
3416
3417static void qemu_event_increment(void)
3418{
malcde1c90c2009-09-27 14:38:18 +04003419 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003420 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003421 GetLastError());
3422 exit (1);
3423 }
aliguori50317c72009-04-24 18:03:29 +00003424}
3425#endif
3426
aliguorid6dc3d42009-04-24 18:04:07 +00003427static int cpu_can_run(CPUState *env)
3428{
3429 if (env->stop)
3430 return 0;
3431 if (env->stopped)
3432 return 0;
3433 return 1;
3434}
3435
3436#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003437static int qemu_init_main_loop(void)
3438{
aliguori50317c72009-04-24 18:03:29 +00003439 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003440}
3441
aliguori0bf46a42009-04-24 18:03:41 +00003442void qemu_init_vcpu(void *_env)
3443{
3444 CPUState *env = _env;
3445
3446 if (kvm_enabled())
3447 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003448 env->nr_cores = smp_cores;
3449 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003450 return;
3451}
3452
aliguori8edac962009-04-24 18:03:45 +00003453int qemu_cpu_self(void *env)
3454{
3455 return 1;
3456}
3457
aliguorid6dc3d42009-04-24 18:04:07 +00003458static void resume_all_vcpus(void)
3459{
3460}
3461
3462static void pause_all_vcpus(void)
3463{
3464}
3465
aliguori8edac962009-04-24 18:03:45 +00003466void qemu_cpu_kick(void *env)
3467{
3468 return;
3469}
3470
aliguorid6dc3d42009-04-24 18:04:07 +00003471void qemu_notify_event(void)
3472{
3473 CPUState *env = cpu_single_env;
3474
3475 if (env) {
3476 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003477 }
aliguorid6dc3d42009-04-24 18:04:07 +00003478}
3479
Glauber Costad549db52009-10-07 16:38:03 -03003480void qemu_mutex_lock_iothread(void) {}
3481void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00003482
aliguori6e29f5d2009-04-24 18:04:02 +00003483void vm_stop(int reason)
3484{
3485 do_vm_stop(reason);
3486}
3487
aliguorid6dc3d42009-04-24 18:04:07 +00003488#else /* CONFIG_IOTHREAD */
3489
3490#include "qemu-thread.h"
3491
3492QemuMutex qemu_global_mutex;
3493static QemuMutex qemu_fair_mutex;
3494
3495static QemuThread io_thread;
3496
3497static QemuThread *tcg_cpu_thread;
3498static QemuCond *tcg_halt_cond;
3499
3500static int qemu_system_ready;
3501/* cpu creation */
3502static QemuCond qemu_cpu_cond;
3503/* system init */
3504static QemuCond qemu_system_cond;
3505static QemuCond qemu_pause_cond;
3506
3507static void block_io_signals(void);
3508static void unblock_io_signals(void);
3509static int tcg_has_work(void);
3510
3511static int qemu_init_main_loop(void)
3512{
3513 int ret;
3514
3515 ret = qemu_event_init();
3516 if (ret)
3517 return ret;
3518
3519 qemu_cond_init(&qemu_pause_cond);
3520 qemu_mutex_init(&qemu_fair_mutex);
3521 qemu_mutex_init(&qemu_global_mutex);
3522 qemu_mutex_lock(&qemu_global_mutex);
3523
3524 unblock_io_signals();
3525 qemu_thread_self(&io_thread);
3526
3527 return 0;
3528}
3529
3530static void qemu_wait_io_event(CPUState *env)
3531{
3532 while (!tcg_has_work())
3533 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3534
3535 qemu_mutex_unlock(&qemu_global_mutex);
3536
3537 /*
3538 * Users of qemu_global_mutex can be starved, having no chance
3539 * to acquire it since this path will get to it first.
3540 * So use another lock to provide fairness.
3541 */
3542 qemu_mutex_lock(&qemu_fair_mutex);
3543 qemu_mutex_unlock(&qemu_fair_mutex);
3544
3545 qemu_mutex_lock(&qemu_global_mutex);
3546 if (env->stop) {
3547 env->stop = 0;
3548 env->stopped = 1;
3549 qemu_cond_signal(&qemu_pause_cond);
3550 }
3551}
3552
3553static int qemu_cpu_exec(CPUState *env);
3554
3555static void *kvm_cpu_thread_fn(void *arg)
3556{
3557 CPUState *env = arg;
3558
3559 block_io_signals();
3560 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003561 if (kvm_enabled())
3562 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003563
3564 /* signal CPU creation */
3565 qemu_mutex_lock(&qemu_global_mutex);
3566 env->created = 1;
3567 qemu_cond_signal(&qemu_cpu_cond);
3568
3569 /* and wait for machine initialization */
3570 while (!qemu_system_ready)
3571 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3572
3573 while (1) {
3574 if (cpu_can_run(env))
3575 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003576 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003577 }
3578
3579 return NULL;
3580}
3581
3582static void tcg_cpu_exec(void);
3583
3584static void *tcg_cpu_thread_fn(void *arg)
3585{
3586 CPUState *env = arg;
3587
3588 block_io_signals();
3589 qemu_thread_self(env->thread);
3590
3591 /* signal CPU creation */
3592 qemu_mutex_lock(&qemu_global_mutex);
3593 for (env = first_cpu; env != NULL; env = env->next_cpu)
3594 env->created = 1;
3595 qemu_cond_signal(&qemu_cpu_cond);
3596
3597 /* and wait for machine initialization */
3598 while (!qemu_system_ready)
3599 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3600
3601 while (1) {
3602 tcg_cpu_exec();
3603 qemu_wait_io_event(cur_cpu);
3604 }
3605
3606 return NULL;
3607}
3608
3609void qemu_cpu_kick(void *_env)
3610{
3611 CPUState *env = _env;
3612 qemu_cond_broadcast(env->halt_cond);
3613 if (kvm_enabled())
3614 qemu_thread_signal(env->thread, SIGUSR1);
3615}
3616
Glauber Costae5bc2012009-09-28 15:27:44 -03003617int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003618{
Glauber Costae5bc2012009-09-28 15:27:44 -03003619 CPUState *env = _env;
3620 QemuThread this;
3621
3622 qemu_thread_self(&this);
3623
3624 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003625}
3626
3627static void cpu_signal(int sig)
3628{
3629 if (cpu_single_env)
3630 cpu_exit(cpu_single_env);
3631}
3632
3633static void block_io_signals(void)
3634{
3635 sigset_t set;
3636 struct sigaction sigact;
3637
3638 sigemptyset(&set);
3639 sigaddset(&set, SIGUSR2);
3640 sigaddset(&set, SIGIO);
3641 sigaddset(&set, SIGALRM);
3642 pthread_sigmask(SIG_BLOCK, &set, NULL);
3643
3644 sigemptyset(&set);
3645 sigaddset(&set, SIGUSR1);
3646 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3647
3648 memset(&sigact, 0, sizeof(sigact));
3649 sigact.sa_handler = cpu_signal;
3650 sigaction(SIGUSR1, &sigact, NULL);
3651}
3652
3653static void unblock_io_signals(void)
3654{
3655 sigset_t set;
3656
3657 sigemptyset(&set);
3658 sigaddset(&set, SIGUSR2);
3659 sigaddset(&set, SIGIO);
3660 sigaddset(&set, SIGALRM);
3661 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3662
3663 sigemptyset(&set);
3664 sigaddset(&set, SIGUSR1);
3665 pthread_sigmask(SIG_BLOCK, &set, NULL);
3666}
3667
3668static void qemu_signal_lock(unsigned int msecs)
3669{
3670 qemu_mutex_lock(&qemu_fair_mutex);
3671
3672 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3673 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3674 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3675 break;
3676 }
3677 qemu_mutex_unlock(&qemu_fair_mutex);
3678}
3679
Glauber Costad549db52009-10-07 16:38:03 -03003680void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003681{
3682 if (kvm_enabled()) {
3683 qemu_mutex_lock(&qemu_fair_mutex);
3684 qemu_mutex_lock(&qemu_global_mutex);
3685 qemu_mutex_unlock(&qemu_fair_mutex);
3686 } else
3687 qemu_signal_lock(100);
3688}
3689
Glauber Costad549db52009-10-07 16:38:03 -03003690void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003691{
3692 qemu_mutex_unlock(&qemu_global_mutex);
3693}
3694
3695static int all_vcpus_paused(void)
3696{
3697 CPUState *penv = first_cpu;
3698
3699 while (penv) {
3700 if (!penv->stopped)
3701 return 0;
3702 penv = (CPUState *)penv->next_cpu;
3703 }
3704
3705 return 1;
3706}
3707
3708static void pause_all_vcpus(void)
3709{
3710 CPUState *penv = first_cpu;
3711
3712 while (penv) {
3713 penv->stop = 1;
3714 qemu_thread_signal(penv->thread, SIGUSR1);
3715 qemu_cpu_kick(penv);
3716 penv = (CPUState *)penv->next_cpu;
3717 }
3718
3719 while (!all_vcpus_paused()) {
3720 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3721 penv = first_cpu;
3722 while (penv) {
3723 qemu_thread_signal(penv->thread, SIGUSR1);
3724 penv = (CPUState *)penv->next_cpu;
3725 }
3726 }
3727}
3728
3729static void resume_all_vcpus(void)
3730{
3731 CPUState *penv = first_cpu;
3732
3733 while (penv) {
3734 penv->stop = 0;
3735 penv->stopped = 0;
3736 qemu_thread_signal(penv->thread, SIGUSR1);
3737 qemu_cpu_kick(penv);
3738 penv = (CPUState *)penv->next_cpu;
3739 }
3740}
3741
3742static void tcg_init_vcpu(void *_env)
3743{
3744 CPUState *env = _env;
3745 /* share a single thread for all cpus with TCG */
3746 if (!tcg_cpu_thread) {
3747 env->thread = qemu_mallocz(sizeof(QemuThread));
3748 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3749 qemu_cond_init(env->halt_cond);
3750 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3751 while (env->created == 0)
3752 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3753 tcg_cpu_thread = env->thread;
3754 tcg_halt_cond = env->halt_cond;
3755 } else {
3756 env->thread = tcg_cpu_thread;
3757 env->halt_cond = tcg_halt_cond;
3758 }
3759}
3760
3761static void kvm_start_vcpu(CPUState *env)
3762{
aliguorid6dc3d42009-04-24 18:04:07 +00003763 env->thread = qemu_mallocz(sizeof(QemuThread));
3764 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3765 qemu_cond_init(env->halt_cond);
3766 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3767 while (env->created == 0)
3768 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3769}
3770
3771void qemu_init_vcpu(void *_env)
3772{
3773 CPUState *env = _env;
3774
3775 if (kvm_enabled())
3776 kvm_start_vcpu(env);
3777 else
3778 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003779 env->nr_cores = smp_cores;
3780 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003781}
3782
3783void qemu_notify_event(void)
3784{
3785 qemu_event_increment();
3786}
3787
3788void vm_stop(int reason)
3789{
3790 QemuThread me;
3791 qemu_thread_self(&me);
3792
3793 if (!qemu_thread_equal(&me, &io_thread)) {
3794 qemu_system_vmstop_request(reason);
3795 /*
3796 * FIXME: should not return to device code in case
3797 * vm_stop() has been requested.
3798 */
3799 if (cpu_single_env) {
3800 cpu_exit(cpu_single_env);
3801 cpu_single_env->stop = 1;
3802 }
3803 return;
3804 }
3805 do_vm_stop(reason);
3806}
3807
3808#endif
3809
3810
ths877cf882007-04-18 18:11:47 +00003811#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003812static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003813{
3814 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003815 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003816
bellardf3311102006-04-12 20:21:17 +00003817
3818 /* XXX: need to suppress polling by better using win32 events */
3819 ret = 0;
3820 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3821 ret |= pe->func(pe->opaque);
3822 }
thse6b1e552007-04-18 17:56:02 +00003823 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003824 int err;
3825 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003826
aliguori56f3a5d2008-10-31 18:07:17 +00003827 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003828 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3829 if (w->func[ret - WAIT_OBJECT_0])
3830 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003831
ths5fafdf22007-09-16 21:08:06 +00003832 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003833 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003834
thse6b1e552007-04-18 17:56:02 +00003835 /* Check if event is signaled */
3836 ret2 = WaitForSingleObject(w->events[i], 0);
3837 if(ret2 == WAIT_OBJECT_0) {
3838 if (w->func[i])
3839 w->func[i](w->opaque[i]);
3840 } else if (ret2 == WAIT_TIMEOUT) {
3841 } else {
3842 err = GetLastError();
3843 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003844 }
3845 }
bellarda18e5242006-06-25 17:18:27 +00003846 } else if (ret == WAIT_TIMEOUT) {
3847 } else {
3848 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003849 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003850 }
bellardf3311102006-04-12 20:21:17 +00003851 }
aliguori56f3a5d2008-10-31 18:07:17 +00003852
3853 *timeout = 0;
3854}
3855#else
blueswir169d64512008-12-07 19:30:18 +00003856static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003857{
3858}
bellardfd1dff42006-02-01 21:29:26 +00003859#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003860
3861void main_loop_wait(int timeout)
3862{
3863 IOHandlerRecord *ioh;
3864 fd_set rfds, wfds, xfds;
3865 int ret, nfds;
3866 struct timeval tv;
3867
3868 qemu_bh_update_timeout(&timeout);
3869
3870 host_main_loop_wait(&timeout);
3871
bellardfd1dff42006-02-01 21:29:26 +00003872 /* poll any events */
3873 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003874 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003875 FD_ZERO(&rfds);
3876 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003877 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003878 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003879 if (ioh->deleted)
3880 continue;
bellardfd1dff42006-02-01 21:29:26 +00003881 if (ioh->fd_read &&
3882 (!ioh->fd_read_poll ||
3883 ioh->fd_read_poll(ioh->opaque) != 0)) {
3884 FD_SET(ioh->fd, &rfds);
3885 if (ioh->fd > nfds)
3886 nfds = ioh->fd;
3887 }
3888 if (ioh->fd_write) {
3889 FD_SET(ioh->fd, &wfds);
3890 if (ioh->fd > nfds)
3891 nfds = ioh->fd;
3892 }
3893 }
ths3b46e622007-09-17 08:09:54 +00003894
aliguori56f3a5d2008-10-31 18:07:17 +00003895 tv.tv_sec = timeout / 1000;
3896 tv.tv_usec = (timeout % 1000) * 1000;
3897
Jan Kiszkad918f232009-06-24 14:42:30 +02003898 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3899
aliguori48708522009-04-24 18:03:49 +00003900 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003901 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003902 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003903 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003904 IOHandlerRecord **pioh;
3905
3906 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003907 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003908 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003909 }
ths6ab43fd2007-08-25 01:34:19 +00003910 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003911 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003912 }
3913 }
thscafffd42007-02-28 21:59:44 +00003914
3915 /* remove deleted IO handlers */
3916 pioh = &first_io_handler;
3917 while (*pioh) {
3918 ioh = *pioh;
3919 if (ioh->deleted) {
3920 *pioh = ioh->next;
3921 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003922 } else
thscafffd42007-02-28 21:59:44 +00003923 pioh = &ioh->next;
3924 }
bellardfd1dff42006-02-01 21:29:26 +00003925 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003926
3927 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003928
aliguori50317c72009-04-24 18:03:29 +00003929 /* rearm timer, if not periodic */
3930 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3931 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3932 qemu_rearm_alarm_timer(alarm_timer);
3933 }
3934
aliguori357c6922008-11-25 17:26:09 +00003935 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003936 if (vm_running) {
3937 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003938 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
3939 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00003940 }
aliguori357c6922008-11-25 17:26:09 +00003941
3942 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003943 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00003944 qemu_get_clock(rt_clock));
3945
Jan Kiszka21d5d122009-09-15 13:36:04 +02003946 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
3947 qemu_get_clock(host_clock));
3948
pbrook423f0742007-05-23 00:06:54 +00003949 /* Check bottom-halves last in case any of the earlier events triggered
3950 them. */
3951 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003952
bellard5905b2e2004-08-01 21:53:26 +00003953}
3954
aliguori43b96852009-04-24 18:03:33 +00003955static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003956{
aliguori43b96852009-04-24 18:03:33 +00003957 int ret;
bellard89bfc102006-02-08 22:46:31 +00003958#ifdef CONFIG_PROFILER
3959 int64_t ti;
3960#endif
aliguori43b96852009-04-24 18:03:33 +00003961
3962#ifdef CONFIG_PROFILER
3963 ti = profile_getclock();
3964#endif
3965 if (use_icount) {
3966 int64_t count;
3967 int decr;
3968 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3969 env->icount_decr.u16.low = 0;
3970 env->icount_extra = 0;
3971 count = qemu_next_deadline();
3972 count = (count + (1 << icount_time_shift) - 1)
3973 >> icount_time_shift;
3974 qemu_icount += count;
3975 decr = (count > 0xffff) ? 0xffff : count;
3976 count -= decr;
3977 env->icount_decr.u16.low = decr;
3978 env->icount_extra = count;
3979 }
3980 ret = cpu_exec(env);
3981#ifdef CONFIG_PROFILER
3982 qemu_time += profile_getclock() - ti;
3983#endif
3984 if (use_icount) {
3985 /* Fold pending instructions back into the
3986 instruction counter, and clear the interrupt flag. */
3987 qemu_icount -= (env->icount_decr.u16.low
3988 + env->icount_extra);
3989 env->icount_decr.u32 = 0;
3990 env->icount_extra = 0;
3991 }
3992 return ret;
3993}
3994
aliguorie6e35b12009-04-24 18:03:57 +00003995static void tcg_cpu_exec(void)
3996{
aliguorid6dc3d42009-04-24 18:04:07 +00003997 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00003998
3999 if (next_cpu == NULL)
4000 next_cpu = first_cpu;
4001 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4002 CPUState *env = cur_cpu = next_cpu;
4003
4004 if (!vm_running)
4005 break;
4006 if (timer_alarm_pending) {
4007 timer_alarm_pending = 0;
4008 break;
4009 }
aliguorid6dc3d42009-04-24 18:04:07 +00004010 if (cpu_can_run(env))
4011 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004012 if (ret == EXCP_DEBUG) {
4013 gdb_set_stop_cpu(env);
4014 debug_requested = 1;
4015 break;
4016 }
4017 }
4018}
4019
aliguori43b96852009-04-24 18:03:33 +00004020static int cpu_has_work(CPUState *env)
4021{
aliguorid6dc3d42009-04-24 18:04:07 +00004022 if (env->stop)
4023 return 1;
4024 if (env->stopped)
4025 return 0;
aliguori43b96852009-04-24 18:03:33 +00004026 if (!env->halted)
4027 return 1;
4028 if (qemu_cpu_has_work(env))
4029 return 1;
4030 return 0;
4031}
4032
4033static int tcg_has_work(void)
4034{
bellard6a00d602005-11-21 23:25:50 +00004035 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004036
aliguori43b96852009-04-24 18:03:33 +00004037 for (env = first_cpu; env != NULL; env = env->next_cpu)
4038 if (cpu_has_work(env))
4039 return 1;
4040 return 0;
4041}
bellard15a76442005-11-23 21:01:03 +00004042
aliguori43b96852009-04-24 18:03:33 +00004043static int qemu_calculate_timeout(void)
4044{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004045#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004046 int timeout;
bellard15a76442005-11-23 21:01:03 +00004047
aliguori43b96852009-04-24 18:03:33 +00004048 if (!vm_running)
4049 timeout = 5000;
4050 else if (tcg_has_work())
4051 timeout = 0;
4052 else if (!use_icount)
4053 timeout = 5000;
4054 else {
4055 /* XXX: use timeout computed from timers */
4056 int64_t add;
4057 int64_t delta;
4058 /* Advance virtual time to the next event. */
4059 if (use_icount == 1) {
4060 /* When not using an adaptive execution frequency
4061 we tend to get badly out of sync with real time,
4062 so just delay for a reasonable amount of time. */
4063 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004064 } else {
aliguori43b96852009-04-24 18:03:33 +00004065 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004066 }
aliguori43b96852009-04-24 18:03:33 +00004067 if (delta > 0) {
4068 /* If virtual time is ahead of real time then just
4069 wait for IO. */
4070 timeout = (delta / 1000000) + 1;
4071 } else {
4072 /* Wait for either IO to occur or the next
4073 timer event. */
4074 add = qemu_next_deadline();
4075 /* We advance the timer before checking for IO.
4076 Limit the amount we advance so that early IO
4077 activity won't get the guest too far ahead. */
4078 if (add > 10000000)
4079 add = 10000000;
4080 delta += add;
4081 add = (add + (1 << icount_time_shift) - 1)
4082 >> icount_time_shift;
4083 qemu_icount += add;
4084 timeout = delta / 1000000;
4085 if (timeout < 0)
4086 timeout = 0;
4087 }
bellardb4608c02003-06-27 17:34:32 +00004088 }
aliguori43b96852009-04-24 18:03:33 +00004089
4090 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004091#else /* CONFIG_IOTHREAD */
4092 return 1000;
4093#endif
aliguori43b96852009-04-24 18:03:33 +00004094}
4095
4096static int vm_can_run(void)
4097{
4098 if (powerdown_requested)
4099 return 0;
4100 if (reset_requested)
4101 return 0;
4102 if (shutdown_requested)
4103 return 0;
aliguorie5689022009-04-24 18:03:54 +00004104 if (debug_requested)
4105 return 0;
aliguori43b96852009-04-24 18:03:33 +00004106 return 1;
4107}
4108
Blue Swirld9c32312009-08-09 08:42:19 +00004109qemu_irq qemu_system_powerdown;
4110
aliguori43b96852009-04-24 18:03:33 +00004111static void main_loop(void)
4112{
aliguori6e29f5d2009-04-24 18:04:02 +00004113 int r;
aliguori43b96852009-04-24 18:03:33 +00004114
aliguorid6dc3d42009-04-24 18:04:07 +00004115#ifdef CONFIG_IOTHREAD
4116 qemu_system_ready = 1;
4117 qemu_cond_broadcast(&qemu_system_cond);
4118#endif
4119
aliguori6e29f5d2009-04-24 18:04:02 +00004120 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004121 do {
4122#ifdef CONFIG_PROFILER
4123 int64_t ti;
4124#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004125#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004126 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004127#endif
aliguori43b96852009-04-24 18:03:33 +00004128#ifdef CONFIG_PROFILER
4129 ti = profile_getclock();
4130#endif
4131 main_loop_wait(qemu_calculate_timeout());
4132#ifdef CONFIG_PROFILER
4133 dev_time += profile_getclock() - ti;
4134#endif
aliguorie5689022009-04-24 18:03:54 +00004135 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004136
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004137 if (qemu_debug_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004138 monitor_protocol_event(QEVENT_DEBUG, NULL);
aliguori43b96852009-04-24 18:03:33 +00004139 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004140 }
aliguori43b96852009-04-24 18:03:33 +00004141 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004142 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00004143 if (no_shutdown) {
4144 vm_stop(0);
4145 no_shutdown = 0;
4146 } else
4147 break;
4148 }
aliguorid6dc3d42009-04-24 18:04:07 +00004149 if (qemu_reset_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004150 monitor_protocol_event(QEVENT_RESET, NULL);
aliguorid6dc3d42009-04-24 18:04:07 +00004151 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004152 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004153 resume_all_vcpus();
4154 }
Blue Swirld9c32312009-08-09 08:42:19 +00004155 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004156 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00004157 qemu_irq_raise(qemu_system_powerdown);
4158 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004159 if ((r = qemu_vmstop_requested())) {
Blue Swirl242cd002009-12-04 18:05:45 +00004160 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00004161 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004162 }
aliguori43b96852009-04-24 18:03:33 +00004163 }
aliguorid6dc3d42009-04-24 18:04:07 +00004164 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004165}
4166
pbrook9bd7e6d2009-04-07 22:58:45 +00004167static void version(void)
4168{
pbrook4a19f1e2009-04-07 23:17:49 +00004169 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004170}
4171
ths15f82202007-06-29 23:26:08 +00004172static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004173{
pbrook9bd7e6d2009-04-07 22:58:45 +00004174 version();
4175 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004176 "\n"
bellarda20dd502003-09-30 21:07:02 +00004177 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004178 "\n"
blueswir15824d652009-03-28 06:44:27 +00004179#define DEF(option, opt_arg, opt_enum, opt_help) \
4180 opt_help
4181#define DEFHEADING(text) stringify(text) "\n"
4182#include "qemu-options.h"
4183#undef DEF
4184#undef DEFHEADING
4185#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004186 "\n"
bellard82c643f2004-07-14 17:28:13 +00004187 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004188 "ctrl-alt-f toggle full screen\n"
4189 "ctrl-alt-n switch to virtual console 'n'\n"
4190 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004191 "\n"
4192 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4193 ,
bellard0db63472003-10-27 21:37:46 +00004194 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004195 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004196#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004197 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004198 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004199#endif
bellard6e44ba72004-01-18 21:56:49 +00004200 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004201 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004202 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004203}
4204
bellardcd6f1162004-05-13 22:02:20 +00004205#define HAS_ARG 0x0001
4206
4207enum {
blueswir15824d652009-03-28 06:44:27 +00004208#define DEF(option, opt_arg, opt_enum, opt_help) \
4209 opt_enum,
4210#define DEFHEADING(text)
4211#include "qemu-options.h"
4212#undef DEF
4213#undef DEFHEADING
4214#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004215};
4216
4217typedef struct QEMUOption {
4218 const char *name;
4219 int flags;
4220 int index;
4221} QEMUOption;
4222
blueswir1dbed7e42008-10-01 19:38:09 +00004223static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004224 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004225#define DEF(option, opt_arg, opt_enum, opt_help) \
4226 { option, opt_arg, opt_enum },
4227#define DEFHEADING(text)
4228#include "qemu-options.h"
4229#undef DEF
4230#undef DEFHEADING
4231#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004232 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004233};
4234
bellard1d14ffa2005-10-30 18:58:22 +00004235#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004236struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004237#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004238#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004239 {
4240 "pcspk",
4241 "PC speaker",
4242 0,
4243 1,
4244 { .init_isa = pcspk_audio_init }
4245 },
4246#endif
malc4c9b53e2009-01-09 10:46:34 +00004247
4248#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004249 {
4250 "sb16",
4251 "Creative Sound Blaster 16",
4252 0,
4253 1,
4254 { .init_isa = SB16_init }
4255 },
malc4c9b53e2009-01-09 10:46:34 +00004256#endif
bellard6a36d842005-12-18 20:34:32 +00004257
malccc53d262008-06-13 10:48:22 +00004258#ifdef CONFIG_CS4231A
4259 {
4260 "cs4231a",
4261 "CS4231A",
4262 0,
4263 1,
4264 { .init_isa = cs4231a_init }
4265 },
4266#endif
4267
bellard6a36d842005-12-18 20:34:32 +00004268#ifdef CONFIG_ADLIB
4269 {
4270 "adlib",
4271#ifdef HAS_YMF262
4272 "Yamaha YMF262 (OPL3)",
4273#else
4274 "Yamaha YM3812 (OPL2)",
4275#endif
4276 0,
4277 1,
4278 { .init_isa = Adlib_init }
4279 },
4280#endif
4281
4282#ifdef CONFIG_GUS
4283 {
4284 "gus",
4285 "Gravis Ultrasound GF1",
4286 0,
4287 1,
4288 { .init_isa = GUS_init }
4289 },
4290#endif
4291
malc4c9b53e2009-01-09 10:46:34 +00004292#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004293 {
4294 "ac97",
4295 "Intel 82801AA AC97 Audio",
4296 0,
4297 0,
4298 { .init_pci = ac97_init }
4299 },
malc4c9b53e2009-01-09 10:46:34 +00004300#endif
balroge5c9a132008-01-14 04:27:55 +00004301
malc4c9b53e2009-01-09 10:46:34 +00004302#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004303 {
4304 "es1370",
4305 "ENSONIQ AudioPCI ES1370",
4306 0,
4307 0,
4308 { .init_pci = es1370_init }
4309 },
balrogb00052e2007-04-30 02:22:06 +00004310#endif
bellard6a36d842005-12-18 20:34:32 +00004311
malc4c9b53e2009-01-09 10:46:34 +00004312#endif /* HAS_AUDIO_CHOICE */
4313
bellard6a36d842005-12-18 20:34:32 +00004314 { NULL, NULL, 0, 0, { NULL } }
4315};
4316
bellard1d14ffa2005-10-30 18:58:22 +00004317static void select_soundhw (const char *optarg)
4318{
bellard6a36d842005-12-18 20:34:32 +00004319 struct soundhw *c;
4320
bellard1d14ffa2005-10-30 18:58:22 +00004321 if (*optarg == '?') {
4322 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004323
bellard1d14ffa2005-10-30 18:58:22 +00004324 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004325 for (c = soundhw; c->name; ++c) {
4326 printf ("%-11s %s\n", c->name, c->descr);
4327 }
4328 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004329 exit (*optarg != '?');
4330 }
4331 else {
bellard6a36d842005-12-18 20:34:32 +00004332 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004333 const char *p;
4334 char *e;
4335 int bad_card = 0;
4336
bellard6a36d842005-12-18 20:34:32 +00004337 if (!strcmp (optarg, "all")) {
4338 for (c = soundhw; c->name; ++c) {
4339 c->enabled = 1;
4340 }
4341 return;
4342 }
bellard1d14ffa2005-10-30 18:58:22 +00004343
bellard6a36d842005-12-18 20:34:32 +00004344 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004345 while (*p) {
4346 e = strchr (p, ',');
4347 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004348
4349 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004350 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004351 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004352 break;
4353 }
4354 }
bellard6a36d842005-12-18 20:34:32 +00004355
4356 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004357 if (l > 80) {
4358 fprintf (stderr,
4359 "Unknown sound card name (too big to show)\n");
4360 }
4361 else {
4362 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4363 (int) l, p);
4364 }
4365 bad_card = 1;
4366 }
4367 p += l + (e != NULL);
4368 }
4369
4370 if (bad_card)
4371 goto show_valid_cards;
4372 }
4373}
4374#endif
4375
malc3893c122008-09-28 00:42:05 +00004376static void select_vgahw (const char *p)
4377{
4378 const char *opts;
4379
Gerd Hoffmann64465292009-12-08 13:11:45 +01004380 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10004381 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004382 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004383 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004384 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004385 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004386 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004387 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004388 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004389 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004390 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004391 invalid_vga:
4392 fprintf(stderr, "Unknown vga type: %s\n", p);
4393 exit(1);
4394 }
malccb5a7aa2008-09-28 00:42:12 +00004395 while (*opts) {
4396 const char *nextopt;
4397
4398 if (strstart(opts, ",retrace=", &nextopt)) {
4399 opts = nextopt;
4400 if (strstart(opts, "dumb", &nextopt))
4401 vga_retrace_method = VGA_RETRACE_DUMB;
4402 else if (strstart(opts, "precise", &nextopt))
4403 vga_retrace_method = VGA_RETRACE_PRECISE;
4404 else goto invalid_vga;
4405 } else goto invalid_vga;
4406 opts = nextopt;
4407 }
malc3893c122008-09-28 00:42:05 +00004408}
4409
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004410#ifdef TARGET_I386
4411static int balloon_parse(const char *arg)
4412{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004413 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004414
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004415 if (strcmp(arg, "none") == 0) {
4416 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004417 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004418
4419 if (!strncmp(arg, "virtio", 6)) {
4420 if (arg[6] == ',') {
4421 /* have params -> parse them */
4422 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4423 if (!opts)
4424 return -1;
4425 } else {
4426 /* create empty opts */
4427 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4428 }
4429 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4430 return 0;
4431 }
4432
4433 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004434}
4435#endif
4436
bellard3587d7e2006-06-26 20:03:44 +00004437#ifdef _WIN32
4438static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4439{
4440 exit(STATUS_CONTROL_C_EXIT);
4441 return TRUE;
4442}
4443#endif
4444
aliguoric4be29f2009-04-17 18:58:14 +00004445int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004446{
4447 int ret;
4448
4449 if(strlen(str) != 36)
4450 return -1;
4451
4452 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4453 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4454 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4455
4456 if(ret != 16)
4457 return -1;
4458
aliguorib6f6e3d2009-04-17 18:59:56 +00004459#ifdef TARGET_I386
4460 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4461#endif
4462
blueswir18fcb1b92008-09-18 18:29:08 +00004463 return 0;
4464}
4465
aliguori5b08fc12008-08-21 20:08:03 +00004466#ifndef _WIN32
4467
4468static void termsig_handler(int signal)
4469{
4470 qemu_system_shutdown_request();
4471}
4472
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004473static void sigchld_handler(int signal)
4474{
4475 waitpid(-1, NULL, WNOHANG);
4476}
4477
4478static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004479{
4480 struct sigaction act;
4481
4482 memset(&act, 0, sizeof(act));
4483 act.sa_handler = termsig_handler;
4484 sigaction(SIGINT, &act, NULL);
4485 sigaction(SIGHUP, &act, NULL);
4486 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004487
4488 act.sa_handler = sigchld_handler;
4489 act.sa_flags = SA_NOCLDSTOP;
4490 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004491}
4492
4493#endif
4494
Paul Brook5cea8592009-05-30 00:52:44 +01004495#ifdef _WIN32
4496/* Look for support files in the same directory as the executable. */
4497static char *find_datadir(const char *argv0)
4498{
4499 char *p;
4500 char buf[MAX_PATH];
4501 DWORD len;
4502
4503 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4504 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004505 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004506 }
4507
4508 buf[len] = 0;
4509 p = buf + len - 1;
4510 while (p != buf && *p != '\\')
4511 p--;
4512 *p = 0;
4513 if (access(buf, R_OK) == 0) {
4514 return qemu_strdup(buf);
4515 }
4516 return NULL;
4517}
4518#else /* !_WIN32 */
4519
4520/* Find a likely location for support files using the location of the binary.
4521 For installed binaries this will be "$bindir/../share/qemu". When
4522 running from the build tree this will be "$bindir/../pc-bios". */
4523#define SHARE_SUFFIX "/share/qemu"
4524#define BUILD_SUFFIX "/pc-bios"
4525static char *find_datadir(const char *argv0)
4526{
4527 char *dir;
4528 char *p = NULL;
4529 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004530 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004531 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004532
4533#if defined(__linux__)
4534 {
4535 int len;
4536 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4537 if (len > 0) {
4538 buf[len] = 0;
4539 p = buf;
4540 }
4541 }
4542#elif defined(__FreeBSD__)
4543 {
4544 int len;
4545 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4546 if (len > 0) {
4547 buf[len] = 0;
4548 p = buf;
4549 }
4550 }
4551#endif
4552 /* If we don't have any way of figuring out the actual executable
4553 location then try argv[0]. */
4554 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004555 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004556 if (!p) {
4557 return NULL;
4558 }
4559 }
4560 dir = dirname(p);
4561 dir = dirname(dir);
4562
Blue Swirl3a417592009-06-09 19:12:21 +00004563 max_len = strlen(dir) +
4564 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4565 res = qemu_mallocz(max_len);
4566 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004567 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004568 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004569 if (access(res, R_OK)) {
4570 qemu_free(res);
4571 res = NULL;
4572 }
4573 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004574
Paul Brook5cea8592009-05-30 00:52:44 +01004575 return res;
4576}
4577#undef SHARE_SUFFIX
4578#undef BUILD_SUFFIX
4579#endif
4580
4581char *qemu_find_file(int type, const char *name)
4582{
4583 int len;
4584 const char *subdir;
4585 char *buf;
4586
4587 /* If name contains path separators then try it as a straight path. */
4588 if ((strchr(name, '/') || strchr(name, '\\'))
4589 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004590 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004591 }
4592 switch (type) {
4593 case QEMU_FILE_TYPE_BIOS:
4594 subdir = "";
4595 break;
4596 case QEMU_FILE_TYPE_KEYMAP:
4597 subdir = "keymaps/";
4598 break;
4599 default:
4600 abort();
4601 }
4602 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4603 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004604 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004605 if (access(buf, R_OK)) {
4606 qemu_free(buf);
4607 return NULL;
4608 }
4609 return buf;
4610}
4611
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004612static int device_init_func(QemuOpts *opts, void *opaque)
4613{
4614 DeviceState *dev;
4615
4616 dev = qdev_device_add(opts);
4617 if (!dev)
4618 return -1;
4619 return 0;
4620}
4621
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004622static int chardev_init_func(QemuOpts *opts, void *opaque)
4623{
4624 CharDriverState *chr;
4625
4626 chr = qemu_chr_open_opts(opts, NULL);
4627 if (!chr)
4628 return -1;
4629 return 0;
4630}
4631
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004632struct device_config {
4633 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004634 DEV_USB, /* -usbdevice */
4635 DEV_BT, /* -bt */
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004636 DEV_SERIAL, /* -serial */
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004637 DEV_PARALLEL, /* -parallel */
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004638 DEV_MONITOR, /* -monitor */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004639 } type;
4640 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004641 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004642};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004643QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004644
4645static void add_device_config(int type, const char *cmdline)
4646{
4647 struct device_config *conf;
4648
4649 conf = qemu_mallocz(sizeof(*conf));
4650 conf->type = type;
4651 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004652 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004653}
4654
4655static int foreach_device_config(int type, int (*func)(const char *cmdline))
4656{
4657 struct device_config *conf;
4658 int rc;
4659
Blue Swirl72cf2d42009-09-12 07:36:22 +00004660 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004661 if (conf->type != type)
4662 continue;
4663 rc = func(conf->cmdline);
4664 if (0 != rc)
4665 return rc;
4666 }
4667 return 0;
4668}
4669
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004670static int serial_parse(const char *devname)
4671{
4672 static int index = 0;
4673 char label[32];
4674
4675 if (strcmp(devname, "none") == 0)
4676 return 0;
4677 if (index == MAX_SERIAL_PORTS) {
4678 fprintf(stderr, "qemu: too many serial ports\n");
4679 exit(1);
4680 }
4681 snprintf(label, sizeof(label), "serial%d", index);
4682 serial_hds[index] = qemu_chr_open(label, devname, NULL);
4683 if (!serial_hds[index]) {
4684 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
4685 devname, strerror(errno));
4686 return -1;
4687 }
4688 index++;
4689 return 0;
4690}
4691
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004692static int parallel_parse(const char *devname)
4693{
4694 static int index = 0;
4695 char label[32];
4696
4697 if (strcmp(devname, "none") == 0)
4698 return 0;
4699 if (index == MAX_PARALLEL_PORTS) {
4700 fprintf(stderr, "qemu: too many parallel ports\n");
4701 exit(1);
4702 }
4703 snprintf(label, sizeof(label), "parallel%d", index);
4704 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
4705 if (!parallel_hds[index]) {
4706 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
4707 devname, strerror(errno));
4708 return -1;
4709 }
4710 index++;
4711 return 0;
4712}
4713
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004714static int monitor_parse(const char *devname)
4715{
4716 static int index = 0;
4717 char label[32];
4718
4719 if (strcmp(devname, "none") == 0)
4720 return 0;
4721 if (index == MAX_MONITOR_DEVICES) {
4722 fprintf(stderr, "qemu: too many monitor devices\n");
4723 exit(1);
4724 }
4725 if (index == 0) {
4726 snprintf(label, sizeof(label), "monitor");
4727 } else {
4728 snprintf(label, sizeof(label), "monitor%d", index);
4729 }
4730 monitor_hds[index] = qemu_chr_open(label, devname, NULL);
4731 if (!monitor_hds[index]) {
4732 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
4733 devname);
4734 return -1;
4735 }
4736 index++;
4737 return 0;
4738}
4739
malc902b3d52008-12-10 19:18:40 +00004740int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004741{
aliguori59030a82009-04-05 18:43:41 +00004742 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004743 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004744 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004745 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004746 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004747 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05004748 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004749 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004750 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004751 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004752 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004753 int optind;
4754 const char *r, *optarg;
aliguori9ede2fd2009-01-15 20:05:25 +00004755 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4756 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004757 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004758 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004759 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004760#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004761 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004762#endif
bellard26a5f132008-05-28 12:30:31 +00004763 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004764 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004765 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004766#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004767 int fd = 0;
4768 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004769 const char *chroot_dir = NULL;
4770 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004771#endif
aliguori268a3622009-04-21 22:30:27 +00004772 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004773 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004774
Jan Kiszka68752042009-09-15 13:36:04 +02004775 init_clocks();
4776
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004777 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004778 qemu_cache_utils_init(envp);
4779
Blue Swirl72cf2d42009-09-12 07:36:22 +00004780 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004781#ifndef _WIN32
4782 {
4783 struct sigaction act;
4784 sigfillset(&act.sa_mask);
4785 act.sa_flags = 0;
4786 act.sa_handler = SIG_IGN;
4787 sigaction(SIGPIPE, &act, NULL);
4788 }
bellard3587d7e2006-06-26 20:03:44 +00004789#else
4790 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004791 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4792 QEMU to run on a single CPU */
4793 {
4794 HANDLE h;
4795 DWORD mask, smask;
4796 int i;
4797 h = GetCurrentProcess();
4798 if (GetProcessAffinityMask(h, &mask, &smask)) {
4799 for(i = 0; i < 32; i++) {
4800 if (mask & (1 << i))
4801 break;
4802 }
4803 if (i != 32) {
4804 mask = 1 << i;
4805 SetProcessAffinityMask(h, mask);
4806 }
4807 }
4808 }
bellard67b915a2004-03-31 23:37:16 +00004809#endif
bellardbe995c22006-06-25 16:25:21 +00004810
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004811 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004812 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004813 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004814 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004815 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004816 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004817 kernel_filename = NULL;
4818 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004819 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004820 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004821
aliguori1b8fc812009-02-27 20:01:39 +00004822 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004823 virtio_consoles[i] = NULL;
4824 virtio_console_index = 0;
4825
aliguori268a3622009-04-21 22:30:27 +00004826 for (i = 0; i < MAX_NODES; i++) {
4827 node_mem[i] = 0;
4828 node_cpumask[i] = 0;
4829 }
4830
aliguori268a3622009-04-21 22:30:27 +00004831 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004832 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004833
bellard26a5f132008-05-28 12:30:31 +00004834 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004835 autostart= 1;
4836
bellardcd6f1162004-05-13 22:02:20 +00004837 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004838 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004839 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004840 break;
bellardcd6f1162004-05-13 22:02:20 +00004841 r = argv[optind];
4842 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004843 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004844 } else {
4845 const QEMUOption *popt;
4846
4847 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004848 /* Treat --foo the same as -foo. */
4849 if (r[1] == '-')
4850 r++;
bellardcd6f1162004-05-13 22:02:20 +00004851 popt = qemu_options;
4852 for(;;) {
4853 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004854 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004855 argv[0], r);
4856 exit(1);
4857 }
4858 if (!strcmp(popt->name, r + 1))
4859 break;
4860 popt++;
4861 }
4862 if (popt->flags & HAS_ARG) {
4863 if (optind >= argc) {
4864 fprintf(stderr, "%s: option '%s' requires an argument\n",
4865 argv[0], r);
4866 exit(1);
4867 }
4868 optarg = argv[optind++];
4869 } else {
4870 optarg = NULL;
4871 }
4872
4873 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004874 case QEMU_OPTION_M:
4875 machine = find_machine(optarg);
4876 if (!machine) {
4877 QEMUMachine *m;
4878 printf("Supported machines are:\n");
4879 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004880 if (m->alias)
4881 printf("%-10s %s (alias of %s)\n",
4882 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004883 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004884 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004885 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004886 }
ths15f82202007-06-29 23:26:08 +00004887 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004888 }
4889 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004890 case QEMU_OPTION_cpu:
4891 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004892 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004893/* XXX: implement xxx_cpu_list for targets that still miss it */
4894#if defined(cpu_list)
4895 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004896#endif
ths15f82202007-06-29 23:26:08 +00004897 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004898 } else {
4899 cpu_model = optarg;
4900 }
4901 break;
bellardcd6f1162004-05-13 22:02:20 +00004902 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004903 initrd_filename = optarg;
4904 break;
bellardcd6f1162004-05-13 22:02:20 +00004905 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004906 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004907 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004908 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004909 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004910 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004911 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004912 translation == BIOS_ATA_TRANSLATION_LBA ?
4913 ",trans=lba" :
4914 translation == BIOS_ATA_TRANSLATION_NONE ?
4915 ",trans=none" : "");
4916 break;
bellardcd6f1162004-05-13 22:02:20 +00004917 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004918 case QEMU_OPTION_hdc:
4919 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004920 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004921 break;
thse4bcb142007-12-02 04:51:10 +00004922 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004923 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004924 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004925 case QEMU_OPTION_set:
4926 if (qemu_set_option(optarg) != 0)
4927 exit(1);
4928 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004929 case QEMU_OPTION_global:
4930 if (qemu_global_option(optarg) != 0)
4931 exit(1);
4932 break;
balrog3e3d5812007-04-30 02:09:25 +00004933 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004934 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004935 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004936 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004937 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004938 break;
j_mayer86f55662007-04-24 06:52:59 +00004939 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004940 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004941 break;
bellardcd6f1162004-05-13 22:02:20 +00004942 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004943 snapshot = 1;
4944 break;
bellardcd6f1162004-05-13 22:02:20 +00004945 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004946 {
bellard330d0412003-07-26 18:11:40 +00004947 const char *p;
4948 p = optarg;
4949 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004950 if (cyls < 1 || cyls > 16383)
4951 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004952 if (*p != ',')
4953 goto chs_fail;
4954 p++;
4955 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004956 if (heads < 1 || heads > 16)
4957 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004958 if (*p != ',')
4959 goto chs_fail;
4960 p++;
4961 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004962 if (secs < 1 || secs > 63)
4963 goto chs_fail;
4964 if (*p == ',') {
4965 p++;
4966 if (!strcmp(p, "none"))
4967 translation = BIOS_ATA_TRANSLATION_NONE;
4968 else if (!strcmp(p, "lba"))
4969 translation = BIOS_ATA_TRANSLATION_LBA;
4970 else if (!strcmp(p, "auto"))
4971 translation = BIOS_ATA_TRANSLATION_AUTO;
4972 else
4973 goto chs_fail;
4974 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004975 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004976 fprintf(stderr, "qemu: invalid physical CHS format\n");
4977 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004978 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004979 if (hda_opts != NULL) {
4980 char num[16];
4981 snprintf(num, sizeof(num), "%d", cyls);
4982 qemu_opt_set(hda_opts, "cyls", num);
4983 snprintf(num, sizeof(num), "%d", heads);
4984 qemu_opt_set(hda_opts, "heads", num);
4985 snprintf(num, sizeof(num), "%d", secs);
4986 qemu_opt_set(hda_opts, "secs", num);
4987 if (translation == BIOS_ATA_TRANSLATION_LBA)
4988 qemu_opt_set(hda_opts, "trans", "lba");
4989 if (translation == BIOS_ATA_TRANSLATION_NONE)
4990 qemu_opt_set(hda_opts, "trans", "none");
4991 }
bellard330d0412003-07-26 18:11:40 +00004992 }
4993 break;
aliguori268a3622009-04-21 22:30:27 +00004994 case QEMU_OPTION_numa:
4995 if (nb_numa_nodes >= MAX_NODES) {
4996 fprintf(stderr, "qemu: too many NUMA nodes\n");
4997 exit(1);
4998 }
4999 numa_add(optarg);
5000 break;
bellardcd6f1162004-05-13 22:02:20 +00005001 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005002 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005003 break;
balrog4d3b6f62008-02-10 16:33:14 +00005004#ifdef CONFIG_CURSES
5005 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005006 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005007 break;
5008#endif
balroga171fe32007-04-30 01:48:07 +00005009 case QEMU_OPTION_portrait:
5010 graphic_rotate = 1;
5011 break;
bellardcd6f1162004-05-13 22:02:20 +00005012 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005013 kernel_filename = optarg;
5014 break;
bellardcd6f1162004-05-13 22:02:20 +00005015 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005016 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005017 break;
bellardcd6f1162004-05-13 22:02:20 +00005018 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005019 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005020 break;
bellardcd6f1162004-05-13 22:02:20 +00005021 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005022 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005023 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005024 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005025 };
5026 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005027 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005028 int legacy = 0;
5029
5030 if (!strchr(optarg, '=')) {
5031 legacy = 1;
5032 pstrcpy(buf, sizeof(buf), optarg);
5033 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5034 fprintf(stderr,
5035 "qemu: unknown boot parameter '%s' in '%s'\n",
5036 buf, optarg);
5037 exit(1);
5038 }
5039
5040 if (legacy ||
5041 get_param_value(buf, sizeof(buf), "order", optarg)) {
5042 boot_devices_bitmap = parse_bootdevices(buf);
5043 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005044 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005045 if (!legacy) {
5046 if (get_param_value(buf, sizeof(buf),
5047 "once", optarg)) {
5048 boot_devices_bitmap |= parse_bootdevices(buf);
5049 standard_boot_devices = qemu_strdup(boot_devices);
5050 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5051 qemu_register_reset(restore_boot_devices,
5052 standard_boot_devices);
5053 }
Jan Kiszka95387492009-07-02 00:19:02 +02005054 if (get_param_value(buf, sizeof(buf),
5055 "menu", optarg)) {
5056 if (!strcmp(buf, "on")) {
5057 boot_menu = 1;
5058 } else if (!strcmp(buf, "off")) {
5059 boot_menu = 0;
5060 } else {
5061 fprintf(stderr,
5062 "qemu: invalid option value '%s'\n",
5063 buf);
5064 exit(1);
5065 }
5066 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005067 }
bellard36b486b2003-11-11 13:36:08 +00005068 }
5069 break;
bellardcd6f1162004-05-13 22:02:20 +00005070 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005071 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005072 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005073 break;
bellard52ca8d62006-06-14 16:03:05 +00005074#ifdef TARGET_I386
5075 case QEMU_OPTION_no_fd_bootchk:
5076 fd_bootchk = 0;
5077 break;
5078#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01005079 case QEMU_OPTION_netdev:
5080 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5081 exit(1);
5082 }
5083 break;
bellard7c9d8e02005-11-15 22:16:05 +00005084 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01005085 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005086 exit(1);
5087 }
bellard702c6512004-04-02 21:21:32 +00005088 break;
bellardc7f74642004-08-24 21:57:12 +00005089#ifdef CONFIG_SLIRP
5090 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005091 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005092 break;
ths47d5d012007-02-20 00:05:08 +00005093 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005094 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005095 break;
bellardc94c8d62004-09-13 21:37:34 +00005096#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005097 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005098 if (net_slirp_smb(optarg) < 0)
5099 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005100 break;
bellardc94c8d62004-09-13 21:37:34 +00005101#endif
bellard9bf05442004-08-25 22:12:49 +00005102 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005103 if (net_slirp_redir(optarg) < 0)
5104 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005105 break;
bellardc7f74642004-08-24 21:57:12 +00005106#endif
balrogdc72ac12008-11-09 00:04:26 +00005107 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005108 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005109 break;
bellard1d14ffa2005-10-30 18:58:22 +00005110#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005111 case QEMU_OPTION_audio_help:
5112 AUD_help ();
5113 exit (0);
5114 break;
5115 case QEMU_OPTION_soundhw:
5116 select_soundhw (optarg);
5117 break;
5118#endif
bellardcd6f1162004-05-13 22:02:20 +00005119 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005120 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005121 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005122 case QEMU_OPTION_version:
5123 version();
5124 exit(0);
5125 break;
aurel3200f82b82008-04-27 21:12:55 +00005126 case QEMU_OPTION_m: {
5127 uint64_t value;
5128 char *ptr;
5129
5130 value = strtoul(optarg, &ptr, 10);
5131 switch (*ptr) {
5132 case 0: case 'M': case 'm':
5133 value <<= 20;
5134 break;
5135 case 'G': case 'g':
5136 value <<= 30;
5137 break;
5138 default:
5139 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005140 exit(1);
5141 }
aurel3200f82b82008-04-27 21:12:55 +00005142
5143 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005144 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005145 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5146 exit(1);
5147 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005148 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005149 fprintf(stderr, "qemu: ram size too large\n");
5150 exit(1);
5151 }
5152 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005153 break;
aurel3200f82b82008-04-27 21:12:55 +00005154 }
bellardcd6f1162004-05-13 22:02:20 +00005155 case QEMU_OPTION_d:
5156 {
5157 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005158 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005159
bellardcd6f1162004-05-13 22:02:20 +00005160 mask = cpu_str_to_log_mask(optarg);
5161 if (!mask) {
5162 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005163 for(item = cpu_log_items; item->mask != 0; item++) {
5164 printf("%-10s %s\n", item->name, item->help);
5165 }
5166 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005167 }
5168 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005169 }
bellardcd6f1162004-05-13 22:02:20 +00005170 break;
bellardcd6f1162004-05-13 22:02:20 +00005171 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005172 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005173 break;
aliguori59030a82009-04-05 18:43:41 +00005174 case QEMU_OPTION_gdb:
5175 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005176 break;
bellardcd6f1162004-05-13 22:02:20 +00005177 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005178 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005179 break;
j_mayer1192dad2007-10-05 13:08:35 +00005180 case QEMU_OPTION_bios:
5181 bios_name = optarg;
5182 break;
aurel321b530a62009-04-05 20:08:59 +00005183 case QEMU_OPTION_singlestep:
5184 singlestep = 1;
5185 break;
bellardcd6f1162004-05-13 22:02:20 +00005186 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005187 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005188 break;
bellard3d11d0e2004-12-12 16:56:30 +00005189 case QEMU_OPTION_k:
5190 keyboard_layout = optarg;
5191 break;
bellardee22c2f2004-06-03 12:49:50 +00005192 case QEMU_OPTION_localtime:
5193 rtc_utc = 0;
5194 break;
malc3893c122008-09-28 00:42:05 +00005195 case QEMU_OPTION_vga:
5196 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005197 break;
blueswir15824d652009-03-28 06:44:27 +00005198#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005199 case QEMU_OPTION_g:
5200 {
5201 const char *p;
5202 int w, h, depth;
5203 p = optarg;
5204 w = strtol(p, (char **)&p, 10);
5205 if (w <= 0) {
5206 graphic_error:
5207 fprintf(stderr, "qemu: invalid resolution or depth\n");
5208 exit(1);
5209 }
5210 if (*p != 'x')
5211 goto graphic_error;
5212 p++;
5213 h = strtol(p, (char **)&p, 10);
5214 if (h <= 0)
5215 goto graphic_error;
5216 if (*p == 'x') {
5217 p++;
5218 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005219 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005220 depth != 24 && depth != 32)
5221 goto graphic_error;
5222 } else if (*p == '\0') {
5223 depth = graphic_depth;
5224 } else {
5225 goto graphic_error;
5226 }
ths3b46e622007-09-17 08:09:54 +00005227
bellarde9b137c2004-06-21 16:46:10 +00005228 graphic_width = w;
5229 graphic_height = h;
5230 graphic_depth = depth;
5231 }
5232 break;
blueswir15824d652009-03-28 06:44:27 +00005233#endif
ths20d8a3e2007-02-18 17:04:49 +00005234 case QEMU_OPTION_echr:
5235 {
5236 char *r;
5237 term_escape_char = strtol(optarg, &r, 0);
5238 if (r == optarg)
5239 printf("Bad argument to echr\n");
5240 break;
5241 }
bellard82c643f2004-07-14 17:28:13 +00005242 case QEMU_OPTION_monitor:
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005243 add_device_config(DEV_MONITOR, optarg);
5244 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00005245 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005246 case QEMU_OPTION_chardev:
5247 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5248 if (!opts) {
5249 fprintf(stderr, "parse error: %s\n", optarg);
5250 exit(1);
5251 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005252 break;
bellard82c643f2004-07-14 17:28:13 +00005253 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005254 add_device_config(DEV_SERIAL, optarg);
5255 default_serial = 0;
bellard82c643f2004-07-14 17:28:13 +00005256 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005257 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005258 if (watchdog) {
5259 fprintf(stderr,
5260 "qemu: only one watchdog option may be given\n");
5261 return 1;
5262 }
5263 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005264 break;
5265 case QEMU_OPTION_watchdog_action:
5266 if (select_watchdog_action(optarg) == -1) {
5267 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5268 exit(1);
5269 }
5270 break;
aliguori51ecf132009-01-15 20:06:40 +00005271 case QEMU_OPTION_virtiocon:
5272 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5273 fprintf(stderr, "qemu: too many virtio consoles\n");
5274 exit(1);
5275 }
5276 virtio_consoles[virtio_console_index] = optarg;
5277 virtio_console_index++;
5278 break;
bellard6508fe52005-01-15 12:02:56 +00005279 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005280 add_device_config(DEV_PARALLEL, optarg);
5281 default_parallel = 0;
bellard6508fe52005-01-15 12:02:56 +00005282 break;
bellardd63d3072004-10-03 13:29:03 +00005283 case QEMU_OPTION_loadvm:
5284 loadvm = optarg;
5285 break;
5286 case QEMU_OPTION_full_screen:
5287 full_screen = 1;
5288 break;
ths667acca2006-12-11 02:08:05 +00005289#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005290 case QEMU_OPTION_no_frame:
5291 no_frame = 1;
5292 break;
ths3780e192007-06-21 21:08:02 +00005293 case QEMU_OPTION_alt_grab:
5294 alt_grab = 1;
5295 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005296 case QEMU_OPTION_ctrl_grab:
5297 ctrl_grab = 1;
5298 break;
ths667acca2006-12-11 02:08:05 +00005299 case QEMU_OPTION_no_quit:
5300 no_quit = 1;
5301 break;
aliguori7d957bd2009-01-15 22:14:11 +00005302 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005303 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005304 break;
ths667acca2006-12-11 02:08:05 +00005305#endif
bellardf7cce892004-12-08 22:21:25 +00005306 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005307 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005308 break;
bellarda09db212005-04-30 16:10:35 +00005309#ifdef TARGET_I386
5310 case QEMU_OPTION_win2k_hack:
5311 win2k_install_hack = 1;
5312 break;
aliguori73822ec2009-01-15 20:11:34 +00005313 case QEMU_OPTION_rtc_td_hack:
5314 rtc_td_hack = 1;
5315 break;
aliguori8a92ea22009-02-27 20:12:36 +00005316 case QEMU_OPTION_acpitable:
5317 if(acpi_table_add(optarg) < 0) {
5318 fprintf(stderr, "Wrong acpi table provided\n");
5319 exit(1);
5320 }
5321 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005322 case QEMU_OPTION_smbios:
5323 if(smbios_entry_add(optarg) < 0) {
5324 fprintf(stderr, "Wrong smbios provided\n");
5325 exit(1);
5326 }
5327 break;
bellarda09db212005-04-30 16:10:35 +00005328#endif
aliguori7ba1e612008-11-05 16:04:33 +00005329#ifdef CONFIG_KVM
5330 case QEMU_OPTION_enable_kvm:
5331 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005332 break;
5333#endif
bellardbb36d472005-11-05 14:22:28 +00005334 case QEMU_OPTION_usb:
5335 usb_enabled = 1;
5336 break;
bellarda594cfb2005-11-06 16:13:29 +00005337 case QEMU_OPTION_usbdevice:
5338 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005339 add_device_config(DEV_USB, optarg);
5340 break;
5341 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005342 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005343 exit(1);
5344 }
bellarda594cfb2005-11-06 16:13:29 +00005345 break;
bellard6a00d602005-11-21 23:25:50 +00005346 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005347 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005348 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005349 fprintf(stderr, "Invalid number of CPUs\n");
5350 exit(1);
5351 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005352 if (max_cpus < smp_cpus) {
5353 fprintf(stderr, "maxcpus must be equal to or greater than "
5354 "smp\n");
5355 exit(1);
5356 }
5357 if (max_cpus > 255) {
5358 fprintf(stderr, "Unsupported number of maxcpus\n");
5359 exit(1);
5360 }
bellard6a00d602005-11-21 23:25:50 +00005361 break;
bellard24236862006-04-30 21:28:36 +00005362 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005363 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005364 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005365 break;
blueswir15824d652009-03-28 06:44:27 +00005366#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005367 case QEMU_OPTION_no_acpi:
5368 acpi_enabled = 0;
5369 break;
aliguori16b29ae2008-12-17 23:28:44 +00005370 case QEMU_OPTION_no_hpet:
5371 no_hpet = 1;
5372 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005373 case QEMU_OPTION_balloon:
5374 if (balloon_parse(optarg) < 0) {
5375 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5376 exit(1);
5377 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005378 break;
blueswir15824d652009-03-28 06:44:27 +00005379#endif
bellardd1beab82006-10-02 19:44:22 +00005380 case QEMU_OPTION_no_reboot:
5381 no_reboot = 1;
5382 break;
aurel32b2f76162008-04-11 21:35:52 +00005383 case QEMU_OPTION_no_shutdown:
5384 no_shutdown = 1;
5385 break;
balrog9467cd42007-05-01 01:34:14 +00005386 case QEMU_OPTION_show_cursor:
5387 cursor_hide = 0;
5388 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005389 case QEMU_OPTION_uuid:
5390 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5391 fprintf(stderr, "Fail to parse UUID string."
5392 " Wrong format.\n");
5393 exit(1);
5394 }
5395 break;
blueswir15824d652009-03-28 06:44:27 +00005396#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005397 case QEMU_OPTION_daemonize:
5398 daemonize = 1;
5399 break;
blueswir15824d652009-03-28 06:44:27 +00005400#endif
ths9ae02552007-01-05 17:39:04 +00005401 case QEMU_OPTION_option_rom:
5402 if (nb_option_roms >= MAX_OPTION_ROMS) {
5403 fprintf(stderr, "Too many option ROMs\n");
5404 exit(1);
5405 }
5406 option_rom[nb_option_roms] = optarg;
5407 nb_option_roms++;
5408 break;
blueswir15824d652009-03-28 06:44:27 +00005409#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005410 case QEMU_OPTION_semihosting:
5411 semihosting_enabled = 1;
5412 break;
blueswir15824d652009-03-28 06:44:27 +00005413#endif
thsc35734b2007-03-19 15:17:08 +00005414 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005415 qemu_name = qemu_strdup(optarg);
5416 {
5417 char *p = strchr(qemu_name, ',');
5418 if (p != NULL) {
5419 *p++ = 0;
5420 if (strncmp(p, "process=", 8)) {
5421 fprintf(stderr, "Unknown subargument %s to -name", p);
5422 exit(1);
5423 }
5424 p += 8;
5425 set_proc_name(p);
5426 }
5427 }
thsc35734b2007-03-19 15:17:08 +00005428 break;
blueswir195efd112008-12-24 20:26:14 +00005429#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005430 case QEMU_OPTION_prom_env:
5431 if (nb_prom_envs >= MAX_PROM_ENVS) {
5432 fprintf(stderr, "Too many prom variables\n");
5433 exit(1);
5434 }
5435 prom_envs[nb_prom_envs] = optarg;
5436 nb_prom_envs++;
5437 break;
5438#endif
balrog2b8f2d42007-07-27 22:08:46 +00005439#ifdef TARGET_ARM
5440 case QEMU_OPTION_old_param:
5441 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005442 break;
balrog2b8f2d42007-07-27 22:08:46 +00005443#endif
thsf3dcfad2007-08-24 01:26:02 +00005444 case QEMU_OPTION_clock:
5445 configure_alarms(optarg);
5446 break;
bellard7e0af5d02007-11-07 16:24:33 +00005447 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005448 configure_rtc_date_offset(optarg, 1);
5449 break;
5450 case QEMU_OPTION_rtc:
5451 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5452 if (!opts) {
5453 fprintf(stderr, "parse error: %s\n", optarg);
5454 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005455 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005456 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005457 break;
bellard26a5f132008-05-28 12:30:31 +00005458 case QEMU_OPTION_tb_size:
5459 tb_size = strtol(optarg, NULL, 0);
5460 if (tb_size < 0)
5461 tb_size = 0;
5462 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005463 case QEMU_OPTION_icount:
5464 use_icount = 1;
5465 if (strcmp(optarg, "auto") == 0) {
5466 icount_time_shift = -1;
5467 } else {
5468 icount_time_shift = strtol(optarg, NULL, 0);
5469 }
5470 break;
aliguori5bb79102008-10-13 03:12:02 +00005471 case QEMU_OPTION_incoming:
5472 incoming = optarg;
5473 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005474 case QEMU_OPTION_nodefaults:
5475 default_serial = 0;
5476 default_parallel = 0;
5477 default_monitor = 0;
5478 default_vga = 0;
5479 break;
blueswir15824d652009-03-28 06:44:27 +00005480#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005481 case QEMU_OPTION_chroot:
5482 chroot_dir = optarg;
5483 break;
5484 case QEMU_OPTION_runas:
5485 run_as = optarg;
5486 break;
blueswir15824d652009-03-28 06:44:27 +00005487#endif
aliguorie37630c2009-04-22 15:19:10 +00005488#ifdef CONFIG_XEN
5489 case QEMU_OPTION_xen_domid:
5490 xen_domid = atoi(optarg);
5491 break;
5492 case QEMU_OPTION_xen_create:
5493 xen_mode = XEN_CREATE;
5494 break;
5495 case QEMU_OPTION_xen_attach:
5496 xen_mode = XEN_ATTACH;
5497 break;
5498#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02005499 case QEMU_OPTION_readconfig:
5500 {
5501 FILE *fp;
5502 fp = fopen(optarg, "r");
5503 if (fp == NULL) {
5504 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5505 exit(1);
5506 }
5507 if (qemu_config_parse(fp) != 0) {
5508 exit(1);
5509 }
5510 fclose(fp);
5511 break;
5512 }
5513 case QEMU_OPTION_writeconfig:
5514 {
5515 FILE *fp;
5516 if (strcmp(optarg, "-") == 0) {
5517 fp = stdout;
5518 } else {
5519 fp = fopen(optarg, "w");
5520 if (fp == NULL) {
5521 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5522 exit(1);
5523 }
5524 }
5525 qemu_config_write(fp);
5526 fclose(fp);
5527 break;
5528 }
bellardcd6f1162004-05-13 22:02:20 +00005529 }
bellard0824d6f2003-06-24 13:42:40 +00005530 }
5531 }
bellard330d0412003-07-26 18:11:40 +00005532
Paul Brook5cea8592009-05-30 00:52:44 +01005533 /* If no data_dir is specified then try to find it relative to the
5534 executable path. */
5535 if (!data_dir) {
5536 data_dir = find_datadir(argv[0]);
5537 }
5538 /* If all else fails use the install patch specified when building. */
5539 if (!data_dir) {
5540 data_dir = CONFIG_QEMU_SHAREDIR;
5541 }
5542
Jes Sorensen6be68d72009-07-23 17:03:42 +02005543 /*
5544 * Default to max_cpus = smp_cpus, in case the user doesn't
5545 * specify a max_cpus value.
5546 */
5547 if (!max_cpus)
5548 max_cpus = smp_cpus;
5549
balrog3d878ca2008-10-28 10:59:59 +00005550 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005551 if (smp_cpus > machine->max_cpus) {
5552 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5553 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5554 machine->max_cpus);
5555 exit(1);
5556 }
5557
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005558 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
5559
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005560 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005561 if (default_parallel)
5562 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005563 if (default_serial && default_monitor) {
5564 add_device_config(DEV_SERIAL, "mon:stdio");
5565 } else {
5566 if (default_serial)
5567 add_device_config(DEV_SERIAL, "stdio");
5568 if (default_monitor)
5569 add_device_config(DEV_MONITOR, "stdio");
5570 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005571 } else {
5572 if (default_serial)
5573 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005574 if (default_parallel)
5575 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005576 if (default_monitor)
5577 add_device_config(DEV_MONITOR, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00005578 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01005579 if (default_vga)
5580 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00005581
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01005582 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5583 exit(1);
5584
ths71e3ceb2006-12-22 02:11:31 +00005585#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005586 if (daemonize) {
5587 pid_t pid;
5588
5589 if (pipe(fds) == -1)
5590 exit(1);
5591
5592 pid = fork();
5593 if (pid > 0) {
5594 uint8_t status;
5595 ssize_t len;
5596
5597 close(fds[1]);
5598
5599 again:
ths93815bc2007-03-19 15:58:31 +00005600 len = read(fds[0], &status, 1);
5601 if (len == -1 && (errno == EINTR))
5602 goto again;
5603
5604 if (len != 1)
5605 exit(1);
5606 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005607 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005608 exit(1);
5609 } else
5610 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005611 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005612 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005613
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005614 close(fds[0]);
5615 qemu_set_cloexec(fds[1]);
5616
ths71e3ceb2006-12-22 02:11:31 +00005617 setsid();
5618
5619 pid = fork();
5620 if (pid > 0)
5621 exit(0);
5622 else if (pid < 0)
5623 exit(1);
5624
5625 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005626
5627 signal(SIGTSTP, SIG_IGN);
5628 signal(SIGTTOU, SIG_IGN);
5629 signal(SIGTTIN, SIG_IGN);
5630 }
ths71e3ceb2006-12-22 02:11:31 +00005631
thsaa26bb22007-03-25 21:33:06 +00005632 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005633 if (daemonize) {
5634 uint8_t status = 1;
5635 write(fds[1], &status, 1);
5636 } else
Justin M. Forbes850810d2009-10-01 09:42:56 -05005637 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005638 exit(1);
5639 }
blueswir1b9e82a52009-04-05 18:03:31 +00005640#endif
ths93815bc2007-03-19 15:58:31 +00005641
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005642 if (kvm_enabled()) {
5643 int ret;
5644
5645 ret = kvm_init(smp_cpus);
5646 if (ret < 0) {
5647 fprintf(stderr, "failed to initialize KVM\n");
5648 exit(1);
5649 }
5650 }
5651
aliguori3fcf7b62009-04-24 18:03:25 +00005652 if (qemu_init_main_loop()) {
5653 fprintf(stderr, "qemu_init_main_loop failed\n");
5654 exit(1);
5655 }
bellarda20dd502003-09-30 21:07:02 +00005656 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005657
thsf8d39c02008-07-03 10:01:15 +00005658 if (!linux_boot && *kernel_cmdline != '\0') {
5659 fprintf(stderr, "-append only allowed with -kernel option\n");
5660 exit(1);
5661 }
5662
5663 if (!linux_boot && initrd_filename != NULL) {
5664 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5665 exit(1);
5666 }
5667
Filip Navarabf65f532009-07-27 10:02:04 -05005668#ifndef _WIN32
5669 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005670 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005671#endif
ths3b46e622007-09-17 08:09:54 +00005672
aliguori7183b4b2008-11-05 20:40:18 +00005673 if (init_timer_alarm() < 0) {
5674 fprintf(stderr, "could not initialize alarm timer\n");
5675 exit(1);
5676 }
pbrook2e70f6e2008-06-29 01:03:05 +00005677 if (use_icount && icount_time_shift < 0) {
5678 use_icount = 2;
5679 /* 125MIPS seems a reasonable initial guess at the guest speed.
5680 It will be corrected fairly quickly anyway. */
5681 icount_time_shift = 3;
5682 init_icount_adjust();
5683 }
pbrook634fce92006-07-15 17:40:09 +00005684
bellardfd1dff42006-02-01 21:29:26 +00005685#ifdef _WIN32
5686 socket_init();
5687#endif
5688
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005689 if (net_init_clients() < 0) {
5690 exit(1);
bellard702c6512004-04-02 21:21:32 +00005691 }
bellardf1510b22003-06-25 00:07:40 +00005692
Glauber Costa406c8df2009-06-17 09:05:30 -04005693 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5694 net_set_boot_mask(net_boot);
5695
balrogdc72ac12008-11-09 00:04:26 +00005696 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005697 if (foreach_device_config(DEV_BT, bt_parse))
5698 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005699
bellard0824d6f2003-06-24 13:42:40 +00005700 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005701 if (ram_size == 0)
5702 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005703
bellard26a5f132008-05-28 12:30:31 +00005704 /* init the dynamic translator */
5705 cpu_exec_init_all(tb_size * 1024 * 1024);
5706
Markus Armbrustereb852012009-10-27 18:41:44 +01005707 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00005708
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005709 blk_mig_init();
5710
thse4bcb142007-12-02 04:51:10 +00005711 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005712 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005713
balrog9d413d12007-12-04 00:10:34 +00005714 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005715 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005716
balrog9d413d12007-12-04 00:10:34 +00005717 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005718 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005719
ths96d30e42007-01-07 20:42:14 +00005720 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005721 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005722 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5723 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005724 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005725
Juan Quintela2faf58c2009-09-10 03:04:28 +02005726 vmstate_register(0, &vmstate_timers ,&timers_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005727 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
5728 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005729
aliguori268a3622009-04-21 22:30:27 +00005730 if (nb_numa_nodes > 0) {
5731 int i;
5732
5733 if (nb_numa_nodes > smp_cpus) {
5734 nb_numa_nodes = smp_cpus;
5735 }
5736
5737 /* If no memory size if given for any node, assume the default case
5738 * and distribute the available memory equally across all nodes
5739 */
5740 for (i = 0; i < nb_numa_nodes; i++) {
5741 if (node_mem[i] != 0)
5742 break;
5743 }
5744 if (i == nb_numa_nodes) {
5745 uint64_t usedmem = 0;
5746
5747 /* On Linux, the each node's border has to be 8MB aligned,
5748 * the final node gets the rest.
5749 */
5750 for (i = 0; i < nb_numa_nodes - 1; i++) {
5751 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5752 usedmem += node_mem[i];
5753 }
5754 node_mem[i] = ram_size - usedmem;
5755 }
5756
5757 for (i = 0; i < nb_numa_nodes; i++) {
5758 if (node_cpumask[i] != 0)
5759 break;
5760 }
5761 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5762 * must cope with this anyway, because there are BIOSes out there in
5763 * real machines which also use this scheme.
5764 */
5765 if (i == nb_numa_nodes) {
5766 for (i = 0; i < smp_cpus; i++) {
5767 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5768 }
5769 }
5770 }
5771
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005772 if (foreach_device_config(DEV_MONITOR, monitor_parse) < 0)
5773 exit(1);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005774 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
5775 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005776 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
5777 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00005778
5779 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5780 const char *devname = virtio_consoles[i];
5781 if (devname && strcmp(devname, "none")) {
5782 char label[32];
5783 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005784 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005785 if (!virtcon_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005786 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
5787 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005788 exit(1);
5789 }
5790 }
5791 }
5792
Paul Brookaae94602009-05-14 22:35:06 +01005793 module_call_init(MODULE_INIT_DEVICE);
5794
Markus Armbruster09aaa162009-08-21 10:31:34 +02005795 if (watchdog) {
5796 i = select_watchdog(watchdog);
5797 if (i > 0)
5798 exit (i == 1 ? 1 : 0);
5799 }
5800
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005801 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01005802 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005803 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005804 qemu_add_globals();
5805
Paul Brookfbe1b592009-05-13 17:56:25 +01005806 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005807 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5808
aliguori268a3622009-04-21 22:30:27 +00005809
Juan Quintela67b3b712009-08-28 19:25:15 +02005810#ifndef _WIN32
5811 /* must be after terminal init, SDL library changes signal handlers */
5812 sighandler_setup();
5813#endif
5814
aliguori268a3622009-04-21 22:30:27 +00005815 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5816 for (i = 0; i < nb_numa_nodes; i++) {
5817 if (node_cpumask[i] & (1 << env->cpu_index)) {
5818 env->numa_node = i;
5819 }
5820 }
5821 }
5822
aliguori6f338c32009-02-11 15:21:54 +00005823 current_machine = machine;
5824
aliguori3023f332009-01-16 19:04:14 +00005825 /* init USB devices */
5826 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005827 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5828 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005829 }
5830
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005831 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005832 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005833 exit(1);
5834
aliguori8f391ab2009-01-19 16:34:10 +00005835 if (!display_state)
5836 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005837 /* just use the first displaystate for the moment */
5838 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005839
5840 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005841#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005842 display_type = DT_SDL;
5843#else
5844 display_type = DT_VNC;
5845 vnc_display = "localhost:0,to=99";
5846 show_vnc_port = 1;
5847#endif
5848 }
5849
5850
5851 switch (display_type) {
5852 case DT_NOGRAPHIC:
5853 break;
5854#if defined(CONFIG_CURSES)
5855 case DT_CURSES:
5856 curses_display_init(ds, full_screen);
5857 break;
5858#endif
bellard5b0753e2005-03-01 21:37:28 +00005859#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005860 case DT_SDL:
5861 sdl_display_init(ds, full_screen, no_frame);
5862 break;
bellard5b0753e2005-03-01 21:37:28 +00005863#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005864 case DT_SDL:
5865 cocoa_display_init(ds, full_screen);
5866 break;
bellard313aa562003-08-10 21:52:11 +00005867#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005868 case DT_VNC:
5869 vnc_display_init(ds);
5870 if (vnc_display_open(ds, vnc_display) < 0)
5871 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005872
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005873 if (show_vnc_port) {
5874 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005875 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005876 break;
5877 default:
5878 break;
bellard313aa562003-08-10 21:52:11 +00005879 }
aliguori7d957bd2009-01-15 22:14:11 +00005880 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005881
aliguori3023f332009-01-16 19:04:14 +00005882 dcl = ds->listeners;
5883 while (dcl != NULL) {
5884 if (dcl->dpy_refresh != NULL) {
5885 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5886 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005887 }
aliguori3023f332009-01-16 19:04:14 +00005888 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005889 }
aliguori3023f332009-01-16 19:04:14 +00005890
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005891 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005892 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5893 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5894 }
5895
aliguori2796dae2009-01-16 20:23:27 +00005896 text_consoles_set_display(display_state);
5897
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005898 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005899 if (monitor_hds[i]) {
Gerd Hoffmann4e307fc2009-12-08 13:11:37 +01005900 monitor_init(monitor_hds[i],
5901 MONITOR_USE_READLINE |
5902 ((i == 0) ? MONITOR_IS_DEFAULT : 0));
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005903 }
5904 }
bellard82c643f2004-07-14 17:28:13 +00005905
aliguori59030a82009-04-05 18:43:41 +00005906 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5907 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5908 gdbstub_dev);
5909 exit(1);
balrog45669e02007-07-02 13:20:17 +00005910 }
balrog45669e02007-07-02 13:20:17 +00005911
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02005912 qdev_machine_creation_done();
5913
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02005914 rom_load_all();
5915
Juan Quintela504c2942009-11-12 00:39:13 +01005916 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02005917 if (loadvm) {
5918 if (load_vmstate(cur_mon, loadvm) < 0) {
5919 autostart = 0;
5920 }
5921 }
bellardd63d3072004-10-03 13:29:03 +00005922
Glauber Costa2bb8c102009-07-24 16:20:23 -04005923 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005924 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005925 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005926 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005927 }
thsffd843b2006-12-21 19:46:43 +00005928
blueswir1b9e82a52009-04-05 18:03:31 +00005929#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005930 if (daemonize) {
5931 uint8_t status = 0;
5932 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005933
5934 again1:
5935 len = write(fds[1], &status, 1);
5936 if (len == -1 && (errno == EINTR))
5937 goto again1;
5938
5939 if (len != 1)
5940 exit(1);
5941
aliguoribd54b862008-07-23 00:58:33 +00005942 chdir("/");
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005943 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005944 if (fd == -1)
5945 exit(1);
aliguori08585322009-02-27 22:09:45 +00005946 }
ths71e3ceb2006-12-22 02:11:31 +00005947
aliguori08585322009-02-27 22:09:45 +00005948 if (run_as) {
5949 pwd = getpwnam(run_as);
5950 if (!pwd) {
5951 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5952 exit(1);
5953 }
5954 }
ths71e3ceb2006-12-22 02:11:31 +00005955
aliguori08585322009-02-27 22:09:45 +00005956 if (chroot_dir) {
5957 if (chroot(chroot_dir) < 0) {
5958 fprintf(stderr, "chroot failed\n");
5959 exit(1);
5960 }
5961 chdir("/");
5962 }
5963
5964 if (run_as) {
5965 if (setgid(pwd->pw_gid) < 0) {
5966 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5967 exit(1);
5968 }
5969 if (setuid(pwd->pw_uid) < 0) {
5970 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5971 exit(1);
5972 }
5973 if (setuid(0) != -1) {
5974 fprintf(stderr, "Dropping privileges failed\n");
5975 exit(1);
5976 }
5977 }
aliguori08585322009-02-27 22:09:45 +00005978
5979 if (daemonize) {
5980 dup2(fd, 0);
5981 dup2(fd, 1);
5982 dup2(fd, 2);
5983
5984 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005985 }
blueswir1b9e82a52009-04-05 18:03:31 +00005986#endif
ths71e3ceb2006-12-22 02:11:31 +00005987
bellard8a7ddc32004-03-31 19:00:16 +00005988 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005989 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005990 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005991
bellard0824d6f2003-06-24 13:42:40 +00005992 return 0;
5993}