bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1 | /* |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 2 | * QEMU System Emulator |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 1df912c | 2003-06-25 16:20:35 +0000 | [diff] [blame] | 6 | * 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. |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 23 | */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <signal.h> |
| 27 | #include <time.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 28 | #include <errno.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 29 | #include <sys/time.h> |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 30 | #include <zlib.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 31 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 32 | /* Needed early for CONFIG_BSD etc. */ |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 33 | #include "config-host.h" |
Blue Swirl | 96555a9 | 2009-07-17 11:01:45 +0000 | [diff] [blame] | 34 | /* Needed early to override system queue definitions on BSD */ |
| 35 | #include "sys-queue.h" |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 36 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 37 | #ifndef _WIN32 |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 38 | #include <libgen.h> |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 39 | #include <pwd.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 40 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 41 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 42 | #include <termios.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 43 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 44 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 45 | #include <sys/resource.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 46 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 47 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 48 | #include <net/if.h> |
| 49 | #if defined(__NetBSD__) |
| 50 | #include <net/if_tap.h> |
| 51 | #endif |
| 52 | #ifdef __linux__ |
| 53 | #include <linux/if_tun.h> |
| 54 | #endif |
| 55 | #include <arpa/inet.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 56 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 57 | #include <netdb.h> |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 58 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 59 | #ifdef CONFIG_BSD |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 60 | #include <sys/stat.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 61 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 62 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 63 | #else |
| 64 | #include <util.h> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 65 | #endif |
ths | 5c40d2b | 2007-06-23 16:03:36 +0000 | [diff] [blame] | 66 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
| 67 | #include <freebsd/stdlib.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 68 | #else |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 69 | #ifdef __linux__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 70 | #include <pty.h> |
| 71 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 72 | #include <linux/rtc.h> |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 73 | #include <sys/prctl.h> |
ths | bd494f4 | 2007-09-16 20:03:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* For the benefit of older linux systems which don't supply it, |
| 76 | we use a local copy of hpet.h. */ |
| 77 | /* #include <linux/hpet.h> */ |
| 78 | #include "hpet.h" |
| 79 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 80 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 81 | #include <linux/parport.h> |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 82 | #endif |
| 83 | #ifdef __sun__ |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 84 | #include <sys/stat.h> |
| 85 | #include <sys/ethernet.h> |
| 86 | #include <sys/sockio.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 87 | #include <netinet/arp.h> |
| 88 | #include <netinet/in.h> |
| 89 | #include <netinet/in_systm.h> |
| 90 | #include <netinet/ip.h> |
| 91 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 92 | #include <netinet/udp.h> |
| 93 | #include <netinet/tcp.h> |
| 94 | #include <net/if.h> |
| 95 | #include <syslog.h> |
| 96 | #include <stropts.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 97 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 98 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 99 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 100 | |
blueswir1 | 9892fbf | 2008-08-24 10:34:20 +0000 | [diff] [blame] | 101 | #if defined(__OpenBSD__) |
| 102 | #include <util.h> |
| 103 | #endif |
| 104 | |
ths | 8a16d27 | 2008-07-19 09:56:24 +0000 | [diff] [blame] | 105 | #if defined(CONFIG_VDE) |
| 106 | #include <libvdeplug.h> |
| 107 | #endif |
| 108 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 109 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 110 | #include <windows.h> |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 111 | #include <mmsystem.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 114 | #ifdef CONFIG_SDL |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 115 | #if defined(__APPLE__) || defined(main) |
Stefan Weil | 6693665 | 2009-06-13 13:19:11 +0200 | [diff] [blame] | 116 | #include <SDL.h> |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 117 | int qemu_main(int argc, char **argv, char **envp); |
| 118 | int main(int argc, char **argv) |
| 119 | { |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 120 | return qemu_main(argc, argv, NULL); |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 121 | } |
| 122 | #undef main |
| 123 | #define main qemu_main |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 124 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 125 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 126 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 127 | #ifdef CONFIG_COCOA |
| 128 | #undef main |
| 129 | #define main qemu_main |
| 130 | #endif /* CONFIG_COCOA */ |
| 131 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 132 | #include "hw/hw.h" |
| 133 | #include "hw/boards.h" |
| 134 | #include "hw/usb.h" |
| 135 | #include "hw/pcmcia.h" |
| 136 | #include "hw/pc.h" |
| 137 | #include "hw/audiodev.h" |
| 138 | #include "hw/isa.h" |
| 139 | #include "hw/baum.h" |
| 140 | #include "hw/bt.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 141 | #include "hw/watchdog.h" |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 142 | #include "hw/smbios.h" |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 143 | #include "hw/xen.h" |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 144 | #include "hw/qdev.h" |
aurel32 | 5ef4efa | 2009-03-10 21:43:35 +0000 | [diff] [blame] | 145 | #include "bt-host.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 146 | #include "net.h" |
| 147 | #include "monitor.h" |
| 148 | #include "console.h" |
| 149 | #include "sysemu.h" |
| 150 | #include "gdbstub.h" |
| 151 | #include "qemu-timer.h" |
| 152 | #include "qemu-char.h" |
| 153 | #include "cache-utils.h" |
| 154 | #include "block.h" |
blueswir1 | a718ace | 2009-03-28 08:24:44 +0000 | [diff] [blame] | 155 | #include "dma.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 156 | #include "audio/audio.h" |
| 157 | #include "migration.h" |
| 158 | #include "kvm.h" |
| 159 | #include "balloon.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 160 | #include "qemu-option.h" |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 161 | #include "qemu-config.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 162 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 163 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 164 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 165 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 166 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 167 | #include "qemu_socket.h" |
| 168 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 169 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 170 | |
blueswir1 | 9dc63a1 | 2008-10-04 07:25:46 +0000 | [diff] [blame] | 171 | //#define DEBUG_NET |
| 172 | //#define DEBUG_SLIRP |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 173 | |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 174 | #define DEFAULT_RAM_SIZE 128 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 175 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 176 | /* Maximum number of monitor devices */ |
| 177 | #define MAX_MONITOR_DEVICES 10 |
| 178 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 179 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 180 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 181 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 182 | to store the VM snapshots */ |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 183 | struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives); |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 184 | struct driveoptlist driveopts = TAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 185 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 186 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 187 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 188 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 189 | int64_t ticks_per_sec; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 190 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 191 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 192 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 193 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 194 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 195 | static int rtc_utc = 1; |
| 196 | static int rtc_date_offset = -1; /* -1 means no change */ |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 197 | int vga_interface_type = VGA_CIRRUS; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 198 | #ifdef TARGET_SPARC |
| 199 | int graphic_width = 1024; |
| 200 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 201 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 202 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 203 | int graphic_width = 800; |
| 204 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 205 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 206 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 207 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 208 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 209 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 210 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 211 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 212 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 213 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 214 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 215 | #ifdef TARGET_I386 |
| 216 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 217 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 218 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 219 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 220 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 221 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 222 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 223 | int smp_cores = 1; |
| 224 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 225 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 226 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 227 | int no_hpet = 0; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 228 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 229 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 230 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 231 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 232 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 233 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 234 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 235 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 236 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 237 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 238 | const char *option_rom[MAX_OPTION_ROMS]; |
| 239 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 240 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 241 | #ifdef TARGET_ARM |
| 242 | int old_param = 0; |
| 243 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 244 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 245 | int alt_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 246 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 247 | unsigned int nb_prom_envs = 0; |
| 248 | const char *prom_envs[MAX_PROM_ENVS]; |
| 249 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 250 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 251 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 252 | int nb_numa_nodes; |
| 253 | uint64_t node_mem[MAX_NODES]; |
| 254 | uint64_t node_cpumask[MAX_NODES]; |
| 255 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 256 | static CPUState *cur_cpu; |
| 257 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 258 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 259 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 260 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 261 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 262 | #define MAX_ICOUNT_SHIFT 10 |
| 263 | /* Compensate for varying guest execution speed. */ |
| 264 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 265 | static QEMUTimer *icount_rt_timer; |
| 266 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 267 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 268 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 269 | uint8_t qemu_uuid[16]; |
| 270 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 271 | static QEMUBootSetHandler *boot_set_handler; |
| 272 | static void *boot_set_opaque; |
| 273 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 274 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 275 | /* x86 ISA bus support */ |
| 276 | |
| 277 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 278 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 279 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 280 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 281 | void hw_error(const char *fmt, ...) |
| 282 | { |
| 283 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 284 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 285 | |
| 286 | va_start(ap, fmt); |
| 287 | fprintf(stderr, "qemu: hardware error: "); |
| 288 | vfprintf(stderr, fmt, ap); |
| 289 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 290 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 291 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 292 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 293 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 294 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 295 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 296 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 297 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 298 | va_end(ap); |
| 299 | abort(); |
| 300 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 301 | |
| 302 | static void set_proc_name(const char *s) |
| 303 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 304 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 305 | char name[16]; |
| 306 | if (!s) |
| 307 | return; |
| 308 | name[sizeof(name) - 1] = 0; |
| 309 | strncpy(name, s, sizeof(name)); |
| 310 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 311 | This simple way is enough for `top'. */ |
| 312 | prctl(PR_SET_NAME, name); |
| 313 | #endif |
| 314 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 315 | |
| 316 | /***************/ |
| 317 | /* ballooning */ |
| 318 | |
| 319 | static QEMUBalloonEvent *qemu_balloon_event; |
| 320 | void *qemu_balloon_event_opaque; |
| 321 | |
| 322 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 323 | { |
| 324 | qemu_balloon_event = func; |
| 325 | qemu_balloon_event_opaque = opaque; |
| 326 | } |
| 327 | |
| 328 | void qemu_balloon(ram_addr_t target) |
| 329 | { |
| 330 | if (qemu_balloon_event) |
| 331 | qemu_balloon_event(qemu_balloon_event_opaque, target); |
| 332 | } |
| 333 | |
| 334 | ram_addr_t qemu_balloon_status(void) |
| 335 | { |
| 336 | if (qemu_balloon_event) |
| 337 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
| 338 | return 0; |
| 339 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 340 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 341 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 342 | /* keyboard/mouse */ |
| 343 | |
| 344 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 345 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 346 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 347 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 348 | |
| 349 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 350 | { |
| 351 | qemu_put_kbd_event_opaque = opaque; |
| 352 | qemu_put_kbd_event = func; |
| 353 | } |
| 354 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 355 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 356 | void *opaque, int absolute, |
| 357 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 358 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 359 | QEMUPutMouseEntry *s, *cursor; |
| 360 | |
| 361 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 362 | |
| 363 | s->qemu_put_mouse_event = func; |
| 364 | s->qemu_put_mouse_event_opaque = opaque; |
| 365 | s->qemu_put_mouse_event_absolute = absolute; |
| 366 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 367 | s->next = NULL; |
| 368 | |
| 369 | if (!qemu_put_mouse_event_head) { |
| 370 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 371 | return s; |
| 372 | } |
| 373 | |
| 374 | cursor = qemu_put_mouse_event_head; |
| 375 | while (cursor->next != NULL) |
| 376 | cursor = cursor->next; |
| 377 | |
| 378 | cursor->next = s; |
| 379 | qemu_put_mouse_event_current = s; |
| 380 | |
| 381 | return s; |
| 382 | } |
| 383 | |
| 384 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 385 | { |
| 386 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 387 | |
| 388 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 389 | return; |
| 390 | |
| 391 | cursor = qemu_put_mouse_event_head; |
| 392 | while (cursor != NULL && cursor != entry) { |
| 393 | prev = cursor; |
| 394 | cursor = cursor->next; |
| 395 | } |
| 396 | |
| 397 | if (cursor == NULL) // does not exist or list empty |
| 398 | return; |
| 399 | else if (prev == NULL) { // entry is head |
| 400 | qemu_put_mouse_event_head = cursor->next; |
| 401 | if (qemu_put_mouse_event_current == entry) |
| 402 | qemu_put_mouse_event_current = cursor->next; |
| 403 | qemu_free(entry->qemu_put_mouse_event_name); |
| 404 | qemu_free(entry); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | prev->next = entry->next; |
| 409 | |
| 410 | if (qemu_put_mouse_event_current == entry) |
| 411 | qemu_put_mouse_event_current = prev; |
| 412 | |
| 413 | qemu_free(entry->qemu_put_mouse_event_name); |
| 414 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void kbd_put_keycode(int keycode) |
| 418 | { |
| 419 | if (qemu_put_kbd_event) { |
| 420 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 425 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 426 | QEMUPutMouseEvent *mouse_event; |
| 427 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 428 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 429 | |
| 430 | if (!qemu_put_mouse_event_current) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | mouse_event = |
| 435 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 436 | mouse_event_opaque = |
| 437 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 438 | |
| 439 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 440 | if (graphic_rotate) { |
| 441 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 442 | width = 0x7fff; |
| 443 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 444 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 445 | mouse_event(mouse_event_opaque, |
| 446 | width - dy, dx, dz, buttons_state); |
| 447 | } else |
| 448 | mouse_event(mouse_event_opaque, |
| 449 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 453 | int kbd_mouse_is_absolute(void) |
| 454 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 455 | if (!qemu_put_mouse_event_current) |
| 456 | return 0; |
| 457 | |
| 458 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 459 | } |
| 460 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 461 | void do_info_mice(Monitor *mon) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 462 | { |
| 463 | QEMUPutMouseEntry *cursor; |
| 464 | int index = 0; |
| 465 | |
| 466 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 467 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 471 | monitor_printf(mon, "Mouse devices available:\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 472 | cursor = qemu_put_mouse_event_head; |
| 473 | while (cursor != NULL) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 474 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
| 475 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 476 | index, cursor->qemu_put_mouse_event_name); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 477 | index++; |
| 478 | cursor = cursor->next; |
| 479 | } |
| 480 | } |
| 481 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 482 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 483 | { |
| 484 | QEMUPutMouseEntry *cursor; |
| 485 | int i = 0; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 486 | int index = qdict_get_int(qdict, "index"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 487 | |
| 488 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 489 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | cursor = qemu_put_mouse_event_head; |
| 494 | while (cursor != NULL && index != i) { |
| 495 | i++; |
| 496 | cursor = cursor->next; |
| 497 | } |
| 498 | |
| 499 | if (cursor != NULL) |
| 500 | qemu_put_mouse_event_current = cursor; |
| 501 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 502 | monitor_printf(mon, "Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 503 | } |
| 504 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 505 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 506 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 507 | { |
| 508 | union { |
| 509 | uint64_t ll; |
| 510 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 511 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 512 | uint32_t high, low; |
| 513 | #else |
| 514 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 515 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 516 | } l; |
| 517 | } u, res; |
| 518 | uint64_t rl, rh; |
| 519 | |
| 520 | u.ll = a; |
| 521 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 522 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 523 | rh += (rl >> 32); |
| 524 | res.l.high = rh / c; |
| 525 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 526 | return res.ll; |
| 527 | } |
| 528 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 529 | /***********************************************************/ |
| 530 | /* real time host monotonic timer */ |
| 531 | |
| 532 | #define QEMU_TIMER_BASE 1000000000LL |
| 533 | |
| 534 | #ifdef WIN32 |
| 535 | |
| 536 | static int64_t clock_freq; |
| 537 | |
| 538 | static void init_get_clock(void) |
| 539 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 540 | LARGE_INTEGER freq; |
| 541 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 542 | ret = QueryPerformanceFrequency(&freq); |
| 543 | if (ret == 0) { |
| 544 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 545 | exit(1); |
| 546 | } |
| 547 | clock_freq = freq.QuadPart; |
| 548 | } |
| 549 | |
| 550 | static int64_t get_clock(void) |
| 551 | { |
| 552 | LARGE_INTEGER ti; |
| 553 | QueryPerformanceCounter(&ti); |
| 554 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 555 | } |
| 556 | |
| 557 | #else |
| 558 | |
| 559 | static int use_rt_clock; |
| 560 | |
| 561 | static void init_get_clock(void) |
| 562 | { |
| 563 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 564 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 565 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 566 | { |
| 567 | struct timespec ts; |
| 568 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 569 | use_rt_clock = 1; |
| 570 | } |
| 571 | } |
| 572 | #endif |
| 573 | } |
| 574 | |
| 575 | static int64_t get_clock(void) |
| 576 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 577 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 578 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 579 | if (use_rt_clock) { |
| 580 | struct timespec ts; |
| 581 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 582 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 583 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 584 | #endif |
| 585 | { |
| 586 | /* XXX: using gettimeofday leads to problems if the date |
| 587 | changes, so it should be avoided. */ |
| 588 | struct timeval tv; |
| 589 | gettimeofday(&tv, NULL); |
| 590 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 591 | } |
| 592 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 593 | #endif |
| 594 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 595 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 596 | static int64_t cpu_get_icount(void) |
| 597 | { |
| 598 | int64_t icount; |
| 599 | CPUState *env = cpu_single_env;; |
| 600 | icount = qemu_icount; |
| 601 | if (env) { |
| 602 | if (!can_do_io(env)) |
| 603 | fprintf(stderr, "Bad clock read\n"); |
| 604 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 605 | } |
| 606 | return qemu_icount_bias + (icount << icount_time_shift); |
| 607 | } |
| 608 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 609 | /***********************************************************/ |
| 610 | /* guest cycle counter */ |
| 611 | |
| 612 | static int64_t cpu_ticks_prev; |
| 613 | static int64_t cpu_ticks_offset; |
| 614 | static int64_t cpu_clock_offset; |
| 615 | static int cpu_ticks_enabled; |
| 616 | |
| 617 | /* return the host CPU cycle counter and handle stop/restart */ |
| 618 | int64_t cpu_get_ticks(void) |
| 619 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 620 | if (use_icount) { |
| 621 | return cpu_get_icount(); |
| 622 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 623 | if (!cpu_ticks_enabled) { |
| 624 | return cpu_ticks_offset; |
| 625 | } else { |
| 626 | int64_t ticks; |
| 627 | ticks = cpu_get_real_ticks(); |
| 628 | if (cpu_ticks_prev > ticks) { |
| 629 | /* Note: non increasing ticks may happen if the host uses |
| 630 | software suspend */ |
| 631 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 632 | } |
| 633 | cpu_ticks_prev = ticks; |
| 634 | return ticks + cpu_ticks_offset; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 639 | static int64_t cpu_get_clock(void) |
| 640 | { |
| 641 | int64_t ti; |
| 642 | if (!cpu_ticks_enabled) { |
| 643 | return cpu_clock_offset; |
| 644 | } else { |
| 645 | ti = get_clock(); |
| 646 | return ti + cpu_clock_offset; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* enable cpu_get_ticks() */ |
| 651 | void cpu_enable_ticks(void) |
| 652 | { |
| 653 | if (!cpu_ticks_enabled) { |
| 654 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 655 | cpu_clock_offset -= get_clock(); |
| 656 | cpu_ticks_enabled = 1; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 661 | cpu_get_ticks() after that. */ |
| 662 | void cpu_disable_ticks(void) |
| 663 | { |
| 664 | if (cpu_ticks_enabled) { |
| 665 | cpu_ticks_offset = cpu_get_ticks(); |
| 666 | cpu_clock_offset = cpu_get_clock(); |
| 667 | cpu_ticks_enabled = 0; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /***********************************************************/ |
| 672 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 673 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 674 | #define QEMU_TIMER_REALTIME 0 |
| 675 | #define QEMU_TIMER_VIRTUAL 1 |
| 676 | |
| 677 | struct QEMUClock { |
| 678 | int type; |
| 679 | /* XXX: add frequency */ |
| 680 | }; |
| 681 | |
| 682 | struct QEMUTimer { |
| 683 | QEMUClock *clock; |
| 684 | int64_t expire_time; |
| 685 | QEMUTimerCB *cb; |
| 686 | void *opaque; |
| 687 | struct QEMUTimer *next; |
| 688 | }; |
| 689 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 690 | struct qemu_alarm_timer { |
| 691 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 692 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 693 | |
| 694 | int (*start)(struct qemu_alarm_timer *t); |
| 695 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 696 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 697 | void *priv; |
| 698 | }; |
| 699 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 700 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 701 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 702 | |
| 703 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 704 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 705 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 709 | { |
| 710 | if (!alarm_has_dynticks(t)) |
| 711 | return; |
| 712 | |
| 713 | t->rearm(t); |
| 714 | } |
| 715 | |
| 716 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 717 | #define MIN_TIMER_REARM_US 250 |
| 718 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 719 | static struct qemu_alarm_timer *alarm_timer; |
| 720 | |
| 721 | #ifdef _WIN32 |
| 722 | |
| 723 | struct qemu_alarm_win32 { |
| 724 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 725 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 726 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 727 | |
| 728 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 729 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 730 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 731 | |
| 732 | #else |
| 733 | |
| 734 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 735 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 736 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 737 | #ifdef __linux__ |
| 738 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 739 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 740 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 741 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 742 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 743 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 744 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 745 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 746 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 747 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 748 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 749 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 750 | |
| 751 | #endif /* _WIN32 */ |
| 752 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 753 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 754 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 755 | When the guest is idle real and virtual time will be aligned in |
| 756 | the IO wait loop. */ |
| 757 | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
| 758 | |
| 759 | static void icount_adjust(void) |
| 760 | { |
| 761 | int64_t cur_time; |
| 762 | int64_t cur_icount; |
| 763 | int64_t delta; |
| 764 | static int64_t last_delta; |
| 765 | /* If the VM is not running, then do nothing. */ |
| 766 | if (!vm_running) |
| 767 | return; |
| 768 | |
| 769 | cur_time = cpu_get_clock(); |
| 770 | cur_icount = qemu_get_clock(vm_clock); |
| 771 | delta = cur_icount - cur_time; |
| 772 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 773 | if (delta > 0 |
| 774 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 775 | && icount_time_shift > 0) { |
| 776 | /* The guest is getting too far ahead. Slow time down. */ |
| 777 | icount_time_shift--; |
| 778 | } |
| 779 | if (delta < 0 |
| 780 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 781 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 782 | /* The guest is getting too far behind. Speed time up. */ |
| 783 | icount_time_shift++; |
| 784 | } |
| 785 | last_delta = delta; |
| 786 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 787 | } |
| 788 | |
| 789 | static void icount_adjust_rt(void * opaque) |
| 790 | { |
| 791 | qemu_mod_timer(icount_rt_timer, |
| 792 | qemu_get_clock(rt_clock) + 1000); |
| 793 | icount_adjust(); |
| 794 | } |
| 795 | |
| 796 | static void icount_adjust_vm(void * opaque) |
| 797 | { |
| 798 | qemu_mod_timer(icount_vm_timer, |
| 799 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 800 | icount_adjust(); |
| 801 | } |
| 802 | |
| 803 | static void init_icount_adjust(void) |
| 804 | { |
| 805 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 806 | The realtime trigger catches emulated time passing too slowly, |
| 807 | the virtual time trigger catches emulated time passing too fast. |
| 808 | Realtime triggers occur even when idle, so use them less frequently |
| 809 | than VM triggers. */ |
| 810 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 811 | qemu_mod_timer(icount_rt_timer, |
| 812 | qemu_get_clock(rt_clock) + 1000); |
| 813 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 814 | qemu_mod_timer(icount_vm_timer, |
| 815 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 816 | } |
| 817 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 818 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 819 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 820 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 821 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 822 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 823 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 824 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 825 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 826 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 827 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 828 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 829 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 830 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 831 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 832 | {"win32", 0, win32_start_timer, |
| 833 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 834 | #endif |
| 835 | {NULL, } |
| 836 | }; |
| 837 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 838 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 839 | { |
| 840 | int i; |
| 841 | |
| 842 | printf("Available alarm timers, in order of precedence:\n"); |
| 843 | for (i = 0; alarm_timers[i].name; i++) |
| 844 | printf("%s\n", alarm_timers[i].name); |
| 845 | } |
| 846 | |
| 847 | static void configure_alarms(char const *opt) |
| 848 | { |
| 849 | int i; |
| 850 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 851 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 852 | char *arg; |
| 853 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 854 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 855 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 856 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 857 | show_available_alarms(); |
| 858 | exit(0); |
| 859 | } |
| 860 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 861 | arg = qemu_strdup(opt); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 862 | |
| 863 | /* Reorder the array */ |
| 864 | name = strtok(arg, ","); |
| 865 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 866 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 867 | if (!strcmp(alarm_timers[i].name, name)) |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | if (i == count) { |
| 872 | fprintf(stderr, "Unknown clock %s\n", name); |
| 873 | goto next; |
| 874 | } |
| 875 | |
| 876 | if (i < cur) |
| 877 | /* Ignore */ |
| 878 | goto next; |
| 879 | |
| 880 | /* Swap */ |
| 881 | tmp = alarm_timers[i]; |
| 882 | alarm_timers[i] = alarm_timers[cur]; |
| 883 | alarm_timers[cur] = tmp; |
| 884 | |
| 885 | cur++; |
| 886 | next: |
| 887 | name = strtok(NULL, ","); |
| 888 | } |
| 889 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 890 | qemu_free(arg); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 891 | |
| 892 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 893 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 894 | for (i = cur; i < count; i++) |
| 895 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 896 | } else { |
| 897 | show_available_alarms(); |
| 898 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 899 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 900 | } |
| 901 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 902 | QEMUClock *rt_clock; |
| 903 | QEMUClock *vm_clock; |
| 904 | |
| 905 | static QEMUTimer *active_timers[2]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 906 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 907 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 908 | { |
| 909 | QEMUClock *clock; |
| 910 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 911 | clock->type = type; |
| 912 | return clock; |
| 913 | } |
| 914 | |
| 915 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 916 | { |
| 917 | QEMUTimer *ts; |
| 918 | |
| 919 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 920 | ts->clock = clock; |
| 921 | ts->cb = cb; |
| 922 | ts->opaque = opaque; |
| 923 | return ts; |
| 924 | } |
| 925 | |
| 926 | void qemu_free_timer(QEMUTimer *ts) |
| 927 | { |
| 928 | qemu_free(ts); |
| 929 | } |
| 930 | |
| 931 | /* stop a timer, but do not dealloc it */ |
| 932 | void qemu_del_timer(QEMUTimer *ts) |
| 933 | { |
| 934 | QEMUTimer **pt, *t; |
| 935 | |
| 936 | /* NOTE: this code must be signal safe because |
| 937 | qemu_timer_expired() can be called from a signal. */ |
| 938 | pt = &active_timers[ts->clock->type]; |
| 939 | for(;;) { |
| 940 | t = *pt; |
| 941 | if (!t) |
| 942 | break; |
| 943 | if (t == ts) { |
| 944 | *pt = t->next; |
| 945 | break; |
| 946 | } |
| 947 | pt = &t->next; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* modify the current timer so that it will be fired when current_time |
| 952 | >= expire_time. The corresponding callback will be called. */ |
| 953 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 954 | { |
| 955 | QEMUTimer **pt, *t; |
| 956 | |
| 957 | qemu_del_timer(ts); |
| 958 | |
| 959 | /* add the timer in the sorted list */ |
| 960 | /* NOTE: this code must be signal safe because |
| 961 | qemu_timer_expired() can be called from a signal. */ |
| 962 | pt = &active_timers[ts->clock->type]; |
| 963 | for(;;) { |
| 964 | t = *pt; |
| 965 | if (!t) |
| 966 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 967 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 968 | break; |
| 969 | pt = &t->next; |
| 970 | } |
| 971 | ts->expire_time = expire_time; |
| 972 | ts->next = *pt; |
| 973 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 974 | |
| 975 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 976 | if (pt == &active_timers[ts->clock->type]) { |
| 977 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 978 | qemu_rearm_alarm_timer(alarm_timer); |
| 979 | } |
| 980 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 981 | if (use_icount) |
| 982 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 983 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | int qemu_timer_pending(QEMUTimer *ts) |
| 987 | { |
| 988 | QEMUTimer *t; |
| 989 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 990 | if (t == ts) |
| 991 | return 1; |
| 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 996 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 997 | { |
| 998 | if (!timer_head) |
| 999 | return 0; |
| 1000 | return (timer_head->expire_time <= current_time); |
| 1001 | } |
| 1002 | |
| 1003 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1004 | { |
| 1005 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1006 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1007 | for(;;) { |
| 1008 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1009 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | /* remove timer from the list before calling the callback */ |
| 1012 | *ptimer_head = ts->next; |
| 1013 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1014 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1015 | /* run the callback (the timer list can be modified) */ |
| 1016 | ts->cb(ts->opaque); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1021 | { |
| 1022 | switch(clock->type) { |
| 1023 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1024 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1025 | default: |
| 1026 | case QEMU_TIMER_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1027 | if (use_icount) { |
| 1028 | return cpu_get_icount(); |
| 1029 | } else { |
| 1030 | return cpu_get_clock(); |
| 1031 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1035 | static void init_timers(void) |
| 1036 | { |
| 1037 | init_get_clock(); |
| 1038 | ticks_per_sec = QEMU_TIMER_BASE; |
| 1039 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 1040 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 1041 | } |
| 1042 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1043 | /* save a timer */ |
| 1044 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1045 | { |
| 1046 | uint64_t expire_time; |
| 1047 | |
| 1048 | if (qemu_timer_pending(ts)) { |
| 1049 | expire_time = ts->expire_time; |
| 1050 | } else { |
| 1051 | expire_time = -1; |
| 1052 | } |
| 1053 | qemu_put_be64(f, expire_time); |
| 1054 | } |
| 1055 | |
| 1056 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1057 | { |
| 1058 | uint64_t expire_time; |
| 1059 | |
| 1060 | expire_time = qemu_get_be64(f); |
| 1061 | if (expire_time != -1) { |
| 1062 | qemu_mod_timer(ts, expire_time); |
| 1063 | } else { |
| 1064 | qemu_del_timer(ts); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | static void timer_save(QEMUFile *f, void *opaque) |
| 1069 | { |
| 1070 | if (cpu_ticks_enabled) { |
| 1071 | hw_error("cannot save state if virtual timers are running"); |
| 1072 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1073 | qemu_put_be64(f, cpu_ticks_offset); |
| 1074 | qemu_put_be64(f, ticks_per_sec); |
| 1075 | qemu_put_be64(f, cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 1079 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1080 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1081 | return -EINVAL; |
| 1082 | if (cpu_ticks_enabled) { |
| 1083 | return -EINVAL; |
| 1084 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1085 | cpu_ticks_offset=qemu_get_be64(f); |
| 1086 | ticks_per_sec=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1087 | if (version_id == 2) { |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1088 | cpu_clock_offset=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1089 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1093 | static void qemu_event_increment(void); |
| 1094 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1095 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 1096 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 1097 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 1098 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1099 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1100 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1101 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1102 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1103 | #if 0 |
| 1104 | #define DISP_FREQ 1000 |
| 1105 | { |
| 1106 | static int64_t delta_min = INT64_MAX; |
| 1107 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1108 | static int count; |
| 1109 | ti = qemu_get_clock(vm_clock); |
| 1110 | if (last_clock != 0) { |
| 1111 | delta = ti - last_clock; |
| 1112 | if (delta < delta_min) |
| 1113 | delta_min = delta; |
| 1114 | if (delta > delta_max) |
| 1115 | delta_max = delta; |
| 1116 | delta_cum += delta; |
| 1117 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1118 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1119 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 1120 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 1121 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 1122 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 1123 | count = 0; |
| 1124 | delta_min = INT64_MAX; |
| 1125 | delta_max = 0; |
| 1126 | delta_cum = 0; |
| 1127 | } |
| 1128 | } |
| 1129 | last_clock = ti; |
| 1130 | } |
| 1131 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1132 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1133 | (!use_icount && |
| 1134 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 1135 | qemu_get_clock(vm_clock))) || |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1136 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 1137 | qemu_get_clock(rt_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1138 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1139 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1140 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1141 | #ifndef CONFIG_IOTHREAD |
| 1142 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1143 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1144 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1145 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1146 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1147 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1148 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1152 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1153 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1154 | int64_t delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (active_timers[QEMU_TIMER_VIRTUAL]) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1157 | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
| 1158 | qemu_get_clock(vm_clock); |
| 1159 | } else { |
| 1160 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1161 | delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1164 | if (delta < 0) |
| 1165 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1166 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1167 | return delta; |
| 1168 | } |
| 1169 | |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1170 | #if defined(__linux__) || defined(_WIN32) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1171 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1172 | { |
| 1173 | int64_t delta; |
| 1174 | int64_t rtdelta; |
| 1175 | |
| 1176 | if (use_icount) |
| 1177 | delta = INT32_MAX; |
| 1178 | else |
| 1179 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1180 | |
| 1181 | if (active_timers[QEMU_TIMER_REALTIME]) { |
| 1182 | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
| 1183 | qemu_get_clock(rt_clock))*1000; |
| 1184 | if (rtdelta < delta) |
| 1185 | delta = rtdelta; |
| 1186 | } |
| 1187 | |
| 1188 | if (delta < MIN_TIMER_REARM_US) |
| 1189 | delta = MIN_TIMER_REARM_US; |
| 1190 | |
| 1191 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1192 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1193 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1194 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1195 | #ifndef _WIN32 |
| 1196 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1197 | /* Sets a specific flag */ |
| 1198 | static int fcntl_setfl(int fd, int flag) |
| 1199 | { |
| 1200 | int flags; |
| 1201 | |
| 1202 | flags = fcntl(fd, F_GETFL); |
| 1203 | if (flags == -1) |
| 1204 | return -errno; |
| 1205 | |
| 1206 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1207 | return -errno; |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1212 | #if defined(__linux__) |
| 1213 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1214 | #define RTC_FREQ 1024 |
| 1215 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1216 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1217 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1218 | struct sigaction act; |
| 1219 | |
| 1220 | /* timer signal */ |
| 1221 | sigfillset(&act.sa_mask); |
| 1222 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1223 | act.sa_handler = host_alarm_handler; |
| 1224 | |
| 1225 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1226 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1227 | fcntl(fd, F_SETOWN, getpid()); |
| 1228 | } |
| 1229 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1230 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1231 | { |
| 1232 | struct hpet_info info; |
| 1233 | int r, fd; |
| 1234 | |
| 1235 | fd = open("/dev/hpet", O_RDONLY); |
| 1236 | if (fd < 0) |
| 1237 | return -1; |
| 1238 | |
| 1239 | /* Set frequency */ |
| 1240 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1241 | if (r < 0) { |
| 1242 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1243 | "error, but for better emulation accuracy type:\n" |
| 1244 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1245 | goto fail; |
| 1246 | } |
| 1247 | |
| 1248 | /* Check capabilities */ |
| 1249 | r = ioctl(fd, HPET_INFO, &info); |
| 1250 | if (r < 0) |
| 1251 | goto fail; |
| 1252 | |
| 1253 | /* Enable periodic mode */ |
| 1254 | r = ioctl(fd, HPET_EPI, 0); |
| 1255 | if (info.hi_flags && (r < 0)) |
| 1256 | goto fail; |
| 1257 | |
| 1258 | /* Enable interrupt */ |
| 1259 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1260 | if (r < 0) |
| 1261 | goto fail; |
| 1262 | |
| 1263 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1264 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1265 | |
| 1266 | return 0; |
| 1267 | fail: |
| 1268 | close(fd); |
| 1269 | return -1; |
| 1270 | } |
| 1271 | |
| 1272 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1273 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1274 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1275 | |
| 1276 | close(fd); |
| 1277 | } |
| 1278 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1279 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1280 | { |
| 1281 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1282 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1283 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 1284 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1285 | if (rtc_fd < 0) |
| 1286 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1287 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1288 | if (current_rtc_freq != RTC_FREQ && |
| 1289 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1290 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1291 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1292 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1293 | goto fail; |
| 1294 | } |
| 1295 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1296 | fail: |
| 1297 | close(rtc_fd); |
| 1298 | return -1; |
| 1299 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1300 | |
| 1301 | enable_sigio_timer(rtc_fd); |
| 1302 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1303 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1304 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |
| 1307 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1308 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1309 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1310 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1311 | |
| 1312 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1315 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1316 | { |
| 1317 | struct sigevent ev; |
| 1318 | timer_t host_timer; |
| 1319 | struct sigaction act; |
| 1320 | |
| 1321 | sigfillset(&act.sa_mask); |
| 1322 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1323 | act.sa_handler = host_alarm_handler; |
| 1324 | |
| 1325 | sigaction(SIGALRM, &act, NULL); |
| 1326 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1327 | /* |
| 1328 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1329 | * about uninitialized data in timer_create call |
| 1330 | */ |
| 1331 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1332 | ev.sigev_value.sival_int = 0; |
| 1333 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1334 | ev.sigev_signo = SIGALRM; |
| 1335 | |
| 1336 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1337 | perror("timer_create"); |
| 1338 | |
| 1339 | /* disable dynticks */ |
| 1340 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1341 | |
| 1342 | return -1; |
| 1343 | } |
| 1344 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1345 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1351 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1352 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1353 | |
| 1354 | timer_delete(host_timer); |
| 1355 | } |
| 1356 | |
| 1357 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1358 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1359 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1360 | struct itimerspec timeout; |
| 1361 | int64_t nearest_delta_us = INT64_MAX; |
| 1362 | int64_t current_us; |
| 1363 | |
| 1364 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1365 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1366 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1367 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1368 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1369 | |
| 1370 | /* check whether a timer is already running */ |
| 1371 | if (timer_gettime(host_timer, &timeout)) { |
| 1372 | perror("gettime"); |
| 1373 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1374 | exit(1); |
| 1375 | } |
| 1376 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1377 | if (current_us && current_us <= nearest_delta_us) |
| 1378 | return; |
| 1379 | |
| 1380 | timeout.it_interval.tv_sec = 0; |
| 1381 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1382 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1383 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1384 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1385 | perror("settime"); |
| 1386 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1387 | exit(1); |
| 1388 | } |
| 1389 | } |
| 1390 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1391 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1392 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1393 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1394 | { |
| 1395 | struct sigaction act; |
| 1396 | struct itimerval itv; |
| 1397 | int err; |
| 1398 | |
| 1399 | /* timer signal */ |
| 1400 | sigfillset(&act.sa_mask); |
| 1401 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1402 | act.sa_handler = host_alarm_handler; |
| 1403 | |
| 1404 | sigaction(SIGALRM, &act, NULL); |
| 1405 | |
| 1406 | itv.it_interval.tv_sec = 0; |
| 1407 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1408 | itv.it_interval.tv_usec = 999; |
| 1409 | itv.it_value.tv_sec = 0; |
| 1410 | itv.it_value.tv_usec = 10 * 1000; |
| 1411 | |
| 1412 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1413 | if (err) |
| 1414 | return -1; |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1420 | { |
| 1421 | struct itimerval itv; |
| 1422 | |
| 1423 | memset(&itv, 0, sizeof(itv)); |
| 1424 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1425 | } |
| 1426 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1427 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1428 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1429 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1430 | #ifdef _WIN32 |
| 1431 | |
| 1432 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1433 | { |
| 1434 | TIMECAPS tc; |
| 1435 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1436 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1437 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1438 | memset(&tc, 0, sizeof(tc)); |
| 1439 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1440 | |
| 1441 | if (data->period < tc.wPeriodMin) |
| 1442 | data->period = tc.wPeriodMin; |
| 1443 | |
| 1444 | timeBeginPeriod(data->period); |
| 1445 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1446 | flags = TIME_CALLBACK_FUNCTION; |
| 1447 | if (alarm_has_dynticks(t)) |
| 1448 | flags |= TIME_ONESHOT; |
| 1449 | else |
| 1450 | flags |= TIME_PERIODIC; |
| 1451 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1452 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1453 | data->period, // resolution |
| 1454 | host_alarm_handler, // function |
| 1455 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1456 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (!data->timerId) { |
| 1459 | perror("Failed to initialize win32 alarm timer"); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1460 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1461 | return -1; |
| 1462 | } |
| 1463 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1468 | { |
| 1469 | struct qemu_alarm_win32 *data = t->priv; |
| 1470 | |
| 1471 | timeKillEvent(data->timerId); |
| 1472 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1475 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1476 | { |
| 1477 | struct qemu_alarm_win32 *data = t->priv; |
| 1478 | uint64_t nearest_delta_us; |
| 1479 | |
| 1480 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1481 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1482 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1483 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1484 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1485 | nearest_delta_us /= 1000; |
| 1486 | |
| 1487 | timeKillEvent(data->timerId); |
| 1488 | |
| 1489 | data->timerId = timeSetEvent(1, |
| 1490 | data->period, |
| 1491 | host_alarm_handler, |
| 1492 | (DWORD)t, |
| 1493 | TIME_ONESHOT | TIME_PERIODIC); |
| 1494 | |
| 1495 | if (!data->timerId) { |
| 1496 | perror("Failed to re-arm win32 alarm timer"); |
| 1497 | |
| 1498 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1499 | exit(1); |
| 1500 | } |
| 1501 | } |
| 1502 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1503 | #endif /* _WIN32 */ |
| 1504 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1505 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1506 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1507 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1508 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1509 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1510 | for (i = 0; alarm_timers[i].name; i++) { |
| 1511 | t = &alarm_timers[i]; |
| 1512 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1513 | err = t->start(t); |
| 1514 | if (!err) |
| 1515 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1516 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1517 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1518 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1519 | err = -ENOENT; |
| 1520 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1521 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1522 | |
| 1523 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1524 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1525 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1526 | |
| 1527 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1528 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1531 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1532 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1533 | alarm_timer->stop(alarm_timer); |
| 1534 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1537 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1538 | /* host time/date access */ |
| 1539 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1540 | { |
| 1541 | time_t ti; |
| 1542 | struct tm *ret; |
| 1543 | |
| 1544 | time(&ti); |
| 1545 | ti += offset; |
| 1546 | if (rtc_date_offset == -1) { |
| 1547 | if (rtc_utc) |
| 1548 | ret = gmtime(&ti); |
| 1549 | else |
| 1550 | ret = localtime(&ti); |
| 1551 | } else { |
| 1552 | ti -= rtc_date_offset; |
| 1553 | ret = gmtime(&ti); |
| 1554 | } |
| 1555 | |
| 1556 | memcpy(tm, ret, sizeof(struct tm)); |
| 1557 | } |
| 1558 | |
| 1559 | int qemu_timedate_diff(struct tm *tm) |
| 1560 | { |
| 1561 | time_t seconds; |
| 1562 | |
| 1563 | if (rtc_date_offset == -1) |
| 1564 | if (rtc_utc) |
| 1565 | seconds = mktimegm(tm); |
| 1566 | else |
| 1567 | seconds = mktime(tm); |
| 1568 | else |
| 1569 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1570 | |
| 1571 | return seconds - time(NULL); |
| 1572 | } |
| 1573 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1574 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1575 | static void socket_cleanup(void) |
| 1576 | { |
| 1577 | WSACleanup(); |
| 1578 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1579 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1580 | static int socket_init(void) |
| 1581 | { |
| 1582 | WSADATA Data; |
| 1583 | int ret, err; |
| 1584 | |
| 1585 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1586 | if (ret != 0) { |
| 1587 | err = WSAGetLastError(); |
| 1588 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | atexit(socket_cleanup); |
| 1592 | return 0; |
| 1593 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1594 | #endif |
| 1595 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1596 | /***********************************************************/ |
| 1597 | /* Bluetooth support */ |
| 1598 | static int nb_hcis; |
| 1599 | static int cur_hci; |
| 1600 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1601 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1602 | static struct bt_vlan_s { |
| 1603 | struct bt_scatternet_s net; |
| 1604 | int id; |
| 1605 | struct bt_vlan_s *next; |
| 1606 | } *first_bt_vlan; |
| 1607 | |
| 1608 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1609 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1610 | { |
| 1611 | struct bt_vlan_s **pvlan, *vlan; |
| 1612 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1613 | if (vlan->id == id) |
| 1614 | return &vlan->net; |
| 1615 | } |
| 1616 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1617 | vlan->id = id; |
| 1618 | pvlan = &first_bt_vlan; |
| 1619 | while (*pvlan != NULL) |
| 1620 | pvlan = &(*pvlan)->next; |
| 1621 | *pvlan = vlan; |
| 1622 | return &vlan->net; |
| 1623 | } |
| 1624 | |
| 1625 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1626 | { |
| 1627 | } |
| 1628 | |
| 1629 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1630 | { |
| 1631 | return -ENOTSUP; |
| 1632 | } |
| 1633 | |
| 1634 | static struct HCIInfo null_hci = { |
| 1635 | .cmd_send = null_hci_send, |
| 1636 | .sco_send = null_hci_send, |
| 1637 | .acl_send = null_hci_send, |
| 1638 | .bdaddr_set = null_hci_addr_set, |
| 1639 | }; |
| 1640 | |
| 1641 | struct HCIInfo *qemu_next_hci(void) |
| 1642 | { |
| 1643 | if (cur_hci == nb_hcis) |
| 1644 | return &null_hci; |
| 1645 | |
| 1646 | return hci_table[cur_hci++]; |
| 1647 | } |
| 1648 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1649 | static struct HCIInfo *hci_init(const char *str) |
| 1650 | { |
| 1651 | char *endp; |
| 1652 | struct bt_scatternet_s *vlan = 0; |
| 1653 | |
| 1654 | if (!strcmp(str, "null")) |
| 1655 | /* null */ |
| 1656 | return &null_hci; |
| 1657 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1658 | /* host[:hciN] */ |
| 1659 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1660 | else if (!strncmp(str, "hci", 3)) { |
| 1661 | /* hci[,vlan=n] */ |
| 1662 | if (str[3]) { |
| 1663 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1664 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1665 | if (*endp) |
| 1666 | vlan = 0; |
| 1667 | } |
| 1668 | } else |
| 1669 | vlan = qemu_find_bt_vlan(0); |
| 1670 | if (vlan) |
| 1671 | return bt_new_hci(vlan); |
| 1672 | } |
| 1673 | |
| 1674 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1675 | |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | static int bt_hci_parse(const char *str) |
| 1680 | { |
| 1681 | struct HCIInfo *hci; |
| 1682 | bdaddr_t bdaddr; |
| 1683 | |
| 1684 | if (nb_hcis >= MAX_NICS) { |
| 1685 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1686 | return -1; |
| 1687 | } |
| 1688 | |
| 1689 | hci = hci_init(str); |
| 1690 | if (!hci) |
| 1691 | return -1; |
| 1692 | |
| 1693 | bdaddr.b[0] = 0x52; |
| 1694 | bdaddr.b[1] = 0x54; |
| 1695 | bdaddr.b[2] = 0x00; |
| 1696 | bdaddr.b[3] = 0x12; |
| 1697 | bdaddr.b[4] = 0x34; |
| 1698 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1699 | hci->bdaddr_set(hci, bdaddr.b); |
| 1700 | |
| 1701 | hci_table[nb_hcis++] = hci; |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static void bt_vhci_add(int vlan_id) |
| 1707 | { |
| 1708 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1709 | |
| 1710 | if (!vlan->slave) |
| 1711 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1712 | "an empty scatternet %i\n", vlan_id); |
| 1713 | |
| 1714 | bt_vhci_init(bt_new_hci(vlan)); |
| 1715 | } |
| 1716 | |
| 1717 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1718 | { |
| 1719 | struct bt_scatternet_s *vlan; |
| 1720 | int vlan_id = 0; |
| 1721 | char *endp = strstr(opt, ",vlan="); |
| 1722 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1723 | char devname[10]; |
| 1724 | |
| 1725 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1726 | |
| 1727 | if (endp) { |
| 1728 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1729 | if (*endp) { |
| 1730 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1731 | return 0; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1736 | |
| 1737 | if (!vlan->slave) |
| 1738 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1739 | "an empty scatternet %i\n", vlan_id); |
| 1740 | |
| 1741 | if (!strcmp(devname, "keyboard")) |
| 1742 | return bt_keyboard_init(vlan); |
| 1743 | |
| 1744 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | static int bt_parse(const char *opt) |
| 1749 | { |
| 1750 | const char *endp, *p; |
| 1751 | int vlan; |
| 1752 | |
| 1753 | if (strstart(opt, "hci", &endp)) { |
| 1754 | if (!*endp || *endp == ',') { |
| 1755 | if (*endp) |
| 1756 | if (!strstart(endp, ",vlan=", 0)) |
| 1757 | opt = endp + 1; |
| 1758 | |
| 1759 | return bt_hci_parse(opt); |
| 1760 | } |
| 1761 | } else if (strstart(opt, "vhci", &endp)) { |
| 1762 | if (!*endp || *endp == ',') { |
| 1763 | if (*endp) { |
| 1764 | if (strstart(endp, ",vlan=", &p)) { |
| 1765 | vlan = strtol(p, (char **) &endp, 0); |
| 1766 | if (*endp) { |
| 1767 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | } else { |
| 1771 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1772 | return 1; |
| 1773 | } |
| 1774 | } else |
| 1775 | vlan = 0; |
| 1776 | |
| 1777 | bt_vhci_add(vlan); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | } else if (strstart(opt, "device:", &endp)) |
| 1781 | return !bt_device_add(endp); |
| 1782 | |
| 1783 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1784 | return 1; |
| 1785 | } |
| 1786 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1787 | /***********************************************************/ |
| 1788 | /* QEMU Block devices */ |
| 1789 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1790 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1791 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1792 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1793 | #define PFLASH_ALIAS "if=pflash" |
| 1794 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1795 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1796 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1797 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1798 | { |
| 1799 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1800 | char optstr[1024]; |
| 1801 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1802 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1803 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1804 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1805 | va_end(ap); |
| 1806 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1807 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1808 | if (!opts) { |
| 1809 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1810 | __FUNCTION__, optstr); |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | if (file) |
| 1814 | qemu_opt_set(opts, "file", file); |
| 1815 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1818 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1819 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1820 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1821 | |
| 1822 | /* seek interface, bus and unit */ |
| 1823 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1824 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1825 | if (dinfo->type == type && |
| 1826 | dinfo->bus == bus && |
| 1827 | dinfo->unit == unit) |
| 1828 | return dinfo; |
| 1829 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1830 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1831 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 1834 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1835 | { |
| 1836 | DriveInfo *dinfo; |
| 1837 | |
| 1838 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1839 | if (strcmp(id, dinfo->id)) |
| 1840 | continue; |
| 1841 | return dinfo; |
| 1842 | } |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1846 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1847 | { |
| 1848 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1849 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1850 | |
| 1851 | max_bus = -1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1852 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1853 | if(dinfo->type == type && |
| 1854 | dinfo->bus > max_bus) |
| 1855 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1856 | } |
| 1857 | return max_bus; |
| 1858 | } |
| 1859 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1860 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 1861 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1862 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1863 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1864 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1865 | if (dinfo->bdrv == bdrv) |
| 1866 | return dinfo->serial; |
| 1867 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1868 | |
| 1869 | return "\0"; |
| 1870 | } |
| 1871 | |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1872 | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
| 1873 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1874 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1875 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1876 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1877 | if (dinfo->bdrv == bdrv) |
| 1878 | return dinfo->onerror; |
| 1879 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1880 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 1881 | return BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 1884 | static void bdrv_format_print(void *opaque, const char *name) |
| 1885 | { |
| 1886 | fprintf(stderr, " %s", name); |
| 1887 | } |
| 1888 | |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1889 | void drive_uninit(BlockDriverState *bdrv) |
| 1890 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1891 | DriveInfo *dinfo; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1892 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1893 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1894 | if (dinfo->bdrv != bdrv) |
| 1895 | continue; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1896 | qemu_opts_del(dinfo->opts); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1897 | TAILQ_REMOVE(&drives, dinfo, next); |
| 1898 | qemu_free(dinfo); |
| 1899 | break; |
| 1900 | } |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1903 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1904 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1905 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1906 | const char *buf; |
| 1907 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1908 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1909 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1910 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1911 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1912 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 1913 | int bus_id, unit_id; |
| 1914 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 1915 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 1916 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1917 | int max_devs; |
| 1918 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 1919 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1920 | int aio = 0; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1921 | int bdrv_flags, onerror; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 1922 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1923 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1924 | int snapshot = 0; |
| 1925 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1926 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1927 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1928 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 1929 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1930 | |
Gerd Hoffmann | 4d00781 | 2009-08-31 14:23:57 +0200 | [diff] [blame] | 1931 | if (machine && machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1932 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1933 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1934 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1935 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1936 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1937 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1938 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1939 | } |
| 1940 | media = MEDIA_DISK; |
| 1941 | |
| 1942 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1943 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 1944 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 1945 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1946 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1947 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 1948 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 1949 | secs = qemu_opt_get_number(opts, "secs", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1950 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1951 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1952 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1953 | file = qemu_opt_get(opts, "file"); |
| 1954 | serial = qemu_opt_get(opts, "serial"); |
| 1955 | |
| 1956 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 1957 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1958 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1959 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1960 | max_devs = MAX_IDE_DEVS; |
| 1961 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1962 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1963 | max_devs = MAX_SCSI_DEVS; |
| 1964 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1965 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1966 | max_devs = 0; |
| 1967 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1968 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1969 | max_devs = 0; |
| 1970 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1971 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1972 | max_devs = 0; |
| 1973 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1974 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1975 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1976 | } else if (!strcmp(buf, "virtio")) { |
| 1977 | type = IF_VIRTIO; |
| 1978 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1979 | } else if (!strcmp(buf, "xen")) { |
| 1980 | type = IF_XEN; |
| 1981 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 1982 | } else if (!strcmp(buf, "none")) { |
| 1983 | type = IF_NONE; |
| 1984 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1985 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1986 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1987 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1991 | if (cyls || heads || secs) { |
| 1992 | if (cyls < 1 || cyls > 16383) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1993 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1994 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1995 | } |
| 1996 | if (heads < 1 || heads > 16) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1997 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1998 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1999 | } |
| 2000 | if (secs < 1 || secs > 63) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2001 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2002 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2006 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2007 | if (!cyls) { |
| 2008 | fprintf(stderr, |
| 2009 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2010 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2011 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2012 | } |
| 2013 | if (!strcmp(buf, "none")) |
| 2014 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2015 | else if (!strcmp(buf, "lba")) |
| 2016 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2017 | else if (!strcmp(buf, "auto")) |
| 2018 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2019 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2020 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2021 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2025 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2026 | if (!strcmp(buf, "disk")) { |
| 2027 | media = MEDIA_DISK; |
| 2028 | } else if (!strcmp(buf, "cdrom")) { |
| 2029 | if (cyls || secs || heads) { |
| 2030 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2031 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2032 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2033 | } |
| 2034 | media = MEDIA_CDROM; |
| 2035 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2036 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2037 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2041 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2042 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2043 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2044 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2045 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2046 | else if (!strcmp(buf, "writeback")) |
| 2047 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2048 | else { |
| 2049 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2050 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2054 | #ifdef CONFIG_LINUX_AIO |
| 2055 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2056 | if (!strcmp(buf, "threads")) |
| 2057 | aio = 0; |
| 2058 | else if (!strcmp(buf, "native")) |
| 2059 | aio = 1; |
| 2060 | else { |
| 2061 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2062 | return NULL; |
| 2063 | } |
| 2064 | } |
| 2065 | #endif |
| 2066 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2067 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2068 | if (strcmp(buf, "?") == 0) { |
| 2069 | fprintf(stderr, "qemu: Supported formats:"); |
| 2070 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2071 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2072 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2073 | } |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2074 | drv = bdrv_find_format(buf); |
| 2075 | if (!drv) { |
| 2076 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2077 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 2081 | onerror = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2082 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2083 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2084 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2085 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2086 | } |
| 2087 | if (!strcmp(buf, "ignore")) |
| 2088 | onerror = BLOCK_ERR_IGNORE; |
| 2089 | else if (!strcmp(buf, "enospc")) |
| 2090 | onerror = BLOCK_ERR_STOP_ENOSPC; |
| 2091 | else if (!strcmp(buf, "stop")) |
| 2092 | onerror = BLOCK_ERR_STOP_ANY; |
| 2093 | else if (!strcmp(buf, "report")) |
| 2094 | onerror = BLOCK_ERR_REPORT; |
| 2095 | else { |
| 2096 | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2097 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2101 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2102 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2103 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2104 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2105 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2108 | /* compute bus and unit according index */ |
| 2109 | |
| 2110 | if (index != -1) { |
| 2111 | if (bus_id != 0 || unit_id != -1) { |
| 2112 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2113 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2114 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2115 | } |
| 2116 | if (max_devs == 0) |
| 2117 | { |
| 2118 | unit_id = index; |
| 2119 | bus_id = 0; |
| 2120 | } else { |
| 2121 | unit_id = index % max_devs; |
| 2122 | bus_id = index / max_devs; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* if user doesn't specify a unit_id, |
| 2127 | * try to find the first free |
| 2128 | */ |
| 2129 | |
| 2130 | if (unit_id == -1) { |
| 2131 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2132 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2133 | unit_id++; |
| 2134 | if (max_devs && unit_id >= max_devs) { |
| 2135 | unit_id -= max_devs; |
| 2136 | bus_id++; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | /* check unit id */ |
| 2142 | |
| 2143 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2144 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2145 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2146 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * ignore multiple definitions |
| 2151 | */ |
| 2152 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2153 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2154 | *fatal_error = 0; |
| 2155 | return NULL; |
| 2156 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2157 | |
| 2158 | /* init */ |
| 2159 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2160 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2161 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2162 | dinfo->id = qemu_strdup(buf); |
| 2163 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2164 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2165 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2166 | if (type == IF_IDE || type == IF_SCSI) |
| 2167 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2168 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2169 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2170 | devname, bus_id, mediastr, unit_id); |
| 2171 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2172 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2173 | devname, mediastr, unit_id); |
| 2174 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2175 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2176 | dinfo->devaddr = devaddr; |
| 2177 | dinfo->type = type; |
| 2178 | dinfo->bus = bus_id; |
| 2179 | dinfo->unit = unit_id; |
| 2180 | dinfo->onerror = onerror; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2181 | dinfo->opts = opts; |
| 2182 | if (serial) |
| 2183 | strncpy(dinfo->serial, serial, sizeof(serial)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2184 | TAILQ_INSERT_TAIL(&drives, dinfo, next); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2185 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2186 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2187 | case IF_IDE: |
| 2188 | case IF_SCSI: |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2189 | case IF_XEN: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2190 | switch(media) { |
| 2191 | case MEDIA_DISK: |
| 2192 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2193 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2194 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2195 | } |
| 2196 | break; |
| 2197 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2198 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2199 | break; |
| 2200 | } |
| 2201 | break; |
| 2202 | case IF_SD: |
| 2203 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2204 | approximation. */ |
| 2205 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2206 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2207 | break; |
| 2208 | case IF_PFLASH: |
| 2209 | case IF_MTD: |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 2210 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2211 | break; |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 2212 | case IF_VIRTIO: |
| 2213 | /* add virtio block device */ |
| 2214 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 2215 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 2216 | qemu_opt_set(opts, "drive", dinfo->id); |
| 2217 | if (devaddr) |
| 2218 | qemu_opt_set(opts, "addr", devaddr); |
| 2219 | break; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 2220 | case IF_COUNT: |
| 2221 | abort(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2222 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2223 | if (!file) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2224 | *fatal_error = 0; |
| 2225 | return NULL; |
| 2226 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2227 | bdrv_flags = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2228 | if (snapshot) { |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2229 | bdrv_flags |= BDRV_O_SNAPSHOT; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2230 | cache = 2; /* always use write-back with snapshot */ |
| 2231 | } |
| 2232 | if (cache == 0) /* no caching */ |
| 2233 | bdrv_flags |= BDRV_O_NOCACHE; |
| 2234 | else if (cache == 2) /* write-back */ |
| 2235 | bdrv_flags |= BDRV_O_CACHE_WB; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2236 | |
| 2237 | if (aio == 1) { |
| 2238 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 2239 | } else { |
| 2240 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
| 2241 | } |
| 2242 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2243 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2244 | fprintf(stderr, "qemu: could not open disk image %s\n", |
| 2245 | file); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2246 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2247 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2248 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2249 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2250 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2251 | *fatal_error = 0; |
| 2252 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2255 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2256 | { |
| 2257 | QEMUMachine *machine = opaque; |
| 2258 | int fatal_error = 0; |
| 2259 | |
| 2260 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2261 | if (fatal_error) |
| 2262 | return 1; |
| 2263 | } |
| 2264 | return 0; |
| 2265 | } |
| 2266 | |
| 2267 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2268 | { |
| 2269 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2270 | qemu_opt_set(opts, "snapshot", "on"); |
| 2271 | } |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2275 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2276 | { |
| 2277 | boot_set_handler = func; |
| 2278 | boot_set_opaque = opaque; |
| 2279 | } |
| 2280 | |
| 2281 | int qemu_boot_set(const char *boot_devices) |
| 2282 | { |
| 2283 | if (!boot_set_handler) { |
| 2284 | return -EINVAL; |
| 2285 | } |
| 2286 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2287 | } |
| 2288 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2289 | static int parse_bootdevices(char *devices) |
| 2290 | { |
| 2291 | /* We just do some generic consistency checks */ |
| 2292 | const char *p; |
| 2293 | int bitmap = 0; |
| 2294 | |
| 2295 | for (p = devices; *p != '\0'; p++) { |
| 2296 | /* Allowed boot devices are: |
| 2297 | * a-b: floppy disk drives |
| 2298 | * c-f: IDE disk drives |
| 2299 | * g-m: machine implementation dependant drives |
| 2300 | * n-p: network devices |
| 2301 | * It's up to each machine implementation to check if the given boot |
| 2302 | * devices match the actual hardware implementation and firmware |
| 2303 | * features. |
| 2304 | */ |
| 2305 | if (*p < 'a' || *p > 'p') { |
| 2306 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2307 | exit(1); |
| 2308 | } |
| 2309 | if (bitmap & (1 << (*p - 'a'))) { |
| 2310 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2311 | exit(1); |
| 2312 | } |
| 2313 | bitmap |= 1 << (*p - 'a'); |
| 2314 | } |
| 2315 | return bitmap; |
| 2316 | } |
| 2317 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2318 | static void restore_boot_devices(void *opaque) |
| 2319 | { |
| 2320 | char *standard_boot_devices = opaque; |
| 2321 | |
| 2322 | qemu_boot_set(standard_boot_devices); |
| 2323 | |
| 2324 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2325 | qemu_free(standard_boot_devices); |
| 2326 | } |
| 2327 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2328 | static void numa_add(const char *optarg) |
| 2329 | { |
| 2330 | char option[128]; |
| 2331 | char *endptr; |
| 2332 | unsigned long long value, endvalue; |
| 2333 | int nodenr; |
| 2334 | |
| 2335 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2336 | if (!strcmp(option, "node")) { |
| 2337 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2338 | nodenr = nb_numa_nodes; |
| 2339 | } else { |
| 2340 | nodenr = strtoull(option, NULL, 10); |
| 2341 | } |
| 2342 | |
| 2343 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2344 | node_mem[nodenr] = 0; |
| 2345 | } else { |
| 2346 | value = strtoull(option, &endptr, 0); |
| 2347 | switch (*endptr) { |
| 2348 | case 0: case 'M': case 'm': |
| 2349 | value <<= 20; |
| 2350 | break; |
| 2351 | case 'G': case 'g': |
| 2352 | value <<= 30; |
| 2353 | break; |
| 2354 | } |
| 2355 | node_mem[nodenr] = value; |
| 2356 | } |
| 2357 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2358 | node_cpumask[nodenr] = 0; |
| 2359 | } else { |
| 2360 | value = strtoull(option, &endptr, 10); |
| 2361 | if (value >= 64) { |
| 2362 | value = 63; |
| 2363 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2364 | } else { |
| 2365 | if (*endptr == '-') { |
| 2366 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2367 | if (endvalue >= 63) { |
| 2368 | endvalue = 62; |
| 2369 | fprintf(stderr, |
| 2370 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2371 | } |
| 2372 | value = (1 << (endvalue + 1)) - (1 << value); |
| 2373 | } else { |
| 2374 | value = 1 << value; |
| 2375 | } |
| 2376 | } |
| 2377 | node_cpumask[nodenr] = value; |
| 2378 | } |
| 2379 | nb_numa_nodes++; |
| 2380 | } |
| 2381 | return; |
| 2382 | } |
| 2383 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2384 | static void smp_parse(const char *optarg) |
| 2385 | { |
| 2386 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2387 | char *endptr; |
| 2388 | char option[128]; |
| 2389 | |
| 2390 | smp = strtoul(optarg, &endptr, 10); |
| 2391 | if (endptr != optarg) { |
| 2392 | if (*endptr == ',') { |
| 2393 | endptr++; |
| 2394 | } |
| 2395 | } |
| 2396 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2397 | sockets = strtoull(option, NULL, 10); |
| 2398 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2399 | cores = strtoull(option, NULL, 10); |
| 2400 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2401 | threads = strtoull(option, NULL, 10); |
| 2402 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2403 | max_cpus = strtoull(option, NULL, 10); |
| 2404 | |
| 2405 | /* compute missing values, prefer sockets over cores over threads */ |
| 2406 | if (smp == 0 || sockets == 0) { |
| 2407 | sockets = sockets > 0 ? sockets : 1; |
| 2408 | cores = cores > 0 ? cores : 1; |
| 2409 | threads = threads > 0 ? threads : 1; |
| 2410 | if (smp == 0) { |
| 2411 | smp = cores * threads * sockets; |
| 2412 | } else { |
| 2413 | sockets = smp / (cores * threads); |
| 2414 | } |
| 2415 | } else { |
| 2416 | if (cores == 0) { |
| 2417 | threads = threads > 0 ? threads : 1; |
| 2418 | cores = smp / (sockets * threads); |
| 2419 | } else { |
| 2420 | if (sockets == 0) { |
| 2421 | sockets = smp / (cores * threads); |
| 2422 | } else { |
| 2423 | threads = smp / (cores * sockets); |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | smp_cpus = smp; |
| 2428 | smp_cores = cores > 0 ? cores : 1; |
| 2429 | smp_threads = threads > 0 ? threads : 1; |
| 2430 | if (max_cpus == 0) |
| 2431 | max_cpus = smp_cpus; |
| 2432 | } |
| 2433 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2434 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2435 | /* USB devices */ |
| 2436 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2437 | static void usb_msd_password_cb(void *opaque, int err) |
| 2438 | { |
| 2439 | USBDevice *dev = opaque; |
| 2440 | |
| 2441 | if (!err) |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2442 | usb_device_attach(dev); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2443 | else |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 2444 | dev->info->handle_destroy(dev); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2447 | static struct { |
| 2448 | const char *name; |
| 2449 | const char *qdev; |
| 2450 | } usbdevs[] = { |
| 2451 | { |
| 2452 | .name = "mouse", |
| 2453 | .qdev = "QEMU USB Mouse", |
| 2454 | },{ |
| 2455 | .name = "tablet", |
| 2456 | .qdev = "QEMU USB Tablet", |
| 2457 | },{ |
| 2458 | .name = "keyboard", |
| 2459 | .qdev = "QEMU USB Keyboard", |
| 2460 | },{ |
| 2461 | .name = "wacom-tablet", |
| 2462 | .qdev = "QEMU PenPartner Tablet", |
| 2463 | } |
| 2464 | }; |
| 2465 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2466 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2467 | { |
| 2468 | const char *p; |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2469 | USBBus *bus = usb_bus_find(-1 /* any */); |
| 2470 | USBDevice *dev = NULL; |
| 2471 | int i; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2472 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2473 | if (!usb_enabled) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2474 | return -1; |
| 2475 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2476 | /* simple devices which don't need extra care */ |
| 2477 | for (i = 0; i < ARRAY_SIZE(usbdevs); i++) { |
| 2478 | if (strcmp(devname, usbdevs[i].name) != 0) |
| 2479 | continue; |
| 2480 | dev = usb_create_simple(bus, usbdevs[i].qdev); |
| 2481 | goto done; |
| 2482 | } |
| 2483 | |
| 2484 | /* the other ones */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2485 | if (strstart(devname, "host:", &p)) { |
| 2486 | dev = usb_host_device_open(p); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2487 | } else if (strstart(devname, "disk:", &p)) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2488 | BlockDriverState *bs; |
| 2489 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2490 | dev = usb_msd_init(p); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2491 | if (!dev) |
| 2492 | return -1; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2493 | bs = usb_msd_get_bdrv(dev); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2494 | if (bdrv_key_required(bs)) { |
| 2495 | autostart = 0; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2496 | if (is_hotplug) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2497 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
| 2498 | dev); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2499 | return 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2500 | } |
| 2501 | } |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2502 | } else if (strstart(devname, "serial:", &p)) { |
| 2503 | dev = usb_serial_init(p); |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 2504 | #ifdef CONFIG_BRLAPI |
| 2505 | } else if (!strcmp(devname, "braille")) { |
| 2506 | dev = usb_baum_init(); |
| 2507 | #endif |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2508 | } else if (strstart(devname, "net:", &p)) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2509 | int nic = nb_nics; |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2510 | |
Jan Kiszka | 10ae5a7 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 2511 | if (net_client_init(NULL, "nic", p) < 0) |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2512 | return -1; |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2513 | nd_table[nic].model = "usb"; |
| 2514 | dev = usb_net_init(&nd_table[nic]); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2515 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2516 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2517 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2518 | } else { |
| 2519 | return -1; |
| 2520 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2521 | if (!dev) |
| 2522 | return -1; |
| 2523 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2524 | done: |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2525 | return 0; |
| 2526 | } |
| 2527 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2528 | static int usb_device_del(const char *devname) |
| 2529 | { |
| 2530 | int bus_num, addr; |
| 2531 | const char *p; |
| 2532 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2533 | if (strstart(devname, "host:", &p)) |
| 2534 | return usb_host_device_close(p); |
| 2535 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2536 | if (!usb_enabled) |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2537 | return -1; |
| 2538 | |
| 2539 | p = strchr(devname, '.'); |
| 2540 | if (!p) |
| 2541 | return -1; |
| 2542 | bus_num = strtoul(devname, NULL, 0); |
| 2543 | addr = strtoul(p + 1, NULL, 0); |
| 2544 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2545 | return usb_device_delete_addr(bus_num, addr); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2548 | static int usb_parse(const char *cmdline) |
| 2549 | { |
| 2550 | return usb_device_add(cmdline, 0); |
| 2551 | } |
| 2552 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2553 | void do_usb_add(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2554 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2555 | usb_device_add(qdict_get_str(qdict, "devname"), 1); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2558 | void do_usb_del(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2559 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2560 | usb_device_del(qdict_get_str(qdict, "devname")); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2563 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2564 | /* PCMCIA/Cardbus */ |
| 2565 | |
| 2566 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2567 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2568 | struct pcmcia_socket_entry_s *next; |
| 2569 | } *pcmcia_sockets = 0; |
| 2570 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2571 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2572 | { |
| 2573 | struct pcmcia_socket_entry_s *entry; |
| 2574 | |
| 2575 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2576 | entry->socket = socket; |
| 2577 | entry->next = pcmcia_sockets; |
| 2578 | pcmcia_sockets = entry; |
| 2579 | } |
| 2580 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2581 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2582 | { |
| 2583 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2584 | |
| 2585 | ptr = &pcmcia_sockets; |
| 2586 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2587 | if (entry->socket == socket) { |
| 2588 | *ptr = entry->next; |
| 2589 | qemu_free(entry); |
| 2590 | } |
| 2591 | } |
| 2592 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2593 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2594 | { |
| 2595 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2596 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2597 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2598 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2599 | |
| 2600 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2601 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2602 | iter->socket->attached ? iter->socket->card_string : |
| 2603 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | /***********************************************************/ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2607 | /* register display */ |
| 2608 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2609 | struct DisplayAllocator default_allocator = { |
| 2610 | defaultallocator_create_displaysurface, |
| 2611 | defaultallocator_resize_displaysurface, |
| 2612 | defaultallocator_free_displaysurface |
| 2613 | }; |
| 2614 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2615 | void register_displaystate(DisplayState *ds) |
| 2616 | { |
| 2617 | DisplayState **s; |
| 2618 | s = &display_state; |
| 2619 | while (*s != NULL) |
| 2620 | s = &(*s)->next; |
| 2621 | ds->next = NULL; |
| 2622 | *s = ds; |
| 2623 | } |
| 2624 | |
| 2625 | DisplayState *get_displaystate(void) |
| 2626 | { |
| 2627 | return display_state; |
| 2628 | } |
| 2629 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2630 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2631 | { |
| 2632 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2633 | return ds->allocator; |
| 2634 | } |
| 2635 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2636 | /* dumb display */ |
| 2637 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2638 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2639 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2640 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2641 | ds->allocator = &default_allocator; |
| 2642 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2643 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2647 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2648 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2649 | typedef struct IOHandlerRecord { |
| 2650 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2651 | IOCanRWHandler *fd_read_poll; |
| 2652 | IOHandler *fd_read; |
| 2653 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2654 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2655 | void *opaque; |
| 2656 | /* temporary data */ |
| 2657 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2658 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2659 | } IOHandlerRecord; |
| 2660 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2661 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2662 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2663 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2664 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2665 | int qemu_set_fd_handler2(int fd, |
| 2666 | IOCanRWHandler *fd_read_poll, |
| 2667 | IOHandler *fd_read, |
| 2668 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2669 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2670 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2671 | IOHandlerRecord **pioh, *ioh; |
| 2672 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2673 | if (!fd_read && !fd_write) { |
| 2674 | pioh = &first_io_handler; |
| 2675 | for(;;) { |
| 2676 | ioh = *pioh; |
| 2677 | if (ioh == NULL) |
| 2678 | break; |
| 2679 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2680 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | } |
| 2683 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2684 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2685 | } else { |
| 2686 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2687 | if (ioh->fd == fd) |
| 2688 | goto found; |
| 2689 | } |
| 2690 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2691 | ioh->next = first_io_handler; |
| 2692 | first_io_handler = ioh; |
| 2693 | found: |
| 2694 | ioh->fd = fd; |
| 2695 | ioh->fd_read_poll = fd_read_poll; |
| 2696 | ioh->fd_read = fd_read; |
| 2697 | ioh->fd_write = fd_write; |
| 2698 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2699 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2700 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2701 | return 0; |
| 2702 | } |
| 2703 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2704 | int qemu_set_fd_handler(int fd, |
| 2705 | IOHandler *fd_read, |
| 2706 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2707 | void *opaque) |
| 2708 | { |
| 2709 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2712 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2713 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2714 | /* Polling handling */ |
| 2715 | |
| 2716 | typedef struct PollingEntry { |
| 2717 | PollingFunc *func; |
| 2718 | void *opaque; |
| 2719 | struct PollingEntry *next; |
| 2720 | } PollingEntry; |
| 2721 | |
| 2722 | static PollingEntry *first_polling_entry; |
| 2723 | |
| 2724 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2725 | { |
| 2726 | PollingEntry **ppe, *pe; |
| 2727 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2728 | pe->func = func; |
| 2729 | pe->opaque = opaque; |
| 2730 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2731 | *ppe = pe; |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2736 | { |
| 2737 | PollingEntry **ppe, *pe; |
| 2738 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2739 | pe = *ppe; |
| 2740 | if (pe->func == func && pe->opaque == opaque) { |
| 2741 | *ppe = pe->next; |
| 2742 | qemu_free(pe); |
| 2743 | break; |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2748 | /***********************************************************/ |
| 2749 | /* Wait objects support */ |
| 2750 | typedef struct WaitObjects { |
| 2751 | int num; |
| 2752 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2753 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2754 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2755 | } WaitObjects; |
| 2756 | |
| 2757 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2758 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2759 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2760 | { |
| 2761 | WaitObjects *w = &wait_objects; |
| 2762 | |
| 2763 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2764 | return -1; |
| 2765 | w->events[w->num] = handle; |
| 2766 | w->func[w->num] = func; |
| 2767 | w->opaque[w->num] = opaque; |
| 2768 | w->num++; |
| 2769 | return 0; |
| 2770 | } |
| 2771 | |
| 2772 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2773 | { |
| 2774 | int i, found; |
| 2775 | WaitObjects *w = &wait_objects; |
| 2776 | |
| 2777 | found = 0; |
| 2778 | for (i = 0; i < w->num; i++) { |
| 2779 | if (w->events[i] == handle) |
| 2780 | found = 1; |
| 2781 | if (found) { |
| 2782 | w->events[i] = w->events[i + 1]; |
| 2783 | w->func[i] = w->func[i + 1]; |
| 2784 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2785 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2786 | } |
| 2787 | if (found) |
| 2788 | w->num--; |
| 2789 | } |
| 2790 | #endif |
| 2791 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2792 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2793 | /* ram save/restore */ |
| 2794 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2795 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 2796 | { |
| 2797 | int v; |
| 2798 | |
| 2799 | v = qemu_get_byte(f); |
| 2800 | switch(v) { |
| 2801 | case 0: |
| 2802 | if (qemu_get_buffer(f, buf, len) != len) |
| 2803 | return -EIO; |
| 2804 | break; |
| 2805 | case 1: |
| 2806 | v = qemu_get_byte(f); |
| 2807 | memset(buf, v, len); |
| 2808 | break; |
| 2809 | default: |
| 2810 | return -EINVAL; |
| 2811 | } |
aliguori | 871d2f0 | 2008-10-13 03:07:56 +0000 | [diff] [blame] | 2812 | |
| 2813 | if (qemu_file_has_error(f)) |
| 2814 | return -EIO; |
| 2815 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2819 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2820 | { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2821 | int ret; |
| 2822 | ram_addr_t i; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2823 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2824 | if (qemu_get_be32(f) != last_ram_offset) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2825 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2826 | for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2827 | ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2828 | if (ret) |
| 2829 | return ret; |
| 2830 | } |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2834 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 2835 | #define IOBUF_SIZE 4096 |
| 2836 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 2837 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2838 | typedef struct RamDecompressState { |
| 2839 | z_stream zstream; |
| 2840 | QEMUFile *f; |
| 2841 | uint8_t buf[IOBUF_SIZE]; |
| 2842 | } RamDecompressState; |
| 2843 | |
| 2844 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 2845 | { |
| 2846 | int ret; |
| 2847 | memset(s, 0, sizeof(*s)); |
| 2848 | s->f = f; |
| 2849 | ret = inflateInit(&s->zstream); |
| 2850 | if (ret != Z_OK) |
| 2851 | return -1; |
| 2852 | return 0; |
| 2853 | } |
| 2854 | |
| 2855 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 2856 | { |
| 2857 | int ret, clen; |
| 2858 | |
| 2859 | s->zstream.avail_out = len; |
| 2860 | s->zstream.next_out = buf; |
| 2861 | while (s->zstream.avail_out > 0) { |
| 2862 | if (s->zstream.avail_in == 0) { |
| 2863 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 2864 | return -1; |
| 2865 | clen = qemu_get_be16(s->f); |
| 2866 | if (clen > IOBUF_SIZE) |
| 2867 | return -1; |
| 2868 | qemu_get_buffer(s->f, s->buf, clen); |
| 2869 | s->zstream.avail_in = clen; |
| 2870 | s->zstream.next_in = s->buf; |
| 2871 | } |
| 2872 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 2873 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 2874 | return -1; |
| 2875 | } |
| 2876 | } |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | static void ram_decompress_close(RamDecompressState *s) |
| 2881 | { |
| 2882 | inflateEnd(&s->zstream); |
| 2883 | } |
| 2884 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2885 | #define RAM_SAVE_FLAG_FULL 0x01 |
| 2886 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2887 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2888 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2889 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2890 | |
| 2891 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2892 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2893 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2894 | uint32_t *array = (uint32_t *)page; |
| 2895 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2896 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2897 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2898 | if (array[i] != val) |
| 2899 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2900 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2901 | |
| 2902 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2905 | static int ram_save_block(QEMUFile *f) |
| 2906 | { |
| 2907 | static ram_addr_t current_addr = 0; |
| 2908 | ram_addr_t saved_addr = current_addr; |
| 2909 | ram_addr_t addr = 0; |
| 2910 | int found = 0; |
| 2911 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2912 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2913 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2914 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2915 | |
| 2916 | cpu_physical_memory_reset_dirty(current_addr, |
| 2917 | current_addr + TARGET_PAGE_SIZE, |
| 2918 | MIGRATION_DIRTY_FLAG); |
| 2919 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2920 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2921 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2922 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2923 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2924 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2925 | } else { |
| 2926 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2927 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | found = 1; |
| 2931 | break; |
| 2932 | } |
| 2933 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2934 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | return found; |
| 2938 | } |
| 2939 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2940 | static uint64_t bytes_transferred = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2941 | |
| 2942 | static ram_addr_t ram_save_remaining(void) |
| 2943 | { |
| 2944 | ram_addr_t addr; |
| 2945 | ram_addr_t count = 0; |
| 2946 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2947 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2948 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 2949 | count++; |
| 2950 | } |
| 2951 | |
| 2952 | return count; |
| 2953 | } |
| 2954 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2955 | uint64_t ram_bytes_remaining(void) |
| 2956 | { |
| 2957 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 2958 | } |
| 2959 | |
| 2960 | uint64_t ram_bytes_transferred(void) |
| 2961 | { |
| 2962 | return bytes_transferred; |
| 2963 | } |
| 2964 | |
| 2965 | uint64_t ram_bytes_total(void) |
| 2966 | { |
| 2967 | return last_ram_offset; |
| 2968 | } |
| 2969 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2970 | static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
| 2971 | { |
| 2972 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2973 | uint64_t bytes_transferred_last; |
| 2974 | double bwidth = 0; |
| 2975 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2976 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 2977 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 2978 | qemu_file_set_error(f); |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2982 | if (stage == 1) { |
| 2983 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2984 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2985 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 2986 | cpu_physical_memory_set_dirty(addr); |
| 2987 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 2988 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2989 | /* Enable dirty memory tracking */ |
| 2990 | cpu_physical_memory_set_dirty_tracking(1); |
| 2991 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2992 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2995 | bytes_transferred_last = bytes_transferred; |
| 2996 | bwidth = get_clock(); |
| 2997 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2998 | while (!qemu_file_rate_limit(f)) { |
| 2999 | int ret; |
| 3000 | |
| 3001 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3002 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3003 | if (ret == 0) /* no more blocks */ |
| 3004 | break; |
| 3005 | } |
| 3006 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3007 | bwidth = get_clock() - bwidth; |
| 3008 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 3009 | |
| 3010 | /* if we haven't transferred anything this round, force expected_time to a |
| 3011 | * a very high value, but without crashing */ |
| 3012 | if (bwidth == 0) |
| 3013 | bwidth = 0.000001; |
| 3014 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3015 | /* try transferring iterative blocks of memory */ |
| 3016 | |
| 3017 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3018 | |
| 3019 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3020 | while (ram_save_block(f) != 0) { |
| 3021 | bytes_transferred += TARGET_PAGE_SIZE; |
| 3022 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 3023 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3027 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3028 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 3029 | |
| 3030 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | static int ram_load_dead(QEMUFile *f, void *opaque) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3034 | { |
| 3035 | RamDecompressState s1, *s = &s1; |
| 3036 | uint8_t buf[10]; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3037 | ram_addr_t i; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3038 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3039 | if (ram_decompress_open(s, f) < 0) |
| 3040 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3041 | for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3042 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 3043 | fprintf(stderr, "Error while reading ram block header\n"); |
| 3044 | goto error; |
| 3045 | } |
| 3046 | if (buf[0] == 0) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3047 | if (ram_decompress_buf(s, qemu_get_ram_ptr(i), |
| 3048 | BDRV_HASH_BLOCK_SIZE) < 0) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3049 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3050 | goto error; |
| 3051 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3052 | } else { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3053 | error: |
| 3054 | printf("Error block header\n"); |
| 3055 | return -EINVAL; |
| 3056 | } |
| 3057 | } |
| 3058 | ram_decompress_close(s); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3059 | |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3064 | { |
| 3065 | ram_addr_t addr; |
| 3066 | int flags; |
| 3067 | |
| 3068 | if (version_id == 1) |
| 3069 | return ram_load_v1(f, opaque); |
| 3070 | |
| 3071 | if (version_id == 2) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3072 | if (qemu_get_be32(f) != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3073 | return -EINVAL; |
| 3074 | return ram_load_dead(f, opaque); |
| 3075 | } |
| 3076 | |
| 3077 | if (version_id != 3) |
| 3078 | return -EINVAL; |
| 3079 | |
| 3080 | do { |
| 3081 | addr = qemu_get_be64(f); |
| 3082 | |
| 3083 | flags = addr & ~TARGET_PAGE_MASK; |
| 3084 | addr &= TARGET_PAGE_MASK; |
| 3085 | |
| 3086 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3087 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3088 | return -EINVAL; |
| 3089 | } |
| 3090 | |
| 3091 | if (flags & RAM_SAVE_FLAG_FULL) { |
| 3092 | if (ram_load_dead(f, opaque) < 0) |
| 3093 | return -EINVAL; |
| 3094 | } |
| 3095 | |
| 3096 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 3097 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3098 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 3099 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3100 | if (ch == 0 && |
| 3101 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 3102 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3103 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3104 | #endif |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3105 | } else if (flags & RAM_SAVE_FLAG_PAGE) |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3106 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3107 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 3108 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3109 | return 0; |
| 3110 | } |
| 3111 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3112 | void qemu_service_io(void) |
| 3113 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3114 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3117 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3118 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 3119 | |
| 3120 | struct QEMUBH { |
| 3121 | QEMUBHFunc *cb; |
| 3122 | void *opaque; |
| 3123 | int scheduled; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3124 | int idle; |
| 3125 | int deleted; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3126 | QEMUBH *next; |
| 3127 | }; |
| 3128 | |
| 3129 | static QEMUBH *first_bh = NULL; |
| 3130 | |
| 3131 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 3132 | { |
| 3133 | QEMUBH *bh; |
| 3134 | bh = qemu_mallocz(sizeof(QEMUBH)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3135 | bh->cb = cb; |
| 3136 | bh->opaque = opaque; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3137 | bh->next = first_bh; |
| 3138 | first_bh = bh; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3139 | return bh; |
| 3140 | } |
| 3141 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3142 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3143 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3144 | QEMUBH *bh, **bhp; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3145 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3146 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3147 | ret = 0; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3148 | for (bh = first_bh; bh; bh = bh->next) { |
| 3149 | if (!bh->deleted && bh->scheduled) { |
| 3150 | bh->scheduled = 0; |
| 3151 | if (!bh->idle) |
| 3152 | ret = 1; |
| 3153 | bh->idle = 0; |
| 3154 | bh->cb(bh->opaque); |
| 3155 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3156 | } |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3157 | |
| 3158 | /* remove deleted bhs */ |
| 3159 | bhp = &first_bh; |
| 3160 | while (*bhp) { |
| 3161 | bh = *bhp; |
| 3162 | if (bh->deleted) { |
| 3163 | *bhp = bh->next; |
| 3164 | qemu_free(bh); |
| 3165 | } else |
| 3166 | bhp = &bh->next; |
| 3167 | } |
| 3168 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3169 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3172 | void qemu_bh_schedule_idle(QEMUBH *bh) |
| 3173 | { |
| 3174 | if (bh->scheduled) |
| 3175 | return; |
| 3176 | bh->scheduled = 1; |
| 3177 | bh->idle = 1; |
| 3178 | } |
| 3179 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3180 | void qemu_bh_schedule(QEMUBH *bh) |
| 3181 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3182 | if (bh->scheduled) |
| 3183 | return; |
| 3184 | bh->scheduled = 1; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3185 | bh->idle = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3186 | /* stop the currently executing CPU to execute the BH ASAP */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3187 | qemu_notify_event(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | void qemu_bh_cancel(QEMUBH *bh) |
| 3191 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3192 | bh->scheduled = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | void qemu_bh_delete(QEMUBH *bh) |
| 3196 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3197 | bh->scheduled = 0; |
| 3198 | bh->deleted = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3201 | static void qemu_bh_update_timeout(int *timeout) |
| 3202 | { |
| 3203 | QEMUBH *bh; |
| 3204 | |
| 3205 | for (bh = first_bh; bh; bh = bh->next) { |
| 3206 | if (!bh->deleted && bh->scheduled) { |
| 3207 | if (bh->idle) { |
| 3208 | /* idle bottom halves will be polled at least |
| 3209 | * every 10ms */ |
| 3210 | *timeout = MIN(10, *timeout); |
| 3211 | } else { |
| 3212 | /* non-idle bottom halves will be executed |
| 3213 | * immediately */ |
| 3214 | *timeout = 0; |
| 3215 | break; |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3221 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3222 | /* machine registration */ |
| 3223 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 3224 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 3225 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3226 | |
| 3227 | int qemu_register_machine(QEMUMachine *m) |
| 3228 | { |
| 3229 | QEMUMachine **pm; |
| 3230 | pm = &first_machine; |
| 3231 | while (*pm != NULL) |
| 3232 | pm = &(*pm)->next; |
| 3233 | m->next = NULL; |
| 3234 | *pm = m; |
| 3235 | return 0; |
| 3236 | } |
| 3237 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3238 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3239 | { |
| 3240 | QEMUMachine *m; |
| 3241 | |
| 3242 | for(m = first_machine; m != NULL; m = m->next) { |
| 3243 | if (!strcmp(m->name, name)) |
| 3244 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 3245 | if (m->alias && !strcmp(m->alias, name)) |
| 3246 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3247 | } |
| 3248 | return NULL; |
| 3249 | } |
| 3250 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 3251 | static QEMUMachine *find_default_machine(void) |
| 3252 | { |
| 3253 | QEMUMachine *m; |
| 3254 | |
| 3255 | for(m = first_machine; m != NULL; m = m->next) { |
| 3256 | if (m->is_default) { |
| 3257 | return m; |
| 3258 | } |
| 3259 | } |
| 3260 | return NULL; |
| 3261 | } |
| 3262 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3263 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3264 | /* main execution loop */ |
| 3265 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3266 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3267 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3268 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 3269 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3270 | DisplayChangeListener *dcl = ds->listeners; |
| 3271 | |
| 3272 | dpy_refresh(ds); |
| 3273 | |
| 3274 | while (dcl != NULL) { |
| 3275 | if (dcl->gui_timer_interval && |
| 3276 | dcl->gui_timer_interval < interval) |
| 3277 | interval = dcl->gui_timer_interval; |
| 3278 | dcl = dcl->next; |
| 3279 | } |
| 3280 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3283 | static void nographic_update(void *opaque) |
| 3284 | { |
| 3285 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3286 | |
| 3287 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3288 | } |
| 3289 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3290 | struct vm_change_state_entry { |
| 3291 | VMChangeStateHandler *cb; |
| 3292 | void *opaque; |
| 3293 | LIST_ENTRY (vm_change_state_entry) entries; |
| 3294 | }; |
| 3295 | |
| 3296 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 3297 | |
| 3298 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3299 | void *opaque) |
| 3300 | { |
| 3301 | VMChangeStateEntry *e; |
| 3302 | |
| 3303 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3304 | |
| 3305 | e->cb = cb; |
| 3306 | e->opaque = opaque; |
| 3307 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 3308 | return e; |
| 3309 | } |
| 3310 | |
| 3311 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3312 | { |
| 3313 | LIST_REMOVE (e, entries); |
| 3314 | qemu_free (e); |
| 3315 | } |
| 3316 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3317 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3318 | { |
| 3319 | VMChangeStateEntry *e; |
| 3320 | |
| 3321 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3322 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3326 | static void resume_all_vcpus(void); |
| 3327 | static void pause_all_vcpus(void); |
| 3328 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3329 | void vm_start(void) |
| 3330 | { |
| 3331 | if (!vm_running) { |
| 3332 | cpu_enable_ticks(); |
| 3333 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3334 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3335 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3336 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3340 | /* reset/shutdown handler */ |
| 3341 | |
| 3342 | typedef struct QEMUResetEntry { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3343 | TAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3344 | QEMUResetHandler *func; |
| 3345 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3346 | } QEMUResetEntry; |
| 3347 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3348 | static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3349 | TAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3350 | static int reset_requested; |
| 3351 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3352 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3353 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3354 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3355 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3356 | int qemu_shutdown_requested(void) |
| 3357 | { |
| 3358 | int r = shutdown_requested; |
| 3359 | shutdown_requested = 0; |
| 3360 | return r; |
| 3361 | } |
| 3362 | |
| 3363 | int qemu_reset_requested(void) |
| 3364 | { |
| 3365 | int r = reset_requested; |
| 3366 | reset_requested = 0; |
| 3367 | return r; |
| 3368 | } |
| 3369 | |
| 3370 | int qemu_powerdown_requested(void) |
| 3371 | { |
| 3372 | int r = powerdown_requested; |
| 3373 | powerdown_requested = 0; |
| 3374 | return r; |
| 3375 | } |
| 3376 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3377 | static int qemu_debug_requested(void) |
| 3378 | { |
| 3379 | int r = debug_requested; |
| 3380 | debug_requested = 0; |
| 3381 | return r; |
| 3382 | } |
| 3383 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3384 | static int qemu_vmstop_requested(void) |
| 3385 | { |
| 3386 | int r = vmstop_requested; |
| 3387 | vmstop_requested = 0; |
| 3388 | return r; |
| 3389 | } |
| 3390 | |
| 3391 | static void do_vm_stop(int reason) |
| 3392 | { |
| 3393 | if (vm_running) { |
| 3394 | cpu_disable_ticks(); |
| 3395 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3396 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3397 | vm_state_notify(0, reason); |
| 3398 | } |
| 3399 | } |
| 3400 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3401 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3402 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3403 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3404 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3405 | re->func = func; |
| 3406 | re->opaque = opaque; |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3407 | TAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3410 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3411 | { |
| 3412 | QEMUResetEntry *re; |
| 3413 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3414 | TAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3415 | if (re->func == func && re->opaque == opaque) { |
| 3416 | TAILQ_REMOVE(&reset_handlers, re, entry); |
| 3417 | qemu_free(re); |
| 3418 | return; |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | void qemu_system_reset(void) |
| 3424 | { |
| 3425 | QEMUResetEntry *re, *nre; |
| 3426 | |
| 3427 | /* reset all devices */ |
| 3428 | TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3429 | re->func(re->opaque); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | void qemu_system_reset_request(void) |
| 3434 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3435 | if (no_reboot) { |
| 3436 | shutdown_requested = 1; |
| 3437 | } else { |
| 3438 | reset_requested = 1; |
| 3439 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3440 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | void qemu_system_shutdown_request(void) |
| 3444 | { |
| 3445 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3446 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3449 | void qemu_system_powerdown_request(void) |
| 3450 | { |
| 3451 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3452 | qemu_notify_event(); |
| 3453 | } |
| 3454 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3455 | #ifdef CONFIG_IOTHREAD |
| 3456 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3457 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3458 | vmstop_requested = reason; |
| 3459 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3460 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3461 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3462 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3463 | #ifndef _WIN32 |
| 3464 | static int io_thread_fd = -1; |
| 3465 | |
| 3466 | static void qemu_event_increment(void) |
| 3467 | { |
| 3468 | static const char byte = 0; |
| 3469 | |
| 3470 | if (io_thread_fd == -1) |
| 3471 | return; |
| 3472 | |
| 3473 | write(io_thread_fd, &byte, sizeof(byte)); |
| 3474 | } |
| 3475 | |
| 3476 | static void qemu_event_read(void *opaque) |
| 3477 | { |
| 3478 | int fd = (unsigned long)opaque; |
| 3479 | ssize_t len; |
| 3480 | |
| 3481 | /* Drain the notify pipe */ |
| 3482 | do { |
| 3483 | char buffer[512]; |
| 3484 | len = read(fd, buffer, sizeof(buffer)); |
| 3485 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3486 | } |
| 3487 | |
| 3488 | static int qemu_event_init(void) |
| 3489 | { |
| 3490 | int err; |
| 3491 | int fds[2]; |
| 3492 | |
| 3493 | err = pipe(fds); |
| 3494 | if (err == -1) |
| 3495 | return -errno; |
| 3496 | |
| 3497 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3498 | if (err < 0) |
| 3499 | goto fail; |
| 3500 | |
| 3501 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3502 | if (err < 0) |
| 3503 | goto fail; |
| 3504 | |
| 3505 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3506 | (void *)(unsigned long)fds[0]); |
| 3507 | |
| 3508 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3509 | return 0; |
| 3510 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3511 | fail: |
| 3512 | close(fds[0]); |
| 3513 | close(fds[1]); |
| 3514 | return err; |
| 3515 | } |
| 3516 | #else |
| 3517 | HANDLE qemu_event_handle; |
| 3518 | |
| 3519 | static void dummy_event_handler(void *opaque) |
| 3520 | { |
| 3521 | } |
| 3522 | |
| 3523 | static int qemu_event_init(void) |
| 3524 | { |
| 3525 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3526 | if (!qemu_event_handle) { |
| 3527 | perror("Failed CreateEvent"); |
| 3528 | return -1; |
| 3529 | } |
| 3530 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3531 | return 0; |
| 3532 | } |
| 3533 | |
| 3534 | static void qemu_event_increment(void) |
| 3535 | { |
| 3536 | SetEvent(qemu_event_handle); |
| 3537 | } |
| 3538 | #endif |
| 3539 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3540 | static int cpu_can_run(CPUState *env) |
| 3541 | { |
| 3542 | if (env->stop) |
| 3543 | return 0; |
| 3544 | if (env->stopped) |
| 3545 | return 0; |
| 3546 | return 1; |
| 3547 | } |
| 3548 | |
| 3549 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3550 | static int qemu_init_main_loop(void) |
| 3551 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3552 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3555 | void qemu_init_vcpu(void *_env) |
| 3556 | { |
| 3557 | CPUState *env = _env; |
| 3558 | |
| 3559 | if (kvm_enabled()) |
| 3560 | kvm_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3561 | env->nr_cores = smp_cores; |
| 3562 | env->nr_threads = smp_threads; |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3563 | return; |
| 3564 | } |
| 3565 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3566 | int qemu_cpu_self(void *env) |
| 3567 | { |
| 3568 | return 1; |
| 3569 | } |
| 3570 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3571 | static void resume_all_vcpus(void) |
| 3572 | { |
| 3573 | } |
| 3574 | |
| 3575 | static void pause_all_vcpus(void) |
| 3576 | { |
| 3577 | } |
| 3578 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3579 | void qemu_cpu_kick(void *env) |
| 3580 | { |
| 3581 | return; |
| 3582 | } |
| 3583 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3584 | void qemu_notify_event(void) |
| 3585 | { |
| 3586 | CPUState *env = cpu_single_env; |
| 3587 | |
| 3588 | if (env) { |
| 3589 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3590 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3593 | #define qemu_mutex_lock_iothread() do { } while (0) |
| 3594 | #define qemu_mutex_unlock_iothread() do { } while (0) |
| 3595 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3596 | void vm_stop(int reason) |
| 3597 | { |
| 3598 | do_vm_stop(reason); |
| 3599 | } |
| 3600 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3601 | #else /* CONFIG_IOTHREAD */ |
| 3602 | |
| 3603 | #include "qemu-thread.h" |
| 3604 | |
| 3605 | QemuMutex qemu_global_mutex; |
| 3606 | static QemuMutex qemu_fair_mutex; |
| 3607 | |
| 3608 | static QemuThread io_thread; |
| 3609 | |
| 3610 | static QemuThread *tcg_cpu_thread; |
| 3611 | static QemuCond *tcg_halt_cond; |
| 3612 | |
| 3613 | static int qemu_system_ready; |
| 3614 | /* cpu creation */ |
| 3615 | static QemuCond qemu_cpu_cond; |
| 3616 | /* system init */ |
| 3617 | static QemuCond qemu_system_cond; |
| 3618 | static QemuCond qemu_pause_cond; |
| 3619 | |
| 3620 | static void block_io_signals(void); |
| 3621 | static void unblock_io_signals(void); |
| 3622 | static int tcg_has_work(void); |
| 3623 | |
| 3624 | static int qemu_init_main_loop(void) |
| 3625 | { |
| 3626 | int ret; |
| 3627 | |
| 3628 | ret = qemu_event_init(); |
| 3629 | if (ret) |
| 3630 | return ret; |
| 3631 | |
| 3632 | qemu_cond_init(&qemu_pause_cond); |
| 3633 | qemu_mutex_init(&qemu_fair_mutex); |
| 3634 | qemu_mutex_init(&qemu_global_mutex); |
| 3635 | qemu_mutex_lock(&qemu_global_mutex); |
| 3636 | |
| 3637 | unblock_io_signals(); |
| 3638 | qemu_thread_self(&io_thread); |
| 3639 | |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
| 3643 | static void qemu_wait_io_event(CPUState *env) |
| 3644 | { |
| 3645 | while (!tcg_has_work()) |
| 3646 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3647 | |
| 3648 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3649 | |
| 3650 | /* |
| 3651 | * Users of qemu_global_mutex can be starved, having no chance |
| 3652 | * to acquire it since this path will get to it first. |
| 3653 | * So use another lock to provide fairness. |
| 3654 | */ |
| 3655 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3656 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3657 | |
| 3658 | qemu_mutex_lock(&qemu_global_mutex); |
| 3659 | if (env->stop) { |
| 3660 | env->stop = 0; |
| 3661 | env->stopped = 1; |
| 3662 | qemu_cond_signal(&qemu_pause_cond); |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | static int qemu_cpu_exec(CPUState *env); |
| 3667 | |
| 3668 | static void *kvm_cpu_thread_fn(void *arg) |
| 3669 | { |
| 3670 | CPUState *env = arg; |
| 3671 | |
| 3672 | block_io_signals(); |
| 3673 | qemu_thread_self(env->thread); |
Jean-Christophe DUBOIS | 321c1cb | 2009-09-02 23:59:04 +0200 | [diff] [blame] | 3674 | if (kvm_enabled()) |
| 3675 | kvm_init_vcpu(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* signal CPU creation */ |
| 3678 | qemu_mutex_lock(&qemu_global_mutex); |
| 3679 | env->created = 1; |
| 3680 | qemu_cond_signal(&qemu_cpu_cond); |
| 3681 | |
| 3682 | /* and wait for machine initialization */ |
| 3683 | while (!qemu_system_ready) |
| 3684 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3685 | |
| 3686 | while (1) { |
| 3687 | if (cpu_can_run(env)) |
| 3688 | qemu_cpu_exec(env); |
Anthony Liguori | 1c3173b | 2009-09-10 08:45:43 -0500 | [diff] [blame] | 3689 | qemu_wait_io_event(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | return NULL; |
| 3693 | } |
| 3694 | |
| 3695 | static void tcg_cpu_exec(void); |
| 3696 | |
| 3697 | static void *tcg_cpu_thread_fn(void *arg) |
| 3698 | { |
| 3699 | CPUState *env = arg; |
| 3700 | |
| 3701 | block_io_signals(); |
| 3702 | qemu_thread_self(env->thread); |
| 3703 | |
| 3704 | /* signal CPU creation */ |
| 3705 | qemu_mutex_lock(&qemu_global_mutex); |
| 3706 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3707 | env->created = 1; |
| 3708 | qemu_cond_signal(&qemu_cpu_cond); |
| 3709 | |
| 3710 | /* and wait for machine initialization */ |
| 3711 | while (!qemu_system_ready) |
| 3712 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3713 | |
| 3714 | while (1) { |
| 3715 | tcg_cpu_exec(); |
| 3716 | qemu_wait_io_event(cur_cpu); |
| 3717 | } |
| 3718 | |
| 3719 | return NULL; |
| 3720 | } |
| 3721 | |
| 3722 | void qemu_cpu_kick(void *_env) |
| 3723 | { |
| 3724 | CPUState *env = _env; |
| 3725 | qemu_cond_broadcast(env->halt_cond); |
| 3726 | if (kvm_enabled()) |
| 3727 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3728 | } |
| 3729 | |
| 3730 | int qemu_cpu_self(void *env) |
| 3731 | { |
| 3732 | return (cpu_single_env != NULL); |
| 3733 | } |
| 3734 | |
| 3735 | static void cpu_signal(int sig) |
| 3736 | { |
| 3737 | if (cpu_single_env) |
| 3738 | cpu_exit(cpu_single_env); |
| 3739 | } |
| 3740 | |
| 3741 | static void block_io_signals(void) |
| 3742 | { |
| 3743 | sigset_t set; |
| 3744 | struct sigaction sigact; |
| 3745 | |
| 3746 | sigemptyset(&set); |
| 3747 | sigaddset(&set, SIGUSR2); |
| 3748 | sigaddset(&set, SIGIO); |
| 3749 | sigaddset(&set, SIGALRM); |
| 3750 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3751 | |
| 3752 | sigemptyset(&set); |
| 3753 | sigaddset(&set, SIGUSR1); |
| 3754 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3755 | |
| 3756 | memset(&sigact, 0, sizeof(sigact)); |
| 3757 | sigact.sa_handler = cpu_signal; |
| 3758 | sigaction(SIGUSR1, &sigact, NULL); |
| 3759 | } |
| 3760 | |
| 3761 | static void unblock_io_signals(void) |
| 3762 | { |
| 3763 | sigset_t set; |
| 3764 | |
| 3765 | sigemptyset(&set); |
| 3766 | sigaddset(&set, SIGUSR2); |
| 3767 | sigaddset(&set, SIGIO); |
| 3768 | sigaddset(&set, SIGALRM); |
| 3769 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3770 | |
| 3771 | sigemptyset(&set); |
| 3772 | sigaddset(&set, SIGUSR1); |
| 3773 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3774 | } |
| 3775 | |
| 3776 | static void qemu_signal_lock(unsigned int msecs) |
| 3777 | { |
| 3778 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3779 | |
| 3780 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3781 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3782 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3783 | break; |
| 3784 | } |
| 3785 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3786 | } |
| 3787 | |
| 3788 | static void qemu_mutex_lock_iothread(void) |
| 3789 | { |
| 3790 | if (kvm_enabled()) { |
| 3791 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3792 | qemu_mutex_lock(&qemu_global_mutex); |
| 3793 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3794 | } else |
| 3795 | qemu_signal_lock(100); |
| 3796 | } |
| 3797 | |
| 3798 | static void qemu_mutex_unlock_iothread(void) |
| 3799 | { |
| 3800 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3801 | } |
| 3802 | |
| 3803 | static int all_vcpus_paused(void) |
| 3804 | { |
| 3805 | CPUState *penv = first_cpu; |
| 3806 | |
| 3807 | while (penv) { |
| 3808 | if (!penv->stopped) |
| 3809 | return 0; |
| 3810 | penv = (CPUState *)penv->next_cpu; |
| 3811 | } |
| 3812 | |
| 3813 | return 1; |
| 3814 | } |
| 3815 | |
| 3816 | static void pause_all_vcpus(void) |
| 3817 | { |
| 3818 | CPUState *penv = first_cpu; |
| 3819 | |
| 3820 | while (penv) { |
| 3821 | penv->stop = 1; |
| 3822 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3823 | qemu_cpu_kick(penv); |
| 3824 | penv = (CPUState *)penv->next_cpu; |
| 3825 | } |
| 3826 | |
| 3827 | while (!all_vcpus_paused()) { |
| 3828 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3829 | penv = first_cpu; |
| 3830 | while (penv) { |
| 3831 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3832 | penv = (CPUState *)penv->next_cpu; |
| 3833 | } |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | static void resume_all_vcpus(void) |
| 3838 | { |
| 3839 | CPUState *penv = first_cpu; |
| 3840 | |
| 3841 | while (penv) { |
| 3842 | penv->stop = 0; |
| 3843 | penv->stopped = 0; |
| 3844 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3845 | qemu_cpu_kick(penv); |
| 3846 | penv = (CPUState *)penv->next_cpu; |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | static void tcg_init_vcpu(void *_env) |
| 3851 | { |
| 3852 | CPUState *env = _env; |
| 3853 | /* share a single thread for all cpus with TCG */ |
| 3854 | if (!tcg_cpu_thread) { |
| 3855 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3856 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3857 | qemu_cond_init(env->halt_cond); |
| 3858 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3859 | while (env->created == 0) |
| 3860 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3861 | tcg_cpu_thread = env->thread; |
| 3862 | tcg_halt_cond = env->halt_cond; |
| 3863 | } else { |
| 3864 | env->thread = tcg_cpu_thread; |
| 3865 | env->halt_cond = tcg_halt_cond; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | static void kvm_start_vcpu(CPUState *env) |
| 3870 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3871 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3872 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3873 | qemu_cond_init(env->halt_cond); |
| 3874 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3875 | while (env->created == 0) |
| 3876 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3877 | } |
| 3878 | |
| 3879 | void qemu_init_vcpu(void *_env) |
| 3880 | { |
| 3881 | CPUState *env = _env; |
| 3882 | |
| 3883 | if (kvm_enabled()) |
| 3884 | kvm_start_vcpu(env); |
| 3885 | else |
| 3886 | tcg_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3887 | env->nr_cores = smp_cores; |
| 3888 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | void qemu_notify_event(void) |
| 3892 | { |
| 3893 | qemu_event_increment(); |
| 3894 | } |
| 3895 | |
| 3896 | void vm_stop(int reason) |
| 3897 | { |
| 3898 | QemuThread me; |
| 3899 | qemu_thread_self(&me); |
| 3900 | |
| 3901 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3902 | qemu_system_vmstop_request(reason); |
| 3903 | /* |
| 3904 | * FIXME: should not return to device code in case |
| 3905 | * vm_stop() has been requested. |
| 3906 | */ |
| 3907 | if (cpu_single_env) { |
| 3908 | cpu_exit(cpu_single_env); |
| 3909 | cpu_single_env->stop = 1; |
| 3910 | } |
| 3911 | return; |
| 3912 | } |
| 3913 | do_vm_stop(reason); |
| 3914 | } |
| 3915 | |
| 3916 | #endif |
| 3917 | |
| 3918 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3919 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3920 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3921 | { |
| 3922 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3923 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3924 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3925 | |
| 3926 | /* XXX: need to suppress polling by better using win32 events */ |
| 3927 | ret = 0; |
| 3928 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 3929 | ret |= pe->func(pe->opaque); |
| 3930 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3931 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3932 | int err; |
| 3933 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3934 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3935 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3936 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 3937 | if (w->func[ret - WAIT_OBJECT_0]) |
| 3938 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3939 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3940 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3941 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3942 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3943 | /* Check if event is signaled */ |
| 3944 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 3945 | if(ret2 == WAIT_OBJECT_0) { |
| 3946 | if (w->func[i]) |
| 3947 | w->func[i](w->opaque[i]); |
| 3948 | } else if (ret2 == WAIT_TIMEOUT) { |
| 3949 | } else { |
| 3950 | err = GetLastError(); |
| 3951 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3952 | } |
| 3953 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3954 | } else if (ret == WAIT_TIMEOUT) { |
| 3955 | } else { |
| 3956 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3957 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3958 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3959 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3960 | |
| 3961 | *timeout = 0; |
| 3962 | } |
| 3963 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3964 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3965 | { |
| 3966 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3967 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3968 | |
| 3969 | void main_loop_wait(int timeout) |
| 3970 | { |
| 3971 | IOHandlerRecord *ioh; |
| 3972 | fd_set rfds, wfds, xfds; |
| 3973 | int ret, nfds; |
| 3974 | struct timeval tv; |
| 3975 | |
| 3976 | qemu_bh_update_timeout(&timeout); |
| 3977 | |
| 3978 | host_main_loop_wait(&timeout); |
| 3979 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3980 | /* poll any events */ |
| 3981 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 3982 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3983 | FD_ZERO(&rfds); |
| 3984 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3985 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3986 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3987 | if (ioh->deleted) |
| 3988 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3989 | if (ioh->fd_read && |
| 3990 | (!ioh->fd_read_poll || |
| 3991 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 3992 | FD_SET(ioh->fd, &rfds); |
| 3993 | if (ioh->fd > nfds) |
| 3994 | nfds = ioh->fd; |
| 3995 | } |
| 3996 | if (ioh->fd_write) { |
| 3997 | FD_SET(ioh->fd, &wfds); |
| 3998 | if (ioh->fd > nfds) |
| 3999 | nfds = ioh->fd; |
| 4000 | } |
| 4001 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4002 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4003 | tv.tv_sec = timeout / 1000; |
| 4004 | tv.tv_usec = (timeout % 1000) * 1000; |
| 4005 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4006 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 4007 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4008 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 4009 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4010 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4011 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4012 | IOHandlerRecord **pioh; |
| 4013 | |
| 4014 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4015 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4016 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4017 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4018 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4019 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* remove deleted IO handlers */ |
| 4024 | pioh = &first_io_handler; |
| 4025 | while (*pioh) { |
| 4026 | ioh = *pioh; |
| 4027 | if (ioh->deleted) { |
| 4028 | *pioh = ioh->next; |
| 4029 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4030 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4031 | pioh = &ioh->next; |
| 4032 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4033 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4034 | |
| 4035 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4036 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 4037 | /* rearm timer, if not periodic */ |
| 4038 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 4039 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 4040 | qemu_rearm_alarm_timer(alarm_timer); |
| 4041 | } |
| 4042 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4043 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4044 | if (vm_running) { |
| 4045 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
| 4046 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 4047 | qemu_get_clock(vm_clock)); |
| 4048 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4049 | |
| 4050 | /* real time timers */ |
| 4051 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 4052 | qemu_get_clock(rt_clock)); |
| 4053 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 4054 | /* Check bottom-halves last in case any of the earlier events triggered |
| 4055 | them. */ |
| 4056 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4057 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4060 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4061 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4062 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 4063 | #ifdef CONFIG_PROFILER |
| 4064 | int64_t ti; |
| 4065 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4066 | |
| 4067 | #ifdef CONFIG_PROFILER |
| 4068 | ti = profile_getclock(); |
| 4069 | #endif |
| 4070 | if (use_icount) { |
| 4071 | int64_t count; |
| 4072 | int decr; |
| 4073 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 4074 | env->icount_decr.u16.low = 0; |
| 4075 | env->icount_extra = 0; |
| 4076 | count = qemu_next_deadline(); |
| 4077 | count = (count + (1 << icount_time_shift) - 1) |
| 4078 | >> icount_time_shift; |
| 4079 | qemu_icount += count; |
| 4080 | decr = (count > 0xffff) ? 0xffff : count; |
| 4081 | count -= decr; |
| 4082 | env->icount_decr.u16.low = decr; |
| 4083 | env->icount_extra = count; |
| 4084 | } |
| 4085 | ret = cpu_exec(env); |
| 4086 | #ifdef CONFIG_PROFILER |
| 4087 | qemu_time += profile_getclock() - ti; |
| 4088 | #endif |
| 4089 | if (use_icount) { |
| 4090 | /* Fold pending instructions back into the |
| 4091 | instruction counter, and clear the interrupt flag. */ |
| 4092 | qemu_icount -= (env->icount_decr.u16.low |
| 4093 | + env->icount_extra); |
| 4094 | env->icount_decr.u32 = 0; |
| 4095 | env->icount_extra = 0; |
| 4096 | } |
| 4097 | return ret; |
| 4098 | } |
| 4099 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4100 | static void tcg_cpu_exec(void) |
| 4101 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4102 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4103 | |
| 4104 | if (next_cpu == NULL) |
| 4105 | next_cpu = first_cpu; |
| 4106 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 4107 | CPUState *env = cur_cpu = next_cpu; |
| 4108 | |
| 4109 | if (!vm_running) |
| 4110 | break; |
| 4111 | if (timer_alarm_pending) { |
| 4112 | timer_alarm_pending = 0; |
| 4113 | break; |
| 4114 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4115 | if (cpu_can_run(env)) |
| 4116 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4117 | if (ret == EXCP_DEBUG) { |
| 4118 | gdb_set_stop_cpu(env); |
| 4119 | debug_requested = 1; |
| 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | } |
| 4124 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4125 | static int cpu_has_work(CPUState *env) |
| 4126 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4127 | if (env->stop) |
| 4128 | return 1; |
| 4129 | if (env->stopped) |
| 4130 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4131 | if (!env->halted) |
| 4132 | return 1; |
| 4133 | if (qemu_cpu_has_work(env)) |
| 4134 | return 1; |
| 4135 | return 0; |
| 4136 | } |
| 4137 | |
| 4138 | static int tcg_has_work(void) |
| 4139 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 4140 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4141 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4142 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 4143 | if (cpu_has_work(env)) |
| 4144 | return 1; |
| 4145 | return 0; |
| 4146 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4147 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4148 | static int qemu_calculate_timeout(void) |
| 4149 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4150 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4151 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4152 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4153 | if (!vm_running) |
| 4154 | timeout = 5000; |
| 4155 | else if (tcg_has_work()) |
| 4156 | timeout = 0; |
| 4157 | else if (!use_icount) |
| 4158 | timeout = 5000; |
| 4159 | else { |
| 4160 | /* XXX: use timeout computed from timers */ |
| 4161 | int64_t add; |
| 4162 | int64_t delta; |
| 4163 | /* Advance virtual time to the next event. */ |
| 4164 | if (use_icount == 1) { |
| 4165 | /* When not using an adaptive execution frequency |
| 4166 | we tend to get badly out of sync with real time, |
| 4167 | so just delay for a reasonable amount of time. */ |
| 4168 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4169 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4170 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4171 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4172 | if (delta > 0) { |
| 4173 | /* If virtual time is ahead of real time then just |
| 4174 | wait for IO. */ |
| 4175 | timeout = (delta / 1000000) + 1; |
| 4176 | } else { |
| 4177 | /* Wait for either IO to occur or the next |
| 4178 | timer event. */ |
| 4179 | add = qemu_next_deadline(); |
| 4180 | /* We advance the timer before checking for IO. |
| 4181 | Limit the amount we advance so that early IO |
| 4182 | activity won't get the guest too far ahead. */ |
| 4183 | if (add > 10000000) |
| 4184 | add = 10000000; |
| 4185 | delta += add; |
| 4186 | add = (add + (1 << icount_time_shift) - 1) |
| 4187 | >> icount_time_shift; |
| 4188 | qemu_icount += add; |
| 4189 | timeout = delta / 1000000; |
| 4190 | if (timeout < 0) |
| 4191 | timeout = 0; |
| 4192 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4193 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4194 | |
| 4195 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4196 | #else /* CONFIG_IOTHREAD */ |
| 4197 | return 1000; |
| 4198 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | static int vm_can_run(void) |
| 4202 | { |
| 4203 | if (powerdown_requested) |
| 4204 | return 0; |
| 4205 | if (reset_requested) |
| 4206 | return 0; |
| 4207 | if (shutdown_requested) |
| 4208 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4209 | if (debug_requested) |
| 4210 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4211 | return 1; |
| 4212 | } |
| 4213 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4214 | qemu_irq qemu_system_powerdown; |
| 4215 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4216 | static void main_loop(void) |
| 4217 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4218 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4219 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4220 | #ifdef CONFIG_IOTHREAD |
| 4221 | qemu_system_ready = 1; |
| 4222 | qemu_cond_broadcast(&qemu_system_cond); |
| 4223 | #endif |
| 4224 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4225 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4226 | do { |
| 4227 | #ifdef CONFIG_PROFILER |
| 4228 | int64_t ti; |
| 4229 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4230 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4231 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4232 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4233 | #ifdef CONFIG_PROFILER |
| 4234 | ti = profile_getclock(); |
| 4235 | #endif |
| 4236 | main_loop_wait(qemu_calculate_timeout()); |
| 4237 | #ifdef CONFIG_PROFILER |
| 4238 | dev_time += profile_getclock() - ti; |
| 4239 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4240 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4241 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4242 | if (qemu_debug_requested()) |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4243 | vm_stop(EXCP_DEBUG); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4244 | if (qemu_shutdown_requested()) { |
| 4245 | if (no_shutdown) { |
| 4246 | vm_stop(0); |
| 4247 | no_shutdown = 0; |
| 4248 | } else |
| 4249 | break; |
| 4250 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4251 | if (qemu_reset_requested()) { |
| 4252 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4253 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4254 | resume_all_vcpus(); |
| 4255 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4256 | if (qemu_powerdown_requested()) { |
| 4257 | qemu_irq_raise(qemu_system_powerdown); |
| 4258 | } |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4259 | if ((r = qemu_vmstop_requested())) |
| 4260 | vm_stop(r); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4261 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4262 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4265 | static void version(void) |
| 4266 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4267 | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4270 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4271 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4272 | version(); |
| 4273 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4274 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4275 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4276 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4277 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4278 | opt_help |
| 4279 | #define DEFHEADING(text) stringify(text) "\n" |
| 4280 | #include "qemu-options.h" |
| 4281 | #undef DEF |
| 4282 | #undef DEFHEADING |
| 4283 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4284 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4285 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4286 | "ctrl-alt-f toggle full screen\n" |
| 4287 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4288 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4289 | "\n" |
| 4290 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4291 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4292 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4293 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4294 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4295 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4296 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4297 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4298 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4299 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4300 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4303 | #define HAS_ARG 0x0001 |
| 4304 | |
| 4305 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4306 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4307 | opt_enum, |
| 4308 | #define DEFHEADING(text) |
| 4309 | #include "qemu-options.h" |
| 4310 | #undef DEF |
| 4311 | #undef DEFHEADING |
| 4312 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4313 | }; |
| 4314 | |
| 4315 | typedef struct QEMUOption { |
| 4316 | const char *name; |
| 4317 | int flags; |
| 4318 | int index; |
| 4319 | } QEMUOption; |
| 4320 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4321 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4322 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4323 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4324 | { option, opt_arg, opt_enum }, |
| 4325 | #define DEFHEADING(text) |
| 4326 | #include "qemu-options.h" |
| 4327 | #undef DEF |
| 4328 | #undef DEFHEADING |
| 4329 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4330 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4331 | }; |
| 4332 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4333 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4334 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4335 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4336 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4337 | { |
| 4338 | "pcspk", |
| 4339 | "PC speaker", |
| 4340 | 0, |
| 4341 | 1, |
| 4342 | { .init_isa = pcspk_audio_init } |
| 4343 | }, |
| 4344 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4345 | |
| 4346 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4347 | { |
| 4348 | "sb16", |
| 4349 | "Creative Sound Blaster 16", |
| 4350 | 0, |
| 4351 | 1, |
| 4352 | { .init_isa = SB16_init } |
| 4353 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4354 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4355 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4356 | #ifdef CONFIG_CS4231A |
| 4357 | { |
| 4358 | "cs4231a", |
| 4359 | "CS4231A", |
| 4360 | 0, |
| 4361 | 1, |
| 4362 | { .init_isa = cs4231a_init } |
| 4363 | }, |
| 4364 | #endif |
| 4365 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4366 | #ifdef CONFIG_ADLIB |
| 4367 | { |
| 4368 | "adlib", |
| 4369 | #ifdef HAS_YMF262 |
| 4370 | "Yamaha YMF262 (OPL3)", |
| 4371 | #else |
| 4372 | "Yamaha YM3812 (OPL2)", |
| 4373 | #endif |
| 4374 | 0, |
| 4375 | 1, |
| 4376 | { .init_isa = Adlib_init } |
| 4377 | }, |
| 4378 | #endif |
| 4379 | |
| 4380 | #ifdef CONFIG_GUS |
| 4381 | { |
| 4382 | "gus", |
| 4383 | "Gravis Ultrasound GF1", |
| 4384 | 0, |
| 4385 | 1, |
| 4386 | { .init_isa = GUS_init } |
| 4387 | }, |
| 4388 | #endif |
| 4389 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4390 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4391 | { |
| 4392 | "ac97", |
| 4393 | "Intel 82801AA AC97 Audio", |
| 4394 | 0, |
| 4395 | 0, |
| 4396 | { .init_pci = ac97_init } |
| 4397 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4398 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4399 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4400 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4401 | { |
| 4402 | "es1370", |
| 4403 | "ENSONIQ AudioPCI ES1370", |
| 4404 | 0, |
| 4405 | 0, |
| 4406 | { .init_pci = es1370_init } |
| 4407 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4408 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4409 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4410 | #endif /* HAS_AUDIO_CHOICE */ |
| 4411 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4412 | { NULL, NULL, 0, 0, { NULL } } |
| 4413 | }; |
| 4414 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4415 | static void select_soundhw (const char *optarg) |
| 4416 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4417 | struct soundhw *c; |
| 4418 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4419 | if (*optarg == '?') { |
| 4420 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4421 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4422 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4423 | for (c = soundhw; c->name; ++c) { |
| 4424 | printf ("%-11s %s\n", c->name, c->descr); |
| 4425 | } |
| 4426 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4427 | exit (*optarg != '?'); |
| 4428 | } |
| 4429 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4430 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4431 | const char *p; |
| 4432 | char *e; |
| 4433 | int bad_card = 0; |
| 4434 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4435 | if (!strcmp (optarg, "all")) { |
| 4436 | for (c = soundhw; c->name; ++c) { |
| 4437 | c->enabled = 1; |
| 4438 | } |
| 4439 | return; |
| 4440 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4441 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4442 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4443 | while (*p) { |
| 4444 | e = strchr (p, ','); |
| 4445 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4446 | |
| 4447 | for (c = soundhw; c->name; ++c) { |
malc | b3d6fb4 | 2009-09-06 06:49:03 +0400 | [diff] [blame] | 4448 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4449 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4450 | break; |
| 4451 | } |
| 4452 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4453 | |
| 4454 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4455 | if (l > 80) { |
| 4456 | fprintf (stderr, |
| 4457 | "Unknown sound card name (too big to show)\n"); |
| 4458 | } |
| 4459 | else { |
| 4460 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4461 | (int) l, p); |
| 4462 | } |
| 4463 | bad_card = 1; |
| 4464 | } |
| 4465 | p += l + (e != NULL); |
| 4466 | } |
| 4467 | |
| 4468 | if (bad_card) |
| 4469 | goto show_valid_cards; |
| 4470 | } |
| 4471 | } |
| 4472 | #endif |
| 4473 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4474 | static void select_vgahw (const char *p) |
| 4475 | { |
| 4476 | const char *opts; |
| 4477 | |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4478 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4479 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4480 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4481 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4482 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4483 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4484 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4485 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4486 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4487 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4488 | invalid_vga: |
| 4489 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4490 | exit(1); |
| 4491 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4492 | while (*opts) { |
| 4493 | const char *nextopt; |
| 4494 | |
| 4495 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4496 | opts = nextopt; |
| 4497 | if (strstart(opts, "dumb", &nextopt)) |
| 4498 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4499 | else if (strstart(opts, "precise", &nextopt)) |
| 4500 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4501 | else goto invalid_vga; |
| 4502 | } else goto invalid_vga; |
| 4503 | opts = nextopt; |
| 4504 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4507 | #ifdef TARGET_I386 |
| 4508 | static int balloon_parse(const char *arg) |
| 4509 | { |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4510 | QemuOpts *opts; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4511 | |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4512 | if (strcmp(arg, "none") == 0) { |
| 4513 | return 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4514 | } |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4515 | |
| 4516 | if (!strncmp(arg, "virtio", 6)) { |
| 4517 | if (arg[6] == ',') { |
| 4518 | /* have params -> parse them */ |
| 4519 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
| 4520 | if (!opts) |
| 4521 | return -1; |
| 4522 | } else { |
| 4523 | /* create empty opts */ |
| 4524 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4525 | } |
| 4526 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
| 4527 | return 0; |
| 4528 | } |
| 4529 | |
| 4530 | return -1; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4531 | } |
| 4532 | #endif |
| 4533 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4534 | #ifdef _WIN32 |
| 4535 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4536 | { |
| 4537 | exit(STATUS_CONTROL_C_EXIT); |
| 4538 | return TRUE; |
| 4539 | } |
| 4540 | #endif |
| 4541 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4542 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4543 | { |
| 4544 | int ret; |
| 4545 | |
| 4546 | if(strlen(str) != 36) |
| 4547 | return -1; |
| 4548 | |
| 4549 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4550 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4551 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4552 | |
| 4553 | if(ret != 16) |
| 4554 | return -1; |
| 4555 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4556 | #ifdef TARGET_I386 |
| 4557 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4558 | #endif |
| 4559 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4560 | return 0; |
| 4561 | } |
| 4562 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4563 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4564 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4565 | #ifndef _WIN32 |
| 4566 | |
| 4567 | static void termsig_handler(int signal) |
| 4568 | { |
| 4569 | qemu_system_shutdown_request(); |
| 4570 | } |
| 4571 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4572 | static void sigchld_handler(int signal) |
| 4573 | { |
| 4574 | waitpid(-1, NULL, WNOHANG); |
| 4575 | } |
| 4576 | |
| 4577 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4578 | { |
| 4579 | struct sigaction act; |
| 4580 | |
| 4581 | memset(&act, 0, sizeof(act)); |
| 4582 | act.sa_handler = termsig_handler; |
| 4583 | sigaction(SIGINT, &act, NULL); |
| 4584 | sigaction(SIGHUP, &act, NULL); |
| 4585 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4586 | |
| 4587 | act.sa_handler = sigchld_handler; |
| 4588 | act.sa_flags = SA_NOCLDSTOP; |
| 4589 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | #endif |
| 4593 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4594 | #ifdef _WIN32 |
| 4595 | /* Look for support files in the same directory as the executable. */ |
| 4596 | static char *find_datadir(const char *argv0) |
| 4597 | { |
| 4598 | char *p; |
| 4599 | char buf[MAX_PATH]; |
| 4600 | DWORD len; |
| 4601 | |
| 4602 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4603 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4604 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | buf[len] = 0; |
| 4608 | p = buf + len - 1; |
| 4609 | while (p != buf && *p != '\\') |
| 4610 | p--; |
| 4611 | *p = 0; |
| 4612 | if (access(buf, R_OK) == 0) { |
| 4613 | return qemu_strdup(buf); |
| 4614 | } |
| 4615 | return NULL; |
| 4616 | } |
| 4617 | #else /* !_WIN32 */ |
| 4618 | |
| 4619 | /* Find a likely location for support files using the location of the binary. |
| 4620 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4621 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4622 | #define SHARE_SUFFIX "/share/qemu" |
| 4623 | #define BUILD_SUFFIX "/pc-bios" |
| 4624 | static char *find_datadir(const char *argv0) |
| 4625 | { |
| 4626 | char *dir; |
| 4627 | char *p = NULL; |
| 4628 | char *res; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4629 | char buf[PATH_MAX]; |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4630 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4631 | |
| 4632 | #if defined(__linux__) |
| 4633 | { |
| 4634 | int len; |
| 4635 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4636 | if (len > 0) { |
| 4637 | buf[len] = 0; |
| 4638 | p = buf; |
| 4639 | } |
| 4640 | } |
| 4641 | #elif defined(__FreeBSD__) |
| 4642 | { |
| 4643 | int len; |
| 4644 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4645 | if (len > 0) { |
| 4646 | buf[len] = 0; |
| 4647 | p = buf; |
| 4648 | } |
| 4649 | } |
| 4650 | #endif |
| 4651 | /* If we don't have any way of figuring out the actual executable |
| 4652 | location then try argv[0]. */ |
| 4653 | if (!p) { |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4654 | p = realpath(argv0, buf); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4655 | if (!p) { |
| 4656 | return NULL; |
| 4657 | } |
| 4658 | } |
| 4659 | dir = dirname(p); |
| 4660 | dir = dirname(dir); |
| 4661 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4662 | max_len = strlen(dir) + |
| 4663 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4664 | res = qemu_mallocz(max_len); |
| 4665 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4666 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4667 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4668 | if (access(res, R_OK)) { |
| 4669 | qemu_free(res); |
| 4670 | res = NULL; |
| 4671 | } |
| 4672 | } |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4673 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4674 | return res; |
| 4675 | } |
| 4676 | #undef SHARE_SUFFIX |
| 4677 | #undef BUILD_SUFFIX |
| 4678 | #endif |
| 4679 | |
| 4680 | char *qemu_find_file(int type, const char *name) |
| 4681 | { |
| 4682 | int len; |
| 4683 | const char *subdir; |
| 4684 | char *buf; |
| 4685 | |
| 4686 | /* If name contains path separators then try it as a straight path. */ |
| 4687 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4688 | && access(name, R_OK) == 0) { |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 4689 | return qemu_strdup(name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4690 | } |
| 4691 | switch (type) { |
| 4692 | case QEMU_FILE_TYPE_BIOS: |
| 4693 | subdir = ""; |
| 4694 | break; |
| 4695 | case QEMU_FILE_TYPE_KEYMAP: |
| 4696 | subdir = "keymaps/"; |
| 4697 | break; |
| 4698 | default: |
| 4699 | abort(); |
| 4700 | } |
| 4701 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4702 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4703 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4704 | if (access(buf, R_OK)) { |
| 4705 | qemu_free(buf); |
| 4706 | return NULL; |
| 4707 | } |
| 4708 | return buf; |
| 4709 | } |
| 4710 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4711 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4712 | { |
| 4713 | DeviceState *dev; |
| 4714 | |
| 4715 | dev = qdev_device_add(opts); |
| 4716 | if (!dev) |
| 4717 | return -1; |
| 4718 | return 0; |
| 4719 | } |
| 4720 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4721 | struct device_config { |
| 4722 | enum { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4723 | DEV_USB, /* -usbdevice */ |
| 4724 | DEV_BT, /* -bt */ |
| 4725 | } type; |
| 4726 | const char *cmdline; |
| 4727 | TAILQ_ENTRY(device_config) next; |
| 4728 | }; |
| 4729 | TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs); |
| 4730 | |
| 4731 | static void add_device_config(int type, const char *cmdline) |
| 4732 | { |
| 4733 | struct device_config *conf; |
| 4734 | |
| 4735 | conf = qemu_mallocz(sizeof(*conf)); |
| 4736 | conf->type = type; |
| 4737 | conf->cmdline = cmdline; |
| 4738 | TAILQ_INSERT_TAIL(&device_configs, conf, next); |
| 4739 | } |
| 4740 | |
| 4741 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4742 | { |
| 4743 | struct device_config *conf; |
| 4744 | int rc; |
| 4745 | |
| 4746 | TAILQ_FOREACH(conf, &device_configs, next) { |
| 4747 | if (conf->type != type) |
| 4748 | continue; |
| 4749 | rc = func(conf->cmdline); |
| 4750 | if (0 != rc) |
| 4751 | return rc; |
| 4752 | } |
| 4753 | return 0; |
| 4754 | } |
| 4755 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4756 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4757 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4758 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4759 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4760 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4761 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4762 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4763 | const char *kernel_filename, *kernel_cmdline; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 4764 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4765 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4766 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4767 | int cyls, heads, secs, translation; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4768 | const char *net_clients[MAX_NET_CLIENTS]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4769 | int nb_net_clients; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4770 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4771 | int optind; |
| 4772 | const char *r, *optarg; |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 4773 | CharDriverState *monitor_hds[MAX_MONITOR_DEVICES]; |
| 4774 | const char *monitor_devices[MAX_MONITOR_DEVICES]; |
| 4775 | int monitor_device_index; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4776 | const char *serial_devices[MAX_SERIAL_PORTS]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4777 | int serial_device_index; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4778 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4779 | int parallel_device_index; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4780 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
| 4781 | int virtio_console_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4782 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4783 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4784 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4785 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4786 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4787 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4788 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4789 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4790 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4791 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4792 | int fd = 0; |
| 4793 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4794 | const char *chroot_dir = NULL; |
| 4795 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4796 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4797 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4798 | int show_vnc_port = 0; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4799 | |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 4800 | qemu_errors_to_file(stderr); |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4801 | qemu_cache_utils_init(envp); |
| 4802 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4803 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4804 | #ifndef _WIN32 |
| 4805 | { |
| 4806 | struct sigaction act; |
| 4807 | sigfillset(&act.sa_mask); |
| 4808 | act.sa_flags = 0; |
| 4809 | act.sa_handler = SIG_IGN; |
| 4810 | sigaction(SIGPIPE, &act, NULL); |
| 4811 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4812 | #else |
| 4813 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4814 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4815 | QEMU to run on a single CPU */ |
| 4816 | { |
| 4817 | HANDLE h; |
| 4818 | DWORD mask, smask; |
| 4819 | int i; |
| 4820 | h = GetCurrentProcess(); |
| 4821 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4822 | for(i = 0; i < 32; i++) { |
| 4823 | if (mask & (1 << i)) |
| 4824 | break; |
| 4825 | } |
| 4826 | if (i != 32) { |
| 4827 | mask = 1 << i; |
| 4828 | SetProcessAffinityMask(h, mask); |
| 4829 | } |
| 4830 | } |
| 4831 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4832 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4833 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4834 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4835 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4836 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4837 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4838 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4839 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4840 | kernel_filename = NULL; |
| 4841 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4842 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4843 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4844 | |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 4845 | serial_devices[0] = "vc:80Cx24C"; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4846 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4847 | serial_devices[i] = NULL; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4848 | serial_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4849 | |
aliguori | 8290edd | 2009-02-27 20:14:29 +0000 | [diff] [blame] | 4850 | parallel_devices[0] = "vc:80Cx24C"; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4851 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4852 | parallel_devices[i] = NULL; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4853 | parallel_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4854 | |
aliguori | 1b8fc81 | 2009-02-27 20:01:39 +0000 | [diff] [blame] | 4855 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4856 | virtio_consoles[i] = NULL; |
| 4857 | virtio_console_index = 0; |
| 4858 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 4859 | monitor_devices[0] = "vc:80Cx24C"; |
| 4860 | for (i = 1; i < MAX_MONITOR_DEVICES; i++) { |
| 4861 | monitor_devices[i] = NULL; |
| 4862 | } |
| 4863 | monitor_device_index = 0; |
| 4864 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4865 | for (i = 0; i < MAX_NODES; i++) { |
| 4866 | node_mem[i] = 0; |
| 4867 | node_cpumask[i] = 0; |
| 4868 | } |
| 4869 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4870 | nb_net_clients = 0; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4871 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4872 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4873 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4874 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4875 | autostart= 1; |
| 4876 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4877 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4878 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4879 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4880 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4881 | r = argv[optind]; |
| 4882 | if (r[0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4883 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4884 | } else { |
| 4885 | const QEMUOption *popt; |
| 4886 | |
| 4887 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 4888 | /* Treat --foo the same as -foo. */ |
| 4889 | if (r[1] == '-') |
| 4890 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4891 | popt = qemu_options; |
| 4892 | for(;;) { |
| 4893 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4894 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4895 | argv[0], r); |
| 4896 | exit(1); |
| 4897 | } |
| 4898 | if (!strcmp(popt->name, r + 1)) |
| 4899 | break; |
| 4900 | popt++; |
| 4901 | } |
| 4902 | if (popt->flags & HAS_ARG) { |
| 4903 | if (optind >= argc) { |
| 4904 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4905 | argv[0], r); |
| 4906 | exit(1); |
| 4907 | } |
| 4908 | optarg = argv[optind++]; |
| 4909 | } else { |
| 4910 | optarg = NULL; |
| 4911 | } |
| 4912 | |
| 4913 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4914 | case QEMU_OPTION_M: |
| 4915 | machine = find_machine(optarg); |
| 4916 | if (!machine) { |
| 4917 | QEMUMachine *m; |
| 4918 | printf("Supported machines are:\n"); |
| 4919 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4920 | if (m->alias) |
| 4921 | printf("%-10s %s (alias of %s)\n", |
| 4922 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4923 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4924 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4925 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4926 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4927 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4928 | } |
| 4929 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4930 | case QEMU_OPTION_cpu: |
| 4931 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4932 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 4933 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 4934 | #if defined(cpu_list) |
| 4935 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4936 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4937 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4938 | } else { |
| 4939 | cpu_model = optarg; |
| 4940 | } |
| 4941 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4942 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4943 | initrd_filename = optarg; |
| 4944 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4945 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4946 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4947 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4948 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4949 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4950 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4951 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4952 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 4953 | ",trans=lba" : |
| 4954 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 4955 | ",trans=none" : ""); |
| 4956 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4957 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4958 | case QEMU_OPTION_hdc: |
| 4959 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4960 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4961 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4962 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4963 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4964 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 4965 | case QEMU_OPTION_set: |
| 4966 | if (qemu_set_option(optarg) != 0) |
| 4967 | exit(1); |
| 4968 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4969 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4970 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4971 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4972 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4973 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4974 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4975 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4976 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4977 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4978 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4979 | snapshot = 1; |
| 4980 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4981 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4982 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4983 | const char *p; |
| 4984 | p = optarg; |
| 4985 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4986 | if (cyls < 1 || cyls > 16383) |
| 4987 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4988 | if (*p != ',') |
| 4989 | goto chs_fail; |
| 4990 | p++; |
| 4991 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4992 | if (heads < 1 || heads > 16) |
| 4993 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4994 | if (*p != ',') |
| 4995 | goto chs_fail; |
| 4996 | p++; |
| 4997 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4998 | if (secs < 1 || secs > 63) |
| 4999 | goto chs_fail; |
| 5000 | if (*p == ',') { |
| 5001 | p++; |
| 5002 | if (!strcmp(p, "none")) |
| 5003 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 5004 | else if (!strcmp(p, "lba")) |
| 5005 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 5006 | else if (!strcmp(p, "auto")) |
| 5007 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5008 | else |
| 5009 | goto chs_fail; |
| 5010 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5011 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5012 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 5013 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5014 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5015 | if (hda_opts != NULL) { |
| 5016 | char num[16]; |
| 5017 | snprintf(num, sizeof(num), "%d", cyls); |
| 5018 | qemu_opt_set(hda_opts, "cyls", num); |
| 5019 | snprintf(num, sizeof(num), "%d", heads); |
| 5020 | qemu_opt_set(hda_opts, "heads", num); |
| 5021 | snprintf(num, sizeof(num), "%d", secs); |
| 5022 | qemu_opt_set(hda_opts, "secs", num); |
| 5023 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 5024 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 5025 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 5026 | qemu_opt_set(hda_opts, "trans", "none"); |
| 5027 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5028 | } |
| 5029 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5030 | case QEMU_OPTION_numa: |
| 5031 | if (nb_numa_nodes >= MAX_NODES) { |
| 5032 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 5033 | exit(1); |
| 5034 | } |
| 5035 | numa_add(optarg); |
| 5036 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5037 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5038 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5039 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5040 | #ifdef CONFIG_CURSES |
| 5041 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5042 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5043 | break; |
| 5044 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5045 | case QEMU_OPTION_portrait: |
| 5046 | graphic_rotate = 1; |
| 5047 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5048 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5049 | kernel_filename = optarg; |
| 5050 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5051 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5052 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5053 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5054 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5055 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5056 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5057 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5058 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5059 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5060 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5061 | }; |
| 5062 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5063 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5064 | int legacy = 0; |
| 5065 | |
| 5066 | if (!strchr(optarg, '=')) { |
| 5067 | legacy = 1; |
| 5068 | pstrcpy(buf, sizeof(buf), optarg); |
| 5069 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5070 | fprintf(stderr, |
| 5071 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5072 | buf, optarg); |
| 5073 | exit(1); |
| 5074 | } |
| 5075 | |
| 5076 | if (legacy || |
| 5077 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5078 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5079 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5080 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5081 | if (!legacy) { |
| 5082 | if (get_param_value(buf, sizeof(buf), |
| 5083 | "once", optarg)) { |
| 5084 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5085 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5086 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5087 | qemu_register_reset(restore_boot_devices, |
| 5088 | standard_boot_devices); |
| 5089 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5090 | if (get_param_value(buf, sizeof(buf), |
| 5091 | "menu", optarg)) { |
| 5092 | if (!strcmp(buf, "on")) { |
| 5093 | boot_menu = 1; |
| 5094 | } else if (!strcmp(buf, "off")) { |
| 5095 | boot_menu = 0; |
| 5096 | } else { |
| 5097 | fprintf(stderr, |
| 5098 | "qemu: invalid option value '%s'\n", |
| 5099 | buf); |
| 5100 | exit(1); |
| 5101 | } |
| 5102 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5103 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5104 | } |
| 5105 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5106 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5107 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5108 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5109 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5110 | #ifdef TARGET_I386 |
| 5111 | case QEMU_OPTION_no_fd_bootchk: |
| 5112 | fd_bootchk = 0; |
| 5113 | break; |
| 5114 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5115 | case QEMU_OPTION_net: |
| 5116 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 5117 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5118 | exit(1); |
| 5119 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5120 | net_clients[nb_net_clients] = optarg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5121 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5122 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5123 | #ifdef CONFIG_SLIRP |
| 5124 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5125 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5126 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5127 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5128 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5129 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5130 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5131 | case QEMU_OPTION_smb: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5132 | net_slirp_smb(optarg); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5133 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5134 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5135 | case QEMU_OPTION_redir: |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5136 | net_slirp_redir(optarg); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5137 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5138 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5139 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5140 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5141 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5142 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5143 | case QEMU_OPTION_audio_help: |
| 5144 | AUD_help (); |
| 5145 | exit (0); |
| 5146 | break; |
| 5147 | case QEMU_OPTION_soundhw: |
| 5148 | select_soundhw (optarg); |
| 5149 | break; |
| 5150 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5151 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5152 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5153 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5154 | case QEMU_OPTION_version: |
| 5155 | version(); |
| 5156 | exit(0); |
| 5157 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5158 | case QEMU_OPTION_m: { |
| 5159 | uint64_t value; |
| 5160 | char *ptr; |
| 5161 | |
| 5162 | value = strtoul(optarg, &ptr, 10); |
| 5163 | switch (*ptr) { |
| 5164 | case 0: case 'M': case 'm': |
| 5165 | value <<= 20; |
| 5166 | break; |
| 5167 | case 'G': case 'g': |
| 5168 | value <<= 30; |
| 5169 | break; |
| 5170 | default: |
| 5171 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5172 | exit(1); |
| 5173 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5174 | |
| 5175 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5176 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5177 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5178 | exit(1); |
| 5179 | } |
| 5180 | if (value != (uint64_t)(ram_addr_t)value) { |
| 5181 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5182 | exit(1); |
| 5183 | } |
| 5184 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5185 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5186 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5187 | case QEMU_OPTION_d: |
| 5188 | { |
| 5189 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5190 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5191 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5192 | mask = cpu_str_to_log_mask(optarg); |
| 5193 | if (!mask) { |
| 5194 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5195 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5196 | printf("%-10s %s\n", item->name, item->help); |
| 5197 | } |
| 5198 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5199 | } |
| 5200 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5201 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5202 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5203 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5204 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5205 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5206 | case QEMU_OPTION_gdb: |
| 5207 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5208 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5209 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5210 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5211 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5212 | case QEMU_OPTION_bios: |
| 5213 | bios_name = optarg; |
| 5214 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5215 | case QEMU_OPTION_singlestep: |
| 5216 | singlestep = 1; |
| 5217 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5218 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5219 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5220 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5221 | #ifndef _WIN32 |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5222 | case QEMU_OPTION_k: |
| 5223 | keyboard_layout = optarg; |
| 5224 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5225 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5226 | case QEMU_OPTION_localtime: |
| 5227 | rtc_utc = 0; |
| 5228 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5229 | case QEMU_OPTION_vga: |
| 5230 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5231 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5232 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5233 | case QEMU_OPTION_g: |
| 5234 | { |
| 5235 | const char *p; |
| 5236 | int w, h, depth; |
| 5237 | p = optarg; |
| 5238 | w = strtol(p, (char **)&p, 10); |
| 5239 | if (w <= 0) { |
| 5240 | graphic_error: |
| 5241 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5242 | exit(1); |
| 5243 | } |
| 5244 | if (*p != 'x') |
| 5245 | goto graphic_error; |
| 5246 | p++; |
| 5247 | h = strtol(p, (char **)&p, 10); |
| 5248 | if (h <= 0) |
| 5249 | goto graphic_error; |
| 5250 | if (*p == 'x') { |
| 5251 | p++; |
| 5252 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5253 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5254 | depth != 24 && depth != 32) |
| 5255 | goto graphic_error; |
| 5256 | } else if (*p == '\0') { |
| 5257 | depth = graphic_depth; |
| 5258 | } else { |
| 5259 | goto graphic_error; |
| 5260 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5261 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5262 | graphic_width = w; |
| 5263 | graphic_height = h; |
| 5264 | graphic_depth = depth; |
| 5265 | } |
| 5266 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5267 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5268 | case QEMU_OPTION_echr: |
| 5269 | { |
| 5270 | char *r; |
| 5271 | term_escape_char = strtol(optarg, &r, 0); |
| 5272 | if (r == optarg) |
| 5273 | printf("Bad argument to echr\n"); |
| 5274 | break; |
| 5275 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5276 | case QEMU_OPTION_monitor: |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5277 | if (monitor_device_index >= MAX_MONITOR_DEVICES) { |
| 5278 | fprintf(stderr, "qemu: too many monitor devices\n"); |
| 5279 | exit(1); |
| 5280 | } |
| 5281 | monitor_devices[monitor_device_index] = optarg; |
| 5282 | monitor_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5283 | break; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame^] | 5284 | case QEMU_OPTION_chardev: |
| 5285 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend"); |
| 5286 | if (!opts) { |
| 5287 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5288 | exit(1); |
| 5289 | } |
| 5290 | if (NULL == qemu_chr_open_opts(opts, NULL)) { |
| 5291 | exit(1); |
| 5292 | } |
| 5293 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5294 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5295 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 5296 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 5297 | exit(1); |
| 5298 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5299 | serial_devices[serial_device_index] = optarg; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5300 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5301 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5302 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5303 | if (watchdog) { |
| 5304 | fprintf(stderr, |
| 5305 | "qemu: only one watchdog option may be given\n"); |
| 5306 | return 1; |
| 5307 | } |
| 5308 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5309 | break; |
| 5310 | case QEMU_OPTION_watchdog_action: |
| 5311 | if (select_watchdog_action(optarg) == -1) { |
| 5312 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5313 | exit(1); |
| 5314 | } |
| 5315 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5316 | case QEMU_OPTION_virtiocon: |
| 5317 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { |
| 5318 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 5319 | exit(1); |
| 5320 | } |
| 5321 | virtio_consoles[virtio_console_index] = optarg; |
| 5322 | virtio_console_index++; |
| 5323 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5324 | case QEMU_OPTION_parallel: |
| 5325 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 5326 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 5327 | exit(1); |
| 5328 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5329 | parallel_devices[parallel_device_index] = optarg; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5330 | parallel_device_index++; |
| 5331 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5332 | case QEMU_OPTION_loadvm: |
| 5333 | loadvm = optarg; |
| 5334 | break; |
| 5335 | case QEMU_OPTION_full_screen: |
| 5336 | full_screen = 1; |
| 5337 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5338 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5339 | case QEMU_OPTION_no_frame: |
| 5340 | no_frame = 1; |
| 5341 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5342 | case QEMU_OPTION_alt_grab: |
| 5343 | alt_grab = 1; |
| 5344 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5345 | case QEMU_OPTION_no_quit: |
| 5346 | no_quit = 1; |
| 5347 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5348 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5349 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5350 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5351 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5352 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5353 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5354 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5355 | #ifdef TARGET_I386 |
| 5356 | case QEMU_OPTION_win2k_hack: |
| 5357 | win2k_install_hack = 1; |
| 5358 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5359 | case QEMU_OPTION_rtc_td_hack: |
| 5360 | rtc_td_hack = 1; |
| 5361 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5362 | case QEMU_OPTION_acpitable: |
| 5363 | if(acpi_table_add(optarg) < 0) { |
| 5364 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5365 | exit(1); |
| 5366 | } |
| 5367 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5368 | case QEMU_OPTION_smbios: |
| 5369 | if(smbios_entry_add(optarg) < 0) { |
| 5370 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5371 | exit(1); |
| 5372 | } |
| 5373 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5374 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5375 | #ifdef CONFIG_KVM |
| 5376 | case QEMU_OPTION_enable_kvm: |
| 5377 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5378 | break; |
| 5379 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5380 | case QEMU_OPTION_usb: |
| 5381 | usb_enabled = 1; |
| 5382 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5383 | case QEMU_OPTION_usbdevice: |
| 5384 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5385 | add_device_config(DEV_USB, optarg); |
| 5386 | break; |
| 5387 | case QEMU_OPTION_device: |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5388 | opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver"); |
| 5389 | if (!opts) { |
| 5390 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5391 | exit(1); |
| 5392 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5393 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5394 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5395 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5396 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5397 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5398 | exit(1); |
| 5399 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5400 | if (max_cpus < smp_cpus) { |
| 5401 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5402 | "smp\n"); |
| 5403 | exit(1); |
| 5404 | } |
| 5405 | if (max_cpus > 255) { |
| 5406 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5407 | exit(1); |
| 5408 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5409 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5410 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5411 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5412 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5413 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5414 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5415 | case QEMU_OPTION_no_acpi: |
| 5416 | acpi_enabled = 0; |
| 5417 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5418 | case QEMU_OPTION_no_hpet: |
| 5419 | no_hpet = 1; |
| 5420 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5421 | case QEMU_OPTION_balloon: |
| 5422 | if (balloon_parse(optarg) < 0) { |
| 5423 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5424 | exit(1); |
| 5425 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5426 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5427 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5428 | case QEMU_OPTION_no_reboot: |
| 5429 | no_reboot = 1; |
| 5430 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5431 | case QEMU_OPTION_no_shutdown: |
| 5432 | no_shutdown = 1; |
| 5433 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5434 | case QEMU_OPTION_show_cursor: |
| 5435 | cursor_hide = 0; |
| 5436 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5437 | case QEMU_OPTION_uuid: |
| 5438 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5439 | fprintf(stderr, "Fail to parse UUID string." |
| 5440 | " Wrong format.\n"); |
| 5441 | exit(1); |
| 5442 | } |
| 5443 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5444 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5445 | case QEMU_OPTION_daemonize: |
| 5446 | daemonize = 1; |
| 5447 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5448 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5449 | case QEMU_OPTION_option_rom: |
| 5450 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5451 | fprintf(stderr, "Too many option ROMs\n"); |
| 5452 | exit(1); |
| 5453 | } |
| 5454 | option_rom[nb_option_roms] = optarg; |
| 5455 | nb_option_roms++; |
| 5456 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5457 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5458 | case QEMU_OPTION_semihosting: |
| 5459 | semihosting_enabled = 1; |
| 5460 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5461 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5462 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5463 | qemu_name = qemu_strdup(optarg); |
| 5464 | { |
| 5465 | char *p = strchr(qemu_name, ','); |
| 5466 | if (p != NULL) { |
| 5467 | *p++ = 0; |
| 5468 | if (strncmp(p, "process=", 8)) { |
| 5469 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5470 | exit(1); |
| 5471 | } |
| 5472 | p += 8; |
| 5473 | set_proc_name(p); |
| 5474 | } |
| 5475 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5476 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5477 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5478 | case QEMU_OPTION_prom_env: |
| 5479 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5480 | fprintf(stderr, "Too many prom variables\n"); |
| 5481 | exit(1); |
| 5482 | } |
| 5483 | prom_envs[nb_prom_envs] = optarg; |
| 5484 | nb_prom_envs++; |
| 5485 | break; |
| 5486 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5487 | #ifdef TARGET_ARM |
| 5488 | case QEMU_OPTION_old_param: |
| 5489 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5490 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5491 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5492 | case QEMU_OPTION_clock: |
| 5493 | configure_alarms(optarg); |
| 5494 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5495 | case QEMU_OPTION_startdate: |
| 5496 | { |
| 5497 | struct tm tm; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5498 | time_t rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5499 | if (!strcmp(optarg, "now")) { |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5500 | rtc_date_offset = -1; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5501 | } else { |
| 5502 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
| 5503 | &tm.tm_year, |
| 5504 | &tm.tm_mon, |
| 5505 | &tm.tm_mday, |
| 5506 | &tm.tm_hour, |
| 5507 | &tm.tm_min, |
| 5508 | &tm.tm_sec) == 6) { |
| 5509 | /* OK */ |
| 5510 | } else if (sscanf(optarg, "%d-%d-%d", |
| 5511 | &tm.tm_year, |
| 5512 | &tm.tm_mon, |
| 5513 | &tm.tm_mday) == 3) { |
| 5514 | tm.tm_hour = 0; |
| 5515 | tm.tm_min = 0; |
| 5516 | tm.tm_sec = 0; |
| 5517 | } else { |
| 5518 | goto date_fail; |
| 5519 | } |
| 5520 | tm.tm_year -= 1900; |
| 5521 | tm.tm_mon--; |
bellard | 3c6b208 | 2007-11-10 19:36:39 +0000 | [diff] [blame] | 5522 | rtc_start_date = mktimegm(&tm); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5523 | if (rtc_start_date == -1) { |
| 5524 | date_fail: |
| 5525 | fprintf(stderr, "Invalid date format. Valid format are:\n" |
| 5526 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 5527 | exit(1); |
| 5528 | } |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5529 | rtc_date_offset = time(NULL) - rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5533 | case QEMU_OPTION_tb_size: |
| 5534 | tb_size = strtol(optarg, NULL, 0); |
| 5535 | if (tb_size < 0) |
| 5536 | tb_size = 0; |
| 5537 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5538 | case QEMU_OPTION_icount: |
| 5539 | use_icount = 1; |
| 5540 | if (strcmp(optarg, "auto") == 0) { |
| 5541 | icount_time_shift = -1; |
| 5542 | } else { |
| 5543 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5544 | } |
| 5545 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5546 | case QEMU_OPTION_incoming: |
| 5547 | incoming = optarg; |
| 5548 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5549 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5550 | case QEMU_OPTION_chroot: |
| 5551 | chroot_dir = optarg; |
| 5552 | break; |
| 5553 | case QEMU_OPTION_runas: |
| 5554 | run_as = optarg; |
| 5555 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5556 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5557 | #ifdef CONFIG_XEN |
| 5558 | case QEMU_OPTION_xen_domid: |
| 5559 | xen_domid = atoi(optarg); |
| 5560 | break; |
| 5561 | case QEMU_OPTION_xen_create: |
| 5562 | xen_mode = XEN_CREATE; |
| 5563 | break; |
| 5564 | case QEMU_OPTION_xen_attach: |
| 5565 | xen_mode = XEN_ATTACH; |
| 5566 | break; |
| 5567 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5568 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5569 | } |
| 5570 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5571 | |
Dustin Kirkland | e09a526 | 2009-09-03 12:31:33 -0500 | [diff] [blame] | 5572 | if (kvm_enabled()) { |
| 5573 | int ret; |
| 5574 | |
| 5575 | ret = kvm_init(smp_cpus); |
| 5576 | if (ret < 0) { |
| 5577 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5578 | exit(1); |
| 5579 | } |
| 5580 | } |
| 5581 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5582 | /* If no data_dir is specified then try to find it relative to the |
| 5583 | executable path. */ |
| 5584 | if (!data_dir) { |
| 5585 | data_dir = find_datadir(argv[0]); |
| 5586 | } |
| 5587 | /* If all else fails use the install patch specified when building. */ |
| 5588 | if (!data_dir) { |
| 5589 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5590 | } |
| 5591 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5592 | /* |
| 5593 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5594 | * specify a max_cpus value. |
| 5595 | */ |
| 5596 | if (!max_cpus) |
| 5597 | max_cpus = smp_cpus; |
| 5598 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5599 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5600 | if (smp_cpus > machine->max_cpus) { |
| 5601 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5602 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5603 | machine->max_cpus); |
| 5604 | exit(1); |
| 5605 | } |
| 5606 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5607 | if (display_type == DT_NOGRAPHIC) { |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5608 | if (serial_device_index == 0) |
| 5609 | serial_devices[0] = "stdio"; |
| 5610 | if (parallel_device_index == 0) |
| 5611 | parallel_devices[0] = "null"; |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5612 | if (strncmp(monitor_devices[0], "vc", 2) == 0) { |
| 5613 | monitor_devices[0] = "stdio"; |
| 5614 | } |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5617 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5618 | if (daemonize) { |
| 5619 | pid_t pid; |
| 5620 | |
| 5621 | if (pipe(fds) == -1) |
| 5622 | exit(1); |
| 5623 | |
| 5624 | pid = fork(); |
| 5625 | if (pid > 0) { |
| 5626 | uint8_t status; |
| 5627 | ssize_t len; |
| 5628 | |
| 5629 | close(fds[1]); |
| 5630 | |
| 5631 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5632 | len = read(fds[0], &status, 1); |
| 5633 | if (len == -1 && (errno == EINTR)) |
| 5634 | goto again; |
| 5635 | |
| 5636 | if (len != 1) |
| 5637 | exit(1); |
| 5638 | else if (status == 1) { |
| 5639 | fprintf(stderr, "Could not acquire pidfile\n"); |
| 5640 | exit(1); |
| 5641 | } else |
| 5642 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5643 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5644 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5645 | |
| 5646 | setsid(); |
| 5647 | |
| 5648 | pid = fork(); |
| 5649 | if (pid > 0) |
| 5650 | exit(0); |
| 5651 | else if (pid < 0) |
| 5652 | exit(1); |
| 5653 | |
| 5654 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5655 | |
| 5656 | signal(SIGTSTP, SIG_IGN); |
| 5657 | signal(SIGTTOU, SIG_IGN); |
| 5658 | signal(SIGTTIN, SIG_IGN); |
| 5659 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5660 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5661 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5662 | if (daemonize) { |
| 5663 | uint8_t status = 1; |
| 5664 | write(fds[1], &status, 1); |
| 5665 | } else |
| 5666 | fprintf(stderr, "Could not acquire pid file\n"); |
| 5667 | exit(1); |
| 5668 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5669 | #endif |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5670 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5671 | if (qemu_init_main_loop()) { |
| 5672 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5673 | exit(1); |
| 5674 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5675 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5676 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5677 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5678 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5679 | exit(1); |
| 5680 | } |
| 5681 | |
| 5682 | if (!linux_boot && initrd_filename != NULL) { |
| 5683 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5684 | exit(1); |
| 5685 | } |
| 5686 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5687 | #ifndef _WIN32 |
| 5688 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5689 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5690 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5691 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5692 | init_timers(); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5693 | if (init_timer_alarm() < 0) { |
| 5694 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5695 | exit(1); |
| 5696 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5697 | if (use_icount && icount_time_shift < 0) { |
| 5698 | use_icount = 2; |
| 5699 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5700 | It will be corrected fairly quickly anyway. */ |
| 5701 | icount_time_shift = 3; |
| 5702 | init_icount_adjust(); |
| 5703 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5704 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5705 | #ifdef _WIN32 |
| 5706 | socket_init(); |
| 5707 | #endif |
| 5708 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5709 | /* init network clients */ |
| 5710 | if (nb_net_clients == 0) { |
| 5711 | /* if no clients, we use a default config */ |
aliguori | f441b28 | 2008-08-28 20:05:14 +0000 | [diff] [blame] | 5712 | net_clients[nb_net_clients++] = "nic"; |
| 5713 | #ifdef CONFIG_SLIRP |
| 5714 | net_clients[nb_net_clients++] = "user"; |
| 5715 | #endif |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5718 | for(i = 0;i < nb_net_clients; i++) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 5719 | if (net_client_parse(net_clients[i]) < 0) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5720 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5721 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5722 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5723 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5724 | net_set_boot_mask(net_boot); |
| 5725 | |
| 5726 | net_client_check(); |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 5727 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5728 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5729 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5730 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5731 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5732 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5733 | if (ram_size == 0) |
| 5734 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5735 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5736 | /* init the dynamic translator */ |
| 5737 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5738 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 5739 | bdrv_init(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5740 | |
| 5741 | /* we always create the cdrom drive, even if no disk is there */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5742 | drive_add(NULL, CDROM_ALIAS); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5743 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5744 | /* we always create at least one floppy */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5745 | drive_add(NULL, FD_ALIAS, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5746 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5747 | /* we always create one sd slot, even if no card is in it */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5748 | drive_add(NULL, SD_ALIAS); |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5749 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5750 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5751 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5752 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5753 | if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5754 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5755 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5756 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 5757 | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5758 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5759 | /* Maintain compatibility with multiple stdio monitors */ |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5760 | if (!strcmp(monitor_devices[0],"stdio")) { |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5761 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5762 | const char *devname = serial_devices[i]; |
| 5763 | if (devname && !strcmp(devname,"mon:stdio")) { |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5764 | monitor_devices[0] = NULL; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5765 | break; |
| 5766 | } else if (devname && !strcmp(devname,"stdio")) { |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5767 | monitor_devices[0] = NULL; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5768 | serial_devices[i] = "mon:stdio"; |
| 5769 | break; |
| 5770 | } |
| 5771 | } |
| 5772 | } |
| 5773 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5774 | if (nb_numa_nodes > 0) { |
| 5775 | int i; |
| 5776 | |
| 5777 | if (nb_numa_nodes > smp_cpus) { |
| 5778 | nb_numa_nodes = smp_cpus; |
| 5779 | } |
| 5780 | |
| 5781 | /* If no memory size if given for any node, assume the default case |
| 5782 | * and distribute the available memory equally across all nodes |
| 5783 | */ |
| 5784 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5785 | if (node_mem[i] != 0) |
| 5786 | break; |
| 5787 | } |
| 5788 | if (i == nb_numa_nodes) { |
| 5789 | uint64_t usedmem = 0; |
| 5790 | |
| 5791 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5792 | * the final node gets the rest. |
| 5793 | */ |
| 5794 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5795 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5796 | usedmem += node_mem[i]; |
| 5797 | } |
| 5798 | node_mem[i] = ram_size - usedmem; |
| 5799 | } |
| 5800 | |
| 5801 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5802 | if (node_cpumask[i] != 0) |
| 5803 | break; |
| 5804 | } |
| 5805 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5806 | * must cope with this anyway, because there are BIOSes out there in |
| 5807 | * real machines which also use this scheme. |
| 5808 | */ |
| 5809 | if (i == nb_numa_nodes) { |
| 5810 | for (i = 0; i < smp_cpus; i++) { |
| 5811 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5812 | } |
| 5813 | } |
| 5814 | } |
| 5815 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5816 | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
| 5817 | const char *devname = monitor_devices[i]; |
| 5818 | if (devname && strcmp(devname, "none")) { |
| 5819 | char label[32]; |
| 5820 | if (i == 0) { |
| 5821 | snprintf(label, sizeof(label), "monitor"); |
| 5822 | } else { |
| 5823 | snprintf(label, sizeof(label), "monitor%d", i); |
| 5824 | } |
| 5825 | monitor_hds[i] = qemu_chr_open(label, devname, NULL); |
| 5826 | if (!monitor_hds[i]) { |
| 5827 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", |
| 5828 | devname); |
| 5829 | exit(1); |
| 5830 | } |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 5831 | } |
| 5832 | } |
| 5833 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5834 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5835 | const char *devname = serial_devices[i]; |
| 5836 | if (devname && strcmp(devname, "none")) { |
| 5837 | char label[32]; |
| 5838 | snprintf(label, sizeof(label), "serial%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5839 | serial_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5840 | if (!serial_hds[i]) { |
| 5841 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
| 5842 | devname); |
| 5843 | exit(1); |
| 5844 | } |
| 5845 | } |
| 5846 | } |
| 5847 | |
| 5848 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
| 5849 | const char *devname = parallel_devices[i]; |
| 5850 | if (devname && strcmp(devname, "none")) { |
| 5851 | char label[32]; |
| 5852 | snprintf(label, sizeof(label), "parallel%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5853 | parallel_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5854 | if (!parallel_hds[i]) { |
| 5855 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
| 5856 | devname); |
| 5857 | exit(1); |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 5863 | const char *devname = virtio_consoles[i]; |
| 5864 | if (devname && strcmp(devname, "none")) { |
| 5865 | char label[32]; |
| 5866 | snprintf(label, sizeof(label), "virtcon%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5867 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5868 | if (!virtcon_hds[i]) { |
| 5869 | fprintf(stderr, "qemu: could not open virtio console '%s'\n", |
| 5870 | devname); |
| 5871 | exit(1); |
| 5872 | } |
| 5873 | } |
| 5874 | } |
| 5875 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5876 | module_call_init(MODULE_INIT_DEVICE); |
| 5877 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5878 | if (watchdog) { |
| 5879 | i = select_watchdog(watchdog); |
| 5880 | if (i > 0) |
| 5881 | exit (i == 1 ? 1 : 0); |
| 5882 | } |
| 5883 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5884 | if (machine->compat_props) { |
| 5885 | qdev_prop_register_compat(machine->compat_props); |
| 5886 | } |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5887 | machine->init(ram_size, boot_devices, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5888 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5889 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5890 | |
Juan Quintela | 67b3b71 | 2009-08-28 19:25:15 +0200 | [diff] [blame] | 5891 | #ifndef _WIN32 |
| 5892 | /* must be after terminal init, SDL library changes signal handlers */ |
| 5893 | sighandler_setup(); |
| 5894 | #endif |
| 5895 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5896 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5897 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5898 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5899 | env->numa_node = i; |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5904 | current_machine = machine; |
| 5905 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5906 | /* init USB devices */ |
| 5907 | if (usb_enabled) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5908 | foreach_device_config(DEV_USB, usb_parse); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5911 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5912 | if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5913 | exit(1); |
| 5914 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5915 | if (!display_state) |
| 5916 | dumb_display_init(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5917 | /* just use the first displaystate for the moment */ |
| 5918 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5919 | |
| 5920 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5921 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5922 | display_type = DT_SDL; |
| 5923 | #else |
| 5924 | display_type = DT_VNC; |
| 5925 | vnc_display = "localhost:0,to=99"; |
| 5926 | show_vnc_port = 1; |
| 5927 | #endif |
| 5928 | } |
| 5929 | |
| 5930 | |
| 5931 | switch (display_type) { |
| 5932 | case DT_NOGRAPHIC: |
| 5933 | break; |
| 5934 | #if defined(CONFIG_CURSES) |
| 5935 | case DT_CURSES: |
| 5936 | curses_display_init(ds, full_screen); |
| 5937 | break; |
| 5938 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5939 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5940 | case DT_SDL: |
| 5941 | sdl_display_init(ds, full_screen, no_frame); |
| 5942 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5943 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5944 | case DT_SDL: |
| 5945 | cocoa_display_init(ds, full_screen); |
| 5946 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5947 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5948 | case DT_VNC: |
| 5949 | vnc_display_init(ds); |
| 5950 | if (vnc_display_open(ds, vnc_display) < 0) |
| 5951 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5952 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5953 | if (show_vnc_port) { |
| 5954 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5955 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5956 | break; |
| 5957 | default: |
| 5958 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5959 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5960 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 5961 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5962 | dcl = ds->listeners; |
| 5963 | while (dcl != NULL) { |
| 5964 | if (dcl->dpy_refresh != NULL) { |
| 5965 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 5966 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5967 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5968 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5969 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5970 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5971 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 5972 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 5973 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 5974 | } |
| 5975 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5976 | text_consoles_set_display(display_state); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 5977 | qemu_chr_initial_reset(); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5978 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5979 | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
| 5980 | if (monitor_devices[i] && monitor_hds[i]) { |
| 5981 | monitor_init(monitor_hds[i], |
| 5982 | MONITOR_USE_READLINE | |
| 5983 | ((i == 0) ? MONITOR_IS_DEFAULT : 0)); |
| 5984 | } |
| 5985 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5986 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5987 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 5988 | const char *devname = serial_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5989 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 5990 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 5991 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5992 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5993 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5994 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5995 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 5996 | const char *devname = parallel_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5997 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 5998 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 5999 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
| 6002 | |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6003 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 6004 | const char *devname = virtio_consoles[i]; |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6005 | if (virtcon_hds[i] && devname) { |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6006 | if (strstart(devname, "vc", 0)) |
| 6007 | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i); |
| 6008 | } |
| 6009 | } |
| 6010 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 6011 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 6012 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 6013 | gdbstub_dev); |
| 6014 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6015 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6016 | |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 6017 | if (loadvm) { |
| 6018 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 6019 | autostart = 0; |
| 6020 | } |
| 6021 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6022 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 6023 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 6024 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6025 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 6026 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6027 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 6028 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6029 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6030 | if (daemonize) { |
| 6031 | uint8_t status = 0; |
| 6032 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6033 | |
| 6034 | again1: |
| 6035 | len = write(fds[1], &status, 1); |
| 6036 | if (len == -1 && (errno == EINTR)) |
| 6037 | goto again1; |
| 6038 | |
| 6039 | if (len != 1) |
| 6040 | exit(1); |
| 6041 | |
aliguori | bd54b86 | 2008-07-23 00:58:33 +0000 | [diff] [blame] | 6042 | chdir("/"); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 6043 | TFR(fd = open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6044 | if (fd == -1) |
| 6045 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6046 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6047 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6048 | if (run_as) { |
| 6049 | pwd = getpwnam(run_as); |
| 6050 | if (!pwd) { |
| 6051 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 6052 | exit(1); |
| 6053 | } |
| 6054 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6055 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6056 | if (chroot_dir) { |
| 6057 | if (chroot(chroot_dir) < 0) { |
| 6058 | fprintf(stderr, "chroot failed\n"); |
| 6059 | exit(1); |
| 6060 | } |
| 6061 | chdir("/"); |
| 6062 | } |
| 6063 | |
| 6064 | if (run_as) { |
| 6065 | if (setgid(pwd->pw_gid) < 0) { |
| 6066 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 6067 | exit(1); |
| 6068 | } |
| 6069 | if (setuid(pwd->pw_uid) < 0) { |
| 6070 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 6071 | exit(1); |
| 6072 | } |
| 6073 | if (setuid(0) != -1) { |
| 6074 | fprintf(stderr, "Dropping privileges failed\n"); |
| 6075 | exit(1); |
| 6076 | } |
| 6077 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6078 | |
| 6079 | if (daemonize) { |
| 6080 | dup2(fd, 0); |
| 6081 | dup2(fd, 1); |
| 6082 | dup2(fd, 2); |
| 6083 | |
| 6084 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6085 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6086 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6087 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6088 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 6089 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 6090 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 6091 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6092 | return 0; |
| 6093 | } |