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 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 176 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 177 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 178 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 179 | to store the VM snapshots */ |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 180 | struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives); |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 181 | struct driveoptlist driveopts = TAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 182 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 183 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 184 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 185 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 186 | int64_t ticks_per_sec; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 187 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 188 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 189 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 190 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 191 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 192 | static int rtc_utc = 1; |
| 193 | static int rtc_date_offset = -1; /* -1 means no change */ |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 194 | int vga_interface_type = VGA_CIRRUS; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 195 | #ifdef TARGET_SPARC |
| 196 | int graphic_width = 1024; |
| 197 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 198 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 199 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 200 | int graphic_width = 800; |
| 201 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 202 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 203 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 204 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 206 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 207 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 208 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 209 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 210 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 211 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 212 | #ifdef TARGET_I386 |
| 213 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 214 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 215 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 216 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 217 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 218 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 219 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 220 | int smp_cores = 1; |
| 221 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 222 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 223 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 224 | int no_hpet = 0; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 225 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 226 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 227 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 228 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 229 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 230 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 231 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 232 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 233 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 234 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 235 | const char *option_rom[MAX_OPTION_ROMS]; |
| 236 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 237 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 238 | #ifdef TARGET_ARM |
| 239 | int old_param = 0; |
| 240 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 241 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 242 | int alt_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 243 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 244 | unsigned int nb_prom_envs = 0; |
| 245 | const char *prom_envs[MAX_PROM_ENVS]; |
| 246 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 247 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 248 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 249 | int nb_numa_nodes; |
| 250 | uint64_t node_mem[MAX_NODES]; |
| 251 | uint64_t node_cpumask[MAX_NODES]; |
| 252 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 253 | static CPUState *cur_cpu; |
| 254 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 255 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 256 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 257 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 258 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 259 | #define MAX_ICOUNT_SHIFT 10 |
| 260 | /* Compensate for varying guest execution speed. */ |
| 261 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 262 | static QEMUTimer *icount_rt_timer; |
| 263 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 264 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 265 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 266 | uint8_t qemu_uuid[16]; |
| 267 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 268 | static QEMUBootSetHandler *boot_set_handler; |
| 269 | static void *boot_set_opaque; |
| 270 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 271 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 272 | /* x86 ISA bus support */ |
| 273 | |
| 274 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 275 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 276 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 277 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 278 | void hw_error(const char *fmt, ...) |
| 279 | { |
| 280 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 281 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 282 | |
| 283 | va_start(ap, fmt); |
| 284 | fprintf(stderr, "qemu: hardware error: "); |
| 285 | vfprintf(stderr, fmt, ap); |
| 286 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 287 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 288 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 289 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 290 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 291 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 292 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 293 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 294 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 295 | va_end(ap); |
| 296 | abort(); |
| 297 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 298 | |
| 299 | static void set_proc_name(const char *s) |
| 300 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 301 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 302 | char name[16]; |
| 303 | if (!s) |
| 304 | return; |
| 305 | name[sizeof(name) - 1] = 0; |
| 306 | strncpy(name, s, sizeof(name)); |
| 307 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 308 | This simple way is enough for `top'. */ |
| 309 | prctl(PR_SET_NAME, name); |
| 310 | #endif |
| 311 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 312 | |
| 313 | /***************/ |
| 314 | /* ballooning */ |
| 315 | |
| 316 | static QEMUBalloonEvent *qemu_balloon_event; |
| 317 | void *qemu_balloon_event_opaque; |
| 318 | |
| 319 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 320 | { |
| 321 | qemu_balloon_event = func; |
| 322 | qemu_balloon_event_opaque = opaque; |
| 323 | } |
| 324 | |
| 325 | void qemu_balloon(ram_addr_t target) |
| 326 | { |
| 327 | if (qemu_balloon_event) |
| 328 | qemu_balloon_event(qemu_balloon_event_opaque, target); |
| 329 | } |
| 330 | |
| 331 | ram_addr_t qemu_balloon_status(void) |
| 332 | { |
| 333 | if (qemu_balloon_event) |
| 334 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
| 335 | return 0; |
| 336 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 337 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 338 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 339 | /* keyboard/mouse */ |
| 340 | |
| 341 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 342 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 343 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 344 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 345 | |
| 346 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 347 | { |
| 348 | qemu_put_kbd_event_opaque = opaque; |
| 349 | qemu_put_kbd_event = func; |
| 350 | } |
| 351 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 352 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 353 | void *opaque, int absolute, |
| 354 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 355 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 356 | QEMUPutMouseEntry *s, *cursor; |
| 357 | |
| 358 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 359 | |
| 360 | s->qemu_put_mouse_event = func; |
| 361 | s->qemu_put_mouse_event_opaque = opaque; |
| 362 | s->qemu_put_mouse_event_absolute = absolute; |
| 363 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 364 | s->next = NULL; |
| 365 | |
| 366 | if (!qemu_put_mouse_event_head) { |
| 367 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 368 | return s; |
| 369 | } |
| 370 | |
| 371 | cursor = qemu_put_mouse_event_head; |
| 372 | while (cursor->next != NULL) |
| 373 | cursor = cursor->next; |
| 374 | |
| 375 | cursor->next = s; |
| 376 | qemu_put_mouse_event_current = s; |
| 377 | |
| 378 | return s; |
| 379 | } |
| 380 | |
| 381 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 382 | { |
| 383 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 384 | |
| 385 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 386 | return; |
| 387 | |
| 388 | cursor = qemu_put_mouse_event_head; |
| 389 | while (cursor != NULL && cursor != entry) { |
| 390 | prev = cursor; |
| 391 | cursor = cursor->next; |
| 392 | } |
| 393 | |
| 394 | if (cursor == NULL) // does not exist or list empty |
| 395 | return; |
| 396 | else if (prev == NULL) { // entry is head |
| 397 | qemu_put_mouse_event_head = cursor->next; |
| 398 | if (qemu_put_mouse_event_current == entry) |
| 399 | qemu_put_mouse_event_current = cursor->next; |
| 400 | qemu_free(entry->qemu_put_mouse_event_name); |
| 401 | qemu_free(entry); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | prev->next = entry->next; |
| 406 | |
| 407 | if (qemu_put_mouse_event_current == entry) |
| 408 | qemu_put_mouse_event_current = prev; |
| 409 | |
| 410 | qemu_free(entry->qemu_put_mouse_event_name); |
| 411 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void kbd_put_keycode(int keycode) |
| 415 | { |
| 416 | if (qemu_put_kbd_event) { |
| 417 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 422 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 423 | QEMUPutMouseEvent *mouse_event; |
| 424 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 425 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 426 | |
| 427 | if (!qemu_put_mouse_event_current) { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | mouse_event = |
| 432 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 433 | mouse_event_opaque = |
| 434 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 435 | |
| 436 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 437 | if (graphic_rotate) { |
| 438 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 439 | width = 0x7fff; |
| 440 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 441 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 442 | mouse_event(mouse_event_opaque, |
| 443 | width - dy, dx, dz, buttons_state); |
| 444 | } else |
| 445 | mouse_event(mouse_event_opaque, |
| 446 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 450 | int kbd_mouse_is_absolute(void) |
| 451 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 452 | if (!qemu_put_mouse_event_current) |
| 453 | return 0; |
| 454 | |
| 455 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 456 | } |
| 457 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 458 | void do_info_mice(Monitor *mon) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 459 | { |
| 460 | QEMUPutMouseEntry *cursor; |
| 461 | int index = 0; |
| 462 | |
| 463 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 464 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 468 | monitor_printf(mon, "Mouse devices available:\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 469 | cursor = qemu_put_mouse_event_head; |
| 470 | while (cursor != NULL) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 471 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
| 472 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 473 | index, cursor->qemu_put_mouse_event_name); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 474 | index++; |
| 475 | cursor = cursor->next; |
| 476 | } |
| 477 | } |
| 478 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 479 | void do_mouse_set(Monitor *mon, int index) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 480 | { |
| 481 | QEMUPutMouseEntry *cursor; |
| 482 | int i = 0; |
| 483 | |
| 484 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 485 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | |
| 489 | cursor = qemu_put_mouse_event_head; |
| 490 | while (cursor != NULL && index != i) { |
| 491 | i++; |
| 492 | cursor = cursor->next; |
| 493 | } |
| 494 | |
| 495 | if (cursor != NULL) |
| 496 | qemu_put_mouse_event_current = cursor; |
| 497 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 498 | monitor_printf(mon, "Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 499 | } |
| 500 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 501 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 502 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 503 | { |
| 504 | union { |
| 505 | uint64_t ll; |
| 506 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 507 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 508 | uint32_t high, low; |
| 509 | #else |
| 510 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 511 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 512 | } l; |
| 513 | } u, res; |
| 514 | uint64_t rl, rh; |
| 515 | |
| 516 | u.ll = a; |
| 517 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 518 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 519 | rh += (rl >> 32); |
| 520 | res.l.high = rh / c; |
| 521 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 522 | return res.ll; |
| 523 | } |
| 524 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 525 | /***********************************************************/ |
| 526 | /* real time host monotonic timer */ |
| 527 | |
| 528 | #define QEMU_TIMER_BASE 1000000000LL |
| 529 | |
| 530 | #ifdef WIN32 |
| 531 | |
| 532 | static int64_t clock_freq; |
| 533 | |
| 534 | static void init_get_clock(void) |
| 535 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 536 | LARGE_INTEGER freq; |
| 537 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 538 | ret = QueryPerformanceFrequency(&freq); |
| 539 | if (ret == 0) { |
| 540 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 541 | exit(1); |
| 542 | } |
| 543 | clock_freq = freq.QuadPart; |
| 544 | } |
| 545 | |
| 546 | static int64_t get_clock(void) |
| 547 | { |
| 548 | LARGE_INTEGER ti; |
| 549 | QueryPerformanceCounter(&ti); |
| 550 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 551 | } |
| 552 | |
| 553 | #else |
| 554 | |
| 555 | static int use_rt_clock; |
| 556 | |
| 557 | static void init_get_clock(void) |
| 558 | { |
| 559 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 560 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 561 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 562 | { |
| 563 | struct timespec ts; |
| 564 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 565 | use_rt_clock = 1; |
| 566 | } |
| 567 | } |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | static int64_t get_clock(void) |
| 572 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 573 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 574 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 575 | if (use_rt_clock) { |
| 576 | struct timespec ts; |
| 577 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 578 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 579 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 580 | #endif |
| 581 | { |
| 582 | /* XXX: using gettimeofday leads to problems if the date |
| 583 | changes, so it should be avoided. */ |
| 584 | struct timeval tv; |
| 585 | gettimeofday(&tv, NULL); |
| 586 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 587 | } |
| 588 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 589 | #endif |
| 590 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 591 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 592 | static int64_t cpu_get_icount(void) |
| 593 | { |
| 594 | int64_t icount; |
| 595 | CPUState *env = cpu_single_env;; |
| 596 | icount = qemu_icount; |
| 597 | if (env) { |
| 598 | if (!can_do_io(env)) |
| 599 | fprintf(stderr, "Bad clock read\n"); |
| 600 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 601 | } |
| 602 | return qemu_icount_bias + (icount << icount_time_shift); |
| 603 | } |
| 604 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 605 | /***********************************************************/ |
| 606 | /* guest cycle counter */ |
| 607 | |
| 608 | static int64_t cpu_ticks_prev; |
| 609 | static int64_t cpu_ticks_offset; |
| 610 | static int64_t cpu_clock_offset; |
| 611 | static int cpu_ticks_enabled; |
| 612 | |
| 613 | /* return the host CPU cycle counter and handle stop/restart */ |
| 614 | int64_t cpu_get_ticks(void) |
| 615 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 616 | if (use_icount) { |
| 617 | return cpu_get_icount(); |
| 618 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 619 | if (!cpu_ticks_enabled) { |
| 620 | return cpu_ticks_offset; |
| 621 | } else { |
| 622 | int64_t ticks; |
| 623 | ticks = cpu_get_real_ticks(); |
| 624 | if (cpu_ticks_prev > ticks) { |
| 625 | /* Note: non increasing ticks may happen if the host uses |
| 626 | software suspend */ |
| 627 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 628 | } |
| 629 | cpu_ticks_prev = ticks; |
| 630 | return ticks + cpu_ticks_offset; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 635 | static int64_t cpu_get_clock(void) |
| 636 | { |
| 637 | int64_t ti; |
| 638 | if (!cpu_ticks_enabled) { |
| 639 | return cpu_clock_offset; |
| 640 | } else { |
| 641 | ti = get_clock(); |
| 642 | return ti + cpu_clock_offset; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* enable cpu_get_ticks() */ |
| 647 | void cpu_enable_ticks(void) |
| 648 | { |
| 649 | if (!cpu_ticks_enabled) { |
| 650 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 651 | cpu_clock_offset -= get_clock(); |
| 652 | cpu_ticks_enabled = 1; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 657 | cpu_get_ticks() after that. */ |
| 658 | void cpu_disable_ticks(void) |
| 659 | { |
| 660 | if (cpu_ticks_enabled) { |
| 661 | cpu_ticks_offset = cpu_get_ticks(); |
| 662 | cpu_clock_offset = cpu_get_clock(); |
| 663 | cpu_ticks_enabled = 0; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /***********************************************************/ |
| 668 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 669 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 670 | #define QEMU_TIMER_REALTIME 0 |
| 671 | #define QEMU_TIMER_VIRTUAL 1 |
| 672 | |
| 673 | struct QEMUClock { |
| 674 | int type; |
| 675 | /* XXX: add frequency */ |
| 676 | }; |
| 677 | |
| 678 | struct QEMUTimer { |
| 679 | QEMUClock *clock; |
| 680 | int64_t expire_time; |
| 681 | QEMUTimerCB *cb; |
| 682 | void *opaque; |
| 683 | struct QEMUTimer *next; |
| 684 | }; |
| 685 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 686 | struct qemu_alarm_timer { |
| 687 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 688 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 689 | |
| 690 | int (*start)(struct qemu_alarm_timer *t); |
| 691 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 692 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 693 | void *priv; |
| 694 | }; |
| 695 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 696 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 697 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 698 | |
| 699 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 700 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 701 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 705 | { |
| 706 | if (!alarm_has_dynticks(t)) |
| 707 | return; |
| 708 | |
| 709 | t->rearm(t); |
| 710 | } |
| 711 | |
| 712 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 713 | #define MIN_TIMER_REARM_US 250 |
| 714 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 715 | static struct qemu_alarm_timer *alarm_timer; |
| 716 | |
| 717 | #ifdef _WIN32 |
| 718 | |
| 719 | struct qemu_alarm_win32 { |
| 720 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 721 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 722 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 723 | |
| 724 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 725 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 726 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 727 | |
| 728 | #else |
| 729 | |
| 730 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 731 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 732 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 733 | #ifdef __linux__ |
| 734 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 735 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 736 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 737 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 738 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 739 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 740 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 741 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 742 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 743 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 744 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 745 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 746 | |
| 747 | #endif /* _WIN32 */ |
| 748 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 749 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 750 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 751 | When the guest is idle real and virtual time will be aligned in |
| 752 | the IO wait loop. */ |
| 753 | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
| 754 | |
| 755 | static void icount_adjust(void) |
| 756 | { |
| 757 | int64_t cur_time; |
| 758 | int64_t cur_icount; |
| 759 | int64_t delta; |
| 760 | static int64_t last_delta; |
| 761 | /* If the VM is not running, then do nothing. */ |
| 762 | if (!vm_running) |
| 763 | return; |
| 764 | |
| 765 | cur_time = cpu_get_clock(); |
| 766 | cur_icount = qemu_get_clock(vm_clock); |
| 767 | delta = cur_icount - cur_time; |
| 768 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 769 | if (delta > 0 |
| 770 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 771 | && icount_time_shift > 0) { |
| 772 | /* The guest is getting too far ahead. Slow time down. */ |
| 773 | icount_time_shift--; |
| 774 | } |
| 775 | if (delta < 0 |
| 776 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 777 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 778 | /* The guest is getting too far behind. Speed time up. */ |
| 779 | icount_time_shift++; |
| 780 | } |
| 781 | last_delta = delta; |
| 782 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 783 | } |
| 784 | |
| 785 | static void icount_adjust_rt(void * opaque) |
| 786 | { |
| 787 | qemu_mod_timer(icount_rt_timer, |
| 788 | qemu_get_clock(rt_clock) + 1000); |
| 789 | icount_adjust(); |
| 790 | } |
| 791 | |
| 792 | static void icount_adjust_vm(void * opaque) |
| 793 | { |
| 794 | qemu_mod_timer(icount_vm_timer, |
| 795 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 796 | icount_adjust(); |
| 797 | } |
| 798 | |
| 799 | static void init_icount_adjust(void) |
| 800 | { |
| 801 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 802 | The realtime trigger catches emulated time passing too slowly, |
| 803 | the virtual time trigger catches emulated time passing too fast. |
| 804 | Realtime triggers occur even when idle, so use them less frequently |
| 805 | than VM triggers. */ |
| 806 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 807 | qemu_mod_timer(icount_rt_timer, |
| 808 | qemu_get_clock(rt_clock) + 1000); |
| 809 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 810 | qemu_mod_timer(icount_vm_timer, |
| 811 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 812 | } |
| 813 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 814 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 815 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 816 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 817 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 818 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 819 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 820 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 821 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 822 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 823 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 824 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 825 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 826 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 827 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 828 | {"win32", 0, win32_start_timer, |
| 829 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 830 | #endif |
| 831 | {NULL, } |
| 832 | }; |
| 833 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 834 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 835 | { |
| 836 | int i; |
| 837 | |
| 838 | printf("Available alarm timers, in order of precedence:\n"); |
| 839 | for (i = 0; alarm_timers[i].name; i++) |
| 840 | printf("%s\n", alarm_timers[i].name); |
| 841 | } |
| 842 | |
| 843 | static void configure_alarms(char const *opt) |
| 844 | { |
| 845 | int i; |
| 846 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 847 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 848 | char *arg; |
| 849 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 850 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 851 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 852 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 853 | show_available_alarms(); |
| 854 | exit(0); |
| 855 | } |
| 856 | |
| 857 | arg = strdup(opt); |
| 858 | |
| 859 | /* Reorder the array */ |
| 860 | name = strtok(arg, ","); |
| 861 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 862 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 863 | if (!strcmp(alarm_timers[i].name, name)) |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | if (i == count) { |
| 868 | fprintf(stderr, "Unknown clock %s\n", name); |
| 869 | goto next; |
| 870 | } |
| 871 | |
| 872 | if (i < cur) |
| 873 | /* Ignore */ |
| 874 | goto next; |
| 875 | |
| 876 | /* Swap */ |
| 877 | tmp = alarm_timers[i]; |
| 878 | alarm_timers[i] = alarm_timers[cur]; |
| 879 | alarm_timers[cur] = tmp; |
| 880 | |
| 881 | cur++; |
| 882 | next: |
| 883 | name = strtok(NULL, ","); |
| 884 | } |
| 885 | |
| 886 | free(arg); |
| 887 | |
| 888 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 889 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 890 | for (i = cur; i < count; i++) |
| 891 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 892 | } else { |
| 893 | show_available_alarms(); |
| 894 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 895 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 896 | } |
| 897 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 898 | QEMUClock *rt_clock; |
| 899 | QEMUClock *vm_clock; |
| 900 | |
| 901 | static QEMUTimer *active_timers[2]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 902 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 903 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 904 | { |
| 905 | QEMUClock *clock; |
| 906 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 907 | clock->type = type; |
| 908 | return clock; |
| 909 | } |
| 910 | |
| 911 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 912 | { |
| 913 | QEMUTimer *ts; |
| 914 | |
| 915 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 916 | ts->clock = clock; |
| 917 | ts->cb = cb; |
| 918 | ts->opaque = opaque; |
| 919 | return ts; |
| 920 | } |
| 921 | |
| 922 | void qemu_free_timer(QEMUTimer *ts) |
| 923 | { |
| 924 | qemu_free(ts); |
| 925 | } |
| 926 | |
| 927 | /* stop a timer, but do not dealloc it */ |
| 928 | void qemu_del_timer(QEMUTimer *ts) |
| 929 | { |
| 930 | QEMUTimer **pt, *t; |
| 931 | |
| 932 | /* NOTE: this code must be signal safe because |
| 933 | qemu_timer_expired() can be called from a signal. */ |
| 934 | pt = &active_timers[ts->clock->type]; |
| 935 | for(;;) { |
| 936 | t = *pt; |
| 937 | if (!t) |
| 938 | break; |
| 939 | if (t == ts) { |
| 940 | *pt = t->next; |
| 941 | break; |
| 942 | } |
| 943 | pt = &t->next; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /* modify the current timer so that it will be fired when current_time |
| 948 | >= expire_time. The corresponding callback will be called. */ |
| 949 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 950 | { |
| 951 | QEMUTimer **pt, *t; |
| 952 | |
| 953 | qemu_del_timer(ts); |
| 954 | |
| 955 | /* add the timer in the sorted list */ |
| 956 | /* NOTE: this code must be signal safe because |
| 957 | qemu_timer_expired() can be called from a signal. */ |
| 958 | pt = &active_timers[ts->clock->type]; |
| 959 | for(;;) { |
| 960 | t = *pt; |
| 961 | if (!t) |
| 962 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 963 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 964 | break; |
| 965 | pt = &t->next; |
| 966 | } |
| 967 | ts->expire_time = expire_time; |
| 968 | ts->next = *pt; |
| 969 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 970 | |
| 971 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 972 | if (pt == &active_timers[ts->clock->type]) { |
| 973 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 974 | qemu_rearm_alarm_timer(alarm_timer); |
| 975 | } |
| 976 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 977 | if (use_icount) |
| 978 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 979 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | int qemu_timer_pending(QEMUTimer *ts) |
| 983 | { |
| 984 | QEMUTimer *t; |
| 985 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 986 | if (t == ts) |
| 987 | return 1; |
| 988 | } |
| 989 | return 0; |
| 990 | } |
| 991 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 992 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 993 | { |
| 994 | if (!timer_head) |
| 995 | return 0; |
| 996 | return (timer_head->expire_time <= current_time); |
| 997 | } |
| 998 | |
| 999 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1000 | { |
| 1001 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1002 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1003 | for(;;) { |
| 1004 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1005 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1006 | break; |
| 1007 | /* remove timer from the list before calling the callback */ |
| 1008 | *ptimer_head = ts->next; |
| 1009 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1010 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1011 | /* run the callback (the timer list can be modified) */ |
| 1012 | ts->cb(ts->opaque); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1017 | { |
| 1018 | switch(clock->type) { |
| 1019 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1020 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1021 | default: |
| 1022 | case QEMU_TIMER_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1023 | if (use_icount) { |
| 1024 | return cpu_get_icount(); |
| 1025 | } else { |
| 1026 | return cpu_get_clock(); |
| 1027 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1031 | static void init_timers(void) |
| 1032 | { |
| 1033 | init_get_clock(); |
| 1034 | ticks_per_sec = QEMU_TIMER_BASE; |
| 1035 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 1036 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 1037 | } |
| 1038 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1039 | /* save a timer */ |
| 1040 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1041 | { |
| 1042 | uint64_t expire_time; |
| 1043 | |
| 1044 | if (qemu_timer_pending(ts)) { |
| 1045 | expire_time = ts->expire_time; |
| 1046 | } else { |
| 1047 | expire_time = -1; |
| 1048 | } |
| 1049 | qemu_put_be64(f, expire_time); |
| 1050 | } |
| 1051 | |
| 1052 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1053 | { |
| 1054 | uint64_t expire_time; |
| 1055 | |
| 1056 | expire_time = qemu_get_be64(f); |
| 1057 | if (expire_time != -1) { |
| 1058 | qemu_mod_timer(ts, expire_time); |
| 1059 | } else { |
| 1060 | qemu_del_timer(ts); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | static void timer_save(QEMUFile *f, void *opaque) |
| 1065 | { |
| 1066 | if (cpu_ticks_enabled) { |
| 1067 | hw_error("cannot save state if virtual timers are running"); |
| 1068 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1069 | qemu_put_be64(f, cpu_ticks_offset); |
| 1070 | qemu_put_be64(f, ticks_per_sec); |
| 1071 | qemu_put_be64(f, cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 1075 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1076 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1077 | return -EINVAL; |
| 1078 | if (cpu_ticks_enabled) { |
| 1079 | return -EINVAL; |
| 1080 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1081 | cpu_ticks_offset=qemu_get_be64(f); |
| 1082 | ticks_per_sec=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1083 | if (version_id == 2) { |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1084 | cpu_clock_offset=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1085 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1089 | static void qemu_event_increment(void); |
| 1090 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1091 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 1092 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 1093 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 1094 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1095 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1096 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1097 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1098 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1099 | #if 0 |
| 1100 | #define DISP_FREQ 1000 |
| 1101 | { |
| 1102 | static int64_t delta_min = INT64_MAX; |
| 1103 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1104 | static int count; |
| 1105 | ti = qemu_get_clock(vm_clock); |
| 1106 | if (last_clock != 0) { |
| 1107 | delta = ti - last_clock; |
| 1108 | if (delta < delta_min) |
| 1109 | delta_min = delta; |
| 1110 | if (delta > delta_max) |
| 1111 | delta_max = delta; |
| 1112 | delta_cum += delta; |
| 1113 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1114 | 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] | 1115 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 1116 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 1117 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 1118 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 1119 | count = 0; |
| 1120 | delta_min = INT64_MAX; |
| 1121 | delta_max = 0; |
| 1122 | delta_cum = 0; |
| 1123 | } |
| 1124 | } |
| 1125 | last_clock = ti; |
| 1126 | } |
| 1127 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1128 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1129 | (!use_icount && |
| 1130 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 1131 | qemu_get_clock(vm_clock))) || |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1132 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 1133 | qemu_get_clock(rt_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1134 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1135 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1136 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1137 | #ifndef CONFIG_IOTHREAD |
| 1138 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1139 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1140 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1141 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1142 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1143 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1144 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1148 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1149 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1150 | int64_t delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1151 | |
| 1152 | if (active_timers[QEMU_TIMER_VIRTUAL]) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1153 | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
| 1154 | qemu_get_clock(vm_clock); |
| 1155 | } else { |
| 1156 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1157 | delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1160 | if (delta < 0) |
| 1161 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1162 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1163 | return delta; |
| 1164 | } |
| 1165 | |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1166 | #if defined(__linux__) || defined(_WIN32) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1167 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1168 | { |
| 1169 | int64_t delta; |
| 1170 | int64_t rtdelta; |
| 1171 | |
| 1172 | if (use_icount) |
| 1173 | delta = INT32_MAX; |
| 1174 | else |
| 1175 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1176 | |
| 1177 | if (active_timers[QEMU_TIMER_REALTIME]) { |
| 1178 | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
| 1179 | qemu_get_clock(rt_clock))*1000; |
| 1180 | if (rtdelta < delta) |
| 1181 | delta = rtdelta; |
| 1182 | } |
| 1183 | |
| 1184 | if (delta < MIN_TIMER_REARM_US) |
| 1185 | delta = MIN_TIMER_REARM_US; |
| 1186 | |
| 1187 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1188 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1189 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1190 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1191 | #ifndef _WIN32 |
| 1192 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1193 | /* Sets a specific flag */ |
| 1194 | static int fcntl_setfl(int fd, int flag) |
| 1195 | { |
| 1196 | int flags; |
| 1197 | |
| 1198 | flags = fcntl(fd, F_GETFL); |
| 1199 | if (flags == -1) |
| 1200 | return -errno; |
| 1201 | |
| 1202 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1203 | return -errno; |
| 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1208 | #if defined(__linux__) |
| 1209 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1210 | #define RTC_FREQ 1024 |
| 1211 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1212 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1213 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1214 | struct sigaction act; |
| 1215 | |
| 1216 | /* timer signal */ |
| 1217 | sigfillset(&act.sa_mask); |
| 1218 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1219 | act.sa_handler = host_alarm_handler; |
| 1220 | |
| 1221 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1222 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1223 | fcntl(fd, F_SETOWN, getpid()); |
| 1224 | } |
| 1225 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1226 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1227 | { |
| 1228 | struct hpet_info info; |
| 1229 | int r, fd; |
| 1230 | |
| 1231 | fd = open("/dev/hpet", O_RDONLY); |
| 1232 | if (fd < 0) |
| 1233 | return -1; |
| 1234 | |
| 1235 | /* Set frequency */ |
| 1236 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1237 | if (r < 0) { |
| 1238 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1239 | "error, but for better emulation accuracy type:\n" |
| 1240 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1241 | goto fail; |
| 1242 | } |
| 1243 | |
| 1244 | /* Check capabilities */ |
| 1245 | r = ioctl(fd, HPET_INFO, &info); |
| 1246 | if (r < 0) |
| 1247 | goto fail; |
| 1248 | |
| 1249 | /* Enable periodic mode */ |
| 1250 | r = ioctl(fd, HPET_EPI, 0); |
| 1251 | if (info.hi_flags && (r < 0)) |
| 1252 | goto fail; |
| 1253 | |
| 1254 | /* Enable interrupt */ |
| 1255 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1256 | if (r < 0) |
| 1257 | goto fail; |
| 1258 | |
| 1259 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1260 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1261 | |
| 1262 | return 0; |
| 1263 | fail: |
| 1264 | close(fd); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
| 1268 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1269 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1270 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1271 | |
| 1272 | close(fd); |
| 1273 | } |
| 1274 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1275 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1276 | { |
| 1277 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1278 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1279 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 1280 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1281 | if (rtc_fd < 0) |
| 1282 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1283 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1284 | if (current_rtc_freq != RTC_FREQ && |
| 1285 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1286 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1287 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1288 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1289 | goto fail; |
| 1290 | } |
| 1291 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1292 | fail: |
| 1293 | close(rtc_fd); |
| 1294 | return -1; |
| 1295 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1296 | |
| 1297 | enable_sigio_timer(rtc_fd); |
| 1298 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1299 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1300 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1301 | return 0; |
| 1302 | } |
| 1303 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1304 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1305 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1306 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1307 | |
| 1308 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1311 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1312 | { |
| 1313 | struct sigevent ev; |
| 1314 | timer_t host_timer; |
| 1315 | struct sigaction act; |
| 1316 | |
| 1317 | sigfillset(&act.sa_mask); |
| 1318 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1319 | act.sa_handler = host_alarm_handler; |
| 1320 | |
| 1321 | sigaction(SIGALRM, &act, NULL); |
| 1322 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1323 | /* |
| 1324 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1325 | * about uninitialized data in timer_create call |
| 1326 | */ |
| 1327 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1328 | ev.sigev_value.sival_int = 0; |
| 1329 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1330 | ev.sigev_signo = SIGALRM; |
| 1331 | |
| 1332 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1333 | perror("timer_create"); |
| 1334 | |
| 1335 | /* disable dynticks */ |
| 1336 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1337 | |
| 1338 | return -1; |
| 1339 | } |
| 1340 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1341 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1347 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1348 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1349 | |
| 1350 | timer_delete(host_timer); |
| 1351 | } |
| 1352 | |
| 1353 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1354 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1355 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1356 | struct itimerspec timeout; |
| 1357 | int64_t nearest_delta_us = INT64_MAX; |
| 1358 | int64_t current_us; |
| 1359 | |
| 1360 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1361 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1362 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1363 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1364 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* check whether a timer is already running */ |
| 1367 | if (timer_gettime(host_timer, &timeout)) { |
| 1368 | perror("gettime"); |
| 1369 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1370 | exit(1); |
| 1371 | } |
| 1372 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1373 | if (current_us && current_us <= nearest_delta_us) |
| 1374 | return; |
| 1375 | |
| 1376 | timeout.it_interval.tv_sec = 0; |
| 1377 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1378 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1379 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1380 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1381 | perror("settime"); |
| 1382 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1383 | exit(1); |
| 1384 | } |
| 1385 | } |
| 1386 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1387 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1388 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1389 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1390 | { |
| 1391 | struct sigaction act; |
| 1392 | struct itimerval itv; |
| 1393 | int err; |
| 1394 | |
| 1395 | /* timer signal */ |
| 1396 | sigfillset(&act.sa_mask); |
| 1397 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1398 | act.sa_handler = host_alarm_handler; |
| 1399 | |
| 1400 | sigaction(SIGALRM, &act, NULL); |
| 1401 | |
| 1402 | itv.it_interval.tv_sec = 0; |
| 1403 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1404 | itv.it_interval.tv_usec = 999; |
| 1405 | itv.it_value.tv_sec = 0; |
| 1406 | itv.it_value.tv_usec = 10 * 1000; |
| 1407 | |
| 1408 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1409 | if (err) |
| 1410 | return -1; |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1416 | { |
| 1417 | struct itimerval itv; |
| 1418 | |
| 1419 | memset(&itv, 0, sizeof(itv)); |
| 1420 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1421 | } |
| 1422 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1423 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1424 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1425 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1426 | #ifdef _WIN32 |
| 1427 | |
| 1428 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1429 | { |
| 1430 | TIMECAPS tc; |
| 1431 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1432 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1433 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1434 | memset(&tc, 0, sizeof(tc)); |
| 1435 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1436 | |
| 1437 | if (data->period < tc.wPeriodMin) |
| 1438 | data->period = tc.wPeriodMin; |
| 1439 | |
| 1440 | timeBeginPeriod(data->period); |
| 1441 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1442 | flags = TIME_CALLBACK_FUNCTION; |
| 1443 | if (alarm_has_dynticks(t)) |
| 1444 | flags |= TIME_ONESHOT; |
| 1445 | else |
| 1446 | flags |= TIME_PERIODIC; |
| 1447 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1448 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1449 | data->period, // resolution |
| 1450 | host_alarm_handler, // function |
| 1451 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1452 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1453 | |
| 1454 | if (!data->timerId) { |
| 1455 | perror("Failed to initialize win32 alarm timer"); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1456 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1457 | return -1; |
| 1458 | } |
| 1459 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1464 | { |
| 1465 | struct qemu_alarm_win32 *data = t->priv; |
| 1466 | |
| 1467 | timeKillEvent(data->timerId); |
| 1468 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1471 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1472 | { |
| 1473 | struct qemu_alarm_win32 *data = t->priv; |
| 1474 | uint64_t nearest_delta_us; |
| 1475 | |
| 1476 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1477 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1478 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1479 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1480 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1481 | nearest_delta_us /= 1000; |
| 1482 | |
| 1483 | timeKillEvent(data->timerId); |
| 1484 | |
| 1485 | data->timerId = timeSetEvent(1, |
| 1486 | data->period, |
| 1487 | host_alarm_handler, |
| 1488 | (DWORD)t, |
| 1489 | TIME_ONESHOT | TIME_PERIODIC); |
| 1490 | |
| 1491 | if (!data->timerId) { |
| 1492 | perror("Failed to re-arm win32 alarm timer"); |
| 1493 | |
| 1494 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1495 | exit(1); |
| 1496 | } |
| 1497 | } |
| 1498 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1499 | #endif /* _WIN32 */ |
| 1500 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1501 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1502 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1503 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1504 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1505 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1506 | for (i = 0; alarm_timers[i].name; i++) { |
| 1507 | t = &alarm_timers[i]; |
| 1508 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1509 | err = t->start(t); |
| 1510 | if (!err) |
| 1511 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1512 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1513 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1514 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1515 | err = -ENOENT; |
| 1516 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1517 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1518 | |
| 1519 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1520 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1521 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1522 | |
| 1523 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1524 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1527 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1528 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1529 | alarm_timer->stop(alarm_timer); |
| 1530 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1533 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1534 | /* host time/date access */ |
| 1535 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1536 | { |
| 1537 | time_t ti; |
| 1538 | struct tm *ret; |
| 1539 | |
| 1540 | time(&ti); |
| 1541 | ti += offset; |
| 1542 | if (rtc_date_offset == -1) { |
| 1543 | if (rtc_utc) |
| 1544 | ret = gmtime(&ti); |
| 1545 | else |
| 1546 | ret = localtime(&ti); |
| 1547 | } else { |
| 1548 | ti -= rtc_date_offset; |
| 1549 | ret = gmtime(&ti); |
| 1550 | } |
| 1551 | |
| 1552 | memcpy(tm, ret, sizeof(struct tm)); |
| 1553 | } |
| 1554 | |
| 1555 | int qemu_timedate_diff(struct tm *tm) |
| 1556 | { |
| 1557 | time_t seconds; |
| 1558 | |
| 1559 | if (rtc_date_offset == -1) |
| 1560 | if (rtc_utc) |
| 1561 | seconds = mktimegm(tm); |
| 1562 | else |
| 1563 | seconds = mktime(tm); |
| 1564 | else |
| 1565 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1566 | |
| 1567 | return seconds - time(NULL); |
| 1568 | } |
| 1569 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1570 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1571 | static void socket_cleanup(void) |
| 1572 | { |
| 1573 | WSACleanup(); |
| 1574 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1575 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1576 | static int socket_init(void) |
| 1577 | { |
| 1578 | WSADATA Data; |
| 1579 | int ret, err; |
| 1580 | |
| 1581 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1582 | if (ret != 0) { |
| 1583 | err = WSAGetLastError(); |
| 1584 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1585 | return -1; |
| 1586 | } |
| 1587 | atexit(socket_cleanup); |
| 1588 | return 0; |
| 1589 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1590 | #endif |
| 1591 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1592 | /***********************************************************/ |
| 1593 | /* Bluetooth support */ |
| 1594 | static int nb_hcis; |
| 1595 | static int cur_hci; |
| 1596 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1597 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1598 | static struct bt_vlan_s { |
| 1599 | struct bt_scatternet_s net; |
| 1600 | int id; |
| 1601 | struct bt_vlan_s *next; |
| 1602 | } *first_bt_vlan; |
| 1603 | |
| 1604 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1605 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1606 | { |
| 1607 | struct bt_vlan_s **pvlan, *vlan; |
| 1608 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1609 | if (vlan->id == id) |
| 1610 | return &vlan->net; |
| 1611 | } |
| 1612 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1613 | vlan->id = id; |
| 1614 | pvlan = &first_bt_vlan; |
| 1615 | while (*pvlan != NULL) |
| 1616 | pvlan = &(*pvlan)->next; |
| 1617 | *pvlan = vlan; |
| 1618 | return &vlan->net; |
| 1619 | } |
| 1620 | |
| 1621 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1622 | { |
| 1623 | } |
| 1624 | |
| 1625 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1626 | { |
| 1627 | return -ENOTSUP; |
| 1628 | } |
| 1629 | |
| 1630 | static struct HCIInfo null_hci = { |
| 1631 | .cmd_send = null_hci_send, |
| 1632 | .sco_send = null_hci_send, |
| 1633 | .acl_send = null_hci_send, |
| 1634 | .bdaddr_set = null_hci_addr_set, |
| 1635 | }; |
| 1636 | |
| 1637 | struct HCIInfo *qemu_next_hci(void) |
| 1638 | { |
| 1639 | if (cur_hci == nb_hcis) |
| 1640 | return &null_hci; |
| 1641 | |
| 1642 | return hci_table[cur_hci++]; |
| 1643 | } |
| 1644 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1645 | static struct HCIInfo *hci_init(const char *str) |
| 1646 | { |
| 1647 | char *endp; |
| 1648 | struct bt_scatternet_s *vlan = 0; |
| 1649 | |
| 1650 | if (!strcmp(str, "null")) |
| 1651 | /* null */ |
| 1652 | return &null_hci; |
| 1653 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1654 | /* host[:hciN] */ |
| 1655 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1656 | else if (!strncmp(str, "hci", 3)) { |
| 1657 | /* hci[,vlan=n] */ |
| 1658 | if (str[3]) { |
| 1659 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1660 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1661 | if (*endp) |
| 1662 | vlan = 0; |
| 1663 | } |
| 1664 | } else |
| 1665 | vlan = qemu_find_bt_vlan(0); |
| 1666 | if (vlan) |
| 1667 | return bt_new_hci(vlan); |
| 1668 | } |
| 1669 | |
| 1670 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | static int bt_hci_parse(const char *str) |
| 1676 | { |
| 1677 | struct HCIInfo *hci; |
| 1678 | bdaddr_t bdaddr; |
| 1679 | |
| 1680 | if (nb_hcis >= MAX_NICS) { |
| 1681 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1682 | return -1; |
| 1683 | } |
| 1684 | |
| 1685 | hci = hci_init(str); |
| 1686 | if (!hci) |
| 1687 | return -1; |
| 1688 | |
| 1689 | bdaddr.b[0] = 0x52; |
| 1690 | bdaddr.b[1] = 0x54; |
| 1691 | bdaddr.b[2] = 0x00; |
| 1692 | bdaddr.b[3] = 0x12; |
| 1693 | bdaddr.b[4] = 0x34; |
| 1694 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1695 | hci->bdaddr_set(hci, bdaddr.b); |
| 1696 | |
| 1697 | hci_table[nb_hcis++] = hci; |
| 1698 | |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | static void bt_vhci_add(int vlan_id) |
| 1703 | { |
| 1704 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1705 | |
| 1706 | if (!vlan->slave) |
| 1707 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1708 | "an empty scatternet %i\n", vlan_id); |
| 1709 | |
| 1710 | bt_vhci_init(bt_new_hci(vlan)); |
| 1711 | } |
| 1712 | |
| 1713 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1714 | { |
| 1715 | struct bt_scatternet_s *vlan; |
| 1716 | int vlan_id = 0; |
| 1717 | char *endp = strstr(opt, ",vlan="); |
| 1718 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1719 | char devname[10]; |
| 1720 | |
| 1721 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1722 | |
| 1723 | if (endp) { |
| 1724 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1725 | if (*endp) { |
| 1726 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1727 | return 0; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1732 | |
| 1733 | if (!vlan->slave) |
| 1734 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1735 | "an empty scatternet %i\n", vlan_id); |
| 1736 | |
| 1737 | if (!strcmp(devname, "keyboard")) |
| 1738 | return bt_keyboard_init(vlan); |
| 1739 | |
| 1740 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | static int bt_parse(const char *opt) |
| 1745 | { |
| 1746 | const char *endp, *p; |
| 1747 | int vlan; |
| 1748 | |
| 1749 | if (strstart(opt, "hci", &endp)) { |
| 1750 | if (!*endp || *endp == ',') { |
| 1751 | if (*endp) |
| 1752 | if (!strstart(endp, ",vlan=", 0)) |
| 1753 | opt = endp + 1; |
| 1754 | |
| 1755 | return bt_hci_parse(opt); |
| 1756 | } |
| 1757 | } else if (strstart(opt, "vhci", &endp)) { |
| 1758 | if (!*endp || *endp == ',') { |
| 1759 | if (*endp) { |
| 1760 | if (strstart(endp, ",vlan=", &p)) { |
| 1761 | vlan = strtol(p, (char **) &endp, 0); |
| 1762 | if (*endp) { |
| 1763 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1764 | return 1; |
| 1765 | } |
| 1766 | } else { |
| 1767 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | } else |
| 1771 | vlan = 0; |
| 1772 | |
| 1773 | bt_vhci_add(vlan); |
| 1774 | return 0; |
| 1775 | } |
| 1776 | } else if (strstart(opt, "device:", &endp)) |
| 1777 | return !bt_device_add(endp); |
| 1778 | |
| 1779 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1780 | return 1; |
| 1781 | } |
| 1782 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1783 | /***********************************************************/ |
| 1784 | /* QEMU Block devices */ |
| 1785 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1786 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1787 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1788 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1789 | #define PFLASH_ALIAS "if=pflash" |
| 1790 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1791 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1792 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1793 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1794 | { |
| 1795 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1796 | char optstr[1024]; |
| 1797 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1798 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1799 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1800 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1801 | va_end(ap); |
| 1802 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1803 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1804 | if (!opts) { |
| 1805 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1806 | __FUNCTION__, optstr); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | if (file) |
| 1810 | qemu_opt_set(opts, "file", file); |
| 1811 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1814 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1815 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1816 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* seek interface, bus and unit */ |
| 1819 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1820 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1821 | if (dinfo->type == type && |
| 1822 | dinfo->bus == bus && |
| 1823 | dinfo->unit == unit) |
| 1824 | return dinfo; |
| 1825 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1826 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1827 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 1830 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1831 | { |
| 1832 | DriveInfo *dinfo; |
| 1833 | |
| 1834 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1835 | if (strcmp(id, dinfo->id)) |
| 1836 | continue; |
| 1837 | return dinfo; |
| 1838 | } |
| 1839 | return NULL; |
| 1840 | } |
| 1841 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1842 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1843 | { |
| 1844 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1845 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1846 | |
| 1847 | max_bus = -1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1848 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1849 | if(dinfo->type == type && |
| 1850 | dinfo->bus > max_bus) |
| 1851 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1852 | } |
| 1853 | return max_bus; |
| 1854 | } |
| 1855 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1856 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 1857 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1858 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1859 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1860 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1861 | if (dinfo->bdrv == bdrv) |
| 1862 | return dinfo->serial; |
| 1863 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1864 | |
| 1865 | return "\0"; |
| 1866 | } |
| 1867 | |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1868 | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
| 1869 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1870 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1871 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1872 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1873 | if (dinfo->bdrv == bdrv) |
| 1874 | return dinfo->onerror; |
| 1875 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1876 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 1877 | return BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 1880 | static void bdrv_format_print(void *opaque, const char *name) |
| 1881 | { |
| 1882 | fprintf(stderr, " %s", name); |
| 1883 | } |
| 1884 | |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1885 | void drive_uninit(BlockDriverState *bdrv) |
| 1886 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1887 | DriveInfo *dinfo; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1888 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1889 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1890 | if (dinfo->bdrv != bdrv) |
| 1891 | continue; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1892 | qemu_opts_del(dinfo->opts); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1893 | TAILQ_REMOVE(&drives, dinfo, next); |
| 1894 | qemu_free(dinfo); |
| 1895 | break; |
| 1896 | } |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1899 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1900 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1901 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1902 | const char *buf; |
| 1903 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1904 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1905 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1906 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1907 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1908 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 1909 | int bus_id, unit_id; |
| 1910 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 1911 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 1912 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1913 | int max_devs; |
| 1914 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 1915 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1916 | int aio = 0; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1917 | int bdrv_flags, onerror; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 1918 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1919 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1920 | int snapshot = 0; |
| 1921 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1922 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1923 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1924 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 1925 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1926 | |
blueswir1 | c9b1ae2 | 2008-09-28 18:55:17 +0000 | [diff] [blame] | 1927 | if (machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1928 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1929 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1930 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1931 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1932 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1933 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1934 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1935 | } |
| 1936 | media = MEDIA_DISK; |
| 1937 | |
| 1938 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1939 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 1940 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 1941 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1942 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1943 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 1944 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 1945 | secs = qemu_opt_get_number(opts, "secs", 0); |
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 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1948 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1949 | file = qemu_opt_get(opts, "file"); |
| 1950 | serial = qemu_opt_get(opts, "serial"); |
| 1951 | |
| 1952 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 1953 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1954 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1955 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1956 | max_devs = MAX_IDE_DEVS; |
| 1957 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1958 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1959 | max_devs = MAX_SCSI_DEVS; |
| 1960 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1961 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1962 | max_devs = 0; |
| 1963 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1964 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1965 | max_devs = 0; |
| 1966 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1967 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1968 | max_devs = 0; |
| 1969 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1970 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1971 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1972 | } else if (!strcmp(buf, "virtio")) { |
| 1973 | type = IF_VIRTIO; |
| 1974 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1975 | } else if (!strcmp(buf, "xen")) { |
| 1976 | type = IF_XEN; |
| 1977 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 1978 | } else if (!strcmp(buf, "none")) { |
| 1979 | type = IF_NONE; |
| 1980 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1981 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1982 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1983 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1987 | if (cyls || heads || secs) { |
| 1988 | if (cyls < 1 || cyls > 16383) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1989 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1990 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1991 | } |
| 1992 | if (heads < 1 || heads > 16) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1993 | fprintf(stderr, "qemu: '%s' invalid physical heads 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 (secs < 1 || secs > 63) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1997 | fprintf(stderr, "qemu: '%s' invalid physical secs 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 | } |
| 2001 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2002 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2003 | if (!cyls) { |
| 2004 | fprintf(stderr, |
| 2005 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2006 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2007 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2008 | } |
| 2009 | if (!strcmp(buf, "none")) |
| 2010 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2011 | else if (!strcmp(buf, "lba")) |
| 2012 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2013 | else if (!strcmp(buf, "auto")) |
| 2014 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2015 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2016 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2017 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2021 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2022 | if (!strcmp(buf, "disk")) { |
| 2023 | media = MEDIA_DISK; |
| 2024 | } else if (!strcmp(buf, "cdrom")) { |
| 2025 | if (cyls || secs || heads) { |
| 2026 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2027 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2028 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2029 | } |
| 2030 | media = MEDIA_CDROM; |
| 2031 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2032 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2033 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2037 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2038 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2039 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2040 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2041 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2042 | else if (!strcmp(buf, "writeback")) |
| 2043 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2044 | else { |
| 2045 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2046 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2050 | #ifdef CONFIG_LINUX_AIO |
| 2051 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2052 | if (!strcmp(buf, "threads")) |
| 2053 | aio = 0; |
| 2054 | else if (!strcmp(buf, "native")) |
| 2055 | aio = 1; |
| 2056 | else { |
| 2057 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | } |
| 2061 | #endif |
| 2062 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2063 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2064 | if (strcmp(buf, "?") == 0) { |
| 2065 | fprintf(stderr, "qemu: Supported formats:"); |
| 2066 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2067 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2068 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2069 | } |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2070 | drv = bdrv_find_format(buf); |
| 2071 | if (!drv) { |
| 2072 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2073 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 2077 | onerror = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2078 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2079 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2080 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2081 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2082 | } |
| 2083 | if (!strcmp(buf, "ignore")) |
| 2084 | onerror = BLOCK_ERR_IGNORE; |
| 2085 | else if (!strcmp(buf, "enospc")) |
| 2086 | onerror = BLOCK_ERR_STOP_ENOSPC; |
| 2087 | else if (!strcmp(buf, "stop")) |
| 2088 | onerror = BLOCK_ERR_STOP_ANY; |
| 2089 | else if (!strcmp(buf, "report")) |
| 2090 | onerror = BLOCK_ERR_REPORT; |
| 2091 | else { |
| 2092 | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2093 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2097 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2098 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2099 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2100 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2101 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2104 | /* compute bus and unit according index */ |
| 2105 | |
| 2106 | if (index != -1) { |
| 2107 | if (bus_id != 0 || unit_id != -1) { |
| 2108 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2109 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2110 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2111 | } |
| 2112 | if (max_devs == 0) |
| 2113 | { |
| 2114 | unit_id = index; |
| 2115 | bus_id = 0; |
| 2116 | } else { |
| 2117 | unit_id = index % max_devs; |
| 2118 | bus_id = index / max_devs; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | /* if user doesn't specify a unit_id, |
| 2123 | * try to find the first free |
| 2124 | */ |
| 2125 | |
| 2126 | if (unit_id == -1) { |
| 2127 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2128 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2129 | unit_id++; |
| 2130 | if (max_devs && unit_id >= max_devs) { |
| 2131 | unit_id -= max_devs; |
| 2132 | bus_id++; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | /* check unit id */ |
| 2138 | |
| 2139 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2140 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2141 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2142 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * ignore multiple definitions |
| 2147 | */ |
| 2148 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2149 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2150 | *fatal_error = 0; |
| 2151 | return NULL; |
| 2152 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* init */ |
| 2155 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2156 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2157 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2158 | dinfo->id = qemu_strdup(buf); |
| 2159 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2160 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2161 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2162 | if (type == IF_IDE || type == IF_SCSI) |
| 2163 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2164 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2165 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2166 | devname, bus_id, mediastr, unit_id); |
| 2167 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2168 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2169 | devname, mediastr, unit_id); |
| 2170 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2171 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2172 | dinfo->devaddr = devaddr; |
| 2173 | dinfo->type = type; |
| 2174 | dinfo->bus = bus_id; |
| 2175 | dinfo->unit = unit_id; |
| 2176 | dinfo->onerror = onerror; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2177 | dinfo->opts = opts; |
| 2178 | if (serial) |
| 2179 | strncpy(dinfo->serial, serial, sizeof(serial)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2180 | TAILQ_INSERT_TAIL(&drives, dinfo, next); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2181 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2182 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2183 | case IF_IDE: |
| 2184 | case IF_SCSI: |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2185 | case IF_XEN: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2186 | switch(media) { |
| 2187 | case MEDIA_DISK: |
| 2188 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2189 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2190 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2191 | } |
| 2192 | break; |
| 2193 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2194 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | } |
| 2197 | break; |
| 2198 | case IF_SD: |
| 2199 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2200 | approximation. */ |
| 2201 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2202 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | case IF_PFLASH: |
| 2205 | case IF_MTD: |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 2206 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2207 | break; |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 2208 | case IF_VIRTIO: |
| 2209 | /* add virtio block device */ |
| 2210 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 2211 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 2212 | qemu_opt_set(opts, "drive", dinfo->id); |
| 2213 | if (devaddr) |
| 2214 | qemu_opt_set(opts, "addr", devaddr); |
| 2215 | break; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 2216 | case IF_COUNT: |
| 2217 | abort(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2218 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2219 | if (!file) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2220 | *fatal_error = 0; |
| 2221 | return NULL; |
| 2222 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2223 | bdrv_flags = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2224 | if (snapshot) { |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2225 | bdrv_flags |= BDRV_O_SNAPSHOT; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2226 | cache = 2; /* always use write-back with snapshot */ |
| 2227 | } |
| 2228 | if (cache == 0) /* no caching */ |
| 2229 | bdrv_flags |= BDRV_O_NOCACHE; |
| 2230 | else if (cache == 2) /* write-back */ |
| 2231 | bdrv_flags |= BDRV_O_CACHE_WB; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2232 | |
| 2233 | if (aio == 1) { |
| 2234 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 2235 | } else { |
| 2236 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
| 2237 | } |
| 2238 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2239 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2240 | fprintf(stderr, "qemu: could not open disk image %s\n", |
| 2241 | file); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2242 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2243 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2244 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2245 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2246 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2247 | *fatal_error = 0; |
| 2248 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2251 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2252 | { |
| 2253 | QEMUMachine *machine = opaque; |
| 2254 | int fatal_error = 0; |
| 2255 | |
| 2256 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2257 | if (fatal_error) |
| 2258 | return 1; |
| 2259 | } |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2264 | { |
| 2265 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2266 | qemu_opt_set(opts, "snapshot", "on"); |
| 2267 | } |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2271 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2272 | { |
| 2273 | boot_set_handler = func; |
| 2274 | boot_set_opaque = opaque; |
| 2275 | } |
| 2276 | |
| 2277 | int qemu_boot_set(const char *boot_devices) |
| 2278 | { |
| 2279 | if (!boot_set_handler) { |
| 2280 | return -EINVAL; |
| 2281 | } |
| 2282 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2283 | } |
| 2284 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2285 | static int parse_bootdevices(char *devices) |
| 2286 | { |
| 2287 | /* We just do some generic consistency checks */ |
| 2288 | const char *p; |
| 2289 | int bitmap = 0; |
| 2290 | |
| 2291 | for (p = devices; *p != '\0'; p++) { |
| 2292 | /* Allowed boot devices are: |
| 2293 | * a-b: floppy disk drives |
| 2294 | * c-f: IDE disk drives |
| 2295 | * g-m: machine implementation dependant drives |
| 2296 | * n-p: network devices |
| 2297 | * It's up to each machine implementation to check if the given boot |
| 2298 | * devices match the actual hardware implementation and firmware |
| 2299 | * features. |
| 2300 | */ |
| 2301 | if (*p < 'a' || *p > 'p') { |
| 2302 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2303 | exit(1); |
| 2304 | } |
| 2305 | if (bitmap & (1 << (*p - 'a'))) { |
| 2306 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2307 | exit(1); |
| 2308 | } |
| 2309 | bitmap |= 1 << (*p - 'a'); |
| 2310 | } |
| 2311 | return bitmap; |
| 2312 | } |
| 2313 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2314 | static void restore_boot_devices(void *opaque) |
| 2315 | { |
| 2316 | char *standard_boot_devices = opaque; |
| 2317 | |
| 2318 | qemu_boot_set(standard_boot_devices); |
| 2319 | |
| 2320 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2321 | qemu_free(standard_boot_devices); |
| 2322 | } |
| 2323 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2324 | static void numa_add(const char *optarg) |
| 2325 | { |
| 2326 | char option[128]; |
| 2327 | char *endptr; |
| 2328 | unsigned long long value, endvalue; |
| 2329 | int nodenr; |
| 2330 | |
| 2331 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2332 | if (!strcmp(option, "node")) { |
| 2333 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2334 | nodenr = nb_numa_nodes; |
| 2335 | } else { |
| 2336 | nodenr = strtoull(option, NULL, 10); |
| 2337 | } |
| 2338 | |
| 2339 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2340 | node_mem[nodenr] = 0; |
| 2341 | } else { |
| 2342 | value = strtoull(option, &endptr, 0); |
| 2343 | switch (*endptr) { |
| 2344 | case 0: case 'M': case 'm': |
| 2345 | value <<= 20; |
| 2346 | break; |
| 2347 | case 'G': case 'g': |
| 2348 | value <<= 30; |
| 2349 | break; |
| 2350 | } |
| 2351 | node_mem[nodenr] = value; |
| 2352 | } |
| 2353 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2354 | node_cpumask[nodenr] = 0; |
| 2355 | } else { |
| 2356 | value = strtoull(option, &endptr, 10); |
| 2357 | if (value >= 64) { |
| 2358 | value = 63; |
| 2359 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2360 | } else { |
| 2361 | if (*endptr == '-') { |
| 2362 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2363 | if (endvalue >= 63) { |
| 2364 | endvalue = 62; |
| 2365 | fprintf(stderr, |
| 2366 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2367 | } |
| 2368 | value = (1 << (endvalue + 1)) - (1 << value); |
| 2369 | } else { |
| 2370 | value = 1 << value; |
| 2371 | } |
| 2372 | } |
| 2373 | node_cpumask[nodenr] = value; |
| 2374 | } |
| 2375 | nb_numa_nodes++; |
| 2376 | } |
| 2377 | return; |
| 2378 | } |
| 2379 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2380 | static void smp_parse(const char *optarg) |
| 2381 | { |
| 2382 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2383 | char *endptr; |
| 2384 | char option[128]; |
| 2385 | |
| 2386 | smp = strtoul(optarg, &endptr, 10); |
| 2387 | if (endptr != optarg) { |
| 2388 | if (*endptr == ',') { |
| 2389 | endptr++; |
| 2390 | } |
| 2391 | } |
| 2392 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2393 | sockets = strtoull(option, NULL, 10); |
| 2394 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2395 | cores = strtoull(option, NULL, 10); |
| 2396 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2397 | threads = strtoull(option, NULL, 10); |
| 2398 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2399 | max_cpus = strtoull(option, NULL, 10); |
| 2400 | |
| 2401 | /* compute missing values, prefer sockets over cores over threads */ |
| 2402 | if (smp == 0 || sockets == 0) { |
| 2403 | sockets = sockets > 0 ? sockets : 1; |
| 2404 | cores = cores > 0 ? cores : 1; |
| 2405 | threads = threads > 0 ? threads : 1; |
| 2406 | if (smp == 0) { |
| 2407 | smp = cores * threads * sockets; |
| 2408 | } else { |
| 2409 | sockets = smp / (cores * threads); |
| 2410 | } |
| 2411 | } else { |
| 2412 | if (cores == 0) { |
| 2413 | threads = threads > 0 ? threads : 1; |
| 2414 | cores = smp / (sockets * threads); |
| 2415 | } else { |
| 2416 | if (sockets == 0) { |
| 2417 | sockets = smp / (cores * threads); |
| 2418 | } else { |
| 2419 | threads = smp / (cores * sockets); |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | smp_cpus = smp; |
| 2424 | smp_cores = cores > 0 ? cores : 1; |
| 2425 | smp_threads = threads > 0 ? threads : 1; |
| 2426 | if (max_cpus == 0) |
| 2427 | max_cpus = smp_cpus; |
| 2428 | } |
| 2429 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2430 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2431 | /* USB devices */ |
| 2432 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2433 | static USBPort *used_usb_ports; |
| 2434 | static USBPort *free_usb_ports; |
| 2435 | |
| 2436 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 2437 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 2438 | usb_attachfn attach) |
| 2439 | { |
| 2440 | port->opaque = opaque; |
| 2441 | port->index = index; |
| 2442 | port->attach = attach; |
| 2443 | port->next = free_usb_ports; |
| 2444 | free_usb_ports = port; |
| 2445 | } |
| 2446 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2447 | int usb_device_add_dev(USBDevice *dev) |
| 2448 | { |
| 2449 | USBPort *port; |
| 2450 | |
| 2451 | /* Find a USB port to add the device to. */ |
| 2452 | port = free_usb_ports; |
| 2453 | if (!port->next) { |
| 2454 | USBDevice *hub; |
| 2455 | |
| 2456 | /* Create a new hub and chain it on. */ |
| 2457 | free_usb_ports = NULL; |
| 2458 | port->next = used_usb_ports; |
| 2459 | used_usb_ports = port; |
| 2460 | |
| 2461 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 2462 | usb_attach(port, hub); |
| 2463 | port = free_usb_ports; |
| 2464 | } |
| 2465 | |
| 2466 | free_usb_ports = port->next; |
| 2467 | port->next = used_usb_ports; |
| 2468 | used_usb_ports = port; |
| 2469 | usb_attach(port, dev); |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2473 | static void usb_msd_password_cb(void *opaque, int err) |
| 2474 | { |
| 2475 | USBDevice *dev = opaque; |
| 2476 | |
| 2477 | if (!err) |
| 2478 | usb_device_add_dev(dev); |
| 2479 | else |
| 2480 | dev->handle_destroy(dev); |
| 2481 | } |
| 2482 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2483 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2484 | { |
| 2485 | const char *p; |
| 2486 | USBDevice *dev; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2487 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2488 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2489 | return -1; |
| 2490 | |
| 2491 | if (strstart(devname, "host:", &p)) { |
| 2492 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2493 | } else if (!strcmp(devname, "mouse")) { |
| 2494 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 2495 | } else if (!strcmp(devname, "tablet")) { |
balrog | 47b2d33 | 2007-06-22 08:16:00 +0000 | [diff] [blame] | 2496 | dev = usb_tablet_init(); |
| 2497 | } else if (!strcmp(devname, "keyboard")) { |
| 2498 | dev = usb_keyboard_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2499 | } else if (strstart(devname, "disk:", &p)) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2500 | BlockDriverState *bs; |
| 2501 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2502 | dev = usb_msd_init(p); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2503 | if (!dev) |
| 2504 | return -1; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2505 | bs = usb_msd_get_bdrv(dev); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2506 | if (bdrv_key_required(bs)) { |
| 2507 | autostart = 0; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2508 | if (is_hotplug) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2509 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
| 2510 | dev); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2511 | return 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
balrog | f6d2a31 | 2007-06-10 19:21:04 +0000 | [diff] [blame] | 2514 | } else if (!strcmp(devname, "wacom-tablet")) { |
| 2515 | dev = usb_wacom_init(); |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2516 | } else if (strstart(devname, "serial:", &p)) { |
| 2517 | dev = usb_serial_init(p); |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 2518 | #ifdef CONFIG_BRLAPI |
| 2519 | } else if (!strcmp(devname, "braille")) { |
| 2520 | dev = usb_baum_init(); |
| 2521 | #endif |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2522 | } else if (strstart(devname, "net:", &p)) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2523 | int nic = nb_nics; |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2524 | |
Jan Kiszka | 10ae5a7 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 2525 | if (net_client_init(NULL, "nic", p) < 0) |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2526 | return -1; |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2527 | nd_table[nic].model = "usb"; |
| 2528 | dev = usb_net_init(&nd_table[nic]); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2529 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2530 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2531 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2532 | } else { |
| 2533 | return -1; |
| 2534 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2535 | if (!dev) |
| 2536 | return -1; |
| 2537 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2538 | return usb_device_add_dev(dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2541 | int usb_device_del_addr(int bus_num, int addr) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2542 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2543 | USBPort *port; |
| 2544 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2545 | USBDevice *dev; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2546 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2547 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2548 | return -1; |
| 2549 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2550 | if (bus_num != 0) |
| 2551 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2552 | |
| 2553 | lastp = &used_usb_ports; |
| 2554 | port = used_usb_ports; |
| 2555 | while (port && port->dev->addr != addr) { |
| 2556 | lastp = &port->next; |
| 2557 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2558 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2559 | |
| 2560 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2561 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2562 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2563 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2564 | *lastp = port->next; |
| 2565 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2566 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2567 | port->next = free_usb_ports; |
| 2568 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
| 2571 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2572 | static int usb_device_del(const char *devname) |
| 2573 | { |
| 2574 | int bus_num, addr; |
| 2575 | const char *p; |
| 2576 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2577 | if (strstart(devname, "host:", &p)) |
| 2578 | return usb_host_device_close(p); |
| 2579 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2580 | if (!used_usb_ports) |
| 2581 | return -1; |
| 2582 | |
| 2583 | p = strchr(devname, '.'); |
| 2584 | if (!p) |
| 2585 | return -1; |
| 2586 | bus_num = strtoul(devname, NULL, 0); |
| 2587 | addr = strtoul(p + 1, NULL, 0); |
| 2588 | |
| 2589 | return usb_device_del_addr(bus_num, addr); |
| 2590 | } |
| 2591 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2592 | static int usb_parse(const char *cmdline) |
| 2593 | { |
| 2594 | return usb_device_add(cmdline, 0); |
| 2595 | } |
| 2596 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2597 | void do_usb_add(Monitor *mon, const char *devname) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2598 | { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2599 | usb_device_add(devname, 1); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2602 | void do_usb_del(Monitor *mon, const char *devname) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2603 | { |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2604 | usb_device_del(devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2607 | void usb_info(Monitor *mon) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2608 | { |
| 2609 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2610 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2611 | const char *speed_str; |
| 2612 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2613 | if (!usb_enabled) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2614 | monitor_printf(mon, "USB support not enabled\n"); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2615 | return; |
| 2616 | } |
| 2617 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2618 | for (port = used_usb_ports; port; port = port->next) { |
| 2619 | dev = port->dev; |
| 2620 | if (!dev) |
| 2621 | continue; |
| 2622 | switch(dev->speed) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2623 | case USB_SPEED_LOW: |
| 2624 | speed_str = "1.5"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2625 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2626 | case USB_SPEED_FULL: |
| 2627 | speed_str = "12"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2628 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2629 | case USB_SPEED_HIGH: |
| 2630 | speed_str = "480"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2631 | break; |
| 2632 | default: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2633 | speed_str = "?"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2634 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2635 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2636 | monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 2637 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2641 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2642 | /* PCMCIA/Cardbus */ |
| 2643 | |
| 2644 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2645 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2646 | struct pcmcia_socket_entry_s *next; |
| 2647 | } *pcmcia_sockets = 0; |
| 2648 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2649 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2650 | { |
| 2651 | struct pcmcia_socket_entry_s *entry; |
| 2652 | |
| 2653 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2654 | entry->socket = socket; |
| 2655 | entry->next = pcmcia_sockets; |
| 2656 | pcmcia_sockets = entry; |
| 2657 | } |
| 2658 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2659 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2660 | { |
| 2661 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2662 | |
| 2663 | ptr = &pcmcia_sockets; |
| 2664 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2665 | if (entry->socket == socket) { |
| 2666 | *ptr = entry->next; |
| 2667 | qemu_free(entry); |
| 2668 | } |
| 2669 | } |
| 2670 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2671 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2672 | { |
| 2673 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2674 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2675 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2676 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2677 | |
| 2678 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2679 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2680 | iter->socket->attached ? iter->socket->card_string : |
| 2681 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | /***********************************************************/ |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2685 | /* register display */ |
| 2686 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2687 | struct DisplayAllocator default_allocator = { |
| 2688 | defaultallocator_create_displaysurface, |
| 2689 | defaultallocator_resize_displaysurface, |
| 2690 | defaultallocator_free_displaysurface |
| 2691 | }; |
| 2692 | |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2693 | void register_displaystate(DisplayState *ds) |
| 2694 | { |
| 2695 | DisplayState **s; |
| 2696 | s = &display_state; |
| 2697 | while (*s != NULL) |
| 2698 | s = &(*s)->next; |
| 2699 | ds->next = NULL; |
| 2700 | *s = ds; |
| 2701 | } |
| 2702 | |
| 2703 | DisplayState *get_displaystate(void) |
| 2704 | { |
| 2705 | return display_state; |
| 2706 | } |
| 2707 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2708 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2709 | { |
| 2710 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2711 | return ds->allocator; |
| 2712 | } |
| 2713 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2714 | /* dumb display */ |
| 2715 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2716 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2717 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2718 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2719 | ds->allocator = &default_allocator; |
| 2720 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2721 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2725 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2726 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2727 | typedef struct IOHandlerRecord { |
| 2728 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2729 | IOCanRWHandler *fd_read_poll; |
| 2730 | IOHandler *fd_read; |
| 2731 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2732 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2733 | void *opaque; |
| 2734 | /* temporary data */ |
| 2735 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2736 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2737 | } IOHandlerRecord; |
| 2738 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2739 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2740 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2741 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2742 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2743 | int qemu_set_fd_handler2(int fd, |
| 2744 | IOCanRWHandler *fd_read_poll, |
| 2745 | IOHandler *fd_read, |
| 2746 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2747 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2748 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2749 | IOHandlerRecord **pioh, *ioh; |
| 2750 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2751 | if (!fd_read && !fd_write) { |
| 2752 | pioh = &first_io_handler; |
| 2753 | for(;;) { |
| 2754 | ioh = *pioh; |
| 2755 | if (ioh == NULL) |
| 2756 | break; |
| 2757 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2758 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | } |
| 2761 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2762 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2763 | } else { |
| 2764 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2765 | if (ioh->fd == fd) |
| 2766 | goto found; |
| 2767 | } |
| 2768 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2769 | ioh->next = first_io_handler; |
| 2770 | first_io_handler = ioh; |
| 2771 | found: |
| 2772 | ioh->fd = fd; |
| 2773 | ioh->fd_read_poll = fd_read_poll; |
| 2774 | ioh->fd_read = fd_read; |
| 2775 | ioh->fd_write = fd_write; |
| 2776 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2777 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2778 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2779 | return 0; |
| 2780 | } |
| 2781 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2782 | int qemu_set_fd_handler(int fd, |
| 2783 | IOHandler *fd_read, |
| 2784 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2785 | void *opaque) |
| 2786 | { |
| 2787 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2790 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2791 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2792 | /* Polling handling */ |
| 2793 | |
| 2794 | typedef struct PollingEntry { |
| 2795 | PollingFunc *func; |
| 2796 | void *opaque; |
| 2797 | struct PollingEntry *next; |
| 2798 | } PollingEntry; |
| 2799 | |
| 2800 | static PollingEntry *first_polling_entry; |
| 2801 | |
| 2802 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2803 | { |
| 2804 | PollingEntry **ppe, *pe; |
| 2805 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2806 | pe->func = func; |
| 2807 | pe->opaque = opaque; |
| 2808 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2809 | *ppe = pe; |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2814 | { |
| 2815 | PollingEntry **ppe, *pe; |
| 2816 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2817 | pe = *ppe; |
| 2818 | if (pe->func == func && pe->opaque == opaque) { |
| 2819 | *ppe = pe->next; |
| 2820 | qemu_free(pe); |
| 2821 | break; |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2826 | /***********************************************************/ |
| 2827 | /* Wait objects support */ |
| 2828 | typedef struct WaitObjects { |
| 2829 | int num; |
| 2830 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2831 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2832 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2833 | } WaitObjects; |
| 2834 | |
| 2835 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2836 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2837 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2838 | { |
| 2839 | WaitObjects *w = &wait_objects; |
| 2840 | |
| 2841 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2842 | return -1; |
| 2843 | w->events[w->num] = handle; |
| 2844 | w->func[w->num] = func; |
| 2845 | w->opaque[w->num] = opaque; |
| 2846 | w->num++; |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2851 | { |
| 2852 | int i, found; |
| 2853 | WaitObjects *w = &wait_objects; |
| 2854 | |
| 2855 | found = 0; |
| 2856 | for (i = 0; i < w->num; i++) { |
| 2857 | if (w->events[i] == handle) |
| 2858 | found = 1; |
| 2859 | if (found) { |
| 2860 | w->events[i] = w->events[i + 1]; |
| 2861 | w->func[i] = w->func[i + 1]; |
| 2862 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2863 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2864 | } |
| 2865 | if (found) |
| 2866 | w->num--; |
| 2867 | } |
| 2868 | #endif |
| 2869 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2870 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2871 | /* ram save/restore */ |
| 2872 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2873 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 2874 | { |
| 2875 | int v; |
| 2876 | |
| 2877 | v = qemu_get_byte(f); |
| 2878 | switch(v) { |
| 2879 | case 0: |
| 2880 | if (qemu_get_buffer(f, buf, len) != len) |
| 2881 | return -EIO; |
| 2882 | break; |
| 2883 | case 1: |
| 2884 | v = qemu_get_byte(f); |
| 2885 | memset(buf, v, len); |
| 2886 | break; |
| 2887 | default: |
| 2888 | return -EINVAL; |
| 2889 | } |
aliguori | 871d2f0 | 2008-10-13 03:07:56 +0000 | [diff] [blame] | 2890 | |
| 2891 | if (qemu_file_has_error(f)) |
| 2892 | return -EIO; |
| 2893 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2894 | return 0; |
| 2895 | } |
| 2896 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2897 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2898 | { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2899 | int ret; |
| 2900 | ram_addr_t i; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2901 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2902 | if (qemu_get_be32(f) != last_ram_offset) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2903 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2904 | for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2905 | ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2906 | if (ret) |
| 2907 | return ret; |
| 2908 | } |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2912 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 2913 | #define IOBUF_SIZE 4096 |
| 2914 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 2915 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2916 | typedef struct RamDecompressState { |
| 2917 | z_stream zstream; |
| 2918 | QEMUFile *f; |
| 2919 | uint8_t buf[IOBUF_SIZE]; |
| 2920 | } RamDecompressState; |
| 2921 | |
| 2922 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 2923 | { |
| 2924 | int ret; |
| 2925 | memset(s, 0, sizeof(*s)); |
| 2926 | s->f = f; |
| 2927 | ret = inflateInit(&s->zstream); |
| 2928 | if (ret != Z_OK) |
| 2929 | return -1; |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
| 2933 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 2934 | { |
| 2935 | int ret, clen; |
| 2936 | |
| 2937 | s->zstream.avail_out = len; |
| 2938 | s->zstream.next_out = buf; |
| 2939 | while (s->zstream.avail_out > 0) { |
| 2940 | if (s->zstream.avail_in == 0) { |
| 2941 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 2942 | return -1; |
| 2943 | clen = qemu_get_be16(s->f); |
| 2944 | if (clen > IOBUF_SIZE) |
| 2945 | return -1; |
| 2946 | qemu_get_buffer(s->f, s->buf, clen); |
| 2947 | s->zstream.avail_in = clen; |
| 2948 | s->zstream.next_in = s->buf; |
| 2949 | } |
| 2950 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 2951 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 2952 | return -1; |
| 2953 | } |
| 2954 | } |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | static void ram_decompress_close(RamDecompressState *s) |
| 2959 | { |
| 2960 | inflateEnd(&s->zstream); |
| 2961 | } |
| 2962 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2963 | #define RAM_SAVE_FLAG_FULL 0x01 |
| 2964 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2965 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2966 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2967 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2968 | |
| 2969 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2970 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2971 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2972 | uint32_t *array = (uint32_t *)page; |
| 2973 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2974 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2975 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2976 | if (array[i] != val) |
| 2977 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2978 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2979 | |
| 2980 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2983 | static int ram_save_block(QEMUFile *f) |
| 2984 | { |
| 2985 | static ram_addr_t current_addr = 0; |
| 2986 | ram_addr_t saved_addr = current_addr; |
| 2987 | ram_addr_t addr = 0; |
| 2988 | int found = 0; |
| 2989 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2990 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2991 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2992 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2993 | |
| 2994 | cpu_physical_memory_reset_dirty(current_addr, |
| 2995 | current_addr + TARGET_PAGE_SIZE, |
| 2996 | MIGRATION_DIRTY_FLAG); |
| 2997 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2998 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2999 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3000 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3001 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3002 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3003 | } else { |
| 3004 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3005 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | found = 1; |
| 3009 | break; |
| 3010 | } |
| 3011 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3012 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | return found; |
| 3016 | } |
| 3017 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3018 | static uint64_t bytes_transferred = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3019 | |
| 3020 | static ram_addr_t ram_save_remaining(void) |
| 3021 | { |
| 3022 | ram_addr_t addr; |
| 3023 | ram_addr_t count = 0; |
| 3024 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3025 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3026 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3027 | count++; |
| 3028 | } |
| 3029 | |
| 3030 | return count; |
| 3031 | } |
| 3032 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3033 | uint64_t ram_bytes_remaining(void) |
| 3034 | { |
| 3035 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 3036 | } |
| 3037 | |
| 3038 | uint64_t ram_bytes_transferred(void) |
| 3039 | { |
| 3040 | return bytes_transferred; |
| 3041 | } |
| 3042 | |
| 3043 | uint64_t ram_bytes_total(void) |
| 3044 | { |
| 3045 | return last_ram_offset; |
| 3046 | } |
| 3047 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3048 | static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
| 3049 | { |
| 3050 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3051 | uint64_t bytes_transferred_last; |
| 3052 | double bwidth = 0; |
| 3053 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3054 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 3055 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3056 | qemu_file_set_error(f); |
| 3057 | return 0; |
| 3058 | } |
| 3059 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3060 | if (stage == 1) { |
| 3061 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3062 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3063 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3064 | cpu_physical_memory_set_dirty(addr); |
| 3065 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3066 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3067 | /* Enable dirty memory tracking */ |
| 3068 | cpu_physical_memory_set_dirty_tracking(1); |
| 3069 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3070 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3073 | bytes_transferred_last = bytes_transferred; |
| 3074 | bwidth = get_clock(); |
| 3075 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3076 | while (!qemu_file_rate_limit(f)) { |
| 3077 | int ret; |
| 3078 | |
| 3079 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3080 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3081 | if (ret == 0) /* no more blocks */ |
| 3082 | break; |
| 3083 | } |
| 3084 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3085 | bwidth = get_clock() - bwidth; |
| 3086 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 3087 | |
| 3088 | /* if we haven't transferred anything this round, force expected_time to a |
| 3089 | * a very high value, but without crashing */ |
| 3090 | if (bwidth == 0) |
| 3091 | bwidth = 0.000001; |
| 3092 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3093 | /* try transferring iterative blocks of memory */ |
| 3094 | |
| 3095 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3096 | |
| 3097 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3098 | while (ram_save_block(f) != 0) { |
| 3099 | bytes_transferred += TARGET_PAGE_SIZE; |
| 3100 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 3101 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3105 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3106 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 3107 | |
| 3108 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | static int ram_load_dead(QEMUFile *f, void *opaque) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3112 | { |
| 3113 | RamDecompressState s1, *s = &s1; |
| 3114 | uint8_t buf[10]; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3115 | ram_addr_t i; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3116 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3117 | if (ram_decompress_open(s, f) < 0) |
| 3118 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3119 | for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3120 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 3121 | fprintf(stderr, "Error while reading ram block header\n"); |
| 3122 | goto error; |
| 3123 | } |
| 3124 | if (buf[0] == 0) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3125 | if (ram_decompress_buf(s, qemu_get_ram_ptr(i), |
| 3126 | BDRV_HASH_BLOCK_SIZE) < 0) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3127 | 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] | 3128 | goto error; |
| 3129 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3130 | } else { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3131 | error: |
| 3132 | printf("Error block header\n"); |
| 3133 | return -EINVAL; |
| 3134 | } |
| 3135 | } |
| 3136 | ram_decompress_close(s); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3137 | |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3142 | { |
| 3143 | ram_addr_t addr; |
| 3144 | int flags; |
| 3145 | |
| 3146 | if (version_id == 1) |
| 3147 | return ram_load_v1(f, opaque); |
| 3148 | |
| 3149 | if (version_id == 2) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3150 | if (qemu_get_be32(f) != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3151 | return -EINVAL; |
| 3152 | return ram_load_dead(f, opaque); |
| 3153 | } |
| 3154 | |
| 3155 | if (version_id != 3) |
| 3156 | return -EINVAL; |
| 3157 | |
| 3158 | do { |
| 3159 | addr = qemu_get_be64(f); |
| 3160 | |
| 3161 | flags = addr & ~TARGET_PAGE_MASK; |
| 3162 | addr &= TARGET_PAGE_MASK; |
| 3163 | |
| 3164 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3165 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3166 | return -EINVAL; |
| 3167 | } |
| 3168 | |
| 3169 | if (flags & RAM_SAVE_FLAG_FULL) { |
| 3170 | if (ram_load_dead(f, opaque) < 0) |
| 3171 | return -EINVAL; |
| 3172 | } |
| 3173 | |
| 3174 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 3175 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3176 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 3177 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3178 | if (ch == 0 && |
| 3179 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 3180 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3181 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3182 | #endif |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3183 | } else if (flags & RAM_SAVE_FLAG_PAGE) |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3184 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3185 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 3186 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3187 | return 0; |
| 3188 | } |
| 3189 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3190 | void qemu_service_io(void) |
| 3191 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3192 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3195 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3196 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 3197 | |
| 3198 | struct QEMUBH { |
| 3199 | QEMUBHFunc *cb; |
| 3200 | void *opaque; |
| 3201 | int scheduled; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3202 | int idle; |
| 3203 | int deleted; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3204 | QEMUBH *next; |
| 3205 | }; |
| 3206 | |
| 3207 | static QEMUBH *first_bh = NULL; |
| 3208 | |
| 3209 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 3210 | { |
| 3211 | QEMUBH *bh; |
| 3212 | bh = qemu_mallocz(sizeof(QEMUBH)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3213 | bh->cb = cb; |
| 3214 | bh->opaque = opaque; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3215 | bh->next = first_bh; |
| 3216 | first_bh = bh; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3217 | return bh; |
| 3218 | } |
| 3219 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3220 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3221 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3222 | QEMUBH *bh, **bhp; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3223 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3224 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3225 | ret = 0; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3226 | for (bh = first_bh; bh; bh = bh->next) { |
| 3227 | if (!bh->deleted && bh->scheduled) { |
| 3228 | bh->scheduled = 0; |
| 3229 | if (!bh->idle) |
| 3230 | ret = 1; |
| 3231 | bh->idle = 0; |
| 3232 | bh->cb(bh->opaque); |
| 3233 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3234 | } |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3235 | |
| 3236 | /* remove deleted bhs */ |
| 3237 | bhp = &first_bh; |
| 3238 | while (*bhp) { |
| 3239 | bh = *bhp; |
| 3240 | if (bh->deleted) { |
| 3241 | *bhp = bh->next; |
| 3242 | qemu_free(bh); |
| 3243 | } else |
| 3244 | bhp = &bh->next; |
| 3245 | } |
| 3246 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3247 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3250 | void qemu_bh_schedule_idle(QEMUBH *bh) |
| 3251 | { |
| 3252 | if (bh->scheduled) |
| 3253 | return; |
| 3254 | bh->scheduled = 1; |
| 3255 | bh->idle = 1; |
| 3256 | } |
| 3257 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3258 | void qemu_bh_schedule(QEMUBH *bh) |
| 3259 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3260 | if (bh->scheduled) |
| 3261 | return; |
| 3262 | bh->scheduled = 1; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3263 | bh->idle = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3264 | /* stop the currently executing CPU to execute the BH ASAP */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3265 | qemu_notify_event(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | void qemu_bh_cancel(QEMUBH *bh) |
| 3269 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3270 | bh->scheduled = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | void qemu_bh_delete(QEMUBH *bh) |
| 3274 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3275 | bh->scheduled = 0; |
| 3276 | bh->deleted = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3279 | static void qemu_bh_update_timeout(int *timeout) |
| 3280 | { |
| 3281 | QEMUBH *bh; |
| 3282 | |
| 3283 | for (bh = first_bh; bh; bh = bh->next) { |
| 3284 | if (!bh->deleted && bh->scheduled) { |
| 3285 | if (bh->idle) { |
| 3286 | /* idle bottom halves will be polled at least |
| 3287 | * every 10ms */ |
| 3288 | *timeout = MIN(10, *timeout); |
| 3289 | } else { |
| 3290 | /* non-idle bottom halves will be executed |
| 3291 | * immediately */ |
| 3292 | *timeout = 0; |
| 3293 | break; |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | } |
| 3298 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3299 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3300 | /* machine registration */ |
| 3301 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 3302 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 3303 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3304 | |
| 3305 | int qemu_register_machine(QEMUMachine *m) |
| 3306 | { |
| 3307 | QEMUMachine **pm; |
| 3308 | pm = &first_machine; |
| 3309 | while (*pm != NULL) |
| 3310 | pm = &(*pm)->next; |
| 3311 | m->next = NULL; |
| 3312 | *pm = m; |
| 3313 | return 0; |
| 3314 | } |
| 3315 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3316 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3317 | { |
| 3318 | QEMUMachine *m; |
| 3319 | |
| 3320 | for(m = first_machine; m != NULL; m = m->next) { |
| 3321 | if (!strcmp(m->name, name)) |
| 3322 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 3323 | if (m->alias && !strcmp(m->alias, name)) |
| 3324 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3325 | } |
| 3326 | return NULL; |
| 3327 | } |
| 3328 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 3329 | static QEMUMachine *find_default_machine(void) |
| 3330 | { |
| 3331 | QEMUMachine *m; |
| 3332 | |
| 3333 | for(m = first_machine; m != NULL; m = m->next) { |
| 3334 | if (m->is_default) { |
| 3335 | return m; |
| 3336 | } |
| 3337 | } |
| 3338 | return NULL; |
| 3339 | } |
| 3340 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3341 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3342 | /* main execution loop */ |
| 3343 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3344 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3345 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3346 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 3347 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3348 | DisplayChangeListener *dcl = ds->listeners; |
| 3349 | |
| 3350 | dpy_refresh(ds); |
| 3351 | |
| 3352 | while (dcl != NULL) { |
| 3353 | if (dcl->gui_timer_interval && |
| 3354 | dcl->gui_timer_interval < interval) |
| 3355 | interval = dcl->gui_timer_interval; |
| 3356 | dcl = dcl->next; |
| 3357 | } |
| 3358 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3361 | static void nographic_update(void *opaque) |
| 3362 | { |
| 3363 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3364 | |
| 3365 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3366 | } |
| 3367 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3368 | struct vm_change_state_entry { |
| 3369 | VMChangeStateHandler *cb; |
| 3370 | void *opaque; |
| 3371 | LIST_ENTRY (vm_change_state_entry) entries; |
| 3372 | }; |
| 3373 | |
| 3374 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 3375 | |
| 3376 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3377 | void *opaque) |
| 3378 | { |
| 3379 | VMChangeStateEntry *e; |
| 3380 | |
| 3381 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3382 | |
| 3383 | e->cb = cb; |
| 3384 | e->opaque = opaque; |
| 3385 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 3386 | return e; |
| 3387 | } |
| 3388 | |
| 3389 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3390 | { |
| 3391 | LIST_REMOVE (e, entries); |
| 3392 | qemu_free (e); |
| 3393 | } |
| 3394 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3395 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3396 | { |
| 3397 | VMChangeStateEntry *e; |
| 3398 | |
| 3399 | 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] | 3400 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3404 | static void resume_all_vcpus(void); |
| 3405 | static void pause_all_vcpus(void); |
| 3406 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3407 | void vm_start(void) |
| 3408 | { |
| 3409 | if (!vm_running) { |
| 3410 | cpu_enable_ticks(); |
| 3411 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3412 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3413 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3414 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3418 | /* reset/shutdown handler */ |
| 3419 | |
| 3420 | typedef struct QEMUResetEntry { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3421 | TAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3422 | QEMUResetHandler *func; |
| 3423 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3424 | } QEMUResetEntry; |
| 3425 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3426 | static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3427 | TAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3428 | static int reset_requested; |
| 3429 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3430 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3431 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3432 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3433 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3434 | int qemu_shutdown_requested(void) |
| 3435 | { |
| 3436 | int r = shutdown_requested; |
| 3437 | shutdown_requested = 0; |
| 3438 | return r; |
| 3439 | } |
| 3440 | |
| 3441 | int qemu_reset_requested(void) |
| 3442 | { |
| 3443 | int r = reset_requested; |
| 3444 | reset_requested = 0; |
| 3445 | return r; |
| 3446 | } |
| 3447 | |
| 3448 | int qemu_powerdown_requested(void) |
| 3449 | { |
| 3450 | int r = powerdown_requested; |
| 3451 | powerdown_requested = 0; |
| 3452 | return r; |
| 3453 | } |
| 3454 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3455 | static int qemu_debug_requested(void) |
| 3456 | { |
| 3457 | int r = debug_requested; |
| 3458 | debug_requested = 0; |
| 3459 | return r; |
| 3460 | } |
| 3461 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3462 | static int qemu_vmstop_requested(void) |
| 3463 | { |
| 3464 | int r = vmstop_requested; |
| 3465 | vmstop_requested = 0; |
| 3466 | return r; |
| 3467 | } |
| 3468 | |
| 3469 | static void do_vm_stop(int reason) |
| 3470 | { |
| 3471 | if (vm_running) { |
| 3472 | cpu_disable_ticks(); |
| 3473 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3474 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3475 | vm_state_notify(0, reason); |
| 3476 | } |
| 3477 | } |
| 3478 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3479 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3480 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3481 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3482 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3483 | re->func = func; |
| 3484 | re->opaque = opaque; |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3485 | TAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3488 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3489 | { |
| 3490 | QEMUResetEntry *re; |
| 3491 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3492 | TAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3493 | if (re->func == func && re->opaque == opaque) { |
| 3494 | TAILQ_REMOVE(&reset_handlers, re, entry); |
| 3495 | qemu_free(re); |
| 3496 | return; |
| 3497 | } |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | void qemu_system_reset(void) |
| 3502 | { |
| 3503 | QEMUResetEntry *re, *nre; |
| 3504 | |
| 3505 | /* reset all devices */ |
| 3506 | TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3507 | re->func(re->opaque); |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | void qemu_system_reset_request(void) |
| 3512 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3513 | if (no_reboot) { |
| 3514 | shutdown_requested = 1; |
| 3515 | } else { |
| 3516 | reset_requested = 1; |
| 3517 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3518 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | void qemu_system_shutdown_request(void) |
| 3522 | { |
| 3523 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3524 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3527 | void qemu_system_powerdown_request(void) |
| 3528 | { |
| 3529 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3530 | qemu_notify_event(); |
| 3531 | } |
| 3532 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3533 | #ifdef CONFIG_IOTHREAD |
| 3534 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3535 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3536 | vmstop_requested = reason; |
| 3537 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3538 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3539 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3540 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3541 | #ifndef _WIN32 |
| 3542 | static int io_thread_fd = -1; |
| 3543 | |
| 3544 | static void qemu_event_increment(void) |
| 3545 | { |
| 3546 | static const char byte = 0; |
| 3547 | |
| 3548 | if (io_thread_fd == -1) |
| 3549 | return; |
| 3550 | |
| 3551 | write(io_thread_fd, &byte, sizeof(byte)); |
| 3552 | } |
| 3553 | |
| 3554 | static void qemu_event_read(void *opaque) |
| 3555 | { |
| 3556 | int fd = (unsigned long)opaque; |
| 3557 | ssize_t len; |
| 3558 | |
| 3559 | /* Drain the notify pipe */ |
| 3560 | do { |
| 3561 | char buffer[512]; |
| 3562 | len = read(fd, buffer, sizeof(buffer)); |
| 3563 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3564 | } |
| 3565 | |
| 3566 | static int qemu_event_init(void) |
| 3567 | { |
| 3568 | int err; |
| 3569 | int fds[2]; |
| 3570 | |
| 3571 | err = pipe(fds); |
| 3572 | if (err == -1) |
| 3573 | return -errno; |
| 3574 | |
| 3575 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3576 | if (err < 0) |
| 3577 | goto fail; |
| 3578 | |
| 3579 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3580 | if (err < 0) |
| 3581 | goto fail; |
| 3582 | |
| 3583 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3584 | (void *)(unsigned long)fds[0]); |
| 3585 | |
| 3586 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3587 | return 0; |
| 3588 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3589 | fail: |
| 3590 | close(fds[0]); |
| 3591 | close(fds[1]); |
| 3592 | return err; |
| 3593 | } |
| 3594 | #else |
| 3595 | HANDLE qemu_event_handle; |
| 3596 | |
| 3597 | static void dummy_event_handler(void *opaque) |
| 3598 | { |
| 3599 | } |
| 3600 | |
| 3601 | static int qemu_event_init(void) |
| 3602 | { |
| 3603 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3604 | if (!qemu_event_handle) { |
| 3605 | perror("Failed CreateEvent"); |
| 3606 | return -1; |
| 3607 | } |
| 3608 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | static void qemu_event_increment(void) |
| 3613 | { |
| 3614 | SetEvent(qemu_event_handle); |
| 3615 | } |
| 3616 | #endif |
| 3617 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3618 | static int cpu_can_run(CPUState *env) |
| 3619 | { |
| 3620 | if (env->stop) |
| 3621 | return 0; |
| 3622 | if (env->stopped) |
| 3623 | return 0; |
| 3624 | return 1; |
| 3625 | } |
| 3626 | |
| 3627 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3628 | static int qemu_init_main_loop(void) |
| 3629 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3630 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3633 | void qemu_init_vcpu(void *_env) |
| 3634 | { |
| 3635 | CPUState *env = _env; |
| 3636 | |
| 3637 | if (kvm_enabled()) |
| 3638 | kvm_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3639 | env->nr_cores = smp_cores; |
| 3640 | env->nr_threads = smp_threads; |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3641 | return; |
| 3642 | } |
| 3643 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3644 | int qemu_cpu_self(void *env) |
| 3645 | { |
| 3646 | return 1; |
| 3647 | } |
| 3648 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3649 | static void resume_all_vcpus(void) |
| 3650 | { |
| 3651 | } |
| 3652 | |
| 3653 | static void pause_all_vcpus(void) |
| 3654 | { |
| 3655 | } |
| 3656 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3657 | void qemu_cpu_kick(void *env) |
| 3658 | { |
| 3659 | return; |
| 3660 | } |
| 3661 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3662 | void qemu_notify_event(void) |
| 3663 | { |
| 3664 | CPUState *env = cpu_single_env; |
| 3665 | |
| 3666 | if (env) { |
| 3667 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3668 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3671 | #define qemu_mutex_lock_iothread() do { } while (0) |
| 3672 | #define qemu_mutex_unlock_iothread() do { } while (0) |
| 3673 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3674 | void vm_stop(int reason) |
| 3675 | { |
| 3676 | do_vm_stop(reason); |
| 3677 | } |
| 3678 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3679 | #else /* CONFIG_IOTHREAD */ |
| 3680 | |
| 3681 | #include "qemu-thread.h" |
| 3682 | |
| 3683 | QemuMutex qemu_global_mutex; |
| 3684 | static QemuMutex qemu_fair_mutex; |
| 3685 | |
| 3686 | static QemuThread io_thread; |
| 3687 | |
| 3688 | static QemuThread *tcg_cpu_thread; |
| 3689 | static QemuCond *tcg_halt_cond; |
| 3690 | |
| 3691 | static int qemu_system_ready; |
| 3692 | /* cpu creation */ |
| 3693 | static QemuCond qemu_cpu_cond; |
| 3694 | /* system init */ |
| 3695 | static QemuCond qemu_system_cond; |
| 3696 | static QemuCond qemu_pause_cond; |
| 3697 | |
| 3698 | static void block_io_signals(void); |
| 3699 | static void unblock_io_signals(void); |
| 3700 | static int tcg_has_work(void); |
| 3701 | |
| 3702 | static int qemu_init_main_loop(void) |
| 3703 | { |
| 3704 | int ret; |
| 3705 | |
| 3706 | ret = qemu_event_init(); |
| 3707 | if (ret) |
| 3708 | return ret; |
| 3709 | |
| 3710 | qemu_cond_init(&qemu_pause_cond); |
| 3711 | qemu_mutex_init(&qemu_fair_mutex); |
| 3712 | qemu_mutex_init(&qemu_global_mutex); |
| 3713 | qemu_mutex_lock(&qemu_global_mutex); |
| 3714 | |
| 3715 | unblock_io_signals(); |
| 3716 | qemu_thread_self(&io_thread); |
| 3717 | |
| 3718 | return 0; |
| 3719 | } |
| 3720 | |
| 3721 | static void qemu_wait_io_event(CPUState *env) |
| 3722 | { |
| 3723 | while (!tcg_has_work()) |
| 3724 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3725 | |
| 3726 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3727 | |
| 3728 | /* |
| 3729 | * Users of qemu_global_mutex can be starved, having no chance |
| 3730 | * to acquire it since this path will get to it first. |
| 3731 | * So use another lock to provide fairness. |
| 3732 | */ |
| 3733 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3734 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3735 | |
| 3736 | qemu_mutex_lock(&qemu_global_mutex); |
| 3737 | if (env->stop) { |
| 3738 | env->stop = 0; |
| 3739 | env->stopped = 1; |
| 3740 | qemu_cond_signal(&qemu_pause_cond); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | static int qemu_cpu_exec(CPUState *env); |
| 3745 | |
| 3746 | static void *kvm_cpu_thread_fn(void *arg) |
| 3747 | { |
| 3748 | CPUState *env = arg; |
| 3749 | |
| 3750 | block_io_signals(); |
| 3751 | qemu_thread_self(env->thread); |
| 3752 | |
| 3753 | /* signal CPU creation */ |
| 3754 | qemu_mutex_lock(&qemu_global_mutex); |
| 3755 | env->created = 1; |
| 3756 | qemu_cond_signal(&qemu_cpu_cond); |
| 3757 | |
| 3758 | /* and wait for machine initialization */ |
| 3759 | while (!qemu_system_ready) |
| 3760 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3761 | |
| 3762 | while (1) { |
| 3763 | if (cpu_can_run(env)) |
| 3764 | qemu_cpu_exec(env); |
| 3765 | qemu_wait_io_event(env); |
| 3766 | } |
| 3767 | |
| 3768 | return NULL; |
| 3769 | } |
| 3770 | |
| 3771 | static void tcg_cpu_exec(void); |
| 3772 | |
| 3773 | static void *tcg_cpu_thread_fn(void *arg) |
| 3774 | { |
| 3775 | CPUState *env = arg; |
| 3776 | |
| 3777 | block_io_signals(); |
| 3778 | qemu_thread_self(env->thread); |
| 3779 | |
| 3780 | /* signal CPU creation */ |
| 3781 | qemu_mutex_lock(&qemu_global_mutex); |
| 3782 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3783 | env->created = 1; |
| 3784 | qemu_cond_signal(&qemu_cpu_cond); |
| 3785 | |
| 3786 | /* and wait for machine initialization */ |
| 3787 | while (!qemu_system_ready) |
| 3788 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3789 | |
| 3790 | while (1) { |
| 3791 | tcg_cpu_exec(); |
| 3792 | qemu_wait_io_event(cur_cpu); |
| 3793 | } |
| 3794 | |
| 3795 | return NULL; |
| 3796 | } |
| 3797 | |
| 3798 | void qemu_cpu_kick(void *_env) |
| 3799 | { |
| 3800 | CPUState *env = _env; |
| 3801 | qemu_cond_broadcast(env->halt_cond); |
| 3802 | if (kvm_enabled()) |
| 3803 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3804 | } |
| 3805 | |
| 3806 | int qemu_cpu_self(void *env) |
| 3807 | { |
| 3808 | return (cpu_single_env != NULL); |
| 3809 | } |
| 3810 | |
| 3811 | static void cpu_signal(int sig) |
| 3812 | { |
| 3813 | if (cpu_single_env) |
| 3814 | cpu_exit(cpu_single_env); |
| 3815 | } |
| 3816 | |
| 3817 | static void block_io_signals(void) |
| 3818 | { |
| 3819 | sigset_t set; |
| 3820 | struct sigaction sigact; |
| 3821 | |
| 3822 | sigemptyset(&set); |
| 3823 | sigaddset(&set, SIGUSR2); |
| 3824 | sigaddset(&set, SIGIO); |
| 3825 | sigaddset(&set, SIGALRM); |
| 3826 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3827 | |
| 3828 | sigemptyset(&set); |
| 3829 | sigaddset(&set, SIGUSR1); |
| 3830 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3831 | |
| 3832 | memset(&sigact, 0, sizeof(sigact)); |
| 3833 | sigact.sa_handler = cpu_signal; |
| 3834 | sigaction(SIGUSR1, &sigact, NULL); |
| 3835 | } |
| 3836 | |
| 3837 | static void unblock_io_signals(void) |
| 3838 | { |
| 3839 | sigset_t set; |
| 3840 | |
| 3841 | sigemptyset(&set); |
| 3842 | sigaddset(&set, SIGUSR2); |
| 3843 | sigaddset(&set, SIGIO); |
| 3844 | sigaddset(&set, SIGALRM); |
| 3845 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3846 | |
| 3847 | sigemptyset(&set); |
| 3848 | sigaddset(&set, SIGUSR1); |
| 3849 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3850 | } |
| 3851 | |
| 3852 | static void qemu_signal_lock(unsigned int msecs) |
| 3853 | { |
| 3854 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3855 | |
| 3856 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3857 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3858 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3859 | break; |
| 3860 | } |
| 3861 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3862 | } |
| 3863 | |
| 3864 | static void qemu_mutex_lock_iothread(void) |
| 3865 | { |
| 3866 | if (kvm_enabled()) { |
| 3867 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3868 | qemu_mutex_lock(&qemu_global_mutex); |
| 3869 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3870 | } else |
| 3871 | qemu_signal_lock(100); |
| 3872 | } |
| 3873 | |
| 3874 | static void qemu_mutex_unlock_iothread(void) |
| 3875 | { |
| 3876 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3877 | } |
| 3878 | |
| 3879 | static int all_vcpus_paused(void) |
| 3880 | { |
| 3881 | CPUState *penv = first_cpu; |
| 3882 | |
| 3883 | while (penv) { |
| 3884 | if (!penv->stopped) |
| 3885 | return 0; |
| 3886 | penv = (CPUState *)penv->next_cpu; |
| 3887 | } |
| 3888 | |
| 3889 | return 1; |
| 3890 | } |
| 3891 | |
| 3892 | static void pause_all_vcpus(void) |
| 3893 | { |
| 3894 | CPUState *penv = first_cpu; |
| 3895 | |
| 3896 | while (penv) { |
| 3897 | penv->stop = 1; |
| 3898 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3899 | qemu_cpu_kick(penv); |
| 3900 | penv = (CPUState *)penv->next_cpu; |
| 3901 | } |
| 3902 | |
| 3903 | while (!all_vcpus_paused()) { |
| 3904 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3905 | penv = first_cpu; |
| 3906 | while (penv) { |
| 3907 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3908 | penv = (CPUState *)penv->next_cpu; |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | static void resume_all_vcpus(void) |
| 3914 | { |
| 3915 | CPUState *penv = first_cpu; |
| 3916 | |
| 3917 | while (penv) { |
| 3918 | penv->stop = 0; |
| 3919 | penv->stopped = 0; |
| 3920 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3921 | qemu_cpu_kick(penv); |
| 3922 | penv = (CPUState *)penv->next_cpu; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | static void tcg_init_vcpu(void *_env) |
| 3927 | { |
| 3928 | CPUState *env = _env; |
| 3929 | /* share a single thread for all cpus with TCG */ |
| 3930 | if (!tcg_cpu_thread) { |
| 3931 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3932 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3933 | qemu_cond_init(env->halt_cond); |
| 3934 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3935 | while (env->created == 0) |
| 3936 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3937 | tcg_cpu_thread = env->thread; |
| 3938 | tcg_halt_cond = env->halt_cond; |
| 3939 | } else { |
| 3940 | env->thread = tcg_cpu_thread; |
| 3941 | env->halt_cond = tcg_halt_cond; |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | static void kvm_start_vcpu(CPUState *env) |
| 3946 | { |
| 3947 | kvm_init_vcpu(env); |
| 3948 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3949 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3950 | qemu_cond_init(env->halt_cond); |
| 3951 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3952 | while (env->created == 0) |
| 3953 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3954 | } |
| 3955 | |
| 3956 | void qemu_init_vcpu(void *_env) |
| 3957 | { |
| 3958 | CPUState *env = _env; |
| 3959 | |
| 3960 | if (kvm_enabled()) |
| 3961 | kvm_start_vcpu(env); |
| 3962 | else |
| 3963 | tcg_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3964 | env->nr_cores = smp_cores; |
| 3965 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | void qemu_notify_event(void) |
| 3969 | { |
| 3970 | qemu_event_increment(); |
| 3971 | } |
| 3972 | |
| 3973 | void vm_stop(int reason) |
| 3974 | { |
| 3975 | QemuThread me; |
| 3976 | qemu_thread_self(&me); |
| 3977 | |
| 3978 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3979 | qemu_system_vmstop_request(reason); |
| 3980 | /* |
| 3981 | * FIXME: should not return to device code in case |
| 3982 | * vm_stop() has been requested. |
| 3983 | */ |
| 3984 | if (cpu_single_env) { |
| 3985 | cpu_exit(cpu_single_env); |
| 3986 | cpu_single_env->stop = 1; |
| 3987 | } |
| 3988 | return; |
| 3989 | } |
| 3990 | do_vm_stop(reason); |
| 3991 | } |
| 3992 | |
| 3993 | #endif |
| 3994 | |
| 3995 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3996 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3997 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3998 | { |
| 3999 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4000 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4001 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4002 | |
| 4003 | /* XXX: need to suppress polling by better using win32 events */ |
| 4004 | ret = 0; |
| 4005 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 4006 | ret |= pe->func(pe->opaque); |
| 4007 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4008 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4009 | int err; |
| 4010 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4011 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4012 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4013 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 4014 | if (w->func[ret - WAIT_OBJECT_0]) |
| 4015 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4016 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4017 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4018 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4019 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4020 | /* Check if event is signaled */ |
| 4021 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 4022 | if(ret2 == WAIT_OBJECT_0) { |
| 4023 | if (w->func[i]) |
| 4024 | w->func[i](w->opaque[i]); |
| 4025 | } else if (ret2 == WAIT_TIMEOUT) { |
| 4026 | } else { |
| 4027 | err = GetLastError(); |
| 4028 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4029 | } |
| 4030 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4031 | } else if (ret == WAIT_TIMEOUT) { |
| 4032 | } else { |
| 4033 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4034 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4035 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4036 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4037 | |
| 4038 | *timeout = 0; |
| 4039 | } |
| 4040 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 4041 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4042 | { |
| 4043 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4044 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4045 | |
| 4046 | void main_loop_wait(int timeout) |
| 4047 | { |
| 4048 | IOHandlerRecord *ioh; |
| 4049 | fd_set rfds, wfds, xfds; |
| 4050 | int ret, nfds; |
| 4051 | struct timeval tv; |
| 4052 | |
| 4053 | qemu_bh_update_timeout(&timeout); |
| 4054 | |
| 4055 | host_main_loop_wait(&timeout); |
| 4056 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4057 | /* poll any events */ |
| 4058 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 4059 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4060 | FD_ZERO(&rfds); |
| 4061 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 4062 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4063 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4064 | if (ioh->deleted) |
| 4065 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4066 | if (ioh->fd_read && |
| 4067 | (!ioh->fd_read_poll || |
| 4068 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 4069 | FD_SET(ioh->fd, &rfds); |
| 4070 | if (ioh->fd > nfds) |
| 4071 | nfds = ioh->fd; |
| 4072 | } |
| 4073 | if (ioh->fd_write) { |
| 4074 | FD_SET(ioh->fd, &wfds); |
| 4075 | if (ioh->fd > nfds) |
| 4076 | nfds = ioh->fd; |
| 4077 | } |
| 4078 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4079 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4080 | tv.tv_sec = timeout / 1000; |
| 4081 | tv.tv_usec = (timeout % 1000) * 1000; |
| 4082 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4083 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 4084 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4085 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 4086 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4087 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4088 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4089 | IOHandlerRecord **pioh; |
| 4090 | |
| 4091 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4092 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4093 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4094 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4095 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4096 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4097 | } |
| 4098 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4099 | |
| 4100 | /* remove deleted IO handlers */ |
| 4101 | pioh = &first_io_handler; |
| 4102 | while (*pioh) { |
| 4103 | ioh = *pioh; |
| 4104 | if (ioh->deleted) { |
| 4105 | *pioh = ioh->next; |
| 4106 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4107 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4108 | pioh = &ioh->next; |
| 4109 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4110 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4111 | |
| 4112 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4113 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 4114 | /* rearm timer, if not periodic */ |
| 4115 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 4116 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 4117 | qemu_rearm_alarm_timer(alarm_timer); |
| 4118 | } |
| 4119 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4120 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4121 | if (vm_running) { |
| 4122 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
| 4123 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 4124 | qemu_get_clock(vm_clock)); |
| 4125 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4126 | |
| 4127 | /* real time timers */ |
| 4128 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 4129 | qemu_get_clock(rt_clock)); |
| 4130 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 4131 | /* Check bottom-halves last in case any of the earlier events triggered |
| 4132 | them. */ |
| 4133 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4134 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4137 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4138 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4139 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 4140 | #ifdef CONFIG_PROFILER |
| 4141 | int64_t ti; |
| 4142 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4143 | |
| 4144 | #ifdef CONFIG_PROFILER |
| 4145 | ti = profile_getclock(); |
| 4146 | #endif |
| 4147 | if (use_icount) { |
| 4148 | int64_t count; |
| 4149 | int decr; |
| 4150 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 4151 | env->icount_decr.u16.low = 0; |
| 4152 | env->icount_extra = 0; |
| 4153 | count = qemu_next_deadline(); |
| 4154 | count = (count + (1 << icount_time_shift) - 1) |
| 4155 | >> icount_time_shift; |
| 4156 | qemu_icount += count; |
| 4157 | decr = (count > 0xffff) ? 0xffff : count; |
| 4158 | count -= decr; |
| 4159 | env->icount_decr.u16.low = decr; |
| 4160 | env->icount_extra = count; |
| 4161 | } |
| 4162 | ret = cpu_exec(env); |
| 4163 | #ifdef CONFIG_PROFILER |
| 4164 | qemu_time += profile_getclock() - ti; |
| 4165 | #endif |
| 4166 | if (use_icount) { |
| 4167 | /* Fold pending instructions back into the |
| 4168 | instruction counter, and clear the interrupt flag. */ |
| 4169 | qemu_icount -= (env->icount_decr.u16.low |
| 4170 | + env->icount_extra); |
| 4171 | env->icount_decr.u32 = 0; |
| 4172 | env->icount_extra = 0; |
| 4173 | } |
| 4174 | return ret; |
| 4175 | } |
| 4176 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4177 | static void tcg_cpu_exec(void) |
| 4178 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4179 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4180 | |
| 4181 | if (next_cpu == NULL) |
| 4182 | next_cpu = first_cpu; |
| 4183 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 4184 | CPUState *env = cur_cpu = next_cpu; |
| 4185 | |
| 4186 | if (!vm_running) |
| 4187 | break; |
| 4188 | if (timer_alarm_pending) { |
| 4189 | timer_alarm_pending = 0; |
| 4190 | break; |
| 4191 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4192 | if (cpu_can_run(env)) |
| 4193 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4194 | if (ret == EXCP_DEBUG) { |
| 4195 | gdb_set_stop_cpu(env); |
| 4196 | debug_requested = 1; |
| 4197 | break; |
| 4198 | } |
| 4199 | } |
| 4200 | } |
| 4201 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4202 | static int cpu_has_work(CPUState *env) |
| 4203 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4204 | if (env->stop) |
| 4205 | return 1; |
| 4206 | if (env->stopped) |
| 4207 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4208 | if (!env->halted) |
| 4209 | return 1; |
| 4210 | if (qemu_cpu_has_work(env)) |
| 4211 | return 1; |
| 4212 | return 0; |
| 4213 | } |
| 4214 | |
| 4215 | static int tcg_has_work(void) |
| 4216 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 4217 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4218 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4219 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 4220 | if (cpu_has_work(env)) |
| 4221 | return 1; |
| 4222 | return 0; |
| 4223 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4224 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4225 | static int qemu_calculate_timeout(void) |
| 4226 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4227 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4228 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4229 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4230 | if (!vm_running) |
| 4231 | timeout = 5000; |
| 4232 | else if (tcg_has_work()) |
| 4233 | timeout = 0; |
| 4234 | else if (!use_icount) |
| 4235 | timeout = 5000; |
| 4236 | else { |
| 4237 | /* XXX: use timeout computed from timers */ |
| 4238 | int64_t add; |
| 4239 | int64_t delta; |
| 4240 | /* Advance virtual time to the next event. */ |
| 4241 | if (use_icount == 1) { |
| 4242 | /* When not using an adaptive execution frequency |
| 4243 | we tend to get badly out of sync with real time, |
| 4244 | so just delay for a reasonable amount of time. */ |
| 4245 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4246 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4247 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4248 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4249 | if (delta > 0) { |
| 4250 | /* If virtual time is ahead of real time then just |
| 4251 | wait for IO. */ |
| 4252 | timeout = (delta / 1000000) + 1; |
| 4253 | } else { |
| 4254 | /* Wait for either IO to occur or the next |
| 4255 | timer event. */ |
| 4256 | add = qemu_next_deadline(); |
| 4257 | /* We advance the timer before checking for IO. |
| 4258 | Limit the amount we advance so that early IO |
| 4259 | activity won't get the guest too far ahead. */ |
| 4260 | if (add > 10000000) |
| 4261 | add = 10000000; |
| 4262 | delta += add; |
| 4263 | add = (add + (1 << icount_time_shift) - 1) |
| 4264 | >> icount_time_shift; |
| 4265 | qemu_icount += add; |
| 4266 | timeout = delta / 1000000; |
| 4267 | if (timeout < 0) |
| 4268 | timeout = 0; |
| 4269 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4270 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4271 | |
| 4272 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4273 | #else /* CONFIG_IOTHREAD */ |
| 4274 | return 1000; |
| 4275 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static int vm_can_run(void) |
| 4279 | { |
| 4280 | if (powerdown_requested) |
| 4281 | return 0; |
| 4282 | if (reset_requested) |
| 4283 | return 0; |
| 4284 | if (shutdown_requested) |
| 4285 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4286 | if (debug_requested) |
| 4287 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4288 | return 1; |
| 4289 | } |
| 4290 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4291 | qemu_irq qemu_system_powerdown; |
| 4292 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4293 | static void main_loop(void) |
| 4294 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4295 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4296 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4297 | #ifdef CONFIG_IOTHREAD |
| 4298 | qemu_system_ready = 1; |
| 4299 | qemu_cond_broadcast(&qemu_system_cond); |
| 4300 | #endif |
| 4301 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4302 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4303 | do { |
| 4304 | #ifdef CONFIG_PROFILER |
| 4305 | int64_t ti; |
| 4306 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4307 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4308 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4309 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4310 | #ifdef CONFIG_PROFILER |
| 4311 | ti = profile_getclock(); |
| 4312 | #endif |
| 4313 | main_loop_wait(qemu_calculate_timeout()); |
| 4314 | #ifdef CONFIG_PROFILER |
| 4315 | dev_time += profile_getclock() - ti; |
| 4316 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4317 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4318 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4319 | if (qemu_debug_requested()) |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4320 | vm_stop(EXCP_DEBUG); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4321 | if (qemu_shutdown_requested()) { |
| 4322 | if (no_shutdown) { |
| 4323 | vm_stop(0); |
| 4324 | no_shutdown = 0; |
| 4325 | } else |
| 4326 | break; |
| 4327 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4328 | if (qemu_reset_requested()) { |
| 4329 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4330 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4331 | resume_all_vcpus(); |
| 4332 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4333 | if (qemu_powerdown_requested()) { |
| 4334 | qemu_irq_raise(qemu_system_powerdown); |
| 4335 | } |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4336 | if ((r = qemu_vmstop_requested())) |
| 4337 | vm_stop(r); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4338 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4339 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4342 | static void version(void) |
| 4343 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4344 | 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] | 4345 | } |
| 4346 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4347 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4348 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4349 | version(); |
| 4350 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4351 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4352 | "'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] | 4353 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4354 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4355 | opt_help |
| 4356 | #define DEFHEADING(text) stringify(text) "\n" |
| 4357 | #include "qemu-options.h" |
| 4358 | #undef DEF |
| 4359 | #undef DEFHEADING |
| 4360 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4361 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4362 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4363 | "ctrl-alt-f toggle full screen\n" |
| 4364 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4365 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4366 | "\n" |
| 4367 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4368 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4369 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4370 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4371 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4372 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4373 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4374 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4375 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4376 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4377 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4380 | #define HAS_ARG 0x0001 |
| 4381 | |
| 4382 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4383 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4384 | opt_enum, |
| 4385 | #define DEFHEADING(text) |
| 4386 | #include "qemu-options.h" |
| 4387 | #undef DEF |
| 4388 | #undef DEFHEADING |
| 4389 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4390 | }; |
| 4391 | |
| 4392 | typedef struct QEMUOption { |
| 4393 | const char *name; |
| 4394 | int flags; |
| 4395 | int index; |
| 4396 | } QEMUOption; |
| 4397 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4398 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4399 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4400 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4401 | { option, opt_arg, opt_enum }, |
| 4402 | #define DEFHEADING(text) |
| 4403 | #include "qemu-options.h" |
| 4404 | #undef DEF |
| 4405 | #undef DEFHEADING |
| 4406 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4407 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4408 | }; |
| 4409 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4410 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4411 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4412 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4413 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4414 | { |
| 4415 | "pcspk", |
| 4416 | "PC speaker", |
| 4417 | 0, |
| 4418 | 1, |
| 4419 | { .init_isa = pcspk_audio_init } |
| 4420 | }, |
| 4421 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4422 | |
| 4423 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4424 | { |
| 4425 | "sb16", |
| 4426 | "Creative Sound Blaster 16", |
| 4427 | 0, |
| 4428 | 1, |
| 4429 | { .init_isa = SB16_init } |
| 4430 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4431 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4432 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4433 | #ifdef CONFIG_CS4231A |
| 4434 | { |
| 4435 | "cs4231a", |
| 4436 | "CS4231A", |
| 4437 | 0, |
| 4438 | 1, |
| 4439 | { .init_isa = cs4231a_init } |
| 4440 | }, |
| 4441 | #endif |
| 4442 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4443 | #ifdef CONFIG_ADLIB |
| 4444 | { |
| 4445 | "adlib", |
| 4446 | #ifdef HAS_YMF262 |
| 4447 | "Yamaha YMF262 (OPL3)", |
| 4448 | #else |
| 4449 | "Yamaha YM3812 (OPL2)", |
| 4450 | #endif |
| 4451 | 0, |
| 4452 | 1, |
| 4453 | { .init_isa = Adlib_init } |
| 4454 | }, |
| 4455 | #endif |
| 4456 | |
| 4457 | #ifdef CONFIG_GUS |
| 4458 | { |
| 4459 | "gus", |
| 4460 | "Gravis Ultrasound GF1", |
| 4461 | 0, |
| 4462 | 1, |
| 4463 | { .init_isa = GUS_init } |
| 4464 | }, |
| 4465 | #endif |
| 4466 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4467 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4468 | { |
| 4469 | "ac97", |
| 4470 | "Intel 82801AA AC97 Audio", |
| 4471 | 0, |
| 4472 | 0, |
| 4473 | { .init_pci = ac97_init } |
| 4474 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4475 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4476 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4477 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4478 | { |
| 4479 | "es1370", |
| 4480 | "ENSONIQ AudioPCI ES1370", |
| 4481 | 0, |
| 4482 | 0, |
| 4483 | { .init_pci = es1370_init } |
| 4484 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4485 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4486 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4487 | #endif /* HAS_AUDIO_CHOICE */ |
| 4488 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4489 | { NULL, NULL, 0, 0, { NULL } } |
| 4490 | }; |
| 4491 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4492 | static void select_soundhw (const char *optarg) |
| 4493 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4494 | struct soundhw *c; |
| 4495 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4496 | if (*optarg == '?') { |
| 4497 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4498 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4499 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4500 | for (c = soundhw; c->name; ++c) { |
| 4501 | printf ("%-11s %s\n", c->name, c->descr); |
| 4502 | } |
| 4503 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4504 | exit (*optarg != '?'); |
| 4505 | } |
| 4506 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4507 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4508 | const char *p; |
| 4509 | char *e; |
| 4510 | int bad_card = 0; |
| 4511 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4512 | if (!strcmp (optarg, "all")) { |
| 4513 | for (c = soundhw; c->name; ++c) { |
| 4514 | c->enabled = 1; |
| 4515 | } |
| 4516 | return; |
| 4517 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4518 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4519 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4520 | while (*p) { |
| 4521 | e = strchr (p, ','); |
| 4522 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4523 | |
| 4524 | for (c = soundhw; c->name; ++c) { |
| 4525 | if (!strncmp (c->name, p, l)) { |
| 4526 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4530 | |
| 4531 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4532 | if (l > 80) { |
| 4533 | fprintf (stderr, |
| 4534 | "Unknown sound card name (too big to show)\n"); |
| 4535 | } |
| 4536 | else { |
| 4537 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4538 | (int) l, p); |
| 4539 | } |
| 4540 | bad_card = 1; |
| 4541 | } |
| 4542 | p += l + (e != NULL); |
| 4543 | } |
| 4544 | |
| 4545 | if (bad_card) |
| 4546 | goto show_valid_cards; |
| 4547 | } |
| 4548 | } |
| 4549 | #endif |
| 4550 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4551 | static void select_vgahw (const char *p) |
| 4552 | { |
| 4553 | const char *opts; |
| 4554 | |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4555 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4556 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4557 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4558 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4559 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4560 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4561 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4562 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4563 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4564 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4565 | invalid_vga: |
| 4566 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4567 | exit(1); |
| 4568 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4569 | while (*opts) { |
| 4570 | const char *nextopt; |
| 4571 | |
| 4572 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4573 | opts = nextopt; |
| 4574 | if (strstart(opts, "dumb", &nextopt)) |
| 4575 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4576 | else if (strstart(opts, "precise", &nextopt)) |
| 4577 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4578 | else goto invalid_vga; |
| 4579 | } else goto invalid_vga; |
| 4580 | opts = nextopt; |
| 4581 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4584 | #ifdef TARGET_I386 |
| 4585 | static int balloon_parse(const char *arg) |
| 4586 | { |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4587 | QemuOpts *opts; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4588 | |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4589 | if (strcmp(arg, "none") == 0) { |
| 4590 | return 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4591 | } |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4592 | |
| 4593 | if (!strncmp(arg, "virtio", 6)) { |
| 4594 | if (arg[6] == ',') { |
| 4595 | /* have params -> parse them */ |
| 4596 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
| 4597 | if (!opts) |
| 4598 | return -1; |
| 4599 | } else { |
| 4600 | /* create empty opts */ |
| 4601 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4602 | } |
| 4603 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
| 4607 | return -1; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4608 | } |
| 4609 | #endif |
| 4610 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4611 | #ifdef _WIN32 |
| 4612 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4613 | { |
| 4614 | exit(STATUS_CONTROL_C_EXIT); |
| 4615 | return TRUE; |
| 4616 | } |
| 4617 | #endif |
| 4618 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4619 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4620 | { |
| 4621 | int ret; |
| 4622 | |
| 4623 | if(strlen(str) != 36) |
| 4624 | return -1; |
| 4625 | |
| 4626 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4627 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4628 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4629 | |
| 4630 | if(ret != 16) |
| 4631 | return -1; |
| 4632 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4633 | #ifdef TARGET_I386 |
| 4634 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4635 | #endif |
| 4636 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4637 | return 0; |
| 4638 | } |
| 4639 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4640 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4641 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4642 | #ifndef _WIN32 |
| 4643 | |
| 4644 | static void termsig_handler(int signal) |
| 4645 | { |
| 4646 | qemu_system_shutdown_request(); |
| 4647 | } |
| 4648 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4649 | static void sigchld_handler(int signal) |
| 4650 | { |
| 4651 | waitpid(-1, NULL, WNOHANG); |
| 4652 | } |
| 4653 | |
| 4654 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4655 | { |
| 4656 | struct sigaction act; |
| 4657 | |
| 4658 | memset(&act, 0, sizeof(act)); |
| 4659 | act.sa_handler = termsig_handler; |
| 4660 | sigaction(SIGINT, &act, NULL); |
| 4661 | sigaction(SIGHUP, &act, NULL); |
| 4662 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4663 | |
| 4664 | act.sa_handler = sigchld_handler; |
| 4665 | act.sa_flags = SA_NOCLDSTOP; |
| 4666 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | #endif |
| 4670 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4671 | #ifdef _WIN32 |
| 4672 | /* Look for support files in the same directory as the executable. */ |
| 4673 | static char *find_datadir(const char *argv0) |
| 4674 | { |
| 4675 | char *p; |
| 4676 | char buf[MAX_PATH]; |
| 4677 | DWORD len; |
| 4678 | |
| 4679 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4680 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4681 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | buf[len] = 0; |
| 4685 | p = buf + len - 1; |
| 4686 | while (p != buf && *p != '\\') |
| 4687 | p--; |
| 4688 | *p = 0; |
| 4689 | if (access(buf, R_OK) == 0) { |
| 4690 | return qemu_strdup(buf); |
| 4691 | } |
| 4692 | return NULL; |
| 4693 | } |
| 4694 | #else /* !_WIN32 */ |
| 4695 | |
| 4696 | /* Find a likely location for support files using the location of the binary. |
| 4697 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4698 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4699 | #define SHARE_SUFFIX "/share/qemu" |
| 4700 | #define BUILD_SUFFIX "/pc-bios" |
| 4701 | static char *find_datadir(const char *argv0) |
| 4702 | { |
| 4703 | char *dir; |
| 4704 | char *p = NULL; |
| 4705 | char *res; |
| 4706 | #ifdef PATH_MAX |
| 4707 | char buf[PATH_MAX]; |
| 4708 | #endif |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4709 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4710 | |
| 4711 | #if defined(__linux__) |
| 4712 | { |
| 4713 | int len; |
| 4714 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4715 | if (len > 0) { |
| 4716 | buf[len] = 0; |
| 4717 | p = buf; |
| 4718 | } |
| 4719 | } |
| 4720 | #elif defined(__FreeBSD__) |
| 4721 | { |
| 4722 | int len; |
| 4723 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4724 | if (len > 0) { |
| 4725 | buf[len] = 0; |
| 4726 | p = buf; |
| 4727 | } |
| 4728 | } |
| 4729 | #endif |
| 4730 | /* If we don't have any way of figuring out the actual executable |
| 4731 | location then try argv[0]. */ |
| 4732 | if (!p) { |
| 4733 | #ifdef PATH_MAX |
| 4734 | p = buf; |
| 4735 | #endif |
| 4736 | p = realpath(argv0, p); |
| 4737 | if (!p) { |
| 4738 | return NULL; |
| 4739 | } |
| 4740 | } |
| 4741 | dir = dirname(p); |
| 4742 | dir = dirname(dir); |
| 4743 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4744 | max_len = strlen(dir) + |
| 4745 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4746 | res = qemu_mallocz(max_len); |
| 4747 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4748 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4749 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4750 | if (access(res, R_OK)) { |
| 4751 | qemu_free(res); |
| 4752 | res = NULL; |
| 4753 | } |
| 4754 | } |
| 4755 | #ifndef PATH_MAX |
| 4756 | free(p); |
| 4757 | #endif |
| 4758 | return res; |
| 4759 | } |
| 4760 | #undef SHARE_SUFFIX |
| 4761 | #undef BUILD_SUFFIX |
| 4762 | #endif |
| 4763 | |
| 4764 | char *qemu_find_file(int type, const char *name) |
| 4765 | { |
| 4766 | int len; |
| 4767 | const char *subdir; |
| 4768 | char *buf; |
| 4769 | |
| 4770 | /* If name contains path separators then try it as a straight path. */ |
| 4771 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4772 | && access(name, R_OK) == 0) { |
| 4773 | return strdup(name); |
| 4774 | } |
| 4775 | switch (type) { |
| 4776 | case QEMU_FILE_TYPE_BIOS: |
| 4777 | subdir = ""; |
| 4778 | break; |
| 4779 | case QEMU_FILE_TYPE_KEYMAP: |
| 4780 | subdir = "keymaps/"; |
| 4781 | break; |
| 4782 | default: |
| 4783 | abort(); |
| 4784 | } |
| 4785 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4786 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4787 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4788 | if (access(buf, R_OK)) { |
| 4789 | qemu_free(buf); |
| 4790 | return NULL; |
| 4791 | } |
| 4792 | return buf; |
| 4793 | } |
| 4794 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4795 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4796 | { |
| 4797 | DeviceState *dev; |
| 4798 | |
| 4799 | dev = qdev_device_add(opts); |
| 4800 | if (!dev) |
| 4801 | return -1; |
| 4802 | return 0; |
| 4803 | } |
| 4804 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4805 | struct device_config { |
| 4806 | enum { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4807 | DEV_USB, /* -usbdevice */ |
| 4808 | DEV_BT, /* -bt */ |
| 4809 | } type; |
| 4810 | const char *cmdline; |
| 4811 | TAILQ_ENTRY(device_config) next; |
| 4812 | }; |
| 4813 | TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs); |
| 4814 | |
| 4815 | static void add_device_config(int type, const char *cmdline) |
| 4816 | { |
| 4817 | struct device_config *conf; |
| 4818 | |
| 4819 | conf = qemu_mallocz(sizeof(*conf)); |
| 4820 | conf->type = type; |
| 4821 | conf->cmdline = cmdline; |
| 4822 | TAILQ_INSERT_TAIL(&device_configs, conf, next); |
| 4823 | } |
| 4824 | |
| 4825 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4826 | { |
| 4827 | struct device_config *conf; |
| 4828 | int rc; |
| 4829 | |
| 4830 | TAILQ_FOREACH(conf, &device_configs, next) { |
| 4831 | if (conf->type != type) |
| 4832 | continue; |
| 4833 | rc = func(conf->cmdline); |
| 4834 | if (0 != rc) |
| 4835 | return rc; |
| 4836 | } |
| 4837 | return 0; |
| 4838 | } |
| 4839 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4840 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4841 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4842 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4843 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4844 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4845 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4846 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4847 | const char *kernel_filename, *kernel_cmdline; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 4848 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4849 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4850 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4851 | int cyls, heads, secs, translation; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4852 | const char *net_clients[MAX_NET_CLIENTS]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4853 | int nb_net_clients; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4854 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4855 | int optind; |
| 4856 | const char *r, *optarg; |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 4857 | CharDriverState *monitor_hd = NULL; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4858 | const char *monitor_device; |
| 4859 | const char *serial_devices[MAX_SERIAL_PORTS]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4860 | int serial_device_index; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4861 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4862 | int parallel_device_index; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4863 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
| 4864 | int virtio_console_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4865 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4866 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4867 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4868 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4869 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4870 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4871 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4872 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4873 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4874 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4875 | int fd = 0; |
| 4876 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4877 | const char *chroot_dir = NULL; |
| 4878 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4879 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4880 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4881 | int show_vnc_port = 0; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4882 | |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 4883 | qemu_errors_to_file(stderr); |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4884 | qemu_cache_utils_init(envp); |
| 4885 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4886 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4887 | #ifndef _WIN32 |
| 4888 | { |
| 4889 | struct sigaction act; |
| 4890 | sigfillset(&act.sa_mask); |
| 4891 | act.sa_flags = 0; |
| 4892 | act.sa_handler = SIG_IGN; |
| 4893 | sigaction(SIGPIPE, &act, NULL); |
| 4894 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4895 | #else |
| 4896 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4897 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4898 | QEMU to run on a single CPU */ |
| 4899 | { |
| 4900 | HANDLE h; |
| 4901 | DWORD mask, smask; |
| 4902 | int i; |
| 4903 | h = GetCurrentProcess(); |
| 4904 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4905 | for(i = 0; i < 32; i++) { |
| 4906 | if (mask & (1 << i)) |
| 4907 | break; |
| 4908 | } |
| 4909 | if (i != 32) { |
| 4910 | mask = 1 << i; |
| 4911 | SetProcessAffinityMask(h, mask); |
| 4912 | } |
| 4913 | } |
| 4914 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4915 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4916 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4917 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4918 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4919 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4920 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4921 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4922 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4923 | kernel_filename = NULL; |
| 4924 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4925 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4926 | translation = BIOS_ATA_TRANSLATION_AUTO; |
aliguori | d47d13b | 2009-03-05 23:00:53 +0000 | [diff] [blame] | 4927 | monitor_device = "vc:80Cx24C"; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4928 | |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 4929 | serial_devices[0] = "vc:80Cx24C"; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4930 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4931 | serial_devices[i] = NULL; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4932 | serial_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4933 | |
aliguori | 8290edd | 2009-02-27 20:14:29 +0000 | [diff] [blame] | 4934 | parallel_devices[0] = "vc:80Cx24C"; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4935 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4936 | parallel_devices[i] = NULL; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4937 | parallel_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4938 | |
aliguori | 1b8fc81 | 2009-02-27 20:01:39 +0000 | [diff] [blame] | 4939 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4940 | virtio_consoles[i] = NULL; |
| 4941 | virtio_console_index = 0; |
| 4942 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4943 | for (i = 0; i < MAX_NODES; i++) { |
| 4944 | node_mem[i] = 0; |
| 4945 | node_cpumask[i] = 0; |
| 4946 | } |
| 4947 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4948 | nb_net_clients = 0; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4949 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4950 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4951 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4952 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4953 | autostart= 1; |
| 4954 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4955 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4956 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4957 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4958 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4959 | r = argv[optind]; |
| 4960 | if (r[0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4961 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4962 | } else { |
| 4963 | const QEMUOption *popt; |
| 4964 | |
| 4965 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 4966 | /* Treat --foo the same as -foo. */ |
| 4967 | if (r[1] == '-') |
| 4968 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4969 | popt = qemu_options; |
| 4970 | for(;;) { |
| 4971 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4972 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4973 | argv[0], r); |
| 4974 | exit(1); |
| 4975 | } |
| 4976 | if (!strcmp(popt->name, r + 1)) |
| 4977 | break; |
| 4978 | popt++; |
| 4979 | } |
| 4980 | if (popt->flags & HAS_ARG) { |
| 4981 | if (optind >= argc) { |
| 4982 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4983 | argv[0], r); |
| 4984 | exit(1); |
| 4985 | } |
| 4986 | optarg = argv[optind++]; |
| 4987 | } else { |
| 4988 | optarg = NULL; |
| 4989 | } |
| 4990 | |
| 4991 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4992 | case QEMU_OPTION_M: |
| 4993 | machine = find_machine(optarg); |
| 4994 | if (!machine) { |
| 4995 | QEMUMachine *m; |
| 4996 | printf("Supported machines are:\n"); |
| 4997 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4998 | if (m->alias) |
| 4999 | printf("%-10s %s (alias of %s)\n", |
| 5000 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5001 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5002 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 5003 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5004 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5005 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5006 | } |
| 5007 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5008 | case QEMU_OPTION_cpu: |
| 5009 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5010 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 5011 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 5012 | #if defined(cpu_list) |
| 5013 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5014 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5015 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5016 | } else { |
| 5017 | cpu_model = optarg; |
| 5018 | } |
| 5019 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5020 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5021 | initrd_filename = optarg; |
| 5022 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5023 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5024 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5025 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5026 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5027 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5028 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5029 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5030 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 5031 | ",trans=lba" : |
| 5032 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 5033 | ",trans=none" : ""); |
| 5034 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5035 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5036 | case QEMU_OPTION_hdc: |
| 5037 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5038 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5039 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5040 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5041 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5042 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 5043 | case QEMU_OPTION_set: |
| 5044 | if (qemu_set_option(optarg) != 0) |
| 5045 | exit(1); |
| 5046 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5047 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5048 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5049 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5050 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5051 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5052 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5053 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5054 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5055 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5056 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 5057 | snapshot = 1; |
| 5058 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5059 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5060 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5061 | const char *p; |
| 5062 | p = optarg; |
| 5063 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5064 | if (cyls < 1 || cyls > 16383) |
| 5065 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5066 | if (*p != ',') |
| 5067 | goto chs_fail; |
| 5068 | p++; |
| 5069 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5070 | if (heads < 1 || heads > 16) |
| 5071 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5072 | if (*p != ',') |
| 5073 | goto chs_fail; |
| 5074 | p++; |
| 5075 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5076 | if (secs < 1 || secs > 63) |
| 5077 | goto chs_fail; |
| 5078 | if (*p == ',') { |
| 5079 | p++; |
| 5080 | if (!strcmp(p, "none")) |
| 5081 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 5082 | else if (!strcmp(p, "lba")) |
| 5083 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 5084 | else if (!strcmp(p, "auto")) |
| 5085 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5086 | else |
| 5087 | goto chs_fail; |
| 5088 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5089 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5090 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 5091 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5092 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5093 | if (hda_opts != NULL) { |
| 5094 | char num[16]; |
| 5095 | snprintf(num, sizeof(num), "%d", cyls); |
| 5096 | qemu_opt_set(hda_opts, "cyls", num); |
| 5097 | snprintf(num, sizeof(num), "%d", heads); |
| 5098 | qemu_opt_set(hda_opts, "heads", num); |
| 5099 | snprintf(num, sizeof(num), "%d", secs); |
| 5100 | qemu_opt_set(hda_opts, "secs", num); |
| 5101 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 5102 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 5103 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 5104 | qemu_opt_set(hda_opts, "trans", "none"); |
| 5105 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5106 | } |
| 5107 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5108 | case QEMU_OPTION_numa: |
| 5109 | if (nb_numa_nodes >= MAX_NODES) { |
| 5110 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 5111 | exit(1); |
| 5112 | } |
| 5113 | numa_add(optarg); |
| 5114 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5115 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5116 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5117 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5118 | #ifdef CONFIG_CURSES |
| 5119 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5120 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5121 | break; |
| 5122 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5123 | case QEMU_OPTION_portrait: |
| 5124 | graphic_rotate = 1; |
| 5125 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5126 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5127 | kernel_filename = optarg; |
| 5128 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5129 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5130 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5131 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5132 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5133 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5134 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5135 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5136 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5137 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5138 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5139 | }; |
| 5140 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5141 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5142 | int legacy = 0; |
| 5143 | |
| 5144 | if (!strchr(optarg, '=')) { |
| 5145 | legacy = 1; |
| 5146 | pstrcpy(buf, sizeof(buf), optarg); |
| 5147 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5148 | fprintf(stderr, |
| 5149 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5150 | buf, optarg); |
| 5151 | exit(1); |
| 5152 | } |
| 5153 | |
| 5154 | if (legacy || |
| 5155 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5156 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5157 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5158 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5159 | if (!legacy) { |
| 5160 | if (get_param_value(buf, sizeof(buf), |
| 5161 | "once", optarg)) { |
| 5162 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5163 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5164 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5165 | qemu_register_reset(restore_boot_devices, |
| 5166 | standard_boot_devices); |
| 5167 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5168 | if (get_param_value(buf, sizeof(buf), |
| 5169 | "menu", optarg)) { |
| 5170 | if (!strcmp(buf, "on")) { |
| 5171 | boot_menu = 1; |
| 5172 | } else if (!strcmp(buf, "off")) { |
| 5173 | boot_menu = 0; |
| 5174 | } else { |
| 5175 | fprintf(stderr, |
| 5176 | "qemu: invalid option value '%s'\n", |
| 5177 | buf); |
| 5178 | exit(1); |
| 5179 | } |
| 5180 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5181 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5182 | } |
| 5183 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5184 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5185 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5186 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5187 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5188 | #ifdef TARGET_I386 |
| 5189 | case QEMU_OPTION_no_fd_bootchk: |
| 5190 | fd_bootchk = 0; |
| 5191 | break; |
| 5192 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5193 | case QEMU_OPTION_net: |
| 5194 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 5195 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5196 | exit(1); |
| 5197 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5198 | net_clients[nb_net_clients] = optarg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5199 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5200 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5201 | #ifdef CONFIG_SLIRP |
| 5202 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5203 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5204 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5205 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5206 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5207 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5208 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5209 | case QEMU_OPTION_smb: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5210 | net_slirp_smb(optarg); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5211 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5212 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5213 | case QEMU_OPTION_redir: |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5214 | net_slirp_redir(optarg); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5215 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5216 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5217 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5218 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5219 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5220 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5221 | case QEMU_OPTION_audio_help: |
| 5222 | AUD_help (); |
| 5223 | exit (0); |
| 5224 | break; |
| 5225 | case QEMU_OPTION_soundhw: |
| 5226 | select_soundhw (optarg); |
| 5227 | break; |
| 5228 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5229 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5230 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5231 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5232 | case QEMU_OPTION_version: |
| 5233 | version(); |
| 5234 | exit(0); |
| 5235 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5236 | case QEMU_OPTION_m: { |
| 5237 | uint64_t value; |
| 5238 | char *ptr; |
| 5239 | |
| 5240 | value = strtoul(optarg, &ptr, 10); |
| 5241 | switch (*ptr) { |
| 5242 | case 0: case 'M': case 'm': |
| 5243 | value <<= 20; |
| 5244 | break; |
| 5245 | case 'G': case 'g': |
| 5246 | value <<= 30; |
| 5247 | break; |
| 5248 | default: |
| 5249 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5250 | exit(1); |
| 5251 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5252 | |
| 5253 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5254 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5255 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5256 | exit(1); |
| 5257 | } |
| 5258 | if (value != (uint64_t)(ram_addr_t)value) { |
| 5259 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5260 | exit(1); |
| 5261 | } |
| 5262 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5263 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5264 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5265 | case QEMU_OPTION_d: |
| 5266 | { |
| 5267 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5268 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5269 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5270 | mask = cpu_str_to_log_mask(optarg); |
| 5271 | if (!mask) { |
| 5272 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5273 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5274 | printf("%-10s %s\n", item->name, item->help); |
| 5275 | } |
| 5276 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5277 | } |
| 5278 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5279 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5280 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5281 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5282 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5283 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5284 | case QEMU_OPTION_gdb: |
| 5285 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5286 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5287 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5288 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5289 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5290 | case QEMU_OPTION_bios: |
| 5291 | bios_name = optarg; |
| 5292 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5293 | case QEMU_OPTION_singlestep: |
| 5294 | singlestep = 1; |
| 5295 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5296 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5297 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5298 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5299 | #ifndef _WIN32 |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5300 | case QEMU_OPTION_k: |
| 5301 | keyboard_layout = optarg; |
| 5302 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5303 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5304 | case QEMU_OPTION_localtime: |
| 5305 | rtc_utc = 0; |
| 5306 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5307 | case QEMU_OPTION_vga: |
| 5308 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5309 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5310 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5311 | case QEMU_OPTION_g: |
| 5312 | { |
| 5313 | const char *p; |
| 5314 | int w, h, depth; |
| 5315 | p = optarg; |
| 5316 | w = strtol(p, (char **)&p, 10); |
| 5317 | if (w <= 0) { |
| 5318 | graphic_error: |
| 5319 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5320 | exit(1); |
| 5321 | } |
| 5322 | if (*p != 'x') |
| 5323 | goto graphic_error; |
| 5324 | p++; |
| 5325 | h = strtol(p, (char **)&p, 10); |
| 5326 | if (h <= 0) |
| 5327 | goto graphic_error; |
| 5328 | if (*p == 'x') { |
| 5329 | p++; |
| 5330 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5331 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5332 | depth != 24 && depth != 32) |
| 5333 | goto graphic_error; |
| 5334 | } else if (*p == '\0') { |
| 5335 | depth = graphic_depth; |
| 5336 | } else { |
| 5337 | goto graphic_error; |
| 5338 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5339 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5340 | graphic_width = w; |
| 5341 | graphic_height = h; |
| 5342 | graphic_depth = depth; |
| 5343 | } |
| 5344 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5345 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5346 | case QEMU_OPTION_echr: |
| 5347 | { |
| 5348 | char *r; |
| 5349 | term_escape_char = strtol(optarg, &r, 0); |
| 5350 | if (r == optarg) |
| 5351 | printf("Bad argument to echr\n"); |
| 5352 | break; |
| 5353 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5354 | case QEMU_OPTION_monitor: |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5355 | monitor_device = optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5356 | break; |
| 5357 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5358 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 5359 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 5360 | exit(1); |
| 5361 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5362 | serial_devices[serial_device_index] = optarg; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5363 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5364 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5365 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5366 | if (watchdog) { |
| 5367 | fprintf(stderr, |
| 5368 | "qemu: only one watchdog option may be given\n"); |
| 5369 | return 1; |
| 5370 | } |
| 5371 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5372 | break; |
| 5373 | case QEMU_OPTION_watchdog_action: |
| 5374 | if (select_watchdog_action(optarg) == -1) { |
| 5375 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5376 | exit(1); |
| 5377 | } |
| 5378 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5379 | case QEMU_OPTION_virtiocon: |
| 5380 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { |
| 5381 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 5382 | exit(1); |
| 5383 | } |
| 5384 | virtio_consoles[virtio_console_index] = optarg; |
| 5385 | virtio_console_index++; |
| 5386 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5387 | case QEMU_OPTION_parallel: |
| 5388 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 5389 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 5390 | exit(1); |
| 5391 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5392 | parallel_devices[parallel_device_index] = optarg; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5393 | parallel_device_index++; |
| 5394 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5395 | case QEMU_OPTION_loadvm: |
| 5396 | loadvm = optarg; |
| 5397 | break; |
| 5398 | case QEMU_OPTION_full_screen: |
| 5399 | full_screen = 1; |
| 5400 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5401 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5402 | case QEMU_OPTION_no_frame: |
| 5403 | no_frame = 1; |
| 5404 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5405 | case QEMU_OPTION_alt_grab: |
| 5406 | alt_grab = 1; |
| 5407 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5408 | case QEMU_OPTION_no_quit: |
| 5409 | no_quit = 1; |
| 5410 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5411 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5412 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5413 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5414 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5415 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5416 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5417 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5418 | #ifdef TARGET_I386 |
| 5419 | case QEMU_OPTION_win2k_hack: |
| 5420 | win2k_install_hack = 1; |
| 5421 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5422 | case QEMU_OPTION_rtc_td_hack: |
| 5423 | rtc_td_hack = 1; |
| 5424 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5425 | case QEMU_OPTION_acpitable: |
| 5426 | if(acpi_table_add(optarg) < 0) { |
| 5427 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5428 | exit(1); |
| 5429 | } |
| 5430 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5431 | case QEMU_OPTION_smbios: |
| 5432 | if(smbios_entry_add(optarg) < 0) { |
| 5433 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5434 | exit(1); |
| 5435 | } |
| 5436 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5437 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5438 | #ifdef CONFIG_KVM |
| 5439 | case QEMU_OPTION_enable_kvm: |
| 5440 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5441 | break; |
| 5442 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5443 | case QEMU_OPTION_usb: |
| 5444 | usb_enabled = 1; |
| 5445 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5446 | case QEMU_OPTION_usbdevice: |
| 5447 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5448 | add_device_config(DEV_USB, optarg); |
| 5449 | break; |
| 5450 | case QEMU_OPTION_device: |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5451 | opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver"); |
| 5452 | if (!opts) { |
| 5453 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5454 | exit(1); |
| 5455 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5456 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5457 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5458 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5459 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5460 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5461 | exit(1); |
| 5462 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5463 | if (max_cpus < smp_cpus) { |
| 5464 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5465 | "smp\n"); |
| 5466 | exit(1); |
| 5467 | } |
| 5468 | if (max_cpus > 255) { |
| 5469 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5470 | exit(1); |
| 5471 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5472 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5473 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5474 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5475 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5476 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5477 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5478 | case QEMU_OPTION_no_acpi: |
| 5479 | acpi_enabled = 0; |
| 5480 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5481 | case QEMU_OPTION_no_hpet: |
| 5482 | no_hpet = 1; |
| 5483 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5484 | case QEMU_OPTION_balloon: |
| 5485 | if (balloon_parse(optarg) < 0) { |
| 5486 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5487 | exit(1); |
| 5488 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5489 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5490 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5491 | case QEMU_OPTION_no_reboot: |
| 5492 | no_reboot = 1; |
| 5493 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5494 | case QEMU_OPTION_no_shutdown: |
| 5495 | no_shutdown = 1; |
| 5496 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5497 | case QEMU_OPTION_show_cursor: |
| 5498 | cursor_hide = 0; |
| 5499 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5500 | case QEMU_OPTION_uuid: |
| 5501 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5502 | fprintf(stderr, "Fail to parse UUID string." |
| 5503 | " Wrong format.\n"); |
| 5504 | exit(1); |
| 5505 | } |
| 5506 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5507 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5508 | case QEMU_OPTION_daemonize: |
| 5509 | daemonize = 1; |
| 5510 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5511 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5512 | case QEMU_OPTION_option_rom: |
| 5513 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5514 | fprintf(stderr, "Too many option ROMs\n"); |
| 5515 | exit(1); |
| 5516 | } |
| 5517 | option_rom[nb_option_roms] = optarg; |
| 5518 | nb_option_roms++; |
| 5519 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5520 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5521 | case QEMU_OPTION_semihosting: |
| 5522 | semihosting_enabled = 1; |
| 5523 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5524 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5525 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5526 | qemu_name = qemu_strdup(optarg); |
| 5527 | { |
| 5528 | char *p = strchr(qemu_name, ','); |
| 5529 | if (p != NULL) { |
| 5530 | *p++ = 0; |
| 5531 | if (strncmp(p, "process=", 8)) { |
| 5532 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5533 | exit(1); |
| 5534 | } |
| 5535 | p += 8; |
| 5536 | set_proc_name(p); |
| 5537 | } |
| 5538 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5539 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5540 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5541 | case QEMU_OPTION_prom_env: |
| 5542 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5543 | fprintf(stderr, "Too many prom variables\n"); |
| 5544 | exit(1); |
| 5545 | } |
| 5546 | prom_envs[nb_prom_envs] = optarg; |
| 5547 | nb_prom_envs++; |
| 5548 | break; |
| 5549 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5550 | #ifdef TARGET_ARM |
| 5551 | case QEMU_OPTION_old_param: |
| 5552 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5553 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5554 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5555 | case QEMU_OPTION_clock: |
| 5556 | configure_alarms(optarg); |
| 5557 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5558 | case QEMU_OPTION_startdate: |
| 5559 | { |
| 5560 | struct tm tm; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5561 | time_t rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5562 | if (!strcmp(optarg, "now")) { |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5563 | rtc_date_offset = -1; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5564 | } else { |
| 5565 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
| 5566 | &tm.tm_year, |
| 5567 | &tm.tm_mon, |
| 5568 | &tm.tm_mday, |
| 5569 | &tm.tm_hour, |
| 5570 | &tm.tm_min, |
| 5571 | &tm.tm_sec) == 6) { |
| 5572 | /* OK */ |
| 5573 | } else if (sscanf(optarg, "%d-%d-%d", |
| 5574 | &tm.tm_year, |
| 5575 | &tm.tm_mon, |
| 5576 | &tm.tm_mday) == 3) { |
| 5577 | tm.tm_hour = 0; |
| 5578 | tm.tm_min = 0; |
| 5579 | tm.tm_sec = 0; |
| 5580 | } else { |
| 5581 | goto date_fail; |
| 5582 | } |
| 5583 | tm.tm_year -= 1900; |
| 5584 | tm.tm_mon--; |
bellard | 3c6b208 | 2007-11-10 19:36:39 +0000 | [diff] [blame] | 5585 | rtc_start_date = mktimegm(&tm); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5586 | if (rtc_start_date == -1) { |
| 5587 | date_fail: |
| 5588 | fprintf(stderr, "Invalid date format. Valid format are:\n" |
| 5589 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 5590 | exit(1); |
| 5591 | } |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5592 | rtc_date_offset = time(NULL) - rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5593 | } |
| 5594 | } |
| 5595 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5596 | case QEMU_OPTION_tb_size: |
| 5597 | tb_size = strtol(optarg, NULL, 0); |
| 5598 | if (tb_size < 0) |
| 5599 | tb_size = 0; |
| 5600 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5601 | case QEMU_OPTION_icount: |
| 5602 | use_icount = 1; |
| 5603 | if (strcmp(optarg, "auto") == 0) { |
| 5604 | icount_time_shift = -1; |
| 5605 | } else { |
| 5606 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5607 | } |
| 5608 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5609 | case QEMU_OPTION_incoming: |
| 5610 | incoming = optarg; |
| 5611 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5612 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5613 | case QEMU_OPTION_chroot: |
| 5614 | chroot_dir = optarg; |
| 5615 | break; |
| 5616 | case QEMU_OPTION_runas: |
| 5617 | run_as = optarg; |
| 5618 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5619 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5620 | #ifdef CONFIG_XEN |
| 5621 | case QEMU_OPTION_xen_domid: |
| 5622 | xen_domid = atoi(optarg); |
| 5623 | break; |
| 5624 | case QEMU_OPTION_xen_create: |
| 5625 | xen_mode = XEN_CREATE; |
| 5626 | break; |
| 5627 | case QEMU_OPTION_xen_attach: |
| 5628 | xen_mode = XEN_ATTACH; |
| 5629 | break; |
| 5630 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5631 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5632 | } |
| 5633 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5634 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5635 | /* If no data_dir is specified then try to find it relative to the |
| 5636 | executable path. */ |
| 5637 | if (!data_dir) { |
| 5638 | data_dir = find_datadir(argv[0]); |
| 5639 | } |
| 5640 | /* If all else fails use the install patch specified when building. */ |
| 5641 | if (!data_dir) { |
| 5642 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5643 | } |
| 5644 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5645 | /* |
| 5646 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5647 | * specify a max_cpus value. |
| 5648 | */ |
| 5649 | if (!max_cpus) |
| 5650 | max_cpus = smp_cpus; |
| 5651 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5652 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5653 | if (smp_cpus > machine->max_cpus) { |
| 5654 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5655 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5656 | machine->max_cpus); |
| 5657 | exit(1); |
| 5658 | } |
| 5659 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5660 | if (display_type == DT_NOGRAPHIC) { |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5661 | if (serial_device_index == 0) |
| 5662 | serial_devices[0] = "stdio"; |
| 5663 | if (parallel_device_index == 0) |
| 5664 | parallel_devices[0] = "null"; |
| 5665 | if (strncmp(monitor_device, "vc", 2) == 0) |
| 5666 | monitor_device = "stdio"; |
| 5667 | } |
| 5668 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5669 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5670 | if (daemonize) { |
| 5671 | pid_t pid; |
| 5672 | |
| 5673 | if (pipe(fds) == -1) |
| 5674 | exit(1); |
| 5675 | |
| 5676 | pid = fork(); |
| 5677 | if (pid > 0) { |
| 5678 | uint8_t status; |
| 5679 | ssize_t len; |
| 5680 | |
| 5681 | close(fds[1]); |
| 5682 | |
| 5683 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5684 | len = read(fds[0], &status, 1); |
| 5685 | if (len == -1 && (errno == EINTR)) |
| 5686 | goto again; |
| 5687 | |
| 5688 | if (len != 1) |
| 5689 | exit(1); |
| 5690 | else if (status == 1) { |
| 5691 | fprintf(stderr, "Could not acquire pidfile\n"); |
| 5692 | exit(1); |
| 5693 | } else |
| 5694 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5695 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5696 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5697 | |
| 5698 | setsid(); |
| 5699 | |
| 5700 | pid = fork(); |
| 5701 | if (pid > 0) |
| 5702 | exit(0); |
| 5703 | else if (pid < 0) |
| 5704 | exit(1); |
| 5705 | |
| 5706 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5707 | |
| 5708 | signal(SIGTSTP, SIG_IGN); |
| 5709 | signal(SIGTTOU, SIG_IGN); |
| 5710 | signal(SIGTTIN, SIG_IGN); |
| 5711 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5712 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5713 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5714 | if (daemonize) { |
| 5715 | uint8_t status = 1; |
| 5716 | write(fds[1], &status, 1); |
| 5717 | } else |
| 5718 | fprintf(stderr, "Could not acquire pid file\n"); |
| 5719 | exit(1); |
| 5720 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5721 | #endif |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5722 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5723 | if (qemu_init_main_loop()) { |
| 5724 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5725 | exit(1); |
| 5726 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5727 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5728 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5729 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5730 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5731 | exit(1); |
| 5732 | } |
| 5733 | |
| 5734 | if (!linux_boot && initrd_filename != NULL) { |
| 5735 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5736 | exit(1); |
| 5737 | } |
| 5738 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5739 | #ifndef _WIN32 |
| 5740 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5741 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5742 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5743 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5744 | init_timers(); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5745 | if (init_timer_alarm() < 0) { |
| 5746 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5747 | exit(1); |
| 5748 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5749 | if (use_icount && icount_time_shift < 0) { |
| 5750 | use_icount = 2; |
| 5751 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5752 | It will be corrected fairly quickly anyway. */ |
| 5753 | icount_time_shift = 3; |
| 5754 | init_icount_adjust(); |
| 5755 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5756 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5757 | #ifdef _WIN32 |
| 5758 | socket_init(); |
| 5759 | #endif |
| 5760 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5761 | /* init network clients */ |
| 5762 | if (nb_net_clients == 0) { |
| 5763 | /* if no clients, we use a default config */ |
aliguori | f441b28 | 2008-08-28 20:05:14 +0000 | [diff] [blame] | 5764 | net_clients[nb_net_clients++] = "nic"; |
| 5765 | #ifdef CONFIG_SLIRP |
| 5766 | net_clients[nb_net_clients++] = "user"; |
| 5767 | #endif |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5770 | for(i = 0;i < nb_net_clients; i++) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 5771 | if (net_client_parse(net_clients[i]) < 0) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5772 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5773 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5774 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5775 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5776 | net_set_boot_mask(net_boot); |
| 5777 | |
| 5778 | net_client_check(); |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 5779 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5780 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5781 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5782 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5783 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5784 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5785 | if (ram_size == 0) |
| 5786 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5787 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5788 | /* init the dynamic translator */ |
| 5789 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5790 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 5791 | bdrv_init(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5792 | |
| 5793 | /* we always create the cdrom drive, even if no disk is there */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5794 | drive_add(NULL, CDROM_ALIAS); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5795 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5796 | /* we always create at least one floppy */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5797 | drive_add(NULL, FD_ALIAS, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5798 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5799 | /* 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] | 5800 | drive_add(NULL, SD_ALIAS); |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5801 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5802 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5803 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5804 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5805 | 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] | 5806 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5807 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5808 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 5809 | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5810 | |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5811 | #ifndef _WIN32 |
| 5812 | /* must be after terminal init, SDL library changes signal handlers */ |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 5813 | sighandler_setup(); |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5814 | #endif |
| 5815 | |
| 5816 | /* Maintain compatibility with multiple stdio monitors */ |
| 5817 | if (!strcmp(monitor_device,"stdio")) { |
| 5818 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5819 | const char *devname = serial_devices[i]; |
| 5820 | if (devname && !strcmp(devname,"mon:stdio")) { |
| 5821 | monitor_device = NULL; |
| 5822 | break; |
| 5823 | } else if (devname && !strcmp(devname,"stdio")) { |
| 5824 | monitor_device = NULL; |
| 5825 | serial_devices[i] = "mon:stdio"; |
| 5826 | break; |
| 5827 | } |
| 5828 | } |
| 5829 | } |
| 5830 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5831 | if (nb_numa_nodes > 0) { |
| 5832 | int i; |
| 5833 | |
| 5834 | if (nb_numa_nodes > smp_cpus) { |
| 5835 | nb_numa_nodes = smp_cpus; |
| 5836 | } |
| 5837 | |
| 5838 | /* If no memory size if given for any node, assume the default case |
| 5839 | * and distribute the available memory equally across all nodes |
| 5840 | */ |
| 5841 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5842 | if (node_mem[i] != 0) |
| 5843 | break; |
| 5844 | } |
| 5845 | if (i == nb_numa_nodes) { |
| 5846 | uint64_t usedmem = 0; |
| 5847 | |
| 5848 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5849 | * the final node gets the rest. |
| 5850 | */ |
| 5851 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5852 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5853 | usedmem += node_mem[i]; |
| 5854 | } |
| 5855 | node_mem[i] = ram_size - usedmem; |
| 5856 | } |
| 5857 | |
| 5858 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5859 | if (node_cpumask[i] != 0) |
| 5860 | break; |
| 5861 | } |
| 5862 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5863 | * must cope with this anyway, because there are BIOSes out there in |
| 5864 | * real machines which also use this scheme. |
| 5865 | */ |
| 5866 | if (i == nb_numa_nodes) { |
| 5867 | for (i = 0; i < smp_cpus; i++) { |
| 5868 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5869 | } |
| 5870 | } |
| 5871 | } |
| 5872 | |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5873 | if (kvm_enabled()) { |
| 5874 | int ret; |
| 5875 | |
| 5876 | ret = kvm_init(smp_cpus); |
| 5877 | if (ret < 0) { |
| 5878 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5879 | exit(1); |
| 5880 | } |
| 5881 | } |
| 5882 | |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 5883 | if (monitor_device) { |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5884 | monitor_hd = qemu_chr_open("monitor", monitor_device, NULL); |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 5885 | if (!monitor_hd) { |
| 5886 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 5887 | exit(1); |
| 5888 | } |
| 5889 | } |
| 5890 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5891 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5892 | const char *devname = serial_devices[i]; |
| 5893 | if (devname && strcmp(devname, "none")) { |
| 5894 | char label[32]; |
| 5895 | snprintf(label, sizeof(label), "serial%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5896 | serial_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5897 | if (!serial_hds[i]) { |
| 5898 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
| 5899 | devname); |
| 5900 | exit(1); |
| 5901 | } |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
| 5906 | const char *devname = parallel_devices[i]; |
| 5907 | if (devname && strcmp(devname, "none")) { |
| 5908 | char label[32]; |
| 5909 | snprintf(label, sizeof(label), "parallel%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5910 | parallel_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5911 | if (!parallel_hds[i]) { |
| 5912 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
| 5913 | devname); |
| 5914 | exit(1); |
| 5915 | } |
| 5916 | } |
| 5917 | } |
| 5918 | |
| 5919 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 5920 | const char *devname = virtio_consoles[i]; |
| 5921 | if (devname && strcmp(devname, "none")) { |
| 5922 | char label[32]; |
| 5923 | snprintf(label, sizeof(label), "virtcon%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5924 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5925 | if (!virtcon_hds[i]) { |
| 5926 | fprintf(stderr, "qemu: could not open virtio console '%s'\n", |
| 5927 | devname); |
| 5928 | exit(1); |
| 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5933 | module_call_init(MODULE_INIT_DEVICE); |
| 5934 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5935 | if (watchdog) { |
| 5936 | i = select_watchdog(watchdog); |
| 5937 | if (i > 0) |
| 5938 | exit (i == 1 ? 1 : 0); |
| 5939 | } |
| 5940 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5941 | if (machine->compat_props) { |
| 5942 | qdev_prop_register_compat(machine->compat_props); |
| 5943 | } |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5944 | machine->init(ram_size, boot_devices, |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5945 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5946 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5947 | |
| 5948 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5949 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5950 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5951 | env->numa_node = i; |
| 5952 | } |
| 5953 | } |
| 5954 | } |
| 5955 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5956 | current_machine = machine; |
| 5957 | |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5958 | /* init USB devices */ |
| 5959 | if (usb_enabled) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5960 | foreach_device_config(DEV_USB, usb_parse); |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5963 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5964 | 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] | 5965 | exit(1); |
| 5966 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5967 | if (!display_state) |
| 5968 | dumb_display_init(); |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5969 | /* just use the first displaystate for the moment */ |
| 5970 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5971 | |
| 5972 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5973 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5974 | display_type = DT_SDL; |
| 5975 | #else |
| 5976 | display_type = DT_VNC; |
| 5977 | vnc_display = "localhost:0,to=99"; |
| 5978 | show_vnc_port = 1; |
| 5979 | #endif |
| 5980 | } |
| 5981 | |
| 5982 | |
| 5983 | switch (display_type) { |
| 5984 | case DT_NOGRAPHIC: |
| 5985 | break; |
| 5986 | #if defined(CONFIG_CURSES) |
| 5987 | case DT_CURSES: |
| 5988 | curses_display_init(ds, full_screen); |
| 5989 | break; |
| 5990 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5991 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5992 | case DT_SDL: |
| 5993 | sdl_display_init(ds, full_screen, no_frame); |
| 5994 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5995 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5996 | case DT_SDL: |
| 5997 | cocoa_display_init(ds, full_screen); |
| 5998 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5999 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6000 | case DT_VNC: |
| 6001 | vnc_display_init(ds); |
| 6002 | if (vnc_display_open(ds, vnc_display) < 0) |
| 6003 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6004 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6005 | if (show_vnc_port) { |
| 6006 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6007 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6008 | break; |
| 6009 | default: |
| 6010 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6011 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 6012 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 6013 | |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6014 | dcl = ds->listeners; |
| 6015 | while (dcl != NULL) { |
| 6016 | if (dcl->dpy_refresh != NULL) { |
| 6017 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 6018 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6019 | } |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6020 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6021 | } |
aliguori | 3023f332 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6022 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6023 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 6024 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 6025 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 6026 | } |
| 6027 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6028 | text_consoles_set_display(display_state); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 6029 | qemu_chr_initial_reset(); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6030 | |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 6031 | if (monitor_device && monitor_hd) |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 6032 | monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6033 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6034 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 6035 | const char *devname = serial_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 6036 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 6037 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 6038 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6039 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6040 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6041 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6042 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 6043 | const char *devname = parallel_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 6044 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 6045 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 6046 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6047 | } |
| 6048 | } |
| 6049 | |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6050 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 6051 | const char *devname = virtio_consoles[i]; |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6052 | if (virtcon_hds[i] && devname) { |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6053 | if (strstart(devname, "vc", 0)) |
| 6054 | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i); |
| 6055 | } |
| 6056 | } |
| 6057 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 6058 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 6059 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 6060 | gdbstub_dev); |
| 6061 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6062 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6063 | |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 6064 | if (loadvm) { |
| 6065 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 6066 | autostart = 0; |
| 6067 | } |
| 6068 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6069 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 6070 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 6071 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6072 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 6073 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6074 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 6075 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6076 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6077 | if (daemonize) { |
| 6078 | uint8_t status = 0; |
| 6079 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6080 | |
| 6081 | again1: |
| 6082 | len = write(fds[1], &status, 1); |
| 6083 | if (len == -1 && (errno == EINTR)) |
| 6084 | goto again1; |
| 6085 | |
| 6086 | if (len != 1) |
| 6087 | exit(1); |
| 6088 | |
aliguori | bd54b86 | 2008-07-23 00:58:33 +0000 | [diff] [blame] | 6089 | chdir("/"); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 6090 | TFR(fd = open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6091 | if (fd == -1) |
| 6092 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6093 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6094 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6095 | if (run_as) { |
| 6096 | pwd = getpwnam(run_as); |
| 6097 | if (!pwd) { |
| 6098 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 6099 | exit(1); |
| 6100 | } |
| 6101 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6102 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6103 | if (chroot_dir) { |
| 6104 | if (chroot(chroot_dir) < 0) { |
| 6105 | fprintf(stderr, "chroot failed\n"); |
| 6106 | exit(1); |
| 6107 | } |
| 6108 | chdir("/"); |
| 6109 | } |
| 6110 | |
| 6111 | if (run_as) { |
| 6112 | if (setgid(pwd->pw_gid) < 0) { |
| 6113 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 6114 | exit(1); |
| 6115 | } |
| 6116 | if (setuid(pwd->pw_uid) < 0) { |
| 6117 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 6118 | exit(1); |
| 6119 | } |
| 6120 | if (setuid(0) != -1) { |
| 6121 | fprintf(stderr, "Dropping privileges failed\n"); |
| 6122 | exit(1); |
| 6123 | } |
| 6124 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6125 | |
| 6126 | if (daemonize) { |
| 6127 | dup2(fd, 0); |
| 6128 | dup2(fd, 1); |
| 6129 | dup2(fd, 2); |
| 6130 | |
| 6131 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6132 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6133 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6134 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6135 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 6136 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 6137 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 6138 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6139 | return 0; |
| 6140 | } |